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
26 #include "FontLoader.h"
27 #include "support/lstrings.h"
28 #include "bufferparams.h" // stateText
33 #ifndef CXX_GLOBAL_CSTD
43 char const * GUIFamilyNames[12] =
44 { N_("Roman"), N_("Sans serif"), N_("Typewriter"), N_("Symbol"),
45 "cmr", "cmsy", "cmm", "cmex", "msa", "msb",
46 N_("Inherit"), N_("Ignore") };
48 char const * GUISeriesNames[4] =
49 { N_("Medium"), N_("Bold"), N_("Inherit"), N_("Ignore") };
51 char const * GUIShapeNames[6] =
52 { N_("Upright"), N_("Italic"), N_("Slanted"), N_("Smallcaps"), N_("Inherit"),
55 char const * GUISizeNames[14] =
56 { N_("Tiny"), N_("Smallest"), N_("Smaller"), N_("Small"), N_("Normal"), N_("Large"),
57 N_("Larger"), N_("Largest"), N_("Huge"), N_("Huger"), N_("Increase"), N_("Decrease"),
58 N_("Inherit"), N_("Ignore") };
60 char const * GUIMiscNames[5] =
61 { N_("Off"), N_("On"), N_("Toggle"), N_("Inherit"), N_("Ignore") };
65 // Strings used to read and write .lyx format files
67 char const * LyXFamilyNames[12] =
68 { "roman", "sans", "typewriter", "symbol",
69 "cmr", "cmsy", "cmm", "cmex", "msa", "msb",
72 char const * LyXSeriesNames[4] =
73 { "medium", "bold", "default", "error" };
75 char const * LyXShapeNames[6] =
76 { "up", "italic", "slanted", "smallcaps", "default", "error" };
78 char const * LyXSizeNames[14] =
79 { "tiny", "scriptsize", "footnotesize", "small", "normal", "large",
80 "larger", "largest", "huge", "giant",
81 "increase", "decrease", "default", "error" };
83 char const * LyXMiscNames[5] =
84 { "off", "on", "toggle", "default", "error" };
87 // Strings used to write LaTeX files
89 char const * LaTeXFamilyNames[6] =
90 { "textrm", "textsf", "texttt", "error1", "error2", "error3" };
92 char const * LaTeXSeriesNames[4] =
93 { "textmd", "textbf", "error4", "error5" };
95 char const * LaTeXShapeNames[6] =
96 { "textup", "textit", "textsl", "textsc", "error6", "error7" };
98 char const * LaTeXSizeNames[14] =
99 { "tiny", "scriptsize", "footnotesize", "small", "normalsize", "large",
100 "Large", "LARGE", "huge", "Huge", "error8", "error9", "error10", "error11" };
105 // Initialize static member
106 LyXFont::FontBits LyXFont::sane = {
117 // Initialize static member
118 LyXFont::FontBits LyXFont::inherit = {
129 // Initialize static member
130 LyXFont::FontBits LyXFont::ignore = {
142 bool LyXFont::FontBits::operator==(LyXFont::FontBits const & fb1) const
144 return fb1.family == family &&
145 fb1.series == series &&
146 fb1.shape == shape &&
148 fb1.color == color &&
150 fb1.underbar == underbar &&
152 fb1.number == number;
156 bool LyXFont::FontBits::operator!=(LyXFont::FontBits const & fb1) const
158 return !(fb1 == *this);
163 : bits(sane), lang(default_language)
167 LyXFont::LyXFont(LyXFont::FONT_INIT1)
168 #ifndef INHERIT_LANGUAGE
169 : bits(inherit), lang(default_language)
171 : bits(inherit), lang(inherit_language)
176 LyXFont::LyXFont(LyXFont::FONT_INIT2)
177 : bits(ignore), lang(ignore_language)
181 LyXFont::LyXFont(LyXFont::FONT_INIT3)
182 : bits(sane), lang(default_language)
186 LyXFont::LyXFont(LyXFont::FONT_INIT1, Language const * l)
187 : bits(inherit), lang(l)
191 LyXFont::LyXFont(LyXFont::FONT_INIT2, Language const * l)
192 : bits(ignore), lang(l)
196 LyXFont::LyXFont(LyXFont::FONT_INIT3, Language const * l)
197 : bits(sane), lang(l)
201 LyXFont::FONT_MISC_STATE LyXFont::underbar() const
203 return bits.underbar;
207 LColor::color LyXFont::color() const
213 Language const * LyXFont::language() const
219 LyXFont::FONT_MISC_STATE LyXFont::number() const
225 bool LyXFont::isRightToLeft() const
227 return lang->RightToLeft();
231 bool LyXFont::isVisibleRightToLeft() const
233 return (lang->RightToLeft() &&
238 LyXFont & LyXFont::setFamily(LyXFont::FONT_FAMILY f)
245 LyXFont & LyXFont::setSeries(LyXFont::FONT_SERIES s)
252 LyXFont & LyXFont::setShape(LyXFont::FONT_SHAPE s)
259 LyXFont & LyXFont::setSize(LyXFont::FONT_SIZE s)
266 LyXFont & LyXFont::setEmph(LyXFont::FONT_MISC_STATE e)
273 LyXFont & LyXFont::setUnderbar(LyXFont::FONT_MISC_STATE u)
280 LyXFont & LyXFont::setNoun(LyXFont::FONT_MISC_STATE n)
287 LyXFont & LyXFont::setColor(LColor::color c)
294 LyXFont & LyXFont::setLanguage(Language const * l)
301 LyXFont & LyXFont::setNumber(LyXFont::FONT_MISC_STATE n)
308 /// Decreases font size by one
309 LyXFont & LyXFont::decSize()
312 case SIZE_HUGER: setSize(SIZE_HUGE); break;
313 case SIZE_HUGE: setSize(SIZE_LARGEST); break;
314 case SIZE_LARGEST: setSize(SIZE_LARGER); break;
315 case SIZE_LARGER: setSize(SIZE_LARGE); break;
316 case SIZE_LARGE: setSize(SIZE_NORMAL); break;
317 case SIZE_NORMAL: setSize(SIZE_SMALL); break;
318 case SIZE_SMALL: setSize(SIZE_FOOTNOTE); break;
319 case SIZE_FOOTNOTE: setSize(SIZE_SCRIPT); break;
320 case SIZE_SCRIPT: setSize(SIZE_TINY); break;
321 case SIZE_TINY: break;
323 lyxerr << "Can't LyXFont::decSize on INCREASE_SIZE" << endl;
326 lyxerr <<"Can't LyXFont::decSize on DECREASE_SIZE" << endl;
329 lyxerr <<"Can't LyXFont::decSize on INHERIT_SIZE" << endl;
332 lyxerr <<"Can't LyXFont::decSize on IGNORE_SIZE" << endl;
339 /// Increases font size by one
340 LyXFont & LyXFont::incSize()
343 case SIZE_HUGER: break;
344 case SIZE_HUGE: setSize(SIZE_HUGER); break;
345 case SIZE_LARGEST: setSize(SIZE_HUGE); break;
346 case SIZE_LARGER: setSize(SIZE_LARGEST); break;
347 case SIZE_LARGE: setSize(SIZE_LARGER); break;
348 case SIZE_NORMAL: setSize(SIZE_LARGE); break;
349 case SIZE_SMALL: setSize(SIZE_NORMAL); break;
350 case SIZE_FOOTNOTE: setSize(SIZE_SMALL); break;
351 case SIZE_SCRIPT: setSize(SIZE_FOOTNOTE); break;
352 case SIZE_TINY: setSize(SIZE_SCRIPT); break;
354 lyxerr <<"Can't LyXFont::incSize on INCREASE_SIZE" << endl;
357 lyxerr <<"Can't LyXFont::incSize on DECREASE_SIZE" << endl;
360 lyxerr <<"Can't LyXFont::incSize on INHERIT_SIZE" << endl;
363 lyxerr <<"Can't LyXFont::incSize on IGNORE_SIZE" << endl;
370 /// Updates a misc setting according to request
371 LyXFont::FONT_MISC_STATE LyXFont::setMisc(FONT_MISC_STATE newfont,
374 if (newfont == TOGGLE) {
380 lyxerr <<"LyXFont::setMisc: Need state"
381 " ON or OFF to toggle. Setting to ON" << endl;
384 } else if (newfont == IGNORE)
391 /// Updates font settings according to request
392 void LyXFont::update(LyXFont const & newfont,
393 Language const * document_language,
396 if (newfont.family() == family() && toggleall)
397 setFamily(INHERIT_FAMILY); // toggle 'back'
398 else if (newfont.family() != IGNORE_FAMILY)
399 setFamily(newfont.family());
400 // else it's IGNORE_SHAPE
402 // "Old" behaviour: "Setting" bold will toggle bold on/off.
403 switch (newfont.series()) {
406 if (series() == BOLD_SERIES && toggleall)
407 setSeries(MEDIUM_SERIES);
409 setSeries(BOLD_SERIES);
413 setSeries(newfont.series());
419 if (newfont.shape() == shape() && toggleall)
420 setShape(INHERIT_SHAPE); // toggle 'back'
421 else if (newfont.shape() != IGNORE_SHAPE)
422 setShape(newfont.shape());
423 // else it's IGNORE_SHAPE
425 if (newfont.size() != IGNORE_SIZE) {
426 if (newfont.size() == INCREASE_SIZE)
428 else if (newfont.size() == DECREASE_SIZE)
431 setSize(newfont.size());
434 setEmph(setMisc(newfont.emph(), emph()));
435 setUnderbar(setMisc(newfont.underbar(), underbar()));
436 setNoun(setMisc(newfont.noun(), noun()));
438 setNumber(setMisc(newfont.number(), number()));
439 if (newfont.language() == language() && toggleall)
440 if (language() == document_language)
441 setLanguage(default_language);
443 #ifndef INHERIT_LANGUAGE
444 setLanguage(document_language);
446 setLanguage(inherit_language);
448 else if (newfont.language() != ignore_language)
449 setLanguage(newfont.language());
451 if (newfont.color() == color() && toggleall)
452 setColor(LColor::inherit); // toggle 'back'
453 else if (newfont.color() != LColor::ignore)
454 setColor(newfont.color());
458 /// Reduce font to fall back to template where possible
459 void LyXFont::reduce(LyXFont const & tmplt)
461 if (family() == tmplt.family())
462 setFamily(INHERIT_FAMILY);
463 if (series() == tmplt.series())
464 setSeries(INHERIT_SERIES);
465 if (shape() == tmplt.shape())
466 setShape(INHERIT_SHAPE);
467 if (size() == tmplt.size())
468 setSize(INHERIT_SIZE);
469 if (emph() == tmplt.emph())
471 if (underbar() == tmplt.underbar())
472 setUnderbar(INHERIT);
473 if (noun() == tmplt.noun())
475 if (color() == tmplt.color())
476 setColor(LColor::inherit);
477 #ifdef INHERIT_LANGUAGE
478 if (language() == tmplt.language())
479 setLanguage(inherit_language);
484 /// Realize font from a template
485 #ifndef INHERIT_LANGUAGE
486 LyXFont & LyXFont::realize(LyXFont const & tmplt)
488 LyXFont & LyXFont::realize(LyXFont const & tmplt, Language const * deflang)
491 #ifdef INHERIT_LANGUAGE
492 if (language() == inherit_language) {
493 if (tmplt.language() == inherit_language ||
494 tmplt.language() == ignore_language ||
495 tmplt.language() == default_language)
497 setLanguage(deflang);
499 setLanguage(tmplt.language());
503 if (bits == inherit) {
508 if (bits.family == INHERIT_FAMILY) {
509 bits.family = tmplt.bits.family;
511 if (bits.series == INHERIT_SERIES) {
512 bits.series = tmplt.bits.series;
514 if (bits.shape == INHERIT_SHAPE) {
515 bits.shape = tmplt.bits.shape;
517 if (bits.size == INHERIT_SIZE) {
518 bits.size = tmplt.bits.size;
520 if (bits.emph == INHERIT) {
521 bits.emph = tmplt.bits.emph;
523 if (bits.underbar == INHERIT) {
524 bits.underbar = tmplt.bits.underbar;
526 if (bits.noun == INHERIT) {
527 bits.noun = tmplt.bits.noun;
529 if (bits.color == LColor::inherit) {
530 bits.color = tmplt.bits.color;
536 /// Is font resolved?
537 bool LyXFont::resolved() const
539 return (family() != INHERIT_FAMILY && series() != INHERIT_SERIES &&
540 shape() != INHERIT_SHAPE && size() != INHERIT_SIZE &&
541 emph() != INHERIT && underbar() != INHERIT &&
543 #ifdef INHERIT_LANGUAGE
544 language() != inherit_language &&
546 color() != LColor::inherit);
550 /// Build GUI description of font state
551 string const LyXFont::stateText(BufferParams * params) const
554 if (family() != INHERIT_FAMILY)
555 ost << _(GUIFamilyNames[family()]) << ", ";
556 if (series() != INHERIT_SERIES)
557 ost << _(GUISeriesNames[series()]) << ", ";
558 if (shape() != INHERIT_SHAPE)
559 ost << _(GUIShapeNames[shape()]) << ", ";
560 if (size() != INHERIT_SIZE)
561 ost << _(GUISizeNames[size()]) << ", ";
562 if (color() != LColor::inherit)
563 ost << lcolor.getGUIName(color()) << ", ";
564 if (emph() != INHERIT)
565 ost << _("Emphasis ")
566 << _(GUIMiscNames[emph()]) << ", ";
567 if (underbar() != INHERIT)
568 ost << _("Underline ")
569 << _(GUIMiscNames[underbar()]) << ", ";
570 if (noun() != INHERIT)
571 ost << _("Noun ") << _(GUIMiscNames[noun()]) << ", ";
573 ost << _("Default") << ", ";
574 if (!params || (language() != params->language))
575 ost << _("Language: ") << _(language()->display()) << ", ";
577 ost << _(" Number ") << _(GUIMiscNames[number()]);
579 string buf(ost.str().c_str());
580 buf = strip(buf, ' ');
581 buf = strip(buf, ',');
586 // Set family according to lyx format string
587 LyXFont & LyXFont::setLyXFamily(string const & fam)
589 string const s = lowercase(fam);
592 while (s != LyXFamilyNames[i] && LyXFamilyNames[i] != "error") ++i;
593 if (s == LyXFamilyNames[i]) {
594 setFamily(LyXFont::FONT_FAMILY(i));
596 lyxerr << "LyXFont::setLyXFamily: Unknown family `"
597 << s << '\'' << endl;
602 // Set series according to lyx format string
603 LyXFont & LyXFont::setLyXSeries(string const & ser)
605 string const s = lowercase(ser);
608 while (s != LyXSeriesNames[i] && LyXSeriesNames[i] != "error") ++i;
609 if (s == LyXSeriesNames[i]) {
610 setSeries(LyXFont::FONT_SERIES(i));
612 lyxerr << "LyXFont::setLyXSeries: Unknown series `"
613 << s << '\'' << endl;
618 // Set shape according to lyx format string
619 LyXFont & LyXFont::setLyXShape(string const & sha)
621 string const s = lowercase(sha);
624 while (s != LyXShapeNames[i] && LyXShapeNames[i] != "error") ++i;
625 if (s == LyXShapeNames[i]) {
626 setShape(LyXFont::FONT_SHAPE(i));
628 lyxerr << "LyXFont::setLyXShape: Unknown shape `"
629 << s << '\'' << endl;
634 // Set size according to lyx format string
635 LyXFont & LyXFont::setLyXSize(string const & siz)
637 string const s = lowercase(siz);
639 while (s != LyXSizeNames[i] && LyXSizeNames[i] != "error") ++i;
640 if (s == LyXSizeNames[i]) {
641 setSize(LyXFont::FONT_SIZE(i));
643 lyxerr << "LyXFont::setLyXSize: Unknown size `"
644 << s << '\'' << endl;
649 // Set size according to lyx format string
650 LyXFont::FONT_MISC_STATE LyXFont::setLyXMisc(string const & siz)
652 string const s = lowercase(siz);
654 while (s != LyXMiscNames[i] && LyXMiscNames[i] != "error") ++i;
655 if (s == LyXMiscNames[i])
656 return FONT_MISC_STATE(i);
657 lyxerr << "LyXFont::setLyXMisc: Unknown misc flag `"
658 << s << '\'' << endl;
663 /// Sets color after LyX text format
664 LyXFont & LyXFont::setLyXColor(string const & col)
666 setColor(lcolor.getFromLyXName(col));
671 // Returns size in latex format
672 string const LyXFont::latexSize() const
674 return LaTeXSizeNames[size()];
678 // Read a font definition from given file in lyx format
680 LyXFont & LyXFont::lyxRead(LyXLex & lex)
683 bool finished = false;
684 while (!finished && lex.isOK() && !error) {
686 string const tok = lowercase(lex.getString());
690 } else if (tok == "endfont") {
692 } else if (tok == "family") {
694 string const ttok = lex.getString();
696 } else if (tok == "series") {
698 string const ttok = lex.getString();
700 } else if (tok == "shape") {
702 string const ttok = lex.getString();
704 } else if (tok == "size") {
706 string const ttok = lex.getString();
708 } else if (tok == "misc") {
710 string const ttok = lowercase(lex.getString());
712 if (ttok == "no_bar") {
714 } else if (ttok == "no_emph") {
716 } else if (ttok == "no_noun") {
718 } else if (ttok == "emph") {
720 } else if (ttok == "underbar") {
722 } else if (ttok == "noun") {
725 lex.printError("Illegal misc type `$$Token´");
727 } else if (tok == "color") {
729 string const ttok = lex.getString();
732 lex.printError("Unknown tag `$$Token'");
740 /// Writes the changes from this font to orgfont in .lyx format in file
741 void LyXFont::lyxWriteChanges(LyXFont const & orgfont,
742 #ifdef INHERIT_LANGUAGE
743 Language const * doclang,
748 if (orgfont.family() != family()) {
749 os << "\\family " << LyXFamilyNames[family()] << " \n";
751 if (orgfont.series() != series()) {
752 os << "\\series " << LyXSeriesNames[series()] << " \n";
754 if (orgfont.shape() != shape()) {
755 os << "\\shape " << LyXShapeNames[shape()] << " \n";
757 if (orgfont.size() != size()) {
758 os << "\\size " << LyXSizeNames[size()] << " \n";
760 if (orgfont.emph() != emph()) {
761 os << "\\emph " << LyXMiscNames[emph()] << " \n";
763 if (orgfont.number() != number()) {
764 os << "\\numeric " << LyXMiscNames[number()] << " \n";
766 if (orgfont.underbar() != underbar()) {
767 // This is only for backwards compatibility
768 switch (underbar()) {
769 case OFF: os << "\\bar no \n"; break;
770 case ON: os << "\\bar under \n"; break;
771 case TOGGLE: lyxerr << "LyXFont::lyxWriteFontChanges: "
772 "TOGGLE should not appear here!"
775 case INHERIT: os << "\\bar default \n"; break;
776 case IGNORE: lyxerr << "LyXFont::lyxWriteFontChanges: "
777 "IGNORE should not appear here!"
782 if (orgfont.noun() != noun()) {
783 os << "\\noun " << LyXMiscNames[noun()] << " \n";
785 if (orgfont.color() != color()) {
786 // To make us file compatible with older
787 // lyx versions we emit "default" instead
789 string col_str(lcolor.getLyXName(color()));
790 if (col_str == "inherit") col_str = "default";
791 os << "\\color " << col_str << "\n";
793 if (orgfont.language() != language()) {
794 #ifndef INHERIT_LANGUAGE
797 if (language() == inherit_language)
798 os << "\\lang " << doclang->lang() << "\n";
801 os << "\\lang " << language()->lang() << "\n";
803 os << "\\lang unknown\n";
808 /// Writes the head of the LaTeX needed to impose this font
809 // Returns number of chars written.
810 int LyXFont::latexWriteStartChanges(ostream & os, LyXFont const & base,
811 LyXFont const & prev) const
816 if (language()->babel() != base.language()->babel() &&
817 language()->babel() != prev.language()->babel()) {
818 if (isRightToLeft() != prev.isRightToLeft()) {
819 if (isRightToLeft()) {
828 subst(lyxrc.language_command_local,
829 "$$lang", language()->babel());
831 count += tmp.length();
835 if (number() == ON && prev.number() != ON &&
836 language()->lang() == "hebrew") {
844 if (f.family() != INHERIT_FAMILY) {
846 << LaTeXFamilyNames[f.family()]
848 count += strlen(LaTeXFamilyNames[f.family()]) + 2;
849 env = true; //We have opened a new environment
851 if (f.series() != INHERIT_SERIES) {
853 << LaTeXSeriesNames[f.series()]
855 count += strlen(LaTeXSeriesNames[f.series()]) + 2;
856 env = true; //We have opened a new environment
858 if (f.shape() != INHERIT_SHAPE) {
860 << LaTeXShapeNames[f.shape()]
862 count += strlen(LaTeXShapeNames[f.shape()]) + 2;
863 env = true; //We have opened a new environment
865 if (f.color() != LColor::inherit && f.color() != LColor::ignore) {
867 << lcolor.getLaTeXName(f.color())
869 count += lcolor.getLaTeXName(f.color()).length() + 13;
870 env = true; //We have opened a new environment
872 if (f.emph() == ON) {
875 env = true; //We have opened a new environment
877 if (f.underbar() == ON) {
880 env = true; //We have opened a new environment
882 // \noun{} is a LyX special macro
883 if (f.noun() == ON) {
886 env = true; //We have opened a new environment
888 if (f.size() != INHERIT_SIZE) {
889 // If we didn't open an environment above, we open one here
895 << LaTeXSizeNames[f.size()]
897 count += strlen(LaTeXSizeNames[f.size()]) + 2;
903 /// Writes ending block of LaTeX needed to close use of this font
904 // Returns number of chars written
905 // This one corresponds to latexWriteStartChanges(). (Asger)
906 int LyXFont::latexWriteEndChanges(ostream & os, LyXFont const & base,
907 LyXFont const & next) const
912 LyXFont f = *this; // why do you need this?
913 f.reduce(base); // why isn't this just "reduce(base);" (Lgb)
914 // Because this function is const. Everything breaks if this
915 // method changes the font it represents. There is no speed penalty
916 // by using the temporary. (Asger)
919 if (f.family() != INHERIT_FAMILY) {
922 env = true; // Size change need not bother about closing env.
924 if (f.series() != INHERIT_SERIES) {
927 env = true; // Size change need not bother about closing env.
929 if (f.shape() != INHERIT_SHAPE) {
932 env = true; // Size change need not bother about closing env.
934 if (f.color() != LColor::inherit && f.color() != LColor::ignore) {
937 env = true; // Size change need not bother about closing env.
939 if (f.emph() == ON) {
942 env = true; // Size change need not bother about closing env.
944 if (f.underbar() == ON) {
947 env = true; // Size change need not bother about closing env.
949 if (f.noun() == ON) {
952 env = true; // Size change need not bother about closing env.
954 if (f.size() != INHERIT_SIZE) {
955 // We only have to close if only size changed
962 if (number() == ON && next.number() != ON &&
963 language()->lang() == "hebrew") {
968 if (language() != base.language() && language() != next.language()) {
977 LColor::color LyXFont::realColor() const
979 if (color() == LColor::none)
980 return LColor::foreground;
985 // Convert logical attributes to concrete shape attribute
986 LyXFont::FONT_SHAPE LyXFont::realShape() const
988 register FONT_SHAPE s = shape();
1002 ostream & operator<<(ostream & o, LyXFont::FONT_MISC_STATE fms)
1004 return o << int(fms);