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