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