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.
23 #include "support/convert.h"
24 #include "support/debug.h"
25 #include "support/docstring.h"
26 #include "support/gettext.h"
27 #include "support/lstrings.h"
28 #include "support/RefChanger.h"
35 using namespace lyx::support;
43 char const * GUIFamilyNames[NUM_FAMILIES + 2 /* default & error */] =
44 { N_("Roman"), N_("Sans Serif"), N_("Typewriter"), N_("Symbol"),
45 "cmr", "cmsy", "cmm", "cmex", "msa", "msb", "ds", "eufrak", "rsfs", "stmry",
46 "wasy", "esint", N_("Inherit"), N_("Ignore") };
48 char const * GUISeriesNames[NUM_SERIES + 2 /* default & error */] =
49 { N_("Medium"), N_("Bold"), N_("Inherit"), N_("Ignore") };
51 char const * GUIShapeNames[NUM_SHAPE + 2 /* default & error */] =
52 { N_("Upright"), N_("Italic"), N_("Slanted"), N_("Smallcaps"), N_("Inherit"),
55 char const * GUISizeNames[NUM_SIZE + 4 /* increase, decrease, default & error */] =
56 { N_("Tiny"), N_("Smallest"), N_("Smaller"), N_("Small"), N_("Normal"), N_("Large"),
57 N_("Larger"), N_("Largest"), N_("Huge"), N_("Huger"), N_("Increase"), N_("Decrease"),
58 N_("Inherit"), N_("Ignore") };
60 char const * GUIMiscNames[5] =
61 { N_("Off"), N_("On"), N_("Toggle"), N_("Inherit"), N_("Ignore") };
65 // Strings used to read and write .lyx format files
67 char const * LyXFamilyNames[NUM_FAMILIES + 2 /* default & error */] =
68 { "roman", "sans", "typewriter", "symbol",
69 "cmr", "cmsy", "cmm", "cmex", "msa", "msb", "ds", "eufrak", "rsfs", "stmry",
70 "wasy", "esint", "default", "error" };
72 char const * LyXSeriesNames[NUM_SERIES + 2 /* default & error */] =
73 { "medium", "bold", "default", "error" };
75 char const * LyXShapeNames[NUM_SHAPE + 2 /* default & error */] =
76 { "up", "italic", "slanted", "smallcaps", "default", "error" };
78 char const * LyXSizeNames[NUM_SIZE + 4 /* increase, decrease, default & error */] =
79 { "tiny", "scriptsize", "footnotesize", "small", "normal", "large",
80 "larger", "largest", "huge", "giant",
81 "increase", "decrease", "default", "error" };
83 char const * LyXMiscNames[5] =
84 { "off", "on", "toggle", "default", "error" };
87 FontInfo const sane_font(
105 FontInfo const inherit_font(
123 FontInfo const ignore_font(
148 /// Decreases font size_ by one
149 FontInfo & FontInfo::decSize()
152 case HUGER_SIZE: size_ = HUGE_SIZE; break;
153 case HUGE_SIZE: size_ = LARGEST_SIZE; break;
154 case LARGEST_SIZE: size_ = LARGER_SIZE; break;
155 case LARGER_SIZE: size_ = LARGE_SIZE; break;
156 case LARGE_SIZE: size_ = NORMAL_SIZE; break;
157 case NORMAL_SIZE: size_ = SMALL_SIZE; break;
158 case SMALL_SIZE: size_ = FOOTNOTE_SIZE; break;
159 case FOOTNOTE_SIZE: size_ = SCRIPT_SIZE; break;
160 case SCRIPT_SIZE: size_ = TINY_SIZE; break;
161 case TINY_SIZE: break;
163 LYXERR0("Can't FontInfo::decSize on INCREASE_SIZE");
166 LYXERR0("Can't FontInfo::decSize on DECREASE_SIZE");
169 LYXERR0("Can't FontInfo::decSize on INHERIT_SIZE");
172 LYXERR0("Can't FontInfo::decSize on IGNORE_SIZE");
179 /// Increases font size_ by one
180 FontInfo & FontInfo::incSize()
183 case HUGER_SIZE: break;
184 case HUGE_SIZE: size_ = HUGER_SIZE; break;
185 case LARGEST_SIZE: size_ = HUGE_SIZE; break;
186 case LARGER_SIZE: size_ = LARGEST_SIZE; break;
187 case LARGE_SIZE: size_ = LARGER_SIZE; break;
188 case NORMAL_SIZE: size_ = LARGE_SIZE; break;
189 case SMALL_SIZE: size_ = NORMAL_SIZE; break;
190 case FOOTNOTE_SIZE: size_ = SMALL_SIZE; break;
191 case SCRIPT_SIZE: size_ = FOOTNOTE_SIZE; break;
192 case TINY_SIZE: size_ = SCRIPT_SIZE; break;
194 LYXERR0("Can't FontInfo::incSize on INCREASE_SIZE");
197 LYXERR0("Can't FontInfo::incSize on DECREASE_SIZE");
200 LYXERR0("Can't FontInfo::incSize on INHERIT_SIZE");
203 LYXERR0("Can't FontInfo::incSize on IGNORE_SIZE");
210 double FontInfo::realSize() const
212 double d = convert<double>(lyxrc.font_sizes[size()]);
213 // The following is according to the average of the values in the
214 // definitions of \defaultscriptratio and \defaultscriptscriptratio in LaTeX
215 // font packages. No attempt is made to implement the actual values from
226 case SCRIPTSCRIPT_STYLE:
230 // Never go below the smallest size
231 return max(d, convert<double>(lyxrc.font_sizes[TINY_SIZE]));
235 /// Reduce font to fall back to template where possible
236 void FontInfo::reduce(FontInfo const & tmplt)
238 if (family_ == tmplt.family_)
239 family_ = INHERIT_FAMILY;
240 if (series_ == tmplt.series_)
241 series_ = INHERIT_SERIES;
242 if (shape_ == tmplt.shape_)
243 shape_ = INHERIT_SHAPE;
244 if (size_ == tmplt.size_)
245 size_ = INHERIT_SIZE;
246 if (style_ == tmplt.style_)
247 style_ = INHERIT_STYLE;
248 if (emph_ == tmplt.emph_)
249 emph_ = FONT_INHERIT;
250 if (underbar_ == tmplt.underbar_)
251 underbar_ = FONT_INHERIT;
252 if (strikeout_ == tmplt.strikeout_)
253 strikeout_ = FONT_INHERIT;
254 if (xout_ == tmplt.xout_)
255 xout_ = FONT_INHERIT;
256 if (uuline_ == tmplt.uuline_)
257 uuline_ = FONT_INHERIT;
258 if (uwave_ == tmplt.uwave_)
259 uwave_ = FONT_INHERIT;
260 if (noun_ == tmplt.noun_)
261 noun_ = FONT_INHERIT;
262 if (color_ == tmplt.color_)
263 color_ = Color_inherit;
264 if (background_ == tmplt.background_)
265 background_ = Color_inherit;
266 if (nospellcheck_ == tmplt.nospellcheck_)
267 nospellcheck_ = FONT_INHERIT;
271 /// Realize font from a template
272 FontInfo & FontInfo::realize(FontInfo const & tmplt)
274 if ((*this) == inherit_font) {
279 if (family_ == INHERIT_FAMILY)
280 family_ = tmplt.family_;
282 if (series_ == INHERIT_SERIES)
283 series_ = tmplt.series_;
285 if (shape_ == INHERIT_SHAPE)
286 shape_ = tmplt.shape_;
288 if (size_ == INHERIT_SIZE)
291 if (style_ == INHERIT_STYLE)
292 style_ = tmplt.style_;
294 if (emph_ == FONT_INHERIT)
297 if (underbar_ == FONT_INHERIT)
298 underbar_ = tmplt.underbar_;
300 if (strikeout_ == FONT_INHERIT)
301 strikeout_ = tmplt.strikeout_;
303 if (xout_ == FONT_INHERIT)
306 if (uuline_ == FONT_INHERIT)
307 uuline_ = tmplt.uuline_;
309 if (uwave_ == FONT_INHERIT)
310 uwave_ = tmplt.uwave_;
312 if (noun_ == FONT_INHERIT)
315 if (color_ == Color_inherit)
316 color_ = tmplt.color_;
318 if (background_ == Color_inherit)
319 background_ = tmplt.background_;
321 if (nospellcheck_ == FONT_INHERIT)
322 nospellcheck_ = tmplt.nospellcheck_;
328 Changer FontInfo::changeColor(ColorCode const color)
330 return make_change(color_, color);
334 Changer FontInfo::changeShape(FontShape const shape)
336 return make_change(shape_, shape);
340 Changer FontInfo::changeStyle(MathStyle const new_style)
342 return make_change(style_, new_style);
346 Changer FontInfo::change(FontInfo font, bool realiz)
350 return make_change(*this, font);
354 /// Updates a misc setting according to request
355 static FontState setMisc(FontState newfont,
358 if (newfont == FONT_TOGGLE) {
361 else if (org == FONT_OFF)
364 LYXERR0("Font::setMisc: Need state"
365 " FONT_ON or FONT_OFF to toggle. Setting to FONT_ON");
368 } else if (newfont == FONT_IGNORE)
374 /// Updates font settings according to request
375 void FontInfo::update(FontInfo const & newfont, bool toggleall)
377 if (newfont.family_ == family_ && toggleall)
378 setFamily(INHERIT_FAMILY); // toggle 'back'
379 else if (newfont.family_ != IGNORE_FAMILY)
380 setFamily(newfont.family_);
381 // else it's IGNORE_SHAPE
383 // "Old" behaviour: "Setting" bold will toggle bold on/off.
384 switch (newfont.series_) {
387 if (series_ == BOLD_SERIES && toggleall)
388 setSeries(MEDIUM_SERIES);
390 setSeries(BOLD_SERIES);
394 setSeries(newfont.series_);
400 if (newfont.shape_ == shape_ && toggleall)
401 shape_ = INHERIT_SHAPE; // toggle 'back'
402 else if (newfont.shape_ != IGNORE_SHAPE)
403 shape_ = newfont.shape_;
404 // else it's IGNORE_SHAPE
406 if (newfont.size_ != IGNORE_SIZE) {
407 if (newfont.size_ == INCREASE_SIZE)
409 else if (newfont.size_ == DECREASE_SIZE)
412 size_ = newfont.size_;
415 if (newfont.style_ != IGNORE_STYLE) {
416 style_ = newfont.style_;
419 setEmph(setMisc(newfont.emph_, emph_));
420 setUnderbar(setMisc(newfont.underbar_, underbar_));
421 setStrikeout(setMisc(newfont.strikeout_, strikeout_));
422 setXout(setMisc(newfont.xout_, xout_));
423 setUuline(setMisc(newfont.uuline_, uuline_));
424 setUwave(setMisc(newfont.uwave_, uwave_));
425 setNoun(setMisc(newfont.noun_, noun_));
426 setNumber(setMisc(newfont.number_, number_));
427 setNoSpellcheck(setMisc(newfont.nospellcheck_, nospellcheck_));
429 if (newfont.color_ == color_ && toggleall)
430 setColor(Color_inherit); // toggle 'back'
431 else if (newfont.color_ != Color_ignore)
432 setColor(newfont.color_);
434 if (newfont.background_ == background_ && toggleall)
435 setBackground(Color_inherit); // toggle 'back'
436 else if (newfont.background_ != Color_ignore)
437 setBackground(newfont.background_);
440 /// Is font resolved?
441 bool FontInfo::resolved() const
443 return (family_ != INHERIT_FAMILY && series_ != INHERIT_SERIES
444 && shape_ != INHERIT_SHAPE && size_ != INHERIT_SIZE
445 && style_ != INHERIT_STYLE
446 && emph_ != FONT_INHERIT && underbar_ != FONT_INHERIT
447 && uuline_ != FONT_INHERIT && uwave_ != FONT_INHERIT
448 && strikeout_ != FONT_INHERIT && xout_ != FONT_INHERIT
449 && noun_ != FONT_INHERIT && color_ != Color_inherit
450 && background_ != Color_inherit && nospellcheck_ != FONT_INHERIT);
454 Color FontInfo::realColor() const
456 if (paint_color_ != Color_none)
458 if (color_ == Color_none)
459 return Color_foreground;
466 void appendSep(string & s1, string const & s2)
470 s1 += s1.empty() ? "" : "\n";
475 string makeCSSTag(string const & key, string const & val)
477 return key + ": " + val + ";";
481 string getFamilyCSS(FontFamily const & f)
488 case TYPEWRITER_FAMILY:
511 string getSeriesCSS(FontSeries const & s)
526 string getShapeCSS(FontShape const & s)
528 string fs = "normal";
529 string fv = "normal";
531 case UP_SHAPE: break;
532 case ITALIC_SHAPE: fs = "italic"; break;
533 case SLANTED_SHAPE: fs = "oblique"; break;
534 case SMALLCAPS_SHAPE: fv = "small-caps"; break;
537 fs = ""; fv = ""; break;
541 appendSep(retval, makeCSSTag("font-style", fs));
543 appendSep(retval, makeCSSTag("font-variant", fv));
548 string getSizeCSS(FontSize const & s)
582 // FIXME This does not yet handle color
583 docstring FontInfo::asCSS() const
586 string tmp = getFamilyCSS(family_);
588 appendSep(retval, makeCSSTag("font-family", tmp));
589 tmp = getSeriesCSS(series_);
591 appendSep(retval, makeCSSTag("font-weight", tmp));
592 appendSep(retval, getShapeCSS(shape_));
593 tmp = getSizeCSS(size_);
595 appendSep(retval, makeCSSTag("font-size", tmp));
596 return from_ascii(retval);
600 docstring const FontInfo::stateText(bool const terse) const
603 if (family() != INHERIT_FAMILY && (!terse || family() != IGNORE_FAMILY))
604 os << _(GUIFamilyNames[family()]) << ", ";
605 if (series() != INHERIT_SERIES && (!terse || series() != IGNORE_SERIES))
606 os << _(GUISeriesNames[series()]) << ", ";
607 if (shape() != INHERIT_SHAPE && (!terse || shape() != IGNORE_SHAPE))
608 os << _(GUIShapeNames[shape()]) << ", ";
609 if (size() != INHERIT_SIZE && (!terse || size() != IGNORE_SIZE))
610 os << _(GUISizeNames[size()]) << ", ";
611 // FIXME: shall style be handled there? Probably not.
612 if (color() != Color_inherit && (!terse || color() != Color_ignore))
613 os << lcolor.getGUIName(color()) << ", ";
614 // FIXME: uncomment this when we support background.
615 //if (background() != Color_inherit)
616 // os << lcolor.getGUIName(background()) << ", ";
617 if (emph() != FONT_INHERIT && (!terse || emph() != FONT_IGNORE))
618 os << bformat(_("Emphasis %1$s, "),
619 _(GUIMiscNames[emph()]));
620 if (underbar() != FONT_INHERIT && (!terse || underbar() == FONT_ON))
621 os << bformat(_("Underline %1$s, "),
622 _(GUIMiscNames[underbar()]));
623 if (uuline() != FONT_INHERIT && (!terse || uuline() == FONT_ON))
624 os << bformat(_("Double underline %1$s, "),
625 _(GUIMiscNames[uuline()]));
626 if (uwave() != FONT_INHERIT && (!terse || uwave() == FONT_ON))
627 os << bformat(_("Wavy underline %1$s, "),
628 _(GUIMiscNames[uwave()]));
629 if (strikeout() != FONT_INHERIT && (!terse || strikeout() == FONT_ON))
630 os << bformat(_("Strike out %1$s, "),
631 _(GUIMiscNames[strikeout()]));
632 if (xout() != FONT_INHERIT && (!terse || xout() == FONT_ON))
633 os << bformat(_("Cross out %1$s, "),
634 _(GUIMiscNames[xout()]));
635 if (noun() != FONT_INHERIT && (!terse || noun() != FONT_IGNORE))
636 os << bformat(_("Noun %1$s, "),
637 _(GUIMiscNames[noun()]));
638 if (*this == inherit_font)
639 os << _("Default") << ", ";
645 // Set family according to lyx format string
646 void setLyXFamily(string const & fam, FontInfo & f)
648 string const s = ascii_lowercase(fam);
651 while (LyXFamilyNames[i] != s &&
652 LyXFamilyNames[i] != string("error"))
654 if (s == LyXFamilyNames[i])
655 f.setFamily(FontFamily(i));
657 LYXERR0("Unknown family `" << s << '\'');
661 // Set series according to lyx format string
662 void setLyXSeries(string const & ser, FontInfo & f)
664 string const s = ascii_lowercase(ser);
667 while (LyXSeriesNames[i] != s &&
668 LyXSeriesNames[i] != string("error")) ++i;
669 if (s == LyXSeriesNames[i]) {
670 f.setSeries(FontSeries(i));
672 LYXERR0("Unknown series `" << s << '\'');
676 // Set shape according to lyx format string
677 void setLyXShape(string const & sha, FontInfo & f)
679 string const s = ascii_lowercase(sha);
682 while (LyXShapeNames[i] != s && LyXShapeNames[i] != string("error"))
684 if (s == LyXShapeNames[i])
685 f.setShape(FontShape(i));
687 LYXERR0("Unknown shape `" << s << '\'');
691 // Set size according to lyx format string
692 void setLyXSize(string const & siz, FontInfo & f)
694 string const s = ascii_lowercase(siz);
696 while (LyXSizeNames[i] != s && LyXSizeNames[i] != string("error"))
698 if (s == LyXSizeNames[i]) {
699 f.setSize(FontSize(i));
701 LYXERR0("Unknown size `" << s << '\'');
705 // Set size according to lyx format string
706 FontState setLyXMisc(string const & siz)
708 string const s = ascii_lowercase(siz);
710 while (LyXMiscNames[i] != s &&
711 LyXMiscNames[i] != string("error")) ++i;
712 if (s == LyXMiscNames[i])
714 LYXERR0("Unknown misc flag `" << s << '\'');
719 /// Sets color after LyX text format
720 void setLyXColor(string const & col, FontInfo & f)
722 f.setColor(lcolor.getFromLyXName(col));
726 // Read a font definition from given file in lyx format
728 FontInfo lyxRead(Lexer & lex, FontInfo const & fi)
732 bool finished = false;
733 while (!finished && lex.isOK() && !error) {
735 string const tok = ascii_lowercase(lex.getString());
739 } else if (tok == "endfont") {
741 } else if (tok == "family") {
743 string const ttok = lex.getString();
744 setLyXFamily(ttok, f);
745 } else if (tok == "series") {
747 string const ttok = lex.getString();
748 setLyXSeries(ttok, f);
749 } else if (tok == "shape") {
751 string const ttok = lex.getString();
752 setLyXShape(ttok, f);
753 } else if (tok == "size") {
755 string const ttok = lex.getString();
757 } else if (tok == "misc") {
759 string const ttok = ascii_lowercase(lex.getString());
761 if (ttok == "no_bar") {
762 f.setUnderbar(FONT_OFF);
763 } else if (ttok == "no_strikeout") {
764 f.setStrikeout(FONT_OFF);
765 } else if (ttok == "no_xout") {
767 } else if (ttok == "no_uuline") {
768 f.setUuline(FONT_OFF);
769 } else if (ttok == "no_uwave") {
770 f.setUwave(FONT_OFF);
771 } else if (ttok == "no_emph") {
773 } else if (ttok == "no_noun") {
775 } else if (ttok == "emph") {
777 } else if (ttok == "underbar") {
778 f.setUnderbar(FONT_ON);
779 } else if (ttok == "strikeout") {
780 f.setStrikeout(FONT_ON);
781 } else if (ttok == "xout") {
783 } else if (ttok == "uuline") {
784 f.setUuline(FONT_ON);
785 } else if (ttok == "uwave") {
787 } else if (ttok == "noun") {
789 } else if (ttok == "nospellcheck") {
790 f.setNoSpellcheck(FONT_ON);
791 } else if (ttok == "no_nospellcheck") {
792 f.setNoSpellcheck(FONT_OFF);
794 lex.printError("Illegal misc type");
796 } else if (tok == "color") {
798 string const ttok = lex.getString();
799 setLyXColor(ttok, f);
801 lex.printError("Unknown tag");
809 void lyxWrite(ostream & os, FontInfo const & f, string const & start, int level)
812 for (int i = 0; i < level; ++i)
815 if (f.family() != INHERIT_FAMILY)
816 oss << indent << "\tFamily " << LyXFamilyNames[f.family()]
818 if (f.series() != INHERIT_SERIES)
819 oss << indent << "\tSeries " << LyXSeriesNames[f.series()]
821 if (f.shape() != INHERIT_SHAPE)
822 oss << indent << "\tShape " << LyXShapeNames[f.shape()]
824 if (f.size() != INHERIT_SIZE)
825 oss << indent << "\tSize " << LyXSizeNames[f.size()]
827 //FIXME: shall style be handled here? Probably not.
828 if (f.underbar() == FONT_ON)
829 oss << indent << "\tMisc Underbar\n";
830 else if (f.underbar() == FONT_OFF)
831 oss << indent << "\tMisc No_Bar\n";
832 if (f.strikeout() == FONT_ON)
833 oss << indent << "\tMisc Strikeout\n";
834 else if (f.strikeout() == FONT_OFF)
835 oss << indent << "\tMisc No_Strikeout\n";
836 if (f.xout() == FONT_ON)
837 oss << indent << "\tMisc Xout\n";
838 else if (f.xout() == FONT_OFF)
839 oss << indent << "\tMisc No_Xout\n";
840 if (f.uuline() == FONT_ON)
841 oss << indent << "\tMisc Uuline\n";
842 else if (f.uuline() == FONT_OFF)
843 oss << indent << "\tMisc No_Uuline\n";
844 if (f.uwave() == FONT_ON)
845 oss << indent << "\tMisc Uwave\n";
846 else if (f.uwave() == FONT_OFF)
847 oss << indent << "\tMisc No_Uwave\n";
848 if (f.emph() == FONT_ON)
849 oss << indent << "\tMisc Emph\n";
850 else if (f.emph() == FONT_OFF)
851 oss << indent << "\tMisc No_Emph\n";
852 if (f.noun() == FONT_ON)
853 oss << indent << "\tMisc Noun\n";
854 else if (f.noun() == FONT_OFF)
855 oss << indent << "\tMisc No_Noun\n";
856 if (f.nospellcheck() == FONT_ON)
857 oss << indent << "\tMisc NoSpellcheck\n";
858 else if (f.nospellcheck() == FONT_OFF)
859 oss << indent << "\tMisc No_NoSpellcheck\n";
860 if (f.color() != Color_inherit && f.color() != Color_none)
861 oss << indent << "\tColor " << lcolor.getLyXName(f.color())
863 if (!oss.str().empty()) {
864 os << indent << start << '\n'
866 << indent << "EndFont\n";