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;
41 #ifndef CXX_GLOBAL_CSTD
51 char const * GUIFamilyNames[LyXFont::NUM_FAMILIES + 2 /* default & error */] =
52 { N_("Roman"), N_("Sans Serif"), N_("Typewriter"), N_("Symbol"),
53 "cmr", "cmsy", "cmm", "cmex", "msa", "msb", "eufrak", "wasy", "esint",
54 N_("Inherit"), N_("Ignore") };
56 char const * GUISeriesNames[4] =
57 { N_("Medium"), N_("Bold"), N_("Inherit"), N_("Ignore") };
59 char const * GUIShapeNames[6] =
60 { N_("Upright"), N_("Italic"), N_("Slanted"), N_("Smallcaps"), N_("Inherit"),
63 char const * GUISizeNames[14] =
64 { N_("Tiny"), N_("Smallest"), N_("Smaller"), N_("Small"), N_("Normal"), N_("Large"),
65 N_("Larger"), N_("Largest"), N_("Huge"), N_("Huger"), N_("Increase"), N_("Decrease"),
66 N_("Inherit"), N_("Ignore") };
68 char const * GUIMiscNames[5] =
69 { N_("Off"), N_("On"), N_("Toggle"), N_("Inherit"), N_("Ignore") };
73 // Strings used to read and write .lyx format files
75 char const * LyXFamilyNames[LyXFont::NUM_FAMILIES + 2 /* default & error */] =
76 { "roman", "sans", "typewriter", "symbol",
77 "cmr", "cmsy", "cmm", "cmex", "msa", "msb", "eufrak", "wasy", "esint",
80 char const * LyXSeriesNames[4] =
81 { "medium", "bold", "default", "error" };
83 char const * LyXShapeNames[6] =
84 { "up", "italic", "slanted", "smallcaps", "default", "error" };
86 char const * LyXSizeNames[14] =
87 { "tiny", "scriptsize", "footnotesize", "small", "normal", "large",
88 "larger", "largest", "huge", "giant",
89 "increase", "decrease", "default", "error" };
91 char const * LyXMiscNames[5] =
92 { "off", "on", "toggle", "default", "error" };
95 // Strings used to write LaTeX files
97 char const * LaTeXFamilyNames[6] =
98 { "textrm", "textsf", "texttt", "error1", "error2", "error3" };
100 char const * LaTeXSeriesNames[4] =
101 { "textmd", "textbf", "error4", "error5" };
103 char const * LaTeXShapeNames[6] =
104 { "textup", "textit", "textsl", "textsc", "error6", "error7" };
106 char const * LaTeXSizeNames[14] =
107 { "tiny", "scriptsize", "footnotesize", "small", "normalsize", "large",
108 "Large", "LARGE", "huge", "Huge", "error8", "error9", "error10", "error11" };
113 // Initialize static member
114 LyXFont::FontBits LyXFont::sane = {
125 // Initialize static member
126 LyXFont::FontBits LyXFont::inherit = {
137 // Initialize static member
138 LyXFont::FontBits LyXFont::ignore = {
150 bool operator==(LyXFont::FontBits const & lhs,
151 LyXFont::FontBits const & rhs)
153 return lhs.family == rhs.family &&
154 lhs.series == rhs.series &&
155 lhs.shape == rhs.shape &&
156 lhs.size == rhs.size &&
157 lhs.color == rhs.color &&
158 lhs.emph == rhs.emph &&
159 lhs.underbar == rhs.underbar &&
160 lhs.noun == rhs.noun &&
161 lhs.number == rhs.number;
166 : bits(sane), lang(default_language)
170 LyXFont::LyXFont(LyXFont::FONT_INIT1)
171 : bits(inherit), lang(default_language)
175 LyXFont::LyXFont(LyXFont::FONT_INIT2)
176 : bits(ignore), lang(ignore_language)
180 LyXFont::LyXFont(LyXFont::FONT_INIT3)
181 : bits(sane), lang(default_language)
185 LyXFont::LyXFont(LyXFont::FONT_INIT1, Language const * l)
186 : bits(inherit), lang(l)
190 LyXFont::LyXFont(LyXFont::FONT_INIT2, Language const * l)
191 : bits(ignore), lang(l)
195 LyXFont::LyXFont(LyXFont::FONT_INIT3, Language const * l)
196 : bits(sane), lang(l)
201 LColor_color LyXFont::color() const
203 return LColor::color(bits.color);
207 bool LyXFont::isRightToLeft() const
209 return lang->rightToLeft();
213 bool LyXFont::isVisibleRightToLeft() const
215 return (lang->rightToLeft() &&
220 void LyXFont::setFamily(LyXFont::FONT_FAMILY f)
226 void LyXFont::setSeries(LyXFont::FONT_SERIES s)
232 void LyXFont::setShape(LyXFont::FONT_SHAPE s)
238 void LyXFont::setSize(LyXFont::FONT_SIZE s)
244 void LyXFont::setEmph(LyXFont::FONT_MISC_STATE e)
250 void LyXFont::setUnderbar(LyXFont::FONT_MISC_STATE u)
256 void LyXFont::setNoun(LyXFont::FONT_MISC_STATE n)
262 void LyXFont::setColor(LColor_color c)
268 void LyXFont::setLanguage(Language const * l)
274 void LyXFont::setNumber(LyXFont::FONT_MISC_STATE n)
280 /// Decreases font size by one
281 LyXFont & LyXFont::decSize()
284 case SIZE_HUGER: setSize(SIZE_HUGE); break;
285 case SIZE_HUGE: setSize(SIZE_LARGEST); break;
286 case SIZE_LARGEST: setSize(SIZE_LARGER); break;
287 case SIZE_LARGER: setSize(SIZE_LARGE); break;
288 case SIZE_LARGE: setSize(SIZE_NORMAL); break;
289 case SIZE_NORMAL: setSize(SIZE_SMALL); break;
290 case SIZE_SMALL: setSize(SIZE_FOOTNOTE); break;
291 case SIZE_FOOTNOTE: setSize(SIZE_SCRIPT); break;
292 case SIZE_SCRIPT: setSize(SIZE_TINY); break;
293 case SIZE_TINY: break;
295 lyxerr << "Can't LyXFont::decSize on INCREASE_SIZE" << endl;
298 lyxerr <<"Can't LyXFont::decSize on DECREASE_SIZE" << endl;
301 lyxerr <<"Can't LyXFont::decSize on INHERIT_SIZE" << endl;
304 lyxerr <<"Can't LyXFont::decSize on IGNORE_SIZE" << endl;
311 /// Increases font size by one
312 LyXFont & LyXFont::incSize()
315 case SIZE_HUGER: break;
316 case SIZE_HUGE: setSize(SIZE_HUGER); break;
317 case SIZE_LARGEST: setSize(SIZE_HUGE); break;
318 case SIZE_LARGER: setSize(SIZE_LARGEST); break;
319 case SIZE_LARGE: setSize(SIZE_LARGER); break;
320 case SIZE_NORMAL: setSize(SIZE_LARGE); break;
321 case SIZE_SMALL: setSize(SIZE_NORMAL); break;
322 case SIZE_FOOTNOTE: setSize(SIZE_SMALL); break;
323 case SIZE_SCRIPT: setSize(SIZE_FOOTNOTE); break;
324 case SIZE_TINY: setSize(SIZE_SCRIPT); break;
326 lyxerr <<"Can't LyXFont::incSize on INCREASE_SIZE" << endl;
329 lyxerr <<"Can't LyXFont::incSize on DECREASE_SIZE" << endl;
332 lyxerr <<"Can't LyXFont::incSize on INHERIT_SIZE" << endl;
335 lyxerr <<"Can't LyXFont::incSize on IGNORE_SIZE" << endl;
342 /// Updates a misc setting according to request
343 LyXFont::FONT_MISC_STATE LyXFont::setMisc(FONT_MISC_STATE newfont,
346 if (newfont == TOGGLE) {
352 lyxerr <<"LyXFont::setMisc: Need state"
353 " ON or OFF to toggle. Setting to ON" << endl;
356 } else if (newfont == IGNORE)
363 /// Updates font settings according to request
364 void LyXFont::update(LyXFont const & newfont,
365 Language const * document_language,
368 if (newfont.family() == family() && toggleall)
369 setFamily(INHERIT_FAMILY); // toggle 'back'
370 else if (newfont.family() != IGNORE_FAMILY)
371 setFamily(newfont.family());
372 // else it's IGNORE_SHAPE
374 // "Old" behaviour: "Setting" bold will toggle bold on/off.
375 switch (newfont.series()) {
378 if (series() == BOLD_SERIES && toggleall)
379 setSeries(MEDIUM_SERIES);
381 setSeries(BOLD_SERIES);
385 setSeries(newfont.series());
391 if (newfont.shape() == shape() && toggleall)
392 setShape(INHERIT_SHAPE); // toggle 'back'
393 else if (newfont.shape() != IGNORE_SHAPE)
394 setShape(newfont.shape());
395 // else it's IGNORE_SHAPE
397 if (newfont.size() != IGNORE_SIZE) {
398 if (newfont.size() == INCREASE_SIZE)
400 else if (newfont.size() == DECREASE_SIZE)
403 setSize(newfont.size());
406 setEmph(setMisc(newfont.emph(), emph()));
407 setUnderbar(setMisc(newfont.underbar(), underbar()));
408 setNoun(setMisc(newfont.noun(), noun()));
410 setNumber(setMisc(newfont.number(), number()));
411 if (newfont.language() == language() && toggleall)
412 if (language() == document_language)
413 setLanguage(default_language);
415 setLanguage(document_language);
416 else if (newfont.language() != ignore_language)
417 setLanguage(newfont.language());
419 if (newfont.color() == color() && toggleall)
420 setColor(LColor::inherit); // toggle 'back'
421 else if (newfont.color() != LColor::ignore)
422 setColor(newfont.color());
426 /// Reduce font to fall back to template where possible
427 void LyXFont::reduce(LyXFont const & tmplt)
429 if (family() == tmplt.family())
430 setFamily(INHERIT_FAMILY);
431 if (series() == tmplt.series())
432 setSeries(INHERIT_SERIES);
433 if (shape() == tmplt.shape())
434 setShape(INHERIT_SHAPE);
435 if (size() == tmplt.size())
436 setSize(INHERIT_SIZE);
437 if (emph() == tmplt.emph())
439 if (underbar() == tmplt.underbar())
440 setUnderbar(INHERIT);
441 if (noun() == tmplt.noun())
443 if (color() == tmplt.color())
444 setColor(LColor::inherit);
448 /// Realize font from a template
449 LyXFont & LyXFont::realize(LyXFont const & tmplt)
451 if (bits == inherit) {
456 if (bits.family == INHERIT_FAMILY)
457 bits.family = tmplt.bits.family;
459 if (bits.series == INHERIT_SERIES)
460 bits.series = tmplt.bits.series;
462 if (bits.shape == INHERIT_SHAPE)
463 bits.shape = tmplt.bits.shape;
465 if (bits.size == INHERIT_SIZE)
466 bits.size = tmplt.bits.size;
468 if (bits.emph == INHERIT)
469 bits.emph = tmplt.bits.emph;
471 if (bits.underbar == INHERIT)
472 bits.underbar = tmplt.bits.underbar;
474 if (bits.noun == INHERIT)
475 bits.noun = tmplt.bits.noun;
477 if (bits.color == LColor::inherit)
478 bits.color = tmplt.bits.color;
484 /// Is font resolved?
485 bool LyXFont::resolved() const
487 return (family() != INHERIT_FAMILY && series() != INHERIT_SERIES &&
488 shape() != INHERIT_SHAPE && size() != INHERIT_SIZE &&
489 emph() != INHERIT && underbar() != INHERIT &&
491 color() != LColor::inherit);
495 docstring const LyXFont::stateText(BufferParams * params) const
498 if (family() != INHERIT_FAMILY)
499 os << _(GUIFamilyNames[family()]) << ", ";
500 if (series() != INHERIT_SERIES)
501 os << _(GUISeriesNames[series()]) << ", ";
502 if (shape() != INHERIT_SHAPE)
503 os << _(GUIShapeNames[shape()]) << ", ";
504 if (size() != INHERIT_SIZE)
505 os << _(GUISizeNames[size()]) << ", ";
506 if (color() != LColor::inherit)
507 os << lcolor.getGUIName(color()) << ", ";
508 if (emph() != INHERIT)
509 os << bformat(_("Emphasis %1$s, "),
510 _(GUIMiscNames[emph()]));
511 if (underbar() != INHERIT)
512 os << bformat(_("Underline %1$s, "),
513 _(GUIMiscNames[underbar()]));
514 if (noun() != INHERIT)
515 os << bformat(_("Noun %1$s, "),
516 _(GUIMiscNames[noun()]));
518 os << _("Default") << ", ";
519 if (!params || (language() != params->language))
520 os << bformat(_("Language: %1$s, "),
521 _(language()->display()));
523 os << bformat(_(" Number %1$s"),
524 _(GUIMiscNames[number()]));
525 return rtrim(os.str(), ", ");
529 // Set family according to lyx format string
530 LyXFont & LyXFont::setLyXFamily(string const & fam)
532 string const s = ascii_lowercase(fam);
535 while (LyXFamilyNames[i] != s &&
536 LyXFamilyNames[i] != string("error"))
538 if (s == LyXFamilyNames[i])
539 setFamily(LyXFont::FONT_FAMILY(i));
541 lyxerr << "LyXFont::setLyXFamily: Unknown family `"
542 << s << '\'' << endl;
547 // Set series according to lyx format string
548 LyXFont & LyXFont::setLyXSeries(string const & ser)
550 string const s = ascii_lowercase(ser);
553 while (LyXSeriesNames[i] != s &&
554 LyXSeriesNames[i] != string("error")) ++i;
555 if (s == LyXSeriesNames[i]) {
556 setSeries(LyXFont::FONT_SERIES(i));
558 lyxerr << "LyXFont::setLyXSeries: Unknown series `"
559 << s << '\'' << endl;
564 // Set shape according to lyx format string
565 LyXFont & LyXFont::setLyXShape(string const & sha)
567 string const s = ascii_lowercase(sha);
570 while (LyXShapeNames[i] != s && LyXShapeNames[i] != string("error"))
572 if (s == LyXShapeNames[i])
573 setShape(LyXFont::FONT_SHAPE(i));
575 lyxerr << "LyXFont::setLyXShape: Unknown shape `"
576 << s << '\'' << endl;
581 // Set size according to lyx format string
582 LyXFont & LyXFont::setLyXSize(string const & siz)
584 string const s = ascii_lowercase(siz);
586 while (LyXSizeNames[i] != s && LyXSizeNames[i] != string("error"))
588 if (s == LyXSizeNames[i]) {
589 setSize(LyXFont::FONT_SIZE(i));
591 lyxerr << "LyXFont::setLyXSize: Unknown size `"
592 << s << '\'' << endl;
597 // Set size according to lyx format string
598 LyXFont::FONT_MISC_STATE LyXFont::setLyXMisc(string const & siz)
600 string const s = ascii_lowercase(siz);
602 while (LyXMiscNames[i] != s &&
603 LyXMiscNames[i] != string("error")) ++i;
604 if (s == LyXMiscNames[i])
605 return FONT_MISC_STATE(i);
606 lyxerr << "LyXFont::setLyXMisc: Unknown misc flag `"
607 << s << '\'' << endl;
612 /// Sets color after LyX text format
613 LyXFont & LyXFont::setLyXColor(string const & col)
615 setColor(lcolor.getFromLyXName(col));
620 // Returns size in latex format
621 string const LyXFont::latexSize() const
623 return LaTeXSizeNames[size()];
627 // Read a font definition from given file in lyx format
629 LyXFont & LyXFont::lyxRead(LyXLex & lex)
632 bool finished = false;
633 while (!finished && lex.isOK() && !error) {
635 string const tok = ascii_lowercase(lex.getString());
639 } else if (tok == "endfont") {
641 } else if (tok == "family") {
643 string const ttok = lex.getString();
645 } else if (tok == "series") {
647 string const ttok = lex.getString();
649 } else if (tok == "shape") {
651 string const ttok = lex.getString();
653 } else if (tok == "size") {
655 string const ttok = lex.getString();
657 } else if (tok == "misc") {
659 string const ttok = ascii_lowercase(lex.getString());
661 if (ttok == "no_bar") {
663 } else if (ttok == "no_emph") {
665 } else if (ttok == "no_noun") {
667 } else if (ttok == "emph") {
669 } else if (ttok == "underbar") {
671 } else if (ttok == "noun") {
674 lex.printError("Illegal misc type `$$Token´");
676 } else if (tok == "color") {
678 string const ttok = lex.getString();
681 lex.printError("Unknown tag `$$Token'");
689 /// Writes the changes from this font to orgfont in .lyx format in file
690 void LyXFont::lyxWriteChanges(LyXFont const & orgfont,
694 if (orgfont.family() != family())
695 os << "\\family " << LyXFamilyNames[family()] << "\n";
696 if (orgfont.series() != series())
697 os << "\\series " << LyXSeriesNames[series()] << "\n";
698 if (orgfont.shape() != shape())
699 os << "\\shape " << LyXShapeNames[shape()] << "\n";
700 if (orgfont.size() != size())
701 os << "\\size " << LyXSizeNames[size()] << "\n";
702 if (orgfont.emph() != emph())
703 os << "\\emph " << LyXMiscNames[emph()] << "\n";
704 if (orgfont.number() != number())
705 os << "\\numeric " << LyXMiscNames[number()] << "\n";
706 if (orgfont.underbar() != underbar()) {
707 // This is only for backwards compatibility
708 switch (underbar()) {
709 case OFF: os << "\\bar no\n"; break;
710 case ON: os << "\\bar under\n"; break;
711 case TOGGLE: lyxerr << "LyXFont::lyxWriteFontChanges: "
712 "TOGGLE should not appear here!"
715 case INHERIT: os << "\\bar default\n"; break;
716 case IGNORE: lyxerr << "LyXFont::lyxWriteFontChanges: "
717 "IGNORE should not appear here!"
722 if (orgfont.noun() != noun()) {
723 os << "\\noun " << LyXMiscNames[noun()] << "\n";
725 if (orgfont.color() != color())
726 os << "\\color " << lcolor.getLyXName(color()) << '\n';
727 if (orgfont.language() != language() &&
728 language() != latex_language) {
730 os << "\\lang " << language()->lang() << "\n";
732 os << "\\lang unknown\n";
737 /// Writes the head of the LaTeX needed to impose this font
738 // Returns number of chars written.
739 int LyXFont::latexWriteStartChanges(odocstream & os, LyXFont const & base,
740 LyXFont const & prev) const
745 if (language()->babel() != base.language()->babel() &&
746 language() != prev.language()) {
747 if (isRightToLeft() != prev.isRightToLeft()) {
748 if (isRightToLeft()) {
757 subst(lyxrc.language_command_local,
758 "$$lang", language()->babel());
759 os << from_ascii(tmp);
760 count += tmp.length();
764 if (number() == ON && prev.number() != ON &&
765 language()->lang() == "hebrew") {
773 if (f.family() != INHERIT_FAMILY) {
775 << LaTeXFamilyNames[f.family()]
777 count += strlen(LaTeXFamilyNames[f.family()]) + 2;
778 env = true; //We have opened a new environment
780 if (f.series() != INHERIT_SERIES) {
782 << LaTeXSeriesNames[f.series()]
784 count += strlen(LaTeXSeriesNames[f.series()]) + 2;
785 env = true; //We have opened a new environment
787 if (f.shape() != INHERIT_SHAPE) {
789 << LaTeXShapeNames[f.shape()]
791 count += strlen(LaTeXShapeNames[f.shape()]) + 2;
792 env = true; //We have opened a new environment
794 if (f.color() != LColor::inherit && f.color() != LColor::ignore) {
796 << from_ascii(lcolor.getLaTeXName(f.color()))
798 count += lcolor.getLaTeXName(f.color()).length() + 13;
799 env = true; //We have opened a new environment
801 if (f.emph() == ON) {
804 env = true; //We have opened a new environment
806 if (f.underbar() == ON) {
809 env = true; //We have opened a new environment
811 // \noun{} is a LyX special macro
812 if (f.noun() == ON) {
815 env = true; //We have opened a new environment
817 if (f.size() != INHERIT_SIZE) {
818 // If we didn't open an environment above, we open one here
824 << LaTeXSizeNames[f.size()]
826 count += strlen(LaTeXSizeNames[f.size()]) + 2;
832 /// Writes ending block of LaTeX needed to close use of this font
833 // Returns number of chars written
834 // This one corresponds to latexWriteStartChanges(). (Asger)
835 int LyXFont::latexWriteEndChanges(odocstream & os, LyXFont const & base,
836 LyXFont const & next) const
841 // reduce the current font to changes against the base
842 // font (of the layout). We use a temporary for this to
843 // avoid changing this font instance, as that would break
847 if (f.family() != INHERIT_FAMILY) {
850 env = true; // Size change need not bother about closing env.
852 if (f.series() != INHERIT_SERIES) {
855 env = true; // Size change need not bother about closing env.
857 if (f.shape() != INHERIT_SHAPE) {
860 env = true; // Size change need not bother about closing env.
862 if (f.color() != LColor::inherit && f.color() != LColor::ignore) {
865 env = true; // Size change need not bother about closing env.
867 if (f.emph() == ON) {
870 env = true; // Size change need not bother about closing env.
872 if (f.underbar() == ON) {
875 env = true; // Size change need not bother about closing env.
877 if (f.noun() == ON) {
880 env = true; // Size change need not bother about closing env.
882 if (f.size() != INHERIT_SIZE) {
883 // We only have to close if only size changed
890 if (number() == ON && next.number() != ON &&
891 language()->lang() == "hebrew") {
896 if (language() != base.language() && language() != next.language()) {
905 LColor_color LyXFont::realColor() const
907 if (color() == LColor::none)
908 return LColor::foreground;
913 ostream & operator<<(ostream & os, LyXFont::FONT_MISC_STATE fms)
915 return os << int(fms);
919 std::ostream & operator<<(std::ostream & os, LyXFont const & font)
922 << " family " << font.bits.family
923 << " series " << font.bits.series
924 << " shape " << font.bits.shape
925 << " size " << font.bits.size
926 << " color " << font.bits.color
927 << " emph " << font.bits.emph
928 << " underbar " << font.bits.underbar
929 << " noun " << font.bits.noun
930 << " number " << font.bits.number
931 << " lang: " << (font.lang ? font.lang->lang() : 0);