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
12 * Full author contact details are available in file CREDITS.
21 #include "support/debug.h"
22 #include "support/docstring.h"
23 #include "support/lstrings.h"
26 using namespace lyx::support;
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", "rsfs", "stmry",
36 "wasy", "esint", "default", "error" };
38 char const * LyXSeriesNames[NUM_SERIES + 2 /* default & error */] =
39 { "medium", "bold", "default", "error" };
41 char const * LyXShapeNames[NUM_SHAPE + 2 /* default & error */] =
42 { "up", "italic", "slanted", "smallcaps", "default", "error" };
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" };
49 char const * LyXMiscNames[5] =
50 { "off", "on", "toggle", "default", "error" };
53 FontInfo const sane_font(
68 FontInfo const inherit_font(
83 FontInfo const ignore_font(
105 /// Decreases font size_ by one
106 FontInfo & FontInfo::decSize()
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");
122 case FONT_SIZE_DECREASE:
123 LYXERR0("Can't FontInfo::decSize on FONT_SIZE_DECREASE");
125 case FONT_SIZE_INHERIT:
126 LYXERR0("Can't FontInfo::decSize on FONT_SIZE_INHERIT");
128 case FONT_SIZE_IGNORE:
129 LYXERR0("Can't FontInfo::decSize on FONT_SIZE_IGNORE");
136 /// Increases font size_ by one
137 FontInfo & FontInfo::incSize()
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");
153 case FONT_SIZE_DECREASE:
154 LYXERR0("Can't FontInfo::incSize on FONT_SIZE_DECREASE");
156 case FONT_SIZE_INHERIT:
157 LYXERR0("Can't FontInfo::incSize on FONT_SIZE_INHERIT");
159 case FONT_SIZE_IGNORE:
160 LYXERR0("Can't FontInfo::incSize on FONT_SIZE_IGNORE");
167 /// Reduce font to fall back to template where possible
168 void FontInfo::reduce(FontInfo const & tmplt)
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;
197 /// Realize font from a template
198 FontInfo & FontInfo::realize(FontInfo const & tmplt)
200 if ((*this) == inherit_font) {
205 if (family_ == INHERIT_FAMILY)
206 family_ = tmplt.family_;
208 if (series_ == INHERIT_SERIES)
209 series_ = tmplt.series_;
211 if (shape_ == INHERIT_SHAPE)
212 shape_ = tmplt.shape_;
214 if (size_ == FONT_SIZE_INHERIT)
217 if (emph_ == FONT_INHERIT)
220 if (underbar_ == FONT_INHERIT)
221 underbar_ = tmplt.underbar_;
223 if (strikeout_ == FONT_INHERIT)
224 strikeout_ = tmplt.strikeout_;
226 if (uuline_ == FONT_INHERIT)
227 uuline_ = tmplt.uuline_;
229 if (uwave_ == FONT_INHERIT)
230 uwave_ = tmplt.uwave_;
232 if (noun_ == FONT_INHERIT)
235 if (color_ == Color_inherit)
236 color_ = tmplt.color_;
238 if (background_ == Color_inherit)
239 background_ = tmplt.background_;
245 /// Updates a misc setting according to request
246 static FontState setMisc(FontState newfont,
249 if (newfont == FONT_TOGGLE) {
252 else if (org == FONT_OFF)
255 LYXERR0("Font::setMisc: Need state"
256 " FONT_ON or FONT_OFF to toggle. Setting to FONT_ON");
259 } else if (newfont == FONT_IGNORE)
265 /// Updates font settings according to request
266 void FontInfo::update(FontInfo const & newfont, bool toggleall)
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
274 // "Old" behaviour: "Setting" bold will toggle bold on/off.
275 switch (newfont.series_) {
278 if (series_ == BOLD_SERIES && toggleall)
279 setSeries(MEDIUM_SERIES);
281 setSeries(BOLD_SERIES);
285 setSeries(newfont.series_);
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
297 if (newfont.size_ != FONT_SIZE_IGNORE) {
298 if (newfont.size_ == FONT_SIZE_INCREASE)
300 else if (newfont.size_ == FONT_SIZE_DECREASE)
303 size_ = newfont.size_;
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_));
314 if (newfont.color_ == color_ && toggleall)
315 setColor(Color_inherit); // toggle 'back'
316 else if (newfont.color_ != Color_ignore)
317 setColor(newfont.color_);
319 if (newfont.background_ == background_ && toggleall)
320 setBackground(Color_inherit); // toggle 'back'
321 else if (newfont.background_ != Color_ignore)
322 setBackground(newfont.background_);
325 /// Is font resolved?
326 bool FontInfo::resolved() const
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);
338 Color FontInfo::realColor() const
340 if (paint_color_ != Color_none)
342 if (color_ == Color_none)
343 return Color_foreground;
350 void appendSep(string & s1, string const & s2)
354 s1 += s1.empty() ? "" : "\n";
359 string makeCSSTag(string const & key, string const & val)
361 return key + ": " + val + ";";
365 string getFamilyCSS(FontFamily const & f)
372 case TYPEWRITER_FAMILY:
394 string getSeriesCSS(FontSeries const & s)
409 string getShapeCSS(FontShape const & s)
411 string fs = "normal";
412 string fv = "normal";
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;
420 fs = ""; fv = ""; break;
424 appendSep(retval, makeCSSTag("font-style", fs));
426 appendSep(retval, makeCSSTag("font-variant", fv));
431 string getSizeCSS(FontSize const & s)
436 case FONT_SIZE_SCRIPT:
438 case FONT_SIZE_FOOTNOTE:
439 case FONT_SIZE_SMALL:
441 case FONT_SIZE_NORMAL:
443 case FONT_SIZE_LARGE:
445 case FONT_SIZE_LARGER:
446 case FONT_SIZE_LARGEST:
449 case FONT_SIZE_HUGER:
451 case FONT_SIZE_INCREASE:
453 case FONT_SIZE_DECREASE:
455 case FONT_SIZE_IGNORE:
456 case FONT_SIZE_INHERIT:
462 } // namespace anonymous
465 // FIXME This does not yet handle color
466 docstring FontInfo::asCSS() const
469 string tmp = getFamilyCSS(family_);
471 appendSep(retval, makeCSSTag("font-family", tmp));
472 tmp = getSeriesCSS(series_);
474 appendSep(retval, makeCSSTag("font-weight", tmp));
475 appendSep(retval, getShapeCSS(shape_));
476 tmp = getSizeCSS(size_);
478 appendSep(retval, makeCSSTag("font-size", tmp));
479 return from_ascii(retval);
483 // Set family according to lyx format string
484 void setLyXFamily(string const & fam, FontInfo & f)
486 string const s = ascii_lowercase(fam);
489 while (LyXFamilyNames[i] != s &&
490 LyXFamilyNames[i] != string("error"))
492 if (s == LyXFamilyNames[i])
493 f.setFamily(FontFamily(i));
495 LYXERR0("Unknown family `" << s << '\'');
499 // Set series according to lyx format string
500 void setLyXSeries(string const & ser, FontInfo & f)
502 string const s = ascii_lowercase(ser);
505 while (LyXSeriesNames[i] != s &&
506 LyXSeriesNames[i] != string("error")) ++i;
507 if (s == LyXSeriesNames[i]) {
508 f.setSeries(FontSeries(i));
510 LYXERR0("Unknown series `" << s << '\'');
514 // Set shape according to lyx format string
515 void setLyXShape(string const & sha, FontInfo & f)
517 string const s = ascii_lowercase(sha);
520 while (LyXShapeNames[i] != s && LyXShapeNames[i] != string("error"))
522 if (s == LyXShapeNames[i])
523 f.setShape(FontShape(i));
525 LYXERR0("Unknown shape `" << s << '\'');
529 // Set size according to lyx format string
530 void setLyXSize(string const & siz, FontInfo & f)
532 string const s = ascii_lowercase(siz);
534 while (LyXSizeNames[i] != s && LyXSizeNames[i] != string("error"))
536 if (s == LyXSizeNames[i]) {
537 f.setSize(FontSize(i));
539 LYXERR0("Unknown size `" << s << '\'');
543 // Set size according to lyx format string
544 FontState setLyXMisc(string const & siz)
546 string const s = ascii_lowercase(siz);
548 while (LyXMiscNames[i] != s &&
549 LyXMiscNames[i] != string("error")) ++i;
550 if (s == LyXMiscNames[i])
552 LYXERR0("Unknown misc flag `" << s << '\'');
557 /// Sets color after LyX text format
558 void setLyXColor(string const & col, FontInfo & f)
560 f.setColor(lcolor.getFromLyXName(col));
564 // Read a font definition from given file in lyx format
566 FontInfo lyxRead(Lexer & lex, FontInfo const & fi)
570 bool finished = false;
571 while (!finished && lex.isOK() && !error) {
573 string const tok = ascii_lowercase(lex.getString());
577 } else if (tok == "endfont") {
579 } else if (tok == "family") {
581 string const ttok = lex.getString();
582 setLyXFamily(ttok, f);
583 } else if (tok == "series") {
585 string const ttok = lex.getString();
586 setLyXSeries(ttok, f);
587 } else if (tok == "shape") {
589 string const ttok = lex.getString();
590 setLyXShape(ttok, f);
591 } else if (tok == "size") {
593 string const ttok = lex.getString();
595 } else if (tok == "misc") {
597 string const ttok = ascii_lowercase(lex.getString());
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") {
609 } else if (ttok == "no_noun") {
611 } else if (ttok == "emph") {
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") {
621 } else if (ttok == "noun") {
624 lex.printError("Illegal misc type");
626 } else if (tok == "color") {
628 string const ttok = lex.getString();
629 setLyXColor(ttok, f);
631 lex.printError("Unknown tag");