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