]> git.lyx.org Git - lyx.git/blob - src/Font.cpp
d3dd3bbc2ab0ed462a6c03b74262d9b3640fad2e
[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 "Color.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
29 #include "support/convert.h"
30 #include "support/debug.h"
31 #include "support/gettext.h"
32 #include "support/lstrings.h"
33
34 using namespace std;
35 using namespace lyx::support;
36
37 namespace lyx {
38
39 //
40 // Names for the GUI
41 //
42
43 namespace {
44
45 char const * GUIFamilyNames[NUM_FAMILIES + 2 /* default & error */] =
46 { N_("Roman"), N_("Sans Serif"), N_("Typewriter"), N_("Symbol"),
47   "cmr", "cmsy", "cmm", "cmex", "msa", "msb", "eufrak", "wasy", "esint",
48   N_("Inherit"), N_("Ignore") };
49
50 char const * GUISeriesNames[4] =
51 { N_("Medium"), N_("Bold"), N_("Inherit"), N_("Ignore") };
52
53 char const * GUIShapeNames[6] =
54 { N_("Upright"), N_("Italic"), N_("Slanted"), N_("Smallcaps"), N_("Inherit"),
55   N_("Ignore") };
56
57 char const * GUISizeNames[14] =
58 { N_("Tiny"), N_("Smallest"), N_("Smaller"), N_("Small"), N_("Normal"), N_("Large"),
59   N_("Larger"), N_("Largest"), N_("Huge"), N_("Huger"), N_("Increase"), N_("Decrease"),
60   N_("Inherit"), N_("Ignore") };
61
62 char const * GUIMiscNames[5] =
63 { N_("Off"), N_("On"), N_("Toggle"), N_("Inherit"), N_("Ignore") };
64
65
66 //
67 // Strings used to read and write .lyx format files
68 //
69 char const * LyXFamilyNames[NUM_FAMILIES + 2 /* default & error */] =
70 { "roman", "sans", "typewriter", "symbol",
71   "cmr", "cmsy", "cmm", "cmex", "msa", "msb", "eufrak", "wasy", "esint",
72   "default", "error" };
73
74 char const * LyXSeriesNames[4] =
75 { "medium", "bold", "default", "error" };
76
77 char const * LyXShapeNames[6] =
78 { "up", "italic", "slanted", "smallcaps", "default", "error" };
79
80 char const * LyXSizeNames[14] =
81 { "tiny", "scriptsize", "footnotesize", "small", "normal", "large",
82   "larger", "largest", "huge", "giant",
83   "increase", "decrease", "default", "error" };
84
85 char const * LyXMiscNames[5] =
86 { "off", "on", "toggle", "default", "error" };
87
88 //
89 // Strings used to write LaTeX files
90 //
91 char const * LaTeXFamilyNames[6] =
92 { "textrm", "textsf", "texttt", "error1", "error2", "error3" };
93
94 char const * LaTeXSeriesNames[4] =
95 { "textmd", "textbf", "error4", "error5" };
96
97 char const * LaTeXShapeNames[6] =
98 { "textup", "textit", "textsl", "textsc", "error6", "error7" };
99
100 char const * LaTeXSizeNames[14] =
101 { "tiny", "scriptsize", "footnotesize", "small", "normalsize", "large",
102   "Large", "LARGE", "huge", "Huge", "error8", "error9", "error10", "error11" };
103
104 } // namespace anon
105
106
107 Font::Font(FontInfo bits, Language const * l)
108         : bits_(bits), lang_(l), open_encoding_(false)
109 {
110         if (!lang_)
111                 lang_ = default_language;
112 }
113
114
115 bool Font::isRightToLeft() const
116 {
117         return lang_->rightToLeft();
118 }
119
120
121 bool Font::isVisibleRightToLeft() const
122 {
123         return (lang_->rightToLeft() &&
124                 bits_.number() != FONT_ON);
125 }
126
127
128 void Font::setLanguage(Language const * l)
129 {
130         lang_ = l;
131 }
132
133
134 /// Updates font settings according to request
135 void Font::update(Font const & newfont,
136                      Language const * document_language,
137                      bool toggleall)
138 {
139         bits_.update(newfont.fontInfo(), toggleall);
140
141         if (newfont.language() == language() && toggleall)
142                 if (language() == document_language)
143                         setLanguage(default_language);
144                 else
145                         setLanguage(document_language);
146         else if (newfont.language() != ignore_language)
147                 setLanguage(newfont.language());
148 }
149
150
151 docstring const stateText(FontInfo const & f)
152 {
153         odocstringstream os;
154         if (f.family() != INHERIT_FAMILY)
155                 os << _(GUIFamilyNames[f.family()]) << ", ";
156         if (f.series() != INHERIT_SERIES)
157                 os << _(GUISeriesNames[f.series()]) << ", ";
158         if (f.shape() != INHERIT_SHAPE)
159                 os << _(GUIShapeNames[f.shape()]) << ", ";
160         if (f.size() != FONT_SIZE_INHERIT)
161                 os << _(GUISizeNames[f.size()]) << ", ";
162         if (f.color() != Color_inherit)
163                 os << lcolor.getGUIName(f.color()) << ", ";
164         // FIXME: uncomment this when we support background.
165         //if (f.background() != Color_inherit)
166         //      os << lcolor.getGUIName(f.background()) << ", ";
167         if (f.emph() != FONT_INHERIT)
168                 os << bformat(_("Emphasis %1$s, "),
169                               _(GUIMiscNames[f.emph()]));
170         if (f.underbar() != FONT_INHERIT)
171                 os << bformat(_("Underline %1$s, "),
172                               _(GUIMiscNames[f.underbar()]));
173         if (f.noun() != FONT_INHERIT)
174                 os << bformat(_("Noun %1$s, "),
175                               _(GUIMiscNames[f.noun()]));
176         if (f == inherit_font)
177                 os << _("Default") << ", ";
178
179         return os.str();
180 }
181
182
183 docstring const Font::stateText(BufferParams * params) const
184 {
185         odocstringstream os;
186         os << lyx::stateText(bits_);
187         if (!params || (language() != params->language))
188                 os << bformat(_("Language: %1$s, "),
189                               _(language()->display()));
190         if (bits_.number() != FONT_OFF)
191                 os << bformat(_("  Number %1$s"),
192                               _(GUIMiscNames[bits_.number()]));
193         return rtrim(os.str(), ", ");
194 }
195
196
197 // Set family according to lyx format string
198 void setLyXFamily(string const & fam, FontInfo & f)
199 {
200         string const s = ascii_lowercase(fam);
201
202         int i = 0;
203         while (LyXFamilyNames[i] != s &&
204                LyXFamilyNames[i] != string("error"))
205                 ++i;
206         if (s == LyXFamilyNames[i])
207                 f.setFamily(FontFamily(i));
208         else
209                 lyxerr << "setLyXFamily: Unknown family `"
210                        << s << '\'' << endl;
211 }
212
213
214 // Set series according to lyx format string
215 void setLyXSeries(string const & ser, FontInfo & f)
216 {
217         string const s = ascii_lowercase(ser);
218
219         int i = 0;
220         while (LyXSeriesNames[i] != s &&
221                LyXSeriesNames[i] != string("error")) ++i;
222         if (s == LyXSeriesNames[i]) {
223                 f.setSeries(FontSeries(i));
224         } else
225                 lyxerr << "setLyXSeries: Unknown series `"
226                        << s << '\'' << endl;
227 }
228
229
230 // Set shape according to lyx format string
231 void setLyXShape(string const & sha, FontInfo & f)
232 {
233         string const s = ascii_lowercase(sha);
234
235         int i = 0;
236         while (LyXShapeNames[i] != s && LyXShapeNames[i] != string("error"))
237                         ++i;
238         if (s == LyXShapeNames[i])
239                 f.setShape(FontShape(i));
240         else
241                 lyxerr << "Font::setLyXShape: Unknown shape `"
242                        << s << '\'' << endl;
243 }
244
245
246 // Set size according to lyx format string
247 void setLyXSize(string const & siz, FontInfo & f)
248 {
249         string const s = ascii_lowercase(siz);
250         int i = 0;
251         while (LyXSizeNames[i] != s && LyXSizeNames[i] != string("error"))
252                 ++i;
253         if (s == LyXSizeNames[i]) {
254                 f.setSize(FontSize(i));
255         } else
256                 lyxerr << "Font::setLyXSize: Unknown size `"
257                        << s << '\'' << endl;
258 }
259
260
261 // Set size according to lyx format string
262 FontState Font::setLyXMisc(string const & siz)
263 {
264         string const s = ascii_lowercase(siz);
265         int i = 0;
266         while (LyXMiscNames[i] != s &&
267                LyXMiscNames[i] != string("error")) ++i;
268         if (s == LyXMiscNames[i])
269                 return FontState(i);
270         lyxerr << "Font::setLyXMisc: Unknown misc flag `"
271                << s << '\'' << endl;
272         return FONT_OFF;
273 }
274
275
276 /// Sets color after LyX text format
277 void setLyXColor(string const & col, FontInfo & f)
278 {
279         f.setColor(lcolor.getFromLyXName(col));
280 }
281
282
283 // Returns size in latex format
284 string const Font::latexSize() const
285 {
286         return LaTeXSizeNames[bits_.size()];
287 }
288
289
290 // Read a font definition from given file in lyx format
291 // Used for layouts
292 FontInfo lyxRead(Lexer & lex, FontInfo const & fi)
293 {
294         FontInfo f = fi;
295         bool error = false;
296         bool finished = false;
297         while (!finished && lex.isOK() && !error) {
298                 lex.next();
299                 string const tok = ascii_lowercase(lex.getString());
300
301                 if (tok.empty()) {
302                         continue;
303                 } else if (tok == "endfont") {
304                         finished = true;
305                 } else if (tok == "family") {
306                         lex.next();
307                         string const ttok = lex.getString();
308                         setLyXFamily(ttok, f);
309                 } else if (tok == "series") {
310                         lex.next();
311                         string const ttok = lex.getString();
312                         setLyXSeries(ttok, f);
313                 } else if (tok == "shape") {
314                         lex.next();
315                         string const ttok = lex.getString();
316                         setLyXShape(ttok, f);
317                 } else if (tok == "size") {
318                         lex.next();
319                         string const ttok = lex.getString();
320                         setLyXSize(ttok, f);
321                 } else if (tok == "misc") {
322                         lex.next();
323                         string const ttok = ascii_lowercase(lex.getString());
324
325                         if (ttok == "no_bar") {
326                                 f.setUnderbar(FONT_OFF);
327                         } else if (ttok == "no_emph") {
328                                 f.setEmph(FONT_OFF);
329                         } else if (ttok == "no_noun") {
330                                 f.setNoun(FONT_OFF);
331                         } else if (ttok == "emph") {
332                                 f.setEmph(FONT_ON);
333                         } else if (ttok == "underbar") {
334                                 f.setUnderbar(FONT_ON);
335                         } else if (ttok == "noun") {
336                                 f.setNoun(FONT_ON);
337                         } else {
338                                 lex.printError("Illegal misc type `$$Token'");
339                         }
340                 } else if (tok == "color") {
341                         lex.next();
342                         string const ttok = lex.getString();
343                         setLyXColor(ttok, f);
344                 } else {
345                         lex.printError("Unknown tag `$$Token'");
346                         error = true;
347                 }
348         }
349         return f;
350 }
351
352
353 /// Writes the changes from this font to orgfont in .lyx format in file
354 void Font::lyxWriteChanges(Font const & orgfont,
355                               ostream & os) const
356 {
357         os << "\n";
358         if (orgfont.fontInfo().family() != bits_.family())
359                 os << "\\family " << LyXFamilyNames[bits_.family()] << "\n";
360         if (orgfont.fontInfo().series() != bits_.series())
361                 os << "\\series " << LyXSeriesNames[bits_.series()] << "\n";
362         if (orgfont.fontInfo().shape() != bits_.shape())
363                 os << "\\shape " << LyXShapeNames[bits_.shape()] << "\n";
364         if (orgfont.fontInfo().size() != bits_.size())
365                 os << "\\size " << LyXSizeNames[bits_.size()] << "\n";
366         if (orgfont.fontInfo().emph() != bits_.emph())
367                 os << "\\emph " << LyXMiscNames[bits_.emph()] << "\n";
368         if (orgfont.fontInfo().number() != bits_.number())
369                 os << "\\numeric " << LyXMiscNames[bits_.number()] << "\n";
370         if (orgfont.fontInfo().underbar() != bits_.underbar()) {
371                 // This is only for backwards compatibility
372                 switch (bits_.underbar()) {
373                 case FONT_OFF:  os << "\\bar no\n"; break;
374                 case FONT_ON:        os << "\\bar under\n"; break;
375                 case FONT_TOGGLE:       lyxerr << "Font::lyxWriteFontChanges: "
376                                         "FONT_TOGGLE should not appear here!"
377                                        << endl;
378                 break;
379                 case FONT_INHERIT:   os << "\\bar default\n"; break;
380                 case FONT_IGNORE:    lyxerr << "Font::lyxWriteFontChanges: "
381                                         "IGNORE should not appear here!"
382                                        << endl;
383                 break;
384                 }
385         }
386         if (orgfont.fontInfo().noun() != bits_.noun()) {
387                 os << "\\noun " << LyXMiscNames[bits_.noun()] << "\n";
388         }
389         if (orgfont.fontInfo().color() != bits_.color())
390                 os << "\\color " << lcolor.getLyXName(bits_.color()) << '\n';
391         // FIXME: uncomment this when we support background.
392         //if (orgfont.fontInfo().background() != bits_.background())
393         //      os << "\\color " << lcolor.getLyXName(bits_.background()) << '\n';
394         if (orgfont.language() != language() &&
395             language() != latex_language) {
396                 if (language())
397                         os << "\\lang " << language()->lang() << "\n";
398                 else
399                         os << "\\lang unknown\n";
400         }
401 }
402
403
404 /// Writes the head of the LaTeX needed to impose this font
405 // Returns number of chars written.
406 int Font::latexWriteStartChanges(odocstream & os, BufferParams const & bparams,
407                                     OutputParams const & runparams,
408                                     Font const & base,
409                                     Font const & prev) const
410 {
411         bool env = false;
412
413         int count = 0;
414         if (language()->babel() != base.language()->babel() &&
415             language() != prev.language()) {
416                 if (language()->lang() == "farsi") {
417                         os << "\\textFR{";
418                         count += 8;
419                 } else if (!isRightToLeft() &&
420                             base.language()->lang() == "farsi") {
421                         os << "\\textLR{";
422                         count += 8;
423                 } else if (language()->lang() == "arabic_arabi") {
424                         os << "\\textAR{";
425                         count += 8;
426                 } else if (!isRightToLeft() &&
427                                 base.language()->lang() == "arabic_arabi") {
428                         os << "\\textLR{";
429                         count += 8;
430                 // currently the remaining RTL languages are arabic_arabtex and hebrew
431                 } else if (isRightToLeft() != prev.isRightToLeft()) {
432                         if (isRightToLeft()) {
433                                 os << "\\R{";
434                                 count += 3;
435                         } else {
436                                 os << "\\L{";
437                                 count += 3;
438                         }
439                 } else if (!language()->babel().empty()) {
440                         string const tmp =
441                                 subst(lyxrc.language_command_local,
442                                       "$$lang", language()->babel());
443                         os << from_ascii(tmp);
444                         count += tmp.length();
445                 } else {
446                         os << '{';
447                         count += 1;
448                 }
449         }
450
451         if (language()->encoding()->package() == Encoding::CJK) {
452                 pair<bool, int> const c = switchEncoding(os, bparams,
453                                 runparams, *(runparams.encoding),
454                                 *(language()->encoding()));
455                 if (c.first) {
456                         open_encoding_ = true;
457                         count += c.second;
458                         runparams.encoding = language()->encoding();
459                 }
460         }
461
462         // When the current language is Hebrew, Arabic, or Farsi
463         // the numbers are written Left-to-Right. ArabTeX package
464         // reorders the number automatically but the packages used
465         // for Hebrew and Farsi (Arabi) do not.
466         if (bits_.number() == FONT_ON && prev.fontInfo().number() != FONT_ON
467                 && (language()->lang() == "hebrew"
468                         || language()->lang() == "farsi" 
469                         || language()->lang() == "arabic_arabi")) {
470                 os << "{\\beginL ";
471                 count += 9;
472         }
473
474         FontInfo f = bits_;
475         f.reduce(base.bits_);
476
477         if (f.family() != INHERIT_FAMILY) {
478                 os << '\\'
479                    << LaTeXFamilyNames[f.family()]
480                    << '{';
481                 count += strlen(LaTeXFamilyNames[f.family()]) + 2;
482                 env = true; //We have opened a new environment
483         }
484         if (f.series() != INHERIT_SERIES) {
485                 os << '\\'
486                    << LaTeXSeriesNames[f.series()]
487                    << '{';
488                 count += strlen(LaTeXSeriesNames[f.series()]) + 2;
489                 env = true; //We have opened a new environment
490         }
491         if (f.shape() != INHERIT_SHAPE) {
492                 os << '\\'
493                    << LaTeXShapeNames[f.shape()]
494                    << '{';
495                 count += strlen(LaTeXShapeNames[f.shape()]) + 2;
496                 env = true; //We have opened a new environment
497         }
498         if (f.color() != Color_inherit && f.color() != Color_ignore) {
499                 os << "\\textcolor{"
500                    << from_ascii(lcolor.getLaTeXName(f.color()))
501                    << "}{";
502                 count += lcolor.getLaTeXName(f.color()).length() + 13;
503                 env = true; //We have opened a new environment
504         }
505         // FIXME: uncomment this when we support background.
506         /*
507         if (f.background() != Color_inherit && f.background() != Color_ignore) {
508                 os << "\\textcolor{"
509                    << from_ascii(lcolor.getLaTeXName(f.background()))
510                    << "}{";
511                 count += lcolor.getLaTeXName(f.background()).length() + 13;
512                 env = true; //We have opened a new environment
513         }
514         */
515         if (f.emph() == FONT_ON) {
516                 os << "\\emph{";
517                 count += 6;
518                 env = true; //We have opened a new environment
519         }
520         if (f.underbar() == FONT_ON) {
521                 os << "\\underbar{";
522                 count += 10;
523                 env = true; //We have opened a new environment
524         }
525         // \noun{} is a LyX special macro
526         if (f.noun() == FONT_ON) {
527                 os << "\\noun{";
528                 count += 6;
529                 env = true; //We have opened a new environment
530         }
531         if (f.size() != FONT_SIZE_INHERIT) {
532                 // If we didn't open an environment above, we open one here
533                 if (!env) {
534                         os << '{';
535                         ++count;
536                 }
537                 os << '\\'
538                    << LaTeXSizeNames[f.size()]
539                    << ' ';
540                 count += strlen(LaTeXSizeNames[f.size()]) + 2;
541         }
542         return count;
543 }
544
545
546 /// Writes ending block of LaTeX needed to close use of this font
547 // Returns number of chars written
548 // This one corresponds to latexWriteStartChanges(). (Asger)
549 int Font::latexWriteEndChanges(odocstream & os, BufferParams const & bparams,
550                                   OutputParams const & runparams,
551                                   Font const & base,
552                                   Font const & next,
553                                   bool const & closeLanguage) const
554 {
555         int count = 0;
556         bool env = false;
557
558         // reduce the current font to changes against the base
559         // font (of the layout). We use a temporary for this to
560         // avoid changing this font instance, as that would break
561         FontInfo f = bits_;
562         f.reduce(base.bits_);
563
564         if (f.family() != INHERIT_FAMILY) {
565                 os << '}';
566                 ++count;
567                 env = true; // Size change need not bother about closing env.
568         }
569         if (f.series() != INHERIT_SERIES) {
570                 os << '}';
571                 ++count;
572                 env = true; // Size change need not bother about closing env.
573         }
574         if (f.shape() != INHERIT_SHAPE) {
575                 os << '}';
576                 ++count;
577                 env = true; // Size change need not bother about closing env.
578         }
579         if (f.color() != Color_inherit && f.color() != Color_ignore) {
580                 os << '}';
581                 ++count;
582                 env = true; // Size change need not bother about closing env.
583         }
584         if (f.emph() == FONT_ON) {
585                 os << '}';
586                 ++count;
587                 env = true; // Size change need not bother about closing env.
588         }
589         if (f.underbar() == FONT_ON) {
590                 os << '}';
591                 ++count;
592                 env = true; // Size change need not bother about closing env.
593         }
594         if (f.noun() == FONT_ON) {
595                 os << '}';
596                 ++count;
597                 env = true; // Size change need not bother about closing env.
598         }
599         if (f.size() != FONT_SIZE_INHERIT) {
600                 // We only have to close if only size changed
601                 if (!env) {
602                         os << '}';
603                         ++count;
604                 }
605         }
606
607         // When the current language is Hebrew, Arabic, or Farsi
608         // the numbers are written Left-to-Right. ArabTeX package
609         // reorders the number automatically but the packages used
610         // for Hebrew and Farsi (Arabi) do not.
611         if (bits_.number() == FONT_ON && next.fontInfo().number() != FONT_ON
612                 && (language()->lang() == "hebrew"
613                         || language()->lang() == "farsi"
614                         || language()->lang() == "arabic_arabi")) {
615                 os << "\\endL}";
616                 count += 6;
617         }
618
619         if (open_encoding_) {
620                 // We need to close the encoding even if it does not change
621                 // to do correct environment nesting
622                 Encoding const * const ascii = encodings.getFromLyXName("ascii");
623                 pair<bool, int> const c = switchEncoding(os, bparams,
624                                 runparams, *(runparams.encoding),
625                                 *ascii);
626                 BOOST_ASSERT(c.first);
627                 count += c.second;
628                 runparams.encoding = ascii;
629                 open_encoding_ = false;
630         }
631
632         if (closeLanguage &&
633                         language() != base.language() && language() != next.language()) {
634                 os << '}';
635                 ++count;
636         }
637
638         return count;
639 }
640
641
642 string Font::toString(bool const toggle) const
643 {
644         string lang = "ignore";
645         if (language())
646                 lang = language()->lang();
647
648         ostringstream os;
649         os << "family " << bits_.family() << '\n'
650            << "series " << bits_.series() << '\n'
651            << "shape " << bits_.shape() << '\n'
652            << "size " << bits_.size() << '\n'
653            << "emph " << bits_.emph() << '\n'
654            << "underbar " << bits_.underbar() << '\n'
655            << "noun " << bits_.noun() << '\n'
656            << "number " << bits_.number() << '\n'
657            << "color " << bits_.color() << '\n'
658            << "language " << lang << '\n'
659            << "toggleall " << convert<string>(toggle);
660         return os.str();
661 }
662
663
664 bool Font::fromString(string const & data, bool & toggle)
665 {
666         istringstream is(data);
667         Lexer lex(0,0);
668         lex.setStream(is);
669
670         int nset = 0;
671         while (lex.isOK()) {
672                 string token;
673                 if (lex.next())
674                         token = lex.getString();
675
676                 if (token.empty() || !lex.next())
677                         break;
678
679                 if (token == "family") {
680                         int const next = lex.getInteger();
681                         bits_.setFamily(FontFamily(next));
682
683                 } else if (token == "series") {
684                         int const next = lex.getInteger();
685                         bits_.setSeries(FontSeries(next));
686
687                 } else if (token == "shape") {
688                         int const next = lex.getInteger();
689                         bits_.setShape(FontShape(next));
690
691                 } else if (token == "size") {
692                         int const next = lex.getInteger();
693                         bits_.setSize(FontSize(next));
694
695                 } else if (token == "emph" || token == "underbar" ||
696                            token == "noun" || token == "number") {
697
698                         int const next = lex.getInteger();
699                         FontState const misc = FontState(next);
700
701                         if (token == "emph")
702                                 bits_.setEmph(misc);
703                         else if (token == "underbar")
704                                 bits_.setUnderbar(misc);
705                         else if (token == "noun")
706                                 bits_.setNoun(misc);
707                         else if (token == "number")
708                                 bits_.setNumber(misc);
709
710                 } else if (token == "color") {
711                         int const next = lex.getInteger();
712                         bits_.setColor(ColorCode(next));
713
714                 /**
715                 } else if (token == "background") {
716                         int const next = lex.getInteger();
717                         bits_.setBackground(ColorCode(next));
718                 */
719
720                 } else if (token == "language") {
721                         string const next = lex.getString();
722                         if (next == "ignore")
723                                 setLanguage(ignore_language);
724                         else
725                                 setLanguage(languages.getLanguage(next));
726
727                 } else if (token == "toggleall") {
728                         toggle = lex.getBool();
729
730                 } else {
731                         // Unrecognised token
732                         break;
733                 }
734
735                 ++nset;
736         }
737         return (nset > 0);
738 }
739
740
741 void Font::validate(LaTeXFeatures & features) const
742 {
743         BufferParams const & bparams = features.bufferParams();
744         Language const * doc_language = bparams.language;
745
746         if (bits_.noun() == FONT_ON) {
747                 LYXERR(Debug::LATEX, "font.noun: " << bits_.noun());
748                 features.require("noun");
749                 LYXERR(Debug::LATEX, "Noun enabled. Font: " << to_utf8(stateText(0)));
750         }
751         switch (bits_.color()) {
752                 case Color_none:
753                 case Color_inherit:
754                 case Color_ignore:
755                         // probably we should put here all interface colors used for
756                         // font displaying! For now I just add this ones I know of (Jug)
757                 case Color_latex:
758                 case Color_note:
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 (lang_->babel() != doc_language->babel() &&
768                 lang_ != ignore_language &&
769                 lang_ != latex_language)
770         {
771                 features.useLanguage(lang_);
772                 LYXERR(Debug::LATEX, "Found language " << lang_->lang());
773         }
774 }
775
776
777 ostream & operator<<(ostream & os, FontState fms)
778 {
779         return os << int(fms);
780 }
781
782
783 ostream & operator<<(ostream & os, FontInfo const & f)
784 {
785         return os << "font:"
786                 << " family " << f.family()
787                 << " series " << f.series()
788                 << " shape " << f.shape()
789                 << " size " << f.size()
790                 << " color " << f.color()
791                 // FIXME: uncomment this when we support background.
792                 //<< " background " << f.background()
793                 << " emph " << f.emph()
794                 << " underbar " << f.underbar()
795                 << " noun " << f.noun()
796                 << " number " << f.number();
797 }
798
799
800 ostream & operator<<(ostream & os, Font const & font)
801 {
802         return os << font.bits_
803                 << " lang: " << (font.lang_ ? font.lang_->lang() : 0);
804 }
805
806
807 } // namespace lyx