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.
22 #include "support/convert.h"
23 #include "support/debug.h"
24 #include "support/docstring.h"
25 #include "support/lstrings.h"
26 #include "support/RefChanger.h"
33 using namespace lyx::support;
38 // Strings used to read and write .lyx format files
40 char const * LyXFamilyNames[NUM_FAMILIES + 2 /* default & error */] =
41 { "roman", "sans", "typewriter", "symbol",
42 "cmr", "cmsy", "cmm", "cmex", "msa", "msb", "eufrak", "rsfs", "stmry",
43 "wasy", "esint", "default", "error" };
45 char const * LyXSeriesNames[NUM_SERIES + 2 /* default & error */] =
46 { "medium", "bold", "default", "error" };
48 char const * LyXShapeNames[NUM_SHAPE + 2 /* default & error */] =
49 { "up", "italic", "slanted", "smallcaps", "default", "error" };
51 char const * LyXSizeNames[NUM_SIZE + 4 /* increase, decrease, default & error */] =
52 { "tiny", "scriptsize", "footnotesize", "small", "normal", "large",
53 "larger", "largest", "huge", "giant",
54 "increase", "decrease", "default", "error" };
56 char const * LyXMiscNames[5] =
57 { "off", "on", "toggle", "default", "error" };
60 FontInfo const sane_font(
75 FontInfo const inherit_font(
90 FontInfo const ignore_font(
112 /// Decreases font size_ by one
113 FontInfo & FontInfo::decSize()
116 case FONT_SIZE_HUGER: size_ = FONT_SIZE_HUGE; break;
117 case FONT_SIZE_HUGE: size_ = FONT_SIZE_LARGEST; break;
118 case FONT_SIZE_LARGEST: size_ = FONT_SIZE_LARGER; break;
119 case FONT_SIZE_LARGER: size_ = FONT_SIZE_LARGE; break;
120 case FONT_SIZE_LARGE: size_ = FONT_SIZE_NORMAL; break;
121 case FONT_SIZE_NORMAL: size_ = FONT_SIZE_SMALL; break;
122 case FONT_SIZE_SMALL: size_ = FONT_SIZE_FOOTNOTE; break;
123 case FONT_SIZE_FOOTNOTE: size_ = FONT_SIZE_SCRIPT; break;
124 case FONT_SIZE_SCRIPT: size_ = FONT_SIZE_TINY; break;
125 case FONT_SIZE_TINY: break;
126 case FONT_SIZE_INCREASE:
127 LYXERR0("Can't FontInfo::decSize on FONT_SIZE_INCREASE");
129 case FONT_SIZE_DECREASE:
130 LYXERR0("Can't FontInfo::decSize on FONT_SIZE_DECREASE");
132 case FONT_SIZE_INHERIT:
133 LYXERR0("Can't FontInfo::decSize on FONT_SIZE_INHERIT");
135 case FONT_SIZE_IGNORE:
136 LYXERR0("Can't FontInfo::decSize on FONT_SIZE_IGNORE");
143 /// Increases font size_ by one
144 FontInfo & FontInfo::incSize()
147 case FONT_SIZE_HUGER: break;
148 case FONT_SIZE_HUGE: size_ = FONT_SIZE_HUGER; break;
149 case FONT_SIZE_LARGEST: size_ = FONT_SIZE_HUGE; break;
150 case FONT_SIZE_LARGER: size_ = FONT_SIZE_LARGEST; break;
151 case FONT_SIZE_LARGE: size_ = FONT_SIZE_LARGER; break;
152 case FONT_SIZE_NORMAL: size_ = FONT_SIZE_LARGE; break;
153 case FONT_SIZE_SMALL: size_ = FONT_SIZE_NORMAL; break;
154 case FONT_SIZE_FOOTNOTE: size_ = FONT_SIZE_SMALL; break;
155 case FONT_SIZE_SCRIPT: size_ = FONT_SIZE_FOOTNOTE; break;
156 case FONT_SIZE_TINY: size_ = FONT_SIZE_SCRIPT; break;
157 case FONT_SIZE_INCREASE:
158 LYXERR0("Can't FontInfo::incSize on FONT_SIZE_INCREASE");
160 case FONT_SIZE_DECREASE:
161 LYXERR0("Can't FontInfo::incSize on FONT_SIZE_DECREASE");
163 case FONT_SIZE_INHERIT:
164 LYXERR0("Can't FontInfo::incSize on FONT_SIZE_INHERIT");
166 case FONT_SIZE_IGNORE:
167 LYXERR0("Can't FontInfo::incSize on FONT_SIZE_IGNORE");
174 double FontInfo::realSize() const
176 double d = convert<double>(lyxrc.font_sizes[size()]);
177 // The following is according to the average of the values in the
178 // definitions of \defaultscriptratio and \defaultscriptscriptratio in LaTeX
179 // font packages. No attempt is made to implement the actual values from
188 case LM_ST_SCRIPTSCRIPT:
192 // Never go below the smallest size
193 return max(d, convert<double>(lyxrc.font_sizes[FONT_SIZE_TINY]));
197 /// Reduce font to fall back to template where possible
198 void FontInfo::reduce(FontInfo const & tmplt)
200 if (family_ == tmplt.family_)
201 family_ = INHERIT_FAMILY;
202 if (series_ == tmplt.series_)
203 series_ = INHERIT_SERIES;
204 if (shape_ == tmplt.shape_)
205 shape_ = INHERIT_SHAPE;
206 if (size_ == tmplt.size_)
207 size_ = FONT_SIZE_INHERIT;
208 if (emph_ == tmplt.emph_)
209 emph_ = FONT_INHERIT;
210 if (underbar_ == tmplt.underbar_)
211 underbar_ = FONT_INHERIT;
212 if (strikeout_ == tmplt.strikeout_)
213 strikeout_ = FONT_INHERIT;
214 if (uuline_ == tmplt.uuline_)
215 uuline_ = FONT_INHERIT;
216 if (uwave_ == tmplt.uwave_)
217 uwave_ = FONT_INHERIT;
218 if (noun_ == tmplt.noun_)
219 noun_ = FONT_INHERIT;
220 if (color_ == tmplt.color_)
221 color_ = Color_inherit;
222 if (background_ == tmplt.background_)
223 background_ = Color_inherit;
227 /// Realize font from a template
228 FontInfo & FontInfo::realize(FontInfo const & tmplt)
230 if ((*this) == inherit_font) {
235 if (family_ == INHERIT_FAMILY)
236 family_ = tmplt.family_;
238 if (series_ == INHERIT_SERIES)
239 series_ = tmplt.series_;
241 if (shape_ == INHERIT_SHAPE)
242 shape_ = tmplt.shape_;
244 if (size_ == FONT_SIZE_INHERIT)
247 if (emph_ == FONT_INHERIT)
250 if (underbar_ == FONT_INHERIT)
251 underbar_ = tmplt.underbar_;
253 if (strikeout_ == FONT_INHERIT)
254 strikeout_ = tmplt.strikeout_;
256 if (uuline_ == FONT_INHERIT)
257 uuline_ = tmplt.uuline_;
259 if (uwave_ == FONT_INHERIT)
260 uwave_ = tmplt.uwave_;
262 if (noun_ == FONT_INHERIT)
265 if (color_ == Color_inherit)
266 color_ = tmplt.color_;
268 if (background_ == Color_inherit)
269 background_ = tmplt.background_;
275 Changer FontInfo::changeColor(ColorCode const color)
277 return make_change(color_, color);
281 Changer FontInfo::changeShape(FontShape const shape)
283 return make_change(shape_, shape);
287 Changer FontInfo::changeStyle(MathStyle const new_style)
289 return make_change(style_, new_style);
293 Changer FontInfo::change(FontInfo font, bool realiz)
297 return make_change(*this, font);
301 /// Updates a misc setting according to request
302 static FontState setMisc(FontState newfont,
305 if (newfont == FONT_TOGGLE) {
308 else if (org == FONT_OFF)
311 LYXERR0("Font::setMisc: Need state"
312 " FONT_ON or FONT_OFF to toggle. Setting to FONT_ON");
315 } else if (newfont == FONT_IGNORE)
321 /// Updates font settings according to request
322 void FontInfo::update(FontInfo const & newfont, bool toggleall)
324 if (newfont.family_ == family_ && toggleall)
325 setFamily(INHERIT_FAMILY); // toggle 'back'
326 else if (newfont.family_ != IGNORE_FAMILY)
327 setFamily(newfont.family_);
328 // else it's IGNORE_SHAPE
330 // "Old" behaviour: "Setting" bold will toggle bold on/off.
331 switch (newfont.series_) {
334 if (series_ == BOLD_SERIES && toggleall)
335 setSeries(MEDIUM_SERIES);
337 setSeries(BOLD_SERIES);
341 setSeries(newfont.series_);
347 if (newfont.shape_ == shape_ && toggleall)
348 shape_ = INHERIT_SHAPE; // toggle 'back'
349 else if (newfont.shape_ != IGNORE_SHAPE)
350 shape_ = newfont.shape_;
351 // else it's IGNORE_SHAPE
353 if (newfont.size_ != FONT_SIZE_IGNORE) {
354 if (newfont.size_ == FONT_SIZE_INCREASE)
356 else if (newfont.size_ == FONT_SIZE_DECREASE)
359 size_ = newfont.size_;
362 setEmph(setMisc(newfont.emph_, emph_));
363 setUnderbar(setMisc(newfont.underbar_, underbar_));
364 setStrikeout(setMisc(newfont.strikeout_, strikeout_));
365 setUuline(setMisc(newfont.uuline_, uuline_));
366 setUwave(setMisc(newfont.uwave_, uwave_));
367 setNoun(setMisc(newfont.noun_, noun_));
368 setNumber(setMisc(newfont.number_, number_));
370 if (newfont.color_ == color_ && toggleall)
371 setColor(Color_inherit); // toggle 'back'
372 else if (newfont.color_ != Color_ignore)
373 setColor(newfont.color_);
375 if (newfont.background_ == background_ && toggleall)
376 setBackground(Color_inherit); // toggle 'back'
377 else if (newfont.background_ != Color_ignore)
378 setBackground(newfont.background_);
381 /// Is font resolved?
382 bool FontInfo::resolved() const
384 return (family_ != INHERIT_FAMILY && series_ != INHERIT_SERIES
385 && shape_ != INHERIT_SHAPE && size_ != FONT_SIZE_INHERIT
386 && emph_ != FONT_INHERIT && underbar_ != FONT_INHERIT
387 && uuline_ != FONT_INHERIT && uwave_ != FONT_INHERIT
388 && strikeout_ != FONT_INHERIT && noun_ != FONT_INHERIT
389 && color_ != Color_inherit
390 && background_ != Color_inherit);
394 Color FontInfo::realColor() const
396 if (paint_color_ != Color_none)
398 if (color_ == Color_none)
399 return Color_foreground;
406 void appendSep(string & s1, string const & s2)
410 s1 += s1.empty() ? "" : "\n";
415 string makeCSSTag(string const & key, string const & val)
417 return key + ": " + val + ";";
421 string getFamilyCSS(FontFamily const & f)
428 case TYPEWRITER_FAMILY:
450 string getSeriesCSS(FontSeries const & s)
465 string getShapeCSS(FontShape const & s)
467 string fs = "normal";
468 string fv = "normal";
470 case UP_SHAPE: break;
471 case ITALIC_SHAPE: fs = "italic"; break;
472 case SLANTED_SHAPE: fs = "oblique"; break;
473 case SMALLCAPS_SHAPE: fv = "small-caps"; break;
476 fs = ""; fv = ""; break;
480 appendSep(retval, makeCSSTag("font-style", fs));
482 appendSep(retval, makeCSSTag("font-variant", fv));
487 string getSizeCSS(FontSize const & s)
492 case FONT_SIZE_SCRIPT:
494 case FONT_SIZE_FOOTNOTE:
495 case FONT_SIZE_SMALL:
497 case FONT_SIZE_NORMAL:
499 case FONT_SIZE_LARGE:
501 case FONT_SIZE_LARGER:
502 case FONT_SIZE_LARGEST:
505 case FONT_SIZE_HUGER:
507 case FONT_SIZE_INCREASE:
509 case FONT_SIZE_DECREASE:
511 case FONT_SIZE_IGNORE:
512 case FONT_SIZE_INHERIT:
518 } // namespace anonymous
521 // FIXME This does not yet handle color
522 docstring FontInfo::asCSS() const
525 string tmp = getFamilyCSS(family_);
527 appendSep(retval, makeCSSTag("font-family", tmp));
528 tmp = getSeriesCSS(series_);
530 appendSep(retval, makeCSSTag("font-weight", tmp));
531 appendSep(retval, getShapeCSS(shape_));
532 tmp = getSizeCSS(size_);
534 appendSep(retval, makeCSSTag("font-size", tmp));
535 return from_ascii(retval);
539 // Set family according to lyx format string
540 void setLyXFamily(string const & fam, FontInfo & f)
542 string const s = ascii_lowercase(fam);
545 while (LyXFamilyNames[i] != s &&
546 LyXFamilyNames[i] != string("error"))
548 if (s == LyXFamilyNames[i])
549 f.setFamily(FontFamily(i));
551 LYXERR0("Unknown family `" << s << '\'');
555 // Set series according to lyx format string
556 void setLyXSeries(string const & ser, FontInfo & f)
558 string const s = ascii_lowercase(ser);
561 while (LyXSeriesNames[i] != s &&
562 LyXSeriesNames[i] != string("error")) ++i;
563 if (s == LyXSeriesNames[i]) {
564 f.setSeries(FontSeries(i));
566 LYXERR0("Unknown series `" << s << '\'');
570 // Set shape according to lyx format string
571 void setLyXShape(string const & sha, FontInfo & f)
573 string const s = ascii_lowercase(sha);
576 while (LyXShapeNames[i] != s && LyXShapeNames[i] != string("error"))
578 if (s == LyXShapeNames[i])
579 f.setShape(FontShape(i));
581 LYXERR0("Unknown shape `" << s << '\'');
585 // Set size according to lyx format string
586 void setLyXSize(string const & siz, FontInfo & f)
588 string const s = ascii_lowercase(siz);
590 while (LyXSizeNames[i] != s && LyXSizeNames[i] != string("error"))
592 if (s == LyXSizeNames[i]) {
593 f.setSize(FontSize(i));
595 LYXERR0("Unknown size `" << s << '\'');
599 // Set size according to lyx format string
600 FontState setLyXMisc(string const & siz)
602 string const s = ascii_lowercase(siz);
604 while (LyXMiscNames[i] != s &&
605 LyXMiscNames[i] != string("error")) ++i;
606 if (s == LyXMiscNames[i])
608 LYXERR0("Unknown misc flag `" << s << '\'');
613 /// Sets color after LyX text format
614 void setLyXColor(string const & col, FontInfo & f)
616 f.setColor(lcolor.getFromLyXName(col));
620 // Read a font definition from given file in lyx format
622 FontInfo lyxRead(Lexer & lex, FontInfo const & fi)
626 bool finished = false;
627 while (!finished && lex.isOK() && !error) {
629 string const tok = ascii_lowercase(lex.getString());
633 } else if (tok == "endfont") {
635 } else if (tok == "family") {
637 string const ttok = lex.getString();
638 setLyXFamily(ttok, f);
639 } else if (tok == "series") {
641 string const ttok = lex.getString();
642 setLyXSeries(ttok, f);
643 } else if (tok == "shape") {
645 string const ttok = lex.getString();
646 setLyXShape(ttok, f);
647 } else if (tok == "size") {
649 string const ttok = lex.getString();
651 } else if (tok == "misc") {
653 string const ttok = ascii_lowercase(lex.getString());
655 if (ttok == "no_bar") {
656 f.setUnderbar(FONT_OFF);
657 } else if (ttok == "no_strikeout") {
658 f.setStrikeout(FONT_OFF);
659 } else if (ttok == "no_uuline") {
660 f.setUuline(FONT_OFF);
661 } else if (ttok == "no_uwave") {
662 f.setUwave(FONT_OFF);
663 } else if (ttok == "no_emph") {
665 } else if (ttok == "no_noun") {
667 } else if (ttok == "emph") {
669 } else if (ttok == "underbar") {
670 f.setUnderbar(FONT_ON);
671 } else if (ttok == "strikeout") {
672 f.setStrikeout(FONT_ON);
673 } else if (ttok == "uuline") {
674 f.setUuline(FONT_ON);
675 } else if (ttok == "uwave") {
677 } else if (ttok == "noun") {
680 lex.printError("Illegal misc type");
682 } else if (tok == "color") {
684 string const ttok = lex.getString();
685 setLyXColor(ttok, f);
687 lex.printError("Unknown tag");
695 void lyxWrite(ostream & os, FontInfo const & f, string const & start, int level)
698 for (int i = 0; i < level; ++i)
701 if (f.family() != INHERIT_FAMILY)
702 oss << indent << "\tFamily " << LyXFamilyNames[f.family()]
704 if (f.series() != INHERIT_SERIES)
705 oss << indent << "\tSeries " << LyXSeriesNames[f.series()]
707 if (f.shape() != INHERIT_SHAPE)
708 oss << indent << "\tShape " << LyXShapeNames[f.shape()]
710 if (f.size() != FONT_SIZE_INHERIT)
711 oss << indent << "\tSize " << LyXSizeNames[f.size()]
713 if (f.underbar() == FONT_ON)
714 oss << indent << "\tMisc Underbar\n";
715 else if (f.underbar() == FONT_OFF)
716 oss << indent << "\tMisc No_Bar\n";
717 if (f.strikeout() == FONT_ON)
718 oss << indent << "\tMisc Strikeout\n";
719 else if (f.strikeout() == FONT_OFF)
720 oss << indent << "\tMisc No_Strikeout\n";
721 if (f.uuline() == FONT_ON)
722 oss << indent << "\tMisc Uuline\n";
723 else if (f.uuline() == FONT_OFF)
724 oss << indent << "\tMisc No_Uuline\n";
725 if (f.uwave() == FONT_ON)
726 oss << indent << "\tMisc Uwave\n";
727 else if (f.uwave() == FONT_OFF)
728 oss << indent << "\tMisc No_Uwave\n";
729 if (f.emph() == FONT_ON)
730 oss << indent << "\tMisc Emph\n";
731 else if (f.emph() == FONT_OFF)
732 oss << indent << "\tMisc No_Emph\n";
733 if (f.noun() == FONT_ON)
734 oss << indent << "\tMisc Noun\n";
735 else if (f.noun() == FONT_OFF)
736 oss << indent << "\tMisc No_Noun\n";
737 if (f.color() != Color_inherit && f.color() != Color_none)
738 oss << indent << "\tColor " << lcolor.getLyXName(f.color())
740 if (!oss.str().empty()) {
741 os << indent << start << '\n'
743 << indent << "EndFont\n";