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 "support/lstrings.h"
27 #include "bufferparams.h" // stateText
32 #ifndef CXX_GLOBAL_CSTD
42 char const * GUIFamilyNames[LyXFont::NUM_FAMILIES + 2 /* default & error */] =
43 { N_("Roman"), N_("Sans serif"), N_("Typewriter"), N_("Symbol"),
44 "cmr", "cmsy", "cmm", "cmex", "msa", "msb", "eufrak",
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[LyXFont::NUM_FAMILIES + 2 /* default & error */] =
67 { "roman", "sans", "typewriter", "symbol",
68 "cmr", "cmsy", "cmm", "cmex", "msa", "msb", "eufrak",
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);
162 : bits(sane), lang(default_language)
166 LyXFont::LyXFont(LyXFont::FONT_INIT1)
167 #ifndef INHERIT_LANGUAGE
168 : bits(inherit), lang(default_language)
170 : bits(inherit), lang(inherit_language)
175 LyXFont::LyXFont(LyXFont::FONT_INIT2)
176 : bits(ignore), lang(ignore_language)
180 LyXFont::LyXFont(LyXFont::FONT_INIT3)
181 : bits(sane), lang(default_language)
185 LyXFont::LyXFont(LyXFont::FONT_INIT1, Language const * l)
186 : bits(inherit), lang(l)
190 LyXFont::LyXFont(LyXFont::FONT_INIT2, Language const * l)
191 : bits(ignore), lang(l)
195 LyXFont::LyXFont(LyXFont::FONT_INIT3, Language const * l)
196 : bits(sane), lang(l)
200 LyXFont::FONT_MISC_STATE LyXFont::underbar() const
202 return bits.underbar;
206 LColor::color LyXFont::color() const
212 Language const * LyXFont::language() const
218 LyXFont::FONT_MISC_STATE LyXFont::number() const
224 bool LyXFont::isRightToLeft() const
226 return lang->RightToLeft();
230 bool LyXFont::isVisibleRightToLeft() const
232 return (lang->RightToLeft() &&
237 LyXFont & LyXFont::setFamily(LyXFont::FONT_FAMILY f)
244 LyXFont & LyXFont::setSeries(LyXFont::FONT_SERIES s)
251 LyXFont & LyXFont::setShape(LyXFont::FONT_SHAPE s)
258 LyXFont & LyXFont::setSize(LyXFont::FONT_SIZE s)
265 LyXFont & LyXFont::setEmph(LyXFont::FONT_MISC_STATE e)
272 LyXFont & LyXFont::setUnderbar(LyXFont::FONT_MISC_STATE u)
279 LyXFont & LyXFont::setNoun(LyXFont::FONT_MISC_STATE n)
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,
395 if (newfont.family() == family() && toggleall)
396 setFamily(INHERIT_FAMILY); // toggle 'back'
397 else if (newfont.family() != IGNORE_FAMILY)
398 setFamily(newfont.family());
399 // else it's IGNORE_SHAPE
401 // "Old" behaviour: "Setting" bold will toggle bold on/off.
402 switch (newfont.series()) {
405 if (series() == BOLD_SERIES && toggleall)
406 setSeries(MEDIUM_SERIES);
408 setSeries(BOLD_SERIES);
412 setSeries(newfont.series());
418 if (newfont.shape() == shape() && toggleall)
419 setShape(INHERIT_SHAPE); // toggle 'back'
420 else if (newfont.shape() != IGNORE_SHAPE)
421 setShape(newfont.shape());
422 // else it's IGNORE_SHAPE
424 if (newfont.size() != IGNORE_SIZE) {
425 if (newfont.size() == INCREASE_SIZE)
427 else if (newfont.size() == DECREASE_SIZE)
430 setSize(newfont.size());
433 setEmph(setMisc(newfont.emph(), emph()));
434 setUnderbar(setMisc(newfont.underbar(), underbar()));
435 setNoun(setMisc(newfont.noun(), noun()));
437 setNumber(setMisc(newfont.number(), number()));
438 if (newfont.language() == language() && toggleall)
439 if (language() == document_language)
440 setLanguage(default_language);
442 #ifndef INHERIT_LANGUAGE
443 setLanguage(document_language);
445 setLanguage(inherit_language);
447 else if (newfont.language() != ignore_language)
448 setLanguage(newfont.language());
450 if (newfont.color() == color() && toggleall)
451 setColor(LColor::inherit); // toggle 'back'
452 else if (newfont.color() != LColor::ignore)
453 setColor(newfont.color());
457 /// Reduce font to fall back to template where possible
458 void LyXFont::reduce(LyXFont const & tmplt)
460 if (family() == tmplt.family())
461 setFamily(INHERIT_FAMILY);
462 if (series() == tmplt.series())
463 setSeries(INHERIT_SERIES);
464 if (shape() == tmplt.shape())
465 setShape(INHERIT_SHAPE);
466 if (size() == tmplt.size())
467 setSize(INHERIT_SIZE);
468 if (emph() == tmplt.emph())
470 if (underbar() == tmplt.underbar())
471 setUnderbar(INHERIT);
472 if (noun() == tmplt.noun())
474 if (color() == tmplt.color())
475 setColor(LColor::inherit);
476 #ifdef INHERIT_LANGUAGE
477 if (language() == tmplt.language())
478 setLanguage(inherit_language);
483 /// Realize font from a template
484 #ifndef INHERIT_LANGUAGE
485 LyXFont & LyXFont::realize(LyXFont const & tmplt)
487 LyXFont & LyXFont::realize(LyXFont const & tmplt, Language const * deflang)
490 #ifdef INHERIT_LANGUAGE
491 if (language() == inherit_language) {
492 if (tmplt.language() == inherit_language ||
493 tmplt.language() == ignore_language ||
494 tmplt.language() == default_language)
496 setLanguage(deflang);
498 setLanguage(tmplt.language());
502 if (bits == inherit) {
507 if (bits.family == INHERIT_FAMILY) {
508 bits.family = tmplt.bits.family;
510 if (bits.series == INHERIT_SERIES) {
511 bits.series = tmplt.bits.series;
513 if (bits.shape == INHERIT_SHAPE) {
514 bits.shape = tmplt.bits.shape;
516 if (bits.size == INHERIT_SIZE) {
517 bits.size = tmplt.bits.size;
519 if (bits.emph == INHERIT) {
520 bits.emph = tmplt.bits.emph;
522 if (bits.underbar == INHERIT) {
523 bits.underbar = tmplt.bits.underbar;
525 if (bits.noun == INHERIT) {
526 bits.noun = tmplt.bits.noun;
528 if (bits.color == LColor::inherit) {
529 bits.color = tmplt.bits.color;
535 /// Is font resolved?
536 bool LyXFont::resolved() const
538 return (family() != INHERIT_FAMILY && series() != INHERIT_SERIES &&
539 shape() != INHERIT_SHAPE && size() != INHERIT_SIZE &&
540 emph() != INHERIT && underbar() != INHERIT &&
542 #ifdef INHERIT_LANGUAGE
543 language() != inherit_language &&
545 color() != LColor::inherit);
549 /// Build GUI description of font state
550 string const LyXFont::stateText(BufferParams * params) const
553 if (family() != INHERIT_FAMILY)
554 ost << _(GUIFamilyNames[family()]) << ", ";
555 if (series() != INHERIT_SERIES)
556 ost << _(GUISeriesNames[series()]) << ", ";
557 if (shape() != INHERIT_SHAPE)
558 ost << _(GUIShapeNames[shape()]) << ", ";
559 if (size() != INHERIT_SIZE)
560 ost << _(GUISizeNames[size()]) << ", ";
561 if (color() != LColor::inherit)
562 ost << lcolor.getGUIName(color()) << ", ";
563 if (emph() != INHERIT)
564 ost << _("Emphasis ")
565 << _(GUIMiscNames[emph()]) << ", ";
566 if (underbar() != INHERIT)
567 ost << _("Underline ")
568 << _(GUIMiscNames[underbar()]) << ", ";
569 if (noun() != INHERIT)
570 ost << _("Noun ") << _(GUIMiscNames[noun()]) << ", ";
572 ost << _("Default") << ", ";
573 if (!params || (language() != params->language))
574 ost << _("Language: ") << _(language()->display()) << ", ";
576 ost << _(" Number ") << _(GUIMiscNames[number()]);
578 string buf(ost.str().c_str());
579 buf = strip(buf, ' ');
580 buf = strip(buf, ',');
585 // Set family according to lyx format string
586 LyXFont & LyXFont::setLyXFamily(string const & fam)
588 string const s = lowercase(fam);
591 while (s != LyXFamilyNames[i] && LyXFamilyNames[i] != "error") ++i;
592 if (s == LyXFamilyNames[i]) {
593 setFamily(LyXFont::FONT_FAMILY(i));
595 lyxerr << "LyXFont::setLyXFamily: Unknown family `"
596 << s << '\'' << endl;
601 // Set series according to lyx format string
602 LyXFont & LyXFont::setLyXSeries(string const & ser)
604 string const s = lowercase(ser);
607 while (s != LyXSeriesNames[i] && LyXSeriesNames[i] != "error") ++i;
608 if (s == LyXSeriesNames[i]) {
609 setSeries(LyXFont::FONT_SERIES(i));
611 lyxerr << "LyXFont::setLyXSeries: Unknown series `"
612 << s << '\'' << endl;
617 // Set shape according to lyx format string
618 LyXFont & LyXFont::setLyXShape(string const & sha)
620 string const s = lowercase(sha);
623 while (s != LyXShapeNames[i] && LyXShapeNames[i] != "error") ++i;
624 if (s == LyXShapeNames[i]) {
625 setShape(LyXFont::FONT_SHAPE(i));
627 lyxerr << "LyXFont::setLyXShape: Unknown shape `"
628 << s << '\'' << endl;
633 // Set size according to lyx format string
634 LyXFont & LyXFont::setLyXSize(string const & siz)
636 string const s = lowercase(siz);
638 while (s != LyXSizeNames[i] && LyXSizeNames[i] != "error") ++i;
639 if (s == LyXSizeNames[i]) {
640 setSize(LyXFont::FONT_SIZE(i));
642 lyxerr << "LyXFont::setLyXSize: Unknown size `"
643 << s << '\'' << endl;
648 // Set size according to lyx format string
649 LyXFont::FONT_MISC_STATE LyXFont::setLyXMisc(string const & siz)
651 string const s = lowercase(siz);
653 while (s != LyXMiscNames[i] && LyXMiscNames[i] != "error") ++i;
654 if (s == LyXMiscNames[i])
655 return FONT_MISC_STATE(i);
656 lyxerr << "LyXFont::setLyXMisc: Unknown misc flag `"
657 << s << '\'' << endl;
662 /// Sets color after LyX text format
663 LyXFont & LyXFont::setLyXColor(string const & col)
665 setColor(lcolor.getFromLyXName(col));
670 // Returns size in latex format
671 string const LyXFont::latexSize() const
673 return LaTeXSizeNames[size()];
677 // Read a font definition from given file in lyx format
679 LyXFont & LyXFont::lyxRead(LyXLex & lex)
682 bool finished = false;
683 while (!finished && lex.isOK() && !error) {
685 string const tok = lowercase(lex.getString());
689 } else if (tok == "endfont") {
691 } else if (tok == "family") {
693 string const ttok = lex.getString();
695 } else if (tok == "series") {
697 string const ttok = lex.getString();
699 } else if (tok == "shape") {
701 string const ttok = lex.getString();
703 } else if (tok == "size") {
705 string const ttok = lex.getString();
707 } else if (tok == "misc") {
709 string const ttok = lowercase(lex.getString());
711 if (ttok == "no_bar") {
713 } else if (ttok == "no_emph") {
715 } else if (ttok == "no_noun") {
717 } else if (ttok == "emph") {
719 } else if (ttok == "underbar") {
721 } else if (ttok == "noun") {
724 lex.printError("Illegal misc type `$$Token´");
726 } else if (tok == "color") {
728 string const ttok = lex.getString();
731 lex.printError("Unknown tag `$$Token'");
739 /// Writes the changes from this font to orgfont in .lyx format in file
740 void LyXFont::lyxWriteChanges(LyXFont const & orgfont,
741 #ifdef INHERIT_LANGUAGE
742 Language const * doclang,
747 if (orgfont.family() != family()) {
748 os << "\\family " << LyXFamilyNames[family()] << " \n";
750 if (orgfont.series() != series()) {
751 os << "\\series " << LyXSeriesNames[series()] << " \n";
753 if (orgfont.shape() != shape()) {
754 os << "\\shape " << LyXShapeNames[shape()] << " \n";
756 if (orgfont.size() != size()) {
757 os << "\\size " << LyXSizeNames[size()] << " \n";
759 if (orgfont.emph() != emph()) {
760 os << "\\emph " << LyXMiscNames[emph()] << " \n";
762 if (orgfont.number() != number()) {
763 os << "\\numeric " << LyXMiscNames[number()] << " \n";
765 if (orgfont.underbar() != underbar()) {
766 // This is only for backwards compatibility
767 switch (underbar()) {
768 case OFF: os << "\\bar no \n"; break;
769 case ON: os << "\\bar under \n"; break;
770 case TOGGLE: lyxerr << "LyXFont::lyxWriteFontChanges: "
771 "TOGGLE should not appear here!"
774 case INHERIT: os << "\\bar default \n"; break;
775 case IGNORE: lyxerr << "LyXFont::lyxWriteFontChanges: "
776 "IGNORE should not appear here!"
781 if (orgfont.noun() != noun()) {
782 os << "\\noun " << LyXMiscNames[noun()] << " \n";
784 if (orgfont.color() != color()) {
785 // To make us file compatible with older
786 // lyx versions we emit "default" instead
788 string col_str(lcolor.getLyXName(color()));
789 if (col_str == "inherit") col_str = "default";
790 os << "\\color " << col_str << "\n";
792 if (orgfont.language() != language()) {
793 #ifndef INHERIT_LANGUAGE
796 if (language() == inherit_language)
797 os << "\\lang " << doclang->lang() << "\n";
800 os << "\\lang " << language()->lang() << "\n";
802 os << "\\lang unknown\n";
807 /// Writes the head of the LaTeX needed to impose this font
808 // Returns number of chars written.
809 int LyXFont::latexWriteStartChanges(ostream & os, LyXFont const & base,
810 LyXFont const & prev) const
815 if (language()->babel() != base.language()->babel() &&
816 language() != prev.language()) {
817 if (isRightToLeft() != prev.isRightToLeft()) {
818 if (isRightToLeft()) {
827 subst(lyxrc.language_command_local,
828 "$$lang", language()->babel());
830 count += tmp.length();
834 if (number() == ON && prev.number() != ON &&
835 language()->lang() == "hebrew") {
843 if (f.family() != INHERIT_FAMILY) {
845 << LaTeXFamilyNames[f.family()]
847 count += strlen(LaTeXFamilyNames[f.family()]) + 2;
848 env = true; //We have opened a new environment
850 if (f.series() != INHERIT_SERIES) {
852 << LaTeXSeriesNames[f.series()]
854 count += strlen(LaTeXSeriesNames[f.series()]) + 2;
855 env = true; //We have opened a new environment
857 if (f.shape() != INHERIT_SHAPE) {
859 << LaTeXShapeNames[f.shape()]
861 count += strlen(LaTeXShapeNames[f.shape()]) + 2;
862 env = true; //We have opened a new environment
864 if (f.color() != LColor::inherit && f.color() != LColor::ignore) {
866 << lcolor.getLaTeXName(f.color())
868 count += lcolor.getLaTeXName(f.color()).length() + 13;
869 env = true; //We have opened a new environment
871 if (f.emph() == ON) {
874 env = true; //We have opened a new environment
876 if (f.underbar() == ON) {
879 env = true; //We have opened a new environment
881 // \noun{} is a LyX special macro
882 if (f.noun() == ON) {
885 env = true; //We have opened a new environment
887 if (f.size() != INHERIT_SIZE) {
888 // If we didn't open an environment above, we open one here
894 << LaTeXSizeNames[f.size()]
896 count += strlen(LaTeXSizeNames[f.size()]) + 2;
902 /// Writes ending block of LaTeX needed to close use of this font
903 // Returns number of chars written
904 // This one corresponds to latexWriteStartChanges(). (Asger)
905 int LyXFont::latexWriteEndChanges(ostream & os, LyXFont const & base,
906 LyXFont const & next) const
911 LyXFont f = *this; // why do you need this?
912 f.reduce(base); // why isn't this just "reduce(base);" (Lgb)
913 // Because this function is const. Everything breaks if this
914 // method changes the font it represents. There is no speed penalty
915 // by using the temporary. (Asger)
918 if (f.family() != INHERIT_FAMILY) {
921 env = true; // Size change need not bother about closing env.
923 if (f.series() != INHERIT_SERIES) {
926 env = true; // Size change need not bother about closing env.
928 if (f.shape() != INHERIT_SHAPE) {
931 env = true; // Size change need not bother about closing env.
933 if (f.color() != LColor::inherit && f.color() != LColor::ignore) {
936 env = true; // Size change need not bother about closing env.
938 if (f.emph() == ON) {
941 env = true; // Size change need not bother about closing env.
943 if (f.underbar() == ON) {
946 env = true; // Size change need not bother about closing env.
948 if (f.noun() == ON) {
951 env = true; // Size change need not bother about closing env.
953 if (f.size() != INHERIT_SIZE) {
954 // We only have to close if only size changed
961 if (number() == ON && next.number() != ON &&
962 language()->lang() == "hebrew") {
967 if (language() != base.language() && language() != next.language()) {
976 LColor::color LyXFont::realColor() const
978 if (color() == LColor::none)
979 return LColor::foreground;
984 // Convert logical attributes to concrete shape attribute
985 LyXFont::FONT_SHAPE LyXFont::realShape() const
987 register FONT_SHAPE s = shape();
1001 ostream & operator<<(ostream & o, LyXFont::FONT_MISC_STATE fms)
1003 return o << int(fms);