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