]> git.lyx.org Git - lyx.git/blob - src/Font.cpp
Provide proper fallback if a bibliography processor is not found
[lyx.git] / src / Font.cpp
1 /**
2  * \file src/Font.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Lars Gullik Bjønnes
7  * \author Jean-Marc Lasgouttes
8  * \author Angus Leeming
9  * \author André Pönitz
10  * \author Dekel Tsur
11  *
12  * Full author contact details are available in file CREDITS.
13  */
14
15 #include <config.h>
16
17 #include "Font.h"
18
19 #include "BufferParams.h" // stateText
20 #include "ColorSet.h"
21 #include "Encoding.h"
22 #include "Language.h"
23 #include "LaTeXFeatures.h"
24 #include "Lexer.h"
25 #include "LyXRC.h"
26 #include "output_latex.h"
27 #include "OutputParams.h"
28 #include "texstream.h"
29
30 #include "support/lassert.h"
31 #include "support/convert.h"
32 #include "support/debug.h"
33 #include "support/gettext.h"
34 #include "support/lstrings.h"
35
36 #include <cstring>
37
38 using namespace std;
39 using namespace lyx::support;
40
41 namespace lyx {
42
43 //
44 // Strings used to read and write .lyx format files
45 //
46 // These are defined in FontInfo.cpp
47 extern char const * LyXFamilyNames[NUM_FAMILIES + 2];
48 extern char const * LyXSeriesNames[NUM_SERIES + 2];
49 extern char const * LyXShapeNames[NUM_SHAPE + 2];
50 extern char const * LyXSizeNames[NUM_SIZE + 4];
51 extern char const * LyXMiscNames[5];
52
53 //
54 // Names for the GUI
55 //
56
57 namespace {
58
59 char const * GUIFamilyNames[NUM_FAMILIES + 2 /* default & error */] =
60 { N_("Roman"), N_("Sans Serif"), N_("Typewriter"), N_("Symbol"),
61   "cmr", "cmsy", "cmm", "cmex", "msa", "msb", "ds", "eufrak", "rsfs", "stmry",
62   "wasy", "esint", N_("Inherit"), N_("Ignore") };
63
64 char const * GUISeriesNames[NUM_SERIES + 2 /* default & error */] =
65 { N_("Medium"), N_("Bold"), N_("Inherit"), N_("Ignore") };
66
67 char const * GUIShapeNames[NUM_SHAPE + 2 /* default & error */] =
68 { N_("Upright"), N_("Italic"), N_("Slanted"), N_("Smallcaps"), N_("Inherit"),
69   N_("Ignore") };
70
71 char const * GUISizeNames[NUM_SIZE + 4 /* increase, decrease, default & error */] =
72 { N_("Tiny"), N_("Smallest"), N_("Smaller"), N_("Small"), N_("Normal"), N_("Large"),
73   N_("Larger"), N_("Largest"), N_("Huge"), N_("Huger"), N_("Increase"), N_("Decrease"),
74   N_("Inherit"), N_("Ignore") };
75
76 char const * GUIMiscNames[5] =
77 { N_("Off"), N_("On"), N_("Toggle"), N_("Inherit"), N_("Ignore") };
78
79 //
80 // Strings used to write LaTeX files
81 //
82 char const * LaTeXFamilyNames[NUM_FAMILIES + 2] =
83 { "textrm", "textsf", "texttt", "error1", "error2", "error3", "error4",
84   "error5", "error6", "error7", "error8", "error9", "error10", "error11",
85   "error12", "error13", "error14" };
86
87 char const * LaTeXSeriesNames[NUM_SERIES + 2] =
88 { "textmd", "textbf", "error4", "error5" };
89
90 char const * LaTeXShapeNames[NUM_SHAPE + 2] =
91 { "textup", "textit", "textsl", "textsc", "error6", "error7" };
92
93 char const * LaTeXSizeNames[NUM_SIZE + 4] =
94 { "tiny", "scriptsize", "footnotesize", "small", "normalsize", "large",
95   "Large", "LARGE", "huge", "Huge", "error8", "error9", "error10", "error11" };
96
97 } // namespace
98
99
100 Font::Font(FontInfo bits, Language const * l)
101         : bits_(bits), lang_(l), open_encoding_(false)
102 {
103         if (!lang_)
104                 lang_ = default_language;
105 }
106
107
108 bool Font::isRightToLeft() const
109 {
110         return lang_->rightToLeft();
111 }
112
113
114 bool Font::isVisibleRightToLeft() const
115 {
116         return (lang_->rightToLeft() &&
117                 bits_.number() != FONT_ON);
118 }
119
120
121 void Font::setLanguage(Language const * l)
122 {
123         lang_ = l;
124 }
125
126
127 /// Updates font settings according to request
128 void Font::update(Font const & newfont,
129                      Language const * document_language,
130                      bool toggleall)
131 {
132         bits_.update(newfont.fontInfo(), toggleall);
133
134         if (newfont.language() == language() && toggleall)
135                 if (language() == document_language)
136                         setLanguage(default_language);
137                 else
138                         setLanguage(document_language);
139         else if (newfont.language() == reset_language)
140                 setLanguage(document_language);
141         else if (newfont.language() != ignore_language)
142                 setLanguage(newfont.language());
143 }
144
145
146 docstring const stateText(FontInfo const & f)
147 {
148         odocstringstream os;
149         if (f.family() != INHERIT_FAMILY)
150                 os << _(GUIFamilyNames[f.family()]) << ", ";
151         if (f.series() != INHERIT_SERIES)
152                 os << _(GUISeriesNames[f.series()]) << ", ";
153         if (f.shape() != INHERIT_SHAPE)
154                 os << _(GUIShapeNames[f.shape()]) << ", ";
155         if (f.size() != FONT_SIZE_INHERIT)
156                 os << _(GUISizeNames[f.size()]) << ", ";
157         if (f.color() != Color_inherit)
158                 os << lcolor.getGUIName(f.color()) << ", ";
159         // FIXME: uncomment this when we support background.
160         //if (f.background() != Color_inherit)
161         //      os << lcolor.getGUIName(f.background()) << ", ";
162         if (f.emph() != FONT_INHERIT)
163                 os << bformat(_("Emphasis %1$s, "),
164                               _(GUIMiscNames[f.emph()]));
165         if (f.underbar() != FONT_INHERIT)
166                 os << bformat(_("Underline %1$s, "),
167                               _(GUIMiscNames[f.underbar()]));
168         if (f.strikeout() != FONT_INHERIT)
169                 os << bformat(_("Strike out %1$s, "),
170                               _(GUIMiscNames[f.strikeout()]));
171         if (f.xout() != FONT_INHERIT)
172                 os << bformat(_("Cross out %1$s, "),
173                               _(GUIMiscNames[f.xout()]));
174         if (f.uuline() != FONT_INHERIT)
175                 os << bformat(_("Double underline %1$s, "),
176                               _(GUIMiscNames[f.uuline()]));
177         if (f.uwave() != FONT_INHERIT)
178                 os << bformat(_("Wavy underline %1$s, "),
179                               _(GUIMiscNames[f.uwave()]));
180         if (f.noun() != FONT_INHERIT)
181                 os << bformat(_("Noun %1$s, "),
182                               _(GUIMiscNames[f.noun()]));
183         if (f == inherit_font)
184                 os << _("Default") << ", ";
185
186         return os.str();
187 }
188
189
190 docstring const Font::stateText(BufferParams * params) const
191 {
192         odocstringstream os;
193         os << lyx::stateText(bits_);
194         if (!params || (language() != params->language))
195                 os << bformat(_("Language: %1$s, "),
196                               _(language()->display()));
197         if (bits_.number() != FONT_OFF)
198                 os << "  " << bformat(_("Number %1$s"),
199                               _(GUIMiscNames[bits_.number()]));
200         return rtrim(os.str(), ", ");
201 }
202
203
204 // Returns size in latex format
205 string const Font::latexSize() const
206 {
207         return LaTeXSizeNames[bits_.size()];
208 }
209
210
211 /// Writes the changes from this font to orgfont in .lyx format in file
212 void Font::lyxWriteChanges(Font const & orgfont,
213                               ostream & os) const
214 {
215         os << "\n";
216         if (orgfont.fontInfo().family() != bits_.family())
217                 os << "\\family " << LyXFamilyNames[bits_.family()] << "\n";
218         if (orgfont.fontInfo().series() != bits_.series())
219                 os << "\\series " << LyXSeriesNames[bits_.series()] << "\n";
220         if (orgfont.fontInfo().shape() != bits_.shape())
221                 os << "\\shape " << LyXShapeNames[bits_.shape()] << "\n";
222         if (orgfont.fontInfo().size() != bits_.size())
223                 os << "\\size " << LyXSizeNames[bits_.size()] << "\n";
224         if (orgfont.fontInfo().emph() != bits_.emph())
225                 os << "\\emph " << LyXMiscNames[bits_.emph()] << "\n";
226         if (orgfont.fontInfo().number() != bits_.number())
227                 os << "\\numeric " << LyXMiscNames[bits_.number()] << "\n";
228         if (orgfont.fontInfo().underbar() != bits_.underbar()) {
229                 // This is only for backwards compatibility
230                 switch (bits_.underbar()) {
231                 case FONT_OFF:  os << "\\bar no\n"; break;
232                 case FONT_ON:        os << "\\bar under\n"; break;
233                 case FONT_TOGGLE:       lyxerr << "Font::lyxWriteFontChanges: "
234                                         "FONT_TOGGLE should not appear here!"
235                                        << endl;
236                 break;
237                 case FONT_INHERIT:   os << "\\bar default\n"; break;
238                 case FONT_IGNORE:    lyxerr << "Font::lyxWriteFontChanges: "
239                                         "IGNORE should not appear here!"
240                                        << endl;
241                 break;
242                 }
243         }
244         if (orgfont.fontInfo().strikeout() != bits_.strikeout()) {
245                 os << "\\strikeout " << LyXMiscNames[bits_.strikeout()] << "\n";
246         }
247         if (orgfont.fontInfo().xout() != bits_.xout()) {
248                 os << "\\xout " << LyXMiscNames[bits_.xout()] << "\n";
249         }
250         if (orgfont.fontInfo().uuline() != bits_.uuline()) {
251                 os << "\\uuline " << LyXMiscNames[bits_.uuline()] << "\n";
252         }
253         if (orgfont.fontInfo().uwave() != bits_.uwave()) {
254                 os << "\\uwave " << LyXMiscNames[bits_.uwave()] << "\n";
255         }
256         if (orgfont.fontInfo().noun() != bits_.noun()) {
257                 os << "\\noun " << LyXMiscNames[bits_.noun()] << "\n";
258         }
259         if (orgfont.fontInfo().color() != bits_.color())
260                 os << "\\color " << lcolor.getLyXName(bits_.color()) << '\n';
261         // FIXME: uncomment this when we support background.
262         //if (orgfont.fontInfo().background() != bits_.background())
263         //      os << "\\color " << lcolor.getLyXName(bits_.background()) << '\n';
264         if (orgfont.language() != language() &&
265             language() != latex_language) {
266                 if (language())
267                         os << "\\lang " << language()->lang() << "\n";
268                 else
269                         os << "\\lang unknown\n";
270         }
271 }
272
273
274 /// Writes the head of the LaTeX needed to impose this font
275 // Returns number of chars written.
276 int Font::latexWriteStartChanges(odocstream & os, BufferParams const & bparams,
277                                     OutputParams const & runparams,
278                                     Font const & base,
279                                     Font const & prev) const
280 {
281         bool env = false;
282
283         int count = 0;
284
285         // polyglossia or babel?
286         if (runparams.use_polyglossia
287             && language()->lang() != base.language()->lang()
288             && language() != prev.language()) {
289                 if (!language()->polyglossia().empty()) {
290                         string tmp = "\\text" + language()->polyglossia();
291                         if (!language()->polyglossiaOpts().empty()) {
292                                 tmp += "[" + language()->polyglossiaOpts() + "]";
293                                 if (runparams.use_hyperref && runparams.moving_arg)
294                                         // We need to strip the command for
295                                         // the pdf string, see #11813
296                                         tmp = "\\texorpdfstring{" + tmp + "}{}";
297                         }
298                         tmp += "{";
299                         os << from_ascii(tmp);
300                         count += tmp.length();
301                         pushLanguageName(language()->polyglossia(), true);
302                 } else if (language()->encoding()->package() != Encoding::CJK) {
303                         os << '{';
304                         count += 1;
305                 }
306         } else if (language()->babel() != base.language()->babel() &&
307             language() != prev.language()) {
308                 if (language()->lang() == "farsi") {
309                         os << "\\textFR{";
310                         count += 8;
311                 } else if (!isRightToLeft() &&
312                             base.language()->lang() == "farsi") {
313                         os << "\\textLR{";
314                         count += 8;
315                 } else if (language()->lang() == "arabic_arabi") {
316                         os << "\\textAR{";
317                         count += 8;
318                 } else if (!isRightToLeft() &&
319                                 base.language()->lang() == "arabic_arabi") {
320                         os << "\\textLR{";
321                         count += 8;
322                 // currently the remaining RTL languages are arabic_arabtex and hebrew
323                 } else if (isRightToLeft() != prev.isRightToLeft()) {
324                         if (isRightToLeft()) {
325                                 os << "\\R{";
326                                 count += 3;
327                         } else {
328                                 os << "\\L{";
329                                 count += 3;
330                         }
331                 } else if (!language()->babel().empty()) {
332                         string const tmp =
333                                 subst(lyxrc.language_command_local,
334                                       "$$lang", language()->babel());
335                         os << from_ascii(tmp);
336                         count += tmp.length();
337                         if (!lyxrc.language_command_end.empty())
338                                 pushLanguageName(language()->babel(), true);
339                 } else if (language()->encoding()->package() != Encoding::CJK) {
340                         os << '{';
341                         count += 1;
342                 }
343         }
344
345         if (language()->encoding()->package() == Encoding::CJK) {
346                 pair<bool, int> const c = switchEncoding(os, bparams,
347                                 runparams, *(language()->encoding()));
348                 if (c.first) {
349                         open_encoding_ = true;
350                         count += c.second;
351                         runparams.encoding = language()->encoding();
352                 }
353         }
354
355         FontInfo f = bits_;
356         f.reduce(base.bits_);
357         FontInfo p = bits_;
358         p.reduce(prev.bits_);
359
360         if (f.family() != INHERIT_FAMILY) {
361                 os << '\\'
362                    << LaTeXFamilyNames[f.family()]
363                    << '{';
364                 count += strlen(LaTeXFamilyNames[f.family()]) + 2;
365                 env = true; //We have opened a new environment
366         }
367         if (f.series() != INHERIT_SERIES) {
368                 os << '\\'
369                    << LaTeXSeriesNames[f.series()]
370                    << '{';
371                 count += strlen(LaTeXSeriesNames[f.series()]) + 2;
372                 env = true; //We have opened a new environment
373         }
374         if (f.shape() != INHERIT_SHAPE) {
375                 os << '\\'
376                    << LaTeXShapeNames[f.shape()]
377                    << '{';
378                 count += strlen(LaTeXShapeNames[f.shape()]) + 2;
379                 env = true; //We have opened a new environment
380         }
381         if (f.color() != Color_inherit && f.color() != Color_ignore) {
382                 if (f.color() == Color_none && p.color() != Color_none) {
383                         // Color none: Close previous color, if any
384                         os << '}';
385                         ++count;
386                 } else if (f.color() != Color_none) {
387                         os << "\\textcolor{"
388                            << from_ascii(lcolor.getLaTeXName(f.color()))
389                            << "}{";
390                         count += lcolor.getLaTeXName(f.color()).length() + 13;
391                 }
392                 env = true; //We have opened a new environment
393         }
394         // FIXME: uncomment this when we support background.
395         /*
396         if (f.background() != Color_inherit && f.background() != Color_ignore) {
397                 os << "\\textcolor{"
398                    << from_ascii(lcolor.getLaTeXName(f.background()))
399                    << "}{";
400                 count += lcolor.getLaTeXName(f.background()).length() + 13;
401                 env = true; //We have opened a new environment
402         }
403         */
404         // If the current language is Hebrew, Arabic, or Farsi
405         // the numbers are written Left-to-Right. ArabTeX package
406         // and bidi (polyglossia with XeTeX) reorder the number automatically
407         // but the packages used for Hebrew and Farsi (Arabi) do not.
408         if (!(runparams.use_polyglossia && runparams.flavor == OutputParams::XETEX)
409             && !runparams.pass_thru
410             && bits_.number() == FONT_ON
411             && prev.fontInfo().number() != FONT_ON
412             && (language()->lang() == "hebrew"
413                 || language()->lang() == "farsi"
414                 || language()->lang() == "arabic_arabi")) {
415                 if (runparams.use_polyglossia) {
416                         // LuaTeX/luabidi
417                         os << "\\LR{";
418                         count += 5;
419                 } else {
420                         os << "{\\beginL ";
421                         count += 9;
422                 }
423         }
424         if (f.emph() == FONT_ON) {
425                 os << "\\emph{";
426                 count += 6;
427                 env = true; //We have opened a new environment
428         }
429         // \noun{} is a LyX special macro
430         if (f.noun() == FONT_ON) {
431                 os << "\\noun{";
432                 count += 6;
433                 env = true; //We have opened a new environment
434         }
435         if (f.size() != FONT_SIZE_INHERIT) {
436                 // If we didn't open an environment above, we open one here
437                 if (!env) {
438                         os << '{';
439                         ++count;
440                 }
441                 os << '\\'
442                    << LaTeXSizeNames[f.size()]
443                    << "{}";
444                 count += strlen(LaTeXSizeNames[f.size()]) + 3;
445         }
446         // The ulem commands need to be on the deepest nesting level
447         // because ulem puts every nested group or macro in a box,
448         // which prevents linebreaks (#8424, #8733)
449         if (f.underbar() == FONT_ON) {
450                 os << "\\uline{";
451                 count += 10;
452                 ++runparams.inulemcmd;
453         }
454         if (f.uuline() == FONT_ON) {
455                 os << "\\uuline{";
456                 count += 11;
457                 ++runparams.inulemcmd;
458         }
459         if (f.strikeout() == FONT_ON) {
460                 os << "\\sout{";
461                 count += 9;
462                 ++runparams.inulemcmd;
463         }
464         if (f.xout() == FONT_ON) {
465                 os << "\\xout{";
466                 count += 9;
467                 ++runparams.inulemcmd;
468         }
469         if (f.uwave() == FONT_ON) {
470                 if (runparams.inulemcmd) {
471                         // needed with nested uwave in xout
472                         // see https://tex.stackexchange.com/a/263042
473                         os << "\\ULdepth=1000pt";
474                         count += 15;
475                 }
476                 os << "\\uwave{";
477                 count += 10;
478                 ++runparams.inulemcmd;
479         }
480         return count;
481 }
482
483
484 /// Writes ending block of LaTeX needed to close use of this font
485 // Returns number of chars written
486 // This one corresponds to latexWriteStartChanges(). (Asger)
487 int Font::latexWriteEndChanges(otexstream & os, BufferParams const & bparams,
488                                   OutputParams const & runparams,
489                                   Font const & base,
490                                   Font const & next,
491                                   bool & needPar,
492                                   bool const & closeLanguage) const
493 {
494         int count = 0;
495         bool env = false;
496
497         // reduce the current font to changes against the base
498         // font (of the layout). We use a temporary for this to
499         // avoid changing this font instance, as that would break
500         FontInfo f = bits_;
501         f.reduce(base.bits_);
502
503         if (f.family() != INHERIT_FAMILY) {
504                 os << '}';
505                 ++count;
506                 env = true; // Size change need not bother about closing env.
507         }
508         if (f.series() != INHERIT_SERIES) {
509                 os << '}';
510                 ++count;
511                 env = true; // Size change need not bother about closing env.
512         }
513         if (f.shape() != INHERIT_SHAPE) {
514                 os << '}';
515                 ++count;
516                 env = true; // Size change need not bother about closing env.
517         }
518         if (f.color() != Color_inherit && f.color() != Color_ignore && f.color() != Color_none) {
519                 os << '}';
520                 ++count;
521                 env = true; // Size change need not bother about closing env.
522         }
523         if (f.emph() == FONT_ON) {
524                 os << '}';
525                 ++count;
526                 env = true; // Size change need not bother about closing env.
527         }
528         if (f.noun() == FONT_ON) {
529                 os << '}';
530                 ++count;
531                 env = true; // Size change need not bother about closing env.
532         }
533         if (f.size() != FONT_SIZE_INHERIT) {
534                 // We only have to close if only size changed
535                 if (!env) {
536                         if (needPar && !closeLanguage) {
537                                 os << "\\par";
538                                 count += 4;
539                                 needPar = false;
540                         }
541                         os << '}';
542                         ++count;
543                 }
544         }
545         if (f.underbar() == FONT_ON) {
546                 os << '}';
547                 ++count;
548                 --runparams.inulemcmd;
549         }
550         if (f.strikeout() == FONT_ON) {
551                 os << '}';
552                 ++count;
553                 --runparams.inulemcmd;
554         }
555         if (f.xout() == FONT_ON) {
556                 os << '}';
557                 ++count;
558                 --runparams.inulemcmd;
559         }
560         if (f.uuline() == FONT_ON) {
561                 os << '}';
562                 ++count;
563                 --runparams.inulemcmd;
564         }
565         if (f.uwave() == FONT_ON) {
566                 os << '}';
567                 ++count;
568                 --runparams.inulemcmd;
569         }
570
571         // If the current language is Hebrew, Arabic, or Farsi
572         // the numbers are written Left-to-Right. ArabTeX package
573         // and bidi (polyglossia with XeTeX) reorder the number automatically
574         // but the packages used for Hebrew and Farsi (Arabi) do not.
575         if (!(runparams.use_polyglossia && runparams.flavor == OutputParams::XETEX)
576             && !runparams.pass_thru
577             && bits_.number() == FONT_ON
578             && next.fontInfo().number() != FONT_ON
579             && (language()->lang() == "hebrew"
580                 || language()->lang() == "farsi"
581                 || language()->lang() == "arabic_arabi")) {
582                 if (runparams.use_polyglossia) {
583                         // LuaTeX/luabidi
584                         os << "}";
585                         count += 1;
586                 } else {
587                         os << "\\endL}";
588                         count += 6;
589                 }
590         }
591
592         if (open_encoding_) {
593                 // We need to close the encoding even if it does not change
594                 // to do correct environment nesting
595                 Encoding const * const ascii = encodings.fromLyXName("ascii");
596                 pair<bool, int> const c = switchEncoding(os.os(), bparams,
597                                 runparams, *ascii);
598                 LATTEST(c.first);
599                 count += c.second;
600                 runparams.encoding = ascii;
601                 open_encoding_ = false;
602         }
603
604         if (closeLanguage
605             && language() != base.language() && language() != next.language()
606             && language()->encoding()->package() != Encoding::CJK) {
607                 os << '}';
608                 ++count;
609                 bool const using_begin_end =
610                         runparams.use_polyglossia ||
611                                 !lyxrc.language_command_end.empty();
612                 if (using_begin_end)
613                         popLanguageName();
614         }
615
616         return count;
617 }
618
619
620 string Font::toString(bool const toggle) const
621 {
622         string const lang = (language() == reset_language)
623                 ? "reset" : language()->lang();
624
625         ostringstream os;
626         os << "family " << bits_.family() << '\n'
627            << "series " << bits_.series() << '\n'
628            << "shape " << bits_.shape() << '\n'
629            << "size " << bits_.size() << '\n'
630            << "emph " << bits_.emph() << '\n'
631            << "underbar " << bits_.underbar() << '\n'
632            << "strikeout " << bits_.strikeout() << '\n'
633            << "xout " << bits_.xout() << '\n'
634            << "uuline " << bits_.uuline() << '\n'
635            << "uwave " << bits_.uwave() << '\n'
636            << "noun " << bits_.noun() << '\n'
637            << "number " << bits_.number() << '\n'
638            << "color " << bits_.color() << '\n'
639            << "language " << lang << '\n'
640            << "toggleall " << convert<string>(toggle);
641         return os.str();
642 }
643
644
645 bool Font::fromString(string const & data, bool & toggle)
646 {
647         istringstream is(data);
648         Lexer lex;
649         lex.setStream(is);
650
651         int nset = 0;
652         while (lex.isOK()) {
653                 string token;
654                 if (lex.next())
655                         token = lex.getString();
656
657                 if (token.empty() || !lex.next())
658                         break;
659
660                 if (token == "family") {
661                         int const next = lex.getInteger();
662                         bits_.setFamily(FontFamily(next));
663
664                 } else if (token == "series") {
665                         int const next = lex.getInteger();
666                         bits_.setSeries(FontSeries(next));
667
668                 } else if (token == "shape") {
669                         int const next = lex.getInteger();
670                         bits_.setShape(FontShape(next));
671
672                 } else if (token == "size") {
673                         int const next = lex.getInteger();
674                         bits_.setSize(FontSize(next));
675
676                 } else if (token == "emph" || token == "underbar"
677                         || token == "noun" || token == "number"
678                         || token == "uuline" || token == "uwave"
679                         || token == "strikeout" || token == "xout") {
680
681                         int const next = lex.getInteger();
682                         FontState const misc = FontState(next);
683
684                         if (token == "emph")
685                                 bits_.setEmph(misc);
686                         else if (token == "underbar")
687                                 bits_.setUnderbar(misc);
688                         else if (token == "strikeout")
689                                 bits_.setStrikeout(misc);
690                         else if (token == "xout")
691                                 bits_.setXout(misc);
692                         else if (token == "uuline")
693                                 bits_.setUuline(misc);
694                         else if (token == "uwave")
695                                 bits_.setUwave(misc);
696                         else if (token == "noun")
697                                 bits_.setNoun(misc);
698                         else if (token == "number")
699                                 bits_.setNumber(misc);
700
701                 } else if (token == "color") {
702                         int const next = lex.getInteger();
703                         bits_.setColor(ColorCode(next));
704
705                 /**
706                 } else if (token == "background") {
707                         int const next = lex.getInteger();
708                         bits_.setBackground(ColorCode(next));
709                 */
710
711                 } else if (token == "language") {
712                         string const next = lex.getString();
713                         setLanguage(languages.getLanguage(next));
714
715                 } else if (token == "toggleall") {
716                         toggle = lex.getBool();
717
718                 } else {
719                         // Unrecognised token
720                         break;
721                 }
722
723                 ++nset;
724         }
725         return (nset > 0);
726 }
727
728
729 void Font::validate(LaTeXFeatures & features) const
730 {
731         BufferParams const & bparams = features.bufferParams();
732         Language const * doc_language = bparams.language;
733
734         if (bits_.noun() == FONT_ON) {
735                 LYXERR(Debug::LATEX, "font.noun: " << bits_.noun());
736                 features.require("noun");
737                 LYXERR(Debug::LATEX, "Noun enabled. Font: " << to_utf8(stateText(0)));
738         }
739         if (bits_.underbar() == FONT_ON) {
740                 LYXERR(Debug::LATEX, "font.underline: " << bits_.underbar());
741                 features.require("ulem");
742                 LYXERR(Debug::LATEX, "Underline enabled. Font: " << to_utf8(stateText(0)));
743         }
744         if (bits_.strikeout() == FONT_ON) {
745                 LYXERR(Debug::LATEX, "font.strikeout: " << bits_.strikeout());
746                 features.require("ulem");
747                 LYXERR(Debug::LATEX, "Strike out enabled. Font: " << to_utf8(stateText(0)));
748         }
749         if (bits_.xout() == FONT_ON) {
750                 LYXERR(Debug::LATEX, "font.xout: " << bits_.xout());
751                 features.require("ulem");
752                 LYXERR(Debug::LATEX, "Cross out enabled. Font: " << to_utf8(stateText(0)));
753         }
754         if (bits_.uuline() == FONT_ON) {
755                 LYXERR(Debug::LATEX, "font.uuline: " << bits_.uuline());
756                 features.require("ulem");
757                 LYXERR(Debug::LATEX, "Double underline enabled. Font: " << to_utf8(stateText(0)));
758         }
759         if (bits_.uwave() == FONT_ON) {
760                 LYXERR(Debug::LATEX, "font.uwave: " << bits_.uwave());
761                 features.require("ulem");
762                 LYXERR(Debug::LATEX, "Wavy underline enabled. Font: " << to_utf8(stateText(0)));
763         }
764         switch (bits_.color()) {
765                 case Color_none:
766                 case Color_inherit:
767                 case Color_ignore:
768                         // probably we should put here all interface colors used for
769                         // font displaying! For now I just add this ones I know of (Jug)
770                 case Color_latex:
771                 case Color_notelabel:
772                         break;
773                 case Color_brown:
774                 case Color_darkgray:
775                 case Color_gray:
776                 case Color_lightgray:
777                 case Color_lime:
778                 case Color_olive:
779                 case Color_orange:
780                 case Color_pink:
781                 case Color_purple:
782                 case Color_teal:
783                 case Color_violet:
784                         features.require("xcolor");
785                         break;
786                 default:
787                         features.require("color");
788                         LYXERR(Debug::LATEX, "Color enabled. Font: " << to_utf8(stateText(0)));
789         }
790
791         // FIXME: Do something for background and soul package?
792
793         if (((features.usePolyglossia() && lang_->polyglossia() != doc_language->polyglossia())
794              || (features.useBabel() && lang_->babel() != doc_language->babel())
795              || (doc_language->encoding()->package() == Encoding::CJK && lang_ != doc_language))
796             && lang_ != ignore_language
797             && lang_ != latex_language)
798         {
799                 features.useLanguage(lang_);
800                 LYXERR(Debug::LATEX, "Found language " << lang_->lang());
801         }
802 }
803
804
805 ostream & operator<<(ostream & os, FontState fms)
806 {
807         return os << int(fms);
808 }
809
810
811 ostream & operator<<(ostream & os, FontInfo const & f)
812 {
813         return os << "font:"
814                 << " family " << f.family()
815                 << " series " << f.series()
816                 << " shape " << f.shape()
817                 << " size " << f.size()
818                 << " color " << f.color()
819                 // FIXME: uncomment this when we support background.
820                 //<< " background " << f.background()
821                 << " emph " << f.emph()
822                 << " underbar " << f.underbar()
823                 << " strikeout " << f.strikeout()
824                 << " xout " << f.xout()
825                 << " uuline " << f.uuline()
826                 << " uwave " << f.uwave()
827                 << " noun " << f.noun()
828                 << " number " << f.number();
829 }
830
831
832 ostream & operator<<(ostream & os, Font const & font)
833 {
834         return os << font.bits_
835                 << " lang: " << (font.lang_ ? font.lang_->lang() : 0);
836 }
837
838
839 } // namespace lyx