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
38 char const * GUIFamilyNames[6] =
39 { 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") };
45 char const * GUIShapeNames[6] =
46 { N_("Upright"), N_("Italic"), N_("Slanted"), N_("Smallcaps"), N_("Inherit"),
49 char const * GUISizeNames[14] =
50 { N_("Tiny"), N_("Smallest"), N_("Smaller"), N_("Small"), N_("Normal"), N_("Large"),
51 N_("Larger"), N_("Largest"), N_("Huge"), N_("Huger"), N_("Increase"), N_("Decrease"),
52 N_("Inherit"), N_("Ignore") };
54 char const * GUIMiscNames[5] =
55 { N_("Off"), N_("On"), N_("Toggle"), N_("Inherit"), N_("Ignore") };
59 // Strings used to read and write .lyx format files
61 char const * LyXFamilyNames[6] =
62 { "roman", "sans", "typewriter", "symbol", "default", "error" };
64 char const * LyXSeriesNames[4] =
65 { "medium", "bold", "default", "error" };
67 char const * LyXShapeNames[6] =
68 { "up", "italic", "slanted", "smallcaps", "default", "error" };
70 char const * LyXSizeNames[14] =
71 { "tiny", "scriptsize", "footnotesize", "small", "normal", "large",
72 "larger", "largest", "huge", "giant",
73 "increase-error", "decrease-error", "default", "error" };
75 char const * LyXMiscNames[5] =
76 { "off", "on", "toggle", "default", "error" };
79 // Strings used to write LaTeX files
81 char const * LaTeXFamilyNames[6] =
82 { "textrm", "textsf", "texttt", "error1", "error2", "error3" };
84 char const * LaTeXSeriesNames[4] =
85 { "textmd", "textbf", "error4", "error5" };
87 char const * LaTeXShapeNames[6] =
88 { "textup", "textit", "textsl", "textsc", "error6", "error7" };
90 char const * LaTeXSizeNames[14] =
91 { "tiny", "scriptsize", "footnotesize", "small", "normalsize", "large",
92 "Large", "LARGE", "huge", "Huge", "error8", "error9", "error10", "error11" };
97 // Initialize static member
98 LyXFont::FontBits LyXFont::sane = {
110 // Initialize static member
111 LyXFont::FontBits LyXFont::inherit = {
123 // Initialize static member
124 LyXFont::FontBits LyXFont::ignore = {
137 bool LyXFont::FontBits::operator==(LyXFont::FontBits const & fb1) const
139 return fb1.family == family &&
140 fb1.series == series &&
141 fb1.shape == shape &&
143 fb1.color == color &&
145 fb1.underbar == underbar &&
147 fb1.latex == latex &&
148 fb1.number == number;
152 bool LyXFont::FontBits::operator!=(LyXFont::FontBits const & fb1) const
154 return !(fb1 == *this);
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 LyXFont::FONT_MISC_STATE LyXFont::latex() const
200 LColor::color LyXFont::color() const
206 Language const * LyXFont::language() const
212 LyXFont::FONT_MISC_STATE LyXFont::number() const
218 bool LyXFont::isRightToLeft() const
220 return lang->RightToLeft();
224 bool LyXFont::isVisibleRightToLeft() const
226 return (lang->RightToLeft() && latex() != ON && number() != ON);
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::setLatex(LyXFont::FONT_MISC_STATE l)
286 LyXFont & LyXFont::setColor(LColor::color c)
293 LyXFont & LyXFont::setLanguage(Language const * l)
300 LyXFont & LyXFont::setNumber(LyXFont::FONT_MISC_STATE n)
307 /// Decreases font size by one
308 LyXFont & LyXFont::decSize()
311 case SIZE_HUGER: setSize(SIZE_HUGE); break;
312 case SIZE_HUGE: setSize(SIZE_LARGEST); break;
313 case SIZE_LARGEST: setSize(SIZE_LARGER); break;
314 case SIZE_LARGER: setSize(SIZE_LARGE); break;
315 case SIZE_LARGE: setSize(SIZE_NORMAL); break;
316 case SIZE_NORMAL: setSize(SIZE_SMALL); break;
317 case SIZE_SMALL: setSize(SIZE_FOOTNOTE); break;
318 case SIZE_FOOTNOTE: setSize(SIZE_SCRIPT); break;
319 case SIZE_SCRIPT: setSize(SIZE_TINY); break;
320 case SIZE_TINY: break;
322 lyxerr << "Can't LyXFont::decSize on INCREASE_SIZE" << endl;
325 lyxerr <<"Can't LyXFont::decSize on DECREASE_SIZE" << endl;
328 lyxerr <<"Can't LyXFont::decSize on INHERIT_SIZE" << endl;
331 lyxerr <<"Can't LyXFont::decSize on IGNORE_SIZE" << endl;
338 /// Increases font size by one
339 LyXFont & LyXFont::incSize()
342 case SIZE_HUGER: break;
343 case SIZE_HUGE: setSize(SIZE_HUGER); break;
344 case SIZE_LARGEST: setSize(SIZE_HUGE); break;
345 case SIZE_LARGER: setSize(SIZE_LARGEST); break;
346 case SIZE_LARGE: setSize(SIZE_LARGER); break;
347 case SIZE_NORMAL: setSize(SIZE_LARGE); break;
348 case SIZE_SMALL: setSize(SIZE_NORMAL); break;
349 case SIZE_FOOTNOTE: setSize(SIZE_SMALL); break;
350 case SIZE_SCRIPT: setSize(SIZE_FOOTNOTE); break;
351 case SIZE_TINY: setSize(SIZE_SCRIPT); break;
353 lyxerr <<"Can't LyXFont::incSize on INCREASE_SIZE" << endl;
356 lyxerr <<"Can't LyXFont::incSize on DECREASE_SIZE" << endl;
359 lyxerr <<"Can't LyXFont::incSize on INHERIT_SIZE" << endl;
362 lyxerr <<"Can't LyXFont::incSize on IGNORE_SIZE" << endl;
369 /// Updates a misc setting according to request
370 LyXFont::FONT_MISC_STATE LyXFont::setMisc(FONT_MISC_STATE newfont,
373 if (newfont == TOGGLE) {
379 lyxerr <<"LyXFont::setMisc: Need state"
380 " ON or OFF to toggle. Setting to ON" << endl;
383 } else if (newfont == IGNORE)
390 /// Updates font settings according to request
391 void LyXFont::update(LyXFont const & newfont,
392 Language const * document_language, bool toggleall)
394 if (newfont.family() == family() && toggleall)
395 setFamily(INHERIT_FAMILY); // toggle 'back'
396 else if (newfont.family() != IGNORE_FAMILY)
397 setFamily(newfont.family());
398 // else it's IGNORE_SHAPE
400 // "Old" behaviour: "Setting" bold will toggle bold on/off.
401 switch (newfont.series()) {
404 if (series() == BOLD_SERIES && toggleall)
405 setSeries(MEDIUM_SERIES);
407 setSeries(BOLD_SERIES);
411 setSeries(newfont.series());
417 if (newfont.shape() == shape() && toggleall)
418 setShape(INHERIT_SHAPE); // toggle 'back'
419 else if (newfont.shape() != IGNORE_SHAPE)
420 setShape(newfont.shape());
421 // else it's IGNORE_SHAPE
423 if (newfont.size() != IGNORE_SIZE) {
424 if (newfont.size() == INCREASE_SIZE)
426 else if (newfont.size() == DECREASE_SIZE)
429 setSize(newfont.size());
432 setEmph(setMisc(newfont.emph(), emph()));
433 setUnderbar(setMisc(newfont.underbar(), underbar()));
434 setNoun(setMisc(newfont.noun(), noun()));
435 setLatex(setMisc(newfont.latex(), latex()));
437 setNumber(setMisc(newfont.number(), number()));
438 if (newfont.language() == language() && toggleall)
439 if (language() == document_language)
440 setLanguage(default_language);
442 setLanguage(document_language);
443 else if (newfont.language() != ignore_language)
444 setLanguage(newfont.language());
446 if (newfont.color() == color() && toggleall)
447 setColor(LColor::inherit); // toggle 'back'
448 else if (newfont.color() != LColor::ignore)
449 setColor(newfont.color());
453 /// Reduce font to fall back to template where possible
454 void LyXFont::reduce(LyXFont const & tmplt)
456 if (family() == tmplt.family())
457 setFamily(INHERIT_FAMILY);
458 if (series() == tmplt.series())
459 setSeries(INHERIT_SERIES);
460 if (shape() == tmplt.shape())
461 setShape(INHERIT_SHAPE);
462 if (size() == tmplt.size())
463 setSize(INHERIT_SIZE);
464 if (emph() == tmplt.emph())
466 if (underbar() == tmplt.underbar())
467 setUnderbar(INHERIT);
468 if (noun() == tmplt.noun())
470 if (latex() == tmplt.latex())
472 if (color() == tmplt.color())
473 setColor(LColor::inherit);
477 /// Realize font from a template
478 LyXFont & LyXFont::realize(LyXFont const & tmplt)
480 if (bits == inherit) {
485 if (bits.family == INHERIT_FAMILY) {
486 bits.family = tmplt.bits.family;
488 if (bits.series == INHERIT_SERIES) {
489 bits.series = tmplt.bits.series;
491 if (bits.shape == INHERIT_SHAPE) {
492 bits.shape = tmplt.bits.shape;
494 if (bits.size == INHERIT_SIZE) {
495 bits.size = tmplt.bits.size;
497 if (bits.emph == INHERIT) {
498 bits.emph = tmplt.bits.emph;
500 if (bits.underbar == INHERIT) {
501 bits.underbar = tmplt.bits.underbar;
503 if (bits.noun == INHERIT) {
504 bits.noun = tmplt.bits.noun;
506 if (bits.latex == INHERIT) {
507 bits.latex = tmplt.bits.latex;
509 if (bits.color == LColor::inherit) {
510 bits.color = tmplt.bits.color;
516 /// Is font resolved?
517 bool LyXFont::resolved() const
519 return (family() != INHERIT_FAMILY && series() != INHERIT_SERIES &&
520 shape() != INHERIT_SHAPE && size() != INHERIT_SIZE &&
521 emph() != INHERIT && underbar() != INHERIT &&
522 noun() != INHERIT && latex() != INHERIT &&
523 color() != LColor::inherit);
527 /// Build GUI description of font state
528 string const LyXFont::stateText(BufferParams * params) const
530 std::ostringstream ost;
531 if (family() != INHERIT_FAMILY)
532 ost << _(GUIFamilyNames[family()]) << ", ";
533 if (series() != INHERIT_SERIES)
534 ost << _(GUISeriesNames[series()]) << ", ";
535 if (shape() != INHERIT_SHAPE)
536 ost << _(GUIShapeNames[shape()]) << ", ";
537 if (size() != INHERIT_SIZE)
538 ost << _(GUISizeNames[size()]) << ", ";
539 if (color() != LColor::inherit)
540 ost << lcolor.getGUIName(color()) << ", ";
541 if (emph() != INHERIT)
542 ost << _("Emphasis ")
543 << _(GUIMiscNames[emph()]) << ", ";
544 if (underbar() != INHERIT)
545 ost << _("Underline ")
546 << _(GUIMiscNames[underbar()]) << ", ";
547 if (noun() != INHERIT)
548 ost << _("Noun ") << _(GUIMiscNames[noun()]) << ", ";
549 if (latex() != INHERIT)
550 ost << _("Latex ") << _(GUIMiscNames[latex()]) << ", ";
552 ost << _("Default") << ", ";
553 if (!params || (language() != params->language))
554 ost << _("Language: ") << _(language()->display()) << ", ";
556 ost << _(" Number ") << _(GUIMiscNames[number()]);
558 string buf(ost.str().c_str());
559 buf = strip(buf, ' ');
560 buf = strip(buf, ',');
565 // Set family according to lyx format string
566 LyXFont & LyXFont::setLyXFamily(string const & fam)
568 string const s = lowercase(fam);
571 while (s != LyXFamilyNames[i] && LyXFamilyNames[i] != "error") ++i;
572 if (s == LyXFamilyNames[i]) {
573 setFamily(LyXFont::FONT_FAMILY(i));
575 lyxerr << "LyXFont::setLyXFamily: Unknown family `"
576 << s << '\'' << endl;
581 // Set series according to lyx format string
582 LyXFont & LyXFont::setLyXSeries(string const & ser)
584 string const s = lowercase(ser);
587 while (s != LyXSeriesNames[i] && LyXSeriesNames[i] != "error") ++i;
588 if (s == LyXSeriesNames[i]) {
589 setSeries(LyXFont::FONT_SERIES(i));
591 lyxerr << "LyXFont::setLyXSeries: Unknown series `"
592 << s << '\'' << endl;
597 // Set shape according to lyx format string
598 LyXFont & LyXFont::setLyXShape(string const & sha)
600 string const s = lowercase(sha);
603 while (s != LyXShapeNames[i] && LyXShapeNames[i] != "error") ++i;
604 if (s == LyXShapeNames[i]) {
605 setShape(LyXFont::FONT_SHAPE(i));
607 lyxerr << "LyXFont::setLyXShape: Unknown shape `"
608 << s << '\'' << endl;
613 // Set size according to lyx format string
614 LyXFont & LyXFont::setLyXSize(string const & siz)
616 string const s = lowercase(siz);
618 while (s != LyXSizeNames[i] && LyXSizeNames[i] != "error") ++i;
619 if (s == LyXSizeNames[i]) {
620 setSize(LyXFont::FONT_SIZE(i));
622 lyxerr << "LyXFont::setLyXSize: Unknown size `"
623 << s << '\'' << endl;
628 // Set size according to lyx format string
629 LyXFont::FONT_MISC_STATE LyXFont::setLyXMisc(string const & siz)
631 string const s = lowercase(siz);
633 while (s != LyXMiscNames[i] && LyXMiscNames[i] != "error") ++i;
634 if (s == LyXMiscNames[i])
635 return FONT_MISC_STATE(i);
636 lyxerr << "LyXFont::setLyXMisc: Unknown misc flag `"
637 << s << '\'' << endl;
642 /// Sets color after LyX text format
643 LyXFont & LyXFont::setLyXColor(string const & col)
645 setColor(lcolor.getFromLyXName(col));
650 // Returns size in latex format
651 string const LyXFont::latexSize() const
653 return LaTeXSizeNames[size()];
657 // Read a font definition from given file in lyx format
659 LyXFont & LyXFont::lyxRead(LyXLex & lex)
662 bool finished = false;
663 while (!finished && lex.IsOK() && !error) {
665 string const tok = lowercase(lex.GetString());
669 } else if (tok == "endfont") {
671 } else if (tok == "family") {
673 string const ttok = lex.GetString();
675 } else if (tok == "series") {
677 string const ttok = lex.GetString();
679 } else if (tok == "shape") {
681 string const ttok = lex.GetString();
683 } else if (tok == "size") {
685 string const ttok = lex.GetString();
687 } else if (tok == "latex") {
689 string const ttok = lowercase(lex.GetString());
691 if (ttok == "no_latex") {
693 } else if (ttok == "latex") {
696 lex.printError("Illegal LaTeX type`$$Token'");
698 } else if (tok == "misc") {
700 string const ttok = lowercase(lex.GetString());
702 if (ttok == "no_bar") {
704 } else if (ttok == "no_emph") {
706 } else if (ttok == "no_noun") {
708 } else if (ttok == "emph") {
710 } else if (ttok == "underbar") {
712 } else if (ttok == "noun") {
715 lex.printError("Illegal misc type `$$Token´");
717 } else if (tok == "color") {
719 string const ttok = lex.GetString();
722 lex.printError("Unknown tag `$$Token'");
730 /// Writes the changes from this font to orgfont in .lyx format in file
731 void LyXFont::lyxWriteChanges(LyXFont const & orgfont, ostream & os) const
734 if (orgfont.family() != family()) {
735 os << "\\family " << LyXFamilyNames[family()] << " \n";
737 if (orgfont.series() != series()) {
738 os << "\\series " << LyXSeriesNames[series()] << " \n";
740 if (orgfont.shape() != shape()) {
741 os << "\\shape " << LyXShapeNames[shape()] << " \n";
743 if (orgfont.size() != size()) {
744 os << "\\size " << LyXSizeNames[size()] << " \n";
746 if (orgfont.emph() != emph()) {
747 os << "\\emph " << LyXMiscNames[emph()] << " \n";
749 if (orgfont.number() != number()) {
750 os << "\\numeric " << LyXMiscNames[number()] << " \n";
752 if (orgfont.underbar() != underbar()) {
753 // This is only for backwards compatibility
754 switch (underbar()) {
755 case OFF: os << "\\bar no \n"; break;
756 case ON: os << "\\bar under \n"; break;
757 case TOGGLE: lyxerr << "LyXFont::lyxWriteFontChanges: "
758 "TOGGLE should not appear here!"
761 case INHERIT: os << "\\bar default \n"; break;
762 case IGNORE: lyxerr << "LyXFont::lyxWriteFontChanges: "
763 "IGNORE should not appear here!"
768 if (orgfont.noun() != noun()) {
769 os << "\\noun " << LyXMiscNames[noun()] << " \n";
771 if (orgfont.latex() != latex()) {
772 // This is only for backwards compatibility
774 case OFF: os << "\\latex no_latex \n"; break;
775 case ON: os << "\\latex latex \n"; break;
776 case TOGGLE: lyxerr << "LyXFont::lyxWriteFontChanges: "
777 "TOGGLE should not appear here!"
780 case INHERIT: os << "\\latex default \n"; break;
781 case IGNORE: lyxerr << "LyXFont::lyxWriteFontChanges: "
782 "IGNORE should not appear here!"
787 if (orgfont.color() != color()) {
788 // To make us file compatible with older
789 // lyx versions we emit "default" instead
791 string col_str(lcolor.getLyXName(color()));
792 if (col_str == "inherit") col_str = "default";
793 os << "\\color " << col_str << "\n";
795 if (orgfont.language() != language()) {
797 os << "\\lang " << language()->lang() << "\n";
799 os << "\\lang unknown\n";
804 /// Writes the head of the LaTeX needed to impose this font
805 // Returns number of chars written.
806 int LyXFont::latexWriteStartChanges(ostream & os, LyXFont const & base,
807 LyXFont const & prev) const
812 if (language()->babel() != base.language()->babel() &&
813 language()->babel() != prev.language()->babel()) {
814 if (isRightToLeft() != prev.isRightToLeft()) {
815 if (isRightToLeft()) {
824 subst(lyxrc.language_command_local,
825 "$$lang", language()->babel());
827 count += tmp.length();
831 if (number() == ON && prev.number() != ON &&
832 language()->lang() == "hebrew") {
840 if (f.family() != INHERIT_FAMILY) {
842 << LaTeXFamilyNames[f.family()]
844 count += std::strlen(LaTeXFamilyNames[f.family()]) + 2;
845 env = true; //We have opened a new environment
847 if (f.series() != INHERIT_SERIES) {
849 << LaTeXSeriesNames[f.series()]
851 count += std::strlen(LaTeXSeriesNames[f.series()]) + 2;
852 env = true; //We have opened a new environment
854 if (f.shape() != INHERIT_SHAPE) {
856 << LaTeXShapeNames[f.shape()]
858 count += std::strlen(LaTeXShapeNames[f.shape()]) + 2;
859 env = true; //We have opened a new environment
861 if (f.color() != LColor::inherit && f.color() != LColor::ignore) {
863 << lcolor.getLaTeXName(f.color())
865 count += lcolor.getLaTeXName(f.color()).length() + 13;
866 env = true; //We have opened a new environment
868 if (f.emph() == ON) {
871 env = true; //We have opened a new environment
873 if (f.underbar() == ON) {
876 env = true; //We have opened a new environment
878 // \noun{} is a LyX special macro
879 if (f.noun() == ON) {
882 env = true; //We have opened a new environment
884 if (f.size() != INHERIT_SIZE) {
885 // If we didn't open an environment above, we open one here
891 << LaTeXSizeNames[f.size()]
893 count += std::strlen(LaTeXSizeNames[f.size()]) + 2;
899 /// Writes ending block of LaTeX needed to close use of this font
900 // Returns number of chars written
901 // This one corresponds to latexWriteStartChanges(). (Asger)
902 int LyXFont::latexWriteEndChanges(ostream & os, LyXFont const & base,
903 LyXFont const & next) const
908 LyXFont f = *this; // why do you need this?
909 f.reduce(base); // why isn't this just "reduce(base);" (Lgb)
910 // Because this function is const. Everything breaks if this
911 // method changes the font it represents. There is no speed penalty
912 // by using the temporary. (Asger)
915 if (f.family() != INHERIT_FAMILY) {
918 env = true; // Size change need not bother about closing env.
920 if (f.series() != INHERIT_SERIES) {
923 env = true; // Size change need not bother about closing env.
925 if (f.shape() != INHERIT_SHAPE) {
928 env = true; // Size change need not bother about closing env.
930 if (f.color() != LColor::inherit && f.color() != LColor::ignore) {
933 env = true; // Size change need not bother about closing env.
935 if (f.emph() == ON) {
938 env = true; // Size change need not bother about closing env.
940 if (f.underbar() == ON) {
943 env = true; // Size change need not bother about closing env.
945 if (f.noun() == ON) {
948 env = true; // Size change need not bother about closing env.
950 if (f.size() != INHERIT_SIZE) {
951 // We only have to close if only size changed
958 if (number() == ON && next.number() != ON &&
959 language()->lang() == "hebrew") {
964 if (language() != base.language() && language() != next.language()) {
973 LColor::color LyXFont::realColor() const
976 return LColor::latex;
977 if (color() == LColor::none)
978 return LColor::foreground;
983 // Convert logical attributes to concrete shape attribute
984 LyXFont::FONT_SHAPE LyXFont::realShape() const
986 register FONT_SHAPE s = shape();
1000 bool LyXFont::equalExceptLatex(LyXFont const & f) const
1003 f1.setLatex(f.latex());
1008 ostream & operator<<(ostream & o, LyXFont::FONT_MISC_STATE fms)
1010 return o << int(fms);