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
25 #include "FontLoader.h"
26 #include "support/lstrings.h"
27 #include "bufferparams.h" // stateText
32 #ifndef CXX_GLOBAL_CSTD
43 char const * GUIFamilyNames[6] =
44 { N_("Roman"), N_("Sans serif"), N_("Typewriter"), N_("Symbol"), N_("Inherit"),
47 char const * GUISeriesNames[4] =
48 { N_("Medium"), N_("Bold"), N_("Inherit"), N_("Ignore") };
50 char const * GUIShapeNames[6] =
51 { N_("Upright"), N_("Italic"), N_("Slanted"), N_("Smallcaps"), N_("Inherit"),
54 char const * GUISizeNames[14] =
55 { N_("Tiny"), N_("Smallest"), N_("Smaller"), N_("Small"), N_("Normal"), N_("Large"),
56 N_("Larger"), N_("Largest"), N_("Huge"), N_("Huger"), N_("Increase"), N_("Decrease"),
57 N_("Inherit"), N_("Ignore") };
59 char const * GUIMiscNames[5] =
60 { N_("Off"), N_("On"), N_("Toggle"), N_("Inherit"), N_("Ignore") };
64 // Strings used to read and write .lyx format files
66 char const * LyXFamilyNames[6] =
67 { "roman", "sans", "typewriter", "symbol", "default", "error" };
69 char const * LyXSeriesNames[4] =
70 { "medium", "bold", "default", "error" };
72 char const * LyXShapeNames[6] =
73 { "up", "italic", "slanted", "smallcaps", "default", "error" };
75 char const * LyXSizeNames[14] =
76 { "tiny", "scriptsize", "footnotesize", "small", "normal", "large",
77 "larger", "largest", "huge", "giant",
78 "increase-error", "decrease-error", "default", "error" };
80 char const * LyXMiscNames[5] =
81 { "off", "on", "toggle", "default", "error" };
84 // Strings used to write LaTeX files
86 char const * LaTeXFamilyNames[6] =
87 { "textrm", "textsf", "texttt", "error1", "error2", "error3" };
89 char const * LaTeXSeriesNames[4] =
90 { "textmd", "textbf", "error4", "error5" };
92 char const * LaTeXShapeNames[6] =
93 { "textup", "textit", "textsl", "textsc", "error6", "error7" };
95 char const * LaTeXSizeNames[14] =
96 { "tiny", "scriptsize", "footnotesize", "small", "normalsize", "large",
97 "Large", "LARGE", "huge", "Huge", "error8", "error9", "error10", "error11" };
102 // Initialize static member
103 LyXFont::FontBits LyXFont::sane = {
117 // Initialize static member
118 LyXFont::FontBits LyXFont::inherit = {
132 // Initialize static member
133 LyXFont::FontBits LyXFont::ignore = {
148 bool LyXFont::FontBits::operator==(LyXFont::FontBits const & fb1) const
150 return fb1.family == family &&
151 fb1.series == series &&
152 fb1.shape == shape &&
154 fb1.color == color &&
156 fb1.underbar == underbar &&
159 fb1.latex == latex &&
161 fb1.number == number;
165 bool LyXFont::FontBits::operator!=(LyXFont::FontBits const & fb1) const
167 return !(fb1 == *this);
171 LyXFont::LyXFont(LyXFont::FONT_INIT1)
172 : bits(inherit), lang(inherit_language)
176 LyXFont::LyXFont(LyXFont::FONT_INIT2)
177 : bits(ignore), lang(ignore_language)
181 LyXFont::LyXFont(LyXFont::FONT_INIT3)
182 : bits(sane), lang(default_language)
186 LyXFont::LyXFont(LyXFont::FONT_INIT1, Language const * l)
187 : bits(inherit), lang(l)
191 LyXFont::LyXFont(LyXFont::FONT_INIT2, Language const * l)
192 : bits(ignore), lang(l)
196 LyXFont::LyXFont(LyXFont::FONT_INIT3, Language const * l)
197 : bits(sane), lang(l)
201 LyXFont::FONT_MISC_STATE LyXFont::underbar() const
203 return bits.underbar;
208 LyXFont::FONT_MISC_STATE LyXFont::latex() const
215 LColor::color LyXFont::color() const
221 Language const * LyXFont::language() const
227 LyXFont::FONT_MISC_STATE LyXFont::number() const
233 bool LyXFont::isRightToLeft() const
235 return lang->RightToLeft();
239 bool LyXFont::isVisibleRightToLeft() const
241 return (lang->RightToLeft() &&
249 LyXFont & LyXFont::setFamily(LyXFont::FONT_FAMILY f)
256 LyXFont & LyXFont::setSeries(LyXFont::FONT_SERIES s)
263 LyXFont & LyXFont::setShape(LyXFont::FONT_SHAPE s)
270 LyXFont & LyXFont::setSize(LyXFont::FONT_SIZE s)
277 LyXFont & LyXFont::setEmph(LyXFont::FONT_MISC_STATE e)
284 LyXFont & LyXFont::setUnderbar(LyXFont::FONT_MISC_STATE u)
291 LyXFont & LyXFont::setNoun(LyXFont::FONT_MISC_STATE n)
299 LyXFont & LyXFont::setLatex(LyXFont::FONT_MISC_STATE l)
307 LyXFont & LyXFont::setColor(LColor::color c)
314 LyXFont & LyXFont::setLanguage(Language const * l)
321 LyXFont & LyXFont::setNumber(LyXFont::FONT_MISC_STATE n)
328 /// Decreases font size by one
329 LyXFont & LyXFont::decSize()
332 case SIZE_HUGER: setSize(SIZE_HUGE); break;
333 case SIZE_HUGE: setSize(SIZE_LARGEST); break;
334 case SIZE_LARGEST: setSize(SIZE_LARGER); break;
335 case SIZE_LARGER: setSize(SIZE_LARGE); break;
336 case SIZE_LARGE: setSize(SIZE_NORMAL); break;
337 case SIZE_NORMAL: setSize(SIZE_SMALL); break;
338 case SIZE_SMALL: setSize(SIZE_FOOTNOTE); break;
339 case SIZE_FOOTNOTE: setSize(SIZE_SCRIPT); break;
340 case SIZE_SCRIPT: setSize(SIZE_TINY); break;
341 case SIZE_TINY: break;
343 lyxerr << "Can't LyXFont::decSize on INCREASE_SIZE" << endl;
346 lyxerr <<"Can't LyXFont::decSize on DECREASE_SIZE" << endl;
349 lyxerr <<"Can't LyXFont::decSize on INHERIT_SIZE" << endl;
352 lyxerr <<"Can't LyXFont::decSize on IGNORE_SIZE" << endl;
359 /// Increases font size by one
360 LyXFont & LyXFont::incSize()
363 case SIZE_HUGER: break;
364 case SIZE_HUGE: setSize(SIZE_HUGER); break;
365 case SIZE_LARGEST: setSize(SIZE_HUGE); break;
366 case SIZE_LARGER: setSize(SIZE_LARGEST); break;
367 case SIZE_LARGE: setSize(SIZE_LARGER); break;
368 case SIZE_NORMAL: setSize(SIZE_LARGE); break;
369 case SIZE_SMALL: setSize(SIZE_NORMAL); break;
370 case SIZE_FOOTNOTE: setSize(SIZE_SMALL); break;
371 case SIZE_SCRIPT: setSize(SIZE_FOOTNOTE); break;
372 case SIZE_TINY: setSize(SIZE_SCRIPT); break;
374 lyxerr <<"Can't LyXFont::incSize on INCREASE_SIZE" << endl;
377 lyxerr <<"Can't LyXFont::incSize on DECREASE_SIZE" << endl;
380 lyxerr <<"Can't LyXFont::incSize on INHERIT_SIZE" << endl;
383 lyxerr <<"Can't LyXFont::incSize on IGNORE_SIZE" << endl;
390 /// Updates a misc setting according to request
391 LyXFont::FONT_MISC_STATE LyXFont::setMisc(FONT_MISC_STATE newfont,
394 if (newfont == TOGGLE) {
400 lyxerr <<"LyXFont::setMisc: Need state"
401 " ON or OFF to toggle. Setting to ON" << endl;
404 } else if (newfont == IGNORE)
411 /// Updates font settings according to request
412 void LyXFont::update(LyXFont const & newfont, bool toggleall)
414 if (newfont.family() == family() && toggleall)
415 setFamily(INHERIT_FAMILY); // toggle 'back'
416 else if (newfont.family() != IGNORE_FAMILY)
417 setFamily(newfont.family());
418 // else it's IGNORE_SHAPE
420 // "Old" behaviour: "Setting" bold will toggle bold on/off.
421 switch (newfont.series()) {
424 if (series() == BOLD_SERIES && toggleall)
425 setSeries(MEDIUM_SERIES);
427 setSeries(BOLD_SERIES);
431 setSeries(newfont.series());
437 if (newfont.shape() == shape() && toggleall)
438 setShape(INHERIT_SHAPE); // toggle 'back'
439 else if (newfont.shape() != IGNORE_SHAPE)
440 setShape(newfont.shape());
441 // else it's IGNORE_SHAPE
443 if (newfont.size() != IGNORE_SIZE) {
444 if (newfont.size() == INCREASE_SIZE)
446 else if (newfont.size() == DECREASE_SIZE)
449 setSize(newfont.size());
452 setEmph(setMisc(newfont.emph(), emph()));
453 setUnderbar(setMisc(newfont.underbar(), underbar()));
454 setNoun(setMisc(newfont.noun(), noun()));
456 setLatex(setMisc(newfont.latex(), latex()));
459 setNumber(setMisc(newfont.number(), number()));
460 if (newfont.language() == language() && toggleall)
461 setLanguage(inherit_language);
462 else if (newfont.language() != ignore_language)
463 setLanguage(newfont.language());
465 if (newfont.color() == color() && toggleall)
466 setColor(LColor::inherit); // toggle 'back'
467 else if (newfont.color() != LColor::ignore)
468 setColor(newfont.color());
472 /// Reduce font to fall back to template where possible
473 void LyXFont::reduce(LyXFont const & tmplt)
475 if (family() == tmplt.family())
476 setFamily(INHERIT_FAMILY);
477 if (series() == tmplt.series())
478 setSeries(INHERIT_SERIES);
479 if (shape() == tmplt.shape())
480 setShape(INHERIT_SHAPE);
481 if (size() == tmplt.size())
482 setSize(INHERIT_SIZE);
483 if (emph() == tmplt.emph())
485 if (underbar() == tmplt.underbar())
486 setUnderbar(INHERIT);
487 if (noun() == tmplt.noun())
490 if (latex() == tmplt.latex())
493 if (color() == tmplt.color())
494 setColor(LColor::inherit);
495 if (language() == tmplt.language())
496 setLanguage(inherit_language);
500 /// Realize font from a template
501 LyXFont & LyXFont::realize(LyXFont const & tmplt, Language const * deflang)
503 if (language() == inherit_language) {
504 if (tmplt.language() == inherit_language ||
505 tmplt.language() == ignore_language ||
506 tmplt.language() == default_language)
508 setLanguage(deflang);
510 setLanguage(tmplt.language());
513 if (bits == inherit) {
518 if (bits.family == INHERIT_FAMILY) {
519 bits.family = tmplt.bits.family;
521 if (bits.series == INHERIT_SERIES) {
522 bits.series = tmplt.bits.series;
524 if (bits.shape == INHERIT_SHAPE) {
525 bits.shape = tmplt.bits.shape;
527 if (bits.size == INHERIT_SIZE) {
528 bits.size = tmplt.bits.size;
530 if (bits.emph == INHERIT) {
531 bits.emph = tmplt.bits.emph;
533 if (bits.underbar == INHERIT) {
534 bits.underbar = tmplt.bits.underbar;
536 if (bits.noun == INHERIT) {
537 bits.noun = tmplt.bits.noun;
540 if (bits.latex == INHERIT) {
541 bits.latex = tmplt.bits.latex;
544 if (bits.color == LColor::inherit) {
545 bits.color = tmplt.bits.color;
551 /// Is font resolved?
552 bool LyXFont::resolved() const
554 return (family() != INHERIT_FAMILY && series() != INHERIT_SERIES &&
555 shape() != INHERIT_SHAPE && size() != INHERIT_SIZE &&
556 emph() != INHERIT && underbar() != INHERIT &&
559 latex() != INHERIT &&
561 color() != LColor::inherit &&
562 language() != inherit_language);
566 /// Build GUI description of font state
567 string const LyXFont::stateText(BufferParams * params) const
570 if (family() != INHERIT_FAMILY)
571 ost << _(GUIFamilyNames[family()]) << ", ";
572 if (series() != INHERIT_SERIES)
573 ost << _(GUISeriesNames[series()]) << ", ";
574 if (shape() != INHERIT_SHAPE)
575 ost << _(GUIShapeNames[shape()]) << ", ";
576 if (size() != INHERIT_SIZE)
577 ost << _(GUISizeNames[size()]) << ", ";
578 if (color() != LColor::inherit)
579 ost << lcolor.getGUIName(color()) << ", ";
580 if (emph() != INHERIT)
581 ost << _("Emphasis ")
582 << _(GUIMiscNames[emph()]) << ", ";
583 if (underbar() != INHERIT)
584 ost << _("Underline ")
585 << _(GUIMiscNames[underbar()]) << ", ";
586 if (noun() != INHERIT)
587 ost << _("Noun ") << _(GUIMiscNames[noun()]) << ", ";
589 if (latex() != INHERIT)
590 ost << _("Latex ") << _(GUIMiscNames[latex()]) << ", ";
593 ost << _("Default") << ", ";
594 if (!params || (language() != params->language))
595 ost << _("Language: ") << _(language()->display()) << ", ";
597 ost << _(" Number ") << _(GUIMiscNames[number()]);
599 string buf(ost.str().c_str());
600 buf = strip(buf, ' ');
601 buf = strip(buf, ',');
606 // Set family according to lyx format string
607 LyXFont & LyXFont::setLyXFamily(string const & fam)
609 string const s = lowercase(fam);
612 while (s != LyXFamilyNames[i] && LyXFamilyNames[i] != "error") ++i;
613 if (s == LyXFamilyNames[i]) {
614 setFamily(LyXFont::FONT_FAMILY(i));
616 lyxerr << "LyXFont::setLyXFamily: Unknown family `"
617 << s << '\'' << endl;
622 // Set series according to lyx format string
623 LyXFont & LyXFont::setLyXSeries(string const & ser)
625 string const s = lowercase(ser);
628 while (s != LyXSeriesNames[i] && LyXSeriesNames[i] != "error") ++i;
629 if (s == LyXSeriesNames[i]) {
630 setSeries(LyXFont::FONT_SERIES(i));
632 lyxerr << "LyXFont::setLyXSeries: Unknown series `"
633 << s << '\'' << endl;
638 // Set shape according to lyx format string
639 LyXFont & LyXFont::setLyXShape(string const & sha)
641 string const s = lowercase(sha);
644 while (s != LyXShapeNames[i] && LyXShapeNames[i] != "error") ++i;
645 if (s == LyXShapeNames[i]) {
646 setShape(LyXFont::FONT_SHAPE(i));
648 lyxerr << "LyXFont::setLyXShape: Unknown shape `"
649 << s << '\'' << endl;
654 // Set size according to lyx format string
655 LyXFont & LyXFont::setLyXSize(string const & siz)
657 string const s = lowercase(siz);
659 while (s != LyXSizeNames[i] && LyXSizeNames[i] != "error") ++i;
660 if (s == LyXSizeNames[i]) {
661 setSize(LyXFont::FONT_SIZE(i));
663 lyxerr << "LyXFont::setLyXSize: Unknown size `"
664 << s << '\'' << endl;
669 // Set size according to lyx format string
670 LyXFont::FONT_MISC_STATE LyXFont::setLyXMisc(string const & siz)
672 string const s = lowercase(siz);
674 while (s != LyXMiscNames[i] && LyXMiscNames[i] != "error") ++i;
675 if (s == LyXMiscNames[i])
676 return FONT_MISC_STATE(i);
677 lyxerr << "LyXFont::setLyXMisc: Unknown misc flag `"
678 << s << '\'' << endl;
683 /// Sets color after LyX text format
684 LyXFont & LyXFont::setLyXColor(string const & col)
686 setColor(lcolor.getFromLyXName(col));
691 // Returns size in latex format
692 string const LyXFont::latexSize() const
694 return LaTeXSizeNames[size()];
698 // Read a font definition from given file in lyx format
700 LyXFont & LyXFont::lyxRead(LyXLex & lex)
703 bool finished = false;
704 while (!finished && lex.IsOK() && !error) {
706 string const tok = lowercase(lex.GetString());
710 } else if (tok == "endfont") {
712 } else if (tok == "family") {
714 string const ttok = lex.GetString();
716 } else if (tok == "series") {
718 string const ttok = lex.GetString();
720 } else if (tok == "shape") {
722 string const ttok = lex.GetString();
724 } else if (tok == "size") {
726 string const ttok = lex.GetString();
729 } else if (tok == "latex") {
731 string const ttok = lowercase(lex.GetString());
733 if (ttok == "no_latex") {
735 } else if (ttok == "latex") {
738 lex.printError("Illegal LaTeX type`$$Token'");
741 } else if (tok == "misc") {
743 string const ttok = lowercase(lex.GetString());
745 if (ttok == "no_bar") {
747 } else if (ttok == "no_emph") {
749 } else if (ttok == "no_noun") {
751 } else if (ttok == "emph") {
753 } else if (ttok == "underbar") {
755 } else if (ttok == "noun") {
758 lex.printError("Illegal misc type `$$Token´");
760 } else if (tok == "color") {
762 string const ttok = lex.GetString();
765 lex.printError("Unknown tag `$$Token'");
773 /// Writes the changes from this font to orgfont in .lyx format in file
774 void LyXFont::lyxWriteChanges(LyXFont const & orgfont,
775 Language const * doclang, ostream & os) const
778 if (orgfont.family() != family()) {
779 os << "\\family " << LyXFamilyNames[family()] << " \n";
781 if (orgfont.series() != series()) {
782 os << "\\series " << LyXSeriesNames[series()] << " \n";
784 if (orgfont.shape() != shape()) {
785 os << "\\shape " << LyXShapeNames[shape()] << " \n";
787 if (orgfont.size() != size()) {
788 os << "\\size " << LyXSizeNames[size()] << " \n";
790 if (orgfont.emph() != emph()) {
791 os << "\\emph " << LyXMiscNames[emph()] << " \n";
793 if (orgfont.number() != number()) {
794 os << "\\numeric " << LyXMiscNames[number()] << " \n";
796 if (orgfont.underbar() != underbar()) {
797 // This is only for backwards compatibility
798 switch (underbar()) {
799 case OFF: os << "\\bar no \n"; break;
800 case ON: os << "\\bar under \n"; break;
801 case TOGGLE: lyxerr << "LyXFont::lyxWriteFontChanges: "
802 "TOGGLE should not appear here!"
805 case INHERIT: os << "\\bar default \n"; break;
806 case IGNORE: lyxerr << "LyXFont::lyxWriteFontChanges: "
807 "IGNORE should not appear here!"
812 if (orgfont.noun() != noun()) {
813 os << "\\noun " << LyXMiscNames[noun()] << " \n";
816 if (orgfont.latex() != latex()) {
817 // This is only for backwards compatibility
819 case OFF: os << "\\latex no_latex \n"; break;
820 case ON: os << "\\latex latex \n"; break;
821 case TOGGLE: lyxerr << "LyXFont::lyxWriteFontChanges: "
822 "TOGGLE should not appear here!"
825 case INHERIT: os << "\\latex default \n"; break;
826 case IGNORE: lyxerr << "LyXFont::lyxWriteFontChanges: "
827 "IGNORE should not appear here!"
833 if (orgfont.color() != color()) {
834 // To make us file compatible with older
835 // lyx versions we emit "default" instead
837 string col_str(lcolor.getLyXName(color()));
838 if (col_str == "inherit") col_str = "default";
839 os << "\\color " << col_str << "\n";
841 if (orgfont.language() != language()) {
842 if (language() == inherit_language)
843 os << "\\lang " << doclang->lang() << "\n";
845 os << "\\lang " << language()->lang() << "\n";
847 os << "\\lang unknown\n";
852 /// Writes the head of the LaTeX needed to impose this font
853 // Returns number of chars written.
854 int LyXFont::latexWriteStartChanges(ostream & os, LyXFont const & base,
855 LyXFont const & prev) const
860 if (language()->babel() != base.language()->babel() &&
861 language()->babel() != prev.language()->babel()) {
862 if (isRightToLeft() != prev.isRightToLeft()) {
863 if (isRightToLeft()) {
872 subst(lyxrc.language_command_local,
873 "$$lang", language()->babel());
875 count += tmp.length();
879 if (number() == ON && prev.number() != ON &&
880 language()->lang() == "hebrew") {
888 if (f.family() != INHERIT_FAMILY) {
890 << LaTeXFamilyNames[f.family()]
892 count += strlen(LaTeXFamilyNames[f.family()]) + 2;
893 env = true; //We have opened a new environment
895 if (f.series() != INHERIT_SERIES) {
897 << LaTeXSeriesNames[f.series()]
899 count += strlen(LaTeXSeriesNames[f.series()]) + 2;
900 env = true; //We have opened a new environment
902 if (f.shape() != INHERIT_SHAPE) {
904 << LaTeXShapeNames[f.shape()]
906 count += strlen(LaTeXShapeNames[f.shape()]) + 2;
907 env = true; //We have opened a new environment
909 if (f.color() != LColor::inherit && f.color() != LColor::ignore) {
911 << lcolor.getLaTeXName(f.color())
913 count += lcolor.getLaTeXName(f.color()).length() + 13;
914 env = true; //We have opened a new environment
916 if (f.emph() == ON) {
919 env = true; //We have opened a new environment
921 if (f.underbar() == ON) {
924 env = true; //We have opened a new environment
926 // \noun{} is a LyX special macro
927 if (f.noun() == ON) {
930 env = true; //We have opened a new environment
932 if (f.size() != INHERIT_SIZE) {
933 // If we didn't open an environment above, we open one here
939 << LaTeXSizeNames[f.size()]
941 count += strlen(LaTeXSizeNames[f.size()]) + 2;
947 /// Writes ending block of LaTeX needed to close use of this font
948 // Returns number of chars written
949 // This one corresponds to latexWriteStartChanges(). (Asger)
950 int LyXFont::latexWriteEndChanges(ostream & os, LyXFont const & base,
951 LyXFont const & next) const
956 LyXFont f = *this; // why do you need this?
957 f.reduce(base); // why isn't this just "reduce(base);" (Lgb)
958 // Because this function is const. Everything breaks if this
959 // method changes the font it represents. There is no speed penalty
960 // by using the temporary. (Asger)
963 if (f.family() != INHERIT_FAMILY) {
966 env = true; // Size change need not bother about closing env.
968 if (f.series() != INHERIT_SERIES) {
971 env = true; // Size change need not bother about closing env.
973 if (f.shape() != INHERIT_SHAPE) {
976 env = true; // Size change need not bother about closing env.
978 if (f.color() != LColor::inherit && f.color() != LColor::ignore) {
981 env = true; // Size change need not bother about closing env.
983 if (f.emph() == ON) {
986 env = true; // Size change need not bother about closing env.
988 if (f.underbar() == ON) {
991 env = true; // Size change need not bother about closing env.
993 if (f.noun() == ON) {
996 env = true; // Size change need not bother about closing env.
998 if (f.size() != INHERIT_SIZE) {
999 // We only have to close if only size changed
1006 if (number() == ON && next.number() != ON &&
1007 language()->lang() == "hebrew") {
1012 if (language() != base.language() && language() != next.language()) {
1021 LColor::color LyXFont::realColor() const
1025 return LColor::latex;
1027 if (color() == LColor::none)
1028 return LColor::foreground;
1033 // Convert logical attributes to concrete shape attribute
1034 LyXFont::FONT_SHAPE LyXFont::realShape() const
1036 register FONT_SHAPE s = shape();
1045 s = SMALLCAPS_SHAPE;
1050 bool LyXFont::equalExceptLatex(LyXFont const & f) const
1054 f1.setLatex(f.latex());
1060 ostream & operator<<(ostream & o, LyXFont::FONT_MISC_STATE fms)
1062 return o << int(fms);