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