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