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 * ====================================================== */
22 #include "support/lstrings.h"
23 #include "bufferparams.h" // stateText
28 #ifndef CXX_GLOBAL_CSTD
38 char const * GUIFamilyNames[LyXFont::NUM_FAMILIES + 2 /* default & error */] =
39 { N_("Roman"), N_("Sans Serif"), N_("Typewriter"), N_("Symbol"),
40 "cmr", "cmsy", "cmm", "cmex", "msa", "msb", "eufrak", "wasy",
41 N_("Inherit"), N_("Ignore") };
43 char const * GUISeriesNames[4] =
44 { N_("Medium"), N_("Bold"), N_("Inherit"), N_("Ignore") };
46 char const * GUIShapeNames[6] =
47 { N_("Upright"), N_("Italic"), N_("Slanted"), N_("Smallcaps"), N_("Inherit"),
50 char const * GUISizeNames[14] =
51 { N_("Tiny"), N_("Smallest"), N_("Smaller"), N_("Small"), N_("Normal"), N_("Large"),
52 N_("Larger"), N_("Largest"), N_("Huge"), N_("Huger"), N_("Increase"), N_("Decrease"),
53 N_("Inherit"), N_("Ignore") };
55 char const * GUIMiscNames[5] =
56 { N_("Off"), N_("On"), N_("Toggle"), N_("Inherit"), N_("Ignore") };
60 // Strings used to read and write .lyx format files
62 char const * LyXFamilyNames[LyXFont::NUM_FAMILIES + 2 /* default & error */] =
63 { "roman", "sans", "typewriter", "symbol",
64 "cmr", "cmsy", "cmm", "cmex", "msa", "msb", "eufrak", "wasy",
67 char const * LyXSeriesNames[4] =
68 { "medium", "bold", "default", "error" };
70 char const * LyXShapeNames[6] =
71 { "up", "italic", "slanted", "smallcaps", "default", "error" };
73 char const * LyXSizeNames[14] =
74 { "tiny", "scriptsize", "footnotesize", "small", "normal", "large",
75 "larger", "largest", "huge", "giant",
76 "increase", "decrease", "default", "error" };
78 char const * LyXMiscNames[5] =
79 { "off", "on", "toggle", "default", "error" };
82 // Strings used to write LaTeX files
84 char const * LaTeXFamilyNames[6] =
85 { "textrm", "textsf", "texttt", "error1", "error2", "error3" };
87 char const * LaTeXSeriesNames[4] =
88 { "textmd", "textbf", "error4", "error5" };
90 char const * LaTeXShapeNames[6] =
91 { "textup", "textit", "textsl", "textsc", "error6", "error7" };
93 char const * LaTeXSizeNames[14] =
94 { "tiny", "scriptsize", "footnotesize", "small", "normalsize", "large",
95 "Large", "LARGE", "huge", "Huge", "error8", "error9", "error10", "error11" };
100 // Initialize static member
101 LyXFont::FontBits LyXFont::sane = {
112 // Initialize static member
113 LyXFont::FontBits LyXFont::inherit = {
124 // Initialize static member
125 LyXFont::FontBits LyXFont::ignore = {
137 bool operator==(LyXFont::FontBits const & lhs,
138 LyXFont::FontBits const & rhs)
140 return lhs.family == rhs.family &&
141 lhs.series == rhs.series &&
142 lhs.shape == rhs.shape &&
143 lhs.size == rhs.size &&
144 lhs.color == rhs.color &&
145 lhs.emph == rhs.emph &&
146 lhs.underbar == rhs.underbar &&
147 lhs.noun == rhs.noun &&
148 lhs.number == rhs.number;
153 : bits(sane), lang(default_language)
157 LyXFont::LyXFont(LyXFont::FONT_INIT1)
158 : bits(inherit), lang(default_language)
162 LyXFont::LyXFont(LyXFont::FONT_INIT2)
163 : bits(ignore), lang(ignore_language)
167 LyXFont::LyXFont(LyXFont::FONT_INIT3)
168 : bits(sane), lang(default_language)
172 LyXFont::LyXFont(LyXFont::FONT_INIT1, Language const * l)
173 : bits(inherit), lang(l)
177 LyXFont::LyXFont(LyXFont::FONT_INIT2, Language const * l)
178 : bits(ignore), lang(l)
182 LyXFont::LyXFont(LyXFont::FONT_INIT3, Language const * l)
183 : bits(sane), lang(l)
187 LyXFont::FONT_MISC_STATE LyXFont::underbar() const
189 return bits.underbar;
193 LColor::color LyXFont::color() const
199 Language const * LyXFont::language() const
205 LyXFont::FONT_MISC_STATE LyXFont::number() const
211 bool LyXFont::isRightToLeft() const
213 return lang->RightToLeft();
217 bool LyXFont::isVisibleRightToLeft() const
219 return (lang->RightToLeft() &&
224 LyXFont & LyXFont::setFamily(LyXFont::FONT_FAMILY f)
231 LyXFont & LyXFont::setSeries(LyXFont::FONT_SERIES s)
238 LyXFont & LyXFont::setShape(LyXFont::FONT_SHAPE s)
245 LyXFont & LyXFont::setSize(LyXFont::FONT_SIZE s)
252 LyXFont & LyXFont::setEmph(LyXFont::FONT_MISC_STATE e)
259 LyXFont & LyXFont::setUnderbar(LyXFont::FONT_MISC_STATE u)
266 LyXFont & LyXFont::setNoun(LyXFont::FONT_MISC_STATE n)
273 LyXFont & LyXFont::setColor(LColor::color c)
280 LyXFont & LyXFont::setLanguage(Language const * l)
287 LyXFont & LyXFont::setNumber(LyXFont::FONT_MISC_STATE n)
294 /// Decreases font size by one
295 LyXFont & LyXFont::decSize()
298 case SIZE_HUGER: setSize(SIZE_HUGE); break;
299 case SIZE_HUGE: setSize(SIZE_LARGEST); break;
300 case SIZE_LARGEST: setSize(SIZE_LARGER); break;
301 case SIZE_LARGER: setSize(SIZE_LARGE); break;
302 case SIZE_LARGE: setSize(SIZE_NORMAL); break;
303 case SIZE_NORMAL: setSize(SIZE_SMALL); break;
304 case SIZE_SMALL: setSize(SIZE_FOOTNOTE); break;
305 case SIZE_FOOTNOTE: setSize(SIZE_SCRIPT); break;
306 case SIZE_SCRIPT: setSize(SIZE_TINY); break;
307 case SIZE_TINY: break;
309 lyxerr << "Can't LyXFont::decSize on INCREASE_SIZE" << endl;
312 lyxerr <<"Can't LyXFont::decSize on DECREASE_SIZE" << endl;
315 lyxerr <<"Can't LyXFont::decSize on INHERIT_SIZE" << endl;
318 lyxerr <<"Can't LyXFont::decSize on IGNORE_SIZE" << endl;
325 /// Increases font size by one
326 LyXFont & LyXFont::incSize()
329 case SIZE_HUGER: break;
330 case SIZE_HUGE: setSize(SIZE_HUGER); break;
331 case SIZE_LARGEST: setSize(SIZE_HUGE); break;
332 case SIZE_LARGER: setSize(SIZE_LARGEST); break;
333 case SIZE_LARGE: setSize(SIZE_LARGER); break;
334 case SIZE_NORMAL: setSize(SIZE_LARGE); break;
335 case SIZE_SMALL: setSize(SIZE_NORMAL); break;
336 case SIZE_FOOTNOTE: setSize(SIZE_SMALL); break;
337 case SIZE_SCRIPT: setSize(SIZE_FOOTNOTE); break;
338 case SIZE_TINY: setSize(SIZE_SCRIPT); break;
340 lyxerr <<"Can't LyXFont::incSize on INCREASE_SIZE" << endl;
343 lyxerr <<"Can't LyXFont::incSize on DECREASE_SIZE" << endl;
346 lyxerr <<"Can't LyXFont::incSize on INHERIT_SIZE" << endl;
349 lyxerr <<"Can't LyXFont::incSize on IGNORE_SIZE" << endl;
356 /// Updates a misc setting according to request
357 LyXFont::FONT_MISC_STATE LyXFont::setMisc(FONT_MISC_STATE newfont,
360 if (newfont == TOGGLE) {
366 lyxerr <<"LyXFont::setMisc: Need state"
367 " ON or OFF to toggle. Setting to ON" << endl;
370 } else if (newfont == IGNORE)
377 /// Updates font settings according to request
378 void LyXFont::update(LyXFont const & newfont,
379 Language const * document_language,
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 if (language() == document_language)
427 setLanguage(default_language);
429 setLanguage(document_language);
430 else if (newfont.language() != ignore_language)
431 setLanguage(newfont.language());
433 if (newfont.color() == color() && toggleall)
434 setColor(LColor::inherit); // toggle 'back'
435 else if (newfont.color() != LColor::ignore)
436 setColor(newfont.color());
440 /// Reduce font to fall back to template where possible
441 void LyXFont::reduce(LyXFont const & tmplt)
443 if (family() == tmplt.family())
444 setFamily(INHERIT_FAMILY);
445 if (series() == tmplt.series())
446 setSeries(INHERIT_SERIES);
447 if (shape() == tmplt.shape())
448 setShape(INHERIT_SHAPE);
449 if (size() == tmplt.size())
450 setSize(INHERIT_SIZE);
451 if (emph() == tmplt.emph())
453 if (underbar() == tmplt.underbar())
454 setUnderbar(INHERIT);
455 if (noun() == tmplt.noun())
457 if (color() == tmplt.color())
458 setColor(LColor::inherit);
462 /// Realize font from a template
463 LyXFont & LyXFont::realize(LyXFont const & tmplt)
465 if (bits == inherit) {
470 if (bits.family == INHERIT_FAMILY) {
471 bits.family = tmplt.bits.family;
473 if (bits.series == INHERIT_SERIES) {
474 bits.series = tmplt.bits.series;
476 if (bits.shape == INHERIT_SHAPE) {
477 bits.shape = tmplt.bits.shape;
479 if (bits.size == INHERIT_SIZE) {
480 bits.size = tmplt.bits.size;
482 if (bits.emph == INHERIT) {
483 bits.emph = tmplt.bits.emph;
485 if (bits.underbar == INHERIT) {
486 bits.underbar = tmplt.bits.underbar;
488 if (bits.noun == INHERIT) {
489 bits.noun = tmplt.bits.noun;
491 if (bits.color == LColor::inherit) {
492 bits.color = tmplt.bits.color;
498 /// Is font resolved?
499 bool LyXFont::resolved() const
501 return (family() != INHERIT_FAMILY && series() != INHERIT_SERIES &&
502 shape() != INHERIT_SHAPE && size() != INHERIT_SIZE &&
503 emph() != INHERIT && underbar() != INHERIT &&
505 color() != LColor::inherit);
509 /// Build GUI description of font state
510 string const LyXFont::stateText(BufferParams * params) const
513 if (family() != INHERIT_FAMILY)
514 os << _(GUIFamilyNames[family()]) << ", ";
515 if (series() != INHERIT_SERIES)
516 os << _(GUISeriesNames[series()]) << ", ";
517 if (shape() != INHERIT_SHAPE)
518 os << _(GUIShapeNames[shape()]) << ", ";
519 if (size() != INHERIT_SIZE)
520 os << _(GUISizeNames[size()]) << ", ";
521 if (color() != LColor::inherit)
522 os << lcolor.getGUIName(color()) << ", ";
523 if (emph() != INHERIT)
524 os << bformat(_("Emphasis %1$s, "), _(GUIMiscNames[emph()]));
525 if (underbar() != INHERIT)
526 os << bformat(_("Underline %1$s, "), _(GUIMiscNames[underbar()]));
527 if (noun() != INHERIT)
528 os << bformat(_("Noun %1$s, "), _(GUIMiscNames[noun()]));
530 os << _("Default") << ", ";
531 if (!params || (language() != params->language))
532 os << bformat(_("Language: %1$s, "), _(language()->display()));
534 os << bformat(_(" Number %1$s"), _(GUIMiscNames[number()]));
535 return rtrim(STRCONV(os.str()), ", ");
539 // Set family according to lyx format string
540 LyXFont & LyXFont::setLyXFamily(string const & fam)
542 string const s = ascii_lowercase(fam);
545 while (s != LyXFamilyNames[i] && LyXFamilyNames[i] != "error")
547 if (s == LyXFamilyNames[i])
548 setFamily(LyXFont::FONT_FAMILY(i));
550 lyxerr << "LyXFont::setLyXFamily: Unknown family `"
551 << s << '\'' << endl;
556 // Set series according to lyx format string
557 LyXFont & LyXFont::setLyXSeries(string const & ser)
559 string const s = ascii_lowercase(ser);
562 while (s != LyXSeriesNames[i] && LyXSeriesNames[i] != "error") ++i;
563 if (s == LyXSeriesNames[i]) {
564 setSeries(LyXFont::FONT_SERIES(i));
566 lyxerr << "LyXFont::setLyXSeries: Unknown series `"
567 << s << '\'' << endl;
572 // Set shape according to lyx format string
573 LyXFont & LyXFont::setLyXShape(string const & sha)
575 string const s = ascii_lowercase(sha);
578 while (s != LyXShapeNames[i] && LyXShapeNames[i] != "error") ++i;
579 if (s == LyXShapeNames[i]) {
580 setShape(LyXFont::FONT_SHAPE(i));
582 lyxerr << "LyXFont::setLyXShape: Unknown shape `"
583 << s << '\'' << endl;
588 // Set size according to lyx format string
589 LyXFont & LyXFont::setLyXSize(string const & siz)
591 string const s = ascii_lowercase(siz);
593 while (s != LyXSizeNames[i] && LyXSizeNames[i] != "error") ++i;
594 if (s == LyXSizeNames[i]) {
595 setSize(LyXFont::FONT_SIZE(i));
597 lyxerr << "LyXFont::setLyXSize: Unknown size `"
598 << s << '\'' << endl;
603 // Set size according to lyx format string
604 LyXFont::FONT_MISC_STATE LyXFont::setLyXMisc(string const & siz)
606 string const s = ascii_lowercase(siz);
608 while (s != LyXMiscNames[i] && LyXMiscNames[i] != "error") ++i;
609 if (s == LyXMiscNames[i])
610 return FONT_MISC_STATE(i);
611 lyxerr << "LyXFont::setLyXMisc: Unknown misc flag `"
612 << s << '\'' << endl;
617 /// Sets color after LyX text format
618 LyXFont & LyXFont::setLyXColor(string const & col)
620 setColor(lcolor.getFromLyXName(col));
625 // Returns size in latex format
626 string const LyXFont::latexSize() const
628 return LaTeXSizeNames[size()];
632 // Read a font definition from given file in lyx format
634 LyXFont & LyXFont::lyxRead(LyXLex & lex)
637 bool finished = false;
638 while (!finished && lex.isOK() && !error) {
640 string const tok = ascii_lowercase(lex.getString());
644 } else if (tok == "endfont") {
646 } else if (tok == "family") {
648 string const ttok = lex.getString();
650 } else if (tok == "series") {
652 string const ttok = lex.getString();
654 } else if (tok == "shape") {
656 string const ttok = lex.getString();
658 } else if (tok == "size") {
660 string const ttok = lex.getString();
662 } else if (tok == "misc") {
664 string const ttok = ascii_lowercase(lex.getString());
666 if (ttok == "no_bar") {
668 } else if (ttok == "no_emph") {
670 } else if (ttok == "no_noun") {
672 } else if (ttok == "emph") {
674 } else if (ttok == "underbar") {
676 } else if (ttok == "noun") {
679 lex.printError("Illegal misc type `$$Token´");
681 } else if (tok == "color") {
683 string const ttok = lex.getString();
686 lex.printError("Unknown tag `$$Token'");
694 /// Writes the changes from this font to orgfont in .lyx format in file
695 void LyXFont::lyxWriteChanges(LyXFont const & orgfont,
699 if (orgfont.family() != family()) {
700 os << "\\family " << LyXFamilyNames[family()] << " \n";
702 if (orgfont.series() != series()) {
703 os << "\\series " << LyXSeriesNames[series()] << " \n";
705 if (orgfont.shape() != shape()) {
706 os << "\\shape " << LyXShapeNames[shape()] << " \n";
708 if (orgfont.size() != size()) {
709 os << "\\size " << LyXSizeNames[size()] << " \n";
711 if (orgfont.emph() != emph()) {
712 os << "\\emph " << LyXMiscNames[emph()] << " \n";
714 if (orgfont.number() != number()) {
715 os << "\\numeric " << LyXMiscNames[number()] << " \n";
717 if (orgfont.underbar() != underbar()) {
718 // This is only for backwards compatibility
719 switch (underbar()) {
720 case OFF: os << "\\bar no \n"; break;
721 case ON: os << "\\bar under \n"; break;
722 case TOGGLE: lyxerr << "LyXFont::lyxWriteFontChanges: "
723 "TOGGLE should not appear here!"
726 case INHERIT: os << "\\bar default \n"; break;
727 case IGNORE: lyxerr << "LyXFont::lyxWriteFontChanges: "
728 "IGNORE should not appear here!"
733 if (orgfont.noun() != noun()) {
734 os << "\\noun " << LyXMiscNames[noun()] << " \n";
736 if (orgfont.color() != color()) {
737 // To make us file compatible with older
738 // lyx versions we emit "default" instead
740 string col_str(lcolor.getLyXName(color()));
741 if (col_str == "inherit") col_str = "default";
742 os << "\\color " << col_str << "\n";
744 if (orgfont.language() != language()) {
746 os << "\\lang " << language()->lang() << "\n";
748 os << "\\lang unknown\n";
753 /// Writes the head of the LaTeX needed to impose this font
754 // Returns number of chars written.
755 int LyXFont::latexWriteStartChanges(ostream & os, LyXFont const & base,
756 LyXFont const & prev) const
761 if (language()->babel() != base.language()->babel() &&
762 language() != prev.language()) {
763 if (isRightToLeft() != prev.isRightToLeft()) {
764 if (isRightToLeft()) {
773 subst(lyxrc.language_command_local,
774 "$$lang", language()->babel());
776 count += tmp.length();
780 if (number() == ON && prev.number() != ON &&
781 language()->lang() == "hebrew") {
789 if (f.family() != INHERIT_FAMILY) {
791 << LaTeXFamilyNames[f.family()]
793 count += strlen(LaTeXFamilyNames[f.family()]) + 2;
794 env = true; //We have opened a new environment
796 if (f.series() != INHERIT_SERIES) {
798 << LaTeXSeriesNames[f.series()]
800 count += strlen(LaTeXSeriesNames[f.series()]) + 2;
801 env = true; //We have opened a new environment
803 if (f.shape() != INHERIT_SHAPE) {
805 << LaTeXShapeNames[f.shape()]
807 count += strlen(LaTeXShapeNames[f.shape()]) + 2;
808 env = true; //We have opened a new environment
810 if (f.color() != LColor::inherit && f.color() != LColor::ignore) {
812 << lcolor.getLaTeXName(f.color())
814 count += lcolor.getLaTeXName(f.color()).length() + 13;
815 env = true; //We have opened a new environment
817 if (f.emph() == ON) {
820 env = true; //We have opened a new environment
822 if (f.underbar() == ON) {
825 env = true; //We have opened a new environment
827 // \noun{} is a LyX special macro
828 if (f.noun() == ON) {
831 env = true; //We have opened a new environment
833 if (f.size() != INHERIT_SIZE) {
834 // If we didn't open an environment above, we open one here
840 << LaTeXSizeNames[f.size()]
842 count += strlen(LaTeXSizeNames[f.size()]) + 2;
848 /// Writes ending block of LaTeX needed to close use of this font
849 // Returns number of chars written
850 // This one corresponds to latexWriteStartChanges(). (Asger)
851 int LyXFont::latexWriteEndChanges(ostream & os, LyXFont const & base,
852 LyXFont const & next) const
857 // reduce the current font to changes against the base
858 // font (of the layout). We use a temporary for this to
859 // avoid changing this font instance, as that would break
863 if (f.family() != INHERIT_FAMILY) {
866 env = true; // Size change need not bother about closing env.
868 if (f.series() != INHERIT_SERIES) {
871 env = true; // Size change need not bother about closing env.
873 if (f.shape() != INHERIT_SHAPE) {
876 env = true; // Size change need not bother about closing env.
878 if (f.color() != LColor::inherit && f.color() != LColor::ignore) {
881 env = true; // Size change need not bother about closing env.
883 if (f.emph() == ON) {
886 env = true; // Size change need not bother about closing env.
888 if (f.underbar() == ON) {
891 env = true; // Size change need not bother about closing env.
893 if (f.noun() == ON) {
896 env = true; // Size change need not bother about closing env.
898 if (f.size() != INHERIT_SIZE) {
899 // We only have to close if only size changed
906 if (number() == ON && next.number() != ON &&
907 language()->lang() == "hebrew") {
912 if (language() != base.language() && language() != next.language()) {
921 LColor::color LyXFont::realColor() const
923 if (color() == LColor::none)
924 return LColor::foreground;
929 // Convert logical attributes to concrete shape attribute
930 LyXFont::FONT_SHAPE LyXFont::realShape() const
932 register FONT_SHAPE s = shape();
946 ostream & operator<<(ostream & o, LyXFont::FONT_MISC_STATE fms)
948 return o << int(fms);