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