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"
29 #include "support/lassert.h"
30 #include "support/convert.h"
31 #include "support/debug.h"
32 #include "support/gettext.h"
33 #include "support/lstrings.h"
38 using namespace lyx::support;
43 // Strings used to read and write .lyx format files
45 // These are defined in FontInfo.cpp
46 extern char const * LyXFamilyNames[NUM_FAMILIES + 2];
47 extern char const * LyXSeriesNames[NUM_SERIES + 2];
48 extern char const * LyXShapeNames[NUM_SHAPE + 2];
49 extern char const * LyXSizeNames[NUM_SIZE + 4];
50 extern char const * LyXMiscNames[5];
58 char const * GUIFamilyNames[NUM_FAMILIES + 2 /* default & error */] =
59 { N_("Roman"), N_("Sans Serif"), N_("Typewriter"), N_("Symbol"),
60 "cmr", "cmsy", "cmm", "cmex", "msa", "msb", "eufrak", "rsfs", "stmry",
61 "wasy", "esint", N_("Inherit"), N_("Ignore") };
63 char const * GUISeriesNames[NUM_SERIES + 2 /* default & error */] =
64 { N_("Medium"), N_("Bold"), N_("Inherit"), N_("Ignore") };
66 char const * GUIShapeNames[NUM_SHAPE + 2 /* default & error */] =
67 { N_("Upright"), N_("Italic"), N_("Slanted"), N_("Smallcaps"), N_("Inherit"),
70 char const * GUISizeNames[NUM_SIZE + 4 /* increase, decrease, default & error */] =
71 { N_("Tiny"), N_("Smallest"), N_("Smaller"), N_("Small"), N_("Normal"), N_("Large"),
72 N_("Larger"), N_("Largest"), N_("Huge"), N_("Huger"), N_("Increase"), N_("Decrease"),
73 N_("Inherit"), N_("Ignore") };
75 char const * GUIMiscNames[5] =
76 { N_("Off"), N_("On"), N_("Toggle"), N_("Inherit"), N_("Ignore") };
79 // Strings used to write LaTeX files
81 char const * LaTeXFamilyNames[NUM_FAMILIES + 2] =
82 { "textrm", "textsf", "texttt", "error1", "error2", "error3", "error4",
83 "error5", "error6", "error7", "error8", "error9", "error10", "error11",
84 "error12", "error13" };
86 char const * LaTeXSeriesNames[NUM_SERIES + 2] =
87 { "textmd", "textbf", "error4", "error5" };
89 char const * LaTeXShapeNames[NUM_SHAPE + 2] =
90 { "textup", "textit", "textsl", "textsc", "error6", "error7" };
92 char const * LaTeXSizeNames[NUM_SIZE + 4] =
93 { "tiny", "scriptsize", "footnotesize", "small", "normalsize", "large",
94 "Large", "LARGE", "huge", "Huge", "error8", "error9", "error10", "error11" };
99 Font::Font(FontInfo bits, Language const * l)
100 : bits_(bits), lang_(l), open_encoding_(false)
103 lang_ = default_language;
107 bool Font::isRightToLeft() const
109 return lang_->rightToLeft();
113 bool Font::isVisibleRightToLeft() const
115 return (lang_->rightToLeft() &&
116 bits_.number() != FONT_ON);
120 void Font::setLanguage(Language const * l)
126 /// Updates font settings according to request
127 void Font::update(Font const & newfont,
128 Language const * document_language,
131 bits_.update(newfont.fontInfo(), toggleall);
133 if (newfont.language() == language() && toggleall)
134 if (language() == document_language)
135 setLanguage(default_language);
137 setLanguage(document_language);
138 else if (newfont.language() == reset_language)
139 setLanguage(document_language);
140 else if (newfont.language() != ignore_language)
141 setLanguage(newfont.language());
145 docstring const stateText(FontInfo const & f)
148 if (f.family() != INHERIT_FAMILY)
149 os << _(GUIFamilyNames[f.family()]) << ", ";
150 if (f.series() != INHERIT_SERIES)
151 os << _(GUISeriesNames[f.series()]) << ", ";
152 if (f.shape() != INHERIT_SHAPE)
153 os << _(GUIShapeNames[f.shape()]) << ", ";
154 if (f.size() != FONT_SIZE_INHERIT)
155 os << _(GUISizeNames[f.size()]) << ", ";
156 if (f.color() != Color_inherit)
157 os << lcolor.getGUIName(f.color()) << ", ";
158 // FIXME: uncomment this when we support background.
159 //if (f.background() != Color_inherit)
160 // os << lcolor.getGUIName(f.background()) << ", ";
161 if (f.emph() != FONT_INHERIT)
162 os << bformat(_("Emphasis %1$s, "),
163 _(GUIMiscNames[f.emph()]));
164 if (f.underbar() != FONT_INHERIT)
165 os << bformat(_("Underline %1$s, "),
166 _(GUIMiscNames[f.underbar()]));
167 if (f.strikeout() != FONT_INHERIT)
168 os << bformat(_("Strikeout %1$s, "),
169 _(GUIMiscNames[f.strikeout()]));
170 if (f.uuline() != FONT_INHERIT)
171 os << bformat(_("Double underline %1$s, "),
172 _(GUIMiscNames[f.uuline()]));
173 if (f.uwave() != FONT_INHERIT)
174 os << bformat(_("Wavy underline %1$s, "),
175 _(GUIMiscNames[f.uwave()]));
176 if (f.noun() != FONT_INHERIT)
177 os << bformat(_("Noun %1$s, "),
178 _(GUIMiscNames[f.noun()]));
179 if (f == inherit_font)
180 os << _("Default") << ", ";
186 docstring const Font::stateText(BufferParams * params) const
189 os << lyx::stateText(bits_);
190 if (!params || (language() != params->language))
191 os << bformat(_("Language: %1$s, "),
192 _(language()->display()));
193 if (bits_.number() != FONT_OFF)
194 os << " " << bformat(_("Number %1$s"),
195 _(GUIMiscNames[bits_.number()]));
196 return rtrim(os.str(), ", ");
200 // Returns size in latex format
201 string const Font::latexSize() const
203 return LaTeXSizeNames[bits_.size()];
207 /// Writes the changes from this font to orgfont in .lyx format in file
208 void Font::lyxWriteChanges(Font const & orgfont,
212 if (orgfont.fontInfo().family() != bits_.family())
213 os << "\\family " << LyXFamilyNames[bits_.family()] << "\n";
214 if (orgfont.fontInfo().series() != bits_.series())
215 os << "\\series " << LyXSeriesNames[bits_.series()] << "\n";
216 if (orgfont.fontInfo().shape() != bits_.shape())
217 os << "\\shape " << LyXShapeNames[bits_.shape()] << "\n";
218 if (orgfont.fontInfo().size() != bits_.size())
219 os << "\\size " << LyXSizeNames[bits_.size()] << "\n";
220 if (orgfont.fontInfo().emph() != bits_.emph())
221 os << "\\emph " << LyXMiscNames[bits_.emph()] << "\n";
222 if (orgfont.fontInfo().number() != bits_.number())
223 os << "\\numeric " << LyXMiscNames[bits_.number()] << "\n";
224 if (orgfont.fontInfo().underbar() != bits_.underbar()) {
225 // This is only for backwards compatibility
226 switch (bits_.underbar()) {
227 case FONT_OFF: os << "\\bar no\n"; break;
228 case FONT_ON: os << "\\bar under\n"; break;
229 case FONT_TOGGLE: lyxerr << "Font::lyxWriteFontChanges: "
230 "FONT_TOGGLE should not appear here!"
233 case FONT_INHERIT: os << "\\bar default\n"; break;
234 case FONT_IGNORE: lyxerr << "Font::lyxWriteFontChanges: "
235 "IGNORE should not appear here!"
240 if (orgfont.fontInfo().strikeout() != bits_.strikeout()) {
241 os << "\\strikeout " << LyXMiscNames[bits_.strikeout()] << "\n";
243 if (orgfont.fontInfo().uuline() != bits_.uuline()) {
244 os << "\\uuline " << LyXMiscNames[bits_.uuline()] << "\n";
246 if (orgfont.fontInfo().uwave() != bits_.uwave()) {
247 os << "\\uwave " << LyXMiscNames[bits_.uwave()] << "\n";
249 if (orgfont.fontInfo().noun() != bits_.noun()) {
250 os << "\\noun " << LyXMiscNames[bits_.noun()] << "\n";
252 if (orgfont.fontInfo().color() != bits_.color())
253 os << "\\color " << lcolor.getLyXName(bits_.color()) << '\n';
254 // FIXME: uncomment this when we support background.
255 //if (orgfont.fontInfo().background() != bits_.background())
256 // os << "\\color " << lcolor.getLyXName(bits_.background()) << '\n';
257 if (orgfont.language() != language() &&
258 language() != latex_language) {
260 os << "\\lang " << language()->lang() << "\n";
262 os << "\\lang unknown\n";
267 /// Writes the head of the LaTeX needed to impose this font
268 // Returns number of chars written.
269 int Font::latexWriteStartChanges(odocstream & os, BufferParams const & bparams,
270 OutputParams const & runparams,
272 Font const & prev) const
278 // polyglossia or babel?
279 if (runparams.use_polyglossia
280 && language()->lang() != base.language()->lang()
281 && language() != prev.language()) {
282 if (!language()->polyglossia().empty()) {
283 string tmp = "\\text" + language()->polyglossia();
284 if (!language()->polyglossiaOpts().empty())
285 tmp += "[" + language()->polyglossiaOpts() + "]";
287 os << from_ascii(tmp);
288 count += tmp.length();
289 } else if (language()->encoding()->package() != Encoding::CJK) {
293 } else if (language()->babel() != base.language()->babel() &&
294 language() != prev.language()) {
295 if (language()->lang() == "farsi") {
298 } else if (!isRightToLeft() &&
299 base.language()->lang() == "farsi") {
302 } else if (language()->lang() == "arabic_arabi") {
305 } else if (!isRightToLeft() &&
306 base.language()->lang() == "arabic_arabi") {
309 // currently the remaining RTL languages are arabic_arabtex and hebrew
310 } else if (isRightToLeft() != prev.isRightToLeft()) {
311 if (isRightToLeft()) {
318 } else if (!language()->babel().empty()) {
320 subst(lyxrc.language_command_local,
321 "$$lang", language()->babel());
322 os << from_ascii(tmp);
323 count += tmp.length();
324 } else if (language()->encoding()->package() != Encoding::CJK) {
330 // FIXME: skip this for XeTeX/LuaTeX? With nonTeXfonts or always?
331 if (language()->encoding()->package() == Encoding::CJK) {
332 pair<bool, int> const c = switchEncoding(os, bparams,
333 runparams, *(language()->encoding()));
335 open_encoding_ = true;
337 runparams.encoding = language()->encoding();
341 // If the current language is Hebrew, Arabic, or Farsi
342 // the numbers are written Left-to-Right. ArabTeX package
343 // reorders the number automatically but the packages used
344 // for Hebrew and Farsi (Arabi) do not.
345 if (bits_.number() == FONT_ON && prev.fontInfo().number() != FONT_ON
346 && (language()->lang() == "hebrew"
347 || language()->lang() == "farsi"
348 || language()->lang() == "arabic_arabi")) {
354 f.reduce(base.bits_);
356 if (f.family() != INHERIT_FAMILY) {
358 << LaTeXFamilyNames[f.family()]
360 count += strlen(LaTeXFamilyNames[f.family()]) + 2;
361 env = true; //We have opened a new environment
363 if (f.series() != INHERIT_SERIES) {
365 << LaTeXSeriesNames[f.series()]
367 count += strlen(LaTeXSeriesNames[f.series()]) + 2;
368 env = true; //We have opened a new environment
370 if (f.shape() != INHERIT_SHAPE) {
372 << LaTeXShapeNames[f.shape()]
374 count += strlen(LaTeXShapeNames[f.shape()]) + 2;
375 env = true; //We have opened a new environment
377 if (f.color() != Color_inherit && f.color() != Color_ignore) {
379 << from_ascii(lcolor.getLaTeXName(f.color()))
381 count += lcolor.getLaTeXName(f.color()).length() + 13;
382 env = true; //We have opened a new environment
384 // FIXME: uncomment this when we support background.
386 if (f.background() != Color_inherit && f.background() != Color_ignore) {
388 << from_ascii(lcolor.getLaTeXName(f.background()))
390 count += lcolor.getLaTeXName(f.background()).length() + 13;
391 env = true; //We have opened a new environment
394 if (f.emph() == FONT_ON) {
397 env = true; //We have opened a new environment
399 // \noun{} is a LyX special macro
400 if (f.noun() == FONT_ON) {
403 env = true; //We have opened a new environment
405 if (f.size() != FONT_SIZE_INHERIT) {
406 // If we didn't open an environment above, we open one here
412 << LaTeXSizeNames[f.size()]
414 count += strlen(LaTeXSizeNames[f.size()]) + 3;
416 // The ulem commands need to be on the deepest nesting level
417 // because ulem puts every nested group or macro in a box,
418 // which prevents linebreaks (#8424, #8733)
419 if (f.underbar() == FONT_ON) {
422 ++runparams.inulemcmd;
424 if (f.strikeout() == FONT_ON) {
427 ++runparams.inulemcmd;
429 if (f.uuline() == FONT_ON) {
432 ++runparams.inulemcmd;
434 if (f.uwave() == FONT_ON) {
437 ++runparams.inulemcmd;
443 /// Writes ending block of LaTeX needed to close use of this font
444 // Returns number of chars written
445 // This one corresponds to latexWriteStartChanges(). (Asger)
446 int Font::latexWriteEndChanges(otexstream & os, BufferParams const & bparams,
447 OutputParams const & runparams,
450 bool const & closeLanguage) const
455 // reduce the current font to changes against the base
456 // font (of the layout). We use a temporary for this to
457 // avoid changing this font instance, as that would break
459 f.reduce(base.bits_);
461 if (f.family() != INHERIT_FAMILY) {
464 env = true; // Size change need not bother about closing env.
466 if (f.series() != INHERIT_SERIES) {
469 env = true; // Size change need not bother about closing env.
471 if (f.shape() != INHERIT_SHAPE) {
474 env = true; // Size change need not bother about closing env.
476 if (f.color() != Color_inherit && f.color() != Color_ignore) {
479 env = true; // Size change need not bother about closing env.
481 if (f.emph() == FONT_ON) {
484 env = true; // Size change need not bother about closing env.
486 if (f.noun() == FONT_ON) {
489 env = true; // Size change need not bother about closing env.
491 if (f.size() != FONT_SIZE_INHERIT) {
492 // We only have to close if only size changed
498 if (f.underbar() == FONT_ON) {
501 --runparams.inulemcmd;
503 if (f.strikeout() == FONT_ON) {
506 --runparams.inulemcmd;
508 if (f.uuline() == FONT_ON) {
511 --runparams.inulemcmd;
513 if (f.uwave() == FONT_ON) {
516 --runparams.inulemcmd;
519 // If the current language is Hebrew, Arabic, or Farsi
520 // the numbers are written Left-to-Right. ArabTeX package
521 // reorders the number automatically but the packages used
522 // for Hebrew and Farsi (Arabi) do not.
523 if (bits_.number() == FONT_ON && next.fontInfo().number() != FONT_ON
524 && (language()->lang() == "hebrew"
525 || language()->lang() == "farsi"
526 || language()->lang() == "arabic_arabi")) {
531 if (open_encoding_) {
532 // We need to close the encoding even if it does not change
533 // to do correct environment nesting
534 Encoding const * const ascii = encodings.fromLyXName("ascii");
535 pair<bool, int> const c = switchEncoding(os.os(), bparams,
539 runparams.encoding = ascii;
540 open_encoding_ = false;
544 && language() != base.language() && language() != next.language()
545 && language()->encoding()->package() != Encoding::CJK) {
554 string Font::toString(bool const toggle) const
556 string const lang = (language() == reset_language)
557 ? "reset" : language()->lang();
560 os << "family " << bits_.family() << '\n'
561 << "series " << bits_.series() << '\n'
562 << "shape " << bits_.shape() << '\n'
563 << "size " << bits_.size() << '\n'
564 << "emph " << bits_.emph() << '\n'
565 << "underbar " << bits_.underbar() << '\n'
566 << "strikeout " << bits_.strikeout() << '\n'
567 << "uuline " << bits_.uuline() << '\n'
568 << "uwave " << bits_.uwave() << '\n'
569 << "noun " << bits_.noun() << '\n'
570 << "number " << bits_.number() << '\n'
571 << "color " << bits_.color() << '\n'
572 << "language " << lang << '\n'
573 << "toggleall " << convert<string>(toggle);
578 bool Font::fromString(string const & data, bool & toggle)
580 istringstream is(data);
588 token = lex.getString();
590 if (token.empty() || !lex.next())
593 if (token == "family") {
594 int const next = lex.getInteger();
595 bits_.setFamily(FontFamily(next));
597 } else if (token == "series") {
598 int const next = lex.getInteger();
599 bits_.setSeries(FontSeries(next));
601 } else if (token == "shape") {
602 int const next = lex.getInteger();
603 bits_.setShape(FontShape(next));
605 } else if (token == "size") {
606 int const next = lex.getInteger();
607 bits_.setSize(FontSize(next));
609 } else if (token == "emph" || token == "underbar" ||
610 token == "noun" || token == "number" ||
611 token == "uuline" || token == "uwave" ||
612 token == "strikeout") {
614 int const next = lex.getInteger();
615 FontState const misc = FontState(next);
619 else if (token == "underbar")
620 bits_.setUnderbar(misc);
621 else if (token == "strikeout")
622 bits_.setStrikeout(misc);
623 else if (token == "uuline")
624 bits_.setUuline(misc);
625 else if (token == "uwave")
626 bits_.setUwave(misc);
627 else if (token == "noun")
629 else if (token == "number")
630 bits_.setNumber(misc);
632 } else if (token == "color") {
633 int const next = lex.getInteger();
634 bits_.setColor(ColorCode(next));
637 } else if (token == "background") {
638 int const next = lex.getInteger();
639 bits_.setBackground(ColorCode(next));
642 } else if (token == "language") {
643 string const next = lex.getString();
644 setLanguage(languages.getLanguage(next));
646 } else if (token == "toggleall") {
647 toggle = lex.getBool();
650 // Unrecognised token
660 void Font::validate(LaTeXFeatures & features) const
662 BufferParams const & bparams = features.bufferParams();
663 Language const * doc_language = bparams.language;
665 if (bits_.noun() == FONT_ON) {
666 LYXERR(Debug::LATEX, "font.noun: " << bits_.noun());
667 features.require("noun");
668 LYXERR(Debug::LATEX, "Noun enabled. Font: " << to_utf8(stateText(0)));
670 if (bits_.underbar() == FONT_ON) {
671 LYXERR(Debug::LATEX, "font.underline: " << bits_.underbar());
672 features.require("ulem");
673 LYXERR(Debug::LATEX, "Underline enabled. Font: " << to_utf8(stateText(0)));
675 if (bits_.strikeout() == FONT_ON) {
676 LYXERR(Debug::LATEX, "font.strikeout: " << bits_.strikeout());
677 features.require("ulem");
678 LYXERR(Debug::LATEX, "Strikeout enabled. Font: " << to_utf8(stateText(0)));
680 if (bits_.uuline() == FONT_ON) {
681 LYXERR(Debug::LATEX, "font.uuline: " << bits_.uuline());
682 features.require("ulem");
683 LYXERR(Debug::LATEX, "Double underline enabled. Font: " << to_utf8(stateText(0)));
685 if (bits_.uwave() == FONT_ON) {
686 LYXERR(Debug::LATEX, "font.uwave: " << bits_.uwave());
687 features.require("ulem");
688 LYXERR(Debug::LATEX, "Wavy underline enabled. Font: " << to_utf8(stateText(0)));
690 switch (bits_.color()) {
694 // probably we should put here all interface colors used for
695 // font displaying! For now I just add this ones I know of (Jug)
697 case Color_notelabel:
702 case Color_lightgray:
710 features.require("xcolor");
713 features.require("color");
714 LYXERR(Debug::LATEX, "Color enabled. Font: " << to_utf8(stateText(0)));
717 // FIXME: Do something for background and soul package?
719 if (((features.usePolyglossia() && lang_->polyglossia() != doc_language->polyglossia())
720 || (features.useBabel() && lang_->babel() != doc_language->babel())
721 || (doc_language->encoding()->package() == Encoding::CJK && lang_ != doc_language))
722 && lang_ != ignore_language
723 && lang_ != latex_language)
725 features.useLanguage(lang_);
726 LYXERR(Debug::LATEX, "Found language " << lang_->lang());
731 ostream & operator<<(ostream & os, FontState fms)
733 return os << int(fms);
737 ostream & operator<<(ostream & os, FontInfo const & f)
740 << " family " << f.family()
741 << " series " << f.series()
742 << " shape " << f.shape()
743 << " size " << f.size()
744 << " color " << f.color()
745 // FIXME: uncomment this when we support background.
746 //<< " background " << f.background()
747 << " emph " << f.emph()
748 << " underbar " << f.underbar()
749 << " strikeout " << f.strikeout()
750 << " uuline " << f.uuline()
751 << " uwave " << f.uwave()
752 << " noun " << f.noun()
753 << " number " << f.number();
757 ostream & operator<<(ostream & os, Font const & font)
759 return os << font.bits_
760 << " lang: " << (font.lang_ ? font.lang_->lang() : 0);