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