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
23 #include "LaTeXFeatures.h"
26 #include "output_latex.h"
27 #include "OutputParams.h"
29 #include "support/lassert.h"
30 #include "support/convert.h"
31 #include "support/debug.h"
32 #include "support/gettext.h"
33 #include "support/lstrings.h"
38 using namespace lyx::support;
48 char const * GUIFamilyNames[NUM_FAMILIES + 2 /* default & error */] =
49 { N_("Roman"), N_("Sans Serif"), N_("Typewriter"), N_("Symbol"),
50 "cmr", "cmsy", "cmm", "cmex", "msa", "msb", "eufrak", "wasy", "esint",
51 N_("Inherit"), N_("Ignore") };
53 char const * GUISeriesNames[4] =
54 { N_("Medium"), N_("Bold"), N_("Inherit"), N_("Ignore") };
56 char const * GUIShapeNames[6] =
57 { N_("Upright"), N_("Italic"), N_("Slanted"), N_("Smallcaps"), N_("Inherit"),
60 char const * GUISizeNames[14] =
61 { N_("Tiny"), N_("Smallest"), N_("Smaller"), N_("Small"), N_("Normal"), N_("Large"),
62 N_("Larger"), N_("Largest"), N_("Huge"), N_("Huger"), N_("Increase"), N_("Decrease"),
63 N_("Inherit"), N_("Ignore") };
65 char const * GUIMiscNames[5] =
66 { N_("Off"), N_("On"), N_("Toggle"), N_("Inherit"), N_("Ignore") };
70 // Strings used to read and write .lyx format files
72 char const * LyXFamilyNames[NUM_FAMILIES + 2 /* default & error */] =
73 { "roman", "sans", "typewriter", "symbol",
74 "cmr", "cmsy", "cmm", "cmex", "msa", "msb", "eufrak", "wasy", "esint",
77 char const * LyXSeriesNames[4] =
78 { "medium", "bold", "default", "error" };
80 char const * LyXShapeNames[6] =
81 { "up", "italic", "slanted", "smallcaps", "default", "error" };
83 char const * LyXSizeNames[14] =
84 { "tiny", "scriptsize", "footnotesize", "small", "normal", "large",
85 "larger", "largest", "huge", "giant",
86 "increase", "decrease", "default", "error" };
88 char const * LyXMiscNames[5] =
89 { "off", "on", "toggle", "default", "error" };
92 // Strings used to write LaTeX files
94 char const * LaTeXFamilyNames[6] =
95 { "textrm", "textsf", "texttt", "error1", "error2", "error3" };
97 char const * LaTeXSeriesNames[4] =
98 { "textmd", "textbf", "error4", "error5" };
100 char const * LaTeXShapeNames[6] =
101 { "textup", "textit", "textsl", "textsc", "error6", "error7" };
103 char const * LaTeXSizeNames[14] =
104 { "tiny", "scriptsize", "footnotesize", "small", "normalsize", "large",
105 "Large", "LARGE", "huge", "Huge", "error8", "error9", "error10", "error11" };
110 Font::Font(FontInfo bits, Language const * l)
111 : bits_(bits), lang_(l), misspelled_(false), open_encoding_(false)
114 lang_ = default_language;
118 bool Font::isRightToLeft() const
120 return lang_->rightToLeft();
124 bool Font::isVisibleRightToLeft() const
126 return (lang_->rightToLeft() &&
127 bits_.number() != FONT_ON);
131 void Font::setLanguage(Language const * l)
137 /// Updates font settings according to request
138 void Font::update(Font const & newfont,
139 Language const * document_language,
142 bits_.update(newfont.fontInfo(), toggleall);
144 if (newfont.language() == language() && toggleall)
145 if (language() == document_language)
146 setLanguage(default_language);
148 setLanguage(document_language);
149 else if (newfont.language() == reset_language)
150 setLanguage(document_language);
151 else if (newfont.language() != ignore_language)
152 setLanguage(newfont.language());
156 docstring const stateText(FontInfo const & f)
159 if (f.family() != INHERIT_FAMILY)
160 os << _(GUIFamilyNames[f.family()]) << ", ";
161 if (f.series() != INHERIT_SERIES)
162 os << _(GUISeriesNames[f.series()]) << ", ";
163 if (f.shape() != INHERIT_SHAPE)
164 os << _(GUIShapeNames[f.shape()]) << ", ";
165 if (f.size() != FONT_SIZE_INHERIT)
166 os << _(GUISizeNames[f.size()]) << ", ";
167 if (f.color() != Color_inherit)
168 os << lcolor.getGUIName(f.color()) << ", ";
169 // FIXME: uncomment this when we support background.
170 //if (f.background() != Color_inherit)
171 // os << lcolor.getGUIName(f.background()) << ", ";
172 if (f.emph() != FONT_INHERIT)
173 os << bformat(_("Emphasis %1$s, "),
174 _(GUIMiscNames[f.emph()]));
175 if (f.underbar() != FONT_INHERIT)
176 os << bformat(_("Underline %1$s, "),
177 _(GUIMiscNames[f.underbar()]));
178 if (f.strikeout() != FONT_INHERIT)
179 os << bformat(_("Strikeout %1$s, "),
180 _(GUIMiscNames[f.strikeout()]));
181 if (f.noun() != FONT_INHERIT)
182 os << bformat(_("Noun %1$s, "),
183 _(GUIMiscNames[f.noun()]));
184 if (f == inherit_font)
185 os << _("Default") << ", ";
191 docstring const Font::stateText(BufferParams * params) const
194 os << lyx::stateText(bits_);
195 if (!params || (language() != params->language))
196 os << bformat(_("Language: %1$s, "),
197 _(language()->display()));
198 if (bits_.number() != FONT_OFF)
199 os << bformat(_(" Number %1$s"),
200 _(GUIMiscNames[bits_.number()]));
201 return rtrim(os.str(), ", ");
205 // Set family according to lyx format string
206 void setLyXFamily(string const & fam, FontInfo & f)
208 string const s = ascii_lowercase(fam);
211 while (LyXFamilyNames[i] != s &&
212 LyXFamilyNames[i] != string("error"))
214 if (s == LyXFamilyNames[i])
215 f.setFamily(FontFamily(i));
217 lyxerr << "setLyXFamily: Unknown family `"
218 << s << '\'' << endl;
222 // Set series according to lyx format string
223 void setLyXSeries(string const & ser, FontInfo & f)
225 string const s = ascii_lowercase(ser);
228 while (LyXSeriesNames[i] != s &&
229 LyXSeriesNames[i] != string("error")) ++i;
230 if (s == LyXSeriesNames[i]) {
231 f.setSeries(FontSeries(i));
233 lyxerr << "setLyXSeries: Unknown series `"
234 << s << '\'' << endl;
238 // Set shape according to lyx format string
239 void setLyXShape(string const & sha, FontInfo & f)
241 string const s = ascii_lowercase(sha);
244 while (LyXShapeNames[i] != s && LyXShapeNames[i] != string("error"))
246 if (s == LyXShapeNames[i])
247 f.setShape(FontShape(i));
249 lyxerr << "Font::setLyXShape: Unknown shape `"
250 << s << '\'' << endl;
254 // Set size according to lyx format string
255 void setLyXSize(string const & siz, FontInfo & f)
257 string const s = ascii_lowercase(siz);
259 while (LyXSizeNames[i] != s && LyXSizeNames[i] != string("error"))
261 if (s == LyXSizeNames[i]) {
262 f.setSize(FontSize(i));
264 lyxerr << "Font::setLyXSize: Unknown size `"
265 << s << '\'' << endl;
269 // Set size according to lyx format string
270 FontState Font::setLyXMisc(string const & siz)
272 string const s = ascii_lowercase(siz);
274 while (LyXMiscNames[i] != s &&
275 LyXMiscNames[i] != string("error")) ++i;
276 if (s == LyXMiscNames[i])
278 lyxerr << "Font::setLyXMisc: Unknown misc flag `"
279 << s << '\'' << endl;
284 /// Sets color after LyX text format
285 void setLyXColor(string const & col, FontInfo & f)
287 f.setColor(lcolor.getFromLyXName(col));
291 // Returns size in latex format
292 string const Font::latexSize() const
294 return LaTeXSizeNames[bits_.size()];
298 // Read a font definition from given file in lyx format
300 FontInfo lyxRead(Lexer & lex, FontInfo const & fi)
304 bool finished = false;
305 while (!finished && lex.isOK() && !error) {
307 string const tok = ascii_lowercase(lex.getString());
311 } else if (tok == "endfont") {
313 } else if (tok == "family") {
315 string const ttok = lex.getString();
316 setLyXFamily(ttok, f);
317 } else if (tok == "series") {
319 string const ttok = lex.getString();
320 setLyXSeries(ttok, f);
321 } else if (tok == "shape") {
323 string const ttok = lex.getString();
324 setLyXShape(ttok, f);
325 } else if (tok == "size") {
327 string const ttok = lex.getString();
329 } else if (tok == "misc") {
331 string const ttok = ascii_lowercase(lex.getString());
333 if (ttok == "no_bar") {
334 f.setUnderbar(FONT_OFF);
335 } else if (ttok == "no_strikeout") {
336 f.setStrikeout(FONT_OFF);
337 } else if (ttok == "no_emph") {
339 } else if (ttok == "no_noun") {
341 } else if (ttok == "emph") {
343 } else if (ttok == "underbar") {
344 f.setUnderbar(FONT_ON);
345 } else if (ttok == "strikeout") {
346 f.setStrikeout(FONT_ON);
347 } else if (ttok == "noun") {
350 lex.printError("Illegal misc type");
352 } else if (tok == "color") {
354 string const ttok = lex.getString();
355 setLyXColor(ttok, f);
357 lex.printError("Unknown tag");
365 /// Writes the changes from this font to orgfont in .lyx format in file
366 void Font::lyxWriteChanges(Font const & orgfont,
370 if (orgfont.fontInfo().family() != bits_.family())
371 os << "\\family " << LyXFamilyNames[bits_.family()] << "\n";
372 if (orgfont.fontInfo().series() != bits_.series())
373 os << "\\series " << LyXSeriesNames[bits_.series()] << "\n";
374 if (orgfont.fontInfo().shape() != bits_.shape())
375 os << "\\shape " << LyXShapeNames[bits_.shape()] << "\n";
376 if (orgfont.fontInfo().size() != bits_.size())
377 os << "\\size " << LyXSizeNames[bits_.size()] << "\n";
378 if (orgfont.fontInfo().emph() != bits_.emph())
379 os << "\\emph " << LyXMiscNames[bits_.emph()] << "\n";
380 if (orgfont.fontInfo().number() != bits_.number())
381 os << "\\numeric " << LyXMiscNames[bits_.number()] << "\n";
382 if (orgfont.fontInfo().underbar() != bits_.underbar()) {
383 // This is only for backwards compatibility
384 switch (bits_.underbar()) {
385 case FONT_OFF: os << "\\bar no\n"; break;
386 case FONT_ON: os << "\\bar under\n"; break;
387 case FONT_TOGGLE: lyxerr << "Font::lyxWriteFontChanges: "
388 "FONT_TOGGLE should not appear here!"
391 case FONT_INHERIT: os << "\\bar default\n"; break;
392 case FONT_IGNORE: lyxerr << "Font::lyxWriteFontChanges: "
393 "IGNORE should not appear here!"
398 if (orgfont.fontInfo().strikeout() != bits_.strikeout()) {
399 os << "\\strikeout " << LyXMiscNames[bits_.strikeout()] << "\n";
401 if (orgfont.fontInfo().noun() != bits_.noun()) {
402 os << "\\noun " << LyXMiscNames[bits_.noun()] << "\n";
404 if (orgfont.fontInfo().color() != bits_.color())
405 os << "\\color " << lcolor.getLyXName(bits_.color()) << '\n';
406 // FIXME: uncomment this when we support background.
407 //if (orgfont.fontInfo().background() != bits_.background())
408 // os << "\\color " << lcolor.getLyXName(bits_.background()) << '\n';
409 if (orgfont.language() != language() &&
410 language() != latex_language) {
412 os << "\\lang " << language()->lang() << "\n";
414 os << "\\lang unknown\n";
419 /// Writes the head of the LaTeX needed to impose this font
420 // Returns number of chars written.
421 int Font::latexWriteStartChanges(odocstream & os, BufferParams const & bparams,
422 OutputParams const & runparams,
424 Font const & prev) const
429 if (language()->babel() != base.language()->babel() &&
430 language() != prev.language()) {
431 if (language()->lang() == "farsi") {
434 } else if (!isRightToLeft() &&
435 base.language()->lang() == "farsi") {
438 } else if (language()->lang() == "arabic_arabi") {
441 } else if (!isRightToLeft() &&
442 base.language()->lang() == "arabic_arabi") {
445 // currently the remaining RTL languages are arabic_arabtex and hebrew
446 } else if (isRightToLeft() != prev.isRightToLeft()) {
447 if (isRightToLeft()) {
454 } else if (!language()->babel().empty()) {
456 subst(lyxrc.language_command_local,
457 "$$lang", language()->babel());
458 os << from_ascii(tmp);
459 count += tmp.length();
466 if (language()->encoding()->package() == Encoding::CJK) {
467 pair<bool, int> const c = switchEncoding(os, bparams,
468 runparams, *(language()->encoding()));
470 open_encoding_ = true;
472 runparams.encoding = language()->encoding();
476 // When the current language is Hebrew, Arabic, or Farsi
477 // the numbers are written Left-to-Right. ArabTeX package
478 // reorders the number automatically but the packages used
479 // for Hebrew and Farsi (Arabi) do not.
480 if (bits_.number() == FONT_ON && prev.fontInfo().number() != FONT_ON
481 && (language()->lang() == "hebrew"
482 || language()->lang() == "farsi"
483 || language()->lang() == "arabic_arabi")) {
489 f.reduce(base.bits_);
491 if (f.family() != INHERIT_FAMILY) {
493 << LaTeXFamilyNames[f.family()]
495 count += strlen(LaTeXFamilyNames[f.family()]) + 2;
496 env = true; //We have opened a new environment
498 if (f.series() != INHERIT_SERIES) {
500 << LaTeXSeriesNames[f.series()]
502 count += strlen(LaTeXSeriesNames[f.series()]) + 2;
503 env = true; //We have opened a new environment
505 if (f.shape() != INHERIT_SHAPE) {
507 << LaTeXShapeNames[f.shape()]
509 count += strlen(LaTeXShapeNames[f.shape()]) + 2;
510 env = true; //We have opened a new environment
512 if (f.color() != Color_inherit && f.color() != Color_ignore) {
514 << from_ascii(lcolor.getLaTeXName(f.color()))
516 count += lcolor.getLaTeXName(f.color()).length() + 13;
517 env = true; //We have opened a new environment
519 // FIXME: uncomment this when we support background.
521 if (f.background() != Color_inherit && f.background() != Color_ignore) {
523 << from_ascii(lcolor.getLaTeXName(f.background()))
525 count += lcolor.getLaTeXName(f.background()).length() + 13;
526 env = true; //We have opened a new environment
529 if (f.emph() == FONT_ON) {
532 env = true; //We have opened a new environment
534 if (f.underbar() == FONT_ON) {
537 env = true; //We have opened a new environment
539 if (f.strikeout() == FONT_ON) {
542 env = true; //We have opened a new environment
544 // \noun{} is a LyX special macro
545 if (f.noun() == FONT_ON) {
548 env = true; //We have opened a new environment
550 if (f.size() != FONT_SIZE_INHERIT) {
551 // If we didn't open an environment above, we open one here
557 << LaTeXSizeNames[f.size()]
559 count += strlen(LaTeXSizeNames[f.size()]) + 2;
565 /// Writes ending block of LaTeX needed to close use of this font
566 // Returns number of chars written
567 // This one corresponds to latexWriteStartChanges(). (Asger)
568 int Font::latexWriteEndChanges(odocstream & os, BufferParams const & bparams,
569 OutputParams const & runparams,
572 bool const & closeLanguage) const
577 // reduce the current font to changes against the base
578 // font (of the layout). We use a temporary for this to
579 // avoid changing this font instance, as that would break
581 f.reduce(base.bits_);
583 if (f.family() != INHERIT_FAMILY) {
586 env = true; // Size change need not bother about closing env.
588 if (f.series() != INHERIT_SERIES) {
591 env = true; // Size change need not bother about closing env.
593 if (f.shape() != INHERIT_SHAPE) {
596 env = true; // Size change need not bother about closing env.
598 if (f.color() != Color_inherit && f.color() != Color_ignore) {
601 env = true; // Size change need not bother about closing env.
603 if (f.emph() == FONT_ON) {
606 env = true; // Size change need not bother about closing env.
608 if (f.underbar() == FONT_ON) {
611 env = true; // Size change need not bother about closing env.
613 if (f.strikeout() == FONT_ON) {
616 env = true; // Size change need not bother about closing env.
618 if (f.noun() == FONT_ON) {
621 env = true; // Size change need not bother about closing env.
623 if (f.size() != FONT_SIZE_INHERIT) {
624 // We only have to close if only size changed
631 // When the current language is Hebrew, Arabic, or Farsi
632 // the numbers are written Left-to-Right. ArabTeX package
633 // reorders the number automatically but the packages used
634 // for Hebrew and Farsi (Arabi) do not.
635 if (bits_.number() == FONT_ON && next.fontInfo().number() != FONT_ON
636 && (language()->lang() == "hebrew"
637 || language()->lang() == "farsi"
638 || language()->lang() == "arabic_arabi")) {
643 if (open_encoding_) {
644 // We need to close the encoding even if it does not change
645 // to do correct environment nesting
646 Encoding const * const ascii = encodings.fromLyXName("ascii");
647 pair<bool, int> const c = switchEncoding(os, bparams,
649 LASSERT(c.first, /**/);
651 runparams.encoding = ascii;
652 open_encoding_ = false;
656 language() != base.language() && language() != next.language()) {
665 string Font::toString(bool const toggle) const
667 string const lang = (language() == reset_language)
668 ? "reset" : language()->lang();
671 os << "family " << bits_.family() << '\n'
672 << "series " << bits_.series() << '\n'
673 << "shape " << bits_.shape() << '\n'
674 << "size " << bits_.size() << '\n'
675 << "emph " << bits_.emph() << '\n'
676 << "underbar " << bits_.underbar() << '\n'
677 << "strikeout " << bits_.strikeout() << '\n'
678 << "noun " << bits_.noun() << '\n'
679 << "number " << bits_.number() << '\n'
680 << "color " << bits_.color() << '\n'
681 << "language " << lang << '\n'
682 << "toggleall " << convert<string>(toggle);
687 bool Font::fromString(string const & data, bool & toggle)
689 istringstream is(data);
697 token = lex.getString();
699 if (token.empty() || !lex.next())
702 if (token == "family") {
703 int const next = lex.getInteger();
704 bits_.setFamily(FontFamily(next));
706 } else if (token == "series") {
707 int const next = lex.getInteger();
708 bits_.setSeries(FontSeries(next));
710 } else if (token == "shape") {
711 int const next = lex.getInteger();
712 bits_.setShape(FontShape(next));
714 } else if (token == "size") {
715 int const next = lex.getInteger();
716 bits_.setSize(FontSize(next));
718 } else if (token == "emph" || token == "underbar" ||
719 token == "noun" || token == "number" ||
720 token == "strikeout") {
722 int const next = lex.getInteger();
723 FontState const misc = FontState(next);
727 else if (token == "underbar")
728 bits_.setUnderbar(misc);
729 else if (token == "strikeout")
730 bits_.setStrikeout(misc);
731 else if (token == "noun")
733 else if (token == "number")
734 bits_.setNumber(misc);
736 } else if (token == "color") {
737 int const next = lex.getInteger();
738 bits_.setColor(ColorCode(next));
741 } else if (token == "background") {
742 int const next = lex.getInteger();
743 bits_.setBackground(ColorCode(next));
746 } else if (token == "language") {
747 string const next = lex.getString();
748 setLanguage(languages.getLanguage(next));
750 } else if (token == "toggleall") {
751 toggle = lex.getBool();
754 // Unrecognised token
764 void Font::validate(LaTeXFeatures & features) const
766 BufferParams const & bparams = features.bufferParams();
767 Language const * doc_language = bparams.language;
769 if (bits_.noun() == FONT_ON) {
770 LYXERR(Debug::LATEX, "font.noun: " << bits_.noun());
771 features.require("noun");
772 LYXERR(Debug::LATEX, "Noun enabled. Font: " << to_utf8(stateText(0)));
774 if (bits_.strikeout() == FONT_ON) {
775 LYXERR(Debug::LATEX, "font.strikeout: " << bits_.strikeout());
776 features.require("ulem");
777 LYXERR(Debug::LATEX, "Strikeout enabled. Font: " << to_utf8(stateText(0)));
779 switch (bits_.color()) {
783 // probably we should put here all interface colors used for
784 // font displaying! For now I just add this ones I know of (Jug)
786 case Color_notelabel:
789 features.require("color");
790 LYXERR(Debug::LATEX, "Color enabled. Font: " << to_utf8(stateText(0)));
793 // FIXME: Do something for background and soul package?
795 if (lang_->babel() != doc_language->babel() &&
796 lang_ != ignore_language &&
797 lang_ != latex_language)
799 features.useLanguage(lang_);
800 LYXERR(Debug::LATEX, "Found language " << lang_->lang());
805 ostream & operator<<(ostream & os, FontState fms)
807 return os << int(fms);
811 ostream & operator<<(ostream & os, FontInfo const & f)
814 << " family " << f.family()
815 << " series " << f.series()
816 << " shape " << f.shape()
817 << " size " << f.size()
818 << " color " << f.color()
819 // FIXME: uncomment this when we support background.
820 //<< " background " << f.background()
821 << " emph " << f.emph()
822 << " underbar " << f.underbar()
823 << " strikeout " << f.strikeout()
824 << " noun " << f.noun()
825 << " number " << f.number();
829 ostream & operator<<(ostream & os, Font const & font)
831 return os << font.bits_
832 << " lang: " << (font.lang_ ? font.lang_->lang() : 0);