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 "output_latex.h"
28 #include "OutputParams.h"
30 #include "support/lstrings.h"
37 #ifndef CXX_GLOBAL_CSTD
44 using support::ascii_lowercase;
45 using support::bformat;
55 char const * GUIFamilyNames[Font::NUM_FAMILIES + 2 /* default & error */] =
56 { N_("Roman"), N_("Sans Serif"), N_("Typewriter"), N_("Symbol"),
57 "cmr", "cmsy", "cmm", "cmex", "msa", "msb", "eufrak", "wasy", "esint",
58 N_("Inherit"), N_("Ignore") };
60 char const * GUISeriesNames[4] =
61 { N_("Medium"), N_("Bold"), N_("Inherit"), N_("Ignore") };
63 char const * GUIShapeNames[6] =
64 { N_("Upright"), N_("Italic"), N_("Slanted"), N_("Smallcaps"), N_("Inherit"),
67 char const * GUISizeNames[14] =
68 { N_("Tiny"), N_("Smallest"), N_("Smaller"), N_("Small"), N_("Normal"), N_("Large"),
69 N_("Larger"), N_("Largest"), N_("Huge"), N_("Huger"), N_("Increase"), N_("Decrease"),
70 N_("Inherit"), N_("Ignore") };
72 char const * GUIMiscNames[5] =
73 { N_("Off"), N_("On"), N_("Toggle"), N_("Inherit"), N_("Ignore") };
77 // Strings used to read and write .lyx format files
79 char const * LyXFamilyNames[Font::NUM_FAMILIES + 2 /* default & error */] =
80 { "roman", "sans", "typewriter", "symbol",
81 "cmr", "cmsy", "cmm", "cmex", "msa", "msb", "eufrak", "wasy", "esint",
84 char const * LyXSeriesNames[4] =
85 { "medium", "bold", "default", "error" };
87 char const * LyXShapeNames[6] =
88 { "up", "italic", "slanted", "smallcaps", "default", "error" };
90 char const * LyXSizeNames[14] =
91 { "tiny", "scriptsize", "footnotesize", "small", "normal", "large",
92 "larger", "largest", "huge", "giant",
93 "increase", "decrease", "default", "error" };
95 char const * LyXMiscNames[5] =
96 { "off", "on", "toggle", "default", "error" };
99 // Strings used to write LaTeX files
101 char const * LaTeXFamilyNames[6] =
102 { "textrm", "textsf", "texttt", "error1", "error2", "error3" };
104 char const * LaTeXSeriesNames[4] =
105 { "textmd", "textbf", "error4", "error5" };
107 char const * LaTeXShapeNames[6] =
108 { "textup", "textit", "textsl", "textsc", "error6", "error7" };
110 char const * LaTeXSizeNames[14] =
111 { "tiny", "scriptsize", "footnotesize", "small", "normalsize", "large",
112 "Large", "LARGE", "huge", "Huge", "error8", "error9", "error10", "error11" };
117 // Initialize static member
118 Font::FontBits Font::sane = {
129 // Initialize static member
130 Font::FontBits Font::inherit = {
141 // Initialize static member
142 Font::FontBits Font::ignore = {
154 bool operator==(Font::FontBits const & lhs,
155 Font::FontBits const & rhs)
157 return lhs.family == rhs.family &&
158 lhs.series == rhs.series &&
159 lhs.shape == rhs.shape &&
160 lhs.size == rhs.size &&
161 lhs.color == rhs.color &&
162 lhs.emph == rhs.emph &&
163 lhs.underbar == rhs.underbar &&
164 lhs.noun == rhs.noun &&
165 lhs.number == rhs.number;
170 : bits(sane), lang(default_language), open_encoding_(false)
174 Font::Font(Font::FONT_INIT1)
175 : bits(inherit), lang(default_language), open_encoding_(false)
179 Font::Font(Font::FONT_INIT2)
180 : bits(ignore), lang(ignore_language), open_encoding_(false)
184 Font::Font(Font::FONT_INIT3)
185 : bits(sane), lang(default_language), open_encoding_(false)
189 Font::Font(Font::FONT_INIT1, Language const * l)
190 : bits(inherit), lang(l), open_encoding_(false)
194 Font::Font(Font::FONT_INIT2, Language const * l)
195 : bits(ignore), lang(l), open_encoding_(false)
199 Font::Font(Font::FONT_INIT3, Language const * l)
200 : bits(sane), lang(l), open_encoding_(false)
205 Color_color Font::color() const
207 return Color::color(bits.color);
211 bool Font::isRightToLeft() const
213 return lang->rightToLeft();
217 bool Font::isVisibleRightToLeft() const
219 return (lang->rightToLeft() &&
224 void Font::setFamily(Font::FONT_FAMILY f)
230 void Font::setSeries(Font::FONT_SERIES s)
236 void Font::setShape(Font::FONT_SHAPE s)
242 void Font::setSize(Font::FONT_SIZE s)
248 void Font::setEmph(Font::FONT_MISC_STATE e)
254 void Font::setUnderbar(Font::FONT_MISC_STATE u)
260 void Font::setNoun(Font::FONT_MISC_STATE n)
266 void Font::setColor(Color_color c)
272 void Font::setLanguage(Language const * l)
278 void Font::setNumber(Font::FONT_MISC_STATE n)
284 /// Decreases font size by one
285 Font & Font::decSize()
288 case SIZE_HUGER: setSize(SIZE_HUGE); break;
289 case SIZE_HUGE: setSize(SIZE_LARGEST); break;
290 case SIZE_LARGEST: setSize(SIZE_LARGER); break;
291 case SIZE_LARGER: setSize(SIZE_LARGE); break;
292 case SIZE_LARGE: setSize(SIZE_NORMAL); break;
293 case SIZE_NORMAL: setSize(SIZE_SMALL); break;
294 case SIZE_SMALL: setSize(SIZE_FOOTNOTE); break;
295 case SIZE_FOOTNOTE: setSize(SIZE_SCRIPT); break;
296 case SIZE_SCRIPT: setSize(SIZE_TINY); break;
297 case SIZE_TINY: break;
299 lyxerr << "Can't Font::decSize on INCREASE_SIZE" << endl;
302 lyxerr <<"Can't Font::decSize on DECREASE_SIZE" << endl;
305 lyxerr <<"Can't Font::decSize on INHERIT_SIZE" << endl;
308 lyxerr <<"Can't Font::decSize on IGNORE_SIZE" << endl;
315 /// Increases font size by one
316 Font & Font::incSize()
319 case SIZE_HUGER: break;
320 case SIZE_HUGE: setSize(SIZE_HUGER); break;
321 case SIZE_LARGEST: setSize(SIZE_HUGE); break;
322 case SIZE_LARGER: setSize(SIZE_LARGEST); break;
323 case SIZE_LARGE: setSize(SIZE_LARGER); break;
324 case SIZE_NORMAL: setSize(SIZE_LARGE); break;
325 case SIZE_SMALL: setSize(SIZE_NORMAL); break;
326 case SIZE_FOOTNOTE: setSize(SIZE_SMALL); break;
327 case SIZE_SCRIPT: setSize(SIZE_FOOTNOTE); break;
328 case SIZE_TINY: setSize(SIZE_SCRIPT); break;
330 lyxerr <<"Can't Font::incSize on INCREASE_SIZE" << endl;
333 lyxerr <<"Can't Font::incSize on DECREASE_SIZE" << endl;
336 lyxerr <<"Can't Font::incSize on INHERIT_SIZE" << endl;
339 lyxerr <<"Can't Font::incSize on IGNORE_SIZE" << endl;
346 /// Updates a misc setting according to request
347 Font::FONT_MISC_STATE Font::setMisc(FONT_MISC_STATE newfont,
350 if (newfont == TOGGLE) {
356 lyxerr <<"Font::setMisc: Need state"
357 " ON or OFF to toggle. Setting to ON" << endl;
360 } else if (newfont == IGNORE)
367 /// Updates font settings according to request
368 void Font::update(Font const & newfont,
369 Language const * document_language,
372 if (newfont.family() == family() && toggleall)
373 setFamily(INHERIT_FAMILY); // toggle 'back'
374 else if (newfont.family() != IGNORE_FAMILY)
375 setFamily(newfont.family());
376 // else it's IGNORE_SHAPE
378 // "Old" behaviour: "Setting" bold will toggle bold on/off.
379 switch (newfont.series()) {
382 if (series() == BOLD_SERIES && toggleall)
383 setSeries(MEDIUM_SERIES);
385 setSeries(BOLD_SERIES);
389 setSeries(newfont.series());
395 if (newfont.shape() == shape() && toggleall)
396 setShape(INHERIT_SHAPE); // toggle 'back'
397 else if (newfont.shape() != IGNORE_SHAPE)
398 setShape(newfont.shape());
399 // else it's IGNORE_SHAPE
401 if (newfont.size() != IGNORE_SIZE) {
402 if (newfont.size() == INCREASE_SIZE)
404 else if (newfont.size() == DECREASE_SIZE)
407 setSize(newfont.size());
410 setEmph(setMisc(newfont.emph(), emph()));
411 setUnderbar(setMisc(newfont.underbar(), underbar()));
412 setNoun(setMisc(newfont.noun(), noun()));
414 setNumber(setMisc(newfont.number(), number()));
415 if (newfont.language() == language() && toggleall)
416 if (language() == document_language)
417 setLanguage(default_language);
419 setLanguage(document_language);
420 else if (newfont.language() != ignore_language)
421 setLanguage(newfont.language());
423 if (newfont.color() == color() && toggleall)
424 setColor(Color::inherit); // toggle 'back'
425 else if (newfont.color() != Color::ignore)
426 setColor(newfont.color());
430 /// Reduce font to fall back to template where possible
431 void Font::reduce(Font const & tmplt)
433 if (family() == tmplt.family())
434 setFamily(INHERIT_FAMILY);
435 if (series() == tmplt.series())
436 setSeries(INHERIT_SERIES);
437 if (shape() == tmplt.shape())
438 setShape(INHERIT_SHAPE);
439 if (size() == tmplt.size())
440 setSize(INHERIT_SIZE);
441 if (emph() == tmplt.emph())
443 if (underbar() == tmplt.underbar())
444 setUnderbar(INHERIT);
445 if (noun() == tmplt.noun())
447 if (color() == tmplt.color())
448 setColor(Color::inherit);
452 /// Realize font from a template
453 Font & Font::realize(Font const & tmplt)
455 if (bits == inherit) {
460 if (bits.family == INHERIT_FAMILY)
461 bits.family = tmplt.bits.family;
463 if (bits.series == INHERIT_SERIES)
464 bits.series = tmplt.bits.series;
466 if (bits.shape == INHERIT_SHAPE)
467 bits.shape = tmplt.bits.shape;
469 if (bits.size == INHERIT_SIZE)
470 bits.size = tmplt.bits.size;
472 if (bits.emph == INHERIT)
473 bits.emph = tmplt.bits.emph;
475 if (bits.underbar == INHERIT)
476 bits.underbar = tmplt.bits.underbar;
478 if (bits.noun == INHERIT)
479 bits.noun = tmplt.bits.noun;
481 if (bits.color == Color::inherit)
482 bits.color = tmplt.bits.color;
488 /// Is font resolved?
489 bool Font::resolved() const
491 return (family() != INHERIT_FAMILY && series() != INHERIT_SERIES &&
492 shape() != INHERIT_SHAPE && size() != INHERIT_SIZE &&
493 emph() != INHERIT && underbar() != INHERIT &&
495 color() != Color::inherit);
499 docstring const Font::stateText(BufferParams * params) const
502 if (family() != INHERIT_FAMILY)
503 os << _(GUIFamilyNames[family()]) << ", ";
504 if (series() != INHERIT_SERIES)
505 os << _(GUISeriesNames[series()]) << ", ";
506 if (shape() != INHERIT_SHAPE)
507 os << _(GUIShapeNames[shape()]) << ", ";
508 if (size() != INHERIT_SIZE)
509 os << _(GUISizeNames[size()]) << ", ";
510 if (color() != Color::inherit)
511 os << lcolor.getGUIName(color()) << ", ";
512 if (emph() != INHERIT)
513 os << bformat(_("Emphasis %1$s, "),
514 _(GUIMiscNames[emph()]));
515 if (underbar() != INHERIT)
516 os << bformat(_("Underline %1$s, "),
517 _(GUIMiscNames[underbar()]));
518 if (noun() != INHERIT)
519 os << bformat(_("Noun %1$s, "),
520 _(GUIMiscNames[noun()]));
522 os << _("Default") << ", ";
523 if (!params || (language() != params->language))
524 os << bformat(_("Language: %1$s, "),
525 _(language()->display()));
527 os << bformat(_(" Number %1$s"),
528 _(GUIMiscNames[number()]));
529 return rtrim(os.str(), ", ");
533 // Set family according to lyx format string
534 Font & Font::setLyXFamily(string const & fam)
536 string const s = ascii_lowercase(fam);
539 while (LyXFamilyNames[i] != s &&
540 LyXFamilyNames[i] != string("error"))
542 if (s == LyXFamilyNames[i])
543 setFamily(Font::FONT_FAMILY(i));
545 lyxerr << "Font::setLyXFamily: Unknown family `"
546 << s << '\'' << endl;
551 // Set series according to lyx format string
552 Font & Font::setLyXSeries(string const & ser)
554 string const s = ascii_lowercase(ser);
557 while (LyXSeriesNames[i] != s &&
558 LyXSeriesNames[i] != string("error")) ++i;
559 if (s == LyXSeriesNames[i]) {
560 setSeries(Font::FONT_SERIES(i));
562 lyxerr << "Font::setLyXSeries: Unknown series `"
563 << s << '\'' << endl;
568 // Set shape according to lyx format string
569 Font & Font::setLyXShape(string const & sha)
571 string const s = ascii_lowercase(sha);
574 while (LyXShapeNames[i] != s && LyXShapeNames[i] != string("error"))
576 if (s == LyXShapeNames[i])
577 setShape(Font::FONT_SHAPE(i));
579 lyxerr << "Font::setLyXShape: Unknown shape `"
580 << s << '\'' << endl;
585 // Set size according to lyx format string
586 Font & Font::setLyXSize(string const & siz)
588 string const s = ascii_lowercase(siz);
590 while (LyXSizeNames[i] != s && LyXSizeNames[i] != string("error"))
592 if (s == LyXSizeNames[i]) {
593 setSize(Font::FONT_SIZE(i));
595 lyxerr << "Font::setLyXSize: Unknown size `"
596 << s << '\'' << endl;
601 // Set size according to lyx format string
602 Font::FONT_MISC_STATE Font::setLyXMisc(string const & siz)
604 string const s = ascii_lowercase(siz);
606 while (LyXMiscNames[i] != s &&
607 LyXMiscNames[i] != string("error")) ++i;
608 if (s == LyXMiscNames[i])
609 return FONT_MISC_STATE(i);
610 lyxerr << "Font::setLyXMisc: Unknown misc flag `"
611 << s << '\'' << endl;
616 /// Sets color after LyX text format
617 Font & Font::setLyXColor(string const & col)
619 setColor(lcolor.getFromLyXName(col));
624 // Returns size in latex format
625 string const Font::latexSize() const
627 return LaTeXSizeNames[size()];
631 // Read a font definition from given file in lyx format
633 Font & Font::lyxRead(Lexer & lex)
636 bool finished = false;
637 while (!finished && lex.isOK() && !error) {
639 string const tok = ascii_lowercase(lex.getString());
643 } else if (tok == "endfont") {
645 } else if (tok == "family") {
647 string const ttok = lex.getString();
649 } else if (tok == "series") {
651 string const ttok = lex.getString();
653 } else if (tok == "shape") {
655 string const ttok = lex.getString();
657 } else if (tok == "size") {
659 string const ttok = lex.getString();
661 } else if (tok == "misc") {
663 string const ttok = ascii_lowercase(lex.getString());
665 if (ttok == "no_bar") {
667 } else if (ttok == "no_emph") {
669 } else if (ttok == "no_noun") {
671 } else if (ttok == "emph") {
673 } else if (ttok == "underbar") {
675 } else if (ttok == "noun") {
678 lex.printError("Illegal misc type `$$Token'");
680 } else if (tok == "color") {
682 string const ttok = lex.getString();
685 lex.printError("Unknown tag `$$Token'");
693 /// Writes the changes from this font to orgfont in .lyx format in file
694 void Font::lyxWriteChanges(Font const & orgfont,
698 if (orgfont.family() != family())
699 os << "\\family " << LyXFamilyNames[family()] << "\n";
700 if (orgfont.series() != series())
701 os << "\\series " << LyXSeriesNames[series()] << "\n";
702 if (orgfont.shape() != shape())
703 os << "\\shape " << LyXShapeNames[shape()] << "\n";
704 if (orgfont.size() != size())
705 os << "\\size " << LyXSizeNames[size()] << "\n";
706 if (orgfont.emph() != emph())
707 os << "\\emph " << LyXMiscNames[emph()] << "\n";
708 if (orgfont.number() != number())
709 os << "\\numeric " << LyXMiscNames[number()] << "\n";
710 if (orgfont.underbar() != underbar()) {
711 // This is only for backwards compatibility
712 switch (underbar()) {
713 case OFF: os << "\\bar no\n"; break;
714 case ON: os << "\\bar under\n"; break;
715 case TOGGLE: lyxerr << "Font::lyxWriteFontChanges: "
716 "TOGGLE should not appear here!"
719 case INHERIT: os << "\\bar default\n"; break;
720 case IGNORE: lyxerr << "Font::lyxWriteFontChanges: "
721 "IGNORE should not appear here!"
726 if (orgfont.noun() != noun()) {
727 os << "\\noun " << LyXMiscNames[noun()] << "\n";
729 if (orgfont.color() != color())
730 os << "\\color " << lcolor.getLyXName(color()) << '\n';
731 if (orgfont.language() != language() &&
732 language() != latex_language) {
734 os << "\\lang " << language()->lang() << "\n";
736 os << "\\lang unknown\n";
741 /// Writes the head of the LaTeX needed to impose this font
742 // Returns number of chars written.
743 int Font::latexWriteStartChanges(odocstream & os, BufferParams const & bparams,
744 OutputParams const & runparams,
746 Font const & prev) const
751 if (language()->babel() != base.language()->babel() &&
752 language() != prev.language()) {
753 if (language()->lang() == "farsi") {
756 } else if (!isRightToLeft() &&
757 base.language()->lang() == "farsi") {
760 } else if (language()->lang() == "arabic_arabi") {
763 } else if (!isRightToLeft() &&
764 base.language()->lang() == "arabic_arabi") {
767 // currently the remaining RTL languages are arabic_arabtex and hebrew
768 } else if (isRightToLeft() != prev.isRightToLeft()) {
769 if (isRightToLeft()) {
776 } else if (!language()->babel().empty()) {
778 subst(lyxrc.language_command_local,
779 "$$lang", language()->babel());
780 os << from_ascii(tmp);
781 count += tmp.length();
788 if (language()->encoding()->package() == Encoding::CJK) {
789 pair<bool, int> const c = switchEncoding(os, bparams,
790 runparams.moving_arg, *(runparams.encoding),
791 *(language()->encoding()));
793 open_encoding_ = true;
795 runparams.encoding = language()->encoding();
799 // When the current language is Hebrew, Arabic, or Farsi
800 // the numbers are written Left-to-Right. ArabTeX package
801 // reorders the number automatically but the packages used
802 // for Hebrew and Farsi (Arabi) do not.
803 if (number() == ON && prev.number() != ON
804 && (language()->lang() == "hebrew"
805 || language()->lang() == "farsi"
806 || language()->lang() == "arabic_arabi")) {
814 if (f.family() != INHERIT_FAMILY) {
816 << LaTeXFamilyNames[f.family()]
818 count += strlen(LaTeXFamilyNames[f.family()]) + 2;
819 env = true; //We have opened a new environment
821 if (f.series() != INHERIT_SERIES) {
823 << LaTeXSeriesNames[f.series()]
825 count += strlen(LaTeXSeriesNames[f.series()]) + 2;
826 env = true; //We have opened a new environment
828 if (f.shape() != INHERIT_SHAPE) {
830 << LaTeXShapeNames[f.shape()]
832 count += strlen(LaTeXShapeNames[f.shape()]) + 2;
833 env = true; //We have opened a new environment
835 if (f.color() != Color::inherit && f.color() != Color::ignore) {
837 << from_ascii(lcolor.getLaTeXName(f.color()))
839 count += lcolor.getLaTeXName(f.color()).length() + 13;
840 env = true; //We have opened a new environment
842 if (f.emph() == ON) {
845 env = true; //We have opened a new environment
847 if (f.underbar() == ON) {
850 env = true; //We have opened a new environment
852 // \noun{} is a LyX special macro
853 if (f.noun() == ON) {
856 env = true; //We have opened a new environment
858 if (f.size() != INHERIT_SIZE) {
859 // If we didn't open an environment above, we open one here
865 << LaTeXSizeNames[f.size()]
867 count += strlen(LaTeXSizeNames[f.size()]) + 2;
873 /// Writes ending block of LaTeX needed to close use of this font
874 // Returns number of chars written
875 // This one corresponds to latexWriteStartChanges(). (Asger)
876 int Font::latexWriteEndChanges(odocstream & os, BufferParams const & bparams,
877 OutputParams const & runparams,
880 bool const & closeLanguage) const
885 // reduce the current font to changes against the base
886 // font (of the layout). We use a temporary for this to
887 // avoid changing this font instance, as that would break
891 if (f.family() != INHERIT_FAMILY) {
894 env = true; // Size change need not bother about closing env.
896 if (f.series() != INHERIT_SERIES) {
899 env = true; // Size change need not bother about closing env.
901 if (f.shape() != INHERIT_SHAPE) {
904 env = true; // Size change need not bother about closing env.
906 if (f.color() != Color::inherit && f.color() != Color::ignore) {
909 env = true; // Size change need not bother about closing env.
911 if (f.emph() == ON) {
914 env = true; // Size change need not bother about closing env.
916 if (f.underbar() == ON) {
919 env = true; // Size change need not bother about closing env.
921 if (f.noun() == ON) {
924 env = true; // Size change need not bother about closing env.
926 if (f.size() != INHERIT_SIZE) {
927 // We only have to close if only size changed
934 // When the current language is Hebrew, Arabic, or Farsi
935 // the numbers are written Left-to-Right. ArabTeX package
936 // reorders the number automatically but the packages used
937 // for Hebrew and Farsi (Arabi) do not.
938 if (number() == ON && next.number() != ON
939 && (language()->lang() == "hebrew"
940 || language()->lang() == "farsi"
941 || language()->lang() == "arabic_arabi")) {
946 if (open_encoding_) {
947 // We need to close the encoding even if it does not change
948 // to do correct environment nesting
949 Encoding const * const ascii = encodings.getFromLyXName("ascii");
950 pair<bool, int> const c = switchEncoding(os, bparams,
951 runparams.moving_arg, *(runparams.encoding),
953 BOOST_ASSERT(c.first);
955 runparams.encoding = ascii;
956 open_encoding_ = false;
960 language() != base.language() && language() != next.language()) {
969 Color_color Font::realColor() const
971 if (color() == Color::none)
972 return Color::foreground;
977 ostream & operator<<(ostream & os, Font::FONT_MISC_STATE fms)
979 return os << int(fms);
983 std::ostream & operator<<(std::ostream & os, Font const & font)
986 << " family " << font.bits.family
987 << " series " << font.bits.series
988 << " shape " << font.bits.shape
989 << " size " << font.bits.size
990 << " color " << font.bits.color
991 << " emph " << font.bits.emph
992 << " underbar " << font.bits.underbar
993 << " noun " << font.bits.noun
994 << " number " << font.bits.number
995 << " lang: " << (font.lang ? font.lang->lang() : 0);