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(
104 /// Decreases font size_ by one
105 FontInfo & FontInfo::decSize()
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");
121 case FONT_SIZE_DECREASE:
122 LYXERR0("Can't FontInfo::decSize on FONT_SIZE_DECREASE");
124 case FONT_SIZE_INHERIT:
125 LYXERR0("Can't FontInfo::decSize on FONT_SIZE_INHERIT");
127 case FONT_SIZE_IGNORE:
128 LYXERR0("Can't FontInfo::decSize on FONT_SIZE_IGNORE");
135 /// Increases font size_ by one
136 FontInfo & FontInfo::incSize()
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");
152 case FONT_SIZE_DECREASE:
153 LYXERR0("Can't FontInfo::incSize on FONT_SIZE_DECREASE");
155 case FONT_SIZE_INHERIT:
156 LYXERR0("Can't FontInfo::incSize on FONT_SIZE_INHERIT");
158 case FONT_SIZE_IGNORE:
159 LYXERR0("Can't FontInfo::incSize on FONT_SIZE_IGNORE");
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) {
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)
216 if (emph_ == FONT_INHERIT)
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)
234 if (color_ == Color_inherit)
235 color_ = tmplt.color_;
237 if (background_ == Color_inherit)
238 background_ = tmplt.background_;
244 /// Updates a misc setting according to request
245 static FontState setMisc(FontState newfont,
248 if (newfont == FONT_TOGGLE) {
251 else if (org == FONT_OFF)
254 LYXERR0("Font::setMisc: Need state"
255 " FONT_ON or FONT_OFF to toggle. Setting to FONT_ON");
258 } else if (newfont == FONT_IGNORE)
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_) {
277 if (series_ == BOLD_SERIES && toggleall)
278 setSeries(MEDIUM_SERIES);
280 setSeries(BOLD_SERIES);
284 setSeries(newfont.series_);
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)
299 else if (newfont.size_ == FONT_SIZE_DECREASE)
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)
341 if (color_ == Color_none)
342 return Color_foreground;
349 void appendSep(string & s1, string const & s2) {
352 s1 += s1.empty() ? "" : "\n";
357 string makeCSSTag(string const & key, string const & val)
359 return key + ": " + val + ";";
363 string getFamilyCSS(FontFamily const & f)
370 case TYPEWRITER_FAMILY:
391 string getSeriesCSS(FontSeries const & s)
406 string getShapeCSS(FontShape const & s)
408 string fs = "normal";
409 string fv = "normal";
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;
417 fs = ""; fv = ""; break;
421 appendSep(retval, makeCSSTag("font-style", fs));
423 appendSep(retval, makeCSSTag("font-variant", fv));
428 string getSizeCSS(FontSize const & s)
433 case FONT_SIZE_SCRIPT:
435 case FONT_SIZE_FOOTNOTE:
436 case FONT_SIZE_SMALL:
438 case FONT_SIZE_NORMAL:
440 case FONT_SIZE_LARGE:
442 case FONT_SIZE_LARGER:
443 case FONT_SIZE_LARGEST:
446 case FONT_SIZE_HUGER:
448 case FONT_SIZE_INCREASE:
450 case FONT_SIZE_DECREASE:
452 case FONT_SIZE_IGNORE:
453 case FONT_SIZE_INHERIT:
459 } // namespace anonymous
462 // FIXME This does not yet handle color
463 docstring FontInfo::asCSS() const
466 string tmp = getFamilyCSS(family_);
468 appendSep(retval, makeCSSTag("font-family", tmp));
469 tmp = getSeriesCSS(series_);
471 appendSep(retval, makeCSSTag("font-weight", tmp));
472 appendSep(retval, getShapeCSS(shape_));
473 tmp = getSizeCSS(size_);
475 appendSep(retval, makeCSSTag("font-size", tmp));
476 return from_ascii(retval);
480 // Set family according to lyx format string
481 void setLyXFamily(string const & fam, FontInfo & f)
483 string const s = ascii_lowercase(fam);
486 while (LyXFamilyNames[i] != s &&
487 LyXFamilyNames[i] != string("error"))
489 if (s == LyXFamilyNames[i])
490 f.setFamily(FontFamily(i));
492 LYXERR0("Unknown family `" << s << '\'');
496 // Set series according to lyx format string
497 void setLyXSeries(string const & ser, FontInfo & f)
499 string const s = ascii_lowercase(ser);
502 while (LyXSeriesNames[i] != s &&
503 LyXSeriesNames[i] != string("error")) ++i;
504 if (s == LyXSeriesNames[i]) {
505 f.setSeries(FontSeries(i));
507 LYXERR0("Unknown series `" << s << '\'');
511 // Set shape according to lyx format string
512 void setLyXShape(string const & sha, FontInfo & f)
514 string const s = ascii_lowercase(sha);
517 while (LyXShapeNames[i] != s && LyXShapeNames[i] != string("error"))
519 if (s == LyXShapeNames[i])
520 f.setShape(FontShape(i));
522 LYXERR0("Unknown shape `" << s << '\'');
526 // Set size according to lyx format string
527 void setLyXSize(string const & siz, FontInfo & f)
529 string const s = ascii_lowercase(siz);
531 while (LyXSizeNames[i] != s && LyXSizeNames[i] != string("error"))
533 if (s == LyXSizeNames[i]) {
534 f.setSize(FontSize(i));
536 LYXERR0("Unknown size `" << s << '\'');
540 // Set size according to lyx format string
541 FontState setLyXMisc(string const & siz)
543 string const s = ascii_lowercase(siz);
545 while (LyXMiscNames[i] != s &&
546 LyXMiscNames[i] != string("error")) ++i;
547 if (s == LyXMiscNames[i])
549 LYXERR0("Unknown misc flag `" << s << '\'');
554 /// Sets color after LyX text format
555 void setLyXColor(string const & col, FontInfo & f)
557 f.setColor(lcolor.getFromLyXName(col));
561 // Read a font definition from given file in lyx format
563 FontInfo lyxRead(Lexer & lex, FontInfo const & fi)
567 bool finished = false;
568 while (!finished && lex.isOK() && !error) {
570 string const tok = ascii_lowercase(lex.getString());
574 } else if (tok == "endfont") {
576 } else if (tok == "family") {
578 string const ttok = lex.getString();
579 setLyXFamily(ttok, f);
580 } else if (tok == "series") {
582 string const ttok = lex.getString();
583 setLyXSeries(ttok, f);
584 } else if (tok == "shape") {
586 string const ttok = lex.getString();
587 setLyXShape(ttok, f);
588 } else if (tok == "size") {
590 string const ttok = lex.getString();
592 } else if (tok == "misc") {
594 string const ttok = ascii_lowercase(lex.getString());
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") {
606 } else if (ttok == "no_noun") {
608 } else if (ttok == "emph") {
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") {
618 } else if (ttok == "noun") {
621 lex.printError("Illegal misc type");
623 } else if (tok == "color") {
625 string const ttok = lex.getString();
626 setLyXColor(ttok, f);
628 lex.printError("Unknown tag");