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