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
25 #include "LaTeXFeatures.h"
28 #include "output_latex.h"
29 #include "OutputParams.h"
31 #include "support/convert.h"
32 #include "support/lstrings.h"
37 using std::ostringstream;
38 using std::istringstream;
41 #ifndef CXX_GLOBAL_CSTD
48 using support::ascii_lowercase;
49 using support::bformat;
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", "wasy", "esint",
62 N_("Inherit"), N_("Ignore") };
64 char const * GUISeriesNames[4] =
65 { N_("Medium"), N_("Bold"), N_("Inherit"), N_("Ignore") };
67 char const * GUIShapeNames[6] =
68 { N_("Upright"), N_("Italic"), N_("Slanted"), N_("Smallcaps"), N_("Inherit"),
71 char const * GUISizeNames[14] =
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") };
81 // Strings used to read and write .lyx format files
83 char const * LyXFamilyNames[NUM_FAMILIES + 2 /* default & error */] =
84 { "roman", "sans", "typewriter", "symbol",
85 "cmr", "cmsy", "cmm", "cmex", "msa", "msb", "eufrak", "wasy", "esint",
88 char const * LyXSeriesNames[4] =
89 { "medium", "bold", "default", "error" };
91 char const * LyXShapeNames[6] =
92 { "up", "italic", "slanted", "smallcaps", "default", "error" };
94 char const * LyXSizeNames[14] =
95 { "tiny", "scriptsize", "footnotesize", "small", "normal", "large",
96 "larger", "largest", "huge", "giant",
97 "increase", "decrease", "default", "error" };
99 char const * LyXMiscNames[5] =
100 { "off", "on", "toggle", "default", "error" };
103 // Strings used to write LaTeX files
105 char const * LaTeXFamilyNames[6] =
106 { "textrm", "textsf", "texttt", "error1", "error2", "error3" };
108 char const * LaTeXSeriesNames[4] =
109 { "textmd", "textbf", "error4", "error5" };
111 char const * LaTeXShapeNames[6] =
112 { "textup", "textit", "textsl", "textsc", "error6", "error7" };
114 char const * LaTeXSizeNames[14] =
115 { "tiny", "scriptsize", "footnotesize", "small", "normalsize", "large",
116 "Large", "LARGE", "huge", "Huge", "error8", "error9", "error10", "error11" };
121 Font::Font(FontInfo bits, Language const * l)
122 : bits_(bits), lang_(l), open_encoding_(false)
125 lang_ = default_language;
129 bool Font::isRightToLeft() const
131 return lang_->rightToLeft();
135 bool Font::isVisibleRightToLeft() const
137 return (lang_->rightToLeft() &&
138 bits_.number() != FONT_ON);
142 void Font::setLanguage(Language const * l)
148 /// Updates font settings according to request
149 void Font::update(Font const & newfont,
150 Language const * document_language,
153 bits_.update(newfont.fontInfo(), toggleall);
155 if (newfont.language() == language() && toggleall)
156 if (language() == document_language)
157 setLanguage(default_language);
159 setLanguage(document_language);
160 else if (newfont.language() != ignore_language)
161 setLanguage(newfont.language());
165 docstring const stateText(FontInfo const & f)
168 if (f.family() != INHERIT_FAMILY)
169 os << _(GUIFamilyNames[f.family()]) << ", ";
170 if (f.series() != INHERIT_SERIES)
171 os << _(GUISeriesNames[f.series()]) << ", ";
172 if (f.shape() != INHERIT_SHAPE)
173 os << _(GUIShapeNames[f.shape()]) << ", ";
174 if (f.size() != FONT_SIZE_INHERIT)
175 os << _(GUISizeNames[f.size()]) << ", ";
176 if (f.color() != Color_inherit)
177 os << lcolor.getGUIName(f.color()) << ", ";
178 // FIXME: uncomment this when we support background.
179 //if (f.background() != Color_inherit)
180 // os << lcolor.getGUIName(f.background()) << ", ";
181 if (f.emph() != FONT_INHERIT)
182 os << bformat(_("Emphasis %1$s, "),
183 _(GUIMiscNames[f.emph()]));
184 if (f.underbar() != FONT_INHERIT)
185 os << bformat(_("Underline %1$s, "),
186 _(GUIMiscNames[f.underbar()]));
187 if (f.noun() != FONT_INHERIT)
188 os << bformat(_("Noun %1$s, "),
189 _(GUIMiscNames[f.noun()]));
190 if (f == inherit_font)
191 os << _("Default") << ", ";
197 docstring const Font::stateText(BufferParams * params) const
200 os << lyx::stateText(bits_);
201 if (!params || (language() != params->language))
202 os << bformat(_("Language: %1$s, "),
203 _(language()->display()));
204 if (bits_.number() != FONT_OFF)
205 os << bformat(_(" Number %1$s"),
206 _(GUIMiscNames[bits_.number()]));
207 return rtrim(os.str(), ", ");
211 // Set family according to lyx format string
212 void setLyXFamily(string const & fam, FontInfo & f)
214 string const s = ascii_lowercase(fam);
217 while (LyXFamilyNames[i] != s &&
218 LyXFamilyNames[i] != string("error"))
220 if (s == LyXFamilyNames[i])
221 f.setFamily(FontFamily(i));
223 lyxerr << "setLyXFamily: Unknown family `"
224 << s << '\'' << endl;
228 // Set series according to lyx format string
229 void setLyXSeries(string const & ser, FontInfo & f)
231 string const s = ascii_lowercase(ser);
234 while (LyXSeriesNames[i] != s &&
235 LyXSeriesNames[i] != string("error")) ++i;
236 if (s == LyXSeriesNames[i]) {
237 f.setSeries(FontSeries(i));
239 lyxerr << "setLyXSeries: Unknown series `"
240 << s << '\'' << endl;
244 // Set shape according to lyx format string
245 void setLyXShape(string const & sha, FontInfo & f)
247 string const s = ascii_lowercase(sha);
250 while (LyXShapeNames[i] != s && LyXShapeNames[i] != string("error"))
252 if (s == LyXShapeNames[i])
253 f.setShape(FontShape(i));
255 lyxerr << "Font::setLyXShape: Unknown shape `"
256 << s << '\'' << endl;
260 // Set size according to lyx format string
261 void setLyXSize(string const & siz, FontInfo & f)
263 string const s = ascii_lowercase(siz);
265 while (LyXSizeNames[i] != s && LyXSizeNames[i] != string("error"))
267 if (s == LyXSizeNames[i]) {
268 f.setSize(FontSize(i));
270 lyxerr << "Font::setLyXSize: Unknown size `"
271 << s << '\'' << endl;
275 // Set size according to lyx format string
276 FontState Font::setLyXMisc(string const & siz)
278 string const s = ascii_lowercase(siz);
280 while (LyXMiscNames[i] != s &&
281 LyXMiscNames[i] != string("error")) ++i;
282 if (s == LyXMiscNames[i])
284 lyxerr << "Font::setLyXMisc: Unknown misc flag `"
285 << s << '\'' << endl;
290 /// Sets color after LyX text format
291 void setLyXColor(string const & col, FontInfo & f)
293 f.setColor(lcolor.getFromLyXName(col));
297 // Returns size in latex format
298 string const Font::latexSize() const
300 return LaTeXSizeNames[bits_.size()];
304 // Read a font definition from given file in lyx format
306 FontInfo lyxRead(Lexer & lex, FontInfo const & fi)
310 bool finished = false;
311 while (!finished && lex.isOK() && !error) {
313 string const tok = ascii_lowercase(lex.getString());
317 } else if (tok == "endfont") {
319 } else if (tok == "family") {
321 string const ttok = lex.getString();
322 setLyXFamily(ttok, f);
323 } else if (tok == "series") {
325 string const ttok = lex.getString();
326 setLyXSeries(ttok, f);
327 } else if (tok == "shape") {
329 string const ttok = lex.getString();
330 setLyXShape(ttok, f);
331 } else if (tok == "size") {
333 string const ttok = lex.getString();
335 } else if (tok == "misc") {
337 string const ttok = ascii_lowercase(lex.getString());
339 if (ttok == "no_bar") {
340 f.setUnderbar(FONT_OFF);
341 } else if (ttok == "no_emph") {
343 } else if (ttok == "no_noun") {
345 } else if (ttok == "emph") {
347 } else if (ttok == "underbar") {
348 f.setUnderbar(FONT_ON);
349 } else if (ttok == "noun") {
352 lex.printError("Illegal misc type `$$Token'");
354 } else if (tok == "color") {
356 string const ttok = lex.getString();
357 setLyXColor(ttok, f);
359 lex.printError("Unknown tag `$$Token'");
367 /// Writes the changes from this font to orgfont in .lyx format in file
368 void Font::lyxWriteChanges(Font const & orgfont,
372 if (orgfont.fontInfo().family() != bits_.family())
373 os << "\\family " << LyXFamilyNames[bits_.family()] << "\n";
374 if (orgfont.fontInfo().series() != bits_.series())
375 os << "\\series " << LyXSeriesNames[bits_.series()] << "\n";
376 if (orgfont.fontInfo().shape() != bits_.shape())
377 os << "\\shape " << LyXShapeNames[bits_.shape()] << "\n";
378 if (orgfont.fontInfo().size() != bits_.size())
379 os << "\\size " << LyXSizeNames[bits_.size()] << "\n";
380 if (orgfont.fontInfo().emph() != bits_.emph())
381 os << "\\emph " << LyXMiscNames[bits_.emph()] << "\n";
382 if (orgfont.fontInfo().number() != bits_.number())
383 os << "\\numeric " << LyXMiscNames[bits_.number()] << "\n";
384 if (orgfont.fontInfo().underbar() != bits_.underbar()) {
385 // This is only for backwards compatibility
386 switch (bits_.underbar()) {
387 case FONT_OFF: os << "\\bar no\n"; break;
388 case FONT_ON: os << "\\bar under\n"; break;
389 case FONT_TOGGLE: lyxerr << "Font::lyxWriteFontChanges: "
390 "FONT_TOGGLE should not appear here!"
393 case FONT_INHERIT: os << "\\bar default\n"; break;
394 case FONT_IGNORE: lyxerr << "Font::lyxWriteFontChanges: "
395 "IGNORE should not appear here!"
400 if (orgfont.fontInfo().noun() != bits_.noun()) {
401 os << "\\noun " << LyXMiscNames[bits_.noun()] << "\n";
403 if (orgfont.fontInfo().color() != bits_.color())
404 os << "\\color " << lcolor.getLyXName(bits_.color()) << '\n';
405 // FIXME: uncomment this when we support background.
406 //if (orgfont.fontInfo().background() != bits_.background())
407 // os << "\\color " << lcolor.getLyXName(bits_.background()) << '\n';
408 if (orgfont.language() != language() &&
409 language() != latex_language) {
411 os << "\\lang " << language()->lang() << "\n";
413 os << "\\lang unknown\n";
418 /// Writes the head of the LaTeX needed to impose this font
419 // Returns number of chars written.
420 int Font::latexWriteStartChanges(odocstream & os, BufferParams const & bparams,
421 OutputParams const & runparams,
423 Font const & prev) const
428 if (language()->babel() != base.language()->babel() &&
429 language() != prev.language()) {
430 if (language()->lang() == "farsi") {
433 } else if (!isRightToLeft() &&
434 base.language()->lang() == "farsi") {
437 } else if (language()->lang() == "arabic_arabi") {
440 } else if (!isRightToLeft() &&
441 base.language()->lang() == "arabic_arabi") {
444 // currently the remaining RTL languages are arabic_arabtex and hebrew
445 } else if (isRightToLeft() != prev.isRightToLeft()) {
446 if (isRightToLeft()) {
453 } else if (!language()->babel().empty()) {
455 subst(lyxrc.language_command_local,
456 "$$lang", language()->babel());
457 os << from_ascii(tmp);
458 count += tmp.length();
465 if (language()->encoding()->package() == Encoding::CJK) {
466 pair<bool, int> const c = switchEncoding(os, bparams,
467 runparams.moving_arg, *(runparams.encoding),
468 *(language()->encoding()));
470 open_encoding_ = true;
472 runparams.encoding = language()->encoding();
476 // When the current language is Hebrew, Arabic, or Farsi
477 // the numbers are written Left-to-Right. ArabTeX package
478 // reorders the number automatically but the packages used
479 // for Hebrew and Farsi (Arabi) do not.
480 if (bits_.number() == FONT_ON && prev.fontInfo().number() != FONT_ON
481 && (language()->lang() == "hebrew"
482 || language()->lang() == "farsi"
483 || language()->lang() == "arabic_arabi")) {
489 f.reduce(base.bits_);
491 if (f.family() != INHERIT_FAMILY) {
493 << LaTeXFamilyNames[f.family()]
495 count += strlen(LaTeXFamilyNames[f.family()]) + 2;
496 env = true; //We have opened a new environment
498 if (f.series() != INHERIT_SERIES) {
500 << LaTeXSeriesNames[f.series()]
502 count += strlen(LaTeXSeriesNames[f.series()]) + 2;
503 env = true; //We have opened a new environment
505 if (f.shape() != INHERIT_SHAPE) {
507 << LaTeXShapeNames[f.shape()]
509 count += strlen(LaTeXShapeNames[f.shape()]) + 2;
510 env = true; //We have opened a new environment
512 if (f.color() != Color_inherit && f.color() != Color_ignore) {
514 << from_ascii(lcolor.getLaTeXName(f.color()))
516 count += lcolor.getLaTeXName(f.color()).length() + 13;
517 env = true; //We have opened a new environment
519 // FIXME: uncomment this when we support background.
521 if (f.background() != Color_inherit && f.background() != Color_ignore) {
523 << from_ascii(lcolor.getLaTeXName(f.background()))
525 count += lcolor.getLaTeXName(f.background()).length() + 13;
526 env = true; //We have opened a new environment
529 if (f.emph() == FONT_ON) {
532 env = true; //We have opened a new environment
534 if (f.underbar() == FONT_ON) {
537 env = true; //We have opened a new environment
539 // \noun{} is a LyX special macro
540 if (f.noun() == FONT_ON) {
543 env = true; //We have opened a new environment
545 if (f.size() != FONT_SIZE_INHERIT) {
546 // If we didn't open an environment above, we open one here
552 << LaTeXSizeNames[f.size()]
554 count += strlen(LaTeXSizeNames[f.size()]) + 2;
560 /// Writes ending block of LaTeX needed to close use of this font
561 // Returns number of chars written
562 // This one corresponds to latexWriteStartChanges(). (Asger)
563 int Font::latexWriteEndChanges(odocstream & os, BufferParams const & bparams,
564 OutputParams const & runparams,
567 bool const & closeLanguage) const
572 // reduce the current font to changes against the base
573 // font (of the layout). We use a temporary for this to
574 // avoid changing this font instance, as that would break
576 f.reduce(base.bits_);
578 if (f.family() != INHERIT_FAMILY) {
581 env = true; // Size change need not bother about closing env.
583 if (f.series() != INHERIT_SERIES) {
586 env = true; // Size change need not bother about closing env.
588 if (f.shape() != INHERIT_SHAPE) {
591 env = true; // Size change need not bother about closing env.
593 if (f.color() != Color_inherit && f.color() != Color_ignore) {
596 env = true; // Size change need not bother about closing env.
598 if (f.emph() == FONT_ON) {
601 env = true; // Size change need not bother about closing env.
603 if (f.underbar() == FONT_ON) {
606 env = true; // Size change need not bother about closing env.
608 if (f.noun() == FONT_ON) {
611 env = true; // Size change need not bother about closing env.
613 if (f.size() != FONT_SIZE_INHERIT) {
614 // We only have to close if only size changed
621 // When the current language is Hebrew, Arabic, or Farsi
622 // the numbers are written Left-to-Right. ArabTeX package
623 // reorders the number automatically but the packages used
624 // for Hebrew and Farsi (Arabi) do not.
625 if (bits_.number() == FONT_ON && next.fontInfo().number() != FONT_ON
626 && (language()->lang() == "hebrew"
627 || language()->lang() == "farsi"
628 || language()->lang() == "arabic_arabi")) {
633 if (open_encoding_) {
634 // We need to close the encoding even if it does not change
635 // to do correct environment nesting
636 Encoding const * const ascii = encodings.getFromLyXName("ascii");
637 pair<bool, int> const c = switchEncoding(os, bparams,
638 runparams.moving_arg, *(runparams.encoding),
640 BOOST_ASSERT(c.first);
642 runparams.encoding = ascii;
643 open_encoding_ = false;
647 language() != base.language() && language() != next.language()) {
656 std::string Font::toString(bool const toggle) const
658 string lang = "ignore";
660 lang = language()->lang();
663 os << "family " << bits_.family() << '\n'
664 << "series " << bits_.series() << '\n'
665 << "shape " << bits_.shape() << '\n'
666 << "size " << bits_.size() << '\n'
667 << "emph " << bits_.emph() << '\n'
668 << "underbar " << bits_.underbar() << '\n'
669 << "noun " << bits_.noun() << '\n'
670 << "number " << bits_.number() << '\n'
671 << "color " << bits_.color() << '\n'
672 << "language " << lang << '\n'
673 << "toggleall " << convert<string>(toggle);
678 bool Font::fromString(string const & data, bool & toggle)
680 istringstream is(data);
688 token = lex.getString();
690 if (token.empty() || !lex.next())
693 if (token == "family") {
694 int const next = lex.getInteger();
695 bits_.setFamily(FontFamily(next));
697 } else if (token == "series") {
698 int const next = lex.getInteger();
699 bits_.setSeries(FontSeries(next));
701 } else if (token == "shape") {
702 int const next = lex.getInteger();
703 bits_.setShape(FontShape(next));
705 } else if (token == "size") {
706 int const next = lex.getInteger();
707 bits_.setSize(FontSize(next));
709 } else if (token == "emph" || token == "underbar" ||
710 token == "noun" || token == "number") {
712 int const next = lex.getInteger();
713 FontState const misc = FontState(next);
717 else if (token == "underbar")
718 bits_.setUnderbar(misc);
719 else if (token == "noun")
721 else if (token == "number")
722 bits_.setNumber(misc);
724 } else if (token == "color") {
725 int const next = lex.getInteger();
726 bits_.setColor(ColorCode(next));
729 } else if (token == "background") {
730 int const next = lex.getInteger();
731 bits_.setBackground(ColorCode(next));
734 } else if (token == "language") {
735 string const next = lex.getString();
736 if (next == "ignore")
737 setLanguage(ignore_language);
739 setLanguage(languages.getLanguage(next));
741 } else if (token == "toggleall") {
742 toggle = lex.getBool();
745 // Unrecognised token
755 void Font::validate(LaTeXFeatures & features) const
757 BufferParams const & bparams = features.bufferParams();
758 Language const * doc_language = bparams.language;
760 if (bits_.noun() == FONT_ON) {
761 LYXERR(Debug::LATEX, "font.noun: " << bits_.noun());
762 features.require("noun");
763 LYXERR(Debug::LATEX, "Noun enabled. Font: " << to_utf8(stateText(0)));
765 switch (bits_.color()) {
769 // probably we should put here all interface colors used for
770 // font displaying! For now I just add this ones I know of (Jug)
775 features.require("color");
776 LYXERR(Debug::LATEX, "Color enabled. Font: " << to_utf8(stateText(0)));
779 // FIXME: Do something for background and soul package?
781 if (lang_->babel() != doc_language->babel() &&
782 lang_ != ignore_language &&
783 lang_ != latex_language)
785 features.useLanguage(lang_);
786 LYXERR(Debug::LATEX, "Found language " << lang_->lang());
791 ostream & operator<<(ostream & os, FontState fms)
793 return os << int(fms);
797 ostream & operator<<(std::ostream & os, FontInfo const & f)
800 << " family " << f.family()
801 << " series " << f.series()
802 << " shape " << f.shape()
803 << " size " << f.size()
804 << " color " << f.color()
805 // FIXME: uncomment this when we support background.
806 //<< " background " << f.background()
807 << " emph " << f.emph()
808 << " underbar " << f.underbar()
809 << " noun " << f.noun()
810 << " number " << f.number();
814 std::ostream & operator<<(std::ostream & os, Font const & font)
816 return os << font.bits_
817 << " lang: " << (font.lang_ ? font.lang_->lang() : 0);