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