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