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 = {
114 // Initialize static member
115 LyXFont::FontBits LyXFont::inherit = {
126 // Initialize static member
127 LyXFont::FontBits LyXFont::ignore = {
139 bool LyXFont::FontBits::operator==(LyXFont::FontBits const & fb1) const
141 return fb1.family == family &&
142 fb1.series == series &&
143 fb1.shape == shape &&
145 fb1.color == color &&
147 fb1.underbar == underbar &&
149 fb1.number == number;
153 bool LyXFont::FontBits::operator!=(LyXFont::FontBits const & fb1) const
155 return !(fb1 == *this);
159 LyXFont::LyXFont(LyXFont::FONT_INIT1)
160 #ifndef INHERIT_LANGUAGE
161 : bits(inherit), lang(default_language)
163 : bits(inherit), lang(inherit_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 LColor::color LyXFont::color() const
205 Language const * LyXFont::language() const
211 LyXFont::FONT_MISC_STATE LyXFont::number() const
217 bool LyXFont::isRightToLeft() const
219 return lang->RightToLeft();
223 bool LyXFont::isVisibleRightToLeft() const
225 return (lang->RightToLeft() &&
230 LyXFont & LyXFont::setFamily(LyXFont::FONT_FAMILY f)
237 LyXFont & LyXFont::setSeries(LyXFont::FONT_SERIES s)
244 LyXFont & LyXFont::setShape(LyXFont::FONT_SHAPE s)
251 LyXFont & LyXFont::setSize(LyXFont::FONT_SIZE s)
258 LyXFont & LyXFont::setEmph(LyXFont::FONT_MISC_STATE e)
265 LyXFont & LyXFont::setUnderbar(LyXFont::FONT_MISC_STATE u)
272 LyXFont & LyXFont::setNoun(LyXFont::FONT_MISC_STATE n)
279 LyXFont & LyXFont::setColor(LColor::color c)
286 LyXFont & LyXFont::setLanguage(Language const * l)
293 LyXFont & LyXFont::setNumber(LyXFont::FONT_MISC_STATE n)
300 /// Decreases font size by one
301 LyXFont & LyXFont::decSize()
304 case SIZE_HUGER: setSize(SIZE_HUGE); break;
305 case SIZE_HUGE: setSize(SIZE_LARGEST); break;
306 case SIZE_LARGEST: setSize(SIZE_LARGER); break;
307 case SIZE_LARGER: setSize(SIZE_LARGE); break;
308 case SIZE_LARGE: setSize(SIZE_NORMAL); break;
309 case SIZE_NORMAL: setSize(SIZE_SMALL); break;
310 case SIZE_SMALL: setSize(SIZE_FOOTNOTE); break;
311 case SIZE_FOOTNOTE: setSize(SIZE_SCRIPT); break;
312 case SIZE_SCRIPT: setSize(SIZE_TINY); break;
313 case SIZE_TINY: break;
315 lyxerr << "Can't LyXFont::decSize on INCREASE_SIZE" << endl;
318 lyxerr <<"Can't LyXFont::decSize on DECREASE_SIZE" << endl;
321 lyxerr <<"Can't LyXFont::decSize on INHERIT_SIZE" << endl;
324 lyxerr <<"Can't LyXFont::decSize on IGNORE_SIZE" << endl;
331 /// Increases font size by one
332 LyXFont & LyXFont::incSize()
335 case SIZE_HUGER: break;
336 case SIZE_HUGE: setSize(SIZE_HUGER); break;
337 case SIZE_LARGEST: setSize(SIZE_HUGE); break;
338 case SIZE_LARGER: setSize(SIZE_LARGEST); break;
339 case SIZE_LARGE: setSize(SIZE_LARGER); break;
340 case SIZE_NORMAL: setSize(SIZE_LARGE); break;
341 case SIZE_SMALL: setSize(SIZE_NORMAL); break;
342 case SIZE_FOOTNOTE: setSize(SIZE_SMALL); break;
343 case SIZE_SCRIPT: setSize(SIZE_FOOTNOTE); break;
344 case SIZE_TINY: setSize(SIZE_SCRIPT); break;
346 lyxerr <<"Can't LyXFont::incSize on INCREASE_SIZE" << endl;
349 lyxerr <<"Can't LyXFont::incSize on DECREASE_SIZE" << endl;
352 lyxerr <<"Can't LyXFont::incSize on INHERIT_SIZE" << endl;
355 lyxerr <<"Can't LyXFont::incSize on IGNORE_SIZE" << endl;
362 /// Updates a misc setting according to request
363 LyXFont::FONT_MISC_STATE LyXFont::setMisc(FONT_MISC_STATE newfont,
366 if (newfont == TOGGLE) {
372 lyxerr <<"LyXFont::setMisc: Need state"
373 " ON or OFF to toggle. Setting to ON" << endl;
376 } else if (newfont == IGNORE)
383 /// Updates font settings according to request
384 void LyXFont::update(LyXFont const & newfont,
385 Language const * document_language,
388 if (newfont.family() == family() && toggleall)
389 setFamily(INHERIT_FAMILY); // toggle 'back'
390 else if (newfont.family() != IGNORE_FAMILY)
391 setFamily(newfont.family());
392 // else it's IGNORE_SHAPE
394 // "Old" behaviour: "Setting" bold will toggle bold on/off.
395 switch (newfont.series()) {
398 if (series() == BOLD_SERIES && toggleall)
399 setSeries(MEDIUM_SERIES);
401 setSeries(BOLD_SERIES);
405 setSeries(newfont.series());
411 if (newfont.shape() == shape() && toggleall)
412 setShape(INHERIT_SHAPE); // toggle 'back'
413 else if (newfont.shape() != IGNORE_SHAPE)
414 setShape(newfont.shape());
415 // else it's IGNORE_SHAPE
417 if (newfont.size() != IGNORE_SIZE) {
418 if (newfont.size() == INCREASE_SIZE)
420 else if (newfont.size() == DECREASE_SIZE)
423 setSize(newfont.size());
426 setEmph(setMisc(newfont.emph(), emph()));
427 setUnderbar(setMisc(newfont.underbar(), underbar()));
428 setNoun(setMisc(newfont.noun(), noun()));
430 setNumber(setMisc(newfont.number(), number()));
431 if (newfont.language() == language() && toggleall)
432 if (language() == document_language)
433 setLanguage(default_language);
435 #ifndef INHERIT_LANGUAGE
436 setLanguage(document_language);
438 setLanguage(inherit_language);
440 else if (newfont.language() != ignore_language)
441 setLanguage(newfont.language());
443 if (newfont.color() == color() && toggleall)
444 setColor(LColor::inherit); // toggle 'back'
445 else if (newfont.color() != LColor::ignore)
446 setColor(newfont.color());
450 /// Reduce font to fall back to template where possible
451 void LyXFont::reduce(LyXFont const & tmplt)
453 if (family() == tmplt.family())
454 setFamily(INHERIT_FAMILY);
455 if (series() == tmplt.series())
456 setSeries(INHERIT_SERIES);
457 if (shape() == tmplt.shape())
458 setShape(INHERIT_SHAPE);
459 if (size() == tmplt.size())
460 setSize(INHERIT_SIZE);
461 if (emph() == tmplt.emph())
463 if (underbar() == tmplt.underbar())
464 setUnderbar(INHERIT);
465 if (noun() == tmplt.noun())
467 if (color() == tmplt.color())
468 setColor(LColor::inherit);
469 #ifdef INHERIT_LANGUAGE
470 if (language() == tmplt.language())
471 setLanguage(inherit_language);
476 /// Realize font from a template
477 #ifndef INHERIT_LANGUAGE
478 LyXFont & LyXFont::realize(LyXFont const & tmplt)
480 LyXFont & LyXFont::realize(LyXFont const & tmplt, Language const * deflang)
483 #ifdef INHERIT_LANGUAGE
484 if (language() == inherit_language) {
485 if (tmplt.language() == inherit_language ||
486 tmplt.language() == ignore_language ||
487 tmplt.language() == default_language)
489 setLanguage(deflang);
491 setLanguage(tmplt.language());
495 if (bits == inherit) {
500 if (bits.family == INHERIT_FAMILY) {
501 bits.family = tmplt.bits.family;
503 if (bits.series == INHERIT_SERIES) {
504 bits.series = tmplt.bits.series;
506 if (bits.shape == INHERIT_SHAPE) {
507 bits.shape = tmplt.bits.shape;
509 if (bits.size == INHERIT_SIZE) {
510 bits.size = tmplt.bits.size;
512 if (bits.emph == INHERIT) {
513 bits.emph = tmplt.bits.emph;
515 if (bits.underbar == INHERIT) {
516 bits.underbar = tmplt.bits.underbar;
518 if (bits.noun == INHERIT) {
519 bits.noun = tmplt.bits.noun;
521 if (bits.color == LColor::inherit) {
522 bits.color = tmplt.bits.color;
528 /// Is font resolved?
529 bool LyXFont::resolved() const
531 return (family() != INHERIT_FAMILY && series() != INHERIT_SERIES &&
532 shape() != INHERIT_SHAPE && size() != INHERIT_SIZE &&
533 emph() != INHERIT && underbar() != INHERIT &&
535 #ifdef INHERIT_LANGUAGE
536 language() != inherit_language &&
538 color() != LColor::inherit);
542 /// Build GUI description of font state
543 string const LyXFont::stateText(BufferParams * params) const
546 if (family() != INHERIT_FAMILY)
547 ost << _(GUIFamilyNames[family()]) << ", ";
548 if (series() != INHERIT_SERIES)
549 ost << _(GUISeriesNames[series()]) << ", ";
550 if (shape() != INHERIT_SHAPE)
551 ost << _(GUIShapeNames[shape()]) << ", ";
552 if (size() != INHERIT_SIZE)
553 ost << _(GUISizeNames[size()]) << ", ";
554 if (color() != LColor::inherit)
555 ost << lcolor.getGUIName(color()) << ", ";
556 if (emph() != INHERIT)
557 ost << _("Emphasis ")
558 << _(GUIMiscNames[emph()]) << ", ";
559 if (underbar() != INHERIT)
560 ost << _("Underline ")
561 << _(GUIMiscNames[underbar()]) << ", ";
562 if (noun() != INHERIT)
563 ost << _("Noun ") << _(GUIMiscNames[noun()]) << ", ";
565 ost << _("Default") << ", ";
566 if (!params || (language() != params->language))
567 ost << _("Language: ") << _(language()->display()) << ", ";
569 ost << _(" Number ") << _(GUIMiscNames[number()]);
571 string buf(ost.str().c_str());
572 buf = strip(buf, ' ');
573 buf = strip(buf, ',');
578 // Set family according to lyx format string
579 LyXFont & LyXFont::setLyXFamily(string const & fam)
581 string const s = lowercase(fam);
584 while (s != LyXFamilyNames[i] && LyXFamilyNames[i] != "error") ++i;
585 if (s == LyXFamilyNames[i]) {
586 setFamily(LyXFont::FONT_FAMILY(i));
588 lyxerr << "LyXFont::setLyXFamily: Unknown family `"
589 << s << '\'' << endl;
594 // Set series according to lyx format string
595 LyXFont & LyXFont::setLyXSeries(string const & ser)
597 string const s = lowercase(ser);
600 while (s != LyXSeriesNames[i] && LyXSeriesNames[i] != "error") ++i;
601 if (s == LyXSeriesNames[i]) {
602 setSeries(LyXFont::FONT_SERIES(i));
604 lyxerr << "LyXFont::setLyXSeries: Unknown series `"
605 << s << '\'' << endl;
610 // Set shape according to lyx format string
611 LyXFont & LyXFont::setLyXShape(string const & sha)
613 string const s = lowercase(sha);
616 while (s != LyXShapeNames[i] && LyXShapeNames[i] != "error") ++i;
617 if (s == LyXShapeNames[i]) {
618 setShape(LyXFont::FONT_SHAPE(i));
620 lyxerr << "LyXFont::setLyXShape: Unknown shape `"
621 << s << '\'' << endl;
626 // Set size according to lyx format string
627 LyXFont & LyXFont::setLyXSize(string const & siz)
629 string const s = lowercase(siz);
631 while (s != LyXSizeNames[i] && LyXSizeNames[i] != "error") ++i;
632 if (s == LyXSizeNames[i]) {
633 setSize(LyXFont::FONT_SIZE(i));
635 lyxerr << "LyXFont::setLyXSize: Unknown size `"
636 << s << '\'' << endl;
641 // Set size according to lyx format string
642 LyXFont::FONT_MISC_STATE LyXFont::setLyXMisc(string const & siz)
644 string const s = lowercase(siz);
646 while (s != LyXMiscNames[i] && LyXMiscNames[i] != "error") ++i;
647 if (s == LyXMiscNames[i])
648 return FONT_MISC_STATE(i);
649 lyxerr << "LyXFont::setLyXMisc: Unknown misc flag `"
650 << s << '\'' << endl;
655 /// Sets color after LyX text format
656 LyXFont & LyXFont::setLyXColor(string const & col)
658 setColor(lcolor.getFromLyXName(col));
663 // Returns size in latex format
664 string const LyXFont::latexSize() const
666 return LaTeXSizeNames[size()];
670 // Read a font definition from given file in lyx format
672 LyXFont & LyXFont::lyxRead(LyXLex & lex)
675 bool finished = false;
676 while (!finished && lex.isOK() && !error) {
678 string const tok = lowercase(lex.getString());
682 } else if (tok == "endfont") {
684 } else if (tok == "family") {
686 string const ttok = lex.getString();
688 } else if (tok == "series") {
690 string const ttok = lex.getString();
692 } else if (tok == "shape") {
694 string const ttok = lex.getString();
696 } else if (tok == "size") {
698 string const ttok = lex.getString();
700 } else if (tok == "misc") {
702 string const ttok = lowercase(lex.getString());
704 if (ttok == "no_bar") {
706 } else if (ttok == "no_emph") {
708 } else if (ttok == "no_noun") {
710 } else if (ttok == "emph") {
712 } else if (ttok == "underbar") {
714 } else if (ttok == "noun") {
717 lex.printError("Illegal misc type `$$Token´");
719 } else if (tok == "color") {
721 string const ttok = lex.getString();
724 lex.printError("Unknown tag `$$Token'");
732 /// Writes the changes from this font to orgfont in .lyx format in file
733 void LyXFont::lyxWriteChanges(LyXFont const & orgfont,
734 #ifdef INHERIT_LANGUAGE
735 Language const * doclang,
740 if (orgfont.family() != family()) {
741 os << "\\family " << LyXFamilyNames[family()] << " \n";
743 if (orgfont.series() != series()) {
744 os << "\\series " << LyXSeriesNames[series()] << " \n";
746 if (orgfont.shape() != shape()) {
747 os << "\\shape " << LyXShapeNames[shape()] << " \n";
749 if (orgfont.size() != size()) {
750 os << "\\size " << LyXSizeNames[size()] << " \n";
752 if (orgfont.emph() != emph()) {
753 os << "\\emph " << LyXMiscNames[emph()] << " \n";
755 if (orgfont.number() != number()) {
756 os << "\\numeric " << LyXMiscNames[number()] << " \n";
758 if (orgfont.underbar() != underbar()) {
759 // This is only for backwards compatibility
760 switch (underbar()) {
761 case OFF: os << "\\bar no \n"; break;
762 case ON: os << "\\bar under \n"; break;
763 case TOGGLE: lyxerr << "LyXFont::lyxWriteFontChanges: "
764 "TOGGLE should not appear here!"
767 case INHERIT: os << "\\bar default \n"; break;
768 case IGNORE: lyxerr << "LyXFont::lyxWriteFontChanges: "
769 "IGNORE should not appear here!"
774 if (orgfont.noun() != noun()) {
775 os << "\\noun " << LyXMiscNames[noun()] << " \n";
777 if (orgfont.color() != color()) {
778 // To make us file compatible with older
779 // lyx versions we emit "default" instead
781 string col_str(lcolor.getLyXName(color()));
782 if (col_str == "inherit") col_str = "default";
783 os << "\\color " << col_str << "\n";
785 if (orgfont.language() != language()) {
786 #ifndef INHERIT_LANGUAGE
789 if (language() == inherit_language)
790 os << "\\lang " << doclang->lang() << "\n";
793 os << "\\lang " << language()->lang() << "\n";
795 os << "\\lang unknown\n";
800 /// Writes the head of the LaTeX needed to impose this font
801 // Returns number of chars written.
802 int LyXFont::latexWriteStartChanges(ostream & os, LyXFont const & base,
803 LyXFont const & prev) const
808 if (language()->babel() != base.language()->babel() &&
809 language()->babel() != prev.language()->babel()) {
810 if (isRightToLeft() != prev.isRightToLeft()) {
811 if (isRightToLeft()) {
820 subst(lyxrc.language_command_local,
821 "$$lang", language()->babel());
823 count += tmp.length();
827 if (number() == ON && prev.number() != ON &&
828 language()->lang() == "hebrew") {
836 if (f.family() != INHERIT_FAMILY) {
838 << LaTeXFamilyNames[f.family()]
840 count += strlen(LaTeXFamilyNames[f.family()]) + 2;
841 env = true; //We have opened a new environment
843 if (f.series() != INHERIT_SERIES) {
845 << LaTeXSeriesNames[f.series()]
847 count += strlen(LaTeXSeriesNames[f.series()]) + 2;
848 env = true; //We have opened a new environment
850 if (f.shape() != INHERIT_SHAPE) {
852 << LaTeXShapeNames[f.shape()]
854 count += strlen(LaTeXShapeNames[f.shape()]) + 2;
855 env = true; //We have opened a new environment
857 if (f.color() != LColor::inherit && f.color() != LColor::ignore) {
859 << lcolor.getLaTeXName(f.color())
861 count += lcolor.getLaTeXName(f.color()).length() + 13;
862 env = true; //We have opened a new environment
864 if (f.emph() == ON) {
867 env = true; //We have opened a new environment
869 if (f.underbar() == ON) {
872 env = true; //We have opened a new environment
874 // \noun{} is a LyX special macro
875 if (f.noun() == ON) {
878 env = true; //We have opened a new environment
880 if (f.size() != INHERIT_SIZE) {
881 // If we didn't open an environment above, we open one here
887 << LaTeXSizeNames[f.size()]
889 count += strlen(LaTeXSizeNames[f.size()]) + 2;
895 /// Writes ending block of LaTeX needed to close use of this font
896 // Returns number of chars written
897 // This one corresponds to latexWriteStartChanges(). (Asger)
898 int LyXFont::latexWriteEndChanges(ostream & os, LyXFont const & base,
899 LyXFont const & next) const
904 LyXFont f = *this; // why do you need this?
905 f.reduce(base); // why isn't this just "reduce(base);" (Lgb)
906 // Because this function is const. Everything breaks if this
907 // method changes the font it represents. There is no speed penalty
908 // by using the temporary. (Asger)
911 if (f.family() != INHERIT_FAMILY) {
914 env = true; // Size change need not bother about closing env.
916 if (f.series() != INHERIT_SERIES) {
919 env = true; // Size change need not bother about closing env.
921 if (f.shape() != INHERIT_SHAPE) {
924 env = true; // Size change need not bother about closing env.
926 if (f.color() != LColor::inherit && f.color() != LColor::ignore) {
929 env = true; // Size change need not bother about closing env.
931 if (f.emph() == ON) {
934 env = true; // Size change need not bother about closing env.
936 if (f.underbar() == ON) {
939 env = true; // Size change need not bother about closing env.
941 if (f.noun() == ON) {
944 env = true; // Size change need not bother about closing env.
946 if (f.size() != INHERIT_SIZE) {
947 // We only have to close if only size changed
954 if (number() == ON && next.number() != ON &&
955 language()->lang() == "hebrew") {
960 if (language() != base.language() && language() != next.language()) {
969 LColor::color LyXFont::realColor() const
971 if (color() == LColor::none)
972 return LColor::foreground;
977 // Convert logical attributes to concrete shape attribute
978 LyXFont::FONT_SHAPE LyXFont::realShape() const
980 register FONT_SHAPE s = shape();
994 ostream & operator<<(ostream & o, LyXFont::FONT_MISC_STATE fms)
996 return o << int(fms);