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"
29 #include "support/std_sstream.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 EnumLColor 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(EnumLColor 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 (s != LyXFamilyNames[i] && LyXFamilyNames[i] != "error")
550 if (s == LyXFamilyNames[i])
551 setFamily(LyXFont::FONT_FAMILY(i));
553 lyxerr << "LyXFont::setLyXFamily: Unknown family `"
554 << s << '\'' << endl;
559 // Set series according to lyx format string
560 LyXFont & LyXFont::setLyXSeries(string const & ser)
562 string const s = ascii_lowercase(ser);
565 while (s != LyXSeriesNames[i] && LyXSeriesNames[i] != "error") ++i;
566 if (s == LyXSeriesNames[i]) {
567 setSeries(LyXFont::FONT_SERIES(i));
569 lyxerr << "LyXFont::setLyXSeries: Unknown series `"
570 << s << '\'' << endl;
575 // Set shape according to lyx format string
576 LyXFont & LyXFont::setLyXShape(string const & sha)
578 string const s = ascii_lowercase(sha);
581 while (s != LyXShapeNames[i] && LyXShapeNames[i] != "error") ++i;
582 if (s == LyXShapeNames[i]) {
583 setShape(LyXFont::FONT_SHAPE(i));
585 lyxerr << "LyXFont::setLyXShape: Unknown shape `"
586 << s << '\'' << endl;
591 // Set size according to lyx format string
592 LyXFont & LyXFont::setLyXSize(string const & siz)
594 string const s = ascii_lowercase(siz);
596 while (s != LyXSizeNames[i] && LyXSizeNames[i] != "error") ++i;
597 if (s == LyXSizeNames[i]) {
598 setSize(LyXFont::FONT_SIZE(i));
600 lyxerr << "LyXFont::setLyXSize: Unknown size `"
601 << s << '\'' << endl;
606 // Set size according to lyx format string
607 LyXFont::FONT_MISC_STATE LyXFont::setLyXMisc(string const & siz)
609 string const s = ascii_lowercase(siz);
611 while (s != LyXMiscNames[i] && LyXMiscNames[i] != "error") ++i;
612 if (s == LyXMiscNames[i])
613 return FONT_MISC_STATE(i);
614 lyxerr << "LyXFont::setLyXMisc: Unknown misc flag `"
615 << s << '\'' << endl;
620 /// Sets color after LyX text format
621 LyXFont & LyXFont::setLyXColor(string const & col)
623 setColor(lcolor.getFromLyXName(col));
628 // Returns size in latex format
629 string const LyXFont::latexSize() const
631 return LaTeXSizeNames[size()];
635 // Read a font definition from given file in lyx format
637 LyXFont & LyXFont::lyxRead(LyXLex & lex)
640 bool finished = false;
641 while (!finished && lex.isOK() && !error) {
643 string const tok = ascii_lowercase(lex.getString());
647 } else if (tok == "endfont") {
649 } else if (tok == "family") {
651 string const ttok = lex.getString();
653 } else if (tok == "series") {
655 string const ttok = lex.getString();
657 } else if (tok == "shape") {
659 string const ttok = lex.getString();
661 } else if (tok == "size") {
663 string const ttok = lex.getString();
665 } else if (tok == "misc") {
667 string const ttok = ascii_lowercase(lex.getString());
669 if (ttok == "no_bar") {
671 } else if (ttok == "no_emph") {
673 } else if (ttok == "no_noun") {
675 } else if (ttok == "emph") {
677 } else if (ttok == "underbar") {
679 } else if (ttok == "noun") {
682 lex.printError("Illegal misc type `$$Token´");
684 } else if (tok == "color") {
686 string const ttok = lex.getString();
689 lex.printError("Unknown tag `$$Token'");
697 /// Writes the changes from this font to orgfont in .lyx format in file
698 void LyXFont::lyxWriteChanges(LyXFont const & orgfont,
702 if (orgfont.family() != family()) {
703 os << "\\family " << LyXFamilyNames[family()] << " \n";
705 if (orgfont.series() != series()) {
706 os << "\\series " << LyXSeriesNames[series()] << " \n";
708 if (orgfont.shape() != shape()) {
709 os << "\\shape " << LyXShapeNames[shape()] << " \n";
711 if (orgfont.size() != size()) {
712 os << "\\size " << LyXSizeNames[size()] << " \n";
714 if (orgfont.emph() != emph()) {
715 os << "\\emph " << LyXMiscNames[emph()] << " \n";
717 if (orgfont.number() != number()) {
718 os << "\\numeric " << LyXMiscNames[number()] << " \n";
720 if (orgfont.underbar() != underbar()) {
721 // This is only for backwards compatibility
722 switch (underbar()) {
723 case OFF: os << "\\bar no \n"; break;
724 case ON: os << "\\bar under \n"; break;
725 case TOGGLE: lyxerr << "LyXFont::lyxWriteFontChanges: "
726 "TOGGLE should not appear here!"
729 case INHERIT: os << "\\bar default \n"; break;
730 case IGNORE: lyxerr << "LyXFont::lyxWriteFontChanges: "
731 "IGNORE should not appear here!"
736 if (orgfont.noun() != noun()) {
737 os << "\\noun " << LyXMiscNames[noun()] << " \n";
739 if (orgfont.color() != color()) {
740 // To make us file compatible with older
741 // lyx versions we emit "default" instead
743 string col_str(lcolor.getLyXName(color()));
744 if (col_str == "inherit") col_str = "default";
745 os << "\\color " << col_str << "\n";
747 if (orgfont.language() != language()) {
749 os << "\\lang " << language()->lang() << "\n";
751 os << "\\lang unknown\n";
756 /// Writes the head of the LaTeX needed to impose this font
757 // Returns number of chars written.
758 int LyXFont::latexWriteStartChanges(ostream & os, LyXFont const & base,
759 LyXFont const & prev) const
764 if (language()->babel() != base.language()->babel() &&
765 language() != prev.language()) {
766 if (isRightToLeft() != prev.isRightToLeft()) {
767 if (isRightToLeft()) {
776 subst(lyxrc.language_command_local,
777 "$$lang", language()->babel());
779 count += tmp.length();
783 if (number() == ON && prev.number() != ON &&
784 language()->lang() == "hebrew") {
792 if (f.family() != INHERIT_FAMILY) {
794 << LaTeXFamilyNames[f.family()]
796 count += strlen(LaTeXFamilyNames[f.family()]) + 2;
797 env = true; //We have opened a new environment
799 if (f.series() != INHERIT_SERIES) {
801 << LaTeXSeriesNames[f.series()]
803 count += strlen(LaTeXSeriesNames[f.series()]) + 2;
804 env = true; //We have opened a new environment
806 if (f.shape() != INHERIT_SHAPE) {
808 << LaTeXShapeNames[f.shape()]
810 count += strlen(LaTeXShapeNames[f.shape()]) + 2;
811 env = true; //We have opened a new environment
813 if (f.color() != LColor::inherit && f.color() != LColor::ignore) {
815 << lcolor.getLaTeXName(f.color())
817 count += lcolor.getLaTeXName(f.color()).length() + 13;
818 env = true; //We have opened a new environment
820 if (f.emph() == ON) {
823 env = true; //We have opened a new environment
825 if (f.underbar() == ON) {
828 env = true; //We have opened a new environment
830 // \noun{} is a LyX special macro
831 if (f.noun() == ON) {
834 env = true; //We have opened a new environment
836 if (f.size() != INHERIT_SIZE) {
837 // If we didn't open an environment above, we open one here
843 << LaTeXSizeNames[f.size()]
845 count += strlen(LaTeXSizeNames[f.size()]) + 2;
851 /// Writes ending block of LaTeX needed to close use of this font
852 // Returns number of chars written
853 // This one corresponds to latexWriteStartChanges(). (Asger)
854 int LyXFont::latexWriteEndChanges(ostream & os, LyXFont const & base,
855 LyXFont const & next) const
860 // reduce the current font to changes against the base
861 // font (of the layout). We use a temporary for this to
862 // avoid changing this font instance, as that would break
866 if (f.family() != INHERIT_FAMILY) {
869 env = true; // Size change need not bother about closing env.
871 if (f.series() != INHERIT_SERIES) {
874 env = true; // Size change need not bother about closing env.
876 if (f.shape() != INHERIT_SHAPE) {
879 env = true; // Size change need not bother about closing env.
881 if (f.color() != LColor::inherit && f.color() != LColor::ignore) {
884 env = true; // Size change need not bother about closing env.
886 if (f.emph() == ON) {
889 env = true; // Size change need not bother about closing env.
891 if (f.underbar() == ON) {
894 env = true; // Size change need not bother about closing env.
896 if (f.noun() == ON) {
899 env = true; // Size change need not bother about closing env.
901 if (f.size() != INHERIT_SIZE) {
902 // We only have to close if only size changed
909 if (number() == ON && next.number() != ON &&
910 language()->lang() == "hebrew") {
915 if (language() != base.language() && language() != next.language()) {
924 EnumLColor LyXFont::realColor() const
926 if (color() == LColor::none)
927 return LColor::foreground;
932 // Convert logical attributes to concrete shape attribute
933 LyXFont::FONT_SHAPE LyXFont::realShape() const
935 register FONT_SHAPE s = shape();
949 ostream & operator<<(ostream & o, LyXFont::FONT_MISC_STATE fms)
951 return o << int(fms);