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