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 (requires_.empty() && package_.empty())
53 else if (!requires_.empty()
54 && LaTeXFeatures::isAvailable(to_ascii(requires_)))
56 else if (requires_.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);
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);
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);
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);
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);
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);
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);
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)
176 if (nomath && !nomathfont_.empty() && available(ot1, true))
178 else if (ot1 && !ot1font_.empty())
179 return (ot1font_ == "none") ? docstring() : ot1font_;
180 else if (family_ == "rm" && complete && !completefont_.empty()
181 && altFont(completefont_).available(ot1, nomath))
182 return completefont_;
183 else if (switchdefault_) {
184 if (requires_.empty()
185 || (!requires_.empty()
186 && LaTeXFeatures::isAvailable(to_ascii(requires_))))
189 else if (!requires_.empty()
190 && LaTeXFeatures::isAvailable(to_ascii(requires_)))
192 else if (!package_.empty()
193 && LaTeXFeatures::isAvailable(to_ascii(package_)))
195 else if (!preamble_.empty() && package_.empty()
196 && requires_.empty() && !switchdefault_
197 && altfonts_.empty()) {
200 else if (!altfonts_.empty()) {
201 for (size_t i = 0; i < altfonts_.size(); ++i) {
202 LaTeXFont altf = altFont(altfonts_[i]);
203 if (altf.available(ot1, nomath))
204 return altf.getUsedFont(ot1, complete, nomath);
212 docstring const LaTeXFont::getUsedPackage(bool ot1, bool complete, bool nomath)
214 docstring const usedfont = getUsedFont(ot1, complete, nomath);
215 if (usedfont.empty())
217 return theLaTeXFonts().getLaTeXFont(usedfont).package();
221 string const LaTeXFont::getAvailablePackage(bool dryrun)
223 if (package_.empty())
226 string const package = to_ascii(package_);
227 if (!requires_.empty() && LaTeXFeatures::isAvailable(to_ascii(requires_)))
229 else if (LaTeXFeatures::isAvailable(package))
231 // Output unavailable packages in source preview
235 docstring const req = requires_.empty() ? package_ : requires_;
236 frontend::Alert::warning(_("Font not available"),
237 bformat(_("The LaTeX package `%1$s' needed for the font `%2$s'\n"
238 "is not available on your system. LyX will fall back to the default font."),
239 req, guiname_), true);
245 string const LaTeXFont::getPackageOptions(bool ot1, bool complete, bool sc, bool osf,
246 int scale, string const extraopts, bool nomath)
249 bool const needosfopt = (osf != osfdefault_);
250 bool const has_osf = providesOSF(ot1, complete, nomath);
251 bool const has_sc = providesSC(ot1, complete, nomath);
252 bool const moreopts = providesMoreOptions(ot1, complete, nomath);
254 if (!packageoptions_.empty())
255 os << to_ascii(packageoptions_);
257 if (sc && needosfopt && has_osf && has_sc) {
258 if (!os.str().empty())
260 if (!osfscoption_.empty())
261 os << to_ascii(osfscoption_);
263 os << to_ascii(osfoption_)
264 << ',' << to_ascii(scoption_);
265 } else if (needosfopt && has_osf) {
266 if (!os.str().empty())
268 os << to_ascii(osfoption_);
269 } else if (sc && has_sc) {
270 if (!os.str().empty())
272 os << to_ascii(scoption_);
275 if (scale != 100 && !scaleoption_.empty()
276 && providesScale(ot1, complete, nomath)) {
277 if (!os.str().empty())
279 os << subst(to_ascii(scaleoption_), "$$val",
280 convert<std::string>(float(scale) / 100));
283 if (moreopts && !extraopts.empty()) {
284 if (!os.str().empty())
292 string const LaTeXFont::getLaTeXCode(bool dryrun, bool ot1, bool complete, bool sc,
293 bool osf, bool nomath, string const extraopts,
298 docstring const usedfont = getUsedFont(ot1, complete, nomath);
299 if (usedfont.empty())
301 else if (usedfont != name_)
302 return altFont(usedfont).getLaTeXCode(dryrun, ot1, complete, sc,
303 osf, nomath, extraopts, scale);
305 if (switchdefault_) {
306 if (family_.empty()) {
307 LYXERR0("Error: Font `" << name_ << "' has no family defined!");
310 if (available(ot1, nomath) || dryrun)
311 os << "\\renewcommand{\\" << to_ascii(family_) << "default}{"
312 << to_ascii(name_) << "}\n";
314 frontend::Alert::warning(_("Font not available"),
315 bformat(_("The LaTeX package `%1$s' needed for the font `%2$s'\n"
316 "is not available on your system. LyX will fall back to the default font."),
317 requires_, guiname_), true);
319 string const package =
320 getAvailablePackage(dryrun);
321 string const packageopts = getPackageOptions(ot1, complete, sc, osf, scale, extraopts, nomath);
322 if (packageopts.empty() && !package.empty())
323 os << "\\usepackage{" << package << "}\n";
324 else if (!packageopts.empty() && !package.empty())
325 os << "\\usepackage[" << packageopts << "]{" << package << "}\n";
327 if (osf && providesOSF(ot1, complete, nomath) && !osffont_.empty())
328 os << altFont(osffont_).getLaTeXCode(dryrun, ot1, complete, sc, osf,
329 nomath, extraopts, scale);
331 if (!preamble_.empty())
332 os << to_utf8(preamble_);
338 bool LaTeXFont::hasFontenc(string const & name) const
340 for (auto const & fe : fontenc_) {
348 bool LaTeXFont::readFont(Lexer & lex)
374 // Keep these sorted alphabetically!
375 LexerKeyword latexFontTags[] = {
376 { "altfonts", LF_ALT_FONTS },
377 { "completefont", LF_COMPLETE_FONT },
378 { "endfont", LF_END },
379 { "family", LF_FAMILY },
380 { "fontencoding", LF_FONTENC },
381 { "guiname", LF_GUINAME },
382 { "moreoptions", LF_MOREOPTS },
383 { "nomathfont", LF_NOMATHFONT },
384 { "osfdefault", LF_OSFDEFAULT },
385 { "osffont", LF_OSFFONT },
386 { "osfoption", LF_OSFOPTION },
387 { "osfscoption", LF_OSFSCOPTION },
388 { "ot1font", LF_OT1_FONT },
389 { "package", LF_PACKAGE },
390 { "packageoptions", LF_PACKAGEOPTIONS },
391 { "preamble", LF_PREAMBLE },
392 { "provides", LF_PROVIDES },
393 { "requires", LF_REQUIRES },
394 { "scaleoption", LF_SCALEOPTION },
395 { "scoption", LF_SCOPTION },
396 { "switchdefault", LF_SWITCHDEFAULT }
400 bool finished = false;
401 lex.pushTable(latexFontTags);
402 // parse style section
403 while (!finished && lex.isOK() && !error) {
405 // See comment in LyXRC.cpp.
407 case Lexer::LEX_FEOF:
410 case Lexer::LEX_UNDEF: // parse error
411 lex.printError("Unknown LaTeXFont tag `$$Token'");
418 switch (static_cast<LaTeXFontTags>(le)) {
419 case LF_END: // end of structure
424 docstring altp = lex.getDocString();
425 altfonts_ = getVectorFromString(altp);
428 case LF_COMPLETE_FONT:
429 lex >> completefont_;
439 string fe = lex.getString();
440 fontenc_ = getVectorFromString(fe);
464 case LF_PACKAGEOPTIONS:
465 lex >> packageoptions_;
468 preamble_ = lex.getLongString(from_ascii("EndPreamble"));
472 string features = lex.getString();
473 provides_ = getVectorFromString(features);
488 case LF_SWITCHDEFAULT:
489 lex >> switchdefault_;
494 lex.printError("No End tag found for LaTeXFont tag `$$Token'");
498 return finished && !error;
502 bool LaTeXFont::read(Lexer & lex)
509 lex.printError("No name given for LaTeX font: `$$Token'.");
513 name_ = lex.getDocString();
514 LYXERR(Debug::INFO, "Reading LaTeX font " << name_);
515 if (!readFont(lex)) {
516 LYXERR0("Error parsing LaTeX font `" << name_ << '\'');
520 if (fontenc_.empty())
521 fontenc_.push_back("T1");
527 void LaTeXFonts::readLaTeXFonts()
529 // Read latexfonts file
530 FileName filename = libFileSearch(string(), "latexfonts");
531 if (filename.empty()) {
532 LYXERR0("Error: latexfonts file not found!");
536 lex.setFile(filename);
537 lex.setContext("LaTeXFeatures::readLaTeXFonts");
541 case Lexer::LEX_FEOF:
547 string const type = lex.getString();
548 if (type != "Font" && type != "AltFont") {
549 lex.printError("Unknown LaTeXFont tag `$$Token'");
557 if (type == "AltFont")
558 texaltfontmap_[f.name()] = f;
560 texfontmap_[f.name()] = f;
565 LaTeXFonts::TexFontMap LaTeXFonts::getLaTeXFonts()
567 if (texfontmap_.empty())
573 LaTeXFont LaTeXFonts::getLaTeXFont(docstring const & name)
575 if (name == "default" || name == "auto")
577 if (texfontmap_.empty())
579 if (texfontmap_.find(name) == texfontmap_.end()) {
580 LYXERR0("LaTeXFonts::getLaTeXFont: font '" << name << "' not found!");
583 return texfontmap_[name];
587 LaTeXFont LaTeXFonts::getAltFont(docstring const & name)
589 if (name == "default" || name == "auto")
591 if (texaltfontmap_.empty())
593 if (texaltfontmap_.find(name) == texaltfontmap_.end()) {
594 LYXERR0("LaTeXFonts::getAltFont: alternative font '" << name << "' not found!");
597 return texaltfontmap_[name];