]> git.lyx.org Git - lyx.git/blob - src/lyxfont.C
remove noload/don't typeset
[lyx.git] / src / lyxfont.C
1 /* This file is part of
2  * ======================================================
3  *
4  *           LyX, The Document Processor
5  *
6  *           Copyright 1995 Matthias Ettrich
7  *           Copyright 1995-2001 The LyX Team.
8  *
9  * ====================================================== */
10
11 #include <config.h>
12 #include <clocale>
13 #include <cctype>
14
15 #ifdef __GNUG__
16 #pragma implementation
17 #endif
18
19
20 #include "gettext.h"
21 #include "lyxfont.h"
22 #include "debug.h"
23 #include "lyxrc.h"
24 #include "lyxlex.h"
25 #include "language.h"
26 #include "support/lstrings.h"
27 #include "bufferparams.h" // stateText
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 ost;
517         if (family() != INHERIT_FAMILY)
518                 ost << _(GUIFamilyNames[family()]) << ", ";
519         if (series() != INHERIT_SERIES)
520                 ost << _(GUISeriesNames[series()]) << ", ";
521         if (shape() != INHERIT_SHAPE)
522                 ost << _(GUIShapeNames[shape()]) << ", ";
523         if (size() != INHERIT_SIZE)
524                 ost << _(GUISizeNames[size()]) << ", ";
525         if (color() != LColor::inherit)
526                 ost << lcolor.getGUIName(color()) << ", ";
527         if (emph() != INHERIT)
528                 ost << _("Emphasis ")
529                     << _(GUIMiscNames[emph()]) << ", ";
530         if (underbar() != INHERIT)
531                 ost << _("Underline ")
532                     << _(GUIMiscNames[underbar()]) << ", ";
533         if (noun() != INHERIT)
534                 ost << _("Noun ") << _(GUIMiscNames[noun()]) << ", ";
535         if (bits == inherit)
536                 ost << _("Default") << ", ";
537         if (!params || (language() != params->language))
538                 ost << _("Language: ") << _(language()->display()) << ", ";
539         if (number() != OFF)
540                 ost << _("  Number ") << _(GUIMiscNames[number()]);
541
542         string buf(ost.str().c_str());
543         buf = rtrim(buf, ", ");
544         return buf;
545 }
546
547
548 // Set family according to lyx format string
549 LyXFont & LyXFont::setLyXFamily(string const & fam)
550 {
551         string const s = ascii_lowercase(fam);
552
553         int i = 0;
554         while (s != LyXFamilyNames[i] && LyXFamilyNames[i] != "error") ++i;
555         if (s == LyXFamilyNames[i]) {
556                 setFamily(LyXFont::FONT_FAMILY(i));
557         } else
558                 lyxerr << "LyXFont::setLyXFamily: Unknown family `"
559                        << s << '\'' << endl;
560         return *this;
561 }
562
563
564 // Set series according to lyx format string
565 LyXFont & LyXFont::setLyXSeries(string const & ser)
566 {
567         string const s = ascii_lowercase(ser);
568
569         int i = 0;
570         while (s != LyXSeriesNames[i] && LyXSeriesNames[i] != "error") ++i;
571         if (s == LyXSeriesNames[i]) {
572                 setSeries(LyXFont::FONT_SERIES(i));
573         } else
574                 lyxerr << "LyXFont::setLyXSeries: Unknown series `"
575                        << s << '\'' << endl;
576         return *this;
577 }
578
579
580 // Set shape according to lyx format string
581 LyXFont & LyXFont::setLyXShape(string const & sha)
582 {
583         string const s = ascii_lowercase(sha);
584
585         int i = 0;
586         while (s != LyXShapeNames[i] && LyXShapeNames[i] != "error") ++i;
587         if (s == LyXShapeNames[i]) {
588                 setShape(LyXFont::FONT_SHAPE(i));
589         } else
590                 lyxerr << "LyXFont::setLyXShape: Unknown shape `"
591                        << s << '\'' << endl;
592         return *this;
593 }
594
595
596 // Set size according to lyx format string
597 LyXFont & LyXFont::setLyXSize(string const & siz)
598 {
599         string const s = ascii_lowercase(siz);
600         int i = 0;
601         while (s != LyXSizeNames[i] && LyXSizeNames[i] != "error") ++i;
602         if (s == LyXSizeNames[i]) {
603                 setSize(LyXFont::FONT_SIZE(i));
604         } else
605                 lyxerr << "LyXFont::setLyXSize: Unknown size `"
606                        << s << '\'' << endl;
607         return *this;
608 }
609
610
611 // Set size according to lyx format string
612 LyXFont::FONT_MISC_STATE LyXFont::setLyXMisc(string const & siz)
613 {
614         string const s = ascii_lowercase(siz);
615         int i = 0;
616         while (s != LyXMiscNames[i] && LyXMiscNames[i] != "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 += 8;
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 & o, LyXFont::FONT_MISC_STATE fms)
955 {
956         return o << int(fms);
957 }