]> git.lyx.org Git - lyx.git/blob - src/lyxfont.C
Added new FINISED states FINISHED_RIGHT, FINISHED_UP, FINISHED_DOWN.
[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 "FontLoader.h"
26 #include "support/lstrings.h"
27 #include "bufferparams.h" // stateText
28
29 using std::ostream;
30 using std::endl;
31
32 #ifndef CXX_GLOBAL_CSTD
33 using std::strlen;
34 #endif
35
36
37 //
38 // Names for the GUI
39 //
40
41 namespace {
42
43 char const * GUIFamilyNames[6] = 
44 { N_("Roman"), N_("Sans serif"), N_("Typewriter"), N_("Symbol"), N_("Inherit"),
45   N_("Ignore") };
46
47 char const * GUISeriesNames[4] = 
48 { N_("Medium"), N_("Bold"), N_("Inherit"), N_("Ignore") };
49
50 char const * GUIShapeNames[6] = 
51 { N_("Upright"), N_("Italic"), N_("Slanted"), N_("Smallcaps"), N_("Inherit"),
52   N_("Ignore") };
53
54 char const * GUISizeNames[14] = 
55 { N_("Tiny"), N_("Smallest"), N_("Smaller"), N_("Small"), N_("Normal"), N_("Large"),
56   N_("Larger"), N_("Largest"), N_("Huge"), N_("Huger"), N_("Increase"), N_("Decrease"), 
57   N_("Inherit"), N_("Ignore") };
58  
59 char const * GUIMiscNames[5] = 
60 { N_("Off"), N_("On"), N_("Toggle"), N_("Inherit"), N_("Ignore") };
61
62
63 //
64 // Strings used to read and write .lyx format files
65 //
66 char const * LyXFamilyNames[6] = 
67 { "roman", "sans", "typewriter", "symbol", "default", "error" };
68
69 char const * LyXSeriesNames[4] = 
70 { "medium", "bold", "default", "error" };
71
72 char const * LyXShapeNames[6] = 
73 { "up", "italic", "slanted", "smallcaps", "default", "error" };
74
75 char const * LyXSizeNames[14] = 
76 { "tiny", "scriptsize", "footnotesize", "small", "normal", "large",
77   "larger", "largest", "huge", "giant", 
78   "increase-error", "decrease-error", "default", "error" };
79
80 char const * LyXMiscNames[5] = 
81 { "off", "on", "toggle", "default", "error" };
82
83 //
84 // Strings used to write LaTeX files
85 //
86 char const * LaTeXFamilyNames[6] = 
87 { "textrm", "textsf", "texttt", "error1", "error2", "error3" };
88
89 char const * LaTeXSeriesNames[4] = 
90 { "textmd", "textbf", "error4", "error5" };
91
92 char const * LaTeXShapeNames[6] = 
93 { "textup", "textit", "textsl", "textsc", "error6", "error7" };
94
95 char const * LaTeXSizeNames[14] = 
96 { "tiny", "scriptsize", "footnotesize", "small", "normalsize", "large",
97   "Large", "LARGE", "huge", "Huge", "error8", "error9", "error10", "error11" };
98
99 } // namespace anon
100
101
102 // Initialize static member
103 LyXFont::FontBits LyXFont::sane = {
104         ROMAN_FAMILY,
105         MEDIUM_SERIES,
106         UP_SHAPE,
107         SIZE_NORMAL,
108         LColor::none,
109         OFF,
110         OFF,
111         OFF,
112         OFF };
113
114 // Initialize static member
115 LyXFont::FontBits LyXFont::inherit = {
116         INHERIT_FAMILY,
117         INHERIT_SERIES,
118         INHERIT_SHAPE,
119         INHERIT_SIZE,
120         LColor::inherit,
121         INHERIT,
122         INHERIT,
123         INHERIT,
124         OFF };
125
126 // Initialize static member
127 LyXFont::FontBits LyXFont::ignore = {
128         IGNORE_FAMILY,
129         IGNORE_SERIES,
130         IGNORE_SHAPE,
131         IGNORE_SIZE,
132         LColor::ignore,
133         IGNORE,
134         IGNORE,
135         IGNORE,
136         IGNORE };
137
138
139 bool LyXFont::FontBits::operator==(LyXFont::FontBits const & fb1) const
140 {
141         return fb1.family == family &&
142                 fb1.series == series &&
143                 fb1.shape == shape &&
144                 fb1.size == size &&
145                 fb1.color == color &&
146                 fb1.emph == emph &&
147                 fb1.underbar == underbar &&
148                 fb1.noun == noun &&
149                 fb1.number == number;
150 }
151
152
153 bool LyXFont::FontBits::operator!=(LyXFont::FontBits const & fb1) const
154 {
155         return !(fb1 == *this);
156 }
157
158
159 LyXFont::LyXFont(LyXFont::FONT_INIT1)
160 #ifndef INHERIT_LANGUAGE
161         : bits(inherit), lang(default_language)
162 #else
163         : bits(inherit), lang(inherit_language)
164 #endif
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 #ifndef INHERIT_LANGUAGE
436                         setLanguage(document_language);
437 #else   
438                         setLanguage(inherit_language);
439 #endif
440         else if (newfont.language() != ignore_language)
441                 setLanguage(newfont.language());
442
443         if (newfont.color() == color() && toggleall)
444                 setColor(LColor::inherit); // toggle 'back'
445         else if (newfont.color() != LColor::ignore)
446                 setColor(newfont.color());
447 }
448
449
450 /// Reduce font to fall back to template where possible
451 void LyXFont::reduce(LyXFont const & tmplt)
452 {
453         if (family() == tmplt.family())
454                 setFamily(INHERIT_FAMILY);
455         if (series() == tmplt.series())
456                 setSeries(INHERIT_SERIES);
457         if (shape() == tmplt.shape())
458                 setShape(INHERIT_SHAPE);
459         if (size() == tmplt.size())
460                 setSize(INHERIT_SIZE);
461         if (emph() == tmplt.emph())
462                 setEmph(INHERIT);
463         if (underbar() == tmplt.underbar())
464                 setUnderbar(INHERIT);
465         if (noun() == tmplt.noun())
466                 setNoun(INHERIT);
467         if (color() == tmplt.color())
468                 setColor(LColor::inherit);
469 #ifdef INHERIT_LANGUAGE
470         if (language() == tmplt.language())
471                 setLanguage(inherit_language);
472 #endif
473 }
474
475
476 /// Realize font from a template
477 #ifndef INHERIT_LANGUAGE
478 LyXFont & LyXFont::realize(LyXFont const & tmplt)
479 #else
480 LyXFont & LyXFont::realize(LyXFont const & tmplt, Language const * deflang)
481 #endif
482 {
483 #ifdef INHERIT_LANGUAGE
484         if (language() == inherit_language) {
485                 if (tmplt.language() == inherit_language ||
486                         tmplt.language() == ignore_language ||
487                         tmplt.language() == default_language)
488                 {
489                         setLanguage(deflang);
490                 } else {
491                         setLanguage(tmplt.language());
492                 }
493         }
494 #endif
495         if (bits == inherit) {
496                 bits = tmplt.bits;
497                 return *this;
498         }
499         
500         if (bits.family == INHERIT_FAMILY) {
501                 bits.family = tmplt.bits.family;
502         }
503         if (bits.series == INHERIT_SERIES) {
504                 bits.series = tmplt.bits.series;
505         }
506         if (bits.shape == INHERIT_SHAPE) {
507                 bits.shape = tmplt.bits.shape;
508         }
509         if (bits.size == INHERIT_SIZE) {
510                 bits.size = tmplt.bits.size;
511         }
512         if (bits.emph == INHERIT) {
513                 bits.emph = tmplt.bits.emph;
514         }
515         if (bits.underbar == INHERIT) {
516                 bits.underbar = tmplt.bits.underbar;
517         }
518         if (bits.noun == INHERIT) {
519                 bits.noun = tmplt.bits.noun;
520         }
521         if (bits.color == LColor::inherit) {
522                 bits.color = tmplt.bits.color;
523         }
524         return *this;
525 }
526
527
528 /// Is font resolved?
529 bool LyXFont::resolved() const
530 {
531         return (family() != INHERIT_FAMILY && series() != INHERIT_SERIES &&
532                 shape() != INHERIT_SHAPE && size() != INHERIT_SIZE &&
533                 emph() != INHERIT && underbar() != INHERIT && 
534                 noun() != INHERIT &&
535 #ifdef INHERIT_LANGUAGE
536                 language() != inherit_language &&
537 #endif
538                 color() != LColor::inherit);
539 }
540
541
542 /// Build GUI description of font state
543 string const LyXFont::stateText(BufferParams * params) const
544 {
545         ostringstream ost;
546         if (family() != INHERIT_FAMILY)
547                 ost << _(GUIFamilyNames[family()]) << ", ";
548         if (series() != INHERIT_SERIES)
549                 ost << _(GUISeriesNames[series()]) << ", ";
550         if (shape() != INHERIT_SHAPE)
551                 ost << _(GUIShapeNames[shape()]) << ", ";
552         if (size() != INHERIT_SIZE)
553                 ost << _(GUISizeNames[size()]) << ", ";
554         if (color() != LColor::inherit)
555                 ost << lcolor.getGUIName(color()) << ", ";
556         if (emph() != INHERIT)
557                 ost << _("Emphasis ")
558                     << _(GUIMiscNames[emph()]) << ", ";
559         if (underbar() != INHERIT)
560                 ost << _("Underline ")
561                     << _(GUIMiscNames[underbar()]) << ", ";
562         if (noun() != INHERIT)
563                 ost << _("Noun ") << _(GUIMiscNames[noun()]) << ", ";
564         if (bits == inherit)
565                 ost << _("Default") << ", ";
566         if (!params || (language() != params->language))
567                 ost << _("Language: ") << _(language()->display()) << ", ";
568         if (number() != OFF)
569                 ost << _("  Number ") << _(GUIMiscNames[number()]);
570
571         string buf(ost.str().c_str());
572         buf = strip(buf, ' ');
573         buf = strip(buf, ',');
574         return buf;
575 }
576
577
578 // Set family according to lyx format string
579 LyXFont & LyXFont::setLyXFamily(string const & fam)
580 {
581         string const s = lowercase(fam);
582
583         int i = 0;
584         while (s != LyXFamilyNames[i] && LyXFamilyNames[i] != "error") ++i;
585         if (s == LyXFamilyNames[i]) {
586                 setFamily(LyXFont::FONT_FAMILY(i));
587         } else
588                 lyxerr << "LyXFont::setLyXFamily: Unknown family `"
589                        << s << '\'' << endl;
590         return *this;
591 }
592
593
594 // Set series according to lyx format string
595 LyXFont & LyXFont::setLyXSeries(string const & ser)
596 {
597         string const s = lowercase(ser);
598
599         int i = 0;
600         while (s != LyXSeriesNames[i] && LyXSeriesNames[i] != "error") ++i;
601         if (s == LyXSeriesNames[i]) {
602                 setSeries(LyXFont::FONT_SERIES(i));
603         } else
604                 lyxerr << "LyXFont::setLyXSeries: Unknown series `"
605                        << s << '\'' << endl;
606         return *this;
607 }
608
609
610 // Set shape according to lyx format string
611 LyXFont & LyXFont::setLyXShape(string const & sha)
612 {
613         string const s = lowercase(sha);
614
615         int i = 0;
616         while (s != LyXShapeNames[i] && LyXShapeNames[i] != "error") ++i;
617         if (s == LyXShapeNames[i]) {
618                 setShape(LyXFont::FONT_SHAPE(i));
619         } else
620                 lyxerr << "LyXFont::setLyXShape: Unknown shape `"
621                        << s << '\'' << endl;
622         return *this;
623 }
624
625
626 // Set size according to lyx format string
627 LyXFont & LyXFont::setLyXSize(string const & siz)
628 {
629         string const s = lowercase(siz);
630         int i = 0;
631         while (s != LyXSizeNames[i] && LyXSizeNames[i] != "error") ++i;
632         if (s == LyXSizeNames[i]) {
633                 setSize(LyXFont::FONT_SIZE(i));
634         } else
635                 lyxerr << "LyXFont::setLyXSize: Unknown size `"
636                        << s << '\'' << endl;
637         return *this;
638 }
639
640
641 // Set size according to lyx format string
642 LyXFont::FONT_MISC_STATE LyXFont::setLyXMisc(string const & siz)
643 {
644         string const s = lowercase(siz);
645         int i = 0;
646         while (s != LyXMiscNames[i] && LyXMiscNames[i] != "error") ++i;
647         if (s == LyXMiscNames[i])
648                 return FONT_MISC_STATE(i);
649         lyxerr << "LyXFont::setLyXMisc: Unknown misc flag `"
650                << s << '\'' << endl;
651         return OFF;
652 }
653
654
655 /// Sets color after LyX text format
656 LyXFont & LyXFont::setLyXColor(string const & col)
657 {
658         setColor(lcolor.getFromLyXName(col));
659         return *this;
660 }
661
662
663 // Returns size in latex format
664 string const LyXFont::latexSize() const
665 {
666         return LaTeXSizeNames[size()];
667 }
668
669
670 // Read a font definition from given file in lyx format
671 // Used for layouts
672 LyXFont & LyXFont::lyxRead(LyXLex & lex)
673 {
674         bool error = false;
675         bool finished = false;
676         while (!finished && lex.isOK() && !error) {
677                 lex.next();
678                 string const tok = lowercase(lex.getString());
679
680                 if (tok.empty()) {
681                         continue;
682                 } else if (tok == "endfont") {
683                         finished = true;
684                 } else if (tok == "family") {
685                         lex.next();
686                         string const ttok = lex.getString();
687                         setLyXFamily(ttok);
688                 } else if (tok == "series") {
689                         lex.next();
690                         string const ttok = lex.getString();
691                         setLyXSeries(ttok);
692                 } else if (tok == "shape") {
693                         lex.next();
694                         string const ttok = lex.getString();
695                         setLyXShape(ttok);
696                 } else if (tok == "size") {
697                         lex.next();
698                         string const ttok = lex.getString();
699                         setLyXSize(ttok);
700                 } else if (tok == "misc") {
701                         lex.next();
702                         string const ttok = lowercase(lex.getString());
703
704                         if (ttok == "no_bar") {
705                                 setUnderbar(OFF);
706                         } else if (ttok == "no_emph") {
707                                 setEmph(OFF);
708                         } else if (ttok == "no_noun") {
709                                 setNoun(OFF);
710                         } else if (ttok == "emph") {
711                                 setEmph(ON);
712                         } else if (ttok == "underbar") {
713                                 setUnderbar(ON);
714                         } else if (ttok == "noun") {
715                                 setNoun(ON);
716                         } else {
717                                 lex.printError("Illegal misc type `$$Token´");
718                         }
719                 } else if (tok == "color") {
720                         lex.next();
721                         string const ttok = lex.getString();
722                         setLyXColor(ttok);
723                 } else {
724                         lex.printError("Unknown tag `$$Token'");
725                         error = true;
726                 }
727         }
728         return *this;
729 }
730
731
732 /// Writes the changes from this font to orgfont in .lyx format in file
733 void LyXFont::lyxWriteChanges(LyXFont const & orgfont,
734 #ifdef INHERIT_LANGUAGE
735                               Language const * doclang, 
736 #endif
737                               ostream & os) const
738 {
739         os << "\n";
740         if (orgfont.family() != family()) {
741                 os << "\\family " << LyXFamilyNames[family()] << " \n";
742         }
743         if (orgfont.series() != series()) {
744                 os << "\\series " << LyXSeriesNames[series()] << " \n";
745         }
746         if (orgfont.shape() != shape()) {
747                 os << "\\shape " << LyXShapeNames[shape()] << " \n";
748         }
749         if (orgfont.size() != size()) {
750                 os << "\\size " << LyXSizeNames[size()] << " \n";
751         }
752         if (orgfont.emph() != emph()) {
753                 os << "\\emph " << LyXMiscNames[emph()] << " \n";
754         }
755         if (orgfont.number() != number()) {
756                 os << "\\numeric " << LyXMiscNames[number()] << " \n";
757         }
758         if (orgfont.underbar() != underbar()) {
759                 // This is only for backwards compatibility
760                 switch (underbar()) {
761                 case OFF:       os << "\\bar no \n"; break;
762                 case ON:        os << "\\bar under \n"; break;
763                 case TOGGLE:    lyxerr << "LyXFont::lyxWriteFontChanges: "
764                                         "TOGGLE should not appear here!"
765                                        << endl;
766                 break;
767                 case INHERIT:   os << "\\bar default \n"; break;
768                 case IGNORE:    lyxerr << "LyXFont::lyxWriteFontChanges: "
769                                         "IGNORE should not appear here!"
770                                        << endl;
771                 break;
772                 }
773         }
774         if (orgfont.noun() != noun()) {
775                 os << "\\noun " << LyXMiscNames[noun()] << " \n";
776         }
777         if (orgfont.color() != color()) {
778                 // To make us file compatible with older
779                 // lyx versions we emit "default" instead
780                 // of "inherit"
781                 string col_str(lcolor.getLyXName(color()));
782                 if (col_str == "inherit") col_str = "default";
783                 os << "\\color " << col_str << "\n";
784         }
785         if (orgfont.language() != language()) {
786 #ifndef INHERIT_LANGUAGE
787                 if (language())
788 #else
789                 if (language() == inherit_language)
790                         os << "\\lang " << doclang->lang() << "\n";
791                 else if (language())
792 #endif
793                         os << "\\lang " << language()->lang() << "\n";
794                 else
795                         os << "\\lang unknown\n";
796         }
797 }
798
799
800 /// Writes the head of the LaTeX needed to impose this font
801 // Returns number of chars written.
802 int LyXFont::latexWriteStartChanges(ostream & os, LyXFont const & base,
803                                     LyXFont const & prev) const
804 {
805         int count = 0;
806         bool env = false;
807
808         if (language()->babel() != base.language()->babel() &&
809             language()->babel() != prev.language()->babel()) {
810                 if (isRightToLeft() != prev.isRightToLeft()) {
811                         if (isRightToLeft()) {
812                                 os << "\\R{";
813                                 count += 3;
814                         } else {
815                                 os << "\\L{";
816                                 count += 3;
817                         }
818                 } else {
819                         string const tmp =
820                                 subst(lyxrc.language_command_local,
821                                       "$$lang", language()->babel());
822                         os << tmp;
823                         count += tmp.length();
824                 }
825         }
826
827         if (number() == ON && prev.number() != ON &&
828             language()->lang() == "hebrew") {
829                 os << "{\\beginL ";
830                 count += 9;
831         }
832
833         LyXFont f = *this;
834         f.reduce(base);
835         
836         if (f.family() != INHERIT_FAMILY) {
837                 os << '\\'
838                    << LaTeXFamilyNames[f.family()]
839                    << '{';
840                 count += strlen(LaTeXFamilyNames[f.family()]) + 2;
841                 env = true; //We have opened a new environment
842         }
843         if (f.series() != INHERIT_SERIES) {
844                 os << '\\'
845                    << LaTeXSeriesNames[f.series()]
846                    << '{';
847                 count += strlen(LaTeXSeriesNames[f.series()]) + 2;
848                 env = true; //We have opened a new environment
849         }
850         if (f.shape() != INHERIT_SHAPE) {
851                 os << '\\'
852                    << LaTeXShapeNames[f.shape()]
853                    << '{';
854                 count += strlen(LaTeXShapeNames[f.shape()]) + 2;
855                 env = true; //We have opened a new environment
856         }
857         if (f.color() != LColor::inherit && f.color() != LColor::ignore) {
858                 os << "\\textcolor{"
859                    << lcolor.getLaTeXName(f.color())
860                    << "}{";
861                 count += lcolor.getLaTeXName(f.color()).length() + 13;
862                 env = true; //We have opened a new environment
863         }
864         if (f.emph() == ON) {
865                 os << "\\emph{";
866                 count += 6;
867                 env = true; //We have opened a new environment
868         }
869         if (f.underbar() == ON) {
870                 os << "\\underbar{";
871                 count += 10;
872                 env = true; //We have opened a new environment
873         }
874         // \noun{} is a LyX special macro
875         if (f.noun() == ON) {
876                 os << "\\noun{";
877                 count += 8;
878                 env = true; //We have opened a new environment
879         }
880         if (f.size() != INHERIT_SIZE) {
881                 // If we didn't open an environment above, we open one here
882                 if (!env) {
883                         os << '{';
884                         ++count;
885                 }
886                 os << '\\'
887                    << LaTeXSizeNames[f.size()]
888                    << ' ';
889                 count += strlen(LaTeXSizeNames[f.size()]) + 2;
890         }
891         return count;
892 }
893
894
895 /// Writes ending block of LaTeX needed to close use of this font
896 // Returns number of chars written
897 // This one corresponds to latexWriteStartChanges(). (Asger)
898 int LyXFont::latexWriteEndChanges(ostream & os, LyXFont const & base,
899                                   LyXFont const & next) const
900 {
901         int count = 0;
902         bool env = false;
903
904         LyXFont f = *this; // why do you need this?
905         f.reduce(base); // why isn't this just "reduce(base);" (Lgb)
906         // Because this function is const. Everything breaks if this
907         // method changes the font it represents. There is no speed penalty
908         // by using the temporary. (Asger)
909
910
911         if (f.family() != INHERIT_FAMILY) {
912                 os << '}';
913                 ++count;
914                 env = true; // Size change need not bother about closing env.
915         }
916         if (f.series() != INHERIT_SERIES) {
917                 os << '}';
918                 ++count;
919                 env = true; // Size change need not bother about closing env.
920         }
921         if (f.shape() != INHERIT_SHAPE) {
922                 os << '}';
923                 ++count;
924                 env = true; // Size change need not bother about closing env.
925         }
926         if (f.color() != LColor::inherit && f.color() != LColor::ignore) {
927                 os << '}';
928                 ++count;
929                 env = true; // Size change need not bother about closing env.
930         }
931         if (f.emph() == ON) {
932                 os << '}';
933                 ++count;
934                 env = true; // Size change need not bother about closing env.
935         }
936         if (f.underbar() == ON) {
937                 os << '}';
938                 ++count;
939                 env = true; // Size change need not bother about closing env.
940         }
941         if (f.noun() == ON) {
942                 os << '}';
943                 ++count;
944                 env = true; // Size change need not bother about closing env.
945         }
946         if (f.size() != INHERIT_SIZE) {
947                 // We only have to close if only size changed
948                 if (!env) {
949                         os << '}';
950                         ++count;
951                 }
952         }
953
954         if (number() == ON && next.number() != ON &&
955             language()->lang() == "hebrew") {
956                 os << "\\endL}";
957                 count += 6;
958         }
959
960         if (language() != base.language() && language() != next.language()) {
961                 os << "}";
962                 ++count;
963         }
964
965         return count;
966 }
967
968
969 LColor::color LyXFont::realColor() const
970 {
971         if (color() == LColor::none)
972                 return LColor::foreground;
973         return color();
974 }
975
976
977 // Convert logical attributes to concrete shape attribute
978 LyXFont::FONT_SHAPE LyXFont::realShape() const
979 {
980         register FONT_SHAPE s = shape();
981
982         if (emph() == ON) {
983                 if (s == UP_SHAPE)
984                         s = ITALIC_SHAPE;
985                 else
986                         s = UP_SHAPE;
987         }
988         if (noun() == ON)
989                 s = SMALLCAPS_SHAPE;
990         return s;
991 }
992
993
994 ostream & operator<<(ostream & o, LyXFont::FONT_MISC_STATE fms)
995 {
996         return o << int(fms);
997 }