]> git.lyx.org Git - lyx.git/blob - src/LaTeXFonts.cpp
6f89cd113107456984a8c2e67f138d91c80f9c04
[lyx.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
82 string const LaTeXFont::getAvailablePackage(bool dryrun, bool ot1, bool complete, bool & alt)
83 {
84         if (ot1 && !ot1package_.empty()) {
85                 if (ot1package_ != "none"
86                     && (LaTeXFeatures::isAvailable(to_ascii(ot1package_)) || dryrun))
87                         return to_ascii(ot1package_);
88                 if (!dryrun && ot1package_ != "none")
89                         frontend::Alert::warning(_("Font not available"),
90                                         bformat(_("The LaTeX package `%1$s' needed for the font `%2$s'\n"
91                                                   "is not available on your system. LyX will fall back to the default font."),
92                                                 ot1package_, guiname_), true);
93                 return string();
94         }
95         if (family_ == "rm" && complete && !completepackage_.empty()) {
96                 if (LaTeXFeatures::isAvailable(to_ascii(completepackage_)) || dryrun)
97                         return to_ascii(completepackage_);
98         }
99         if (!package_.empty()) {
100                 if (!requires_.empty() && LaTeXFeatures::isAvailable(to_ascii(requires_)))
101                         return to_ascii(package_);
102                 if (LaTeXFeatures::isAvailable(to_ascii(package_)))
103                         return to_ascii(package_);
104                 else if (!altpackages_.empty()) {
105                         for (size_t i = 0; i < altpackages_.size(); ++i) {
106                                 if (LaTeXFeatures::isAvailable(to_ascii(altpackages_[i]))) {
107                                         alt = true;
108                                         return to_ascii(altpackages_[i]);
109                                 }
110                         }
111                 }
112                 // Output unavailable packages in source preview
113                 if (dryrun)
114                         return to_ascii(package_);
115                 docstring const req = requires_.empty() ? package_ : requires_;
116                         frontend::Alert::warning(_("Font not available"),
117                                         bformat(_("The LaTeX package `%1$s' needed for the font `%2$s'\n"
118                                                   "is not available on your system. LyX will fall back to the default font."),
119                                                 req, guiname_), true);
120         }
121         return string();
122 }
123
124
125 string const LaTeXFont::getPackageOptions(bool ot1, bool sc, bool osf, int scale)
126 {
127         if (ot1 && !ot1package_.empty())
128                 return string();
129
130         ostringstream os;
131         if (!packageoption_.empty())
132                 os << to_ascii(packageoption_);
133         if (sc && osf && providesOSF() && providesSC()) {
134                 if (!os.str().empty())
135                         os << ',';
136                 if (!osfscoption_.empty())
137                         os << to_ascii(osfscoption_);
138                 else
139                         os << to_ascii(osfoption_) << ',' << to_ascii(scoption_);
140         } else if (osf && providesOSF())
141                 os << to_ascii(osfoption_);
142         else if (sc && providesSC())
143                 os << to_ascii(scoption_);
144         if (scale != 100 && !scaleoption_.empty()) {
145                 if (!os.str().empty())
146                         os << ',';
147                 os << subst(to_ascii(scaleoption_), "$$val",
148                             convert<std::string>(float(scale) / 100));
149         }
150         return os.str();
151 }
152
153
154 string const LaTeXFont::getLaTeXCode(bool dryrun, bool ot1, bool complete, bool sc,
155                                      bool osf, int const & scale)
156 {
157         ostringstream os;
158
159         if (switchdefault_) {
160                 if (family_.empty()) {
161                         LYXERR0("Error: Font `" << name_ << "' has no family defined!");
162                         return string();
163                 }
164                 if (available(ot1) || dryrun)
165                         os << "\\renewcommand{\\" << to_ascii(family_) << "default}{"
166                         << to_ascii(name_) << "}\n";
167                 else
168                         frontend::Alert::warning(_("Font not available"),
169                                         bformat(_("The LaTeX package `%1$s' needed for the font `%2$s'\n"
170                                                   "is not available on your system. LyX will fall back to the default font."),
171                                                 requires_, guiname_), true);
172         } else {
173                 bool alt = false;
174                 string const package =
175                         getAvailablePackage(dryrun, ot1, complete, alt);
176                 // Package options are not for alternative packages
177                 string const packageopts = alt ? string() : getPackageOptions(ot1, sc, osf, scale);
178                 if (packageopts.empty() && !package.empty())
179                         os << "\\usepackage{" << package << "}\n";
180                 else if (!packageopts.empty() && !package.empty())
181                         os << "\\usepackage[" << packageopts << "]{" << package << "}\n";
182         }
183         if (osf && providesOSF(ot1) && !osfpackage_.empty())
184                 os << "\\usepackage{" << to_ascii(osfpackage_) << "}\n";
185
186         return os.str();
187 }
188
189
190 bool LaTeXFont::readFont(Lexer & lex)
191 {
192         enum LaTeXFontTags {
193                 LF_ALT_PACKAGES = 1,
194                 LF_COMPLETE_PACKAGE,
195                 LF_END,
196                 LF_FAMILY,
197                 LF_GUINAME,
198                 LF_OSFOPTION,
199                 LF_OSFPACKAGE,
200                 LF_OSFSCOPTION,
201                 LF_OT1_PACKAGE,
202                 LF_PACKAGE,
203                 LF_PACKAGEOPTION,
204                 LF_REQUIRES,
205                 LF_SCALEOPTION,
206                 LF_SCOPTION,
207                 LF_SWITCHDEFAULT
208         };
209
210         // Keep these sorted alphabetically!
211         LexerKeyword latexFontTags[] = {
212                 { "altpackages",          LF_ALT_PACKAGES },
213                 { "completepackage",      LF_COMPLETE_PACKAGE },
214                 { "endfont",              LF_END },
215                 { "family",               LF_FAMILY },
216                 { "guiname",              LF_GUINAME },
217                 { "osfoption",            LF_OSFOPTION },
218                 { "osfpackage",           LF_OSFPACKAGE },
219                 { "osfscoption",          LF_OSFSCOPTION },
220                 { "ot1package",           LF_OT1_PACKAGE },
221                 { "package",              LF_PACKAGE },
222                 { "packageoption",        LF_PACKAGEOPTION },
223                 { "requires",             LF_REQUIRES },
224                 { "scaleoption",          LF_SCALEOPTION },
225                 { "scoption",             LF_SCOPTION },
226                 { "switchdefault",        LF_SWITCHDEFAULT }
227         };
228
229         bool error = false;
230         bool finished = false;
231         lex.pushTable(latexFontTags);
232         // parse style section
233         while (!finished && lex.isOK() && !error) {
234                 int le = lex.lex();
235                 // See comment in LyXRC.cpp.
236                 switch (le) {
237                 case Lexer::LEX_FEOF:
238                         continue;
239
240                 case Lexer::LEX_UNDEF: // parse error
241                         lex.printError("Unknown LaTeXFont tag `$$Token'");
242                         error = true;
243                         continue;
244
245                 default: 
246                         break;
247                 }
248                 switch (static_cast<LaTeXFontTags>(le)) {
249                 case LF_END: // end of structure
250                         finished = true;
251                         break;
252                 case LF_ALT_PACKAGES: {
253                         docstring altp;
254                         lex >> altp;
255                         altpackages_ = getVectorFromString(altp);
256                         break;
257                 }
258                 case LF_COMPLETE_PACKAGE:
259                         lex >> completepackage_;
260                         break;
261                 case LF_FAMILY:
262                         lex >> family_;
263                         break;
264                 case LF_GUINAME:
265                         lex >> guiname_;
266                         break;
267                 case LF_OSFOPTION:
268                         lex >> osfoption_;
269                         break;
270                 case LF_OSFPACKAGE:
271                         lex >> osfpackage_;
272                         break;
273                 case LF_OSFSCOPTION:
274                         lex >> osfscoption_;
275                         break;
276                 case LF_OT1_PACKAGE:
277                         lex >> ot1package_;
278                         break;
279                 case LF_PACKAGE:
280                         lex >> package_;
281                         break;
282                 case LF_PACKAGEOPTION:
283                         lex >> packageoption_;
284                         break;
285                 case LF_REQUIRES:
286                         lex >> requires_;
287                         break;
288                 case LF_SCALEOPTION:
289                         lex >> scaleoption_;
290                         break;
291                 case LF_SCOPTION:
292                         lex >> scoption_;
293                         break;
294                 case LF_SWITCHDEFAULT:
295                         lex >> switchdefault_;
296                         break;
297                 }
298         }
299         if (!finished) {
300                 lex.printError("No End tag found for LaTeXFont tag `$$Token'");
301                 return false;
302         }
303         lex.popTable();
304         return finished && !error;
305 }
306
307
308 bool LaTeXFont::read(Lexer & lex)
309 {
310         switchdefault_ = 0;
311
312         if (!lex.next()) {
313                 lex.printError("No name given for LaTeX font: `$$Token'.");
314                 return false;
315         }
316
317         name_ = lex.getDocString();
318         LYXERR(Debug::INFO, "Reading LaTeX font " << name_);
319         if (!readFont(lex)) {
320                 LYXERR0("Error parsing LaTeX font `" << name_ << '\'');
321                 return false;
322         }
323
324         bool available = true;
325         if (!requires_.empty())
326                 available = LaTeXFeatures::isAvailable(to_ascii(requires_));
327         else if (!package_.empty()) {
328                 available = LaTeXFeatures::isAvailable(to_ascii(package_));
329                 if (!available && !altpackages_.empty()) {
330                         for (size_t i = 0; i < altpackages_.size(); ++i) {
331                                 available = LaTeXFeatures::isAvailable(to_ascii(altpackages_[i]));
332                                 if (available)
333                                         break;
334                         }
335                 }
336         }
337         available_ = available;
338
339         if (!ot1package_.empty() && ot1package_ != "none")
340                 available_ot1_ = LaTeXFeatures::isAvailable(to_ascii(ot1package_));
341         else
342                 available_ot1_ = available;
343
344         return true;
345 }
346
347
348 void LaTeXFonts::readLaTeXFonts()
349 {
350         // Read latexfonts file
351         FileName filename = libFileSearch(string(), "latexfonts");
352         if (filename.empty()) {
353                 LYXERR0("Error: latexfonts file not found!");
354                 return;
355         }
356         Lexer lex;
357         lex.setFile(filename);
358         lex.setContext("LaTeXFeatures::readLaTeXFonts");
359         while (lex.isOK()) {
360                 int le = lex.lex();
361                 switch (le) {
362                 case Lexer::LEX_FEOF:
363                         continue;
364
365                 default:
366                         break;
367                 }
368                 if (lex.getString() != "Font") {
369                         lex.printError("Unknown LaTeXFont tag `$$Token'");
370                         continue;
371                 }
372                 LaTeXFont f;
373                 f.read(lex);
374                 if (!lex)
375                         break;
376
377                 texfontmap_[f.name()] = f;
378         }
379 }
380
381
382 LaTeXFonts::TexFontMap LaTeXFonts::getLaTeXFonts()
383 {
384         if (texfontmap_.empty())
385                 readLaTeXFonts();
386         return texfontmap_;
387 }
388
389
390 LaTeXFont LaTeXFonts::getLaTeXFont(docstring const & name)
391 {
392         if (texfontmap_.empty())
393                 readLaTeXFonts();
394         return texfontmap_[name];
395 }
396
397
398 } // namespace lyx