]> git.lyx.org Git - features.git/blob - src/LaTeXFonts.cpp
Implement Provides tag to LaTeXFonts
[features.git] / src / LaTeXFonts.cpp
1 /**
2  * \file LaTeXFonts.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Jürgen Spitzmüller
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "LaTeXFonts.h"
14
15 #include "LaTeXFeatures.h"
16 #include "Lexer.h"
17
18 #include "frontends/alert.h"
19
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"
26
27
28 using namespace std;
29 using namespace lyx::support;
30
31
32 namespace lyx {
33
34 LaTeXFonts latexfonts;
35
36
37 bool LaTeXFont::available(bool ot1) const
38 {
39         return ot1 ? available_ot1_ : available_;
40 }
41
42
43 bool LaTeXFont::providesOSF(bool ot1) const
44 {
45         if (!osfpackage_.empty())
46                 return LaTeXFeatures::isAvailable(to_ascii(osfpackage_));
47
48         if (ot1 && !ot1package_.empty() && ot1package_ != "none")
49                 return false;
50
51         if (!package_.empty() && !LaTeXFeatures::isAvailable(to_ascii(package_)))
52                 return false;
53
54         return (!osfoption_.empty() || !osfscoption_.empty());
55 }
56
57
58 bool LaTeXFont::providesSC(bool ot1) const
59 {
60         if (ot1 && !ot1package_.empty() && ot1package_ != "none")
61                 return false;
62
63         if (!package_.empty() && !LaTeXFeatures::isAvailable(to_ascii(package_)))
64                 return false;
65
66         return (!scoption_.empty() || !osfscoption_.empty());
67 }
68
69
70 bool LaTeXFont::providesScale(bool ot1) const
71 {
72         if (ot1 && !ot1package_.empty() && ot1package_ != "none")
73                 return false;
74
75         if (!package_.empty() && !LaTeXFeatures::isAvailable(to_ascii(package_)))
76                 return false;
77
78         return (!scaleoption_.empty());
79 }
80
81 bool LaTeXFont::provides(std::string const & name) const
82 {
83         if (provides_.empty())
84                 return false;
85         for (size_t i = 0; i < provides_.size(); ++i) {
86                 if (provides_[i] == name)
87                         return true;
88         }
89         return false;
90 }
91
92
93 string const LaTeXFont::getAvailablePackage(bool dryrun, bool ot1, bool complete, bool & alt)
94 {
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);
104                 return string();
105         }
106         if (family_ == "rm" && complete && !completepackage_.empty()) {
107                 if (LaTeXFeatures::isAvailable(to_ascii(completepackage_)) || dryrun)
108                         return to_ascii(completepackage_);
109         }
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]))) {
118                                         alt = true;
119                                         return to_ascii(altpackages_[i]);
120                                 }
121                         }
122                 }
123                 // Output unavailable packages in source preview
124                 if (dryrun)
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);
131         }
132         return string();
133 }
134
135
136 string const LaTeXFont::getPackageOptions(bool ot1, bool sc, bool osf, int scale)
137 {
138         if (ot1 && !ot1package_.empty())
139                 return string();
140
141         ostringstream os;
142         if (!packageoption_.empty())
143                 os << to_ascii(packageoption_);
144         if (sc && osf && providesOSF() && providesSC()) {
145                 if (!os.str().empty())
146                         os << ',';
147                 if (!osfscoption_.empty())
148                         os << to_ascii(osfscoption_);
149                 else
150                         os << to_ascii(osfoption_) << ',' << to_ascii(scoption_);
151         } else if (osf && providesOSF()) {
152                 if (!os.str().empty())
153                         os << ',';
154                 os << to_ascii(osfoption_);
155         } else if (sc && providesSC()) {
156                 if (!os.str().empty())
157                         os << ',';
158                 os << to_ascii(scoption_);
159         }
160         if (scale != 100 && !scaleoption_.empty()) {
161                 if (!os.str().empty())
162                         os << ',';
163                 os << subst(to_ascii(scaleoption_), "$$val",
164                             convert<std::string>(float(scale) / 100));
165         }
166         return os.str();
167 }
168
169
170 string const LaTeXFont::getLaTeXCode(bool dryrun, bool ot1, bool complete, bool sc,
171                                      bool osf, int const & scale)
172 {
173         ostringstream os;
174
175         if (switchdefault_) {
176                 if (family_.empty()) {
177                         LYXERR0("Error: Font `" << name_ << "' has no family defined!");
178                         return string();
179                 }
180                 if (available(ot1) || dryrun)
181                         os << "\\renewcommand{\\" << to_ascii(family_) << "default}{"
182                         << to_ascii(name_) << "}\n";
183                 else
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);
188         } else {
189                 bool alt = false;
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";
198         }
199         if (osf && providesOSF(ot1) && !osfpackage_.empty())
200                 os << "\\usepackage{" << to_ascii(osfpackage_) << "}\n";
201
202         return os.str();
203 }
204
205
206 bool LaTeXFont::readFont(Lexer & lex)
207 {
208         enum LaTeXFontTags {
209                 LF_ALT_PACKAGES = 1,
210                 LF_COMPLETE_PACKAGE,
211                 LF_END,
212                 LF_FAMILY,
213                 LF_GUINAME,
214                 LF_OSFOPTION,
215                 LF_OSFPACKAGE,
216                 LF_OSFSCOPTION,
217                 LF_OT1_PACKAGE,
218                 LF_PACKAGE,
219                 LF_PACKAGEOPTION,
220                 LF_PROVIDES,
221                 LF_REQUIRES,
222                 LF_SCALEOPTION,
223                 LF_SCOPTION,
224                 LF_SWITCHDEFAULT
225         };
226
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 }
245         };
246
247         bool error = false;
248         bool finished = false;
249         lex.pushTable(latexFontTags);
250         // parse style section
251         while (!finished && lex.isOK() && !error) {
252                 int le = lex.lex();
253                 // See comment in LyXRC.cpp.
254                 switch (le) {
255                 case Lexer::LEX_FEOF:
256                         continue;
257
258                 case Lexer::LEX_UNDEF: // parse error
259                         lex.printError("Unknown LaTeXFont tag `$$Token'");
260                         error = true;
261                         continue;
262
263                 default: 
264                         break;
265                 }
266                 switch (static_cast<LaTeXFontTags>(le)) {
267                 case LF_END: // end of structure
268                         finished = true;
269                         break;
270                 case LF_ALT_PACKAGES: {
271                         lex.eatLine();
272                         docstring altp = lex.getDocString();
273                         altpackages_ = getVectorFromString(altp);
274                         break;
275                 }
276                 case LF_COMPLETE_PACKAGE:
277                         lex >> completepackage_;
278                         break;
279                 case LF_FAMILY:
280                         lex >> family_;
281                         break;
282                 case LF_GUINAME:
283                         lex >> guiname_;
284                         break;
285                 case LF_OSFOPTION:
286                         lex >> osfoption_;
287                         break;
288                 case LF_OSFPACKAGE:
289                         lex >> osfpackage_;
290                         break;
291                 case LF_OSFSCOPTION:
292                         lex >> osfscoption_;
293                         break;
294                 case LF_OT1_PACKAGE:
295                         lex >> ot1package_;
296                         break;
297                 case LF_PACKAGE:
298                         lex >> package_;
299                         break;
300                 case LF_PACKAGEOPTION:
301                         lex >> packageoption_;
302                         break;
303                 case LF_PROVIDES: {
304                         lex.eatLine();
305                         string features = lex.getString();
306                         provides_ = getVectorFromString(features);
307                         break;
308                 }
309                 case LF_REQUIRES:
310                         lex >> requires_;
311                         break;
312                 case LF_SCALEOPTION:
313                         lex >> scaleoption_;
314                         break;
315                 case LF_SCOPTION:
316                         lex >> scoption_;
317                         break;
318                 case LF_SWITCHDEFAULT:
319                         lex >> switchdefault_;
320                         break;
321                 }
322         }
323         if (!finished) {
324                 lex.printError("No End tag found for LaTeXFont tag `$$Token'");
325                 return false;
326         }
327         lex.popTable();
328         return finished && !error;
329 }
330
331
332 bool LaTeXFont::read(Lexer & lex)
333 {
334         switchdefault_ = 0;
335
336         if (!lex.next()) {
337                 lex.printError("No name given for LaTeX font: `$$Token'.");
338                 return false;
339         }
340
341         name_ = lex.getDocString();
342         LYXERR(Debug::INFO, "Reading LaTeX font " << name_);
343         if (!readFont(lex)) {
344                 LYXERR0("Error parsing LaTeX font `" << name_ << '\'');
345                 return false;
346         }
347
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]));
356                                 if (available)
357                                         break;
358                         }
359                 }
360         }
361         available_ = available;
362
363         if (!ot1package_.empty() && ot1package_ != "none")
364                 available_ot1_ = LaTeXFeatures::isAvailable(to_ascii(ot1package_));
365         else
366                 available_ot1_ = available;
367
368         return true;
369 }
370
371
372 void LaTeXFonts::readLaTeXFonts()
373 {
374         // Read latexfonts file
375         FileName filename = libFileSearch(string(), "latexfonts");
376         if (filename.empty()) {
377                 LYXERR0("Error: latexfonts file not found!");
378                 return;
379         }
380         Lexer lex;
381         lex.setFile(filename);
382         lex.setContext("LaTeXFeatures::readLaTeXFonts");
383         while (lex.isOK()) {
384                 int le = lex.lex();
385                 switch (le) {
386                 case Lexer::LEX_FEOF:
387                         continue;
388
389                 default:
390                         break;
391                 }
392                 if (lex.getString() != "Font") {
393                         lex.printError("Unknown LaTeXFont tag `$$Token'");
394                         continue;
395                 }
396                 LaTeXFont f;
397                 f.read(lex);
398                 if (!lex)
399                         break;
400
401                 texfontmap_[f.name()] = f;
402         }
403 }
404
405
406 LaTeXFonts::TexFontMap LaTeXFonts::getLaTeXFonts()
407 {
408         if (texfontmap_.empty())
409                 readLaTeXFonts();
410         return texfontmap_;
411 }
412
413
414 LaTeXFont LaTeXFonts::getLaTeXFont(docstring const & name)
415 {
416         if (name == "default")
417                 return LaTeXFont();
418         if (texfontmap_.empty())
419                 readLaTeXFonts();
420         if (texfontmap_.find(name) == texfontmap_.end()) {
421                 LYXERR0("LaTeXFonts::getLaTeXFont: font '" << name << "' not found!");
422                 return LaTeXFont();
423         }
424         return texfontmap_[name];
425 }
426
427
428 } // namespace lyx