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" };
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() + "]";
234 os << from_ascii(tmp);
235 count += tmp.length();
236 pushLanguageName(language()->polyglossia(), true);
237 } else if (language()->encoding()->package() != Encoding::CJK) {
241 } else if (language()->babel() != base.language()->babel() &&
242 language() != prev.language()) {
243 if (language()->lang() == "farsi") {
246 } else if (!isRightToLeft() &&
247 base.language()->lang() == "farsi") {
250 } else if (language()->lang() == "arabic_arabi") {
253 } else if (!isRightToLeft() &&
254 base.language()->lang() == "arabic_arabi") {
257 // currently the remaining RTL languages are arabic_arabtex and hebrew
258 } else if (isRightToLeft() != prev.isRightToLeft()) {
259 if (isRightToLeft()) {
266 } else if (!language()->babel().empty()) {
268 subst(lyxrc.language_command_local,
269 "$$lang", language()->babel());
270 os << from_ascii(tmp);
271 count += tmp.length();
272 if (!lyxrc.language_command_end.empty())
273 pushLanguageName(language()->babel(), true);
274 } else if (language()->encoding()->package() != Encoding::CJK) {
280 if (language()->encoding()->package() == Encoding::CJK) {
281 pair<bool, int> const c = switchEncoding(os, bparams,
282 runparams, *(language()->encoding()));
284 open_encoding_ = true;
286 runparams.encoding = language()->encoding();
291 f.reduce(base.bits_);
293 p.reduce(prev.bits_);
295 if (f.family() != INHERIT_FAMILY) {
297 << LaTeXFamilyNames[f.family()]
299 count += strlen(LaTeXFamilyNames[f.family()]) + 2;
300 env = true; //We have opened a new environment
302 if (f.series() != INHERIT_SERIES) {
304 << LaTeXSeriesNames[f.series()]
306 count += strlen(LaTeXSeriesNames[f.series()]) + 2;
307 env = true; //We have opened a new environment
309 if (f.shape() != INHERIT_SHAPE) {
311 << LaTeXShapeNames[f.shape()]
313 count += strlen(LaTeXShapeNames[f.shape()]) + 2;
314 env = true; //We have opened a new environment
316 if (f.color() != Color_inherit && f.color() != Color_ignore) {
317 if (f.color() == Color_none && p.color() != Color_none) {
318 // Color none: Close previous color, if any
321 } else if (f.color() != Color_none) {
323 << from_ascii(lcolor.getLaTeXName(f.color()))
325 count += lcolor.getLaTeXName(f.color()).length() + 13;
327 env = true; //We have opened a new environment
329 // FIXME: uncomment this when we support background.
331 if (f.background() != Color_inherit && f.background() != Color_ignore) {
333 << from_ascii(lcolor.getLaTeXName(f.background()))
335 count += lcolor.getLaTeXName(f.background()).length() + 13;
336 env = true; //We have opened a new environment
339 // If the current language is Hebrew, Arabic, or Farsi
340 // the numbers are written Left-to-Right. ArabTeX package
341 // and bidi (polyglossia) reorder the number automatically
342 // but the packages used for Hebrew and Farsi (Arabi) do not.
343 if (!runparams.use_polyglossia
344 && !runparams.pass_thru
345 && bits_.number() == FONT_ON
346 && prev.fontInfo().number() != FONT_ON
347 && (language()->lang() == "hebrew"
348 || language()->lang() == "farsi"
349 || language()->lang() == "arabic_arabi")) {
353 if (f.emph() == FONT_ON) {
356 env = true; //We have opened a new environment
358 // \noun{} is a LyX special macro
359 if (f.noun() == FONT_ON) {
362 env = true; //We have opened a new environment
364 if (f.size() != INHERIT_SIZE) {
365 // If we didn't open an environment above, we open one here
371 << LaTeXSizeNames[f.size()]
373 count += strlen(LaTeXSizeNames[f.size()]) + 3;
375 // The ulem commands need to be on the deepest nesting level
376 // because ulem puts every nested group or macro in a box,
377 // which prevents linebreaks (#8424, #8733)
378 if (f.underbar() == FONT_ON) {
381 ++runparams.inulemcmd;
383 if (f.uuline() == FONT_ON) {
386 ++runparams.inulemcmd;
388 if (f.strikeout() == FONT_ON) {
391 ++runparams.inulemcmd;
393 if (f.xout() == FONT_ON) {
396 ++runparams.inulemcmd;
398 if (f.uwave() == FONT_ON) {
399 if (runparams.inulemcmd) {
400 // needed with nested uwave in xout
401 // see https://tex.stackexchange.com/a/263042
402 os << "\\ULdepth=1000pt";
407 ++runparams.inulemcmd;
413 /// Writes ending block of LaTeX needed to close use of this font
414 // Returns number of chars written
415 // This one corresponds to latexWriteStartChanges(). (Asger)
416 int Font::latexWriteEndChanges(otexstream & os, BufferParams const & bparams,
417 OutputParams const & runparams,
421 bool const & closeLanguage) const
426 // reduce the current font to changes against the base
427 // font (of the layout). We use a temporary for this to
428 // avoid changing this font instance, as that would break
430 f.reduce(base.bits_);
432 if (f.family() != INHERIT_FAMILY) {
435 env = true; // Size change need not bother about closing env.
437 if (f.series() != INHERIT_SERIES) {
440 env = true; // Size change need not bother about closing env.
442 if (f.shape() != INHERIT_SHAPE) {
445 env = true; // Size change need not bother about closing env.
447 if (f.color() != Color_inherit && f.color() != Color_ignore && f.color() != Color_none) {
450 env = true; // Size change need not bother about closing env.
452 if (f.emph() == FONT_ON) {
455 env = true; // Size change need not bother about closing env.
457 if (f.noun() == FONT_ON) {
460 env = true; // Size change need not bother about closing env.
462 if (f.size() != INHERIT_SIZE) {
463 // We only have to close if only size changed
465 if (needPar && !closeLanguage) {
474 if (f.underbar() == FONT_ON) {
477 --runparams.inulemcmd;
479 if (f.strikeout() == FONT_ON) {
482 --runparams.inulemcmd;
484 if (f.xout() == FONT_ON) {
487 --runparams.inulemcmd;
489 if (f.uuline() == FONT_ON) {
492 --runparams.inulemcmd;
494 if (f.uwave() == FONT_ON) {
497 --runparams.inulemcmd;
500 // If the current language is Hebrew, Arabic, or Farsi
501 // the numbers are written Left-to-Right. ArabTeX package
502 // and bidi (polyglossia) reorder the number automatically
503 // but the packages used for Hebrew and Farsi (Arabi) do not.
504 if (!runparams.use_polyglossia
505 && !runparams.pass_thru
506 && bits_.number() == FONT_ON
507 && next.fontInfo().number() != FONT_ON
508 && (language()->lang() == "hebrew"
509 || language()->lang() == "farsi"
510 || language()->lang() == "arabic_arabi")) {
515 if (open_encoding_) {
516 // We need to close the encoding even if it does not change
517 // to do correct environment nesting
518 Encoding const * const ascii = encodings.fromLyXName("ascii");
519 pair<bool, int> const c = switchEncoding(os.os(), bparams,
523 runparams.encoding = ascii;
524 open_encoding_ = false;
528 && language() != base.language() && language() != next.language()
529 && (language()->encoding()->package() != Encoding::CJK
530 || bparams.useNonTeXFonts)) {
533 bool const using_begin_end =
534 runparams.use_polyglossia ||
535 !lyxrc.language_command_end.empty();
544 string Font::toString(bool const toggle) const
546 string const lang = (language() == reset_language)
547 ? "reset" : language()->lang();
550 os << "family " << bits_.family() << '\n'
551 << "series " << bits_.series() << '\n'
552 << "shape " << bits_.shape() << '\n'
553 << "size " << bits_.size() << '\n'
554 << "emph " << bits_.emph() << '\n'
555 << "underbar " << bits_.underbar() << '\n'
556 << "strikeout " << bits_.strikeout() << '\n'
557 << "xout " << bits_.xout() << '\n'
558 << "uuline " << bits_.uuline() << '\n'
559 << "uwave " << bits_.uwave() << '\n'
560 << "noun " << bits_.noun() << '\n'
561 << "number " << bits_.number() << '\n'
562 << "nospellcheck " << bits_.nospellcheck() << '\n'
563 << "color " << bits_.color() << '\n'
564 << "language " << lang << '\n'
565 << "toggleall " << convert<string>(toggle);
570 bool Font::fromString(string const & data, bool & toggle)
572 istringstream is(data);
580 token = lex.getString();
582 if (token.empty() || !lex.next())
585 if (token == "family") {
586 int const next = lex.getInteger();
587 bits_.setFamily(FontFamily(next));
589 } else if (token == "series") {
590 int const next = lex.getInteger();
591 bits_.setSeries(FontSeries(next));
593 } else if (token == "shape") {
594 int const next = lex.getInteger();
595 bits_.setShape(FontShape(next));
597 } else if (token == "size") {
598 int const next = lex.getInteger();
599 bits_.setSize(FontSize(next));
600 // FIXME: shall style be handled there? Probably not.
601 } else if (token == "emph" || token == "underbar"
602 || token == "noun" || token == "number"
603 || token == "uuline" || token == "uwave"
604 || token == "strikeout" || token == "xout"
605 || token == "nospellcheck") {
607 int const next = lex.getInteger();
608 FontState const misc = FontState(next);
612 else if (token == "underbar")
613 bits_.setUnderbar(misc);
614 else if (token == "strikeout")
615 bits_.setStrikeout(misc);
616 else if (token == "xout")
618 else if (token == "uuline")
619 bits_.setUuline(misc);
620 else if (token == "uwave")
621 bits_.setUwave(misc);
622 else if (token == "noun")
624 else if (token == "number")
625 bits_.setNumber(misc);
626 else if (token == "nospellcheck")
627 bits_.setNoSpellcheck(misc);
629 } else if (token == "color") {
630 int const next = lex.getInteger();
631 bits_.setColor(ColorCode(next));
634 } else if (token == "background") {
635 int const next = lex.getInteger();
636 bits_.setBackground(ColorCode(next));
639 } else if (token == "language") {
640 string const next = lex.getString();
641 setLanguage(languages.getLanguage(next));
643 } else if (token == "toggleall") {
644 toggle = lex.getBool();
647 // Unrecognised token
657 void Font::validate(LaTeXFeatures & features) const
659 BufferParams const & bparams = features.bufferParams();
660 Language const * doc_language = bparams.language;
662 if (bits_.noun() == FONT_ON) {
663 LYXERR(Debug::LATEX, "font.noun: " << bits_.noun());
664 features.require("noun");
665 LYXERR(Debug::LATEX, "Noun enabled. Font: " << to_utf8(stateText()));
667 if (bits_.underbar() == FONT_ON) {
668 LYXERR(Debug::LATEX, "font.underline: " << bits_.underbar());
669 features.require("ulem");
670 LYXERR(Debug::LATEX, "Underline enabled. Font: " << to_utf8(stateText()));
672 if (bits_.strikeout() == FONT_ON) {
673 LYXERR(Debug::LATEX, "font.strikeout: " << bits_.strikeout());
674 features.require("ulem");
675 LYXERR(Debug::LATEX, "Strike out enabled. Font: " << to_utf8(stateText()));
677 if (bits_.xout() == FONT_ON) {
678 LYXERR(Debug::LATEX, "font.xout: " << bits_.xout());
679 features.require("ulem");
680 LYXERR(Debug::LATEX, "Cross out enabled. Font: " << to_utf8(stateText()));
682 if (bits_.uuline() == FONT_ON) {
683 LYXERR(Debug::LATEX, "font.uuline: " << bits_.uuline());
684 features.require("ulem");
685 LYXERR(Debug::LATEX, "Double underline enabled. Font: " << to_utf8(stateText()));
687 if (bits_.uwave() == FONT_ON) {
688 LYXERR(Debug::LATEX, "font.uwave: " << bits_.uwave());
689 features.require("ulem");
690 LYXERR(Debug::LATEX, "Wavy underline enabled. Font: " << to_utf8(stateText()));
692 switch (bits_.color()) {
696 // probably we should put here all interface colors used for
697 // font displaying! For now I just add this ones I know of (Jug)
699 case Color_notelabel:
704 case Color_lightgray:
712 features.require("xcolor");
715 features.require("color");
716 LYXERR(Debug::LATEX, "Color enabled. Font: " << to_utf8(stateText()));
719 // FIXME: Do something for background and soul package?
721 if (((features.usePolyglossia() && lang_->polyglossia() != doc_language->polyglossia())
722 || (features.useBabel() && lang_->babel() != doc_language->babel())
723 || (doc_language->encoding()->package() == Encoding::CJK && lang_ != doc_language))
724 && lang_ != ignore_language
725 && lang_ != latex_language)
727 features.useLanguage(lang_);
728 LYXERR(Debug::LATEX, "Found language " << lang_->lang());
733 ostream & operator<<(ostream & os, FontState fms)
735 return os << int(fms);
739 ostream & operator<<(ostream & os, FontInfo const & f)
742 << " family " << f.family()
743 << " series " << f.series()
744 << " shape " << f.shape()
745 << " size " << f.size()
746 << " style " << f.style()
747 << " color " << f.color()
748 // FIXME: uncomment this when we support background.
749 //<< " background " << f.background()
750 << " emph " << f.emph()
751 << " underbar " << f.underbar()
752 << " strikeout " << f.strikeout()
753 << " xout " << f.xout()
754 << " uuline " << f.uuline()
755 << " uwave " << f.uwave()
756 << " noun " << f.noun()
757 << " number " << f.number()
758 << " nospellcheck " << f.nospellcheck();
762 ostream & operator<<(ostream & os, Font const & font)
764 return os << font.bits_
765 << " lang: " << (font.lang_ ? font.lang_->lang() : 0);