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