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 * ====================================================== */
16 #pragma implementation
26 #include "support/lstrings.h"
27 #include "bufferparams.h" // stateText
29 #include "BoostFormat.h"
34 #ifndef CXX_GLOBAL_CSTD
44 char const * GUIFamilyNames[LyXFont::NUM_FAMILIES + 2 /* default & error */] =
45 { N_("Roman"), N_("Sans serif"), N_("Typewriter"), N_("Symbol"),
46 "cmr", "cmsy", "cmm", "cmex", "msa", "msb", "eufrak", "wasy",
47 N_("Inherit"), N_("Ignore") };
49 char const * GUISeriesNames[4] =
50 { N_("Medium"), N_("Bold"), N_("Inherit"), N_("Ignore") };
52 char const * GUIShapeNames[6] =
53 { N_("Upright"), N_("Italic"), N_("Slanted"), N_("Smallcaps"), N_("Inherit"),
56 char const * GUISizeNames[14] =
57 { N_("Tiny"), N_("Smallest"), N_("Smaller"), N_("Small"), N_("Normal"), N_("Large"),
58 N_("Larger"), N_("Largest"), N_("Huge"), N_("Huger"), N_("Increase"), N_("Decrease"),
59 N_("Inherit"), N_("Ignore") };
61 char const * GUIMiscNames[5] =
62 { N_("Off"), N_("On"), N_("Toggle"), N_("Inherit"), N_("Ignore") };
66 // Strings used to read and write .lyx format files
68 char const * LyXFamilyNames[LyXFont::NUM_FAMILIES + 2 /* default & error */] =
69 { "roman", "sans", "typewriter", "symbol",
70 "cmr", "cmsy", "cmm", "cmex", "msa", "msb", "eufrak", "wasy",
73 char const * LyXSeriesNames[4] =
74 { "medium", "bold", "default", "error" };
76 char const * LyXShapeNames[6] =
77 { "up", "italic", "slanted", "smallcaps", "default", "error" };
79 char const * LyXSizeNames[14] =
80 { "tiny", "scriptsize", "footnotesize", "small", "normal", "large",
81 "larger", "largest", "huge", "giant",
82 "increase", "decrease", "default", "error" };
84 char const * LyXMiscNames[5] =
85 { "off", "on", "toggle", "default", "error" };
88 // Strings used to write LaTeX files
90 char const * LaTeXFamilyNames[6] =
91 { "textrm", "textsf", "texttt", "error1", "error2", "error3" };
93 char const * LaTeXSeriesNames[4] =
94 { "textmd", "textbf", "error4", "error5" };
96 char const * LaTeXShapeNames[6] =
97 { "textup", "textit", "textsl", "textsc", "error6", "error7" };
99 char const * LaTeXSizeNames[14] =
100 { "tiny", "scriptsize", "footnotesize", "small", "normalsize", "large",
101 "Large", "LARGE", "huge", "Huge", "error8", "error9", "error10", "error11" };
106 // Initialize static member
107 LyXFont::FontBits LyXFont::sane = {
118 // Initialize static member
119 LyXFont::FontBits LyXFont::inherit = {
130 // Initialize static member
131 LyXFont::FontBits LyXFont::ignore = {
143 bool operator==(LyXFont::FontBits const & lhs,
144 LyXFont::FontBits const & rhs)
146 return lhs.family == rhs.family &&
147 lhs.series == rhs.series &&
148 lhs.shape == rhs.shape &&
149 lhs.size == rhs.size &&
150 lhs.color == rhs.color &&
151 lhs.emph == rhs.emph &&
152 lhs.underbar == rhs.underbar &&
153 lhs.noun == rhs.noun &&
154 lhs.number == rhs.number;
159 : bits(sane), lang(default_language)
163 LyXFont::LyXFont(LyXFont::FONT_INIT1)
164 : bits(inherit), lang(default_language)
168 LyXFont::LyXFont(LyXFont::FONT_INIT2)
169 : bits(ignore), lang(ignore_language)
173 LyXFont::LyXFont(LyXFont::FONT_INIT3)
174 : bits(sane), lang(default_language)
178 LyXFont::LyXFont(LyXFont::FONT_INIT1, Language const * l)
179 : bits(inherit), lang(l)
183 LyXFont::LyXFont(LyXFont::FONT_INIT2, Language const * l)
184 : bits(ignore), lang(l)
188 LyXFont::LyXFont(LyXFont::FONT_INIT3, Language const * l)
189 : bits(sane), lang(l)
193 LyXFont::FONT_MISC_STATE LyXFont::underbar() const
195 return bits.underbar;
199 LColor::color LyXFont::color() const
205 Language const * LyXFont::language() const
211 LyXFont::FONT_MISC_STATE LyXFont::number() const
217 bool LyXFont::isRightToLeft() const
219 return lang->RightToLeft();
223 bool LyXFont::isVisibleRightToLeft() const
225 return (lang->RightToLeft() &&
230 LyXFont & LyXFont::setFamily(LyXFont::FONT_FAMILY f)
237 LyXFont & LyXFont::setSeries(LyXFont::FONT_SERIES s)
244 LyXFont & LyXFont::setShape(LyXFont::FONT_SHAPE s)
251 LyXFont & LyXFont::setSize(LyXFont::FONT_SIZE s)
258 LyXFont & LyXFont::setEmph(LyXFont::FONT_MISC_STATE e)
265 LyXFont & LyXFont::setUnderbar(LyXFont::FONT_MISC_STATE u)
272 LyXFont & LyXFont::setNoun(LyXFont::FONT_MISC_STATE n)
279 LyXFont & LyXFont::setColor(LColor::color c)
286 LyXFont & LyXFont::setLanguage(Language const * l)
293 LyXFont & LyXFont::setNumber(LyXFont::FONT_MISC_STATE n)
300 /// Decreases font size by one
301 LyXFont & LyXFont::decSize()
304 case SIZE_HUGER: setSize(SIZE_HUGE); break;
305 case SIZE_HUGE: setSize(SIZE_LARGEST); break;
306 case SIZE_LARGEST: setSize(SIZE_LARGER); break;
307 case SIZE_LARGER: setSize(SIZE_LARGE); break;
308 case SIZE_LARGE: setSize(SIZE_NORMAL); break;
309 case SIZE_NORMAL: setSize(SIZE_SMALL); break;
310 case SIZE_SMALL: setSize(SIZE_FOOTNOTE); break;
311 case SIZE_FOOTNOTE: setSize(SIZE_SCRIPT); break;
312 case SIZE_SCRIPT: setSize(SIZE_TINY); break;
313 case SIZE_TINY: break;
315 lyxerr << "Can't LyXFont::decSize on INCREASE_SIZE" << endl;
318 lyxerr <<"Can't LyXFont::decSize on DECREASE_SIZE" << endl;
321 lyxerr <<"Can't LyXFont::decSize on INHERIT_SIZE" << endl;
324 lyxerr <<"Can't LyXFont::decSize on IGNORE_SIZE" << endl;
331 /// Increases font size by one
332 LyXFont & LyXFont::incSize()
335 case SIZE_HUGER: break;
336 case SIZE_HUGE: setSize(SIZE_HUGER); break;
337 case SIZE_LARGEST: setSize(SIZE_HUGE); break;
338 case SIZE_LARGER: setSize(SIZE_LARGEST); break;
339 case SIZE_LARGE: setSize(SIZE_LARGER); break;
340 case SIZE_NORMAL: setSize(SIZE_LARGE); break;
341 case SIZE_SMALL: setSize(SIZE_NORMAL); break;
342 case SIZE_FOOTNOTE: setSize(SIZE_SMALL); break;
343 case SIZE_SCRIPT: setSize(SIZE_FOOTNOTE); break;
344 case SIZE_TINY: setSize(SIZE_SCRIPT); break;
346 lyxerr <<"Can't LyXFont::incSize on INCREASE_SIZE" << endl;
349 lyxerr <<"Can't LyXFont::incSize on DECREASE_SIZE" << endl;
352 lyxerr <<"Can't LyXFont::incSize on INHERIT_SIZE" << endl;
355 lyxerr <<"Can't LyXFont::incSize on IGNORE_SIZE" << endl;
362 /// Updates a misc setting according to request
363 LyXFont::FONT_MISC_STATE LyXFont::setMisc(FONT_MISC_STATE newfont,
366 if (newfont == TOGGLE) {
372 lyxerr <<"LyXFont::setMisc: Need state"
373 " ON or OFF to toggle. Setting to ON" << endl;
376 } else if (newfont == IGNORE)
383 /// Updates font settings according to request
384 void LyXFont::update(LyXFont const & newfont,
385 Language const * document_language,
388 if (newfont.family() == family() && toggleall)
389 setFamily(INHERIT_FAMILY); // toggle 'back'
390 else if (newfont.family() != IGNORE_FAMILY)
391 setFamily(newfont.family());
392 // else it's IGNORE_SHAPE
394 // "Old" behaviour: "Setting" bold will toggle bold on/off.
395 switch (newfont.series()) {
398 if (series() == BOLD_SERIES && toggleall)
399 setSeries(MEDIUM_SERIES);
401 setSeries(BOLD_SERIES);
405 setSeries(newfont.series());
411 if (newfont.shape() == shape() && toggleall)
412 setShape(INHERIT_SHAPE); // toggle 'back'
413 else if (newfont.shape() != IGNORE_SHAPE)
414 setShape(newfont.shape());
415 // else it's IGNORE_SHAPE
417 if (newfont.size() != IGNORE_SIZE) {
418 if (newfont.size() == INCREASE_SIZE)
420 else if (newfont.size() == DECREASE_SIZE)
423 setSize(newfont.size());
426 setEmph(setMisc(newfont.emph(), emph()));
427 setUnderbar(setMisc(newfont.underbar(), underbar()));
428 setNoun(setMisc(newfont.noun(), noun()));
430 setNumber(setMisc(newfont.number(), number()));
431 if (newfont.language() == language() && toggleall)
432 if (language() == document_language)
433 setLanguage(default_language);
435 setLanguage(document_language);
436 else if (newfont.language() != ignore_language)
437 setLanguage(newfont.language());
439 if (newfont.color() == color() && toggleall)
440 setColor(LColor::inherit); // toggle 'back'
441 else if (newfont.color() != LColor::ignore)
442 setColor(newfont.color());
446 /// Reduce font to fall back to template where possible
447 void LyXFont::reduce(LyXFont const & tmplt)
449 if (family() == tmplt.family())
450 setFamily(INHERIT_FAMILY);
451 if (series() == tmplt.series())
452 setSeries(INHERIT_SERIES);
453 if (shape() == tmplt.shape())
454 setShape(INHERIT_SHAPE);
455 if (size() == tmplt.size())
456 setSize(INHERIT_SIZE);
457 if (emph() == tmplt.emph())
459 if (underbar() == tmplt.underbar())
460 setUnderbar(INHERIT);
461 if (noun() == tmplt.noun())
463 if (color() == tmplt.color())
464 setColor(LColor::inherit);
468 /// Realize font from a template
469 LyXFont & LyXFont::realize(LyXFont const & tmplt)
471 if (bits == inherit) {
476 if (bits.family == INHERIT_FAMILY) {
477 bits.family = tmplt.bits.family;
479 if (bits.series == INHERIT_SERIES) {
480 bits.series = tmplt.bits.series;
482 if (bits.shape == INHERIT_SHAPE) {
483 bits.shape = tmplt.bits.shape;
485 if (bits.size == INHERIT_SIZE) {
486 bits.size = tmplt.bits.size;
488 if (bits.emph == INHERIT) {
489 bits.emph = tmplt.bits.emph;
491 if (bits.underbar == INHERIT) {
492 bits.underbar = tmplt.bits.underbar;
494 if (bits.noun == INHERIT) {
495 bits.noun = tmplt.bits.noun;
497 if (bits.color == LColor::inherit) {
498 bits.color = tmplt.bits.color;
504 /// Is font resolved?
505 bool LyXFont::resolved() const
507 return (family() != INHERIT_FAMILY && series() != INHERIT_SERIES &&
508 shape() != INHERIT_SHAPE && size() != INHERIT_SIZE &&
509 emph() != INHERIT && underbar() != INHERIT &&
511 color() != LColor::inherit);
515 /// Build GUI description of font state
516 string const LyXFont::stateText(BufferParams * params) const
519 if (family() != INHERIT_FAMILY)
520 ost << _(GUIFamilyNames[family()]) << ", ";
521 if (series() != INHERIT_SERIES)
522 ost << _(GUISeriesNames[series()]) << ", ";
523 if (shape() != INHERIT_SHAPE)
524 ost << _(GUIShapeNames[shape()]) << ", ";
525 if (size() != INHERIT_SIZE)
526 ost << _(GUISizeNames[size()]) << ", ";
527 if (color() != LColor::inherit)
528 ost << lcolor.getGUIName(color()) << ", ";
529 if (emph() != INHERIT) {
531 ost << boost::format(_("Emphasis %1$s, "))
532 % _(GUIMiscNames[emph()]);
534 ost << _("Emphasis ") << _(GUIMiscNames[emph()]) << ", ";
537 if (underbar() != INHERIT) {
539 ost << boost::format(_("Underline %1$s, "))
540 % _(GUIMiscNames[underbar()]);
542 ost << _("Underline ") << _(GUIMiscNames[underbar()]) << ", ";
545 if (noun() != INHERIT) {
547 ost << boost::format(_("Noun %1$s, "))
548 % _(GUIMiscNames[noun()]);
550 ost << _("Noun ") << _(GUIMiscNames[noun()]) << ", ";
554 ost << _("Default") << ", ";
555 if (!params || (language() != params->language)) {
557 ost << boost::format(_("Language: %1$s, "))
558 % _(language()->display());
560 ost << _("Language: ") << _(language()->display()) << ", ";
563 if (number() != OFF) {
565 ost << boost::format(_(" Number %1$s"))
566 % _(GUIMiscNames[number()]);
568 ost << _(" Number ") << _(GUIMiscNames[number()]);
572 string const buf = rtrim(STRCONV(ost.str()), ", ");
577 // Set family according to lyx format string
578 LyXFont & LyXFont::setLyXFamily(string const & fam)
580 string const s = ascii_lowercase(fam);
583 while (s != LyXFamilyNames[i] && LyXFamilyNames[i] != "error") ++i;
584 if (s == LyXFamilyNames[i]) {
585 setFamily(LyXFont::FONT_FAMILY(i));
587 lyxerr << "LyXFont::setLyXFamily: Unknown family `"
588 << s << '\'' << endl;
593 // Set series according to lyx format string
594 LyXFont & LyXFont::setLyXSeries(string const & ser)
596 string const s = ascii_lowercase(ser);
599 while (s != LyXSeriesNames[i] && LyXSeriesNames[i] != "error") ++i;
600 if (s == LyXSeriesNames[i]) {
601 setSeries(LyXFont::FONT_SERIES(i));
603 lyxerr << "LyXFont::setLyXSeries: Unknown series `"
604 << s << '\'' << endl;
609 // Set shape according to lyx format string
610 LyXFont & LyXFont::setLyXShape(string const & sha)
612 string const s = ascii_lowercase(sha);
615 while (s != LyXShapeNames[i] && LyXShapeNames[i] != "error") ++i;
616 if (s == LyXShapeNames[i]) {
617 setShape(LyXFont::FONT_SHAPE(i));
619 lyxerr << "LyXFont::setLyXShape: Unknown shape `"
620 << s << '\'' << endl;
625 // Set size according to lyx format string
626 LyXFont & LyXFont::setLyXSize(string const & siz)
628 string const s = ascii_lowercase(siz);
630 while (s != LyXSizeNames[i] && LyXSizeNames[i] != "error") ++i;
631 if (s == LyXSizeNames[i]) {
632 setSize(LyXFont::FONT_SIZE(i));
634 lyxerr << "LyXFont::setLyXSize: Unknown size `"
635 << s << '\'' << endl;
640 // Set size according to lyx format string
641 LyXFont::FONT_MISC_STATE LyXFont::setLyXMisc(string const & siz)
643 string const s = ascii_lowercase(siz);
645 while (s != LyXMiscNames[i] && LyXMiscNames[i] != "error") ++i;
646 if (s == LyXMiscNames[i])
647 return FONT_MISC_STATE(i);
648 lyxerr << "LyXFont::setLyXMisc: Unknown misc flag `"
649 << s << '\'' << endl;
654 /// Sets color after LyX text format
655 LyXFont & LyXFont::setLyXColor(string const & col)
657 setColor(lcolor.getFromLyXName(col));
662 // Returns size in latex format
663 string const LyXFont::latexSize() const
665 return LaTeXSizeNames[size()];
669 // Read a font definition from given file in lyx format
671 LyXFont & LyXFont::lyxRead(LyXLex & lex)
674 bool finished = false;
675 while (!finished && lex.isOK() && !error) {
677 string const tok = ascii_lowercase(lex.getString());
681 } else if (tok == "endfont") {
683 } else if (tok == "family") {
685 string const ttok = lex.getString();
687 } else if (tok == "series") {
689 string const ttok = lex.getString();
691 } else if (tok == "shape") {
693 string const ttok = lex.getString();
695 } else if (tok == "size") {
697 string const ttok = lex.getString();
699 } else if (tok == "misc") {
701 string const ttok = ascii_lowercase(lex.getString());
703 if (ttok == "no_bar") {
705 } else if (ttok == "no_emph") {
707 } else if (ttok == "no_noun") {
709 } else if (ttok == "emph") {
711 } else if (ttok == "underbar") {
713 } else if (ttok == "noun") {
716 lex.printError("Illegal misc type `$$Token´");
718 } else if (tok == "color") {
720 string const ttok = lex.getString();
723 lex.printError("Unknown tag `$$Token'");
731 /// Writes the changes from this font to orgfont in .lyx format in file
732 void LyXFont::lyxWriteChanges(LyXFont const & orgfont,
736 if (orgfont.family() != family()) {
737 os << "\\family " << LyXFamilyNames[family()] << " \n";
739 if (orgfont.series() != series()) {
740 os << "\\series " << LyXSeriesNames[series()] << " \n";
742 if (orgfont.shape() != shape()) {
743 os << "\\shape " << LyXShapeNames[shape()] << " \n";
745 if (orgfont.size() != size()) {
746 os << "\\size " << LyXSizeNames[size()] << " \n";
748 if (orgfont.emph() != emph()) {
749 os << "\\emph " << LyXMiscNames[emph()] << " \n";
751 if (orgfont.number() != number()) {
752 os << "\\numeric " << LyXMiscNames[number()] << " \n";
754 if (orgfont.underbar() != underbar()) {
755 // This is only for backwards compatibility
756 switch (underbar()) {
757 case OFF: os << "\\bar no \n"; break;
758 case ON: os << "\\bar under \n"; break;
759 case TOGGLE: lyxerr << "LyXFont::lyxWriteFontChanges: "
760 "TOGGLE should not appear here!"
763 case INHERIT: os << "\\bar default \n"; break;
764 case IGNORE: lyxerr << "LyXFont::lyxWriteFontChanges: "
765 "IGNORE should not appear here!"
770 if (orgfont.noun() != noun()) {
771 os << "\\noun " << LyXMiscNames[noun()] << " \n";
773 if (orgfont.color() != color()) {
774 // To make us file compatible with older
775 // lyx versions we emit "default" instead
777 string col_str(lcolor.getLyXName(color()));
778 if (col_str == "inherit") col_str = "default";
779 os << "\\color " << col_str << "\n";
781 if (orgfont.language() != language()) {
783 os << "\\lang " << language()->lang() << "\n";
785 os << "\\lang unknown\n";
790 /// Writes the head of the LaTeX needed to impose this font
791 // Returns number of chars written.
792 int LyXFont::latexWriteStartChanges(ostream & os, LyXFont const & base,
793 LyXFont const & prev) const
798 if (language()->babel() != base.language()->babel() &&
799 language() != prev.language()) {
800 if (isRightToLeft() != prev.isRightToLeft()) {
801 if (isRightToLeft()) {
810 subst(lyxrc.language_command_local,
811 "$$lang", language()->babel());
813 count += tmp.length();
817 if (number() == ON && prev.number() != ON &&
818 language()->lang() == "hebrew") {
826 if (f.family() != INHERIT_FAMILY) {
828 << LaTeXFamilyNames[f.family()]
830 count += strlen(LaTeXFamilyNames[f.family()]) + 2;
831 env = true; //We have opened a new environment
833 if (f.series() != INHERIT_SERIES) {
835 << LaTeXSeriesNames[f.series()]
837 count += strlen(LaTeXSeriesNames[f.series()]) + 2;
838 env = true; //We have opened a new environment
840 if (f.shape() != INHERIT_SHAPE) {
842 << LaTeXShapeNames[f.shape()]
844 count += strlen(LaTeXShapeNames[f.shape()]) + 2;
845 env = true; //We have opened a new environment
847 if (f.color() != LColor::inherit && f.color() != LColor::ignore) {
849 << lcolor.getLaTeXName(f.color())
851 count += lcolor.getLaTeXName(f.color()).length() + 13;
852 env = true; //We have opened a new environment
854 if (f.emph() == ON) {
857 env = true; //We have opened a new environment
859 if (f.underbar() == ON) {
862 env = true; //We have opened a new environment
864 // \noun{} is a LyX special macro
865 if (f.noun() == ON) {
868 env = true; //We have opened a new environment
870 if (f.size() != INHERIT_SIZE) {
871 // If we didn't open an environment above, we open one here
877 << LaTeXSizeNames[f.size()]
879 count += strlen(LaTeXSizeNames[f.size()]) + 2;
885 /// Writes ending block of LaTeX needed to close use of this font
886 // Returns number of chars written
887 // This one corresponds to latexWriteStartChanges(). (Asger)
888 int LyXFont::latexWriteEndChanges(ostream & os, LyXFont const & base,
889 LyXFont const & next) const
894 // reduce the current font to changes against the base
895 // font (of the layout). We use a temporary for this to
896 // avoid changing this font instance, as that would break
900 if (f.family() != INHERIT_FAMILY) {
903 env = true; // Size change need not bother about closing env.
905 if (f.series() != INHERIT_SERIES) {
908 env = true; // Size change need not bother about closing env.
910 if (f.shape() != INHERIT_SHAPE) {
913 env = true; // Size change need not bother about closing env.
915 if (f.color() != LColor::inherit && f.color() != LColor::ignore) {
918 env = true; // Size change need not bother about closing env.
920 if (f.emph() == ON) {
923 env = true; // Size change need not bother about closing env.
925 if (f.underbar() == ON) {
928 env = true; // Size change need not bother about closing env.
930 if (f.noun() == ON) {
933 env = true; // Size change need not bother about closing env.
935 if (f.size() != INHERIT_SIZE) {
936 // We only have to close if only size changed
943 if (number() == ON && next.number() != ON &&
944 language()->lang() == "hebrew") {
949 if (language() != base.language() && language() != next.language()) {
958 LColor::color LyXFont::realColor() const
960 if (color() == LColor::none)
961 return LColor::foreground;
966 // Convert logical attributes to concrete shape attribute
967 LyXFont::FONT_SHAPE LyXFont::realShape() const
969 register FONT_SHAPE s = shape();
983 ostream & operator<<(ostream & o, LyXFont::FONT_MISC_STATE fms)
985 return o << int(fms);