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/convert.h"
30 #include "support/debug.h"
31 #include "support/gettext.h"
32 #include "support/lstrings.h"
38 using support::ascii_lowercase;
39 using support::bformat;
49 char const * GUIFamilyNames[NUM_FAMILIES + 2 /* default & error */] =
50 { N_("Roman"), N_("Sans Serif"), N_("Typewriter"), N_("Symbol"),
51 "cmr", "cmsy", "cmm", "cmex", "msa", "msb", "eufrak", "wasy", "esint",
52 N_("Inherit"), N_("Ignore") };
54 char const * GUISeriesNames[4] =
55 { N_("Medium"), N_("Bold"), N_("Inherit"), N_("Ignore") };
57 char const * GUIShapeNames[6] =
58 { N_("Upright"), N_("Italic"), N_("Slanted"), N_("Smallcaps"), N_("Inherit"),
61 char const * GUISizeNames[14] =
62 { N_("Tiny"), N_("Smallest"), N_("Smaller"), N_("Small"), N_("Normal"), N_("Large"),
63 N_("Larger"), N_("Largest"), N_("Huge"), N_("Huger"), N_("Increase"), N_("Decrease"),
64 N_("Inherit"), N_("Ignore") };
66 char const * GUIMiscNames[5] =
67 { N_("Off"), N_("On"), N_("Toggle"), N_("Inherit"), N_("Ignore") };
71 // Strings used to read and write .lyx format files
73 char const * LyXFamilyNames[NUM_FAMILIES + 2 /* default & error */] =
74 { "roman", "sans", "typewriter", "symbol",
75 "cmr", "cmsy", "cmm", "cmex", "msa", "msb", "eufrak", "wasy", "esint",
78 char const * LyXSeriesNames[4] =
79 { "medium", "bold", "default", "error" };
81 char const * LyXShapeNames[6] =
82 { "up", "italic", "slanted", "smallcaps", "default", "error" };
84 char const * LyXSizeNames[14] =
85 { "tiny", "scriptsize", "footnotesize", "small", "normal", "large",
86 "larger", "largest", "huge", "giant",
87 "increase", "decrease", "default", "error" };
89 char const * LyXMiscNames[5] =
90 { "off", "on", "toggle", "default", "error" };
93 // Strings used to write LaTeX files
95 char const * LaTeXFamilyNames[6] =
96 { "textrm", "textsf", "texttt", "error1", "error2", "error3" };
98 char const * LaTeXSeriesNames[4] =
99 { "textmd", "textbf", "error4", "error5" };
101 char const * LaTeXShapeNames[6] =
102 { "textup", "textit", "textsl", "textsc", "error6", "error7" };
104 char const * LaTeXSizeNames[14] =
105 { "tiny", "scriptsize", "footnotesize", "small", "normalsize", "large",
106 "Large", "LARGE", "huge", "Huge", "error8", "error9", "error10", "error11" };
111 Font::Font(FontInfo bits, Language const * l)
112 : bits_(bits), lang_(l), open_encoding_(false)
115 lang_ = default_language;
119 bool Font::isRightToLeft() const
121 return lang_->rightToLeft();
125 bool Font::isVisibleRightToLeft() const
127 return (lang_->rightToLeft() &&
128 bits_.number() != FONT_ON);
132 void Font::setLanguage(Language const * l)
138 /// Updates font settings according to request
139 void Font::update(Font const & newfont,
140 Language const * document_language,
143 bits_.update(newfont.fontInfo(), toggleall);
145 if (newfont.language() == language() && toggleall)
146 if (language() == document_language)
147 setLanguage(default_language);
149 setLanguage(document_language);
150 else if (newfont.language() != ignore_language)
151 setLanguage(newfont.language());
155 docstring const stateText(FontInfo const & f)
158 if (f.family() != INHERIT_FAMILY)
159 os << _(GUIFamilyNames[f.family()]) << ", ";
160 if (f.series() != INHERIT_SERIES)
161 os << _(GUISeriesNames[f.series()]) << ", ";
162 if (f.shape() != INHERIT_SHAPE)
163 os << _(GUIShapeNames[f.shape()]) << ", ";
164 if (f.size() != FONT_SIZE_INHERIT)
165 os << _(GUISizeNames[f.size()]) << ", ";
166 if (f.color() != Color_inherit)
167 os << lcolor.getGUIName(f.color()) << ", ";
168 // FIXME: uncomment this when we support background.
169 //if (f.background() != Color_inherit)
170 // os << lcolor.getGUIName(f.background()) << ", ";
171 if (f.emph() != FONT_INHERIT)
172 os << bformat(_("Emphasis %1$s, "),
173 _(GUIMiscNames[f.emph()]));
174 if (f.underbar() != FONT_INHERIT)
175 os << bformat(_("Underline %1$s, "),
176 _(GUIMiscNames[f.underbar()]));
177 if (f.noun() != FONT_INHERIT)
178 os << bformat(_("Noun %1$s, "),
179 _(GUIMiscNames[f.noun()]));
180 if (f == inherit_font)
181 os << _("Default") << ", ";
187 docstring const Font::stateText(BufferParams * params) const
190 os << lyx::stateText(bits_);
191 if (!params || (language() != params->language))
192 os << bformat(_("Language: %1$s, "),
193 _(language()->display()));
194 if (bits_.number() != FONT_OFF)
195 os << bformat(_(" Number %1$s"),
196 _(GUIMiscNames[bits_.number()]));
197 return rtrim(os.str(), ", ");
201 // Set family according to lyx format string
202 void setLyXFamily(string const & fam, FontInfo & f)
204 string const s = ascii_lowercase(fam);
207 while (LyXFamilyNames[i] != s &&
208 LyXFamilyNames[i] != string("error"))
210 if (s == LyXFamilyNames[i])
211 f.setFamily(FontFamily(i));
213 lyxerr << "setLyXFamily: Unknown family `"
214 << s << '\'' << endl;
218 // Set series according to lyx format string
219 void setLyXSeries(string const & ser, FontInfo & f)
221 string const s = ascii_lowercase(ser);
224 while (LyXSeriesNames[i] != s &&
225 LyXSeriesNames[i] != string("error")) ++i;
226 if (s == LyXSeriesNames[i]) {
227 f.setSeries(FontSeries(i));
229 lyxerr << "setLyXSeries: Unknown series `"
230 << s << '\'' << endl;
234 // Set shape according to lyx format string
235 void setLyXShape(string const & sha, FontInfo & f)
237 string const s = ascii_lowercase(sha);
240 while (LyXShapeNames[i] != s && LyXShapeNames[i] != string("error"))
242 if (s == LyXShapeNames[i])
243 f.setShape(FontShape(i));
245 lyxerr << "Font::setLyXShape: Unknown shape `"
246 << s << '\'' << endl;
250 // Set size according to lyx format string
251 void setLyXSize(string const & siz, FontInfo & f)
253 string const s = ascii_lowercase(siz);
255 while (LyXSizeNames[i] != s && LyXSizeNames[i] != string("error"))
257 if (s == LyXSizeNames[i]) {
258 f.setSize(FontSize(i));
260 lyxerr << "Font::setLyXSize: Unknown size `"
261 << s << '\'' << endl;
265 // Set size according to lyx format string
266 FontState Font::setLyXMisc(string const & siz)
268 string const s = ascii_lowercase(siz);
270 while (LyXMiscNames[i] != s &&
271 LyXMiscNames[i] != string("error")) ++i;
272 if (s == LyXMiscNames[i])
274 lyxerr << "Font::setLyXMisc: Unknown misc flag `"
275 << s << '\'' << endl;
280 /// Sets color after LyX text format
281 void setLyXColor(string const & col, FontInfo & f)
283 f.setColor(lcolor.getFromLyXName(col));
287 // Returns size in latex format
288 string const Font::latexSize() const
290 return LaTeXSizeNames[bits_.size()];
294 // Read a font definition from given file in lyx format
296 FontInfo lyxRead(Lexer & lex, FontInfo const & fi)
300 bool finished = false;
301 while (!finished && lex.isOK() && !error) {
303 string const tok = ascii_lowercase(lex.getString());
307 } else if (tok == "endfont") {
309 } else if (tok == "family") {
311 string const ttok = lex.getString();
312 setLyXFamily(ttok, f);
313 } else if (tok == "series") {
315 string const ttok = lex.getString();
316 setLyXSeries(ttok, f);
317 } else if (tok == "shape") {
319 string const ttok = lex.getString();
320 setLyXShape(ttok, f);
321 } else if (tok == "size") {
323 string const ttok = lex.getString();
325 } else if (tok == "misc") {
327 string const ttok = ascii_lowercase(lex.getString());
329 if (ttok == "no_bar") {
330 f.setUnderbar(FONT_OFF);
331 } else if (ttok == "no_emph") {
333 } else if (ttok == "no_noun") {
335 } else if (ttok == "emph") {
337 } else if (ttok == "underbar") {
338 f.setUnderbar(FONT_ON);
339 } else if (ttok == "noun") {
342 lex.printError("Illegal misc type `$$Token'");
344 } else if (tok == "color") {
346 string const ttok = lex.getString();
347 setLyXColor(ttok, f);
349 lex.printError("Unknown tag `$$Token'");
357 /// Writes the changes from this font to orgfont in .lyx format in file
358 void Font::lyxWriteChanges(Font const & orgfont,
362 if (orgfont.fontInfo().family() != bits_.family())
363 os << "\\family " << LyXFamilyNames[bits_.family()] << "\n";
364 if (orgfont.fontInfo().series() != bits_.series())
365 os << "\\series " << LyXSeriesNames[bits_.series()] << "\n";
366 if (orgfont.fontInfo().shape() != bits_.shape())
367 os << "\\shape " << LyXShapeNames[bits_.shape()] << "\n";
368 if (orgfont.fontInfo().size() != bits_.size())
369 os << "\\size " << LyXSizeNames[bits_.size()] << "\n";
370 if (orgfont.fontInfo().emph() != bits_.emph())
371 os << "\\emph " << LyXMiscNames[bits_.emph()] << "\n";
372 if (orgfont.fontInfo().number() != bits_.number())
373 os << "\\numeric " << LyXMiscNames[bits_.number()] << "\n";
374 if (orgfont.fontInfo().underbar() != bits_.underbar()) {
375 // This is only for backwards compatibility
376 switch (bits_.underbar()) {
377 case FONT_OFF: os << "\\bar no\n"; break;
378 case FONT_ON: os << "\\bar under\n"; break;
379 case FONT_TOGGLE: lyxerr << "Font::lyxWriteFontChanges: "
380 "FONT_TOGGLE should not appear here!"
383 case FONT_INHERIT: os << "\\bar default\n"; break;
384 case FONT_IGNORE: lyxerr << "Font::lyxWriteFontChanges: "
385 "IGNORE should not appear here!"
390 if (orgfont.fontInfo().noun() != bits_.noun()) {
391 os << "\\noun " << LyXMiscNames[bits_.noun()] << "\n";
393 if (orgfont.fontInfo().color() != bits_.color())
394 os << "\\color " << lcolor.getLyXName(bits_.color()) << '\n';
395 // FIXME: uncomment this when we support background.
396 //if (orgfont.fontInfo().background() != bits_.background())
397 // os << "\\color " << lcolor.getLyXName(bits_.background()) << '\n';
398 if (orgfont.language() != language() &&
399 language() != latex_language) {
401 os << "\\lang " << language()->lang() << "\n";
403 os << "\\lang unknown\n";
408 /// Writes the head of the LaTeX needed to impose this font
409 // Returns number of chars written.
410 int Font::latexWriteStartChanges(odocstream & os, BufferParams const & bparams,
411 OutputParams const & runparams,
413 Font const & prev) const
418 if (language()->babel() != base.language()->babel() &&
419 language() != prev.language()) {
420 if (language()->lang() == "farsi") {
423 } else if (!isRightToLeft() &&
424 base.language()->lang() == "farsi") {
427 } else if (language()->lang() == "arabic_arabi") {
430 } else if (!isRightToLeft() &&
431 base.language()->lang() == "arabic_arabi") {
434 // currently the remaining RTL languages are arabic_arabtex and hebrew
435 } else if (isRightToLeft() != prev.isRightToLeft()) {
436 if (isRightToLeft()) {
443 } else if (!language()->babel().empty()) {
445 subst(lyxrc.language_command_local,
446 "$$lang", language()->babel());
447 os << from_ascii(tmp);
448 count += tmp.length();
455 if (language()->encoding()->package() == Encoding::CJK) {
456 pair<bool, int> const c = switchEncoding(os, bparams,
457 runparams, *(runparams.encoding),
458 *(language()->encoding()));
460 open_encoding_ = true;
462 runparams.encoding = language()->encoding();
466 // When the current language is Hebrew, Arabic, or Farsi
467 // the numbers are written Left-to-Right. ArabTeX package
468 // reorders the number automatically but the packages used
469 // for Hebrew and Farsi (Arabi) do not.
470 if (bits_.number() == FONT_ON && prev.fontInfo().number() != FONT_ON
471 && (language()->lang() == "hebrew"
472 || language()->lang() == "farsi"
473 || language()->lang() == "arabic_arabi")) {
479 f.reduce(base.bits_);
481 if (f.family() != INHERIT_FAMILY) {
483 << LaTeXFamilyNames[f.family()]
485 count += strlen(LaTeXFamilyNames[f.family()]) + 2;
486 env = true; //We have opened a new environment
488 if (f.series() != INHERIT_SERIES) {
490 << LaTeXSeriesNames[f.series()]
492 count += strlen(LaTeXSeriesNames[f.series()]) + 2;
493 env = true; //We have opened a new environment
495 if (f.shape() != INHERIT_SHAPE) {
497 << LaTeXShapeNames[f.shape()]
499 count += strlen(LaTeXShapeNames[f.shape()]) + 2;
500 env = true; //We have opened a new environment
502 if (f.color() != Color_inherit && f.color() != Color_ignore) {
504 << from_ascii(lcolor.getLaTeXName(f.color()))
506 count += lcolor.getLaTeXName(f.color()).length() + 13;
507 env = true; //We have opened a new environment
509 // FIXME: uncomment this when we support background.
511 if (f.background() != Color_inherit && f.background() != Color_ignore) {
513 << from_ascii(lcolor.getLaTeXName(f.background()))
515 count += lcolor.getLaTeXName(f.background()).length() + 13;
516 env = true; //We have opened a new environment
519 if (f.emph() == FONT_ON) {
522 env = true; //We have opened a new environment
524 if (f.underbar() == FONT_ON) {
527 env = true; //We have opened a new environment
529 // \noun{} is a LyX special macro
530 if (f.noun() == FONT_ON) {
533 env = true; //We have opened a new environment
535 if (f.size() != FONT_SIZE_INHERIT) {
536 // If we didn't open an environment above, we open one here
542 << LaTeXSizeNames[f.size()]
544 count += strlen(LaTeXSizeNames[f.size()]) + 2;
550 /// Writes ending block of LaTeX needed to close use of this font
551 // Returns number of chars written
552 // This one corresponds to latexWriteStartChanges(). (Asger)
553 int Font::latexWriteEndChanges(odocstream & os, BufferParams const & bparams,
554 OutputParams const & runparams,
557 bool const & closeLanguage) const
562 // reduce the current font to changes against the base
563 // font (of the layout). We use a temporary for this to
564 // avoid changing this font instance, as that would break
566 f.reduce(base.bits_);
568 if (f.family() != INHERIT_FAMILY) {
571 env = true; // Size change need not bother about closing env.
573 if (f.series() != INHERIT_SERIES) {
576 env = true; // Size change need not bother about closing env.
578 if (f.shape() != INHERIT_SHAPE) {
581 env = true; // Size change need not bother about closing env.
583 if (f.color() != Color_inherit && f.color() != Color_ignore) {
586 env = true; // Size change need not bother about closing env.
588 if (f.emph() == FONT_ON) {
591 env = true; // Size change need not bother about closing env.
593 if (f.underbar() == FONT_ON) {
596 env = true; // Size change need not bother about closing env.
598 if (f.noun() == FONT_ON) {
601 env = true; // Size change need not bother about closing env.
603 if (f.size() != FONT_SIZE_INHERIT) {
604 // We only have to close if only size changed
611 // When the current language is Hebrew, Arabic, or Farsi
612 // the numbers are written Left-to-Right. ArabTeX package
613 // reorders the number automatically but the packages used
614 // for Hebrew and Farsi (Arabi) do not.
615 if (bits_.number() == FONT_ON && next.fontInfo().number() != FONT_ON
616 && (language()->lang() == "hebrew"
617 || language()->lang() == "farsi"
618 || language()->lang() == "arabic_arabi")) {
623 if (open_encoding_) {
624 // We need to close the encoding even if it does not change
625 // to do correct environment nesting
626 Encoding const * const ascii = encodings.getFromLyXName("ascii");
627 pair<bool, int> const c = switchEncoding(os, bparams,
628 runparams, *(runparams.encoding),
630 BOOST_ASSERT(c.first);
632 runparams.encoding = ascii;
633 open_encoding_ = false;
637 language() != base.language() && language() != next.language()) {
646 std::string Font::toString(bool const toggle) const
648 string lang = "ignore";
650 lang = language()->lang();
653 os << "family " << bits_.family() << '\n'
654 << "series " << bits_.series() << '\n'
655 << "shape " << bits_.shape() << '\n'
656 << "size " << bits_.size() << '\n'
657 << "emph " << bits_.emph() << '\n'
658 << "underbar " << bits_.underbar() << '\n'
659 << "noun " << bits_.noun() << '\n'
660 << "number " << bits_.number() << '\n'
661 << "color " << bits_.color() << '\n'
662 << "language " << lang << '\n'
663 << "toggleall " << convert<string>(toggle);
668 bool Font::fromString(string const & data, bool & toggle)
670 istringstream is(data);
678 token = lex.getString();
680 if (token.empty() || !lex.next())
683 if (token == "family") {
684 int const next = lex.getInteger();
685 bits_.setFamily(FontFamily(next));
687 } else if (token == "series") {
688 int const next = lex.getInteger();
689 bits_.setSeries(FontSeries(next));
691 } else if (token == "shape") {
692 int const next = lex.getInteger();
693 bits_.setShape(FontShape(next));
695 } else if (token == "size") {
696 int const next = lex.getInteger();
697 bits_.setSize(FontSize(next));
699 } else if (token == "emph" || token == "underbar" ||
700 token == "noun" || token == "number") {
702 int const next = lex.getInteger();
703 FontState const misc = FontState(next);
707 else if (token == "underbar")
708 bits_.setUnderbar(misc);
709 else if (token == "noun")
711 else if (token == "number")
712 bits_.setNumber(misc);
714 } else if (token == "color") {
715 int const next = lex.getInteger();
716 bits_.setColor(ColorCode(next));
719 } else if (token == "background") {
720 int const next = lex.getInteger();
721 bits_.setBackground(ColorCode(next));
724 } else if (token == "language") {
725 string const next = lex.getString();
726 if (next == "ignore")
727 setLanguage(ignore_language);
729 setLanguage(languages.getLanguage(next));
731 } else if (token == "toggleall") {
732 toggle = lex.getBool();
735 // Unrecognised token
745 void Font::validate(LaTeXFeatures & features) const
747 BufferParams const & bparams = features.bufferParams();
748 Language const * doc_language = bparams.language;
750 if (bits_.noun() == FONT_ON) {
751 LYXERR(Debug::LATEX, "font.noun: " << bits_.noun());
752 features.require("noun");
753 LYXERR(Debug::LATEX, "Noun enabled. Font: " << to_utf8(stateText(0)));
755 switch (bits_.color()) {
759 // probably we should put here all interface colors used for
760 // font displaying! For now I just add this ones I know of (Jug)
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 (lang_->babel() != doc_language->babel() &&
772 lang_ != ignore_language &&
773 lang_ != latex_language)
775 features.useLanguage(lang_);
776 LYXERR(Debug::LATEX, "Found language " << lang_->lang());
781 ostream & operator<<(ostream & os, FontState fms)
783 return os << int(fms);
787 ostream & operator<<(std::ostream & os, FontInfo const & f)
790 << " family " << f.family()
791 << " series " << f.series()
792 << " shape " << f.shape()
793 << " size " << f.size()
794 << " color " << f.color()
795 // FIXME: uncomment this when we support background.
796 //<< " background " << f.background()
797 << " emph " << f.emph()
798 << " underbar " << f.underbar()
799 << " noun " << f.noun()
800 << " number " << f.number();
804 std::ostream & operator<<(std::ostream & os, Font const & font)
806 return os << font.bits_
807 << " lang: " << (font.lang_ ? font.lang_->lang() : 0);