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
43 char const * GUIFamilyNames[6] =
44 { N_("Roman"), N_("Sans serif"), N_("Typewriter"), N_("Symbol"), N_("Inherit"),
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[6] =
67 { "roman", "sans", "typewriter", "symbol", "default", "error" };
69 char const * LyXSeriesNames[4] =
70 { "medium", "bold", "default", "error" };
72 char const * LyXShapeNames[6] =
73 { "up", "italic", "slanted", "smallcaps", "default", "error" };
75 char const * LyXSizeNames[14] =
76 { "tiny", "scriptsize", "footnotesize", "small", "normal", "large",
77 "larger", "largest", "huge", "giant",
78 "increase-error", "decrease-error", "default", "error" };
80 char const * LyXMiscNames[5] =
81 { "off", "on", "toggle", "default", "error" };
84 // Strings used to write LaTeX files
86 char const * LaTeXFamilyNames[6] =
87 { "textrm", "textsf", "texttt", "error1", "error2", "error3" };
89 char const * LaTeXSeriesNames[4] =
90 { "textmd", "textbf", "error4", "error5" };
92 char const * LaTeXShapeNames[6] =
93 { "textup", "textit", "textsl", "textsc", "error6", "error7" };
95 char const * LaTeXSizeNames[14] =
96 { "tiny", "scriptsize", "footnotesize", "small", "normalsize", "large",
97 "Large", "LARGE", "huge", "Huge", "error8", "error9", "error10", "error11" };
102 // Initialize static member
103 LyXFont::FontBits LyXFont::sane = {
115 // Initialize static member
116 LyXFont::FontBits LyXFont::inherit = {
128 // Initialize static member
129 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.latex == latex &&
153 fb1.number == number;
157 bool LyXFont::FontBits::operator!=(LyXFont::FontBits const & fb1) const
159 return !(fb1 == *this);
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_MISC_STATE LyXFont::underbar() const
195 return bits.underbar;
199 LyXFont::FONT_MISC_STATE LyXFont::latex() const
205 LColor::color LyXFont::color() const
211 Language const * LyXFont::language() const
217 LyXFont::FONT_MISC_STATE LyXFont::number() const
223 bool LyXFont::isRightToLeft() const
225 return lang->RightToLeft();
229 bool LyXFont::isVisibleRightToLeft() const
231 return (lang->RightToLeft() && latex() != ON && number() != ON);
235 LyXFont & LyXFont::setFamily(LyXFont::FONT_FAMILY f)
242 LyXFont & LyXFont::setSeries(LyXFont::FONT_SERIES s)
249 LyXFont & LyXFont::setShape(LyXFont::FONT_SHAPE s)
256 LyXFont & LyXFont::setSize(LyXFont::FONT_SIZE s)
263 LyXFont & LyXFont::setEmph(LyXFont::FONT_MISC_STATE e)
270 LyXFont & LyXFont::setUnderbar(LyXFont::FONT_MISC_STATE u)
277 LyXFont & LyXFont::setNoun(LyXFont::FONT_MISC_STATE n)
284 LyXFont & LyXFont::setLatex(LyXFont::FONT_MISC_STATE l)
291 LyXFont & LyXFont::setColor(LColor::color c)
298 LyXFont & LyXFont::setLanguage(Language const * l)
305 LyXFont & LyXFont::setNumber(LyXFont::FONT_MISC_STATE n)
312 /// Decreases font size by one
313 LyXFont & LyXFont::decSize()
316 case SIZE_HUGER: setSize(SIZE_HUGE); break;
317 case SIZE_HUGE: setSize(SIZE_LARGEST); break;
318 case SIZE_LARGEST: setSize(SIZE_LARGER); break;
319 case SIZE_LARGER: setSize(SIZE_LARGE); break;
320 case SIZE_LARGE: setSize(SIZE_NORMAL); break;
321 case SIZE_NORMAL: setSize(SIZE_SMALL); break;
322 case SIZE_SMALL: setSize(SIZE_FOOTNOTE); break;
323 case SIZE_FOOTNOTE: setSize(SIZE_SCRIPT); break;
324 case SIZE_SCRIPT: setSize(SIZE_TINY); break;
325 case SIZE_TINY: break;
327 lyxerr << "Can't LyXFont::decSize on INCREASE_SIZE" << endl;
330 lyxerr <<"Can't LyXFont::decSize on DECREASE_SIZE" << endl;
333 lyxerr <<"Can't LyXFont::decSize on INHERIT_SIZE" << endl;
336 lyxerr <<"Can't LyXFont::decSize on IGNORE_SIZE" << endl;
343 /// Increases font size by one
344 LyXFont & LyXFont::incSize()
347 case SIZE_HUGER: break;
348 case SIZE_HUGE: setSize(SIZE_HUGER); break;
349 case SIZE_LARGEST: setSize(SIZE_HUGE); break;
350 case SIZE_LARGER: setSize(SIZE_LARGEST); break;
351 case SIZE_LARGE: setSize(SIZE_LARGER); break;
352 case SIZE_NORMAL: setSize(SIZE_LARGE); break;
353 case SIZE_SMALL: setSize(SIZE_NORMAL); break;
354 case SIZE_FOOTNOTE: setSize(SIZE_SMALL); break;
355 case SIZE_SCRIPT: setSize(SIZE_FOOTNOTE); break;
356 case SIZE_TINY: setSize(SIZE_SCRIPT); break;
358 lyxerr <<"Can't LyXFont::incSize on INCREASE_SIZE" << endl;
361 lyxerr <<"Can't LyXFont::incSize on DECREASE_SIZE" << endl;
364 lyxerr <<"Can't LyXFont::incSize on INHERIT_SIZE" << endl;
367 lyxerr <<"Can't LyXFont::incSize on IGNORE_SIZE" << endl;
374 /// Updates a misc setting according to request
375 LyXFont::FONT_MISC_STATE LyXFont::setMisc(FONT_MISC_STATE newfont,
378 if (newfont == TOGGLE) {
384 lyxerr <<"LyXFont::setMisc: Need state"
385 " ON or OFF to toggle. Setting to ON" << endl;
388 } else if (newfont == IGNORE)
395 /// Updates font settings according to request
396 void LyXFont::update(LyXFont const & newfont,
397 Language const * document_language, bool toggleall)
399 if (newfont.family() == family() && toggleall)
400 setFamily(INHERIT_FAMILY); // toggle 'back'
401 else if (newfont.family() != IGNORE_FAMILY)
402 setFamily(newfont.family());
403 // else it's IGNORE_SHAPE
405 // "Old" behaviour: "Setting" bold will toggle bold on/off.
406 switch (newfont.series()) {
409 if (series() == BOLD_SERIES && toggleall)
410 setSeries(MEDIUM_SERIES);
412 setSeries(BOLD_SERIES);
416 setSeries(newfont.series());
422 if (newfont.shape() == shape() && toggleall)
423 setShape(INHERIT_SHAPE); // toggle 'back'
424 else if (newfont.shape() != IGNORE_SHAPE)
425 setShape(newfont.shape());
426 // else it's IGNORE_SHAPE
428 if (newfont.size() != IGNORE_SIZE) {
429 if (newfont.size() == INCREASE_SIZE)
431 else if (newfont.size() == DECREASE_SIZE)
434 setSize(newfont.size());
437 setEmph(setMisc(newfont.emph(), emph()));
438 setUnderbar(setMisc(newfont.underbar(), underbar()));
439 setNoun(setMisc(newfont.noun(), noun()));
440 setLatex(setMisc(newfont.latex(), latex()));
442 setNumber(setMisc(newfont.number(), number()));
443 if (newfont.language() == language() && toggleall)
444 if (language() == document_language)
445 setLanguage(default_language);
447 setLanguage(document_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 (latex() == tmplt.latex())
477 if (color() == tmplt.color())
478 setColor(LColor::inherit);
482 /// Realize font from a template
483 LyXFont & LyXFont::realize(LyXFont const & tmplt)
485 if (bits == inherit) {
490 if (bits.family == INHERIT_FAMILY) {
491 bits.family = tmplt.bits.family;
493 if (bits.series == INHERIT_SERIES) {
494 bits.series = tmplt.bits.series;
496 if (bits.shape == INHERIT_SHAPE) {
497 bits.shape = tmplt.bits.shape;
499 if (bits.size == INHERIT_SIZE) {
500 bits.size = tmplt.bits.size;
502 if (bits.emph == INHERIT) {
503 bits.emph = tmplt.bits.emph;
505 if (bits.underbar == INHERIT) {
506 bits.underbar = tmplt.bits.underbar;
508 if (bits.noun == INHERIT) {
509 bits.noun = tmplt.bits.noun;
511 if (bits.latex == INHERIT) {
512 bits.latex = tmplt.bits.latex;
514 if (bits.color == LColor::inherit) {
515 bits.color = tmplt.bits.color;
521 /// Is font resolved?
522 bool LyXFont::resolved() const
524 return (family() != INHERIT_FAMILY && series() != INHERIT_SERIES &&
525 shape() != INHERIT_SHAPE && size() != INHERIT_SIZE &&
526 emph() != INHERIT && underbar() != INHERIT &&
527 noun() != INHERIT && latex() != INHERIT &&
528 color() != LColor::inherit);
532 /// Build GUI description of font state
533 string const LyXFont::stateText(BufferParams * params) const
535 std::ostringstream ost;
536 if (family() != INHERIT_FAMILY)
537 ost << _(GUIFamilyNames[family()]) << ", ";
538 if (series() != INHERIT_SERIES)
539 ost << _(GUISeriesNames[series()]) << ", ";
540 if (shape() != INHERIT_SHAPE)
541 ost << _(GUIShapeNames[shape()]) << ", ";
542 if (size() != INHERIT_SIZE)
543 ost << _(GUISizeNames[size()]) << ", ";
544 if (color() != LColor::inherit)
545 ost << lcolor.getGUIName(color()) << ", ";
546 if (emph() != INHERIT)
547 ost << _("Emphasis ")
548 << _(GUIMiscNames[emph()]) << ", ";
549 if (underbar() != INHERIT)
550 ost << _("Underline ")
551 << _(GUIMiscNames[underbar()]) << ", ";
552 if (noun() != INHERIT)
553 ost << _("Noun ") << _(GUIMiscNames[noun()]) << ", ";
554 if (latex() != INHERIT)
555 ost << _("Latex ") << _(GUIMiscNames[latex()]) << ", ";
557 ost << _("Default") << ", ";
558 if (!params || (language() != params->language))
559 ost << _("Language: ") << _(language()->display()) << ", ";
561 ost << _(" Number ") << _(GUIMiscNames[number()]);
563 string buf(ost.str().c_str());
564 buf = strip(buf, ' ');
565 buf = strip(buf, ',');
570 // Set family according to lyx format string
571 LyXFont & LyXFont::setLyXFamily(string const & fam)
573 string const s = lowercase(fam);
576 while (s != LyXFamilyNames[i] && LyXFamilyNames[i] != "error") ++i;
577 if (s == LyXFamilyNames[i]) {
578 setFamily(LyXFont::FONT_FAMILY(i));
580 lyxerr << "LyXFont::setLyXFamily: Unknown family `"
581 << s << '\'' << endl;
586 // Set series according to lyx format string
587 LyXFont & LyXFont::setLyXSeries(string const & ser)
589 string const s = lowercase(ser);
592 while (s != LyXSeriesNames[i] && LyXSeriesNames[i] != "error") ++i;
593 if (s == LyXSeriesNames[i]) {
594 setSeries(LyXFont::FONT_SERIES(i));
596 lyxerr << "LyXFont::setLyXSeries: Unknown series `"
597 << s << '\'' << endl;
602 // Set shape according to lyx format string
603 LyXFont & LyXFont::setLyXShape(string const & sha)
605 string const s = lowercase(sha);
608 while (s != LyXShapeNames[i] && LyXShapeNames[i] != "error") ++i;
609 if (s == LyXShapeNames[i]) {
610 setShape(LyXFont::FONT_SHAPE(i));
612 lyxerr << "LyXFont::setLyXShape: Unknown shape `"
613 << s << '\'' << endl;
618 // Set size according to lyx format string
619 LyXFont & LyXFont::setLyXSize(string const & siz)
621 string const s = lowercase(siz);
623 while (s != LyXSizeNames[i] && LyXSizeNames[i] != "error") ++i;
624 if (s == LyXSizeNames[i]) {
625 setSize(LyXFont::FONT_SIZE(i));
627 lyxerr << "LyXFont::setLyXSize: Unknown size `"
628 << s << '\'' << endl;
633 // Set size according to lyx format string
634 LyXFont::FONT_MISC_STATE LyXFont::setLyXMisc(string const & siz)
636 string const s = lowercase(siz);
638 while (s != LyXMiscNames[i] && LyXMiscNames[i] != "error") ++i;
639 if (s == LyXMiscNames[i])
640 return FONT_MISC_STATE(i);
641 lyxerr << "LyXFont::setLyXMisc: Unknown misc flag `"
642 << s << '\'' << endl;
647 /// Sets color after LyX text format
648 LyXFont & LyXFont::setLyXColor(string const & col)
650 setColor(lcolor.getFromLyXName(col));
655 // Returns size in latex format
656 string const LyXFont::latexSize() const
658 return LaTeXSizeNames[size()];
662 // Read a font definition from given file in lyx format
664 LyXFont & LyXFont::lyxRead(LyXLex & lex)
667 bool finished = false;
668 while (!finished && lex.IsOK() && !error) {
670 string const tok = lowercase(lex.GetString());
674 } else if (tok == "endfont") {
676 } else if (tok == "family") {
678 string const ttok = lex.GetString();
680 } else if (tok == "series") {
682 string const ttok = lex.GetString();
684 } else if (tok == "shape") {
686 string const ttok = lex.GetString();
688 } else if (tok == "size") {
690 string const ttok = lex.GetString();
692 } else if (tok == "latex") {
694 string const ttok = lowercase(lex.GetString());
696 if (ttok == "no_latex") {
698 } else if (ttok == "latex") {
701 lex.printError("Illegal LaTeX type`$$Token'");
703 } else if (tok == "misc") {
705 string const ttok = lowercase(lex.GetString());
707 if (ttok == "no_bar") {
709 } else if (ttok == "no_emph") {
711 } else if (ttok == "no_noun") {
713 } else if (ttok == "emph") {
715 } else if (ttok == "underbar") {
717 } else if (ttok == "noun") {
720 lex.printError("Illegal misc type `$$Token´");
722 } else if (tok == "color") {
724 string const ttok = lex.GetString();
727 lex.printError("Unknown tag `$$Token'");
735 /// Writes the changes from this font to orgfont in .lyx format in file
736 void LyXFont::lyxWriteChanges(LyXFont const & orgfont, ostream & os) const
739 if (orgfont.family() != family()) {
740 os << "\\family " << LyXFamilyNames[family()] << " \n";
742 if (orgfont.series() != series()) {
743 os << "\\series " << LyXSeriesNames[series()] << " \n";
745 if (orgfont.shape() != shape()) {
746 os << "\\shape " << LyXShapeNames[shape()] << " \n";
748 if (orgfont.size() != size()) {
749 os << "\\size " << LyXSizeNames[size()] << " \n";
751 if (orgfont.emph() != emph()) {
752 os << "\\emph " << LyXMiscNames[emph()] << " \n";
754 if (orgfont.number() != number()) {
755 os << "\\numeric " << LyXMiscNames[number()] << " \n";
757 if (orgfont.underbar() != underbar()) {
758 // This is only for backwards compatibility
759 switch (underbar()) {
760 case OFF: os << "\\bar no \n"; break;
761 case ON: os << "\\bar under \n"; break;
762 case TOGGLE: lyxerr << "LyXFont::lyxWriteFontChanges: "
763 "TOGGLE should not appear here!"
766 case INHERIT: os << "\\bar default \n"; break;
767 case IGNORE: lyxerr << "LyXFont::lyxWriteFontChanges: "
768 "IGNORE should not appear here!"
773 if (orgfont.noun() != noun()) {
774 os << "\\noun " << LyXMiscNames[noun()] << " \n";
776 if (orgfont.latex() != latex()) {
777 // This is only for backwards compatibility
779 case OFF: os << "\\latex no_latex \n"; break;
780 case ON: os << "\\latex latex \n"; break;
781 case TOGGLE: lyxerr << "LyXFont::lyxWriteFontChanges: "
782 "TOGGLE should not appear here!"
785 case INHERIT: os << "\\latex default \n"; break;
786 case IGNORE: lyxerr << "LyXFont::lyxWriteFontChanges: "
787 "IGNORE should not appear here!"
792 if (orgfont.color() != color()) {
793 // To make us file compatible with older
794 // lyx versions we emit "default" instead
796 string col_str(lcolor.getLyXName(color()));
797 if (col_str == "inherit") col_str = "default";
798 os << "\\color " << col_str << "\n";
800 if (orgfont.language() != language()) {
802 os << "\\lang " << language()->lang() << "\n";
804 os << "\\lang unknown\n";
809 /// Writes the head of the LaTeX needed to impose this font
810 // Returns number of chars written.
811 int LyXFont::latexWriteStartChanges(ostream & os, LyXFont const & base,
812 LyXFont const & prev) const
817 if (language()->babel() != base.language()->babel() &&
818 language()->babel() != prev.language()->babel()) {
819 if (isRightToLeft() != prev.isRightToLeft()) {
820 if (isRightToLeft()) {
829 subst(lyxrc.language_command_local,
830 "$$lang", language()->babel());
832 count += tmp.length();
836 if (number() == ON && prev.number() != ON &&
837 language()->lang() == "hebrew") {
845 if (f.family() != INHERIT_FAMILY) {
847 << LaTeXFamilyNames[f.family()]
849 count += strlen(LaTeXFamilyNames[f.family()]) + 2;
850 env = true; //We have opened a new environment
852 if (f.series() != INHERIT_SERIES) {
854 << LaTeXSeriesNames[f.series()]
856 count += strlen(LaTeXSeriesNames[f.series()]) + 2;
857 env = true; //We have opened a new environment
859 if (f.shape() != INHERIT_SHAPE) {
861 << LaTeXShapeNames[f.shape()]
863 count += strlen(LaTeXShapeNames[f.shape()]) + 2;
864 env = true; //We have opened a new environment
866 if (f.color() != LColor::inherit && f.color() != LColor::ignore) {
868 << lcolor.getLaTeXName(f.color())
870 count += lcolor.getLaTeXName(f.color()).length() + 13;
871 env = true; //We have opened a new environment
873 if (f.emph() == ON) {
876 env = true; //We have opened a new environment
878 if (f.underbar() == ON) {
881 env = true; //We have opened a new environment
883 // \noun{} is a LyX special macro
884 if (f.noun() == ON) {
887 env = true; //We have opened a new environment
889 if (f.size() != INHERIT_SIZE) {
890 // If we didn't open an environment above, we open one here
896 << LaTeXSizeNames[f.size()]
898 count += strlen(LaTeXSizeNames[f.size()]) + 2;
904 /// Writes ending block of LaTeX needed to close use of this font
905 // Returns number of chars written
906 // This one corresponds to latexWriteStartChanges(). (Asger)
907 int LyXFont::latexWriteEndChanges(ostream & os, LyXFont const & base,
908 LyXFont const & next) const
913 LyXFont f = *this; // why do you need this?
914 f.reduce(base); // why isn't this just "reduce(base);" (Lgb)
915 // Because this function is const. Everything breaks if this
916 // method changes the font it represents. There is no speed penalty
917 // by using the temporary. (Asger)
920 if (f.family() != INHERIT_FAMILY) {
923 env = true; // Size change need not bother about closing env.
925 if (f.series() != INHERIT_SERIES) {
928 env = true; // Size change need not bother about closing env.
930 if (f.shape() != INHERIT_SHAPE) {
933 env = true; // Size change need not bother about closing env.
935 if (f.color() != LColor::inherit && f.color() != LColor::ignore) {
938 env = true; // Size change need not bother about closing env.
940 if (f.emph() == ON) {
943 env = true; // Size change need not bother about closing env.
945 if (f.underbar() == ON) {
948 env = true; // Size change need not bother about closing env.
950 if (f.noun() == ON) {
953 env = true; // Size change need not bother about closing env.
955 if (f.size() != INHERIT_SIZE) {
956 // We only have to close if only size changed
963 if (number() == ON && next.number() != ON &&
964 language()->lang() == "hebrew") {
969 if (language() != base.language() && language() != next.language()) {
978 LColor::color LyXFont::realColor() const
981 return LColor::latex;
982 if (color() == LColor::none)
983 return LColor::foreground;
988 // Convert logical attributes to concrete shape attribute
989 LyXFont::FONT_SHAPE LyXFont::realShape() const
991 register FONT_SHAPE s = shape();
1000 s = SMALLCAPS_SHAPE;
1005 bool LyXFont::equalExceptLatex(LyXFont const & f) const
1008 f1.setLatex(f.latex());
1013 ostream & operator<<(ostream & o, LyXFont::FONT_MISC_STATE fms)
1015 return o << int(fms);