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 * LaTeXFamilyNames[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 * LaTeXSeriesNames[NUM_SERIES + 2] =
65 { "textmd", "textbf", "error4", "error5" };
67 char const * LaTeXShapeNames[NUM_SHAPE + 2] =
68 { "textup", "textit", "textsl", "textsc", "error6", "error7" };
70 char const * LaTeXSizeNames[NUM_SIZE + 4] =
71 { "tiny", "scriptsize", "footnotesize", "small", "normalsize", "large",
72 "Large", "LARGE", "huge", "Huge", "error8", "error9", "error10", "error11" };
77 Font::Font(FontInfo bits, Language const * l)
78 : bits_(bits), lang_(l), open_encoding_(false)
81 lang_ = default_language;
85 bool Font::isRightToLeft() const
87 return lang_->rightToLeft();
91 bool Font::isVisibleRightToLeft() const
93 return (lang_->rightToLeft() &&
94 bits_.number() != FONT_ON);
98 void Font::setLanguage(Language const * l)
104 /// Updates font settings according to request
105 void Font::update(Font const & newfont,
106 Language const * document_language,
109 bits_.update(newfont.fontInfo(), toggleall);
111 if (newfont.language() == language() && toggleall)
112 if (language() == document_language)
113 setLanguage(default_language);
115 setLanguage(document_language);
116 else if (newfont.language() == reset_language)
117 setLanguage(document_language);
118 else if (newfont.language() != ignore_language)
119 setLanguage(newfont.language());
123 docstring const Font::stateText(BufferParams * params, bool const terse) const
126 os << bits_.stateText(terse);
127 if ((!params || (language() != params->language))
128 && (!terse || language() != ignore_language)) {
129 // reset_language is a null pointer!
130 os << bformat(_("Language: %1$s, "),
131 (language() == reset_language) ? _("Default")
132 : _(language()->display()));
134 if (bits_.number() != FONT_OFF)
135 os << " " << bformat(_("Number %1$s"),
136 _(GUIMiscNames[bits_.number()]));
137 return rtrim(os.str(), ", ");
141 // Returns size in latex format
142 string const Font::latexSize() const
144 return LaTeXSizeNames[bits_.size()];
148 /// Writes the changes from this font to orgfont in .lyx format in file
149 void Font::lyxWriteChanges(Font const & orgfont,
153 if (orgfont.fontInfo().family() != bits_.family())
154 os << "\\family " << LyXFamilyNames[bits_.family()] << "\n";
155 if (orgfont.fontInfo().series() != bits_.series())
156 os << "\\series " << LyXSeriesNames[bits_.series()] << "\n";
157 if (orgfont.fontInfo().shape() != bits_.shape())
158 os << "\\shape " << LyXShapeNames[bits_.shape()] << "\n";
159 if (orgfont.fontInfo().size() != bits_.size())
160 os << "\\size " << LyXSizeNames[bits_.size()] << "\n";
161 // FIXME: shall style be handled there? Probably not.
162 if (orgfont.fontInfo().emph() != bits_.emph())
163 os << "\\emph " << LyXMiscNames[bits_.emph()] << "\n";
164 if (orgfont.fontInfo().number() != bits_.number())
165 os << "\\numeric " << LyXMiscNames[bits_.number()] << "\n";
166 if (orgfont.fontInfo().nospellcheck() != bits_.nospellcheck())
167 os << "\\nospellcheck " << LyXMiscNames[bits_.nospellcheck()] << "\n";
168 if (orgfont.fontInfo().underbar() != bits_.underbar()) {
169 // This is only for backwards compatibility
170 switch (bits_.underbar()) {
171 case FONT_OFF: os << "\\bar no\n"; break;
172 case FONT_ON: os << "\\bar under\n"; break;
173 case FONT_TOGGLE: lyxerr << "Font::lyxWriteFontChanges: "
174 "FONT_TOGGLE should not appear here!"
177 case FONT_INHERIT: os << "\\bar default\n"; break;
178 case FONT_IGNORE: lyxerr << "Font::lyxWriteFontChanges: "
179 "IGNORE should not appear here!"
184 if (orgfont.fontInfo().strikeout() != bits_.strikeout()) {
185 os << "\\strikeout " << LyXMiscNames[bits_.strikeout()] << "\n";
187 if (orgfont.fontInfo().xout() != bits_.xout()) {
188 os << "\\xout " << LyXMiscNames[bits_.xout()] << "\n";
190 if (orgfont.fontInfo().uuline() != bits_.uuline()) {
191 os << "\\uuline " << LyXMiscNames[bits_.uuline()] << "\n";
193 if (orgfont.fontInfo().uwave() != bits_.uwave()) {
194 os << "\\uwave " << LyXMiscNames[bits_.uwave()] << "\n";
196 if (orgfont.fontInfo().noun() != bits_.noun()) {
197 os << "\\noun " << LyXMiscNames[bits_.noun()] << "\n";
199 if (orgfont.fontInfo().color() != bits_.color())
200 os << "\\color " << lcolor.getLyXName(bits_.color()) << '\n';
201 // FIXME: uncomment this when we support background.
202 //if (orgfont.fontInfo().background() != bits_.background())
203 // os << "\\color " << lcolor.getLyXName(bits_.background()) << '\n';
204 if (orgfont.language() != language() &&
205 language() != latex_language) {
207 os << "\\lang " << language()->lang() << "\n";
209 os << "\\lang unknown\n";
214 /// Writes the head of the LaTeX needed to impose this font
215 // Returns number of chars written.
216 int Font::latexWriteStartChanges(odocstream & os, BufferParams const & bparams,
217 OutputParams const & runparams,
219 Font const & prev) const
225 // polyglossia or babel?
226 if (runparams.use_polyglossia
227 && language()->lang() != base.language()->lang()
228 && language() != prev.language()) {
229 if (!language()->polyglossia().empty()) {
230 string tmp = "\\text" + language()->polyglossia();
231 if (!language()->polyglossiaOpts().empty()) {
232 tmp += "[" + language()->polyglossiaOpts() + "]";
233 if (runparams.use_hyperref && runparams.moving_arg)
234 // We need to strip the command for
235 // the pdf string, see #11813
236 tmp = "\\texorpdfstring{" + tmp + "}{}";
239 os << from_ascii(tmp);
240 count += tmp.length();
241 pushLanguageName(language()->polyglossia(), true);
242 } else if (language()->encoding()->package() != Encoding::CJK) {
246 } else if (language()->babel() != base.language()->babel() &&
247 language() != prev.language()) {
248 if (language()->lang() == "farsi") {
251 } else if (!isRightToLeft() &&
252 base.language()->lang() == "farsi") {
255 } else if (language()->lang() == "arabic_arabi") {
258 } else if (!isRightToLeft() &&
259 base.language()->lang() == "arabic_arabi") {
262 // currently the remaining RTL languages are arabic_arabtex and hebrew
263 } else if (isRightToLeft() != prev.isRightToLeft()) {
264 if (isRightToLeft()) {
271 } else if (!language()->babel().empty()) {
273 subst(lyxrc.language_command_local,
274 "$$lang", language()->babel());
275 os << from_ascii(tmp);
276 count += tmp.length();
277 if (!lyxrc.language_command_end.empty())
278 pushLanguageName(language()->babel(), true);
279 } else if (language()->encoding()->package() != Encoding::CJK) {
285 if (language()->encoding()->package() == Encoding::CJK) {
286 pair<bool, int> const c = switchEncoding(os, bparams,
287 runparams, *(language()->encoding()));
289 open_encoding_ = true;
291 runparams.encoding = language()->encoding();
296 f.reduce(base.bits_);
298 p.reduce(prev.bits_);
300 if (f.family() != INHERIT_FAMILY) {
302 << LaTeXFamilyNames[f.family()]
304 count += strlen(LaTeXFamilyNames[f.family()]) + 2;
305 env = true; //We have opened a new environment
307 if (f.series() != INHERIT_SERIES) {
309 << LaTeXSeriesNames[f.series()]
311 count += strlen(LaTeXSeriesNames[f.series()]) + 2;
312 env = true; //We have opened a new environment
314 if (f.shape() != INHERIT_SHAPE) {
316 << LaTeXShapeNames[f.shape()]
318 count += strlen(LaTeXShapeNames[f.shape()]) + 2;
319 env = true; //We have opened a new environment
321 if (f.color() != Color_inherit && f.color() != Color_ignore) {
322 if (f.color() == Color_none && p.color() != Color_none) {
323 // Color none: Close previous color, if any
326 } else if (f.color() != Color_none) {
328 << from_ascii(lcolor.getLaTeXName(f.color()))
330 count += lcolor.getLaTeXName(f.color()).length() + 13;
332 env = true; //We have opened a new environment
334 // FIXME: uncomment this when we support background.
336 if (f.background() != Color_inherit && f.background() != Color_ignore) {
338 << from_ascii(lcolor.getLaTeXName(f.background()))
340 count += lcolor.getLaTeXName(f.background()).length() + 13;
341 env = true; //We have opened a new environment
344 // If the current language is Hebrew, Arabic, or Farsi
345 // the numbers are written Left-to-Right. ArabTeX package
346 // and bidi (polyglossia with XeTeX) reorder the number automatically
347 // but the packages used for Hebrew and Farsi (Arabi) do not.
348 if (!runparams.useBidiPackage()
349 && !runparams.pass_thru
350 && bits_.number() == FONT_ON
351 && prev.fontInfo().number() != FONT_ON
352 && (language()->lang() == "hebrew"
353 || language()->lang() == "farsi"
354 || language()->lang() == "arabic_arabi")) {
355 if (runparams.use_polyglossia) {
364 if (f.emph() == FONT_ON) {
367 env = true; //We have opened a new environment
369 // \noun{} is a LyX special macro
370 if (f.noun() == FONT_ON) {
373 env = true; //We have opened a new environment
375 if (f.size() != INHERIT_SIZE) {
376 // If we didn't open an environment above, we open one here
382 << LaTeXSizeNames[f.size()]
384 count += strlen(LaTeXSizeNames[f.size()]) + 3;
386 // The ulem commands need to be on the deepest nesting level
387 // because ulem puts every nested group or macro in a box,
388 // which prevents linebreaks (#8424, #8733)
389 if (f.underbar() == FONT_ON) {
392 ++runparams.inulemcmd;
394 if (f.uuline() == FONT_ON) {
397 ++runparams.inulemcmd;
399 if (f.strikeout() == FONT_ON) {
402 ++runparams.inulemcmd;
404 if (f.xout() == FONT_ON) {
407 ++runparams.inulemcmd;
409 if (f.uwave() == FONT_ON) {
410 if (runparams.inulemcmd) {
411 // needed with nested uwave in xout
412 // see https://tex.stackexchange.com/a/263042
413 os << "\\ULdepth=1000pt";
418 ++runparams.inulemcmd;
424 /// Writes ending block of LaTeX needed to close use of this font
425 // Returns number of chars written
426 // This one corresponds to latexWriteStartChanges(). (Asger)
427 int Font::latexWriteEndChanges(otexstream & os, BufferParams const & bparams,
428 OutputParams const & runparams,
432 bool const & closeLanguage) const
437 // reduce the current font to changes against the base
438 // font (of the layout). We use a temporary for this to
439 // avoid changing this font instance, as that would break
441 f.reduce(base.bits_);
443 if (f.family() != INHERIT_FAMILY) {
446 env = true; // Size change need not bother about closing env.
448 if (f.series() != INHERIT_SERIES) {
451 env = true; // Size change need not bother about closing env.
453 if (f.shape() != INHERIT_SHAPE) {
456 env = true; // Size change need not bother about closing env.
458 if (f.color() != Color_inherit && f.color() != Color_ignore && f.color() != Color_none) {
461 env = true; // Size change need not bother about closing env.
463 if (f.emph() == FONT_ON) {
466 env = true; // Size change need not bother about closing env.
468 if (f.noun() == FONT_ON) {
471 env = true; // Size change need not bother about closing env.
473 if (f.size() != INHERIT_SIZE) {
474 // We only have to close if only size changed
476 if (needPar && !closeLanguage) {
485 if (f.underbar() == FONT_ON) {
488 --runparams.inulemcmd;
490 if (f.strikeout() == FONT_ON) {
493 --runparams.inulemcmd;
495 if (f.xout() == FONT_ON) {
498 --runparams.inulemcmd;
500 if (f.uuline() == FONT_ON) {
503 --runparams.inulemcmd;
505 if (f.uwave() == FONT_ON) {
508 --runparams.inulemcmd;
511 // If the current language is Hebrew, Arabic, or Farsi
512 // the numbers are written Left-to-Right. ArabTeX package
513 // and bidi (polyglossia with XeTeX) reorder the number automatically
514 // but the packages used for Hebrew and Farsi (Arabi) do not.
515 if (!runparams.useBidiPackage()
516 && !runparams.pass_thru
517 && bits_.number() == FONT_ON
518 && next.fontInfo().number() != FONT_ON
519 && (language()->lang() == "hebrew"
520 || language()->lang() == "farsi"
521 || language()->lang() == "arabic_arabi")) {
522 if (runparams.use_polyglossia) {
532 if (open_encoding_) {
533 // We need to close the encoding even if it does not change
534 // to do correct environment nesting
535 Encoding const * const ascii = encodings.fromLyXName("ascii");
536 pair<bool, int> const c = switchEncoding(os.os(), bparams,
540 runparams.encoding = ascii;
541 open_encoding_ = false;
545 && language() != base.language() && language() != next.language()
546 && (language()->encoding()->package() != Encoding::CJK)) {
549 bool const using_begin_end =
550 runparams.use_polyglossia ||
551 !lyxrc.language_command_end.empty();
560 string Font::toString(bool const toggle) const
562 string const lang = (language() == reset_language)
563 ? "reset" : language()->lang();
566 os << "family " << bits_.family() << '\n'
567 << "series " << bits_.series() << '\n'
568 << "shape " << bits_.shape() << '\n'
569 << "size " << bits_.size() << '\n'
570 << "emph " << bits_.emph() << '\n'
571 << "underbar " << bits_.underbar() << '\n'
572 << "strikeout " << bits_.strikeout() << '\n'
573 << "xout " << bits_.xout() << '\n'
574 << "uuline " << bits_.uuline() << '\n'
575 << "uwave " << bits_.uwave() << '\n'
576 << "noun " << bits_.noun() << '\n'
577 << "number " << bits_.number() << '\n'
578 << "nospellcheck " << bits_.nospellcheck() << '\n'
579 << "color " << bits_.color() << '\n'
580 << "language " << lang << '\n'
581 << "toggleall " << convert<string>(toggle);
586 bool Font::fromString(string const & data, bool & toggle)
588 istringstream is(data);
596 token = lex.getString();
598 if (token.empty() || !lex.next())
601 if (token == "family") {
602 int const next = lex.getInteger();
603 bits_.setFamily(FontFamily(next));
605 } else if (token == "series") {
606 int const next = lex.getInteger();
607 bits_.setSeries(FontSeries(next));
609 } else if (token == "shape") {
610 int const next = lex.getInteger();
611 bits_.setShape(FontShape(next));
613 } else if (token == "size") {
614 int const next = lex.getInteger();
615 bits_.setSize(FontSize(next));
616 // FIXME: shall style be handled there? Probably not.
617 } else if (token == "emph" || token == "underbar"
618 || token == "noun" || token == "number"
619 || token == "uuline" || token == "uwave"
620 || token == "strikeout" || token == "xout"
621 || token == "nospellcheck") {
623 int const next = lex.getInteger();
624 FontState const misc = FontState(next);
628 else if (token == "underbar")
629 bits_.setUnderbar(misc);
630 else if (token == "strikeout")
631 bits_.setStrikeout(misc);
632 else if (token == "xout")
634 else if (token == "uuline")
635 bits_.setUuline(misc);
636 else if (token == "uwave")
637 bits_.setUwave(misc);
638 else if (token == "noun")
640 else if (token == "number")
641 bits_.setNumber(misc);
642 else if (token == "nospellcheck")
643 bits_.setNoSpellcheck(misc);
645 } else if (token == "color") {
646 int const next = lex.getInteger();
647 bits_.setColor(ColorCode(next));
650 } else if (token == "background") {
651 int const next = lex.getInteger();
652 bits_.setBackground(ColorCode(next));
655 } else if (token == "language") {
656 string const next = lex.getString();
657 setLanguage(languages.getLanguage(next));
659 } else if (token == "toggleall") {
660 toggle = lex.getBool();
663 // Unrecognised token
673 void Font::validate(LaTeXFeatures & features) const
675 BufferParams const & bparams = features.bufferParams();
676 Language const * doc_language = bparams.language;
678 if (bits_.noun() == FONT_ON) {
679 LYXERR(Debug::LATEX, "font.noun: " << bits_.noun());
680 features.require("noun");
681 LYXERR(Debug::LATEX, "Noun enabled. Font: " << to_utf8(stateText()));
683 if (bits_.underbar() == FONT_ON) {
684 LYXERR(Debug::LATEX, "font.underline: " << bits_.underbar());
685 features.require("ulem");
686 LYXERR(Debug::LATEX, "Underline enabled. Font: " << to_utf8(stateText()));
688 if (bits_.strikeout() == FONT_ON) {
689 LYXERR(Debug::LATEX, "font.strikeout: " << bits_.strikeout());
690 features.require("ulem");
691 LYXERR(Debug::LATEX, "Strike out enabled. Font: " << to_utf8(stateText()));
693 if (bits_.xout() == FONT_ON) {
694 LYXERR(Debug::LATEX, "font.xout: " << bits_.xout());
695 features.require("ulem");
696 LYXERR(Debug::LATEX, "Cross out enabled. Font: " << to_utf8(stateText()));
698 if (bits_.uuline() == FONT_ON) {
699 LYXERR(Debug::LATEX, "font.uuline: " << bits_.uuline());
700 features.require("ulem");
701 LYXERR(Debug::LATEX, "Double underline enabled. Font: " << to_utf8(stateText()));
703 if (bits_.uwave() == FONT_ON) {
704 LYXERR(Debug::LATEX, "font.uwave: " << bits_.uwave());
705 features.require("ulem");
706 LYXERR(Debug::LATEX, "Wavy underline enabled. Font: " << to_utf8(stateText()));
708 switch (bits_.color()) {
712 // probably we should put here all interface colors used for
713 // font displaying! For now I just add this ones I know of (Jug)
715 case Color_notelabel:
720 case Color_lightgray:
728 features.require("xcolor");
731 features.require("color");
732 LYXERR(Debug::LATEX, "Color enabled. Font: " << to_utf8(stateText()));
735 // FIXME: Do something for background and soul package?
737 if (((features.usePolyglossia() && lang_->polyglossia() != doc_language->polyglossia())
738 || (features.useBabel() && lang_->babel() != doc_language->babel())
739 || (doc_language->encoding()->package() == Encoding::CJK && lang_ != doc_language))
740 && lang_ != ignore_language
741 && lang_ != latex_language)
743 features.useLanguage(lang_);
744 LYXERR(Debug::LATEX, "Found language " << lang_->lang());
749 ostream & operator<<(ostream & os, FontState fms)
751 return os << int(fms);
755 ostream & operator<<(ostream & os, FontInfo const & f)
758 << " family " << f.family()
759 << " series " << f.series()
760 << " shape " << f.shape()
761 << " size " << f.size()
762 << " style " << f.style()
763 << " color " << f.color()
764 // FIXME: uncomment this when we support background.
765 //<< " background " << f.background()
766 << " emph " << f.emph()
767 << " underbar " << f.underbar()
768 << " strikeout " << f.strikeout()
769 << " xout " << f.xout()
770 << " uuline " << f.uuline()
771 << " uwave " << f.uwave()
772 << " noun " << f.noun()
773 << " number " << f.number()
774 << " nospellcheck " << f.nospellcheck();
778 ostream & operator<<(ostream & os, Font const & font)
780 return os << font.bits_
781 << " lang: " << (font.lang_ ? font.lang_->lang() : nullptr);