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