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"
28 #include "texstream.h"
30 #include "support/lassert.h"
31 #include "support/convert.h"
32 #include "support/debug.h"
33 #include "support/gettext.h"
34 #include "support/lstrings.h"
39 using namespace lyx::support;
44 // Strings used to read and write .lyx format files
46 // These are defined in FontInfo.cpp
47 extern char const * LyXFamilyNames[NUM_FAMILIES + 2];
48 extern char const * LyXSeriesNames[NUM_SERIES + 2];
49 extern char const * LyXShapeNames[NUM_SHAPE + 2];
50 extern char const * LyXSizeNames[NUM_SIZE + 4];
51 extern char const * LyXMiscNames[5];
59 char const * GUIFamilyNames[NUM_FAMILIES + 2 /* default & error */] =
60 { N_("Roman"), N_("Sans Serif"), N_("Typewriter"), N_("Symbol"),
61 "cmr", "cmsy", "cmm", "cmex", "msa", "msb", "eufrak", "rsfs", "stmry",
62 "wasy", "esint", N_("Inherit"), N_("Ignore") };
64 char const * GUISeriesNames[NUM_SERIES + 2 /* default & error */] =
65 { N_("Medium"), N_("Bold"), N_("Inherit"), N_("Ignore") };
67 char const * GUIShapeNames[NUM_SHAPE + 2 /* default & error */] =
68 { N_("Upright"), N_("Italic"), N_("Slanted"), N_("Smallcaps"), N_("Inherit"),
71 char const * GUISizeNames[NUM_SIZE + 4 /* increase, decrease, default & error */] =
72 { N_("Tiny"), N_("Smallest"), N_("Smaller"), N_("Small"), N_("Normal"), N_("Large"),
73 N_("Larger"), N_("Largest"), N_("Huge"), N_("Huger"), N_("Increase"), N_("Decrease"),
74 N_("Inherit"), N_("Ignore") };
76 char const * GUIMiscNames[5] =
77 { N_("Off"), N_("On"), N_("Toggle"), N_("Inherit"), N_("Ignore") };
80 // Strings used to write LaTeX files
82 char const * LaTeXFamilyNames[NUM_FAMILIES + 2] =
83 { "textrm", "textsf", "texttt", "error1", "error2", "error3", "error4",
84 "error5", "error6", "error7", "error8", "error9", "error10", "error11",
85 "error12", "error13" };
87 char const * LaTeXSeriesNames[NUM_SERIES + 2] =
88 { "textmd", "textbf", "error4", "error5" };
90 char const * LaTeXShapeNames[NUM_SHAPE + 2] =
91 { "textup", "textit", "textsl", "textsc", "error6", "error7" };
93 char const * LaTeXSizeNames[NUM_SIZE + 4] =
94 { "tiny", "scriptsize", "footnotesize", "small", "normalsize", "large",
95 "Large", "LARGE", "huge", "Huge", "error8", "error9", "error10", "error11" };
100 Font::Font(FontInfo bits, Language const * l)
101 : bits_(bits), lang_(l), open_encoding_(false)
104 lang_ = default_language;
108 bool Font::isRightToLeft() const
110 return lang_->rightToLeft();
114 bool Font::isVisibleRightToLeft() const
116 return (lang_->rightToLeft() &&
117 bits_.number() != FONT_ON);
121 void Font::setLanguage(Language const * l)
127 /// Updates font settings according to request
128 void Font::update(Font const & newfont,
129 Language const * document_language,
132 bits_.update(newfont.fontInfo(), toggleall);
134 if (newfont.language() == language() && toggleall)
135 if (language() == document_language)
136 setLanguage(default_language);
138 setLanguage(document_language);
139 else if (newfont.language() == reset_language)
140 setLanguage(document_language);
141 else if (newfont.language() != ignore_language)
142 setLanguage(newfont.language());
146 docstring const stateText(FontInfo const & f, bool const terse)
149 if (f.family() != INHERIT_FAMILY && (!terse || f.family() != IGNORE_FAMILY))
150 os << _(GUIFamilyNames[f.family()]) << ", ";
151 if (f.series() != INHERIT_SERIES && (!terse || f.series() != IGNORE_SERIES))
152 os << _(GUISeriesNames[f.series()]) << ", ";
153 if (f.shape() != INHERIT_SHAPE && (!terse || f.shape() != IGNORE_SHAPE))
154 os << _(GUIShapeNames[f.shape()]) << ", ";
155 if (f.size() != FONT_SIZE_INHERIT && (!terse || f.size() != FONT_SIZE_IGNORE))
156 os << _(GUISizeNames[f.size()]) << ", ";
157 if (f.color() != Color_inherit && (!terse || f.color() != Color_ignore))
158 os << lcolor.getGUIName(f.color()) << ", ";
159 // FIXME: uncomment this when we support background.
160 //if (f.background() != Color_inherit)
161 // os << lcolor.getGUIName(f.background()) << ", ";
162 if (f.emph() != FONT_INHERIT && (!terse || f.emph() != FONT_IGNORE))
163 os << bformat(_("Emphasis %1$s, "),
164 _(GUIMiscNames[f.emph()]));
165 if (f.underbar() != FONT_INHERIT && (!terse || f.underbar() == FONT_ON))
166 os << bformat(_("Underline %1$s, "),
167 _(GUIMiscNames[f.underbar()]));
168 if (f.uuline() != FONT_INHERIT && (!terse || f.uuline() == FONT_ON))
169 os << bformat(_("Double underline %1$s, "),
170 _(GUIMiscNames[f.uuline()]));
171 if (f.uwave() != FONT_INHERIT && (!terse || f.uwave() == FONT_ON))
172 os << bformat(_("Wavy underline %1$s, "),
173 _(GUIMiscNames[f.uwave()]));
174 if (f.strikeout() != FONT_INHERIT && (!terse || f.strikeout() == FONT_ON))
175 os << bformat(_("Strike out %1$s, "),
176 _(GUIMiscNames[f.strikeout()]));
177 if (f.xout() != FONT_INHERIT && (!terse || f.strikeout() == FONT_ON))
178 os << bformat(_("Cross out %1$s, "),
179 _(GUIMiscNames[f.xout()]));
180 if (f.noun() != FONT_INHERIT && (!terse || f.noun() != FONT_IGNORE))
181 os << bformat(_("Noun %1$s, "),
182 _(GUIMiscNames[f.noun()]));
183 if (f == inherit_font)
184 os << _("Default") << ", ";
190 docstring const Font::stateText(BufferParams * params, bool const terse) const
193 os << lyx::stateText(bits_, terse);
194 if ((!params || (language() != params->language))
195 && (!terse || language() != ignore_language)) {
196 // reset_language is a null pointer!
197 os << bformat(_("Language: %1$s, "),
198 (language() == reset_language) ? _("Default")
199 : _(language()->display()));
201 if (bits_.number() != FONT_OFF)
202 os << " " << bformat(_("Number %1$s"),
203 _(GUIMiscNames[bits_.number()]));
204 return rtrim(os.str(), ", ");
208 // Returns size in latex format
209 string const Font::latexSize() const
211 return LaTeXSizeNames[bits_.size()];
215 /// Writes the changes from this font to orgfont in .lyx format in file
216 void Font::lyxWriteChanges(Font const & orgfont,
220 if (orgfont.fontInfo().family() != bits_.family())
221 os << "\\family " << LyXFamilyNames[bits_.family()] << "\n";
222 if (orgfont.fontInfo().series() != bits_.series())
223 os << "\\series " << LyXSeriesNames[bits_.series()] << "\n";
224 if (orgfont.fontInfo().shape() != bits_.shape())
225 os << "\\shape " << LyXShapeNames[bits_.shape()] << "\n";
226 if (orgfont.fontInfo().size() != bits_.size())
227 os << "\\size " << LyXSizeNames[bits_.size()] << "\n";
228 if (orgfont.fontInfo().emph() != bits_.emph())
229 os << "\\emph " << LyXMiscNames[bits_.emph()] << "\n";
230 if (orgfont.fontInfo().number() != bits_.number())
231 os << "\\numeric " << LyXMiscNames[bits_.number()] << "\n";
232 if (orgfont.fontInfo().nospellcheck() != bits_.nospellcheck())
233 os << "\\nospellcheck " << LyXMiscNames[bits_.nospellcheck()] << "\n";
234 if (orgfont.fontInfo().underbar() != bits_.underbar()) {
235 // This is only for backwards compatibility
236 switch (bits_.underbar()) {
237 case FONT_OFF: os << "\\bar no\n"; break;
238 case FONT_ON: os << "\\bar under\n"; break;
239 case FONT_TOGGLE: lyxerr << "Font::lyxWriteFontChanges: "
240 "FONT_TOGGLE should not appear here!"
243 case FONT_INHERIT: os << "\\bar default\n"; break;
244 case FONT_IGNORE: lyxerr << "Font::lyxWriteFontChanges: "
245 "IGNORE should not appear here!"
250 if (orgfont.fontInfo().strikeout() != bits_.strikeout()) {
251 os << "\\strikeout " << LyXMiscNames[bits_.strikeout()] << "\n";
253 if (orgfont.fontInfo().xout() != bits_.xout()) {
254 os << "\\xout " << LyXMiscNames[bits_.xout()] << "\n";
256 if (orgfont.fontInfo().uuline() != bits_.uuline()) {
257 os << "\\uuline " << LyXMiscNames[bits_.uuline()] << "\n";
259 if (orgfont.fontInfo().uwave() != bits_.uwave()) {
260 os << "\\uwave " << LyXMiscNames[bits_.uwave()] << "\n";
262 if (orgfont.fontInfo().noun() != bits_.noun()) {
263 os << "\\noun " << LyXMiscNames[bits_.noun()] << "\n";
265 if (orgfont.fontInfo().color() != bits_.color())
266 os << "\\color " << lcolor.getLyXName(bits_.color()) << '\n';
267 // FIXME: uncomment this when we support background.
268 //if (orgfont.fontInfo().background() != bits_.background())
269 // os << "\\color " << lcolor.getLyXName(bits_.background()) << '\n';
270 if (orgfont.language() != language() &&
271 language() != latex_language) {
273 os << "\\lang " << language()->lang() << "\n";
275 os << "\\lang unknown\n";
280 /// Writes the head of the LaTeX needed to impose this font
281 // Returns number of chars written.
282 int Font::latexWriteStartChanges(odocstream & os, BufferParams const & bparams,
283 OutputParams const & runparams,
285 Font const & prev) const
291 // polyglossia or babel?
292 if (runparams.use_polyglossia
293 && language()->lang() != base.language()->lang()
294 && language() != prev.language()) {
295 if (!language()->polyglossia().empty()) {
296 string tmp = "\\text" + language()->polyglossia();
297 if (!language()->polyglossiaOpts().empty())
298 tmp += "[" + language()->polyglossiaOpts() + "]";
300 os << from_ascii(tmp);
301 count += tmp.length();
302 pushLanguageName(language()->polyglossia(), true);
303 } else if (language()->encoding()->package() != Encoding::CJK) {
307 } else if (language()->babel() != base.language()->babel() &&
308 language() != prev.language()) {
309 if (language()->lang() == "farsi") {
312 } else if (!isRightToLeft() &&
313 base.language()->lang() == "farsi") {
316 } else if (language()->lang() == "arabic_arabi") {
319 } else if (!isRightToLeft() &&
320 base.language()->lang() == "arabic_arabi") {
323 // currently the remaining RTL languages are arabic_arabtex and hebrew
324 } else if (isRightToLeft() != prev.isRightToLeft()) {
325 if (isRightToLeft()) {
332 } else if (!language()->babel().empty()) {
334 subst(lyxrc.language_command_local,
335 "$$lang", language()->babel());
336 os << from_ascii(tmp);
337 count += tmp.length();
338 if (!lyxrc.language_command_end.empty())
339 pushLanguageName(language()->babel(), true);
340 } else if (language()->encoding()->package() != Encoding::CJK) {
346 if (language()->encoding()->package() == Encoding::CJK) {
347 pair<bool, int> const c = switchEncoding(os, bparams,
348 runparams, *(language()->encoding()));
350 open_encoding_ = true;
352 runparams.encoding = language()->encoding();
357 f.reduce(base.bits_);
359 p.reduce(prev.bits_);
361 if (f.family() != INHERIT_FAMILY) {
363 << LaTeXFamilyNames[f.family()]
365 count += strlen(LaTeXFamilyNames[f.family()]) + 2;
366 env = true; //We have opened a new environment
368 if (f.series() != INHERIT_SERIES) {
370 << LaTeXSeriesNames[f.series()]
372 count += strlen(LaTeXSeriesNames[f.series()]) + 2;
373 env = true; //We have opened a new environment
375 if (f.shape() != INHERIT_SHAPE) {
377 << LaTeXShapeNames[f.shape()]
379 count += strlen(LaTeXShapeNames[f.shape()]) + 2;
380 env = true; //We have opened a new environment
382 if (f.color() != Color_inherit && f.color() != Color_ignore) {
383 if (f.color() == Color_none && p.color() != Color_none) {
384 // Color none: Close previous color, if any
387 } else if (f.color() != Color_none) {
389 << from_ascii(lcolor.getLaTeXName(f.color()))
391 count += lcolor.getLaTeXName(f.color()).length() + 13;
393 env = true; //We have opened a new environment
395 // FIXME: uncomment this when we support background.
397 if (f.background() != Color_inherit && f.background() != Color_ignore) {
399 << from_ascii(lcolor.getLaTeXName(f.background()))
401 count += lcolor.getLaTeXName(f.background()).length() + 13;
402 env = true; //We have opened a new environment
405 // If the current language is Hebrew, Arabic, or Farsi
406 // the numbers are written Left-to-Right. ArabTeX package
407 // and bidi (polyglossia) reorder the number automatically
408 // but the packages used for Hebrew and Farsi (Arabi) do not.
409 if (!runparams.use_polyglossia
410 && !runparams.pass_thru
411 && bits_.number() == FONT_ON
412 && prev.fontInfo().number() != FONT_ON
413 && (language()->lang() == "hebrew"
414 || language()->lang() == "farsi"
415 || language()->lang() == "arabic_arabi")) {
419 if (f.emph() == FONT_ON) {
422 env = true; //We have opened a new environment
424 // \noun{} is a LyX special macro
425 if (f.noun() == FONT_ON) {
428 env = true; //We have opened a new environment
430 if (f.size() != FONT_SIZE_INHERIT) {
431 // If we didn't open an environment above, we open one here
437 << LaTeXSizeNames[f.size()]
439 count += strlen(LaTeXSizeNames[f.size()]) + 3;
441 // The ulem commands need to be on the deepest nesting level
442 // because ulem puts every nested group or macro in a box,
443 // which prevents linebreaks (#8424, #8733)
444 if (f.underbar() == FONT_ON) {
447 ++runparams.inulemcmd;
449 if (f.uuline() == FONT_ON) {
452 ++runparams.inulemcmd;
454 if (f.strikeout() == FONT_ON) {
457 ++runparams.inulemcmd;
459 if (f.xout() == FONT_ON) {
462 ++runparams.inulemcmd;
464 if (f.uwave() == FONT_ON) {
465 if (runparams.inulemcmd) {
466 // needed with nested uwave in xout
467 // see https://tex.stackexchange.com/a/263042
468 os << "\\ULdepth=1000pt";
473 ++runparams.inulemcmd;
479 /// Writes ending block of LaTeX needed to close use of this font
480 // Returns number of chars written
481 // This one corresponds to latexWriteStartChanges(). (Asger)
482 int Font::latexWriteEndChanges(otexstream & os, BufferParams const & bparams,
483 OutputParams const & runparams,
487 bool const & closeLanguage) const
492 // reduce the current font to changes against the base
493 // font (of the layout). We use a temporary for this to
494 // avoid changing this font instance, as that would break
496 f.reduce(base.bits_);
498 if (f.family() != INHERIT_FAMILY) {
501 env = true; // Size change need not bother about closing env.
503 if (f.series() != INHERIT_SERIES) {
506 env = true; // Size change need not bother about closing env.
508 if (f.shape() != INHERIT_SHAPE) {
511 env = true; // Size change need not bother about closing env.
513 if (f.color() != Color_inherit && f.color() != Color_ignore && f.color() != Color_none) {
516 env = true; // Size change need not bother about closing env.
518 if (f.emph() == FONT_ON) {
521 env = true; // Size change need not bother about closing env.
523 if (f.noun() == FONT_ON) {
526 env = true; // Size change need not bother about closing env.
528 if (f.size() != FONT_SIZE_INHERIT) {
529 // We only have to close if only size changed
531 if (needPar && !closeLanguage) {
540 if (f.underbar() == FONT_ON) {
543 --runparams.inulemcmd;
545 if (f.strikeout() == FONT_ON) {
548 --runparams.inulemcmd;
550 if (f.xout() == FONT_ON) {
553 --runparams.inulemcmd;
555 if (f.uuline() == FONT_ON) {
558 --runparams.inulemcmd;
560 if (f.uwave() == FONT_ON) {
563 --runparams.inulemcmd;
566 // If the current language is Hebrew, Arabic, or Farsi
567 // the numbers are written Left-to-Right. ArabTeX package
568 // and bidi (polyglossia) reorder the number automatically
569 // but the packages used for Hebrew and Farsi (Arabi) do not.
570 if (!runparams.use_polyglossia
571 && !runparams.pass_thru
572 && bits_.number() == FONT_ON
573 && next.fontInfo().number() != FONT_ON
574 && (language()->lang() == "hebrew"
575 || language()->lang() == "farsi"
576 || language()->lang() == "arabic_arabi")) {
581 if (open_encoding_) {
582 // We need to close the encoding even if it does not change
583 // to do correct environment nesting
584 Encoding const * const ascii = encodings.fromLyXName("ascii");
585 pair<bool, int> const c = switchEncoding(os.os(), bparams,
589 runparams.encoding = ascii;
590 open_encoding_ = false;
594 && language() != base.language() && language() != next.language()
595 && language()->encoding()->package() != Encoding::CJK) {
598 bool const using_begin_end =
599 runparams.use_polyglossia ||
600 !lyxrc.language_command_end.empty();
609 string Font::toString(bool const toggle) const
611 string const lang = (language() == reset_language)
612 ? "reset" : language()->lang();
615 os << "family " << bits_.family() << '\n'
616 << "series " << bits_.series() << '\n'
617 << "shape " << bits_.shape() << '\n'
618 << "size " << bits_.size() << '\n'
619 << "emph " << bits_.emph() << '\n'
620 << "underbar " << bits_.underbar() << '\n'
621 << "strikeout " << bits_.strikeout() << '\n'
622 << "xout " << bits_.xout() << '\n'
623 << "uuline " << bits_.uuline() << '\n'
624 << "uwave " << bits_.uwave() << '\n'
625 << "noun " << bits_.noun() << '\n'
626 << "number " << bits_.number() << '\n'
627 << "nospellcheck " << bits_.nospellcheck() << '\n'
628 << "color " << bits_.color() << '\n'
629 << "language " << lang << '\n'
630 << "toggleall " << convert<string>(toggle);
635 bool Font::fromString(string const & data, bool & toggle)
637 istringstream is(data);
645 token = lex.getString();
647 if (token.empty() || !lex.next())
650 if (token == "family") {
651 int const next = lex.getInteger();
652 bits_.setFamily(FontFamily(next));
654 } else if (token == "series") {
655 int const next = lex.getInteger();
656 bits_.setSeries(FontSeries(next));
658 } else if (token == "shape") {
659 int const next = lex.getInteger();
660 bits_.setShape(FontShape(next));
662 } else if (token == "size") {
663 int const next = lex.getInteger();
664 bits_.setSize(FontSize(next));
666 } else if (token == "emph" || token == "underbar"
667 || token == "noun" || token == "number"
668 || token == "uuline" || token == "uwave"
669 || token == "strikeout" || token == "xout"
670 || token == "nospellcheck") {
672 int const next = lex.getInteger();
673 FontState const misc = FontState(next);
677 else if (token == "underbar")
678 bits_.setUnderbar(misc);
679 else if (token == "strikeout")
680 bits_.setStrikeout(misc);
681 else if (token == "xout")
683 else if (token == "uuline")
684 bits_.setUuline(misc);
685 else if (token == "uwave")
686 bits_.setUwave(misc);
687 else if (token == "noun")
689 else if (token == "number")
690 bits_.setNumber(misc);
691 else if (token == "nospellcheck")
692 bits_.setNoSpellcheck(misc);
694 } else if (token == "color") {
695 int const next = lex.getInteger();
696 bits_.setColor(ColorCode(next));
699 } else if (token == "background") {
700 int const next = lex.getInteger();
701 bits_.setBackground(ColorCode(next));
704 } else if (token == "language") {
705 string const next = lex.getString();
706 setLanguage(languages.getLanguage(next));
708 } else if (token == "toggleall") {
709 toggle = lex.getBool();
712 // Unrecognised token
722 void Font::validate(LaTeXFeatures & features) const
724 BufferParams const & bparams = features.bufferParams();
725 Language const * doc_language = bparams.language;
727 if (bits_.noun() == FONT_ON) {
728 LYXERR(Debug::LATEX, "font.noun: " << bits_.noun());
729 features.require("noun");
730 LYXERR(Debug::LATEX, "Noun enabled. Font: " << to_utf8(stateText(0)));
732 if (bits_.underbar() == FONT_ON) {
733 LYXERR(Debug::LATEX, "font.underline: " << bits_.underbar());
734 features.require("ulem");
735 LYXERR(Debug::LATEX, "Underline enabled. Font: " << to_utf8(stateText(0)));
737 if (bits_.strikeout() == FONT_ON) {
738 LYXERR(Debug::LATEX, "font.strikeout: " << bits_.strikeout());
739 features.require("ulem");
740 LYXERR(Debug::LATEX, "Strike out enabled. Font: " << to_utf8(stateText(0)));
742 if (bits_.xout() == FONT_ON) {
743 LYXERR(Debug::LATEX, "font.xout: " << bits_.xout());
744 features.require("ulem");
745 LYXERR(Debug::LATEX, "Cross out enabled. Font: " << to_utf8(stateText(0)));
747 if (bits_.uuline() == FONT_ON) {
748 LYXERR(Debug::LATEX, "font.uuline: " << bits_.uuline());
749 features.require("ulem");
750 LYXERR(Debug::LATEX, "Double underline enabled. Font: " << to_utf8(stateText(0)));
752 if (bits_.uwave() == FONT_ON) {
753 LYXERR(Debug::LATEX, "font.uwave: " << bits_.uwave());
754 features.require("ulem");
755 LYXERR(Debug::LATEX, "Wavy underline enabled. Font: " << to_utf8(stateText(0)));
757 switch (bits_.color()) {
761 // probably we should put here all interface colors used for
762 // font displaying! For now I just add this ones I know of (Jug)
764 case Color_notelabel:
769 case Color_lightgray:
777 features.require("xcolor");
780 features.require("color");
781 LYXERR(Debug::LATEX, "Color enabled. Font: " << to_utf8(stateText(0)));
784 // FIXME: Do something for background and soul package?
786 if (((features.usePolyglossia() && lang_->polyglossia() != doc_language->polyglossia())
787 || (features.useBabel() && lang_->babel() != doc_language->babel())
788 || (doc_language->encoding()->package() == Encoding::CJK && lang_ != doc_language))
789 && lang_ != ignore_language
790 && lang_ != latex_language)
792 features.useLanguage(lang_);
793 LYXERR(Debug::LATEX, "Found language " << lang_->lang());
798 ostream & operator<<(ostream & os, FontState fms)
800 return os << int(fms);
804 ostream & operator<<(ostream & os, FontInfo const & f)
807 << " family " << f.family()
808 << " series " << f.series()
809 << " shape " << f.shape()
810 << " size " << f.size()
811 << " color " << f.color()
812 // FIXME: uncomment this when we support background.
813 //<< " background " << f.background()
814 << " emph " << f.emph()
815 << " underbar " << f.underbar()
816 << " strikeout " << f.strikeout()
817 << " xout " << f.xout()
818 << " uuline " << f.uuline()
819 << " uwave " << f.uwave()
820 << " noun " << f.noun()
821 << " number " << f.number()
822 << " nospellcheck " << f.nospellcheck();
826 ostream & operator<<(ostream & os, Font const & font)
828 return os << font.bits_
829 << " lang: " << (font.lang_ ? font.lang_->lang() : 0);