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"
31 using lyx::support::ascii_lowercase;
32 using lyx::support::bformat;
33 using lyx::support::rtrim;
34 using lyx::support::subst;
39 using std::ostringstream;
41 #ifndef CXX_GLOBAL_CSTD
51 char const * GUIFamilyNames[LyXFont::NUM_FAMILIES + 2 /* default & error */] =
52 { N_("Roman"), N_("Sans Serif"), N_("Typewriter"), N_("Symbol"),
53 "cmr", "cmsy", "cmm", "cmex", "msa", "msb", "eufrak", "wasy",
54 N_("Inherit"), N_("Ignore") };
56 char const * GUISeriesNames[4] =
57 { N_("Medium"), N_("Bold"), N_("Inherit"), N_("Ignore") };
59 char const * GUIShapeNames[6] =
60 { N_("Upright"), N_("Italic"), N_("Slanted"), N_("Smallcaps"), N_("Inherit"),
63 char const * GUISizeNames[14] =
64 { N_("Tiny"), N_("Smallest"), N_("Smaller"), N_("Small"), N_("Normal"), N_("Large"),
65 N_("Larger"), N_("Largest"), N_("Huge"), N_("Huger"), N_("Increase"), N_("Decrease"),
66 N_("Inherit"), N_("Ignore") };
68 char const * GUIMiscNames[5] =
69 { N_("Off"), N_("On"), N_("Toggle"), N_("Inherit"), N_("Ignore") };
73 // Strings used to read and write .lyx format files
75 char const * LyXFamilyNames[LyXFont::NUM_FAMILIES + 2 /* default & error */] =
76 { "roman", "sans", "typewriter", "symbol",
77 "cmr", "cmsy", "cmm", "cmex", "msa", "msb", "eufrak", "wasy",
80 char const * LyXSeriesNames[4] =
81 { "medium", "bold", "default", "error" };
83 char const * LyXShapeNames[6] =
84 { "up", "italic", "slanted", "smallcaps", "default", "error" };
86 char const * LyXSizeNames[14] =
87 { "tiny", "scriptsize", "footnotesize", "small", "normal", "large",
88 "larger", "largest", "huge", "giant",
89 "increase", "decrease", "default", "error" };
91 char const * LyXMiscNames[5] =
92 { "off", "on", "toggle", "default", "error" };
95 // Strings used to write LaTeX files
97 char const * LaTeXFamilyNames[6] =
98 { "textrm", "textsf", "texttt", "error1", "error2", "error3" };
100 char const * LaTeXSeriesNames[4] =
101 { "textmd", "textbf", "error4", "error5" };
103 char const * LaTeXShapeNames[6] =
104 { "textup", "textit", "textsl", "textsc", "error6", "error7" };
106 char const * LaTeXSizeNames[14] =
107 { "tiny", "scriptsize", "footnotesize", "small", "normalsize", "large",
108 "Large", "LARGE", "huge", "Huge", "error8", "error9", "error10", "error11" };
113 // Initialize static member
114 LyXFont::FontBits LyXFont::sane = {
125 // Initialize static member
126 LyXFont::FontBits LyXFont::inherit = {
137 // Initialize static member
138 LyXFont::FontBits LyXFont::ignore = {
150 bool operator==(LyXFont::FontBits const & lhs,
151 LyXFont::FontBits const & rhs)
153 return lhs.family == rhs.family &&
154 lhs.series == rhs.series &&
155 lhs.shape == rhs.shape &&
156 lhs.size == rhs.size &&
157 lhs.color == rhs.color &&
158 lhs.emph == rhs.emph &&
159 lhs.underbar == rhs.underbar &&
160 lhs.noun == rhs.noun &&
161 lhs.number == rhs.number;
166 : bits(sane), lang(default_language)
170 LyXFont::LyXFont(LyXFont::FONT_INIT1)
171 : bits(inherit), lang(default_language)
175 LyXFont::LyXFont(LyXFont::FONT_INIT2)
176 : bits(ignore), lang(ignore_language)
180 LyXFont::LyXFont(LyXFont::FONT_INIT3)
181 : bits(sane), lang(default_language)
185 LyXFont::LyXFont(LyXFont::FONT_INIT1, Language const * l)
186 : bits(inherit), lang(l)
190 LyXFont::LyXFont(LyXFont::FONT_INIT2, Language const * l)
191 : bits(ignore), lang(l)
195 LyXFont::LyXFont(LyXFont::FONT_INIT3, Language const * l)
196 : bits(sane), lang(l)
200 LyXFont::FONT_MISC_STATE LyXFont::underbar() const
202 return bits.underbar;
206 LColor_color LyXFont::color() const
208 return LColor::color(bits.color);
212 Language const * LyXFont::language() const
218 LyXFont::FONT_MISC_STATE LyXFont::number() const
224 bool LyXFont::isRightToLeft() const
226 return lang->RightToLeft();
230 bool LyXFont::isVisibleRightToLeft() const
232 return (lang->RightToLeft() &&
237 void LyXFont::setFamily(LyXFont::FONT_FAMILY f)
243 void LyXFont::setSeries(LyXFont::FONT_SERIES s)
249 void LyXFont::setShape(LyXFont::FONT_SHAPE s)
255 void LyXFont::setSize(LyXFont::FONT_SIZE s)
261 void LyXFont::setEmph(LyXFont::FONT_MISC_STATE e)
267 void LyXFont::setUnderbar(LyXFont::FONT_MISC_STATE u)
273 void LyXFont::setNoun(LyXFont::FONT_MISC_STATE n)
279 void LyXFont::setColor(LColor_color c)
285 void LyXFont::setLanguage(Language const * l)
291 void LyXFont::setNumber(LyXFont::FONT_MISC_STATE n)
297 /// Decreases font size by one
298 LyXFont & LyXFont::decSize()
301 case SIZE_HUGER: setSize(SIZE_HUGE); break;
302 case SIZE_HUGE: setSize(SIZE_LARGEST); break;
303 case SIZE_LARGEST: setSize(SIZE_LARGER); break;
304 case SIZE_LARGER: setSize(SIZE_LARGE); break;
305 case SIZE_LARGE: setSize(SIZE_NORMAL); break;
306 case SIZE_NORMAL: setSize(SIZE_SMALL); break;
307 case SIZE_SMALL: setSize(SIZE_FOOTNOTE); break;
308 case SIZE_FOOTNOTE: setSize(SIZE_SCRIPT); break;
309 case SIZE_SCRIPT: setSize(SIZE_TINY); break;
310 case SIZE_TINY: break;
312 lyxerr << "Can't LyXFont::decSize on INCREASE_SIZE" << endl;
315 lyxerr <<"Can't LyXFont::decSize on DECREASE_SIZE" << endl;
318 lyxerr <<"Can't LyXFont::decSize on INHERIT_SIZE" << endl;
321 lyxerr <<"Can't LyXFont::decSize on IGNORE_SIZE" << endl;
328 /// Increases font size by one
329 LyXFont & LyXFont::incSize()
332 case SIZE_HUGER: break;
333 case SIZE_HUGE: setSize(SIZE_HUGER); break;
334 case SIZE_LARGEST: setSize(SIZE_HUGE); break;
335 case SIZE_LARGER: setSize(SIZE_LARGEST); break;
336 case SIZE_LARGE: setSize(SIZE_LARGER); break;
337 case SIZE_NORMAL: setSize(SIZE_LARGE); break;
338 case SIZE_SMALL: setSize(SIZE_NORMAL); break;
339 case SIZE_FOOTNOTE: setSize(SIZE_SMALL); break;
340 case SIZE_SCRIPT: setSize(SIZE_FOOTNOTE); break;
341 case SIZE_TINY: setSize(SIZE_SCRIPT); break;
343 lyxerr <<"Can't LyXFont::incSize on INCREASE_SIZE" << endl;
346 lyxerr <<"Can't LyXFont::incSize on DECREASE_SIZE" << endl;
349 lyxerr <<"Can't LyXFont::incSize on INHERIT_SIZE" << endl;
352 lyxerr <<"Can't LyXFont::incSize on IGNORE_SIZE" << endl;
359 /// Updates a misc setting according to request
360 LyXFont::FONT_MISC_STATE LyXFont::setMisc(FONT_MISC_STATE newfont,
363 if (newfont == TOGGLE) {
369 lyxerr <<"LyXFont::setMisc: Need state"
370 " ON or OFF to toggle. Setting to ON" << endl;
373 } else if (newfont == IGNORE)
380 /// Updates font settings according to request
381 void LyXFont::update(LyXFont const & newfont,
382 Language const * document_language,
385 if (newfont.family() == family() && toggleall)
386 setFamily(INHERIT_FAMILY); // toggle 'back'
387 else if (newfont.family() != IGNORE_FAMILY)
388 setFamily(newfont.family());
389 // else it's IGNORE_SHAPE
391 // "Old" behaviour: "Setting" bold will toggle bold on/off.
392 switch (newfont.series()) {
395 if (series() == BOLD_SERIES && toggleall)
396 setSeries(MEDIUM_SERIES);
398 setSeries(BOLD_SERIES);
402 setSeries(newfont.series());
408 if (newfont.shape() == shape() && toggleall)
409 setShape(INHERIT_SHAPE); // toggle 'back'
410 else if (newfont.shape() != IGNORE_SHAPE)
411 setShape(newfont.shape());
412 // else it's IGNORE_SHAPE
414 if (newfont.size() != IGNORE_SIZE) {
415 if (newfont.size() == INCREASE_SIZE)
417 else if (newfont.size() == DECREASE_SIZE)
420 setSize(newfont.size());
423 setEmph(setMisc(newfont.emph(), emph()));
424 setUnderbar(setMisc(newfont.underbar(), underbar()));
425 setNoun(setMisc(newfont.noun(), noun()));
427 setNumber(setMisc(newfont.number(), number()));
428 if (newfont.language() == language() && toggleall)
429 if (language() == document_language)
430 setLanguage(default_language);
432 setLanguage(document_language);
433 else if (newfont.language() != ignore_language)
434 setLanguage(newfont.language());
436 if (newfont.color() == color() && toggleall)
437 setColor(LColor::inherit); // toggle 'back'
438 else if (newfont.color() != LColor::ignore)
439 setColor(newfont.color());
443 /// Reduce font to fall back to template where possible
444 void LyXFont::reduce(LyXFont const & tmplt)
446 if (family() == tmplt.family())
447 setFamily(INHERIT_FAMILY);
448 if (series() == tmplt.series())
449 setSeries(INHERIT_SERIES);
450 if (shape() == tmplt.shape())
451 setShape(INHERIT_SHAPE);
452 if (size() == tmplt.size())
453 setSize(INHERIT_SIZE);
454 if (emph() == tmplt.emph())
456 if (underbar() == tmplt.underbar())
457 setUnderbar(INHERIT);
458 if (noun() == tmplt.noun())
460 if (color() == tmplt.color())
461 setColor(LColor::inherit);
465 /// Realize font from a template
466 LyXFont & LyXFont::realize(LyXFont const & tmplt)
468 if (bits == inherit) {
473 if (bits.family == INHERIT_FAMILY) {
474 bits.family = tmplt.bits.family;
476 if (bits.series == INHERIT_SERIES) {
477 bits.series = tmplt.bits.series;
479 if (bits.shape == INHERIT_SHAPE) {
480 bits.shape = tmplt.bits.shape;
482 if (bits.size == INHERIT_SIZE) {
483 bits.size = tmplt.bits.size;
485 if (bits.emph == INHERIT) {
486 bits.emph = tmplt.bits.emph;
488 if (bits.underbar == INHERIT) {
489 bits.underbar = tmplt.bits.underbar;
491 if (bits.noun == INHERIT) {
492 bits.noun = tmplt.bits.noun;
494 if (bits.color == LColor::inherit) {
495 bits.color = tmplt.bits.color;
501 /// Is font resolved?
502 bool LyXFont::resolved() const
504 return (family() != INHERIT_FAMILY && series() != INHERIT_SERIES &&
505 shape() != INHERIT_SHAPE && size() != INHERIT_SIZE &&
506 emph() != INHERIT && underbar() != INHERIT &&
508 color() != LColor::inherit);
512 /// Build GUI description of font state
513 string const LyXFont::stateText(BufferParams * params) const
516 if (family() != INHERIT_FAMILY)
517 os << _(GUIFamilyNames[family()]) << ", ";
518 if (series() != INHERIT_SERIES)
519 os << _(GUISeriesNames[series()]) << ", ";
520 if (shape() != INHERIT_SHAPE)
521 os << _(GUIShapeNames[shape()]) << ", ";
522 if (size() != INHERIT_SIZE)
523 os << _(GUISizeNames[size()]) << ", ";
524 if (color() != LColor::inherit)
525 os << lcolor.getGUIName(color()) << ", ";
526 if (emph() != INHERIT)
527 os << bformat(_("Emphasis %1$s, "), _(GUIMiscNames[emph()]));
528 if (underbar() != INHERIT)
529 os << bformat(_("Underline %1$s, "), _(GUIMiscNames[underbar()]));
530 if (noun() != INHERIT)
531 os << bformat(_("Noun %1$s, "), _(GUIMiscNames[noun()]));
533 os << _("Default") << ", ";
534 if (!params || (language() != params->language))
535 os << bformat(_("Language: %1$s, "), _(language()->display()));
537 os << bformat(_(" Number %1$s"), _(GUIMiscNames[number()]));
538 return rtrim(os.str(), ", ");
542 // Set family according to lyx format string
543 LyXFont & LyXFont::setLyXFamily(string const & fam)
545 string const s = ascii_lowercase(fam);
548 while (LyXFamilyNames[i] != s &&
549 LyXFamilyNames[i] != string("error"))
551 if (s == LyXFamilyNames[i])
552 setFamily(LyXFont::FONT_FAMILY(i));
554 lyxerr << "LyXFont::setLyXFamily: Unknown family `"
555 << s << '\'' << endl;
560 // Set series according to lyx format string
561 LyXFont & LyXFont::setLyXSeries(string const & ser)
563 string const s = ascii_lowercase(ser);
566 while (LyXSeriesNames[i] != s &&
567 LyXSeriesNames[i] != string("error")) ++i;
568 if (s == LyXSeriesNames[i]) {
569 setSeries(LyXFont::FONT_SERIES(i));
571 lyxerr << "LyXFont::setLyXSeries: Unknown series `"
572 << s << '\'' << endl;
577 // Set shape according to lyx format string
578 LyXFont & LyXFont::setLyXShape(string const & sha)
580 string const s = ascii_lowercase(sha);
583 while (LyXShapeNames[i] != s &&
584 LyXShapeNames[i] != string("error")) ++i;
585 if (s == LyXShapeNames[i]) {
586 setShape(LyXFont::FONT_SHAPE(i));
588 lyxerr << "LyXFont::setLyXShape: Unknown shape `"
589 << s << '\'' << endl;
594 // Set size according to lyx format string
595 LyXFont & LyXFont::setLyXSize(string const & siz)
597 string const s = ascii_lowercase(siz);
599 while (LyXSizeNames[i] != s &&
600 LyXSizeNames[i] != string("error")) ++i;
601 if (s == LyXSizeNames[i]) {
602 setSize(LyXFont::FONT_SIZE(i));
604 lyxerr << "LyXFont::setLyXSize: Unknown size `"
605 << s << '\'' << endl;
610 // Set size according to lyx format string
611 LyXFont::FONT_MISC_STATE LyXFont::setLyXMisc(string const & siz)
613 string const s = ascii_lowercase(siz);
615 while (LyXMiscNames[i] != s &&
616 LyXMiscNames[i] != string("error")) ++i;
617 if (s == LyXMiscNames[i])
618 return FONT_MISC_STATE(i);
619 lyxerr << "LyXFont::setLyXMisc: Unknown misc flag `"
620 << s << '\'' << endl;
625 /// Sets color after LyX text format
626 LyXFont & LyXFont::setLyXColor(string const & col)
628 setColor(lcolor.getFromLyXName(col));
633 // Returns size in latex format
634 string const LyXFont::latexSize() const
636 return LaTeXSizeNames[size()];
640 // Read a font definition from given file in lyx format
642 LyXFont & LyXFont::lyxRead(LyXLex & lex)
645 bool finished = false;
646 while (!finished && lex.isOK() && !error) {
648 string const tok = ascii_lowercase(lex.getString());
652 } else if (tok == "endfont") {
654 } else if (tok == "family") {
656 string const ttok = lex.getString();
658 } else if (tok == "series") {
660 string const ttok = lex.getString();
662 } else if (tok == "shape") {
664 string const ttok = lex.getString();
666 } else if (tok == "size") {
668 string const ttok = lex.getString();
670 } else if (tok == "misc") {
672 string const ttok = ascii_lowercase(lex.getString());
674 if (ttok == "no_bar") {
676 } else if (ttok == "no_emph") {
678 } else if (ttok == "no_noun") {
680 } else if (ttok == "emph") {
682 } else if (ttok == "underbar") {
684 } else if (ttok == "noun") {
687 lex.printError("Illegal misc type `$$Token´");
689 } else if (tok == "color") {
691 string const ttok = lex.getString();
694 lex.printError("Unknown tag `$$Token'");
702 /// Writes the changes from this font to orgfont in .lyx format in file
703 void LyXFont::lyxWriteChanges(LyXFont const & orgfont,
707 if (orgfont.family() != family()) {
708 os << "\\family " << LyXFamilyNames[family()] << "\n";
710 if (orgfont.series() != series()) {
711 os << "\\series " << LyXSeriesNames[series()] << "\n";
713 if (orgfont.shape() != shape()) {
714 os << "\\shape " << LyXShapeNames[shape()] << "\n";
716 if (orgfont.size() != size()) {
717 os << "\\size " << LyXSizeNames[size()] << "\n";
719 if (orgfont.emph() != emph()) {
720 os << "\\emph " << LyXMiscNames[emph()] << "\n";
722 if (orgfont.number() != number()) {
723 os << "\\numeric " << LyXMiscNames[number()] << "\n";
725 if (orgfont.underbar() != underbar()) {
726 // This is only for backwards compatibility
727 switch (underbar()) {
728 case OFF: os << "\\bar no\n"; break;
729 case ON: os << "\\bar under\n"; break;
730 case TOGGLE: lyxerr << "LyXFont::lyxWriteFontChanges: "
731 "TOGGLE should not appear here!"
734 case INHERIT: os << "\\bar default\n"; break;
735 case IGNORE: lyxerr << "LyXFont::lyxWriteFontChanges: "
736 "IGNORE should not appear here!"
741 if (orgfont.noun() != noun()) {
742 os << "\\noun " << LyXMiscNames[noun()] << "\n";
744 if (orgfont.color() != color()) {
745 // To make us file compatible with older
746 // lyx versions we emit "default" instead
748 string col_str(lcolor.getLyXName(color()));
749 if (col_str == "inherit") col_str = "default";
750 os << "\\color " << col_str << "\n";
752 if (orgfont.language() != language()) {
754 os << "\\lang " << language()->lang() << "\n";
756 os << "\\lang unknown\n";
761 /// Writes the head of the LaTeX needed to impose this font
762 // Returns number of chars written.
763 int LyXFont::latexWriteStartChanges(ostream & os, LyXFont const & base,
764 LyXFont const & prev) const
769 if (language()->babel() != base.language()->babel() &&
770 language() != prev.language()) {
771 if (isRightToLeft() != prev.isRightToLeft()) {
772 if (isRightToLeft()) {
781 subst(lyxrc.language_command_local,
782 "$$lang", language()->babel());
784 count += tmp.length();
788 if (number() == ON && prev.number() != ON &&
789 language()->lang() == "hebrew") {
797 if (f.family() != INHERIT_FAMILY) {
799 << LaTeXFamilyNames[f.family()]
801 count += strlen(LaTeXFamilyNames[f.family()]) + 2;
802 env = true; //We have opened a new environment
804 if (f.series() != INHERIT_SERIES) {
806 << LaTeXSeriesNames[f.series()]
808 count += strlen(LaTeXSeriesNames[f.series()]) + 2;
809 env = true; //We have opened a new environment
811 if (f.shape() != INHERIT_SHAPE) {
813 << LaTeXShapeNames[f.shape()]
815 count += strlen(LaTeXShapeNames[f.shape()]) + 2;
816 env = true; //We have opened a new environment
818 if (f.color() != LColor::inherit && f.color() != LColor::ignore) {
820 << lcolor.getLaTeXName(f.color())
822 count += lcolor.getLaTeXName(f.color()).length() + 13;
823 env = true; //We have opened a new environment
825 if (f.emph() == ON) {
828 env = true; //We have opened a new environment
830 if (f.underbar() == ON) {
833 env = true; //We have opened a new environment
835 // \noun{} is a LyX special macro
836 if (f.noun() == ON) {
839 env = true; //We have opened a new environment
841 if (f.size() != INHERIT_SIZE) {
842 // If we didn't open an environment above, we open one here
848 << LaTeXSizeNames[f.size()]
850 count += strlen(LaTeXSizeNames[f.size()]) + 2;
856 /// Writes ending block of LaTeX needed to close use of this font
857 // Returns number of chars written
858 // This one corresponds to latexWriteStartChanges(). (Asger)
859 int LyXFont::latexWriteEndChanges(ostream & os, LyXFont const & base,
860 LyXFont const & next) const
865 // reduce the current font to changes against the base
866 // font (of the layout). We use a temporary for this to
867 // avoid changing this font instance, as that would break
871 if (f.family() != INHERIT_FAMILY) {
874 env = true; // Size change need not bother about closing env.
876 if (f.series() != INHERIT_SERIES) {
879 env = true; // Size change need not bother about closing env.
881 if (f.shape() != INHERIT_SHAPE) {
884 env = true; // Size change need not bother about closing env.
886 if (f.color() != LColor::inherit && f.color() != LColor::ignore) {
889 env = true; // Size change need not bother about closing env.
891 if (f.emph() == ON) {
894 env = true; // Size change need not bother about closing env.
896 if (f.underbar() == ON) {
899 env = true; // Size change need not bother about closing env.
901 if (f.noun() == ON) {
904 env = true; // Size change need not bother about closing env.
906 if (f.size() != INHERIT_SIZE) {
907 // We only have to close if only size changed
914 if (number() == ON && next.number() != ON &&
915 language()->lang() == "hebrew") {
920 if (language() != base.language() && language() != next.language()) {
929 LColor_color LyXFont::realColor() const
931 if (color() == LColor::none)
932 return LColor::foreground;
937 // Convert logical attributes to concrete shape attribute
938 LyXFont::FONT_SHAPE LyXFont::realShape() const
940 register FONT_SHAPE s = shape();
954 ostream & operator<<(ostream & os, LyXFont::FONT_MISC_STATE fms)
956 return os << int(fms);
960 std::ostream & operator<<(std::ostream & os, LyXFont const & font)
963 << " family " << font.bits.family
964 << " series " << font.bits.series
965 << " shape " << font.bits.shape
966 << " size " << font.bits.size
967 << " color " << font.bits.color
968 << " emph " << font.bits.emph
969 << " underbar " << font.bits.underbar
970 << " noun " << font.bits.noun
971 << " number " << font.bits.number
972 << " lang: " << (font.lang ? font.lang->lang() : 0);