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