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