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 "frontends/alert.h"
20 #include "support/convert.h"
21 #include "support/debug.h"
22 #include "support/FileName.h"
23 #include "support/filetools.h"
24 #include "support/gettext.h"
25 #include "support/lstrings.h"
29 using namespace lyx::support;
34 LaTeXFonts latexfonts;
37 bool LaTeXFont::available(bool ot1) const
39 return ot1 ? available_ot1_ : available_;
43 bool LaTeXFont::providesOSF(bool ot1) const
45 if (!osfpackage_.empty())
46 return LaTeXFeatures::isAvailable(to_ascii(osfpackage_));
48 if (ot1 && !ot1package_.empty() && ot1package_ != "none")
51 if (!package_.empty() && !LaTeXFeatures::isAvailable(to_ascii(package_)))
54 return (!osfoption_.empty() || !osfscoption_.empty());
58 bool LaTeXFont::providesSC(bool ot1) const
60 if (ot1 && !ot1package_.empty() && ot1package_ != "none")
63 if (!package_.empty() && !LaTeXFeatures::isAvailable(to_ascii(package_)))
66 return (!scoption_.empty() || !osfscoption_.empty());
70 bool LaTeXFont::providesScale(bool ot1) const
72 if (ot1 && !ot1package_.empty() && ot1package_ != "none")
75 if (!package_.empty() && !LaTeXFeatures::isAvailable(to_ascii(package_)))
78 return (!scaleoption_.empty());
82 string const LaTeXFont::getAvailablePackage(bool dryrun, bool ot1, bool complete, bool & alt)
84 if (ot1 && !ot1package_.empty()) {
85 if (ot1package_ != "none"
86 && (LaTeXFeatures::isAvailable(to_ascii(ot1package_)) || dryrun))
87 return to_ascii(ot1package_);
88 if (!dryrun && ot1package_ != "none")
89 frontend::Alert::warning(_("Font not available"),
90 bformat(_("The LaTeX package `%1$s' needed for the font `%2$s'\n"
91 "is not available on your system. LyX will fall back to the default font."),
92 ot1package_, guiname_), true);
95 if (family_ == "rm" && complete && !completepackage_.empty()) {
96 if (LaTeXFeatures::isAvailable(to_ascii(completepackage_)) || dryrun)
97 return to_ascii(completepackage_);
99 if (!package_.empty()) {
100 if (!requires_.empty() && LaTeXFeatures::isAvailable(to_ascii(requires_)))
101 return to_ascii(package_);
102 if (LaTeXFeatures::isAvailable(to_ascii(package_)))
103 return to_ascii(package_);
104 else if (!altpackages_.empty()) {
105 for (size_t i = 0; i < altpackages_.size(); ++i) {
106 if (LaTeXFeatures::isAvailable(to_ascii(altpackages_[i]))) {
108 return to_ascii(altpackages_[i]);
112 // Output unavailable packages in source preview
114 return to_ascii(package_);
115 docstring const req = requires_.empty() ? package_ : requires_;
116 frontend::Alert::warning(_("Font not available"),
117 bformat(_("The LaTeX package `%1$s' needed for the font `%2$s'\n"
118 "is not available on your system. LyX will fall back to the default font."),
119 req, guiname_), true);
125 string const LaTeXFont::getPackageOptions(bool ot1, bool sc, bool osf, int scale)
127 if (ot1 && !ot1package_.empty())
131 if (!packageoption_.empty())
132 os << to_ascii(packageoption_);
133 if (sc && osf && providesOSF() && providesSC()) {
134 if (!os.str().empty())
136 if (!osfscoption_.empty())
137 os << to_ascii(osfscoption_);
139 os << to_ascii(osfoption_) << ',' << to_ascii(scoption_);
140 } else if (osf && providesOSF()) {
141 if (!os.str().empty())
143 os << to_ascii(osfoption_);
144 } else if (sc && providesSC()) {
145 if (!os.str().empty())
147 os << to_ascii(scoption_);
149 if (scale != 100 && !scaleoption_.empty()) {
150 if (!os.str().empty())
152 os << subst(to_ascii(scaleoption_), "$$val",
153 convert<std::string>(float(scale) / 100));
159 string const LaTeXFont::getLaTeXCode(bool dryrun, bool ot1, bool complete, bool sc,
160 bool osf, int const & scale)
164 if (switchdefault_) {
165 if (family_.empty()) {
166 LYXERR0("Error: Font `" << name_ << "' has no family defined!");
169 if (available(ot1) || dryrun)
170 os << "\\renewcommand{\\" << to_ascii(family_) << "default}{"
171 << to_ascii(name_) << "}\n";
173 frontend::Alert::warning(_("Font not available"),
174 bformat(_("The LaTeX package `%1$s' needed for the font `%2$s'\n"
175 "is not available on your system. LyX will fall back to the default font."),
176 requires_, guiname_), true);
179 string const package =
180 getAvailablePackage(dryrun, ot1, complete, alt);
181 // Package options are not for alternative packages
182 string const packageopts = alt ? string() : getPackageOptions(ot1, sc, osf, scale);
183 if (packageopts.empty() && !package.empty())
184 os << "\\usepackage{" << package << "}\n";
185 else if (!packageopts.empty() && !package.empty())
186 os << "\\usepackage[" << packageopts << "]{" << package << "}\n";
188 if (osf && providesOSF(ot1) && !osfpackage_.empty())
189 os << "\\usepackage{" << to_ascii(osfpackage_) << "}\n";
195 bool LaTeXFont::readFont(Lexer & lex)
215 // Keep these sorted alphabetically!
216 LexerKeyword latexFontTags[] = {
217 { "altpackages", LF_ALT_PACKAGES },
218 { "completepackage", LF_COMPLETE_PACKAGE },
219 { "endfont", LF_END },
220 { "family", LF_FAMILY },
221 { "guiname", LF_GUINAME },
222 { "osfoption", LF_OSFOPTION },
223 { "osfpackage", LF_OSFPACKAGE },
224 { "osfscoption", LF_OSFSCOPTION },
225 { "ot1package", LF_OT1_PACKAGE },
226 { "package", LF_PACKAGE },
227 { "packageoption", LF_PACKAGEOPTION },
228 { "requires", LF_REQUIRES },
229 { "scaleoption", LF_SCALEOPTION },
230 { "scoption", LF_SCOPTION },
231 { "switchdefault", LF_SWITCHDEFAULT }
235 bool finished = false;
236 lex.pushTable(latexFontTags);
237 // parse style section
238 while (!finished && lex.isOK() && !error) {
240 // See comment in LyXRC.cpp.
242 case Lexer::LEX_FEOF:
245 case Lexer::LEX_UNDEF: // parse error
246 lex.printError("Unknown LaTeXFont tag `$$Token'");
253 switch (static_cast<LaTeXFontTags>(le)) {
254 case LF_END: // end of structure
257 case LF_ALT_PACKAGES: {
260 altpackages_ = getVectorFromString(altp);
263 case LF_COMPLETE_PACKAGE:
264 lex >> completepackage_;
287 case LF_PACKAGEOPTION:
288 lex >> packageoption_;
299 case LF_SWITCHDEFAULT:
300 lex >> switchdefault_;
305 lex.printError("No End tag found for LaTeXFont tag `$$Token'");
309 return finished && !error;
313 bool LaTeXFont::read(Lexer & lex)
318 lex.printError("No name given for LaTeX font: `$$Token'.");
322 name_ = lex.getDocString();
323 LYXERR(Debug::INFO, "Reading LaTeX font " << name_);
324 if (!readFont(lex)) {
325 LYXERR0("Error parsing LaTeX font `" << name_ << '\'');
329 bool available = true;
330 if (!requires_.empty())
331 available = LaTeXFeatures::isAvailable(to_ascii(requires_));
332 else if (!package_.empty()) {
333 available = LaTeXFeatures::isAvailable(to_ascii(package_));
334 if (!available && !altpackages_.empty()) {
335 for (size_t i = 0; i < altpackages_.size(); ++i) {
336 available = LaTeXFeatures::isAvailable(to_ascii(altpackages_[i]));
342 available_ = available;
344 if (!ot1package_.empty() && ot1package_ != "none")
345 available_ot1_ = LaTeXFeatures::isAvailable(to_ascii(ot1package_));
347 available_ot1_ = available;
353 void LaTeXFonts::readLaTeXFonts()
355 // Read latexfonts file
356 FileName filename = libFileSearch(string(), "latexfonts");
357 if (filename.empty()) {
358 LYXERR0("Error: latexfonts file not found!");
362 lex.setFile(filename);
363 lex.setContext("LaTeXFeatures::readLaTeXFonts");
367 case Lexer::LEX_FEOF:
373 if (lex.getString() != "Font") {
374 lex.printError("Unknown LaTeXFont tag `$$Token'");
382 texfontmap_[f.name()] = f;
387 LaTeXFonts::TexFontMap LaTeXFonts::getLaTeXFonts()
389 if (texfontmap_.empty())
395 LaTeXFont LaTeXFonts::getLaTeXFont(docstring const & name)
397 if (name == "default")
399 if (texfontmap_.empty())
401 if (texfontmap_.find(name) == texfontmap_.end()) {
402 LYXERR0("LaTeXFonts::getLaTeXFont: font '" << name << "' not found!");
405 return texfontmap_[name];