]> git.lyx.org Git - lyx.git/blob - src/lyxfont.C
Trivial fixes to some warnings thrown up by MSVS.Net 2003.
[lyx.git] / src / lyxfont.C
1 /**
2  * \file src/lyxfont.C
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 "lyxfont.h"
18
19 #include "bufferparams.h" // stateText
20 #include "debug.h"
21 #include "gettext.h"
22 #include "language.h"
23 #include "LColor.h"
24 #include "lyxlex.h"
25 #include "lyxrc.h"
26
27 #include "support/lstrings.h"
28
29 #include <sstream>
30
31 using lyx::support::ascii_lowercase;
32 using lyx::support::bformat;
33 using lyx::support::rtrim;
34 using lyx::support::subst;
35
36 using std::endl;
37 using std::string;
38 using std::ostream;
39 using std::ostringstream;
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[LyXFont::NUM_FAMILIES + 2 /* default & error */] =
52 { N_("Roman"), N_("Sans Serif"), N_("Typewriter"), N_("Symbol"),
53   "cmr", "cmsy", "cmm", "cmex", "msa", "msb", "eufrak", "wasy",
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[LyXFont::NUM_FAMILIES + 2 /* default & error */] =
76 { "roman", "sans", "typewriter", "symbol",
77   "cmr", "cmsy", "cmm", "cmex", "msa", "msb", "eufrak", "wasy",
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 LyXFont::FontBits LyXFont::sane = {
115         ROMAN_FAMILY,
116         MEDIUM_SERIES,
117         UP_SHAPE,
118         SIZE_NORMAL,
119         LColor::none,
120         OFF,
121         OFF,
122         OFF,
123         OFF };
124
125 // Initialize static member
126 LyXFont::FontBits LyXFont::inherit = {
127         INHERIT_FAMILY,
128         INHERIT_SERIES,
129         INHERIT_SHAPE,
130         INHERIT_SIZE,
131         LColor::inherit,
132         INHERIT,
133         INHERIT,
134         INHERIT,
135         OFF };
136
137 // Initialize static member
138 LyXFont::FontBits LyXFont::ignore = {
139         IGNORE_FAMILY,
140         IGNORE_SERIES,
141         IGNORE_SHAPE,
142         IGNORE_SIZE,
143         LColor::ignore,
144         IGNORE,
145         IGNORE,
146         IGNORE,
147         IGNORE };
148
149
150 bool operator==(LyXFont::FontBits const & lhs,
151                 LyXFont::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 LyXFont::LyXFont()
166         : bits(sane), lang(default_language)
167 {}
168
169
170 LyXFont::LyXFont(LyXFont::FONT_INIT1)
171         : bits(inherit), lang(default_language)
172 {}
173
174
175 LyXFont::LyXFont(LyXFont::FONT_INIT2)
176         : bits(ignore), lang(ignore_language)
177 {}
178
179
180 LyXFont::LyXFont(LyXFont::FONT_INIT3)
181         : bits(sane), lang(default_language)
182 {}
183
184
185 LyXFont::LyXFont(LyXFont::FONT_INIT1, Language const * l)
186         : bits(inherit), lang(l)
187 {}
188
189
190 LyXFont::LyXFont(LyXFont::FONT_INIT2, Language const * l)
191         : bits(ignore), lang(l)
192 {}
193
194
195 LyXFont::LyXFont(LyXFont::FONT_INIT3, Language const * l)
196         : bits(sane), lang(l)
197 {}
198
199
200
201 LColor_color LyXFont::color() const
202 {
203         return LColor::color(bits.color);
204 }
205
206
207 bool LyXFont::isRightToLeft() const
208 {
209         return lang->RightToLeft();
210 }
211
212
213 bool LyXFont::isVisibleRightToLeft() const
214 {
215         return (lang->RightToLeft() &&
216                 number() != ON);
217 }
218
219
220 void LyXFont::setFamily(LyXFont::FONT_FAMILY f)
221 {
222         bits.family = f;
223 }
224
225
226 void LyXFont::setSeries(LyXFont::FONT_SERIES s)
227 {
228         bits.series = s;
229 }
230
231
232 void LyXFont::setShape(LyXFont::FONT_SHAPE s)
233 {
234         bits.shape = s;
235 }
236
237
238 void LyXFont::setSize(LyXFont::FONT_SIZE s)
239 {
240         bits.size = s;
241 }
242
243
244 void LyXFont::setEmph(LyXFont::FONT_MISC_STATE e)
245 {
246         bits.emph = e;
247 }
248
249
250 void LyXFont::setUnderbar(LyXFont::FONT_MISC_STATE u)
251 {
252         bits.underbar = u;
253 }
254
255
256 void LyXFont::setNoun(LyXFont::FONT_MISC_STATE n)
257 {
258         bits.noun = n;
259 }
260
261
262 void LyXFont::setColor(LColor_color c)
263 {
264         bits.color = int(c);
265 }
266
267
268 void LyXFont::setLanguage(Language const * l)
269 {
270         lang = l;
271 }
272
273
274 void LyXFont::setNumber(LyXFont::FONT_MISC_STATE n)
275 {
276         bits.number = n;
277 }
278
279
280 /// Decreases font size by one
281 LyXFont & LyXFont::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 LyXFont::decSize on INCREASE_SIZE" << endl;
296                 break;
297         case DECREASE_SIZE:
298                 lyxerr <<"Can't LyXFont::decSize on DECREASE_SIZE" << endl;
299                 break;
300         case INHERIT_SIZE:
301                 lyxerr <<"Can't LyXFont::decSize on INHERIT_SIZE" << endl;
302                 break;
303         case IGNORE_SIZE:
304                 lyxerr <<"Can't LyXFont::decSize on IGNORE_SIZE" << endl;
305                 break;
306         }
307         return *this;
308 }
309
310
311 /// Increases font size by one
312 LyXFont & LyXFont::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 LyXFont::incSize on INCREASE_SIZE" << endl;
327                 break;
328         case DECREASE_SIZE:
329                 lyxerr <<"Can't LyXFont::incSize on DECREASE_SIZE" << endl;
330                 break;
331         case INHERIT_SIZE:
332                 lyxerr <<"Can't LyXFont::incSize on INHERIT_SIZE" << endl;
333                 break;
334         case IGNORE_SIZE:
335                 lyxerr <<"Can't LyXFont::incSize on IGNORE_SIZE" << endl;
336                 break;
337         }
338         return *this;
339 }
340
341
342 /// Updates a misc setting according to request
343 LyXFont::FONT_MISC_STATE LyXFont::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 <<"LyXFont::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 LyXFont::update(LyXFont 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(LColor::inherit); // toggle 'back'
421         else if (newfont.color() != LColor::ignore)
422                 setColor(newfont.color());
423 }
424
425
426 /// Reduce font to fall back to template where possible
427 void LyXFont::reduce(LyXFont 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(LColor::inherit);
445 }
446
447
448 /// Realize font from a template
449 LyXFont & LyXFont::realize(LyXFont 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 == LColor::inherit)
478                 bits.color = tmplt.bits.color;
479
480         return *this;
481 }
482
483
484 /// Is font resolved?
485 bool LyXFont::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() != LColor::inherit);
492 }
493
494
495 /// Build GUI description of font state
496 string const LyXFont::stateText(BufferParams * params) const
497 {
498         ostringstream os;
499         if (family() != INHERIT_FAMILY)
500                 os << _(GUIFamilyNames[family()]) << ", ";
501         if (series() != INHERIT_SERIES)
502                 os << _(GUISeriesNames[series()]) << ", ";
503         if (shape() != INHERIT_SHAPE)
504                 os << _(GUIShapeNames[shape()]) << ", ";
505         if (size() != INHERIT_SIZE)
506                 os << _(GUISizeNames[size()]) << ", ";
507         if (color() != LColor::inherit)
508                 os << lcolor.getGUIName(color()) << ", ";
509         if (emph() != INHERIT)
510                 os << bformat(_("Emphasis %1$s, "), _(GUIMiscNames[emph()]));
511         if (underbar() != INHERIT)
512                 os << bformat(_("Underline %1$s, "), _(GUIMiscNames[underbar()]));
513         if (noun() != INHERIT)
514                 os << bformat(_("Noun %1$s, "), _(GUIMiscNames[noun()]));
515         if (bits == inherit)
516                 os << _("Default") << ", ";
517         if (!params || (language() != params->language))
518                 os << bformat(_("Language: %1$s, "), _(language()->display()));
519         if (number() != OFF)
520                 os << bformat(_("  Number %1$s"), _(GUIMiscNames[number()]));
521         return rtrim(os.str(), ", ");
522 }
523
524
525 // Set family according to lyx format string
526 LyXFont & LyXFont::setLyXFamily(string const & fam)
527 {
528         string const s = ascii_lowercase(fam);
529
530         int i = 0;
531         while (LyXFamilyNames[i] != s &&
532                LyXFamilyNames[i] != string("error"))
533                 ++i;
534         if (s == LyXFamilyNames[i])
535                 setFamily(LyXFont::FONT_FAMILY(i));
536         else
537                 lyxerr << "LyXFont::setLyXFamily: Unknown family `"
538                        << s << '\'' << endl;
539         return *this;
540 }
541
542
543 // Set series according to lyx format string
544 LyXFont & LyXFont::setLyXSeries(string const & ser)
545 {
546         string const s = ascii_lowercase(ser);
547
548         int i = 0;
549         while (LyXSeriesNames[i] != s &&
550                LyXSeriesNames[i] != string("error")) ++i;
551         if (s == LyXSeriesNames[i]) {
552                 setSeries(LyXFont::FONT_SERIES(i));
553         } else
554                 lyxerr << "LyXFont::setLyXSeries: Unknown series `"
555                        << s << '\'' << endl;
556         return *this;
557 }
558
559
560 // Set shape according to lyx format string
561 LyXFont & LyXFont::setLyXShape(string const & sha)
562 {
563         string const s = ascii_lowercase(sha);
564
565         int i = 0;
566         while (LyXShapeNames[i] != s && LyXShapeNames[i] != string("error"))
567                         ++i;
568         if (s == LyXShapeNames[i])
569                 setShape(LyXFont::FONT_SHAPE(i));
570         else
571                 lyxerr << "LyXFont::setLyXShape: Unknown shape `"
572                        << s << '\'' << endl;
573         return *this;
574 }
575
576
577 // Set size according to lyx format string
578 LyXFont & LyXFont::setLyXSize(string const & siz)
579 {
580         string const s = ascii_lowercase(siz);
581         int i = 0;
582         while (LyXSizeNames[i] != s && LyXSizeNames[i] != string("error"))
583                 ++i;
584         if (s == LyXSizeNames[i]) {
585                 setSize(LyXFont::FONT_SIZE(i));
586         } else
587                 lyxerr << "LyXFont::setLyXSize: Unknown size `"
588                        << s << '\'' << endl;
589         return *this;
590 }
591
592
593 // Set size according to lyx format string
594 LyXFont::FONT_MISC_STATE LyXFont::setLyXMisc(string const & siz)
595 {
596         string const s = ascii_lowercase(siz);
597         int i = 0;
598         while (LyXMiscNames[i] != s &&
599                LyXMiscNames[i] != string("error")) ++i;
600         if (s == LyXMiscNames[i])
601                 return FONT_MISC_STATE(i);
602         lyxerr << "LyXFont::setLyXMisc: Unknown misc flag `"
603                << s << '\'' << endl;
604         return OFF;
605 }
606
607
608 /// Sets color after LyX text format
609 LyXFont & LyXFont::setLyXColor(string const & col)
610 {
611         setColor(lcolor.getFromLyXName(col));
612         return *this;
613 }
614
615
616 // Returns size in latex format
617 string const LyXFont::latexSize() const
618 {
619         return LaTeXSizeNames[size()];
620 }
621
622
623 // Read a font definition from given file in lyx format
624 // Used for layouts
625 LyXFont & LyXFont::lyxRead(LyXLex & lex)
626 {
627         bool error = false;
628         bool finished = false;
629         while (!finished && lex.isOK() && !error) {
630                 lex.next();
631                 string const tok = ascii_lowercase(lex.getString());
632
633                 if (tok.empty()) {
634                         continue;
635                 } else if (tok == "endfont") {
636                         finished = true;
637                 } else if (tok == "family") {
638                         lex.next();
639                         string const ttok = lex.getString();
640                         setLyXFamily(ttok);
641                 } else if (tok == "series") {
642                         lex.next();
643                         string const ttok = lex.getString();
644                         setLyXSeries(ttok);
645                 } else if (tok == "shape") {
646                         lex.next();
647                         string const ttok = lex.getString();
648                         setLyXShape(ttok);
649                 } else if (tok == "size") {
650                         lex.next();
651                         string const ttok = lex.getString();
652                         setLyXSize(ttok);
653                 } else if (tok == "misc") {
654                         lex.next();
655                         string const ttok = ascii_lowercase(lex.getString());
656
657                         if (ttok == "no_bar") {
658                                 setUnderbar(OFF);
659                         } else if (ttok == "no_emph") {
660                                 setEmph(OFF);
661                         } else if (ttok == "no_noun") {
662                                 setNoun(OFF);
663                         } else if (ttok == "emph") {
664                                 setEmph(ON);
665                         } else if (ttok == "underbar") {
666                                 setUnderbar(ON);
667                         } else if (ttok == "noun") {
668                                 setNoun(ON);
669                         } else {
670                                 lex.printError("Illegal misc type `$$Token´");
671                         }
672                 } else if (tok == "color") {
673                         lex.next();
674                         string const ttok = lex.getString();
675                         setLyXColor(ttok);
676                 } else {
677                         lex.printError("Unknown tag `$$Token'");
678                         error = true;
679                 }
680         }
681         return *this;
682 }
683
684
685 /// Writes the changes from this font to orgfont in .lyx format in file
686 void LyXFont::lyxWriteChanges(LyXFont const & orgfont,
687                               ostream & os) const
688 {
689         os << "\n";
690         if (orgfont.family() != family())
691                 os << "\\family " << LyXFamilyNames[family()] << "\n";
692         if (orgfont.series() != series())
693                 os << "\\series " << LyXSeriesNames[series()] << "\n";
694         if (orgfont.shape() != shape())
695                 os << "\\shape " << LyXShapeNames[shape()] << "\n";
696         if (orgfont.size() != size())
697                 os << "\\size " << LyXSizeNames[size()] << "\n";
698         if (orgfont.emph() != emph())
699                 os << "\\emph " << LyXMiscNames[emph()] << "\n";
700         if (orgfont.number() != number())
701                 os << "\\numeric " << LyXMiscNames[number()] << "\n";
702         if (orgfont.underbar() != underbar()) {
703                 // This is only for backwards compatibility
704                 switch (underbar()) {
705                 case OFF:       os << "\\bar no\n"; break;
706                 case ON:        os << "\\bar under\n"; break;
707                 case TOGGLE:    lyxerr << "LyXFont::lyxWriteFontChanges: "
708                                         "TOGGLE should not appear here!"
709                                        << endl;
710                 break;
711                 case INHERIT:   os << "\\bar default\n"; break;
712                 case IGNORE:    lyxerr << "LyXFont::lyxWriteFontChanges: "
713                                         "IGNORE should not appear here!"
714                                        << endl;
715                 break;
716                 }
717         }
718         if (orgfont.noun() != noun()) {
719                 os << "\\noun " << LyXMiscNames[noun()] << "\n";
720         }
721         if (orgfont.color() != color()) {
722                 // To make us file compatible with older
723                 // lyx versions we emit "default" instead
724                 // of "inherit"
725                 string col_str(lcolor.getLyXName(color()));
726                 if (col_str == "inherit") col_str = "default";
727                 os << "\\color " << col_str << "\n";
728         }
729         if (orgfont.language() != language()) {
730                 if (language())
731                         os << "\\lang " << language()->lang() << "\n";
732                 else
733                         os << "\\lang unknown\n";
734         }
735 }
736
737
738 /// Writes the head of the LaTeX needed to impose this font
739 // Returns number of chars written.
740 int LyXFont::latexWriteStartChanges(ostream & os, LyXFont const & base,
741                                     LyXFont const & prev) const
742 {
743         int count = 0;
744         bool env = false;
745
746         if (language()->babel() != base.language()->babel() &&
747             language() != prev.language()) {
748                 if (isRightToLeft() != prev.isRightToLeft()) {
749                         if (isRightToLeft()) {
750                                 os << "\\R{";
751                                 count += 3;
752                         } else {
753                                 os << "\\L{";
754                                 count += 3;
755                         }
756                 } else {
757                         string const tmp =
758                                 subst(lyxrc.language_command_local,
759                                       "$$lang", language()->babel());
760                         os << tmp;
761                         count += tmp.length();
762                 }
763         }
764
765         if (number() == ON && prev.number() != ON &&
766             language()->lang() == "hebrew") {
767                 os << "{\\beginL ";
768                 count += 9;
769         }
770
771         LyXFont f = *this;
772         f.reduce(base);
773
774         if (f.family() != INHERIT_FAMILY) {
775                 os << '\\'
776                    << LaTeXFamilyNames[f.family()]
777                    << '{';
778                 count += strlen(LaTeXFamilyNames[f.family()]) + 2;
779                 env = true; //We have opened a new environment
780         }
781         if (f.series() != INHERIT_SERIES) {
782                 os << '\\'
783                    << LaTeXSeriesNames[f.series()]
784                    << '{';
785                 count += strlen(LaTeXSeriesNames[f.series()]) + 2;
786                 env = true; //We have opened a new environment
787         }
788         if (f.shape() != INHERIT_SHAPE) {
789                 os << '\\'
790                    << LaTeXShapeNames[f.shape()]
791                    << '{';
792                 count += strlen(LaTeXShapeNames[f.shape()]) + 2;
793                 env = true; //We have opened a new environment
794         }
795         if (f.color() != LColor::inherit && f.color() != LColor::ignore) {
796                 os << "\\textcolor{"
797                    << lcolor.getLaTeXName(f.color())
798                    << "}{";
799                 count += lcolor.getLaTeXName(f.color()).length() + 13;
800                 env = true; //We have opened a new environment
801         }
802         if (f.emph() == ON) {
803                 os << "\\emph{";
804                 count += 6;
805                 env = true; //We have opened a new environment
806         }
807         if (f.underbar() == ON) {
808                 os << "\\underbar{";
809                 count += 10;
810                 env = true; //We have opened a new environment
811         }
812         // \noun{} is a LyX special macro
813         if (f.noun() == ON) {
814                 os << "\\noun{";
815                 count += 6;
816                 env = true; //We have opened a new environment
817         }
818         if (f.size() != INHERIT_SIZE) {
819                 // If we didn't open an environment above, we open one here
820                 if (!env) {
821                         os << '{';
822                         ++count;
823                 }
824                 os << '\\'
825                    << LaTeXSizeNames[f.size()]
826                    << ' ';
827                 count += strlen(LaTeXSizeNames[f.size()]) + 2;
828         }
829         return count;
830 }
831
832
833 /// Writes ending block of LaTeX needed to close use of this font
834 // Returns number of chars written
835 // This one corresponds to latexWriteStartChanges(). (Asger)
836 int LyXFont::latexWriteEndChanges(ostream & os, LyXFont const & base,
837                                   LyXFont const & next) const
838 {
839         int count = 0;
840         bool env = false;
841
842         // reduce the current font to changes against the base
843         // font (of the layout). We use a temporary for this to
844         // avoid changing this font instance, as that would break
845         LyXFont f = *this;
846         f.reduce(base);
847
848         if (f.family() != INHERIT_FAMILY) {
849                 os << '}';
850                 ++count;
851                 env = true; // Size change need not bother about closing env.
852         }
853         if (f.series() != INHERIT_SERIES) {
854                 os << '}';
855                 ++count;
856                 env = true; // Size change need not bother about closing env.
857         }
858         if (f.shape() != INHERIT_SHAPE) {
859                 os << '}';
860                 ++count;
861                 env = true; // Size change need not bother about closing env.
862         }
863         if (f.color() != LColor::inherit && f.color() != LColor::ignore) {
864                 os << '}';
865                 ++count;
866                 env = true; // Size change need not bother about closing env.
867         }
868         if (f.emph() == ON) {
869                 os << '}';
870                 ++count;
871                 env = true; // Size change need not bother about closing env.
872         }
873         if (f.underbar() == ON) {
874                 os << '}';
875                 ++count;
876                 env = true; // Size change need not bother about closing env.
877         }
878         if (f.noun() == ON) {
879                 os << '}';
880                 ++count;
881                 env = true; // Size change need not bother about closing env.
882         }
883         if (f.size() != INHERIT_SIZE) {
884                 // We only have to close if only size changed
885                 if (!env) {
886                         os << '}';
887                         ++count;
888                 }
889         }
890
891         if (number() == ON && next.number() != ON &&
892             language()->lang() == "hebrew") {
893                 os << "\\endL}";
894                 count += 6;
895         }
896
897         if (language() != base.language() && language() != next.language()) {
898                 os << '}';
899                 ++count;
900         }
901
902         return count;
903 }
904
905
906 LColor_color LyXFont::realColor() const
907 {
908         if (color() == LColor::none)
909                 return LColor::foreground;
910         return color();
911 }
912
913
914 ostream & operator<<(ostream & os, LyXFont::FONT_MISC_STATE fms)
915 {
916         return os << int(fms);
917 }
918
919
920 std::ostream & operator<<(std::ostream & os, LyXFont const & font)
921 {
922         return os << "font:"
923                 << " family " << font.bits.family
924                 << " series " << font.bits.series
925                 << " shape " << font.bits.shape
926                 << " size " << font.bits.size
927                 << " color " << font.bits.color
928                 << " emph " << font.bits.emph
929                 << " underbar " << font.bits.underbar
930                 << " noun " << font.bits.noun
931                 << " number " << font.bits.number
932                 << " lang: " << (font.lang ? font.lang->lang() : 0);
933 }