]> git.lyx.org Git - lyx.git/blob - src/lyxfont.C
f805eec4f92eec0bc92010cc57fdc5ea7d341f6a
[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() == language() && toggleall)
293                 if (language() == document_language)
294                         setLanguage(default_language);
295                 else
296                         setLanguage(document_language);
297         else if (newfont.language() != ignore_language)
298                 setLanguage(newfont.language());
299
300         if (newfont.color() == color() && toggleall)
301                 setColor(LColor::inherit); // toggle 'back'
302         else if (newfont.color() != LColor::ignore)
303                 setColor(newfont.color());
304 }
305
306
307 /// Reduce font to fall back to template where possible
308 void LyXFont::reduce(LyXFont const & tmplt)
309 {
310         if (family() == tmplt.family())
311                 setFamily(INHERIT_FAMILY);
312         if (series() == tmplt.series())
313                 setSeries(INHERIT_SERIES);
314         if (shape() == tmplt.shape())
315                 setShape(INHERIT_SHAPE);
316         if (size() == tmplt.size())
317                 setSize(INHERIT_SIZE);
318         if (emph() == tmplt.emph())
319                 setEmph(INHERIT);
320         if (underbar() == tmplt.underbar())
321                 setUnderbar(INHERIT);
322         if (noun() == tmplt.noun())
323                 setNoun(INHERIT);
324         if (latex() == tmplt.latex())
325                 setLatex(INHERIT);
326         if (color() == tmplt.color())
327                 setColor(LColor::inherit);
328 }
329
330
331 /// Realize font from a template
332 LyXFont & LyXFont::realize(LyXFont const & tmplt)
333 {
334         if (bits == inherit) {
335                 bits = tmplt.bits;
336                 return *this;
337         }
338         
339         if (bits.family == INHERIT_FAMILY) {
340                 bits.family = tmplt.bits.family;
341         }
342         if (bits.series == INHERIT_SERIES) {
343                 bits.series = tmplt.bits.series;
344         }
345         if (bits.shape == INHERIT_SHAPE) {
346                 bits.shape = tmplt.bits.shape;
347         }
348         if (bits.size == INHERIT_SIZE) {
349                 bits.size = tmplt.bits.size;
350         }
351         if (bits.emph == INHERIT) {
352                 bits.emph = tmplt.bits.emph;
353         }
354         if (bits.underbar == INHERIT) {
355                 bits.underbar = tmplt.bits.underbar;
356         }
357         if (bits.noun == INHERIT) {
358                 bits.noun = tmplt.bits.noun;
359         }
360         if (bits.latex == INHERIT) {
361                 bits.latex = tmplt.bits.latex;
362         }
363         if (bits.color == LColor::inherit) {
364                 bits.color = tmplt.bits.color;
365         }
366
367         return *this;
368 }
369
370
371 /// Is font resolved?
372 bool LyXFont::resolved() const
373 {
374         return (family() != INHERIT_FAMILY && series() != INHERIT_SERIES &&
375                 shape() != INHERIT_SHAPE && size() != INHERIT_SIZE &&
376                 emph() != INHERIT && underbar() != INHERIT && 
377                 noun() != INHERIT && latex() != INHERIT && 
378                 color() != LColor::inherit);
379 }
380
381
382 /// Build GUI description of font state
383 string LyXFont::stateText() const
384 {
385 #ifdef HAVE_SSTREAM
386         std::ostringstream ost;
387 #else
388         char str[1024];
389         ostrstream ost(str, 1024);
390 #endif
391         if (family() != INHERIT_FAMILY)
392                 ost << _(GUIFamilyNames[family()]) << ", ";
393         if (series() != INHERIT_SERIES)
394                 ost << _(GUISeriesNames[series()]) << ", ";
395         if (shape() != INHERIT_SHAPE)
396                 ost << _(GUIShapeNames[shape()]) << ", ";
397         if (size() != INHERIT_SIZE)
398                 ost << _(GUISizeNames[size()]) << ", ";
399         if (color() != LColor::inherit)
400                 ost << lcolor.getGUIName(color()) << ", ";
401         if (emph() != INHERIT)
402                 ost << _("Emphasis ")
403                     << _(GUIMiscNames[emph()]) << ", ";
404         if (underbar() != INHERIT)
405                 ost << _("Underline ")
406                     << _(GUIMiscNames[underbar()]) << ", ";
407         if (noun() != INHERIT)
408                 ost << _("Noun ") << _(GUIMiscNames[noun()]) << ", ";
409         if (latex() != INHERIT)
410                 ost << _("Latex ") << _(GUIMiscNames[latex()]) << ", ";
411         //if (buf.empty())
412         //      ost << _("Default");
413         unsigned int opos = ost.tellp();
414         //lyxerr << "Opos: " << opos << endl;
415         if (opos == 0)
416                 ost << _("Default") << ", ";
417         ost << _("Language: ") << _(language()->display.c_str());
418 #ifdef HAVE_SSTREAM
419         string buf(ost.str().c_str());
420 #else
421         ost << '\0';
422         string buf(ost.str());
423 #endif
424         buf = strip(buf, ' ');
425         buf = strip(buf, ',');
426         return buf;
427 }
428
429
430 // Set family according to lyx format string
431 LyXFont & LyXFont::setLyXFamily(string const & fam)
432 {
433         string s = lowercase(fam);
434
435         int i = 0;
436         while (s != LyXFamilyNames[i] && LyXFamilyNames[i] != "error") ++i;
437         if (s == LyXFamilyNames[i]) {
438                 setFamily(LyXFont::FONT_FAMILY(i));
439         } else
440                 lyxerr << "LyXFont::setLyXFamily: Unknown family `"
441                        << s << '\'' << endl;
442         return *this;
443 }
444
445
446 // Set series according to lyx format string
447 LyXFont & LyXFont::setLyXSeries(string const & ser)
448 {
449         string s = lowercase(ser);
450
451         int i = 0;
452         while (s != LyXSeriesNames[i] && LyXSeriesNames[i] != "error") ++i;
453         if (s == LyXSeriesNames[i]) {
454                 setSeries(LyXFont::FONT_SERIES(i));
455         } else
456                 lyxerr << "LyXFont::setLyXSeries: Unknown series `"
457                        << s << '\'' << endl;
458         return *this;
459 }
460
461
462 // Set shape according to lyx format string
463 LyXFont & LyXFont::setLyXShape(string const & sha)
464 {
465         string s = lowercase(sha);
466
467         int i = 0;
468         while (s != LyXShapeNames[i] && LyXShapeNames[i] != "error") ++i;
469         if (s == LyXShapeNames[i]) {
470                 setShape(LyXFont::FONT_SHAPE(i));
471         } else
472                 lyxerr << "LyXFont::setLyXShape: Unknown shape `"
473                        << s << '\'' << endl;
474         return *this;
475 }
476
477
478 // Set size according to lyx format string
479 LyXFont & LyXFont::setLyXSize(string const & siz)
480 {
481         string s = lowercase(siz);
482         int i = 0;
483         while (s != LyXSizeNames[i] && LyXSizeNames[i] != "error") ++i;
484         if (s == LyXSizeNames[i]) {
485                 setSize(LyXFont::FONT_SIZE(i));
486         } else
487                 lyxerr << "LyXFont::setLyXSize: Unknown size `"
488                        << s << '\'' << endl;
489         return *this;
490 }
491
492
493 // Set size according to lyx format string
494 LyXFont::FONT_MISC_STATE LyXFont::setLyXMisc(string const & siz)
495 {
496         string s = lowercase(siz);
497         int i = 0;
498         while (s != LyXMiscNames[i] && LyXMiscNames[i] != "error") ++i;
499         if (s == LyXMiscNames[i])
500                 return FONT_MISC_STATE(i);
501         lyxerr << "LyXFont::setLyXMisc: Unknown misc flag `"
502                << s << '\'' << endl;
503         return OFF;
504 }
505
506
507 /// Sets color after LyX text format
508 LyXFont & LyXFont::setLyXColor(string const & col)
509 {
510         setColor(lcolor.getFromLyXName(col));
511         return *this;
512 }
513
514
515 /// Sets size after GUI name
516 LyXFont & LyXFont::setGUISize(string const & siz)
517 {
518         // CHECK this might be wrong.
519         /// ??????
520         // this is how it was how it was done in the lyx repository...
521         // but this does not make sense.
522         setColor(lcolor.getFromGUIName(siz));
523         return *this;
524 }
525
526
527 // Returns size in latex format
528 string LyXFont::latexSize() const
529 {
530         return LaTeXSizeNames[size()];
531 }
532
533
534 // Read a font definition from given file in lyx format
535 // Used for layouts
536 LyXFont & LyXFont::lyxRead(LyXLex & lex)
537 {
538         bool error = false;
539         bool finished = false;
540         while (!finished && lex.IsOK() && !error) {
541                 lex.next();
542                 string tok = lowercase(lex.GetString());
543
544                 if (tok.empty()) {
545                         continue;
546                 } else if (tok == "endfont") {
547                         finished = true;
548                 } else if (tok == "family") {
549                         lex.next();
550                         string tok = lex.GetString();
551                         setLyXFamily(tok);
552                 } else if (tok == "series") {
553                         lex.next();
554                         string tok = lex.GetString();
555                         setLyXSeries(tok);
556                 } else if (tok == "shape") {
557                         lex.next();
558                         string tok = lex.GetString();
559                         setLyXShape(tok);
560                 } else if (tok == "size") {
561                         lex.next();
562                         string tok = lex.GetString();
563                         setLyXSize(tok);
564                 } else if (tok == "latex") {
565                         lex.next();
566                         string tok = lowercase(lex.GetString());
567
568                         if (tok == "no_latex") {
569                                 setLatex(OFF);
570                         } else if (tok == "latex") {
571                                 setLatex(ON);
572                         } else {
573                                 lex.printError("Illegal LaTeX type`$$Token'");
574                         }
575                 } else if (tok == "misc") {
576                         lex.next();
577                         string tok = lowercase(lex.GetString());
578
579                         if (tok == "no_bar") {
580                                 setUnderbar(OFF);
581                         } else if (tok == "no_emph") {
582                                 setEmph(OFF);
583                         } else if (tok == "no_noun") {
584                                 setNoun(OFF);
585                         } else if (tok == "emph") {
586                                 setEmph(ON);
587                         } else if (tok == "underbar") {
588                                 setUnderbar(ON);
589                         } else if (tok == "noun") {
590                                 setNoun(ON);
591                         } else {
592                                 lex.printError("Illegal misc type `$$Token´");
593                         }
594                 } else if (tok == "color") {
595                         lex.next();
596                         string tok = lex.GetString();
597                         setLyXColor(tok);
598                 } else {
599                         lex.printError("Unknown tag `$$Token'");
600                         error = true;
601                 }
602         }
603         return *this;
604 }
605
606
607 /// Writes the changes from this font to orgfont in .lyx format in file
608 void LyXFont::lyxWriteChanges(LyXFont const & orgfont, ostream & os) const
609 {
610         os << "\n";
611         if (orgfont.family() != family()) {
612                 os << "\\family " << LyXFamilyNames[family()] << " \n";
613         }
614         if (orgfont.series() != series()) {
615                 os << "\\series " << LyXSeriesNames[series()] << " \n";
616         }
617         if (orgfont.shape() != shape()) {
618                 os << "\\shape " << LyXShapeNames[shape()] << " \n";
619         }
620         if (orgfont.size() != size()) {
621                 os << "\\size " << LyXSizeNames[size()] << " \n";
622         }
623         if (orgfont.emph() != emph()) {
624                 os << "\\emph " << LyXMiscNames[emph()] << " \n";
625         }
626         if (orgfont.underbar() != underbar()) {
627                 // This is only for backwards compatibility
628                 switch (underbar()) {
629                 case OFF:       os << "\\bar no \n"; break;
630                 case ON:        os << "\\bar under \n"; break;
631                 case TOGGLE:    lyxerr << "LyXFont::lyxWriteFontChanges: "
632                                         "TOGGLE should not appear here!"
633                                        << endl;
634                 break;
635                 case INHERIT:   os << "\\bar default \n"; break;
636                 case IGNORE:    lyxerr << "LyXFont::lyxWriteFontChanges: "
637                                         "IGNORE should not appear here!"
638                                        << endl;
639                 break;
640                 }
641         }
642         if (orgfont.noun() != noun()) {
643                 os << "\\noun " << LyXMiscNames[noun()] << " \n";
644         }
645         if (orgfont.latex() != latex()) {
646                 // This is only for backwards compatibility
647                 switch (latex()) {
648                 case OFF:       os << "\\latex no_latex \n"; break;
649                 case ON:        os << "\\latex latex \n"; break;
650                 case TOGGLE:    lyxerr << "LyXFont::lyxWriteFontChanges: "
651                                         "TOGGLE should not appear here!"
652                                        << endl;
653                 break;
654                 case INHERIT:   os << "\\latex default \n"; break;
655                 case IGNORE:    lyxerr << "LyXFont::lyxWriteFontChanges: "
656                                         "IGNORE should not appear here!"
657                                        << endl;
658                 break;
659                 }
660         }
661         if (orgfont.color() != color()) {
662                 os << "\\color " << lcolor.getLyXName(color()) << "\n";
663         }
664         if (orgfont.language() != language()) {
665                 if (language())
666                         os << "\\lang " << language()->lang << "\n";
667                 else
668                         os << "\\lang unknown\n";
669         }
670 }
671
672
673 /// Writes the head of the LaTeX needed to impose this font
674 // Returns number of chars written.
675 int LyXFont::latexWriteStartChanges(ostream & os, LyXFont const & base,
676                                     LyXFont const & prev) const
677 {
678         int count = 0;
679         bool env = false;
680
681         if (language() != base.language() && language() != prev.language()) {
682                 if (isRightToLeft() != prev.isRightToLeft()) {
683                         if (isRightToLeft()) {
684                                 os << "\\R{";
685                                 count += 3;
686                                 env = true; //We have opened a new environment
687                         } else {
688                                 os << "\\L{";
689                                 count += 3;
690                                 env = true; //We have opened a new environment
691                         }
692                 } else {
693                         string tmp = '{' + 
694                                 subst(lyxrc.language_command_begin,
695                                       "$$lang", language()->lang);
696                         os << tmp;
697                         count += tmp.length();
698                         env = true; //We have opened a new environment
699                 }
700         }
701
702         LyXFont f = *this;
703         f.reduce(base);
704         
705         if (f.family() != INHERIT_FAMILY) {
706                 os << '\\'
707                    << LaTeXFamilyNames[f.family()]
708                    << '{';
709                 count += strlen(LaTeXFamilyNames[f.family()]) + 2;
710                 env = true; //We have opened a new environment
711         }
712         if (f.series() != INHERIT_SERIES) {
713                 os << '\\'
714                    << LaTeXSeriesNames[f.series()]
715                    << '{';
716                 count += strlen(LaTeXSeriesNames[f.series()]) + 2;
717                 env = true; //We have opened a new environment
718         }
719         if (f.shape() != INHERIT_SHAPE) {
720                 os << '\\'
721                    << LaTeXShapeNames[f.shape()]
722                    << '{';
723                 count += strlen(LaTeXShapeNames[f.shape()]) + 2;
724                 env = true; //We have opened a new environment
725         }
726         if (f.color() != LColor::inherit) {
727                 os << "\\textcolor{"
728                    << lcolor.getLaTeXName(f.color())
729                    << "}{";
730                 count += lcolor.getLaTeXName(f.color()).length() + 13;
731                 env = true; //We have opened a new environment
732         }
733         if (f.emph() == ON) {
734                 os << "\\emph{";
735                 count += 6;
736                 env = true; //We have opened a new environment
737         }
738         if (f.underbar() == ON) {
739                 os << "\\underbar{";
740                 count += 10;
741                 env = true; //We have opened a new environment
742         }
743         // \noun{} is a LyX special macro
744         if (f.noun() == ON) {
745                 os << "\\noun{";
746                 count += 8;
747                 env = true; //We have opened a new environment
748         }
749         if (f.size() != INHERIT_SIZE) {
750                 // If we didn't open an environment above, we open one here
751                 if (!env) {
752                         os << '{';
753                         ++count;
754                 }
755                 os << '\\'
756                    << LaTeXSizeNames[f.size()]
757                    << ' ';
758                 count += strlen(LaTeXSizeNames[f.size()]) + 2;
759         }
760         return count;
761 }
762
763
764 /// Writes ending block of LaTeX needed to close use of this font
765 // Returns number of chars written
766 // This one corresponds to latexWriteStartChanges(). (Asger)
767 int LyXFont::latexWriteEndChanges(ostream & os, LyXFont const & base,
768                                   LyXFont const & next) const
769 {
770         int count = 0;
771         bool env = false;
772
773         if (language() != base.language() && language() != next.language()) {
774                 os << "}";
775                 ++count;
776                 env = true; // Size change need not bother about closing env.
777         }
778
779         LyXFont f = *this; // why do you need this?
780         f.reduce(base); // why isn't this just "reduce(base);" (Lgb)
781         // Because this function is const. Everything breaks if this
782         // method changes the font it represents. There is no speed penalty
783         // by using the temporary. (Asger)
784
785         if (f.family() != INHERIT_FAMILY) {
786                 os << '}';
787                 ++count;
788                 env = true; // Size change need not bother about closing env.
789         }
790         if (f.series() != INHERIT_SERIES) {
791                 os << '}';
792                 ++count;
793                 env = true; // Size change need not bother about closing env.
794         }
795         if (f.shape() != INHERIT_SHAPE) {
796                 os << '}';
797                 ++count;
798                 env = true; // Size change need not bother about closing env.
799         }
800         if (f.color() != LColor::inherit) {
801                 os << '}';
802                 ++count;
803                 env = true; // Size change need not bother about closing env.
804         }
805         if (f.emph() == ON) {
806                 os << '}';
807                 ++count;
808                 env = true; // Size change need not bother about closing env.
809         }
810         if (f.underbar() == ON) {
811                 os << '}';
812                 ++count;
813                 env = true; // Size change need not bother about closing env.
814         }
815         if (f.noun() == ON) {
816                 os << '}';
817                 ++count;
818                 env = true; // Size change need not bother about closing env.
819         }
820         if (f.size() != INHERIT_SIZE) {
821                 // We only have to close if only size changed
822                 if (!env) {
823                         os << '}';
824                         ++count;
825                 }
826         }
827
828         return count;
829 }
830
831
832 LColor::color LyXFont::realColor() const
833 {
834         if (latex() == ON)
835                 return LColor::latex;
836         if (color() == LColor::none)
837                 return LColor::foreground;
838         return color();
839 }
840
841
842 // Convert logical attributes to concrete shape attribute
843 LyXFont::FONT_SHAPE LyXFont::realShape() const
844 {
845         register FONT_SHAPE s = shape();
846
847         if (emph() == ON) {
848                 if (s == UP_SHAPE)
849                         s = ITALIC_SHAPE;
850                 else
851                         s = UP_SHAPE;
852         }
853         if (noun() == ON)
854                 s = SMALLCAPS_SHAPE;
855         return s;
856 }
857
858
859 bool LyXFont::equalExceptLatex(LyXFont const & f) const 
860 {
861         LyXFont f1 = *this;
862         f1.setLatex(f.latex());
863         return f1 == f;
864 }
865
866
867 ostream & operator<<(ostream & o, LyXFont::FONT_MISC_STATE fms)
868 {
869         return o << int(fms);
870 }