1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1995 Matthias Ettrich
7 * Copyright 1995-2001 The LyX Team.
9 * ====================================================== */
21 #include "support/lstrings.h"
22 #include "bufferparams.h" // stateText
24 #include "support/BoostFormat.h"
29 #ifndef CXX_GLOBAL_CSTD
39 char const * GUIFamilyNames[LyXFont::NUM_FAMILIES + 2 /* default & error */] =
40 { N_("Roman"), N_("Sans Serif"), N_("Typewriter"), N_("Symbol"),
41 "cmr", "cmsy", "cmm", "cmex", "msa", "msb", "eufrak", "wasy",
42 N_("Inherit"), N_("Ignore") };
44 char const * GUISeriesNames[4] =
45 { N_("Medium"), N_("Bold"), N_("Inherit"), N_("Ignore") };
47 char const * GUIShapeNames[6] =
48 { N_("Upright"), N_("Italic"), N_("Slanted"), N_("Smallcaps"), N_("Inherit"),
51 char const * GUISizeNames[14] =
52 { N_("Tiny"), N_("Smallest"), N_("Smaller"), N_("Small"), N_("Normal"), N_("Large"),
53 N_("Larger"), N_("Largest"), N_("Huge"), N_("Huger"), N_("Increase"), N_("Decrease"),
54 N_("Inherit"), N_("Ignore") };
56 char const * GUIMiscNames[5] =
57 { N_("Off"), N_("On"), N_("Toggle"), N_("Inherit"), N_("Ignore") };
61 // Strings used to read and write .lyx format files
63 char const * LyXFamilyNames[LyXFont::NUM_FAMILIES + 2 /* default & error */] =
64 { "roman", "sans", "typewriter", "symbol",
65 "cmr", "cmsy", "cmm", "cmex", "msa", "msb", "eufrak", "wasy",
68 char const * LyXSeriesNames[4] =
69 { "medium", "bold", "default", "error" };
71 char const * LyXShapeNames[6] =
72 { "up", "italic", "slanted", "smallcaps", "default", "error" };
74 char const * LyXSizeNames[14] =
75 { "tiny", "scriptsize", "footnotesize", "small", "normal", "large",
76 "larger", "largest", "huge", "giant",
77 "increase", "decrease", "default", "error" };
79 char const * LyXMiscNames[5] =
80 { "off", "on", "toggle", "default", "error" };
83 // Strings used to write LaTeX files
85 char const * LaTeXFamilyNames[6] =
86 { "textrm", "textsf", "texttt", "error1", "error2", "error3" };
88 char const * LaTeXSeriesNames[4] =
89 { "textmd", "textbf", "error4", "error5" };
91 char const * LaTeXShapeNames[6] =
92 { "textup", "textit", "textsl", "textsc", "error6", "error7" };
94 char const * LaTeXSizeNames[14] =
95 { "tiny", "scriptsize", "footnotesize", "small", "normalsize", "large",
96 "Large", "LARGE", "huge", "Huge", "error8", "error9", "error10", "error11" };
101 // Initialize static member
102 LyXFont::FontBits LyXFont::sane = {
113 // Initialize static member
114 LyXFont::FontBits LyXFont::inherit = {
125 // Initialize static member
126 LyXFont::FontBits LyXFont::ignore = {
138 bool operator==(LyXFont::FontBits const & lhs,
139 LyXFont::FontBits const & rhs)
141 return lhs.family == rhs.family &&
142 lhs.series == rhs.series &&
143 lhs.shape == rhs.shape &&
144 lhs.size == rhs.size &&
145 lhs.color == rhs.color &&
146 lhs.emph == rhs.emph &&
147 lhs.underbar == rhs.underbar &&
148 lhs.noun == rhs.noun &&
149 lhs.number == rhs.number;
154 : bits(sane), lang(default_language)
158 LyXFont::LyXFont(LyXFont::FONT_INIT1)
159 : bits(inherit), lang(default_language)
163 LyXFont::LyXFont(LyXFont::FONT_INIT2)
164 : bits(ignore), lang(ignore_language)
168 LyXFont::LyXFont(LyXFont::FONT_INIT3)
169 : bits(sane), lang(default_language)
173 LyXFont::LyXFont(LyXFont::FONT_INIT1, Language const * l)
174 : bits(inherit), lang(l)
178 LyXFont::LyXFont(LyXFont::FONT_INIT2, Language const * l)
179 : bits(ignore), lang(l)
183 LyXFont::LyXFont(LyXFont::FONT_INIT3, Language const * l)
184 : bits(sane), lang(l)
188 LyXFont::FONT_MISC_STATE LyXFont::underbar() const
190 return bits.underbar;
194 LColor::color LyXFont::color() const
200 Language const * LyXFont::language() const
206 LyXFont::FONT_MISC_STATE LyXFont::number() const
212 bool LyXFont::isRightToLeft() const
214 return lang->RightToLeft();
218 bool LyXFont::isVisibleRightToLeft() const
220 return (lang->RightToLeft() &&
225 LyXFont & LyXFont::setFamily(LyXFont::FONT_FAMILY f)
232 LyXFont & LyXFont::setSeries(LyXFont::FONT_SERIES s)
239 LyXFont & LyXFont::setShape(LyXFont::FONT_SHAPE s)
246 LyXFont & LyXFont::setSize(LyXFont::FONT_SIZE s)
253 LyXFont & LyXFont::setEmph(LyXFont::FONT_MISC_STATE e)
260 LyXFont & LyXFont::setUnderbar(LyXFont::FONT_MISC_STATE u)
267 LyXFont & LyXFont::setNoun(LyXFont::FONT_MISC_STATE n)
274 LyXFont & LyXFont::setColor(LColor::color c)
281 LyXFont & LyXFont::setLanguage(Language const * l)
288 LyXFont & LyXFont::setNumber(LyXFont::FONT_MISC_STATE n)
295 /// Decreases font size by one
296 LyXFont & LyXFont::decSize()
299 case SIZE_HUGER: setSize(SIZE_HUGE); break;
300 case SIZE_HUGE: setSize(SIZE_LARGEST); break;
301 case SIZE_LARGEST: setSize(SIZE_LARGER); break;
302 case SIZE_LARGER: setSize(SIZE_LARGE); break;
303 case SIZE_LARGE: setSize(SIZE_NORMAL); break;
304 case SIZE_NORMAL: setSize(SIZE_SMALL); break;
305 case SIZE_SMALL: setSize(SIZE_FOOTNOTE); break;
306 case SIZE_FOOTNOTE: setSize(SIZE_SCRIPT); break;
307 case SIZE_SCRIPT: setSize(SIZE_TINY); break;
308 case SIZE_TINY: break;
310 lyxerr << "Can't LyXFont::decSize on INCREASE_SIZE" << endl;
313 lyxerr <<"Can't LyXFont::decSize on DECREASE_SIZE" << endl;
316 lyxerr <<"Can't LyXFont::decSize on INHERIT_SIZE" << endl;
319 lyxerr <<"Can't LyXFont::decSize on IGNORE_SIZE" << endl;
326 /// Increases font size by one
327 LyXFont & LyXFont::incSize()
330 case SIZE_HUGER: break;
331 case SIZE_HUGE: setSize(SIZE_HUGER); break;
332 case SIZE_LARGEST: setSize(SIZE_HUGE); break;
333 case SIZE_LARGER: setSize(SIZE_LARGEST); break;
334 case SIZE_LARGE: setSize(SIZE_LARGER); break;
335 case SIZE_NORMAL: setSize(SIZE_LARGE); break;
336 case SIZE_SMALL: setSize(SIZE_NORMAL); break;
337 case SIZE_FOOTNOTE: setSize(SIZE_SMALL); break;
338 case SIZE_SCRIPT: setSize(SIZE_FOOTNOTE); break;
339 case SIZE_TINY: setSize(SIZE_SCRIPT); break;
341 lyxerr <<"Can't LyXFont::incSize on INCREASE_SIZE" << endl;
344 lyxerr <<"Can't LyXFont::incSize on DECREASE_SIZE" << endl;
347 lyxerr <<"Can't LyXFont::incSize on INHERIT_SIZE" << endl;
350 lyxerr <<"Can't LyXFont::incSize on IGNORE_SIZE" << endl;
357 /// Updates a misc setting according to request
358 LyXFont::FONT_MISC_STATE LyXFont::setMisc(FONT_MISC_STATE newfont,
361 if (newfont == TOGGLE) {
367 lyxerr <<"LyXFont::setMisc: Need state"
368 " ON or OFF to toggle. Setting to ON" << endl;
371 } else if (newfont == IGNORE)
378 /// Updates font settings according to request
379 void LyXFont::update(LyXFont const & newfont,
380 Language const * document_language,
383 if (newfont.family() == family() && toggleall)
384 setFamily(INHERIT_FAMILY); // toggle 'back'
385 else if (newfont.family() != IGNORE_FAMILY)
386 setFamily(newfont.family());
387 // else it's IGNORE_SHAPE
389 // "Old" behaviour: "Setting" bold will toggle bold on/off.
390 switch (newfont.series()) {
393 if (series() == BOLD_SERIES && toggleall)
394 setSeries(MEDIUM_SERIES);
396 setSeries(BOLD_SERIES);
400 setSeries(newfont.series());
406 if (newfont.shape() == shape() && toggleall)
407 setShape(INHERIT_SHAPE); // toggle 'back'
408 else if (newfont.shape() != IGNORE_SHAPE)
409 setShape(newfont.shape());
410 // else it's IGNORE_SHAPE
412 if (newfont.size() != IGNORE_SIZE) {
413 if (newfont.size() == INCREASE_SIZE)
415 else if (newfont.size() == DECREASE_SIZE)
418 setSize(newfont.size());
421 setEmph(setMisc(newfont.emph(), emph()));
422 setUnderbar(setMisc(newfont.underbar(), underbar()));
423 setNoun(setMisc(newfont.noun(), noun()));
425 setNumber(setMisc(newfont.number(), number()));
426 if (newfont.language() == language() && toggleall)
427 if (language() == document_language)
428 setLanguage(default_language);
430 setLanguage(document_language);
431 else if (newfont.language() != ignore_language)
432 setLanguage(newfont.language());
434 if (newfont.color() == color() && toggleall)
435 setColor(LColor::inherit); // toggle 'back'
436 else if (newfont.color() != LColor::ignore)
437 setColor(newfont.color());
441 /// Reduce font to fall back to template where possible
442 void LyXFont::reduce(LyXFont const & tmplt)
444 if (family() == tmplt.family())
445 setFamily(INHERIT_FAMILY);
446 if (series() == tmplt.series())
447 setSeries(INHERIT_SERIES);
448 if (shape() == tmplt.shape())
449 setShape(INHERIT_SHAPE);
450 if (size() == tmplt.size())
451 setSize(INHERIT_SIZE);
452 if (emph() == tmplt.emph())
454 if (underbar() == tmplt.underbar())
455 setUnderbar(INHERIT);
456 if (noun() == tmplt.noun())
458 if (color() == tmplt.color())
459 setColor(LColor::inherit);
463 /// Realize font from a template
464 LyXFont & LyXFont::realize(LyXFont const & tmplt)
466 if (bits == inherit) {
471 if (bits.family == INHERIT_FAMILY) {
472 bits.family = tmplt.bits.family;
474 if (bits.series == INHERIT_SERIES) {
475 bits.series = tmplt.bits.series;
477 if (bits.shape == INHERIT_SHAPE) {
478 bits.shape = tmplt.bits.shape;
480 if (bits.size == INHERIT_SIZE) {
481 bits.size = tmplt.bits.size;
483 if (bits.emph == INHERIT) {
484 bits.emph = tmplt.bits.emph;
486 if (bits.underbar == INHERIT) {
487 bits.underbar = tmplt.bits.underbar;
489 if (bits.noun == INHERIT) {
490 bits.noun = tmplt.bits.noun;
492 if (bits.color == LColor::inherit) {
493 bits.color = tmplt.bits.color;
499 /// Is font resolved?
500 bool LyXFont::resolved() const
502 return (family() != INHERIT_FAMILY && series() != INHERIT_SERIES &&
503 shape() != INHERIT_SHAPE && size() != INHERIT_SIZE &&
504 emph() != INHERIT && underbar() != INHERIT &&
506 color() != LColor::inherit);
510 /// Build GUI description of font state
511 string const LyXFont::stateText(BufferParams * params) const
514 if (family() != INHERIT_FAMILY)
515 ost << _(GUIFamilyNames[family()]) << ", ";
516 if (series() != INHERIT_SERIES)
517 ost << _(GUISeriesNames[series()]) << ", ";
518 if (shape() != INHERIT_SHAPE)
519 ost << _(GUIShapeNames[shape()]) << ", ";
520 if (size() != INHERIT_SIZE)
521 ost << _(GUISizeNames[size()]) << ", ";
522 if (color() != LColor::inherit)
523 ost << lcolor.getGUIName(color()) << ", ";
524 if (emph() != INHERIT) {
526 ost << boost::format(_("Emphasis %1$s, "))
527 % _(GUIMiscNames[emph()]);
529 ost << _("Emphasis ") << _(GUIMiscNames[emph()]) << ", ";
532 if (underbar() != INHERIT) {
534 ost << boost::format(_("Underline %1$s, "))
535 % _(GUIMiscNames[underbar()]);
537 ost << _("Underline ") << _(GUIMiscNames[underbar()]) << ", ";
540 if (noun() != INHERIT) {
542 ost << boost::format(_("Noun %1$s, "))
543 % _(GUIMiscNames[noun()]);
545 ost << _("Noun ") << _(GUIMiscNames[noun()]) << ", ";
549 ost << _("Default") << ", ";
550 if (!params || (language() != params->language)) {
552 ost << boost::format(_("Language: %1$s, "))
553 % _(language()->display());
555 ost << _("Language: ") << _(language()->display()) << ", ";
558 if (number() != OFF) {
560 ost << boost::format(_(" Number %1$s"))
561 % _(GUIMiscNames[number()]);
563 ost << _(" Number ") << _(GUIMiscNames[number()]);
567 string const buf = rtrim(STRCONV(ost.str()), ", ");
572 // Set family according to lyx format string
573 LyXFont & LyXFont::setLyXFamily(string const & fam)
575 string const s = ascii_lowercase(fam);
578 while (s != LyXFamilyNames[i] && LyXFamilyNames[i] != "error") ++i;
579 if (s == LyXFamilyNames[i]) {
580 setFamily(LyXFont::FONT_FAMILY(i));
582 lyxerr << "LyXFont::setLyXFamily: Unknown family `"
583 << s << '\'' << endl;
588 // Set series according to lyx format string
589 LyXFont & LyXFont::setLyXSeries(string const & ser)
591 string const s = ascii_lowercase(ser);
594 while (s != LyXSeriesNames[i] && LyXSeriesNames[i] != "error") ++i;
595 if (s == LyXSeriesNames[i]) {
596 setSeries(LyXFont::FONT_SERIES(i));
598 lyxerr << "LyXFont::setLyXSeries: Unknown series `"
599 << s << '\'' << endl;
604 // Set shape according to lyx format string
605 LyXFont & LyXFont::setLyXShape(string const & sha)
607 string const s = ascii_lowercase(sha);
610 while (s != LyXShapeNames[i] && LyXShapeNames[i] != "error") ++i;
611 if (s == LyXShapeNames[i]) {
612 setShape(LyXFont::FONT_SHAPE(i));
614 lyxerr << "LyXFont::setLyXShape: Unknown shape `"
615 << s << '\'' << endl;
620 // Set size according to lyx format string
621 LyXFont & LyXFont::setLyXSize(string const & siz)
623 string const s = ascii_lowercase(siz);
625 while (s != LyXSizeNames[i] && LyXSizeNames[i] != "error") ++i;
626 if (s == LyXSizeNames[i]) {
627 setSize(LyXFont::FONT_SIZE(i));
629 lyxerr << "LyXFont::setLyXSize: Unknown size `"
630 << s << '\'' << endl;
635 // Set size according to lyx format string
636 LyXFont::FONT_MISC_STATE LyXFont::setLyXMisc(string const & siz)
638 string const s = ascii_lowercase(siz);
640 while (s != LyXMiscNames[i] && LyXMiscNames[i] != "error") ++i;
641 if (s == LyXMiscNames[i])
642 return FONT_MISC_STATE(i);
643 lyxerr << "LyXFont::setLyXMisc: Unknown misc flag `"
644 << s << '\'' << endl;
649 /// Sets color after LyX text format
650 LyXFont & LyXFont::setLyXColor(string const & col)
652 setColor(lcolor.getFromLyXName(col));
657 // Returns size in latex format
658 string const LyXFont::latexSize() const
660 return LaTeXSizeNames[size()];
664 // Read a font definition from given file in lyx format
666 LyXFont & LyXFont::lyxRead(LyXLex & lex)
669 bool finished = false;
670 while (!finished && lex.isOK() && !error) {
672 string const tok = ascii_lowercase(lex.getString());
676 } else if (tok == "endfont") {
678 } else if (tok == "family") {
680 string const ttok = lex.getString();
682 } else if (tok == "series") {
684 string const ttok = lex.getString();
686 } else if (tok == "shape") {
688 string const ttok = lex.getString();
690 } else if (tok == "size") {
692 string const ttok = lex.getString();
694 } else if (tok == "misc") {
696 string const ttok = ascii_lowercase(lex.getString());
698 if (ttok == "no_bar") {
700 } else if (ttok == "no_emph") {
702 } else if (ttok == "no_noun") {
704 } else if (ttok == "emph") {
706 } else if (ttok == "underbar") {
708 } else if (ttok == "noun") {
711 lex.printError("Illegal misc type `$$Token´");
713 } else if (tok == "color") {
715 string const ttok = lex.getString();
718 lex.printError("Unknown tag `$$Token'");
726 /// Writes the changes from this font to orgfont in .lyx format in file
727 void LyXFont::lyxWriteChanges(LyXFont const & orgfont,
731 if (orgfont.family() != family()) {
732 os << "\\family " << LyXFamilyNames[family()] << " \n";
734 if (orgfont.series() != series()) {
735 os << "\\series " << LyXSeriesNames[series()] << " \n";
737 if (orgfont.shape() != shape()) {
738 os << "\\shape " << LyXShapeNames[shape()] << " \n";
740 if (orgfont.size() != size()) {
741 os << "\\size " << LyXSizeNames[size()] << " \n";
743 if (orgfont.emph() != emph()) {
744 os << "\\emph " << LyXMiscNames[emph()] << " \n";
746 if (orgfont.number() != number()) {
747 os << "\\numeric " << LyXMiscNames[number()] << " \n";
749 if (orgfont.underbar() != underbar()) {
750 // This is only for backwards compatibility
751 switch (underbar()) {
752 case OFF: os << "\\bar no \n"; break;
753 case ON: os << "\\bar under \n"; break;
754 case TOGGLE: lyxerr << "LyXFont::lyxWriteFontChanges: "
755 "TOGGLE should not appear here!"
758 case INHERIT: os << "\\bar default \n"; break;
759 case IGNORE: lyxerr << "LyXFont::lyxWriteFontChanges: "
760 "IGNORE should not appear here!"
765 if (orgfont.noun() != noun()) {
766 os << "\\noun " << LyXMiscNames[noun()] << " \n";
768 if (orgfont.color() != color()) {
769 // To make us file compatible with older
770 // lyx versions we emit "default" instead
772 string col_str(lcolor.getLyXName(color()));
773 if (col_str == "inherit") col_str = "default";
774 os << "\\color " << col_str << "\n";
776 if (orgfont.language() != language()) {
778 os << "\\lang " << language()->lang() << "\n";
780 os << "\\lang unknown\n";
785 /// Writes the head of the LaTeX needed to impose this font
786 // Returns number of chars written.
787 int LyXFont::latexWriteStartChanges(ostream & os, LyXFont const & base,
788 LyXFont const & prev) const
793 if (language()->babel() != base.language()->babel() &&
794 language() != prev.language()) {
795 if (isRightToLeft() != prev.isRightToLeft()) {
796 if (isRightToLeft()) {
805 subst(lyxrc.language_command_local,
806 "$$lang", language()->babel());
808 count += tmp.length();
812 if (number() == ON && prev.number() != ON &&
813 language()->lang() == "hebrew") {
821 if (f.family() != INHERIT_FAMILY) {
823 << LaTeXFamilyNames[f.family()]
825 count += strlen(LaTeXFamilyNames[f.family()]) + 2;
826 env = true; //We have opened a new environment
828 if (f.series() != INHERIT_SERIES) {
830 << LaTeXSeriesNames[f.series()]
832 count += strlen(LaTeXSeriesNames[f.series()]) + 2;
833 env = true; //We have opened a new environment
835 if (f.shape() != INHERIT_SHAPE) {
837 << LaTeXShapeNames[f.shape()]
839 count += strlen(LaTeXShapeNames[f.shape()]) + 2;
840 env = true; //We have opened a new environment
842 if (f.color() != LColor::inherit && f.color() != LColor::ignore) {
844 << lcolor.getLaTeXName(f.color())
846 count += lcolor.getLaTeXName(f.color()).length() + 13;
847 env = true; //We have opened a new environment
849 if (f.emph() == ON) {
852 env = true; //We have opened a new environment
854 if (f.underbar() == ON) {
857 env = true; //We have opened a new environment
859 // \noun{} is a LyX special macro
860 if (f.noun() == ON) {
863 env = true; //We have opened a new environment
865 if (f.size() != INHERIT_SIZE) {
866 // If we didn't open an environment above, we open one here
872 << LaTeXSizeNames[f.size()]
874 count += strlen(LaTeXSizeNames[f.size()]) + 2;
880 /// Writes ending block of LaTeX needed to close use of this font
881 // Returns number of chars written
882 // This one corresponds to latexWriteStartChanges(). (Asger)
883 int LyXFont::latexWriteEndChanges(ostream & os, LyXFont const & base,
884 LyXFont const & next) const
889 // reduce the current font to changes against the base
890 // font (of the layout). We use a temporary for this to
891 // avoid changing this font instance, as that would break
895 if (f.family() != INHERIT_FAMILY) {
898 env = true; // Size change need not bother about closing env.
900 if (f.series() != INHERIT_SERIES) {
903 env = true; // Size change need not bother about closing env.
905 if (f.shape() != INHERIT_SHAPE) {
908 env = true; // Size change need not bother about closing env.
910 if (f.color() != LColor::inherit && f.color() != LColor::ignore) {
913 env = true; // Size change need not bother about closing env.
915 if (f.emph() == ON) {
918 env = true; // Size change need not bother about closing env.
920 if (f.underbar() == ON) {
923 env = true; // Size change need not bother about closing env.
925 if (f.noun() == ON) {
928 env = true; // Size change need not bother about closing env.
930 if (f.size() != INHERIT_SIZE) {
931 // We only have to close if only size changed
938 if (number() == ON && next.number() != ON &&
939 language()->lang() == "hebrew") {
944 if (language() != base.language() && language() != next.language()) {
953 LColor::color LyXFont::realColor() const
955 if (color() == LColor::none)
956 return LColor::foreground;
961 // Convert logical attributes to concrete shape attribute
962 LyXFont::FONT_SHAPE LyXFont::realShape() const
964 register FONT_SHAPE s = shape();
978 ostream & operator<<(ostream & o, LyXFont::FONT_MISC_STATE fms)
980 return o << int(fms);