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 LaTeXFont LaTeXFont::altFont(docstring const & name)
39 return theLaTeXFonts().getAltFont(name);
43 bool LaTeXFont::available(bool ot1, bool nomath)
45 if (nomath && !nomathfont_.empty())
46 return altFont(nomathfont_).available(ot1, nomath);
47 else if (ot1 && !ot1font_.empty())
48 return (ot1font_ == "none") ?
49 true : altFont(ot1font_).available(ot1, nomath);
50 else if (requires_.empty() && package_.empty())
52 else if (!requires_.empty()
53 && LaTeXFeatures::isAvailable(to_ascii(requires_)))
55 else if (requires_.empty() && !package_.empty()
56 && LaTeXFeatures::isAvailable(to_ascii(package_)))
58 else if (!altfonts_.empty()) {
59 for (size_t i = 0; i < altfonts_.size(); ++i) {
60 if (altFont(altfonts_[i]).available(ot1, nomath))
68 bool LaTeXFont::providesNoMath(bool ot1, bool complete)
70 docstring const usedfont = getUsedFont(ot1, complete, false);
74 else if (usedfont != name_)
75 return altFont(usedfont).providesNoMath(ot1, complete);
77 return (!nomathfont_.empty() && available(ot1, true));
81 bool LaTeXFont::providesOSF(bool ot1, bool complete, bool nomath)
83 docstring const usedfont = getUsedFont(ot1, complete, nomath);
87 else if (usedfont != name_)
88 return altFont(usedfont).providesOSF(ot1, complete, nomath);
89 else if (!osffont_.empty())
90 return altFont(osffont_).available(ot1, nomath);
91 else if (!available(ot1, nomath))
94 return (!osfoption_.empty() || !osfscoption_.empty());
98 bool LaTeXFont::providesSC(bool ot1, bool complete, bool nomath)
100 docstring const usedfont = getUsedFont(ot1, complete, nomath);
102 if (usedfont.empty())
104 else if (usedfont != name_)
105 return altFont(usedfont).providesSC(ot1, complete, nomath);
106 else if (!available(ot1, nomath))
109 return (!scoption_.empty() || !osfscoption_.empty());
113 bool LaTeXFont::hasMonolithicExpertSet(bool ot1, bool complete, bool nomath)
115 docstring const usedfont = getUsedFont(ot1, complete, nomath);
117 if (usedfont.empty())
119 else if (usedfont != name_)
120 return altFont(usedfont).hasMonolithicExpertSet(ot1, complete, nomath);
121 return (!osfoption_.empty() && !scoption_.empty() && osfoption_ == scoption_)
122 || (osfoption_.empty() && scoption_.empty() && !osfscoption_.empty());
126 bool LaTeXFont::providesScale(bool ot1, bool complete, bool nomath)
128 docstring const usedfont = getUsedFont(ot1, complete, nomath);
130 if (usedfont.empty())
132 else if (usedfont != name_)
133 return altFont(usedfont).providesScale(ot1, complete, nomath);
134 else if (!available(ot1, nomath))
136 return (!scaleoption_.empty());
139 bool LaTeXFont::provides(std::string const & name, bool ot1, bool complete, bool nomath)
141 docstring const usedfont = getUsedFont(ot1, complete, nomath);
143 if (usedfont.empty())
145 else if (usedfont != name_)
146 return altFont(usedfont).provides(name, ot1, complete, nomath);
147 else if (provides_.empty())
150 for (size_t i = 0; i < provides_.size(); ++i) {
151 if (provides_[i] == name)
158 docstring const LaTeXFont::getUsedFont(bool ot1, bool complete, bool nomath)
160 if (nomath && !nomathfont_.empty() && available(ot1, true))
162 else if (ot1 && !ot1font_.empty())
163 return (ot1font_ == "none") ? docstring() : ot1font_;
164 else if (family_ == "rm" && complete && !completefont_.empty()
165 && altFont(completefont_).available(ot1, nomath))
166 return completefont_;
167 else if (switchdefault_) {
168 if (requires_.empty()
169 || (!requires_.empty()
170 && LaTeXFeatures::isAvailable(to_ascii(requires_))))
173 else if (!requires_.empty()
174 && LaTeXFeatures::isAvailable(to_ascii(requires_)))
176 else if (!package_.empty()
177 && LaTeXFeatures::isAvailable(to_ascii(package_)))
179 else if (!preamble_.empty() && package_.empty()
180 && requires_.empty() && !switchdefault_
181 && altfonts_.empty()) {
184 else if (!altfonts_.empty()) {
185 for (size_t i = 0; i < altfonts_.size(); ++i) {
186 LaTeXFont altf = altFont(altfonts_[i]);
187 if (altf.available(ot1, nomath))
188 return altf.getUsedFont(ot1, complete, nomath);
196 string const LaTeXFont::getAvailablePackage(bool dryrun)
198 if (package_.empty())
201 string const package = to_ascii(package_);
202 if (!requires_.empty() && LaTeXFeatures::isAvailable(to_ascii(requires_)))
204 else if (LaTeXFeatures::isAvailable(package))
206 // Output unavailable packages in source preview
210 docstring const req = requires_.empty() ? package_ : requires_;
211 frontend::Alert::warning(_("Font not available"),
212 bformat(_("The LaTeX package `%1$s' needed for the font `%2$s'\n"
213 "is not available on your system. LyX will fall back to the default font."),
214 req, guiname_), true);
220 string const LaTeXFont::getPackageOptions(bool ot1, bool complete, bool sc, bool osf,
221 int scale, bool nomath)
224 bool const needosfopt = (osf != osfdefault_);
225 bool const has_osf = providesOSF(ot1, complete, nomath);
226 bool const has_sc = providesSC(ot1, complete, nomath);
228 if (!packageoption_.empty())
229 os << to_ascii(packageoption_);
231 if (sc && needosfopt && has_osf && has_sc) {
232 if (!os.str().empty())
234 if (!osfscoption_.empty())
235 os << to_ascii(osfscoption_);
237 os << to_ascii(osfoption_)
238 << ',' << to_ascii(scoption_);
239 } else if (needosfopt && has_osf) {
240 if (!os.str().empty())
242 os << to_ascii(osfoption_);
243 } else if (sc && has_sc) {
244 if (!os.str().empty())
246 os << to_ascii(scoption_);
249 if (scale != 100 && !scaleoption_.empty()
250 && providesScale(ot1, complete, nomath)) {
251 if (!os.str().empty())
253 os << subst(to_ascii(scaleoption_), "$$val",
254 convert<std::string>(float(scale) / 100));
260 string const LaTeXFont::getLaTeXCode(bool dryrun, bool ot1, bool complete, bool sc,
261 bool osf, bool nomath, int const & scale)
265 docstring const usedfont = getUsedFont(ot1, complete, nomath);
266 if (usedfont.empty())
268 else if (usedfont != name_)
269 return altFont(usedfont).getLaTeXCode(dryrun, ot1, complete, sc, osf, nomath, scale);
271 if (switchdefault_) {
272 if (family_.empty()) {
273 LYXERR0("Error: Font `" << name_ << "' has no family defined!");
276 if (available(ot1, nomath) || dryrun)
277 os << "\\renewcommand{\\" << to_ascii(family_) << "default}{"
278 << to_ascii(name_) << "}\n";
280 frontend::Alert::warning(_("Font not available"),
281 bformat(_("The LaTeX package `%1$s' needed for the font `%2$s'\n"
282 "is not available on your system. LyX will fall back to the default font."),
283 requires_, guiname_), true);
285 string const package =
286 getAvailablePackage(dryrun);
287 string const packageopts = getPackageOptions(ot1, complete, sc, osf, scale, nomath);
288 if (packageopts.empty() && !package.empty())
289 os << "\\usepackage{" << package << "}\n";
290 else if (!packageopts.empty() && !package.empty())
291 os << "\\usepackage[" << packageopts << "]{" << package << "}\n";
293 if (osf && providesOSF(ot1, complete, nomath) && !osffont_.empty())
294 os << altFont(osffont_).getLaTeXCode(dryrun, ot1, complete, sc, osf, nomath, scale);
296 if (!preamble_.empty())
303 bool LaTeXFont::readFont(Lexer & lex)
327 // Keep these sorted alphabetically!
328 LexerKeyword latexFontTags[] = {
329 { "altfonts", LF_ALT_FONTS },
330 { "completefont", LF_COMPLETE_FONT },
331 { "endfont", LF_END },
332 { "family", LF_FAMILY },
333 { "guiname", LF_GUINAME },
334 { "nomathfont", LF_NOMATHFONT },
335 { "osfdefault", LF_OSFDEFAULT },
336 { "osffont", LF_OSFFONT },
337 { "osfoption", LF_OSFOPTION },
338 { "osfscoption", LF_OSFSCOPTION },
339 { "ot1font", LF_OT1_FONT },
340 { "package", LF_PACKAGE },
341 { "packageoption", LF_PACKAGEOPTION },
342 { "preamble", LF_PREAMBLE },
343 { "provides", LF_PROVIDES },
344 { "requires", LF_REQUIRES },
345 { "scaleoption", LF_SCALEOPTION },
346 { "scoption", LF_SCOPTION },
347 { "switchdefault", LF_SWITCHDEFAULT }
351 bool finished = false;
352 lex.pushTable(latexFontTags);
353 // parse style section
354 while (!finished && lex.isOK() && !error) {
356 // See comment in LyXRC.cpp.
358 case Lexer::LEX_FEOF:
361 case Lexer::LEX_UNDEF: // parse error
362 lex.printError("Unknown LaTeXFont tag `$$Token'");
369 switch (static_cast<LaTeXFontTags>(le)) {
370 case LF_END: // end of structure
375 docstring altp = lex.getDocString();
376 altfonts_ = getVectorFromString(altp);
379 case LF_COMPLETE_FONT:
380 lex >> completefont_;
409 case LF_PACKAGEOPTION:
410 lex >> packageoption_;
413 preamble_ = lex.getLongString("EndPreamble");
417 string features = lex.getString();
418 provides_ = getVectorFromString(features);
430 case LF_SWITCHDEFAULT:
431 lex >> switchdefault_;
436 lex.printError("No End tag found for LaTeXFont tag `$$Token'");
440 return finished && !error;
444 bool LaTeXFont::read(Lexer & lex)
450 lex.printError("No name given for LaTeX font: `$$Token'.");
454 name_ = lex.getDocString();
455 LYXERR(Debug::INFO, "Reading LaTeX font " << name_);
456 if (!readFont(lex)) {
457 LYXERR0("Error parsing LaTeX font `" << name_ << '\'');
465 void LaTeXFonts::readLaTeXFonts()
467 // Read latexfonts file
468 FileName filename = libFileSearch(string(), "latexfonts");
469 if (filename.empty()) {
470 LYXERR0("Error: latexfonts file not found!");
474 lex.setFile(filename);
475 lex.setContext("LaTeXFeatures::readLaTeXFonts");
479 case Lexer::LEX_FEOF:
485 string const type = lex.getString();
486 if (type != "Font" && type != "AltFont") {
487 lex.printError("Unknown LaTeXFont tag `$$Token'");
495 if (type == "AltFont")
496 texaltfontmap_[f.name()] = f;
498 texfontmap_[f.name()] = f;
503 LaTeXFonts::TexFontMap LaTeXFonts::getLaTeXFonts()
505 if (texfontmap_.empty())
511 LaTeXFont LaTeXFonts::getLaTeXFont(docstring const & name)
513 if (name == "default" || name == "auto")
515 if (texfontmap_.empty())
517 if (texfontmap_.find(name) == texfontmap_.end()) {
518 LYXERR0("LaTeXFonts::getLaTeXFont: font '" << name << "' not found!");
521 return texfontmap_[name];
525 LaTeXFont LaTeXFonts::getAltFont(docstring const & name)
527 if (name == "default" || name == "auto")
529 if (texaltfontmap_.empty())
531 if (texaltfontmap_.find(name) == texaltfontmap_.end()) {
532 LYXERR0("LaTeXFonts::getAltFont: alternative font '" << name << "' not found!");
535 return texaltfontmap_[name];