]> git.lyx.org Git - features.git/blob - src/Font.cpp
No need for catcode change here
[features.git] / src / Font.cpp
1 /**
2  * \file src/Font.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Lars Gullik Bjønnes
7  * \author Jean-Marc Lasgouttes
8  * \author Angus Leeming
9  * \author André Pönitz
10  * \author Dekel Tsur
11  *
12  * Full author contact details are available in file CREDITS.
13  */
14
15 #include <config.h>
16
17 #include "Font.h"
18
19 #include "BufferParams.h" // stateText
20 #include "ColorSet.h"
21 #include "Encoding.h"
22 #include "Language.h"
23 #include "LaTeXFeatures.h"
24 #include "Lexer.h"
25 #include "LyXRC.h"
26 #include "output_latex.h"
27 #include "OutputParams.h"
28 #include "texstream.h"
29
30 #include "support/lassert.h"
31 #include "support/convert.h"
32 #include "support/debug.h"
33 #include "support/gettext.h"
34 #include "support/lstrings.h"
35
36 #include <cstring>
37
38 using namespace std;
39 using namespace lyx::support;
40
41 namespace lyx {
42
43 //
44 // Strings used to read and write .lyx format files
45 //
46 // These are defined in FontInfo.cpp
47 extern char const * LyXFamilyNames[NUM_FAMILIES + 2];
48 extern char const * LyXSeriesNames[NUM_SERIES + 2];
49 extern char const * LyXShapeNames[NUM_SHAPE + 2];
50 extern char const * LyXSizeNames[NUM_SIZE + 4];
51 extern char const * LyXMiscNames[5];
52 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 * document_language,
118                      bool toggleall)
119 {
120         bits_.update(newfont.fontInfo(), toggleall);
121
122         if (newfont.language() == language() && toggleall)
123                 if (language() == document_language)
124                         setLanguage(default_language);
125                 else
126                         setLanguage(document_language);
127         else if (newfont.language() == reset_language)
128                 setLanguage(document_language);
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 const & non_inherit_inset,
232                                     bool const & 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()) {
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                         if (needs_cprotection && !prefixIs(tmp, "\\begin{")) {
314                                 os << "\\cprotect";
315                                 count += 9;
316                         }
317                         os << from_ascii(tmp);
318                         count += tmp.length();
319                         if (!lyxrc.language_command_end.empty())
320                                 pushLanguageName(language()->babel(), true);
321                 } else if (language()->encoding()->package() != Encoding::CJK) {
322                         os << '{';
323                         count += 1;
324                 }
325         }
326
327         if (language()->encoding()->package() == Encoding::CJK) {
328                 pair<bool, int> const c = switchEncoding(os.os(), bparams,
329                                 runparams, *(language()->encoding()));
330                 if (c.first) {
331                         open_encoding_ = true;
332                         count += c.second;
333                         runparams.encoding = language()->encoding();
334                 }
335         }
336
337         FontInfo f = bits_;
338         f.reduce(base.bits_);
339         FontInfo p = bits_;
340         p.reduce(prev.bits_);
341
342         if (f.size() != INHERIT_SIZE) {
343                 os << '{';
344                 ++count;
345                 os << '\\'
346                    << LaTeXSizeSwitchNames[f.size()] << termcmd;
347                 count += strlen(LaTeXSizeSwitchNames[f.size()]) + 1;
348         }
349         if (f.family() != INHERIT_FAMILY) {
350                 if (non_inherit_inset) {
351                         os << '{';
352                         ++count;
353                         os << '\\' << LaTeXFamilySwitchNames[f.family()] << termcmd;
354                         count += strlen(LaTeXFamilySwitchNames[f.family()]) + 1;
355                 } else {
356                         if (needs_cprotection) {
357                                 os << "\\cprotect";
358                                 count += 9;
359                         }
360                         os << '\\'
361                            << LaTeXFamilyCommandNames[f.family()]
362                            << '{';
363                         count += strlen(LaTeXFamilyCommandNames[f.family()]) + 2;
364                 }
365         }
366         if (f.series() != INHERIT_SERIES) {
367                 if (non_inherit_inset) {
368                         os << '{';
369                         ++count;
370                         os << '\\' << LaTeXSeriesSwitchNames[f.series()] << termcmd;
371                         count += strlen(LaTeXSeriesSwitchNames[f.series()]) + 1;
372                 } else {
373                         if (needs_cprotection) {
374                                 os << "\\cprotect";
375                                 count += 9;
376                         }
377                         os << '\\'
378                            << LaTeXSeriesCommandNames[f.series()]
379                            << '{';
380                         count += strlen(LaTeXSeriesCommandNames[f.series()]) + 2;
381                 }
382         }
383         if (f.shape() != INHERIT_SHAPE) {
384                 if (non_inherit_inset) {
385                         os << '{';
386                         ++count;
387                         os << '\\' << LaTeXShapeSwitchNames[f.shape()] << termcmd;
388                         count += strlen(LaTeXShapeSwitchNames[f.shape()]) + 1;
389                 } else {
390                         if (needs_cprotection) {
391                                 os << "\\cprotect";
392                                 count += 9;
393                         }
394                         os << '\\'
395                            << LaTeXShapeCommandNames[f.shape()]
396                            << '{';
397                         count += strlen(LaTeXShapeCommandNames[f.shape()]) + 2;
398                 }
399         }
400         if (f.color() != Color_inherit && f.color() != Color_ignore) {
401                 if (f.color() == Color_none && p.color() != Color_none) {
402                         // Color none: Close previous color, if any
403                         os << '}';
404                         ++count;
405                 } else if (f.color() != Color_none) {
406                         if (needs_cprotection) {
407                                 os << "\\cprotect";
408                                 count += 9;
409                         }
410                         os << "\\textcolor{"
411                            << from_ascii(lcolor.getLaTeXName(f.color()))
412                            << "}{";
413                         count += lcolor.getLaTeXName(f.color()).length() + 13;
414                 }
415         }
416         // FIXME: uncomment this when we support background.
417         /*
418         if (f.background() != Color_inherit && f.background() != Color_ignore) {
419                 os << "\\textcolor{"
420                    << from_ascii(lcolor.getLaTeXName(f.background()))
421                    << "}{";
422                 count += lcolor.getLaTeXName(f.background()).length() + 13;
423                 env = true; //We have opened a new environment
424         }
425         */
426         // If the current language is Hebrew, Arabic, or Farsi
427         // the numbers are written Left-to-Right. ArabTeX package
428         // and bidi (polyglossia with XeTeX) reorder the number automatically
429         // but the packages used for Hebrew and Farsi (Arabi) do not.
430         if (!runparams.useBidiPackage()
431             && !runparams.pass_thru
432             && bits_.number() == FONT_ON
433             && prev.fontInfo().number() != FONT_ON
434             && (language()->lang() == "hebrew"
435                 || language()->lang() == "farsi"
436                 || language()->lang() == "arabic_arabi")) {
437                 if (runparams.use_polyglossia) {
438                         // LuaTeX/luabidi
439                         os << "\\LR{";
440                         count += 5;
441                 } else {
442                         os << "{\\beginL ";
443                         count += 9;
444                 }
445         }
446         if (f.emph() == FONT_ON) {
447                 if (needs_cprotection) {
448                         os << "\\cprotect";
449                         count += 9;
450                 }
451                 os << "\\emph{";
452                 count += 6;
453         }
454         // \noun{} is a LyX special macro
455         if (f.noun() == FONT_ON) {
456                 if (needs_cprotection) {
457                         os << "\\cprotect";
458                         count += 9;
459                 }
460                 os << "\\noun{";
461                 count += 6;
462         }
463         // The ulem commands need to be on the deepest nesting level
464         // because ulem puts every nested group or macro in a box,
465         // which prevents linebreaks (#8424, #8733)
466         if (f.underbar() == FONT_ON) {
467                 if (needs_cprotection) {
468                         os << "\\cprotect";
469                         count += 9;
470                 }
471                 os << "\\uline{";
472                 count += 7;
473                 ++runparams.inulemcmd;
474         }
475         if (f.uuline() == FONT_ON) {
476                 if (needs_cprotection) {
477                         os << "\\cprotect";
478                         count += 9;
479                 }
480                 os << "\\uuline{";
481                 count += 8;
482                 ++runparams.inulemcmd;
483         }
484         if (f.strikeout() == FONT_ON) {
485                 if (needs_cprotection) {
486                         os << "\\cprotect";
487                         count += 9;
488                 }
489                 os << "\\sout{";
490                 count += 6;
491                 ++runparams.inulemcmd;
492         }
493         if (f.xout() == FONT_ON) {
494                 if (needs_cprotection) {
495                         os << "\\cprotect";
496                         count += 9;
497                 }
498                 os << "\\xout{";
499                 count += 6;
500                 ++runparams.inulemcmd;
501         }
502         if (f.uwave() == FONT_ON) {
503                 if (runparams.inulemcmd) {
504                         // needed with nested uwave in xout
505                         // see https://tex.stackexchange.com/a/263042
506                         os << "\\ULdepth=1000pt";
507                         count += 15;
508                 }
509                 if (needs_cprotection) {
510                         os << "\\cprotect";
511                         count += 9;
512                 }
513                 os << "\\uwave{";
514                 count += 7;
515                 ++runparams.inulemcmd;
516         }
517         return count;
518 }
519
520
521 /// Writes ending block of LaTeX needed to close use of this font
522 // Returns number of chars written
523 // This one corresponds to latexWriteStartChanges(). (Asger)
524 int Font::latexWriteEndChanges(otexstream & os, BufferParams const & bparams,
525                                   OutputParams const & runparams,
526                                   Font const & base,
527                                   Font const & next,
528                                   bool & needPar,
529                                   bool const & closeLanguage) const
530 {
531         int count = 0;
532
533         // reduce the current font to changes against the base
534         // font (of the layout). We use a temporary for this to
535         // avoid changing this font instance, as that would break
536         FontInfo f = bits_;
537         f.reduce(base.bits_);
538
539         if (f.family() != INHERIT_FAMILY) {
540                 os << '}';
541                 ++count;
542         }
543         if (f.series() != INHERIT_SERIES) {
544                 os << '}';
545                 ++count;
546         }
547         if (f.shape() != INHERIT_SHAPE) {
548                 os << '}';
549                 ++count;
550         }
551         if (f.color() != Color_inherit && f.color() != Color_ignore && f.color() != Color_none) {
552                 os << '}';
553                 ++count;
554         }
555         if (f.emph() == FONT_ON) {
556                 os << '}';
557                 ++count;
558         }
559         if (f.noun() == FONT_ON) {
560                 os << '}';
561                 ++count;
562         }
563         if (f.size() != INHERIT_SIZE) {
564                 // We do not close size group in front of
565                 // insets with InheritFont() false (as opposed
566                 // to all other font properties) (#8384)
567                 if (needPar && !closeLanguage) {
568                         os << "\\par";
569                         count += 4;
570                         needPar = false;
571                 }
572                 os << '}';
573                 ++count;
574         }
575         if (f.underbar() == FONT_ON) {
576                 os << '}';
577                 ++count;
578                 --runparams.inulemcmd;
579         }
580         if (f.strikeout() == FONT_ON) {
581                 os << '}';
582                 ++count;
583                 --runparams.inulemcmd;
584         }
585         if (f.xout() == FONT_ON) {
586                 os << '}';
587                 ++count;
588                 --runparams.inulemcmd;
589         }
590         if (f.uuline() == FONT_ON) {
591                 os << '}';
592                 ++count;
593                 --runparams.inulemcmd;
594         }
595         if (f.uwave() == FONT_ON) {
596                 os << '}';
597                 ++count;
598                 --runparams.inulemcmd;
599         }
600
601         // If the current language is Hebrew, Arabic, or Farsi
602         // the numbers are written Left-to-Right. ArabTeX package
603         // and bidi (polyglossia with XeTeX) reorder the number automatically
604         // but the packages used for Hebrew and Farsi (Arabi) do not.
605         if (!runparams.useBidiPackage()
606             && !runparams.pass_thru
607             && bits_.number() == FONT_ON
608             && next.fontInfo().number() != FONT_ON
609             && (language()->lang() == "hebrew"
610                 || language()->lang() == "farsi"
611                 || language()->lang() == "arabic_arabi")) {
612                 if (runparams.use_polyglossia) {
613                         // LuaTeX/luabidi
614                         os << "}";
615                         count += 1;
616                 } else {
617                         os << "\\endL}";
618                         count += 6;
619                 }
620         }
621
622         if (open_encoding_) {
623                 // We need to close the encoding even if it does not change
624                 // to do correct environment nesting
625                 Encoding const * const ascii = encodings.fromLyXName("ascii");
626                 pair<bool, int> const c = switchEncoding(os.os(), bparams,
627                                 runparams, *ascii);
628                 LATTEST(c.first);
629                 count += c.second;
630                 runparams.encoding = ascii;
631                 open_encoding_ = false;
632         }
633
634         if (closeLanguage
635             && language() != base.language() && language() != next.language()
636             && (language()->encoding()->package() != Encoding::CJK)) {
637                 os << '}';
638                 ++count;
639                 bool const using_begin_end =
640                         runparams.use_polyglossia ||
641                                 !lyxrc.language_command_end.empty();
642                 if (using_begin_end)
643                         popLanguageName();
644         }
645
646         return count;
647 }
648
649
650 string Font::toString(bool const toggle) const
651 {
652         string const lang = (language() == reset_language)
653                 ? "reset" : language()->lang();
654
655         ostringstream os;
656         os << "family " << bits_.family() << '\n'
657            << "series " << bits_.series() << '\n'
658            << "shape " << bits_.shape() << '\n'
659            << "size " << bits_.size() << '\n'
660            << "emph " << bits_.emph() << '\n'
661            << "underbar " << bits_.underbar() << '\n'
662            << "strikeout " << bits_.strikeout() << '\n'
663            << "xout " << bits_.xout() << '\n'
664            << "uuline " << bits_.uuline() << '\n'
665            << "uwave " << bits_.uwave() << '\n'
666            << "noun " << bits_.noun() << '\n'
667            << "number " << bits_.number() << '\n'
668            << "nospellcheck " << bits_.nospellcheck() << '\n'
669            << "color " << bits_.color() << '\n'
670            << "language " << lang << '\n'
671            << "toggleall " << convert<string>(toggle);
672         return os.str();
673 }
674
675
676 bool Font::fromString(string const & data, bool & toggle)
677 {
678         istringstream is(data);
679         Lexer lex;
680         lex.setStream(is);
681
682         int nset = 0;
683         while (lex.isOK()) {
684                 string token;
685                 if (lex.next())
686                         token = lex.getString();
687
688                 if (token.empty() || !lex.next())
689                         break;
690
691                 if (token == "family") {
692                         int const next = lex.getInteger();
693                         bits_.setFamily(FontFamily(next));
694
695                 } else if (token == "series") {
696                         int const next = lex.getInteger();
697                         bits_.setSeries(FontSeries(next));
698
699                 } else if (token == "shape") {
700                         int const next = lex.getInteger();
701                         bits_.setShape(FontShape(next));
702
703                 } else if (token == "size") {
704                         int const next = lex.getInteger();
705                         bits_.setSize(FontSize(next));
706                 // FIXME: shall style be handled there? Probably not.
707                 } else if (token == "emph" || token == "underbar"
708                         || token == "noun" || token == "number"
709                         || token == "uuline" || token == "uwave"
710                         || token == "strikeout" || token == "xout"
711                         || token == "nospellcheck") {
712
713                         int const next = lex.getInteger();
714                         FontState const misc = FontState(next);
715
716                         if (token == "emph")
717                                 bits_.setEmph(misc);
718                         else if (token == "underbar")
719                                 bits_.setUnderbar(misc);
720                         else if (token == "strikeout")
721                                 bits_.setStrikeout(misc);
722                         else if (token == "xout")
723                                 bits_.setXout(misc);
724                         else if (token == "uuline")
725                                 bits_.setUuline(misc);
726                         else if (token == "uwave")
727                                 bits_.setUwave(misc);
728                         else if (token == "noun")
729                                 bits_.setNoun(misc);
730                         else if (token == "number")
731                                 bits_.setNumber(misc);
732                         else if (token == "nospellcheck")
733                                 bits_.setNoSpellcheck(misc);
734
735                 } else if (token == "color") {
736                         int const next = lex.getInteger();
737                         bits_.setColor(ColorCode(next));
738
739                 /**
740                 } else if (token == "background") {
741                         int const next = lex.getInteger();
742                         bits_.setBackground(ColorCode(next));
743                 */
744
745                 } else if (token == "language") {
746                         string const next = lex.getString();
747                         setLanguage(languages.getLanguage(next));
748
749                 } else if (token == "toggleall") {
750                         toggle = lex.getBool();
751
752                 } else {
753                         // Unrecognised token
754                         break;
755                 }
756
757                 ++nset;
758         }
759         return (nset > 0);
760 }
761
762
763 void Font::validate(LaTeXFeatures & features) const
764 {
765         BufferParams const & bparams = features.bufferParams();
766         Language const * doc_language = bparams.language;
767
768         if (bits_.noun() == FONT_ON) {
769                 LYXERR(Debug::LATEX, "font.noun: " << bits_.noun());
770                 features.require("noun");
771                 LYXERR(Debug::LATEX, "Noun enabled. Font: " << to_utf8(stateText()));
772         }
773         if (bits_.underbar() == FONT_ON) {
774                 LYXERR(Debug::LATEX, "font.underline: " << bits_.underbar());
775                 features.require("ulem");
776                 LYXERR(Debug::LATEX, "Underline enabled. Font: " << to_utf8(stateText()));
777         }
778         if (bits_.strikeout() == FONT_ON) {
779                 LYXERR(Debug::LATEX, "font.strikeout: " << bits_.strikeout());
780                 features.require("ulem");
781                 LYXERR(Debug::LATEX, "Strike out enabled. Font: " << to_utf8(stateText()));
782         }
783         if (bits_.xout() == FONT_ON) {
784                 LYXERR(Debug::LATEX, "font.xout: " << bits_.xout());
785                 features.require("ulem");
786                 LYXERR(Debug::LATEX, "Cross out enabled. Font: " << to_utf8(stateText()));
787         }
788         if (bits_.uuline() == FONT_ON) {
789                 LYXERR(Debug::LATEX, "font.uuline: " << bits_.uuline());
790                 features.require("ulem");
791                 LYXERR(Debug::LATEX, "Double underline enabled. Font: " << to_utf8(stateText()));
792         }
793         if (bits_.uwave() == FONT_ON) {
794                 LYXERR(Debug::LATEX, "font.uwave: " << bits_.uwave());
795                 features.require("ulem");
796                 LYXERR(Debug::LATEX, "Wavy underline enabled. Font: " << to_utf8(stateText()));
797         }
798         switch (bits_.color()) {
799                 case Color_none:
800                 case Color_inherit:
801                 case Color_ignore:
802                         // probably we should put here all interface colors used for
803                         // font displaying! For now I just add this ones I know of (Jug)
804                 case Color_latex:
805                 case Color_notelabel:
806                         break;
807                 case Color_brown:
808                 case Color_darkgray:
809                 case Color_gray:
810                 case Color_lightgray:
811                 case Color_lime:
812                 case Color_olive:
813                 case Color_orange:
814                 case Color_pink:
815                 case Color_purple:
816                 case Color_teal:
817                 case Color_violet:
818                         features.require("xcolor");
819                         break;
820                 default:
821                         features.require("color");
822                         LYXERR(Debug::LATEX, "Color enabled. Font: " << to_utf8(stateText()));
823         }
824
825         // FIXME: Do something for background and soul package?
826
827         if (((features.usePolyglossia() && lang_->polyglossia() != doc_language->polyglossia())
828              || (features.useBabel() && lang_->babel() != doc_language->babel())
829              || (doc_language->encoding()->package() == Encoding::CJK && lang_ != doc_language))
830             && lang_ != ignore_language
831             && lang_ != latex_language)
832         {
833                 features.useLanguage(lang_);
834                 LYXERR(Debug::LATEX, "Found language " << lang_->lang());
835         }
836 }
837
838
839 ostream & operator<<(ostream & os, FontState fms)
840 {
841         return os << int(fms);
842 }
843
844
845 ostream & operator<<(ostream & os, FontInfo const & f)
846 {
847         return os << "font:"
848                 << " family " << f.family()
849                 << " series " << f.series()
850                 << " shape " << f.shape()
851                 << " size " << f.size()
852                 << " style " << f.style()
853                 << " color " << f.color()
854                 // FIXME: uncomment this when we support background.
855                 //<< " background " << f.background()
856                 << " emph " << f.emph()
857                 << " underbar " << f.underbar()
858                 << " strikeout " << f.strikeout()
859                 << " xout " << f.xout()
860                 << " uuline " << f.uuline()
861                 << " uwave " << f.uwave()
862                 << " noun " << f.noun()
863                 << " number " << f.number()
864                 << " nospellcheck " << f.nospellcheck();
865 }
866
867
868 ostream & operator<<(ostream & os, Font const & font)
869 {
870         return os << font.bits_
871                 << " lang: " << (font.lang_ ? font.lang_->lang() : nullptr);
872 }
873
874
875 } // namespace lyx