]> git.lyx.org Git - lyx.git/blob - src/lyxfont.C
Rename EnumLColor as LColor_color.
[lyx.git] / src / lyxfont.C
1 /**
2  * \file 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 "support/std_sstream.h"
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
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 LyXFont::FONT_MISC_STATE LyXFont::underbar() const
201 {
202         return bits.underbar;
203 }
204
205
206 LColor_color LyXFont::color() const
207 {
208         return LColor::color(bits.color);
209 }
210
211
212 Language const * LyXFont::language() const
213 {
214         return lang;
215 }
216
217
218 LyXFont::FONT_MISC_STATE LyXFont::number() const
219 {
220         return bits.number;
221 }
222
223
224 bool LyXFont::isRightToLeft() const
225 {
226         return lang->RightToLeft();
227 }
228
229
230 bool LyXFont::isVisibleRightToLeft() const
231 {
232         return (lang->RightToLeft() &&
233                 number() != ON);
234 }
235
236
237 void LyXFont::setFamily(LyXFont::FONT_FAMILY f)
238 {
239         bits.family = f;
240 }
241
242
243 void LyXFont::setSeries(LyXFont::FONT_SERIES s)
244 {
245         bits.series = s;
246 }
247
248
249 void LyXFont::setShape(LyXFont::FONT_SHAPE s)
250 {
251         bits.shape = s;
252 }
253
254
255 void LyXFont::setSize(LyXFont::FONT_SIZE s)
256 {
257         bits.size = s;
258 }
259
260
261 void LyXFont::setEmph(LyXFont::FONT_MISC_STATE e)
262 {
263         bits.emph = e;
264 }
265
266
267 void LyXFont::setUnderbar(LyXFont::FONT_MISC_STATE u)
268 {
269         bits.underbar = u;
270 }
271
272
273 void LyXFont::setNoun(LyXFont::FONT_MISC_STATE n)
274 {
275         bits.noun = n;
276 }
277
278
279 void LyXFont::setColor(LColor_color c)
280 {
281         bits.color = int(c);
282 }
283
284
285 void LyXFont::setLanguage(Language const * l)
286 {
287         lang = l;
288 }
289
290
291 void LyXFont::setNumber(LyXFont::FONT_MISC_STATE n)
292 {
293         bits.number = n;
294 }
295
296
297 /// Decreases font size by one
298 LyXFont & LyXFont::decSize()
299 {
300         switch (size()) {
301         case SIZE_HUGER:        setSize(SIZE_HUGE);     break;
302         case SIZE_HUGE:         setSize(SIZE_LARGEST);  break;
303         case SIZE_LARGEST:      setSize(SIZE_LARGER);   break;
304         case SIZE_LARGER:       setSize(SIZE_LARGE);    break;
305         case SIZE_LARGE:        setSize(SIZE_NORMAL);   break;
306         case SIZE_NORMAL:       setSize(SIZE_SMALL);    break;
307         case SIZE_SMALL:        setSize(SIZE_FOOTNOTE); break;
308         case SIZE_FOOTNOTE:     setSize(SIZE_SCRIPT);   break;
309         case SIZE_SCRIPT:       setSize(SIZE_TINY);     break;
310         case SIZE_TINY:         break;
311         case INCREASE_SIZE:
312                 lyxerr << "Can't LyXFont::decSize on INCREASE_SIZE" << endl;
313                 break;
314         case DECREASE_SIZE:
315                 lyxerr <<"Can't LyXFont::decSize on DECREASE_SIZE" << endl;
316                 break;
317         case INHERIT_SIZE:
318                 lyxerr <<"Can't LyXFont::decSize on INHERIT_SIZE" << endl;
319                 break;
320         case IGNORE_SIZE:
321                 lyxerr <<"Can't LyXFont::decSize on IGNORE_SIZE" << endl;
322                 break;
323         }
324         return *this;
325 }
326
327
328 /// Increases font size by one
329 LyXFont & LyXFont::incSize()
330 {
331         switch (size()) {
332         case SIZE_HUGER:        break;
333         case SIZE_HUGE:         setSize(SIZE_HUGER);    break;
334         case SIZE_LARGEST:      setSize(SIZE_HUGE);     break;
335         case SIZE_LARGER:       setSize(SIZE_LARGEST);  break;
336         case SIZE_LARGE:        setSize(SIZE_LARGER);   break;
337         case SIZE_NORMAL:       setSize(SIZE_LARGE);    break;
338         case SIZE_SMALL:        setSize(SIZE_NORMAL);   break;
339         case SIZE_FOOTNOTE:     setSize(SIZE_SMALL);    break;
340         case SIZE_SCRIPT:       setSize(SIZE_FOOTNOTE); break;
341         case SIZE_TINY:         setSize(SIZE_SCRIPT);   break;
342         case INCREASE_SIZE:
343                 lyxerr <<"Can't LyXFont::incSize on INCREASE_SIZE" << endl;
344                 break;
345         case DECREASE_SIZE:
346                 lyxerr <<"Can't LyXFont::incSize on DECREASE_SIZE" << endl;
347                 break;
348         case INHERIT_SIZE:
349                 lyxerr <<"Can't LyXFont::incSize on INHERIT_SIZE" << endl;
350                 break;
351         case IGNORE_SIZE:
352                 lyxerr <<"Can't LyXFont::incSize on IGNORE_SIZE" << endl;
353                 break;
354         }
355         return *this;
356 }
357
358
359 /// Updates a misc setting according to request
360 LyXFont::FONT_MISC_STATE LyXFont::setMisc(FONT_MISC_STATE newfont,
361                                           FONT_MISC_STATE org)
362 {
363         if (newfont == TOGGLE) {
364                 if (org == ON)
365                         return OFF;
366                 else if (org == OFF)
367                         return ON;
368                 else {
369                         lyxerr <<"LyXFont::setMisc: Need state"
370                                 " ON or OFF to toggle. Setting to ON" << endl;
371                         return ON;
372                 }
373         } else if (newfont == IGNORE)
374                 return org;
375         else
376                 return newfont;
377 }
378
379
380 /// Updates font settings according to request
381 void LyXFont::update(LyXFont const & newfont,
382                      Language const * document_language,
383                      bool toggleall)
384 {
385         if (newfont.family() == family() && toggleall)
386                 setFamily(INHERIT_FAMILY); // toggle 'back'
387         else if (newfont.family() != IGNORE_FAMILY)
388                 setFamily(newfont.family());
389         // else it's IGNORE_SHAPE
390
391         // "Old" behaviour: "Setting" bold will toggle bold on/off.
392         switch (newfont.series()) {
393         case BOLD_SERIES:
394                 // We toggle...
395                 if (series() == BOLD_SERIES && toggleall)
396                         setSeries(MEDIUM_SERIES);
397                 else
398                         setSeries(BOLD_SERIES);
399                 break;
400         case MEDIUM_SERIES:
401         case INHERIT_SERIES:
402                 setSeries(newfont.series());
403                 break;
404         case IGNORE_SERIES:
405                 break;
406         }
407
408         if (newfont.shape() == shape() && toggleall)
409                 setShape(INHERIT_SHAPE); // toggle 'back'
410         else if (newfont.shape() != IGNORE_SHAPE)
411                 setShape(newfont.shape());
412         // else it's IGNORE_SHAPE
413
414         if (newfont.size() != IGNORE_SIZE) {
415                 if (newfont.size() == INCREASE_SIZE)
416                         incSize();
417                 else if (newfont.size() == DECREASE_SIZE)
418                         decSize();
419                 else
420                         setSize(newfont.size());
421         }
422
423         setEmph(setMisc(newfont.emph(), emph()));
424         setUnderbar(setMisc(newfont.underbar(), underbar()));
425         setNoun(setMisc(newfont.noun(), noun()));
426
427         setNumber(setMisc(newfont.number(), number()));
428         if (newfont.language() == language() && toggleall)
429                 if (language() == document_language)
430                         setLanguage(default_language);
431                 else
432                         setLanguage(document_language);
433         else if (newfont.language() != ignore_language)
434                 setLanguage(newfont.language());
435
436         if (newfont.color() == color() && toggleall)
437                 setColor(LColor::inherit); // toggle 'back'
438         else if (newfont.color() != LColor::ignore)
439                 setColor(newfont.color());
440 }
441
442
443 /// Reduce font to fall back to template where possible
444 void LyXFont::reduce(LyXFont const & tmplt)
445 {
446         if (family() == tmplt.family())
447                 setFamily(INHERIT_FAMILY);
448         if (series() == tmplt.series())
449                 setSeries(INHERIT_SERIES);
450         if (shape() == tmplt.shape())
451                 setShape(INHERIT_SHAPE);
452         if (size() == tmplt.size())
453                 setSize(INHERIT_SIZE);
454         if (emph() == tmplt.emph())
455                 setEmph(INHERIT);
456         if (underbar() == tmplt.underbar())
457                 setUnderbar(INHERIT);
458         if (noun() == tmplt.noun())
459                 setNoun(INHERIT);
460         if (color() == tmplt.color())
461                 setColor(LColor::inherit);
462 }
463
464
465 /// Realize font from a template
466 LyXFont & LyXFont::realize(LyXFont const & tmplt)
467 {
468         if (bits == inherit) {
469                 bits = tmplt.bits;
470                 return *this;
471         }
472
473         if (bits.family == INHERIT_FAMILY) {
474                 bits.family = tmplt.bits.family;
475         }
476         if (bits.series == INHERIT_SERIES) {
477                 bits.series = tmplt.bits.series;
478         }
479         if (bits.shape == INHERIT_SHAPE) {
480                 bits.shape = tmplt.bits.shape;
481         }
482         if (bits.size == INHERIT_SIZE) {
483                 bits.size = tmplt.bits.size;
484         }
485         if (bits.emph == INHERIT) {
486                 bits.emph = tmplt.bits.emph;
487         }
488         if (bits.underbar == INHERIT) {
489                 bits.underbar = tmplt.bits.underbar;
490         }
491         if (bits.noun == INHERIT) {
492                 bits.noun = tmplt.bits.noun;
493         }
494         if (bits.color == LColor::inherit) {
495                 bits.color = tmplt.bits.color;
496         }
497         return *this;
498 }
499
500
501 /// Is font resolved?
502 bool LyXFont::resolved() const
503 {
504         return (family() != INHERIT_FAMILY && series() != INHERIT_SERIES &&
505                 shape() != INHERIT_SHAPE && size() != INHERIT_SIZE &&
506                 emph() != INHERIT && underbar() != INHERIT &&
507                 noun() != INHERIT &&
508                 color() != LColor::inherit);
509 }
510
511
512 /// Build GUI description of font state
513 string const LyXFont::stateText(BufferParams * params) const
514 {
515         ostringstream os;
516         if (family() != INHERIT_FAMILY)
517                 os << _(GUIFamilyNames[family()]) << ", ";
518         if (series() != INHERIT_SERIES)
519                 os << _(GUISeriesNames[series()]) << ", ";
520         if (shape() != INHERIT_SHAPE)
521                 os << _(GUIShapeNames[shape()]) << ", ";
522         if (size() != INHERIT_SIZE)
523                 os << _(GUISizeNames[size()]) << ", ";
524         if (color() != LColor::inherit)
525                 os << lcolor.getGUIName(color()) << ", ";
526         if (emph() != INHERIT)
527                 os << bformat(_("Emphasis %1$s, "), _(GUIMiscNames[emph()]));
528         if (underbar() != INHERIT)
529                 os << bformat(_("Underline %1$s, "), _(GUIMiscNames[underbar()]));
530         if (noun() != INHERIT)
531                 os << bformat(_("Noun %1$s, "), _(GUIMiscNames[noun()]));
532         if (bits == inherit)
533                 os << _("Default") << ", ";
534         if (!params || (language() != params->language))
535                 os << bformat(_("Language: %1$s, "), _(language()->display()));
536         if (number() != OFF)
537                 os << bformat(_("  Number %1$s"), _(GUIMiscNames[number()]));
538         return rtrim(os.str(), ", ");
539 }
540
541
542 // Set family according to lyx format string
543 LyXFont & LyXFont::setLyXFamily(string const & fam)
544 {
545         string const s = ascii_lowercase(fam);
546
547         int i = 0;
548         while (s != LyXFamilyNames[i] && LyXFamilyNames[i] != "error")
549                 ++i;
550         if (s == LyXFamilyNames[i])
551                 setFamily(LyXFont::FONT_FAMILY(i));
552         else
553                 lyxerr << "LyXFont::setLyXFamily: Unknown family `"
554                        << s << '\'' << endl;
555         return *this;
556 }
557
558
559 // Set series according to lyx format string
560 LyXFont & LyXFont::setLyXSeries(string const & ser)
561 {
562         string const s = ascii_lowercase(ser);
563
564         int i = 0;
565         while (s != LyXSeriesNames[i] && LyXSeriesNames[i] != "error") ++i;
566         if (s == LyXSeriesNames[i]) {
567                 setSeries(LyXFont::FONT_SERIES(i));
568         } else
569                 lyxerr << "LyXFont::setLyXSeries: Unknown series `"
570                        << s << '\'' << endl;
571         return *this;
572 }
573
574
575 // Set shape according to lyx format string
576 LyXFont & LyXFont::setLyXShape(string const & sha)
577 {
578         string const s = ascii_lowercase(sha);
579
580         int i = 0;
581         while (s != LyXShapeNames[i] && LyXShapeNames[i] != "error") ++i;
582         if (s == LyXShapeNames[i]) {
583                 setShape(LyXFont::FONT_SHAPE(i));
584         } else
585                 lyxerr << "LyXFont::setLyXShape: Unknown shape `"
586                        << s << '\'' << endl;
587         return *this;
588 }
589
590
591 // Set size according to lyx format string
592 LyXFont & LyXFont::setLyXSize(string const & siz)
593 {
594         string const s = ascii_lowercase(siz);
595         int i = 0;
596         while (s != LyXSizeNames[i] && LyXSizeNames[i] != "error") ++i;
597         if (s == LyXSizeNames[i]) {
598                 setSize(LyXFont::FONT_SIZE(i));
599         } else
600                 lyxerr << "LyXFont::setLyXSize: Unknown size `"
601                        << s << '\'' << endl;
602         return *this;
603 }
604
605
606 // Set size according to lyx format string
607 LyXFont::FONT_MISC_STATE LyXFont::setLyXMisc(string const & siz)
608 {
609         string const s = ascii_lowercase(siz);
610         int i = 0;
611         while (s != LyXMiscNames[i] && LyXMiscNames[i] != "error") ++i;
612         if (s == LyXMiscNames[i])
613                 return FONT_MISC_STATE(i);
614         lyxerr << "LyXFont::setLyXMisc: Unknown misc flag `"
615                << s << '\'' << endl;
616         return OFF;
617 }
618
619
620 /// Sets color after LyX text format
621 LyXFont & LyXFont::setLyXColor(string const & col)
622 {
623         setColor(lcolor.getFromLyXName(col));
624         return *this;
625 }
626
627
628 // Returns size in latex format
629 string const LyXFont::latexSize() const
630 {
631         return LaTeXSizeNames[size()];
632 }
633
634
635 // Read a font definition from given file in lyx format
636 // Used for layouts
637 LyXFont & LyXFont::lyxRead(LyXLex & lex)
638 {
639         bool error = false;
640         bool finished = false;
641         while (!finished && lex.isOK() && !error) {
642                 lex.next();
643                 string const tok = ascii_lowercase(lex.getString());
644
645                 if (tok.empty()) {
646                         continue;
647                 } else if (tok == "endfont") {
648                         finished = true;
649                 } else if (tok == "family") {
650                         lex.next();
651                         string const ttok = lex.getString();
652                         setLyXFamily(ttok);
653                 } else if (tok == "series") {
654                         lex.next();
655                         string const ttok = lex.getString();
656                         setLyXSeries(ttok);
657                 } else if (tok == "shape") {
658                         lex.next();
659                         string const ttok = lex.getString();
660                         setLyXShape(ttok);
661                 } else if (tok == "size") {
662                         lex.next();
663                         string const ttok = lex.getString();
664                         setLyXSize(ttok);
665                 } else if (tok == "misc") {
666                         lex.next();
667                         string const ttok = ascii_lowercase(lex.getString());
668
669                         if (ttok == "no_bar") {
670                                 setUnderbar(OFF);
671                         } else if (ttok == "no_emph") {
672                                 setEmph(OFF);
673                         } else if (ttok == "no_noun") {
674                                 setNoun(OFF);
675                         } else if (ttok == "emph") {
676                                 setEmph(ON);
677                         } else if (ttok == "underbar") {
678                                 setUnderbar(ON);
679                         } else if (ttok == "noun") {
680                                 setNoun(ON);
681                         } else {
682                                 lex.printError("Illegal misc type `$$Token´");
683                         }
684                 } else if (tok == "color") {
685                         lex.next();
686                         string const ttok = lex.getString();
687                         setLyXColor(ttok);
688                 } else {
689                         lex.printError("Unknown tag `$$Token'");
690                         error = true;
691                 }
692         }
693         return *this;
694 }
695
696
697 /// Writes the changes from this font to orgfont in .lyx format in file
698 void LyXFont::lyxWriteChanges(LyXFont const & orgfont,
699                               ostream & os) const
700 {
701         os << "\n";
702         if (orgfont.family() != family()) {
703                 os << "\\family " << LyXFamilyNames[family()] << " \n";
704         }
705         if (orgfont.series() != series()) {
706                 os << "\\series " << LyXSeriesNames[series()] << " \n";
707         }
708         if (orgfont.shape() != shape()) {
709                 os << "\\shape " << LyXShapeNames[shape()] << " \n";
710         }
711         if (orgfont.size() != size()) {
712                 os << "\\size " << LyXSizeNames[size()] << " \n";
713         }
714         if (orgfont.emph() != emph()) {
715                 os << "\\emph " << LyXMiscNames[emph()] << " \n";
716         }
717         if (orgfont.number() != number()) {
718                 os << "\\numeric " << LyXMiscNames[number()] << " \n";
719         }
720         if (orgfont.underbar() != underbar()) {
721                 // This is only for backwards compatibility
722                 switch (underbar()) {
723                 case OFF:       os << "\\bar no \n"; break;
724                 case ON:        os << "\\bar under \n"; break;
725                 case TOGGLE:    lyxerr << "LyXFont::lyxWriteFontChanges: "
726                                         "TOGGLE should not appear here!"
727                                        << endl;
728                 break;
729                 case INHERIT:   os << "\\bar default \n"; break;
730                 case IGNORE:    lyxerr << "LyXFont::lyxWriteFontChanges: "
731                                         "IGNORE should not appear here!"
732                                        << endl;
733                 break;
734                 }
735         }
736         if (orgfont.noun() != noun()) {
737                 os << "\\noun " << LyXMiscNames[noun()] << " \n";
738         }
739         if (orgfont.color() != color()) {
740                 // To make us file compatible with older
741                 // lyx versions we emit "default" instead
742                 // of "inherit"
743                 string col_str(lcolor.getLyXName(color()));
744                 if (col_str == "inherit") col_str = "default";
745                 os << "\\color " << col_str << "\n";
746         }
747         if (orgfont.language() != language()) {
748                 if (language())
749                         os << "\\lang " << language()->lang() << "\n";
750                 else
751                         os << "\\lang unknown\n";
752         }
753 }
754
755
756 /// Writes the head of the LaTeX needed to impose this font
757 // Returns number of chars written.
758 int LyXFont::latexWriteStartChanges(ostream & os, LyXFont const & base,
759                                     LyXFont const & prev) const
760 {
761         int count = 0;
762         bool env = false;
763
764         if (language()->babel() != base.language()->babel() &&
765             language() != prev.language()) {
766                 if (isRightToLeft() != prev.isRightToLeft()) {
767                         if (isRightToLeft()) {
768                                 os << "\\R{";
769                                 count += 3;
770                         } else {
771                                 os << "\\L{";
772                                 count += 3;
773                         }
774                 } else {
775                         string const tmp =
776                                 subst(lyxrc.language_command_local,
777                                       "$$lang", language()->babel());
778                         os << tmp;
779                         count += tmp.length();
780                 }
781         }
782
783         if (number() == ON && prev.number() != ON &&
784             language()->lang() == "hebrew") {
785                 os << "{\\beginL ";
786                 count += 9;
787         }
788
789         LyXFont 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() != LColor::inherit && f.color() != LColor::ignore) {
814                 os << "\\textcolor{"
815                    << 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 LyXFont::latexWriteEndChanges(ostream & os, LyXFont const & base,
855                                   LyXFont const & next) const
856 {
857         int count = 0;
858         bool env = false;
859
860         // reduce the current font to changes against the base
861         // font (of the layout). We use a temporary for this to
862         // avoid changing this font instance, as that would break
863         LyXFont f = *this;
864         f.reduce(base);
865
866         if (f.family() != INHERIT_FAMILY) {
867                 os << '}';
868                 ++count;
869                 env = true; // Size change need not bother about closing env.
870         }
871         if (f.series() != INHERIT_SERIES) {
872                 os << '}';
873                 ++count;
874                 env = true; // Size change need not bother about closing env.
875         }
876         if (f.shape() != INHERIT_SHAPE) {
877                 os << '}';
878                 ++count;
879                 env = true; // Size change need not bother about closing env.
880         }
881         if (f.color() != LColor::inherit && f.color() != LColor::ignore) {
882                 os << '}';
883                 ++count;
884                 env = true; // Size change need not bother about closing env.
885         }
886         if (f.emph() == ON) {
887                 os << '}';
888                 ++count;
889                 env = true; // Size change need not bother about closing env.
890         }
891         if (f.underbar() == ON) {
892                 os << '}';
893                 ++count;
894                 env = true; // Size change need not bother about closing env.
895         }
896         if (f.noun() == ON) {
897                 os << '}';
898                 ++count;
899                 env = true; // Size change need not bother about closing env.
900         }
901         if (f.size() != INHERIT_SIZE) {
902                 // We only have to close if only size changed
903                 if (!env) {
904                         os << '}';
905                         ++count;
906                 }
907         }
908
909         if (number() == ON && next.number() != ON &&
910             language()->lang() == "hebrew") {
911                 os << "\\endL}";
912                 count += 6;
913         }
914
915         if (language() != base.language() && language() != next.language()) {
916                 os << '}';
917                 ++count;
918         }
919
920         return count;
921 }
922
923
924 LColor_color LyXFont::realColor() const
925 {
926         if (color() == LColor::none)
927                 return LColor::foreground;
928         return color();
929 }
930
931
932 // Convert logical attributes to concrete shape attribute
933 LyXFont::FONT_SHAPE LyXFont::realShape() const
934 {
935         register FONT_SHAPE s = shape();
936
937         if (emph() == ON) {
938                 if (s == UP_SHAPE)
939                         s = ITALIC_SHAPE;
940                 else
941                         s = UP_SHAPE;
942         }
943         if (noun() == ON)
944                 s = SMALLCAPS_SHAPE;
945         return s;
946 }
947
948
949 ostream & operator<<(ostream & o, LyXFont::FONT_MISC_STATE fms)
950 {
951         return o << int(fms);
952 }