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];
52 extern char const * GUIMiscNames[5];
57 // Strings used to write LaTeX files
59 char const * LaTeXFamilyCommandNames[NUM_FAMILIES + 2] =
60 { "textrm", "textsf", "texttt", "error1", "error2", "error3", "error4",
61 "error5", "error6", "error7", "error8", "error9", "error10", "error11",
62 "error12", "error13", "error14" };
64 char const * LaTeXFamilySwitchNames[NUM_FAMILIES + 2] =
65 { "rmfamily", "sffamily", "ttfamily", "error1", "error2", "error3", "error4",
66 "error5", "error6", "error7", "error8", "error9", "error10", "error11",
67 "error12", "error13", "error14" };
69 char const * LaTeXSeriesCommandNames[NUM_SERIES + 2] =
70 { "textmd", "textbf", "error4", "error5" };
72 char const * LaTeXSeriesSwitchNames[NUM_SERIES + 2] =
73 { "mdseries", "bfseries", "error4", "error5" };
75 char const * LaTeXShapeCommandNames[NUM_SHAPE + 2] =
76 { "textup", "textit", "textsl", "textsc", "error6", "error7" };
78 char const * LaTeXShapeSwitchNames[NUM_SHAPE + 2] =
79 { "upshape", "itshape", "slshape", "scshape", "error6", "error7" };
81 char const * LaTeXSizeSwitchNames[NUM_SIZE + 4] =
82 { "tiny", "scriptsize", "footnotesize", "small", "normalsize", "large",
83 "Large", "LARGE", "huge", "Huge", "error8", "error9", "error10", "error11" };
88 Font::Font(FontInfo bits, Language const * l)
89 : bits_(bits), lang_(l), open_encoding_(false)
92 lang_ = default_language;
96 bool Font::isRightToLeft() const
98 return lang_->rightToLeft();
102 bool Font::isVisibleRightToLeft() const
104 return (lang_->rightToLeft() &&
105 bits_.number() != FONT_ON);
109 void Font::setLanguage(Language const * l)
115 void Font::setProperties(FontInfo const & f)
117 bits_.setFamily(f.family());
118 bits_.setSeries(f.series());
119 bits_.setShape(f.shape());
120 bits_.setSize(f.size());
124 /// Updates font settings according to request
125 void Font::update(Font const & newfont,
126 Language const * document_language,
129 bits_.update(newfont.fontInfo(), toggleall);
131 if (newfont.language() == language() && toggleall)
132 if (language() == document_language)
133 setLanguage(default_language);
135 setLanguage(document_language);
136 else if (newfont.language() == reset_language)
137 setLanguage(document_language);
138 else if (newfont.language() != ignore_language)
139 setLanguage(newfont.language());
143 docstring const Font::stateText(BufferParams * params, bool const terse) const
146 os << bits_.stateText(terse);
147 if ((!params || (language() != params->language))
148 && (!terse || language() != ignore_language)) {
149 // reset_language is a null pointer!
150 os << bformat(_("Language: %1$s, "),
151 (language() == reset_language) ? _("Default")
152 : _(language()->display()));
154 if (bits_.number() != FONT_OFF)
155 os << " " << bformat(_("Number %1$s"),
156 _(GUIMiscNames[bits_.number()]));
157 return rtrim(os.str(), ", ");
161 // Returns size in latex format
162 string const Font::latexSize() const
164 return LaTeXSizeSwitchNames[bits_.size()];
168 /// Writes the changes from this font to orgfont in .lyx format in file
169 void Font::lyxWriteChanges(Font const & orgfont,
173 if (orgfont.fontInfo().family() != bits_.family())
174 os << "\\family " << LyXFamilyNames[bits_.family()] << "\n";
175 if (orgfont.fontInfo().series() != bits_.series())
176 os << "\\series " << LyXSeriesNames[bits_.series()] << "\n";
177 if (orgfont.fontInfo().shape() != bits_.shape())
178 os << "\\shape " << LyXShapeNames[bits_.shape()] << "\n";
179 if (orgfont.fontInfo().size() != bits_.size())
180 os << "\\size " << LyXSizeNames[bits_.size()] << "\n";
181 // FIXME: shall style be handled there? Probably not.
182 if (orgfont.fontInfo().emph() != bits_.emph())
183 os << "\\emph " << LyXMiscNames[bits_.emph()] << "\n";
184 if (orgfont.fontInfo().number() != bits_.number())
185 os << "\\numeric " << LyXMiscNames[bits_.number()] << "\n";
186 if (orgfont.fontInfo().nospellcheck() != bits_.nospellcheck())
187 os << "\\nospellcheck " << LyXMiscNames[bits_.nospellcheck()] << "\n";
188 if (orgfont.fontInfo().underbar() != bits_.underbar()) {
189 // This is only for backwards compatibility
190 switch (bits_.underbar()) {
191 case FONT_OFF: os << "\\bar no\n"; break;
192 case FONT_ON: os << "\\bar under\n"; break;
193 case FONT_TOGGLE: lyxerr << "Font::lyxWriteFontChanges: "
194 "FONT_TOGGLE should not appear here!"
197 case FONT_INHERIT: os << "\\bar default\n"; break;
198 case FONT_IGNORE: lyxerr << "Font::lyxWriteFontChanges: "
199 "IGNORE should not appear here!"
204 if (orgfont.fontInfo().strikeout() != bits_.strikeout()) {
205 os << "\\strikeout " << LyXMiscNames[bits_.strikeout()] << "\n";
207 if (orgfont.fontInfo().xout() != bits_.xout()) {
208 os << "\\xout " << LyXMiscNames[bits_.xout()] << "\n";
210 if (orgfont.fontInfo().uuline() != bits_.uuline()) {
211 os << "\\uuline " << LyXMiscNames[bits_.uuline()] << "\n";
213 if (orgfont.fontInfo().uwave() != bits_.uwave()) {
214 os << "\\uwave " << LyXMiscNames[bits_.uwave()] << "\n";
216 if (orgfont.fontInfo().noun() != bits_.noun()) {
217 os << "\\noun " << LyXMiscNames[bits_.noun()] << "\n";
219 if (orgfont.fontInfo().color() != bits_.color())
220 os << "\\color " << lcolor.getLyXName(bits_.color()) << '\n';
221 // FIXME: uncomment this when we support background.
222 //if (orgfont.fontInfo().background() != bits_.background())
223 // os << "\\color " << lcolor.getLyXName(bits_.background()) << '\n';
224 if (orgfont.language() != language() &&
225 language() != latex_language) {
227 os << "\\lang " << language()->lang() << "\n";
229 os << "\\lang unknown\n";
234 /// Writes the head of the LaTeX needed to impose this font
235 // Returns number of chars written.
236 int Font::latexWriteStartChanges(otexstream & os, BufferParams const & bparams,
237 OutputParams const & runparams,
240 bool const & multipar_inset,
241 bool const & needs_cprotection) const
245 // polyglossia or babel?
246 if (runparams.use_polyglossia
247 && language()->lang() != base.language()->lang()
248 && language() != prev.language()) {
249 if (!language()->polyglossia().empty()) {
251 if (needs_cprotection)
253 tmp += "\\text" + language()->polyglossia();
254 if (!language()->polyglossiaOpts().empty()) {
255 tmp += "[" + language()->polyglossiaOpts() + "]";
256 if (runparams.use_hyperref && runparams.moving_arg) {
257 // We need to strip the command for
258 // the pdf string, see #11813
260 if (needs_cprotection)
262 tmp = tmpp + "\\texorpdfstring{" + tmp + "}{}";
266 os << from_ascii(tmp);
267 count += tmp.length();
268 pushLanguageName(language()->polyglossia(), true);
269 } else if (language()->encoding()->package() != Encoding::CJK) {
273 } else if (language()->babel() != base.language()->babel() &&
274 language() != prev.language()) {
275 if (language()->lang() == "farsi") {
276 if (needs_cprotection) {
282 } else if (!isRightToLeft() &&
283 base.language()->lang() == "farsi") {
284 if (needs_cprotection) {
290 } else if (language()->lang() == "arabic_arabi") {
291 if (needs_cprotection) {
297 } else if (!isRightToLeft() &&
298 base.language()->lang() == "arabic_arabi") {
299 if (needs_cprotection) {
305 // currently the remaining RTL languages are arabic_arabtex and hebrew
306 } else if (isRightToLeft() != prev.isRightToLeft()) {
307 if (needs_cprotection) {
311 if (isRightToLeft()) {
318 } else if (!language()->babel().empty()) {
320 subst(lyxrc.language_command_local,
321 "$$lang", language()->babel());
322 os << from_ascii(tmp);
323 count += tmp.length();
324 if (!lyxrc.language_command_end.empty())
325 pushLanguageName(language()->babel(), true);
326 } else if (language()->encoding()->package() != Encoding::CJK) {
332 if (language()->encoding()->package() == Encoding::CJK) {
333 pair<bool, int> const c = switchEncoding(os.os(), bparams,
334 runparams, *(language()->encoding()));
336 open_encoding_ = true;
338 runparams.encoding = language()->encoding();
343 f.reduce(base.bits_);
345 p.reduce(prev.bits_);
347 if (f.size() != INHERIT_SIZE) {
351 << LaTeXSizeSwitchNames[f.size()] << termcmd;
352 count += strlen(LaTeXSizeSwitchNames[f.size()]) + 1;
354 if (f.family() != INHERIT_FAMILY) {
355 if (multipar_inset) {
358 os << '\\' << LaTeXFamilySwitchNames[f.family()] << termcmd;
359 count += strlen(LaTeXFamilySwitchNames[f.family()]) + 1;
361 if (needs_cprotection) {
366 << LaTeXFamilyCommandNames[f.family()]
368 count += strlen(LaTeXFamilyCommandNames[f.family()]) + 2;
371 if (f.series() != INHERIT_SERIES) {
372 if (multipar_inset) {
375 os << '\\' << LaTeXSeriesSwitchNames[f.series()] << termcmd;
376 count += strlen(LaTeXSeriesSwitchNames[f.series()]) + 1;
378 if (needs_cprotection) {
383 << LaTeXSeriesCommandNames[f.series()]
385 count += strlen(LaTeXSeriesCommandNames[f.series()]) + 2;
388 if (f.shape() != INHERIT_SHAPE) {
389 if (multipar_inset) {
392 os << '\\' << LaTeXShapeSwitchNames[f.shape()] << termcmd;
393 count += strlen(LaTeXShapeSwitchNames[f.shape()]) + 1;
395 if (needs_cprotection) {
400 << LaTeXShapeCommandNames[f.shape()]
402 count += strlen(LaTeXShapeCommandNames[f.shape()]) + 2;
405 if (f.color() != Color_inherit && f.color() != Color_ignore && !multipar_inset) {
406 if (f.color() == Color_none && p.color() != Color_none) {
407 // Color none: Close previous color, if any
410 } else if (f.color() != Color_none) {
411 if (needs_cprotection) {
416 << from_ascii(lcolor.getLaTeXName(f.color()))
418 count += lcolor.getLaTeXName(f.color()).length() + 13;
421 // FIXME: uncomment this when we support background.
423 if (f.background() != Color_inherit && f.background() != Color_ignore) {
425 << from_ascii(lcolor.getLaTeXName(f.background()))
427 count += lcolor.getLaTeXName(f.background()).length() + 13;
428 env = true; //We have opened a new environment
431 // If the current language is Hebrew, Arabic, or Farsi
432 // the numbers are written Left-to-Right. ArabTeX package
433 // and bidi (polyglossia with XeTeX) reorder the number automatically
434 // but the packages used for Hebrew and Farsi (Arabi) do not.
435 if (!runparams.useBidiPackage()
436 && !runparams.pass_thru
437 && bits_.number() == FONT_ON
438 && prev.fontInfo().number() != FONT_ON
439 && (language()->lang() == "hebrew"
440 || language()->lang() == "farsi"
441 || language()->lang() == "arabic_arabi")) {
442 if (runparams.use_polyglossia) {
451 if (f.emph() == FONT_ON && !multipar_inset) {
452 if (needs_cprotection) {
459 // \noun{} is a LyX special macro
460 if (f.noun() == FONT_ON && !multipar_inset) {
461 if (needs_cprotection) {
468 // The ulem commands need to be on the deepest nesting level
469 // because ulem puts every nested group or macro in a box,
470 // which prevents linebreaks (#8424, #8733)
471 if (f.underbar() == FONT_ON && !multipar_inset) {
472 if (needs_cprotection) {
478 ++runparams.inulemcmd;
480 if (f.uuline() == FONT_ON && !multipar_inset) {
481 if (needs_cprotection) {
487 ++runparams.inulemcmd;
489 if (f.strikeout() == FONT_ON && !multipar_inset) {
490 if (needs_cprotection) {
496 ++runparams.inulemcmd;
498 if (f.xout() == FONT_ON && !multipar_inset) {
499 if (needs_cprotection) {
505 ++runparams.inulemcmd;
507 if (f.uwave() == FONT_ON && !multipar_inset) {
508 if (runparams.inulemcmd) {
509 // needed with nested uwave in xout
510 // see https://tex.stackexchange.com/a/263042
511 os << "\\ULdepth=1000pt";
514 if (needs_cprotection) {
520 ++runparams.inulemcmd;
526 /// Writes ending block of LaTeX needed to close use of this font
527 // Returns number of chars written
528 // This one corresponds to latexWriteStartChanges(). (Asger)
529 int Font::latexWriteEndChanges(otexstream & os, BufferParams const & bparams,
530 OutputParams const & runparams,
534 bool const & closeLanguage,
535 bool const & multipar_inset) const
539 // reduce the current font to changes against the base
540 // font (of the layout). We use a temporary for this to
541 // avoid changing this font instance, as that would break
543 f.reduce(base.bits_);
545 if (f.family() != INHERIT_FAMILY && !multipar_inset) {
549 if (f.series() != INHERIT_SERIES && !multipar_inset) {
553 if (f.shape() != INHERIT_SHAPE && !multipar_inset) {
557 if (f.color() != Color_inherit && f.color() != Color_ignore && f.color() != Color_none) {
561 if (f.emph() == FONT_ON) {
565 if (f.noun() == FONT_ON) {
569 if (f.size() != INHERIT_SIZE) {
570 // We do not close size group in front of
571 // insets with allowMultiPar() true (as opposed
572 // to all other font properties) (#8384)
573 if (!multipar_inset) {
574 if (needPar && !closeLanguage) {
583 if (f.underbar() == FONT_ON) {
586 --runparams.inulemcmd;
588 if (f.strikeout() == FONT_ON) {
591 --runparams.inulemcmd;
593 if (f.xout() == FONT_ON) {
596 --runparams.inulemcmd;
598 if (f.uuline() == FONT_ON) {
601 --runparams.inulemcmd;
603 if (f.uwave() == FONT_ON) {
606 --runparams.inulemcmd;
609 // If the current language is Hebrew, Arabic, or Farsi
610 // the numbers are written Left-to-Right. ArabTeX package
611 // and bidi (polyglossia with XeTeX) reorder the number automatically
612 // but the packages used for Hebrew and Farsi (Arabi) do not.
613 if (!runparams.useBidiPackage()
614 && !runparams.pass_thru
615 && bits_.number() == FONT_ON
616 && next.fontInfo().number() != FONT_ON
617 && (language()->lang() == "hebrew"
618 || language()->lang() == "farsi"
619 || language()->lang() == "arabic_arabi")) {
620 if (runparams.use_polyglossia) {
630 if (open_encoding_) {
631 // We need to close the encoding even if it does not change
632 // to do correct environment nesting
633 Encoding const * const ascii = encodings.fromLyXName("ascii");
634 pair<bool, int> const c = switchEncoding(os.os(), bparams,
638 runparams.encoding = ascii;
639 open_encoding_ = false;
643 && language() != base.language() && language() != next.language()
644 && (language()->encoding()->package() != Encoding::CJK)) {
647 bool const using_begin_end =
648 runparams.use_polyglossia ||
649 !lyxrc.language_command_end.empty();
658 string Font::toString(bool const toggle) const
660 string const lang = (language() == reset_language)
661 ? "reset" : language()->lang();
664 os << "family " << bits_.family() << '\n'
665 << "series " << bits_.series() << '\n'
666 << "shape " << bits_.shape() << '\n'
667 << "size " << bits_.size() << '\n'
668 << "emph " << bits_.emph() << '\n'
669 << "underbar " << bits_.underbar() << '\n'
670 << "strikeout " << bits_.strikeout() << '\n'
671 << "xout " << bits_.xout() << '\n'
672 << "uuline " << bits_.uuline() << '\n'
673 << "uwave " << bits_.uwave() << '\n'
674 << "noun " << bits_.noun() << '\n'
675 << "number " << bits_.number() << '\n'
676 << "nospellcheck " << bits_.nospellcheck() << '\n'
677 << "color " << bits_.color() << '\n'
678 << "language " << lang << '\n'
679 << "toggleall " << convert<string>(toggle);
684 bool Font::fromString(string const & data, bool & toggle)
686 istringstream is(data);
694 token = lex.getString();
696 if (token.empty() || !lex.next())
699 if (token == "family") {
700 int const next = lex.getInteger();
701 bits_.setFamily(FontFamily(next));
703 } else if (token == "series") {
704 int const next = lex.getInteger();
705 bits_.setSeries(FontSeries(next));
707 } else if (token == "shape") {
708 int const next = lex.getInteger();
709 bits_.setShape(FontShape(next));
711 } else if (token == "size") {
712 int const next = lex.getInteger();
713 bits_.setSize(FontSize(next));
714 // FIXME: shall style be handled there? Probably not.
715 } else if (token == "emph" || token == "underbar"
716 || token == "noun" || token == "number"
717 || token == "uuline" || token == "uwave"
718 || token == "strikeout" || token == "xout"
719 || token == "nospellcheck") {
721 int const next = lex.getInteger();
722 FontState const misc = FontState(next);
726 else if (token == "underbar")
727 bits_.setUnderbar(misc);
728 else if (token == "strikeout")
729 bits_.setStrikeout(misc);
730 else if (token == "xout")
732 else if (token == "uuline")
733 bits_.setUuline(misc);
734 else if (token == "uwave")
735 bits_.setUwave(misc);
736 else if (token == "noun")
738 else if (token == "number")
739 bits_.setNumber(misc);
740 else if (token == "nospellcheck")
741 bits_.setNoSpellcheck(misc);
743 } else if (token == "color") {
744 int const next = lex.getInteger();
745 bits_.setColor(ColorCode(next));
748 } else if (token == "background") {
749 int const next = lex.getInteger();
750 bits_.setBackground(ColorCode(next));
753 } else if (token == "language") {
754 string const next = lex.getString();
755 setLanguage(languages.getLanguage(next));
757 } else if (token == "toggleall") {
758 toggle = lex.getBool();
761 // Unrecognised token
771 void Font::validate(LaTeXFeatures & features) const
773 BufferParams const & bparams = features.bufferParams();
774 Language const * doc_language = bparams.language;
776 if (bits_.noun() == FONT_ON) {
777 LYXERR(Debug::LATEX, "font.noun: " << bits_.noun());
778 features.require("noun");
779 LYXERR(Debug::LATEX, "Noun enabled. Font: " << to_utf8(stateText()));
781 if (bits_.underbar() == FONT_ON) {
782 LYXERR(Debug::LATEX, "font.underline: " << bits_.underbar());
783 features.require("ulem");
784 LYXERR(Debug::LATEX, "Underline enabled. Font: " << to_utf8(stateText()));
786 if (bits_.strikeout() == FONT_ON) {
787 LYXERR(Debug::LATEX, "font.strikeout: " << bits_.strikeout());
788 features.require("ulem");
789 LYXERR(Debug::LATEX, "Strike out enabled. Font: " << to_utf8(stateText()));
791 if (bits_.xout() == FONT_ON) {
792 LYXERR(Debug::LATEX, "font.xout: " << bits_.xout());
793 features.require("ulem");
794 LYXERR(Debug::LATEX, "Cross out enabled. Font: " << to_utf8(stateText()));
796 if (bits_.uuline() == FONT_ON) {
797 LYXERR(Debug::LATEX, "font.uuline: " << bits_.uuline());
798 features.require("ulem");
799 LYXERR(Debug::LATEX, "Double underline enabled. Font: " << to_utf8(stateText()));
801 if (bits_.uwave() == FONT_ON) {
802 LYXERR(Debug::LATEX, "font.uwave: " << bits_.uwave());
803 features.require("ulem");
804 LYXERR(Debug::LATEX, "Wavy underline enabled. Font: " << to_utf8(stateText()));
806 switch (bits_.color()) {
810 // probably we should put here all interface colors used for
811 // font displaying! For now I just add this ones I know of (Jug)
813 case Color_notelabel:
818 case Color_lightgray:
826 features.require("xcolor");
829 features.require("color");
830 LYXERR(Debug::LATEX, "Color enabled. Font: " << to_utf8(stateText()));
833 // FIXME: Do something for background and soul package?
835 if (((features.usePolyglossia() && lang_->polyglossia() != doc_language->polyglossia())
836 || (features.useBabel() && lang_->babel() != doc_language->babel())
837 || (doc_language->encoding()->package() == Encoding::CJK && lang_ != doc_language))
838 && lang_ != ignore_language
839 && lang_ != latex_language)
841 features.useLanguage(lang_);
842 LYXERR(Debug::LATEX, "Found language " << lang_->lang());
847 ostream & operator<<(ostream & os, FontState fms)
849 return os << int(fms);
853 ostream & operator<<(ostream & os, FontInfo const & f)
856 << " family " << f.family()
857 << " series " << f.series()
858 << " shape " << f.shape()
859 << " size " << f.size()
860 << " style " << f.style()
861 << " color " << f.color()
862 // FIXME: uncomment this when we support background.
863 //<< " background " << f.background()
864 << " emph " << f.emph()
865 << " underbar " << f.underbar()
866 << " strikeout " << f.strikeout()
867 << " xout " << f.xout()
868 << " uuline " << f.uuline()
869 << " uwave " << f.uwave()
870 << " noun " << f.noun()
871 << " number " << f.number()
872 << " nospellcheck " << f.nospellcheck();
876 ostream & operator<<(ostream & os, Font const & font)
878 return os << font.bits_
879 << " lang: " << (font.lang_ ? font.lang_->lang() : nullptr);