Not so soon, I guess, since that FIXME was from r6305.
[lyx.git] / src / FontInfo.cpp
blob7efe2c76112d1c76d04df8444a8157950b58d7e6
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.
6 * \author Lars Gullik Bjønnes
7 * \author Jean-Marc Lasgouttes
8 * \author Angus Leeming
9 * \author André Pönitz
10 * \author Dekel Tsur
12 * Full author contact details are available in file CREDITS.
15 #include <config.h>
17 #include "ColorSet.h"
18 #include "FontInfo.h"
19 #include "Lexer.h"
21 #include "support/debug.h"
22 #include "support/docstring.h"
23 #include "support/lstrings.h"
25 using namespace std;
26 using namespace lyx::support;
28 namespace lyx {
31 // Strings used to read and write .lyx format files
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" };
38 char const * LyXSeriesNames[4] =
39 { "medium", "bold", "default", "error" };
41 char const * LyXShapeNames[6] =
42 { "up", "italic", "slanted", "smallcaps", "default", "error" };
44 char const * LyXSizeNames[14] =
45 { "tiny", "scriptsize", "footnotesize", "small", "normal", "large",
46 "larger", "largest", "huge", "giant",
47 "increase", "decrease", "default", "error" };
49 char const * LyXMiscNames[5] =
50 { "off", "on", "toggle", "default", "error" };
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);
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);
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);
99 FontInfo::FontInfo()
101 *this = sane_font;
104 /// Decreases font size_ by one
105 FontInfo & FontInfo::decSize()
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;
131 return *this;
135 /// Increases font size_ by one
136 FontInfo & FontInfo::incSize()
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;
162 return *this;
166 /// Reduce font to fall back to template where possible
167 void FontInfo::reduce(FontInfo const & tmplt)
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;
196 /// Realize font from a template
197 FontInfo & FontInfo::realize(FontInfo const & tmplt)
199 if ((*this) == inherit_font) {
200 operator=(tmplt);
201 return *this;
204 if (family_ == INHERIT_FAMILY)
205 family_ = tmplt.family_;
207 if (series_ == INHERIT_SERIES)
208 series_ = tmplt.series_;
210 if (shape_ == INHERIT_SHAPE)
211 shape_ = tmplt.shape_;
213 if (size_ == FONT_SIZE_INHERIT)
214 size_ = tmplt.size_;
216 if (emph_ == FONT_INHERIT)
217 emph_ = tmplt.emph_;
219 if (underbar_ == FONT_INHERIT)
220 underbar_ = tmplt.underbar_;
222 if (strikeout_ == FONT_INHERIT)
223 strikeout_ = tmplt.strikeout_;
225 if (uuline_ == FONT_INHERIT)
226 uuline_ = tmplt.uuline_;
228 if (uwave_ == FONT_INHERIT)
229 uwave_ = tmplt.uwave_;
231 if (noun_ == FONT_INHERIT)
232 noun_ = tmplt.noun_;
234 if (color_ == Color_inherit)
235 color_ = tmplt.color_;
237 if (background_ == Color_inherit)
238 background_ = tmplt.background_;
240 return *this;
244 /// Updates a misc setting according to request
245 static FontState setMisc(FontState newfont,
246 FontState org)
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;
258 } else if (newfont == FONT_IGNORE)
259 return org;
260 else
261 return newfont;
264 /// Updates font settings according to request
265 void FontInfo::update(FontInfo const & newfont, bool toggleall)
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
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;
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
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_;
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_));
313 if (newfont.color_ == color_ && toggleall)
314 setColor(Color_inherit); // toggle 'back'
315 else if (newfont.color_ != Color_ignore)
316 setColor(newfont.color_);
318 if (newfont.background_ == background_ && toggleall)
319 setBackground(Color_inherit); // toggle 'back'
320 else if (newfont.background_ != Color_ignore)
321 setBackground(newfont.background_);
324 /// Is font resolved?
325 bool FontInfo::resolved() const
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);
337 Color FontInfo::realColor() const
339 if (paint_color_ != Color_none)
340 return paint_color_;
341 if (color_ == Color_none)
342 return Color_foreground;
343 return color_;
347 namespace {
349 void appendSep(string & s1, string const & s2) {
350 if (s2.empty())
351 return;
352 s1 += s1.empty() ? "" : "\n";
353 s1 += s2;
357 string makeCSSTag(string const & key, string const & val)
359 return key + ": " + val + ";";
363 string getFamilyCSS(FontFamily const & f)
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;
386 return "";
390 string getSeriesCSS(FontSeries const & s)
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;
401 return "";
405 string getShapeCSS(FontShape const & s)
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;
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;
427 string getSizeCSS(FontSize const & s)
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;
455 return "";
458 } // namespace anonymous
461 // FIXME This does not yet handle color
462 docstring FontInfo::asCSS() const
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);
479 // Set family according to lyx format string
480 void setLyXFamily(string const & fam, FontInfo & f)
482 string const s = ascii_lowercase(fam);
484 int i = 0;
485 while (LyXFamilyNames[i] != s &&
486 LyXFamilyNames[i] != string("error"))
487 ++i;
488 if (s == LyXFamilyNames[i])
489 f.setFamily(FontFamily(i));
490 else
491 LYXERR0("Unknown family `" << s << '\'');
495 // Set series according to lyx format string
496 void setLyXSeries(string const & ser, FontInfo & f)
498 string const s = ascii_lowercase(ser);
500 int i = 0;
501 while (LyXSeriesNames[i] != s &&
502 LyXSeriesNames[i] != string("error")) ++i;
503 if (s == LyXSeriesNames[i]) {
504 f.setSeries(FontSeries(i));
505 } else
506 LYXERR0("Unknown series `" << s << '\'');
510 // Set shape according to lyx format string
511 void setLyXShape(string const & sha, FontInfo & f)
513 string const s = ascii_lowercase(sha);
515 int i = 0;
516 while (LyXShapeNames[i] != s && LyXShapeNames[i] != string("error"))
517 ++i;
518 if (s == LyXShapeNames[i])
519 f.setShape(FontShape(i));
520 else
521 LYXERR0("Unknown shape `" << s << '\'');
525 // Set size according to lyx format string
526 void setLyXSize(string const & siz, FontInfo & f)
528 string const s = ascii_lowercase(siz);
529 int i = 0;
530 while (LyXSizeNames[i] != s && LyXSizeNames[i] != string("error"))
531 ++i;
532 if (s == LyXSizeNames[i]) {
533 f.setSize(FontSize(i));
534 } else
535 LYXERR0("Unknown size `" << s << '\'');
539 // Set size according to lyx format string
540 FontState setLyXMisc(string const & siz)
542 string const s = ascii_lowercase(siz);
543 int i = 0;
544 while (LyXMiscNames[i] != s &&
545 LyXMiscNames[i] != string("error")) ++i;
546 if (s == LyXMiscNames[i])
547 return FontState(i);
548 LYXERR0("Unknown misc flag `" << s << '\'');
549 return FONT_OFF;
553 /// Sets color after LyX text format
554 void setLyXColor(string const & col, FontInfo & f)
556 f.setColor(lcolor.getFromLyXName(col));
560 // Read a font definition from given file in lyx format
561 // Used for layouts
562 FontInfo lyxRead(Lexer & lex, FontInfo const & fi)
564 FontInfo f = fi;
565 bool error = false;
566 bool finished = false;
567 while (!finished && lex.isOK() && !error) {
568 lex.next();
569 string const tok = ascii_lowercase(lex.getString());
571 if (tok.empty()) {
572 continue;
573 } else if (tok == "endfont") {
574 finished = true;
575 } else if (tok == "family") {
576 lex.next();
577 string const ttok = lex.getString();
578 setLyXFamily(ttok, f);
579 } else if (tok == "series") {
580 lex.next();
581 string const ttok = lex.getString();
582 setLyXSeries(ttok, f);
583 } else if (tok == "shape") {
584 lex.next();
585 string const ttok = lex.getString();
586 setLyXShape(ttok, f);
587 } else if (tok == "size") {
588 lex.next();
589 string const ttok = lex.getString();
590 setLyXSize(ttok, f);
591 } else if (tok == "misc") {
592 lex.next();
593 string const ttok = ascii_lowercase(lex.getString());
595 if (ttok == "no_bar") {
596 f.setUnderbar(FONT_OFF);
597 } else if (ttok == "no_strikeout") {
598 f.setStrikeout(FONT_OFF);
599 } else if (ttok == "no_uuline") {
600 f.setUuline(FONT_OFF);
601 } else if (ttok == "no_uwave") {
602 f.setUwave(FONT_OFF);
603 } else if (ttok == "no_emph") {
604 f.setEmph(FONT_OFF);
605 } else if (ttok == "no_noun") {
606 f.setNoun(FONT_OFF);
607 } else if (ttok == "emph") {
608 f.setEmph(FONT_ON);
609 } else if (ttok == "underbar") {
610 f.setUnderbar(FONT_ON);
611 } else if (ttok == "strikeout") {
612 f.setStrikeout(FONT_ON);
613 } else if (ttok == "uuline") {
614 f.setUuline(FONT_ON);
615 } else if (ttok == "uwave") {
616 f.setUwave(FONT_ON);
617 } else if (ttok == "noun") {
618 f.setNoun(FONT_ON);
619 } else {
620 lex.printError("Illegal misc type");
622 } else if (tok == "color") {
623 lex.next();
624 string const ttok = lex.getString();
625 setLyXColor(ttok, f);
626 } else {
627 lex.printError("Unknown tag");
628 error = true;
631 return f;
635 } // namespace lyx