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 : bits(inherit), lang(inherit_language)
164 LyXFont::LyXFont(LyXFont::FONT_INIT2)
165 : bits(ignore), lang(ignore_language)
169 LyXFont::LyXFont(LyXFont::FONT_INIT3)
170 : bits(sane), lang(default_language)
174 LyXFont::LyXFont(LyXFont::FONT_INIT1, Language const * l)
175 : bits(inherit), lang(l)
179 LyXFont::LyXFont(LyXFont::FONT_INIT2, Language const * l)
180 : bits(ignore), lang(l)
184 LyXFont::LyXFont(LyXFont::FONT_INIT3, Language const * l)
185 : bits(sane), lang(l)
189 LyXFont::FONT_MISC_STATE LyXFont::underbar() const
191 return bits.underbar;
195 LColor::color LyXFont::color() const
201 Language const * LyXFont::language() const
207 LyXFont::FONT_MISC_STATE LyXFont::number() const
213 bool LyXFont::isRightToLeft() const
215 return lang->RightToLeft();
219 bool LyXFont::isVisibleRightToLeft() const
221 return (lang->RightToLeft() &&
226 LyXFont & LyXFont::setFamily(LyXFont::FONT_FAMILY f)
233 LyXFont & LyXFont::setSeries(LyXFont::FONT_SERIES s)
240 LyXFont & LyXFont::setShape(LyXFont::FONT_SHAPE s)
247 LyXFont & LyXFont::setSize(LyXFont::FONT_SIZE s)
254 LyXFont & LyXFont::setEmph(LyXFont::FONT_MISC_STATE e)
261 LyXFont & LyXFont::setUnderbar(LyXFont::FONT_MISC_STATE u)
268 LyXFont & LyXFont::setNoun(LyXFont::FONT_MISC_STATE n)
275 LyXFont & LyXFont::setColor(LColor::color c)
282 LyXFont & LyXFont::setLanguage(Language const * l)
289 LyXFont & LyXFont::setNumber(LyXFont::FONT_MISC_STATE n)
296 /// Decreases font size by one
297 LyXFont & LyXFont::decSize()
300 case SIZE_HUGER: setSize(SIZE_HUGE); break;
301 case SIZE_HUGE: setSize(SIZE_LARGEST); break;
302 case SIZE_LARGEST: setSize(SIZE_LARGER); break;
303 case SIZE_LARGER: setSize(SIZE_LARGE); break;
304 case SIZE_LARGE: setSize(SIZE_NORMAL); break;
305 case SIZE_NORMAL: setSize(SIZE_SMALL); break;
306 case SIZE_SMALL: setSize(SIZE_FOOTNOTE); break;
307 case SIZE_FOOTNOTE: setSize(SIZE_SCRIPT); break;
308 case SIZE_SCRIPT: setSize(SIZE_TINY); break;
309 case SIZE_TINY: break;
311 lyxerr << "Can't LyXFont::decSize on INCREASE_SIZE" << endl;
314 lyxerr <<"Can't LyXFont::decSize on DECREASE_SIZE" << endl;
317 lyxerr <<"Can't LyXFont::decSize on INHERIT_SIZE" << endl;
320 lyxerr <<"Can't LyXFont::decSize on IGNORE_SIZE" << endl;
327 /// Increases font size by one
328 LyXFont & LyXFont::incSize()
331 case SIZE_HUGER: break;
332 case SIZE_HUGE: setSize(SIZE_HUGER); break;
333 case SIZE_LARGEST: setSize(SIZE_HUGE); break;
334 case SIZE_LARGER: setSize(SIZE_LARGEST); break;
335 case SIZE_LARGE: setSize(SIZE_LARGER); break;
336 case SIZE_NORMAL: setSize(SIZE_LARGE); break;
337 case SIZE_SMALL: setSize(SIZE_NORMAL); break;
338 case SIZE_FOOTNOTE: setSize(SIZE_SMALL); break;
339 case SIZE_SCRIPT: setSize(SIZE_FOOTNOTE); break;
340 case SIZE_TINY: setSize(SIZE_SCRIPT); break;
342 lyxerr <<"Can't LyXFont::incSize on INCREASE_SIZE" << endl;
345 lyxerr <<"Can't LyXFont::incSize on DECREASE_SIZE" << endl;
348 lyxerr <<"Can't LyXFont::incSize on INHERIT_SIZE" << endl;
351 lyxerr <<"Can't LyXFont::incSize on IGNORE_SIZE" << endl;
358 /// Updates a misc setting according to request
359 LyXFont::FONT_MISC_STATE LyXFont::setMisc(FONT_MISC_STATE newfont,
362 if (newfont == TOGGLE) {
368 lyxerr <<"LyXFont::setMisc: Need state"
369 " ON or OFF to toggle. Setting to ON" << endl;
372 } else if (newfont == IGNORE)
379 /// Updates font settings according to request
380 void LyXFont::update(LyXFont const & newfont, bool toggleall)
382 if (newfont.family() == family() && toggleall)
383 setFamily(INHERIT_FAMILY); // toggle 'back'
384 else if (newfont.family() != IGNORE_FAMILY)
385 setFamily(newfont.family());
386 // else it's IGNORE_SHAPE
388 // "Old" behaviour: "Setting" bold will toggle bold on/off.
389 switch (newfont.series()) {
392 if (series() == BOLD_SERIES && toggleall)
393 setSeries(MEDIUM_SERIES);
395 setSeries(BOLD_SERIES);
399 setSeries(newfont.series());
405 if (newfont.shape() == shape() && toggleall)
406 setShape(INHERIT_SHAPE); // toggle 'back'
407 else if (newfont.shape() != IGNORE_SHAPE)
408 setShape(newfont.shape());
409 // else it's IGNORE_SHAPE
411 if (newfont.size() != IGNORE_SIZE) {
412 if (newfont.size() == INCREASE_SIZE)
414 else if (newfont.size() == DECREASE_SIZE)
417 setSize(newfont.size());
420 setEmph(setMisc(newfont.emph(), emph()));
421 setUnderbar(setMisc(newfont.underbar(), underbar()));
422 setNoun(setMisc(newfont.noun(), noun()));
424 setNumber(setMisc(newfont.number(), number()));
425 if (newfont.language() == language() && toggleall)
426 setLanguage(inherit_language);
427 else if (newfont.language() != ignore_language)
428 setLanguage(newfont.language());
430 if (newfont.color() == color() && toggleall)
431 setColor(LColor::inherit); // toggle 'back'
432 else if (newfont.color() != LColor::ignore)
433 setColor(newfont.color());
437 /// Reduce font to fall back to template where possible
438 void LyXFont::reduce(LyXFont const & tmplt)
440 if (family() == tmplt.family())
441 setFamily(INHERIT_FAMILY);
442 if (series() == tmplt.series())
443 setSeries(INHERIT_SERIES);
444 if (shape() == tmplt.shape())
445 setShape(INHERIT_SHAPE);
446 if (size() == tmplt.size())
447 setSize(INHERIT_SIZE);
448 if (emph() == tmplt.emph())
450 if (underbar() == tmplt.underbar())
451 setUnderbar(INHERIT);
452 if (noun() == tmplt.noun())
454 if (color() == tmplt.color())
455 setColor(LColor::inherit);
456 if (language() == tmplt.language())
457 setLanguage(inherit_language);
461 /// Realize font from a template
462 LyXFont & LyXFont::realize(LyXFont const & tmplt, Language const * deflang)
464 if (language() == inherit_language) {
465 if (tmplt.language() == inherit_language ||
466 tmplt.language() == ignore_language ||
467 tmplt.language() == default_language)
469 setLanguage(deflang);
471 setLanguage(tmplt.language());
474 if (bits == inherit) {
479 if (bits.family == INHERIT_FAMILY) {
480 bits.family = tmplt.bits.family;
482 if (bits.series == INHERIT_SERIES) {
483 bits.series = tmplt.bits.series;
485 if (bits.shape == INHERIT_SHAPE) {
486 bits.shape = tmplt.bits.shape;
488 if (bits.size == INHERIT_SIZE) {
489 bits.size = tmplt.bits.size;
491 if (bits.emph == INHERIT) {
492 bits.emph = tmplt.bits.emph;
494 if (bits.underbar == INHERIT) {
495 bits.underbar = tmplt.bits.underbar;
497 if (bits.noun == INHERIT) {
498 bits.noun = tmplt.bits.noun;
500 if (bits.color == LColor::inherit) {
501 bits.color = tmplt.bits.color;
507 /// Is font resolved?
508 bool LyXFont::resolved() const
510 return (family() != INHERIT_FAMILY && series() != INHERIT_SERIES &&
511 shape() != INHERIT_SHAPE && size() != INHERIT_SIZE &&
512 emph() != INHERIT && underbar() != INHERIT &&
514 color() != LColor::inherit &&
515 language() != inherit_language);
519 /// Build GUI description of font state
520 string const LyXFont::stateText(BufferParams * params) const
523 if (family() != INHERIT_FAMILY)
524 ost << _(GUIFamilyNames[family()]) << ", ";
525 if (series() != INHERIT_SERIES)
526 ost << _(GUISeriesNames[series()]) << ", ";
527 if (shape() != INHERIT_SHAPE)
528 ost << _(GUIShapeNames[shape()]) << ", ";
529 if (size() != INHERIT_SIZE)
530 ost << _(GUISizeNames[size()]) << ", ";
531 if (color() != LColor::inherit)
532 ost << lcolor.getGUIName(color()) << ", ";
533 if (emph() != INHERIT)
534 ost << _("Emphasis ")
535 << _(GUIMiscNames[emph()]) << ", ";
536 if (underbar() != INHERIT)
537 ost << _("Underline ")
538 << _(GUIMiscNames[underbar()]) << ", ";
539 if (noun() != INHERIT)
540 ost << _("Noun ") << _(GUIMiscNames[noun()]) << ", ";
542 ost << _("Default") << ", ";
543 if (!params || (language() != params->language))
544 ost << _("Language: ") << _(language()->display()) << ", ";
546 ost << _(" Number ") << _(GUIMiscNames[number()]);
548 string buf(ost.str().c_str());
549 buf = strip(buf, ' ');
550 buf = strip(buf, ',');
555 // Set family according to lyx format string
556 LyXFont & LyXFont::setLyXFamily(string const & fam)
558 string const s = lowercase(fam);
561 while (s != LyXFamilyNames[i] && LyXFamilyNames[i] != "error") ++i;
562 if (s == LyXFamilyNames[i]) {
563 setFamily(LyXFont::FONT_FAMILY(i));
565 lyxerr << "LyXFont::setLyXFamily: Unknown family `"
566 << s << '\'' << endl;
571 // Set series according to lyx format string
572 LyXFont & LyXFont::setLyXSeries(string const & ser)
574 string const s = lowercase(ser);
577 while (s != LyXSeriesNames[i] && LyXSeriesNames[i] != "error") ++i;
578 if (s == LyXSeriesNames[i]) {
579 setSeries(LyXFont::FONT_SERIES(i));
581 lyxerr << "LyXFont::setLyXSeries: Unknown series `"
582 << s << '\'' << endl;
587 // Set shape according to lyx format string
588 LyXFont & LyXFont::setLyXShape(string const & sha)
590 string const s = lowercase(sha);
593 while (s != LyXShapeNames[i] && LyXShapeNames[i] != "error") ++i;
594 if (s == LyXShapeNames[i]) {
595 setShape(LyXFont::FONT_SHAPE(i));
597 lyxerr << "LyXFont::setLyXShape: Unknown shape `"
598 << s << '\'' << endl;
603 // Set size according to lyx format string
604 LyXFont & LyXFont::setLyXSize(string const & siz)
606 string const s = lowercase(siz);
608 while (s != LyXSizeNames[i] && LyXSizeNames[i] != "error") ++i;
609 if (s == LyXSizeNames[i]) {
610 setSize(LyXFont::FONT_SIZE(i));
612 lyxerr << "LyXFont::setLyXSize: Unknown size `"
613 << s << '\'' << endl;
618 // Set size according to lyx format string
619 LyXFont::FONT_MISC_STATE LyXFont::setLyXMisc(string const & siz)
621 string const s = lowercase(siz);
623 while (s != LyXMiscNames[i] && LyXMiscNames[i] != "error") ++i;
624 if (s == LyXMiscNames[i])
625 return FONT_MISC_STATE(i);
626 lyxerr << "LyXFont::setLyXMisc: Unknown misc flag `"
627 << s << '\'' << endl;
632 /// Sets color after LyX text format
633 LyXFont & LyXFont::setLyXColor(string const & col)
635 setColor(lcolor.getFromLyXName(col));
640 // Returns size in latex format
641 string const LyXFont::latexSize() const
643 return LaTeXSizeNames[size()];
647 // Read a font definition from given file in lyx format
649 LyXFont & LyXFont::lyxRead(LyXLex & lex)
652 bool finished = false;
653 while (!finished && lex.isOK() && !error) {
655 string const tok = lowercase(lex.getString());
659 } else if (tok == "endfont") {
661 } else if (tok == "family") {
663 string const ttok = lex.getString();
665 } else if (tok == "series") {
667 string const ttok = lex.getString();
669 } else if (tok == "shape") {
671 string const ttok = lex.getString();
673 } else if (tok == "size") {
675 string const ttok = lex.getString();
677 } else if (tok == "misc") {
679 string const ttok = lowercase(lex.getString());
681 if (ttok == "no_bar") {
683 } else if (ttok == "no_emph") {
685 } else if (ttok == "no_noun") {
687 } else if (ttok == "emph") {
689 } else if (ttok == "underbar") {
691 } else if (ttok == "noun") {
694 lex.printError("Illegal misc type `$$Token´");
696 } else if (tok == "color") {
698 string const ttok = lex.getString();
701 lex.printError("Unknown tag `$$Token'");
709 /// Writes the changes from this font to orgfont in .lyx format in file
710 void LyXFont::lyxWriteChanges(LyXFont const & orgfont,
711 Language const * doclang, ostream & os) const
714 if (orgfont.family() != family()) {
715 os << "\\family " << LyXFamilyNames[family()] << " \n";
717 if (orgfont.series() != series()) {
718 os << "\\series " << LyXSeriesNames[series()] << " \n";
720 if (orgfont.shape() != shape()) {
721 os << "\\shape " << LyXShapeNames[shape()] << " \n";
723 if (orgfont.size() != size()) {
724 os << "\\size " << LyXSizeNames[size()] << " \n";
726 if (orgfont.emph() != emph()) {
727 os << "\\emph " << LyXMiscNames[emph()] << " \n";
729 if (orgfont.number() != number()) {
730 os << "\\numeric " << LyXMiscNames[number()] << " \n";
732 if (orgfont.underbar() != underbar()) {
733 // This is only for backwards compatibility
734 switch (underbar()) {
735 case OFF: os << "\\bar no \n"; break;
736 case ON: os << "\\bar under \n"; break;
737 case TOGGLE: lyxerr << "LyXFont::lyxWriteFontChanges: "
738 "TOGGLE should not appear here!"
741 case INHERIT: os << "\\bar default \n"; break;
742 case IGNORE: lyxerr << "LyXFont::lyxWriteFontChanges: "
743 "IGNORE should not appear here!"
748 if (orgfont.noun() != noun()) {
749 os << "\\noun " << LyXMiscNames[noun()] << " \n";
751 if (orgfont.color() != color()) {
752 // To make us file compatible with older
753 // lyx versions we emit "default" instead
755 string col_str(lcolor.getLyXName(color()));
756 if (col_str == "inherit") col_str = "default";
757 os << "\\color " << col_str << "\n";
759 if (orgfont.language() != language()) {
760 if (language() == inherit_language)
761 os << "\\lang " << doclang->lang() << "\n";
763 os << "\\lang " << language()->lang() << "\n";
765 os << "\\lang unknown\n";
770 /// Writes the head of the LaTeX needed to impose this font
771 // Returns number of chars written.
772 int LyXFont::latexWriteStartChanges(ostream & os, LyXFont const & base,
773 LyXFont const & prev) const
778 if (language()->babel() != base.language()->babel() &&
779 language()->babel() != prev.language()->babel()) {
780 if (isRightToLeft() != prev.isRightToLeft()) {
781 if (isRightToLeft()) {
790 subst(lyxrc.language_command_local,
791 "$$lang", language()->babel());
793 count += tmp.length();
797 if (number() == ON && prev.number() != ON &&
798 language()->lang() == "hebrew") {
806 if (f.family() != INHERIT_FAMILY) {
808 << LaTeXFamilyNames[f.family()]
810 count += strlen(LaTeXFamilyNames[f.family()]) + 2;
811 env = true; //We have opened a new environment
813 if (f.series() != INHERIT_SERIES) {
815 << LaTeXSeriesNames[f.series()]
817 count += strlen(LaTeXSeriesNames[f.series()]) + 2;
818 env = true; //We have opened a new environment
820 if (f.shape() != INHERIT_SHAPE) {
822 << LaTeXShapeNames[f.shape()]
824 count += strlen(LaTeXShapeNames[f.shape()]) + 2;
825 env = true; //We have opened a new environment
827 if (f.color() != LColor::inherit && f.color() != LColor::ignore) {
829 << lcolor.getLaTeXName(f.color())
831 count += lcolor.getLaTeXName(f.color()).length() + 13;
832 env = true; //We have opened a new environment
834 if (f.emph() == ON) {
837 env = true; //We have opened a new environment
839 if (f.underbar() == ON) {
842 env = true; //We have opened a new environment
844 // \noun{} is a LyX special macro
845 if (f.noun() == ON) {
848 env = true; //We have opened a new environment
850 if (f.size() != INHERIT_SIZE) {
851 // If we didn't open an environment above, we open one here
857 << LaTeXSizeNames[f.size()]
859 count += strlen(LaTeXSizeNames[f.size()]) + 2;
865 /// Writes ending block of LaTeX needed to close use of this font
866 // Returns number of chars written
867 // This one corresponds to latexWriteStartChanges(). (Asger)
868 int LyXFont::latexWriteEndChanges(ostream & os, LyXFont const & base,
869 LyXFont const & next) const
874 LyXFont f = *this; // why do you need this?
875 f.reduce(base); // why isn't this just "reduce(base);" (Lgb)
876 // Because this function is const. Everything breaks if this
877 // method changes the font it represents. There is no speed penalty
878 // by using the temporary. (Asger)
881 if (f.family() != INHERIT_FAMILY) {
884 env = true; // Size change need not bother about closing env.
886 if (f.series() != INHERIT_SERIES) {
889 env = true; // Size change need not bother about closing env.
891 if (f.shape() != INHERIT_SHAPE) {
894 env = true; // Size change need not bother about closing env.
896 if (f.color() != LColor::inherit && f.color() != LColor::ignore) {
899 env = true; // Size change need not bother about closing env.
901 if (f.emph() == ON) {
904 env = true; // Size change need not bother about closing env.
906 if (f.underbar() == ON) {
909 env = true; // Size change need not bother about closing env.
911 if (f.noun() == ON) {
914 env = true; // Size change need not bother about closing env.
916 if (f.size() != INHERIT_SIZE) {
917 // We only have to close if only size changed
924 if (number() == ON && next.number() != ON &&
925 language()->lang() == "hebrew") {
930 if (language() != base.language() && language() != next.language()) {
939 LColor::color LyXFont::realColor() const
941 if (color() == LColor::none)
942 return LColor::foreground;
947 // Convert logical attributes to concrete shape attribute
948 LyXFont::FONT_SHAPE LyXFont::realShape() const
950 register FONT_SHAPE s = shape();
964 ostream & operator<<(ostream & o, LyXFont::FONT_MISC_STATE fms)
966 return o << int(fms);