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
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);
161 lang = default_language;
165 LyXFont::LyXFont(LyXFont::FONT_INIT1)
168 lang = default_language;
172 LyXFont::LyXFont(LyXFont::FONT_INIT2)
175 lang = ignore_language;
179 LyXFont::LyXFont(LyXFont::FONT_INIT3)
182 lang = default_language;
186 LyXFont::LyXFont(LyXFont::FONT_INIT1, Language const * l)
193 LyXFont::LyXFont(LyXFont::FONT_INIT2, Language const * l)
200 LyXFont::LyXFont(LyXFont::FONT_INIT3, Language const * l)
207 LyXFont::FONT_FAMILY LyXFont::family() const
213 LyXFont::FONT_SERIES LyXFont::series() const
219 LyXFont::FONT_SHAPE LyXFont::shape() const
225 LyXFont::FONT_SIZE LyXFont::size() const
231 LyXFont::FONT_MISC_STATE LyXFont::emph() const
237 LyXFont::FONT_MISC_STATE LyXFont::underbar() const
239 return bits.underbar;
243 LyXFont::FONT_MISC_STATE LyXFont::noun() const
249 LyXFont::FONT_MISC_STATE LyXFont::latex() const
255 LColor::color LyXFont::color() const
261 Language const * LyXFont::language() const
267 LyXFont::FONT_MISC_STATE LyXFont::number() const
273 bool LyXFont::isRightToLeft() const
275 return lang->RightToLeft();
279 bool LyXFont::isVisibleRightToLeft() const
281 return (lang->RightToLeft() && latex() != ON && number() != ON);
285 LyXFont & LyXFont::setFamily(LyXFont::FONT_FAMILY f)
292 LyXFont & LyXFont::setSeries(LyXFont::FONT_SERIES s)
299 LyXFont & LyXFont::setShape(LyXFont::FONT_SHAPE s)
306 LyXFont & LyXFont::setSize(LyXFont::FONT_SIZE s)
313 LyXFont & LyXFont::setEmph(LyXFont::FONT_MISC_STATE e)
320 LyXFont & LyXFont::setUnderbar(LyXFont::FONT_MISC_STATE u)
327 LyXFont & LyXFont::setNoun(LyXFont::FONT_MISC_STATE n)
334 LyXFont & LyXFont::setLatex(LyXFont::FONT_MISC_STATE l)
341 LyXFont & LyXFont::setColor(LColor::color c)
348 LyXFont & LyXFont::setLanguage(Language const * l)
355 LyXFont & LyXFont::setNumber(LyXFont::FONT_MISC_STATE n)
362 /// Decreases font size by one
363 LyXFont & LyXFont::decSize()
366 case SIZE_HUGER: setSize(SIZE_HUGE); break;
367 case SIZE_HUGE: setSize(SIZE_LARGEST); break;
368 case SIZE_LARGEST: setSize(SIZE_LARGER); break;
369 case SIZE_LARGER: setSize(SIZE_LARGE); break;
370 case SIZE_LARGE: setSize(SIZE_NORMAL); break;
371 case SIZE_NORMAL: setSize(SIZE_SMALL); break;
372 case SIZE_SMALL: setSize(SIZE_FOOTNOTE); break;
373 case SIZE_FOOTNOTE: setSize(SIZE_SCRIPT); break;
374 case SIZE_SCRIPT: setSize(SIZE_TINY); break;
375 case SIZE_TINY: break;
377 lyxerr << "Can't LyXFont::decSize on INCREASE_SIZE" << endl;
380 lyxerr <<"Can't LyXFont::decSize on DECREASE_SIZE" << endl;
383 lyxerr <<"Can't LyXFont::decSize on INHERIT_SIZE" << endl;
386 lyxerr <<"Can't LyXFont::decSize on IGNORE_SIZE" << endl;
393 /// Increases font size by one
394 LyXFont & LyXFont::incSize()
397 case SIZE_HUGER: break;
398 case SIZE_HUGE: setSize(SIZE_HUGER); break;
399 case SIZE_LARGEST: setSize(SIZE_HUGE); break;
400 case SIZE_LARGER: setSize(SIZE_LARGEST); break;
401 case SIZE_LARGE: setSize(SIZE_LARGER); break;
402 case SIZE_NORMAL: setSize(SIZE_LARGE); break;
403 case SIZE_SMALL: setSize(SIZE_NORMAL); break;
404 case SIZE_FOOTNOTE: setSize(SIZE_SMALL); break;
405 case SIZE_SCRIPT: setSize(SIZE_FOOTNOTE); break;
406 case SIZE_TINY: setSize(SIZE_SCRIPT); break;
408 lyxerr <<"Can't LyXFont::incSize on INCREASE_SIZE" << endl;
411 lyxerr <<"Can't LyXFont::incSize on DECREASE_SIZE" << endl;
414 lyxerr <<"Can't LyXFont::incSize on INHERIT_SIZE" << endl;
417 lyxerr <<"Can't LyXFont::incSize on IGNORE_SIZE" << endl;
424 /// Updates a misc setting according to request
425 LyXFont::FONT_MISC_STATE LyXFont::setMisc(FONT_MISC_STATE newfont,
428 if (newfont == TOGGLE) {
434 lyxerr <<"LyXFont::setMisc: Need state"
435 " ON or OFF to toggle. Setting to ON" << endl;
438 } else if (newfont == IGNORE)
445 /// Updates font settings according to request
446 void LyXFont::update(LyXFont const & newfont,
447 Language const * document_language, bool toggleall)
449 if (newfont.family() == family() && toggleall)
450 setFamily(INHERIT_FAMILY); // toggle 'back'
451 else if (newfont.family() != IGNORE_FAMILY)
452 setFamily(newfont.family());
453 // else it's IGNORE_SHAPE
455 // "Old" behaviour: "Setting" bold will toggle bold on/off.
456 switch (newfont.series()) {
459 if (series() == BOLD_SERIES && toggleall)
460 setSeries(MEDIUM_SERIES);
462 setSeries(BOLD_SERIES);
466 setSeries(newfont.series());
472 if (newfont.shape() == shape() && toggleall)
473 setShape(INHERIT_SHAPE); // toggle 'back'
474 else if (newfont.shape() != IGNORE_SHAPE)
475 setShape(newfont.shape());
476 // else it's IGNORE_SHAPE
478 if (newfont.size() != IGNORE_SIZE) {
479 if (newfont.size() == INCREASE_SIZE)
481 else if (newfont.size() == DECREASE_SIZE)
484 setSize(newfont.size());
487 setEmph(setMisc(newfont.emph(), emph()));
488 setUnderbar(setMisc(newfont.underbar(), underbar()));
489 setNoun(setMisc(newfont.noun(), noun()));
490 setLatex(setMisc(newfont.latex(), latex()));
492 setNumber(setMisc(newfont.number(), number()));
493 if (newfont.language() == language() && toggleall)
494 if (language() == document_language)
495 setLanguage(default_language);
497 setLanguage(document_language);
498 else if (newfont.language() != ignore_language)
499 setLanguage(newfont.language());
501 if (newfont.color() == color() && toggleall)
502 setColor(LColor::inherit); // toggle 'back'
503 else if (newfont.color() != LColor::ignore)
504 setColor(newfont.color());
508 /// Reduce font to fall back to template where possible
509 void LyXFont::reduce(LyXFont const & tmplt)
511 if (family() == tmplt.family())
512 setFamily(INHERIT_FAMILY);
513 if (series() == tmplt.series())
514 setSeries(INHERIT_SERIES);
515 if (shape() == tmplt.shape())
516 setShape(INHERIT_SHAPE);
517 if (size() == tmplt.size())
518 setSize(INHERIT_SIZE);
519 if (emph() == tmplt.emph())
521 if (underbar() == tmplt.underbar())
522 setUnderbar(INHERIT);
523 if (noun() == tmplt.noun())
525 if (latex() == tmplt.latex())
527 if (color() == tmplt.color())
528 setColor(LColor::inherit);
532 /// Realize font from a template
533 LyXFont & LyXFont::realize(LyXFont const & tmplt)
535 if (bits == inherit) {
540 if (bits.family == INHERIT_FAMILY) {
541 bits.family = tmplt.bits.family;
543 if (bits.series == INHERIT_SERIES) {
544 bits.series = tmplt.bits.series;
546 if (bits.shape == INHERIT_SHAPE) {
547 bits.shape = tmplt.bits.shape;
549 if (bits.size == INHERIT_SIZE) {
550 bits.size = tmplt.bits.size;
552 if (bits.emph == INHERIT) {
553 bits.emph = tmplt.bits.emph;
555 if (bits.underbar == INHERIT) {
556 bits.underbar = tmplt.bits.underbar;
558 if (bits.noun == INHERIT) {
559 bits.noun = tmplt.bits.noun;
561 if (bits.latex == INHERIT) {
562 bits.latex = tmplt.bits.latex;
564 if (bits.color == LColor::inherit) {
565 bits.color = tmplt.bits.color;
571 /// Is font resolved?
572 bool LyXFont::resolved() const
574 return (family() != INHERIT_FAMILY && series() != INHERIT_SERIES &&
575 shape() != INHERIT_SHAPE && size() != INHERIT_SIZE &&
576 emph() != INHERIT && underbar() != INHERIT &&
577 noun() != INHERIT && latex() != INHERIT &&
578 color() != LColor::inherit);
582 /// Build GUI description of font state
583 string const LyXFont::stateText(BufferParams * params) const
585 std::ostringstream ost;
586 if (family() != INHERIT_FAMILY)
587 ost << _(GUIFamilyNames[family()]) << ", ";
588 if (series() != INHERIT_SERIES)
589 ost << _(GUISeriesNames[series()]) << ", ";
590 if (shape() != INHERIT_SHAPE)
591 ost << _(GUIShapeNames[shape()]) << ", ";
592 if (size() != INHERIT_SIZE)
593 ost << _(GUISizeNames[size()]) << ", ";
594 if (color() != LColor::inherit)
595 ost << lcolor.getGUIName(color()) << ", ";
596 if (emph() != INHERIT)
597 ost << _("Emphasis ")
598 << _(GUIMiscNames[emph()]) << ", ";
599 if (underbar() != INHERIT)
600 ost << _("Underline ")
601 << _(GUIMiscNames[underbar()]) << ", ";
602 if (noun() != INHERIT)
603 ost << _("Noun ") << _(GUIMiscNames[noun()]) << ", ";
604 if (latex() != INHERIT)
605 ost << _("Latex ") << _(GUIMiscNames[latex()]) << ", ";
607 ost << _("Default") << ", ";
608 if (!params || (language() != params->language))
609 ost << _("Language: ") << _(language()->display()) << ", ";
611 ost << _(" Number ") << _(GUIMiscNames[number()]);
613 string buf(ost.str().c_str());
614 buf = strip(buf, ' ');
615 buf = strip(buf, ',');
620 // Set family according to lyx format string
621 LyXFont & LyXFont::setLyXFamily(string const & fam)
623 string const s = lowercase(fam);
626 while (s != LyXFamilyNames[i] && LyXFamilyNames[i] != "error") ++i;
627 if (s == LyXFamilyNames[i]) {
628 setFamily(LyXFont::FONT_FAMILY(i));
630 lyxerr << "LyXFont::setLyXFamily: Unknown family `"
631 << s << '\'' << endl;
636 // Set series according to lyx format string
637 LyXFont & LyXFont::setLyXSeries(string const & ser)
639 string const s = lowercase(ser);
642 while (s != LyXSeriesNames[i] && LyXSeriesNames[i] != "error") ++i;
643 if (s == LyXSeriesNames[i]) {
644 setSeries(LyXFont::FONT_SERIES(i));
646 lyxerr << "LyXFont::setLyXSeries: Unknown series `"
647 << s << '\'' << endl;
652 // Set shape according to lyx format string
653 LyXFont & LyXFont::setLyXShape(string const & sha)
655 string const s = lowercase(sha);
658 while (s != LyXShapeNames[i] && LyXShapeNames[i] != "error") ++i;
659 if (s == LyXShapeNames[i]) {
660 setShape(LyXFont::FONT_SHAPE(i));
662 lyxerr << "LyXFont::setLyXShape: Unknown shape `"
663 << s << '\'' << endl;
668 // Set size according to lyx format string
669 LyXFont & LyXFont::setLyXSize(string const & siz)
671 string const s = lowercase(siz);
673 while (s != LyXSizeNames[i] && LyXSizeNames[i] != "error") ++i;
674 if (s == LyXSizeNames[i]) {
675 setSize(LyXFont::FONT_SIZE(i));
677 lyxerr << "LyXFont::setLyXSize: Unknown size `"
678 << s << '\'' << endl;
683 // Set size according to lyx format string
684 LyXFont::FONT_MISC_STATE LyXFont::setLyXMisc(string const & siz)
686 string const s = lowercase(siz);
688 while (s != LyXMiscNames[i] && LyXMiscNames[i] != "error") ++i;
689 if (s == LyXMiscNames[i])
690 return FONT_MISC_STATE(i);
691 lyxerr << "LyXFont::setLyXMisc: Unknown misc flag `"
692 << s << '\'' << endl;
697 /// Sets color after LyX text format
698 LyXFont & LyXFont::setLyXColor(string const & col)
700 setColor(lcolor.getFromLyXName(col));
705 // Returns size in latex format
706 string const LyXFont::latexSize() const
708 return LaTeXSizeNames[size()];
712 // Read a font definition from given file in lyx format
714 LyXFont & LyXFont::lyxRead(LyXLex & lex)
717 bool finished = false;
718 while (!finished && lex.IsOK() && !error) {
720 string const tok = lowercase(lex.GetString());
724 } else if (tok == "endfont") {
726 } else if (tok == "family") {
728 string const ttok = lex.GetString();
730 } else if (tok == "series") {
732 string const ttok = lex.GetString();
734 } else if (tok == "shape") {
736 string const ttok = lex.GetString();
738 } else if (tok == "size") {
740 string const ttok = lex.GetString();
742 } else if (tok == "latex") {
744 string const ttok = lowercase(lex.GetString());
746 if (ttok == "no_latex") {
748 } else if (ttok == "latex") {
751 lex.printError("Illegal LaTeX type`$$Token'");
753 } else if (tok == "misc") {
755 string const ttok = lowercase(lex.GetString());
757 if (ttok == "no_bar") {
759 } else if (ttok == "no_emph") {
761 } else if (ttok == "no_noun") {
763 } else if (ttok == "emph") {
765 } else if (ttok == "underbar") {
767 } else if (ttok == "noun") {
770 lex.printError("Illegal misc type `$$Token´");
772 } else if (tok == "color") {
774 string const ttok = lex.GetString();
777 lex.printError("Unknown tag `$$Token'");
785 /// Writes the changes from this font to orgfont in .lyx format in file
786 void LyXFont::lyxWriteChanges(LyXFont const & orgfont, ostream & os) const
789 if (orgfont.family() != family()) {
790 os << "\\family " << LyXFamilyNames[family()] << " \n";
792 if (orgfont.series() != series()) {
793 os << "\\series " << LyXSeriesNames[series()] << " \n";
795 if (orgfont.shape() != shape()) {
796 os << "\\shape " << LyXShapeNames[shape()] << " \n";
798 if (orgfont.size() != size()) {
799 os << "\\size " << LyXSizeNames[size()] << " \n";
801 if (orgfont.emph() != emph()) {
802 os << "\\emph " << LyXMiscNames[emph()] << " \n";
804 if (orgfont.number() != number()) {
805 os << "\\numeric " << LyXMiscNames[number()] << " \n";
807 if (orgfont.underbar() != underbar()) {
808 // This is only for backwards compatibility
809 switch (underbar()) {
810 case OFF: os << "\\bar no \n"; break;
811 case ON: os << "\\bar under \n"; break;
812 case TOGGLE: lyxerr << "LyXFont::lyxWriteFontChanges: "
813 "TOGGLE should not appear here!"
816 case INHERIT: os << "\\bar default \n"; break;
817 case IGNORE: lyxerr << "LyXFont::lyxWriteFontChanges: "
818 "IGNORE should not appear here!"
823 if (orgfont.noun() != noun()) {
824 os << "\\noun " << LyXMiscNames[noun()] << " \n";
826 if (orgfont.latex() != latex()) {
827 // This is only for backwards compatibility
829 case OFF: os << "\\latex no_latex \n"; break;
830 case ON: os << "\\latex latex \n"; break;
831 case TOGGLE: lyxerr << "LyXFont::lyxWriteFontChanges: "
832 "TOGGLE should not appear here!"
835 case INHERIT: os << "\\latex default \n"; break;
836 case IGNORE: lyxerr << "LyXFont::lyxWriteFontChanges: "
837 "IGNORE should not appear here!"
842 if (orgfont.color() != color()) {
843 // To make us file compatible with older
844 // lyx versions we emit "default" instead
846 string col_str(lcolor.getLyXName(color()));
847 if (col_str == "inherit") col_str = "default";
848 os << "\\color " << col_str << "\n";
850 if (orgfont.language() != language()) {
852 os << "\\lang " << language()->lang() << "\n";
854 os << "\\lang unknown\n";
859 /// Writes the head of the LaTeX needed to impose this font
860 // Returns number of chars written.
861 int LyXFont::latexWriteStartChanges(ostream & os, LyXFont const & base,
862 LyXFont const & prev) const
867 if (language()->babel() != base.language()->babel() &&
868 language()->babel() != prev.language()->babel()) {
869 if (isRightToLeft() != prev.isRightToLeft()) {
870 if (isRightToLeft()) {
879 subst(lyxrc.language_command_local,
880 "$$lang", language()->babel());
882 count += tmp.length();
886 if (number() == ON && prev.number() != ON &&
887 language()->lang() == "hebrew") {
895 if (f.family() != INHERIT_FAMILY) {
897 << LaTeXFamilyNames[f.family()]
899 count += strlen(LaTeXFamilyNames[f.family()]) + 2;
900 env = true; //We have opened a new environment
902 if (f.series() != INHERIT_SERIES) {
904 << LaTeXSeriesNames[f.series()]
906 count += strlen(LaTeXSeriesNames[f.series()]) + 2;
907 env = true; //We have opened a new environment
909 if (f.shape() != INHERIT_SHAPE) {
911 << LaTeXShapeNames[f.shape()]
913 count += strlen(LaTeXShapeNames[f.shape()]) + 2;
914 env = true; //We have opened a new environment
916 if (f.color() != LColor::inherit && f.color() != LColor::ignore) {
918 << lcolor.getLaTeXName(f.color())
920 count += lcolor.getLaTeXName(f.color()).length() + 13;
921 env = true; //We have opened a new environment
923 if (f.emph() == ON) {
926 env = true; //We have opened a new environment
928 if (f.underbar() == ON) {
931 env = true; //We have opened a new environment
933 // \noun{} is a LyX special macro
934 if (f.noun() == ON) {
937 env = true; //We have opened a new environment
939 if (f.size() != INHERIT_SIZE) {
940 // If we didn't open an environment above, we open one here
946 << LaTeXSizeNames[f.size()]
948 count += strlen(LaTeXSizeNames[f.size()]) + 2;
954 /// Writes ending block of LaTeX needed to close use of this font
955 // Returns number of chars written
956 // This one corresponds to latexWriteStartChanges(). (Asger)
957 int LyXFont::latexWriteEndChanges(ostream & os, LyXFont const & base,
958 LyXFont const & next) const
963 LyXFont f = *this; // why do you need this?
964 f.reduce(base); // why isn't this just "reduce(base);" (Lgb)
965 // Because this function is const. Everything breaks if this
966 // method changes the font it represents. There is no speed penalty
967 // by using the temporary. (Asger)
970 if (f.family() != INHERIT_FAMILY) {
973 env = true; // Size change need not bother about closing env.
975 if (f.series() != INHERIT_SERIES) {
978 env = true; // Size change need not bother about closing env.
980 if (f.shape() != INHERIT_SHAPE) {
983 env = true; // Size change need not bother about closing env.
985 if (f.color() != LColor::inherit && f.color() != LColor::ignore) {
988 env = true; // Size change need not bother about closing env.
990 if (f.emph() == ON) {
993 env = true; // Size change need not bother about closing env.
995 if (f.underbar() == ON) {
998 env = true; // Size change need not bother about closing env.
1000 if (f.noun() == ON) {
1003 env = true; // Size change need not bother about closing env.
1005 if (f.size() != INHERIT_SIZE) {
1006 // We only have to close if only size changed
1013 if (number() == ON && next.number() != ON &&
1014 language()->lang() == "hebrew") {
1019 if (language() != base.language() && language() != next.language()) {
1028 LColor::color LyXFont::realColor() const
1031 return LColor::latex;
1032 if (color() == LColor::none)
1033 return LColor::foreground;
1038 // Convert logical attributes to concrete shape attribute
1039 LyXFont::FONT_SHAPE LyXFont::realShape() const
1041 register FONT_SHAPE s = shape();
1050 s = SMALLCAPS_SHAPE;
1055 bool LyXFont::equalExceptLatex(LyXFont const & f) const
1058 f1.setLatex(f.latex());
1063 ostream & operator<<(ostream & o, LyXFont::FONT_MISC_STATE fms)
1065 return o << int(fms);