]> git.lyx.org Git - lyx.git/blob - src/FontInfo.cpp
9adef0c59bba15be2773aa6e3516a51b183f3894
[lyx.git] / src / FontInfo.cpp
1 /**
2  * \file src/FontInfo.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 "ColorSet.h"
18 #include "FontInfo.h"
19 #include "Lexer.h"
20 #include "LyXRC.h"
21
22 #include "support/convert.h"
23 #include "support/debug.h"
24 #include "support/docstring.h"
25 #include "support/gettext.h"
26 #include "support/lstrings.h"
27 #include "support/RefChanger.h"
28
29 #include <algorithm>
30 #include <ostream>
31 #include <sstream>
32
33 using namespace std;
34 using namespace lyx::support;
35
36 namespace lyx {
37
38 //
39 // Names for the GUI
40 //
41
42 char const * GUIFamilyNames[NUM_FAMILIES + 2 /* default & error */] =
43 { N_("Roman"), N_("Sans Serif"), N_("Typewriter"), N_("Symbol"),
44   "cmr", "cmsy", "cmm", "cmex", "msa", "msb", "eufrak", "rsfs", "stmry",
45   "wasy", "esint", N_("Inherit"), N_("Ignore") };
46
47 char const * GUISeriesNames[NUM_SERIES + 2 /* default & error */] =
48 { N_("Medium"), N_("Bold"), N_("Inherit"), N_("Ignore") };
49
50 char const * GUIShapeNames[NUM_SHAPE + 2 /* default & error */] =
51 { N_("Upright"), N_("Italic"), N_("Slanted"), N_("Smallcaps"), N_("Inherit"),
52   N_("Ignore") };
53
54 char const * GUISizeNames[NUM_SIZE + 4 /* increase, decrease, default & error */] =
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[NUM_FAMILIES + 2 /* default & error */] =
67 { "roman", "sans", "typewriter", "symbol",
68   "cmr", "cmsy", "cmm", "cmex", "msa", "msb", "eufrak", "rsfs", "stmry",
69   "wasy", "esint", "default", "error" };
70
71 char const * LyXSeriesNames[NUM_SERIES + 2 /* default & error */] =
72 { "medium", "bold", "default", "error" };
73
74 char const * LyXShapeNames[NUM_SHAPE + 2 /* default & error */] =
75 { "up", "italic", "slanted", "smallcaps", "default", "error" };
76
77 char const * LyXSizeNames[NUM_SIZE + 4 /* increase, decrease, default & error */] =
78 { "tiny", "scriptsize", "footnotesize", "small", "normal", "large",
79   "larger", "largest", "huge", "giant",
80   "increase", "decrease", "default", "error" };
81
82 char const * LyXMiscNames[5] =
83 { "off", "on", "toggle", "default", "error" };
84
85
86 FontInfo const sane_font(
87         ROMAN_FAMILY,
88         MEDIUM_SERIES,
89         UP_SHAPE,
90         FONT_SIZE_NORMAL,
91         FONT_STYLE_TEXT,
92         Color_none,
93         Color_background,
94         FONT_OFF,
95         FONT_OFF,
96         FONT_OFF,
97         FONT_OFF,
98         FONT_OFF,
99         FONT_OFF,
100         FONT_OFF,
101         FONT_OFF,
102         FONT_OFF);
103
104 FontInfo const inherit_font(
105         INHERIT_FAMILY,
106         INHERIT_SERIES,
107         INHERIT_SHAPE,
108         FONT_SIZE_INHERIT,
109         FONT_STYLE_INHERIT,
110         Color_inherit,
111         Color_inherit,
112         FONT_INHERIT,
113         FONT_INHERIT,
114         FONT_INHERIT,
115         FONT_INHERIT,
116         FONT_INHERIT,
117         FONT_INHERIT,
118         FONT_INHERIT,
119         FONT_OFF,
120         FONT_INHERIT);
121
122 FontInfo const ignore_font(
123         IGNORE_FAMILY,
124         IGNORE_SERIES,
125         IGNORE_SHAPE,
126         FONT_SIZE_IGNORE,
127         FONT_STYLE_IGNORE,
128         Color_ignore,
129         Color_ignore,
130         FONT_IGNORE,
131         FONT_IGNORE,
132         FONT_IGNORE,
133         FONT_IGNORE,
134         FONT_IGNORE,
135         FONT_IGNORE,
136         FONT_IGNORE,
137         FONT_IGNORE,
138         FONT_IGNORE);
139
140
141 FontInfo::FontInfo()
142 {
143         *this = sane_font;
144 }
145
146
147 /// Decreases font size_ by one
148 FontInfo & FontInfo::decSize()
149 {
150         switch (size_) {
151         case FONT_SIZE_HUGER:        size_ = FONT_SIZE_HUGE;     break;
152         case FONT_SIZE_HUGE:         size_ = FONT_SIZE_LARGEST;  break;
153         case FONT_SIZE_LARGEST:      size_ = FONT_SIZE_LARGER;   break;
154         case FONT_SIZE_LARGER:       size_ = FONT_SIZE_LARGE;    break;
155         case FONT_SIZE_LARGE:        size_ = FONT_SIZE_NORMAL;   break;
156         case FONT_SIZE_NORMAL:       size_ = FONT_SIZE_SMALL;    break;
157         case FONT_SIZE_SMALL:        size_ = FONT_SIZE_FOOTNOTE; break;
158         case FONT_SIZE_FOOTNOTE:     size_ = FONT_SIZE_SCRIPT;   break;
159         case FONT_SIZE_SCRIPT:       size_ = FONT_SIZE_TINY;     break;
160         case FONT_SIZE_TINY:         break;
161         case FONT_SIZE_INCREASE:
162                 LYXERR0("Can't FontInfo::decSize on FONT_SIZE_INCREASE");
163                 break;
164         case FONT_SIZE_DECREASE:
165                 LYXERR0("Can't FontInfo::decSize on FONT_SIZE_DECREASE");
166                 break;
167         case FONT_SIZE_INHERIT:
168                 LYXERR0("Can't FontInfo::decSize on FONT_SIZE_INHERIT");
169                 break;
170         case FONT_SIZE_IGNORE:
171                 LYXERR0("Can't FontInfo::decSize on FONT_SIZE_IGNORE");
172                 break;
173         }
174         return *this;
175 }
176
177
178 /// Increases font size_ by one
179 FontInfo & FontInfo::incSize()
180 {
181         switch (size_) {
182         case FONT_SIZE_HUGER:   break;
183         case FONT_SIZE_HUGE:         size_ = FONT_SIZE_HUGER;    break;
184         case FONT_SIZE_LARGEST:      size_ = FONT_SIZE_HUGE;     break;
185         case FONT_SIZE_LARGER:       size_ = FONT_SIZE_LARGEST;  break;
186         case FONT_SIZE_LARGE:        size_ = FONT_SIZE_LARGER;   break;
187         case FONT_SIZE_NORMAL:       size_ = FONT_SIZE_LARGE;    break;
188         case FONT_SIZE_SMALL:        size_ = FONT_SIZE_NORMAL;   break;
189         case FONT_SIZE_FOOTNOTE:     size_ = FONT_SIZE_SMALL;    break;
190         case FONT_SIZE_SCRIPT:       size_ = FONT_SIZE_FOOTNOTE; break;
191         case FONT_SIZE_TINY:         size_ = FONT_SIZE_SCRIPT;   break;
192         case FONT_SIZE_INCREASE:
193                 LYXERR0("Can't FontInfo::incSize on FONT_SIZE_INCREASE");
194                 break;
195         case FONT_SIZE_DECREASE:
196                 LYXERR0("Can't FontInfo::incSize on FONT_SIZE_DECREASE");
197                 break;
198         case FONT_SIZE_INHERIT:
199                 LYXERR0("Can't FontInfo::incSize on FONT_SIZE_INHERIT");
200                 break;
201         case FONT_SIZE_IGNORE:
202                 LYXERR0("Can't FontInfo::incSize on FONT_SIZE_IGNORE");
203                 break;
204         }
205         return *this;
206 }
207
208
209 double FontInfo::realSize() const
210 {
211         double d = convert<double>(lyxrc.font_sizes[size()]);
212         // The following is according to the average of the values in the
213         // definitions of \defaultscriptratio and \defaultscriptscriptratio in LaTeX
214         // font packages. No attempt is made to implement the actual values from
215         // \DefineMathSizes.
216         switch (style()) {
217         case FONT_STYLE_DISPLAY:
218         case FONT_STYLE_TEXT:
219         case FONT_STYLE_INHERIT:
220         case FONT_STYLE_IGNORE:
221                 break;
222         case FONT_STYLE_SCRIPT:
223                 d *= .73;
224                 break;
225         case FONT_STYLE_SCRIPTSCRIPT:
226                 d *= .55;
227                 break;
228         }
229         // Never go below the smallest size
230         return max(d, convert<double>(lyxrc.font_sizes[FONT_SIZE_TINY]));
231 }
232
233
234 /// Reduce font to fall back to template where possible
235 void FontInfo::reduce(FontInfo const & tmplt)
236 {
237         if (family_ == tmplt.family_)
238                 family_ = INHERIT_FAMILY;
239         if (series_ == tmplt.series_)
240                 series_ = INHERIT_SERIES;
241         if (shape_ == tmplt.shape_)
242                 shape_ = INHERIT_SHAPE;
243         if (size_ == tmplt.size_)
244                 size_ = FONT_SIZE_INHERIT;
245         if (style_ == tmplt.style_)
246                 style_ = FONT_STYLE_INHERIT;
247         if (emph_ == tmplt.emph_)
248                 emph_ = FONT_INHERIT;
249         if (underbar_ == tmplt.underbar_)
250                 underbar_ = FONT_INHERIT;
251         if (strikeout_ == tmplt.strikeout_)
252                 strikeout_ = FONT_INHERIT;
253         if (xout_ == tmplt.xout_)
254                 xout_ = FONT_INHERIT;
255         if (uuline_ == tmplt.uuline_)
256                 uuline_ = FONT_INHERIT;
257         if (uwave_ == tmplt.uwave_)
258                 uwave_ = FONT_INHERIT;
259         if (noun_ == tmplt.noun_)
260                 noun_ = FONT_INHERIT;
261         if (color_ == tmplt.color_)
262                 color_ = Color_inherit;
263         if (background_ == tmplt.background_)
264                 background_ = Color_inherit;
265         if (nospellcheck_ == tmplt.nospellcheck_)
266                 nospellcheck_ = FONT_INHERIT;
267 }
268
269
270 /// Realize font from a template
271 FontInfo & FontInfo::realize(FontInfo const & tmplt)
272 {
273         if ((*this) == inherit_font) {
274                 operator=(tmplt);
275                 return *this;
276         }
277
278         if (family_ == INHERIT_FAMILY)
279                 family_ = tmplt.family_;
280
281         if (series_ == INHERIT_SERIES)
282                 series_ = tmplt.series_;
283
284         if (shape_ == INHERIT_SHAPE)
285                 shape_ = tmplt.shape_;
286
287         if (size_ == FONT_SIZE_INHERIT)
288                 size_ = tmplt.size_;
289
290         if (style_ == FONT_STYLE_INHERIT)
291                 style_ = tmplt.style_;
292
293         if (emph_ == FONT_INHERIT)
294                 emph_ = tmplt.emph_;
295
296         if (underbar_ == FONT_INHERIT)
297                 underbar_ = tmplt.underbar_;
298
299         if (strikeout_ == FONT_INHERIT)
300                 strikeout_ = tmplt.strikeout_;
301
302         if (xout_ == FONT_INHERIT)
303                 xout_ = tmplt.xout_;
304
305         if (uuline_ == FONT_INHERIT)
306                 uuline_ = tmplt.uuline_;
307
308         if (uwave_ == FONT_INHERIT)
309                 uwave_ = tmplt.uwave_;
310
311         if (noun_ == FONT_INHERIT)
312                 noun_ = tmplt.noun_;
313
314         if (color_ == Color_inherit)
315                 color_ = tmplt.color_;
316
317         if (background_ == Color_inherit)
318                 background_ = tmplt.background_;
319
320         if (nospellcheck_ == FONT_INHERIT)
321                 nospellcheck_ = tmplt.nospellcheck_;
322
323         return *this;
324 }
325
326
327 Changer FontInfo::changeColor(ColorCode const color)
328 {
329         return make_change(color_, color);
330 }
331
332
333 Changer FontInfo::changeShape(FontShape const shape)
334 {
335         return make_change(shape_, shape);
336 }
337
338
339 Changer FontInfo::changeStyle(MathStyle const new_style)
340 {
341         return make_change(style_, new_style);
342 }
343
344
345 Changer FontInfo::change(FontInfo font, bool realiz)
346 {
347         if (realiz)
348                 font.realize(*this);
349         return make_change(*this, font);
350 }
351
352
353 /// Updates a misc setting according to request
354 static FontState setMisc(FontState newfont,
355         FontState org)
356 {
357         if (newfont == FONT_TOGGLE) {
358                 if (org == FONT_ON)
359                         return FONT_OFF;
360                 else if (org == FONT_OFF)
361                         return FONT_ON;
362                 else {
363                         LYXERR0("Font::setMisc: Need state"
364                                 " FONT_ON or FONT_OFF to toggle. Setting to FONT_ON");
365                         return FONT_ON;
366                 }
367         } else if (newfont == FONT_IGNORE)
368                 return org;
369         else
370                 return newfont;
371 }
372
373 /// Updates font settings according to request
374 void FontInfo::update(FontInfo const & newfont, bool toggleall)
375 {
376         if (newfont.family_ == family_ && toggleall)
377                 setFamily(INHERIT_FAMILY); // toggle 'back'
378         else if (newfont.family_ != IGNORE_FAMILY)
379                 setFamily(newfont.family_);
380         // else it's IGNORE_SHAPE
381
382         // "Old" behaviour: "Setting" bold will toggle bold on/off.
383         switch (newfont.series_) {
384         case BOLD_SERIES:
385                 // We toggle...
386                 if (series_ == BOLD_SERIES && toggleall)
387                         setSeries(MEDIUM_SERIES);
388                 else
389                         setSeries(BOLD_SERIES);
390                 break;
391         case MEDIUM_SERIES:
392         case INHERIT_SERIES:
393                 setSeries(newfont.series_);
394                 break;
395         case IGNORE_SERIES:
396                 break;
397         }
398
399         if (newfont.shape_ == shape_ && toggleall)
400                 shape_ = INHERIT_SHAPE; // toggle 'back'
401         else if (newfont.shape_ != IGNORE_SHAPE)
402                 shape_ = newfont.shape_;
403         // else it's IGNORE_SHAPE
404
405         if (newfont.size_ != FONT_SIZE_IGNORE) {
406                 if (newfont.size_ == FONT_SIZE_INCREASE)
407                         incSize();
408                 else if (newfont.size_ == FONT_SIZE_DECREASE)
409                         decSize();
410                 else
411                         size_ = newfont.size_;
412         }
413
414         if (newfont.style_ != FONT_STYLE_IGNORE) {
415                         style_ = newfont.style_;
416         }
417
418         setEmph(setMisc(newfont.emph_, emph_));
419         setUnderbar(setMisc(newfont.underbar_, underbar_));
420         setStrikeout(setMisc(newfont.strikeout_, strikeout_));
421         setXout(setMisc(newfont.xout_, xout_));
422         setUuline(setMisc(newfont.uuline_, uuline_));
423         setUwave(setMisc(newfont.uwave_, uwave_));
424         setNoun(setMisc(newfont.noun_, noun_));
425         setNumber(setMisc(newfont.number_, number_));
426         setNoSpellcheck(setMisc(newfont.nospellcheck_, nospellcheck_));
427
428         if (newfont.color_ == color_ && toggleall)
429                 setColor(Color_inherit); // toggle 'back'
430         else if (newfont.color_ != Color_ignore)
431                 setColor(newfont.color_);
432
433         if (newfont.background_ == background_ && toggleall)
434                 setBackground(Color_inherit); // toggle 'back'
435         else if (newfont.background_ != Color_ignore)
436                 setBackground(newfont.background_);
437 }
438
439 /// Is font resolved?
440 bool FontInfo::resolved() const
441 {
442         return (family_ != INHERIT_FAMILY && series_ != INHERIT_SERIES
443                 && shape_ != INHERIT_SHAPE && size_ != FONT_SIZE_INHERIT
444                 && style_ != FONT_STYLE_INHERIT
445                 && emph_ != FONT_INHERIT && underbar_ != FONT_INHERIT
446                 && uuline_ != FONT_INHERIT && uwave_ != FONT_INHERIT
447                 && strikeout_ != FONT_INHERIT && xout_ != FONT_INHERIT
448                 && noun_ != FONT_INHERIT && color_ != Color_inherit
449                 && background_ != Color_inherit && nospellcheck_ != FONT_INHERIT);
450 }
451
452
453 Color FontInfo::realColor() const
454 {
455         if (paint_color_ != Color_none)
456                 return paint_color_;
457         if (color_ == Color_none)
458                 return Color_foreground;
459         return color_;
460 }
461
462
463 namespace {
464
465 void appendSep(string & s1, string const & s2)
466 {
467         if (s2.empty())
468                 return;
469         s1 += s1.empty() ? "" : "\n";
470         s1 += s2;
471 }
472
473
474 string makeCSSTag(string const & key, string const & val)
475 {
476         return key + ": " + val + ";";
477 }
478
479
480 string getFamilyCSS(FontFamily const & f)
481 {
482         switch (f) {
483         case ROMAN_FAMILY:
484                 return "serif";
485         case SANS_FAMILY:
486                 return "sans-serif";
487         case TYPEWRITER_FAMILY:
488                 return "monospace";
489         case SYMBOL_FAMILY:
490         case CMR_FAMILY:
491         case CMSY_FAMILY:
492         case CMM_FAMILY:
493         case CMEX_FAMILY:
494         case MSA_FAMILY:
495         case MSB_FAMILY:
496         case EUFRAK_FAMILY:
497         case RSFS_FAMILY:
498         case STMARY_FAMILY:
499         case WASY_FAMILY:
500         case ESINT_FAMILY:
501         case INHERIT_FAMILY:
502         case IGNORE_FAMILY:
503                 break;
504         }
505         return "";
506 }
507
508
509 string getSeriesCSS(FontSeries const & s)
510 {
511         switch (s) {
512         case MEDIUM_SERIES:
513                 return "normal";
514         case BOLD_SERIES:
515                 return "bold";
516         case INHERIT_SERIES:
517         case IGNORE_SERIES:
518                 break;
519         }
520         return "";
521 }
522
523
524 string getShapeCSS(FontShape const & s)
525 {
526         string fs = "normal";
527         string fv = "normal";
528         switch (s) {
529         case UP_SHAPE: break;
530         case ITALIC_SHAPE: fs = "italic"; break;
531         case SLANTED_SHAPE: fs = "oblique"; break;
532         case SMALLCAPS_SHAPE: fv = "small-caps"; break;
533         case IGNORE_SHAPE:
534         case INHERIT_SHAPE:
535                 fs = ""; fv = ""; break;
536         }
537         string retval;
538         if (!fs.empty())
539                 appendSep(retval, makeCSSTag("font-style", fs));
540         if (!fv.empty())
541                 appendSep(retval, makeCSSTag("font-variant", fv));
542         return retval;
543 }
544
545
546 string getSizeCSS(FontSize const & s)
547 {
548         switch (s) {
549         case FONT_SIZE_TINY:
550                 return "xx-small";
551         case FONT_SIZE_SCRIPT:
552                 return "x-small";
553         case FONT_SIZE_FOOTNOTE:
554         case FONT_SIZE_SMALL:
555                 return "small";
556         case FONT_SIZE_NORMAL:
557                 return "medium";
558         case FONT_SIZE_LARGE:
559                 return "large";
560         case FONT_SIZE_LARGER:
561         case FONT_SIZE_LARGEST:
562                 return "x-large";
563         case FONT_SIZE_HUGE:
564         case FONT_SIZE_HUGER:
565                 return "xx-large";
566         case FONT_SIZE_INCREASE:
567                 return "larger";
568         case FONT_SIZE_DECREASE:
569                 return "smaller";
570         case FONT_SIZE_IGNORE:
571         case FONT_SIZE_INHERIT:
572                 break;
573         }
574         return "";
575 }
576
577 } // namespace
578
579
580 // FIXME This does not yet handle color
581 docstring FontInfo::asCSS() const
582 {
583         string retval;
584         string tmp = getFamilyCSS(family_);
585         if (!tmp.empty())
586                 appendSep(retval, makeCSSTag("font-family", tmp));
587         tmp = getSeriesCSS(series_);
588         if (!tmp.empty())
589                 appendSep(retval, makeCSSTag("font-weight", tmp));
590         appendSep(retval, getShapeCSS(shape_));
591         tmp = getSizeCSS(size_);
592         if (!tmp.empty())
593                 appendSep(retval, makeCSSTag("font-size", tmp));
594         return from_ascii(retval);
595 }
596
597
598 docstring const FontInfo::stateText(bool const terse) const
599 {
600         odocstringstream os;
601         if (family() != INHERIT_FAMILY && (!terse || family() != IGNORE_FAMILY))
602                 os << _(GUIFamilyNames[family()]) << ", ";
603         if (series() != INHERIT_SERIES && (!terse || series() != IGNORE_SERIES))
604                 os << _(GUISeriesNames[series()]) << ", ";
605         if (shape() != INHERIT_SHAPE && (!terse || shape() != IGNORE_SHAPE))
606                 os << _(GUIShapeNames[shape()]) << ", ";
607         if (size() != FONT_SIZE_INHERIT && (!terse || size() != FONT_SIZE_IGNORE))
608                 os << _(GUISizeNames[size()]) << ", ";
609         // FIXME: shall style be handled there? Probably not.
610         if (color() != Color_inherit && (!terse || color() != Color_ignore))
611                 os << lcolor.getGUIName(color()) << ", ";
612         // FIXME: uncomment this when we support background.
613         //if (background() != Color_inherit)
614         //      os << lcolor.getGUIName(background()) << ", ";
615         if (emph() != FONT_INHERIT && (!terse || emph() != FONT_IGNORE))
616                 os << bformat(_("Emphasis %1$s, "),
617                               _(GUIMiscNames[emph()]));
618         if (underbar() != FONT_INHERIT && (!terse || underbar() == FONT_ON))
619                 os << bformat(_("Underline %1$s, "),
620                               _(GUIMiscNames[underbar()]));
621         if (uuline() != FONT_INHERIT && (!terse || uuline() == FONT_ON))
622                 os << bformat(_("Double underline %1$s, "),
623                               _(GUIMiscNames[uuline()]));
624         if (uwave() != FONT_INHERIT && (!terse || uwave() == FONT_ON))
625                 os << bformat(_("Wavy underline %1$s, "),
626                               _(GUIMiscNames[uwave()]));
627         if (strikeout() != FONT_INHERIT && (!terse || strikeout() == FONT_ON))
628                 os << bformat(_("Strike out %1$s, "),
629                               _(GUIMiscNames[strikeout()]));
630         if (xout() != FONT_INHERIT && (!terse || xout() == FONT_ON))
631                 os << bformat(_("Cross out %1$s, "),
632                               _(GUIMiscNames[xout()]));
633         if (noun() != FONT_INHERIT && (!terse || noun() != FONT_IGNORE))
634                 os << bformat(_("Noun %1$s, "),
635                               _(GUIMiscNames[noun()]));
636         if (*this == inherit_font)
637                 os << _("Default") << ", ";
638
639         return os.str();
640 }
641
642
643 // Set family according to lyx format string
644 void setLyXFamily(string const & fam, FontInfo & f)
645 {
646         string const s = ascii_lowercase(fam);
647
648         int i = 0;
649         while (LyXFamilyNames[i] != s &&
650                LyXFamilyNames[i] != string("error"))
651                 ++i;
652         if (s == LyXFamilyNames[i])
653                 f.setFamily(FontFamily(i));
654         else
655                 LYXERR0("Unknown family `" << s << '\'');
656 }
657
658
659 // Set series according to lyx format string
660 void setLyXSeries(string const & ser, FontInfo & f)
661 {
662         string const s = ascii_lowercase(ser);
663
664         int i = 0;
665         while (LyXSeriesNames[i] != s &&
666                LyXSeriesNames[i] != string("error")) ++i;
667         if (s == LyXSeriesNames[i]) {
668                 f.setSeries(FontSeries(i));
669         } else
670                 LYXERR0("Unknown series `" << s << '\'');
671 }
672
673
674 // Set shape according to lyx format string
675 void setLyXShape(string const & sha, FontInfo & f)
676 {
677         string const s = ascii_lowercase(sha);
678
679         int i = 0;
680         while (LyXShapeNames[i] != s && LyXShapeNames[i] != string("error"))
681                         ++i;
682         if (s == LyXShapeNames[i])
683                 f.setShape(FontShape(i));
684         else
685                 LYXERR0("Unknown shape `" << s << '\'');
686 }
687
688
689 // Set size according to lyx format string
690 void setLyXSize(string const & siz, FontInfo & f)
691 {
692         string const s = ascii_lowercase(siz);
693         int i = 0;
694         while (LyXSizeNames[i] != s && LyXSizeNames[i] != string("error"))
695                 ++i;
696         if (s == LyXSizeNames[i]) {
697                 f.setSize(FontSize(i));
698         } else
699                 LYXERR0("Unknown size `" << s << '\'');
700 }
701
702
703 // Set size according to lyx format string
704 FontState setLyXMisc(string const & siz)
705 {
706         string const s = ascii_lowercase(siz);
707         int i = 0;
708         while (LyXMiscNames[i] != s &&
709                LyXMiscNames[i] != string("error")) ++i;
710         if (s == LyXMiscNames[i])
711                 return FontState(i);
712         LYXERR0("Unknown misc flag `" << s << '\'');
713         return FONT_OFF;
714 }
715
716
717 /// Sets color after LyX text format
718 void setLyXColor(string const & col, FontInfo & f)
719 {
720         f.setColor(lcolor.getFromLyXName(col));
721 }
722
723
724 // Read a font definition from given file in lyx format
725 // Used for layouts
726 FontInfo lyxRead(Lexer & lex, FontInfo const & fi)
727 {
728         FontInfo f = fi;
729         bool error = false;
730         bool finished = false;
731         while (!finished && lex.isOK() && !error) {
732                 lex.next();
733                 string const tok = ascii_lowercase(lex.getString());
734
735                 if (tok.empty()) {
736                         continue;
737                 } else if (tok == "endfont") {
738                         finished = true;
739                 } else if (tok == "family") {
740                         lex.next();
741                         string const ttok = lex.getString();
742                         setLyXFamily(ttok, f);
743                 } else if (tok == "series") {
744                         lex.next();
745                         string const ttok = lex.getString();
746                         setLyXSeries(ttok, f);
747                 } else if (tok == "shape") {
748                         lex.next();
749                         string const ttok = lex.getString();
750                         setLyXShape(ttok, f);
751                 } else if (tok == "size") {
752                         lex.next();
753                         string const ttok = lex.getString();
754                         setLyXSize(ttok, f);
755                 } else if (tok == "misc") {
756                         lex.next();
757                         string const ttok = ascii_lowercase(lex.getString());
758
759                         if (ttok == "no_bar") {
760                                 f.setUnderbar(FONT_OFF);
761                         } else if (ttok == "no_strikeout") {
762                                 f.setStrikeout(FONT_OFF);
763                         } else if (ttok == "no_xout") {
764                                 f.setXout(FONT_OFF);
765                         } else if (ttok == "no_uuline") {
766                                 f.setUuline(FONT_OFF);
767                         } else if (ttok == "no_uwave") {
768                                 f.setUwave(FONT_OFF);
769                         } else if (ttok == "no_emph") {
770                                 f.setEmph(FONT_OFF);
771                         } else if (ttok == "no_noun") {
772                                 f.setNoun(FONT_OFF);
773                         } else if (ttok == "emph") {
774                                 f.setEmph(FONT_ON);
775                         } else if (ttok == "underbar") {
776                                 f.setUnderbar(FONT_ON);
777                         } else if (ttok == "strikeout") {
778                                 f.setStrikeout(FONT_ON);
779                         } else if (ttok == "xout") {
780                                 f.setXout(FONT_ON);
781                         } else if (ttok == "uuline") {
782                                 f.setUuline(FONT_ON);
783                         } else if (ttok == "uwave") {
784                                 f.setUwave(FONT_ON);
785                         } else if (ttok == "noun") {
786                                 f.setNoun(FONT_ON);
787                         } else if (ttok == "nospellcheck") {
788                                 f.setNoSpellcheck(FONT_ON);
789                         } else if (ttok == "no_nospellcheck") {
790                                 f.setNoSpellcheck(FONT_OFF);
791                         } else {
792                                 lex.printError("Illegal misc type");
793                         }
794                 } else if (tok == "color") {
795                         lex.next();
796                         string const ttok = lex.getString();
797                         setLyXColor(ttok, f);
798                 } else {
799                         lex.printError("Unknown tag");
800                         error = true;
801                 }
802         }
803         return f;
804 }
805
806
807 void lyxWrite(ostream & os, FontInfo const & f, string const & start, int level)
808 {
809         string indent;
810         for (int i = 0; i < level; ++i)
811                 indent += '\t';
812         ostringstream oss;
813         if (f.family() != INHERIT_FAMILY)
814                 oss << indent << "\tFamily " << LyXFamilyNames[f.family()]
815                     << '\n';
816         if (f.series() != INHERIT_SERIES)
817                 oss << indent << "\tSeries " << LyXSeriesNames[f.series()]
818                     << '\n';
819         if (f.shape() != INHERIT_SHAPE)
820                 oss << indent << "\tShape " << LyXShapeNames[f.shape()]
821                     << '\n';
822         if (f.size() != FONT_SIZE_INHERIT)
823                 oss << indent << "\tSize " << LyXSizeNames[f.size()]
824                     << '\n';
825         //FIXME: shall style be handled here? Probably not.
826         if (f.underbar() == FONT_ON)
827                 oss << indent << "\tMisc Underbar\n";
828         else if (f.underbar() == FONT_OFF)
829                 oss << indent << "\tMisc No_Bar\n";
830         if (f.strikeout() == FONT_ON)
831                 oss << indent << "\tMisc Strikeout\n";
832         else if (f.strikeout() == FONT_OFF)
833                 oss << indent << "\tMisc No_Strikeout\n";
834         if (f.xout() == FONT_ON)
835                 oss << indent << "\tMisc Xout\n";
836         else if (f.xout() == FONT_OFF)
837                 oss << indent << "\tMisc No_Xout\n";
838         if (f.uuline() == FONT_ON)
839                 oss << indent << "\tMisc Uuline\n";
840         else if (f.uuline() == FONT_OFF)
841                 oss << indent << "\tMisc No_Uuline\n";
842         if (f.uwave() == FONT_ON)
843                 oss << indent << "\tMisc Uwave\n";
844         else if (f.uwave() == FONT_OFF)
845                 oss << indent << "\tMisc No_Uwave\n";
846         if (f.emph() == FONT_ON)
847                 oss << indent << "\tMisc Emph\n";
848         else if (f.emph() == FONT_OFF)
849                 oss << indent << "\tMisc No_Emph\n";
850         if (f.noun() == FONT_ON)
851                 oss << indent << "\tMisc Noun\n";
852         else if (f.noun() == FONT_OFF)
853                 oss << indent << "\tMisc No_Noun\n";
854         if (f.nospellcheck() == FONT_ON)
855                 oss << indent << "\tMisc NoSpellcheck\n";
856         else if (f.nospellcheck() == FONT_OFF)
857                 oss << indent << "\tMisc No_NoSpellcheck\n";
858         if (f.color() != Color_inherit && f.color() != Color_none)
859                 oss << indent << "\tColor " << lcolor.getLyXName(f.color())
860                     << '\n';
861         if (!oss.str().empty()) {
862                 os << indent << start << '\n'
863                    << oss.str()
864                    << indent << "EndFont\n";
865         }
866 }
867
868
869 } // namespace lyx