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 && language()->lang() != base.language()->lang()
279 && language() != prev.language()) {
280 if (!language()->polyglossia().empty()) {
281 string tmp = "\\text" + language()->polyglossia();
282 if (!language()->polyglossiaOpts().empty())
283 tmp += "[" + language()->polyglossiaOpts() + "]";
285 os << from_ascii(tmp);
286 count += tmp.length();
288 } else if (language()->babel() != base.language()->babel() &&
289 language() != prev.language()) {
290 if (language()->lang() == "farsi") {
293 } else if (!isRightToLeft() &&
294 base.language()->lang() == "farsi") {
297 } else if (language()->lang() == "arabic_arabi") {
300 } else if (!isRightToLeft() &&
301 base.language()->lang() == "arabic_arabi") {
304 // currently the remaining RTL languages are arabic_arabtex and hebrew
305 } else if (isRightToLeft() != prev.isRightToLeft()) {
306 if (isRightToLeft()) {
313 } else if (!language()->babel().empty()) {
315 subst(lyxrc.language_command_local,
316 "$$lang", language()->babel());
317 os << from_ascii(tmp);
318 count += tmp.length();
325 if (language()->encoding()->package() == Encoding::CJK) {
326 pair<bool, int> const c = switchEncoding(os, bparams,
327 runparams, *(language()->encoding()));
329 open_encoding_ = true;
331 runparams.encoding = language()->encoding();
335 // When the current language is Hebrew, Arabic, or Farsi
336 // the numbers are written Left-to-Right. ArabTeX package
337 // reorders the number automatically but the packages used
338 // for Hebrew and Farsi (Arabi) do not.
339 if (bits_.number() == FONT_ON && prev.fontInfo().number() != FONT_ON
340 && (language()->lang() == "hebrew"
341 || language()->lang() == "farsi"
342 || language()->lang() == "arabic_arabi")) {
348 f.reduce(base.bits_);
350 if (f.family() != INHERIT_FAMILY) {
352 << LaTeXFamilyNames[f.family()]
354 count += strlen(LaTeXFamilyNames[f.family()]) + 2;
355 env = true; //We have opened a new environment
357 if (f.series() != INHERIT_SERIES) {
359 << LaTeXSeriesNames[f.series()]
361 count += strlen(LaTeXSeriesNames[f.series()]) + 2;
362 env = true; //We have opened a new environment
364 if (f.shape() != INHERIT_SHAPE) {
366 << LaTeXShapeNames[f.shape()]
368 count += strlen(LaTeXShapeNames[f.shape()]) + 2;
369 env = true; //We have opened a new environment
371 if (f.color() != Color_inherit && f.color() != Color_ignore) {
373 << from_ascii(lcolor.getLaTeXName(f.color()))
375 count += lcolor.getLaTeXName(f.color()).length() + 13;
376 env = true; //We have opened a new environment
378 // FIXME: uncomment this when we support background.
380 if (f.background() != Color_inherit && f.background() != Color_ignore) {
382 << from_ascii(lcolor.getLaTeXName(f.background()))
384 count += lcolor.getLaTeXName(f.background()).length() + 13;
385 env = true; //We have opened a new environment
388 if (f.emph() == FONT_ON) {
391 env = true; //We have opened a new environment
393 if (f.underbar() == FONT_ON) {
396 runparams.inulemcmd = true;
397 env = true; //We have opened a new environment
399 if (f.strikeout() == FONT_ON) {
402 runparams.inulemcmd = true;
403 env = true; //We have opened a new environment
405 if (f.uuline() == FONT_ON) {
408 runparams.inulemcmd = true;
409 env = true; //We have opened a new environment
411 if (f.uwave() == FONT_ON) {
414 runparams.inulemcmd = true;
415 env = true; //We have opened a new environment
417 // \noun{} is a LyX special macro
418 if (f.noun() == FONT_ON) {
421 env = true; //We have opened a new environment
423 if (f.size() != FONT_SIZE_INHERIT) {
424 // If we didn't open an environment above, we open one here
430 << LaTeXSizeNames[f.size()]
432 count += strlen(LaTeXSizeNames[f.size()]) + 2;
438 /// Writes ending block of LaTeX needed to close use of this font
439 // Returns number of chars written
440 // This one corresponds to latexWriteStartChanges(). (Asger)
441 int Font::latexWriteEndChanges(otexstream & os, BufferParams const & bparams,
442 OutputParams const & runparams,
445 bool const & closeLanguage) const
450 // reduce the current font to changes against the base
451 // font (of the layout). We use a temporary for this to
452 // avoid changing this font instance, as that would break
454 f.reduce(base.bits_);
456 if (f.family() != INHERIT_FAMILY) {
459 env = true; // Size change need not bother about closing env.
461 if (f.series() != INHERIT_SERIES) {
464 env = true; // Size change need not bother about closing env.
466 if (f.shape() != INHERIT_SHAPE) {
469 env = true; // Size change need not bother about closing env.
471 if (f.color() != Color_inherit && f.color() != Color_ignore) {
474 env = true; // Size change need not bother about closing env.
476 if (f.emph() == FONT_ON) {
479 env = true; // Size change need not bother about closing env.
481 if (f.underbar() == FONT_ON) {
484 runparams.inulemcmd = false;
485 env = true; // Size change need not bother about closing env.
487 if (f.strikeout() == FONT_ON) {
490 runparams.inulemcmd = false;
491 env = true; // Size change need not bother about closing env.
493 if (f.uuline() == FONT_ON) {
496 runparams.inulemcmd = false;
497 env = true; // Size change need not bother about closing env.
499 if (f.uwave() == FONT_ON) {
502 runparams.inulemcmd = false;
503 env = true; // Size change need not bother about closing env.
505 if (f.noun() == FONT_ON) {
508 env = true; // Size change need not bother about closing env.
510 if (f.size() != FONT_SIZE_INHERIT) {
511 // We only have to close if only size changed
518 // When the current language is Hebrew, Arabic, or Farsi
519 // the numbers are written Left-to-Right. ArabTeX package
520 // reorders the number automatically but the packages used
521 // for Hebrew and Farsi (Arabi) do not.
522 if (bits_.number() == FONT_ON && next.fontInfo().number() != FONT_ON
523 && (language()->lang() == "hebrew"
524 || language()->lang() == "farsi"
525 || language()->lang() == "arabic_arabi")) {
530 if (open_encoding_) {
531 // We need to close the encoding even if it does not change
532 // to do correct environment nesting
533 Encoding const * const ascii = encodings.fromLyXName("ascii");
534 pair<bool, int> const c = switchEncoding(os.os(), bparams,
536 LASSERT(c.first, /**/);
538 runparams.encoding = ascii;
539 open_encoding_ = false;
543 language() != base.language() && language() != next.language()) {
552 string Font::toString(bool const toggle) const
554 string const lang = (language() == reset_language)
555 ? "reset" : language()->lang();
558 os << "family " << bits_.family() << '\n'
559 << "series " << bits_.series() << '\n'
560 << "shape " << bits_.shape() << '\n'
561 << "size " << bits_.size() << '\n'
562 << "emph " << bits_.emph() << '\n'
563 << "underbar " << bits_.underbar() << '\n'
564 << "strikeout " << bits_.strikeout() << '\n'
565 << "uuline " << bits_.uuline() << '\n'
566 << "uwave " << bits_.uwave() << '\n'
567 << "noun " << bits_.noun() << '\n'
568 << "number " << bits_.number() << '\n'
569 << "color " << bits_.color() << '\n'
570 << "language " << lang << '\n'
571 << "toggleall " << convert<string>(toggle);
576 bool Font::fromString(string const & data, bool & toggle)
578 istringstream is(data);
586 token = lex.getString();
588 if (token.empty() || !lex.next())
591 if (token == "family") {
592 int const next = lex.getInteger();
593 bits_.setFamily(FontFamily(next));
595 } else if (token == "series") {
596 int const next = lex.getInteger();
597 bits_.setSeries(FontSeries(next));
599 } else if (token == "shape") {
600 int const next = lex.getInteger();
601 bits_.setShape(FontShape(next));
603 } else if (token == "size") {
604 int const next = lex.getInteger();
605 bits_.setSize(FontSize(next));
607 } else if (token == "emph" || token == "underbar" ||
608 token == "noun" || token == "number" ||
609 token == "uuline" || token == "uwave" ||
610 token == "strikeout") {
612 int const next = lex.getInteger();
613 FontState const misc = FontState(next);
617 else if (token == "underbar")
618 bits_.setUnderbar(misc);
619 else if (token == "strikeout")
620 bits_.setStrikeout(misc);
621 else if (token == "uuline")
622 bits_.setUuline(misc);
623 else if (token == "uwave")
624 bits_.setUwave(misc);
625 else if (token == "noun")
627 else if (token == "number")
628 bits_.setNumber(misc);
630 } else if (token == "color") {
631 int const next = lex.getInteger();
632 bits_.setColor(ColorCode(next));
635 } else if (token == "background") {
636 int const next = lex.getInteger();
637 bits_.setBackground(ColorCode(next));
640 } else if (token == "language") {
641 string const next = lex.getString();
642 setLanguage(languages.getLanguage(next));
644 } else if (token == "toggleall") {
645 toggle = lex.getBool();
648 // Unrecognised token
658 void Font::validate(LaTeXFeatures & features) const
660 BufferParams const & bparams = features.bufferParams();
661 Language const * doc_language = bparams.language;
663 if (bits_.noun() == FONT_ON) {
664 LYXERR(Debug::LATEX, "font.noun: " << bits_.noun());
665 features.require("noun");
666 LYXERR(Debug::LATEX, "Noun enabled. Font: " << to_utf8(stateText(0)));
668 if (bits_.underbar() == FONT_ON) {
669 LYXERR(Debug::LATEX, "font.underline: " << bits_.underbar());
670 features.require("ulem");
671 LYXERR(Debug::LATEX, "Underline enabled. Font: " << to_utf8(stateText(0)));
673 if (bits_.strikeout() == FONT_ON) {
674 LYXERR(Debug::LATEX, "font.strikeout: " << bits_.strikeout());
675 features.require("ulem");
676 LYXERR(Debug::LATEX, "Strikeout enabled. Font: " << to_utf8(stateText(0)));
678 if (bits_.uuline() == FONT_ON) {
679 LYXERR(Debug::LATEX, "font.uuline: " << bits_.uuline());
680 features.require("ulem");
681 LYXERR(Debug::LATEX, "Double underline enabled. Font: " << to_utf8(stateText(0)));
683 if (bits_.uwave() == FONT_ON) {
684 LYXERR(Debug::LATEX, "font.uwave: " << bits_.uwave());
685 features.require("ulem");
686 LYXERR(Debug::LATEX, "Wavy underline enabled. Font: " << to_utf8(stateText(0)));
688 switch (bits_.color()) {
692 // probably we should put here all interface colors used for
693 // font displaying! For now I just add this ones I know of (Jug)
695 case Color_notelabel:
698 features.require("color");
699 LYXERR(Debug::LATEX, "Color enabled. Font: " << to_utf8(stateText(0)));
702 // FIXME: Do something for background and soul package?
704 if (lang_->babel() != doc_language->babel() &&
705 lang_ != ignore_language &&
706 lang_ != latex_language)
708 features.useLanguage(lang_);
709 LYXERR(Debug::LATEX, "Found language " << lang_->lang());
714 ostream & operator<<(ostream & os, FontState fms)
716 return os << int(fms);
720 ostream & operator<<(ostream & os, FontInfo const & f)
723 << " family " << f.family()
724 << " series " << f.series()
725 << " shape " << f.shape()
726 << " size " << f.size()
727 << " color " << f.color()
728 // FIXME: uncomment this when we support background.
729 //<< " background " << f.background()
730 << " emph " << f.emph()
731 << " underbar " << f.underbar()
732 << " strikeout " << f.strikeout()
733 << " uuline " << f.uuline()
734 << " uwave " << f.uwave()
735 << " noun " << f.noun()
736 << " number " << f.number();
740 ostream & operator<<(ostream & os, Font const & font)
742 return os << font.bits_
743 << " lang: " << (font.lang_ ? font.lang_->lang() : 0);