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)
494 else if (newfont.size() == size() && toggleall)
495 setSize(INHERIT_SIZE); // toggle 'back'
497 setSize(newfont.size());
500 setEmph(setMisc(newfont.emph(), emph()));
501 setUnderbar(setMisc(newfont.underbar(), underbar()));
502 setNoun(setMisc(newfont.noun(), noun()));
503 setLatex(setMisc(newfont.latex(), latex()));
505 setNumber(setMisc(newfont.number(), number()));
506 if (newfont.language() == language() && toggleall)
507 if (language() == document_language)
508 setLanguage(default_language);
510 setLanguage(document_language);
511 else if (newfont.language() != ignore_language)
512 setLanguage(newfont.language());
514 if (newfont.color() == color() && toggleall)
515 setColor(LColor::inherit); // toggle 'back'
516 else if (newfont.color() != LColor::ignore)
517 setColor(newfont.color());
521 /// Reduce font to fall back to template where possible
522 void LyXFont::reduce(LyXFont const & tmplt)
524 if (family() == tmplt.family())
525 setFamily(INHERIT_FAMILY);
526 if (series() == tmplt.series())
527 setSeries(INHERIT_SERIES);
528 if (shape() == tmplt.shape())
529 setShape(INHERIT_SHAPE);
530 if (size() == tmplt.size())
531 setSize(INHERIT_SIZE);
532 if (emph() == tmplt.emph())
534 if (underbar() == tmplt.underbar())
535 setUnderbar(INHERIT);
536 if (noun() == tmplt.noun())
538 if (latex() == tmplt.latex())
540 if (color() == tmplt.color())
541 setColor(LColor::inherit);
545 /// Realize font from a template
546 LyXFont & LyXFont::realize(LyXFont const & tmplt)
548 if (bits == inherit) {
553 if (bits.family == INHERIT_FAMILY) {
554 bits.family = tmplt.bits.family;
556 if (bits.series == INHERIT_SERIES) {
557 bits.series = tmplt.bits.series;
559 if (bits.shape == INHERIT_SHAPE) {
560 bits.shape = tmplt.bits.shape;
562 if (bits.size == INHERIT_SIZE) {
563 bits.size = tmplt.bits.size;
565 if (bits.emph == INHERIT) {
566 bits.emph = tmplt.bits.emph;
568 if (bits.underbar == INHERIT) {
569 bits.underbar = tmplt.bits.underbar;
571 if (bits.noun == INHERIT) {
572 bits.noun = tmplt.bits.noun;
574 if (bits.latex == INHERIT) {
575 bits.latex = tmplt.bits.latex;
577 if (bits.color == LColor::inherit) {
578 bits.color = tmplt.bits.color;
584 /// Is font resolved?
585 bool LyXFont::resolved() const
587 return (family() != INHERIT_FAMILY && series() != INHERIT_SERIES &&
588 shape() != INHERIT_SHAPE && size() != INHERIT_SIZE &&
589 emph() != INHERIT && underbar() != INHERIT &&
590 noun() != INHERIT && latex() != INHERIT &&
591 color() != LColor::inherit);
595 /// Build GUI description of font state
596 string const LyXFont::stateText(BufferParams * params) const
598 std::ostringstream ost;
599 if (family() != INHERIT_FAMILY)
600 ost << _(GUIFamilyNames[family()]) << ", ";
601 if (series() != INHERIT_SERIES)
602 ost << _(GUISeriesNames[series()]) << ", ";
603 if (shape() != INHERIT_SHAPE)
604 ost << _(GUIShapeNames[shape()]) << ", ";
605 if (size() != INHERIT_SIZE)
606 ost << _(GUISizeNames[size()]) << ", ";
607 if (color() != LColor::inherit)
608 ost << lcolor.getGUIName(color()) << ", ";
609 if (emph() != INHERIT)
610 ost << _("Emphasis ")
611 << _(GUIMiscNames[emph()]) << ", ";
612 if (underbar() != INHERIT)
613 ost << _("Underline ")
614 << _(GUIMiscNames[underbar()]) << ", ";
615 if (noun() != INHERIT)
616 ost << _("Noun ") << _(GUIMiscNames[noun()]) << ", ";
617 if (latex() != INHERIT)
618 ost << _("Latex ") << _(GUIMiscNames[latex()]) << ", ";
620 ost << _("Default") << ", ";
621 if (!params || (language() != params->language))
622 ost << _("Language: ") << _(language()->display()) << ", ";
624 ost << _(" Number ") << _(GUIMiscNames[number()]);
626 string buf(ost.str().c_str());
627 buf = strip(buf, ' ');
628 buf = strip(buf, ',');
633 // Set family according to lyx format string
634 LyXFont & LyXFont::setLyXFamily(string const & fam)
636 string const s = lowercase(fam);
639 while (s != LyXFamilyNames[i] && LyXFamilyNames[i] != "error") ++i;
640 if (s == LyXFamilyNames[i]) {
641 setFamily(LyXFont::FONT_FAMILY(i));
643 lyxerr << "LyXFont::setLyXFamily: Unknown family `"
644 << s << '\'' << endl;
649 // Set series according to lyx format string
650 LyXFont & LyXFont::setLyXSeries(string const & ser)
652 string const s = lowercase(ser);
655 while (s != LyXSeriesNames[i] && LyXSeriesNames[i] != "error") ++i;
656 if (s == LyXSeriesNames[i]) {
657 setSeries(LyXFont::FONT_SERIES(i));
659 lyxerr << "LyXFont::setLyXSeries: Unknown series `"
660 << s << '\'' << endl;
665 // Set shape according to lyx format string
666 LyXFont & LyXFont::setLyXShape(string const & sha)
668 string const s = lowercase(sha);
671 while (s != LyXShapeNames[i] && LyXShapeNames[i] != "error") ++i;
672 if (s == LyXShapeNames[i]) {
673 setShape(LyXFont::FONT_SHAPE(i));
675 lyxerr << "LyXFont::setLyXShape: Unknown shape `"
676 << s << '\'' << endl;
681 // Set size according to lyx format string
682 LyXFont & LyXFont::setLyXSize(string const & siz)
684 string const s = lowercase(siz);
686 while (s != LyXSizeNames[i] && LyXSizeNames[i] != "error") ++i;
687 if (s == LyXSizeNames[i]) {
688 setSize(LyXFont::FONT_SIZE(i));
690 lyxerr << "LyXFont::setLyXSize: Unknown size `"
691 << s << '\'' << endl;
696 // Set size according to lyx format string
697 LyXFont::FONT_MISC_STATE LyXFont::setLyXMisc(string const & siz)
699 string const s = lowercase(siz);
701 while (s != LyXMiscNames[i] && LyXMiscNames[i] != "error") ++i;
702 if (s == LyXMiscNames[i])
703 return FONT_MISC_STATE(i);
704 lyxerr << "LyXFont::setLyXMisc: Unknown misc flag `"
705 << s << '\'' << endl;
710 /// Sets color after LyX text format
711 LyXFont & LyXFont::setLyXColor(string const & col)
713 setColor(lcolor.getFromLyXName(col));
718 // Returns size in latex format
719 string const LyXFont::latexSize() const
721 return LaTeXSizeNames[size()];
725 // Read a font definition from given file in lyx format
727 LyXFont & LyXFont::lyxRead(LyXLex & lex)
730 bool finished = false;
731 while (!finished && lex.IsOK() && !error) {
733 string const tok = lowercase(lex.GetString());
737 } else if (tok == "endfont") {
739 } else if (tok == "family") {
741 string const ttok = lex.GetString();
743 } else if (tok == "series") {
745 string const ttok = lex.GetString();
747 } else if (tok == "shape") {
749 string const ttok = lex.GetString();
751 } else if (tok == "size") {
753 string const ttok = lex.GetString();
755 } else if (tok == "latex") {
757 string const ttok = lowercase(lex.GetString());
759 if (ttok == "no_latex") {
761 } else if (ttok == "latex") {
764 lex.printError("Illegal LaTeX type`$$Token'");
766 } else if (tok == "misc") {
768 string const ttok = lowercase(lex.GetString());
770 if (ttok == "no_bar") {
772 } else if (ttok == "no_emph") {
774 } else if (ttok == "no_noun") {
776 } else if (ttok == "emph") {
778 } else if (ttok == "underbar") {
780 } else if (ttok == "noun") {
783 lex.printError("Illegal misc type `$$Token´");
785 } else if (tok == "color") {
787 string const ttok = lex.GetString();
790 lex.printError("Unknown tag `$$Token'");
798 /// Writes the changes from this font to orgfont in .lyx format in file
799 void LyXFont::lyxWriteChanges(LyXFont const & orgfont, ostream & os) const
802 if (orgfont.family() != family()) {
803 os << "\\family " << LyXFamilyNames[family()] << " \n";
805 if (orgfont.series() != series()) {
806 os << "\\series " << LyXSeriesNames[series()] << " \n";
808 if (orgfont.shape() != shape()) {
809 os << "\\shape " << LyXShapeNames[shape()] << " \n";
811 if (orgfont.size() != size()) {
812 os << "\\size " << LyXSizeNames[size()] << " \n";
814 if (orgfont.emph() != emph()) {
815 os << "\\emph " << LyXMiscNames[emph()] << " \n";
817 if (orgfont.number() != number()) {
818 os << "\\numeric " << LyXMiscNames[number()] << " \n";
820 if (orgfont.underbar() != underbar()) {
821 // This is only for backwards compatibility
822 switch (underbar()) {
823 case OFF: os << "\\bar no \n"; break;
824 case ON: os << "\\bar under \n"; break;
825 case TOGGLE: lyxerr << "LyXFont::lyxWriteFontChanges: "
826 "TOGGLE should not appear here!"
829 case INHERIT: os << "\\bar default \n"; break;
830 case IGNORE: lyxerr << "LyXFont::lyxWriteFontChanges: "
831 "IGNORE should not appear here!"
836 if (orgfont.noun() != noun()) {
837 os << "\\noun " << LyXMiscNames[noun()] << " \n";
839 if (orgfont.latex() != latex()) {
840 // This is only for backwards compatibility
842 case OFF: os << "\\latex no_latex \n"; break;
843 case ON: os << "\\latex latex \n"; break;
844 case TOGGLE: lyxerr << "LyXFont::lyxWriteFontChanges: "
845 "TOGGLE should not appear here!"
848 case INHERIT: os << "\\latex default \n"; break;
849 case IGNORE: lyxerr << "LyXFont::lyxWriteFontChanges: "
850 "IGNORE should not appear here!"
855 if (orgfont.color() != color()) {
856 // To make us file compatible with older
857 // lyx versions we emit "default" instead
859 string col_str(lcolor.getLyXName(color()));
860 if (col_str == "inherit") col_str = "default";
861 os << "\\color " << col_str << "\n";
863 if (orgfont.language() != language()) {
865 os << "\\lang " << language()->lang() << "\n";
867 os << "\\lang unknown\n";
872 /// Writes the head of the LaTeX needed to impose this font
873 // Returns number of chars written.
874 int LyXFont::latexWriteStartChanges(ostream & os, LyXFont const & base,
875 LyXFont const & prev) const
880 if (language()->babel() != base.language()->babel() &&
881 language()->babel() != prev.language()->babel()) {
882 if (isRightToLeft() != prev.isRightToLeft()) {
883 if (isRightToLeft()) {
892 subst(lyxrc.language_command_local,
893 "$$lang", language()->babel());
895 count += tmp.length();
899 if (number() == ON && prev.number() != ON &&
900 language()->lang() == "hebrew") {
908 if (f.family() != INHERIT_FAMILY) {
910 << LaTeXFamilyNames[f.family()]
912 count += strlen(LaTeXFamilyNames[f.family()]) + 2;
913 env = true; //We have opened a new environment
915 if (f.series() != INHERIT_SERIES) {
917 << LaTeXSeriesNames[f.series()]
919 count += strlen(LaTeXSeriesNames[f.series()]) + 2;
920 env = true; //We have opened a new environment
922 if (f.shape() != INHERIT_SHAPE) {
924 << LaTeXShapeNames[f.shape()]
926 count += strlen(LaTeXShapeNames[f.shape()]) + 2;
927 env = true; //We have opened a new environment
929 if (f.color() != LColor::inherit && f.color() != LColor::ignore) {
931 << lcolor.getLaTeXName(f.color())
933 count += lcolor.getLaTeXName(f.color()).length() + 13;
934 env = true; //We have opened a new environment
936 if (f.emph() == ON) {
939 env = true; //We have opened a new environment
941 if (f.underbar() == ON) {
944 env = true; //We have opened a new environment
946 // \noun{} is a LyX special macro
947 if (f.noun() == ON) {
950 env = true; //We have opened a new environment
952 if (f.size() != INHERIT_SIZE) {
953 // If we didn't open an environment above, we open one here
959 << LaTeXSizeNames[f.size()]
961 count += strlen(LaTeXSizeNames[f.size()]) + 2;
967 /// Writes ending block of LaTeX needed to close use of this font
968 // Returns number of chars written
969 // This one corresponds to latexWriteStartChanges(). (Asger)
970 int LyXFont::latexWriteEndChanges(ostream & os, LyXFont const & base,
971 LyXFont const & next) const
976 LyXFont f = *this; // why do you need this?
977 f.reduce(base); // why isn't this just "reduce(base);" (Lgb)
978 // Because this function is const. Everything breaks if this
979 // method changes the font it represents. There is no speed penalty
980 // by using the temporary. (Asger)
983 if (f.family() != INHERIT_FAMILY) {
986 env = true; // Size change need not bother about closing env.
988 if (f.series() != INHERIT_SERIES) {
991 env = true; // Size change need not bother about closing env.
993 if (f.shape() != INHERIT_SHAPE) {
996 env = true; // Size change need not bother about closing env.
998 if (f.color() != LColor::inherit && f.color() != LColor::ignore) {
1001 env = true; // Size change need not bother about closing env.
1003 if (f.emph() == ON) {
1006 env = true; // Size change need not bother about closing env.
1008 if (f.underbar() == ON) {
1011 env = true; // Size change need not bother about closing env.
1013 if (f.noun() == ON) {
1016 env = true; // Size change need not bother about closing env.
1018 if (f.size() != INHERIT_SIZE) {
1019 // We only have to close if only size changed
1026 if (number() == ON && next.number() != ON &&
1027 language()->lang() == "hebrew") {
1032 if (language() != base.language() && language() != next.language()) {
1041 LColor::color LyXFont::realColor() const
1044 return LColor::latex;
1045 if (color() == LColor::none)
1046 return LColor::foreground;
1051 // Convert logical attributes to concrete shape attribute
1052 LyXFont::FONT_SHAPE LyXFont::realShape() const
1054 register FONT_SHAPE s = shape();
1063 s = SMALLCAPS_SHAPE;
1068 bool LyXFont::equalExceptLatex(LyXFont const & f) const
1071 f1.setLatex(f.latex());
1076 ostream & operator<<(ostream & o, LyXFont::FONT_MISC_STATE fms)
1078 return o << int(fms);