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 (!package_.empty() && !LaTeXFeatures::isAvailable(to_ascii(package_)))
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 (!package_.empty() && !LaTeXFeatures::isAvailable(to_ascii(package_)))
109 return (!scoption_.empty() || !osfscoption_.empty());
113 bool LaTeXFont::providesScale(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).providesScale(ot1, complete, nomath);
121 else if (!package_.empty() && !LaTeXFeatures::isAvailable(to_ascii(package_)))
124 return (!scaleoption_.empty());
127 bool LaTeXFont::provides(std::string const & name, 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).provides(name, ot1, complete, nomath);
135 else if (provides_.empty())
138 for (size_t i = 0; i < provides_.size(); ++i) {
139 if (provides_[i] == name)
146 docstring const LaTeXFont::getUsedFont(bool ot1, bool complete, bool nomath)
148 if (nomath && !nomathfont_.empty() && available(ot1, true))
150 else if (ot1 && !ot1font_.empty())
151 return (ot1font_ == "none") ? docstring() : ot1font_;
152 else if (family_ == "rm" && complete && !completefont_.empty()
153 && altFont(completefont_).available(ot1, nomath))
154 return completefont_;
155 else if (switchdefault_) {
156 if (requires_.empty()
157 || (!requires_.empty()
158 && LaTeXFeatures::isAvailable(to_ascii(requires_))))
161 else if (!requires_.empty()
162 && LaTeXFeatures::isAvailable(to_ascii(requires_)))
164 else if (!package_.empty()
165 && LaTeXFeatures::isAvailable(to_ascii(package_)))
167 else if (!altfonts_.empty()) {
168 for (size_t i = 0; i < altfonts_.size(); ++i) {
169 LaTeXFont altf = altFont(altfonts_[i]);
170 if (altf.available(ot1, nomath))
171 return altf.getUsedFont(ot1, complete, nomath);
179 string const LaTeXFont::getAvailablePackage(bool dryrun)
181 if (package_.empty())
184 string const package = to_ascii(package_);
185 if (!requires_.empty() && LaTeXFeatures::isAvailable(to_ascii(requires_)))
187 else if (LaTeXFeatures::isAvailable(package))
189 // Output unavailable packages in source preview
193 docstring const req = requires_.empty() ? package_ : requires_;
194 frontend::Alert::warning(_("Font not available"),
195 bformat(_("The LaTeX package `%1$s' needed for the font `%2$s'\n"
196 "is not available on your system. LyX will fall back to the default font."),
197 req, guiname_), true);
203 string const LaTeXFont::getPackageOptions(bool ot1, bool complete, bool sc, bool osf,
204 int scale, bool nomath)
207 bool const needosfopt = (osf != osfdefault_);
208 bool const has_osf = providesOSF(ot1, complete, nomath);
209 bool const has_sc = providesSC(ot1, complete, nomath);
211 if (!packageoption_.empty())
212 os << to_ascii(packageoption_);
214 if (sc && needosfopt && has_osf && has_sc) {
215 if (!os.str().empty())
217 if (!osfscoption_.empty())
218 os << to_ascii(osfscoption_);
220 os << to_ascii(osfoption_)
221 << ',' << to_ascii(scoption_);
222 } else if (needosfopt && has_osf) {
223 if (!os.str().empty())
225 os << to_ascii(osfoption_);
226 } else if (sc && has_sc) {
227 if (!os.str().empty())
229 os << to_ascii(scoption_);
232 if (scale != 100 && !scaleoption_.empty()
233 && providesScale(ot1, complete, nomath)) {
234 if (!os.str().empty())
236 os << subst(to_ascii(scaleoption_), "$$val",
237 convert<std::string>(float(scale) / 100));
243 string const LaTeXFont::getLaTeXCode(bool dryrun, bool ot1, bool complete, bool sc,
244 bool osf, bool nomath, int const & scale)
248 docstring const usedfont = getUsedFont(ot1, complete, nomath);
249 if (usedfont.empty())
251 else if (usedfont != name_)
252 return altFont(usedfont).getLaTeXCode(dryrun, ot1, complete, sc, osf, nomath, scale);
254 if (switchdefault_) {
255 if (family_.empty()) {
256 LYXERR0("Error: Font `" << name_ << "' has no family defined!");
259 if (available(ot1, nomath) || dryrun)
260 os << "\\renewcommand{\\" << to_ascii(family_) << "default}{"
261 << to_ascii(name_) << "}\n";
263 frontend::Alert::warning(_("Font not available"),
264 bformat(_("The LaTeX package `%1$s' needed for the font `%2$s'\n"
265 "is not available on your system. LyX will fall back to the default font."),
266 requires_, guiname_), true);
268 string const package =
269 getAvailablePackage(dryrun);
270 string const packageopts = getPackageOptions(ot1, complete, sc, osf, scale, nomath);
271 if (packageopts.empty() && !package.empty())
272 os << "\\usepackage{" << package << "}\n";
273 else if (!packageopts.empty() && !package.empty())
274 os << "\\usepackage[" << packageopts << "]{" << package << "}\n";
276 if (osf && providesOSF(ot1, complete, nomath) && !osffont_.empty())
277 os << altFont(osffont_).getLaTeXCode(dryrun, ot1, complete, sc, osf, nomath, scale);
283 bool LaTeXFont::readFont(Lexer & lex)
306 // Keep these sorted alphabetically!
307 LexerKeyword latexFontTags[] = {
308 { "altfonts", LF_ALT_FONTS },
309 { "completefont", LF_COMPLETE_FONT },
310 { "endfont", LF_END },
311 { "family", LF_FAMILY },
312 { "guiname", LF_GUINAME },
313 { "nomathfont", LF_NOMATHFONT },
314 { "osfdefault", LF_OSFDEFAULT },
315 { "osffont", LF_OSFFONT },
316 { "osfoption", LF_OSFOPTION },
317 { "osfscoption", LF_OSFSCOPTION },
318 { "ot1font", LF_OT1_FONT },
319 { "package", LF_PACKAGE },
320 { "packageoption", LF_PACKAGEOPTION },
321 { "provides", LF_PROVIDES },
322 { "requires", LF_REQUIRES },
323 { "scaleoption", LF_SCALEOPTION },
324 { "scoption", LF_SCOPTION },
325 { "switchdefault", LF_SWITCHDEFAULT }
329 bool finished = false;
330 lex.pushTable(latexFontTags);
331 // parse style section
332 while (!finished && lex.isOK() && !error) {
334 // See comment in LyXRC.cpp.
336 case Lexer::LEX_FEOF:
339 case Lexer::LEX_UNDEF: // parse error
340 lex.printError("Unknown LaTeXFont tag `$$Token'");
347 switch (static_cast<LaTeXFontTags>(le)) {
348 case LF_END: // end of structure
353 docstring altp = lex.getDocString();
354 altfonts_ = getVectorFromString(altp);
357 case LF_COMPLETE_FONT:
358 lex >> completefont_;
387 case LF_PACKAGEOPTION:
388 lex >> packageoption_;
392 string features = lex.getString();
393 provides_ = getVectorFromString(features);
405 case LF_SWITCHDEFAULT:
406 lex >> switchdefault_;
411 lex.printError("No End tag found for LaTeXFont tag `$$Token'");
415 return finished && !error;
419 bool LaTeXFont::read(Lexer & lex)
425 lex.printError("No name given for LaTeX font: `$$Token'.");
429 name_ = lex.getDocString();
430 LYXERR(Debug::INFO, "Reading LaTeX font " << name_);
431 if (!readFont(lex)) {
432 LYXERR0("Error parsing LaTeX font `" << name_ << '\'');
440 void LaTeXFonts::readLaTeXFonts()
442 // Read latexfonts file
443 FileName filename = libFileSearch(string(), "latexfonts");
444 if (filename.empty()) {
445 LYXERR0("Error: latexfonts file not found!");
449 lex.setFile(filename);
450 lex.setContext("LaTeXFeatures::readLaTeXFonts");
454 case Lexer::LEX_FEOF:
460 string const type = lex.getString();
461 if (type != "Font" && type != "AltFont") {
462 lex.printError("Unknown LaTeXFont tag `$$Token'");
470 if (type == "AltFont")
471 texaltfontmap_[f.name()] = f;
473 texfontmap_[f.name()] = f;
478 LaTeXFonts::TexFontMap LaTeXFonts::getLaTeXFonts()
480 if (texfontmap_.empty())
486 LaTeXFont LaTeXFonts::getLaTeXFont(docstring const & name)
488 if (name == "default" || name == "auto")
490 if (texfontmap_.empty())
492 if (texfontmap_.find(name) == texfontmap_.end()) {
493 LYXERR0("LaTeXFonts::getLaTeXFont: font '" << name << "' not found!");
496 return texfontmap_[name];
500 LaTeXFont LaTeXFonts::getAltFont(docstring const & name)
502 if (name == "default" || name == "auto")
504 if (texaltfontmap_.empty())
506 if (texaltfontmap_.find(name) == texaltfontmap_.end()) {
507 LYXERR0("LaTeXFonts::getAltFont: alternative font '" << name << "' not found!");
510 return texaltfontmap_[name];