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