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());
81 bool LaTeXFont::provides(std::string const & name) const
83 if (provides_.empty())
85 for (size_t i = 0; i < provides_.size(); ++i) {
86 if (provides_[i] == name)
93 string const LaTeXFont::getAvailablePackage(bool dryrun, bool ot1, bool complete, bool & alt)
95 if (ot1 && !ot1package_.empty()) {
96 if (ot1package_ != "none"
97 && (LaTeXFeatures::isAvailable(to_ascii(ot1package_)) || dryrun))
98 return to_ascii(ot1package_);
99 if (!dryrun && ot1package_ != "none")
100 frontend::Alert::warning(_("Font not available"),
101 bformat(_("The LaTeX package `%1$s' needed for the font `%2$s'\n"
102 "is not available on your system. LyX will fall back to the default font."),
103 ot1package_, guiname_), true);
106 if (family_ == "rm" && complete && !completepackage_.empty()) {
107 if (LaTeXFeatures::isAvailable(to_ascii(completepackage_)) || dryrun)
108 return to_ascii(completepackage_);
110 if (!package_.empty()) {
111 if (!requires_.empty() && LaTeXFeatures::isAvailable(to_ascii(requires_)))
112 return to_ascii(package_);
113 if (LaTeXFeatures::isAvailable(to_ascii(package_)))
114 return to_ascii(package_);
115 else if (!altpackages_.empty()) {
116 for (size_t i = 0; i < altpackages_.size(); ++i) {
117 if (LaTeXFeatures::isAvailable(to_ascii(altpackages_[i]))) {
119 return to_ascii(altpackages_[i]);
123 // Output unavailable packages in source preview
125 return to_ascii(package_);
126 docstring const req = requires_.empty() ? package_ : requires_;
127 frontend::Alert::warning(_("Font not available"),
128 bformat(_("The LaTeX package `%1$s' needed for the font `%2$s'\n"
129 "is not available on your system. LyX will fall back to the default font."),
130 req, guiname_), true);
136 string const LaTeXFont::getPackageOptions(bool ot1, bool sc, bool osf, int scale)
138 if (ot1 && !ot1package_.empty())
142 if (!packageoption_.empty())
143 os << to_ascii(packageoption_);
144 if (sc && osf && providesOSF() && providesSC()) {
145 if (!os.str().empty())
147 if (!osfscoption_.empty())
148 os << to_ascii(osfscoption_);
150 os << to_ascii(osfoption_) << ',' << to_ascii(scoption_);
151 } else if (osf && providesOSF()) {
152 if (!os.str().empty())
154 os << to_ascii(osfoption_);
155 } else if (sc && providesSC()) {
156 if (!os.str().empty())
158 os << to_ascii(scoption_);
160 if (scale != 100 && !scaleoption_.empty()) {
161 if (!os.str().empty())
163 os << subst(to_ascii(scaleoption_), "$$val",
164 convert<std::string>(float(scale) / 100));
170 string const LaTeXFont::getLaTeXCode(bool dryrun, bool ot1, bool complete, bool sc,
171 bool osf, int const & scale)
175 if (switchdefault_) {
176 if (family_.empty()) {
177 LYXERR0("Error: Font `" << name_ << "' has no family defined!");
180 if (available(ot1) || dryrun)
181 os << "\\renewcommand{\\" << to_ascii(family_) << "default}{"
182 << to_ascii(name_) << "}\n";
184 frontend::Alert::warning(_("Font not available"),
185 bformat(_("The LaTeX package `%1$s' needed for the font `%2$s'\n"
186 "is not available on your system. LyX will fall back to the default font."),
187 requires_, guiname_), true);
190 string const package =
191 getAvailablePackage(dryrun, ot1, complete, alt);
192 // Package options are not for alternative packages
193 string const packageopts = alt ? string() : getPackageOptions(ot1, sc, osf, scale);
194 if (packageopts.empty() && !package.empty())
195 os << "\\usepackage{" << package << "}\n";
196 else if (!packageopts.empty() && !package.empty())
197 os << "\\usepackage[" << packageopts << "]{" << package << "}\n";
199 if (osf && providesOSF(ot1) && !osfpackage_.empty())
200 os << "\\usepackage{" << to_ascii(osfpackage_) << "}\n";
206 bool LaTeXFont::readFont(Lexer & lex)
227 // Keep these sorted alphabetically!
228 LexerKeyword latexFontTags[] = {
229 { "altpackages", LF_ALT_PACKAGES },
230 { "completepackage", LF_COMPLETE_PACKAGE },
231 { "endfont", LF_END },
232 { "family", LF_FAMILY },
233 { "guiname", LF_GUINAME },
234 { "osfoption", LF_OSFOPTION },
235 { "osfpackage", LF_OSFPACKAGE },
236 { "osfscoption", LF_OSFSCOPTION },
237 { "ot1package", LF_OT1_PACKAGE },
238 { "package", LF_PACKAGE },
239 { "packageoption", LF_PACKAGEOPTION },
240 { "provides", LF_PROVIDES },
241 { "requires", LF_REQUIRES },
242 { "scaleoption", LF_SCALEOPTION },
243 { "scoption", LF_SCOPTION },
244 { "switchdefault", LF_SWITCHDEFAULT }
248 bool finished = false;
249 lex.pushTable(latexFontTags);
250 // parse style section
251 while (!finished && lex.isOK() && !error) {
253 // See comment in LyXRC.cpp.
255 case Lexer::LEX_FEOF:
258 case Lexer::LEX_UNDEF: // parse error
259 lex.printError("Unknown LaTeXFont tag `$$Token'");
266 switch (static_cast<LaTeXFontTags>(le)) {
267 case LF_END: // end of structure
270 case LF_ALT_PACKAGES: {
272 docstring altp = lex.getDocString();
273 altpackages_ = getVectorFromString(altp);
276 case LF_COMPLETE_PACKAGE:
277 lex >> completepackage_;
300 case LF_PACKAGEOPTION:
301 lex >> packageoption_;
305 string features = lex.getString();
306 provides_ = getVectorFromString(features);
318 case LF_SWITCHDEFAULT:
319 lex >> switchdefault_;
324 lex.printError("No End tag found for LaTeXFont tag `$$Token'");
328 return finished && !error;
332 bool LaTeXFont::read(Lexer & lex)
337 lex.printError("No name given for LaTeX font: `$$Token'.");
341 name_ = lex.getDocString();
342 LYXERR(Debug::INFO, "Reading LaTeX font " << name_);
343 if (!readFont(lex)) {
344 LYXERR0("Error parsing LaTeX font `" << name_ << '\'');
348 bool available = true;
349 if (!requires_.empty())
350 available = LaTeXFeatures::isAvailable(to_ascii(requires_));
351 else if (!package_.empty()) {
352 available = LaTeXFeatures::isAvailable(to_ascii(package_));
353 if (!available && !altpackages_.empty()) {
354 for (size_t i = 0; i < altpackages_.size(); ++i) {
355 available = LaTeXFeatures::isAvailable(to_ascii(altpackages_[i]));
361 available_ = available;
363 if (!ot1package_.empty() && ot1package_ != "none")
364 available_ot1_ = LaTeXFeatures::isAvailable(to_ascii(ot1package_));
366 available_ot1_ = available;
372 void LaTeXFonts::readLaTeXFonts()
374 // Read latexfonts file
375 FileName filename = libFileSearch(string(), "latexfonts");
376 if (filename.empty()) {
377 LYXERR0("Error: latexfonts file not found!");
381 lex.setFile(filename);
382 lex.setContext("LaTeXFeatures::readLaTeXFonts");
386 case Lexer::LEX_FEOF:
392 if (lex.getString() != "Font") {
393 lex.printError("Unknown LaTeXFont tag `$$Token'");
401 texfontmap_[f.name()] = f;
406 LaTeXFonts::TexFontMap LaTeXFonts::getLaTeXFonts()
408 if (texfontmap_.empty())
414 LaTeXFont LaTeXFonts::getLaTeXFont(docstring const & name)
416 if (name == "default")
418 if (texfontmap_.empty())
420 if (texfontmap_.find(name) == texfontmap_.end()) {
421 LYXERR0("LaTeXFonts::getLaTeXFont: font '" << name << "' not found!");
424 return texfontmap_[name];