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
26 #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 EnumLColor LyXFont::color() const
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 LyXFont & LyXFont::setFamily(LyXFont::FONT_FAMILY f)
243 LyXFont & LyXFont::setSeries(LyXFont::FONT_SERIES s)
250 LyXFont & LyXFont::setShape(LyXFont::FONT_SHAPE s)
257 LyXFont & LyXFont::setSize(LyXFont::FONT_SIZE s)
264 LyXFont & LyXFont::setEmph(LyXFont::FONT_MISC_STATE e)
271 LyXFont & LyXFont::setUnderbar(LyXFont::FONT_MISC_STATE u)
278 LyXFont & LyXFont::setNoun(LyXFont::FONT_MISC_STATE n)
285 LyXFont & LyXFont::setColor(EnumLColor c)
292 LyXFont & LyXFont::setLanguage(Language const * l)
299 LyXFont & LyXFont::setNumber(LyXFont::FONT_MISC_STATE n)
306 /// Decreases font size by one
307 LyXFont & LyXFont::decSize()
310 case SIZE_HUGER: setSize(SIZE_HUGE); break;
311 case SIZE_HUGE: setSize(SIZE_LARGEST); break;
312 case SIZE_LARGEST: setSize(SIZE_LARGER); break;
313 case SIZE_LARGER: setSize(SIZE_LARGE); break;
314 case SIZE_LARGE: setSize(SIZE_NORMAL); break;
315 case SIZE_NORMAL: setSize(SIZE_SMALL); break;
316 case SIZE_SMALL: setSize(SIZE_FOOTNOTE); break;
317 case SIZE_FOOTNOTE: setSize(SIZE_SCRIPT); break;
318 case SIZE_SCRIPT: setSize(SIZE_TINY); break;
319 case SIZE_TINY: break;
321 lyxerr << "Can't LyXFont::decSize on INCREASE_SIZE" << endl;
324 lyxerr <<"Can't LyXFont::decSize on DECREASE_SIZE" << endl;
327 lyxerr <<"Can't LyXFont::decSize on INHERIT_SIZE" << endl;
330 lyxerr <<"Can't LyXFont::decSize on IGNORE_SIZE" << endl;
337 /// Increases font size by one
338 LyXFont & LyXFont::incSize()
341 case SIZE_HUGER: break;
342 case SIZE_HUGE: setSize(SIZE_HUGER); break;
343 case SIZE_LARGEST: setSize(SIZE_HUGE); break;
344 case SIZE_LARGER: setSize(SIZE_LARGEST); break;
345 case SIZE_LARGE: setSize(SIZE_LARGER); break;
346 case SIZE_NORMAL: setSize(SIZE_LARGE); break;
347 case SIZE_SMALL: setSize(SIZE_NORMAL); break;
348 case SIZE_FOOTNOTE: setSize(SIZE_SMALL); break;
349 case SIZE_SCRIPT: setSize(SIZE_FOOTNOTE); break;
350 case SIZE_TINY: setSize(SIZE_SCRIPT); break;
352 lyxerr <<"Can't LyXFont::incSize on INCREASE_SIZE" << endl;
355 lyxerr <<"Can't LyXFont::incSize on DECREASE_SIZE" << endl;
358 lyxerr <<"Can't LyXFont::incSize on INHERIT_SIZE" << endl;
361 lyxerr <<"Can't LyXFont::incSize on IGNORE_SIZE" << endl;
368 /// Updates a misc setting according to request
369 LyXFont::FONT_MISC_STATE LyXFont::setMisc(FONT_MISC_STATE newfont,
372 if (newfont == TOGGLE) {
378 lyxerr <<"LyXFont::setMisc: Need state"
379 " ON or OFF to toggle. Setting to ON" << endl;
382 } else if (newfont == IGNORE)
389 /// Updates font settings according to request
390 void LyXFont::update(LyXFont const & newfont,
391 Language const * document_language,
394 if (newfont.family() == family() && toggleall)
395 setFamily(INHERIT_FAMILY); // toggle 'back'
396 else if (newfont.family() != IGNORE_FAMILY)
397 setFamily(newfont.family());
398 // else it's IGNORE_SHAPE
400 // "Old" behaviour: "Setting" bold will toggle bold on/off.
401 switch (newfont.series()) {
404 if (series() == BOLD_SERIES && toggleall)
405 setSeries(MEDIUM_SERIES);
407 setSeries(BOLD_SERIES);
411 setSeries(newfont.series());
417 if (newfont.shape() == shape() && toggleall)
418 setShape(INHERIT_SHAPE); // toggle 'back'
419 else if (newfont.shape() != IGNORE_SHAPE)
420 setShape(newfont.shape());
421 // else it's IGNORE_SHAPE
423 if (newfont.size() != IGNORE_SIZE) {
424 if (newfont.size() == INCREASE_SIZE)
426 else if (newfont.size() == DECREASE_SIZE)
429 setSize(newfont.size());
432 setEmph(setMisc(newfont.emph(), emph()));
433 setUnderbar(setMisc(newfont.underbar(), underbar()));
434 setNoun(setMisc(newfont.noun(), noun()));
436 setNumber(setMisc(newfont.number(), number()));
437 if (newfont.language() == language() && toggleall)
438 if (language() == document_language)
439 setLanguage(default_language);
441 setLanguage(document_language);
442 else if (newfont.language() != ignore_language)
443 setLanguage(newfont.language());
445 if (newfont.color() == color() && toggleall)
446 setColor(LColor::inherit); // toggle 'back'
447 else if (newfont.color() != LColor::ignore)
448 setColor(newfont.color());
452 /// Reduce font to fall back to template where possible
453 void LyXFont::reduce(LyXFont const & tmplt)
455 if (family() == tmplt.family())
456 setFamily(INHERIT_FAMILY);
457 if (series() == tmplt.series())
458 setSeries(INHERIT_SERIES);
459 if (shape() == tmplt.shape())
460 setShape(INHERIT_SHAPE);
461 if (size() == tmplt.size())
462 setSize(INHERIT_SIZE);
463 if (emph() == tmplt.emph())
465 if (underbar() == tmplt.underbar())
466 setUnderbar(INHERIT);
467 if (noun() == tmplt.noun())
469 if (color() == tmplt.color())
470 setColor(LColor::inherit);
474 /// Realize font from a template
475 LyXFont & LyXFont::realize(LyXFont const & tmplt)
477 if (bits == inherit) {
482 if (bits.family == INHERIT_FAMILY) {
483 bits.family = tmplt.bits.family;
485 if (bits.series == INHERIT_SERIES) {
486 bits.series = tmplt.bits.series;
488 if (bits.shape == INHERIT_SHAPE) {
489 bits.shape = tmplt.bits.shape;
491 if (bits.size == INHERIT_SIZE) {
492 bits.size = tmplt.bits.size;
494 if (bits.emph == INHERIT) {
495 bits.emph = tmplt.bits.emph;
497 if (bits.underbar == INHERIT) {
498 bits.underbar = tmplt.bits.underbar;
500 if (bits.noun == INHERIT) {
501 bits.noun = tmplt.bits.noun;
503 if (bits.color == LColor::inherit) {
504 bits.color = tmplt.bits.color;
510 /// Is font resolved?
511 bool LyXFont::resolved() const
513 return (family() != INHERIT_FAMILY && series() != INHERIT_SERIES &&
514 shape() != INHERIT_SHAPE && size() != INHERIT_SIZE &&
515 emph() != INHERIT && underbar() != INHERIT &&
517 color() != LColor::inherit);
521 /// Build GUI description of font state
522 string const LyXFont::stateText(BufferParams * params) const
525 if (family() != INHERIT_FAMILY)
526 os << _(GUIFamilyNames[family()]) << ", ";
527 if (series() != INHERIT_SERIES)
528 os << _(GUISeriesNames[series()]) << ", ";
529 if (shape() != INHERIT_SHAPE)
530 os << _(GUIShapeNames[shape()]) << ", ";
531 if (size() != INHERIT_SIZE)
532 os << _(GUISizeNames[size()]) << ", ";
533 if (color() != LColor::inherit)
534 os << lcolor.getGUIName(color()) << ", ";
535 if (emph() != INHERIT)
536 os << bformat(_("Emphasis %1$s, "), _(GUIMiscNames[emph()]));
537 if (underbar() != INHERIT)
538 os << bformat(_("Underline %1$s, "), _(GUIMiscNames[underbar()]));
539 if (noun() != INHERIT)
540 os << bformat(_("Noun %1$s, "), _(GUIMiscNames[noun()]));
542 os << _("Default") << ", ";
543 if (!params || (language() != params->language))
544 os << bformat(_("Language: %1$s, "), _(language()->display()));
546 os << bformat(_(" Number %1$s"), _(GUIMiscNames[number()]));
547 return rtrim(os.str(), ", ");
551 // Set family according to lyx format string
552 LyXFont & LyXFont::setLyXFamily(string const & fam)
554 string const s = ascii_lowercase(fam);
557 while (s != LyXFamilyNames[i] && LyXFamilyNames[i] != "error")
559 if (s == LyXFamilyNames[i])
560 setFamily(LyXFont::FONT_FAMILY(i));
562 lyxerr << "LyXFont::setLyXFamily: Unknown family `"
563 << s << '\'' << endl;
568 // Set series according to lyx format string
569 LyXFont & LyXFont::setLyXSeries(string const & ser)
571 string const s = ascii_lowercase(ser);
574 while (s != LyXSeriesNames[i] && LyXSeriesNames[i] != "error") ++i;
575 if (s == LyXSeriesNames[i]) {
576 setSeries(LyXFont::FONT_SERIES(i));
578 lyxerr << "LyXFont::setLyXSeries: Unknown series `"
579 << s << '\'' << endl;
584 // Set shape according to lyx format string
585 LyXFont & LyXFont::setLyXShape(string const & sha)
587 string const s = ascii_lowercase(sha);
590 while (s != LyXShapeNames[i] && LyXShapeNames[i] != "error") ++i;
591 if (s == LyXShapeNames[i]) {
592 setShape(LyXFont::FONT_SHAPE(i));
594 lyxerr << "LyXFont::setLyXShape: Unknown shape `"
595 << s << '\'' << endl;
600 // Set size according to lyx format string
601 LyXFont & LyXFont::setLyXSize(string const & siz)
603 string const s = ascii_lowercase(siz);
605 while (s != LyXSizeNames[i] && LyXSizeNames[i] != "error") ++i;
606 if (s == LyXSizeNames[i]) {
607 setSize(LyXFont::FONT_SIZE(i));
609 lyxerr << "LyXFont::setLyXSize: Unknown size `"
610 << s << '\'' << endl;
615 // Set size according to lyx format string
616 LyXFont::FONT_MISC_STATE LyXFont::setLyXMisc(string const & siz)
618 string const s = ascii_lowercase(siz);
620 while (s != LyXMiscNames[i] && LyXMiscNames[i] != "error") ++i;
621 if (s == LyXMiscNames[i])
622 return FONT_MISC_STATE(i);
623 lyxerr << "LyXFont::setLyXMisc: Unknown misc flag `"
624 << s << '\'' << endl;
629 /// Sets color after LyX text format
630 LyXFont & LyXFont::setLyXColor(string const & col)
632 setColor(lcolor.getFromLyXName(col));
637 // Returns size in latex format
638 string const LyXFont::latexSize() const
640 return LaTeXSizeNames[size()];
644 // Read a font definition from given file in lyx format
646 LyXFont & LyXFont::lyxRead(LyXLex & lex)
649 bool finished = false;
650 while (!finished && lex.isOK() && !error) {
652 string const tok = ascii_lowercase(lex.getString());
656 } else if (tok == "endfont") {
658 } else if (tok == "family") {
660 string const ttok = lex.getString();
662 } else if (tok == "series") {
664 string const ttok = lex.getString();
666 } else if (tok == "shape") {
668 string const ttok = lex.getString();
670 } else if (tok == "size") {
672 string const ttok = lex.getString();
674 } else if (tok == "misc") {
676 string const ttok = ascii_lowercase(lex.getString());
678 if (ttok == "no_bar") {
680 } else if (ttok == "no_emph") {
682 } else if (ttok == "no_noun") {
684 } else if (ttok == "emph") {
686 } else if (ttok == "underbar") {
688 } else if (ttok == "noun") {
691 lex.printError("Illegal misc type `$$Token´");
693 } else if (tok == "color") {
695 string const ttok = lex.getString();
698 lex.printError("Unknown tag `$$Token'");
706 /// Writes the changes from this font to orgfont in .lyx format in file
707 void LyXFont::lyxWriteChanges(LyXFont const & orgfont,
711 if (orgfont.family() != family()) {
712 os << "\\family " << LyXFamilyNames[family()] << " \n";
714 if (orgfont.series() != series()) {
715 os << "\\series " << LyXSeriesNames[series()] << " \n";
717 if (orgfont.shape() != shape()) {
718 os << "\\shape " << LyXShapeNames[shape()] << " \n";
720 if (orgfont.size() != size()) {
721 os << "\\size " << LyXSizeNames[size()] << " \n";
723 if (orgfont.emph() != emph()) {
724 os << "\\emph " << LyXMiscNames[emph()] << " \n";
726 if (orgfont.number() != number()) {
727 os << "\\numeric " << LyXMiscNames[number()] << " \n";
729 if (orgfont.underbar() != underbar()) {
730 // This is only for backwards compatibility
731 switch (underbar()) {
732 case OFF: os << "\\bar no \n"; break;
733 case ON: os << "\\bar under \n"; break;
734 case TOGGLE: lyxerr << "LyXFont::lyxWriteFontChanges: "
735 "TOGGLE should not appear here!"
738 case INHERIT: os << "\\bar default \n"; break;
739 case IGNORE: lyxerr << "LyXFont::lyxWriteFontChanges: "
740 "IGNORE should not appear here!"
745 if (orgfont.noun() != noun()) {
746 os << "\\noun " << LyXMiscNames[noun()] << " \n";
748 if (orgfont.color() != color()) {
749 // To make us file compatible with older
750 // lyx versions we emit "default" instead
752 string col_str(lcolor.getLyXName(color()));
753 if (col_str == "inherit") col_str = "default";
754 os << "\\color " << col_str << "\n";
756 if (orgfont.language() != language()) {
758 os << "\\lang " << language()->lang() << "\n";
760 os << "\\lang unknown\n";
765 /// Writes the head of the LaTeX needed to impose this font
766 // Returns number of chars written.
767 int LyXFont::latexWriteStartChanges(ostream & os, LyXFont const & base,
768 LyXFont const & prev) const
773 if (language()->babel() != base.language()->babel() &&
774 language() != prev.language()) {
775 if (isRightToLeft() != prev.isRightToLeft()) {
776 if (isRightToLeft()) {
785 subst(lyxrc.language_command_local,
786 "$$lang", language()->babel());
788 count += tmp.length();
792 if (number() == ON && prev.number() != ON &&
793 language()->lang() == "hebrew") {
801 if (f.family() != INHERIT_FAMILY) {
803 << LaTeXFamilyNames[f.family()]
805 count += strlen(LaTeXFamilyNames[f.family()]) + 2;
806 env = true; //We have opened a new environment
808 if (f.series() != INHERIT_SERIES) {
810 << LaTeXSeriesNames[f.series()]
812 count += strlen(LaTeXSeriesNames[f.series()]) + 2;
813 env = true; //We have opened a new environment
815 if (f.shape() != INHERIT_SHAPE) {
817 << LaTeXShapeNames[f.shape()]
819 count += strlen(LaTeXShapeNames[f.shape()]) + 2;
820 env = true; //We have opened a new environment
822 if (f.color() != LColor::inherit && f.color() != LColor::ignore) {
824 << lcolor.getLaTeXName(f.color())
826 count += lcolor.getLaTeXName(f.color()).length() + 13;
827 env = true; //We have opened a new environment
829 if (f.emph() == ON) {
832 env = true; //We have opened a new environment
834 if (f.underbar() == ON) {
837 env = true; //We have opened a new environment
839 // \noun{} is a LyX special macro
840 if (f.noun() == ON) {
843 env = true; //We have opened a new environment
845 if (f.size() != INHERIT_SIZE) {
846 // If we didn't open an environment above, we open one here
852 << LaTeXSizeNames[f.size()]
854 count += strlen(LaTeXSizeNames[f.size()]) + 2;
860 /// Writes ending block of LaTeX needed to close use of this font
861 // Returns number of chars written
862 // This one corresponds to latexWriteStartChanges(). (Asger)
863 int LyXFont::latexWriteEndChanges(ostream & os, LyXFont const & base,
864 LyXFont const & next) const
869 // reduce the current font to changes against the base
870 // font (of the layout). We use a temporary for this to
871 // avoid changing this font instance, as that would break
875 if (f.family() != INHERIT_FAMILY) {
878 env = true; // Size change need not bother about closing env.
880 if (f.series() != INHERIT_SERIES) {
883 env = true; // Size change need not bother about closing env.
885 if (f.shape() != INHERIT_SHAPE) {
888 env = true; // Size change need not bother about closing env.
890 if (f.color() != LColor::inherit && f.color() != LColor::ignore) {
893 env = true; // Size change need not bother about closing env.
895 if (f.emph() == ON) {
898 env = true; // Size change need not bother about closing env.
900 if (f.underbar() == ON) {
903 env = true; // Size change need not bother about closing env.
905 if (f.noun() == ON) {
908 env = true; // Size change need not bother about closing env.
910 if (f.size() != INHERIT_SIZE) {
911 // We only have to close if only size changed
918 if (number() == ON && next.number() != ON &&
919 language()->lang() == "hebrew") {
924 if (language() != base.language() && language() != next.language()) {
933 EnumLColor LyXFont::realColor() const
935 if (color() == LColor::none)
936 return LColor::foreground;
941 // Convert logical attributes to concrete shape attribute
942 LyXFont::FONT_SHAPE LyXFont::realShape() const
944 register FONT_SHAPE s = shape();
958 ostream & operator<<(ostream & o, LyXFont::FONT_MISC_STATE fms)
960 return o << int(fms);