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