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.
19 #include "bufferparams.h" // stateText
27 #include "support/lstrings.h"
32 using support::ascii_lowercase;
33 using support::bformat;
40 using std::ostringstream;
42 #ifndef CXX_GLOBAL_CSTD
52 char const * GUIFamilyNames[LyXFont::NUM_FAMILIES + 2 /* default & error */] =
53 { N_("Roman"), N_("Sans Serif"), N_("Typewriter"), N_("Symbol"),
54 "cmr", "cmsy", "cmm", "cmex", "msa", "msb", "eufrak", "wasy",
55 N_("Inherit"), N_("Ignore") };
57 char const * GUISeriesNames[4] =
58 { N_("Medium"), N_("Bold"), N_("Inherit"), N_("Ignore") };
60 char const * GUIShapeNames[6] =
61 { N_("Upright"), N_("Italic"), N_("Slanted"), N_("Smallcaps"), N_("Inherit"),
64 char const * GUISizeNames[14] =
65 { N_("Tiny"), N_("Smallest"), N_("Smaller"), N_("Small"), N_("Normal"), N_("Large"),
66 N_("Larger"), N_("Largest"), N_("Huge"), N_("Huger"), N_("Increase"), N_("Decrease"),
67 N_("Inherit"), N_("Ignore") };
69 char const * GUIMiscNames[5] =
70 { N_("Off"), N_("On"), N_("Toggle"), N_("Inherit"), N_("Ignore") };
74 // Strings used to read and write .lyx format files
76 char const * LyXFamilyNames[LyXFont::NUM_FAMILIES + 2 /* default & error */] =
77 { "roman", "sans", "typewriter", "symbol",
78 "cmr", "cmsy", "cmm", "cmex", "msa", "msb", "eufrak", "wasy",
81 char const * LyXSeriesNames[4] =
82 { "medium", "bold", "default", "error" };
84 char const * LyXShapeNames[6] =
85 { "up", "italic", "slanted", "smallcaps", "default", "error" };
87 char const * LyXSizeNames[14] =
88 { "tiny", "scriptsize", "footnotesize", "small", "normal", "large",
89 "larger", "largest", "huge", "giant",
90 "increase", "decrease", "default", "error" };
92 char const * LyXMiscNames[5] =
93 { "off", "on", "toggle", "default", "error" };
96 // Strings used to write LaTeX files
98 char const * LaTeXFamilyNames[6] =
99 { "textrm", "textsf", "texttt", "error1", "error2", "error3" };
101 char const * LaTeXSeriesNames[4] =
102 { "textmd", "textbf", "error4", "error5" };
104 char const * LaTeXShapeNames[6] =
105 { "textup", "textit", "textsl", "textsc", "error6", "error7" };
107 char const * LaTeXSizeNames[14] =
108 { "tiny", "scriptsize", "footnotesize", "small", "normalsize", "large",
109 "Large", "LARGE", "huge", "Huge", "error8", "error9", "error10", "error11" };
114 // Initialize static member
115 LyXFont::FontBits LyXFont::sane = {
126 // Initialize static member
127 LyXFont::FontBits LyXFont::inherit = {
138 // Initialize static member
139 LyXFont::FontBits LyXFont::ignore = {
151 bool operator==(LyXFont::FontBits const & lhs,
152 LyXFont::FontBits const & rhs)
154 return lhs.family == rhs.family &&
155 lhs.series == rhs.series &&
156 lhs.shape == rhs.shape &&
157 lhs.size == rhs.size &&
158 lhs.color == rhs.color &&
159 lhs.emph == rhs.emph &&
160 lhs.underbar == rhs.underbar &&
161 lhs.noun == rhs.noun &&
162 lhs.number == rhs.number;
167 : bits(sane), lang(default_language)
171 LyXFont::LyXFont(LyXFont::FONT_INIT1)
172 : bits(inherit), lang(default_language)
176 LyXFont::LyXFont(LyXFont::FONT_INIT2)
177 : bits(ignore), lang(ignore_language)
181 LyXFont::LyXFont(LyXFont::FONT_INIT3)
182 : bits(sane), lang(default_language)
186 LyXFont::LyXFont(LyXFont::FONT_INIT1, Language const * l)
187 : bits(inherit), lang(l)
191 LyXFont::LyXFont(LyXFont::FONT_INIT2, Language const * l)
192 : bits(ignore), lang(l)
196 LyXFont::LyXFont(LyXFont::FONT_INIT3, Language const * l)
197 : bits(sane), lang(l)
202 LColor_color LyXFont::color() const
204 return LColor::color(bits.color);
208 bool LyXFont::isRightToLeft() const
210 return lang->rightToLeft();
214 bool LyXFont::isVisibleRightToLeft() const
216 return (lang->rightToLeft() &&
221 void LyXFont::setFamily(LyXFont::FONT_FAMILY f)
227 void LyXFont::setSeries(LyXFont::FONT_SERIES s)
233 void LyXFont::setShape(LyXFont::FONT_SHAPE s)
239 void LyXFont::setSize(LyXFont::FONT_SIZE s)
245 void LyXFont::setEmph(LyXFont::FONT_MISC_STATE e)
251 void LyXFont::setUnderbar(LyXFont::FONT_MISC_STATE u)
257 void LyXFont::setNoun(LyXFont::FONT_MISC_STATE n)
263 void LyXFont::setColor(LColor_color c)
269 void LyXFont::setLanguage(Language const * l)
275 void LyXFont::setNumber(LyXFont::FONT_MISC_STATE n)
281 /// Decreases font size by one
282 LyXFont & LyXFont::decSize()
285 case SIZE_HUGER: setSize(SIZE_HUGE); break;
286 case SIZE_HUGE: setSize(SIZE_LARGEST); break;
287 case SIZE_LARGEST: setSize(SIZE_LARGER); break;
288 case SIZE_LARGER: setSize(SIZE_LARGE); break;
289 case SIZE_LARGE: setSize(SIZE_NORMAL); break;
290 case SIZE_NORMAL: setSize(SIZE_SMALL); break;
291 case SIZE_SMALL: setSize(SIZE_FOOTNOTE); break;
292 case SIZE_FOOTNOTE: setSize(SIZE_SCRIPT); break;
293 case SIZE_SCRIPT: setSize(SIZE_TINY); break;
294 case SIZE_TINY: break;
296 lyxerr << "Can't LyXFont::decSize on INCREASE_SIZE" << endl;
299 lyxerr <<"Can't LyXFont::decSize on DECREASE_SIZE" << endl;
302 lyxerr <<"Can't LyXFont::decSize on INHERIT_SIZE" << endl;
305 lyxerr <<"Can't LyXFont::decSize on IGNORE_SIZE" << endl;
312 /// Increases font size by one
313 LyXFont & LyXFont::incSize()
316 case SIZE_HUGER: break;
317 case SIZE_HUGE: setSize(SIZE_HUGER); break;
318 case SIZE_LARGEST: setSize(SIZE_HUGE); break;
319 case SIZE_LARGER: setSize(SIZE_LARGEST); break;
320 case SIZE_LARGE: setSize(SIZE_LARGER); break;
321 case SIZE_NORMAL: setSize(SIZE_LARGE); break;
322 case SIZE_SMALL: setSize(SIZE_NORMAL); break;
323 case SIZE_FOOTNOTE: setSize(SIZE_SMALL); break;
324 case SIZE_SCRIPT: setSize(SIZE_FOOTNOTE); break;
325 case SIZE_TINY: setSize(SIZE_SCRIPT); break;
327 lyxerr <<"Can't LyXFont::incSize on INCREASE_SIZE" << endl;
330 lyxerr <<"Can't LyXFont::incSize on DECREASE_SIZE" << endl;
333 lyxerr <<"Can't LyXFont::incSize on INHERIT_SIZE" << endl;
336 lyxerr <<"Can't LyXFont::incSize on IGNORE_SIZE" << endl;
343 /// Updates a misc setting according to request
344 LyXFont::FONT_MISC_STATE LyXFont::setMisc(FONT_MISC_STATE newfont,
347 if (newfont == TOGGLE) {
353 lyxerr <<"LyXFont::setMisc: Need state"
354 " ON or OFF to toggle. Setting to ON" << endl;
357 } else if (newfont == IGNORE)
364 /// Updates font settings according to request
365 void LyXFont::update(LyXFont const & newfont,
366 Language const * document_language,
369 if (newfont.family() == family() && toggleall)
370 setFamily(INHERIT_FAMILY); // toggle 'back'
371 else if (newfont.family() != IGNORE_FAMILY)
372 setFamily(newfont.family());
373 // else it's IGNORE_SHAPE
375 // "Old" behaviour: "Setting" bold will toggle bold on/off.
376 switch (newfont.series()) {
379 if (series() == BOLD_SERIES && toggleall)
380 setSeries(MEDIUM_SERIES);
382 setSeries(BOLD_SERIES);
386 setSeries(newfont.series());
392 if (newfont.shape() == shape() && toggleall)
393 setShape(INHERIT_SHAPE); // toggle 'back'
394 else if (newfont.shape() != IGNORE_SHAPE)
395 setShape(newfont.shape());
396 // else it's IGNORE_SHAPE
398 if (newfont.size() != IGNORE_SIZE) {
399 if (newfont.size() == INCREASE_SIZE)
401 else if (newfont.size() == DECREASE_SIZE)
404 setSize(newfont.size());
407 setEmph(setMisc(newfont.emph(), emph()));
408 setUnderbar(setMisc(newfont.underbar(), underbar()));
409 setNoun(setMisc(newfont.noun(), noun()));
411 setNumber(setMisc(newfont.number(), number()));
412 if (newfont.language() == language() && toggleall)
413 if (language() == document_language)
414 setLanguage(default_language);
416 setLanguage(document_language);
417 else if (newfont.language() != ignore_language)
418 setLanguage(newfont.language());
420 if (newfont.color() == color() && toggleall)
421 setColor(LColor::inherit); // toggle 'back'
422 else if (newfont.color() != LColor::ignore)
423 setColor(newfont.color());
427 /// Reduce font to fall back to template where possible
428 void LyXFont::reduce(LyXFont const & tmplt)
430 if (family() == tmplt.family())
431 setFamily(INHERIT_FAMILY);
432 if (series() == tmplt.series())
433 setSeries(INHERIT_SERIES);
434 if (shape() == tmplt.shape())
435 setShape(INHERIT_SHAPE);
436 if (size() == tmplt.size())
437 setSize(INHERIT_SIZE);
438 if (emph() == tmplt.emph())
440 if (underbar() == tmplt.underbar())
441 setUnderbar(INHERIT);
442 if (noun() == tmplt.noun())
444 if (color() == tmplt.color())
445 setColor(LColor::inherit);
449 /// Realize font from a template
450 LyXFont & LyXFont::realize(LyXFont const & tmplt)
452 if (bits == inherit) {
457 if (bits.family == INHERIT_FAMILY)
458 bits.family = tmplt.bits.family;
460 if (bits.series == INHERIT_SERIES)
461 bits.series = tmplt.bits.series;
463 if (bits.shape == INHERIT_SHAPE)
464 bits.shape = tmplt.bits.shape;
466 if (bits.size == INHERIT_SIZE)
467 bits.size = tmplt.bits.size;
469 if (bits.emph == INHERIT)
470 bits.emph = tmplt.bits.emph;
472 if (bits.underbar == INHERIT)
473 bits.underbar = tmplt.bits.underbar;
475 if (bits.noun == INHERIT)
476 bits.noun = tmplt.bits.noun;
478 if (bits.color == LColor::inherit)
479 bits.color = tmplt.bits.color;
485 /// Is font resolved?
486 bool LyXFont::resolved() const
488 return (family() != INHERIT_FAMILY && series() != INHERIT_SERIES &&
489 shape() != INHERIT_SHAPE && size() != INHERIT_SIZE &&
490 emph() != INHERIT && underbar() != INHERIT &&
492 color() != LColor::inherit);
496 /// Build GUI description of font state
497 string const LyXFont::stateText(BufferParams * params) const
500 if (family() != INHERIT_FAMILY)
501 os << to_utf8(_(GUIFamilyNames[family()])) << ", ";
502 if (series() != INHERIT_SERIES)
503 os << to_utf8(_(GUISeriesNames[series()])) << ", ";
504 if (shape() != INHERIT_SHAPE)
505 os << to_utf8(_(GUIShapeNames[shape()])) << ", ";
506 if (size() != INHERIT_SIZE)
507 os << to_utf8(_(GUISizeNames[size()])) << ", ";
508 if (color() != LColor::inherit)
509 os << lcolor.getGUIName(color()) << ", ";
510 if (emph() != INHERIT)
511 os << to_utf8(bformat(_("Emphasis %1$s, "),
512 _(GUIMiscNames[emph()])));
513 if (underbar() != INHERIT)
514 os << to_utf8(bformat(_("Underline %1$s, "),
515 _(GUIMiscNames[underbar()])));
516 if (noun() != INHERIT)
517 os << to_utf8(bformat(_("Noun %1$s, "),
518 _(GUIMiscNames[noun()])));
520 os << to_utf8(_("Default")) << ", ";
521 if (!params || (language() != params->language))
522 os << to_utf8(bformat(_("Language: %1$s, "),
523 _(language()->display())));
525 os << to_utf8(bformat(_(" Number %1$s"),
526 _(GUIMiscNames[number()])));
527 return rtrim(os.str(), ", ");
531 // Set family according to lyx format string
532 LyXFont & LyXFont::setLyXFamily(string const & fam)
534 string const s = ascii_lowercase(fam);
537 while (LyXFamilyNames[i] != s &&
538 LyXFamilyNames[i] != string("error"))
540 if (s == LyXFamilyNames[i])
541 setFamily(LyXFont::FONT_FAMILY(i));
543 lyxerr << "LyXFont::setLyXFamily: Unknown family `"
544 << s << '\'' << endl;
549 // Set series according to lyx format string
550 LyXFont & LyXFont::setLyXSeries(string const & ser)
552 string const s = ascii_lowercase(ser);
555 while (LyXSeriesNames[i] != s &&
556 LyXSeriesNames[i] != string("error")) ++i;
557 if (s == LyXSeriesNames[i]) {
558 setSeries(LyXFont::FONT_SERIES(i));
560 lyxerr << "LyXFont::setLyXSeries: Unknown series `"
561 << s << '\'' << endl;
566 // Set shape according to lyx format string
567 LyXFont & LyXFont::setLyXShape(string const & sha)
569 string const s = ascii_lowercase(sha);
572 while (LyXShapeNames[i] != s && LyXShapeNames[i] != string("error"))
574 if (s == LyXShapeNames[i])
575 setShape(LyXFont::FONT_SHAPE(i));
577 lyxerr << "LyXFont::setLyXShape: Unknown shape `"
578 << s << '\'' << endl;
583 // Set size according to lyx format string
584 LyXFont & LyXFont::setLyXSize(string const & siz)
586 string const s = ascii_lowercase(siz);
588 while (LyXSizeNames[i] != s && LyXSizeNames[i] != string("error"))
590 if (s == LyXSizeNames[i]) {
591 setSize(LyXFont::FONT_SIZE(i));
593 lyxerr << "LyXFont::setLyXSize: Unknown size `"
594 << s << '\'' << endl;
599 // Set size according to lyx format string
600 LyXFont::FONT_MISC_STATE LyXFont::setLyXMisc(string const & siz)
602 string const s = ascii_lowercase(siz);
604 while (LyXMiscNames[i] != s &&
605 LyXMiscNames[i] != string("error")) ++i;
606 if (s == LyXMiscNames[i])
607 return FONT_MISC_STATE(i);
608 lyxerr << "LyXFont::setLyXMisc: Unknown misc flag `"
609 << s << '\'' << endl;
614 /// Sets color after LyX text format
615 LyXFont & LyXFont::setLyXColor(string const & col)
617 setColor(lcolor.getFromLyXName(col));
622 // Returns size in latex format
623 string const LyXFont::latexSize() const
625 return LaTeXSizeNames[size()];
629 // Read a font definition from given file in lyx format
631 LyXFont & LyXFont::lyxRead(LyXLex & lex)
634 bool finished = false;
635 while (!finished && lex.isOK() && !error) {
637 string const tok = ascii_lowercase(lex.getString());
641 } else if (tok == "endfont") {
643 } else if (tok == "family") {
645 string const ttok = lex.getString();
647 } else if (tok == "series") {
649 string const ttok = lex.getString();
651 } else if (tok == "shape") {
653 string const ttok = lex.getString();
655 } else if (tok == "size") {
657 string const ttok = lex.getString();
659 } else if (tok == "misc") {
661 string const ttok = ascii_lowercase(lex.getString());
663 if (ttok == "no_bar") {
665 } else if (ttok == "no_emph") {
667 } else if (ttok == "no_noun") {
669 } else if (ttok == "emph") {
671 } else if (ttok == "underbar") {
673 } else if (ttok == "noun") {
676 lex.printError("Illegal misc type `$$Token´");
678 } else if (tok == "color") {
680 string const ttok = lex.getString();
683 lex.printError("Unknown tag `$$Token'");
691 /// Writes the changes from this font to orgfont in .lyx format in file
692 void LyXFont::lyxWriteChanges(LyXFont const & orgfont,
696 if (orgfont.family() != family())
697 os << "\\family " << LyXFamilyNames[family()] << "\n";
698 if (orgfont.series() != series())
699 os << "\\series " << LyXSeriesNames[series()] << "\n";
700 if (orgfont.shape() != shape())
701 os << "\\shape " << LyXShapeNames[shape()] << "\n";
702 if (orgfont.size() != size())
703 os << "\\size " << LyXSizeNames[size()] << "\n";
704 if (orgfont.emph() != emph())
705 os << "\\emph " << LyXMiscNames[emph()] << "\n";
706 if (orgfont.number() != number())
707 os << "\\numeric " << LyXMiscNames[number()] << "\n";
708 if (orgfont.underbar() != underbar()) {
709 // This is only for backwards compatibility
710 switch (underbar()) {
711 case OFF: os << "\\bar no\n"; break;
712 case ON: os << "\\bar under\n"; break;
713 case TOGGLE: lyxerr << "LyXFont::lyxWriteFontChanges: "
714 "TOGGLE should not appear here!"
717 case INHERIT: os << "\\bar default\n"; break;
718 case IGNORE: lyxerr << "LyXFont::lyxWriteFontChanges: "
719 "IGNORE should not appear here!"
724 if (orgfont.noun() != noun()) {
725 os << "\\noun " << LyXMiscNames[noun()] << "\n";
727 if (orgfont.color() != color()) {
728 // To make us file compatible with older
729 // lyx versions we emit "default" instead
731 string col_str(lcolor.getLyXName(color()));
732 if (col_str == "inherit") col_str = "default";
733 os << "\\color " << col_str << "\n";
735 if (orgfont.language() != language() &&
736 language() != latex_language) {
738 os << "\\lang " << language()->lang() << "\n";
740 os << "\\lang unknown\n";
745 /// Writes the head of the LaTeX needed to impose this font
746 // Returns number of chars written.
747 int LyXFont::latexWriteStartChanges(odocstream & os, LyXFont const & base,
748 LyXFont const & prev) const
753 if (language()->babel() != base.language()->babel() &&
754 language() != prev.language()) {
755 if (isRightToLeft() != prev.isRightToLeft()) {
756 if (isRightToLeft()) {
765 subst(lyxrc.language_command_local,
766 "$$lang", language()->babel());
767 os << from_ascii(tmp);
768 count += tmp.length();
772 if (number() == ON && prev.number() != ON &&
773 language()->lang() == "hebrew") {
781 if (f.family() != INHERIT_FAMILY) {
783 << LaTeXFamilyNames[f.family()]
785 count += strlen(LaTeXFamilyNames[f.family()]) + 2;
786 env = true; //We have opened a new environment
788 if (f.series() != INHERIT_SERIES) {
790 << LaTeXSeriesNames[f.series()]
792 count += strlen(LaTeXSeriesNames[f.series()]) + 2;
793 env = true; //We have opened a new environment
795 if (f.shape() != INHERIT_SHAPE) {
797 << LaTeXShapeNames[f.shape()]
799 count += strlen(LaTeXShapeNames[f.shape()]) + 2;
800 env = true; //We have opened a new environment
802 if (f.color() != LColor::inherit && f.color() != LColor::ignore) {
804 << from_ascii(lcolor.getLaTeXName(f.color()))
806 count += lcolor.getLaTeXName(f.color()).length() + 13;
807 env = true; //We have opened a new environment
809 if (f.emph() == ON) {
812 env = true; //We have opened a new environment
814 if (f.underbar() == ON) {
817 env = true; //We have opened a new environment
819 // \noun{} is a LyX special macro
820 if (f.noun() == ON) {
823 env = true; //We have opened a new environment
825 if (f.size() != INHERIT_SIZE) {
826 // If we didn't open an environment above, we open one here
832 << LaTeXSizeNames[f.size()]
834 count += strlen(LaTeXSizeNames[f.size()]) + 2;
840 /// Writes ending block of LaTeX needed to close use of this font
841 // Returns number of chars written
842 // This one corresponds to latexWriteStartChanges(). (Asger)
843 int LyXFont::latexWriteEndChanges(odocstream & os, LyXFont const & base,
844 LyXFont const & next) const
849 // reduce the current font to changes against the base
850 // font (of the layout). We use a temporary for this to
851 // avoid changing this font instance, as that would break
855 if (f.family() != INHERIT_FAMILY) {
858 env = true; // Size change need not bother about closing env.
860 if (f.series() != INHERIT_SERIES) {
863 env = true; // Size change need not bother about closing env.
865 if (f.shape() != INHERIT_SHAPE) {
868 env = true; // Size change need not bother about closing env.
870 if (f.color() != LColor::inherit && f.color() != LColor::ignore) {
873 env = true; // Size change need not bother about closing env.
875 if (f.emph() == ON) {
878 env = true; // Size change need not bother about closing env.
880 if (f.underbar() == ON) {
883 env = true; // Size change need not bother about closing env.
885 if (f.noun() == ON) {
888 env = true; // Size change need not bother about closing env.
890 if (f.size() != INHERIT_SIZE) {
891 // We only have to close if only size changed
898 if (number() == ON && next.number() != ON &&
899 language()->lang() == "hebrew") {
904 if (language() != base.language() && language() != next.language()) {
913 LColor_color LyXFont::realColor() const
915 if (color() == LColor::none)
916 return LColor::foreground;
921 ostream & operator<<(ostream & os, LyXFont::FONT_MISC_STATE fms)
923 return os << int(fms);
927 std::ostream & operator<<(std::ostream & os, LyXFont const & font)
930 << " family " << font.bits.family
931 << " series " << font.bits.series
932 << " shape " << font.bits.shape
933 << " size " << font.bits.size
934 << " color " << font.bits.color
935 << " emph " << font.bits.emph
936 << " underbar " << font.bits.underbar
937 << " noun " << font.bits.noun
938 << " number " << font.bits.number
939 << " lang: " << (font.lang ? font.lang->lang() : 0);