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[4];
48 extern char const * LyXShapeNames[6];
49 extern char const * LyXSizeNames[14];
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", "wasy", "esint",
61 N_("Inherit"), N_("Ignore") };
63 char const * GUISeriesNames[4] =
64 { N_("Medium"), N_("Bold"), N_("Inherit"), N_("Ignore") };
66 char const * GUIShapeNames[6] =
67 { N_("Upright"), N_("Italic"), N_("Slanted"), N_("Smallcaps"), N_("Inherit"),
70 char const * GUISizeNames[14] =
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[6] =
82 { "textrm", "textsf", "texttt", "error1", "error2", "error3" };
84 char const * LaTeXSeriesNames[4] =
85 { "textmd", "textbf", "error4", "error5" };
87 char const * LaTeXShapeNames[6] =
88 { "textup", "textit", "textsl", "textsc", "error6", "error7" };
90 char const * LaTeXSizeNames[14] =
91 { "tiny", "scriptsize", "footnotesize", "small", "normalsize", "large",
92 "Large", "LARGE", "huge", "Huge", "error8", "error9", "error10", "error11" };
97 Font::Font(FontInfo bits, Language const * l)
98 : bits_(bits), lang_(l), open_encoding_(false)
101 lang_ = default_language;
105 bool Font::isRightToLeft() const
107 return lang_->rightToLeft();
111 bool Font::isVisibleRightToLeft() const
113 return (lang_->rightToLeft() &&
114 bits_.number() != FONT_ON);
118 void Font::setLanguage(Language const * l)
124 /// Updates font settings according to request
125 void Font::update(Font const & newfont,
126 Language const * document_language,
129 bits_.update(newfont.fontInfo(), toggleall);
131 if (newfont.language() == language() && toggleall)
132 if (language() == document_language)
133 setLanguage(default_language);
135 setLanguage(document_language);
136 else if (newfont.language() == reset_language)
137 setLanguage(document_language);
138 else if (newfont.language() != ignore_language)
139 setLanguage(newfont.language());
143 docstring const stateText(FontInfo const & f)
146 if (f.family() != INHERIT_FAMILY)
147 os << _(GUIFamilyNames[f.family()]) << ", ";
148 if (f.series() != INHERIT_SERIES)
149 os << _(GUISeriesNames[f.series()]) << ", ";
150 if (f.shape() != INHERIT_SHAPE)
151 os << _(GUIShapeNames[f.shape()]) << ", ";
152 if (f.size() != FONT_SIZE_INHERIT)
153 os << _(GUISizeNames[f.size()]) << ", ";
154 if (f.color() != Color_inherit)
155 os << lcolor.getGUIName(f.color()) << ", ";
156 // FIXME: uncomment this when we support background.
157 //if (f.background() != Color_inherit)
158 // os << lcolor.getGUIName(f.background()) << ", ";
159 if (f.emph() != FONT_INHERIT)
160 os << bformat(_("Emphasis %1$s, "),
161 _(GUIMiscNames[f.emph()]));
162 if (f.underbar() != FONT_INHERIT)
163 os << bformat(_("Underline %1$s, "),
164 _(GUIMiscNames[f.underbar()]));
165 if (f.strikeout() != FONT_INHERIT)
166 os << bformat(_("Strikeout %1$s, "),
167 _(GUIMiscNames[f.strikeout()]));
168 if (f.uuline() != FONT_INHERIT)
169 os << bformat(_("Double underline %1$s, "),
170 _(GUIMiscNames[f.uuline()]));
171 if (f.uwave() != FONT_INHERIT)
172 os << bformat(_("Wavy underline %1$s, "),
173 _(GUIMiscNames[f.uwave()]));
174 if (f.noun() != FONT_INHERIT)
175 os << bformat(_("Noun %1$s, "),
176 _(GUIMiscNames[f.noun()]));
177 if (f == inherit_font)
178 os << _("Default") << ", ";
184 docstring const Font::stateText(BufferParams * params) const
187 os << lyx::stateText(bits_);
188 if (!params || (language() != params->language))
189 os << bformat(_("Language: %1$s, "),
190 _(language()->display()));
191 if (bits_.number() != FONT_OFF)
192 os << bformat(_(" Number %1$s"),
193 _(GUIMiscNames[bits_.number()]));
194 return rtrim(os.str(), ", ");
198 // Returns size in latex format
199 string const Font::latexSize() const
201 return LaTeXSizeNames[bits_.size()];
205 /// Writes the changes from this font to orgfont in .lyx format in file
206 void Font::lyxWriteChanges(Font const & orgfont,
210 if (orgfont.fontInfo().family() != bits_.family())
211 os << "\\family " << LyXFamilyNames[bits_.family()] << "\n";
212 if (orgfont.fontInfo().series() != bits_.series())
213 os << "\\series " << LyXSeriesNames[bits_.series()] << "\n";
214 if (orgfont.fontInfo().shape() != bits_.shape())
215 os << "\\shape " << LyXShapeNames[bits_.shape()] << "\n";
216 if (orgfont.fontInfo().size() != bits_.size())
217 os << "\\size " << LyXSizeNames[bits_.size()] << "\n";
218 if (orgfont.fontInfo().emph() != bits_.emph())
219 os << "\\emph " << LyXMiscNames[bits_.emph()] << "\n";
220 if (orgfont.fontInfo().number() != bits_.number())
221 os << "\\numeric " << LyXMiscNames[bits_.number()] << "\n";
222 if (orgfont.fontInfo().underbar() != bits_.underbar()) {
223 // This is only for backwards compatibility
224 switch (bits_.underbar()) {
225 case FONT_OFF: os << "\\bar no\n"; break;
226 case FONT_ON: os << "\\bar under\n"; break;
227 case FONT_TOGGLE: lyxerr << "Font::lyxWriteFontChanges: "
228 "FONT_TOGGLE should not appear here!"
231 case FONT_INHERIT: os << "\\bar default\n"; break;
232 case FONT_IGNORE: lyxerr << "Font::lyxWriteFontChanges: "
233 "IGNORE should not appear here!"
238 if (orgfont.fontInfo().strikeout() != bits_.strikeout()) {
239 os << "\\strikeout " << LyXMiscNames[bits_.strikeout()] << "\n";
241 if (orgfont.fontInfo().uuline() != bits_.uuline()) {
242 os << "\\uuline " << LyXMiscNames[bits_.uuline()] << "\n";
244 if (orgfont.fontInfo().uwave() != bits_.uwave()) {
245 os << "\\uwave " << LyXMiscNames[bits_.uwave()] << "\n";
247 if (orgfont.fontInfo().noun() != bits_.noun()) {
248 os << "\\noun " << LyXMiscNames[bits_.noun()] << "\n";
250 if (orgfont.fontInfo().color() != bits_.color())
251 os << "\\color " << lcolor.getLyXName(bits_.color()) << '\n';
252 // FIXME: uncomment this when we support background.
253 //if (orgfont.fontInfo().background() != bits_.background())
254 // os << "\\color " << lcolor.getLyXName(bits_.background()) << '\n';
255 if (orgfont.language() != language() &&
256 language() != latex_language) {
258 os << "\\lang " << language()->lang() << "\n";
260 os << "\\lang unknown\n";
265 /// Writes the head of the LaTeX needed to impose this font
266 // Returns number of chars written.
267 int Font::latexWriteStartChanges(odocstream & os, BufferParams const & bparams,
268 OutputParams const & runparams,
270 Font const & prev) const
276 // polyglossia or babel?
277 if (runparams.use_polyglossia) {
278 if (!language()->polyglossia().empty()) {
279 string tmp = "\\text" + language()->polyglossia();
280 if (!language()->polyglossiaOpts().empty())
281 tmp += "[" + language()->polyglossiaOpts() + "]";
283 os << from_ascii(tmp);
284 count += tmp.length();
286 } else if (language()->babel() != base.language()->babel() &&
287 language() != prev.language()) {
288 if (language()->lang() == "farsi") {
291 } else if (!isRightToLeft() &&
292 base.language()->lang() == "farsi") {
295 } else if (language()->lang() == "arabic_arabi") {
298 } else if (!isRightToLeft() &&
299 base.language()->lang() == "arabic_arabi") {
302 // currently the remaining RTL languages are arabic_arabtex and hebrew
303 } else if (isRightToLeft() != prev.isRightToLeft()) {
304 if (isRightToLeft()) {
311 } else if (!language()->babel().empty()) {
313 subst(lyxrc.language_command_local,
314 "$$lang", language()->babel());
315 os << from_ascii(tmp);
316 count += tmp.length();
323 if (language()->encoding()->package() == Encoding::CJK) {
324 pair<bool, int> const c = switchEncoding(os, bparams,
325 runparams, *(language()->encoding()));
327 open_encoding_ = true;
329 runparams.encoding = language()->encoding();
333 // When the current language is Hebrew, Arabic, or Farsi
334 // the numbers are written Left-to-Right. ArabTeX package
335 // reorders the number automatically but the packages used
336 // for Hebrew and Farsi (Arabi) do not.
337 if (bits_.number() == FONT_ON && prev.fontInfo().number() != FONT_ON
338 && (language()->lang() == "hebrew"
339 || language()->lang() == "farsi"
340 || language()->lang() == "arabic_arabi")) {
346 f.reduce(base.bits_);
348 if (f.family() != INHERIT_FAMILY) {
350 << LaTeXFamilyNames[f.family()]
352 count += strlen(LaTeXFamilyNames[f.family()]) + 2;
353 env = true; //We have opened a new environment
355 if (f.series() != INHERIT_SERIES) {
357 << LaTeXSeriesNames[f.series()]
359 count += strlen(LaTeXSeriesNames[f.series()]) + 2;
360 env = true; //We have opened a new environment
362 if (f.shape() != INHERIT_SHAPE) {
364 << LaTeXShapeNames[f.shape()]
366 count += strlen(LaTeXShapeNames[f.shape()]) + 2;
367 env = true; //We have opened a new environment
369 if (f.color() != Color_inherit && f.color() != Color_ignore) {
371 << from_ascii(lcolor.getLaTeXName(f.color()))
373 count += lcolor.getLaTeXName(f.color()).length() + 13;
374 env = true; //We have opened a new environment
376 // FIXME: uncomment this when we support background.
378 if (f.background() != Color_inherit && f.background() != Color_ignore) {
380 << from_ascii(lcolor.getLaTeXName(f.background()))
382 count += lcolor.getLaTeXName(f.background()).length() + 13;
383 env = true; //We have opened a new environment
386 if (f.emph() == FONT_ON) {
389 env = true; //We have opened a new environment
391 if (f.underbar() == FONT_ON) {
394 runparams.inulemcmd = true;
395 env = true; //We have opened a new environment
397 if (f.strikeout() == FONT_ON) {
400 runparams.inulemcmd = true;
401 env = true; //We have opened a new environment
403 if (f.uuline() == FONT_ON) {
406 runparams.inulemcmd = true;
407 env = true; //We have opened a new environment
409 if (f.uwave() == FONT_ON) {
412 runparams.inulemcmd = true;
413 env = true; //We have opened a new environment
415 // \noun{} is a LyX special macro
416 if (f.noun() == FONT_ON) {
419 env = true; //We have opened a new environment
421 if (f.size() != FONT_SIZE_INHERIT) {
422 // If we didn't open an environment above, we open one here
428 << LaTeXSizeNames[f.size()]
430 count += strlen(LaTeXSizeNames[f.size()]) + 2;
436 /// Writes ending block of LaTeX needed to close use of this font
437 // Returns number of chars written
438 // This one corresponds to latexWriteStartChanges(). (Asger)
439 int Font::latexWriteEndChanges(odocstream & os, BufferParams const & bparams,
440 OutputParams const & runparams,
443 bool const & closeLanguage) const
448 // reduce the current font to changes against the base
449 // font (of the layout). We use a temporary for this to
450 // avoid changing this font instance, as that would break
452 f.reduce(base.bits_);
454 if (f.family() != INHERIT_FAMILY) {
457 env = true; // Size change need not bother about closing env.
459 if (f.series() != INHERIT_SERIES) {
462 env = true; // Size change need not bother about closing env.
464 if (f.shape() != INHERIT_SHAPE) {
467 env = true; // Size change need not bother about closing env.
469 if (f.color() != Color_inherit && f.color() != Color_ignore) {
472 env = true; // Size change need not bother about closing env.
474 if (f.emph() == FONT_ON) {
477 env = true; // Size change need not bother about closing env.
479 if (f.underbar() == FONT_ON) {
482 runparams.inulemcmd = false;
483 env = true; // Size change need not bother about closing env.
485 if (f.strikeout() == FONT_ON) {
488 runparams.inulemcmd = false;
489 env = true; // Size change need not bother about closing env.
491 if (f.uuline() == FONT_ON) {
494 runparams.inulemcmd = false;
495 env = true; // Size change need not bother about closing env.
497 if (f.uwave() == FONT_ON) {
500 runparams.inulemcmd = false;
501 env = true; // Size change need not bother about closing env.
503 if (f.noun() == FONT_ON) {
506 env = true; // Size change need not bother about closing env.
508 if (f.size() != FONT_SIZE_INHERIT) {
509 // We only have to close if only size changed
516 // When the current language is Hebrew, Arabic, or Farsi
517 // the numbers are written Left-to-Right. ArabTeX package
518 // reorders the number automatically but the packages used
519 // for Hebrew and Farsi (Arabi) do not.
520 if (bits_.number() == FONT_ON && next.fontInfo().number() != FONT_ON
521 && (language()->lang() == "hebrew"
522 || language()->lang() == "farsi"
523 || language()->lang() == "arabic_arabi")) {
528 if (open_encoding_) {
529 // We need to close the encoding even if it does not change
530 // to do correct environment nesting
531 Encoding const * const ascii = encodings.fromLyXName("ascii");
532 pair<bool, int> const c = switchEncoding(os, bparams,
534 LASSERT(c.first, /**/);
536 runparams.encoding = ascii;
537 open_encoding_ = false;
541 language() != base.language() && language() != next.language()) {
550 string Font::toString(bool const toggle) const
552 string const lang = (language() == reset_language)
553 ? "reset" : language()->lang();
556 os << "family " << bits_.family() << '\n'
557 << "series " << bits_.series() << '\n'
558 << "shape " << bits_.shape() << '\n'
559 << "size " << bits_.size() << '\n'
560 << "emph " << bits_.emph() << '\n'
561 << "underbar " << bits_.underbar() << '\n'
562 << "strikeout " << bits_.strikeout() << '\n'
563 << "uuline " << bits_.uuline() << '\n'
564 << "uwave " << bits_.uwave() << '\n'
565 << "noun " << bits_.noun() << '\n'
566 << "number " << bits_.number() << '\n'
567 << "color " << bits_.color() << '\n'
568 << "language " << lang << '\n'
569 << "toggleall " << convert<string>(toggle);
574 bool Font::fromString(string const & data, bool & toggle)
576 istringstream is(data);
584 token = lex.getString();
586 if (token.empty() || !lex.next())
589 if (token == "family") {
590 int const next = lex.getInteger();
591 bits_.setFamily(FontFamily(next));
593 } else if (token == "series") {
594 int const next = lex.getInteger();
595 bits_.setSeries(FontSeries(next));
597 } else if (token == "shape") {
598 int const next = lex.getInteger();
599 bits_.setShape(FontShape(next));
601 } else if (token == "size") {
602 int const next = lex.getInteger();
603 bits_.setSize(FontSize(next));
605 } else if (token == "emph" || token == "underbar" ||
606 token == "noun" || token == "number" ||
607 token == "uuline" || token == "uwave" ||
608 token == "strikeout") {
610 int const next = lex.getInteger();
611 FontState const misc = FontState(next);
615 else if (token == "underbar")
616 bits_.setUnderbar(misc);
617 else if (token == "strikeout")
618 bits_.setStrikeout(misc);
619 else if (token == "uuline")
620 bits_.setUuline(misc);
621 else if (token == "uwave")
622 bits_.setUwave(misc);
623 else if (token == "noun")
625 else if (token == "number")
626 bits_.setNumber(misc);
628 } else if (token == "color") {
629 int const next = lex.getInteger();
630 bits_.setColor(ColorCode(next));
633 } else if (token == "background") {
634 int const next = lex.getInteger();
635 bits_.setBackground(ColorCode(next));
638 } else if (token == "language") {
639 string const next = lex.getString();
640 setLanguage(languages.getLanguage(next));
642 } else if (token == "toggleall") {
643 toggle = lex.getBool();
646 // Unrecognised token
656 void Font::validate(LaTeXFeatures & features) const
658 BufferParams const & bparams = features.bufferParams();
659 Language const * doc_language = bparams.language;
661 if (bits_.noun() == FONT_ON) {
662 LYXERR(Debug::LATEX, "font.noun: " << bits_.noun());
663 features.require("noun");
664 LYXERR(Debug::LATEX, "Noun enabled. Font: " << to_utf8(stateText(0)));
666 if (bits_.underbar() == FONT_ON) {
667 LYXERR(Debug::LATEX, "font.underline: " << bits_.underbar());
668 features.require("ulem");
669 LYXERR(Debug::LATEX, "Underline enabled. Font: " << to_utf8(stateText(0)));
671 if (bits_.strikeout() == FONT_ON) {
672 LYXERR(Debug::LATEX, "font.strikeout: " << bits_.strikeout());
673 features.require("ulem");
674 LYXERR(Debug::LATEX, "Strikeout enabled. Font: " << to_utf8(stateText(0)));
676 if (bits_.uuline() == FONT_ON) {
677 LYXERR(Debug::LATEX, "font.uuline: " << bits_.uuline());
678 features.require("ulem");
679 LYXERR(Debug::LATEX, "Double underline enabled. Font: " << to_utf8(stateText(0)));
681 if (bits_.uwave() == FONT_ON) {
682 LYXERR(Debug::LATEX, "font.uwave: " << bits_.uwave());
683 features.require("ulem");
684 LYXERR(Debug::LATEX, "Wavy underline enabled. Font: " << to_utf8(stateText(0)));
686 switch (bits_.color()) {
690 // probably we should put here all interface colors used for
691 // font displaying! For now I just add this ones I know of (Jug)
693 case Color_notelabel:
696 features.require("color");
697 LYXERR(Debug::LATEX, "Color enabled. Font: " << to_utf8(stateText(0)));
700 // FIXME: Do something for background and soul package?
702 if (lang_->babel() != doc_language->babel() &&
703 lang_ != ignore_language &&
704 lang_ != latex_language)
706 features.useLanguage(lang_);
707 LYXERR(Debug::LATEX, "Found language " << lang_->lang());
712 ostream & operator<<(ostream & os, FontState fms)
714 return os << int(fms);
718 ostream & operator<<(ostream & os, FontInfo const & f)
721 << " family " << f.family()
722 << " series " << f.series()
723 << " shape " << f.shape()
724 << " size " << f.size()
725 << " color " << f.color()
726 // FIXME: uncomment this when we support background.
727 //<< " background " << f.background()
728 << " emph " << f.emph()
729 << " underbar " << f.underbar()
730 << " strikeout " << f.strikeout()
731 << " uuline " << f.uuline()
732 << " uwave " << f.uwave()
733 << " noun " << f.noun()
734 << " number " << f.number();
738 ostream & operator<<(ostream & os, Font const & font)
740 return os << font.bits_
741 << " lang: " << (font.lang_ ? font.lang_->lang() : 0);