]> git.lyx.org Git - lyx.git/blob - src/lyxfont.C
065a5d8d72752f108753e1d9003c464bad2be183
[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         setNumber(setMisc(newfont.number(), number()));
294         if (newfont.language() == language() && toggleall)
295                 setLanguage(document_language);
296         else if (newfont.language() != ignore_language)
297                 setLanguage(newfont.language());
298
299         if (newfont.color() == color() && toggleall)
300                 setColor(LColor::inherit); // toggle 'back'
301         else if (newfont.color() != LColor::ignore)
302                 setColor(newfont.color());
303 }
304
305
306 /// Reduce font to fall back to template where possible
307 void LyXFont::reduce(LyXFont const & tmplt)
308 {
309         if (family() == tmplt.family())
310                 setFamily(INHERIT_FAMILY);
311         if (series() == tmplt.series())
312                 setSeries(INHERIT_SERIES);
313         if (shape() == tmplt.shape())
314                 setShape(INHERIT_SHAPE);
315         if (size() == tmplt.size())
316                 setSize(INHERIT_SIZE);
317         if (emph() == tmplt.emph())
318                 setEmph(INHERIT);
319         if (underbar() == tmplt.underbar())
320                 setUnderbar(INHERIT);
321         if (noun() == tmplt.noun())
322                 setNoun(INHERIT);
323         if (latex() == tmplt.latex())
324                 setLatex(INHERIT);
325         if (color() == tmplt.color())
326                 setColor(LColor::inherit);
327 }
328
329
330 /// Realize font from a template
331 LyXFont & LyXFont::realize(LyXFont const & tmplt)
332 {
333         if (bits == inherit) {
334                 bits = tmplt.bits;
335                 return *this;
336         }
337         
338         if (bits.family == INHERIT_FAMILY) {
339                 bits.family = tmplt.bits.family;
340         }
341         if (bits.series == INHERIT_SERIES) {
342                 bits.series = tmplt.bits.series;
343         }
344         if (bits.shape == INHERIT_SHAPE) {
345                 bits.shape = tmplt.bits.shape;
346         }
347         if (bits.size == INHERIT_SIZE) {
348                 bits.size = tmplt.bits.size;
349         }
350         if (bits.emph == INHERIT) {
351                 bits.emph = tmplt.bits.emph;
352         }
353         if (bits.underbar == INHERIT) {
354                 bits.underbar = tmplt.bits.underbar;
355         }
356         if (bits.noun == INHERIT) {
357                 bits.noun = tmplt.bits.noun;
358         }
359         if (bits.latex == INHERIT) {
360                 bits.latex = tmplt.bits.latex;
361         }
362         if (bits.color == LColor::inherit) {
363                 bits.color = tmplt.bits.color;
364         }
365
366         return *this;
367 }
368
369
370 /// Is font resolved?
371 bool LyXFont::resolved() const
372 {
373         return (family() != INHERIT_FAMILY && series() != INHERIT_SERIES &&
374                 shape() != INHERIT_SHAPE && size() != INHERIT_SIZE &&
375                 emph() != INHERIT && underbar() != INHERIT && 
376                 noun() != INHERIT && latex() != INHERIT && 
377                 color() != LColor::inherit);
378 }
379
380
381 /// Build GUI description of font state
382 string const LyXFont::stateText(BufferParams * params) const
383 {
384         std::ostringstream ost;
385         if (family() != INHERIT_FAMILY)
386                 ost << _(GUIFamilyNames[family()]) << ", ";
387         if (series() != INHERIT_SERIES)
388                 ost << _(GUISeriesNames[series()]) << ", ";
389         if (shape() != INHERIT_SHAPE)
390                 ost << _(GUIShapeNames[shape()]) << ", ";
391         if (size() != INHERIT_SIZE)
392                 ost << _(GUISizeNames[size()]) << ", ";
393         if (color() != LColor::inherit)
394                 ost << lcolor.getGUIName(color()) << ", ";
395         if (emph() != INHERIT)
396                 ost << _("Emphasis ")
397                     << _(GUIMiscNames[emph()]) << ", ";
398         if (underbar() != INHERIT)
399                 ost << _("Underline ")
400                     << _(GUIMiscNames[underbar()]) << ", ";
401         if (noun() != INHERIT)
402                 ost << _("Noun ") << _(GUIMiscNames[noun()]) << ", ";
403         if (latex() != INHERIT)
404                 ost << _("Latex ") << _(GUIMiscNames[latex()]) << ", ";
405         if (bits == inherit)
406                 ost << _("Default") << ", ";
407         if (!params || (language() != params->language_info))
408                 ost << _("Language: ") << _(language()->display().c_str()) << ", ";
409         if (number() != OFF)
410                 ost << _("  Number ") << _(GUIMiscNames[number()]);
411
412
413         string buf(ost.str().c_str());
414         buf = strip(buf, ' ');
415         buf = strip(buf, ',');
416         return buf;
417 }
418
419
420 // Set family according to lyx format string
421 LyXFont & LyXFont::setLyXFamily(string const & fam)
422 {
423         string s = lowercase(fam);
424
425         int i = 0;
426         while (s != LyXFamilyNames[i] && LyXFamilyNames[i] != "error") ++i;
427         if (s == LyXFamilyNames[i]) {
428                 setFamily(LyXFont::FONT_FAMILY(i));
429         } else
430                 lyxerr << "LyXFont::setLyXFamily: Unknown family `"
431                        << s << '\'' << endl;
432         return *this;
433 }
434
435
436 // Set series according to lyx format string
437 LyXFont & LyXFont::setLyXSeries(string const & ser)
438 {
439         string s = lowercase(ser);
440
441         int i = 0;
442         while (s != LyXSeriesNames[i] && LyXSeriesNames[i] != "error") ++i;
443         if (s == LyXSeriesNames[i]) {
444                 setSeries(LyXFont::FONT_SERIES(i));
445         } else
446                 lyxerr << "LyXFont::setLyXSeries: Unknown series `"
447                        << s << '\'' << endl;
448         return *this;
449 }
450
451
452 // Set shape according to lyx format string
453 LyXFont & LyXFont::setLyXShape(string const & sha)
454 {
455         string s = lowercase(sha);
456
457         int i = 0;
458         while (s != LyXShapeNames[i] && LyXShapeNames[i] != "error") ++i;
459         if (s == LyXShapeNames[i]) {
460                 setShape(LyXFont::FONT_SHAPE(i));
461         } else
462                 lyxerr << "LyXFont::setLyXShape: Unknown shape `"
463                        << s << '\'' << endl;
464         return *this;
465 }
466
467
468 // Set size according to lyx format string
469 LyXFont & LyXFont::setLyXSize(string const & siz)
470 {
471         string s = lowercase(siz);
472         int i = 0;
473         while (s != LyXSizeNames[i] && LyXSizeNames[i] != "error") ++i;
474         if (s == LyXSizeNames[i]) {
475                 setSize(LyXFont::FONT_SIZE(i));
476         } else
477                 lyxerr << "LyXFont::setLyXSize: Unknown size `"
478                        << s << '\'' << endl;
479         return *this;
480 }
481
482
483 // Set size according to lyx format string
484 LyXFont::FONT_MISC_STATE LyXFont::setLyXMisc(string const & siz)
485 {
486         string s = lowercase(siz);
487         int i = 0;
488         while (s != LyXMiscNames[i] && LyXMiscNames[i] != "error") ++i;
489         if (s == LyXMiscNames[i])
490                 return FONT_MISC_STATE(i);
491         lyxerr << "LyXFont::setLyXMisc: Unknown misc flag `"
492                << s << '\'' << endl;
493         return OFF;
494 }
495
496
497 /// Sets color after LyX text format
498 LyXFont & LyXFont::setLyXColor(string const & col)
499 {
500         setColor(lcolor.getFromLyXName(col));
501         return *this;
502 }
503
504
505 /// Sets size after GUI name
506 LyXFont & LyXFont::setGUISize(string const & siz)
507 {
508         // CHECK this might be wrong.
509         /// ??????
510         // this is how it was how it was done in the lyx repository...
511         // but this does not make sense.
512         setColor(lcolor.getFromGUIName(siz));
513         return *this;
514 }
515
516
517 // Returns size in latex format
518 string const LyXFont::latexSize() const
519 {
520         return LaTeXSizeNames[size()];
521 }
522
523
524 // Read a font definition from given file in lyx format
525 // Used for layouts
526 LyXFont & LyXFont::lyxRead(LyXLex & lex)
527 {
528         bool error = false;
529         bool finished = false;
530         while (!finished && lex.IsOK() && !error) {
531                 lex.next();
532                 string tok = lowercase(lex.GetString());
533
534                 if (tok.empty()) {
535                         continue;
536                 } else if (tok == "endfont") {
537                         finished = true;
538                 } else if (tok == "family") {
539                         lex.next();
540                         string tok = lex.GetString();
541                         setLyXFamily(tok);
542                 } else if (tok == "series") {
543                         lex.next();
544                         string tok = lex.GetString();
545                         setLyXSeries(tok);
546                 } else if (tok == "shape") {
547                         lex.next();
548                         string tok = lex.GetString();
549                         setLyXShape(tok);
550                 } else if (tok == "size") {
551                         lex.next();
552                         string tok = lex.GetString();
553                         setLyXSize(tok);
554                 } else if (tok == "latex") {
555                         lex.next();
556                         string tok = lowercase(lex.GetString());
557
558                         if (tok == "no_latex") {
559                                 setLatex(OFF);
560                         } else if (tok == "latex") {
561                                 setLatex(ON);
562                         } else {
563                                 lex.printError("Illegal LaTeX type`$$Token'");
564                         }
565                 } else if (tok == "misc") {
566                         lex.next();
567                         string tok = lowercase(lex.GetString());
568
569                         if (tok == "no_bar") {
570                                 setUnderbar(OFF);
571                         } else if (tok == "no_emph") {
572                                 setEmph(OFF);
573                         } else if (tok == "no_noun") {
574                                 setNoun(OFF);
575                         } else if (tok == "emph") {
576                                 setEmph(ON);
577                         } else if (tok == "underbar") {
578                                 setUnderbar(ON);
579                         } else if (tok == "noun") {
580                                 setNoun(ON);
581                         } else {
582                                 lex.printError("Illegal misc type `$$Token´");
583                         }
584                 } else if (tok == "color") {
585                         lex.next();
586                         string tok = lex.GetString();
587                         setLyXColor(tok);
588                 } else {
589                         lex.printError("Unknown tag `$$Token'");
590                         error = true;
591                 }
592         }
593         return *this;
594 }
595
596
597 /// Writes the changes from this font to orgfont in .lyx format in file
598 void LyXFont::lyxWriteChanges(LyXFont const & orgfont, ostream & os) const
599 {
600         os << "\n";
601         if (orgfont.family() != family()) {
602                 os << "\\family " << LyXFamilyNames[family()] << " \n";
603         }
604         if (orgfont.series() != series()) {
605                 os << "\\series " << LyXSeriesNames[series()] << " \n";
606         }
607         if (orgfont.shape() != shape()) {
608                 os << "\\shape " << LyXShapeNames[shape()] << " \n";
609         }
610         if (orgfont.size() != size()) {
611                 os << "\\size " << LyXSizeNames[size()] << " \n";
612         }
613         if (orgfont.emph() != emph()) {
614                 os << "\\emph " << LyXMiscNames[emph()] << " \n";
615         }
616         if (orgfont.number() != number()) {
617                 os << "\\numeric " << LyXMiscNames[number()] << " \n";
618         }
619         if (orgfont.underbar() != underbar()) {
620                 // This is only for backwards compatibility
621                 switch (underbar()) {
622                 case OFF:       os << "\\bar no \n"; break;
623                 case ON:        os << "\\bar under \n"; break;
624                 case TOGGLE:    lyxerr << "LyXFont::lyxWriteFontChanges: "
625                                         "TOGGLE should not appear here!"
626                                        << endl;
627                 break;
628                 case INHERIT:   os << "\\bar default \n"; break;
629                 case IGNORE:    lyxerr << "LyXFont::lyxWriteFontChanges: "
630                                         "IGNORE should not appear here!"
631                                        << endl;
632                 break;
633                 }
634         }
635         if (orgfont.noun() != noun()) {
636                 os << "\\noun " << LyXMiscNames[noun()] << " \n";
637         }
638         if (orgfont.latex() != latex()) {
639                 // This is only for backwards compatibility
640                 switch (latex()) {
641                 case OFF:       os << "\\latex no_latex \n"; break;
642                 case ON:        os << "\\latex latex \n"; break;
643                 case TOGGLE:    lyxerr << "LyXFont::lyxWriteFontChanges: "
644                                         "TOGGLE should not appear here!"
645                                        << endl;
646                 break;
647                 case INHERIT:   os << "\\latex default \n"; break;
648                 case IGNORE:    lyxerr << "LyXFont::lyxWriteFontChanges: "
649                                         "IGNORE should not appear here!"
650                                        << endl;
651                 break;
652                 }
653         }
654         if (orgfont.color() != color()) {
655                 // To make us file compatible with older
656                 // lyx versions we emit "default" instead
657                 // of "inherit"
658                 string col_str(lcolor.getLyXName(color()));
659                 if (col_str == "inherit") col_str = "default";
660                 os << "\\color " << col_str << "\n";
661         }
662         if (orgfont.language() != language()) {
663                 if (language())
664                         os << "\\lang " << language()->lang() << "\n";
665                 else
666                         os << "\\lang unknown\n";
667         }
668 }
669
670
671 /// Writes the head of the LaTeX needed to impose this font
672 // Returns number of chars written.
673 int LyXFont::latexWriteStartChanges(ostream & os, LyXFont const & base,
674                                     LyXFont const & prev) const
675 {
676         int count = 0;
677         bool env = false;
678
679         if (language() != base.language() && language() != prev.language()) {
680                 if (isRightToLeft() != prev.isRightToLeft()) {
681                         if (isRightToLeft()) {
682                                 os << "\\R{";
683                                 count += 3;
684                         } else {
685                                 os << "\\L{";
686                                 count += 3;
687                         }
688                 } else {
689                         string tmp = '{' + 
690                                 subst(lyxrc.language_command_begin,
691                                       "$$lang", language()->lang());
692                         os << tmp;
693                         count += tmp.length();
694                 }
695         }
696
697         if (number() == ON && prev.number() != ON &&
698             language()->lang() == "hebrew") {
699                 os << "{\\beginL ";
700                 count += 9;
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 && f.color() != LColor::ignore) {
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         LyXFont f = *this; // why do you need this?
775         f.reduce(base); // why isn't this just "reduce(base);" (Lgb)
776         // Because this function is const. Everything breaks if this
777         // method changes the font it represents. There is no speed penalty
778         // by using the temporary. (Asger)
779
780
781         if (f.family() != INHERIT_FAMILY) {
782                 os << '}';
783                 ++count;
784                 env = true; // Size change need not bother about closing env.
785         }
786         if (f.series() != INHERIT_SERIES) {
787                 os << '}';
788                 ++count;
789                 env = true; // Size change need not bother about closing env.
790         }
791         if (f.shape() != INHERIT_SHAPE) {
792                 os << '}';
793                 ++count;
794                 env = true; // Size change need not bother about closing env.
795         }
796         if (f.color() != LColor::inherit && f.color() != LColor::ignore) {
797                 os << '}';
798                 ++count;
799                 env = true; // Size change need not bother about closing env.
800         }
801         if (f.emph() == ON) {
802                 os << '}';
803                 ++count;
804                 env = true; // Size change need not bother about closing env.
805         }
806         if (f.underbar() == ON) {
807                 os << '}';
808                 ++count;
809                 env = true; // Size change need not bother about closing env.
810         }
811         if (f.noun() == ON) {
812                 os << '}';
813                 ++count;
814                 env = true; // Size change need not bother about closing env.
815         }
816         if (f.size() != INHERIT_SIZE) {
817                 // We only have to close if only size changed
818                 if (!env) {
819                         os << '}';
820                         ++count;
821                 }
822         }
823
824         if (number() == ON && next.number() != ON &&
825             language()->lang() == "hebrew") {
826                 os << "\\endL}";
827                 count += 6;
828         }
829
830         if (language() != base.language() && language() != next.language()) {
831                 os << "}";
832                 ++count;
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 }