]> git.lyx.org Git - lyx.git/blob - src/lyxfont.C
- Link against qt-mt333.lib which is what the current qt3 cvs produces
[lyx.git] / src / lyxfont.C
1 /**
2  * \file src/lyxfont.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Lars Gullik Bjønnes
7  * \author Jean-Marc Lasgouttes
8  * \author Angus Leeming
9  * \author André Pönitz
10  * \author Dekel Tsur
11  *
12  * Full author contact details are available in file CREDITS.
13  */
14
15 #include <config.h>
16
17 #include "lyxfont.h"
18
19 #include "bufferparams.h" // stateText
20 #include "debug.h"
21 #include "gettext.h"
22 #include "language.h"
23 #include "LColor.h"
24 #include "lyxlex.h"
25 #include "lyxrc.h"
26
27 #include "support/lstrings.h"
28
29 #include <sstream>
30
31 using lyx::support::ascii_lowercase;
32 using lyx::support::bformat;
33 using lyx::support::rtrim;
34 using lyx::support::subst;
35
36 using std::endl;
37 using std::string;
38 using std::ostream;
39 using std::ostringstream;
40
41 #ifndef CXX_GLOBAL_CSTD
42 using std::strlen;
43 #endif
44
45 //
46 // Names for the GUI
47 //
48
49 namespace {
50
51 char const * GUIFamilyNames[LyXFont::NUM_FAMILIES + 2 /* default & error */] =
52 { N_("Roman"), N_("Sans Serif"), N_("Typewriter"), N_("Symbol"),
53   "cmr", "cmsy", "cmm", "cmex", "msa", "msb", "eufrak", "wasy",
54   N_("Inherit"), N_("Ignore") };
55
56 char const * GUISeriesNames[4] =
57 { N_("Medium"), N_("Bold"), N_("Inherit"), N_("Ignore") };
58
59 char const * GUIShapeNames[6] =
60 { N_("Upright"), N_("Italic"), N_("Slanted"), N_("Smallcaps"), N_("Inherit"),
61   N_("Ignore") };
62
63 char const * GUISizeNames[14] =
64 { N_("Tiny"), N_("Smallest"), N_("Smaller"), N_("Small"), N_("Normal"), N_("Large"),
65   N_("Larger"), N_("Largest"), N_("Huge"), N_("Huger"), N_("Increase"), N_("Decrease"),
66   N_("Inherit"), N_("Ignore") };
67
68 char const * GUIMiscNames[5] =
69 { N_("Off"), N_("On"), N_("Toggle"), N_("Inherit"), N_("Ignore") };
70
71
72 //
73 // Strings used to read and write .lyx format files
74 //
75 char const * LyXFamilyNames[LyXFont::NUM_FAMILIES + 2 /* default & error */] =
76 { "roman", "sans", "typewriter", "symbol",
77   "cmr", "cmsy", "cmm", "cmex", "msa", "msb", "eufrak", "wasy",
78   "default", "error" };
79
80 char const * LyXSeriesNames[4] =
81 { "medium", "bold", "default", "error" };
82
83 char const * LyXShapeNames[6] =
84 { "up", "italic", "slanted", "smallcaps", "default", "error" };
85
86 char const * LyXSizeNames[14] =
87 { "tiny", "scriptsize", "footnotesize", "small", "normal", "large",
88   "larger", "largest", "huge", "giant",
89   "increase", "decrease", "default", "error" };
90
91 char const * LyXMiscNames[5] =
92 { "off", "on", "toggle", "default", "error" };
93
94 //
95 // Strings used to write LaTeX files
96 //
97 char const * LaTeXFamilyNames[6] =
98 { "textrm", "textsf", "texttt", "error1", "error2", "error3" };
99
100 char const * LaTeXSeriesNames[4] =
101 { "textmd", "textbf", "error4", "error5" };
102
103 char const * LaTeXShapeNames[6] =
104 { "textup", "textit", "textsl", "textsc", "error6", "error7" };
105
106 char const * LaTeXSizeNames[14] =
107 { "tiny", "scriptsize", "footnotesize", "small", "normalsize", "large",
108   "Large", "LARGE", "huge", "Huge", "error8", "error9", "error10", "error11" };
109
110 } // namespace anon
111
112
113 // Initialize static member
114 LyXFont::FontBits LyXFont::sane = {
115         ROMAN_FAMILY,
116         MEDIUM_SERIES,
117         UP_SHAPE,
118         SIZE_NORMAL,
119         LColor::none,
120         OFF,
121         OFF,
122         OFF,
123         OFF };
124
125 // Initialize static member
126 LyXFont::FontBits LyXFont::inherit = {
127         INHERIT_FAMILY,
128         INHERIT_SERIES,
129         INHERIT_SHAPE,
130         INHERIT_SIZE,
131         LColor::inherit,
132         INHERIT,
133         INHERIT,
134         INHERIT,
135         OFF };
136
137 // Initialize static member
138 LyXFont::FontBits LyXFont::ignore = {
139         IGNORE_FAMILY,
140         IGNORE_SERIES,
141         IGNORE_SHAPE,
142         IGNORE_SIZE,
143         LColor::ignore,
144         IGNORE,
145         IGNORE,
146         IGNORE,
147         IGNORE };
148
149
150 bool operator==(LyXFont::FontBits const & lhs,
151                 LyXFont::FontBits const & rhs)
152 {
153         return lhs.family == rhs.family &&
154                 lhs.series == rhs.series &&
155                 lhs.shape == rhs.shape &&
156                 lhs.size == rhs.size &&
157                 lhs.color == rhs.color &&
158                 lhs.emph == rhs.emph &&
159                 lhs.underbar == rhs.underbar &&
160                 lhs.noun == rhs.noun &&
161                 lhs.number == rhs.number;
162 }
163
164
165 LyXFont::LyXFont()
166         : bits(sane), lang(default_language)
167 {}
168
169
170 LyXFont::LyXFont(LyXFont::FONT_INIT1)
171         : bits(inherit), lang(default_language)
172 {}
173
174
175 LyXFont::LyXFont(LyXFont::FONT_INIT2)
176         : bits(ignore), lang(ignore_language)
177 {}
178
179
180 LyXFont::LyXFont(LyXFont::FONT_INIT3)
181         : bits(sane), lang(default_language)
182 {}
183
184
185 LyXFont::LyXFont(LyXFont::FONT_INIT1, Language const * l)
186         : bits(inherit), lang(l)
187 {}
188
189
190 LyXFont::LyXFont(LyXFont::FONT_INIT2, Language const * l)
191         : bits(ignore), lang(l)
192 {}
193
194
195 LyXFont::LyXFont(LyXFont::FONT_INIT3, Language const * l)
196         : bits(sane), lang(l)
197 {}
198
199
200 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 (LyXFamilyNames[i] != s &&
549                LyXFamilyNames[i] != string("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 (LyXSeriesNames[i] != s &&
567                LyXSeriesNames[i] != string("error")) ++i;
568         if (s == LyXSeriesNames[i]) {
569                 setSeries(LyXFont::FONT_SERIES(i));
570         } else
571                 lyxerr << "LyXFont::setLyXSeries: Unknown series `"
572                        << s << '\'' << endl;
573         return *this;
574 }
575
576
577 // Set shape according to lyx format string
578 LyXFont & LyXFont::setLyXShape(string const & sha)
579 {
580         string const s = ascii_lowercase(sha);
581
582         int i = 0;
583         while (LyXShapeNames[i] != s &&
584                LyXShapeNames[i] != string("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 (LyXSizeNames[i] != s &&
600                LyXSizeNames[i] != string("error")) ++i;
601         if (s == LyXSizeNames[i]) {
602                 setSize(LyXFont::FONT_SIZE(i));
603         } else
604                 lyxerr << "LyXFont::setLyXSize: Unknown size `"
605                        << s << '\'' << endl;
606         return *this;
607 }
608
609
610 // Set size according to lyx format string
611 LyXFont::FONT_MISC_STATE LyXFont::setLyXMisc(string const & siz)
612 {
613         string const s = ascii_lowercase(siz);
614         int i = 0;
615         while (LyXMiscNames[i] != s &&
616                LyXMiscNames[i] != string("error")) ++i;
617         if (s == LyXMiscNames[i])
618                 return FONT_MISC_STATE(i);
619         lyxerr << "LyXFont::setLyXMisc: Unknown misc flag `"
620                << s << '\'' << endl;
621         return OFF;
622 }
623
624
625 /// Sets color after LyX text format
626 LyXFont & LyXFont::setLyXColor(string const & col)
627 {
628         setColor(lcolor.getFromLyXName(col));
629         return *this;
630 }
631
632
633 // Returns size in latex format
634 string const LyXFont::latexSize() const
635 {
636         return LaTeXSizeNames[size()];
637 }
638
639
640 // Read a font definition from given file in lyx format
641 // Used for layouts
642 LyXFont & LyXFont::lyxRead(LyXLex & lex)
643 {
644         bool error = false;
645         bool finished = false;
646         while (!finished && lex.isOK() && !error) {
647                 lex.next();
648                 string const tok = ascii_lowercase(lex.getString());
649
650                 if (tok.empty()) {
651                         continue;
652                 } else if (tok == "endfont") {
653                         finished = true;
654                 } else if (tok == "family") {
655                         lex.next();
656                         string const ttok = lex.getString();
657                         setLyXFamily(ttok);
658                 } else if (tok == "series") {
659                         lex.next();
660                         string const ttok = lex.getString();
661                         setLyXSeries(ttok);
662                 } else if (tok == "shape") {
663                         lex.next();
664                         string const ttok = lex.getString();
665                         setLyXShape(ttok);
666                 } else if (tok == "size") {
667                         lex.next();
668                         string const ttok = lex.getString();
669                         setLyXSize(ttok);
670                 } else if (tok == "misc") {
671                         lex.next();
672                         string const ttok = ascii_lowercase(lex.getString());
673
674                         if (ttok == "no_bar") {
675                                 setUnderbar(OFF);
676                         } else if (ttok == "no_emph") {
677                                 setEmph(OFF);
678                         } else if (ttok == "no_noun") {
679                                 setNoun(OFF);
680                         } else if (ttok == "emph") {
681                                 setEmph(ON);
682                         } else if (ttok == "underbar") {
683                                 setUnderbar(ON);
684                         } else if (ttok == "noun") {
685                                 setNoun(ON);
686                         } else {
687                                 lex.printError("Illegal misc type `$$Token´");
688                         }
689                 } else if (tok == "color") {
690                         lex.next();
691                         string const ttok = lex.getString();
692                         setLyXColor(ttok);
693                 } else {
694                         lex.printError("Unknown tag `$$Token'");
695                         error = true;
696                 }
697         }
698         return *this;
699 }
700
701
702 /// Writes the changes from this font to orgfont in .lyx format in file
703 void LyXFont::lyxWriteChanges(LyXFont const & orgfont,
704                               ostream & os) const
705 {
706         os << "\n";
707         if (orgfont.family() != family()) {
708                 os << "\\family " << LyXFamilyNames[family()] << "\n";
709         }
710         if (orgfont.series() != series()) {
711                 os << "\\series " << LyXSeriesNames[series()] << "\n";
712         }
713         if (orgfont.shape() != shape()) {
714                 os << "\\shape " << LyXShapeNames[shape()] << "\n";
715         }
716         if (orgfont.size() != size()) {
717                 os << "\\size " << LyXSizeNames[size()] << "\n";
718         }
719         if (orgfont.emph() != emph()) {
720                 os << "\\emph " << LyXMiscNames[emph()] << "\n";
721         }
722         if (orgfont.number() != number()) {
723                 os << "\\numeric " << LyXMiscNames[number()] << "\n";
724         }
725         if (orgfont.underbar() != underbar()) {
726                 // This is only for backwards compatibility
727                 switch (underbar()) {
728                 case OFF:       os << "\\bar no\n"; break;
729                 case ON:        os << "\\bar under\n"; break;
730                 case TOGGLE:    lyxerr << "LyXFont::lyxWriteFontChanges: "
731                                         "TOGGLE should not appear here!"
732                                        << endl;
733                 break;
734                 case INHERIT:   os << "\\bar default\n"; break;
735                 case IGNORE:    lyxerr << "LyXFont::lyxWriteFontChanges: "
736                                         "IGNORE should not appear here!"
737                                        << endl;
738                 break;
739                 }
740         }
741         if (orgfont.noun() != noun()) {
742                 os << "\\noun " << LyXMiscNames[noun()] << "\n";
743         }
744         if (orgfont.color() != color()) {
745                 // To make us file compatible with older
746                 // lyx versions we emit "default" instead
747                 // of "inherit"
748                 string col_str(lcolor.getLyXName(color()));
749                 if (col_str == "inherit") col_str = "default";
750                 os << "\\color " << col_str << "\n";
751         }
752         if (orgfont.language() != language()) {
753                 if (language())
754                         os << "\\lang " << language()->lang() << "\n";
755                 else
756                         os << "\\lang unknown\n";
757         }
758 }
759
760
761 /// Writes the head of the LaTeX needed to impose this font
762 // Returns number of chars written.
763 int LyXFont::latexWriteStartChanges(ostream & os, LyXFont const & base,
764                                     LyXFont const & prev) const
765 {
766         int count = 0;
767         bool env = false;
768
769         if (language()->babel() != base.language()->babel() &&
770             language() != prev.language()) {
771                 if (isRightToLeft() != prev.isRightToLeft()) {
772                         if (isRightToLeft()) {
773                                 os << "\\R{";
774                                 count += 3;
775                         } else {
776                                 os << "\\L{";
777                                 count += 3;
778                         }
779                 } else {
780                         string const tmp =
781                                 subst(lyxrc.language_command_local,
782                                       "$$lang", language()->babel());
783                         os << tmp;
784                         count += tmp.length();
785                 }
786         }
787
788         if (number() == ON && prev.number() != ON &&
789             language()->lang() == "hebrew") {
790                 os << "{\\beginL ";
791                 count += 9;
792         }
793
794         LyXFont f = *this;
795         f.reduce(base);
796
797         if (f.family() != INHERIT_FAMILY) {
798                 os << '\\'
799                    << LaTeXFamilyNames[f.family()]
800                    << '{';
801                 count += strlen(LaTeXFamilyNames[f.family()]) + 2;
802                 env = true; //We have opened a new environment
803         }
804         if (f.series() != INHERIT_SERIES) {
805                 os << '\\'
806                    << LaTeXSeriesNames[f.series()]
807                    << '{';
808                 count += strlen(LaTeXSeriesNames[f.series()]) + 2;
809                 env = true; //We have opened a new environment
810         }
811         if (f.shape() != INHERIT_SHAPE) {
812                 os << '\\'
813                    << LaTeXShapeNames[f.shape()]
814                    << '{';
815                 count += strlen(LaTeXShapeNames[f.shape()]) + 2;
816                 env = true; //We have opened a new environment
817         }
818         if (f.color() != LColor::inherit && f.color() != LColor::ignore) {
819                 os << "\\textcolor{"
820                    << lcolor.getLaTeXName(f.color())
821                    << "}{";
822                 count += lcolor.getLaTeXName(f.color()).length() + 13;
823                 env = true; //We have opened a new environment
824         }
825         if (f.emph() == ON) {
826                 os << "\\emph{";
827                 count += 6;
828                 env = true; //We have opened a new environment
829         }
830         if (f.underbar() == ON) {
831                 os << "\\underbar{";
832                 count += 10;
833                 env = true; //We have opened a new environment
834         }
835         // \noun{} is a LyX special macro
836         if (f.noun() == ON) {
837                 os << "\\noun{";
838                 count += 6;
839                 env = true; //We have opened a new environment
840         }
841         if (f.size() != INHERIT_SIZE) {
842                 // If we didn't open an environment above, we open one here
843                 if (!env) {
844                         os << '{';
845                         ++count;
846                 }
847                 os << '\\'
848                    << LaTeXSizeNames[f.size()]
849                    << ' ';
850                 count += strlen(LaTeXSizeNames[f.size()]) + 2;
851         }
852         return count;
853 }
854
855
856 /// Writes ending block of LaTeX needed to close use of this font
857 // Returns number of chars written
858 // This one corresponds to latexWriteStartChanges(). (Asger)
859 int LyXFont::latexWriteEndChanges(ostream & os, LyXFont const & base,
860                                   LyXFont const & next) const
861 {
862         int count = 0;
863         bool env = false;
864
865         // reduce the current font to changes against the base
866         // font (of the layout). We use a temporary for this to
867         // avoid changing this font instance, as that would break
868         LyXFont f = *this;
869         f.reduce(base);
870
871         if (f.family() != INHERIT_FAMILY) {
872                 os << '}';
873                 ++count;
874                 env = true; // Size change need not bother about closing env.
875         }
876         if (f.series() != INHERIT_SERIES) {
877                 os << '}';
878                 ++count;
879                 env = true; // Size change need not bother about closing env.
880         }
881         if (f.shape() != INHERIT_SHAPE) {
882                 os << '}';
883                 ++count;
884                 env = true; // Size change need not bother about closing env.
885         }
886         if (f.color() != LColor::inherit && f.color() != LColor::ignore) {
887                 os << '}';
888                 ++count;
889                 env = true; // Size change need not bother about closing env.
890         }
891         if (f.emph() == ON) {
892                 os << '}';
893                 ++count;
894                 env = true; // Size change need not bother about closing env.
895         }
896         if (f.underbar() == ON) {
897                 os << '}';
898                 ++count;
899                 env = true; // Size change need not bother about closing env.
900         }
901         if (f.noun() == ON) {
902                 os << '}';
903                 ++count;
904                 env = true; // Size change need not bother about closing env.
905         }
906         if (f.size() != INHERIT_SIZE) {
907                 // We only have to close if only size changed
908                 if (!env) {
909                         os << '}';
910                         ++count;
911                 }
912         }
913
914         if (number() == ON && next.number() != ON &&
915             language()->lang() == "hebrew") {
916                 os << "\\endL}";
917                 count += 6;
918         }
919
920         if (language() != base.language() && language() != next.language()) {
921                 os << '}';
922                 ++count;
923         }
924
925         return count;
926 }
927
928
929 LColor_color LyXFont::realColor() const
930 {
931         if (color() == LColor::none)
932                 return LColor::foreground;
933         return color();
934 }
935
936
937 // Convert logical attributes to concrete shape attribute
938 LyXFont::FONT_SHAPE LyXFont::realShape() const
939 {
940         register FONT_SHAPE s = shape();
941
942         if (emph() == ON) {
943                 if (s == UP_SHAPE)
944                         s = ITALIC_SHAPE;
945                 else
946                         s = UP_SHAPE;
947         }
948         if (noun() == ON)
949                 s = SMALLCAPS_SHAPE;
950         return s;
951 }
952
953
954 ostream & operator<<(ostream & os, LyXFont::FONT_MISC_STATE fms)
955 {
956         return os << int(fms);
957 }
958
959
960 std::ostream & operator<<(std::ostream & os, LyXFont const & font)
961 {
962         return os << "font:"
963                 << " family " << font.bits.family
964                 << " series " << font.bits.series
965                 << " shape " << font.bits.shape
966                 << " size " << font.bits.size
967                 << " color " << font.bits.color
968                 << " emph " << font.bits.emph
969                 << " underbar " << font.bits.underbar
970                 << " noun " << font.bits.noun
971                 << " number " << font.bits.number
972                 << " lang: " << (font.lang ? font.lang->lang() : 0);
973 }