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, *(language()->encoding()));
455 open_encoding_ = true;
457 runparams.encoding = language()->encoding();
461 // When the current language is Hebrew, Arabic, or Farsi
462 // the numbers are written Left-to-Right. ArabTeX package
463 // reorders the number automatically but the packages used
464 // for Hebrew and Farsi (Arabi) do not.
465 if (bits_.number() == FONT_ON && prev.fontInfo().number() != FONT_ON
466 && (language()->lang() == "hebrew"
467 || language()->lang() == "farsi"
468 || language()->lang() == "arabic_arabi")) {
474 f.reduce(base.bits_);
476 if (f.family() != INHERIT_FAMILY) {
478 << LaTeXFamilyNames[f.family()]
480 count += strlen(LaTeXFamilyNames[f.family()]) + 2;
481 env = true; //We have opened a new environment
483 if (f.series() != INHERIT_SERIES) {
485 << LaTeXSeriesNames[f.series()]
487 count += strlen(LaTeXSeriesNames[f.series()]) + 2;
488 env = true; //We have opened a new environment
490 if (f.shape() != INHERIT_SHAPE) {
492 << LaTeXShapeNames[f.shape()]
494 count += strlen(LaTeXShapeNames[f.shape()]) + 2;
495 env = true; //We have opened a new environment
497 if (f.color() != Color_inherit && f.color() != Color_ignore) {
499 << from_ascii(lcolor.getLaTeXName(f.color()))
501 count += lcolor.getLaTeXName(f.color()).length() + 13;
502 env = true; //We have opened a new environment
504 // FIXME: uncomment this when we support background.
506 if (f.background() != Color_inherit && f.background() != Color_ignore) {
508 << from_ascii(lcolor.getLaTeXName(f.background()))
510 count += lcolor.getLaTeXName(f.background()).length() + 13;
511 env = true; //We have opened a new environment
514 if (f.emph() == FONT_ON) {
517 env = true; //We have opened a new environment
519 if (f.underbar() == FONT_ON) {
522 env = true; //We have opened a new environment
524 // \noun{} is a LyX special macro
525 if (f.noun() == FONT_ON) {
528 env = true; //We have opened a new environment
530 if (f.size() != FONT_SIZE_INHERIT) {
531 // If we didn't open an environment above, we open one here
537 << LaTeXSizeNames[f.size()]
539 count += strlen(LaTeXSizeNames[f.size()]) + 2;
545 /// Writes ending block of LaTeX needed to close use of this font
546 // Returns number of chars written
547 // This one corresponds to latexWriteStartChanges(). (Asger)
548 int Font::latexWriteEndChanges(odocstream & os, BufferParams const & bparams,
549 OutputParams const & runparams,
552 bool const & closeLanguage) const
557 // reduce the current font to changes against the base
558 // font (of the layout). We use a temporary for this to
559 // avoid changing this font instance, as that would break
561 f.reduce(base.bits_);
563 if (f.family() != INHERIT_FAMILY) {
566 env = true; // Size change need not bother about closing env.
568 if (f.series() != INHERIT_SERIES) {
571 env = true; // Size change need not bother about closing env.
573 if (f.shape() != INHERIT_SHAPE) {
576 env = true; // Size change need not bother about closing env.
578 if (f.color() != Color_inherit && f.color() != Color_ignore) {
581 env = true; // Size change need not bother about closing env.
583 if (f.emph() == FONT_ON) {
586 env = true; // Size change need not bother about closing env.
588 if (f.underbar() == FONT_ON) {
591 env = true; // Size change need not bother about closing env.
593 if (f.noun() == FONT_ON) {
596 env = true; // Size change need not bother about closing env.
598 if (f.size() != FONT_SIZE_INHERIT) {
599 // We only have to close if only size changed
606 // When the current language is Hebrew, Arabic, or Farsi
607 // the numbers are written Left-to-Right. ArabTeX package
608 // reorders the number automatically but the packages used
609 // for Hebrew and Farsi (Arabi) do not.
610 if (bits_.number() == FONT_ON && next.fontInfo().number() != FONT_ON
611 && (language()->lang() == "hebrew"
612 || language()->lang() == "farsi"
613 || language()->lang() == "arabic_arabi")) {
618 if (open_encoding_) {
619 // We need to close the encoding even if it does not change
620 // to do correct environment nesting
621 Encoding const * const ascii = encodings.getFromLyXName("ascii");
622 pair<bool, int> const c = switchEncoding(os, bparams,
624 BOOST_ASSERT(c.first);
626 runparams.encoding = ascii;
627 open_encoding_ = false;
631 language() != base.language() && language() != next.language()) {
640 string Font::toString(bool const toggle) const
642 string lang = "ignore";
644 lang = language()->lang();
647 os << "family " << bits_.family() << '\n'
648 << "series " << bits_.series() << '\n'
649 << "shape " << bits_.shape() << '\n'
650 << "size " << bits_.size() << '\n'
651 << "emph " << bits_.emph() << '\n'
652 << "underbar " << bits_.underbar() << '\n'
653 << "noun " << bits_.noun() << '\n'
654 << "number " << bits_.number() << '\n'
655 << "color " << bits_.color() << '\n'
656 << "language " << lang << '\n'
657 << "toggleall " << convert<string>(toggle);
662 bool Font::fromString(string const & data, bool & toggle)
664 istringstream is(data);
672 token = lex.getString();
674 if (token.empty() || !lex.next())
677 if (token == "family") {
678 int const next = lex.getInteger();
679 bits_.setFamily(FontFamily(next));
681 } else if (token == "series") {
682 int const next = lex.getInteger();
683 bits_.setSeries(FontSeries(next));
685 } else if (token == "shape") {
686 int const next = lex.getInteger();
687 bits_.setShape(FontShape(next));
689 } else if (token == "size") {
690 int const next = lex.getInteger();
691 bits_.setSize(FontSize(next));
693 } else if (token == "emph" || token == "underbar" ||
694 token == "noun" || token == "number") {
696 int const next = lex.getInteger();
697 FontState const misc = FontState(next);
701 else if (token == "underbar")
702 bits_.setUnderbar(misc);
703 else if (token == "noun")
705 else if (token == "number")
706 bits_.setNumber(misc);
708 } else if (token == "color") {
709 int const next = lex.getInteger();
710 bits_.setColor(ColorCode(next));
713 } else if (token == "background") {
714 int const next = lex.getInteger();
715 bits_.setBackground(ColorCode(next));
718 } else if (token == "language") {
719 string const next = lex.getString();
720 if (next == "ignore")
721 setLanguage(ignore_language);
723 setLanguage(languages.getLanguage(next));
725 } else if (token == "toggleall") {
726 toggle = lex.getBool();
729 // Unrecognised token
739 void Font::validate(LaTeXFeatures & features) const
741 BufferParams const & bparams = features.bufferParams();
742 Language const * doc_language = bparams.language;
744 if (bits_.noun() == FONT_ON) {
745 LYXERR(Debug::LATEX, "font.noun: " << bits_.noun());
746 features.require("noun");
747 LYXERR(Debug::LATEX, "Noun enabled. Font: " << to_utf8(stateText(0)));
749 switch (bits_.color()) {
753 // probably we should put here all interface colors used for
754 // font displaying! For now I just add this ones I know of (Jug)
759 features.require("color");
760 LYXERR(Debug::LATEX, "Color enabled. Font: " << to_utf8(stateText(0)));
763 // FIXME: Do something for background and soul package?
765 if (lang_->babel() != doc_language->babel() &&
766 lang_ != ignore_language &&
767 lang_ != latex_language)
769 features.useLanguage(lang_);
770 LYXERR(Debug::LATEX, "Found language " << lang_->lang());
775 ostream & operator<<(ostream & os, FontState fms)
777 return os << int(fms);
781 ostream & operator<<(ostream & os, FontInfo const & f)
784 << " family " << f.family()
785 << " series " << f.series()
786 << " shape " << f.shape()
787 << " size " << f.size()
788 << " color " << f.color()
789 // FIXME: uncomment this when we support background.
790 //<< " background " << f.background()
791 << " emph " << f.emph()
792 << " underbar " << f.underbar()
793 << " noun " << f.noun()
794 << " number " << f.number();
798 ostream & operator<<(ostream & os, Font const & font)
800 return os << font.bits_
801 << " lang: " << (font.lang_ ? font.lang_->lang() : 0);