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