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);
159 : bits(sane), lang(default_language)
163 LyXFont::LyXFont(LyXFont::FONT_INIT1)
164 : bits(inherit), lang(default_language)
168 LyXFont::LyXFont(LyXFont::FONT_INIT2)
169 : bits(ignore), lang(ignore_language)
173 LyXFont::LyXFont(LyXFont::FONT_INIT3)
174 : bits(sane), lang(default_language)
178 LyXFont::LyXFont(LyXFont::FONT_INIT1, Language const * l)
179 : bits(inherit), lang(l)
183 LyXFont::LyXFont(LyXFont::FONT_INIT2, Language const * l)
184 : bits(ignore), lang(l)
188 LyXFont::LyXFont(LyXFont::FONT_INIT3, Language const * l)
189 : bits(sane), lang(l)
193 LyXFont::FONT_FAMILY LyXFont::family() const
199 LyXFont::FONT_SERIES LyXFont::series() const
205 LyXFont::FONT_SIZE LyXFont::size() const
211 LyXFont::FONT_MISC_STATE LyXFont::underbar() const
213 return bits.underbar;
217 LyXFont::FONT_MISC_STATE LyXFont::noun() const
223 LyXFont::FONT_MISC_STATE LyXFont::latex() const
229 LColor::color LyXFont::color() const
235 Language const * LyXFont::language() const
241 LyXFont::FONT_MISC_STATE LyXFont::number() const
247 bool LyXFont::isRightToLeft() const
249 return lang->RightToLeft();
253 bool LyXFont::isVisibleRightToLeft() const
255 return (lang->RightToLeft() && latex() != ON && number() != ON);
259 LyXFont & LyXFont::setFamily(LyXFont::FONT_FAMILY f)
266 LyXFont & LyXFont::setSeries(LyXFont::FONT_SERIES s)
273 LyXFont & LyXFont::setShape(LyXFont::FONT_SHAPE s)
280 LyXFont & LyXFont::setSize(LyXFont::FONT_SIZE s)
287 LyXFont & LyXFont::setEmph(LyXFont::FONT_MISC_STATE e)
294 LyXFont & LyXFont::setUnderbar(LyXFont::FONT_MISC_STATE u)
301 LyXFont & LyXFont::setNoun(LyXFont::FONT_MISC_STATE n)
308 LyXFont & LyXFont::setLatex(LyXFont::FONT_MISC_STATE l)
315 LyXFont & LyXFont::setColor(LColor::color c)
322 LyXFont & LyXFont::setLanguage(Language const * l)
329 LyXFont & LyXFont::setNumber(LyXFont::FONT_MISC_STATE n)
336 /// Decreases font size by one
337 LyXFont & LyXFont::decSize()
340 case SIZE_HUGER: setSize(SIZE_HUGE); break;
341 case SIZE_HUGE: setSize(SIZE_LARGEST); break;
342 case SIZE_LARGEST: setSize(SIZE_LARGER); break;
343 case SIZE_LARGER: setSize(SIZE_LARGE); break;
344 case SIZE_LARGE: setSize(SIZE_NORMAL); break;
345 case SIZE_NORMAL: setSize(SIZE_SMALL); break;
346 case SIZE_SMALL: setSize(SIZE_FOOTNOTE); break;
347 case SIZE_FOOTNOTE: setSize(SIZE_SCRIPT); break;
348 case SIZE_SCRIPT: setSize(SIZE_TINY); break;
349 case SIZE_TINY: break;
351 lyxerr << "Can't LyXFont::decSize on INCREASE_SIZE" << endl;
354 lyxerr <<"Can't LyXFont::decSize on DECREASE_SIZE" << endl;
357 lyxerr <<"Can't LyXFont::decSize on INHERIT_SIZE" << endl;
360 lyxerr <<"Can't LyXFont::decSize on IGNORE_SIZE" << endl;
367 /// Increases font size by one
368 LyXFont & LyXFont::incSize()
371 case SIZE_HUGER: break;
372 case SIZE_HUGE: setSize(SIZE_HUGER); break;
373 case SIZE_LARGEST: setSize(SIZE_HUGE); break;
374 case SIZE_LARGER: setSize(SIZE_LARGEST); break;
375 case SIZE_LARGE: setSize(SIZE_LARGER); break;
376 case SIZE_NORMAL: setSize(SIZE_LARGE); break;
377 case SIZE_SMALL: setSize(SIZE_NORMAL); break;
378 case SIZE_FOOTNOTE: setSize(SIZE_SMALL); break;
379 case SIZE_SCRIPT: setSize(SIZE_FOOTNOTE); break;
380 case SIZE_TINY: setSize(SIZE_SCRIPT); break;
382 lyxerr <<"Can't LyXFont::incSize on INCREASE_SIZE" << endl;
385 lyxerr <<"Can't LyXFont::incSize on DECREASE_SIZE" << endl;
388 lyxerr <<"Can't LyXFont::incSize on INHERIT_SIZE" << endl;
391 lyxerr <<"Can't LyXFont::incSize on IGNORE_SIZE" << endl;
398 /// Updates a misc setting according to request
399 LyXFont::FONT_MISC_STATE LyXFont::setMisc(FONT_MISC_STATE newfont,
402 if (newfont == TOGGLE) {
408 lyxerr <<"LyXFont::setMisc: Need state"
409 " ON or OFF to toggle. Setting to ON" << endl;
412 } else if (newfont == IGNORE)
419 /// Updates font settings according to request
420 void LyXFont::update(LyXFont const & newfont,
421 Language const * document_language, bool toggleall)
423 if (newfont.family() == family() && toggleall)
424 setFamily(INHERIT_FAMILY); // toggle 'back'
425 else if (newfont.family() != IGNORE_FAMILY)
426 setFamily(newfont.family());
427 // else it's IGNORE_SHAPE
429 // "Old" behaviour: "Setting" bold will toggle bold on/off.
430 switch (newfont.series()) {
433 if (series() == BOLD_SERIES && toggleall)
434 setSeries(MEDIUM_SERIES);
436 setSeries(BOLD_SERIES);
440 setSeries(newfont.series());
446 if (newfont.shape() == shape() && toggleall)
447 setShape(INHERIT_SHAPE); // toggle 'back'
448 else if (newfont.shape() != IGNORE_SHAPE)
449 setShape(newfont.shape());
450 // else it's IGNORE_SHAPE
452 if (newfont.size() != IGNORE_SIZE) {
453 if (newfont.size() == INCREASE_SIZE)
455 else if (newfont.size() == DECREASE_SIZE)
458 setSize(newfont.size());
461 setEmph(setMisc(newfont.emph(), emph()));
462 setUnderbar(setMisc(newfont.underbar(), underbar()));
463 setNoun(setMisc(newfont.noun(), noun()));
464 setLatex(setMisc(newfont.latex(), latex()));
466 setNumber(setMisc(newfont.number(), number()));
467 if (newfont.language() == language() && toggleall)
468 if (language() == document_language)
469 setLanguage(default_language);
471 setLanguage(document_language);
472 else if (newfont.language() != ignore_language)
473 setLanguage(newfont.language());
475 if (newfont.color() == color() && toggleall)
476 setColor(LColor::inherit); // toggle 'back'
477 else if (newfont.color() != LColor::ignore)
478 setColor(newfont.color());
482 /// Reduce font to fall back to template where possible
483 void LyXFont::reduce(LyXFont const & tmplt)
485 if (family() == tmplt.family())
486 setFamily(INHERIT_FAMILY);
487 if (series() == tmplt.series())
488 setSeries(INHERIT_SERIES);
489 if (shape() == tmplt.shape())
490 setShape(INHERIT_SHAPE);
491 if (size() == tmplt.size())
492 setSize(INHERIT_SIZE);
493 if (emph() == tmplt.emph())
495 if (underbar() == tmplt.underbar())
496 setUnderbar(INHERIT);
497 if (noun() == tmplt.noun())
499 if (latex() == tmplt.latex())
501 if (color() == tmplt.color())
502 setColor(LColor::inherit);
506 /// Realize font from a template
507 LyXFont & LyXFont::realize(LyXFont const & tmplt)
509 if (bits == inherit) {
514 if (bits.family == INHERIT_FAMILY) {
515 bits.family = tmplt.bits.family;
517 if (bits.series == INHERIT_SERIES) {
518 bits.series = tmplt.bits.series;
520 if (bits.shape == INHERIT_SHAPE) {
521 bits.shape = tmplt.bits.shape;
523 if (bits.size == INHERIT_SIZE) {
524 bits.size = tmplt.bits.size;
526 if (bits.emph == INHERIT) {
527 bits.emph = tmplt.bits.emph;
529 if (bits.underbar == INHERIT) {
530 bits.underbar = tmplt.bits.underbar;
532 if (bits.noun == INHERIT) {
533 bits.noun = tmplt.bits.noun;
535 if (bits.latex == INHERIT) {
536 bits.latex = tmplt.bits.latex;
538 if (bits.color == LColor::inherit) {
539 bits.color = tmplt.bits.color;
545 /// Is font resolved?
546 bool LyXFont::resolved() const
548 return (family() != INHERIT_FAMILY && series() != INHERIT_SERIES &&
549 shape() != INHERIT_SHAPE && size() != INHERIT_SIZE &&
550 emph() != INHERIT && underbar() != INHERIT &&
551 noun() != INHERIT && latex() != INHERIT &&
552 color() != LColor::inherit);
556 /// Build GUI description of font state
557 string const LyXFont::stateText(BufferParams * params) const
559 std::ostringstream ost;
560 if (family() != INHERIT_FAMILY)
561 ost << _(GUIFamilyNames[family()]) << ", ";
562 if (series() != INHERIT_SERIES)
563 ost << _(GUISeriesNames[series()]) << ", ";
564 if (shape() != INHERIT_SHAPE)
565 ost << _(GUIShapeNames[shape()]) << ", ";
566 if (size() != INHERIT_SIZE)
567 ost << _(GUISizeNames[size()]) << ", ";
568 if (color() != LColor::inherit)
569 ost << lcolor.getGUIName(color()) << ", ";
570 if (emph() != INHERIT)
571 ost << _("Emphasis ")
572 << _(GUIMiscNames[emph()]) << ", ";
573 if (underbar() != INHERIT)
574 ost << _("Underline ")
575 << _(GUIMiscNames[underbar()]) << ", ";
576 if (noun() != INHERIT)
577 ost << _("Noun ") << _(GUIMiscNames[noun()]) << ", ";
578 if (latex() != INHERIT)
579 ost << _("Latex ") << _(GUIMiscNames[latex()]) << ", ";
581 ost << _("Default") << ", ";
582 if (!params || (language() != params->language))
583 ost << _("Language: ") << _(language()->display()) << ", ";
585 ost << _(" Number ") << _(GUIMiscNames[number()]);
587 string buf(ost.str().c_str());
588 buf = strip(buf, ' ');
589 buf = strip(buf, ',');
594 // Set family according to lyx format string
595 LyXFont & LyXFont::setLyXFamily(string const & fam)
597 string const s = lowercase(fam);
600 while (s != LyXFamilyNames[i] && LyXFamilyNames[i] != "error") ++i;
601 if (s == LyXFamilyNames[i]) {
602 setFamily(LyXFont::FONT_FAMILY(i));
604 lyxerr << "LyXFont::setLyXFamily: Unknown family `"
605 << s << '\'' << endl;
610 // Set series according to lyx format string
611 LyXFont & LyXFont::setLyXSeries(string const & ser)
613 string const s = lowercase(ser);
616 while (s != LyXSeriesNames[i] && LyXSeriesNames[i] != "error") ++i;
617 if (s == LyXSeriesNames[i]) {
618 setSeries(LyXFont::FONT_SERIES(i));
620 lyxerr << "LyXFont::setLyXSeries: Unknown series `"
621 << s << '\'' << endl;
626 // Set shape according to lyx format string
627 LyXFont & LyXFont::setLyXShape(string const & sha)
629 string const s = lowercase(sha);
632 while (s != LyXShapeNames[i] && LyXShapeNames[i] != "error") ++i;
633 if (s == LyXShapeNames[i]) {
634 setShape(LyXFont::FONT_SHAPE(i));
636 lyxerr << "LyXFont::setLyXShape: Unknown shape `"
637 << s << '\'' << endl;
642 // Set size according to lyx format string
643 LyXFont & LyXFont::setLyXSize(string const & siz)
645 string const s = lowercase(siz);
647 while (s != LyXSizeNames[i] && LyXSizeNames[i] != "error") ++i;
648 if (s == LyXSizeNames[i]) {
649 setSize(LyXFont::FONT_SIZE(i));
651 lyxerr << "LyXFont::setLyXSize: Unknown size `"
652 << s << '\'' << endl;
657 // Set size according to lyx format string
658 LyXFont::FONT_MISC_STATE LyXFont::setLyXMisc(string const & siz)
660 string const s = lowercase(siz);
662 while (s != LyXMiscNames[i] && LyXMiscNames[i] != "error") ++i;
663 if (s == LyXMiscNames[i])
664 return FONT_MISC_STATE(i);
665 lyxerr << "LyXFont::setLyXMisc: Unknown misc flag `"
666 << s << '\'' << endl;
671 /// Sets color after LyX text format
672 LyXFont & LyXFont::setLyXColor(string const & col)
674 setColor(lcolor.getFromLyXName(col));
679 // Returns size in latex format
680 string const LyXFont::latexSize() const
682 return LaTeXSizeNames[size()];
686 // Read a font definition from given file in lyx format
688 LyXFont & LyXFont::lyxRead(LyXLex & lex)
691 bool finished = false;
692 while (!finished && lex.IsOK() && !error) {
694 string const tok = lowercase(lex.GetString());
698 } else if (tok == "endfont") {
700 } else if (tok == "family") {
702 string const ttok = lex.GetString();
704 } else if (tok == "series") {
706 string const ttok = lex.GetString();
708 } else if (tok == "shape") {
710 string const ttok = lex.GetString();
712 } else if (tok == "size") {
714 string const ttok = lex.GetString();
716 } else if (tok == "latex") {
718 string const ttok = lowercase(lex.GetString());
720 if (ttok == "no_latex") {
722 } else if (ttok == "latex") {
725 lex.printError("Illegal LaTeX type`$$Token'");
727 } else if (tok == "misc") {
729 string const ttok = lowercase(lex.GetString());
731 if (ttok == "no_bar") {
733 } else if (ttok == "no_emph") {
735 } else if (ttok == "no_noun") {
737 } else if (ttok == "emph") {
739 } else if (ttok == "underbar") {
741 } else if (ttok == "noun") {
744 lex.printError("Illegal misc type `$$Token´");
746 } else if (tok == "color") {
748 string const ttok = lex.GetString();
751 lex.printError("Unknown tag `$$Token'");
759 /// Writes the changes from this font to orgfont in .lyx format in file
760 void LyXFont::lyxWriteChanges(LyXFont const & orgfont, ostream & os) const
763 if (orgfont.family() != family()) {
764 os << "\\family " << LyXFamilyNames[family()] << " \n";
766 if (orgfont.series() != series()) {
767 os << "\\series " << LyXSeriesNames[series()] << " \n";
769 if (orgfont.shape() != shape()) {
770 os << "\\shape " << LyXShapeNames[shape()] << " \n";
772 if (orgfont.size() != size()) {
773 os << "\\size " << LyXSizeNames[size()] << " \n";
775 if (orgfont.emph() != emph()) {
776 os << "\\emph " << LyXMiscNames[emph()] << " \n";
778 if (orgfont.number() != number()) {
779 os << "\\numeric " << LyXMiscNames[number()] << " \n";
781 if (orgfont.underbar() != underbar()) {
782 // This is only for backwards compatibility
783 switch (underbar()) {
784 case OFF: os << "\\bar no \n"; break;
785 case ON: os << "\\bar under \n"; break;
786 case TOGGLE: lyxerr << "LyXFont::lyxWriteFontChanges: "
787 "TOGGLE should not appear here!"
790 case INHERIT: os << "\\bar default \n"; break;
791 case IGNORE: lyxerr << "LyXFont::lyxWriteFontChanges: "
792 "IGNORE should not appear here!"
797 if (orgfont.noun() != noun()) {
798 os << "\\noun " << LyXMiscNames[noun()] << " \n";
800 if (orgfont.latex() != latex()) {
801 // This is only for backwards compatibility
803 case OFF: os << "\\latex no_latex \n"; break;
804 case ON: os << "\\latex latex \n"; break;
805 case TOGGLE: lyxerr << "LyXFont::lyxWriteFontChanges: "
806 "TOGGLE should not appear here!"
809 case INHERIT: os << "\\latex default \n"; break;
810 case IGNORE: lyxerr << "LyXFont::lyxWriteFontChanges: "
811 "IGNORE should not appear here!"
816 if (orgfont.color() != color()) {
817 // To make us file compatible with older
818 // lyx versions we emit "default" instead
820 string col_str(lcolor.getLyXName(color()));
821 if (col_str == "inherit") col_str = "default";
822 os << "\\color " << col_str << "\n";
824 if (orgfont.language() != language()) {
826 os << "\\lang " << language()->lang() << "\n";
828 os << "\\lang unknown\n";
833 /// Writes the head of the LaTeX needed to impose this font
834 // Returns number of chars written.
835 int LyXFont::latexWriteStartChanges(ostream & os, LyXFont const & base,
836 LyXFont const & prev) const
841 if (language()->babel() != base.language()->babel() &&
842 language()->babel() != prev.language()->babel()) {
843 if (isRightToLeft() != prev.isRightToLeft()) {
844 if (isRightToLeft()) {
853 subst(lyxrc.language_command_local,
854 "$$lang", language()->babel());
856 count += tmp.length();
860 if (number() == ON && prev.number() != ON &&
861 language()->lang() == "hebrew") {
869 if (f.family() != INHERIT_FAMILY) {
871 << LaTeXFamilyNames[f.family()]
873 count += strlen(LaTeXFamilyNames[f.family()]) + 2;
874 env = true; //We have opened a new environment
876 if (f.series() != INHERIT_SERIES) {
878 << LaTeXSeriesNames[f.series()]
880 count += strlen(LaTeXSeriesNames[f.series()]) + 2;
881 env = true; //We have opened a new environment
883 if (f.shape() != INHERIT_SHAPE) {
885 << LaTeXShapeNames[f.shape()]
887 count += strlen(LaTeXShapeNames[f.shape()]) + 2;
888 env = true; //We have opened a new environment
890 if (f.color() != LColor::inherit && f.color() != LColor::ignore) {
892 << lcolor.getLaTeXName(f.color())
894 count += lcolor.getLaTeXName(f.color()).length() + 13;
895 env = true; //We have opened a new environment
897 if (f.emph() == ON) {
900 env = true; //We have opened a new environment
902 if (f.underbar() == ON) {
905 env = true; //We have opened a new environment
907 // \noun{} is a LyX special macro
908 if (f.noun() == ON) {
911 env = true; //We have opened a new environment
913 if (f.size() != INHERIT_SIZE) {
914 // If we didn't open an environment above, we open one here
920 << LaTeXSizeNames[f.size()]
922 count += strlen(LaTeXSizeNames[f.size()]) + 2;
928 /// Writes ending block of LaTeX needed to close use of this font
929 // Returns number of chars written
930 // This one corresponds to latexWriteStartChanges(). (Asger)
931 int LyXFont::latexWriteEndChanges(ostream & os, LyXFont const & base,
932 LyXFont const & next) const
937 LyXFont f = *this; // why do you need this?
938 f.reduce(base); // why isn't this just "reduce(base);" (Lgb)
939 // Because this function is const. Everything breaks if this
940 // method changes the font it represents. There is no speed penalty
941 // by using the temporary. (Asger)
944 if (f.family() != INHERIT_FAMILY) {
947 env = true; // Size change need not bother about closing env.
949 if (f.series() != INHERIT_SERIES) {
952 env = true; // Size change need not bother about closing env.
954 if (f.shape() != INHERIT_SHAPE) {
957 env = true; // Size change need not bother about closing env.
959 if (f.color() != LColor::inherit && f.color() != LColor::ignore) {
962 env = true; // Size change need not bother about closing env.
964 if (f.emph() == ON) {
967 env = true; // Size change need not bother about closing env.
969 if (f.underbar() == ON) {
972 env = true; // Size change need not bother about closing env.
974 if (f.noun() == ON) {
977 env = true; // Size change need not bother about closing env.
979 if (f.size() != INHERIT_SIZE) {
980 // We only have to close if only size changed
987 if (number() == ON && next.number() != ON &&
988 language()->lang() == "hebrew") {
993 if (language() != base.language() && language() != next.language()) {
1002 LColor::color LyXFont::realColor() const
1005 return LColor::latex;
1006 if (color() == LColor::none)
1007 return LColor::foreground;
1012 // Convert logical attributes to concrete shape attribute
1013 LyXFont::FONT_SHAPE LyXFont::realShape() const
1015 register FONT_SHAPE s = shape();
1024 s = SMALLCAPS_SHAPE;
1029 bool LyXFont::equalExceptLatex(LyXFont const & f) const
1032 f1.setLatex(f.latex());
1037 ostream & operator<<(ostream & o, LyXFont::FONT_MISC_STATE fms)
1039 return o << int(fms);