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().underbar() != bits_.underbar()) {
229 // This is only for backwards compatibility
230 switch (bits_.underbar()) {
231 case FONT_OFF: os << "\\bar no\n"; break;
232 case FONT_ON: os << "\\bar under\n"; break;
233 case FONT_TOGGLE: lyxerr << "Font::lyxWriteFontChanges: "
234 "FONT_TOGGLE should not appear here!"
237 case FONT_INHERIT: os << "\\bar default\n"; break;
238 case FONT_IGNORE: lyxerr << "Font::lyxWriteFontChanges: "
239 "IGNORE should not appear here!"
244 if (orgfont.fontInfo().strikeout() != bits_.strikeout()) {
245 os << "\\strikeout " << LyXMiscNames[bits_.strikeout()] << "\n";
247 if (orgfont.fontInfo().xout() != bits_.xout()) {
248 os << "\\xout " << LyXMiscNames[bits_.xout()] << "\n";
250 if (orgfont.fontInfo().uuline() != bits_.uuline()) {
251 os << "\\uuline " << LyXMiscNames[bits_.uuline()] << "\n";
253 if (orgfont.fontInfo().uwave() != bits_.uwave()) {
254 os << "\\uwave " << LyXMiscNames[bits_.uwave()] << "\n";
256 if (orgfont.fontInfo().noun() != bits_.noun()) {
257 os << "\\noun " << LyXMiscNames[bits_.noun()] << "\n";
259 if (orgfont.fontInfo().color() != bits_.color())
260 os << "\\color " << lcolor.getLyXName(bits_.color()) << '\n';
261 // FIXME: uncomment this when we support background.
262 //if (orgfont.fontInfo().background() != bits_.background())
263 // os << "\\color " << lcolor.getLyXName(bits_.background()) << '\n';
264 if (orgfont.language() != language() &&
265 language() != latex_language) {
267 os << "\\lang " << language()->lang() << "\n";
269 os << "\\lang unknown\n";
274 /// Writes the head of the LaTeX needed to impose this font
275 // Returns number of chars written.
276 int Font::latexWriteStartChanges(odocstream & os, BufferParams const & bparams,
277 OutputParams const & runparams,
279 Font const & prev) const
285 // polyglossia or babel?
286 if (runparams.use_polyglossia
287 && language()->lang() != base.language()->lang()
288 && language() != prev.language()) {
289 if (!language()->polyglossia().empty()) {
290 string tmp = "\\text" + language()->polyglossia();
291 if (!language()->polyglossiaOpts().empty())
292 tmp += "[" + language()->polyglossiaOpts() + "]";
294 os << from_ascii(tmp);
295 count += tmp.length();
296 pushLanguageName(language()->polyglossia(), true);
297 } else if (language()->encoding()->package() != Encoding::CJK) {
301 } else if (language()->babel() != base.language()->babel() &&
302 language() != prev.language()) {
303 if (language()->lang() == "farsi") {
306 } else if (!isRightToLeft() &&
307 base.language()->lang() == "farsi") {
310 } else if (language()->lang() == "arabic_arabi") {
313 } else if (!isRightToLeft() &&
314 base.language()->lang() == "arabic_arabi") {
317 // currently the remaining RTL languages are arabic_arabtex and hebrew
318 } else if (isRightToLeft() != prev.isRightToLeft()) {
319 if (isRightToLeft()) {
326 } else if (!language()->babel().empty()) {
328 subst(lyxrc.language_command_local,
329 "$$lang", language()->babel());
330 os << from_ascii(tmp);
331 count += tmp.length();
332 if (!lyxrc.language_command_end.empty())
333 pushLanguageName(language()->babel(), true);
334 } else if (language()->encoding()->package() != Encoding::CJK) {
340 if (language()->encoding()->package() == Encoding::CJK) {
341 pair<bool, int> const c = switchEncoding(os, bparams,
342 runparams, *(language()->encoding()));
344 open_encoding_ = true;
346 runparams.encoding = language()->encoding();
350 // If the current language is Hebrew, Arabic, or Farsi
351 // the numbers are written Left-to-Right. ArabTeX package
352 // and bidi (polyglossia) reorder the number automatically
353 // but the packages used for Hebrew and Farsi (Arabi) do not.
354 if (!runparams.use_polyglossia
355 && !runparams.pass_thru
356 && bits_.number() == FONT_ON
357 && prev.fontInfo().number() != FONT_ON
358 && (language()->lang() == "hebrew"
359 || language()->lang() == "farsi"
360 || language()->lang() == "arabic_arabi")) {
366 f.reduce(base.bits_);
368 p.reduce(prev.bits_);
370 if (f.family() != INHERIT_FAMILY) {
372 << LaTeXFamilyNames[f.family()]
374 count += strlen(LaTeXFamilyNames[f.family()]) + 2;
375 env = true; //We have opened a new environment
377 if (f.series() != INHERIT_SERIES) {
379 << LaTeXSeriesNames[f.series()]
381 count += strlen(LaTeXSeriesNames[f.series()]) + 2;
382 env = true; //We have opened a new environment
384 if (f.shape() != INHERIT_SHAPE) {
386 << LaTeXShapeNames[f.shape()]
388 count += strlen(LaTeXShapeNames[f.shape()]) + 2;
389 env = true; //We have opened a new environment
391 if (f.color() != Color_inherit && f.color() != Color_ignore) {
392 if (f.color() == Color_none && p.color() != Color_none) {
393 // Color none: Close previous color, if any
396 } else if (f.color() != Color_none) {
398 << from_ascii(lcolor.getLaTeXName(f.color()))
400 count += lcolor.getLaTeXName(f.color()).length() + 13;
402 env = true; //We have opened a new environment
404 // FIXME: uncomment this when we support background.
406 if (f.background() != Color_inherit && f.background() != Color_ignore) {
408 << from_ascii(lcolor.getLaTeXName(f.background()))
410 count += lcolor.getLaTeXName(f.background()).length() + 13;
411 env = true; //We have opened a new environment
414 if (f.emph() == FONT_ON) {
417 env = true; //We have opened a new environment
419 // \noun{} is a LyX special macro
420 if (f.noun() == FONT_ON) {
423 env = true; //We have opened a new environment
425 if (f.size() != FONT_SIZE_INHERIT) {
426 // If we didn't open an environment above, we open one here
432 << LaTeXSizeNames[f.size()]
434 count += strlen(LaTeXSizeNames[f.size()]) + 3;
436 // The ulem commands need to be on the deepest nesting level
437 // because ulem puts every nested group or macro in a box,
438 // which prevents linebreaks (#8424, #8733)
439 if (f.underbar() == FONT_ON) {
442 ++runparams.inulemcmd;
444 if (f.strikeout() == FONT_ON) {
447 ++runparams.inulemcmd;
449 if (f.xout() == FONT_ON) {
452 ++runparams.inulemcmd;
454 if (f.uuline() == FONT_ON) {
457 ++runparams.inulemcmd;
459 if (f.uwave() == FONT_ON) {
462 ++runparams.inulemcmd;
468 /// Writes ending block of LaTeX needed to close use of this font
469 // Returns number of chars written
470 // This one corresponds to latexWriteStartChanges(). (Asger)
471 int Font::latexWriteEndChanges(otexstream & os, BufferParams const & bparams,
472 OutputParams const & runparams,
476 bool const & closeLanguage) const
481 // reduce the current font to changes against the base
482 // font (of the layout). We use a temporary for this to
483 // avoid changing this font instance, as that would break
485 f.reduce(base.bits_);
487 if (f.family() != INHERIT_FAMILY) {
490 env = true; // Size change need not bother about closing env.
492 if (f.series() != INHERIT_SERIES) {
495 env = true; // Size change need not bother about closing env.
497 if (f.shape() != INHERIT_SHAPE) {
500 env = true; // Size change need not bother about closing env.
502 if (f.color() != Color_inherit && f.color() != Color_ignore && f.color() != Color_none) {
505 env = true; // Size change need not bother about closing env.
507 if (f.emph() == FONT_ON) {
510 env = true; // Size change need not bother about closing env.
512 if (f.noun() == FONT_ON) {
515 env = true; // Size change need not bother about closing env.
517 if (f.size() != FONT_SIZE_INHERIT) {
518 // We only have to close if only size changed
520 if (needPar && !closeLanguage) {
529 if (f.underbar() == FONT_ON) {
532 --runparams.inulemcmd;
534 if (f.strikeout() == FONT_ON) {
537 --runparams.inulemcmd;
539 if (f.xout() == FONT_ON) {
542 --runparams.inulemcmd;
544 if (f.uuline() == FONT_ON) {
547 --runparams.inulemcmd;
549 if (f.uwave() == FONT_ON) {
552 --runparams.inulemcmd;
555 // If the current language is Hebrew, Arabic, or Farsi
556 // the numbers are written Left-to-Right. ArabTeX package
557 // and bidi (polyglossia) reorder the number automatically
558 // but the packages used for Hebrew and Farsi (Arabi) do not.
559 if (!runparams.use_polyglossia
560 && !runparams.pass_thru
561 && bits_.number() == FONT_ON
562 && next.fontInfo().number() != FONT_ON
563 && (language()->lang() == "hebrew"
564 || language()->lang() == "farsi"
565 || language()->lang() == "arabic_arabi")) {
570 if (open_encoding_) {
571 // We need to close the encoding even if it does not change
572 // to do correct environment nesting
573 Encoding const * const ascii = encodings.fromLyXName("ascii");
574 pair<bool, int> const c = switchEncoding(os.os(), bparams,
578 runparams.encoding = ascii;
579 open_encoding_ = false;
583 && language() != base.language() && language() != next.language()
584 && language()->encoding()->package() != Encoding::CJK) {
587 bool const using_begin_end =
588 runparams.use_polyglossia ||
589 !lyxrc.language_command_end.empty();
598 string Font::toString(bool const toggle) const
600 string const lang = (language() == reset_language)
601 ? "reset" : language()->lang();
604 os << "family " << bits_.family() << '\n'
605 << "series " << bits_.series() << '\n'
606 << "shape " << bits_.shape() << '\n'
607 << "size " << bits_.size() << '\n'
608 << "emph " << bits_.emph() << '\n'
609 << "underbar " << bits_.underbar() << '\n'
610 << "strikeout " << bits_.strikeout() << '\n'
611 << "xout " << bits_.xout() << '\n'
612 << "uuline " << bits_.uuline() << '\n'
613 << "uwave " << bits_.uwave() << '\n'
614 << "noun " << bits_.noun() << '\n'
615 << "number " << bits_.number() << '\n'
616 << "color " << bits_.color() << '\n'
617 << "language " << lang << '\n'
618 << "toggleall " << convert<string>(toggle);
623 bool Font::fromString(string const & data, bool & toggle)
625 istringstream is(data);
633 token = lex.getString();
635 if (token.empty() || !lex.next())
638 if (token == "family") {
639 int const next = lex.getInteger();
640 bits_.setFamily(FontFamily(next));
642 } else if (token == "series") {
643 int const next = lex.getInteger();
644 bits_.setSeries(FontSeries(next));
646 } else if (token == "shape") {
647 int const next = lex.getInteger();
648 bits_.setShape(FontShape(next));
650 } else if (token == "size") {
651 int const next = lex.getInteger();
652 bits_.setSize(FontSize(next));
654 } else if (token == "emph" || token == "underbar"
655 || token == "noun" || token == "number"
656 || token == "uuline" || token == "uwave"
657 || token == "strikeout" || token == "xout") {
659 int const next = lex.getInteger();
660 FontState const misc = FontState(next);
664 else if (token == "underbar")
665 bits_.setUnderbar(misc);
666 else if (token == "strikeout")
667 bits_.setStrikeout(misc);
668 else if (token == "xout")
670 else if (token == "uuline")
671 bits_.setUuline(misc);
672 else if (token == "uwave")
673 bits_.setUwave(misc);
674 else if (token == "noun")
676 else if (token == "number")
677 bits_.setNumber(misc);
679 } else if (token == "color") {
680 int const next = lex.getInteger();
681 bits_.setColor(ColorCode(next));
684 } else if (token == "background") {
685 int const next = lex.getInteger();
686 bits_.setBackground(ColorCode(next));
689 } else if (token == "language") {
690 string const next = lex.getString();
691 setLanguage(languages.getLanguage(next));
693 } else if (token == "toggleall") {
694 toggle = lex.getBool();
697 // Unrecognised token
707 void Font::validate(LaTeXFeatures & features) const
709 BufferParams const & bparams = features.bufferParams();
710 Language const * doc_language = bparams.language;
712 if (bits_.noun() == FONT_ON) {
713 LYXERR(Debug::LATEX, "font.noun: " << bits_.noun());
714 features.require("noun");
715 LYXERR(Debug::LATEX, "Noun enabled. Font: " << to_utf8(stateText(0)));
717 if (bits_.underbar() == FONT_ON) {
718 LYXERR(Debug::LATEX, "font.underline: " << bits_.underbar());
719 features.require("ulem");
720 LYXERR(Debug::LATEX, "Underline enabled. Font: " << to_utf8(stateText(0)));
722 if (bits_.strikeout() == FONT_ON) {
723 LYXERR(Debug::LATEX, "font.strikeout: " << bits_.strikeout());
724 features.require("ulem");
725 LYXERR(Debug::LATEX, "Strike out enabled. Font: " << to_utf8(stateText(0)));
727 if (bits_.xout() == FONT_ON) {
728 LYXERR(Debug::LATEX, "font.xout: " << bits_.xout());
729 features.require("ulem");
730 LYXERR(Debug::LATEX, "Cross out enabled. Font: " << to_utf8(stateText(0)));
732 if (bits_.uuline() == FONT_ON) {
733 LYXERR(Debug::LATEX, "font.uuline: " << bits_.uuline());
734 features.require("ulem");
735 LYXERR(Debug::LATEX, "Double underline enabled. Font: " << to_utf8(stateText(0)));
737 if (bits_.uwave() == FONT_ON) {
738 LYXERR(Debug::LATEX, "font.uwave: " << bits_.uwave());
739 features.require("ulem");
740 LYXERR(Debug::LATEX, "Wavy underline enabled. Font: " << to_utf8(stateText(0)));
742 switch (bits_.color()) {
746 // probably we should put here all interface colors used for
747 // font displaying! For now I just add this ones I know of (Jug)
749 case Color_notelabel:
754 case Color_lightgray:
762 features.require("xcolor");
765 features.require("color");
766 LYXERR(Debug::LATEX, "Color enabled. Font: " << to_utf8(stateText(0)));
769 // FIXME: Do something for background and soul package?
771 if (((features.usePolyglossia() && lang_->polyglossia() != doc_language->polyglossia())
772 || (features.useBabel() && lang_->babel() != doc_language->babel())
773 || (doc_language->encoding()->package() == Encoding::CJK && lang_ != doc_language))
774 && lang_ != ignore_language
775 && lang_ != latex_language)
777 features.useLanguage(lang_);
778 LYXERR(Debug::LATEX, "Found language " << lang_->lang());
783 ostream & operator<<(ostream & os, FontState fms)
785 return os << int(fms);
789 ostream & operator<<(ostream & os, FontInfo const & f)
792 << " family " << f.family()
793 << " series " << f.series()
794 << " shape " << f.shape()
795 << " size " << f.size()
796 << " color " << f.color()
797 // FIXME: uncomment this when we support background.
798 //<< " background " << f.background()
799 << " emph " << f.emph()
800 << " underbar " << f.underbar()
801 << " strikeout " << f.strikeout()
802 << " xout " << f.xout()
803 << " uuline " << f.uuline()
804 << " uwave " << f.uwave()
805 << " noun " << f.noun()
806 << " number " << f.number();
810 ostream & operator<<(ostream & os, Font const & font)
812 return os << font.bits_
813 << " lang: " << (font.lang_ ? font.lang_->lang() : 0);