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"
35 using namespace lyx::support;
45 char const * GUIFamilyNames[NUM_FAMILIES + 2 /* default & error */] =
46 { N_("Roman"), N_("Sans Serif"), N_("Typewriter"), N_("Symbol"),
47 "cmr", "cmsy", "cmm", "cmex", "msa", "msb", "eufrak", "wasy", "esint",
48 N_("Inherit"), N_("Ignore") };
50 char const * GUISeriesNames[4] =
51 { N_("Medium"), N_("Bold"), N_("Inherit"), N_("Ignore") };
53 char const * GUIShapeNames[6] =
54 { N_("Upright"), N_("Italic"), N_("Slanted"), N_("Smallcaps"), N_("Inherit"),
57 char const * GUISizeNames[14] =
58 { N_("Tiny"), N_("Smallest"), N_("Smaller"), N_("Small"), N_("Normal"), N_("Large"),
59 N_("Larger"), N_("Largest"), N_("Huge"), N_("Huger"), N_("Increase"), N_("Decrease"),
60 N_("Inherit"), N_("Ignore") };
62 char const * GUIMiscNames[5] =
63 { N_("Off"), N_("On"), N_("Toggle"), N_("Inherit"), N_("Ignore") };
67 // Strings used to read and write .lyx format files
69 char const * LyXFamilyNames[NUM_FAMILIES + 2 /* default & error */] =
70 { "roman", "sans", "typewriter", "symbol",
71 "cmr", "cmsy", "cmm", "cmex", "msa", "msb", "eufrak", "wasy", "esint",
74 char const * LyXSeriesNames[4] =
75 { "medium", "bold", "default", "error" };
77 char const * LyXShapeNames[6] =
78 { "up", "italic", "slanted", "smallcaps", "default", "error" };
80 char const * LyXSizeNames[14] =
81 { "tiny", "scriptsize", "footnotesize", "small", "normal", "large",
82 "larger", "largest", "huge", "giant",
83 "increase", "decrease", "default", "error" };
85 char const * LyXMiscNames[5] =
86 { "off", "on", "toggle", "default", "error" };
89 // Strings used to write LaTeX files
91 char const * LaTeXFamilyNames[6] =
92 { "textrm", "textsf", "texttt", "error1", "error2", "error3" };
94 char const * LaTeXSeriesNames[4] =
95 { "textmd", "textbf", "error4", "error5" };
97 char const * LaTeXShapeNames[6] =
98 { "textup", "textit", "textsl", "textsc", "error6", "error7" };
100 char const * LaTeXSizeNames[14] =
101 { "tiny", "scriptsize", "footnotesize", "small", "normalsize", "large",
102 "Large", "LARGE", "huge", "Huge", "error8", "error9", "error10", "error11" };
107 Font::Font(FontInfo bits, Language const * l)
108 : bits_(bits), lang_(l), open_encoding_(false)
111 lang_ = default_language;
115 bool Font::isRightToLeft() const
117 return lang_->rightToLeft();
121 bool Font::isVisibleRightToLeft() const
123 return (lang_->rightToLeft() &&
124 bits_.number() != FONT_ON);
128 void Font::setLanguage(Language const * l)
134 /// Updates font settings according to request
135 void Font::update(Font const & newfont,
136 Language const * document_language,
139 bits_.update(newfont.fontInfo(), toggleall);
141 if (newfont.language() == language() && toggleall)
142 if (language() == document_language)
143 setLanguage(default_language);
145 setLanguage(document_language);
146 else if (newfont.language() != ignore_language)
147 setLanguage(newfont.language());
151 docstring const stateText(FontInfo const & f)
154 if (f.family() != INHERIT_FAMILY)
155 os << _(GUIFamilyNames[f.family()]) << ", ";
156 if (f.series() != INHERIT_SERIES)
157 os << _(GUISeriesNames[f.series()]) << ", ";
158 if (f.shape() != INHERIT_SHAPE)
159 os << _(GUIShapeNames[f.shape()]) << ", ";
160 if (f.size() != FONT_SIZE_INHERIT)
161 os << _(GUISizeNames[f.size()]) << ", ";
162 if (f.color() != Color_inherit)
163 os << lcolor.getGUIName(f.color()) << ", ";
164 // FIXME: uncomment this when we support background.
165 //if (f.background() != Color_inherit)
166 // os << lcolor.getGUIName(f.background()) << ", ";
167 if (f.emph() != FONT_INHERIT)
168 os << bformat(_("Emphasis %1$s, "),
169 _(GUIMiscNames[f.emph()]));
170 if (f.underbar() != FONT_INHERIT)
171 os << bformat(_("Underline %1$s, "),
172 _(GUIMiscNames[f.underbar()]));
173 if (f.noun() != FONT_INHERIT)
174 os << bformat(_("Noun %1$s, "),
175 _(GUIMiscNames[f.noun()]));
176 if (f == inherit_font)
177 os << _("Default") << ", ";
183 docstring const Font::stateText(BufferParams * params) const
186 os << lyx::stateText(bits_);
187 if (!params || (language() != params->language))
188 os << bformat(_("Language: %1$s, "),
189 _(language()->display()));
190 if (bits_.number() != FONT_OFF)
191 os << bformat(_(" Number %1$s"),
192 _(GUIMiscNames[bits_.number()]));
193 return rtrim(os.str(), ", ");
197 // Set family according to lyx format string
198 void setLyXFamily(string const & fam, FontInfo & f)
200 string const s = ascii_lowercase(fam);
203 while (LyXFamilyNames[i] != s &&
204 LyXFamilyNames[i] != string("error"))
206 if (s == LyXFamilyNames[i])
207 f.setFamily(FontFamily(i));
209 lyxerr << "setLyXFamily: Unknown family `"
210 << s << '\'' << endl;
214 // Set series according to lyx format string
215 void setLyXSeries(string const & ser, FontInfo & f)
217 string const s = ascii_lowercase(ser);
220 while (LyXSeriesNames[i] != s &&
221 LyXSeriesNames[i] != string("error")) ++i;
222 if (s == LyXSeriesNames[i]) {
223 f.setSeries(FontSeries(i));
225 lyxerr << "setLyXSeries: Unknown series `"
226 << s << '\'' << endl;
230 // Set shape according to lyx format string
231 void setLyXShape(string const & sha, FontInfo & f)
233 string const s = ascii_lowercase(sha);
236 while (LyXShapeNames[i] != s && LyXShapeNames[i] != string("error"))
238 if (s == LyXShapeNames[i])
239 f.setShape(FontShape(i));
241 lyxerr << "Font::setLyXShape: Unknown shape `"
242 << s << '\'' << endl;
246 // Set size according to lyx format string
247 void setLyXSize(string const & siz, FontInfo & f)
249 string const s = ascii_lowercase(siz);
251 while (LyXSizeNames[i] != s && LyXSizeNames[i] != string("error"))
253 if (s == LyXSizeNames[i]) {
254 f.setSize(FontSize(i));
256 lyxerr << "Font::setLyXSize: Unknown size `"
257 << s << '\'' << endl;
261 // Set size according to lyx format string
262 FontState Font::setLyXMisc(string const & siz)
264 string const s = ascii_lowercase(siz);
266 while (LyXMiscNames[i] != s &&
267 LyXMiscNames[i] != string("error")) ++i;
268 if (s == LyXMiscNames[i])
270 lyxerr << "Font::setLyXMisc: Unknown misc flag `"
271 << s << '\'' << endl;
276 /// Sets color after LyX text format
277 void setLyXColor(string const & col, FontInfo & f)
279 f.setColor(lcolor.getFromLyXName(col));
283 // Returns size in latex format
284 string const Font::latexSize() const
286 return LaTeXSizeNames[bits_.size()];
290 // Read a font definition from given file in lyx format
292 FontInfo lyxRead(Lexer & lex, FontInfo const & fi)
296 bool finished = false;
297 while (!finished && lex.isOK() && !error) {
299 string const tok = ascii_lowercase(lex.getString());
303 } else if (tok == "endfont") {
305 } else if (tok == "family") {
307 string const ttok = lex.getString();
308 setLyXFamily(ttok, f);
309 } else if (tok == "series") {
311 string const ttok = lex.getString();
312 setLyXSeries(ttok, f);
313 } else if (tok == "shape") {
315 string const ttok = lex.getString();
316 setLyXShape(ttok, f);
317 } else if (tok == "size") {
319 string const ttok = lex.getString();
321 } else if (tok == "misc") {
323 string const ttok = ascii_lowercase(lex.getString());
325 if (ttok == "no_bar") {
326 f.setUnderbar(FONT_OFF);
327 } else if (ttok == "no_emph") {
329 } else if (ttok == "no_noun") {
331 } else if (ttok == "emph") {
333 } else if (ttok == "underbar") {
334 f.setUnderbar(FONT_ON);
335 } else if (ttok == "noun") {
338 lex.printError("Illegal misc type `$$Token'");
340 } else if (tok == "color") {
342 string const ttok = lex.getString();
343 setLyXColor(ttok, f);
345 lex.printError("Unknown tag `$$Token'");
353 /// Writes the changes from this font to orgfont in .lyx format in file
354 void Font::lyxWriteChanges(Font const & orgfont,
358 if (orgfont.fontInfo().family() != bits_.family())
359 os << "\\family " << LyXFamilyNames[bits_.family()] << "\n";
360 if (orgfont.fontInfo().series() != bits_.series())
361 os << "\\series " << LyXSeriesNames[bits_.series()] << "\n";
362 if (orgfont.fontInfo().shape() != bits_.shape())
363 os << "\\shape " << LyXShapeNames[bits_.shape()] << "\n";
364 if (orgfont.fontInfo().size() != bits_.size())
365 os << "\\size " << LyXSizeNames[bits_.size()] << "\n";
366 if (orgfont.fontInfo().emph() != bits_.emph())
367 os << "\\emph " << LyXMiscNames[bits_.emph()] << "\n";
368 if (orgfont.fontInfo().number() != bits_.number())
369 os << "\\numeric " << LyXMiscNames[bits_.number()] << "\n";
370 if (orgfont.fontInfo().underbar() != bits_.underbar()) {
371 // This is only for backwards compatibility
372 switch (bits_.underbar()) {
373 case FONT_OFF: os << "\\bar no\n"; break;
374 case FONT_ON: os << "\\bar under\n"; break;
375 case FONT_TOGGLE: lyxerr << "Font::lyxWriteFontChanges: "
376 "FONT_TOGGLE should not appear here!"
379 case FONT_INHERIT: os << "\\bar default\n"; break;
380 case FONT_IGNORE: lyxerr << "Font::lyxWriteFontChanges: "
381 "IGNORE should not appear here!"
386 if (orgfont.fontInfo().noun() != bits_.noun()) {
387 os << "\\noun " << LyXMiscNames[bits_.noun()] << "\n";
389 if (orgfont.fontInfo().color() != bits_.color())
390 os << "\\color " << lcolor.getLyXName(bits_.color()) << '\n';
391 // FIXME: uncomment this when we support background.
392 //if (orgfont.fontInfo().background() != bits_.background())
393 // os << "\\color " << lcolor.getLyXName(bits_.background()) << '\n';
394 if (orgfont.language() != language() &&
395 language() != latex_language) {
397 os << "\\lang " << language()->lang() << "\n";
399 os << "\\lang unknown\n";
404 /// Writes the head of the LaTeX needed to impose this font
405 // Returns number of chars written.
406 int Font::latexWriteStartChanges(odocstream & os, BufferParams const & bparams,
407 OutputParams const & runparams,
409 Font const & prev) const
414 if (language()->babel() != base.language()->babel() &&
415 language() != prev.language()) {
416 if (language()->lang() == "farsi") {
419 } else if (!isRightToLeft() &&
420 base.language()->lang() == "farsi") {
423 } else if (language()->lang() == "arabic_arabi") {
426 } else if (!isRightToLeft() &&
427 base.language()->lang() == "arabic_arabi") {
430 // currently the remaining RTL languages are arabic_arabtex and hebrew
431 } else if (isRightToLeft() != prev.isRightToLeft()) {
432 if (isRightToLeft()) {
439 } else if (!language()->babel().empty()) {
441 subst(lyxrc.language_command_local,
442 "$$lang", language()->babel());
443 os << from_ascii(tmp);
444 count += tmp.length();
451 if (language()->encoding()->package() == Encoding::CJK) {
452 pair<bool, int> const c = switchEncoding(os, bparams,
453 runparams, *(runparams.encoding),
454 *(language()->encoding()));
456 open_encoding_ = true;
458 runparams.encoding = language()->encoding();
462 // When the current language is Hebrew, Arabic, or Farsi
463 // the numbers are written Left-to-Right. ArabTeX package
464 // reorders the number automatically but the packages used
465 // for Hebrew and Farsi (Arabi) do not.
466 if (bits_.number() == FONT_ON && prev.fontInfo().number() != FONT_ON
467 && (language()->lang() == "hebrew"
468 || language()->lang() == "farsi"
469 || language()->lang() == "arabic_arabi")) {
475 f.reduce(base.bits_);
477 if (f.family() != INHERIT_FAMILY) {
479 << LaTeXFamilyNames[f.family()]
481 count += strlen(LaTeXFamilyNames[f.family()]) + 2;
482 env = true; //We have opened a new environment
484 if (f.series() != INHERIT_SERIES) {
486 << LaTeXSeriesNames[f.series()]
488 count += strlen(LaTeXSeriesNames[f.series()]) + 2;
489 env = true; //We have opened a new environment
491 if (f.shape() != INHERIT_SHAPE) {
493 << LaTeXShapeNames[f.shape()]
495 count += strlen(LaTeXShapeNames[f.shape()]) + 2;
496 env = true; //We have opened a new environment
498 if (f.color() != Color_inherit && f.color() != Color_ignore) {
500 << from_ascii(lcolor.getLaTeXName(f.color()))
502 count += lcolor.getLaTeXName(f.color()).length() + 13;
503 env = true; //We have opened a new environment
505 // FIXME: uncomment this when we support background.
507 if (f.background() != Color_inherit && f.background() != Color_ignore) {
509 << from_ascii(lcolor.getLaTeXName(f.background()))
511 count += lcolor.getLaTeXName(f.background()).length() + 13;
512 env = true; //We have opened a new environment
515 if (f.emph() == FONT_ON) {
518 env = true; //We have opened a new environment
520 if (f.underbar() == FONT_ON) {
523 env = true; //We have opened a new environment
525 // \noun{} is a LyX special macro
526 if (f.noun() == FONT_ON) {
529 env = true; //We have opened a new environment
531 if (f.size() != FONT_SIZE_INHERIT) {
532 // If we didn't open an environment above, we open one here
538 << LaTeXSizeNames[f.size()]
540 count += strlen(LaTeXSizeNames[f.size()]) + 2;
546 /// Writes ending block of LaTeX needed to close use of this font
547 // Returns number of chars written
548 // This one corresponds to latexWriteStartChanges(). (Asger)
549 int Font::latexWriteEndChanges(odocstream & os, BufferParams const & bparams,
550 OutputParams const & runparams,
553 bool const & closeLanguage) const
558 // reduce the current font to changes against the base
559 // font (of the layout). We use a temporary for this to
560 // avoid changing this font instance, as that would break
562 f.reduce(base.bits_);
564 if (f.family() != INHERIT_FAMILY) {
567 env = true; // Size change need not bother about closing env.
569 if (f.series() != INHERIT_SERIES) {
572 env = true; // Size change need not bother about closing env.
574 if (f.shape() != INHERIT_SHAPE) {
577 env = true; // Size change need not bother about closing env.
579 if (f.color() != Color_inherit && f.color() != Color_ignore) {
582 env = true; // Size change need not bother about closing env.
584 if (f.emph() == FONT_ON) {
587 env = true; // Size change need not bother about closing env.
589 if (f.underbar() == FONT_ON) {
592 env = true; // Size change need not bother about closing env.
594 if (f.noun() == FONT_ON) {
597 env = true; // Size change need not bother about closing env.
599 if (f.size() != FONT_SIZE_INHERIT) {
600 // We only have to close if only size changed
607 // When the current language is Hebrew, Arabic, or Farsi
608 // the numbers are written Left-to-Right. ArabTeX package
609 // reorders the number automatically but the packages used
610 // for Hebrew and Farsi (Arabi) do not.
611 if (bits_.number() == FONT_ON && next.fontInfo().number() != FONT_ON
612 && (language()->lang() == "hebrew"
613 || language()->lang() == "farsi"
614 || language()->lang() == "arabic_arabi")) {
619 if (open_encoding_) {
620 // We need to close the encoding even if it does not change
621 // to do correct environment nesting
622 Encoding const * const ascii = encodings.getFromLyXName("ascii");
623 pair<bool, int> const c = switchEncoding(os, bparams,
624 runparams, *(runparams.encoding),
626 BOOST_ASSERT(c.first);
628 runparams.encoding = ascii;
629 open_encoding_ = false;
633 language() != base.language() && language() != next.language()) {
642 std::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)
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<<(std::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 std::ostream & operator<<(std::ostream & os, Font const & font)
802 return os << font.bits_
803 << " lang: " << (font.lang_ ? font.lang_->lang() : 0);