]> git.lyx.org Git - lyx.git/blob - src/Font.cpp
Avoid full metrics computation with Update:FitCursor
[lyx.git] / src / Font.cpp
1 /**
2  * \file src/Font.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Lars Gullik Bjønnes
7  * \author Jean-Marc Lasgouttes
8  * \author Angus Leeming
9  * \author André Pönitz
10  * \author Dekel Tsur
11  *
12  * Full author contact details are available in file CREDITS.
13  */
14
15 #include <config.h>
16
17 #include "Font.h"
18
19 #include "BufferParams.h" // stateText
20 #include "ColorSet.h"
21 #include "Encoding.h"
22 #include "Language.h"
23 #include "LaTeXFeatures.h"
24 #include "LyXRC.h"
25 #include "output_latex.h"
26 #include "OutputParams.h"
27 #include "texstream.h"
28
29 #include "support/lassert.h"
30 #include "support/convert.h"
31 #include "support/debug.h"
32 #include "support/gettext.h"
33 #include "support/Lexer.h"
34 #include "support/lstrings.h"
35
36 #include <cstring>
37
38 using namespace std;
39 using namespace lyx::support;
40
41 namespace lyx {
42
43 //
44 // Strings used to read and write .lyx format files
45 //
46 // These are defined in FontInfo.cpp
47 extern char const * LyXFamilyNames[NUM_FAMILIES + 2];
48 extern char const * LyXSeriesNames[NUM_SERIES + 2];
49 extern char const * LyXShapeNames[NUM_SHAPE + 2];
50 extern char const * LyXSizeNames[NUM_SIZE + 4];
51 extern char const * LyXMiscNames[5];
52 extern char const * GUIMiscNames[5];
53
54 namespace {
55
56 //
57 // Strings used to write LaTeX files
58 //
59 char const * LaTeXFamilyCommandNames[NUM_FAMILIES + 2] =
60 { "textrm", "textsf", "texttt", "error1", "error2", "error3", "error4",
61   "error5", "error6", "error7", "error8", "error9", "error10", "error11",
62   "error12", "error13", "error14" };
63
64 char const * LaTeXFamilySwitchNames[NUM_FAMILIES + 2] =
65 { "rmfamily", "sffamily", "ttfamily", "error1", "error2", "error3", "error4",
66   "error5", "error6", "error7", "error8", "error9", "error10", "error11",
67   "error12", "error13", "error14" };
68
69 char const * LaTeXSeriesCommandNames[NUM_SERIES + 2] =
70 { "textmd", "textbf", "error4", "error5" };
71
72 char const * LaTeXSeriesSwitchNames[NUM_SERIES + 2] =
73 { "mdseries", "bfseries", "error4", "error5" };
74
75 char const * LaTeXShapeCommandNames[NUM_SHAPE + 2] =
76 { "textup", "textit", "textsl", "textsc", "error6", "error7" };
77
78 char const * LaTeXShapeSwitchNames[NUM_SHAPE + 2] =
79 { "upshape", "itshape", "slshape", "scshape", "error6", "error7" };
80
81 char const * LaTeXSizeSwitchNames[NUM_SIZE + 4] =
82 { "tiny", "scriptsize", "footnotesize", "small", "normalsize", "large",
83   "Large", "LARGE", "huge", "Huge", "error8", "error9", "error10", "error11" };
84
85 } // namespace
86
87
88 Font::Font(FontInfo bits, Language const * l)
89         : bits_(bits), lang_(l), open_encoding_(false)
90 {
91         if (!lang_)
92                 lang_ = default_language;
93 }
94
95
96 bool Font::isRightToLeft() const
97 {
98         return lang_->rightToLeft();
99 }
100
101
102 bool Font::isVisibleRightToLeft() const
103 {
104         return (lang_->rightToLeft() &&
105                 bits_.number() != FONT_ON);
106 }
107
108
109 void Font::setLanguage(Language const * l)
110 {
111         lang_ = l;
112 }
113
114
115 /// Updates font settings according to request
116 void Font::update(Font const & newfont,
117                      Language const * default_lang,
118                      bool toggleall)
119 {
120         bits_.update(newfont.fontInfo(), toggleall);
121
122         if (newfont.language() == language() && toggleall)
123                 if (language() == default_lang)
124                         setLanguage(default_language);
125                 else
126                         setLanguage(default_lang);
127         else if (newfont.language() == reset_language)
128                 setLanguage(default_lang);
129         else if (newfont.language() != ignore_language)
130                 setLanguage(newfont.language());
131 }
132
133
134 docstring const Font::stateText(BufferParams * params, bool const terse) const
135 {
136         odocstringstream os;
137         os << bits_.stateText(terse);
138         if ((!params || (language() != params->language))
139             && (!terse || language() != ignore_language)) {
140                 // reset_language is a null pointer!
141                 os << bformat(_("Language: %1$s, "),
142                               (language() == reset_language) ? _("Default")
143                                                              : _(language()->display()));
144         }
145         if (bits_.number() != FONT_OFF)
146                 os << "  " << bformat(_("Number %1$s"),
147                               _(GUIMiscNames[bits_.number()]));
148         return rtrim(os.str(), ", ");
149 }
150
151
152 // Returns size in latex format
153 string const Font::latexSize() const
154 {
155         return LaTeXSizeSwitchNames[bits_.size()];
156 }
157
158
159 /// Writes the changes from this font to orgfont in .lyx format in file
160 void Font::lyxWriteChanges(Font const & orgfont,
161                               ostream & os) const
162 {
163         os << "\n";
164         if (orgfont.fontInfo().family() != bits_.family())
165                 os << "\\family " << LyXFamilyNames[bits_.family()] << "\n";
166         if (orgfont.fontInfo().series() != bits_.series())
167                 os << "\\series " << LyXSeriesNames[bits_.series()] << "\n";
168         if (orgfont.fontInfo().shape() != bits_.shape())
169                 os << "\\shape " << LyXShapeNames[bits_.shape()] << "\n";
170         if (orgfont.fontInfo().size() != bits_.size())
171                 os << "\\size " << LyXSizeNames[bits_.size()] << "\n";
172         // FIXME: shall style be handled there? Probably not.
173         if (orgfont.fontInfo().emph() != bits_.emph())
174                 os << "\\emph " << LyXMiscNames[bits_.emph()] << "\n";
175         if (orgfont.fontInfo().number() != bits_.number())
176                 os << "\\numeric " << LyXMiscNames[bits_.number()] << "\n";
177         if (orgfont.fontInfo().nospellcheck() != bits_.nospellcheck())
178                 os << "\\nospellcheck " << LyXMiscNames[bits_.nospellcheck()] << "\n";
179         if (orgfont.fontInfo().underbar() != bits_.underbar()) {
180                 // This is only for backwards compatibility
181                 switch (bits_.underbar()) {
182                 case FONT_OFF:  os << "\\bar no\n"; break;
183                 case FONT_ON:        os << "\\bar under\n"; break;
184                 case FONT_TOGGLE:       lyxerr << "Font::lyxWriteFontChanges: "
185                                         "FONT_TOGGLE should not appear here!"
186                                        << endl;
187                 break;
188                 case FONT_INHERIT:   os << "\\bar default\n"; break;
189                 case FONT_IGNORE:    lyxerr << "Font::lyxWriteFontChanges: "
190                                         "IGNORE should not appear here!"
191                                        << endl;
192                 break;
193                 }
194         }
195         if (orgfont.fontInfo().strikeout() != bits_.strikeout()) {
196                 os << "\\strikeout " << LyXMiscNames[bits_.strikeout()] << "\n";
197         }
198         if (orgfont.fontInfo().xout() != bits_.xout()) {
199                 os << "\\xout " << LyXMiscNames[bits_.xout()] << "\n";
200         }
201         if (orgfont.fontInfo().uuline() != bits_.uuline()) {
202                 os << "\\uuline " << LyXMiscNames[bits_.uuline()] << "\n";
203         }
204         if (orgfont.fontInfo().uwave() != bits_.uwave()) {
205                 os << "\\uwave " << LyXMiscNames[bits_.uwave()] << "\n";
206         }
207         if (orgfont.fontInfo().noun() != bits_.noun()) {
208                 os << "\\noun " << LyXMiscNames[bits_.noun()] << "\n";
209         }
210         if (orgfont.fontInfo().color() != bits_.color())
211                 os << "\\color " << lcolor.getLyXName(bits_.color()) << '\n';
212         // FIXME: uncomment this when we support background.
213         //if (orgfont.fontInfo().background() != bits_.background())
214         //      os << "\\color " << lcolor.getLyXName(bits_.background()) << '\n';
215         if (orgfont.language() != language() &&
216             language() != latex_language) {
217                 if (language())
218                         os << "\\lang " << language()->lang() << "\n";
219                 else
220                         os << "\\lang unknown\n";
221         }
222 }
223
224
225 /// Writes the head of the LaTeX needed to impose this font
226 // Returns number of chars written.
227 int Font::latexWriteStartChanges(otexstream & os, BufferParams const & bparams,
228                                     OutputParams const & runparams,
229                                     Font const & base,
230                                     Font const & prev,
231                                     bool non_inherit_inset,
232                                     bool needs_cprotection) const
233 {
234         int count = 0;
235
236         // polyglossia or babel?
237         if (runparams.use_polyglossia
238             && language()->lang() != base.language()->lang()
239             && language() != prev.language()) {
240                 if (!language()->polyglossia().empty()) {
241                         string tmp;
242                         if (needs_cprotection)
243                                 tmp += "\\cprotect";
244                         tmp += "\\text" + language()->polyglossia();
245                         if (!language()->polyglossiaOpts().empty()) {
246                                 tmp += "[" + language()->polyglossiaOpts() + "]";
247                                 if (runparams.use_hyperref && runparams.moving_arg) {
248                                         // We need to strip the command for
249                                         // the pdf string, see #11813
250                                         string tmpp;
251                                         if (needs_cprotection)
252                                                 tmpp = "\\cprotect";
253                                         tmp = tmpp + "\\texorpdfstring{" + tmp + "}{}";
254                                 }
255                         }
256                         tmp += "{";
257                         os << from_ascii(tmp);
258                         count += tmp.length();
259                         pushLanguageName(language()->polyglossia(), true);
260                 } else if (language()->encoding()->package() != Encoding::CJK) {
261                         os << '{';
262                         count += 1;
263                 }
264         } else if (language()->babel() != base.language()->babel() &&
265             language() != prev.language()) {
266                 if (language()->lang() == "farsi") {
267                         if (needs_cprotection) {
268                                 os << "\\cprotect";
269                                 count += 9;
270                         }
271                         os << "\\textFR{";
272                         count += 8;
273                 } else if (!isRightToLeft() &&
274                             base.language()->lang() == "farsi") {
275                         if (needs_cprotection) {
276                                 os << "\\cprotect";
277                                 count += 9;
278                         }
279                         os << "\\textLR{";
280                         count += 8;
281                 } else if (language()->lang() == "arabic_arabi") {
282                         if (needs_cprotection) {
283                                 os << "\\cprotect";
284                                 count += 9;
285                         }
286                         os << "\\textAR{";
287                         count += 8;
288                 } else if (!isRightToLeft() &&
289                                 base.language()->lang() == "arabic_arabi") {
290                         if (needs_cprotection) {
291                                 os << "\\cprotect";
292                                 count += 9;
293                         }
294                         os << "\\textLR{";
295                         count += 8;
296                 // currently the remaining RTL languages are arabic_arabtex and hebrew
297                 } else if (isRightToLeft() != prev.isRightToLeft() && !runparams.isFullUnicode()) {
298                         if (needs_cprotection) {
299                                 os << "\\cprotect";
300                                 count += 9;
301                         }
302                         if (isRightToLeft()) {
303                                 os << "\\R{";
304                                 count += 3;
305                         } else {
306                                 os << "\\L{";
307                                 count += 3;
308                         }
309                 } else if (!language()->babel().empty()) {
310                         string const tmp =
311                                 subst(lyxrc.language_command_local,
312                                       "$$lang", language()->babel());
313                         os << from_ascii(tmp);
314                         count += tmp.length();
315                         if (!lyxrc.language_command_end.empty())
316                                 pushLanguageName(language()->babel(), true);
317                 } else if (language()->encoding()->package() != Encoding::CJK) {
318                         os << '{';
319                         count += 1;
320                 }
321         }
322
323         if (language()->encoding()->package() == Encoding::CJK) {
324                 pair<bool, int> const c = switchEncoding(os.os(), bparams,
325                                 runparams, *(language()->encoding()));
326                 if (c.first) {
327                         open_encoding_ = true;
328                         count += c.second;
329                         runparams.encoding = language()->encoding();
330                 }
331         }
332
333         FontInfo f = bits_;
334         f.reduce(base.bits_);
335         FontInfo p = bits_;
336         p.reduce(prev.bits_);
337
338         if (f.size() != INHERIT_SIZE) {
339                 if (!runparams.find_effective()) {
340                         os << '{';
341                         ++count;
342                         os << '\\'
343                            << LaTeXSizeSwitchNames[f.size()] << termcmd;
344                         count += strlen(LaTeXSizeSwitchNames[f.size()]) + 1;
345                 }
346                 else {
347                         os << '\\'
348                            << LaTeXSizeSwitchNames[f.size()] << '{';
349                         count += strlen(LaTeXSizeSwitchNames[f.size()]) + 2;
350                 }
351         }
352         if (f.family() != INHERIT_FAMILY) {
353                 if (non_inherit_inset) {
354                         os << '{';
355                         ++count;
356                         os << '\\' << LaTeXFamilySwitchNames[f.family()] << termcmd;
357                         count += strlen(LaTeXFamilySwitchNames[f.family()]) + 1;
358                 } else {
359                         if (needs_cprotection) {
360                                 os << "\\cprotect";
361                                 count += 9;
362                         }
363                         os << '\\'
364                            << LaTeXFamilyCommandNames[f.family()]
365                            << '{';
366                         count += strlen(LaTeXFamilyCommandNames[f.family()]) + 2;
367                 }
368         }
369         if (f.series() != INHERIT_SERIES) {
370                 if (non_inherit_inset) {
371                         os << '{';
372                         ++count;
373                         os << '\\' << LaTeXSeriesSwitchNames[f.series()] << termcmd;
374                         count += strlen(LaTeXSeriesSwitchNames[f.series()]) + 1;
375                 } else {
376                         if (needs_cprotection) {
377                                 os << "\\cprotect";
378                                 count += 9;
379                         }
380                         os << '\\'
381                            << LaTeXSeriesCommandNames[f.series()]
382                            << '{';
383                         count += strlen(LaTeXSeriesCommandNames[f.series()]) + 2;
384                 }
385         }
386         if (f.shape() != INHERIT_SHAPE) {
387                 if (non_inherit_inset) {
388                         os << '{';
389                         ++count;
390                         os << '\\' << LaTeXShapeSwitchNames[f.shape()] << termcmd;
391                         count += strlen(LaTeXShapeSwitchNames[f.shape()]) + 1;
392                 } else {
393                         if (needs_cprotection) {
394                                 os << "\\cprotect";
395                                 count += 9;
396                         }
397                         os << '\\'
398                            << LaTeXShapeCommandNames[f.shape()]
399                            << '{';
400                         count += strlen(LaTeXShapeCommandNames[f.shape()]) + 2;
401                 }
402         }
403         if (f.color() != Color_inherit && f.color() != Color_ignore) {
404                 if (f.color() == Color_none && p.color() != Color_none) {
405                         // Color none: Close previous color, if any
406                         os << '}';
407                         ++count;
408                 } else if (f.color() != Color_none) {
409                         os << "\\textcolor{"
410                            << from_ascii(lcolor.getLaTeXName(f.color()))
411                            << "}{";
412                         count += lcolor.getLaTeXName(f.color()).length() + 13;
413                 }
414         }
415         // FIXME: uncomment this when we support background.
416         /*
417         if (f.background() != Color_inherit && f.background() != Color_ignore) {
418                 os << "\\textcolor{"
419                    << from_ascii(lcolor.getLaTeXName(f.background()))
420                    << "}{";
421                 count += lcolor.getLaTeXName(f.background()).length() + 13;
422                 env = true; //We have opened a new environment
423         }
424         */
425         // If the current language is Hebrew, Arabic, or Farsi
426         // the numbers are written Left-to-Right. ArabTeX package
427         // and bidi (polyglossia with XeTeX) reorder the number automatically
428         // but the packages used for Hebrew and Farsi (Arabi) do not.
429         if (!bparams.useBidiPackage(runparams)
430             && !runparams.pass_thru
431             && bits_.number() == FONT_ON
432             && prev.fontInfo().number() != FONT_ON
433             && (language()->lang() == "hebrew"
434                 || language()->lang() == "farsi"
435                 || language()->lang() == "arabic_arabi")) {
436                 if (runparams.use_polyglossia) {
437                         // LuaTeX/luabidi
438                         // \LR needs extra grouping
439                         // (possibly a LuaTeX bug)
440                         os << "{\\LR{";
441                         count += 6;
442                 } else if (!runparams.isFullUnicode()) {
443                         // not needed with babel/lua|xetex
444                         os << "{\\beginL ";
445                         count += 9;
446                 }
447         }
448         if (f.emph() == FONT_ON) {
449                 if (needs_cprotection) {
450                         os << "\\cprotect";
451                         count += 9;
452                 }
453                 os << "\\emph{";
454                 count += 6;
455         }
456         // \noun{} is a LyX special macro
457         if (f.noun() == FONT_ON) {
458                 if (needs_cprotection) {
459                         os << "\\cprotect";
460                         count += 9;
461                 }
462                 os << "\\noun{";
463                 count += 6;
464         }
465         // The ulem commands need to be on the deepest nesting level
466         // because ulem puts every nested group or macro in a box,
467         // which prevents linebreaks (#8424, #8733)
468         if (f.underbar() == FONT_ON) {
469                 if (needs_cprotection) {
470                         os << "\\cprotect";
471                         count += 9;
472                 }
473                 os << "\\uline{";
474                 count += 7;
475                 ++runparams.inulemcmd;
476         }
477         if (f.uuline() == FONT_ON) {
478                 if (needs_cprotection) {
479                         os << "\\cprotect";
480                         count += 9;
481                 }
482                 os << "\\uuline{";
483                 count += 8;
484                 ++runparams.inulemcmd;
485         }
486         if (f.strikeout() == FONT_ON) {
487                 if (needs_cprotection) {
488                         os << "\\cprotect";
489                         count += 9;
490                 }
491                 os << "\\sout{";
492                 count += 6;
493                 ++runparams.inulemcmd;
494         }
495         if (f.xout() == FONT_ON) {
496                 if (needs_cprotection) {
497                         os << "\\cprotect";
498                         count += 9;
499                 }
500                 os << "\\xout{";
501                 count += 6;
502                 ++runparams.inulemcmd;
503         }
504         if (f.uwave() == FONT_ON) {
505                 if (runparams.inulemcmd) {
506                         // needed with nested uwave in xout
507                         // see https://tex.stackexchange.com/a/263042
508                         os << "\\ULdepth=\\maxdimen";
509                         count += 18;
510                 }
511                 if (needs_cprotection) {
512                         os << "\\cprotect";
513                         count += 9;
514                 }
515                 os << "\\uwave{";
516                 count += 7;
517                 ++runparams.inulemcmd;
518         }
519         return count;
520 }
521
522
523 /// Writes ending block of LaTeX needed to close use of this font
524 // Returns number of chars written
525 // This one corresponds to latexWriteStartChanges(). (Asger)
526 int Font::latexWriteEndChanges(otexstream & os, BufferParams const & bparams,
527                                   OutputParams const & runparams,
528                                   Font const & base,
529                                   Font const & next,
530                                   bool & needPar,
531                                   bool closeLanguage) const
532 {
533         int count = 0;
534
535         // reduce the current font to changes against the base
536         // font (of the layout). We use a temporary for this to
537         // avoid changing this font instance, as that would break
538         FontInfo f = bits_;
539         f.reduce(base.bits_);
540
541         if (f.family() != INHERIT_FAMILY) {
542                 os << '}';
543                 ++count;
544         }
545         if (f.series() != INHERIT_SERIES) {
546                 os << '}';
547                 ++count;
548         }
549         if (f.shape() != INHERIT_SHAPE) {
550                 os << '}';
551                 ++count;
552         }
553         if (f.color() != Color_inherit && f.color() != Color_ignore && f.color() != Color_none) {
554                 os << '}';
555                 ++count;
556         }
557         if (f.emph() == FONT_ON) {
558                 os << '}';
559                 ++count;
560         }
561         if (f.noun() == FONT_ON) {
562                 os << '}';
563                 ++count;
564         }
565         if (f.size() != INHERIT_SIZE) {
566                 // We do not close size group in front of
567                 // insets with InheritFont() false (as opposed
568                 // to all other font properties) (#8384)
569                 if (needPar && !closeLanguage) {
570                         os << "\\par";
571                         count += 4;
572                         needPar = false;
573                 }
574                 os << '}';
575                 ++count;
576         }
577         if (f.underbar() == FONT_ON) {
578                 os << '}';
579                 ++count;
580                 --runparams.inulemcmd;
581         }
582         if (f.strikeout() == FONT_ON) {
583                 os << '}';
584                 ++count;
585                 --runparams.inulemcmd;
586         }
587         if (f.xout() == FONT_ON) {
588                 os << '}';
589                 ++count;
590                 --runparams.inulemcmd;
591         }
592         if (f.uuline() == FONT_ON) {
593                 os << '}';
594                 ++count;
595                 --runparams.inulemcmd;
596         }
597         if (f.uwave() == FONT_ON) {
598                 os << '}';
599                 ++count;
600                 --runparams.inulemcmd;
601         }
602
603         // If the current language is Hebrew, Arabic, or Farsi
604         // the numbers are written Left-to-Right. ArabTeX package
605         // and bidi (polyglossia with XeTeX) reorder the number automatically
606         // but the packages used for Hebrew and Farsi (Arabi) do not.
607         if (!bparams.useBidiPackage(runparams)
608             && !runparams.pass_thru
609             && bits_.number() == FONT_ON
610             && next.fontInfo().number() != FONT_ON
611             && (language()->lang() == "hebrew"
612                 || language()->lang() == "farsi"
613                 || language()->lang() == "arabic_arabi")) {
614                 if (runparams.use_polyglossia) {
615                         // LuaTeX/luabidi
616                         // luabidi's \LR needs extra grouping
617                         // (possibly a LuaTeX bug)
618                         os << "}}";
619                         count += 2;
620                 } else if (!runparams.isFullUnicode()) {
621                         // not needed with babel/lua|xetex
622                         os << "\\endL}";
623                         count += 6;
624                 }
625         }
626
627         if (open_encoding_) {
628                 // We need to close the encoding even if it does not change
629                 // to do correct environment nesting
630                 Encoding const * const ascii = encodings.fromLyXName("ascii");
631                 pair<bool, int> const c = switchEncoding(os.os(), bparams,
632                                 runparams, *ascii);
633                 LATTEST(c.first);
634                 count += c.second;
635                 runparams.encoding = ascii;
636                 open_encoding_ = false;
637         }
638
639         if (closeLanguage
640             && language() != base.language() && language() != next.language()
641             && (language()->encoding()->package() != Encoding::CJK)) {
642                 os << '}';
643                 ++count;
644                 bool const using_begin_end =
645                         runparams.use_polyglossia ||
646                                 !lyxrc.language_command_end.empty();
647                 if (using_begin_end && !languageStackEmpty())
648                         popLanguageName();
649         }
650
651         return count;
652 }
653
654
655 string Font::toString(bool const toggle) const
656 {
657         string const lang = (language() == reset_language)
658                 ? "reset" : language()->lang();
659
660         ostringstream os;
661         os << "family " << bits_.family() << '\n'
662            << "series " << bits_.series() << '\n'
663            << "shape " << bits_.shape() << '\n'
664            << "size " << bits_.size() << '\n'
665            << "emph " << bits_.emph() << '\n'
666            << "underbar " << bits_.underbar() << '\n'
667            << "strikeout " << bits_.strikeout() << '\n'
668            << "xout " << bits_.xout() << '\n'
669            << "uuline " << bits_.uuline() << '\n'
670            << "uwave " << bits_.uwave() << '\n'
671            << "noun " << bits_.noun() << '\n'
672            << "number " << bits_.number() << '\n'
673            << "nospellcheck " << bits_.nospellcheck() << '\n'
674            << "color " << bits_.color() << '\n'
675            << "language " << lang << '\n'
676            << "toggleall " << convert<string>(toggle);
677         return os.str();
678 }
679
680
681 bool Font::fromString(string const & data, bool & toggle)
682 {
683         istringstream is(data);
684         Lexer lex;
685         lex.setStream(is);
686
687         int nset = 0;
688         while (lex.isOK()) {
689                 string token;
690                 if (lex.next())
691                         token = lex.getString();
692
693                 if (token.empty() || !lex.next())
694                         break;
695
696                 if (token == "family") {
697                         int const next = lex.getInteger();
698                         bits_.setFamily(FontFamily(next));
699
700                 } else if (token == "series") {
701                         int const next = lex.getInteger();
702                         bits_.setSeries(FontSeries(next));
703
704                 } else if (token == "shape") {
705                         int const next = lex.getInteger();
706                         bits_.setShape(FontShape(next));
707
708                 } else if (token == "size") {
709                         int const next = lex.getInteger();
710                         bits_.setSize(FontSize(next));
711                 // FIXME: shall style be handled there? Probably not.
712                 } else if (token == "emph" || token == "underbar"
713                         || token == "noun" || token == "number"
714                         || token == "uuline" || token == "uwave"
715                         || token == "strikeout" || token == "xout"
716                         || token == "nospellcheck") {
717
718                         int const next = lex.getInteger();
719                         FontState const misc = FontState(next);
720
721                         if (token == "emph")
722                                 bits_.setEmph(misc);
723                         else if (token == "underbar")
724                                 bits_.setUnderbar(misc);
725                         else if (token == "strikeout")
726                                 bits_.setStrikeout(misc);
727                         else if (token == "xout")
728                                 bits_.setXout(misc);
729                         else if (token == "uuline")
730                                 bits_.setUuline(misc);
731                         else if (token == "uwave")
732                                 bits_.setUwave(misc);
733                         else if (token == "noun")
734                                 bits_.setNoun(misc);
735                         else if (token == "number")
736                                 bits_.setNumber(misc);
737                         else if (token == "nospellcheck")
738                                 bits_.setNoSpellcheck(misc);
739
740                 } else if (token == "color") {
741                         int const next = lex.getInteger();
742                         bits_.setColor(ColorCode(next));
743
744                 /**
745                 } else if (token == "background") {
746                         int const next = lex.getInteger();
747                         bits_.setBackground(ColorCode(next));
748                 */
749
750                 } else if (token == "language") {
751                         string const next = lex.getString();
752                         setLanguage(languages.getLanguage(next));
753
754                 } else if (token == "toggleall") {
755                         toggle = lex.getBool();
756
757                 } else {
758                         // Unrecognised token
759                         break;
760                 }
761
762                 ++nset;
763         }
764         return (nset > 0);
765 }
766
767
768 void Font::validate(LaTeXFeatures & features) const
769 {
770         BufferParams const & bparams = features.bufferParams();
771         Language const * doc_language = bparams.language;
772
773         if (bits_.noun() == FONT_ON) {
774                 LYXERR(Debug::OUTFILE, "font.noun: " << bits_.noun());
775                 features.require("noun");
776                 LYXERR(Debug::OUTFILE, "Noun enabled. Font: " << to_utf8(stateText()));
777         }
778         if (bits_.underbar() == FONT_ON) {
779                 LYXERR(Debug::OUTFILE, "font.underline: " << bits_.underbar());
780                 features.require("ulem");
781                 LYXERR(Debug::OUTFILE, "Underline enabled. Font: " << to_utf8(stateText()));
782         }
783         if (bits_.strikeout() == FONT_ON) {
784                 LYXERR(Debug::OUTFILE, "font.strikeout: " << bits_.strikeout());
785                 features.require("ulem");
786                 LYXERR(Debug::OUTFILE, "Strike out enabled. Font: " << to_utf8(stateText()));
787         }
788         if (bits_.xout() == FONT_ON) {
789                 LYXERR(Debug::OUTFILE, "font.xout: " << bits_.xout());
790                 features.require("ulem");
791                 LYXERR(Debug::OUTFILE, "Cross out enabled. Font: " << to_utf8(stateText()));
792         }
793         if (bits_.uuline() == FONT_ON) {
794                 LYXERR(Debug::OUTFILE, "font.uuline: " << bits_.uuline());
795                 features.require("ulem");
796                 LYXERR(Debug::OUTFILE, "Double underline enabled. Font: " << to_utf8(stateText()));
797         }
798         if (bits_.uwave() == FONT_ON) {
799                 LYXERR(Debug::OUTFILE, "font.uwave: " << bits_.uwave());
800                 features.require("ulem");
801                 LYXERR(Debug::OUTFILE, "Wavy underline enabled. Font: " << to_utf8(stateText()));
802         }
803         switch (bits_.color()) {
804                 case Color_none:
805                 case Color_inherit:
806                 case Color_ignore:
807                         // probably we should put here all interface colors used for
808                         // font displaying! For now I just add this ones I know of (Jug)
809                 case Color_latex:
810                 case Color_notelabel:
811                         break;
812                 case Color_brown:
813                 case Color_darkgray:
814                 case Color_gray:
815                 case Color_lightgray:
816                 case Color_lime:
817                 case Color_olive:
818                 case Color_orange:
819                 case Color_pink:
820                 case Color_purple:
821                 case Color_teal:
822                 case Color_violet:
823                         features.require("xcolor");
824                         break;
825                 default:
826                         features.require("color");
827                         LYXERR(Debug::OUTFILE, "Color enabled. Font: " << to_utf8(stateText()));
828         }
829
830         // FIXME: Do something for background and soul package?
831
832         if (((features.usePolyglossia() && lang_->polyglossia() != doc_language->polyglossia())
833              || (features.useBabel() && lang_->babel() != doc_language->babel())
834              || (doc_language->encoding()->package() == Encoding::CJK && lang_ != doc_language))
835             && lang_ != ignore_language
836             && lang_ != latex_language)
837         {
838                 features.useLanguage(lang_);
839                 LYXERR(Debug::OUTFILE, "Found language " << lang_->lang());
840         }
841 }
842
843
844 ostream & operator<<(ostream & os, FontState fms)
845 {
846         return os << int(fms);
847 }
848
849
850 ostream & operator<<(ostream & os, FontInfo const & f)
851 {
852         return os << "font:"
853                 << " family " << f.family()
854                 << " series " << f.series()
855                 << " shape " << f.shape()
856                 << " size " << f.size()
857                 << " style " << f.style()
858                 << " color " << f.color()
859                 // FIXME: uncomment this when we support background.
860                 //<< " background " << f.background()
861                 << " emph " << f.emph()
862                 << " underbar " << f.underbar()
863                 << " strikeout " << f.strikeout()
864                 << " xout " << f.xout()
865                 << " uuline " << f.uuline()
866                 << " uwave " << f.uwave()
867                 << " noun " << f.noun()
868                 << " number " << f.number()
869                 << " nospellcheck " << f.nospellcheck();
870 }
871
872
873 ostream & operator<<(ostream & os, Font const & font)
874 {
875         return os << font.bits_
876                 << " lang: " << (font.lang_ ? font.lang_->lang() : "");
877 }
878
879
880 } // namespace lyx