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