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