3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Jürgen Spitzmüller
8 * Full author contact details are available in file CREDITS.
13 #include "LaTeXFonts.h"
15 #include "LaTeXFeatures.h"
18 #include "support/convert.h"
19 #include "support/debug.h"
20 #include "support/FileName.h"
21 #include "support/filetools.h"
22 #include "support/lstrings.h"
26 using namespace lyx::support;
31 LaTeXFonts latexfonts;
34 bool LaTeXFont::available(bool ot1) const
36 return ot1 ? available_ot1_ : available_;
40 bool LaTeXFont::providesOSF(bool ot1) const
42 if (!osfpackage_.empty())
43 return LaTeXFeatures::isAvailable(to_ascii(osfpackage_));
45 if (ot1 && !ot1package_.empty() && ot1package_ != "none")
48 if (!package_.empty() && !LaTeXFeatures::isAvailable(to_ascii(package_)))
51 return (!osfoption_.empty() || !osfscoption_.empty());
55 bool LaTeXFont::providesSC(bool ot1) const
57 if (ot1 && !ot1package_.empty() && ot1package_ != "none")
60 if (!package_.empty() && !LaTeXFeatures::isAvailable(to_ascii(package_)))
63 return (!scoption_.empty() || !osfscoption_.empty());
67 bool LaTeXFont::providesScale(bool ot1) const
69 if (ot1 && !ot1package_.empty() && ot1package_ != "none")
72 if (!package_.empty() && !LaTeXFeatures::isAvailable(to_ascii(package_)))
75 return (!scaleoption_.empty());
79 string const LaTeXFont::getAvailablePackage(bool ot1, bool complete)
81 if (ot1 && !ot1package_.empty()) {
82 if (ot1package_ != "none" && LaTeXFeatures::isAvailable(to_ascii(ot1package_)))
83 return to_ascii(ot1package_);
86 if (complete && !completepackage_.empty()) {
87 if (LaTeXFeatures::isAvailable(to_ascii(completepackage_)))
88 return to_ascii(completepackage_);
90 if (!package_.empty()) {
91 if (!requires_.empty() && LaTeXFeatures::isAvailable(to_ascii(requires_)))
92 return to_ascii(package_);
93 if (LaTeXFeatures::isAvailable(to_ascii(package_)))
94 return to_ascii(package_);
95 else if (!altpackages_.empty()) {
96 for (size_t i = 0; i < altpackages_.size(); ++i) {
97 if (LaTeXFeatures::isAvailable(to_ascii(altpackages_[i])))
98 return to_ascii(altpackages_[i]);
106 string const LaTeXFont::getPackageOptions(bool const & ot1, bool const & sc,
107 bool const & osf, int const & scale)
109 if (ot1 && !ot1package_.empty())
113 if (sc && osf && providesOSF() && providesSC()) {
114 if (!osfscoption_.empty())
115 os << to_ascii(osfscoption_);
117 os << to_ascii(osfoption_) << ',' << to_ascii(scoption_);
118 } else if (osf && providesOSF())
119 os << to_ascii(osfoption_);
120 else if (sc && providesSC())
121 os << to_ascii(scoption_);
122 if (scale != 100 && !scaleoption_.empty()) {
123 if (!os.str().empty())
125 os << subst(to_ascii(scaleoption_), "$$val",
126 convert<std::string>(float(scale) / 100));
132 bool LaTeXFont::readFont(Lexer & lex)
151 // Keep these sorted alphabetically!
152 LexerKeyword latexFontTags[] = {
153 { "altpackages", LF_ALT_PACKAGES },
154 { "completepackage", LF_COMPLETE_PACKAGE },
155 { "endfont", LF_END },
156 { "family", LF_FAMILY },
157 { "guiname", LF_GUINAME },
158 { "osfoption", LF_OSFOPTION },
159 { "osfpackage", LF_OSFPACKAGE },
160 { "osfscoption", LF_OSFSCOPTION },
161 { "ot1package", LF_OT1_PACKAGE },
162 { "package", LF_PACKAGE },
163 { "requires", LF_REQUIRES },
164 { "scaleoption", LF_SCALEOPTION },
165 { "scoption", LF_SCOPTION },
166 { "switchdefault", LF_SWITCHDEFAULT }
170 bool finished = false;
171 lex.pushTable(latexFontTags);
172 // parse style section
173 while (!finished && lex.isOK() && !error) {
175 // See comment in LyXRC.cpp.
177 case Lexer::LEX_FEOF:
180 case Lexer::LEX_UNDEF: // parse error
181 lex.printError("Unknown LaTeXFont tag `$$Token'");
188 switch (static_cast<LaTeXFontTags>(le)) {
189 case LF_END: // end of structure
192 case LF_ALT_PACKAGES: {
195 altpackages_ = getVectorFromString(altp);
198 case LF_COMPLETE_PACKAGE:
199 lex >> completepackage_;
231 case LF_SWITCHDEFAULT:
232 lex >> switchdefault_;
237 lex.printError("No End tag found for LaTeXFont tag `$$Token'");
241 return finished && !error;
245 bool LaTeXFont::read(Lexer & lex)
250 lex.printError("No name given for LaTeX font: `$$Token'.");
254 name_ = lex.getDocString();
255 LYXERR(Debug::INFO, "Reading LaTeX font " << name_);
256 if (!readFont(lex)) {
257 LYXERR0("Error parsing LaTeX font `" << name_ << '\'');
261 bool available = true;
262 if (!requires_.empty())
263 available = LaTeXFeatures::isAvailable(to_ascii(requires_));
264 else if (!package_.empty()) {
265 available = LaTeXFeatures::isAvailable(to_ascii(package_));
266 if (!available && !altpackages_.empty()) {
267 for (size_t i = 0; i < altpackages_.size(); ++i) {
268 available = LaTeXFeatures::isAvailable(to_ascii(altpackages_[i]));
274 available_ = available;
276 if (!ot1package_.empty() && ot1package_ != "none")
277 available_ot1_ = LaTeXFeatures::isAvailable(to_ascii(ot1package_));
279 available_ot1_ = available;
285 void LaTeXFonts::readLaTeXFonts()
287 // Read latexfonts file
288 FileName filename = libFileSearch(string(), "latexfonts");
289 if (filename.empty()) {
290 LYXERR0("Error: latexfonts file not found!");
294 lex.setFile(filename);
295 lex.setContext("LaTeXFeatures::readLaTeXFonts");
299 case Lexer::LEX_FEOF:
305 if (lex.getString() != "Font") {
306 lex.printError("Unknown LaTeXFont tag `$$Token'");
314 texfontmap_[f.name()] = f;
319 LaTeXFonts::TexFontMap LaTeXFonts::getLaTeXFonts()
321 if (texfontmap_.empty())
327 LaTeXFont LaTeXFonts::getLaTeXFont(docstring const & name)
329 if (texfontmap_.empty())
331 return texfontmap_[name];