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