1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1995 Matthias Ettrich
7 * Copyright 1995-2000 The LyX Team.
9 * ====================================================== */
16 #pragma implementation
25 #include "FontLoader.h"
26 #include "support/lstrings.h"
27 #include "bufferparams.h" // stateText
37 char const * GUIFamilyNames[6] =
38 { N_("Roman"), N_("Sans serif"), N_("Typewriter"), N_("Symbol"), N_("Inherit"),
42 char const * GUISeriesNames[4] =
43 { N_("Medium"), N_("Bold"), N_("Inherit"), N_("Ignore") };
46 char const * GUIShapeNames[6] =
47 { 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") };
57 char const * GUIMiscNames[5] =
58 { N_("Off"), N_("On"), N_("Toggle"), N_("Inherit"), N_("Ignore") };
62 // Strings used to read and write .lyx format files
65 char const * LyXFamilyNames[6] =
66 { "roman", "sans", "typewriter", "symbol", "default", "error" };
69 char const * LyXSeriesNames[4] =
70 { "medium", "bold", "default", "error" };
73 char const * LyXShapeNames[6] =
74 { "up", "italic", "slanted", "smallcaps", "default", "error" };
77 char const * LyXSizeNames[14] =
78 { "tiny", "scriptsize", "footnotesize", "small", "normal", "large",
79 "larger", "largest", "huge", "giant",
80 "increase-error", "decrease-error", "default", "error" };
83 char const * LyXMiscNames[5] =
84 { "off", "on", "toggle", "default", "error" };
87 // Strings used to write LaTeX files
90 char const * LaTeXFamilyNames[6] =
91 { "textrm", "textsf", "texttt", "error1", "error2", "error3" };
94 char const * LaTeXSeriesNames[4] =
95 { "textmd", "textbf", "error4", "error5" };
98 char const * LaTeXShapeNames[6] =
99 { "textup", "textit", "textsl", "textsc", "error6", "error7" };
102 char const * LaTeXSizeNames[14] =
103 { "tiny", "scriptsize", "footnotesize", "small", "normalsize", "large",
104 "Large", "LARGE", "huge", "Huge", "error8", "error9", "error10", "error11" };
108 // Initialize static member
109 LyXFont::FontBits LyXFont::sane = {
121 // Initialize static member
122 LyXFont::FontBits LyXFont::inherit = {
134 // Initialize static member
135 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 &&
158 fb1.latex == latex &&
159 fb1.number == number;
163 bool LyXFont::FontBits::operator!=(LyXFont::FontBits const & fb1) const
165 return !(fb1 == *this);
172 lang = default_language;
176 LyXFont::LyXFont(LyXFont::FONT_INIT1)
179 lang = default_language;
183 LyXFont::LyXFont(LyXFont::FONT_INIT2)
186 lang = ignore_language;
190 LyXFont::LyXFont(LyXFont::FONT_INIT3)
193 lang = default_language;
197 LyXFont::LyXFont(LyXFont::FONT_INIT1, Language const * l)
204 LyXFont::LyXFont(LyXFont::FONT_INIT2, Language const * l)
211 LyXFont::LyXFont(LyXFont::FONT_INIT3, Language const * l)
218 LyXFont::FONT_FAMILY LyXFont::family() const
224 LyXFont::FONT_SERIES LyXFont::series() const
230 LyXFont::FONT_SHAPE LyXFont::shape() const
236 LyXFont::FONT_SIZE LyXFont::size() const
242 LyXFont::FONT_MISC_STATE LyXFont::emph() const
248 LyXFont::FONT_MISC_STATE LyXFont::underbar() const
250 return bits.underbar;
254 LyXFont::FONT_MISC_STATE LyXFont::noun() const
260 LyXFont::FONT_MISC_STATE LyXFont::latex() const
266 LColor::color LyXFont::color() const
272 Language const * LyXFont::language() const
278 LyXFont::FONT_MISC_STATE LyXFont::number() const
284 bool LyXFont::isRightToLeft() const
286 return lang->RightToLeft();
290 bool LyXFont::isVisibleRightToLeft() const
292 return (lang->RightToLeft() && latex() != ON && number() != ON);
296 LyXFont & LyXFont::setFamily(LyXFont::FONT_FAMILY f)
303 LyXFont & LyXFont::setSeries(LyXFont::FONT_SERIES s)
310 LyXFont & LyXFont::setShape(LyXFont::FONT_SHAPE s)
317 LyXFont & LyXFont::setSize(LyXFont::FONT_SIZE s)
324 LyXFont & LyXFont::setEmph(LyXFont::FONT_MISC_STATE e)
331 LyXFont & LyXFont::setUnderbar(LyXFont::FONT_MISC_STATE u)
338 LyXFont & LyXFont::setNoun(LyXFont::FONT_MISC_STATE n)
345 LyXFont & LyXFont::setLatex(LyXFont::FONT_MISC_STATE l)
352 LyXFont & LyXFont::setColor(LColor::color c)
359 LyXFont & LyXFont::setLanguage(Language const * l)
366 LyXFont & LyXFont::setNumber(LyXFont::FONT_MISC_STATE n)
373 /// Decreases font size by one
374 LyXFont & LyXFont::decSize()
377 case SIZE_HUGER: setSize(SIZE_HUGE); break;
378 case SIZE_HUGE: setSize(SIZE_LARGEST); break;
379 case SIZE_LARGEST: setSize(SIZE_LARGER); break;
380 case SIZE_LARGER: setSize(SIZE_LARGE); break;
381 case SIZE_LARGE: setSize(SIZE_NORMAL); break;
382 case SIZE_NORMAL: setSize(SIZE_SMALL); break;
383 case SIZE_SMALL: setSize(SIZE_FOOTNOTE); break;
384 case SIZE_FOOTNOTE: setSize(SIZE_SCRIPT); break;
385 case SIZE_SCRIPT: setSize(SIZE_TINY); break;
386 case SIZE_TINY: break;
388 lyxerr << "Can't LyXFont::decSize on INCREASE_SIZE" << endl;
391 lyxerr <<"Can't LyXFont::decSize on DECREASE_SIZE" << endl;
394 lyxerr <<"Can't LyXFont::decSize on INHERIT_SIZE" << endl;
397 lyxerr <<"Can't LyXFont::decSize on IGNORE_SIZE" << endl;
404 /// Increases font size by one
405 LyXFont & LyXFont::incSize()
408 case SIZE_HUGER: break;
409 case SIZE_HUGE: setSize(SIZE_HUGER); break;
410 case SIZE_LARGEST: setSize(SIZE_HUGE); break;
411 case SIZE_LARGER: setSize(SIZE_LARGEST); break;
412 case SIZE_LARGE: setSize(SIZE_LARGER); break;
413 case SIZE_NORMAL: setSize(SIZE_LARGE); break;
414 case SIZE_SMALL: setSize(SIZE_NORMAL); break;
415 case SIZE_FOOTNOTE: setSize(SIZE_SMALL); break;
416 case SIZE_SCRIPT: setSize(SIZE_FOOTNOTE); break;
417 case SIZE_TINY: setSize(SIZE_SCRIPT); break;
419 lyxerr <<"Can't LyXFont::incSize on INCREASE_SIZE" << endl;
422 lyxerr <<"Can't LyXFont::incSize on DECREASE_SIZE" << endl;
425 lyxerr <<"Can't LyXFont::incSize on INHERIT_SIZE" << endl;
428 lyxerr <<"Can't LyXFont::incSize on IGNORE_SIZE" << endl;
435 /// Updates a misc setting according to request
436 LyXFont::FONT_MISC_STATE LyXFont::setMisc(FONT_MISC_STATE newfont,
439 if (newfont == TOGGLE) {
445 lyxerr <<"LyXFont::setMisc: Need state"
446 " ON or OFF to toggle. Setting to ON" << endl;
449 } else if (newfont == IGNORE)
456 /// Updates font settings according to request
457 void LyXFont::update(LyXFont const & newfont,
458 Language const * document_language, bool toggleall)
460 if (newfont.family() == family() && toggleall)
461 setFamily(INHERIT_FAMILY); // toggle 'back'
462 else if (newfont.family() != IGNORE_FAMILY)
463 setFamily(newfont.family());
464 // else it's IGNORE_SHAPE
466 // "Old" behaviour: "Setting" bold will toggle bold on/off.
467 switch (newfont.series()) {
470 if (series() == BOLD_SERIES && toggleall)
471 setSeries(MEDIUM_SERIES);
473 setSeries(BOLD_SERIES);
477 setSeries(newfont.series());
483 if (newfont.shape() == shape() && toggleall)
484 setShape(INHERIT_SHAPE); // toggle 'back'
485 else if (newfont.shape() != IGNORE_SHAPE)
486 setShape(newfont.shape());
487 // else it's IGNORE_SHAPE
489 if (newfont.size() != IGNORE_SIZE) {
490 if (newfont.size() == INCREASE_SIZE)
492 else if (newfont.size() == DECREASE_SIZE)
495 setSize(newfont.size());
498 setEmph(setMisc(newfont.emph(), emph()));
499 setUnderbar(setMisc(newfont.underbar(), underbar()));
500 setNoun(setMisc(newfont.noun(), noun()));
501 setLatex(setMisc(newfont.latex(), latex()));
503 setNumber(setMisc(newfont.number(), number()));
504 if (newfont.language() == language() && toggleall)
505 if (language() == document_language)
506 setLanguage(default_language);
508 setLanguage(document_language);
509 else if (newfont.language() != ignore_language)
510 setLanguage(newfont.language());
512 if (newfont.color() == color() && toggleall)
513 setColor(LColor::inherit); // toggle 'back'
514 else if (newfont.color() != LColor::ignore)
515 setColor(newfont.color());
519 /// Reduce font to fall back to template where possible
520 void LyXFont::reduce(LyXFont const & tmplt)
522 if (family() == tmplt.family())
523 setFamily(INHERIT_FAMILY);
524 if (series() == tmplt.series())
525 setSeries(INHERIT_SERIES);
526 if (shape() == tmplt.shape())
527 setShape(INHERIT_SHAPE);
528 if (size() == tmplt.size())
529 setSize(INHERIT_SIZE);
530 if (emph() == tmplt.emph())
532 if (underbar() == tmplt.underbar())
533 setUnderbar(INHERIT);
534 if (noun() == tmplt.noun())
536 if (latex() == tmplt.latex())
538 if (color() == tmplt.color())
539 setColor(LColor::inherit);
543 /// Realize font from a template
544 LyXFont & LyXFont::realize(LyXFont const & tmplt)
546 if (bits == inherit) {
551 if (bits.family == INHERIT_FAMILY) {
552 bits.family = tmplt.bits.family;
554 if (bits.series == INHERIT_SERIES) {
555 bits.series = tmplt.bits.series;
557 if (bits.shape == INHERIT_SHAPE) {
558 bits.shape = tmplt.bits.shape;
560 if (bits.size == INHERIT_SIZE) {
561 bits.size = tmplt.bits.size;
563 if (bits.emph == INHERIT) {
564 bits.emph = tmplt.bits.emph;
566 if (bits.underbar == INHERIT) {
567 bits.underbar = tmplt.bits.underbar;
569 if (bits.noun == INHERIT) {
570 bits.noun = tmplt.bits.noun;
572 if (bits.latex == INHERIT) {
573 bits.latex = tmplt.bits.latex;
575 if (bits.color == LColor::inherit) {
576 bits.color = tmplt.bits.color;
582 /// Is font resolved?
583 bool LyXFont::resolved() const
585 return (family() != INHERIT_FAMILY && series() != INHERIT_SERIES &&
586 shape() != INHERIT_SHAPE && size() != INHERIT_SIZE &&
587 emph() != INHERIT && underbar() != INHERIT &&
588 noun() != INHERIT && latex() != INHERIT &&
589 color() != LColor::inherit);
593 /// Build GUI description of font state
594 string const LyXFont::stateText(BufferParams * params) const
596 std::ostringstream ost;
597 if (family() != INHERIT_FAMILY)
598 ost << _(GUIFamilyNames[family()]) << ", ";
599 if (series() != INHERIT_SERIES)
600 ost << _(GUISeriesNames[series()]) << ", ";
601 if (shape() != INHERIT_SHAPE)
602 ost << _(GUIShapeNames[shape()]) << ", ";
603 if (size() != INHERIT_SIZE)
604 ost << _(GUISizeNames[size()]) << ", ";
605 if (color() != LColor::inherit)
606 ost << lcolor.getGUIName(color()) << ", ";
607 if (emph() != INHERIT)
608 ost << _("Emphasis ")
609 << _(GUIMiscNames[emph()]) << ", ";
610 if (underbar() != INHERIT)
611 ost << _("Underline ")
612 << _(GUIMiscNames[underbar()]) << ", ";
613 if (noun() != INHERIT)
614 ost << _("Noun ") << _(GUIMiscNames[noun()]) << ", ";
615 if (latex() != INHERIT)
616 ost << _("Latex ") << _(GUIMiscNames[latex()]) << ", ";
618 ost << _("Default") << ", ";
619 if (!params || (language() != params->language))
620 ost << _("Language: ") << _(language()->display()) << ", ";
622 ost << _(" Number ") << _(GUIMiscNames[number()]);
624 string buf(ost.str().c_str());
625 buf = strip(buf, ' ');
626 buf = strip(buf, ',');
631 // Set family according to lyx format string
632 LyXFont & LyXFont::setLyXFamily(string const & fam)
634 string const s = lowercase(fam);
637 while (s != LyXFamilyNames[i] && LyXFamilyNames[i] != "error") ++i;
638 if (s == LyXFamilyNames[i]) {
639 setFamily(LyXFont::FONT_FAMILY(i));
641 lyxerr << "LyXFont::setLyXFamily: Unknown family `"
642 << s << '\'' << endl;
647 // Set series according to lyx format string
648 LyXFont & LyXFont::setLyXSeries(string const & ser)
650 string const s = lowercase(ser);
653 while (s != LyXSeriesNames[i] && LyXSeriesNames[i] != "error") ++i;
654 if (s == LyXSeriesNames[i]) {
655 setSeries(LyXFont::FONT_SERIES(i));
657 lyxerr << "LyXFont::setLyXSeries: Unknown series `"
658 << s << '\'' << endl;
663 // Set shape according to lyx format string
664 LyXFont & LyXFont::setLyXShape(string const & sha)
666 string const s = lowercase(sha);
669 while (s != LyXShapeNames[i] && LyXShapeNames[i] != "error") ++i;
670 if (s == LyXShapeNames[i]) {
671 setShape(LyXFont::FONT_SHAPE(i));
673 lyxerr << "LyXFont::setLyXShape: Unknown shape `"
674 << s << '\'' << endl;
679 // Set size according to lyx format string
680 LyXFont & LyXFont::setLyXSize(string const & siz)
682 string const s = lowercase(siz);
684 while (s != LyXSizeNames[i] && LyXSizeNames[i] != "error") ++i;
685 if (s == LyXSizeNames[i]) {
686 setSize(LyXFont::FONT_SIZE(i));
688 lyxerr << "LyXFont::setLyXSize: Unknown size `"
689 << s << '\'' << endl;
694 // Set size according to lyx format string
695 LyXFont::FONT_MISC_STATE LyXFont::setLyXMisc(string const & siz)
697 string const s = lowercase(siz);
699 while (s != LyXMiscNames[i] && LyXMiscNames[i] != "error") ++i;
700 if (s == LyXMiscNames[i])
701 return FONT_MISC_STATE(i);
702 lyxerr << "LyXFont::setLyXMisc: Unknown misc flag `"
703 << s << '\'' << endl;
708 /// Sets color after LyX text format
709 LyXFont & LyXFont::setLyXColor(string const & col)
711 setColor(lcolor.getFromLyXName(col));
716 // Returns size in latex format
717 string const LyXFont::latexSize() const
719 return LaTeXSizeNames[size()];
723 // Read a font definition from given file in lyx format
725 LyXFont & LyXFont::lyxRead(LyXLex & lex)
728 bool finished = false;
729 while (!finished && lex.IsOK() && !error) {
731 string const tok = lowercase(lex.GetString());
735 } else if (tok == "endfont") {
737 } else if (tok == "family") {
739 string const ttok = lex.GetString();
741 } else if (tok == "series") {
743 string const ttok = lex.GetString();
745 } else if (tok == "shape") {
747 string const ttok = lex.GetString();
749 } else if (tok == "size") {
751 string const ttok = lex.GetString();
753 } else if (tok == "latex") {
755 string const ttok = lowercase(lex.GetString());
757 if (ttok == "no_latex") {
759 } else if (ttok == "latex") {
762 lex.printError("Illegal LaTeX type`$$Token'");
764 } else if (tok == "misc") {
766 string const ttok = lowercase(lex.GetString());
768 if (ttok == "no_bar") {
770 } else if (ttok == "no_emph") {
772 } else if (ttok == "no_noun") {
774 } else if (ttok == "emph") {
776 } else if (ttok == "underbar") {
778 } else if (ttok == "noun") {
781 lex.printError("Illegal misc type `$$Token´");
783 } else if (tok == "color") {
785 string const ttok = lex.GetString();
788 lex.printError("Unknown tag `$$Token'");
796 /// Writes the changes from this font to orgfont in .lyx format in file
797 void LyXFont::lyxWriteChanges(LyXFont const & orgfont, ostream & os) const
800 if (orgfont.family() != family()) {
801 os << "\\family " << LyXFamilyNames[family()] << " \n";
803 if (orgfont.series() != series()) {
804 os << "\\series " << LyXSeriesNames[series()] << " \n";
806 if (orgfont.shape() != shape()) {
807 os << "\\shape " << LyXShapeNames[shape()] << " \n";
809 if (orgfont.size() != size()) {
810 os << "\\size " << LyXSizeNames[size()] << " \n";
812 if (orgfont.emph() != emph()) {
813 os << "\\emph " << LyXMiscNames[emph()] << " \n";
815 if (orgfont.number() != number()) {
816 os << "\\numeric " << LyXMiscNames[number()] << " \n";
818 if (orgfont.underbar() != underbar()) {
819 // This is only for backwards compatibility
820 switch (underbar()) {
821 case OFF: os << "\\bar no \n"; break;
822 case ON: os << "\\bar under \n"; break;
823 case TOGGLE: lyxerr << "LyXFont::lyxWriteFontChanges: "
824 "TOGGLE should not appear here!"
827 case INHERIT: os << "\\bar default \n"; break;
828 case IGNORE: lyxerr << "LyXFont::lyxWriteFontChanges: "
829 "IGNORE should not appear here!"
834 if (orgfont.noun() != noun()) {
835 os << "\\noun " << LyXMiscNames[noun()] << " \n";
837 if (orgfont.latex() != latex()) {
838 // This is only for backwards compatibility
840 case OFF: os << "\\latex no_latex \n"; break;
841 case ON: os << "\\latex latex \n"; break;
842 case TOGGLE: lyxerr << "LyXFont::lyxWriteFontChanges: "
843 "TOGGLE should not appear here!"
846 case INHERIT: os << "\\latex default \n"; break;
847 case IGNORE: lyxerr << "LyXFont::lyxWriteFontChanges: "
848 "IGNORE should not appear here!"
853 if (orgfont.color() != color()) {
854 // To make us file compatible with older
855 // lyx versions we emit "default" instead
857 string col_str(lcolor.getLyXName(color()));
858 if (col_str == "inherit") col_str = "default";
859 os << "\\color " << col_str << "\n";
861 if (orgfont.language() != language()) {
863 os << "\\lang " << language()->lang() << "\n";
865 os << "\\lang unknown\n";
870 /// Writes the head of the LaTeX needed to impose this font
871 // Returns number of chars written.
872 int LyXFont::latexWriteStartChanges(ostream & os, LyXFont const & base,
873 LyXFont const & prev) const
878 if (language()->babel() != base.language()->babel() &&
879 language()->babel() != prev.language()->babel()) {
880 if (isRightToLeft() != prev.isRightToLeft()) {
881 if (isRightToLeft()) {
890 subst(lyxrc.language_command_local,
891 "$$lang", language()->babel());
893 count += tmp.length();
897 if (number() == ON && prev.number() != ON &&
898 language()->lang() == "hebrew") {
906 if (f.family() != INHERIT_FAMILY) {
908 << LaTeXFamilyNames[f.family()]
910 count += strlen(LaTeXFamilyNames[f.family()]) + 2;
911 env = true; //We have opened a new environment
913 if (f.series() != INHERIT_SERIES) {
915 << LaTeXSeriesNames[f.series()]
917 count += strlen(LaTeXSeriesNames[f.series()]) + 2;
918 env = true; //We have opened a new environment
920 if (f.shape() != INHERIT_SHAPE) {
922 << LaTeXShapeNames[f.shape()]
924 count += strlen(LaTeXShapeNames[f.shape()]) + 2;
925 env = true; //We have opened a new environment
927 if (f.color() != LColor::inherit && f.color() != LColor::ignore) {
929 << lcolor.getLaTeXName(f.color())
931 count += lcolor.getLaTeXName(f.color()).length() + 13;
932 env = true; //We have opened a new environment
934 if (f.emph() == ON) {
937 env = true; //We have opened a new environment
939 if (f.underbar() == ON) {
942 env = true; //We have opened a new environment
944 // \noun{} is a LyX special macro
945 if (f.noun() == ON) {
948 env = true; //We have opened a new environment
950 if (f.size() != INHERIT_SIZE) {
951 // If we didn't open an environment above, we open one here
957 << LaTeXSizeNames[f.size()]
959 count += strlen(LaTeXSizeNames[f.size()]) + 2;
965 /// Writes ending block of LaTeX needed to close use of this font
966 // Returns number of chars written
967 // This one corresponds to latexWriteStartChanges(). (Asger)
968 int LyXFont::latexWriteEndChanges(ostream & os, LyXFont const & base,
969 LyXFont const & next) const
974 LyXFont f = *this; // why do you need this?
975 f.reduce(base); // why isn't this just "reduce(base);" (Lgb)
976 // Because this function is const. Everything breaks if this
977 // method changes the font it represents. There is no speed penalty
978 // by using the temporary. (Asger)
981 if (f.family() != INHERIT_FAMILY) {
984 env = true; // Size change need not bother about closing env.
986 if (f.series() != INHERIT_SERIES) {
989 env = true; // Size change need not bother about closing env.
991 if (f.shape() != INHERIT_SHAPE) {
994 env = true; // Size change need not bother about closing env.
996 if (f.color() != LColor::inherit && f.color() != LColor::ignore) {
999 env = true; // Size change need not bother about closing env.
1001 if (f.emph() == ON) {
1004 env = true; // Size change need not bother about closing env.
1006 if (f.underbar() == ON) {
1009 env = true; // Size change need not bother about closing env.
1011 if (f.noun() == ON) {
1014 env = true; // Size change need not bother about closing env.
1016 if (f.size() != INHERIT_SIZE) {
1017 // We only have to close if only size changed
1024 if (number() == ON && next.number() != ON &&
1025 language()->lang() == "hebrew") {
1030 if (language() != base.language() && language() != next.language()) {
1039 LColor::color LyXFont::realColor() const
1042 return LColor::latex;
1043 if (color() == LColor::none)
1044 return LColor::foreground;
1049 // Convert logical attributes to concrete shape attribute
1050 LyXFont::FONT_SHAPE LyXFont::realShape() const
1052 register FONT_SHAPE s = shape();
1061 s = SMALLCAPS_SHAPE;
1066 bool LyXFont::equalExceptLatex(LyXFont const & f) const
1069 f1.setLatex(f.latex());
1074 ostream & operator<<(ostream & o, LyXFont::FONT_MISC_STATE fms)
1076 return o << int(fms);