3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Lars Gullik Bjønnes
7 * \author Jean-Marc Lasgouttes
8 * \author Angus Leeming
12 * Full author contact details are available in file CREDITS.
26 #include "support/lstrings.h"
27 #include "bufferparams.h" // stateText
29 using namespace lyx::support;
34 #ifndef CXX_GLOBAL_CSTD
44 char const * GUIFamilyNames[LyXFont::NUM_FAMILIES + 2 /* default & error */] =
45 { N_("Roman"), N_("Sans Serif"), N_("Typewriter"), N_("Symbol"),
46 "cmr", "cmsy", "cmm", "cmex", "msa", "msb", "eufrak", "wasy",
47 N_("Inherit"), N_("Ignore") };
49 char const * GUISeriesNames[4] =
50 { N_("Medium"), N_("Bold"), N_("Inherit"), N_("Ignore") };
52 char const * GUIShapeNames[6] =
53 { N_("Upright"), N_("Italic"), N_("Slanted"), N_("Smallcaps"), N_("Inherit"),
56 char const * GUISizeNames[14] =
57 { N_("Tiny"), N_("Smallest"), N_("Smaller"), N_("Small"), N_("Normal"), N_("Large"),
58 N_("Larger"), N_("Largest"), N_("Huge"), N_("Huger"), N_("Increase"), N_("Decrease"),
59 N_("Inherit"), N_("Ignore") };
61 char const * GUIMiscNames[5] =
62 { N_("Off"), N_("On"), N_("Toggle"), N_("Inherit"), N_("Ignore") };
66 // Strings used to read and write .lyx format files
68 char const * LyXFamilyNames[LyXFont::NUM_FAMILIES + 2 /* default & error */] =
69 { "roman", "sans", "typewriter", "symbol",
70 "cmr", "cmsy", "cmm", "cmex", "msa", "msb", "eufrak", "wasy",
73 char const * LyXSeriesNames[4] =
74 { "medium", "bold", "default", "error" };
76 char const * LyXShapeNames[6] =
77 { "up", "italic", "slanted", "smallcaps", "default", "error" };
79 char const * LyXSizeNames[14] =
80 { "tiny", "scriptsize", "footnotesize", "small", "normal", "large",
81 "larger", "largest", "huge", "giant",
82 "increase", "decrease", "default", "error" };
84 char const * LyXMiscNames[5] =
85 { "off", "on", "toggle", "default", "error" };
88 // Strings used to write LaTeX files
90 char const * LaTeXFamilyNames[6] =
91 { "textrm", "textsf", "texttt", "error1", "error2", "error3" };
93 char const * LaTeXSeriesNames[4] =
94 { "textmd", "textbf", "error4", "error5" };
96 char const * LaTeXShapeNames[6] =
97 { "textup", "textit", "textsl", "textsc", "error6", "error7" };
99 char const * LaTeXSizeNames[14] =
100 { "tiny", "scriptsize", "footnotesize", "small", "normalsize", "large",
101 "Large", "LARGE", "huge", "Huge", "error8", "error9", "error10", "error11" };
106 // Initialize static member
107 LyXFont::FontBits LyXFont::sane = {
118 // Initialize static member
119 LyXFont::FontBits LyXFont::inherit = {
130 // Initialize static member
131 LyXFont::FontBits LyXFont::ignore = {
143 bool operator==(LyXFont::FontBits const & lhs,
144 LyXFont::FontBits const & rhs)
146 return lhs.family == rhs.family &&
147 lhs.series == rhs.series &&
148 lhs.shape == rhs.shape &&
149 lhs.size == rhs.size &&
150 lhs.color == rhs.color &&
151 lhs.emph == rhs.emph &&
152 lhs.underbar == rhs.underbar &&
153 lhs.noun == rhs.noun &&
154 lhs.number == rhs.number;
159 : bits(sane), lang(default_language)
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 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 setLanguage(document_language);
436 else if (newfont.language() != ignore_language)
437 setLanguage(newfont.language());
439 if (newfont.color() == color() && toggleall)
440 setColor(LColor::inherit); // toggle 'back'
441 else if (newfont.color() != LColor::ignore)
442 setColor(newfont.color());
446 /// Reduce font to fall back to template where possible
447 void LyXFont::reduce(LyXFont const & tmplt)
449 if (family() == tmplt.family())
450 setFamily(INHERIT_FAMILY);
451 if (series() == tmplt.series())
452 setSeries(INHERIT_SERIES);
453 if (shape() == tmplt.shape())
454 setShape(INHERIT_SHAPE);
455 if (size() == tmplt.size())
456 setSize(INHERIT_SIZE);
457 if (emph() == tmplt.emph())
459 if (underbar() == tmplt.underbar())
460 setUnderbar(INHERIT);
461 if (noun() == tmplt.noun())
463 if (color() == tmplt.color())
464 setColor(LColor::inherit);
468 /// Realize font from a template
469 LyXFont & LyXFont::realize(LyXFont const & tmplt)
471 if (bits == inherit) {
476 if (bits.family == INHERIT_FAMILY) {
477 bits.family = tmplt.bits.family;
479 if (bits.series == INHERIT_SERIES) {
480 bits.series = tmplt.bits.series;
482 if (bits.shape == INHERIT_SHAPE) {
483 bits.shape = tmplt.bits.shape;
485 if (bits.size == INHERIT_SIZE) {
486 bits.size = tmplt.bits.size;
488 if (bits.emph == INHERIT) {
489 bits.emph = tmplt.bits.emph;
491 if (bits.underbar == INHERIT) {
492 bits.underbar = tmplt.bits.underbar;
494 if (bits.noun == INHERIT) {
495 bits.noun = tmplt.bits.noun;
497 if (bits.color == LColor::inherit) {
498 bits.color = tmplt.bits.color;
504 /// Is font resolved?
505 bool LyXFont::resolved() const
507 return (family() != INHERIT_FAMILY && series() != INHERIT_SERIES &&
508 shape() != INHERIT_SHAPE && size() != INHERIT_SIZE &&
509 emph() != INHERIT && underbar() != INHERIT &&
511 color() != LColor::inherit);
515 /// Build GUI description of font state
516 string const LyXFont::stateText(BufferParams * params) const
519 if (family() != INHERIT_FAMILY)
520 os << _(GUIFamilyNames[family()]) << ", ";
521 if (series() != INHERIT_SERIES)
522 os << _(GUISeriesNames[series()]) << ", ";
523 if (shape() != INHERIT_SHAPE)
524 os << _(GUIShapeNames[shape()]) << ", ";
525 if (size() != INHERIT_SIZE)
526 os << _(GUISizeNames[size()]) << ", ";
527 if (color() != LColor::inherit)
528 os << lcolor.getGUIName(color()) << ", ";
529 if (emph() != INHERIT)
530 os << bformat(_("Emphasis %1$s, "), _(GUIMiscNames[emph()]));
531 if (underbar() != INHERIT)
532 os << bformat(_("Underline %1$s, "), _(GUIMiscNames[underbar()]));
533 if (noun() != INHERIT)
534 os << bformat(_("Noun %1$s, "), _(GUIMiscNames[noun()]));
536 os << _("Default") << ", ";
537 if (!params || (language() != params->language))
538 os << bformat(_("Language: %1$s, "), _(language()->display()));
540 os << bformat(_(" Number %1$s"), _(GUIMiscNames[number()]));
541 return rtrim(STRCONV(os.str()), ", ");
545 // Set family according to lyx format string
546 LyXFont & LyXFont::setLyXFamily(string const & fam)
548 string const s = ascii_lowercase(fam);
551 while (s != LyXFamilyNames[i] && LyXFamilyNames[i] != "error")
553 if (s == LyXFamilyNames[i])
554 setFamily(LyXFont::FONT_FAMILY(i));
556 lyxerr << "LyXFont::setLyXFamily: Unknown family `"
557 << s << '\'' << endl;
562 // Set series according to lyx format string
563 LyXFont & LyXFont::setLyXSeries(string const & ser)
565 string const s = ascii_lowercase(ser);
568 while (s != LyXSeriesNames[i] && LyXSeriesNames[i] != "error") ++i;
569 if (s == LyXSeriesNames[i]) {
570 setSeries(LyXFont::FONT_SERIES(i));
572 lyxerr << "LyXFont::setLyXSeries: Unknown series `"
573 << s << '\'' << endl;
578 // Set shape according to lyx format string
579 LyXFont & LyXFont::setLyXShape(string const & sha)
581 string const s = ascii_lowercase(sha);
584 while (s != LyXShapeNames[i] && LyXShapeNames[i] != "error") ++i;
585 if (s == LyXShapeNames[i]) {
586 setShape(LyXFont::FONT_SHAPE(i));
588 lyxerr << "LyXFont::setLyXShape: Unknown shape `"
589 << s << '\'' << endl;
594 // Set size according to lyx format string
595 LyXFont & LyXFont::setLyXSize(string const & siz)
597 string const s = ascii_lowercase(siz);
599 while (s != LyXSizeNames[i] && LyXSizeNames[i] != "error") ++i;
600 if (s == LyXSizeNames[i]) {
601 setSize(LyXFont::FONT_SIZE(i));
603 lyxerr << "LyXFont::setLyXSize: Unknown size `"
604 << s << '\'' << endl;
609 // Set size according to lyx format string
610 LyXFont::FONT_MISC_STATE LyXFont::setLyXMisc(string const & siz)
612 string const s = ascii_lowercase(siz);
614 while (s != LyXMiscNames[i] && LyXMiscNames[i] != "error") ++i;
615 if (s == LyXMiscNames[i])
616 return FONT_MISC_STATE(i);
617 lyxerr << "LyXFont::setLyXMisc: Unknown misc flag `"
618 << s << '\'' << endl;
623 /// Sets color after LyX text format
624 LyXFont & LyXFont::setLyXColor(string const & col)
626 setColor(lcolor.getFromLyXName(col));
631 // Returns size in latex format
632 string const LyXFont::latexSize() const
634 return LaTeXSizeNames[size()];
638 // Read a font definition from given file in lyx format
640 LyXFont & LyXFont::lyxRead(LyXLex & lex)
643 bool finished = false;
644 while (!finished && lex.isOK() && !error) {
646 string const tok = ascii_lowercase(lex.getString());
650 } else if (tok == "endfont") {
652 } else if (tok == "family") {
654 string const ttok = lex.getString();
656 } else if (tok == "series") {
658 string const ttok = lex.getString();
660 } else if (tok == "shape") {
662 string const ttok = lex.getString();
664 } else if (tok == "size") {
666 string const ttok = lex.getString();
668 } else if (tok == "misc") {
670 string const ttok = ascii_lowercase(lex.getString());
672 if (ttok == "no_bar") {
674 } else if (ttok == "no_emph") {
676 } else if (ttok == "no_noun") {
678 } else if (ttok == "emph") {
680 } else if (ttok == "underbar") {
682 } else if (ttok == "noun") {
685 lex.printError("Illegal misc type `$$Token´");
687 } else if (tok == "color") {
689 string const ttok = lex.getString();
692 lex.printError("Unknown tag `$$Token'");
700 /// Writes the changes from this font to orgfont in .lyx format in file
701 void LyXFont::lyxWriteChanges(LyXFont const & orgfont,
705 if (orgfont.family() != family()) {
706 os << "\\family " << LyXFamilyNames[family()] << " \n";
708 if (orgfont.series() != series()) {
709 os << "\\series " << LyXSeriesNames[series()] << " \n";
711 if (orgfont.shape() != shape()) {
712 os << "\\shape " << LyXShapeNames[shape()] << " \n";
714 if (orgfont.size() != size()) {
715 os << "\\size " << LyXSizeNames[size()] << " \n";
717 if (orgfont.emph() != emph()) {
718 os << "\\emph " << LyXMiscNames[emph()] << " \n";
720 if (orgfont.number() != number()) {
721 os << "\\numeric " << LyXMiscNames[number()] << " \n";
723 if (orgfont.underbar() != underbar()) {
724 // This is only for backwards compatibility
725 switch (underbar()) {
726 case OFF: os << "\\bar no \n"; break;
727 case ON: os << "\\bar under \n"; break;
728 case TOGGLE: lyxerr << "LyXFont::lyxWriteFontChanges: "
729 "TOGGLE should not appear here!"
732 case INHERIT: os << "\\bar default \n"; break;
733 case IGNORE: lyxerr << "LyXFont::lyxWriteFontChanges: "
734 "IGNORE should not appear here!"
739 if (orgfont.noun() != noun()) {
740 os << "\\noun " << LyXMiscNames[noun()] << " \n";
742 if (orgfont.color() != color()) {
743 // To make us file compatible with older
744 // lyx versions we emit "default" instead
746 string col_str(lcolor.getLyXName(color()));
747 if (col_str == "inherit") col_str = "default";
748 os << "\\color " << col_str << "\n";
750 if (orgfont.language() != language()) {
752 os << "\\lang " << language()->lang() << "\n";
754 os << "\\lang unknown\n";
759 /// Writes the head of the LaTeX needed to impose this font
760 // Returns number of chars written.
761 int LyXFont::latexWriteStartChanges(ostream & os, LyXFont const & base,
762 LyXFont const & prev) const
767 if (language()->babel() != base.language()->babel() &&
768 language() != prev.language()) {
769 if (isRightToLeft() != prev.isRightToLeft()) {
770 if (isRightToLeft()) {
779 subst(lyxrc.language_command_local,
780 "$$lang", language()->babel());
782 count += tmp.length();
786 if (number() == ON && prev.number() != ON &&
787 language()->lang() == "hebrew") {
795 if (f.family() != INHERIT_FAMILY) {
797 << LaTeXFamilyNames[f.family()]
799 count += strlen(LaTeXFamilyNames[f.family()]) + 2;
800 env = true; //We have opened a new environment
802 if (f.series() != INHERIT_SERIES) {
804 << LaTeXSeriesNames[f.series()]
806 count += strlen(LaTeXSeriesNames[f.series()]) + 2;
807 env = true; //We have opened a new environment
809 if (f.shape() != INHERIT_SHAPE) {
811 << LaTeXShapeNames[f.shape()]
813 count += strlen(LaTeXShapeNames[f.shape()]) + 2;
814 env = true; //We have opened a new environment
816 if (f.color() != LColor::inherit && f.color() != LColor::ignore) {
818 << lcolor.getLaTeXName(f.color())
820 count += lcolor.getLaTeXName(f.color()).length() + 13;
821 env = true; //We have opened a new environment
823 if (f.emph() == ON) {
826 env = true; //We have opened a new environment
828 if (f.underbar() == ON) {
831 env = true; //We have opened a new environment
833 // \noun{} is a LyX special macro
834 if (f.noun() == ON) {
837 env = true; //We have opened a new environment
839 if (f.size() != INHERIT_SIZE) {
840 // If we didn't open an environment above, we open one here
846 << LaTeXSizeNames[f.size()]
848 count += strlen(LaTeXSizeNames[f.size()]) + 2;
854 /// Writes ending block of LaTeX needed to close use of this font
855 // Returns number of chars written
856 // This one corresponds to latexWriteStartChanges(). (Asger)
857 int LyXFont::latexWriteEndChanges(ostream & os, LyXFont const & base,
858 LyXFont const & next) const
863 // reduce the current font to changes against the base
864 // font (of the layout). We use a temporary for this to
865 // avoid changing this font instance, as that would break
869 if (f.family() != INHERIT_FAMILY) {
872 env = true; // Size change need not bother about closing env.
874 if (f.series() != INHERIT_SERIES) {
877 env = true; // Size change need not bother about closing env.
879 if (f.shape() != INHERIT_SHAPE) {
882 env = true; // Size change need not bother about closing env.
884 if (f.color() != LColor::inherit && f.color() != LColor::ignore) {
887 env = true; // Size change need not bother about closing env.
889 if (f.emph() == ON) {
892 env = true; // Size change need not bother about closing env.
894 if (f.underbar() == ON) {
897 env = true; // Size change need not bother about closing env.
899 if (f.noun() == ON) {
902 env = true; // Size change need not bother about closing env.
904 if (f.size() != INHERIT_SIZE) {
905 // We only have to close if only size changed
912 if (number() == ON && next.number() != ON &&
913 language()->lang() == "hebrew") {
918 if (language() != base.language() && language() != next.language()) {
927 LColor::color LyXFont::realColor() const
929 if (color() == LColor::none)
930 return LColor::foreground;
935 // Convert logical attributes to concrete shape attribute
936 LyXFont::FONT_SHAPE LyXFont::realShape() const
938 register FONT_SHAPE s = shape();
952 ostream & operator<<(ostream & o, LyXFont::FONT_MISC_STATE fms)
954 return o << int(fms);