]> git.lyx.org Git - features.git/blob - src/Font.cpp
Handle null pointer
[features.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", "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" };
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                 // reset_language is a null pointer!
196                 os << bformat(_("Language: %1$s, "),
197                               (language() == reset_language) ? _("Default")
198                                                              : _(language()->display()));
199         }
200         if (bits_.number() != FONT_OFF)
201                 os << "  " << bformat(_("Number %1$s"),
202                               _(GUIMiscNames[bits_.number()]));
203         return rtrim(os.str(), ", ");
204 }
205
206
207 // Returns size in latex format
208 string const Font::latexSize() const
209 {
210         return LaTeXSizeNames[bits_.size()];
211 }
212
213
214 /// Writes the changes from this font to orgfont in .lyx format in file
215 void Font::lyxWriteChanges(Font const & orgfont,
216                               ostream & os) const
217 {
218         os << "\n";
219         if (orgfont.fontInfo().family() != bits_.family())
220                 os << "\\family " << LyXFamilyNames[bits_.family()] << "\n";
221         if (orgfont.fontInfo().series() != bits_.series())
222                 os << "\\series " << LyXSeriesNames[bits_.series()] << "\n";
223         if (orgfont.fontInfo().shape() != bits_.shape())
224                 os << "\\shape " << LyXShapeNames[bits_.shape()] << "\n";
225         if (orgfont.fontInfo().size() != bits_.size())
226                 os << "\\size " << LyXSizeNames[bits_.size()] << "\n";
227         if (orgfont.fontInfo().emph() != bits_.emph())
228                 os << "\\emph " << LyXMiscNames[bits_.emph()] << "\n";
229         if (orgfont.fontInfo().number() != bits_.number())
230                 os << "\\numeric " << LyXMiscNames[bits_.number()] << "\n";
231         if (orgfont.fontInfo().nospellcheck() != bits_.nospellcheck())
232                 os << "\\nospellcheck " << LyXMiscNames[bits_.nospellcheck()] << "\n";
233         if (orgfont.fontInfo().underbar() != bits_.underbar()) {
234                 // This is only for backwards compatibility
235                 switch (bits_.underbar()) {
236                 case FONT_OFF:  os << "\\bar no\n"; break;
237                 case FONT_ON:        os << "\\bar under\n"; break;
238                 case FONT_TOGGLE:       lyxerr << "Font::lyxWriteFontChanges: "
239                                         "FONT_TOGGLE should not appear here!"
240                                        << endl;
241                 break;
242                 case FONT_INHERIT:   os << "\\bar default\n"; break;
243                 case FONT_IGNORE:    lyxerr << "Font::lyxWriteFontChanges: "
244                                         "IGNORE should not appear here!"
245                                        << endl;
246                 break;
247                 }
248         }
249         if (orgfont.fontInfo().strikeout() != bits_.strikeout()) {
250                 os << "\\strikeout " << LyXMiscNames[bits_.strikeout()] << "\n";
251         }
252         if (orgfont.fontInfo().xout() != bits_.xout()) {
253                 os << "\\xout " << LyXMiscNames[bits_.xout()] << "\n";
254         }
255         if (orgfont.fontInfo().uuline() != bits_.uuline()) {
256                 os << "\\uuline " << LyXMiscNames[bits_.uuline()] << "\n";
257         }
258         if (orgfont.fontInfo().uwave() != bits_.uwave()) {
259                 os << "\\uwave " << LyXMiscNames[bits_.uwave()] << "\n";
260         }
261         if (orgfont.fontInfo().noun() != bits_.noun()) {
262                 os << "\\noun " << LyXMiscNames[bits_.noun()] << "\n";
263         }
264         if (orgfont.fontInfo().color() != bits_.color())
265                 os << "\\color " << lcolor.getLyXName(bits_.color()) << '\n';
266         // FIXME: uncomment this when we support background.
267         //if (orgfont.fontInfo().background() != bits_.background())
268         //      os << "\\color " << lcolor.getLyXName(bits_.background()) << '\n';
269         if (orgfont.language() != language() &&
270             language() != latex_language) {
271                 if (language())
272                         os << "\\lang " << language()->lang() << "\n";
273                 else
274                         os << "\\lang unknown\n";
275         }
276 }
277
278
279 /// Writes the head of the LaTeX needed to impose this font
280 // Returns number of chars written.
281 int Font::latexWriteStartChanges(odocstream & os, BufferParams const & bparams,
282                                     OutputParams const & runparams,
283                                     Font const & base,
284                                     Font const & prev) const
285 {
286         bool env = false;
287
288         int count = 0;
289
290         // polyglossia or babel?
291         if (runparams.use_polyglossia
292             && language()->lang() != base.language()->lang()
293             && language() != prev.language()) {
294                 if (!language()->polyglossia().empty()) {
295                         string tmp = "\\text" + language()->polyglossia();
296                         if (!language()->polyglossiaOpts().empty())
297                                 tmp += "[" + language()->polyglossiaOpts() + "]";
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) reorder the number automatically
407         // but the packages used for Hebrew and Farsi (Arabi) do not.
408         if (!runparams.use_polyglossia
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                 os << "{\\beginL ";
416                 count += 9;
417         }
418         if (f.emph() == FONT_ON) {
419                 os << "\\emph{";
420                 count += 6;
421                 env = true; //We have opened a new environment
422         }
423         // \noun{} is a LyX special macro
424         if (f.noun() == FONT_ON) {
425                 os << "\\noun{";
426                 count += 6;
427                 env = true; //We have opened a new environment
428         }
429         if (f.size() != FONT_SIZE_INHERIT) {
430                 // If we didn't open an environment above, we open one here
431                 if (!env) {
432                         os << '{';
433                         ++count;
434                 }
435                 os << '\\'
436                    << LaTeXSizeNames[f.size()]
437                    << "{}";
438                 count += strlen(LaTeXSizeNames[f.size()]) + 3;
439         }
440         // The ulem commands need to be on the deepest nesting level
441         // because ulem puts every nested group or macro in a box,
442         // which prevents linebreaks (#8424, #8733)
443         if (f.underbar() == FONT_ON) {
444                 os << "\\uline{";
445                 count += 10;
446                 ++runparams.inulemcmd;
447         }
448         if (f.uuline() == FONT_ON) {
449                 os << "\\uuline{";
450                 count += 11;
451                 ++runparams.inulemcmd;
452         }
453         if (f.strikeout() == FONT_ON) {
454                 os << "\\sout{";
455                 count += 9;
456                 ++runparams.inulemcmd;
457         }
458         if (f.xout() == FONT_ON) {
459                 os << "\\xout{";
460                 count += 9;
461                 ++runparams.inulemcmd;
462         }
463         if (f.uwave() == FONT_ON) {
464                 if (runparams.inulemcmd) {
465                         // needed with nested uwave in xout
466                         // see https://tex.stackexchange.com/a/263042
467                         os << "\\ULdepth=1000pt";
468                         count += 15;
469                 }
470                 os << "\\uwave{";
471                 count += 10;
472                 ++runparams.inulemcmd;
473         }
474         return count;
475 }
476
477
478 /// Writes ending block of LaTeX needed to close use of this font
479 // Returns number of chars written
480 // This one corresponds to latexWriteStartChanges(). (Asger)
481 int Font::latexWriteEndChanges(otexstream & os, BufferParams const & bparams,
482                                   OutputParams const & runparams,
483                                   Font const & base,
484                                   Font const & next,
485                                   bool & needPar,
486                                   bool const & closeLanguage) const
487 {
488         int count = 0;
489         bool env = false;
490
491         // reduce the current font to changes against the base
492         // font (of the layout). We use a temporary for this to
493         // avoid changing this font instance, as that would break
494         FontInfo f = bits_;
495         f.reduce(base.bits_);
496
497         if (f.family() != INHERIT_FAMILY) {
498                 os << '}';
499                 ++count;
500                 env = true; // Size change need not bother about closing env.
501         }
502         if (f.series() != INHERIT_SERIES) {
503                 os << '}';
504                 ++count;
505                 env = true; // Size change need not bother about closing env.
506         }
507         if (f.shape() != INHERIT_SHAPE) {
508                 os << '}';
509                 ++count;
510                 env = true; // Size change need not bother about closing env.
511         }
512         if (f.color() != Color_inherit && f.color() != Color_ignore && f.color() != Color_none) {
513                 os << '}';
514                 ++count;
515                 env = true; // Size change need not bother about closing env.
516         }
517         if (f.emph() == FONT_ON) {
518                 os << '}';
519                 ++count;
520                 env = true; // Size change need not bother about closing env.
521         }
522         if (f.noun() == FONT_ON) {
523                 os << '}';
524                 ++count;
525                 env = true; // Size change need not bother about closing env.
526         }
527         if (f.size() != FONT_SIZE_INHERIT) {
528                 // We only have to close if only size changed
529                 if (!env) {
530                         if (needPar && !closeLanguage) {
531                                 os << "\\par";
532                                 count += 4;
533                                 needPar = false;
534                         }
535                         os << '}';
536                         ++count;
537                 }
538         }
539         if (f.underbar() == FONT_ON) {
540                 os << '}';
541                 ++count;
542                 --runparams.inulemcmd;
543         }
544         if (f.strikeout() == FONT_ON) {
545                 os << '}';
546                 ++count;
547                 --runparams.inulemcmd;
548         }
549         if (f.xout() == FONT_ON) {
550                 os << '}';
551                 ++count;
552                 --runparams.inulemcmd;
553         }
554         if (f.uuline() == FONT_ON) {
555                 os << '}';
556                 ++count;
557                 --runparams.inulemcmd;
558         }
559         if (f.uwave() == FONT_ON) {
560                 os << '}';
561                 ++count;
562                 --runparams.inulemcmd;
563         }
564
565         // If the current language is Hebrew, Arabic, or Farsi
566         // the numbers are written Left-to-Right. ArabTeX package
567         // and bidi (polyglossia) reorder the number automatically
568         // but the packages used for Hebrew and Farsi (Arabi) do not.
569         if (!runparams.use_polyglossia
570             && !runparams.pass_thru
571             && bits_.number() == FONT_ON
572             && next.fontInfo().number() != FONT_ON
573             && (language()->lang() == "hebrew"
574                 || language()->lang() == "farsi"
575                 || language()->lang() == "arabic_arabi")) {
576                 os << "\\endL}";
577                 count += 6;
578         }
579
580         if (open_encoding_) {
581                 // We need to close the encoding even if it does not change
582                 // to do correct environment nesting
583                 Encoding const * const ascii = encodings.fromLyXName("ascii");
584                 pair<bool, int> const c = switchEncoding(os.os(), bparams,
585                                 runparams, *ascii);
586                 LATTEST(c.first);
587                 count += c.second;
588                 runparams.encoding = ascii;
589                 open_encoding_ = false;
590         }
591
592         if (closeLanguage
593             && language() != base.language() && language() != next.language()
594             && language()->encoding()->package() != Encoding::CJK) {
595                 os << '}';
596                 ++count;
597                 bool const using_begin_end =
598                         runparams.use_polyglossia ||
599                                 !lyxrc.language_command_end.empty();
600                 if (using_begin_end)
601                         popLanguageName();
602         }
603
604         return count;
605 }
606
607
608 string Font::toString(bool const toggle) const
609 {
610         string const lang = (language() == reset_language)
611                 ? "reset" : language()->lang();
612
613         ostringstream os;
614         os << "family " << bits_.family() << '\n'
615            << "series " << bits_.series() << '\n'
616            << "shape " << bits_.shape() << '\n'
617            << "size " << bits_.size() << '\n'
618            << "emph " << bits_.emph() << '\n'
619            << "underbar " << bits_.underbar() << '\n'
620            << "strikeout " << bits_.strikeout() << '\n'
621            << "xout " << bits_.xout() << '\n'
622            << "uuline " << bits_.uuline() << '\n'
623            << "uwave " << bits_.uwave() << '\n'
624            << "noun " << bits_.noun() << '\n'
625            << "number " << bits_.number() << '\n'
626            << "nospellcheck " << bits_.nospellcheck() << '\n'
627            << "color " << bits_.color() << '\n'
628            << "language " << lang << '\n'
629            << "toggleall " << convert<string>(toggle);
630         return os.str();
631 }
632
633
634 bool Font::fromString(string const & data, bool & toggle)
635 {
636         istringstream is(data);
637         Lexer lex;
638         lex.setStream(is);
639
640         int nset = 0;
641         while (lex.isOK()) {
642                 string token;
643                 if (lex.next())
644                         token = lex.getString();
645
646                 if (token.empty() || !lex.next())
647                         break;
648
649                 if (token == "family") {
650                         int const next = lex.getInteger();
651                         bits_.setFamily(FontFamily(next));
652
653                 } else if (token == "series") {
654                         int const next = lex.getInteger();
655                         bits_.setSeries(FontSeries(next));
656
657                 } else if (token == "shape") {
658                         int const next = lex.getInteger();
659                         bits_.setShape(FontShape(next));
660
661                 } else if (token == "size") {
662                         int const next = lex.getInteger();
663                         bits_.setSize(FontSize(next));
664
665                 } else if (token == "emph" || token == "underbar"
666                         || token == "noun" || token == "number"
667                         || token == "uuline" || token == "uwave"
668                         || token == "strikeout" || token == "xout"
669                         || token == "nospellcheck") {
670
671                         int const next = lex.getInteger();
672                         FontState const misc = FontState(next);
673
674                         if (token == "emph")
675                                 bits_.setEmph(misc);
676                         else if (token == "underbar")
677                                 bits_.setUnderbar(misc);
678                         else if (token == "strikeout")
679                                 bits_.setStrikeout(misc);
680                         else if (token == "xout")
681                                 bits_.setXout(misc);
682                         else if (token == "uuline")
683                                 bits_.setUuline(misc);
684                         else if (token == "uwave")
685                                 bits_.setUwave(misc);
686                         else if (token == "noun")
687                                 bits_.setNoun(misc);
688                         else if (token == "number")
689                                 bits_.setNumber(misc);
690                         else if (token == "nospellcheck")
691                                 bits_.setNoSpellcheck(misc);
692
693                 } else if (token == "color") {
694                         int const next = lex.getInteger();
695                         bits_.setColor(ColorCode(next));
696
697                 /**
698                 } else if (token == "background") {
699                         int const next = lex.getInteger();
700                         bits_.setBackground(ColorCode(next));
701                 */
702
703                 } else if (token == "language") {
704                         string const next = lex.getString();
705                         setLanguage(languages.getLanguage(next));
706
707                 } else if (token == "toggleall") {
708                         toggle = lex.getBool();
709
710                 } else {
711                         // Unrecognised token
712                         break;
713                 }
714
715                 ++nset;
716         }
717         return (nset > 0);
718 }
719
720
721 void Font::validate(LaTeXFeatures & features) const
722 {
723         BufferParams const & bparams = features.bufferParams();
724         Language const * doc_language = bparams.language;
725
726         if (bits_.noun() == FONT_ON) {
727                 LYXERR(Debug::LATEX, "font.noun: " << bits_.noun());
728                 features.require("noun");
729                 LYXERR(Debug::LATEX, "Noun enabled. Font: " << to_utf8(stateText(0)));
730         }
731         if (bits_.underbar() == FONT_ON) {
732                 LYXERR(Debug::LATEX, "font.underline: " << bits_.underbar());
733                 features.require("ulem");
734                 LYXERR(Debug::LATEX, "Underline enabled. Font: " << to_utf8(stateText(0)));
735         }
736         if (bits_.strikeout() == FONT_ON) {
737                 LYXERR(Debug::LATEX, "font.strikeout: " << bits_.strikeout());
738                 features.require("ulem");
739                 LYXERR(Debug::LATEX, "Strike out enabled. Font: " << to_utf8(stateText(0)));
740         }
741         if (bits_.xout() == FONT_ON) {
742                 LYXERR(Debug::LATEX, "font.xout: " << bits_.xout());
743                 features.require("ulem");
744                 LYXERR(Debug::LATEX, "Cross out enabled. Font: " << to_utf8(stateText(0)));
745         }
746         if (bits_.uuline() == FONT_ON) {
747                 LYXERR(Debug::LATEX, "font.uuline: " << bits_.uuline());
748                 features.require("ulem");
749                 LYXERR(Debug::LATEX, "Double underline enabled. Font: " << to_utf8(stateText(0)));
750         }
751         if (bits_.uwave() == FONT_ON) {
752                 LYXERR(Debug::LATEX, "font.uwave: " << bits_.uwave());
753                 features.require("ulem");
754                 LYXERR(Debug::LATEX, "Wavy underline enabled. Font: " << to_utf8(stateText(0)));
755         }
756         switch (bits_.color()) {
757                 case Color_none:
758                 case Color_inherit:
759                 case Color_ignore:
760                         // probably we should put here all interface colors used for
761                         // font displaying! For now I just add this ones I know of (Jug)
762                 case Color_latex:
763                 case Color_notelabel:
764                         break;
765                 case Color_brown:
766                 case Color_darkgray:
767                 case Color_gray:
768                 case Color_lightgray:
769                 case Color_lime:
770                 case Color_olive:
771                 case Color_orange:
772                 case Color_pink:
773                 case Color_purple:
774                 case Color_teal:
775                 case Color_violet:
776                         features.require("xcolor");
777                         break;
778                 default:
779                         features.require("color");
780                         LYXERR(Debug::LATEX, "Color enabled. Font: " << to_utf8(stateText(0)));
781         }
782
783         // FIXME: Do something for background and soul package?
784
785         if (((features.usePolyglossia() && lang_->polyglossia() != doc_language->polyglossia())
786              || (features.useBabel() && lang_->babel() != doc_language->babel())
787              || (doc_language->encoding()->package() == Encoding::CJK && lang_ != doc_language))
788             && lang_ != ignore_language
789             && lang_ != latex_language)
790         {
791                 features.useLanguage(lang_);
792                 LYXERR(Debug::LATEX, "Found language " << lang_->lang());
793         }
794 }
795
796
797 ostream & operator<<(ostream & os, FontState fms)
798 {
799         return os << int(fms);
800 }
801
802
803 ostream & operator<<(ostream & os, FontInfo const & f)
804 {
805         return os << "font:"
806                 << " family " << f.family()
807                 << " series " << f.series()
808                 << " shape " << f.shape()
809                 << " size " << f.size()
810                 << " color " << f.color()
811                 // FIXME: uncomment this when we support background.
812                 //<< " background " << f.background()
813                 << " emph " << f.emph()
814                 << " underbar " << f.underbar()
815                 << " strikeout " << f.strikeout()
816                 << " xout " << f.xout()
817                 << " uuline " << f.uuline()
818                 << " uwave " << f.uwave()
819                 << " noun " << f.noun()
820                 << " number " << f.number()
821                 << " nospellcheck " << f.nospellcheck();
822 }
823
824
825 ostream & operator<<(ostream & os, Font const & font)
826 {
827         return os << font.bits_
828                 << " lang: " << (font.lang_ ? font.lang_->lang() : 0);
829 }
830
831
832 } // namespace lyx