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