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