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