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", "wasy",
36 "esint", "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(
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:
393 string getSeriesCSS(FontSeries const & s)
408 string getShapeCSS(FontShape const & s)
410 string fs = "normal";
411 string fv = "normal";
413 case UP_SHAPE: break;
414 case ITALIC_SHAPE: fs = "italic"; break;
415 case SLANTED_SHAPE: fs = "oblique"; break;
416 case SMALLCAPS_SHAPE: fv = "small-caps"; break;
419 fs = ""; fv = ""; break;
423 appendSep(retval, makeCSSTag("font-style", fs));
425 appendSep(retval, makeCSSTag("font-variant", fv));
430 string getSizeCSS(FontSize const & s)
435 case FONT_SIZE_SCRIPT:
437 case FONT_SIZE_FOOTNOTE:
438 case FONT_SIZE_SMALL:
440 case FONT_SIZE_NORMAL:
442 case FONT_SIZE_LARGE:
444 case FONT_SIZE_LARGER:
445 case FONT_SIZE_LARGEST:
448 case FONT_SIZE_HUGER:
450 case FONT_SIZE_INCREASE:
452 case FONT_SIZE_DECREASE:
454 case FONT_SIZE_IGNORE:
455 case FONT_SIZE_INHERIT:
461 } // namespace anonymous
464 // FIXME This does not yet handle color
465 docstring FontInfo::asCSS() const
468 string tmp = getFamilyCSS(family_);
470 appendSep(retval, makeCSSTag("font-family", tmp));
471 tmp = getSeriesCSS(series_);
473 appendSep(retval, makeCSSTag("font-weight", tmp));
474 appendSep(retval, getShapeCSS(shape_));
475 tmp = getSizeCSS(size_);
477 appendSep(retval, makeCSSTag("font-size", tmp));
478 return from_ascii(retval);
482 // Set family according to lyx format string
483 void setLyXFamily(string const & fam, FontInfo & f)
485 string const s = ascii_lowercase(fam);
488 while (LyXFamilyNames[i] != s &&
489 LyXFamilyNames[i] != string("error"))
491 if (s == LyXFamilyNames[i])
492 f.setFamily(FontFamily(i));
494 LYXERR0("Unknown family `" << s << '\'');
498 // Set series according to lyx format string
499 void setLyXSeries(string const & ser, FontInfo & f)
501 string const s = ascii_lowercase(ser);
504 while (LyXSeriesNames[i] != s &&
505 LyXSeriesNames[i] != string("error")) ++i;
506 if (s == LyXSeriesNames[i]) {
507 f.setSeries(FontSeries(i));
509 LYXERR0("Unknown series `" << s << '\'');
513 // Set shape according to lyx format string
514 void setLyXShape(string const & sha, FontInfo & f)
516 string const s = ascii_lowercase(sha);
519 while (LyXShapeNames[i] != s && LyXShapeNames[i] != string("error"))
521 if (s == LyXShapeNames[i])
522 f.setShape(FontShape(i));
524 LYXERR0("Unknown shape `" << s << '\'');
528 // Set size according to lyx format string
529 void setLyXSize(string const & siz, FontInfo & f)
531 string const s = ascii_lowercase(siz);
533 while (LyXSizeNames[i] != s && LyXSizeNames[i] != string("error"))
535 if (s == LyXSizeNames[i]) {
536 f.setSize(FontSize(i));
538 LYXERR0("Unknown size `" << s << '\'');
542 // Set size according to lyx format string
543 FontState setLyXMisc(string const & siz)
545 string const s = ascii_lowercase(siz);
547 while (LyXMiscNames[i] != s &&
548 LyXMiscNames[i] != string("error")) ++i;
549 if (s == LyXMiscNames[i])
551 LYXERR0("Unknown misc flag `" << s << '\'');
556 /// Sets color after LyX text format
557 void setLyXColor(string const & col, FontInfo & f)
559 f.setColor(lcolor.getFromLyXName(col));
563 // Read a font definition from given file in lyx format
565 FontInfo lyxRead(Lexer & lex, FontInfo const & fi)
569 bool finished = false;
570 while (!finished && lex.isOK() && !error) {
572 string const tok = ascii_lowercase(lex.getString());
576 } else if (tok == "endfont") {
578 } else if (tok == "family") {
580 string const ttok = lex.getString();
581 setLyXFamily(ttok, f);
582 } else if (tok == "series") {
584 string const ttok = lex.getString();
585 setLyXSeries(ttok, f);
586 } else if (tok == "shape") {
588 string const ttok = lex.getString();
589 setLyXShape(ttok, f);
590 } else if (tok == "size") {
592 string const ttok = lex.getString();
594 } else if (tok == "misc") {
596 string const ttok = ascii_lowercase(lex.getString());
598 if (ttok == "no_bar") {
599 f.setUnderbar(FONT_OFF);
600 } else if (ttok == "no_strikeout") {
601 f.setStrikeout(FONT_OFF);
602 } else if (ttok == "no_uuline") {
603 f.setUuline(FONT_OFF);
604 } else if (ttok == "no_uwave") {
605 f.setUwave(FONT_OFF);
606 } else if (ttok == "no_emph") {
608 } else if (ttok == "no_noun") {
610 } else if (ttok == "emph") {
612 } else if (ttok == "underbar") {
613 f.setUnderbar(FONT_ON);
614 } else if (ttok == "strikeout") {
615 f.setStrikeout(FONT_ON);
616 } else if (ttok == "uuline") {
617 f.setUuline(FONT_ON);
618 } else if (ttok == "uwave") {
620 } else if (ttok == "noun") {
623 lex.printError("Illegal misc type");
625 } else if (tok == "color") {
627 string const ttok = lex.getString();
628 setLyXColor(ttok, f);
630 lex.printError("Unknown tag");