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