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