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