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