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"
37 using namespace lyx::support;
47 char const * GUIFamilyNames[NUM_FAMILIES + 2 /* default & error */] =
48 { N_("Roman"), N_("Sans Serif"), N_("Typewriter"), N_("Symbol"),
49 "cmr", "cmsy", "cmm", "cmex", "msa", "msb", "eufrak", "wasy", "esint",
50 N_("Inherit"), N_("Ignore") };
52 char const * GUISeriesNames[4] =
53 { N_("Medium"), N_("Bold"), N_("Inherit"), N_("Ignore") };
55 char const * GUIShapeNames[6] =
56 { N_("Upright"), N_("Italic"), N_("Slanted"), N_("Smallcaps"), N_("Inherit"),
59 char const * GUISizeNames[14] =
60 { N_("Tiny"), N_("Smallest"), N_("Smaller"), N_("Small"), N_("Normal"), N_("Large"),
61 N_("Larger"), N_("Largest"), N_("Huge"), N_("Huger"), N_("Increase"), N_("Decrease"),
62 N_("Inherit"), N_("Ignore") };
64 char const * GUIMiscNames[5] =
65 { N_("Off"), N_("On"), N_("Toggle"), N_("Inherit"), N_("Ignore") };
69 // Strings used to read and write .lyx format files
71 char const * LyXFamilyNames[NUM_FAMILIES + 2 /* default & error */] =
72 { "roman", "sans", "typewriter", "symbol",
73 "cmr", "cmsy", "cmm", "cmex", "msa", "msb", "eufrak", "wasy", "esint",
76 char const * LyXSeriesNames[4] =
77 { "medium", "bold", "default", "error" };
79 char const * LyXShapeNames[6] =
80 { "up", "italic", "slanted", "smallcaps", "default", "error" };
82 char const * LyXSizeNames[14] =
83 { "tiny", "scriptsize", "footnotesize", "small", "normal", "large",
84 "larger", "largest", "huge", "giant",
85 "increase", "decrease", "default", "error" };
87 char const * LyXMiscNames[5] =
88 { "off", "on", "toggle", "default", "error" };
91 // Strings used to write LaTeX files
93 char const * LaTeXFamilyNames[6] =
94 { "textrm", "textsf", "texttt", "error1", "error2", "error3" };
96 char const * LaTeXSeriesNames[4] =
97 { "textmd", "textbf", "error4", "error5" };
99 char const * LaTeXShapeNames[6] =
100 { "textup", "textit", "textsl", "textsc", "error6", "error7" };
102 char const * LaTeXSizeNames[14] =
103 { "tiny", "scriptsize", "footnotesize", "small", "normalsize", "large",
104 "Large", "LARGE", "huge", "Huge", "error8", "error9", "error10", "error11" };
109 Font::Font(FontInfo bits, Language const * l)
110 : bits_(bits), lang_(l), open_encoding_(false)
113 lang_ = default_language;
117 bool Font::isRightToLeft() const
119 return lang_->rightToLeft();
123 bool Font::isVisibleRightToLeft() const
125 return (lang_->rightToLeft() &&
126 bits_.number() != FONT_ON);
130 void Font::setLanguage(Language const * l)
136 /// Updates font settings according to request
137 void Font::update(Font const & newfont,
138 Language const * document_language,
141 bits_.update(newfont.fontInfo(), toggleall);
143 if (newfont.language() == language() && toggleall)
144 if (language() == document_language)
145 setLanguage(default_language);
147 setLanguage(document_language);
148 else if (newfont.language() != ignore_language)
149 setLanguage(newfont.language());
153 docstring const stateText(FontInfo const & f)
156 if (f.family() != INHERIT_FAMILY)
157 os << _(GUIFamilyNames[f.family()]) << ", ";
158 if (f.series() != INHERIT_SERIES)
159 os << _(GUISeriesNames[f.series()]) << ", ";
160 if (f.shape() != INHERIT_SHAPE)
161 os << _(GUIShapeNames[f.shape()]) << ", ";
162 if (f.size() != FONT_SIZE_INHERIT)
163 os << _(GUISizeNames[f.size()]) << ", ";
164 if (f.color() != Color_inherit)
165 os << lcolor.getGUIName(f.color()) << ", ";
166 // FIXME: uncomment this when we support background.
167 //if (f.background() != Color_inherit)
168 // os << lcolor.getGUIName(f.background()) << ", ";
169 if (f.emph() != FONT_INHERIT)
170 os << bformat(_("Emphasis %1$s, "),
171 _(GUIMiscNames[f.emph()]));
172 if (f.underbar() != FONT_INHERIT)
173 os << bformat(_("Underline %1$s, "),
174 _(GUIMiscNames[f.underbar()]));
175 if (f.noun() != FONT_INHERIT)
176 os << bformat(_("Noun %1$s, "),
177 _(GUIMiscNames[f.noun()]));
178 if (f == inherit_font)
179 os << _("Default") << ", ";
185 docstring const Font::stateText(BufferParams * params) const
188 os << lyx::stateText(bits_);
189 if (!params || (language() != params->language))
190 os << bformat(_("Language: %1$s, "),
191 _(language()->display()));
192 if (bits_.number() != FONT_OFF)
193 os << bformat(_(" Number %1$s"),
194 _(GUIMiscNames[bits_.number()]));
195 return rtrim(os.str(), ", ");
199 // Set family according to lyx format string
200 void setLyXFamily(string const & fam, FontInfo & f)
202 string const s = ascii_lowercase(fam);
205 while (LyXFamilyNames[i] != s &&
206 LyXFamilyNames[i] != string("error"))
208 if (s == LyXFamilyNames[i])
209 f.setFamily(FontFamily(i));
211 lyxerr << "setLyXFamily: Unknown family `"
212 << s << '\'' << endl;
216 // Set series according to lyx format string
217 void setLyXSeries(string const & ser, FontInfo & f)
219 string const s = ascii_lowercase(ser);
222 while (LyXSeriesNames[i] != s &&
223 LyXSeriesNames[i] != string("error")) ++i;
224 if (s == LyXSeriesNames[i]) {
225 f.setSeries(FontSeries(i));
227 lyxerr << "setLyXSeries: Unknown series `"
228 << s << '\'' << endl;
232 // Set shape according to lyx format string
233 void setLyXShape(string const & sha, FontInfo & f)
235 string const s = ascii_lowercase(sha);
238 while (LyXShapeNames[i] != s && LyXShapeNames[i] != string("error"))
240 if (s == LyXShapeNames[i])
241 f.setShape(FontShape(i));
243 lyxerr << "Font::setLyXShape: Unknown shape `"
244 << s << '\'' << endl;
248 // Set size according to lyx format string
249 void setLyXSize(string const & siz, FontInfo & f)
251 string const s = ascii_lowercase(siz);
253 while (LyXSizeNames[i] != s && LyXSizeNames[i] != string("error"))
255 if (s == LyXSizeNames[i]) {
256 f.setSize(FontSize(i));
258 lyxerr << "Font::setLyXSize: Unknown size `"
259 << s << '\'' << endl;
263 // Set size according to lyx format string
264 FontState Font::setLyXMisc(string const & siz)
266 string const s = ascii_lowercase(siz);
268 while (LyXMiscNames[i] != s &&
269 LyXMiscNames[i] != string("error")) ++i;
270 if (s == LyXMiscNames[i])
272 lyxerr << "Font::setLyXMisc: Unknown misc flag `"
273 << s << '\'' << endl;
278 /// Sets color after LyX text format
279 void setLyXColor(string const & col, FontInfo & f)
281 f.setColor(lcolor.getFromLyXName(col));
285 // Returns size in latex format
286 string const Font::latexSize() const
288 return LaTeXSizeNames[bits_.size()];
292 // Read a font definition from given file in lyx format
294 FontInfo lyxRead(Lexer & lex, FontInfo const & fi)
298 bool finished = false;
299 while (!finished && lex.isOK() && !error) {
301 string const tok = ascii_lowercase(lex.getString());
305 } else if (tok == "endfont") {
307 } else if (tok == "family") {
309 string const ttok = lex.getString();
310 setLyXFamily(ttok, f);
311 } else if (tok == "series") {
313 string const ttok = lex.getString();
314 setLyXSeries(ttok, f);
315 } else if (tok == "shape") {
317 string const ttok = lex.getString();
318 setLyXShape(ttok, f);
319 } else if (tok == "size") {
321 string const ttok = lex.getString();
323 } else if (tok == "misc") {
325 string const ttok = ascii_lowercase(lex.getString());
327 if (ttok == "no_bar") {
328 f.setUnderbar(FONT_OFF);
329 } else if (ttok == "no_emph") {
331 } else if (ttok == "no_noun") {
333 } else if (ttok == "emph") {
335 } else if (ttok == "underbar") {
336 f.setUnderbar(FONT_ON);
337 } else if (ttok == "noun") {
340 lex.printError("Illegal misc type `$$Token'");
342 } else if (tok == "color") {
344 string const ttok = lex.getString();
345 setLyXColor(ttok, f);
347 lex.printError("Unknown tag `$$Token'");
355 /// Writes the changes from this font to orgfont in .lyx format in file
356 void Font::lyxWriteChanges(Font const & orgfont,
360 if (orgfont.fontInfo().family() != bits_.family())
361 os << "\\family " << LyXFamilyNames[bits_.family()] << "\n";
362 if (orgfont.fontInfo().series() != bits_.series())
363 os << "\\series " << LyXSeriesNames[bits_.series()] << "\n";
364 if (orgfont.fontInfo().shape() != bits_.shape())
365 os << "\\shape " << LyXShapeNames[bits_.shape()] << "\n";
366 if (orgfont.fontInfo().size() != bits_.size())
367 os << "\\size " << LyXSizeNames[bits_.size()] << "\n";
368 if (orgfont.fontInfo().emph() != bits_.emph())
369 os << "\\emph " << LyXMiscNames[bits_.emph()] << "\n";
370 if (orgfont.fontInfo().number() != bits_.number())
371 os << "\\numeric " << LyXMiscNames[bits_.number()] << "\n";
372 if (orgfont.fontInfo().underbar() != bits_.underbar()) {
373 // This is only for backwards compatibility
374 switch (bits_.underbar()) {
375 case FONT_OFF: os << "\\bar no\n"; break;
376 case FONT_ON: os << "\\bar under\n"; break;
377 case FONT_TOGGLE: lyxerr << "Font::lyxWriteFontChanges: "
378 "FONT_TOGGLE should not appear here!"
381 case FONT_INHERIT: os << "\\bar default\n"; break;
382 case FONT_IGNORE: lyxerr << "Font::lyxWriteFontChanges: "
383 "IGNORE should not appear here!"
388 if (orgfont.fontInfo().noun() != bits_.noun()) {
389 os << "\\noun " << LyXMiscNames[bits_.noun()] << "\n";
391 if (orgfont.fontInfo().color() != bits_.color())
392 os << "\\color " << lcolor.getLyXName(bits_.color()) << '\n';
393 // FIXME: uncomment this when we support background.
394 //if (orgfont.fontInfo().background() != bits_.background())
395 // os << "\\color " << lcolor.getLyXName(bits_.background()) << '\n';
396 if (orgfont.language() != language() &&
397 language() != latex_language) {
399 os << "\\lang " << language()->lang() << "\n";
401 os << "\\lang unknown\n";
406 /// Writes the head of the LaTeX needed to impose this font
407 // Returns number of chars written.
408 int Font::latexWriteStartChanges(odocstream & os, BufferParams const & bparams,
409 OutputParams const & runparams,
411 Font const & prev) const
416 if (language()->babel() != base.language()->babel() &&
417 language() != prev.language()) {
418 if (language()->lang() == "farsi") {
421 } else if (!isRightToLeft() &&
422 base.language()->lang() == "farsi") {
425 } else if (language()->lang() == "arabic_arabi") {
428 } else if (!isRightToLeft() &&
429 base.language()->lang() == "arabic_arabi") {
432 // currently the remaining RTL languages are arabic_arabtex and hebrew
433 } else if (isRightToLeft() != prev.isRightToLeft()) {
434 if (isRightToLeft()) {
441 } else if (!language()->babel().empty()) {
443 subst(lyxrc.language_command_local,
444 "$$lang", language()->babel());
445 os << from_ascii(tmp);
446 count += tmp.length();
453 if (language()->encoding()->package() == Encoding::CJK) {
454 pair<bool, int> const c = switchEncoding(os, bparams,
455 runparams, *(language()->encoding()));
457 open_encoding_ = true;
459 runparams.encoding = language()->encoding();
463 // When the current language is Hebrew, Arabic, or Farsi
464 // the numbers are written Left-to-Right. ArabTeX package
465 // reorders the number automatically but the packages used
466 // for Hebrew and Farsi (Arabi) do not.
467 if (bits_.number() == FONT_ON && prev.fontInfo().number() != FONT_ON
468 && (language()->lang() == "hebrew"
469 || language()->lang() == "farsi"
470 || language()->lang() == "arabic_arabi")) {
476 f.reduce(base.bits_);
478 if (f.family() != INHERIT_FAMILY) {
480 << LaTeXFamilyNames[f.family()]
482 count += strlen(LaTeXFamilyNames[f.family()]) + 2;
483 env = true; //We have opened a new environment
485 if (f.series() != INHERIT_SERIES) {
487 << LaTeXSeriesNames[f.series()]
489 count += strlen(LaTeXSeriesNames[f.series()]) + 2;
490 env = true; //We have opened a new environment
492 if (f.shape() != INHERIT_SHAPE) {
494 << LaTeXShapeNames[f.shape()]
496 count += strlen(LaTeXShapeNames[f.shape()]) + 2;
497 env = true; //We have opened a new environment
499 if (f.color() != Color_inherit && f.color() != Color_ignore) {
501 << from_ascii(lcolor.getLaTeXName(f.color()))
503 count += lcolor.getLaTeXName(f.color()).length() + 13;
504 env = true; //We have opened a new environment
506 // FIXME: uncomment this when we support background.
508 if (f.background() != Color_inherit && f.background() != Color_ignore) {
510 << from_ascii(lcolor.getLaTeXName(f.background()))
512 count += lcolor.getLaTeXName(f.background()).length() + 13;
513 env = true; //We have opened a new environment
516 if (f.emph() == FONT_ON) {
519 env = true; //We have opened a new environment
521 if (f.underbar() == FONT_ON) {
524 env = true; //We have opened a new environment
526 // \noun{} is a LyX special macro
527 if (f.noun() == FONT_ON) {
530 env = true; //We have opened a new environment
532 if (f.size() != FONT_SIZE_INHERIT) {
533 // If we didn't open an environment above, we open one here
539 << LaTeXSizeNames[f.size()]
541 count += strlen(LaTeXSizeNames[f.size()]) + 2;
547 /// Writes ending block of LaTeX needed to close use of this font
548 // Returns number of chars written
549 // This one corresponds to latexWriteStartChanges(). (Asger)
550 int Font::latexWriteEndChanges(odocstream & os, BufferParams const & bparams,
551 OutputParams const & runparams,
554 bool const & closeLanguage) const
559 // reduce the current font to changes against the base
560 // font (of the layout). We use a temporary for this to
561 // avoid changing this font instance, as that would break
563 f.reduce(base.bits_);
565 if (f.family() != INHERIT_FAMILY) {
568 env = true; // Size change need not bother about closing env.
570 if (f.series() != INHERIT_SERIES) {
573 env = true; // Size change need not bother about closing env.
575 if (f.shape() != INHERIT_SHAPE) {
578 env = true; // Size change need not bother about closing env.
580 if (f.color() != Color_inherit && f.color() != Color_ignore) {
583 env = true; // Size change need not bother about closing env.
585 if (f.emph() == FONT_ON) {
588 env = true; // Size change need not bother about closing env.
590 if (f.underbar() == FONT_ON) {
593 env = true; // Size change need not bother about closing env.
595 if (f.noun() == FONT_ON) {
598 env = true; // Size change need not bother about closing env.
600 if (f.size() != FONT_SIZE_INHERIT) {
601 // We only have to close if only size changed
608 // When the current language is Hebrew, Arabic, or Farsi
609 // the numbers are written Left-to-Right. ArabTeX package
610 // reorders the number automatically but the packages used
611 // for Hebrew and Farsi (Arabi) do not.
612 if (bits_.number() == FONT_ON && next.fontInfo().number() != FONT_ON
613 && (language()->lang() == "hebrew"
614 || language()->lang() == "farsi"
615 || language()->lang() == "arabic_arabi")) {
620 if (open_encoding_) {
621 // We need to close the encoding even if it does not change
622 // to do correct environment nesting
623 Encoding const * const ascii = encodings.fromLyXName("ascii");
624 pair<bool, int> const c = switchEncoding(os, bparams,
626 BOOST_ASSERT(c.first);
628 runparams.encoding = ascii;
629 open_encoding_ = false;
633 language() != base.language() && language() != next.language()) {
642 string Font::toString(bool const toggle) const
644 string lang = "ignore";
646 lang = language()->lang();
649 os << "family " << bits_.family() << '\n'
650 << "series " << bits_.series() << '\n'
651 << "shape " << bits_.shape() << '\n'
652 << "size " << bits_.size() << '\n'
653 << "emph " << bits_.emph() << '\n'
654 << "underbar " << bits_.underbar() << '\n'
655 << "noun " << bits_.noun() << '\n'
656 << "number " << bits_.number() << '\n'
657 << "color " << bits_.color() << '\n'
658 << "language " << lang << '\n'
659 << "toggleall " << convert<string>(toggle);
664 bool Font::fromString(string const & data, bool & toggle)
666 istringstream is(data);
674 token = lex.getString();
676 if (token.empty() || !lex.next())
679 if (token == "family") {
680 int const next = lex.getInteger();
681 bits_.setFamily(FontFamily(next));
683 } else if (token == "series") {
684 int const next = lex.getInteger();
685 bits_.setSeries(FontSeries(next));
687 } else if (token == "shape") {
688 int const next = lex.getInteger();
689 bits_.setShape(FontShape(next));
691 } else if (token == "size") {
692 int const next = lex.getInteger();
693 bits_.setSize(FontSize(next));
695 } else if (token == "emph" || token == "underbar" ||
696 token == "noun" || token == "number") {
698 int const next = lex.getInteger();
699 FontState const misc = FontState(next);
703 else if (token == "underbar")
704 bits_.setUnderbar(misc);
705 else if (token == "noun")
707 else if (token == "number")
708 bits_.setNumber(misc);
710 } else if (token == "color") {
711 int const next = lex.getInteger();
712 bits_.setColor(ColorCode(next));
715 } else if (token == "background") {
716 int const next = lex.getInteger();
717 bits_.setBackground(ColorCode(next));
720 } else if (token == "language") {
721 string const next = lex.getString();
722 if (next == "ignore")
723 setLanguage(ignore_language);
725 setLanguage(languages.getLanguage(next));
727 } else if (token == "toggleall") {
728 toggle = lex.getBool();
731 // Unrecognised token
741 void Font::validate(LaTeXFeatures & features) const
743 BufferParams const & bparams = features.bufferParams();
744 Language const * doc_language = bparams.language;
746 if (bits_.noun() == FONT_ON) {
747 LYXERR(Debug::LATEX, "font.noun: " << bits_.noun());
748 features.require("noun");
749 LYXERR(Debug::LATEX, "Noun enabled. Font: " << to_utf8(stateText(0)));
751 switch (bits_.color()) {
755 // probably we should put here all interface colors used for
756 // font displaying! For now I just add this ones I know of (Jug)
758 case Color_notelabel:
761 features.require("color");
762 LYXERR(Debug::LATEX, "Color enabled. Font: " << to_utf8(stateText(0)));
765 // FIXME: Do something for background and soul package?
767 if (lang_->babel() != doc_language->babel() &&
768 lang_ != ignore_language &&
769 lang_ != latex_language)
771 features.useLanguage(lang_);
772 LYXERR(Debug::LATEX, "Found language " << lang_->lang());
777 ostream & operator<<(ostream & os, FontState fms)
779 return os << int(fms);
783 ostream & operator<<(ostream & os, FontInfo const & f)
786 << " family " << f.family()
787 << " series " << f.series()
788 << " shape " << f.shape()
789 << " size " << f.size()
790 << " color " << f.color()
791 // FIXME: uncomment this when we support background.
792 //<< " background " << f.background()
793 << " emph " << f.emph()
794 << " underbar " << f.underbar()
795 << " noun " << f.noun()
796 << " number " << f.number();
800 ostream & operator<<(ostream & os, Font const & font)
802 return os << font.bits_
803 << " lang: " << (font.lang_ ? font.lang_->lang() : 0);