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 * default_lang,
120 bits_.update(newfont.fontInfo(), toggleall);
122 if (newfont.language() == language() && toggleall)
123 if (language() == default_lang)
124 setLanguage(default_language);
126 setLanguage(default_lang);
127 else if (newfont.language() == reset_language)
128 setLanguage(default_lang);
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 non_inherit_inset,
232 bool 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 os << from_ascii(tmp);
314 count += tmp.length();
315 if (!lyxrc.language_command_end.empty())
316 pushLanguageName(language()->babel(), true);
317 } else if (language()->encoding()->package() != Encoding::CJK) {
323 if (language()->encoding()->package() == Encoding::CJK) {
324 pair<bool, int> const c = switchEncoding(os.os(), bparams,
325 runparams, *(language()->encoding()));
327 open_encoding_ = true;
329 runparams.encoding = language()->encoding();
334 f.reduce(base.bits_);
336 p.reduce(prev.bits_);
338 if (f.size() != INHERIT_SIZE) {
339 if (!runparams.find_effective()) {
343 << LaTeXSizeSwitchNames[f.size()] << termcmd;
344 count += strlen(LaTeXSizeSwitchNames[f.size()]) + 1;
348 << LaTeXSizeSwitchNames[f.size()] << '{';
349 count += strlen(LaTeXSizeSwitchNames[f.size()]) + 2;
352 if (f.family() != INHERIT_FAMILY) {
353 if (non_inherit_inset) {
356 os << '\\' << LaTeXFamilySwitchNames[f.family()] << termcmd;
357 count += strlen(LaTeXFamilySwitchNames[f.family()]) + 1;
359 if (needs_cprotection) {
364 << LaTeXFamilyCommandNames[f.family()]
366 count += strlen(LaTeXFamilyCommandNames[f.family()]) + 2;
369 if (f.series() != INHERIT_SERIES) {
370 if (non_inherit_inset) {
373 os << '\\' << LaTeXSeriesSwitchNames[f.series()] << termcmd;
374 count += strlen(LaTeXSeriesSwitchNames[f.series()]) + 1;
376 if (needs_cprotection) {
381 << LaTeXSeriesCommandNames[f.series()]
383 count += strlen(LaTeXSeriesCommandNames[f.series()]) + 2;
386 if (f.shape() != INHERIT_SHAPE) {
387 if (non_inherit_inset) {
390 os << '\\' << LaTeXShapeSwitchNames[f.shape()] << termcmd;
391 count += strlen(LaTeXShapeSwitchNames[f.shape()]) + 1;
393 if (needs_cprotection) {
398 << LaTeXShapeCommandNames[f.shape()]
400 count += strlen(LaTeXShapeCommandNames[f.shape()]) + 2;
403 if (f.color() != Color_inherit && f.color() != Color_ignore) {
404 if (f.color() == Color_none && p.color() != Color_none) {
405 // Color none: Close previous color, if any
408 } else if (f.color() != Color_none) {
410 << from_ascii(lcolor.getLaTeXName(f.color()))
412 count += lcolor.getLaTeXName(f.color()).length() + 13;
415 // FIXME: uncomment this when we support background.
417 if (f.background() != Color_inherit && f.background() != Color_ignore) {
419 << from_ascii(lcolor.getLaTeXName(f.background()))
421 count += lcolor.getLaTeXName(f.background()).length() + 13;
422 env = true; //We have opened a new environment
425 // If the current language is Hebrew, Arabic, or Farsi
426 // the numbers are written Left-to-Right. ArabTeX package
427 // and bidi (polyglossia with XeTeX) reorder the number automatically
428 // but the packages used for Hebrew and Farsi (Arabi) do not.
429 if (!runparams.useBidiPackage()
430 && !runparams.pass_thru
431 && bits_.number() == FONT_ON
432 && prev.fontInfo().number() != FONT_ON
433 && (language()->lang() == "hebrew"
434 || language()->lang() == "farsi"
435 || language()->lang() == "arabic_arabi")) {
436 if (runparams.use_polyglossia) {
445 if (f.emph() == FONT_ON) {
446 if (needs_cprotection) {
453 // \noun{} is a LyX special macro
454 if (f.noun() == FONT_ON) {
455 if (needs_cprotection) {
462 // The ulem commands need to be on the deepest nesting level
463 // because ulem puts every nested group or macro in a box,
464 // which prevents linebreaks (#8424, #8733)
465 if (f.underbar() == FONT_ON) {
466 if (needs_cprotection) {
472 ++runparams.inulemcmd;
474 if (f.uuline() == FONT_ON) {
475 if (needs_cprotection) {
481 ++runparams.inulemcmd;
483 if (f.strikeout() == FONT_ON) {
484 if (needs_cprotection) {
490 ++runparams.inulemcmd;
492 if (f.xout() == FONT_ON) {
493 if (needs_cprotection) {
499 ++runparams.inulemcmd;
501 if (f.uwave() == FONT_ON) {
502 if (runparams.inulemcmd) {
503 // needed with nested uwave in xout
504 // see https://tex.stackexchange.com/a/263042
505 os << "\\ULdepth=1000pt";
508 if (needs_cprotection) {
514 ++runparams.inulemcmd;
520 /// Writes ending block of LaTeX needed to close use of this font
521 // Returns number of chars written
522 // This one corresponds to latexWriteStartChanges(). (Asger)
523 int Font::latexWriteEndChanges(otexstream & os, BufferParams const & bparams,
524 OutputParams const & runparams,
528 bool closeLanguage) const
532 // reduce the current font to changes against the base
533 // font (of the layout). We use a temporary for this to
534 // avoid changing this font instance, as that would break
536 f.reduce(base.bits_);
538 if (f.family() != INHERIT_FAMILY) {
542 if (f.series() != INHERIT_SERIES) {
546 if (f.shape() != INHERIT_SHAPE) {
550 if (f.color() != Color_inherit && f.color() != Color_ignore && f.color() != Color_none) {
554 if (f.emph() == FONT_ON) {
558 if (f.noun() == FONT_ON) {
562 if (f.size() != INHERIT_SIZE) {
563 // We do not close size group in front of
564 // insets with InheritFont() false (as opposed
565 // to all other font properties) (#8384)
566 if (needPar && !closeLanguage) {
574 if (f.underbar() == FONT_ON) {
577 --runparams.inulemcmd;
579 if (f.strikeout() == FONT_ON) {
582 --runparams.inulemcmd;
584 if (f.xout() == FONT_ON) {
587 --runparams.inulemcmd;
589 if (f.uuline() == FONT_ON) {
592 --runparams.inulemcmd;
594 if (f.uwave() == FONT_ON) {
597 --runparams.inulemcmd;
600 // If the current language is Hebrew, Arabic, or Farsi
601 // the numbers are written Left-to-Right. ArabTeX package
602 // and bidi (polyglossia with XeTeX) reorder the number automatically
603 // but the packages used for Hebrew and Farsi (Arabi) do not.
604 if (!runparams.useBidiPackage()
605 && !runparams.pass_thru
606 && bits_.number() == FONT_ON
607 && next.fontInfo().number() != FONT_ON
608 && (language()->lang() == "hebrew"
609 || language()->lang() == "farsi"
610 || language()->lang() == "arabic_arabi")) {
611 if (runparams.use_polyglossia) {
621 if (open_encoding_) {
622 // We need to close the encoding even if it does not change
623 // to do correct environment nesting
624 Encoding const * const ascii = encodings.fromLyXName("ascii");
625 pair<bool, int> const c = switchEncoding(os.os(), bparams,
629 runparams.encoding = ascii;
630 open_encoding_ = false;
634 && language() != base.language() && language() != next.language()
635 && (language()->encoding()->package() != Encoding::CJK)) {
638 bool const using_begin_end =
639 runparams.use_polyglossia ||
640 !lyxrc.language_command_end.empty();
641 if (using_begin_end && !languageStackEmpty())
649 string Font::toString(bool const toggle) const
651 string const lang = (language() == reset_language)
652 ? "reset" : language()->lang();
655 os << "family " << bits_.family() << '\n'
656 << "series " << bits_.series() << '\n'
657 << "shape " << bits_.shape() << '\n'
658 << "size " << bits_.size() << '\n'
659 << "emph " << bits_.emph() << '\n'
660 << "underbar " << bits_.underbar() << '\n'
661 << "strikeout " << bits_.strikeout() << '\n'
662 << "xout " << bits_.xout() << '\n'
663 << "uuline " << bits_.uuline() << '\n'
664 << "uwave " << bits_.uwave() << '\n'
665 << "noun " << bits_.noun() << '\n'
666 << "number " << bits_.number() << '\n'
667 << "nospellcheck " << bits_.nospellcheck() << '\n'
668 << "color " << bits_.color() << '\n'
669 << "language " << lang << '\n'
670 << "toggleall " << convert<string>(toggle);
675 bool Font::fromString(string const & data, bool & toggle)
677 istringstream is(data);
685 token = lex.getString();
687 if (token.empty() || !lex.next())
690 if (token == "family") {
691 int const next = lex.getInteger();
692 bits_.setFamily(FontFamily(next));
694 } else if (token == "series") {
695 int const next = lex.getInteger();
696 bits_.setSeries(FontSeries(next));
698 } else if (token == "shape") {
699 int const next = lex.getInteger();
700 bits_.setShape(FontShape(next));
702 } else if (token == "size") {
703 int const next = lex.getInteger();
704 bits_.setSize(FontSize(next));
705 // FIXME: shall style be handled there? Probably not.
706 } else if (token == "emph" || token == "underbar"
707 || token == "noun" || token == "number"
708 || token == "uuline" || token == "uwave"
709 || token == "strikeout" || token == "xout"
710 || token == "nospellcheck") {
712 int const next = lex.getInteger();
713 FontState const misc = FontState(next);
717 else if (token == "underbar")
718 bits_.setUnderbar(misc);
719 else if (token == "strikeout")
720 bits_.setStrikeout(misc);
721 else if (token == "xout")
723 else if (token == "uuline")
724 bits_.setUuline(misc);
725 else if (token == "uwave")
726 bits_.setUwave(misc);
727 else if (token == "noun")
729 else if (token == "number")
730 bits_.setNumber(misc);
731 else if (token == "nospellcheck")
732 bits_.setNoSpellcheck(misc);
734 } else if (token == "color") {
735 int const next = lex.getInteger();
736 bits_.setColor(ColorCode(next));
739 } else if (token == "background") {
740 int const next = lex.getInteger();
741 bits_.setBackground(ColorCode(next));
744 } else if (token == "language") {
745 string const next = lex.getString();
746 setLanguage(languages.getLanguage(next));
748 } else if (token == "toggleall") {
749 toggle = lex.getBool();
752 // Unrecognised token
762 void Font::validate(LaTeXFeatures & features) const
764 BufferParams const & bparams = features.bufferParams();
765 Language const * doc_language = bparams.language;
767 if (bits_.noun() == FONT_ON) {
768 LYXERR(Debug::OUTFILE, "font.noun: " << bits_.noun());
769 features.require("noun");
770 LYXERR(Debug::OUTFILE, "Noun enabled. Font: " << to_utf8(stateText()));
772 if (bits_.underbar() == FONT_ON) {
773 LYXERR(Debug::OUTFILE, "font.underline: " << bits_.underbar());
774 features.require("ulem");
775 LYXERR(Debug::OUTFILE, "Underline enabled. Font: " << to_utf8(stateText()));
777 if (bits_.strikeout() == FONT_ON) {
778 LYXERR(Debug::OUTFILE, "font.strikeout: " << bits_.strikeout());
779 features.require("ulem");
780 LYXERR(Debug::OUTFILE, "Strike out enabled. Font: " << to_utf8(stateText()));
782 if (bits_.xout() == FONT_ON) {
783 LYXERR(Debug::OUTFILE, "font.xout: " << bits_.xout());
784 features.require("ulem");
785 LYXERR(Debug::OUTFILE, "Cross out enabled. Font: " << to_utf8(stateText()));
787 if (bits_.uuline() == FONT_ON) {
788 LYXERR(Debug::OUTFILE, "font.uuline: " << bits_.uuline());
789 features.require("ulem");
790 LYXERR(Debug::OUTFILE, "Double underline enabled. Font: " << to_utf8(stateText()));
792 if (bits_.uwave() == FONT_ON) {
793 LYXERR(Debug::OUTFILE, "font.uwave: " << bits_.uwave());
794 features.require("ulem");
795 LYXERR(Debug::OUTFILE, "Wavy underline enabled. Font: " << to_utf8(stateText()));
797 switch (bits_.color()) {
801 // probably we should put here all interface colors used for
802 // font displaying! For now I just add this ones I know of (Jug)
804 case Color_notelabel:
809 case Color_lightgray:
817 features.require("xcolor");
820 features.require("color");
821 LYXERR(Debug::OUTFILE, "Color enabled. Font: " << to_utf8(stateText()));
824 // FIXME: Do something for background and soul package?
826 if (((features.usePolyglossia() && lang_->polyglossia() != doc_language->polyglossia())
827 || (features.useBabel() && lang_->babel() != doc_language->babel())
828 || (doc_language->encoding()->package() == Encoding::CJK && lang_ != doc_language))
829 && lang_ != ignore_language
830 && lang_ != latex_language)
832 features.useLanguage(lang_);
833 LYXERR(Debug::OUTFILE, "Found language " << lang_->lang());
838 ostream & operator<<(ostream & os, FontState fms)
840 return os << int(fms);
844 ostream & operator<<(ostream & os, FontInfo const & f)
847 << " family " << f.family()
848 << " series " << f.series()
849 << " shape " << f.shape()
850 << " size " << f.size()
851 << " style " << f.style()
852 << " color " << f.color()
853 // FIXME: uncomment this when we support background.
854 //<< " background " << f.background()
855 << " emph " << f.emph()
856 << " underbar " << f.underbar()
857 << " strikeout " << f.strikeout()
858 << " xout " << f.xout()
859 << " uuline " << f.uuline()
860 << " uwave " << f.uwave()
861 << " noun " << f.noun()
862 << " number " << f.number()
863 << " nospellcheck " << f.nospellcheck();
867 ostream & operator<<(ostream & os, Font const & font)
869 return os << font.bits_
870 << " lang: " << (font.lang_ ? font.lang_->lang() : "");