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) const
235 // polyglossia or babel?
236 if (runparams.use_polyglossia
237 && language()->lang() != base.language()->lang()
238 && language() != prev.language()) {
239 if (!language()->polyglossia().empty()) {
240 string tmp = "\\text" + language()->polyglossia();
241 if (!language()->polyglossiaOpts().empty()) {
242 tmp += "[" + language()->polyglossiaOpts() + "]";
243 if (runparams.use_hyperref && runparams.moving_arg)
244 // We need to strip the command for
245 // the pdf string, see #11813
246 tmp = "\\texorpdfstring{" + tmp + "}{}";
249 os << from_ascii(tmp);
250 count += tmp.length();
251 pushLanguageName(language()->polyglossia(), true);
252 } else if (language()->encoding()->package() != Encoding::CJK) {
256 } else if (language()->babel() != base.language()->babel() &&
257 language() != prev.language()) {
258 if (language()->lang() == "farsi") {
261 } else if (!isRightToLeft() &&
262 base.language()->lang() == "farsi") {
265 } else if (language()->lang() == "arabic_arabi") {
268 } else if (!isRightToLeft() &&
269 base.language()->lang() == "arabic_arabi") {
272 // currently the remaining RTL languages are arabic_arabtex and hebrew
273 } else if (isRightToLeft() != prev.isRightToLeft()) {
274 if (isRightToLeft()) {
281 } else if (!language()->babel().empty()) {
283 subst(lyxrc.language_command_local,
284 "$$lang", language()->babel());
285 os << from_ascii(tmp);
286 count += tmp.length();
287 if (!lyxrc.language_command_end.empty())
288 pushLanguageName(language()->babel(), true);
289 } else if (language()->encoding()->package() != Encoding::CJK) {
295 if (language()->encoding()->package() == Encoding::CJK) {
296 pair<bool, int> const c = switchEncoding(os.os(), bparams,
297 runparams, *(language()->encoding()));
299 open_encoding_ = true;
301 runparams.encoding = language()->encoding();
306 f.reduce(base.bits_);
308 p.reduce(prev.bits_);
310 if (f.size() != INHERIT_SIZE) {
314 << LaTeXSizeSwitchNames[f.size()] << termcmd;
315 count += strlen(LaTeXSizeSwitchNames[f.size()]) + 1;
317 if (f.family() != INHERIT_FAMILY) {
318 if (non_inherit_inset) {
321 os << '\\' << LaTeXFamilySwitchNames[f.family()] << termcmd;
322 count += strlen(LaTeXFamilySwitchNames[f.family()]) + 1;
325 << LaTeXFamilyCommandNames[f.family()]
327 count += strlen(LaTeXFamilyCommandNames[f.family()]) + 2;
330 if (f.series() != INHERIT_SERIES) {
331 if (non_inherit_inset) {
334 os << '\\' << LaTeXSeriesSwitchNames[f.series()] << termcmd;
335 count += strlen(LaTeXSeriesSwitchNames[f.series()]) + 1;
338 << LaTeXSeriesCommandNames[f.series()]
340 count += strlen(LaTeXSeriesCommandNames[f.series()]) + 2;
343 if (f.shape() != INHERIT_SHAPE) {
344 if (non_inherit_inset) {
347 os << '\\' << LaTeXShapeSwitchNames[f.shape()] << termcmd;
348 count += strlen(LaTeXShapeSwitchNames[f.shape()]) + 1;
351 << LaTeXShapeCommandNames[f.shape()]
353 count += strlen(LaTeXShapeCommandNames[f.shape()]) + 2;
356 if (f.color() != Color_inherit && f.color() != Color_ignore) {
357 if (f.color() == Color_none && p.color() != Color_none) {
358 // Color none: Close previous color, if any
361 } else if (f.color() != Color_none) {
363 << from_ascii(lcolor.getLaTeXName(f.color()))
365 count += lcolor.getLaTeXName(f.color()).length() + 13;
368 // FIXME: uncomment this when we support background.
370 if (f.background() != Color_inherit && f.background() != Color_ignore) {
372 << from_ascii(lcolor.getLaTeXName(f.background()))
374 count += lcolor.getLaTeXName(f.background()).length() + 13;
375 env = true; //We have opened a new environment
378 // If the current language is Hebrew, Arabic, or Farsi
379 // the numbers are written Left-to-Right. ArabTeX package
380 // and bidi (polyglossia with XeTeX) reorder the number automatically
381 // but the packages used for Hebrew and Farsi (Arabi) do not.
382 if (!runparams.useBidiPackage()
383 && !runparams.pass_thru
384 && bits_.number() == FONT_ON
385 && prev.fontInfo().number() != FONT_ON
386 && (language()->lang() == "hebrew"
387 || language()->lang() == "farsi"
388 || language()->lang() == "arabic_arabi")) {
389 if (runparams.use_polyglossia) {
398 if (f.emph() == FONT_ON) {
402 // \noun{} is a LyX special macro
403 if (f.noun() == FONT_ON) {
407 // The ulem commands need to be on the deepest nesting level
408 // because ulem puts every nested group or macro in a box,
409 // which prevents linebreaks (#8424, #8733)
410 if (f.underbar() == FONT_ON) {
413 ++runparams.inulemcmd;
415 if (f.uuline() == FONT_ON) {
418 ++runparams.inulemcmd;
420 if (f.strikeout() == FONT_ON) {
423 ++runparams.inulemcmd;
425 if (f.xout() == FONT_ON) {
428 ++runparams.inulemcmd;
430 if (f.uwave() == FONT_ON) {
431 if (runparams.inulemcmd) {
432 // needed with nested uwave in xout
433 // see https://tex.stackexchange.com/a/263042
434 os << "\\ULdepth=1000pt";
439 ++runparams.inulemcmd;
445 /// Writes ending block of LaTeX needed to close use of this font
446 // Returns number of chars written
447 // This one corresponds to latexWriteStartChanges(). (Asger)
448 int Font::latexWriteEndChanges(otexstream & os, BufferParams const & bparams,
449 OutputParams const & runparams,
453 bool const & closeLanguage,
454 bool const & non_inherit_inset) const
458 // reduce the current font to changes against the base
459 // font (of the layout). We use a temporary for this to
460 // avoid changing this font instance, as that would break
462 f.reduce(base.bits_);
464 if (f.family() != INHERIT_FAMILY && !non_inherit_inset) {
468 if (f.series() != INHERIT_SERIES && !non_inherit_inset) {
472 if (f.shape() != INHERIT_SHAPE && !non_inherit_inset) {
476 if (f.color() != Color_inherit && f.color() != Color_ignore && f.color() != Color_none) {
480 if (f.emph() == FONT_ON) {
484 if (f.noun() == FONT_ON) {
488 if (f.size() != INHERIT_SIZE) {
489 // We do not close size group in front of
490 // insets with InheritFont() false (as opposed
491 // to all other font properties) (#8384)
492 if (!non_inherit_inset) {
493 if (needPar && !closeLanguage) {
502 if (f.underbar() == FONT_ON) {
505 --runparams.inulemcmd;
507 if (f.strikeout() == FONT_ON) {
510 --runparams.inulemcmd;
512 if (f.xout() == FONT_ON) {
515 --runparams.inulemcmd;
517 if (f.uuline() == FONT_ON) {
520 --runparams.inulemcmd;
522 if (f.uwave() == FONT_ON) {
525 --runparams.inulemcmd;
528 // If the current language is Hebrew, Arabic, or Farsi
529 // the numbers are written Left-to-Right. ArabTeX package
530 // and bidi (polyglossia with XeTeX) reorder the number automatically
531 // but the packages used for Hebrew and Farsi (Arabi) do not.
532 if (!runparams.useBidiPackage()
533 && !runparams.pass_thru
534 && bits_.number() == FONT_ON
535 && next.fontInfo().number() != FONT_ON
536 && (language()->lang() == "hebrew"
537 || language()->lang() == "farsi"
538 || language()->lang() == "arabic_arabi")) {
539 if (runparams.use_polyglossia) {
549 if (open_encoding_) {
550 // We need to close the encoding even if it does not change
551 // to do correct environment nesting
552 Encoding const * const ascii = encodings.fromLyXName("ascii");
553 pair<bool, int> const c = switchEncoding(os.os(), bparams,
557 runparams.encoding = ascii;
558 open_encoding_ = false;
562 && language() != base.language() && language() != next.language()
563 && (language()->encoding()->package() != Encoding::CJK)) {
566 bool const using_begin_end =
567 runparams.use_polyglossia ||
568 !lyxrc.language_command_end.empty();
577 string Font::toString(bool const toggle) const
579 string const lang = (language() == reset_language)
580 ? "reset" : language()->lang();
583 os << "family " << bits_.family() << '\n'
584 << "series " << bits_.series() << '\n'
585 << "shape " << bits_.shape() << '\n'
586 << "size " << bits_.size() << '\n'
587 << "emph " << bits_.emph() << '\n'
588 << "underbar " << bits_.underbar() << '\n'
589 << "strikeout " << bits_.strikeout() << '\n'
590 << "xout " << bits_.xout() << '\n'
591 << "uuline " << bits_.uuline() << '\n'
592 << "uwave " << bits_.uwave() << '\n'
593 << "noun " << bits_.noun() << '\n'
594 << "number " << bits_.number() << '\n'
595 << "nospellcheck " << bits_.nospellcheck() << '\n'
596 << "color " << bits_.color() << '\n'
597 << "language " << lang << '\n'
598 << "toggleall " << convert<string>(toggle);
603 bool Font::fromString(string const & data, bool & toggle)
605 istringstream is(data);
613 token = lex.getString();
615 if (token.empty() || !lex.next())
618 if (token == "family") {
619 int const next = lex.getInteger();
620 bits_.setFamily(FontFamily(next));
622 } else if (token == "series") {
623 int const next = lex.getInteger();
624 bits_.setSeries(FontSeries(next));
626 } else if (token == "shape") {
627 int const next = lex.getInteger();
628 bits_.setShape(FontShape(next));
630 } else if (token == "size") {
631 int const next = lex.getInteger();
632 bits_.setSize(FontSize(next));
633 // FIXME: shall style be handled there? Probably not.
634 } else if (token == "emph" || token == "underbar"
635 || token == "noun" || token == "number"
636 || token == "uuline" || token == "uwave"
637 || token == "strikeout" || token == "xout"
638 || token == "nospellcheck") {
640 int const next = lex.getInteger();
641 FontState const misc = FontState(next);
645 else if (token == "underbar")
646 bits_.setUnderbar(misc);
647 else if (token == "strikeout")
648 bits_.setStrikeout(misc);
649 else if (token == "xout")
651 else if (token == "uuline")
652 bits_.setUuline(misc);
653 else if (token == "uwave")
654 bits_.setUwave(misc);
655 else if (token == "noun")
657 else if (token == "number")
658 bits_.setNumber(misc);
659 else if (token == "nospellcheck")
660 bits_.setNoSpellcheck(misc);
662 } else if (token == "color") {
663 int const next = lex.getInteger();
664 bits_.setColor(ColorCode(next));
667 } else if (token == "background") {
668 int const next = lex.getInteger();
669 bits_.setBackground(ColorCode(next));
672 } else if (token == "language") {
673 string const next = lex.getString();
674 setLanguage(languages.getLanguage(next));
676 } else if (token == "toggleall") {
677 toggle = lex.getBool();
680 // Unrecognised token
690 void Font::validate(LaTeXFeatures & features) const
692 BufferParams const & bparams = features.bufferParams();
693 Language const * doc_language = bparams.language;
695 if (bits_.noun() == FONT_ON) {
696 LYXERR(Debug::LATEX, "font.noun: " << bits_.noun());
697 features.require("noun");
698 LYXERR(Debug::LATEX, "Noun enabled. Font: " << to_utf8(stateText()));
700 if (bits_.underbar() == FONT_ON) {
701 LYXERR(Debug::LATEX, "font.underline: " << bits_.underbar());
702 features.require("ulem");
703 LYXERR(Debug::LATEX, "Underline enabled. Font: " << to_utf8(stateText()));
705 if (bits_.strikeout() == FONT_ON) {
706 LYXERR(Debug::LATEX, "font.strikeout: " << bits_.strikeout());
707 features.require("ulem");
708 LYXERR(Debug::LATEX, "Strike out enabled. Font: " << to_utf8(stateText()));
710 if (bits_.xout() == FONT_ON) {
711 LYXERR(Debug::LATEX, "font.xout: " << bits_.xout());
712 features.require("ulem");
713 LYXERR(Debug::LATEX, "Cross out enabled. Font: " << to_utf8(stateText()));
715 if (bits_.uuline() == FONT_ON) {
716 LYXERR(Debug::LATEX, "font.uuline: " << bits_.uuline());
717 features.require("ulem");
718 LYXERR(Debug::LATEX, "Double underline enabled. Font: " << to_utf8(stateText()));
720 if (bits_.uwave() == FONT_ON) {
721 LYXERR(Debug::LATEX, "font.uwave: " << bits_.uwave());
722 features.require("ulem");
723 LYXERR(Debug::LATEX, "Wavy underline enabled. Font: " << to_utf8(stateText()));
725 switch (bits_.color()) {
729 // probably we should put here all interface colors used for
730 // font displaying! For now I just add this ones I know of (Jug)
732 case Color_notelabel:
737 case Color_lightgray:
745 features.require("xcolor");
748 features.require("color");
749 LYXERR(Debug::LATEX, "Color enabled. Font: " << to_utf8(stateText()));
752 // FIXME: Do something for background and soul package?
754 if (((features.usePolyglossia() && lang_->polyglossia() != doc_language->polyglossia())
755 || (features.useBabel() && lang_->babel() != doc_language->babel())
756 || (doc_language->encoding()->package() == Encoding::CJK && lang_ != doc_language))
757 && lang_ != ignore_language
758 && lang_ != latex_language)
760 features.useLanguage(lang_);
761 LYXERR(Debug::LATEX, "Found language " << lang_->lang());
766 ostream & operator<<(ostream & os, FontState fms)
768 return os << int(fms);
772 ostream & operator<<(ostream & os, FontInfo const & f)
775 << " family " << f.family()
776 << " series " << f.series()
777 << " shape " << f.shape()
778 << " size " << f.size()
779 << " style " << f.style()
780 << " color " << f.color()
781 // FIXME: uncomment this when we support background.
782 //<< " background " << f.background()
783 << " emph " << f.emph()
784 << " underbar " << f.underbar()
785 << " strikeout " << f.strikeout()
786 << " xout " << f.xout()
787 << " uuline " << f.uuline()
788 << " uwave " << f.uwave()
789 << " noun " << f.noun()
790 << " number " << f.number()
791 << " nospellcheck " << f.nospellcheck();
795 ostream & operator<<(ostream & os, Font const & font)
797 return os << font.bits_
798 << " lang: " << (font.lang_ ? font.lang_->lang() : nullptr);