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/docstream.h"
23 #include "support/FileName.h"
24 #include "support/filetools.h"
25 #include "support/gettext.h"
26 #include "support/lstrings.h"
30 using namespace lyx::support;
35 LaTeXFonts latexfonts;
38 LaTeXFont LaTeXFont::altFont(docstring const & name)
40 return theLaTeXFonts().getAltFont(name);
44 bool LaTeXFont::available(bool ot1, bool nomath)
46 if (nomath && !nomathfont_.empty())
47 return altFont(nomathfont_).available(ot1, nomath);
48 else if (ot1 && !ot1font_.empty())
49 return (ot1font_ == "none") ?
50 true : altFont(ot1font_).available(ot1, nomath);
51 else if (required_.empty() && package_.empty())
53 else if (!required_.empty()
54 && LaTeXFeatures::isAvailable(to_ascii(required_)))
56 else if (required_.empty() && !package_.empty()
57 && LaTeXFeatures::isAvailable(to_ascii(package_)))
59 else if (!altfonts_.empty()) {
60 for (size_t i = 0; i < altfonts_.size(); ++i) {
61 if (altFont(altfonts_[i]).available(ot1, nomath))
69 bool LaTeXFont::providesNoMath(bool ot1, bool complete)
71 docstring const usedfont = getUsedFont(ot1, complete, false, false);
75 else if (usedfont != name_)
76 return altFont(usedfont).providesNoMath(ot1, complete);
78 return (!nomathfont_.empty() && available(ot1, true));
82 bool LaTeXFont::providesOSF(bool ot1, bool complete, bool nomath)
84 docstring const usedfont = getUsedFont(ot1, complete, nomath, false);
88 else if (usedfont != name_)
89 return altFont(usedfont).providesOSF(ot1, complete, nomath);
90 else if (!osffont_.empty())
91 return altFont(osffont_).available(ot1, nomath);
92 else if (!available(ot1, nomath))
95 return (!osfoption_.empty() || !osfscoption_.empty());
99 bool LaTeXFont::providesSC(bool ot1, bool complete, bool nomath)
101 docstring const usedfont = getUsedFont(ot1, complete, nomath, false);
103 if (usedfont.empty())
105 else if (usedfont != name_)
106 return altFont(usedfont).providesSC(ot1, complete, nomath);
107 else if (!available(ot1, nomath))
110 return (!scoption_.empty() || !osfscoption_.empty());
114 bool LaTeXFont::hasMonolithicExpertSet(bool ot1, bool complete, bool nomath)
116 docstring const usedfont = getUsedFont(ot1, complete, nomath, false);
118 if (usedfont.empty())
120 else if (usedfont != name_)
121 return altFont(usedfont).hasMonolithicExpertSet(ot1, complete, nomath);
122 return (!osfoption_.empty() && !scoption_.empty() && osfoption_ == scoption_)
123 || (osfoption_.empty() && scoption_.empty() && !osfscoption_.empty());
127 bool LaTeXFont::providesScale(bool ot1, bool complete, bool nomath)
129 docstring const usedfont = getUsedFont(ot1, complete, nomath, false);
131 if (usedfont.empty())
133 else if (usedfont != name_)
134 return altFont(usedfont).providesScale(ot1, complete, nomath);
135 else if (!available(ot1, nomath))
137 return (!scaleoption_.empty());
141 bool LaTeXFont::providesMoreOptions(bool ot1, bool complete, bool nomath)
143 docstring const usedfont = getUsedFont(ot1, complete, nomath, false);
145 if (usedfont.empty())
147 else if (usedfont != name_)
148 return altFont(usedfont).providesMoreOptions(ot1, complete, nomath);
149 else if (!available(ot1, nomath))
155 bool LaTeXFont::provides(std::string const & name, bool ot1, bool complete, bool nomath)
157 docstring const usedfont = getUsedFont(ot1, complete, nomath, false);
159 if (usedfont.empty())
161 else if (usedfont != name_)
162 return altFont(usedfont).provides(name, ot1, complete, nomath);
163 else if (provides_.empty())
166 for (size_t i = 0; i < provides_.size(); ++i) {
167 if (provides_[i] == name)
174 docstring const LaTeXFont::getUsedFont(bool ot1, bool complete, bool nomath, bool osf)
176 if (osf && osfFontOnly())
178 else if (nomath && !nomathfont_.empty() && available(ot1, true))
180 else if (ot1 && !ot1font_.empty())
181 return (ot1font_ == "none") ? docstring() : ot1font_;
182 else if (family_ == "rm" && complete && !completefont_.empty()
183 && altFont(completefont_).available(ot1, nomath))
184 return completefont_;
185 else if (switchdefault_) {
186 if (required_.empty()
187 || (!required_.empty()
188 && LaTeXFeatures::isAvailable(to_ascii(required_))))
191 else if (!required_.empty()
192 && LaTeXFeatures::isAvailable(to_ascii(required_)))
194 else if (!package_.empty()
195 && LaTeXFeatures::isAvailable(to_ascii(package_)))
197 else if (!preamble_.empty() && package_.empty()
198 && required_.empty() && !switchdefault_
199 && altfonts_.empty()) {
202 else if (!altfonts_.empty()) {
203 for (size_t i = 0; i < altfonts_.size(); ++i) {
204 LaTeXFont altf = altFont(altfonts_[i]);
205 if (altf.available(ot1, nomath))
206 return altf.getUsedFont(ot1, complete, nomath, osf);
214 docstring const LaTeXFont::getUsedPackage(bool ot1, bool complete, bool nomath)
216 docstring const usedfont = getUsedFont(ot1, complete, nomath, false);
217 if (usedfont.empty())
219 return theLaTeXFonts().getLaTeXFont(usedfont).package();
223 string const LaTeXFont::getAvailablePackage(bool dryrun)
225 if (package_.empty())
228 string const package = to_ascii(package_);
229 if (!required_.empty() && LaTeXFeatures::isAvailable(to_ascii(required_)))
231 else if (LaTeXFeatures::isAvailable(package))
233 // Output unavailable packages in source preview
237 docstring const req = required_.empty() ? package_ : required_;
238 frontend::Alert::warning(_("Font not available"),
239 bformat(_("The LaTeX package `%1$s' needed for the font `%2$s'\n"
240 "is not available on your system. LyX will fall back to the default font."),
241 req, guiname_), true);
247 string const LaTeXFont::getPackageOptions(bool ot1, bool complete, bool sc, bool osf,
248 int scale, string const & extraopts, bool nomath)
251 bool const needosfopt = (osf != osfdefault_);
252 bool const has_osf = providesOSF(ot1, complete, nomath);
253 bool const has_sc = providesSC(ot1, complete, nomath);
254 bool const moreopts = providesMoreOptions(ot1, complete, nomath);
256 if (!packageoptions_.empty())
257 os << to_ascii(packageoptions_);
259 if (sc && needosfopt && has_osf && has_sc) {
260 if (!os.str().empty())
262 if (!osfscoption_.empty())
263 os << to_ascii(osfscoption_);
265 os << to_ascii(osfoption_)
266 << ',' << to_ascii(scoption_);
267 } else if (needosfopt && has_osf) {
268 if (!os.str().empty())
270 os << to_ascii(osfoption_);
271 } else if (sc && has_sc) {
272 if (!os.str().empty())
274 os << to_ascii(scoption_);
277 if (scale != 100 && !scaleoption_.empty()
278 && providesScale(ot1, complete, nomath)) {
279 if (!os.str().empty())
281 os << subst(to_ascii(scaleoption_), "$$val",
282 convert<std::string>(float(scale) / 100));
285 if (moreopts && !extraopts.empty()) {
286 if (!os.str().empty())
294 string const LaTeXFont::getLaTeXCode(bool dryrun, bool ot1, bool complete, bool sc,
295 bool osf, bool nomath, string const & extraopts,
300 docstring const usedfont = getUsedFont(ot1, complete, nomath, osf);
301 if (usedfont.empty())
303 else if (usedfont != name_)
304 return altFont(usedfont).getLaTeXCode(dryrun, ot1, complete, sc,
305 osf, nomath, extraopts, scale);
307 if (switchdefault_) {
308 if (family_.empty()) {
309 LYXERR0("Error: Font `" << name_ << "' has no family defined!");
312 if (available(ot1, nomath) || dryrun)
313 os << "\\renewcommand{\\" << to_ascii(family_) << "default}{"
314 << to_ascii(name_) << "}\n";
316 frontend::Alert::warning(_("Font not available"),
317 bformat(_("The LaTeX package `%1$s' needed for the font `%2$s'\n"
318 "is not available on your system. LyX will fall back to the default font."),
319 required_, guiname_), true);
321 string const package =
322 getAvailablePackage(dryrun);
323 string const packageopts = getPackageOptions(ot1, complete, sc, osf, scale, extraopts, nomath);
324 if (packageopts.empty() && !package.empty())
325 os << "\\usepackage{" << package << "}\n";
326 else if (!packageopts.empty() && !package.empty())
327 os << "\\usepackage[" << packageopts << "]{" << package << "}\n";
329 if (osf && providesOSF(ot1, complete, nomath) && !osffont_.empty())
330 os << altFont(osffont_).getLaTeXCode(dryrun, ot1, complete, sc, osf,
331 nomath, extraopts, scale);
333 if (!preamble_.empty())
334 os << to_utf8(preamble_);
340 bool LaTeXFont::hasFontenc(string const & name) const
342 for (auto const & fe : fontenc_) {
350 bool LaTeXFont::readFont(Lexer & lex)
377 // Keep these sorted alphabetically!
378 LexerKeyword latexFontTags[] = {
379 { "altfonts", LF_ALT_FONTS },
380 { "completefont", LF_COMPLETE_FONT },
381 { "endfont", LF_END },
382 { "family", LF_FAMILY },
383 { "fontencoding", LF_FONTENC },
384 { "guiname", LF_GUINAME },
385 { "moreoptions", LF_MOREOPTS },
386 { "nomathfont", LF_NOMATHFONT },
387 { "osfdefault", LF_OSFDEFAULT },
388 { "osffont", LF_OSFFONT },
389 { "osffontonly", LF_OSFFONTONLY },
390 { "osfoption", LF_OSFOPTION },
391 { "osfscoption", LF_OSFSCOPTION },
392 { "ot1font", LF_OT1_FONT },
393 { "package", LF_PACKAGE },
394 { "packageoptions", LF_PACKAGEOPTIONS },
395 { "preamble", LF_PREAMBLE },
396 { "provides", LF_PROVIDES },
397 { "requires", LF_REQUIRES },
398 { "scaleoption", LF_SCALEOPTION },
399 { "scoption", LF_SCOPTION },
400 { "switchdefault", LF_SWITCHDEFAULT }
404 bool finished = false;
405 lex.pushTable(latexFontTags);
406 // parse style section
407 while (!finished && lex.isOK() && !error) {
409 // See comment in LyXRC.cpp.
411 case Lexer::LEX_FEOF:
414 case Lexer::LEX_UNDEF: // parse error
415 lex.printError("Unknown LaTeXFont tag `$$Token'");
422 switch (static_cast<LaTeXFontTags>(le)) {
423 case LF_END: // end of structure
428 docstring altp = lex.getDocString();
429 altfonts_ = getVectorFromString(altp);
432 case LF_COMPLETE_FONT:
433 lex >> completefont_;
443 string fe = lex.getString();
444 fontenc_ = getVectorFromString(fe);
471 case LF_PACKAGEOPTIONS:
472 lex >> packageoptions_;
475 preamble_ = lex.getLongString(from_ascii("EndPreamble"));
479 string features = lex.getString();
480 provides_ = getVectorFromString(features);
495 case LF_SWITCHDEFAULT:
496 lex >> switchdefault_;
501 lex.printError("No End tag found for LaTeXFont tag `$$Token'");
505 return finished && !error;
509 bool LaTeXFont::read(Lexer & lex)
517 lex.printError("No name given for LaTeX font: `$$Token'.");
521 name_ = lex.getDocString();
522 LYXERR(Debug::INFO, "Reading LaTeX font " << name_);
523 if (!readFont(lex)) {
524 LYXERR0("Error parsing LaTeX font `" << name_ << '\'');
528 if (fontenc_.empty())
529 fontenc_.push_back("T1");
535 void LaTeXFonts::readLaTeXFonts()
537 // Read latexfonts file
538 FileName filename = libFileSearch(string(), "latexfonts");
539 if (filename.empty()) {
540 LYXERR0("Error: latexfonts file not found!");
544 lex.setFile(filename);
545 lex.setContext("LaTeXFeatures::readLaTeXFonts");
549 case Lexer::LEX_FEOF:
555 string const type = lex.getString();
556 if (type != "Font" && type != "AltFont") {
557 lex.printError("Unknown LaTeXFont tag `$$Token'");
565 if (type == "AltFont")
566 texaltfontmap_[f.name()] = f;
568 texfontmap_[f.name()] = f;
573 LaTeXFonts::TexFontMap LaTeXFonts::getLaTeXFonts()
575 if (texfontmap_.empty())
581 LaTeXFont LaTeXFonts::getLaTeXFont(docstring const & name)
583 if (name == "default" || name == "auto")
585 if (texfontmap_.empty())
587 if (texfontmap_.find(name) == texfontmap_.end()) {
588 LYXERR0("LaTeXFonts::getLaTeXFont: font '" << name << "' not found!");
591 return texfontmap_[name];
595 LaTeXFont LaTeXFonts::getAltFont(docstring const & name)
597 if (name == "default" || name == "auto")
599 if (texaltfontmap_.empty())
601 if (texaltfontmap_.find(name) == texaltfontmap_.end()) {
602 LYXERR0("LaTeXFonts::getAltFont: alternative font '" << name << "' not found!");
605 return texaltfontmap_[name];