]> git.lyx.org Git - lyx.git/blob - src/lyxfont.C
some sun compile fixes the need clipboard code patch from Dekel ans some other fixes
[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 (bits == inherit)
412                 ost << _("Default") << ", ";
413         ost << _("Language: ") << _(language()->display.c_str());
414 #ifdef HAVE_SSTREAM
415         string buf(ost.str().c_str());
416 #else
417         ost << '\0';
418         string buf(ost.str());
419 #endif
420         buf = strip(buf, ' ');
421         buf = strip(buf, ',');
422         return buf;
423 }
424
425
426 // Set family according to lyx format string
427 LyXFont & LyXFont::setLyXFamily(string const & fam)
428 {
429         string s = lowercase(fam);
430
431         int i = 0;
432         while (s != LyXFamilyNames[i] && LyXFamilyNames[i] != "error") ++i;
433         if (s == LyXFamilyNames[i]) {
434                 setFamily(LyXFont::FONT_FAMILY(i));
435         } else
436                 lyxerr << "LyXFont::setLyXFamily: Unknown family `"
437                        << s << '\'' << endl;
438         return *this;
439 }
440
441
442 // Set series according to lyx format string
443 LyXFont & LyXFont::setLyXSeries(string const & ser)
444 {
445         string s = lowercase(ser);
446
447         int i = 0;
448         while (s != LyXSeriesNames[i] && LyXSeriesNames[i] != "error") ++i;
449         if (s == LyXSeriesNames[i]) {
450                 setSeries(LyXFont::FONT_SERIES(i));
451         } else
452                 lyxerr << "LyXFont::setLyXSeries: Unknown series `"
453                        << s << '\'' << endl;
454         return *this;
455 }
456
457
458 // Set shape according to lyx format string
459 LyXFont & LyXFont::setLyXShape(string const & sha)
460 {
461         string s = lowercase(sha);
462
463         int i = 0;
464         while (s != LyXShapeNames[i] && LyXShapeNames[i] != "error") ++i;
465         if (s == LyXShapeNames[i]) {
466                 setShape(LyXFont::FONT_SHAPE(i));
467         } else
468                 lyxerr << "LyXFont::setLyXShape: Unknown shape `"
469                        << s << '\'' << endl;
470         return *this;
471 }
472
473
474 // Set size according to lyx format string
475 LyXFont & LyXFont::setLyXSize(string const & siz)
476 {
477         string s = lowercase(siz);
478         int i = 0;
479         while (s != LyXSizeNames[i] && LyXSizeNames[i] != "error") ++i;
480         if (s == LyXSizeNames[i]) {
481                 setSize(LyXFont::FONT_SIZE(i));
482         } else
483                 lyxerr << "LyXFont::setLyXSize: Unknown size `"
484                        << s << '\'' << endl;
485         return *this;
486 }
487
488
489 // Set size according to lyx format string
490 LyXFont::FONT_MISC_STATE LyXFont::setLyXMisc(string const & siz)
491 {
492         string s = lowercase(siz);
493         int i = 0;
494         while (s != LyXMiscNames[i] && LyXMiscNames[i] != "error") ++i;
495         if (s == LyXMiscNames[i])
496                 return FONT_MISC_STATE(i);
497         lyxerr << "LyXFont::setLyXMisc: Unknown misc flag `"
498                << s << '\'' << endl;
499         return OFF;
500 }
501
502
503 /// Sets color after LyX text format
504 LyXFont & LyXFont::setLyXColor(string const & col)
505 {
506         setColor(lcolor.getFromLyXName(col));
507         return *this;
508 }
509
510
511 /// Sets size after GUI name
512 LyXFont & LyXFont::setGUISize(string const & siz)
513 {
514         // CHECK this might be wrong.
515         /// ??????
516         // this is how it was how it was done in the lyx repository...
517         // but this does not make sense.
518         setColor(lcolor.getFromGUIName(siz));
519         return *this;
520 }
521
522
523 // Returns size in latex format
524 string LyXFont::latexSize() const
525 {
526         return LaTeXSizeNames[size()];
527 }
528
529
530 // Read a font definition from given file in lyx format
531 // Used for layouts
532 LyXFont & LyXFont::lyxRead(LyXLex & lex)
533 {
534         bool error = false;
535         bool finished = false;
536         while (!finished && lex.IsOK() && !error) {
537                 lex.next();
538                 string tok = lowercase(lex.GetString());
539
540                 if (tok.empty()) {
541                         continue;
542                 } else if (tok == "endfont") {
543                         finished = true;
544                 } else if (tok == "family") {
545                         lex.next();
546                         string tok = lex.GetString();
547                         setLyXFamily(tok);
548                 } else if (tok == "series") {
549                         lex.next();
550                         string tok = lex.GetString();
551                         setLyXSeries(tok);
552                 } else if (tok == "shape") {
553                         lex.next();
554                         string tok = lex.GetString();
555                         setLyXShape(tok);
556                 } else if (tok == "size") {
557                         lex.next();
558                         string tok = lex.GetString();
559                         setLyXSize(tok);
560                 } else if (tok == "latex") {
561                         lex.next();
562                         string tok = lowercase(lex.GetString());
563
564                         if (tok == "no_latex") {
565                                 setLatex(OFF);
566                         } else if (tok == "latex") {
567                                 setLatex(ON);
568                         } else {
569                                 lex.printError("Illegal LaTeX type`$$Token'");
570                         }
571                 } else if (tok == "misc") {
572                         lex.next();
573                         string tok = lowercase(lex.GetString());
574
575                         if (tok == "no_bar") {
576                                 setUnderbar(OFF);
577                         } else if (tok == "no_emph") {
578                                 setEmph(OFF);
579                         } else if (tok == "no_noun") {
580                                 setNoun(OFF);
581                         } else if (tok == "emph") {
582                                 setEmph(ON);
583                         } else if (tok == "underbar") {
584                                 setUnderbar(ON);
585                         } else if (tok == "noun") {
586                                 setNoun(ON);
587                         } else {
588                                 lex.printError("Illegal misc type `$$Token´");
589                         }
590                 } else if (tok == "color") {
591                         lex.next();
592                         string tok = lex.GetString();
593                         setLyXColor(tok);
594                 } else {
595                         lex.printError("Unknown tag `$$Token'");
596                         error = true;
597                 }
598         }
599         return *this;
600 }
601
602
603 /// Writes the changes from this font to orgfont in .lyx format in file
604 void LyXFont::lyxWriteChanges(LyXFont const & orgfont, ostream & os) const
605 {
606         os << "\n";
607         if (orgfont.family() != family()) {
608                 os << "\\family " << LyXFamilyNames[family()] << " \n";
609         }
610         if (orgfont.series() != series()) {
611                 os << "\\series " << LyXSeriesNames[series()] << " \n";
612         }
613         if (orgfont.shape() != shape()) {
614                 os << "\\shape " << LyXShapeNames[shape()] << " \n";
615         }
616         if (orgfont.size() != size()) {
617                 os << "\\size " << LyXSizeNames[size()] << " \n";
618         }
619         if (orgfont.emph() != emph()) {
620                 os << "\\emph " << LyXMiscNames[emph()] << " \n";
621         }
622         if (orgfont.underbar() != underbar()) {
623                 // This is only for backwards compatibility
624                 switch (underbar()) {
625                 case OFF:       os << "\\bar no \n"; break;
626                 case ON:        os << "\\bar under \n"; break;
627                 case TOGGLE:    lyxerr << "LyXFont::lyxWriteFontChanges: "
628                                         "TOGGLE should not appear here!"
629                                        << endl;
630                 break;
631                 case INHERIT:   os << "\\bar default \n"; break;
632                 case IGNORE:    lyxerr << "LyXFont::lyxWriteFontChanges: "
633                                         "IGNORE should not appear here!"
634                                        << endl;
635                 break;
636                 }
637         }
638         if (orgfont.noun() != noun()) {
639                 os << "\\noun " << LyXMiscNames[noun()] << " \n";
640         }
641         if (orgfont.latex() != latex()) {
642                 // This is only for backwards compatibility
643                 switch (latex()) {
644                 case OFF:       os << "\\latex no_latex \n"; break;
645                 case ON:        os << "\\latex latex \n"; break;
646                 case TOGGLE:    lyxerr << "LyXFont::lyxWriteFontChanges: "
647                                         "TOGGLE should not appear here!"
648                                        << endl;
649                 break;
650                 case INHERIT:   os << "\\latex default \n"; break;
651                 case IGNORE:    lyxerr << "LyXFont::lyxWriteFontChanges: "
652                                         "IGNORE should not appear here!"
653                                        << endl;
654                 break;
655                 }
656         }
657         if (orgfont.color() != color()) {
658                 // To make us file compatible with older
659                 // lyx versions we emit "default" instead
660                 // of "inherit"
661                 string col_str(lcolor.getLyXName(color()));
662                 if (col_str == "inherit") col_str = "default";
663                 os << "\\color " << col_str << "\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 }