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.
24 #include "support/lstrings.h"
25 #include "bufferparams.h" // stateText
27 using namespace lyx::support;
32 #ifndef CXX_GLOBAL_CSTD
42 char const * GUIFamilyNames[LyXFont::NUM_FAMILIES + 2 /* default & error */] =
43 { N_("Roman"), N_("Sans Serif"), N_("Typewriter"), N_("Symbol"),
44 "cmr", "cmsy", "cmm", "cmex", "msa", "msb", "eufrak", "wasy",
45 N_("Inherit"), N_("Ignore") };
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[LyXFont::NUM_FAMILIES + 2 /* default & error */] =
67 { "roman", "sans", "typewriter", "symbol",
68 "cmr", "cmsy", "cmm", "cmex", "msa", "msb", "eufrak", "wasy",
71 char const * LyXSeriesNames[4] =
72 { "medium", "bold", "default", "error" };
74 char const * LyXShapeNames[6] =
75 { "up", "italic", "slanted", "smallcaps", "default", "error" };
77 char const * LyXSizeNames[14] =
78 { "tiny", "scriptsize", "footnotesize", "small", "normal", "large",
79 "larger", "largest", "huge", "giant",
80 "increase", "decrease", "default", "error" };
82 char const * LyXMiscNames[5] =
83 { "off", "on", "toggle", "default", "error" };
86 // Strings used to write LaTeX files
88 char const * LaTeXFamilyNames[6] =
89 { "textrm", "textsf", "texttt", "error1", "error2", "error3" };
91 char const * LaTeXSeriesNames[4] =
92 { "textmd", "textbf", "error4", "error5" };
94 char const * LaTeXShapeNames[6] =
95 { "textup", "textit", "textsl", "textsc", "error6", "error7" };
97 char const * LaTeXSizeNames[14] =
98 { "tiny", "scriptsize", "footnotesize", "small", "normalsize", "large",
99 "Large", "LARGE", "huge", "Huge", "error8", "error9", "error10", "error11" };
104 // Initialize static member
105 LyXFont::FontBits LyXFont::sane = {
116 // Initialize static member
117 LyXFont::FontBits LyXFont::inherit = {
128 // Initialize static member
129 LyXFont::FontBits LyXFont::ignore = {
141 bool operator==(LyXFont::FontBits const & lhs,
142 LyXFont::FontBits const & rhs)
144 return lhs.family == rhs.family &&
145 lhs.series == rhs.series &&
146 lhs.shape == rhs.shape &&
147 lhs.size == rhs.size &&
148 lhs.color == rhs.color &&
149 lhs.emph == rhs.emph &&
150 lhs.underbar == rhs.underbar &&
151 lhs.noun == rhs.noun &&
152 lhs.number == rhs.number;
157 : bits(sane), lang(default_language)
161 LyXFont::LyXFont(LyXFont::FONT_INIT1)
162 : bits(inherit), lang(default_language)
166 LyXFont::LyXFont(LyXFont::FONT_INIT2)
167 : bits(ignore), lang(ignore_language)
171 LyXFont::LyXFont(LyXFont::FONT_INIT3)
172 : bits(sane), lang(default_language)
176 LyXFont::LyXFont(LyXFont::FONT_INIT1, Language const * l)
177 : bits(inherit), lang(l)
181 LyXFont::LyXFont(LyXFont::FONT_INIT2, Language const * l)
182 : bits(ignore), lang(l)
186 LyXFont::LyXFont(LyXFont::FONT_INIT3, Language const * l)
187 : bits(sane), lang(l)
191 LyXFont::FONT_MISC_STATE LyXFont::underbar() const
193 return bits.underbar;
197 LColor::color LyXFont::color() const
203 Language const * LyXFont::language() const
209 LyXFont::FONT_MISC_STATE LyXFont::number() const
215 bool LyXFont::isRightToLeft() const
217 return lang->RightToLeft();
221 bool LyXFont::isVisibleRightToLeft() const
223 return (lang->RightToLeft() &&
228 LyXFont & LyXFont::setFamily(LyXFont::FONT_FAMILY f)
235 LyXFont & LyXFont::setSeries(LyXFont::FONT_SERIES s)
242 LyXFont & LyXFont::setShape(LyXFont::FONT_SHAPE s)
249 LyXFont & LyXFont::setSize(LyXFont::FONT_SIZE s)
256 LyXFont & LyXFont::setEmph(LyXFont::FONT_MISC_STATE e)
263 LyXFont & LyXFont::setUnderbar(LyXFont::FONT_MISC_STATE u)
270 LyXFont & LyXFont::setNoun(LyXFont::FONT_MISC_STATE n)
277 LyXFont & LyXFont::setColor(LColor::color c)
284 LyXFont & LyXFont::setLanguage(Language const * l)
291 LyXFont & LyXFont::setNumber(LyXFont::FONT_MISC_STATE n)
298 /// Decreases font size by one
299 LyXFont & LyXFont::decSize()
302 case SIZE_HUGER: setSize(SIZE_HUGE); break;
303 case SIZE_HUGE: setSize(SIZE_LARGEST); break;
304 case SIZE_LARGEST: setSize(SIZE_LARGER); break;
305 case SIZE_LARGER: setSize(SIZE_LARGE); break;
306 case SIZE_LARGE: setSize(SIZE_NORMAL); break;
307 case SIZE_NORMAL: setSize(SIZE_SMALL); break;
308 case SIZE_SMALL: setSize(SIZE_FOOTNOTE); break;
309 case SIZE_FOOTNOTE: setSize(SIZE_SCRIPT); break;
310 case SIZE_SCRIPT: setSize(SIZE_TINY); break;
311 case SIZE_TINY: break;
313 lyxerr << "Can't LyXFont::decSize on INCREASE_SIZE" << endl;
316 lyxerr <<"Can't LyXFont::decSize on DECREASE_SIZE" << endl;
319 lyxerr <<"Can't LyXFont::decSize on INHERIT_SIZE" << endl;
322 lyxerr <<"Can't LyXFont::decSize on IGNORE_SIZE" << endl;
329 /// Increases font size by one
330 LyXFont & LyXFont::incSize()
333 case SIZE_HUGER: break;
334 case SIZE_HUGE: setSize(SIZE_HUGER); break;
335 case SIZE_LARGEST: setSize(SIZE_HUGE); break;
336 case SIZE_LARGER: setSize(SIZE_LARGEST); break;
337 case SIZE_LARGE: setSize(SIZE_LARGER); break;
338 case SIZE_NORMAL: setSize(SIZE_LARGE); break;
339 case SIZE_SMALL: setSize(SIZE_NORMAL); break;
340 case SIZE_FOOTNOTE: setSize(SIZE_SMALL); break;
341 case SIZE_SCRIPT: setSize(SIZE_FOOTNOTE); break;
342 case SIZE_TINY: setSize(SIZE_SCRIPT); break;
344 lyxerr <<"Can't LyXFont::incSize on INCREASE_SIZE" << endl;
347 lyxerr <<"Can't LyXFont::incSize on DECREASE_SIZE" << endl;
350 lyxerr <<"Can't LyXFont::incSize on INHERIT_SIZE" << endl;
353 lyxerr <<"Can't LyXFont::incSize on IGNORE_SIZE" << endl;
360 /// Updates a misc setting according to request
361 LyXFont::FONT_MISC_STATE LyXFont::setMisc(FONT_MISC_STATE newfont,
364 if (newfont == TOGGLE) {
370 lyxerr <<"LyXFont::setMisc: Need state"
371 " ON or OFF to toggle. Setting to ON" << endl;
374 } else if (newfont == IGNORE)
381 /// Updates font settings according to request
382 void LyXFont::update(LyXFont const & newfont,
383 Language const * document_language,
386 if (newfont.family() == family() && toggleall)
387 setFamily(INHERIT_FAMILY); // toggle 'back'
388 else if (newfont.family() != IGNORE_FAMILY)
389 setFamily(newfont.family());
390 // else it's IGNORE_SHAPE
392 // "Old" behaviour: "Setting" bold will toggle bold on/off.
393 switch (newfont.series()) {
396 if (series() == BOLD_SERIES && toggleall)
397 setSeries(MEDIUM_SERIES);
399 setSeries(BOLD_SERIES);
403 setSeries(newfont.series());
409 if (newfont.shape() == shape() && toggleall)
410 setShape(INHERIT_SHAPE); // toggle 'back'
411 else if (newfont.shape() != IGNORE_SHAPE)
412 setShape(newfont.shape());
413 // else it's IGNORE_SHAPE
415 if (newfont.size() != IGNORE_SIZE) {
416 if (newfont.size() == INCREASE_SIZE)
418 else if (newfont.size() == DECREASE_SIZE)
421 setSize(newfont.size());
424 setEmph(setMisc(newfont.emph(), emph()));
425 setUnderbar(setMisc(newfont.underbar(), underbar()));
426 setNoun(setMisc(newfont.noun(), noun()));
428 setNumber(setMisc(newfont.number(), number()));
429 if (newfont.language() == language() && toggleall)
430 if (language() == document_language)
431 setLanguage(default_language);
433 setLanguage(document_language);
434 else if (newfont.language() != ignore_language)
435 setLanguage(newfont.language());
437 if (newfont.color() == color() && toggleall)
438 setColor(LColor::inherit); // toggle 'back'
439 else if (newfont.color() != LColor::ignore)
440 setColor(newfont.color());
444 /// Reduce font to fall back to template where possible
445 void LyXFont::reduce(LyXFont const & tmplt)
447 if (family() == tmplt.family())
448 setFamily(INHERIT_FAMILY);
449 if (series() == tmplt.series())
450 setSeries(INHERIT_SERIES);
451 if (shape() == tmplt.shape())
452 setShape(INHERIT_SHAPE);
453 if (size() == tmplt.size())
454 setSize(INHERIT_SIZE);
455 if (emph() == tmplt.emph())
457 if (underbar() == tmplt.underbar())
458 setUnderbar(INHERIT);
459 if (noun() == tmplt.noun())
461 if (color() == tmplt.color())
462 setColor(LColor::inherit);
466 /// Realize font from a template
467 LyXFont & LyXFont::realize(LyXFont const & tmplt)
469 if (bits == inherit) {
474 if (bits.family == INHERIT_FAMILY) {
475 bits.family = tmplt.bits.family;
477 if (bits.series == INHERIT_SERIES) {
478 bits.series = tmplt.bits.series;
480 if (bits.shape == INHERIT_SHAPE) {
481 bits.shape = tmplt.bits.shape;
483 if (bits.size == INHERIT_SIZE) {
484 bits.size = tmplt.bits.size;
486 if (bits.emph == INHERIT) {
487 bits.emph = tmplt.bits.emph;
489 if (bits.underbar == INHERIT) {
490 bits.underbar = tmplt.bits.underbar;
492 if (bits.noun == INHERIT) {
493 bits.noun = tmplt.bits.noun;
495 if (bits.color == LColor::inherit) {
496 bits.color = tmplt.bits.color;
502 /// Is font resolved?
503 bool LyXFont::resolved() const
505 return (family() != INHERIT_FAMILY && series() != INHERIT_SERIES &&
506 shape() != INHERIT_SHAPE && size() != INHERIT_SIZE &&
507 emph() != INHERIT && underbar() != INHERIT &&
509 color() != LColor::inherit);
513 /// Build GUI description of font state
514 string const LyXFont::stateText(BufferParams * params) const
517 if (family() != INHERIT_FAMILY)
518 os << _(GUIFamilyNames[family()]) << ", ";
519 if (series() != INHERIT_SERIES)
520 os << _(GUISeriesNames[series()]) << ", ";
521 if (shape() != INHERIT_SHAPE)
522 os << _(GUIShapeNames[shape()]) << ", ";
523 if (size() != INHERIT_SIZE)
524 os << _(GUISizeNames[size()]) << ", ";
525 if (color() != LColor::inherit)
526 os << lcolor.getGUIName(color()) << ", ";
527 if (emph() != INHERIT)
528 os << bformat(_("Emphasis %1$s, "), _(GUIMiscNames[emph()]));
529 if (underbar() != INHERIT)
530 os << bformat(_("Underline %1$s, "), _(GUIMiscNames[underbar()]));
531 if (noun() != INHERIT)
532 os << bformat(_("Noun %1$s, "), _(GUIMiscNames[noun()]));
534 os << _("Default") << ", ";
535 if (!params || (language() != params->language))
536 os << bformat(_("Language: %1$s, "), _(language()->display()));
538 os << bformat(_(" Number %1$s"), _(GUIMiscNames[number()]));
539 return rtrim(STRCONV(os.str()), ", ");
543 // Set family according to lyx format string
544 LyXFont & LyXFont::setLyXFamily(string const & fam)
546 string const s = ascii_lowercase(fam);
549 while (s != LyXFamilyNames[i] && LyXFamilyNames[i] != "error")
551 if (s == LyXFamilyNames[i])
552 setFamily(LyXFont::FONT_FAMILY(i));
554 lyxerr << "LyXFont::setLyXFamily: Unknown family `"
555 << s << '\'' << endl;
560 // Set series according to lyx format string
561 LyXFont & LyXFont::setLyXSeries(string const & ser)
563 string const s = ascii_lowercase(ser);
566 while (s != LyXSeriesNames[i] && LyXSeriesNames[i] != "error") ++i;
567 if (s == LyXSeriesNames[i]) {
568 setSeries(LyXFont::FONT_SERIES(i));
570 lyxerr << "LyXFont::setLyXSeries: Unknown series `"
571 << s << '\'' << endl;
576 // Set shape according to lyx format string
577 LyXFont & LyXFont::setLyXShape(string const & sha)
579 string const s = ascii_lowercase(sha);
582 while (s != LyXShapeNames[i] && LyXShapeNames[i] != "error") ++i;
583 if (s == LyXShapeNames[i]) {
584 setShape(LyXFont::FONT_SHAPE(i));
586 lyxerr << "LyXFont::setLyXShape: Unknown shape `"
587 << s << '\'' << endl;
592 // Set size according to lyx format string
593 LyXFont & LyXFont::setLyXSize(string const & siz)
595 string const s = ascii_lowercase(siz);
597 while (s != LyXSizeNames[i] && LyXSizeNames[i] != "error") ++i;
598 if (s == LyXSizeNames[i]) {
599 setSize(LyXFont::FONT_SIZE(i));
601 lyxerr << "LyXFont::setLyXSize: Unknown size `"
602 << s << '\'' << endl;
607 // Set size according to lyx format string
608 LyXFont::FONT_MISC_STATE LyXFont::setLyXMisc(string const & siz)
610 string const s = ascii_lowercase(siz);
612 while (s != LyXMiscNames[i] && LyXMiscNames[i] != "error") ++i;
613 if (s == LyXMiscNames[i])
614 return FONT_MISC_STATE(i);
615 lyxerr << "LyXFont::setLyXMisc: Unknown misc flag `"
616 << s << '\'' << endl;
621 /// Sets color after LyX text format
622 LyXFont & LyXFont::setLyXColor(string const & col)
624 setColor(lcolor.getFromLyXName(col));
629 // Returns size in latex format
630 string const LyXFont::latexSize() const
632 return LaTeXSizeNames[size()];
636 // Read a font definition from given file in lyx format
638 LyXFont & LyXFont::lyxRead(LyXLex & lex)
641 bool finished = false;
642 while (!finished && lex.isOK() && !error) {
644 string const tok = ascii_lowercase(lex.getString());
648 } else if (tok == "endfont") {
650 } else if (tok == "family") {
652 string const ttok = lex.getString();
654 } else if (tok == "series") {
656 string const ttok = lex.getString();
658 } else if (tok == "shape") {
660 string const ttok = lex.getString();
662 } else if (tok == "size") {
664 string const ttok = lex.getString();
666 } else if (tok == "misc") {
668 string const ttok = ascii_lowercase(lex.getString());
670 if (ttok == "no_bar") {
672 } else if (ttok == "no_emph") {
674 } else if (ttok == "no_noun") {
676 } else if (ttok == "emph") {
678 } else if (ttok == "underbar") {
680 } else if (ttok == "noun") {
683 lex.printError("Illegal misc type `$$Token´");
685 } else if (tok == "color") {
687 string const ttok = lex.getString();
690 lex.printError("Unknown tag `$$Token'");
698 /// Writes the changes from this font to orgfont in .lyx format in file
699 void LyXFont::lyxWriteChanges(LyXFont const & orgfont,
703 if (orgfont.family() != family()) {
704 os << "\\family " << LyXFamilyNames[family()] << " \n";
706 if (orgfont.series() != series()) {
707 os << "\\series " << LyXSeriesNames[series()] << " \n";
709 if (orgfont.shape() != shape()) {
710 os << "\\shape " << LyXShapeNames[shape()] << " \n";
712 if (orgfont.size() != size()) {
713 os << "\\size " << LyXSizeNames[size()] << " \n";
715 if (orgfont.emph() != emph()) {
716 os << "\\emph " << LyXMiscNames[emph()] << " \n";
718 if (orgfont.number() != number()) {
719 os << "\\numeric " << LyXMiscNames[number()] << " \n";
721 if (orgfont.underbar() != underbar()) {
722 // This is only for backwards compatibility
723 switch (underbar()) {
724 case OFF: os << "\\bar no \n"; break;
725 case ON: os << "\\bar under \n"; break;
726 case TOGGLE: lyxerr << "LyXFont::lyxWriteFontChanges: "
727 "TOGGLE should not appear here!"
730 case INHERIT: os << "\\bar default \n"; break;
731 case IGNORE: lyxerr << "LyXFont::lyxWriteFontChanges: "
732 "IGNORE should not appear here!"
737 if (orgfont.noun() != noun()) {
738 os << "\\noun " << LyXMiscNames[noun()] << " \n";
740 if (orgfont.color() != color()) {
741 // To make us file compatible with older
742 // lyx versions we emit "default" instead
744 string col_str(lcolor.getLyXName(color()));
745 if (col_str == "inherit") col_str = "default";
746 os << "\\color " << col_str << "\n";
748 if (orgfont.language() != language()) {
750 os << "\\lang " << language()->lang() << "\n";
752 os << "\\lang unknown\n";
757 /// Writes the head of the LaTeX needed to impose this font
758 // Returns number of chars written.
759 int LyXFont::latexWriteStartChanges(ostream & os, LyXFont const & base,
760 LyXFont const & prev) const
765 if (language()->babel() != base.language()->babel() &&
766 language() != prev.language()) {
767 if (isRightToLeft() != prev.isRightToLeft()) {
768 if (isRightToLeft()) {
777 subst(lyxrc.language_command_local,
778 "$$lang", language()->babel());
780 count += tmp.length();
784 if (number() == ON && prev.number() != ON &&
785 language()->lang() == "hebrew") {
793 if (f.family() != INHERIT_FAMILY) {
795 << LaTeXFamilyNames[f.family()]
797 count += strlen(LaTeXFamilyNames[f.family()]) + 2;
798 env = true; //We have opened a new environment
800 if (f.series() != INHERIT_SERIES) {
802 << LaTeXSeriesNames[f.series()]
804 count += strlen(LaTeXSeriesNames[f.series()]) + 2;
805 env = true; //We have opened a new environment
807 if (f.shape() != INHERIT_SHAPE) {
809 << LaTeXShapeNames[f.shape()]
811 count += strlen(LaTeXShapeNames[f.shape()]) + 2;
812 env = true; //We have opened a new environment
814 if (f.color() != LColor::inherit && f.color() != LColor::ignore) {
816 << lcolor.getLaTeXName(f.color())
818 count += lcolor.getLaTeXName(f.color()).length() + 13;
819 env = true; //We have opened a new environment
821 if (f.emph() == ON) {
824 env = true; //We have opened a new environment
826 if (f.underbar() == ON) {
829 env = true; //We have opened a new environment
831 // \noun{} is a LyX special macro
832 if (f.noun() == ON) {
835 env = true; //We have opened a new environment
837 if (f.size() != INHERIT_SIZE) {
838 // If we didn't open an environment above, we open one here
844 << LaTeXSizeNames[f.size()]
846 count += strlen(LaTeXSizeNames[f.size()]) + 2;
852 /// Writes ending block of LaTeX needed to close use of this font
853 // Returns number of chars written
854 // This one corresponds to latexWriteStartChanges(). (Asger)
855 int LyXFont::latexWriteEndChanges(ostream & os, LyXFont const & base,
856 LyXFont const & next) const
861 // reduce the current font to changes against the base
862 // font (of the layout). We use a temporary for this to
863 // avoid changing this font instance, as that would break
867 if (f.family() != INHERIT_FAMILY) {
870 env = true; // Size change need not bother about closing env.
872 if (f.series() != INHERIT_SERIES) {
875 env = true; // Size change need not bother about closing env.
877 if (f.shape() != INHERIT_SHAPE) {
880 env = true; // Size change need not bother about closing env.
882 if (f.color() != LColor::inherit && f.color() != LColor::ignore) {
885 env = true; // Size change need not bother about closing env.
887 if (f.emph() == ON) {
890 env = true; // Size change need not bother about closing env.
892 if (f.underbar() == ON) {
895 env = true; // Size change need not bother about closing env.
897 if (f.noun() == ON) {
900 env = true; // Size change need not bother about closing env.
902 if (f.size() != INHERIT_SIZE) {
903 // We only have to close if only size changed
910 if (number() == ON && next.number() != ON &&
911 language()->lang() == "hebrew") {
916 if (language() != base.language() && language() != next.language()) {
925 LColor::color LyXFont::realColor() const
927 if (color() == LColor::none)
928 return LColor::foreground;
933 // Convert logical attributes to concrete shape attribute
934 LyXFont::FONT_SHAPE LyXFont::realShape() const
936 register FONT_SHAPE s = shape();
950 ostream & operator<<(ostream & o, LyXFont::FONT_MISC_STATE fms)
952 return o << int(fms);