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 /// Updates font settings according to request
116 void Font::update(Font const & newfont,
117 Language const * document_language,
120 bits_.update(newfont.fontInfo(), toggleall);
122 if (newfont.language() == language() && toggleall)
123 if (language() == document_language)
124 setLanguage(default_language);
126 setLanguage(document_language);
127 else if (newfont.language() == reset_language)
128 setLanguage(document_language);
129 else if (newfont.language() != ignore_language)
130 setLanguage(newfont.language());
134 docstring const Font::stateText(BufferParams * params, bool const terse) const
137 os << bits_.stateText(terse);
138 if ((!params || (language() != params->language))
139 && (!terse || language() != ignore_language)) {
140 // reset_language is a null pointer!
141 os << bformat(_("Language: %1$s, "),
142 (language() == reset_language) ? _("Default")
143 : _(language()->display()));
145 if (bits_.number() != FONT_OFF)
146 os << " " << bformat(_("Number %1$s"),
147 _(GUIMiscNames[bits_.number()]));
148 return rtrim(os.str(), ", ");
152 // Returns size in latex format
153 string const Font::latexSize() const
155 return LaTeXSizeSwitchNames[bits_.size()];
159 /// Writes the changes from this font to orgfont in .lyx format in file
160 void Font::lyxWriteChanges(Font const & orgfont,
164 if (orgfont.fontInfo().family() != bits_.family())
165 os << "\\family " << LyXFamilyNames[bits_.family()] << "\n";
166 if (orgfont.fontInfo().series() != bits_.series())
167 os << "\\series " << LyXSeriesNames[bits_.series()] << "\n";
168 if (orgfont.fontInfo().shape() != bits_.shape())
169 os << "\\shape " << LyXShapeNames[bits_.shape()] << "\n";
170 if (orgfont.fontInfo().size() != bits_.size())
171 os << "\\size " << LyXSizeNames[bits_.size()] << "\n";
172 // FIXME: shall style be handled there? Probably not.
173 if (orgfont.fontInfo().emph() != bits_.emph())
174 os << "\\emph " << LyXMiscNames[bits_.emph()] << "\n";
175 if (orgfont.fontInfo().number() != bits_.number())
176 os << "\\numeric " << LyXMiscNames[bits_.number()] << "\n";
177 if (orgfont.fontInfo().nospellcheck() != bits_.nospellcheck())
178 os << "\\nospellcheck " << LyXMiscNames[bits_.nospellcheck()] << "\n";
179 if (orgfont.fontInfo().underbar() != bits_.underbar()) {
180 // This is only for backwards compatibility
181 switch (bits_.underbar()) {
182 case FONT_OFF: os << "\\bar no\n"; break;
183 case FONT_ON: os << "\\bar under\n"; break;
184 case FONT_TOGGLE: lyxerr << "Font::lyxWriteFontChanges: "
185 "FONT_TOGGLE should not appear here!"
188 case FONT_INHERIT: os << "\\bar default\n"; break;
189 case FONT_IGNORE: lyxerr << "Font::lyxWriteFontChanges: "
190 "IGNORE should not appear here!"
195 if (orgfont.fontInfo().strikeout() != bits_.strikeout()) {
196 os << "\\strikeout " << LyXMiscNames[bits_.strikeout()] << "\n";
198 if (orgfont.fontInfo().xout() != bits_.xout()) {
199 os << "\\xout " << LyXMiscNames[bits_.xout()] << "\n";
201 if (orgfont.fontInfo().uuline() != bits_.uuline()) {
202 os << "\\uuline " << LyXMiscNames[bits_.uuline()] << "\n";
204 if (orgfont.fontInfo().uwave() != bits_.uwave()) {
205 os << "\\uwave " << LyXMiscNames[bits_.uwave()] << "\n";
207 if (orgfont.fontInfo().noun() != bits_.noun()) {
208 os << "\\noun " << LyXMiscNames[bits_.noun()] << "\n";
210 if (orgfont.fontInfo().color() != bits_.color())
211 os << "\\color " << lcolor.getLyXName(bits_.color()) << '\n';
212 // FIXME: uncomment this when we support background.
213 //if (orgfont.fontInfo().background() != bits_.background())
214 // os << "\\color " << lcolor.getLyXName(bits_.background()) << '\n';
215 if (orgfont.language() != language() &&
216 language() != latex_language) {
218 os << "\\lang " << language()->lang() << "\n";
220 os << "\\lang unknown\n";
225 /// Writes the head of the LaTeX needed to impose this font
226 // Returns number of chars written.
227 int Font::latexWriteStartChanges(otexstream & os, BufferParams const & bparams,
228 OutputParams const & runparams,
231 bool const & non_inherit_inset,
232 bool const & needs_cprotection) const
236 // polyglossia or babel?
237 if (runparams.use_polyglossia
238 && language()->lang() != base.language()->lang()
239 && language() != prev.language()) {
240 if (!language()->polyglossia().empty()) {
242 if (needs_cprotection)
244 tmp += "\\text" + language()->polyglossia();
245 if (!language()->polyglossiaOpts().empty()) {
246 tmp += "[" + language()->polyglossiaOpts() + "]";
247 if (runparams.use_hyperref && runparams.moving_arg) {
248 // We need to strip the command for
249 // the pdf string, see #11813
251 if (needs_cprotection)
253 tmp = tmpp + "\\texorpdfstring{" + tmp + "}{}";
257 os << from_ascii(tmp);
258 count += tmp.length();
259 pushLanguageName(language()->polyglossia(), true);
260 } else if (language()->encoding()->package() != Encoding::CJK) {
264 } else if (language()->babel() != base.language()->babel() &&
265 language() != prev.language()) {
266 if (language()->lang() == "farsi") {
267 if (needs_cprotection) {
273 } else if (!isRightToLeft() &&
274 base.language()->lang() == "farsi") {
275 if (needs_cprotection) {
281 } else if (language()->lang() == "arabic_arabi") {
282 if (needs_cprotection) {
288 } else if (!isRightToLeft() &&
289 base.language()->lang() == "arabic_arabi") {
290 if (needs_cprotection) {
296 // currently the remaining RTL languages are arabic_arabtex and hebrew
297 } else if (isRightToLeft() != prev.isRightToLeft()) {
298 if (needs_cprotection) {
302 if (isRightToLeft()) {
309 } else if (!language()->babel().empty()) {
311 subst(lyxrc.language_command_local,
312 "$$lang", language()->babel());
313 if (needs_cprotection && !prefixIs(tmp, "\\begin{")) {
317 os << from_ascii(tmp);
318 count += tmp.length();
319 if (!lyxrc.language_command_end.empty())
320 pushLanguageName(language()->babel(), true);
321 } else if (language()->encoding()->package() != Encoding::CJK) {
327 if (language()->encoding()->package() == Encoding::CJK) {
328 pair<bool, int> const c = switchEncoding(os.os(), bparams,
329 runparams, *(language()->encoding()));
331 open_encoding_ = true;
333 runparams.encoding = language()->encoding();
338 f.reduce(base.bits_);
340 p.reduce(prev.bits_);
342 if (f.size() != INHERIT_SIZE) {
346 << LaTeXSizeSwitchNames[f.size()] << termcmd;
347 count += strlen(LaTeXSizeSwitchNames[f.size()]) + 1;
349 if (f.family() != INHERIT_FAMILY) {
350 if (non_inherit_inset) {
353 os << '\\' << LaTeXFamilySwitchNames[f.family()] << termcmd;
354 count += strlen(LaTeXFamilySwitchNames[f.family()]) + 1;
356 if (needs_cprotection) {
361 << LaTeXFamilyCommandNames[f.family()]
363 count += strlen(LaTeXFamilyCommandNames[f.family()]) + 2;
366 if (f.series() != INHERIT_SERIES) {
367 if (non_inherit_inset) {
370 os << '\\' << LaTeXSeriesSwitchNames[f.series()] << termcmd;
371 count += strlen(LaTeXSeriesSwitchNames[f.series()]) + 1;
373 if (needs_cprotection) {
378 << LaTeXSeriesCommandNames[f.series()]
380 count += strlen(LaTeXSeriesCommandNames[f.series()]) + 2;
383 if (f.shape() != INHERIT_SHAPE) {
384 if (non_inherit_inset) {
387 os << '\\' << LaTeXShapeSwitchNames[f.shape()] << termcmd;
388 count += strlen(LaTeXShapeSwitchNames[f.shape()]) + 1;
390 if (needs_cprotection) {
395 << LaTeXShapeCommandNames[f.shape()]
397 count += strlen(LaTeXShapeCommandNames[f.shape()]) + 2;
400 if (f.color() != Color_inherit && f.color() != Color_ignore) {
401 if (f.color() == Color_none && p.color() != Color_none) {
402 // Color none: Close previous color, if any
405 } else if (f.color() != Color_none) {
406 if (needs_cprotection) {
411 << from_ascii(lcolor.getLaTeXName(f.color()))
413 count += lcolor.getLaTeXName(f.color()).length() + 13;
416 // FIXME: uncomment this when we support background.
418 if (f.background() != Color_inherit && f.background() != Color_ignore) {
420 << from_ascii(lcolor.getLaTeXName(f.background()))
422 count += lcolor.getLaTeXName(f.background()).length() + 13;
423 env = true; //We have opened a new environment
426 // If the current language is Hebrew, Arabic, or Farsi
427 // the numbers are written Left-to-Right. ArabTeX package
428 // and bidi (polyglossia with XeTeX) reorder the number automatically
429 // but the packages used for Hebrew and Farsi (Arabi) do not.
430 if (!runparams.useBidiPackage()
431 && !runparams.pass_thru
432 && bits_.number() == FONT_ON
433 && prev.fontInfo().number() != FONT_ON
434 && (language()->lang() == "hebrew"
435 || language()->lang() == "farsi"
436 || language()->lang() == "arabic_arabi")) {
437 if (runparams.use_polyglossia) {
446 if (f.emph() == FONT_ON) {
447 if (needs_cprotection) {
454 // \noun{} is a LyX special macro
455 if (f.noun() == FONT_ON) {
456 if (needs_cprotection) {
463 // The ulem commands need to be on the deepest nesting level
464 // because ulem puts every nested group or macro in a box,
465 // which prevents linebreaks (#8424, #8733)
466 if (f.underbar() == FONT_ON) {
467 if (needs_cprotection) {
473 ++runparams.inulemcmd;
475 if (f.uuline() == FONT_ON) {
476 if (needs_cprotection) {
482 ++runparams.inulemcmd;
484 if (f.strikeout() == FONT_ON) {
485 if (needs_cprotection) {
491 ++runparams.inulemcmd;
493 if (f.xout() == FONT_ON) {
494 if (needs_cprotection) {
500 ++runparams.inulemcmd;
502 if (f.uwave() == FONT_ON) {
503 if (runparams.inulemcmd) {
504 // needed with nested uwave in xout
505 // see https://tex.stackexchange.com/a/263042
506 os << "\\ULdepth=1000pt";
509 if (needs_cprotection) {
515 ++runparams.inulemcmd;
521 /// Writes ending block of LaTeX needed to close use of this font
522 // Returns number of chars written
523 // This one corresponds to latexWriteStartChanges(). (Asger)
524 int Font::latexWriteEndChanges(otexstream & os, BufferParams const & bparams,
525 OutputParams const & runparams,
529 bool const & closeLanguage) const
533 // reduce the current font to changes against the base
534 // font (of the layout). We use a temporary for this to
535 // avoid changing this font instance, as that would break
537 f.reduce(base.bits_);
539 if (f.family() != INHERIT_FAMILY) {
543 if (f.series() != INHERIT_SERIES) {
547 if (f.shape() != INHERIT_SHAPE) {
551 if (f.color() != Color_inherit && f.color() != Color_ignore && f.color() != Color_none) {
555 if (f.emph() == FONT_ON) {
559 if (f.noun() == FONT_ON) {
563 if (f.size() != INHERIT_SIZE) {
564 // We do not close size group in front of
565 // insets with InheritFont() false (as opposed
566 // to all other font properties) (#8384)
567 if (needPar && !closeLanguage) {
575 if (f.underbar() == FONT_ON) {
578 --runparams.inulemcmd;
580 if (f.strikeout() == FONT_ON) {
583 --runparams.inulemcmd;
585 if (f.xout() == FONT_ON) {
588 --runparams.inulemcmd;
590 if (f.uuline() == FONT_ON) {
593 --runparams.inulemcmd;
595 if (f.uwave() == FONT_ON) {
598 --runparams.inulemcmd;
601 // If the current language is Hebrew, Arabic, or Farsi
602 // the numbers are written Left-to-Right. ArabTeX package
603 // and bidi (polyglossia with XeTeX) reorder the number automatically
604 // but the packages used for Hebrew and Farsi (Arabi) do not.
605 if (!runparams.useBidiPackage()
606 && !runparams.pass_thru
607 && bits_.number() == FONT_ON
608 && next.fontInfo().number() != FONT_ON
609 && (language()->lang() == "hebrew"
610 || language()->lang() == "farsi"
611 || language()->lang() == "arabic_arabi")) {
612 if (runparams.use_polyglossia) {
622 if (open_encoding_) {
623 // We need to close the encoding even if it does not change
624 // to do correct environment nesting
625 Encoding const * const ascii = encodings.fromLyXName("ascii");
626 pair<bool, int> const c = switchEncoding(os.os(), bparams,
630 runparams.encoding = ascii;
631 open_encoding_ = false;
635 && language() != base.language() && language() != next.language()
636 && (language()->encoding()->package() != Encoding::CJK)) {
639 bool const using_begin_end =
640 runparams.use_polyglossia ||
641 !lyxrc.language_command_end.empty();
650 string Font::toString(bool const toggle) const
652 string const lang = (language() == reset_language)
653 ? "reset" : language()->lang();
656 os << "family " << bits_.family() << '\n'
657 << "series " << bits_.series() << '\n'
658 << "shape " << bits_.shape() << '\n'
659 << "size " << bits_.size() << '\n'
660 << "emph " << bits_.emph() << '\n'
661 << "underbar " << bits_.underbar() << '\n'
662 << "strikeout " << bits_.strikeout() << '\n'
663 << "xout " << bits_.xout() << '\n'
664 << "uuline " << bits_.uuline() << '\n'
665 << "uwave " << bits_.uwave() << '\n'
666 << "noun " << bits_.noun() << '\n'
667 << "number " << bits_.number() << '\n'
668 << "nospellcheck " << bits_.nospellcheck() << '\n'
669 << "color " << bits_.color() << '\n'
670 << "language " << lang << '\n'
671 << "toggleall " << convert<string>(toggle);
676 bool Font::fromString(string const & data, bool & toggle)
678 istringstream is(data);
686 token = lex.getString();
688 if (token.empty() || !lex.next())
691 if (token == "family") {
692 int const next = lex.getInteger();
693 bits_.setFamily(FontFamily(next));
695 } else if (token == "series") {
696 int const next = lex.getInteger();
697 bits_.setSeries(FontSeries(next));
699 } else if (token == "shape") {
700 int const next = lex.getInteger();
701 bits_.setShape(FontShape(next));
703 } else if (token == "size") {
704 int const next = lex.getInteger();
705 bits_.setSize(FontSize(next));
706 // FIXME: shall style be handled there? Probably not.
707 } else if (token == "emph" || token == "underbar"
708 || token == "noun" || token == "number"
709 || token == "uuline" || token == "uwave"
710 || token == "strikeout" || token == "xout"
711 || token == "nospellcheck") {
713 int const next = lex.getInteger();
714 FontState const misc = FontState(next);
718 else if (token == "underbar")
719 bits_.setUnderbar(misc);
720 else if (token == "strikeout")
721 bits_.setStrikeout(misc);
722 else if (token == "xout")
724 else if (token == "uuline")
725 bits_.setUuline(misc);
726 else if (token == "uwave")
727 bits_.setUwave(misc);
728 else if (token == "noun")
730 else if (token == "number")
731 bits_.setNumber(misc);
732 else if (token == "nospellcheck")
733 bits_.setNoSpellcheck(misc);
735 } else if (token == "color") {
736 int const next = lex.getInteger();
737 bits_.setColor(ColorCode(next));
740 } else if (token == "background") {
741 int const next = lex.getInteger();
742 bits_.setBackground(ColorCode(next));
745 } else if (token == "language") {
746 string const next = lex.getString();
747 setLanguage(languages.getLanguage(next));
749 } else if (token == "toggleall") {
750 toggle = lex.getBool();
753 // Unrecognised token
763 void Font::validate(LaTeXFeatures & features) const
765 BufferParams const & bparams = features.bufferParams();
766 Language const * doc_language = bparams.language;
768 if (bits_.noun() == FONT_ON) {
769 LYXERR(Debug::LATEX, "font.noun: " << bits_.noun());
770 features.require("noun");
771 LYXERR(Debug::LATEX, "Noun enabled. Font: " << to_utf8(stateText()));
773 if (bits_.underbar() == FONT_ON) {
774 LYXERR(Debug::LATEX, "font.underline: " << bits_.underbar());
775 features.require("ulem");
776 LYXERR(Debug::LATEX, "Underline enabled. Font: " << to_utf8(stateText()));
778 if (bits_.strikeout() == FONT_ON) {
779 LYXERR(Debug::LATEX, "font.strikeout: " << bits_.strikeout());
780 features.require("ulem");
781 LYXERR(Debug::LATEX, "Strike out enabled. Font: " << to_utf8(stateText()));
783 if (bits_.xout() == FONT_ON) {
784 LYXERR(Debug::LATEX, "font.xout: " << bits_.xout());
785 features.require("ulem");
786 LYXERR(Debug::LATEX, "Cross out enabled. Font: " << to_utf8(stateText()));
788 if (bits_.uuline() == FONT_ON) {
789 LYXERR(Debug::LATEX, "font.uuline: " << bits_.uuline());
790 features.require("ulem");
791 LYXERR(Debug::LATEX, "Double underline enabled. Font: " << to_utf8(stateText()));
793 if (bits_.uwave() == FONT_ON) {
794 LYXERR(Debug::LATEX, "font.uwave: " << bits_.uwave());
795 features.require("ulem");
796 LYXERR(Debug::LATEX, "Wavy underline enabled. Font: " << to_utf8(stateText()));
798 switch (bits_.color()) {
802 // probably we should put here all interface colors used for
803 // font displaying! For now I just add this ones I know of (Jug)
805 case Color_notelabel:
810 case Color_lightgray:
818 features.require("xcolor");
821 features.require("color");
822 LYXERR(Debug::LATEX, "Color enabled. Font: " << to_utf8(stateText()));
825 // FIXME: Do something for background and soul package?
827 if (((features.usePolyglossia() && lang_->polyglossia() != doc_language->polyglossia())
828 || (features.useBabel() && lang_->babel() != doc_language->babel())
829 || (doc_language->encoding()->package() == Encoding::CJK && lang_ != doc_language))
830 && lang_ != ignore_language
831 && lang_ != latex_language)
833 features.useLanguage(lang_);
834 LYXERR(Debug::LATEX, "Found language " << lang_->lang());
839 ostream & operator<<(ostream & os, FontState fms)
841 return os << int(fms);
845 ostream & operator<<(ostream & os, FontInfo const & f)
848 << " family " << f.family()
849 << " series " << f.series()
850 << " shape " << f.shape()
851 << " size " << f.size()
852 << " style " << f.style()
853 << " color " << f.color()
854 // FIXME: uncomment this when we support background.
855 //<< " background " << f.background()
856 << " emph " << f.emph()
857 << " underbar " << f.underbar()
858 << " strikeout " << f.strikeout()
859 << " xout " << f.xout()
860 << " uuline " << f.uuline()
861 << " uwave " << f.uwave()
862 << " noun " << f.noun()
863 << " number " << f.number()
864 << " nospellcheck " << f.nospellcheck();
868 ostream & operator<<(ostream & os, Font const & font)
870 return os << font.bits_
871 << " lang: " << (font.lang_ ? font.lang_->lang() : nullptr);