]> git.lyx.org Git - lyx.git/blob - src/FontInfo.cpp
8343df0751768d78650423881012d028d364fcdd
[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
21 #include "support/debug.h"
22 #include "support/docstring.h"
23 #include "support/lstrings.h"
24
25 using namespace std;
26 using namespace lyx::support;
27
28 namespace lyx {
29
30 //
31 // Strings used to read and write .lyx format files
32 //
33 char const * LyXFamilyNames[NUM_FAMILIES + 2 /* default & error */] =
34 { "roman", "sans", "typewriter", "symbol",
35   "cmr", "cmsy", "cmm", "cmex", "msa", "msb", "eufrak", "wasy", "esint",
36   "default", "error" };
37
38 char const * LyXSeriesNames[4] =
39 { "medium", "bold", "default", "error" };
40
41 char const * LyXShapeNames[6] =
42 { "up", "italic", "slanted", "smallcaps", "default", "error" };
43
44 char const * LyXSizeNames[14] =
45 { "tiny", "scriptsize", "footnotesize", "small", "normal", "large",
46   "larger", "largest", "huge", "giant",
47   "increase", "decrease", "default", "error" };
48
49 char const * LyXMiscNames[5] =
50 { "off", "on", "toggle", "default", "error" };
51
52
53 FontInfo const sane_font(
54         ROMAN_FAMILY,
55         MEDIUM_SERIES,
56         UP_SHAPE,
57         FONT_SIZE_NORMAL,
58         Color_none,
59         Color_background,
60         FONT_OFF,
61         FONT_OFF,
62         FONT_OFF,
63         FONT_OFF,
64         FONT_OFF,
65         FONT_OFF,
66         FONT_OFF);
67
68 FontInfo const inherit_font(
69         INHERIT_FAMILY,
70         INHERIT_SERIES,
71         INHERIT_SHAPE,
72         FONT_SIZE_INHERIT,
73         Color_inherit,
74         Color_inherit,
75         FONT_INHERIT,
76         FONT_INHERIT,
77         FONT_INHERIT,
78         FONT_INHERIT,
79         FONT_INHERIT,
80         FONT_INHERIT,
81         FONT_OFF);
82
83 FontInfo const ignore_font(
84         IGNORE_FAMILY,
85         IGNORE_SERIES,
86         IGNORE_SHAPE,
87         FONT_SIZE_IGNORE,
88         Color_ignore,
89         Color_ignore,
90         FONT_IGNORE,
91         FONT_IGNORE,
92         FONT_IGNORE,
93         FONT_IGNORE,
94         FONT_IGNORE,
95         FONT_IGNORE,
96         FONT_IGNORE);
97
98
99 FontInfo::FontInfo()
100 {
101         *this = sane_font;
102 }
103
104 /// Decreases font size_ by one
105 FontInfo & FontInfo::decSize()
106 {
107         switch (size_) {
108         case FONT_SIZE_HUGER:        size_ = FONT_SIZE_HUGE;     break;
109         case FONT_SIZE_HUGE:         size_ = FONT_SIZE_LARGEST;  break;
110         case FONT_SIZE_LARGEST:      size_ = FONT_SIZE_LARGER;   break;
111         case FONT_SIZE_LARGER:       size_ = FONT_SIZE_LARGE;    break;
112         case FONT_SIZE_LARGE:        size_ = FONT_SIZE_NORMAL;   break;
113         case FONT_SIZE_NORMAL:       size_ = FONT_SIZE_SMALL;    break;
114         case FONT_SIZE_SMALL:        size_ = FONT_SIZE_FOOTNOTE; break;
115         case FONT_SIZE_FOOTNOTE:     size_ = FONT_SIZE_SCRIPT;   break;
116         case FONT_SIZE_SCRIPT:       size_ = FONT_SIZE_TINY;     break;
117         case FONT_SIZE_TINY:         break;
118         case FONT_SIZE_INCREASE:
119                 LYXERR0("Can't FontInfo::decSize on FONT_SIZE_INCREASE");
120                 break;
121         case FONT_SIZE_DECREASE:
122                 LYXERR0("Can't FontInfo::decSize on FONT_SIZE_DECREASE");
123                 break;
124         case FONT_SIZE_INHERIT:
125                 LYXERR0("Can't FontInfo::decSize on FONT_SIZE_INHERIT");
126                 break;
127         case FONT_SIZE_IGNORE:
128                 LYXERR0("Can't FontInfo::decSize on FONT_SIZE_IGNORE");
129                 break;
130         }
131         return *this;
132 }
133
134
135 /// Increases font size_ by one
136 FontInfo & FontInfo::incSize()
137 {
138         switch (size_) {
139         case FONT_SIZE_HUGER:   break;
140         case FONT_SIZE_HUGE:         size_ = FONT_SIZE_HUGER;    break;
141         case FONT_SIZE_LARGEST:      size_ = FONT_SIZE_HUGE;     break;
142         case FONT_SIZE_LARGER:       size_ = FONT_SIZE_LARGEST;  break;
143         case FONT_SIZE_LARGE:        size_ = FONT_SIZE_LARGER;   break;
144         case FONT_SIZE_NORMAL:       size_ = FONT_SIZE_LARGE;    break;
145         case FONT_SIZE_SMALL:        size_ = FONT_SIZE_NORMAL;   break;
146         case FONT_SIZE_FOOTNOTE:     size_ = FONT_SIZE_SMALL;    break;
147         case FONT_SIZE_SCRIPT:       size_ = FONT_SIZE_FOOTNOTE; break;
148         case FONT_SIZE_TINY:         size_ = FONT_SIZE_SCRIPT;   break;
149         case FONT_SIZE_INCREASE:
150                 LYXERR0("Can't FontInfo::incSize on FONT_SIZE_INCREASE");
151                 break;
152         case FONT_SIZE_DECREASE:
153                 LYXERR0("Can't FontInfo::incSize on FONT_SIZE_DECREASE");
154                 break;
155         case FONT_SIZE_INHERIT:
156                 LYXERR0("Can't FontInfo::incSize on FONT_SIZE_INHERIT");
157                 break;
158         case FONT_SIZE_IGNORE:
159                 LYXERR0("Can't FontInfo::incSize on FONT_SIZE_IGNORE");
160                 break;
161         }
162         return *this;
163 }
164
165
166 /// Reduce font to fall back to template where possible
167 void FontInfo::reduce(FontInfo const & tmplt)
168 {
169         if (family_ == tmplt.family_)
170                 family_ = INHERIT_FAMILY;
171         if (series_ == tmplt.series_)
172                 series_ = INHERIT_SERIES;
173         if (shape_ == tmplt.shape_)
174                 shape_ = INHERIT_SHAPE;
175         if (size_ == tmplt.size_)
176                 size_ = FONT_SIZE_INHERIT;
177         if (emph_ == tmplt.emph_)
178                 emph_ = FONT_INHERIT;
179         if (underbar_ == tmplt.underbar_)
180                 underbar_ = FONT_INHERIT;
181         if (strikeout_ == tmplt.strikeout_)
182                 strikeout_ = FONT_INHERIT;
183         if (uuline_ == tmplt.uuline_)
184                 uuline_ = FONT_INHERIT;
185         if (uwave_ == tmplt.uwave_)
186                 uwave_ = FONT_INHERIT;
187         if (noun_ == tmplt.noun_)
188                 noun_ = FONT_INHERIT;
189         if (color_ == tmplt.color_)
190                 color_ = Color_inherit;
191         if (background_ == tmplt.background_)
192                 background_ = Color_inherit;
193 }
194
195
196 /// Realize font from a template
197 FontInfo & FontInfo::realize(FontInfo const & tmplt)
198 {
199         if ((*this) == inherit_font) {
200                 operator=(tmplt);
201                 return *this;
202         }
203
204         if (family_ == INHERIT_FAMILY)
205                 family_ = tmplt.family_;
206
207         if (series_ == INHERIT_SERIES)
208                 series_ = tmplt.series_;
209
210         if (shape_ == INHERIT_SHAPE)
211                 shape_ = tmplt.shape_;
212
213         if (size_ == FONT_SIZE_INHERIT)
214                 size_ = tmplt.size_;
215
216         if (emph_ == FONT_INHERIT)
217                 emph_ = tmplt.emph_;
218
219         if (underbar_ == FONT_INHERIT)
220                 underbar_ = tmplt.underbar_;
221
222         if (strikeout_ == FONT_INHERIT)
223                 strikeout_ = tmplt.strikeout_;
224
225         if (uuline_ == FONT_INHERIT)
226                 uuline_ = tmplt.uuline_;
227
228         if (uwave_ == FONT_INHERIT)
229                 uwave_ = tmplt.uwave_;
230
231         if (noun_ == FONT_INHERIT)
232                 noun_ = tmplt.noun_;
233
234         if (color_ == Color_inherit)
235                 color_ = tmplt.color_;
236
237         if (background_ == Color_inherit)
238                 background_ = tmplt.background_;
239
240         return *this;
241 }
242
243
244 /// Updates a misc setting according to request
245 static FontState setMisc(FontState newfont,
246         FontState org)
247 {
248         if (newfont == FONT_TOGGLE) {
249                 if (org == FONT_ON)
250                         return FONT_OFF;
251                 else if (org == FONT_OFF)
252                         return FONT_ON;
253                 else {
254                         LYXERR0("Font::setMisc: Need state"
255                                 " FONT_ON or FONT_OFF to toggle. Setting to FONT_ON");
256                         return FONT_ON;
257                 }
258         } else if (newfont == FONT_IGNORE)
259                 return org;
260         else
261                 return newfont;
262 }
263
264 /// Updates font settings according to request
265 void FontInfo::update(FontInfo const & newfont, bool toggleall)
266 {
267         if (newfont.family_ == family_ && toggleall)
268                 setFamily(INHERIT_FAMILY); // toggle 'back'
269         else if (newfont.family_ != IGNORE_FAMILY)
270                 setFamily(newfont.family_);
271         // else it's IGNORE_SHAPE
272
273         // "Old" behaviour: "Setting" bold will toggle bold on/off.
274         switch (newfont.series_) {
275         case BOLD_SERIES:
276                 // We toggle...
277                 if (series_ == BOLD_SERIES && toggleall)
278                         setSeries(MEDIUM_SERIES);
279                 else
280                         setSeries(BOLD_SERIES);
281                 break;
282         case MEDIUM_SERIES:
283         case INHERIT_SERIES:
284                 setSeries(newfont.series_);
285                 break;
286         case IGNORE_SERIES:
287                 break;
288         }
289
290         if (newfont.shape_ == shape_ && toggleall)
291                 shape_ = INHERIT_SHAPE; // toggle 'back'
292         else if (newfont.shape_ != IGNORE_SHAPE)
293                 shape_ = newfont.shape_;
294         // else it's IGNORE_SHAPE
295
296         if (newfont.size_ != FONT_SIZE_IGNORE) {
297                 if (newfont.size_ == FONT_SIZE_INCREASE)
298                         incSize();
299                 else if (newfont.size_ == FONT_SIZE_DECREASE)
300                         decSize();
301                 else
302                         size_ = newfont.size_;
303         }
304
305         setEmph(setMisc(newfont.emph_, emph_));
306         setUnderbar(setMisc(newfont.underbar_, underbar_));
307         setStrikeout(setMisc(newfont.strikeout_, strikeout_));
308         setUuline(setMisc(newfont.uuline_, uuline_));
309         setUwave(setMisc(newfont.uwave_, uwave_));
310         setNoun(setMisc(newfont.noun_, noun_));
311         setNumber(setMisc(newfont.number_, number_));
312
313         if (newfont.color_ == color_ && toggleall)
314                 setColor(Color_inherit); // toggle 'back'
315         else if (newfont.color_ != Color_ignore)
316                 setColor(newfont.color_);
317
318         if (newfont.background_ == background_ && toggleall)
319                 setBackground(Color_inherit); // toggle 'back'
320         else if (newfont.background_ != Color_ignore)
321                 setBackground(newfont.background_);
322 }
323
324 /// Is font resolved?
325 bool FontInfo::resolved() const
326 {
327         return (family_ != INHERIT_FAMILY && series_ != INHERIT_SERIES
328                 && shape_ != INHERIT_SHAPE && size_ != FONT_SIZE_INHERIT
329                 && emph_ != FONT_INHERIT && underbar_ != FONT_INHERIT
330                 && uuline_ != FONT_INHERIT && uwave_ != FONT_INHERIT
331                 && strikeout_ != FONT_INHERIT && noun_ != FONT_INHERIT
332                 && color_ != Color_inherit
333                 && background_ != Color_inherit);
334 }
335
336
337 Color FontInfo::realColor() const
338 {
339         if (paint_color_ != Color_none)
340                 return paint_color_;
341         if (color_ == Color_none)
342                 return Color_foreground;
343         return color_;
344 }
345
346
347 namespace {
348
349         void appendSep(string & s1, string const & s2) {
350                 if (s2.empty()) 
351                         return;
352                 s1 += s1.empty() ? "" : "\n";
353                 s1 += s2;
354         }
355
356
357         string makeCSSTag(string const & key, string const & val)
358         {
359                 return key + ": " + val + ";";
360         }
361
362
363         string getFamilyCSS(FontFamily const & f)
364         {
365                 switch (f) {
366                 case ROMAN_FAMILY: 
367                         return "serif";
368                 case SANS_FAMILY: 
369                         return "sans-serif";
370                 case TYPEWRITER_FAMILY: 
371                         return "monospace";
372                 case SYMBOL_FAMILY:
373                 case CMR_FAMILY:
374                 case CMSY_FAMILY:
375                 case CMM_FAMILY:
376                 case CMEX_FAMILY:
377                 case MSA_FAMILY:
378                 case MSB_FAMILY:
379                 case EUFRAK_FAMILY:
380                 case WASY_FAMILY:
381                 case ESINT_FAMILY:
382                 case INHERIT_FAMILY:
383                 case IGNORE_FAMILY:
384                         break;
385                 }
386                 return "";
387         }
388
389
390         string getSeriesCSS(FontSeries const & s)
391         {
392                 switch (s) {
393                 case MEDIUM_SERIES: 
394                         return "normal";
395                 case BOLD_SERIES: 
396                         return "bold";
397                 case INHERIT_SERIES:
398                 case IGNORE_SERIES:
399                   break;
400                 }
401                 return "";
402         }
403
404
405         string getShapeCSS(FontShape const & s)
406         {
407                 string fs = "normal";
408                 string fv = "normal";
409                 switch (s) {
410                 case UP_SHAPE: break;
411                 case ITALIC_SHAPE: fs = "italic"; break;
412                 case SLANTED_SHAPE: fs = "oblique"; break;
413                 case SMALLCAPS_SHAPE: fv = "small-caps"; break;
414                 case IGNORE_SHAPE: 
415                 case INHERIT_SHAPE:
416                         fs = ""; fv = ""; break;
417                 }
418                 string retval;
419                 if (!fs.empty())
420                         appendSep(retval, makeCSSTag("font-style", fs));
421                 if (!fv.empty())
422                         appendSep(retval, makeCSSTag("font-variant", fv));
423                 return retval;
424         }
425
426
427         string getSizeCSS(FontSize const & s)
428         {
429                 switch (s) {
430                 case FONT_SIZE_TINY: 
431                         return "xx-small";
432                 case FONT_SIZE_SCRIPT: 
433                         return "x-small";
434                 case FONT_SIZE_FOOTNOTE: 
435                 case FONT_SIZE_SMALL: 
436                         return "small";
437                 case FONT_SIZE_NORMAL: 
438                         return "medium";
439                 case FONT_SIZE_LARGE: 
440                         return "large";
441                 case FONT_SIZE_LARGER: 
442                 case FONT_SIZE_LARGEST: 
443                         return "x-large";
444                 case FONT_SIZE_HUGE: 
445                 case FONT_SIZE_HUGER: 
446                         return "xx-large";
447                 case FONT_SIZE_INCREASE: 
448                         return "larger";
449                 case FONT_SIZE_DECREASE: 
450                         return "smaller";
451                 case FONT_SIZE_IGNORE: 
452                 case FONT_SIZE_INHERIT: 
453                                 break;
454                 }       
455                 return "";
456         }
457         
458 } // namespace anonymous
459
460
461 // FIXME This does not yet handle color
462 docstring FontInfo::asCSS() const 
463 {
464         string retval;
465         string tmp = getFamilyCSS(family_);
466         if (!tmp.empty())
467                 appendSep(retval, makeCSSTag("font-family", tmp));
468         tmp = getSeriesCSS(series_);
469         if (!tmp.empty())
470                 appendSep(retval, makeCSSTag("font-weight", tmp));
471         appendSep(retval, getShapeCSS(shape_));
472         tmp = getSizeCSS(size_);
473         if (!tmp.empty())
474                 appendSep(retval, makeCSSTag("font-size", tmp));
475         return from_ascii(retval);      
476 }
477
478 // Set family according to lyx format string
479 void setLyXFamily(string const & fam, FontInfo & f)
480 {
481         string const s = ascii_lowercase(fam);
482
483         int i = 0;
484         while (LyXFamilyNames[i] != s &&
485                LyXFamilyNames[i] != string("error"))
486                 ++i;
487         if (s == LyXFamilyNames[i])
488                 f.setFamily(FontFamily(i));
489         else
490                 LYXERR0("Unknown family `" << s << '\'');
491 }
492
493
494 // Set series according to lyx format string
495 void setLyXSeries(string const & ser, FontInfo & f)
496 {
497         string const s = ascii_lowercase(ser);
498
499         int i = 0;
500         while (LyXSeriesNames[i] != s &&
501                LyXSeriesNames[i] != string("error")) ++i;
502         if (s == LyXSeriesNames[i]) {
503                 f.setSeries(FontSeries(i));
504         } else
505                 LYXERR0("Unknown series `" << s << '\'');
506 }
507
508
509 // Set shape according to lyx format string
510 void setLyXShape(string const & sha, FontInfo & f)
511 {
512         string const s = ascii_lowercase(sha);
513
514         int i = 0;
515         while (LyXShapeNames[i] != s && LyXShapeNames[i] != string("error"))
516                         ++i;
517         if (s == LyXShapeNames[i])
518                 f.setShape(FontShape(i));
519         else
520                 LYXERR0("Unknown shape `" << s << '\'');
521 }
522
523
524 // Set size according to lyx format string
525 void setLyXSize(string const & siz, FontInfo & f)
526 {
527         string const s = ascii_lowercase(siz);
528         int i = 0;
529         while (LyXSizeNames[i] != s && LyXSizeNames[i] != string("error"))
530                 ++i;
531         if (s == LyXSizeNames[i]) {
532                 f.setSize(FontSize(i));
533         } else
534                 LYXERR0("Unknown size `" << s << '\'');
535 }
536
537
538 // Set size according to lyx format string
539 FontState setLyXMisc(string const & siz)
540 {
541         string const s = ascii_lowercase(siz);
542         int i = 0;
543         while (LyXMiscNames[i] != s &&
544                LyXMiscNames[i] != string("error")) ++i;
545         if (s == LyXMiscNames[i])
546                 return FontState(i);
547         LYXERR0("Unknown misc flag `" << s << '\'');
548         return FONT_OFF;
549 }
550
551
552 /// Sets color after LyX text format
553 void setLyXColor(string const & col, FontInfo & f)
554 {
555         f.setColor(lcolor.getFromLyXName(col));
556 }
557
558
559 // Read a font definition from given file in lyx format
560 // Used for layouts
561 FontInfo lyxRead(Lexer & lex, FontInfo const & fi)
562 {
563         FontInfo f = fi;
564         bool error = false;
565         bool finished = false;
566         while (!finished && lex.isOK() && !error) {
567                 lex.next();
568                 string const tok = ascii_lowercase(lex.getString());
569
570                 if (tok.empty()) {
571                         continue;
572                 } else if (tok == "endfont") {
573                         finished = true;
574                 } else if (tok == "family") {
575                         lex.next();
576                         string const ttok = lex.getString();
577                         setLyXFamily(ttok, f);
578                 } else if (tok == "series") {
579                         lex.next();
580                         string const ttok = lex.getString();
581                         setLyXSeries(ttok, f);
582                 } else if (tok == "shape") {
583                         lex.next();
584                         string const ttok = lex.getString();
585                         setLyXShape(ttok, f);
586                 } else if (tok == "size") {
587                         lex.next();
588                         string const ttok = lex.getString();
589                         setLyXSize(ttok, f);
590                 } else if (tok == "misc") {
591                         lex.next();
592                         string const ttok = ascii_lowercase(lex.getString());
593
594                         if (ttok == "no_bar") {
595                                 f.setUnderbar(FONT_OFF);
596                         } else if (ttok == "no_strikeout") {
597                                 f.setStrikeout(FONT_OFF);
598                         } else if (ttok == "no_uuline") {
599                                 f.setUuline(FONT_OFF);
600                         } else if (ttok == "no_uwave") {
601                                 f.setUwave(FONT_OFF);
602                         } else if (ttok == "no_emph") {
603                                 f.setEmph(FONT_OFF);
604                         } else if (ttok == "no_noun") {
605                                 f.setNoun(FONT_OFF);
606                         } else if (ttok == "emph") {
607                                 f.setEmph(FONT_ON);
608                         } else if (ttok == "underbar") {
609                                 f.setUnderbar(FONT_ON);
610                         } else if (ttok == "strikeout") {
611                                 f.setStrikeout(FONT_ON);
612                         } else if (ttok == "uuline") {
613                                 f.setUuline(FONT_ON);
614                         } else if (ttok == "uwave") {
615                                 f.setUwave(FONT_ON);
616                         } else if (ttok == "noun") {
617                                 f.setNoun(FONT_ON);
618                         } else {
619                                 lex.printError("Illegal misc type");
620                         }
621                 } else if (tok == "color") {
622                         lex.next();
623                         string const ttok = lex.getString();
624                         setLyXColor(ttok, f);
625                 } else {
626                         lex.printError("Unknown tag");
627                         error = true;
628                 }
629         }
630         return f;
631 }
632
633
634 } // namespace lyx