]> git.lyx.org Git - features.git/blob - src/LaTeXFonts.cpp
LaTeXFonts: Support the rare case where there is really only preamble code
[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 LaTeXFont LaTeXFont::altFont(docstring const & name)
38 {
39         return theLaTeXFonts().getAltFont(name);
40 }
41
42
43 bool LaTeXFont::available(bool ot1, bool nomath)
44 {
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())
51                 return true;
52         else if (!requires_.empty()
53                 && LaTeXFeatures::isAvailable(to_ascii(requires_)))
54                 return true;
55         else if (requires_.empty() && !package_.empty()
56                 && LaTeXFeatures::isAvailable(to_ascii(package_)))
57                 return true;
58         else if (!altfonts_.empty()) {
59                 for (size_t i = 0; i < altfonts_.size(); ++i) {
60                         if (altFont(altfonts_[i]).available(ot1, nomath))
61                                 return true;
62                 }
63         }
64         return false;
65 }
66
67
68 bool LaTeXFont::providesNoMath(bool ot1, bool complete)
69 {
70         docstring const usedfont = getUsedFont(ot1, complete, false);
71
72         if (usedfont.empty())
73                 return false;
74         else if (usedfont != name_)
75                 return altFont(usedfont).providesNoMath(ot1, complete);
76
77         return (!nomathfont_.empty() && available(ot1, true));
78 }
79
80
81 bool LaTeXFont::providesOSF(bool ot1, bool complete, bool nomath)
82 {
83         docstring const usedfont = getUsedFont(ot1, complete, nomath);
84
85         if (usedfont.empty())
86                 return false;
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 (!available(ot1, nomath))
92                 return false;
93
94         return (!osfoption_.empty() || !osfscoption_.empty());
95 }
96
97
98 bool LaTeXFont::providesSC(bool ot1, bool complete, bool nomath)
99 {
100         docstring const usedfont = getUsedFont(ot1, complete, nomath);
101
102         if (usedfont.empty())
103                 return false;
104         else if (usedfont != name_)
105                 return altFont(usedfont).providesSC(ot1, complete, nomath);
106         else if (!available(ot1, nomath))
107                 return false;
108
109         return (!scoption_.empty() || !osfscoption_.empty());
110 }
111
112
113 bool LaTeXFont::providesScale(bool ot1, bool complete, bool nomath)
114 {
115         docstring const usedfont = getUsedFont(ot1, complete, nomath);
116
117         if (usedfont.empty())
118                 return false;
119         else if (usedfont != name_)
120                 return altFont(usedfont).providesScale(ot1, complete, nomath);
121         else if (!available(ot1, nomath))
122                 return false;
123         return (!scaleoption_.empty());
124 }
125
126 bool LaTeXFont::provides(std::string const & name, bool ot1, bool complete, bool nomath)
127 {
128         docstring const usedfont = getUsedFont(ot1, complete, nomath);
129
130         if (usedfont.empty())
131                 return false;
132         else if (usedfont != name_)
133                 return altFont(usedfont).provides(name, ot1, complete, nomath);
134         else if (provides_.empty())
135                 return false;
136
137         for (size_t i = 0; i < provides_.size(); ++i) {
138                 if (provides_[i] == name)
139                         return true;
140         }
141         return false;
142 }
143
144
145 docstring const LaTeXFont::getUsedFont(bool ot1, bool complete, bool nomath)
146 {
147         if (nomath && !nomathfont_.empty() && available(ot1, true))
148                 return nomathfont_;
149         else if (ot1 && !ot1font_.empty())
150                 return (ot1font_ == "none") ? docstring() : ot1font_;
151         else if (family_ == "rm" && complete && !completefont_.empty()
152                  && altFont(completefont_).available(ot1, nomath))
153                         return completefont_;
154         else if (switchdefault_) {
155                 if (requires_.empty()
156                     || (!requires_.empty()
157                         && LaTeXFeatures::isAvailable(to_ascii(requires_))))
158                         return name_;
159         }
160         else if (!requires_.empty()
161                 && LaTeXFeatures::isAvailable(to_ascii(requires_)))
162                         return name_;
163         else if (!package_.empty()
164                 && LaTeXFeatures::isAvailable(to_ascii(package_)))
165                         return name_;
166         else if (!preamble_.empty() && package_.empty()
167                  && requires_.empty() && !switchdefault_
168                  && altfonts_.empty()) {
169                         return name_;
170         }
171         else if (!altfonts_.empty()) {
172                 for (size_t i = 0; i < altfonts_.size(); ++i) {
173                         LaTeXFont altf = altFont(altfonts_[i]);
174                         if (altf.available(ot1, nomath))
175                                 return altf.getUsedFont(ot1, complete, nomath);
176                 }
177         }
178
179         return docstring();
180 }
181
182
183 string const LaTeXFont::getAvailablePackage(bool dryrun)
184 {
185         if (package_.empty())
186                 return string();
187
188         string const package = to_ascii(package_);
189         if (!requires_.empty() && LaTeXFeatures::isAvailable(to_ascii(requires_)))
190                 return package;
191         else if (LaTeXFeatures::isAvailable(package))
192                 return package;
193         // Output unavailable packages in source preview
194         else if (dryrun)
195                 return package;
196
197         docstring const req = requires_.empty() ? package_ : requires_;
198         frontend::Alert::warning(_("Font not available"),
199                         bformat(_("The LaTeX package `%1$s' needed for the font `%2$s'\n"
200                                   "is not available on your system. LyX will fall back to the default font."),
201                                 req, guiname_), true);
202
203         return string();
204 }
205
206
207 string const LaTeXFont::getPackageOptions(bool ot1, bool complete, bool sc, bool osf,
208                                           int scale, bool nomath)
209 {
210         ostringstream os;
211         bool const needosfopt = (osf != osfdefault_);
212         bool const has_osf = providesOSF(ot1, complete, nomath);
213         bool const has_sc = providesSC(ot1, complete, nomath);
214
215         if (!packageoption_.empty())
216                 os << to_ascii(packageoption_);
217
218         if (sc && needosfopt && has_osf && has_sc) {
219                 if (!os.str().empty())
220                         os << ',';
221                 if (!osfscoption_.empty())
222                         os << to_ascii(osfscoption_);
223                 else
224                         os << to_ascii(osfoption_)
225                            << ',' << to_ascii(scoption_);
226         } else if (needosfopt && has_osf) {
227                 if (!os.str().empty())
228                         os << ',';
229                 os << to_ascii(osfoption_);
230         } else if (sc && has_sc) {
231                 if (!os.str().empty())
232                         os << ',';
233                 os << to_ascii(scoption_);
234         }
235
236         if (scale != 100 && !scaleoption_.empty()
237             && providesScale(ot1, complete, nomath)) {
238                 if (!os.str().empty())
239                         os << ',';
240                 os << subst(to_ascii(scaleoption_), "$$val",
241                             convert<std::string>(float(scale) / 100));
242         }
243         return os.str();
244 }
245
246
247 string const LaTeXFont::getLaTeXCode(bool dryrun, bool ot1, bool complete, bool sc,
248                                      bool osf, bool nomath, int const & scale)
249 {
250         ostringstream os;
251
252         docstring const usedfont = getUsedFont(ot1, complete, nomath);
253         if (usedfont.empty())
254                 return string();
255         else if (usedfont != name_)
256                 return altFont(usedfont).getLaTeXCode(dryrun, ot1, complete, sc, osf, nomath, scale);
257
258         if (switchdefault_) {
259                 if (family_.empty()) {
260                         LYXERR0("Error: Font `" << name_ << "' has no family defined!");
261                         return string();
262                 }
263                 if (available(ot1, nomath) || dryrun)
264                         os << "\\renewcommand{\\" << to_ascii(family_) << "default}{"
265                            << to_ascii(name_) << "}\n";
266                 else
267                         frontend::Alert::warning(_("Font not available"),
268                                         bformat(_("The LaTeX package `%1$s' needed for the font `%2$s'\n"
269                                                   "is not available on your system. LyX will fall back to the default font."),
270                                                 requires_, guiname_), true);
271         } else {
272                 string const package =
273                         getAvailablePackage(dryrun);
274                 string const packageopts = getPackageOptions(ot1, complete, sc, osf, scale, nomath);
275                 if (packageopts.empty() && !package.empty())
276                         os << "\\usepackage{" << package << "}\n";
277                 else if (!packageopts.empty() && !package.empty())
278                         os << "\\usepackage[" << packageopts << "]{" << package << "}\n";
279         }
280         if (osf && providesOSF(ot1, complete, nomath) && !osffont_.empty())
281                 os << altFont(osffont_).getLaTeXCode(dryrun, ot1, complete, sc, osf, nomath, scale);
282
283         if (!preamble_.empty())
284                 os << preamble_;
285
286         return os.str();
287 }
288
289
290 bool LaTeXFont::readFont(Lexer & lex)
291 {
292         enum LaTeXFontTags {
293                 LF_ALT_FONTS = 1,
294                 LF_COMPLETE_FONT,
295                 LF_END,
296                 LF_FAMILY,
297                 LF_GUINAME,
298                 LF_NOMATHFONT,
299                 LF_OSFDEFAULT,
300                 LF_OSFFONT,
301                 LF_OSFOPTION,
302                 LF_OSFSCOPTION,
303                 LF_OT1_FONT,
304                 LF_PACKAGE,
305                 LF_PACKAGEOPTION,
306                 LF_PREAMBLE,
307                 LF_PROVIDES,
308                 LF_REQUIRES,
309                 LF_SCALEOPTION,
310                 LF_SCOPTION,
311                 LF_SWITCHDEFAULT
312         };
313
314         // Keep these sorted alphabetically!
315         LexerKeyword latexFontTags[] = {
316                 { "altfonts",             LF_ALT_FONTS },
317                 { "completefont",         LF_COMPLETE_FONT },
318                 { "endfont",              LF_END },
319                 { "family",               LF_FAMILY },
320                 { "guiname",              LF_GUINAME },
321                 { "nomathfont",           LF_NOMATHFONT },
322                 { "osfdefault",           LF_OSFDEFAULT },
323                 { "osffont",              LF_OSFFONT },
324                 { "osfoption",            LF_OSFOPTION },
325                 { "osfscoption",          LF_OSFSCOPTION },
326                 { "ot1font",              LF_OT1_FONT },
327                 { "package",              LF_PACKAGE },
328                 { "packageoption",        LF_PACKAGEOPTION },
329                 { "preamble",             LF_PREAMBLE },
330                 { "provides",             LF_PROVIDES },
331                 { "requires",             LF_REQUIRES },
332                 { "scaleoption",          LF_SCALEOPTION },
333                 { "scoption",             LF_SCOPTION },
334                 { "switchdefault",        LF_SWITCHDEFAULT }
335         };
336
337         bool error = false;
338         bool finished = false;
339         lex.pushTable(latexFontTags);
340         // parse style section
341         while (!finished && lex.isOK() && !error) {
342                 int le = lex.lex();
343                 // See comment in LyXRC.cpp.
344                 switch (le) {
345                 case Lexer::LEX_FEOF:
346                         continue;
347
348                 case Lexer::LEX_UNDEF: // parse error
349                         lex.printError("Unknown LaTeXFont tag `$$Token'");
350                         error = true;
351                         continue;
352
353                 default: 
354                         break;
355                 }
356                 switch (static_cast<LaTeXFontTags>(le)) {
357                 case LF_END: // end of structure
358                         finished = true;
359                         break;
360                 case LF_ALT_FONTS: {
361                         lex.eatLine();
362                         docstring altp = lex.getDocString();
363                         altfonts_ = getVectorFromString(altp);
364                         break;
365                 }
366                 case LF_COMPLETE_FONT:
367                         lex >> completefont_;
368                         break;
369                 case LF_FAMILY:
370                         lex >> family_;
371                         break;
372                 case LF_GUINAME:
373                         lex >> guiname_;
374                         break;
375                 case LF_NOMATHFONT:
376                         lex >> nomathfont_;
377                         break;
378                 case LF_OSFOPTION:
379                         lex >> osfoption_;
380                         break;
381                 case LF_OSFFONT:
382                         lex >> osffont_;
383                         break;
384                 case LF_OSFDEFAULT:
385                         lex >> osfdefault_;
386                         break;
387                 case LF_OSFSCOPTION:
388                         lex >> osfscoption_;
389                         break;
390                 case LF_OT1_FONT:
391                         lex >> ot1font_;
392                         break;
393                 case LF_PACKAGE:
394                         lex >> package_;
395                         break;
396                 case LF_PACKAGEOPTION:
397                         lex >> packageoption_;
398                         break;
399                 case LF_PREAMBLE:
400                         preamble_ = lex.getLongString("EndPreamble");
401                         break;
402                 case LF_PROVIDES: {
403                         lex.eatLine();
404                         string features = lex.getString();
405                         provides_ = getVectorFromString(features);
406                         break;
407                 }
408                 case LF_REQUIRES:
409                         lex >> requires_;
410                         break;
411                 case LF_SCALEOPTION:
412                         lex >> scaleoption_;
413                         break;
414                 case LF_SCOPTION:
415                         lex >> scoption_;
416                         break;
417                 case LF_SWITCHDEFAULT:
418                         lex >> switchdefault_;
419                         break;
420                 }
421         }
422         if (!finished) {
423                 lex.printError("No End tag found for LaTeXFont tag `$$Token'");
424                 return false;
425         }
426         lex.popTable();
427         return finished && !error;
428 }
429
430
431 bool LaTeXFont::read(Lexer & lex)
432 {
433         switchdefault_ = 0;
434         osfdefault_ = 0;
435
436         if (!lex.next()) {
437                 lex.printError("No name given for LaTeX font: `$$Token'.");
438                 return false;
439         }
440
441         name_ = lex.getDocString();
442         LYXERR(Debug::INFO, "Reading LaTeX font " << name_);
443         if (!readFont(lex)) {
444                 LYXERR0("Error parsing LaTeX font `" << name_ << '\'');
445                 return false;
446         }
447
448         return true;
449 }
450
451
452 void LaTeXFonts::readLaTeXFonts()
453 {
454         // Read latexfonts file
455         FileName filename = libFileSearch(string(), "latexfonts");
456         if (filename.empty()) {
457                 LYXERR0("Error: latexfonts file not found!");
458                 return;
459         }
460         Lexer lex;
461         lex.setFile(filename);
462         lex.setContext("LaTeXFeatures::readLaTeXFonts");
463         while (lex.isOK()) {
464                 int le = lex.lex();
465                 switch (le) {
466                 case Lexer::LEX_FEOF:
467                         continue;
468
469                 default:
470                         break;
471                 }
472                 string const type = lex.getString();
473                 if (type != "Font" && type != "AltFont") {
474                         lex.printError("Unknown LaTeXFont tag `$$Token'");
475                         continue;
476                 }
477                 LaTeXFont f;
478                 f.read(lex);
479                 if (!lex)
480                         break;
481
482                 if (type == "AltFont")
483                         texaltfontmap_[f.name()] = f;
484                 else
485                         texfontmap_[f.name()] = f;
486         }
487 }
488
489
490 LaTeXFonts::TexFontMap LaTeXFonts::getLaTeXFonts()
491 {
492         if (texfontmap_.empty())
493                 readLaTeXFonts();
494         return texfontmap_;
495 }
496
497
498 LaTeXFont LaTeXFonts::getLaTeXFont(docstring const & name)
499 {
500         if (name == "default" || name == "auto")
501                 return LaTeXFont();
502         if (texfontmap_.empty())
503                 readLaTeXFonts();
504         if (texfontmap_.find(name) == texfontmap_.end()) {
505                 LYXERR0("LaTeXFonts::getLaTeXFont: font '" << name << "' not found!");
506                 return LaTeXFont();
507         }
508         return texfontmap_[name];
509 }
510
511
512 LaTeXFont LaTeXFonts::getAltFont(docstring const & name)
513 {
514         if (name == "default" || name == "auto")
515                 return LaTeXFont();
516         if (texaltfontmap_.empty())
517                 readLaTeXFonts();
518         if (texaltfontmap_.find(name) == texaltfontmap_.end()) {
519                 LYXERR0("LaTeXFonts::getAltFont: alternative font '" << name << "' not found!");
520                 return LaTeXFont();
521         }
522         return texaltfontmap_[name];
523 }
524
525
526 } // namespace lyx