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];
59 char const * GUIFamilyNames[NUM_FAMILIES + 2 /* default & error */] =
60 { N_("Roman"), N_("Sans Serif"), N_("Typewriter"), N_("Symbol"),
61 "cmr", "cmsy", "cmm", "cmex", "msa", "msb", "eufrak", "rsfs", "stmry",
62 "wasy", "esint", N_("Inherit"), N_("Ignore") };
64 char const * GUISeriesNames[NUM_SERIES + 2 /* default & error */] =
65 { N_("Medium"), N_("Bold"), N_("Inherit"), N_("Ignore") };
67 char const * GUIShapeNames[NUM_SHAPE + 2 /* default & error */] =
68 { N_("Upright"), N_("Italic"), N_("Slanted"), N_("Smallcaps"), N_("Inherit"),
71 char const * GUISizeNames[NUM_SIZE + 4 /* increase, decrease, default & error */] =
72 { N_("Tiny"), N_("Smallest"), N_("Smaller"), N_("Small"), N_("Normal"), N_("Large"),
73 N_("Larger"), N_("Largest"), N_("Huge"), N_("Huger"), N_("Increase"), N_("Decrease"),
74 N_("Inherit"), N_("Ignore") };
76 char const * GUIMiscNames[5] =
77 { N_("Off"), N_("On"), N_("Toggle"), N_("Inherit"), N_("Ignore") };
80 // Strings used to write LaTeX files
82 char const * LaTeXFamilyNames[NUM_FAMILIES + 2] =
83 { "textrm", "textsf", "texttt", "error1", "error2", "error3", "error4",
84 "error5", "error6", "error7", "error8", "error9", "error10", "error11",
85 "error12", "error13" };
87 char const * LaTeXSeriesNames[NUM_SERIES + 2] =
88 { "textmd", "textbf", "error4", "error5" };
90 char const * LaTeXShapeNames[NUM_SHAPE + 2] =
91 { "textup", "textit", "textsl", "textsc", "error6", "error7" };
93 char const * LaTeXSizeNames[NUM_SIZE + 4] =
94 { "tiny", "scriptsize", "footnotesize", "small", "normalsize", "large",
95 "Large", "LARGE", "huge", "Huge", "error8", "error9", "error10", "error11" };
100 Font::Font(FontInfo bits, Language const * l)
101 : bits_(bits), lang_(l), open_encoding_(false)
104 lang_ = default_language;
108 bool Font::isRightToLeft() const
110 return lang_->rightToLeft();
114 bool Font::isVisibleRightToLeft() const
116 return (lang_->rightToLeft() &&
117 bits_.number() != FONT_ON);
121 void Font::setLanguage(Language const * l)
127 /// Updates font settings according to request
128 void Font::update(Font const & newfont,
129 Language const * document_language,
132 bits_.update(newfont.fontInfo(), toggleall);
134 if (newfont.language() == language() && toggleall)
135 if (language() == document_language)
136 setLanguage(default_language);
138 setLanguage(document_language);
139 else if (newfont.language() == reset_language)
140 setLanguage(document_language);
141 else if (newfont.language() != ignore_language)
142 setLanguage(newfont.language());
146 docstring const stateText(FontInfo const & f)
149 if (f.family() != INHERIT_FAMILY)
150 os << _(GUIFamilyNames[f.family()]) << ", ";
151 if (f.series() != INHERIT_SERIES)
152 os << _(GUISeriesNames[f.series()]) << ", ";
153 if (f.shape() != INHERIT_SHAPE)
154 os << _(GUIShapeNames[f.shape()]) << ", ";
155 if (f.size() != FONT_SIZE_INHERIT)
156 os << _(GUISizeNames[f.size()]) << ", ";
157 if (f.color() != Color_inherit)
158 os << lcolor.getGUIName(f.color()) << ", ";
159 // FIXME: uncomment this when we support background.
160 //if (f.background() != Color_inherit)
161 // os << lcolor.getGUIName(f.background()) << ", ";
162 if (f.emph() != FONT_INHERIT)
163 os << bformat(_("Emphasis %1$s, "),
164 _(GUIMiscNames[f.emph()]));
165 if (f.underbar() != FONT_INHERIT)
166 os << bformat(_("Underline %1$s, "),
167 _(GUIMiscNames[f.underbar()]));
168 if (f.strikeout() != FONT_INHERIT)
169 os << bformat(_("Strike out %1$s, "),
170 _(GUIMiscNames[f.strikeout()]));
171 if (f.xout() != FONT_INHERIT)
172 os << bformat(_("Cross out %1$s, "),
173 _(GUIMiscNames[f.xout()]));
174 if (f.uuline() != FONT_INHERIT)
175 os << bformat(_("Double underline %1$s, "),
176 _(GUIMiscNames[f.uuline()]));
177 if (f.uwave() != FONT_INHERIT)
178 os << bformat(_("Wavy underline %1$s, "),
179 _(GUIMiscNames[f.uwave()]));
180 if (f.noun() != FONT_INHERIT)
181 os << bformat(_("Noun %1$s, "),
182 _(GUIMiscNames[f.noun()]));
183 if (f == inherit_font)
184 os << _("Default") << ", ";
190 docstring const Font::stateText(BufferParams * params) const
193 os << lyx::stateText(bits_);
194 if (!params || (language() != params->language))
195 os << bformat(_("Language: %1$s, "),
196 _(language()->display()));
197 if (bits_.number() != FONT_OFF)
198 os << " " << bformat(_("Number %1$s"),
199 _(GUIMiscNames[bits_.number()]));
200 return rtrim(os.str(), ", ");
204 // Returns size in latex format
205 string const Font::latexSize() const
207 return LaTeXSizeNames[bits_.size()];
211 /// Writes the changes from this font to orgfont in .lyx format in file
212 void Font::lyxWriteChanges(Font const & orgfont,
216 if (orgfont.fontInfo().family() != bits_.family())
217 os << "\\family " << LyXFamilyNames[bits_.family()] << "\n";
218 if (orgfont.fontInfo().series() != bits_.series())
219 os << "\\series " << LyXSeriesNames[bits_.series()] << "\n";
220 if (orgfont.fontInfo().shape() != bits_.shape())
221 os << "\\shape " << LyXShapeNames[bits_.shape()] << "\n";
222 if (orgfont.fontInfo().size() != bits_.size())
223 os << "\\size " << LyXSizeNames[bits_.size()] << "\n";
224 if (orgfont.fontInfo().emph() != bits_.emph())
225 os << "\\emph " << LyXMiscNames[bits_.emph()] << "\n";
226 if (orgfont.fontInfo().number() != bits_.number())
227 os << "\\numeric " << LyXMiscNames[bits_.number()] << "\n";
228 if (orgfont.fontInfo().nospellcheck() != bits_.nospellcheck())
229 os << "\\nospellcheck " << LyXMiscNames[bits_.nospellcheck()] << "\n";
230 if (orgfont.fontInfo().underbar() != bits_.underbar()) {
231 // This is only for backwards compatibility
232 switch (bits_.underbar()) {
233 case FONT_OFF: os << "\\bar no\n"; break;
234 case FONT_ON: os << "\\bar under\n"; break;
235 case FONT_TOGGLE: lyxerr << "Font::lyxWriteFontChanges: "
236 "FONT_TOGGLE should not appear here!"
239 case FONT_INHERIT: os << "\\bar default\n"; break;
240 case FONT_IGNORE: lyxerr << "Font::lyxWriteFontChanges: "
241 "IGNORE should not appear here!"
246 if (orgfont.fontInfo().strikeout() != bits_.strikeout()) {
247 os << "\\strikeout " << LyXMiscNames[bits_.strikeout()] << "\n";
249 if (orgfont.fontInfo().xout() != bits_.xout()) {
250 os << "\\xout " << LyXMiscNames[bits_.xout()] << "\n";
252 if (orgfont.fontInfo().uuline() != bits_.uuline()) {
253 os << "\\uuline " << LyXMiscNames[bits_.uuline()] << "\n";
255 if (orgfont.fontInfo().uwave() != bits_.uwave()) {
256 os << "\\uwave " << LyXMiscNames[bits_.uwave()] << "\n";
258 if (orgfont.fontInfo().noun() != bits_.noun()) {
259 os << "\\noun " << LyXMiscNames[bits_.noun()] << "\n";
261 if (orgfont.fontInfo().color() != bits_.color())
262 os << "\\color " << lcolor.getLyXName(bits_.color()) << '\n';
263 // FIXME: uncomment this when we support background.
264 //if (orgfont.fontInfo().background() != bits_.background())
265 // os << "\\color " << lcolor.getLyXName(bits_.background()) << '\n';
266 if (orgfont.language() != language() &&
267 language() != latex_language) {
269 os << "\\lang " << language()->lang() << "\n";
271 os << "\\lang unknown\n";
276 /// Writes the head of the LaTeX needed to impose this font
277 // Returns number of chars written.
278 int Font::latexWriteStartChanges(odocstream & os, BufferParams const & bparams,
279 OutputParams const & runparams,
281 Font const & prev) const
287 // polyglossia or babel?
288 if (runparams.use_polyglossia
289 && language()->lang() != base.language()->lang()
290 && language() != prev.language()) {
291 if (!language()->polyglossia().empty()) {
292 string tmp = "\\text" + language()->polyglossia();
293 if (!language()->polyglossiaOpts().empty())
294 tmp += "[" + language()->polyglossiaOpts() + "]";
296 os << from_ascii(tmp);
297 count += tmp.length();
298 pushLanguageName(language()->polyglossia(), true);
299 } else if (language()->encoding()->package() != Encoding::CJK) {
303 } else if (language()->babel() != base.language()->babel() &&
304 language() != prev.language()) {
305 if (language()->lang() == "farsi") {
308 } else if (!isRightToLeft() &&
309 base.language()->lang() == "farsi") {
312 } else if (language()->lang() == "arabic_arabi") {
315 } else if (!isRightToLeft() &&
316 base.language()->lang() == "arabic_arabi") {
319 // currently the remaining RTL languages are arabic_arabtex and hebrew
320 } else if (isRightToLeft() != prev.isRightToLeft()) {
321 if (isRightToLeft()) {
328 } else if (!language()->babel().empty()) {
330 subst(lyxrc.language_command_local,
331 "$$lang", language()->babel());
332 os << from_ascii(tmp);
333 count += tmp.length();
334 if (!lyxrc.language_command_end.empty())
335 pushLanguageName(language()->babel(), true);
336 } else if (language()->encoding()->package() != Encoding::CJK) {
342 if (language()->encoding()->package() == Encoding::CJK) {
343 pair<bool, int> const c = switchEncoding(os, bparams,
344 runparams, *(language()->encoding()));
346 open_encoding_ = true;
348 runparams.encoding = language()->encoding();
352 // If the current language is Hebrew, Arabic, or Farsi
353 // the numbers are written Left-to-Right. ArabTeX package
354 // and bidi (polyglossia) reorder the number automatically
355 // but the packages used for Hebrew and Farsi (Arabi) do not.
356 if (!runparams.use_polyglossia
357 && !runparams.pass_thru
358 && bits_.number() == FONT_ON
359 && prev.fontInfo().number() != FONT_ON
360 && (language()->lang() == "hebrew"
361 || language()->lang() == "farsi"
362 || language()->lang() == "arabic_arabi")) {
368 f.reduce(base.bits_);
370 p.reduce(prev.bits_);
372 if (f.family() != INHERIT_FAMILY) {
374 << LaTeXFamilyNames[f.family()]
376 count += strlen(LaTeXFamilyNames[f.family()]) + 2;
377 env = true; //We have opened a new environment
379 if (f.series() != INHERIT_SERIES) {
381 << LaTeXSeriesNames[f.series()]
383 count += strlen(LaTeXSeriesNames[f.series()]) + 2;
384 env = true; //We have opened a new environment
386 if (f.shape() != INHERIT_SHAPE) {
388 << LaTeXShapeNames[f.shape()]
390 count += strlen(LaTeXShapeNames[f.shape()]) + 2;
391 env = true; //We have opened a new environment
393 if (f.color() != Color_inherit && f.color() != Color_ignore) {
394 if (f.color() == Color_none && p.color() != Color_none) {
395 // Color none: Close previous color, if any
398 } else if (f.color() != Color_none) {
400 << from_ascii(lcolor.getLaTeXName(f.color()))
402 count += lcolor.getLaTeXName(f.color()).length() + 13;
404 env = true; //We have opened a new environment
406 // FIXME: uncomment this when we support background.
408 if (f.background() != Color_inherit && f.background() != Color_ignore) {
410 << from_ascii(lcolor.getLaTeXName(f.background()))
412 count += lcolor.getLaTeXName(f.background()).length() + 13;
413 env = true; //We have opened a new environment
416 if (f.emph() == FONT_ON) {
419 env = true; //We have opened a new environment
421 // \noun{} is a LyX special macro
422 if (f.noun() == FONT_ON) {
425 env = true; //We have opened a new environment
427 if (f.size() != FONT_SIZE_INHERIT) {
428 // If we didn't open an environment above, we open one here
434 << LaTeXSizeNames[f.size()]
436 count += strlen(LaTeXSizeNames[f.size()]) + 3;
438 // The ulem commands need to be on the deepest nesting level
439 // because ulem puts every nested group or macro in a box,
440 // which prevents linebreaks (#8424, #8733)
441 if (f.underbar() == FONT_ON) {
444 ++runparams.inulemcmd;
446 if (f.uuline() == FONT_ON) {
449 ++runparams.inulemcmd;
451 if (f.strikeout() == FONT_ON) {
454 ++runparams.inulemcmd;
456 if (f.xout() == FONT_ON) {
459 ++runparams.inulemcmd;
461 if (f.uwave() == FONT_ON) {
462 if (runparams.inulemcmd) {
463 // needed with nested uwave in xout
464 // see https://tex.stackexchange.com/a/263042
465 os << "\\ULdepth=1000pt";
470 ++runparams.inulemcmd;
476 /// Writes ending block of LaTeX needed to close use of this font
477 // Returns number of chars written
478 // This one corresponds to latexWriteStartChanges(). (Asger)
479 int Font::latexWriteEndChanges(otexstream & os, BufferParams const & bparams,
480 OutputParams const & runparams,
484 bool const & closeLanguage) const
489 // reduce the current font to changes against the base
490 // font (of the layout). We use a temporary for this to
491 // avoid changing this font instance, as that would break
493 f.reduce(base.bits_);
495 if (f.family() != INHERIT_FAMILY) {
498 env = true; // Size change need not bother about closing env.
500 if (f.series() != INHERIT_SERIES) {
503 env = true; // Size change need not bother about closing env.
505 if (f.shape() != INHERIT_SHAPE) {
508 env = true; // Size change need not bother about closing env.
510 if (f.color() != Color_inherit && f.color() != Color_ignore && f.color() != Color_none) {
513 env = true; // Size change need not bother about closing env.
515 if (f.emph() == FONT_ON) {
518 env = true; // Size change need not bother about closing env.
520 if (f.noun() == FONT_ON) {
523 env = true; // Size change need not bother about closing env.
525 if (f.size() != FONT_SIZE_INHERIT) {
526 // We only have to close if only size changed
528 if (needPar && !closeLanguage) {
537 if (f.underbar() == FONT_ON) {
540 --runparams.inulemcmd;
542 if (f.strikeout() == FONT_ON) {
545 --runparams.inulemcmd;
547 if (f.xout() == FONT_ON) {
550 --runparams.inulemcmd;
552 if (f.uuline() == FONT_ON) {
555 --runparams.inulemcmd;
557 if (f.uwave() == FONT_ON) {
560 --runparams.inulemcmd;
563 // If the current language is Hebrew, Arabic, or Farsi
564 // the numbers are written Left-to-Right. ArabTeX package
565 // and bidi (polyglossia) reorder the number automatically
566 // but the packages used for Hebrew and Farsi (Arabi) do not.
567 if (!runparams.use_polyglossia
568 && !runparams.pass_thru
569 && bits_.number() == FONT_ON
570 && next.fontInfo().number() != FONT_ON
571 && (language()->lang() == "hebrew"
572 || language()->lang() == "farsi"
573 || language()->lang() == "arabic_arabi")) {
578 if (open_encoding_) {
579 // We need to close the encoding even if it does not change
580 // to do correct environment nesting
581 Encoding const * const ascii = encodings.fromLyXName("ascii");
582 pair<bool, int> const c = switchEncoding(os.os(), bparams,
586 runparams.encoding = ascii;
587 open_encoding_ = false;
591 && language() != base.language() && language() != next.language()
592 && language()->encoding()->package() != Encoding::CJK) {
595 bool const using_begin_end =
596 runparams.use_polyglossia ||
597 !lyxrc.language_command_end.empty();
606 string Font::toString(bool const toggle) const
608 string const lang = (language() == reset_language)
609 ? "reset" : language()->lang();
612 os << "family " << bits_.family() << '\n'
613 << "series " << bits_.series() << '\n'
614 << "shape " << bits_.shape() << '\n'
615 << "size " << bits_.size() << '\n'
616 << "emph " << bits_.emph() << '\n'
617 << "underbar " << bits_.underbar() << '\n'
618 << "strikeout " << bits_.strikeout() << '\n'
619 << "xout " << bits_.xout() << '\n'
620 << "uuline " << bits_.uuline() << '\n'
621 << "uwave " << bits_.uwave() << '\n'
622 << "noun " << bits_.noun() << '\n'
623 << "number " << bits_.number() << '\n'
624 << "nospellcheck " << bits_.nospellcheck() << '\n'
625 << "color " << bits_.color() << '\n'
626 << "language " << lang << '\n'
627 << "toggleall " << convert<string>(toggle);
632 bool Font::fromString(string const & data, bool & toggle)
634 istringstream is(data);
642 token = lex.getString();
644 if (token.empty() || !lex.next())
647 if (token == "family") {
648 int const next = lex.getInteger();
649 bits_.setFamily(FontFamily(next));
651 } else if (token == "series") {
652 int const next = lex.getInteger();
653 bits_.setSeries(FontSeries(next));
655 } else if (token == "shape") {
656 int const next = lex.getInteger();
657 bits_.setShape(FontShape(next));
659 } else if (token == "size") {
660 int const next = lex.getInteger();
661 bits_.setSize(FontSize(next));
663 } else if (token == "emph" || token == "underbar"
664 || token == "noun" || token == "number"
665 || token == "uuline" || token == "uwave"
666 || token == "strikeout" || token == "xout"
667 || token == "nospellcheck") {
669 int const next = lex.getInteger();
670 FontState const misc = FontState(next);
674 else if (token == "underbar")
675 bits_.setUnderbar(misc);
676 else if (token == "strikeout")
677 bits_.setStrikeout(misc);
678 else if (token == "xout")
680 else if (token == "uuline")
681 bits_.setUuline(misc);
682 else if (token == "uwave")
683 bits_.setUwave(misc);
684 else if (token == "noun")
686 else if (token == "number")
687 bits_.setNumber(misc);
688 else if (token == "nospellcheck")
689 bits_.setNoSpellcheck(misc);
691 } else if (token == "color") {
692 int const next = lex.getInteger();
693 bits_.setColor(ColorCode(next));
696 } else if (token == "background") {
697 int const next = lex.getInteger();
698 bits_.setBackground(ColorCode(next));
701 } else if (token == "language") {
702 string const next = lex.getString();
703 setLanguage(languages.getLanguage(next));
705 } else if (token == "toggleall") {
706 toggle = lex.getBool();
709 // Unrecognised token
719 void Font::validate(LaTeXFeatures & features) const
721 BufferParams const & bparams = features.bufferParams();
722 Language const * doc_language = bparams.language;
724 if (bits_.noun() == FONT_ON) {
725 LYXERR(Debug::LATEX, "font.noun: " << bits_.noun());
726 features.require("noun");
727 LYXERR(Debug::LATEX, "Noun enabled. Font: " << to_utf8(stateText(0)));
729 if (bits_.underbar() == FONT_ON) {
730 LYXERR(Debug::LATEX, "font.underline: " << bits_.underbar());
731 features.require("ulem");
732 LYXERR(Debug::LATEX, "Underline enabled. Font: " << to_utf8(stateText(0)));
734 if (bits_.strikeout() == FONT_ON) {
735 LYXERR(Debug::LATEX, "font.strikeout: " << bits_.strikeout());
736 features.require("ulem");
737 LYXERR(Debug::LATEX, "Strike out enabled. Font: " << to_utf8(stateText(0)));
739 if (bits_.xout() == FONT_ON) {
740 LYXERR(Debug::LATEX, "font.xout: " << bits_.xout());
741 features.require("ulem");
742 LYXERR(Debug::LATEX, "Cross out enabled. Font: " << to_utf8(stateText(0)));
744 if (bits_.uuline() == FONT_ON) {
745 LYXERR(Debug::LATEX, "font.uuline: " << bits_.uuline());
746 features.require("ulem");
747 LYXERR(Debug::LATEX, "Double underline enabled. Font: " << to_utf8(stateText(0)));
749 if (bits_.uwave() == FONT_ON) {
750 LYXERR(Debug::LATEX, "font.uwave: " << bits_.uwave());
751 features.require("ulem");
752 LYXERR(Debug::LATEX, "Wavy underline enabled. Font: " << to_utf8(stateText(0)));
754 switch (bits_.color()) {
758 // probably we should put here all interface colors used for
759 // font displaying! For now I just add this ones I know of (Jug)
761 case Color_notelabel:
766 case Color_lightgray:
774 features.require("xcolor");
777 features.require("color");
778 LYXERR(Debug::LATEX, "Color enabled. Font: " << to_utf8(stateText(0)));
781 // FIXME: Do something for background and soul package?
783 if (((features.usePolyglossia() && lang_->polyglossia() != doc_language->polyglossia())
784 || (features.useBabel() && lang_->babel() != doc_language->babel())
785 || (doc_language->encoding()->package() == Encoding::CJK && lang_ != doc_language))
786 && lang_ != ignore_language
787 && lang_ != latex_language)
789 features.useLanguage(lang_);
790 LYXERR(Debug::LATEX, "Found language " << lang_->lang());
795 ostream & operator<<(ostream & os, FontState fms)
797 return os << int(fms);
801 ostream & operator<<(ostream & os, FontInfo const & f)
804 << " family " << f.family()
805 << " series " << f.series()
806 << " shape " << f.shape()
807 << " size " << f.size()
808 << " color " << f.color()
809 // FIXME: uncomment this when we support background.
810 //<< " background " << f.background()
811 << " emph " << f.emph()
812 << " underbar " << f.underbar()
813 << " strikeout " << f.strikeout()
814 << " xout " << f.xout()
815 << " uuline " << f.uuline()
816 << " uwave " << f.uwave()
817 << " noun " << f.noun()
818 << " number " << f.number()
819 << " nospellcheck " << f.nospellcheck();
823 ostream & operator<<(ostream & os, Font const & font)
825 return os << font.bits_
826 << " lang: " << (font.lang_ ? font.lang_->lang() : 0);