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"
32 using namespace lyx::support;
37 // Strings used to read and write .lyx format files
39 char const * LyXFamilyNames[NUM_FAMILIES + 2 /* default & error */] =
40 { "roman", "sans", "typewriter", "symbol",
41 "cmr", "cmsy", "cmm", "cmex", "msa", "msb", "eufrak", "rsfs", "stmry",
42 "wasy", "esint", "default", "error" };
44 char const * LyXSeriesNames[NUM_SERIES + 2 /* default & error */] =
45 { "medium", "bold", "default", "error" };
47 char const * LyXShapeNames[NUM_SHAPE + 2 /* default & error */] =
48 { "up", "italic", "slanted", "smallcaps", "default", "error" };
50 char const * LyXSizeNames[NUM_SIZE + 4 /* increase, decrease, default & error */] =
51 { "tiny", "scriptsize", "footnotesize", "small", "normal", "large",
52 "larger", "largest", "huge", "giant",
53 "increase", "decrease", "default", "error" };
55 char const * LyXMiscNames[5] =
56 { "off", "on", "toggle", "default", "error" };
59 FontInfo const sane_font(
74 FontInfo const inherit_font(
89 FontInfo const ignore_font(
111 /// Decreases font size_ by one
112 FontInfo & FontInfo::decSize()
115 case FONT_SIZE_HUGER: size_ = FONT_SIZE_HUGE; break;
116 case FONT_SIZE_HUGE: size_ = FONT_SIZE_LARGEST; break;
117 case FONT_SIZE_LARGEST: size_ = FONT_SIZE_LARGER; break;
118 case FONT_SIZE_LARGER: size_ = FONT_SIZE_LARGE; break;
119 case FONT_SIZE_LARGE: size_ = FONT_SIZE_NORMAL; break;
120 case FONT_SIZE_NORMAL: size_ = FONT_SIZE_SMALL; break;
121 case FONT_SIZE_SMALL: size_ = FONT_SIZE_FOOTNOTE; break;
122 case FONT_SIZE_FOOTNOTE: size_ = FONT_SIZE_SCRIPT; break;
123 case FONT_SIZE_SCRIPT: size_ = FONT_SIZE_TINY; break;
124 case FONT_SIZE_TINY: break;
125 case FONT_SIZE_INCREASE:
126 LYXERR0("Can't FontInfo::decSize on FONT_SIZE_INCREASE");
128 case FONT_SIZE_DECREASE:
129 LYXERR0("Can't FontInfo::decSize on FONT_SIZE_DECREASE");
131 case FONT_SIZE_INHERIT:
132 LYXERR0("Can't FontInfo::decSize on FONT_SIZE_INHERIT");
134 case FONT_SIZE_IGNORE:
135 LYXERR0("Can't FontInfo::decSize on FONT_SIZE_IGNORE");
142 /// Increases font size_ by one
143 FontInfo & FontInfo::incSize()
146 case FONT_SIZE_HUGER: break;
147 case FONT_SIZE_HUGE: size_ = FONT_SIZE_HUGER; break;
148 case FONT_SIZE_LARGEST: size_ = FONT_SIZE_HUGE; break;
149 case FONT_SIZE_LARGER: size_ = FONT_SIZE_LARGEST; break;
150 case FONT_SIZE_LARGE: size_ = FONT_SIZE_LARGER; break;
151 case FONT_SIZE_NORMAL: size_ = FONT_SIZE_LARGE; break;
152 case FONT_SIZE_SMALL: size_ = FONT_SIZE_NORMAL; break;
153 case FONT_SIZE_FOOTNOTE: size_ = FONT_SIZE_SMALL; break;
154 case FONT_SIZE_SCRIPT: size_ = FONT_SIZE_FOOTNOTE; break;
155 case FONT_SIZE_TINY: size_ = FONT_SIZE_SCRIPT; break;
156 case FONT_SIZE_INCREASE:
157 LYXERR0("Can't FontInfo::incSize on FONT_SIZE_INCREASE");
159 case FONT_SIZE_DECREASE:
160 LYXERR0("Can't FontInfo::incSize on FONT_SIZE_DECREASE");
162 case FONT_SIZE_INHERIT:
163 LYXERR0("Can't FontInfo::incSize on FONT_SIZE_INHERIT");
165 case FONT_SIZE_IGNORE:
166 LYXERR0("Can't FontInfo::incSize on FONT_SIZE_IGNORE");
173 double FontInfo::realSize() const
175 double d = convert<double>(lyxrc.font_sizes[size()]);
176 // The following is according to the average of the values in the
177 // definitions of \defaultscriptratio and \defaultscriptscriptratio in LaTeX
178 // font packages. No attempt is made to implement the actual values from
187 case LM_ST_SCRIPTSCRIPT:
191 // Never go below the smallest size
192 return max(d, convert<double>(lyxrc.font_sizes[FONT_SIZE_TINY]));
196 /// Reduce font to fall back to template where possible
197 void FontInfo::reduce(FontInfo const & tmplt)
199 if (family_ == tmplt.family_)
200 family_ = INHERIT_FAMILY;
201 if (series_ == tmplt.series_)
202 series_ = INHERIT_SERIES;
203 if (shape_ == tmplt.shape_)
204 shape_ = INHERIT_SHAPE;
205 if (size_ == tmplt.size_)
206 size_ = FONT_SIZE_INHERIT;
207 if (emph_ == tmplt.emph_)
208 emph_ = FONT_INHERIT;
209 if (underbar_ == tmplt.underbar_)
210 underbar_ = FONT_INHERIT;
211 if (strikeout_ == tmplt.strikeout_)
212 strikeout_ = FONT_INHERIT;
213 if (uuline_ == tmplt.uuline_)
214 uuline_ = FONT_INHERIT;
215 if (uwave_ == tmplt.uwave_)
216 uwave_ = FONT_INHERIT;
217 if (noun_ == tmplt.noun_)
218 noun_ = FONT_INHERIT;
219 if (color_ == tmplt.color_)
220 color_ = Color_inherit;
221 if (background_ == tmplt.background_)
222 background_ = Color_inherit;
226 /// Realize font from a template
227 FontInfo & FontInfo::realize(FontInfo const & tmplt)
229 if ((*this) == inherit_font) {
234 if (family_ == INHERIT_FAMILY)
235 family_ = tmplt.family_;
237 if (series_ == INHERIT_SERIES)
238 series_ = tmplt.series_;
240 if (shape_ == INHERIT_SHAPE)
241 shape_ = tmplt.shape_;
243 if (size_ == FONT_SIZE_INHERIT)
246 if (emph_ == FONT_INHERIT)
249 if (underbar_ == FONT_INHERIT)
250 underbar_ = tmplt.underbar_;
252 if (strikeout_ == FONT_INHERIT)
253 strikeout_ = tmplt.strikeout_;
255 if (uuline_ == FONT_INHERIT)
256 uuline_ = tmplt.uuline_;
258 if (uwave_ == FONT_INHERIT)
259 uwave_ = tmplt.uwave_;
261 if (noun_ == FONT_INHERIT)
264 if (color_ == Color_inherit)
265 color_ = tmplt.color_;
267 if (background_ == Color_inherit)
268 background_ = tmplt.background_;
274 Changer FontInfo::changeColor(ColorCode const color)
276 return make_change(color_, color);
280 Changer FontInfo::changeShape(FontShape const shape)
282 return make_change(shape_, shape);
286 Changer FontInfo::changeStyle(MathStyle const new_style)
288 return make_change(style_, new_style);
292 Changer FontInfo::change(FontInfo font, bool realiz)
296 return make_change(*this, font);
300 /// Updates a misc setting according to request
301 static FontState setMisc(FontState newfont,
304 if (newfont == FONT_TOGGLE) {
307 else if (org == FONT_OFF)
310 LYXERR0("Font::setMisc: Need state"
311 " FONT_ON or FONT_OFF to toggle. Setting to FONT_ON");
314 } else if (newfont == FONT_IGNORE)
320 /// Updates font settings according to request
321 void FontInfo::update(FontInfo const & newfont, bool toggleall)
323 if (newfont.family_ == family_ && toggleall)
324 setFamily(INHERIT_FAMILY); // toggle 'back'
325 else if (newfont.family_ != IGNORE_FAMILY)
326 setFamily(newfont.family_);
327 // else it's IGNORE_SHAPE
329 // "Old" behaviour: "Setting" bold will toggle bold on/off.
330 switch (newfont.series_) {
333 if (series_ == BOLD_SERIES && toggleall)
334 setSeries(MEDIUM_SERIES);
336 setSeries(BOLD_SERIES);
340 setSeries(newfont.series_);
346 if (newfont.shape_ == shape_ && toggleall)
347 shape_ = INHERIT_SHAPE; // toggle 'back'
348 else if (newfont.shape_ != IGNORE_SHAPE)
349 shape_ = newfont.shape_;
350 // else it's IGNORE_SHAPE
352 if (newfont.size_ != FONT_SIZE_IGNORE) {
353 if (newfont.size_ == FONT_SIZE_INCREASE)
355 else if (newfont.size_ == FONT_SIZE_DECREASE)
358 size_ = newfont.size_;
361 setEmph(setMisc(newfont.emph_, emph_));
362 setUnderbar(setMisc(newfont.underbar_, underbar_));
363 setStrikeout(setMisc(newfont.strikeout_, strikeout_));
364 setUuline(setMisc(newfont.uuline_, uuline_));
365 setUwave(setMisc(newfont.uwave_, uwave_));
366 setNoun(setMisc(newfont.noun_, noun_));
367 setNumber(setMisc(newfont.number_, number_));
369 if (newfont.color_ == color_ && toggleall)
370 setColor(Color_inherit); // toggle 'back'
371 else if (newfont.color_ != Color_ignore)
372 setColor(newfont.color_);
374 if (newfont.background_ == background_ && toggleall)
375 setBackground(Color_inherit); // toggle 'back'
376 else if (newfont.background_ != Color_ignore)
377 setBackground(newfont.background_);
380 /// Is font resolved?
381 bool FontInfo::resolved() const
383 return (family_ != INHERIT_FAMILY && series_ != INHERIT_SERIES
384 && shape_ != INHERIT_SHAPE && size_ != FONT_SIZE_INHERIT
385 && emph_ != FONT_INHERIT && underbar_ != FONT_INHERIT
386 && uuline_ != FONT_INHERIT && uwave_ != FONT_INHERIT
387 && strikeout_ != FONT_INHERIT && noun_ != FONT_INHERIT
388 && color_ != Color_inherit
389 && background_ != Color_inherit);
393 Color FontInfo::realColor() const
395 if (paint_color_ != Color_none)
397 if (color_ == Color_none)
398 return Color_foreground;
405 void appendSep(string & s1, string const & s2)
409 s1 += s1.empty() ? "" : "\n";
414 string makeCSSTag(string const & key, string const & val)
416 return key + ": " + val + ";";
420 string getFamilyCSS(FontFamily const & f)
427 case TYPEWRITER_FAMILY:
449 string getSeriesCSS(FontSeries const & s)
464 string getShapeCSS(FontShape const & s)
466 string fs = "normal";
467 string fv = "normal";
469 case UP_SHAPE: break;
470 case ITALIC_SHAPE: fs = "italic"; break;
471 case SLANTED_SHAPE: fs = "oblique"; break;
472 case SMALLCAPS_SHAPE: fv = "small-caps"; break;
475 fs = ""; fv = ""; break;
479 appendSep(retval, makeCSSTag("font-style", fs));
481 appendSep(retval, makeCSSTag("font-variant", fv));
486 string getSizeCSS(FontSize const & s)
491 case FONT_SIZE_SCRIPT:
493 case FONT_SIZE_FOOTNOTE:
494 case FONT_SIZE_SMALL:
496 case FONT_SIZE_NORMAL:
498 case FONT_SIZE_LARGE:
500 case FONT_SIZE_LARGER:
501 case FONT_SIZE_LARGEST:
504 case FONT_SIZE_HUGER:
506 case FONT_SIZE_INCREASE:
508 case FONT_SIZE_DECREASE:
510 case FONT_SIZE_IGNORE:
511 case FONT_SIZE_INHERIT:
517 } // namespace anonymous
520 // FIXME This does not yet handle color
521 docstring FontInfo::asCSS() const
524 string tmp = getFamilyCSS(family_);
526 appendSep(retval, makeCSSTag("font-family", tmp));
527 tmp = getSeriesCSS(series_);
529 appendSep(retval, makeCSSTag("font-weight", tmp));
530 appendSep(retval, getShapeCSS(shape_));
531 tmp = getSizeCSS(size_);
533 appendSep(retval, makeCSSTag("font-size", tmp));
534 return from_ascii(retval);
538 // Set family according to lyx format string
539 void setLyXFamily(string const & fam, FontInfo & f)
541 string const s = ascii_lowercase(fam);
544 while (LyXFamilyNames[i] != s &&
545 LyXFamilyNames[i] != string("error"))
547 if (s == LyXFamilyNames[i])
548 f.setFamily(FontFamily(i));
550 LYXERR0("Unknown family `" << s << '\'');
554 // Set series according to lyx format string
555 void setLyXSeries(string const & ser, FontInfo & f)
557 string const s = ascii_lowercase(ser);
560 while (LyXSeriesNames[i] != s &&
561 LyXSeriesNames[i] != string("error")) ++i;
562 if (s == LyXSeriesNames[i]) {
563 f.setSeries(FontSeries(i));
565 LYXERR0("Unknown series `" << s << '\'');
569 // Set shape according to lyx format string
570 void setLyXShape(string const & sha, FontInfo & f)
572 string const s = ascii_lowercase(sha);
575 while (LyXShapeNames[i] != s && LyXShapeNames[i] != string("error"))
577 if (s == LyXShapeNames[i])
578 f.setShape(FontShape(i));
580 LYXERR0("Unknown shape `" << s << '\'');
584 // Set size according to lyx format string
585 void setLyXSize(string const & siz, FontInfo & f)
587 string const s = ascii_lowercase(siz);
589 while (LyXSizeNames[i] != s && LyXSizeNames[i] != string("error"))
591 if (s == LyXSizeNames[i]) {
592 f.setSize(FontSize(i));
594 LYXERR0("Unknown size `" << s << '\'');
598 // Set size according to lyx format string
599 FontState setLyXMisc(string const & siz)
601 string const s = ascii_lowercase(siz);
603 while (LyXMiscNames[i] != s &&
604 LyXMiscNames[i] != string("error")) ++i;
605 if (s == LyXMiscNames[i])
607 LYXERR0("Unknown misc flag `" << s << '\'');
612 /// Sets color after LyX text format
613 void setLyXColor(string const & col, FontInfo & f)
615 f.setColor(lcolor.getFromLyXName(col));
619 // Read a font definition from given file in lyx format
621 FontInfo lyxRead(Lexer & lex, FontInfo const & fi)
625 bool finished = false;
626 while (!finished && lex.isOK() && !error) {
628 string const tok = ascii_lowercase(lex.getString());
632 } else if (tok == "endfont") {
634 } else if (tok == "family") {
636 string const ttok = lex.getString();
637 setLyXFamily(ttok, f);
638 } else if (tok == "series") {
640 string const ttok = lex.getString();
641 setLyXSeries(ttok, f);
642 } else if (tok == "shape") {
644 string const ttok = lex.getString();
645 setLyXShape(ttok, f);
646 } else if (tok == "size") {
648 string const ttok = lex.getString();
650 } else if (tok == "misc") {
652 string const ttok = ascii_lowercase(lex.getString());
654 if (ttok == "no_bar") {
655 f.setUnderbar(FONT_OFF);
656 } else if (ttok == "no_strikeout") {
657 f.setStrikeout(FONT_OFF);
658 } else if (ttok == "no_uuline") {
659 f.setUuline(FONT_OFF);
660 } else if (ttok == "no_uwave") {
661 f.setUwave(FONT_OFF);
662 } else if (ttok == "no_emph") {
664 } else if (ttok == "no_noun") {
666 } else if (ttok == "emph") {
668 } else if (ttok == "underbar") {
669 f.setUnderbar(FONT_ON);
670 } else if (ttok == "strikeout") {
671 f.setStrikeout(FONT_ON);
672 } else if (ttok == "uuline") {
673 f.setUuline(FONT_ON);
674 } else if (ttok == "uwave") {
676 } else if (ttok == "noun") {
679 lex.printError("Illegal misc type");
681 } else if (tok == "color") {
683 string const ttok = lex.getString();
684 setLyXColor(ttok, f);
686 lex.printError("Unknown tag");
694 void lyxWrite(ostream & os, FontInfo const & f, string const & start, int level)
697 for (int i = 0; i < level; ++i)
700 if (f.family() != INHERIT_FAMILY)
701 oss << indent << "\tFamily " << LyXFamilyNames[f.family()]
703 if (f.series() != INHERIT_SERIES)
704 oss << indent << "\tSeries " << LyXSeriesNames[f.series()]
706 if (f.shape() != INHERIT_SHAPE)
707 oss << indent << "\tShape " << LyXShapeNames[f.shape()]
709 if (f.size() != FONT_SIZE_INHERIT)
710 oss << indent << "\tSize " << LyXSizeNames[f.size()]
712 if (f.underbar() == FONT_ON)
713 oss << indent << "\tMisc Underbar\n";
714 else if (f.underbar() == FONT_OFF)
715 oss << indent << "\tMisc No_Bar\n";
716 if (f.strikeout() == FONT_ON)
717 oss << indent << "\tMisc Strikeout\n";
718 else if (f.strikeout() == FONT_OFF)
719 oss << indent << "\tMisc No_Strikeout\n";
720 if (f.uuline() == FONT_ON)
721 oss << indent << "\tMisc Uuline\n";
722 else if (f.uuline() == FONT_OFF)
723 oss << indent << "\tMisc No_Uuline\n";
724 if (f.uwave() == FONT_ON)
725 oss << indent << "\tMisc Uwave\n";
726 else if (f.uwave() == FONT_OFF)
727 oss << indent << "\tMisc No_Uwave\n";
728 if (f.emph() == FONT_ON)
729 oss << indent << "\tMisc Emph\n";
730 else if (f.emph() == FONT_OFF)
731 oss << indent << "\tMisc No_Emph\n";
732 if (f.noun() == FONT_ON)
733 oss << indent << "\tMisc Noun\n";
734 else if (f.noun() == FONT_OFF)
735 oss << indent << "\tMisc No_Noun\n";
736 if (f.color() != Color_inherit && f.color() != Color_none)
737 oss << indent << "\tColor " << lcolor.getLyXName(f.color())
739 if (!oss.str().empty()) {
740 os << indent << start << '\n'
742 << indent << "EndFont\n";