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"
35 using support::ascii_lowercase;
36 using support::bformat;
44 #ifndef CXX_GLOBAL_CSTD
54 char const * GUIFamilyNames[Font::NUM_FAMILIES + 2 /* default & error */] =
55 { N_("Roman"), N_("Sans Serif"), N_("Typewriter"), N_("Symbol"),
56 "cmr", "cmsy", "cmm", "cmex", "msa", "msb", "eufrak", "wasy", "esint",
57 N_("Inherit"), N_("Ignore") };
59 char const * GUISeriesNames[4] =
60 { N_("Medium"), N_("Bold"), N_("Inherit"), N_("Ignore") };
62 char const * GUIShapeNames[6] =
63 { N_("Upright"), N_("Italic"), N_("Slanted"), N_("Smallcaps"), N_("Inherit"),
66 char const * GUISizeNames[14] =
67 { N_("Tiny"), N_("Smallest"), N_("Smaller"), N_("Small"), N_("Normal"), N_("Large"),
68 N_("Larger"), N_("Largest"), N_("Huge"), N_("Huger"), N_("Increase"), N_("Decrease"),
69 N_("Inherit"), N_("Ignore") };
71 char const * GUIMiscNames[5] =
72 { N_("Off"), N_("On"), N_("Toggle"), N_("Inherit"), N_("Ignore") };
76 // Strings used to read and write .lyx format files
78 char const * LyXFamilyNames[Font::NUM_FAMILIES + 2 /* default & error */] =
79 { "roman", "sans", "typewriter", "symbol",
80 "cmr", "cmsy", "cmm", "cmex", "msa", "msb", "eufrak", "wasy", "esint",
83 char const * LyXSeriesNames[4] =
84 { "medium", "bold", "default", "error" };
86 char const * LyXShapeNames[6] =
87 { "up", "italic", "slanted", "smallcaps", "default", "error" };
89 char const * LyXSizeNames[14] =
90 { "tiny", "scriptsize", "footnotesize", "small", "normal", "large",
91 "larger", "largest", "huge", "giant",
92 "increase", "decrease", "default", "error" };
94 char const * LyXMiscNames[5] =
95 { "off", "on", "toggle", "default", "error" };
98 // Strings used to write LaTeX files
100 char const * LaTeXFamilyNames[6] =
101 { "textrm", "textsf", "texttt", "error1", "error2", "error3" };
103 char const * LaTeXSeriesNames[4] =
104 { "textmd", "textbf", "error4", "error5" };
106 char const * LaTeXShapeNames[6] =
107 { "textup", "textit", "textsl", "textsc", "error6", "error7" };
109 char const * LaTeXSizeNames[14] =
110 { "tiny", "scriptsize", "footnotesize", "small", "normalsize", "large",
111 "Large", "LARGE", "huge", "Huge", "error8", "error9", "error10", "error11" };
116 // Initialize static member
117 Font::FontBits Font::sane = {
128 // Initialize static member
129 Font::FontBits Font::inherit = {
140 // Initialize static member
141 Font::FontBits Font::ignore = {
153 bool operator==(Font::FontBits const & lhs,
154 Font::FontBits const & rhs)
156 return lhs.family == rhs.family &&
157 lhs.series == rhs.series &&
158 lhs.shape == rhs.shape &&
159 lhs.size == rhs.size &&
160 lhs.color == rhs.color &&
161 lhs.emph == rhs.emph &&
162 lhs.underbar == rhs.underbar &&
163 lhs.noun == rhs.noun &&
164 lhs.number == rhs.number;
169 : bits(sane), lang(default_language), open_encoding_(false)
173 Font::Font(Font::FONT_INIT1)
174 : bits(inherit), lang(default_language), open_encoding_(false)
178 Font::Font(Font::FONT_INIT2)
179 : bits(ignore), lang(ignore_language), open_encoding_(false)
183 Font::Font(Font::FONT_INIT3)
184 : bits(sane), lang(default_language), open_encoding_(false)
188 Font::Font(Font::FONT_INIT1, Language const * l)
189 : bits(inherit), lang(l), open_encoding_(false)
193 Font::Font(Font::FONT_INIT2, Language const * l)
194 : bits(ignore), lang(l), open_encoding_(false)
198 Font::Font(Font::FONT_INIT3, Language const * l)
199 : bits(sane), lang(l), open_encoding_(false)
204 Color_color Font::color() const
206 return Color::color(bits.color);
210 bool Font::isRightToLeft() const
212 return lang->rightToLeft();
216 bool Font::isVisibleRightToLeft() const
218 return (lang->rightToLeft() &&
223 void Font::setFamily(Font::FONT_FAMILY f)
229 void Font::setSeries(Font::FONT_SERIES s)
235 void Font::setShape(Font::FONT_SHAPE s)
241 void Font::setSize(Font::FONT_SIZE s)
247 void Font::setEmph(Font::FONT_MISC_STATE e)
253 void Font::setUnderbar(Font::FONT_MISC_STATE u)
259 void Font::setNoun(Font::FONT_MISC_STATE n)
265 void Font::setColor(Color_color c)
271 void Font::setLanguage(Language const * l)
277 void Font::setNumber(Font::FONT_MISC_STATE n)
283 /// Decreases font size by one
284 Font & Font::decSize()
287 case SIZE_HUGER: setSize(SIZE_HUGE); break;
288 case SIZE_HUGE: setSize(SIZE_LARGEST); break;
289 case SIZE_LARGEST: setSize(SIZE_LARGER); break;
290 case SIZE_LARGER: setSize(SIZE_LARGE); break;
291 case SIZE_LARGE: setSize(SIZE_NORMAL); break;
292 case SIZE_NORMAL: setSize(SIZE_SMALL); break;
293 case SIZE_SMALL: setSize(SIZE_FOOTNOTE); break;
294 case SIZE_FOOTNOTE: setSize(SIZE_SCRIPT); break;
295 case SIZE_SCRIPT: setSize(SIZE_TINY); break;
296 case SIZE_TINY: break;
298 lyxerr << "Can't Font::decSize on INCREASE_SIZE" << endl;
301 lyxerr <<"Can't Font::decSize on DECREASE_SIZE" << endl;
304 lyxerr <<"Can't Font::decSize on INHERIT_SIZE" << endl;
307 lyxerr <<"Can't Font::decSize on IGNORE_SIZE" << endl;
314 /// Increases font size by one
315 Font & Font::incSize()
318 case SIZE_HUGER: break;
319 case SIZE_HUGE: setSize(SIZE_HUGER); break;
320 case SIZE_LARGEST: setSize(SIZE_HUGE); break;
321 case SIZE_LARGER: setSize(SIZE_LARGEST); break;
322 case SIZE_LARGE: setSize(SIZE_LARGER); break;
323 case SIZE_NORMAL: setSize(SIZE_LARGE); break;
324 case SIZE_SMALL: setSize(SIZE_NORMAL); break;
325 case SIZE_FOOTNOTE: setSize(SIZE_SMALL); break;
326 case SIZE_SCRIPT: setSize(SIZE_FOOTNOTE); break;
327 case SIZE_TINY: setSize(SIZE_SCRIPT); break;
329 lyxerr <<"Can't Font::incSize on INCREASE_SIZE" << endl;
332 lyxerr <<"Can't Font::incSize on DECREASE_SIZE" << endl;
335 lyxerr <<"Can't Font::incSize on INHERIT_SIZE" << endl;
338 lyxerr <<"Can't Font::incSize on IGNORE_SIZE" << endl;
345 /// Updates a misc setting according to request
346 Font::FONT_MISC_STATE Font::setMisc(FONT_MISC_STATE newfont,
349 if (newfont == TOGGLE) {
355 lyxerr <<"Font::setMisc: Need state"
356 " ON or OFF to toggle. Setting to ON" << endl;
359 } else if (newfont == IGNORE)
366 /// Updates font settings according to request
367 void Font::update(Font const & newfont,
368 Language const * document_language,
371 if (newfont.family() == family() && toggleall)
372 setFamily(INHERIT_FAMILY); // toggle 'back'
373 else if (newfont.family() != IGNORE_FAMILY)
374 setFamily(newfont.family());
375 // else it's IGNORE_SHAPE
377 // "Old" behaviour: "Setting" bold will toggle bold on/off.
378 switch (newfont.series()) {
381 if (series() == BOLD_SERIES && toggleall)
382 setSeries(MEDIUM_SERIES);
384 setSeries(BOLD_SERIES);
388 setSeries(newfont.series());
394 if (newfont.shape() == shape() && toggleall)
395 setShape(INHERIT_SHAPE); // toggle 'back'
396 else if (newfont.shape() != IGNORE_SHAPE)
397 setShape(newfont.shape());
398 // else it's IGNORE_SHAPE
400 if (newfont.size() != IGNORE_SIZE) {
401 if (newfont.size() == INCREASE_SIZE)
403 else if (newfont.size() == DECREASE_SIZE)
406 setSize(newfont.size());
409 setEmph(setMisc(newfont.emph(), emph()));
410 setUnderbar(setMisc(newfont.underbar(), underbar()));
411 setNoun(setMisc(newfont.noun(), noun()));
413 setNumber(setMisc(newfont.number(), number()));
414 if (newfont.language() == language() && toggleall)
415 if (language() == document_language)
416 setLanguage(default_language);
418 setLanguage(document_language);
419 else if (newfont.language() != ignore_language)
420 setLanguage(newfont.language());
422 if (newfont.color() == color() && toggleall)
423 setColor(Color::inherit); // toggle 'back'
424 else if (newfont.color() != Color::ignore)
425 setColor(newfont.color());
429 /// Reduce font to fall back to template where possible
430 void Font::reduce(Font const & tmplt)
432 if (family() == tmplt.family())
433 setFamily(INHERIT_FAMILY);
434 if (series() == tmplt.series())
435 setSeries(INHERIT_SERIES);
436 if (shape() == tmplt.shape())
437 setShape(INHERIT_SHAPE);
438 if (size() == tmplt.size())
439 setSize(INHERIT_SIZE);
440 if (emph() == tmplt.emph())
442 if (underbar() == tmplt.underbar())
443 setUnderbar(INHERIT);
444 if (noun() == tmplt.noun())
446 if (color() == tmplt.color())
447 setColor(Color::inherit);
451 /// Realize font from a template
452 Font & Font::realize(Font const & tmplt)
454 if (bits == inherit) {
459 if (bits.family == INHERIT_FAMILY)
460 bits.family = tmplt.bits.family;
462 if (bits.series == INHERIT_SERIES)
463 bits.series = tmplt.bits.series;
465 if (bits.shape == INHERIT_SHAPE)
466 bits.shape = tmplt.bits.shape;
468 if (bits.size == INHERIT_SIZE)
469 bits.size = tmplt.bits.size;
471 if (bits.emph == INHERIT)
472 bits.emph = tmplt.bits.emph;
474 if (bits.underbar == INHERIT)
475 bits.underbar = tmplt.bits.underbar;
477 if (bits.noun == INHERIT)
478 bits.noun = tmplt.bits.noun;
480 if (bits.color == Color::inherit)
481 bits.color = tmplt.bits.color;
487 /// Is font resolved?
488 bool Font::resolved() const
490 return (family() != INHERIT_FAMILY && series() != INHERIT_SERIES &&
491 shape() != INHERIT_SHAPE && size() != INHERIT_SIZE &&
492 emph() != INHERIT && underbar() != INHERIT &&
494 color() != Color::inherit);
498 docstring const Font::stateText(BufferParams * params) const
501 if (family() != INHERIT_FAMILY)
502 os << _(GUIFamilyNames[family()]) << ", ";
503 if (series() != INHERIT_SERIES)
504 os << _(GUISeriesNames[series()]) << ", ";
505 if (shape() != INHERIT_SHAPE)
506 os << _(GUIShapeNames[shape()]) << ", ";
507 if (size() != INHERIT_SIZE)
508 os << _(GUISizeNames[size()]) << ", ";
509 if (color() != Color::inherit)
510 os << lcolor.getGUIName(color()) << ", ";
511 if (emph() != INHERIT)
512 os << bformat(_("Emphasis %1$s, "),
513 _(GUIMiscNames[emph()]));
514 if (underbar() != INHERIT)
515 os << bformat(_("Underline %1$s, "),
516 _(GUIMiscNames[underbar()]));
517 if (noun() != INHERIT)
518 os << bformat(_("Noun %1$s, "),
519 _(GUIMiscNames[noun()]));
521 os << _("Default") << ", ";
522 if (!params || (language() != params->language))
523 os << bformat(_("Language: %1$s, "),
524 _(language()->display()));
526 os << bformat(_(" Number %1$s"),
527 _(GUIMiscNames[number()]));
528 return rtrim(os.str(), ", ");
532 // Set family according to lyx format string
533 Font & Font::setLyXFamily(string const & fam)
535 string const s = ascii_lowercase(fam);
538 while (LyXFamilyNames[i] != s &&
539 LyXFamilyNames[i] != string("error"))
541 if (s == LyXFamilyNames[i])
542 setFamily(Font::FONT_FAMILY(i));
544 lyxerr << "Font::setLyXFamily: Unknown family `"
545 << s << '\'' << endl;
550 // Set series according to lyx format string
551 Font & Font::setLyXSeries(string const & ser)
553 string const s = ascii_lowercase(ser);
556 while (LyXSeriesNames[i] != s &&
557 LyXSeriesNames[i] != string("error")) ++i;
558 if (s == LyXSeriesNames[i]) {
559 setSeries(Font::FONT_SERIES(i));
561 lyxerr << "Font::setLyXSeries: Unknown series `"
562 << s << '\'' << endl;
567 // Set shape according to lyx format string
568 Font & Font::setLyXShape(string const & sha)
570 string const s = ascii_lowercase(sha);
573 while (LyXShapeNames[i] != s && LyXShapeNames[i] != string("error"))
575 if (s == LyXShapeNames[i])
576 setShape(Font::FONT_SHAPE(i));
578 lyxerr << "Font::setLyXShape: Unknown shape `"
579 << s << '\'' << endl;
584 // Set size according to lyx format string
585 Font & Font::setLyXSize(string const & siz)
587 string const s = ascii_lowercase(siz);
589 while (LyXSizeNames[i] != s && LyXSizeNames[i] != string("error"))
591 if (s == LyXSizeNames[i]) {
592 setSize(Font::FONT_SIZE(i));
594 lyxerr << "Font::setLyXSize: Unknown size `"
595 << s << '\'' << endl;
600 // Set size according to lyx format string
601 Font::FONT_MISC_STATE Font::setLyXMisc(string const & siz)
603 string const s = ascii_lowercase(siz);
605 while (LyXMiscNames[i] != s &&
606 LyXMiscNames[i] != string("error")) ++i;
607 if (s == LyXMiscNames[i])
608 return FONT_MISC_STATE(i);
609 lyxerr << "Font::setLyXMisc: Unknown misc flag `"
610 << s << '\'' << endl;
615 /// Sets color after LyX text format
616 Font & Font::setLyXColor(string const & col)
618 setColor(lcolor.getFromLyXName(col));
623 // Returns size in latex format
624 string const Font::latexSize() const
626 return LaTeXSizeNames[size()];
630 // Read a font definition from given file in lyx format
632 Font & Font::lyxRead(Lexer & lex)
635 bool finished = false;
636 while (!finished && lex.isOK() && !error) {
638 string const tok = ascii_lowercase(lex.getString());
642 } else if (tok == "endfont") {
644 } else if (tok == "family") {
646 string const ttok = lex.getString();
648 } else if (tok == "series") {
650 string const ttok = lex.getString();
652 } else if (tok == "shape") {
654 string const ttok = lex.getString();
656 } else if (tok == "size") {
658 string const ttok = lex.getString();
660 } else if (tok == "misc") {
662 string const ttok = ascii_lowercase(lex.getString());
664 if (ttok == "no_bar") {
666 } else if (ttok == "no_emph") {
668 } else if (ttok == "no_noun") {
670 } else if (ttok == "emph") {
672 } else if (ttok == "underbar") {
674 } else if (ttok == "noun") {
677 lex.printError("Illegal misc type `$$Token'");
679 } else if (tok == "color") {
681 string const ttok = lex.getString();
684 lex.printError("Unknown tag `$$Token'");
692 /// Writes the changes from this font to orgfont in .lyx format in file
693 void Font::lyxWriteChanges(Font const & orgfont,
697 if (orgfont.family() != family())
698 os << "\\family " << LyXFamilyNames[family()] << "\n";
699 if (orgfont.series() != series())
700 os << "\\series " << LyXSeriesNames[series()] << "\n";
701 if (orgfont.shape() != shape())
702 os << "\\shape " << LyXShapeNames[shape()] << "\n";
703 if (orgfont.size() != size())
704 os << "\\size " << LyXSizeNames[size()] << "\n";
705 if (orgfont.emph() != emph())
706 os << "\\emph " << LyXMiscNames[emph()] << "\n";
707 if (orgfont.number() != number())
708 os << "\\numeric " << LyXMiscNames[number()] << "\n";
709 if (orgfont.underbar() != underbar()) {
710 // This is only for backwards compatibility
711 switch (underbar()) {
712 case OFF: os << "\\bar no\n"; break;
713 case ON: os << "\\bar under\n"; break;
714 case TOGGLE: lyxerr << "Font::lyxWriteFontChanges: "
715 "TOGGLE should not appear here!"
718 case INHERIT: os << "\\bar default\n"; break;
719 case IGNORE: lyxerr << "Font::lyxWriteFontChanges: "
720 "IGNORE should not appear here!"
725 if (orgfont.noun() != noun()) {
726 os << "\\noun " << LyXMiscNames[noun()] << "\n";
728 if (orgfont.color() != color())
729 os << "\\color " << lcolor.getLyXName(color()) << '\n';
730 if (orgfont.language() != language() &&
731 language() != latex_language) {
733 os << "\\lang " << language()->lang() << "\n";
735 os << "\\lang unknown\n";
740 /// Writes the head of the LaTeX needed to impose this font
741 // Returns number of chars written.
742 int Font::latexWriteStartChanges(odocstream & os, BufferParams const & bparams,
743 OutputParams const & runparams,
745 Font const & prev) const
750 if (language()->babel() != base.language()->babel() &&
751 language() != prev.language()) {
752 if (isRightToLeft() != prev.isRightToLeft()) {
753 if (isRightToLeft()) {
760 } else if (!language()->babel().empty()) {
762 subst(lyxrc.language_command_local,
763 "$$lang", language()->babel());
764 os << from_ascii(tmp);
765 count += tmp.length();
772 if (language()->encoding()->package() == Encoding::CJK) {
773 int const c = switchEncoding(os, bparams,
774 runparams.moving_arg, *(runparams.encoding),
775 *(language()->encoding()));
777 open_encoding_ = true;
779 runparams.encoding = language()->encoding();
783 // When the current language is Hebrew, Arabic, or Farsi
784 // the numbers are written Left-to-Right.
785 if (number() == ON && prev.number() != ON && isRightToLeft()) {
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() != Color::inherit && f.color() != Color::ignore) {
816 << from_ascii(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 Font::latexWriteEndChanges(odocstream & os, BufferParams const & bparams,
856 OutputParams const & runparams,
858 Font 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() != Color::inherit && f.color() != Color::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 // When the current language is Hebrew, Arabic, or Farsi
913 // the numbers are written Left-to-Right.
914 if (number() == ON && next.number() != ON && isRightToLeft()) {
919 if (open_encoding_) {
920 // We need to close the encoding even if it does not change
921 // to do correct environment nesting
922 Encoding const * const ascii = encodings.getFromLyXName("ascii");
923 int const c = switchEncoding(os, bparams,
924 runparams.moving_arg, *(runparams.encoding),
928 runparams.encoding = ascii;
929 open_encoding_ = false;
932 if (language() != base.language() && language() != next.language()) {
941 Color_color Font::realColor() const
943 if (color() == Color::none)
944 return Color::foreground;
949 ostream & operator<<(ostream & os, Font::FONT_MISC_STATE fms)
951 return os << int(fms);
955 std::ostream & operator<<(std::ostream & os, Font const & font)
958 << " family " << font.bits.family
959 << " series " << font.bits.series
960 << " shape " << font.bits.shape
961 << " size " << font.bits.size
962 << " color " << font.bits.color
963 << " emph " << font.bits.emph
964 << " underbar " << font.bits.underbar
965 << " noun " << font.bits.noun
966 << " number " << font.bits.number
967 << " lang: " << (font.lang ? font.lang->lang() : 0);