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
32 #ifndef CXX_GLOBAL_CSTD
42 char const * GUIFamilyNames[12] =
43 { N_("Roman"), N_("Sans serif"), N_("Typewriter"), N_("Symbol"),
44 "cmr", "cmsy", "cmm", "cmex", "msa", "msb",
45 N_("Inherit"), N_("Ignore") };
47 char const * GUISeriesNames[4] =
48 { N_("Medium"), N_("Bold"), N_("Inherit"), N_("Ignore") };
50 char const * GUIShapeNames[6] =
51 { N_("Upright"), N_("Italic"), N_("Slanted"), N_("Smallcaps"), N_("Inherit"),
54 char const * GUISizeNames[14] =
55 { N_("Tiny"), N_("Smallest"), N_("Smaller"), N_("Small"), N_("Normal"), N_("Large"),
56 N_("Larger"), N_("Largest"), N_("Huge"), N_("Huger"), N_("Increase"), N_("Decrease"),
57 N_("Inherit"), N_("Ignore") };
59 char const * GUIMiscNames[5] =
60 { N_("Off"), N_("On"), N_("Toggle"), N_("Inherit"), N_("Ignore") };
64 // Strings used to read and write .lyx format files
66 char const * LyXFamilyNames[12] =
67 { "roman", "sans", "typewriter", "symbol",
68 "cmr", "cmsy", "cmm", "cmex", "msa", "msb",
71 char const * LyXSeriesNames[4] =
72 { "medium", "bold", "default", "error" };
74 char const * LyXShapeNames[6] =
75 { "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", "decrease", "default", "error" };
82 char const * LyXMiscNames[5] =
83 { "off", "on", "toggle", "default", "error" };
86 // Strings used to write LaTeX files
88 char const * LaTeXFamilyNames[6] =
89 { "textrm", "textsf", "texttt", "error1", "error2", "error3" };
91 char const * LaTeXSeriesNames[4] =
92 { "textmd", "textbf", "error4", "error5" };
94 char const * LaTeXShapeNames[6] =
95 { "textup", "textit", "textsl", "textsc", "error6", "error7" };
97 char const * LaTeXSizeNames[14] =
98 { "tiny", "scriptsize", "footnotesize", "small", "normalsize", "large",
99 "Large", "LARGE", "huge", "Huge", "error8", "error9", "error10", "error11" };
104 // Initialize static member
105 LyXFont::FontBits LyXFont::sane = {
116 // Initialize static member
117 LyXFont::FontBits LyXFont::inherit = {
128 // Initialize static member
129 LyXFont::FontBits LyXFont::ignore = {
141 bool LyXFont::FontBits::operator==(LyXFont::FontBits const & fb1) const
143 return fb1.family == family &&
144 fb1.series == series &&
145 fb1.shape == shape &&
147 fb1.color == color &&
149 fb1.underbar == underbar &&
151 fb1.number == number;
155 bool LyXFont::FontBits::operator!=(LyXFont::FontBits const & fb1) const
157 return !(fb1 == *this);
161 LyXFont::LyXFont(LyXFont::FONT_INIT1)
162 #ifndef INHERIT_LANGUAGE
163 : bits(inherit), lang(default_language)
165 : bits(inherit), lang(inherit_language)
170 LyXFont::LyXFont(LyXFont::FONT_INIT2)
171 : bits(ignore), lang(ignore_language)
175 LyXFont::LyXFont(LyXFont::FONT_INIT3)
176 : bits(sane), lang(default_language)
180 LyXFont::LyXFont(LyXFont::FONT_INIT1, Language const * l)
181 : bits(inherit), lang(l)
185 LyXFont::LyXFont(LyXFont::FONT_INIT2, Language const * l)
186 : bits(ignore), lang(l)
190 LyXFont::LyXFont(LyXFont::FONT_INIT3, Language const * l)
191 : bits(sane), lang(l)
195 LyXFont::FONT_MISC_STATE LyXFont::underbar() const
197 return bits.underbar;
201 LColor::color LyXFont::color() const
207 Language const * LyXFont::language() const
213 LyXFont::FONT_MISC_STATE LyXFont::number() const
219 bool LyXFont::isRightToLeft() const
221 return lang->RightToLeft();
225 bool LyXFont::isVisibleRightToLeft() const
227 return (lang->RightToLeft() &&
232 LyXFont & LyXFont::setFamily(LyXFont::FONT_FAMILY f)
239 LyXFont & LyXFont::setSeries(LyXFont::FONT_SERIES s)
246 LyXFont & LyXFont::setShape(LyXFont::FONT_SHAPE s)
253 LyXFont & LyXFont::setSize(LyXFont::FONT_SIZE s)
260 LyXFont & LyXFont::setEmph(LyXFont::FONT_MISC_STATE e)
267 LyXFont & LyXFont::setUnderbar(LyXFont::FONT_MISC_STATE u)
274 LyXFont & LyXFont::setNoun(LyXFont::FONT_MISC_STATE n)
281 LyXFont & LyXFont::setColor(LColor::color c)
288 LyXFont & LyXFont::setLanguage(Language const * l)
295 LyXFont & LyXFont::setNumber(LyXFont::FONT_MISC_STATE n)
302 /// Decreases font size by one
303 LyXFont & LyXFont::decSize()
306 case SIZE_HUGER: setSize(SIZE_HUGE); break;
307 case SIZE_HUGE: setSize(SIZE_LARGEST); break;
308 case SIZE_LARGEST: setSize(SIZE_LARGER); break;
309 case SIZE_LARGER: setSize(SIZE_LARGE); break;
310 case SIZE_LARGE: setSize(SIZE_NORMAL); break;
311 case SIZE_NORMAL: setSize(SIZE_SMALL); break;
312 case SIZE_SMALL: setSize(SIZE_FOOTNOTE); break;
313 case SIZE_FOOTNOTE: setSize(SIZE_SCRIPT); break;
314 case SIZE_SCRIPT: setSize(SIZE_TINY); break;
315 case SIZE_TINY: break;
317 lyxerr << "Can't LyXFont::decSize on INCREASE_SIZE" << endl;
320 lyxerr <<"Can't LyXFont::decSize on DECREASE_SIZE" << endl;
323 lyxerr <<"Can't LyXFont::decSize on INHERIT_SIZE" << endl;
326 lyxerr <<"Can't LyXFont::decSize on IGNORE_SIZE" << endl;
333 /// Increases font size by one
334 LyXFont & LyXFont::incSize()
337 case SIZE_HUGER: break;
338 case SIZE_HUGE: setSize(SIZE_HUGER); break;
339 case SIZE_LARGEST: setSize(SIZE_HUGE); break;
340 case SIZE_LARGER: setSize(SIZE_LARGEST); break;
341 case SIZE_LARGE: setSize(SIZE_LARGER); break;
342 case SIZE_NORMAL: setSize(SIZE_LARGE); break;
343 case SIZE_SMALL: setSize(SIZE_NORMAL); break;
344 case SIZE_FOOTNOTE: setSize(SIZE_SMALL); break;
345 case SIZE_SCRIPT: setSize(SIZE_FOOTNOTE); break;
346 case SIZE_TINY: setSize(SIZE_SCRIPT); break;
348 lyxerr <<"Can't LyXFont::incSize on INCREASE_SIZE" << endl;
351 lyxerr <<"Can't LyXFont::incSize on DECREASE_SIZE" << endl;
354 lyxerr <<"Can't LyXFont::incSize on INHERIT_SIZE" << endl;
357 lyxerr <<"Can't LyXFont::incSize on IGNORE_SIZE" << endl;
364 /// Updates a misc setting according to request
365 LyXFont::FONT_MISC_STATE LyXFont::setMisc(FONT_MISC_STATE newfont,
368 if (newfont == TOGGLE) {
374 lyxerr <<"LyXFont::setMisc: Need state"
375 " ON or OFF to toggle. Setting to ON" << endl;
378 } else if (newfont == IGNORE)
385 /// Updates font settings according to request
386 void LyXFont::update(LyXFont const & newfont,
387 Language const * document_language,
390 if (newfont.family() == family() && toggleall)
391 setFamily(INHERIT_FAMILY); // toggle 'back'
392 else if (newfont.family() != IGNORE_FAMILY)
393 setFamily(newfont.family());
394 // else it's IGNORE_SHAPE
396 // "Old" behaviour: "Setting" bold will toggle bold on/off.
397 switch (newfont.series()) {
400 if (series() == BOLD_SERIES && toggleall)
401 setSeries(MEDIUM_SERIES);
403 setSeries(BOLD_SERIES);
407 setSeries(newfont.series());
413 if (newfont.shape() == shape() && toggleall)
414 setShape(INHERIT_SHAPE); // toggle 'back'
415 else if (newfont.shape() != IGNORE_SHAPE)
416 setShape(newfont.shape());
417 // else it's IGNORE_SHAPE
419 if (newfont.size() != IGNORE_SIZE) {
420 if (newfont.size() == INCREASE_SIZE)
422 else if (newfont.size() == DECREASE_SIZE)
425 setSize(newfont.size());
428 setEmph(setMisc(newfont.emph(), emph()));
429 setUnderbar(setMisc(newfont.underbar(), underbar()));
430 setNoun(setMisc(newfont.noun(), noun()));
432 setNumber(setMisc(newfont.number(), number()));
433 if (newfont.language() == language() && toggleall)
434 if (language() == document_language)
435 setLanguage(default_language);
437 #ifndef INHERIT_LANGUAGE
438 setLanguage(document_language);
440 setLanguage(inherit_language);
442 else if (newfont.language() != ignore_language)
443 setLanguage(newfont.language());
445 if (newfont.color() == color() && toggleall)
446 setColor(LColor::inherit); // toggle 'back'
447 else if (newfont.color() != LColor::ignore)
448 setColor(newfont.color());
452 /// Reduce font to fall back to template where possible
453 void LyXFont::reduce(LyXFont const & tmplt)
455 if (family() == tmplt.family())
456 setFamily(INHERIT_FAMILY);
457 if (series() == tmplt.series())
458 setSeries(INHERIT_SERIES);
459 if (shape() == tmplt.shape())
460 setShape(INHERIT_SHAPE);
461 if (size() == tmplt.size())
462 setSize(INHERIT_SIZE);
463 if (emph() == tmplt.emph())
465 if (underbar() == tmplt.underbar())
466 setUnderbar(INHERIT);
467 if (noun() == tmplt.noun())
469 if (color() == tmplt.color())
470 setColor(LColor::inherit);
471 #ifdef INHERIT_LANGUAGE
472 if (language() == tmplt.language())
473 setLanguage(inherit_language);
478 /// Realize font from a template
479 #ifndef INHERIT_LANGUAGE
480 LyXFont & LyXFont::realize(LyXFont const & tmplt)
482 LyXFont & LyXFont::realize(LyXFont const & tmplt, Language const * deflang)
485 #ifdef INHERIT_LANGUAGE
486 if (language() == inherit_language) {
487 if (tmplt.language() == inherit_language ||
488 tmplt.language() == ignore_language ||
489 tmplt.language() == default_language)
491 setLanguage(deflang);
493 setLanguage(tmplt.language());
497 if (bits == inherit) {
502 if (bits.family == INHERIT_FAMILY) {
503 bits.family = tmplt.bits.family;
505 if (bits.series == INHERIT_SERIES) {
506 bits.series = tmplt.bits.series;
508 if (bits.shape == INHERIT_SHAPE) {
509 bits.shape = tmplt.bits.shape;
511 if (bits.size == INHERIT_SIZE) {
512 bits.size = tmplt.bits.size;
514 if (bits.emph == INHERIT) {
515 bits.emph = tmplt.bits.emph;
517 if (bits.underbar == INHERIT) {
518 bits.underbar = tmplt.bits.underbar;
520 if (bits.noun == INHERIT) {
521 bits.noun = tmplt.bits.noun;
523 if (bits.color == LColor::inherit) {
524 bits.color = tmplt.bits.color;
530 /// Is font resolved?
531 bool LyXFont::resolved() const
533 return (family() != INHERIT_FAMILY && series() != INHERIT_SERIES &&
534 shape() != INHERIT_SHAPE && size() != INHERIT_SIZE &&
535 emph() != INHERIT && underbar() != INHERIT &&
537 #ifdef INHERIT_LANGUAGE
538 language() != inherit_language &&
540 color() != LColor::inherit);
544 /// Build GUI description of font state
545 string const LyXFont::stateText(BufferParams * params) const
548 if (family() != INHERIT_FAMILY)
549 ost << _(GUIFamilyNames[family()]) << ", ";
550 if (series() != INHERIT_SERIES)
551 ost << _(GUISeriesNames[series()]) << ", ";
552 if (shape() != INHERIT_SHAPE)
553 ost << _(GUIShapeNames[shape()]) << ", ";
554 if (size() != INHERIT_SIZE)
555 ost << _(GUISizeNames[size()]) << ", ";
556 if (color() != LColor::inherit)
557 ost << lcolor.getGUIName(color()) << ", ";
558 if (emph() != INHERIT)
559 ost << _("Emphasis ")
560 << _(GUIMiscNames[emph()]) << ", ";
561 if (underbar() != INHERIT)
562 ost << _("Underline ")
563 << _(GUIMiscNames[underbar()]) << ", ";
564 if (noun() != INHERIT)
565 ost << _("Noun ") << _(GUIMiscNames[noun()]) << ", ";
567 ost << _("Default") << ", ";
568 if (!params || (language() != params->language))
569 ost << _("Language: ") << _(language()->display()) << ", ";
571 ost << _(" Number ") << _(GUIMiscNames[number()]);
573 string buf(ost.str().c_str());
574 buf = strip(buf, ' ');
575 buf = strip(buf, ',');
580 // Set family according to lyx format string
581 LyXFont & LyXFont::setLyXFamily(string const & fam)
583 string const s = lowercase(fam);
586 while (s != LyXFamilyNames[i] && LyXFamilyNames[i] != "error") ++i;
587 if (s == LyXFamilyNames[i]) {
588 setFamily(LyXFont::FONT_FAMILY(i));
590 lyxerr << "LyXFont::setLyXFamily: Unknown family `"
591 << s << '\'' << endl;
596 // Set series according to lyx format string
597 LyXFont & LyXFont::setLyXSeries(string const & ser)
599 string const s = lowercase(ser);
602 while (s != LyXSeriesNames[i] && LyXSeriesNames[i] != "error") ++i;
603 if (s == LyXSeriesNames[i]) {
604 setSeries(LyXFont::FONT_SERIES(i));
606 lyxerr << "LyXFont::setLyXSeries: Unknown series `"
607 << s << '\'' << endl;
612 // Set shape according to lyx format string
613 LyXFont & LyXFont::setLyXShape(string const & sha)
615 string const s = lowercase(sha);
618 while (s != LyXShapeNames[i] && LyXShapeNames[i] != "error") ++i;
619 if (s == LyXShapeNames[i]) {
620 setShape(LyXFont::FONT_SHAPE(i));
622 lyxerr << "LyXFont::setLyXShape: Unknown shape `"
623 << s << '\'' << endl;
628 // Set size according to lyx format string
629 LyXFont & LyXFont::setLyXSize(string const & siz)
631 string const s = lowercase(siz);
633 while (s != LyXSizeNames[i] && LyXSizeNames[i] != "error") ++i;
634 if (s == LyXSizeNames[i]) {
635 setSize(LyXFont::FONT_SIZE(i));
637 lyxerr << "LyXFont::setLyXSize: Unknown size `"
638 << s << '\'' << endl;
643 // Set size according to lyx format string
644 LyXFont::FONT_MISC_STATE LyXFont::setLyXMisc(string const & siz)
646 string const s = lowercase(siz);
648 while (s != LyXMiscNames[i] && LyXMiscNames[i] != "error") ++i;
649 if (s == LyXMiscNames[i])
650 return FONT_MISC_STATE(i);
651 lyxerr << "LyXFont::setLyXMisc: Unknown misc flag `"
652 << s << '\'' << endl;
657 /// Sets color after LyX text format
658 LyXFont & LyXFont::setLyXColor(string const & col)
660 setColor(lcolor.getFromLyXName(col));
665 // Returns size in latex format
666 string const LyXFont::latexSize() const
668 return LaTeXSizeNames[size()];
672 // Read a font definition from given file in lyx format
674 LyXFont & LyXFont::lyxRead(LyXLex & lex)
677 bool finished = false;
678 while (!finished && lex.isOK() && !error) {
680 string const tok = lowercase(lex.getString());
684 } else if (tok == "endfont") {
686 } else if (tok == "family") {
688 string const ttok = lex.getString();
690 } else if (tok == "series") {
692 string const ttok = lex.getString();
694 } else if (tok == "shape") {
696 string const ttok = lex.getString();
698 } else if (tok == "size") {
700 string const ttok = lex.getString();
702 } else if (tok == "misc") {
704 string const ttok = lowercase(lex.getString());
706 if (ttok == "no_bar") {
708 } else if (ttok == "no_emph") {
710 } else if (ttok == "no_noun") {
712 } else if (ttok == "emph") {
714 } else if (ttok == "underbar") {
716 } else if (ttok == "noun") {
719 lex.printError("Illegal misc type `$$Token´");
721 } else if (tok == "color") {
723 string const ttok = lex.getString();
726 lex.printError("Unknown tag `$$Token'");
734 /// Writes the changes from this font to orgfont in .lyx format in file
735 void LyXFont::lyxWriteChanges(LyXFont const & orgfont,
736 #ifdef INHERIT_LANGUAGE
737 Language const * doclang,
742 if (orgfont.family() != family()) {
743 os << "\\family " << LyXFamilyNames[family()] << " \n";
745 if (orgfont.series() != series()) {
746 os << "\\series " << LyXSeriesNames[series()] << " \n";
748 if (orgfont.shape() != shape()) {
749 os << "\\shape " << LyXShapeNames[shape()] << " \n";
751 if (orgfont.size() != size()) {
752 os << "\\size " << LyXSizeNames[size()] << " \n";
754 if (orgfont.emph() != emph()) {
755 os << "\\emph " << LyXMiscNames[emph()] << " \n";
757 if (orgfont.number() != number()) {
758 os << "\\numeric " << LyXMiscNames[number()] << " \n";
760 if (orgfont.underbar() != underbar()) {
761 // This is only for backwards compatibility
762 switch (underbar()) {
763 case OFF: os << "\\bar no \n"; break;
764 case ON: os << "\\bar under \n"; break;
765 case TOGGLE: lyxerr << "LyXFont::lyxWriteFontChanges: "
766 "TOGGLE should not appear here!"
769 case INHERIT: os << "\\bar default \n"; break;
770 case IGNORE: lyxerr << "LyXFont::lyxWriteFontChanges: "
771 "IGNORE should not appear here!"
776 if (orgfont.noun() != noun()) {
777 os << "\\noun " << LyXMiscNames[noun()] << " \n";
779 if (orgfont.color() != color()) {
780 // To make us file compatible with older
781 // lyx versions we emit "default" instead
783 string col_str(lcolor.getLyXName(color()));
784 if (col_str == "inherit") col_str = "default";
785 os << "\\color " << col_str << "\n";
787 if (orgfont.language() != language()) {
788 #ifndef INHERIT_LANGUAGE
791 if (language() == inherit_language)
792 os << "\\lang " << doclang->lang() << "\n";
795 os << "\\lang " << language()->lang() << "\n";
797 os << "\\lang unknown\n";
802 /// Writes the head of the LaTeX needed to impose this font
803 // Returns number of chars written.
804 int LyXFont::latexWriteStartChanges(ostream & os, LyXFont const & base,
805 LyXFont const & prev) const
810 if (language()->babel() != base.language()->babel() &&
811 language()->babel() != prev.language()->babel()) {
812 if (isRightToLeft() != prev.isRightToLeft()) {
813 if (isRightToLeft()) {
822 subst(lyxrc.language_command_local,
823 "$$lang", language()->babel());
825 count += tmp.length();
829 if (number() == ON && prev.number() != ON &&
830 language()->lang() == "hebrew") {
838 if (f.family() != INHERIT_FAMILY) {
840 << LaTeXFamilyNames[f.family()]
842 count += strlen(LaTeXFamilyNames[f.family()]) + 2;
843 env = true; //We have opened a new environment
845 if (f.series() != INHERIT_SERIES) {
847 << LaTeXSeriesNames[f.series()]
849 count += strlen(LaTeXSeriesNames[f.series()]) + 2;
850 env = true; //We have opened a new environment
852 if (f.shape() != INHERIT_SHAPE) {
854 << LaTeXShapeNames[f.shape()]
856 count += strlen(LaTeXShapeNames[f.shape()]) + 2;
857 env = true; //We have opened a new environment
859 if (f.color() != LColor::inherit && f.color() != LColor::ignore) {
861 << lcolor.getLaTeXName(f.color())
863 count += lcolor.getLaTeXName(f.color()).length() + 13;
864 env = true; //We have opened a new environment
866 if (f.emph() == ON) {
869 env = true; //We have opened a new environment
871 if (f.underbar() == ON) {
874 env = true; //We have opened a new environment
876 // \noun{} is a LyX special macro
877 if (f.noun() == ON) {
880 env = true; //We have opened a new environment
882 if (f.size() != INHERIT_SIZE) {
883 // If we didn't open an environment above, we open one here
889 << LaTeXSizeNames[f.size()]
891 count += strlen(LaTeXSizeNames[f.size()]) + 2;
897 /// Writes ending block of LaTeX needed to close use of this font
898 // Returns number of chars written
899 // This one corresponds to latexWriteStartChanges(). (Asger)
900 int LyXFont::latexWriteEndChanges(ostream & os, LyXFont const & base,
901 LyXFont const & next) const
906 LyXFont f = *this; // why do you need this?
907 f.reduce(base); // why isn't this just "reduce(base);" (Lgb)
908 // Because this function is const. Everything breaks if this
909 // method changes the font it represents. There is no speed penalty
910 // by using the temporary. (Asger)
913 if (f.family() != INHERIT_FAMILY) {
916 env = true; // Size change need not bother about closing env.
918 if (f.series() != INHERIT_SERIES) {
921 env = true; // Size change need not bother about closing env.
923 if (f.shape() != INHERIT_SHAPE) {
926 env = true; // Size change need not bother about closing env.
928 if (f.color() != LColor::inherit && f.color() != LColor::ignore) {
931 env = true; // Size change need not bother about closing env.
933 if (f.emph() == ON) {
936 env = true; // Size change need not bother about closing env.
938 if (f.underbar() == ON) {
941 env = true; // Size change need not bother about closing env.
943 if (f.noun() == ON) {
946 env = true; // Size change need not bother about closing env.
948 if (f.size() != INHERIT_SIZE) {
949 // We only have to close if only size changed
956 if (number() == ON && next.number() != ON &&
957 language()->lang() == "hebrew") {
962 if (language() != base.language() && language() != next.language()) {
971 LColor::color LyXFont::realColor() const
973 if (color() == LColor::none)
974 return LColor::foreground;
979 // Convert logical attributes to concrete shape attribute
980 LyXFont::FONT_SHAPE LyXFont::realShape() const
982 register FONT_SHAPE s = shape();
996 ostream & operator<<(ostream & o, LyXFont::FONT_MISC_STATE fms)
998 return o << int(fms);