]> git.lyx.org Git - lyx.git/blob - src/FontInfo.cpp
In InsetText::fixParagraphsFont, differentiate the case of pass thru paragraphs for...
[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", "wasy",
36   "esint", "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 RSFS_FAMILY:
381                 case WASY_FAMILY:
382                 case ESINT_FAMILY:
383                 case INHERIT_FAMILY:
384                 case IGNORE_FAMILY:
385                         break;
386                 }
387                 return "";
388         }
389
390
391         string getSeriesCSS(FontSeries const & s)
392         {
393                 switch (s) {
394                 case MEDIUM_SERIES: 
395                         return "normal";
396                 case BOLD_SERIES: 
397                         return "bold";
398                 case INHERIT_SERIES:
399                 case IGNORE_SERIES:
400                   break;
401                 }
402                 return "";
403         }
404
405
406         string getShapeCSS(FontShape const & s)
407         {
408                 string fs = "normal";
409                 string fv = "normal";
410                 switch (s) {
411                 case UP_SHAPE: break;
412                 case ITALIC_SHAPE: fs = "italic"; break;
413                 case SLANTED_SHAPE: fs = "oblique"; break;
414                 case SMALLCAPS_SHAPE: fv = "small-caps"; break;
415                 case IGNORE_SHAPE: 
416                 case INHERIT_SHAPE:
417                         fs = ""; fv = ""; break;
418                 }
419                 string retval;
420                 if (!fs.empty())
421                         appendSep(retval, makeCSSTag("font-style", fs));
422                 if (!fv.empty())
423                         appendSep(retval, makeCSSTag("font-variant", fv));
424                 return retval;
425         }
426
427
428         string getSizeCSS(FontSize const & s)
429         {
430                 switch (s) {
431                 case FONT_SIZE_TINY: 
432                         return "xx-small";
433                 case FONT_SIZE_SCRIPT: 
434                         return "x-small";
435                 case FONT_SIZE_FOOTNOTE: 
436                 case FONT_SIZE_SMALL: 
437                         return "small";
438                 case FONT_SIZE_NORMAL: 
439                         return "medium";
440                 case FONT_SIZE_LARGE: 
441                         return "large";
442                 case FONT_SIZE_LARGER: 
443                 case FONT_SIZE_LARGEST: 
444                         return "x-large";
445                 case FONT_SIZE_HUGE: 
446                 case FONT_SIZE_HUGER: 
447                         return "xx-large";
448                 case FONT_SIZE_INCREASE: 
449                         return "larger";
450                 case FONT_SIZE_DECREASE: 
451                         return "smaller";
452                 case FONT_SIZE_IGNORE: 
453                 case FONT_SIZE_INHERIT: 
454                                 break;
455                 }       
456                 return "";
457         }
458         
459 } // namespace anonymous
460
461
462 // FIXME This does not yet handle color
463 docstring FontInfo::asCSS() const 
464 {
465         string retval;
466         string tmp = getFamilyCSS(family_);
467         if (!tmp.empty())
468                 appendSep(retval, makeCSSTag("font-family", tmp));
469         tmp = getSeriesCSS(series_);
470         if (!tmp.empty())
471                 appendSep(retval, makeCSSTag("font-weight", tmp));
472         appendSep(retval, getShapeCSS(shape_));
473         tmp = getSizeCSS(size_);
474         if (!tmp.empty())
475                 appendSep(retval, makeCSSTag("font-size", tmp));
476         return from_ascii(retval);      
477 }
478
479
480 // Set family according to lyx format string
481 void setLyXFamily(string const & fam, FontInfo & f)
482 {
483         string const s = ascii_lowercase(fam);
484
485         int i = 0;
486         while (LyXFamilyNames[i] != s &&
487                LyXFamilyNames[i] != string("error"))
488                 ++i;
489         if (s == LyXFamilyNames[i])
490                 f.setFamily(FontFamily(i));
491         else
492                 LYXERR0("Unknown family `" << s << '\'');
493 }
494
495
496 // Set series according to lyx format string
497 void setLyXSeries(string const & ser, FontInfo & f)
498 {
499         string const s = ascii_lowercase(ser);
500
501         int i = 0;
502         while (LyXSeriesNames[i] != s &&
503                LyXSeriesNames[i] != string("error")) ++i;
504         if (s == LyXSeriesNames[i]) {
505                 f.setSeries(FontSeries(i));
506         } else
507                 LYXERR0("Unknown series `" << s << '\'');
508 }
509
510
511 // Set shape according to lyx format string
512 void setLyXShape(string const & sha, FontInfo & f)
513 {
514         string const s = ascii_lowercase(sha);
515
516         int i = 0;
517         while (LyXShapeNames[i] != s && LyXShapeNames[i] != string("error"))
518                         ++i;
519         if (s == LyXShapeNames[i])
520                 f.setShape(FontShape(i));
521         else
522                 LYXERR0("Unknown shape `" << s << '\'');
523 }
524
525
526 // Set size according to lyx format string
527 void setLyXSize(string const & siz, FontInfo & f)
528 {
529         string const s = ascii_lowercase(siz);
530         int i = 0;
531         while (LyXSizeNames[i] != s && LyXSizeNames[i] != string("error"))
532                 ++i;
533         if (s == LyXSizeNames[i]) {
534                 f.setSize(FontSize(i));
535         } else
536                 LYXERR0("Unknown size `" << s << '\'');
537 }
538
539
540 // Set size according to lyx format string
541 FontState setLyXMisc(string const & siz)
542 {
543         string const s = ascii_lowercase(siz);
544         int i = 0;
545         while (LyXMiscNames[i] != s &&
546                LyXMiscNames[i] != string("error")) ++i;
547         if (s == LyXMiscNames[i])
548                 return FontState(i);
549         LYXERR0("Unknown misc flag `" << s << '\'');
550         return FONT_OFF;
551 }
552
553
554 /// Sets color after LyX text format
555 void setLyXColor(string const & col, FontInfo & f)
556 {
557         f.setColor(lcolor.getFromLyXName(col));
558 }
559
560
561 // Read a font definition from given file in lyx format
562 // Used for layouts
563 FontInfo lyxRead(Lexer & lex, FontInfo const & fi)
564 {
565         FontInfo f = fi;
566         bool error = false;
567         bool finished = false;
568         while (!finished && lex.isOK() && !error) {
569                 lex.next();
570                 string const tok = ascii_lowercase(lex.getString());
571
572                 if (tok.empty()) {
573                         continue;
574                 } else if (tok == "endfont") {
575                         finished = true;
576                 } else if (tok == "family") {
577                         lex.next();
578                         string const ttok = lex.getString();
579                         setLyXFamily(ttok, f);
580                 } else if (tok == "series") {
581                         lex.next();
582                         string const ttok = lex.getString();
583                         setLyXSeries(ttok, f);
584                 } else if (tok == "shape") {
585                         lex.next();
586                         string const ttok = lex.getString();
587                         setLyXShape(ttok, f);
588                 } else if (tok == "size") {
589                         lex.next();
590                         string const ttok = lex.getString();
591                         setLyXSize(ttok, f);
592                 } else if (tok == "misc") {
593                         lex.next();
594                         string const ttok = ascii_lowercase(lex.getString());
595
596                         if (ttok == "no_bar") {
597                                 f.setUnderbar(FONT_OFF);
598                         } else if (ttok == "no_strikeout") {
599                                 f.setStrikeout(FONT_OFF);
600                         } else if (ttok == "no_uuline") {
601                                 f.setUuline(FONT_OFF);
602                         } else if (ttok == "no_uwave") {
603                                 f.setUwave(FONT_OFF);
604                         } else if (ttok == "no_emph") {
605                                 f.setEmph(FONT_OFF);
606                         } else if (ttok == "no_noun") {
607                                 f.setNoun(FONT_OFF);
608                         } else if (ttok == "emph") {
609                                 f.setEmph(FONT_ON);
610                         } else if (ttok == "underbar") {
611                                 f.setUnderbar(FONT_ON);
612                         } else if (ttok == "strikeout") {
613                                 f.setStrikeout(FONT_ON);
614                         } else if (ttok == "uuline") {
615                                 f.setUuline(FONT_ON);
616                         } else if (ttok == "uwave") {
617                                 f.setUwave(FONT_ON);
618                         } else if (ttok == "noun") {
619                                 f.setNoun(FONT_ON);
620                         } else {
621                                 lex.printError("Illegal misc type");
622                         }
623                 } else if (tok == "color") {
624                         lex.next();
625                         string const ttok = lex.getString();
626                         setLyXColor(ttok, f);
627                 } else {
628                         lex.printError("Unknown tag");
629                         error = true;
630                 }
631         }
632         return f;
633 }
634
635
636 } // namespace lyx