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.
19 #include "bufferparams.h" // stateText
27 #include "support/lstrings.h"
28 #include "support/std_sstream.h"
30 using lyx::support::ascii_lowercase;
31 using lyx::support::bformat;
32 using lyx::support::rtrim;
33 using lyx::support::subst;
38 using std::ostringstream;
40 #ifndef CXX_GLOBAL_CSTD
50 char const * GUIFamilyNames[LyXFont::NUM_FAMILIES + 2 /* default & error */] =
51 { N_("Roman"), N_("Sans Serif"), N_("Typewriter"), N_("Symbol"),
52 "cmr", "cmsy", "cmm", "cmex", "msa", "msb", "eufrak", "wasy",
53 N_("Inherit"), N_("Ignore") };
55 char const * GUISeriesNames[4] =
56 { N_("Medium"), N_("Bold"), N_("Inherit"), N_("Ignore") };
58 char const * GUIShapeNames[6] =
59 { N_("Upright"), N_("Italic"), N_("Slanted"), N_("Smallcaps"), N_("Inherit"),
62 char const * GUISizeNames[14] =
63 { N_("Tiny"), N_("Smallest"), N_("Smaller"), N_("Small"), N_("Normal"), N_("Large"),
64 N_("Larger"), N_("Largest"), N_("Huge"), N_("Huger"), N_("Increase"), N_("Decrease"),
65 N_("Inherit"), N_("Ignore") };
67 char const * GUIMiscNames[5] =
68 { N_("Off"), N_("On"), N_("Toggle"), N_("Inherit"), N_("Ignore") };
72 // Strings used to read and write .lyx format files
74 char const * LyXFamilyNames[LyXFont::NUM_FAMILIES + 2 /* default & error */] =
75 { "roman", "sans", "typewriter", "symbol",
76 "cmr", "cmsy", "cmm", "cmex", "msa", "msb", "eufrak", "wasy",
79 char const * LyXSeriesNames[4] =
80 { "medium", "bold", "default", "error" };
82 char const * LyXShapeNames[6] =
83 { "up", "italic", "slanted", "smallcaps", "default", "error" };
85 char const * LyXSizeNames[14] =
86 { "tiny", "scriptsize", "footnotesize", "small", "normal", "large",
87 "larger", "largest", "huge", "giant",
88 "increase", "decrease", "default", "error" };
90 char const * LyXMiscNames[5] =
91 { "off", "on", "toggle", "default", "error" };
94 // Strings used to write LaTeX files
96 char const * LaTeXFamilyNames[6] =
97 { "textrm", "textsf", "texttt", "error1", "error2", "error3" };
99 char const * LaTeXSeriesNames[4] =
100 { "textmd", "textbf", "error4", "error5" };
102 char const * LaTeXShapeNames[6] =
103 { "textup", "textit", "textsl", "textsc", "error6", "error7" };
105 char const * LaTeXSizeNames[14] =
106 { "tiny", "scriptsize", "footnotesize", "small", "normalsize", "large",
107 "Large", "LARGE", "huge", "Huge", "error8", "error9", "error10", "error11" };
112 // Initialize static member
113 LyXFont::FontBits LyXFont::sane = {
124 // Initialize static member
125 LyXFont::FontBits LyXFont::inherit = {
136 // Initialize static member
137 LyXFont::FontBits LyXFont::ignore = {
149 bool operator==(LyXFont::FontBits const & lhs,
150 LyXFont::FontBits const & rhs)
152 return lhs.family == rhs.family &&
153 lhs.series == rhs.series &&
154 lhs.shape == rhs.shape &&
155 lhs.size == rhs.size &&
156 lhs.color == rhs.color &&
157 lhs.emph == rhs.emph &&
158 lhs.underbar == rhs.underbar &&
159 lhs.noun == rhs.noun &&
160 lhs.number == rhs.number;
165 : bits(sane), lang(default_language)
169 LyXFont::LyXFont(LyXFont::FONT_INIT1)
170 : bits(inherit), lang(default_language)
174 LyXFont::LyXFont(LyXFont::FONT_INIT2)
175 : bits(ignore), lang(ignore_language)
179 LyXFont::LyXFont(LyXFont::FONT_INIT3)
180 : bits(sane), lang(default_language)
184 LyXFont::LyXFont(LyXFont::FONT_INIT1, Language const * l)
185 : bits(inherit), lang(l)
189 LyXFont::LyXFont(LyXFont::FONT_INIT2, Language const * l)
190 : bits(ignore), lang(l)
194 LyXFont::LyXFont(LyXFont::FONT_INIT3, Language const * l)
195 : bits(sane), lang(l)
199 LyXFont::FONT_MISC_STATE LyXFont::underbar() const
201 return bits.underbar;
205 LColor_color LyXFont::color() const
207 return LColor::color(bits.color);
211 Language const * LyXFont::language() const
217 LyXFont::FONT_MISC_STATE LyXFont::number() const
223 bool LyXFont::isRightToLeft() const
225 return lang->RightToLeft();
229 bool LyXFont::isVisibleRightToLeft() const
231 return (lang->RightToLeft() &&
236 void LyXFont::setFamily(LyXFont::FONT_FAMILY f)
242 void LyXFont::setSeries(LyXFont::FONT_SERIES s)
248 void LyXFont::setShape(LyXFont::FONT_SHAPE s)
254 void LyXFont::setSize(LyXFont::FONT_SIZE s)
260 void LyXFont::setEmph(LyXFont::FONT_MISC_STATE e)
266 void LyXFont::setUnderbar(LyXFont::FONT_MISC_STATE u)
272 void LyXFont::setNoun(LyXFont::FONT_MISC_STATE n)
278 void LyXFont::setColor(LColor_color c)
284 void LyXFont::setLanguage(Language const * l)
290 void LyXFont::setNumber(LyXFont::FONT_MISC_STATE n)
296 /// Decreases font size by one
297 LyXFont & LyXFont::decSize()
300 case SIZE_HUGER: setSize(SIZE_HUGE); break;
301 case SIZE_HUGE: setSize(SIZE_LARGEST); break;
302 case SIZE_LARGEST: setSize(SIZE_LARGER); break;
303 case SIZE_LARGER: setSize(SIZE_LARGE); break;
304 case SIZE_LARGE: setSize(SIZE_NORMAL); break;
305 case SIZE_NORMAL: setSize(SIZE_SMALL); break;
306 case SIZE_SMALL: setSize(SIZE_FOOTNOTE); break;
307 case SIZE_FOOTNOTE: setSize(SIZE_SCRIPT); break;
308 case SIZE_SCRIPT: setSize(SIZE_TINY); break;
309 case SIZE_TINY: break;
311 lyxerr << "Can't LyXFont::decSize on INCREASE_SIZE" << endl;
314 lyxerr <<"Can't LyXFont::decSize on DECREASE_SIZE" << endl;
317 lyxerr <<"Can't LyXFont::decSize on INHERIT_SIZE" << endl;
320 lyxerr <<"Can't LyXFont::decSize on IGNORE_SIZE" << endl;
327 /// Increases font size by one
328 LyXFont & LyXFont::incSize()
331 case SIZE_HUGER: break;
332 case SIZE_HUGE: setSize(SIZE_HUGER); break;
333 case SIZE_LARGEST: setSize(SIZE_HUGE); break;
334 case SIZE_LARGER: setSize(SIZE_LARGEST); break;
335 case SIZE_LARGE: setSize(SIZE_LARGER); break;
336 case SIZE_NORMAL: setSize(SIZE_LARGE); break;
337 case SIZE_SMALL: setSize(SIZE_NORMAL); break;
338 case SIZE_FOOTNOTE: setSize(SIZE_SMALL); break;
339 case SIZE_SCRIPT: setSize(SIZE_FOOTNOTE); break;
340 case SIZE_TINY: setSize(SIZE_SCRIPT); break;
342 lyxerr <<"Can't LyXFont::incSize on INCREASE_SIZE" << endl;
345 lyxerr <<"Can't LyXFont::incSize on DECREASE_SIZE" << endl;
348 lyxerr <<"Can't LyXFont::incSize on INHERIT_SIZE" << endl;
351 lyxerr <<"Can't LyXFont::incSize on IGNORE_SIZE" << endl;
358 /// Updates a misc setting according to request
359 LyXFont::FONT_MISC_STATE LyXFont::setMisc(FONT_MISC_STATE newfont,
362 if (newfont == TOGGLE) {
368 lyxerr <<"LyXFont::setMisc: Need state"
369 " ON or OFF to toggle. Setting to ON" << endl;
372 } else if (newfont == IGNORE)
379 /// Updates font settings according to request
380 void LyXFont::update(LyXFont const & newfont,
381 Language const * document_language,
384 if (newfont.family() == family() && toggleall)
385 setFamily(INHERIT_FAMILY); // toggle 'back'
386 else if (newfont.family() != IGNORE_FAMILY)
387 setFamily(newfont.family());
388 // else it's IGNORE_SHAPE
390 // "Old" behaviour: "Setting" bold will toggle bold on/off.
391 switch (newfont.series()) {
394 if (series() == BOLD_SERIES && toggleall)
395 setSeries(MEDIUM_SERIES);
397 setSeries(BOLD_SERIES);
401 setSeries(newfont.series());
407 if (newfont.shape() == shape() && toggleall)
408 setShape(INHERIT_SHAPE); // toggle 'back'
409 else if (newfont.shape() != IGNORE_SHAPE)
410 setShape(newfont.shape());
411 // else it's IGNORE_SHAPE
413 if (newfont.size() != IGNORE_SIZE) {
414 if (newfont.size() == INCREASE_SIZE)
416 else if (newfont.size() == DECREASE_SIZE)
419 setSize(newfont.size());
422 setEmph(setMisc(newfont.emph(), emph()));
423 setUnderbar(setMisc(newfont.underbar(), underbar()));
424 setNoun(setMisc(newfont.noun(), noun()));
426 setNumber(setMisc(newfont.number(), number()));
427 if (newfont.language() == language() && toggleall)
428 if (language() == document_language)
429 setLanguage(default_language);
431 setLanguage(document_language);
432 else if (newfont.language() != ignore_language)
433 setLanguage(newfont.language());
435 if (newfont.color() == color() && toggleall)
436 setColor(LColor::inherit); // toggle 'back'
437 else if (newfont.color() != LColor::ignore)
438 setColor(newfont.color());
442 /// Reduce font to fall back to template where possible
443 void LyXFont::reduce(LyXFont const & tmplt)
445 if (family() == tmplt.family())
446 setFamily(INHERIT_FAMILY);
447 if (series() == tmplt.series())
448 setSeries(INHERIT_SERIES);
449 if (shape() == tmplt.shape())
450 setShape(INHERIT_SHAPE);
451 if (size() == tmplt.size())
452 setSize(INHERIT_SIZE);
453 if (emph() == tmplt.emph())
455 if (underbar() == tmplt.underbar())
456 setUnderbar(INHERIT);
457 if (noun() == tmplt.noun())
459 if (color() == tmplt.color())
460 setColor(LColor::inherit);
464 /// Realize font from a template
465 LyXFont & LyXFont::realize(LyXFont const & tmplt)
467 if (bits == inherit) {
472 if (bits.family == INHERIT_FAMILY) {
473 bits.family = tmplt.bits.family;
475 if (bits.series == INHERIT_SERIES) {
476 bits.series = tmplt.bits.series;
478 if (bits.shape == INHERIT_SHAPE) {
479 bits.shape = tmplt.bits.shape;
481 if (bits.size == INHERIT_SIZE) {
482 bits.size = tmplt.bits.size;
484 if (bits.emph == INHERIT) {
485 bits.emph = tmplt.bits.emph;
487 if (bits.underbar == INHERIT) {
488 bits.underbar = tmplt.bits.underbar;
490 if (bits.noun == INHERIT) {
491 bits.noun = tmplt.bits.noun;
493 if (bits.color == LColor::inherit) {
494 bits.color = tmplt.bits.color;
500 /// Is font resolved?
501 bool LyXFont::resolved() const
503 return (family() != INHERIT_FAMILY && series() != INHERIT_SERIES &&
504 shape() != INHERIT_SHAPE && size() != INHERIT_SIZE &&
505 emph() != INHERIT && underbar() != INHERIT &&
507 color() != LColor::inherit);
511 /// Build GUI description of font state
512 string const LyXFont::stateText(BufferParams * params) const
515 if (family() != INHERIT_FAMILY)
516 os << _(GUIFamilyNames[family()]) << ", ";
517 if (series() != INHERIT_SERIES)
518 os << _(GUISeriesNames[series()]) << ", ";
519 if (shape() != INHERIT_SHAPE)
520 os << _(GUIShapeNames[shape()]) << ", ";
521 if (size() != INHERIT_SIZE)
522 os << _(GUISizeNames[size()]) << ", ";
523 if (color() != LColor::inherit)
524 os << lcolor.getGUIName(color()) << ", ";
525 if (emph() != INHERIT)
526 os << bformat(_("Emphasis %1$s, "), _(GUIMiscNames[emph()]));
527 if (underbar() != INHERIT)
528 os << bformat(_("Underline %1$s, "), _(GUIMiscNames[underbar()]));
529 if (noun() != INHERIT)
530 os << bformat(_("Noun %1$s, "), _(GUIMiscNames[noun()]));
532 os << _("Default") << ", ";
533 if (!params || (language() != params->language))
534 os << bformat(_("Language: %1$s, "), _(language()->display()));
536 os << bformat(_(" Number %1$s"), _(GUIMiscNames[number()]));
537 return rtrim(os.str(), ", ");
541 // Set family according to lyx format string
542 LyXFont & LyXFont::setLyXFamily(string const & fam)
544 string const s = ascii_lowercase(fam);
547 while (s != LyXFamilyNames[i] && LyXFamilyNames[i] != "error")
549 if (s == LyXFamilyNames[i])
550 setFamily(LyXFont::FONT_FAMILY(i));
552 lyxerr << "LyXFont::setLyXFamily: Unknown family `"
553 << s << '\'' << endl;
558 // Set series according to lyx format string
559 LyXFont & LyXFont::setLyXSeries(string const & ser)
561 string const s = ascii_lowercase(ser);
564 while (s != LyXSeriesNames[i] && LyXSeriesNames[i] != "error") ++i;
565 if (s == LyXSeriesNames[i]) {
566 setSeries(LyXFont::FONT_SERIES(i));
568 lyxerr << "LyXFont::setLyXSeries: Unknown series `"
569 << s << '\'' << endl;
574 // Set shape according to lyx format string
575 LyXFont & LyXFont::setLyXShape(string const & sha)
577 string const s = ascii_lowercase(sha);
580 while (s != LyXShapeNames[i] && LyXShapeNames[i] != "error") ++i;
581 if (s == LyXShapeNames[i]) {
582 setShape(LyXFont::FONT_SHAPE(i));
584 lyxerr << "LyXFont::setLyXShape: Unknown shape `"
585 << s << '\'' << endl;
590 // Set size according to lyx format string
591 LyXFont & LyXFont::setLyXSize(string const & siz)
593 string const s = ascii_lowercase(siz);
595 while (s != LyXSizeNames[i] && LyXSizeNames[i] != "error") ++i;
596 if (s == LyXSizeNames[i]) {
597 setSize(LyXFont::FONT_SIZE(i));
599 lyxerr << "LyXFont::setLyXSize: Unknown size `"
600 << s << '\'' << endl;
605 // Set size according to lyx format string
606 LyXFont::FONT_MISC_STATE LyXFont::setLyXMisc(string const & siz)
608 string const s = ascii_lowercase(siz);
610 while (s != LyXMiscNames[i] && LyXMiscNames[i] != "error") ++i;
611 if (s == LyXMiscNames[i])
612 return FONT_MISC_STATE(i);
613 lyxerr << "LyXFont::setLyXMisc: Unknown misc flag `"
614 << s << '\'' << endl;
619 /// Sets color after LyX text format
620 LyXFont & LyXFont::setLyXColor(string const & col)
622 setColor(lcolor.getFromLyXName(col));
627 // Returns size in latex format
628 string const LyXFont::latexSize() const
630 return LaTeXSizeNames[size()];
634 // Read a font definition from given file in lyx format
636 LyXFont & LyXFont::lyxRead(LyXLex & lex)
639 bool finished = false;
640 while (!finished && lex.isOK() && !error) {
642 string const tok = ascii_lowercase(lex.getString());
646 } else if (tok == "endfont") {
648 } else if (tok == "family") {
650 string const ttok = lex.getString();
652 } else if (tok == "series") {
654 string const ttok = lex.getString();
656 } else if (tok == "shape") {
658 string const ttok = lex.getString();
660 } else if (tok == "size") {
662 string const ttok = lex.getString();
664 } else if (tok == "misc") {
666 string const ttok = ascii_lowercase(lex.getString());
668 if (ttok == "no_bar") {
670 } else if (ttok == "no_emph") {
672 } else if (ttok == "no_noun") {
674 } else if (ttok == "emph") {
676 } else if (ttok == "underbar") {
678 } else if (ttok == "noun") {
681 lex.printError("Illegal misc type `$$Token´");
683 } else if (tok == "color") {
685 string const ttok = lex.getString();
688 lex.printError("Unknown tag `$$Token'");
696 /// Writes the changes from this font to orgfont in .lyx format in file
697 void LyXFont::lyxWriteChanges(LyXFont const & orgfont,
701 if (orgfont.family() != family()) {
702 os << "\\family " << LyXFamilyNames[family()] << " \n";
704 if (orgfont.series() != series()) {
705 os << "\\series " << LyXSeriesNames[series()] << " \n";
707 if (orgfont.shape() != shape()) {
708 os << "\\shape " << LyXShapeNames[shape()] << " \n";
710 if (orgfont.size() != size()) {
711 os << "\\size " << LyXSizeNames[size()] << " \n";
713 if (orgfont.emph() != emph()) {
714 os << "\\emph " << LyXMiscNames[emph()] << " \n";
716 if (orgfont.number() != number()) {
717 os << "\\numeric " << LyXMiscNames[number()] << " \n";
719 if (orgfont.underbar() != underbar()) {
720 // This is only for backwards compatibility
721 switch (underbar()) {
722 case OFF: os << "\\bar no \n"; break;
723 case ON: os << "\\bar under \n"; break;
724 case TOGGLE: lyxerr << "LyXFont::lyxWriteFontChanges: "
725 "TOGGLE should not appear here!"
728 case INHERIT: os << "\\bar default \n"; break;
729 case IGNORE: lyxerr << "LyXFont::lyxWriteFontChanges: "
730 "IGNORE should not appear here!"
735 if (orgfont.noun() != noun()) {
736 os << "\\noun " << LyXMiscNames[noun()] << " \n";
738 if (orgfont.color() != color()) {
739 // To make us file compatible with older
740 // lyx versions we emit "default" instead
742 string col_str(lcolor.getLyXName(color()));
743 if (col_str == "inherit") col_str = "default";
744 os << "\\color " << col_str << "\n";
746 if (orgfont.language() != language()) {
748 os << "\\lang " << language()->lang() << "\n";
750 os << "\\lang unknown\n";
755 /// Writes the head of the LaTeX needed to impose this font
756 // Returns number of chars written.
757 int LyXFont::latexWriteStartChanges(ostream & os, LyXFont const & base,
758 LyXFont const & prev) const
763 if (language()->babel() != base.language()->babel() &&
764 language() != prev.language()) {
765 if (isRightToLeft() != prev.isRightToLeft()) {
766 if (isRightToLeft()) {
775 subst(lyxrc.language_command_local,
776 "$$lang", language()->babel());
778 count += tmp.length();
782 if (number() == ON && prev.number() != ON &&
783 language()->lang() == "hebrew") {
791 if (f.family() != INHERIT_FAMILY) {
793 << LaTeXFamilyNames[f.family()]
795 count += strlen(LaTeXFamilyNames[f.family()]) + 2;
796 env = true; //We have opened a new environment
798 if (f.series() != INHERIT_SERIES) {
800 << LaTeXSeriesNames[f.series()]
802 count += strlen(LaTeXSeriesNames[f.series()]) + 2;
803 env = true; //We have opened a new environment
805 if (f.shape() != INHERIT_SHAPE) {
807 << LaTeXShapeNames[f.shape()]
809 count += strlen(LaTeXShapeNames[f.shape()]) + 2;
810 env = true; //We have opened a new environment
812 if (f.color() != LColor::inherit && f.color() != LColor::ignore) {
814 << lcolor.getLaTeXName(f.color())
816 count += lcolor.getLaTeXName(f.color()).length() + 13;
817 env = true; //We have opened a new environment
819 if (f.emph() == ON) {
822 env = true; //We have opened a new environment
824 if (f.underbar() == ON) {
827 env = true; //We have opened a new environment
829 // \noun{} is a LyX special macro
830 if (f.noun() == ON) {
833 env = true; //We have opened a new environment
835 if (f.size() != INHERIT_SIZE) {
836 // If we didn't open an environment above, we open one here
842 << LaTeXSizeNames[f.size()]
844 count += strlen(LaTeXSizeNames[f.size()]) + 2;
850 /// Writes ending block of LaTeX needed to close use of this font
851 // Returns number of chars written
852 // This one corresponds to latexWriteStartChanges(). (Asger)
853 int LyXFont::latexWriteEndChanges(ostream & os, LyXFont const & base,
854 LyXFont const & next) const
859 // reduce the current font to changes against the base
860 // font (of the layout). We use a temporary for this to
861 // avoid changing this font instance, as that would break
865 if (f.family() != INHERIT_FAMILY) {
868 env = true; // Size change need not bother about closing env.
870 if (f.series() != INHERIT_SERIES) {
873 env = true; // Size change need not bother about closing env.
875 if (f.shape() != INHERIT_SHAPE) {
878 env = true; // Size change need not bother about closing env.
880 if (f.color() != LColor::inherit && f.color() != LColor::ignore) {
883 env = true; // Size change need not bother about closing env.
885 if (f.emph() == ON) {
888 env = true; // Size change need not bother about closing env.
890 if (f.underbar() == ON) {
893 env = true; // Size change need not bother about closing env.
895 if (f.noun() == ON) {
898 env = true; // Size change need not bother about closing env.
900 if (f.size() != INHERIT_SIZE) {
901 // We only have to close if only size changed
908 if (number() == ON && next.number() != ON &&
909 language()->lang() == "hebrew") {
914 if (language() != base.language() && language() != next.language()) {
923 LColor_color LyXFont::realColor() const
925 if (color() == LColor::none)
926 return LColor::foreground;
931 // Convert logical attributes to concrete shape attribute
932 LyXFont::FONT_SHAPE LyXFont::realShape() const
934 register FONT_SHAPE s = shape();
948 ostream & operator<<(ostream & os, LyXFont::FONT_MISC_STATE fms)
950 return os << int(fms);
954 std::ostream & operator<<(std::ostream & os, LyXFont const & font)
957 << " family " << font.bits.family
958 << " series " << font.bits.series
959 << " shape " << font.bits.shape
960 << " size " << font.bits.size
961 << " color " << font.bits.color
962 << " emph " << font.bits.emph
963 << " underbar " << font.bits.underbar
964 << " noun " << font.bits.noun
965 << " number " << font.bits.number
966 << " lang: " << (font.lang ? font.lang->lang() : 0);