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