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