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