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"
29 using namespace lyx::support;
34 // Strings used to read and write .lyx format files
36 char const * LyXFamilyNames[NUM_FAMILIES + 2 /* default & error */] =
37 { "roman", "sans", "typewriter", "symbol",
38 "cmr", "cmsy", "cmm", "cmex", "msa", "msb", "eufrak", "rsfs", "stmry",
39 "wasy", "esint", "default", "error" };
41 char const * LyXSeriesNames[NUM_SERIES + 2 /* default & error */] =
42 { "medium", "bold", "default", "error" };
44 char const * LyXShapeNames[NUM_SHAPE + 2 /* default & error */] =
45 { "up", "italic", "slanted", "smallcaps", "default", "error" };
47 char const * LyXSizeNames[NUM_SIZE + 4 /* increase, decrease, default & error */] =
48 { "tiny", "scriptsize", "footnotesize", "small", "normal", "large",
49 "larger", "largest", "huge", "giant",
50 "increase", "decrease", "default", "error" };
52 char const * LyXMiscNames[5] =
53 { "off", "on", "toggle", "default", "error" };
56 FontInfo const sane_font(
71 FontInfo const inherit_font(
86 FontInfo const ignore_font(
108 /// Decreases font size_ by one
109 FontInfo & FontInfo::decSize()
112 case FONT_SIZE_HUGER: size_ = FONT_SIZE_HUGE; break;
113 case FONT_SIZE_HUGE: size_ = FONT_SIZE_LARGEST; break;
114 case FONT_SIZE_LARGEST: size_ = FONT_SIZE_LARGER; break;
115 case FONT_SIZE_LARGER: size_ = FONT_SIZE_LARGE; break;
116 case FONT_SIZE_LARGE: size_ = FONT_SIZE_NORMAL; break;
117 case FONT_SIZE_NORMAL: size_ = FONT_SIZE_SMALL; break;
118 case FONT_SIZE_SMALL: size_ = FONT_SIZE_FOOTNOTE; break;
119 case FONT_SIZE_FOOTNOTE: size_ = FONT_SIZE_SCRIPT; break;
120 case FONT_SIZE_SCRIPT: size_ = FONT_SIZE_TINY; break;
121 case FONT_SIZE_TINY: break;
122 case FONT_SIZE_INCREASE:
123 LYXERR0("Can't FontInfo::decSize on FONT_SIZE_INCREASE");
125 case FONT_SIZE_DECREASE:
126 LYXERR0("Can't FontInfo::decSize on FONT_SIZE_DECREASE");
128 case FONT_SIZE_INHERIT:
129 LYXERR0("Can't FontInfo::decSize on FONT_SIZE_INHERIT");
131 case FONT_SIZE_IGNORE:
132 LYXERR0("Can't FontInfo::decSize on FONT_SIZE_IGNORE");
139 /// Increases font size_ by one
140 FontInfo & FontInfo::incSize()
143 case FONT_SIZE_HUGER: break;
144 case FONT_SIZE_HUGE: size_ = FONT_SIZE_HUGER; break;
145 case FONT_SIZE_LARGEST: size_ = FONT_SIZE_HUGE; break;
146 case FONT_SIZE_LARGER: size_ = FONT_SIZE_LARGEST; break;
147 case FONT_SIZE_LARGE: size_ = FONT_SIZE_LARGER; break;
148 case FONT_SIZE_NORMAL: size_ = FONT_SIZE_LARGE; break;
149 case FONT_SIZE_SMALL: size_ = FONT_SIZE_NORMAL; break;
150 case FONT_SIZE_FOOTNOTE: size_ = FONT_SIZE_SMALL; break;
151 case FONT_SIZE_SCRIPT: size_ = FONT_SIZE_FOOTNOTE; break;
152 case FONT_SIZE_TINY: size_ = FONT_SIZE_SCRIPT; break;
153 case FONT_SIZE_INCREASE:
154 LYXERR0("Can't FontInfo::incSize on FONT_SIZE_INCREASE");
156 case FONT_SIZE_DECREASE:
157 LYXERR0("Can't FontInfo::incSize on FONT_SIZE_DECREASE");
159 case FONT_SIZE_INHERIT:
160 LYXERR0("Can't FontInfo::incSize on FONT_SIZE_INHERIT");
162 case FONT_SIZE_IGNORE:
163 LYXERR0("Can't FontInfo::incSize on FONT_SIZE_IGNORE");
170 /// Reduce font to fall back to template where possible
171 void FontInfo::reduce(FontInfo const & tmplt)
173 if (family_ == tmplt.family_)
174 family_ = INHERIT_FAMILY;
175 if (series_ == tmplt.series_)
176 series_ = INHERIT_SERIES;
177 if (shape_ == tmplt.shape_)
178 shape_ = INHERIT_SHAPE;
179 if (size_ == tmplt.size_)
180 size_ = FONT_SIZE_INHERIT;
181 if (emph_ == tmplt.emph_)
182 emph_ = FONT_INHERIT;
183 if (underbar_ == tmplt.underbar_)
184 underbar_ = FONT_INHERIT;
185 if (strikeout_ == tmplt.strikeout_)
186 strikeout_ = FONT_INHERIT;
187 if (uuline_ == tmplt.uuline_)
188 uuline_ = FONT_INHERIT;
189 if (uwave_ == tmplt.uwave_)
190 uwave_ = FONT_INHERIT;
191 if (noun_ == tmplt.noun_)
192 noun_ = FONT_INHERIT;
193 if (color_ == tmplt.color_)
194 color_ = Color_inherit;
195 if (background_ == tmplt.background_)
196 background_ = Color_inherit;
200 /// Realize font from a template
201 FontInfo & FontInfo::realize(FontInfo const & tmplt)
203 if ((*this) == inherit_font) {
208 if (family_ == INHERIT_FAMILY)
209 family_ = tmplt.family_;
211 if (series_ == INHERIT_SERIES)
212 series_ = tmplt.series_;
214 if (shape_ == INHERIT_SHAPE)
215 shape_ = tmplt.shape_;
217 if (size_ == FONT_SIZE_INHERIT)
220 if (emph_ == FONT_INHERIT)
223 if (underbar_ == FONT_INHERIT)
224 underbar_ = tmplt.underbar_;
226 if (strikeout_ == FONT_INHERIT)
227 strikeout_ = tmplt.strikeout_;
229 if (uuline_ == FONT_INHERIT)
230 uuline_ = tmplt.uuline_;
232 if (uwave_ == FONT_INHERIT)
233 uwave_ = tmplt.uwave_;
235 if (noun_ == FONT_INHERIT)
238 if (color_ == Color_inherit)
239 color_ = tmplt.color_;
241 if (background_ == Color_inherit)
242 background_ = tmplt.background_;
248 /// Updates a misc setting according to request
249 static FontState setMisc(FontState newfont,
252 if (newfont == FONT_TOGGLE) {
255 else if (org == FONT_OFF)
258 LYXERR0("Font::setMisc: Need state"
259 " FONT_ON or FONT_OFF to toggle. Setting to FONT_ON");
262 } else if (newfont == FONT_IGNORE)
268 /// Updates font settings according to request
269 void FontInfo::update(FontInfo const & newfont, bool toggleall)
271 if (newfont.family_ == family_ && toggleall)
272 setFamily(INHERIT_FAMILY); // toggle 'back'
273 else if (newfont.family_ != IGNORE_FAMILY)
274 setFamily(newfont.family_);
275 // else it's IGNORE_SHAPE
277 // "Old" behaviour: "Setting" bold will toggle bold on/off.
278 switch (newfont.series_) {
281 if (series_ == BOLD_SERIES && toggleall)
282 setSeries(MEDIUM_SERIES);
284 setSeries(BOLD_SERIES);
288 setSeries(newfont.series_);
294 if (newfont.shape_ == shape_ && toggleall)
295 shape_ = INHERIT_SHAPE; // toggle 'back'
296 else if (newfont.shape_ != IGNORE_SHAPE)
297 shape_ = newfont.shape_;
298 // else it's IGNORE_SHAPE
300 if (newfont.size_ != FONT_SIZE_IGNORE) {
301 if (newfont.size_ == FONT_SIZE_INCREASE)
303 else if (newfont.size_ == FONT_SIZE_DECREASE)
306 size_ = newfont.size_;
309 setEmph(setMisc(newfont.emph_, emph_));
310 setUnderbar(setMisc(newfont.underbar_, underbar_));
311 setStrikeout(setMisc(newfont.strikeout_, strikeout_));
312 setUuline(setMisc(newfont.uuline_, uuline_));
313 setUwave(setMisc(newfont.uwave_, uwave_));
314 setNoun(setMisc(newfont.noun_, noun_));
315 setNumber(setMisc(newfont.number_, number_));
317 if (newfont.color_ == color_ && toggleall)
318 setColor(Color_inherit); // toggle 'back'
319 else if (newfont.color_ != Color_ignore)
320 setColor(newfont.color_);
322 if (newfont.background_ == background_ && toggleall)
323 setBackground(Color_inherit); // toggle 'back'
324 else if (newfont.background_ != Color_ignore)
325 setBackground(newfont.background_);
328 /// Is font resolved?
329 bool FontInfo::resolved() const
331 return (family_ != INHERIT_FAMILY && series_ != INHERIT_SERIES
332 && shape_ != INHERIT_SHAPE && size_ != FONT_SIZE_INHERIT
333 && emph_ != FONT_INHERIT && underbar_ != FONT_INHERIT
334 && uuline_ != FONT_INHERIT && uwave_ != FONT_INHERIT
335 && strikeout_ != FONT_INHERIT && noun_ != FONT_INHERIT
336 && color_ != Color_inherit
337 && background_ != Color_inherit);
341 Color FontInfo::realColor() const
343 if (paint_color_ != Color_none)
345 if (color_ == Color_none)
346 return Color_foreground;
353 void appendSep(string & s1, string const & s2)
357 s1 += s1.empty() ? "" : "\n";
362 string makeCSSTag(string const & key, string const & val)
364 return key + ": " + val + ";";
368 string getFamilyCSS(FontFamily const & f)
375 case TYPEWRITER_FAMILY:
397 string getSeriesCSS(FontSeries const & s)
412 string getShapeCSS(FontShape const & s)
414 string fs = "normal";
415 string fv = "normal";
417 case UP_SHAPE: break;
418 case ITALIC_SHAPE: fs = "italic"; break;
419 case SLANTED_SHAPE: fs = "oblique"; break;
420 case SMALLCAPS_SHAPE: fv = "small-caps"; break;
423 fs = ""; fv = ""; break;
427 appendSep(retval, makeCSSTag("font-style", fs));
429 appendSep(retval, makeCSSTag("font-variant", fv));
434 string getSizeCSS(FontSize const & s)
439 case FONT_SIZE_SCRIPT:
441 case FONT_SIZE_FOOTNOTE:
442 case FONT_SIZE_SMALL:
444 case FONT_SIZE_NORMAL:
446 case FONT_SIZE_LARGE:
448 case FONT_SIZE_LARGER:
449 case FONT_SIZE_LARGEST:
452 case FONT_SIZE_HUGER:
454 case FONT_SIZE_INCREASE:
456 case FONT_SIZE_DECREASE:
458 case FONT_SIZE_IGNORE:
459 case FONT_SIZE_INHERIT:
465 } // namespace anonymous
468 // FIXME This does not yet handle color
469 docstring FontInfo::asCSS() const
472 string tmp = getFamilyCSS(family_);
474 appendSep(retval, makeCSSTag("font-family", tmp));
475 tmp = getSeriesCSS(series_);
477 appendSep(retval, makeCSSTag("font-weight", tmp));
478 appendSep(retval, getShapeCSS(shape_));
479 tmp = getSizeCSS(size_);
481 appendSep(retval, makeCSSTag("font-size", tmp));
482 return from_ascii(retval);
486 // Set family according to lyx format string
487 void setLyXFamily(string const & fam, FontInfo & f)
489 string const s = ascii_lowercase(fam);
492 while (LyXFamilyNames[i] != s &&
493 LyXFamilyNames[i] != string("error"))
495 if (s == LyXFamilyNames[i])
496 f.setFamily(FontFamily(i));
498 LYXERR0("Unknown family `" << s << '\'');
502 // Set series according to lyx format string
503 void setLyXSeries(string const & ser, FontInfo & f)
505 string const s = ascii_lowercase(ser);
508 while (LyXSeriesNames[i] != s &&
509 LyXSeriesNames[i] != string("error")) ++i;
510 if (s == LyXSeriesNames[i]) {
511 f.setSeries(FontSeries(i));
513 LYXERR0("Unknown series `" << s << '\'');
517 // Set shape according to lyx format string
518 void setLyXShape(string const & sha, FontInfo & f)
520 string const s = ascii_lowercase(sha);
523 while (LyXShapeNames[i] != s && LyXShapeNames[i] != string("error"))
525 if (s == LyXShapeNames[i])
526 f.setShape(FontShape(i));
528 LYXERR0("Unknown shape `" << s << '\'');
532 // Set size according to lyx format string
533 void setLyXSize(string const & siz, FontInfo & f)
535 string const s = ascii_lowercase(siz);
537 while (LyXSizeNames[i] != s && LyXSizeNames[i] != string("error"))
539 if (s == LyXSizeNames[i]) {
540 f.setSize(FontSize(i));
542 LYXERR0("Unknown size `" << s << '\'');
546 // Set size according to lyx format string
547 FontState setLyXMisc(string const & siz)
549 string const s = ascii_lowercase(siz);
551 while (LyXMiscNames[i] != s &&
552 LyXMiscNames[i] != string("error")) ++i;
553 if (s == LyXMiscNames[i])
555 LYXERR0("Unknown misc flag `" << s << '\'');
560 /// Sets color after LyX text format
561 void setLyXColor(string const & col, FontInfo & f)
563 f.setColor(lcolor.getFromLyXName(col));
567 // Read a font definition from given file in lyx format
569 FontInfo lyxRead(Lexer & lex, FontInfo const & fi)
573 bool finished = false;
574 while (!finished && lex.isOK() && !error) {
576 string const tok = ascii_lowercase(lex.getString());
580 } else if (tok == "endfont") {
582 } else if (tok == "family") {
584 string const ttok = lex.getString();
585 setLyXFamily(ttok, f);
586 } else if (tok == "series") {
588 string const ttok = lex.getString();
589 setLyXSeries(ttok, f);
590 } else if (tok == "shape") {
592 string const ttok = lex.getString();
593 setLyXShape(ttok, f);
594 } else if (tok == "size") {
596 string const ttok = lex.getString();
598 } else if (tok == "misc") {
600 string const ttok = ascii_lowercase(lex.getString());
602 if (ttok == "no_bar") {
603 f.setUnderbar(FONT_OFF);
604 } else if (ttok == "no_strikeout") {
605 f.setStrikeout(FONT_OFF);
606 } else if (ttok == "no_uuline") {
607 f.setUuline(FONT_OFF);
608 } else if (ttok == "no_uwave") {
609 f.setUwave(FONT_OFF);
610 } else if (ttok == "no_emph") {
612 } else if (ttok == "no_noun") {
614 } else if (ttok == "emph") {
616 } else if (ttok == "underbar") {
617 f.setUnderbar(FONT_ON);
618 } else if (ttok == "strikeout") {
619 f.setStrikeout(FONT_ON);
620 } else if (ttok == "uuline") {
621 f.setUuline(FONT_ON);
622 } else if (ttok == "uwave") {
624 } else if (ttok == "noun") {
627 lex.printError("Illegal misc type");
629 } else if (tok == "color") {
631 string const ttok = lex.getString();
632 setLyXColor(ttok, f);
634 lex.printError("Unknown tag");
642 void lyxWrite(ostream & os, FontInfo const & f, string const & start, int level)
645 for (int i = 0; i < level; ++i)
648 if (f.family() != INHERIT_FAMILY)
649 oss << indent << "\tFamily " << LyXFamilyNames[f.family()]
651 if (f.series() != INHERIT_SERIES)
652 oss << indent << "\tSeries " << LyXSeriesNames[f.series()]
654 if (f.shape() != INHERIT_SHAPE)
655 oss << indent << "\tShape " << LyXShapeNames[f.shape()]
657 if (f.size() != FONT_SIZE_INHERIT)
658 oss << indent << "\tSize " << LyXSizeNames[f.size()]
660 if (f.underbar() == FONT_ON)
661 oss << indent << "\tMisc Underbar\n";
662 else if (f.underbar() == FONT_OFF)
663 oss << indent << "\tMisc No_Bar\n";
664 if (f.strikeout() == FONT_ON)
665 oss << indent << "\tMisc Strikeout\n";
666 else if (f.strikeout() == FONT_OFF)
667 oss << indent << "\tMisc No_Strikeout\n";
668 if (f.uuline() == FONT_ON)
669 oss << indent << "\tMisc Uuline\n";
670 else if (f.uuline() == FONT_OFF)
671 oss << indent << "\tMisc No_Uuline\n";
672 if (f.uwave() == FONT_ON)
673 oss << indent << "\tMisc Uwave\n";
674 else if (f.uwave() == FONT_OFF)
675 oss << indent << "\tMisc No_Uwave\n";
676 if (f.emph() == FONT_ON)
677 oss << indent << "\tMisc Emph\n";
678 else if (f.emph() == FONT_OFF)
679 oss << indent << "\tMisc No_Emph\n";
680 if (f.noun() == FONT_ON)
681 oss << indent << "\tMisc Noun\n";
682 else if (f.noun() == FONT_OFF)
683 oss << indent << "\tMisc No_Noun\n";
684 if (f.color() != Color_inherit && f.color() != Color_none)
685 oss << indent << "\tColor " << lcolor.getLyXName(f.color())
687 if (!oss.str().empty()) {
688 os << indent << start << '\n'
690 << indent << "EndFont\n";