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/assert.h"
30 #include "support/convert.h"
31 #include "support/debug.h"
32 #include "support/gettext.h"
33 #include "support/lstrings.h"
38 using namespace lyx::support;
48 char const * GUIFamilyNames[NUM_FAMILIES + 2 /* default & error */] =
49 { N_("Roman"), N_("Sans Serif"), N_("Typewriter"), N_("Symbol"),
50 "cmr", "cmsy", "cmm", "cmex", "msa", "msb", "eufrak", "wasy", "esint",
51 N_("Inherit"), N_("Ignore") };
53 char const * GUISeriesNames[4] =
54 { N_("Medium"), N_("Bold"), N_("Inherit"), N_("Ignore") };
56 char const * GUIShapeNames[6] =
57 { N_("Upright"), N_("Italic"), N_("Slanted"), N_("Smallcaps"), N_("Inherit"),
60 char const * GUISizeNames[14] =
61 { N_("Tiny"), N_("Smallest"), N_("Smaller"), N_("Small"), N_("Normal"), N_("Large"),
62 N_("Larger"), N_("Largest"), N_("Huge"), N_("Huger"), N_("Increase"), N_("Decrease"),
63 N_("Inherit"), N_("Ignore") };
65 char const * GUIMiscNames[5] =
66 { N_("Off"), N_("On"), N_("Toggle"), N_("Inherit"), N_("Ignore") };
70 // Strings used to read and write .lyx format files
72 char const * LyXFamilyNames[NUM_FAMILIES + 2 /* default & error */] =
73 { "roman", "sans", "typewriter", "symbol",
74 "cmr", "cmsy", "cmm", "cmex", "msa", "msb", "eufrak", "wasy", "esint",
77 char const * LyXSeriesNames[4] =
78 { "medium", "bold", "default", "error" };
80 char const * LyXShapeNames[6] =
81 { "up", "italic", "slanted", "smallcaps", "default", "error" };
83 char const * LyXSizeNames[14] =
84 { "tiny", "scriptsize", "footnotesize", "small", "normal", "large",
85 "larger", "largest", "huge", "giant",
86 "increase", "decrease", "default", "error" };
88 char const * LyXMiscNames[5] =
89 { "off", "on", "toggle", "default", "error" };
92 // Strings used to write LaTeX files
94 char const * LaTeXFamilyNames[6] =
95 { "textrm", "textsf", "texttt", "error1", "error2", "error3" };
97 char const * LaTeXSeriesNames[4] =
98 { "textmd", "textbf", "error4", "error5" };
100 char const * LaTeXShapeNames[6] =
101 { "textup", "textit", "textsl", "textsc", "error6", "error7" };
103 char const * LaTeXSizeNames[14] =
104 { "tiny", "scriptsize", "footnotesize", "small", "normalsize", "large",
105 "Large", "LARGE", "huge", "Huge", "error8", "error9", "error10", "error11" };
110 Font::Font(FontInfo bits, Language const * l)
111 : bits_(bits), lang_(l), open_encoding_(false)
114 lang_ = default_language;
118 bool Font::isRightToLeft() const
120 return lang_->rightToLeft();
124 bool Font::isVisibleRightToLeft() const
126 return (lang_->rightToLeft() &&
127 bits_.number() != FONT_ON);
131 void Font::setLanguage(Language const * l)
137 /// Updates font settings according to request
138 void Font::update(Font const & newfont,
139 Language const * document_language,
142 bits_.update(newfont.fontInfo(), toggleall);
144 if (newfont.language() == language() && toggleall)
145 if (language() == document_language)
146 setLanguage(default_language);
148 setLanguage(document_language);
149 else if (newfont.language() != ignore_language)
150 setLanguage(newfont.language());
154 docstring const stateText(FontInfo const & f)
157 if (f.family() != INHERIT_FAMILY)
158 os << _(GUIFamilyNames[f.family()]) << ", ";
159 if (f.series() != INHERIT_SERIES)
160 os << _(GUISeriesNames[f.series()]) << ", ";
161 if (f.shape() != INHERIT_SHAPE)
162 os << _(GUIShapeNames[f.shape()]) << ", ";
163 if (f.size() != FONT_SIZE_INHERIT)
164 os << _(GUISizeNames[f.size()]) << ", ";
165 if (f.color() != Color_inherit)
166 os << lcolor.getGUIName(f.color()) << ", ";
167 // FIXME: uncomment this when we support background.
168 //if (f.background() != Color_inherit)
169 // os << lcolor.getGUIName(f.background()) << ", ";
170 if (f.emph() != FONT_INHERIT)
171 os << bformat(_("Emphasis %1$s, "),
172 _(GUIMiscNames[f.emph()]));
173 if (f.underbar() != FONT_INHERIT)
174 os << bformat(_("Underline %1$s, "),
175 _(GUIMiscNames[f.underbar()]));
176 if (f.noun() != FONT_INHERIT)
177 os << bformat(_("Noun %1$s, "),
178 _(GUIMiscNames[f.noun()]));
179 if (f == inherit_font)
180 os << _("Default") << ", ";
186 docstring const Font::stateText(BufferParams * params) const
189 os << lyx::stateText(bits_);
190 if (!params || (language() != params->language))
191 os << bformat(_("Language: %1$s, "),
192 _(language()->display()));
193 if (bits_.number() != FONT_OFF)
194 os << bformat(_(" Number %1$s"),
195 _(GUIMiscNames[bits_.number()]));
196 return rtrim(os.str(), ", ");
200 // Set family according to lyx format string
201 void setLyXFamily(string const & fam, FontInfo & f)
203 string const s = ascii_lowercase(fam);
206 while (LyXFamilyNames[i] != s &&
207 LyXFamilyNames[i] != string("error"))
209 if (s == LyXFamilyNames[i])
210 f.setFamily(FontFamily(i));
212 lyxerr << "setLyXFamily: Unknown family `"
213 << s << '\'' << endl;
217 // Set series according to lyx format string
218 void setLyXSeries(string const & ser, FontInfo & f)
220 string const s = ascii_lowercase(ser);
223 while (LyXSeriesNames[i] != s &&
224 LyXSeriesNames[i] != string("error")) ++i;
225 if (s == LyXSeriesNames[i]) {
226 f.setSeries(FontSeries(i));
228 lyxerr << "setLyXSeries: Unknown series `"
229 << s << '\'' << endl;
233 // Set shape according to lyx format string
234 void setLyXShape(string const & sha, FontInfo & f)
236 string const s = ascii_lowercase(sha);
239 while (LyXShapeNames[i] != s && LyXShapeNames[i] != string("error"))
241 if (s == LyXShapeNames[i])
242 f.setShape(FontShape(i));
244 lyxerr << "Font::setLyXShape: Unknown shape `"
245 << s << '\'' << endl;
249 // Set size according to lyx format string
250 void setLyXSize(string const & siz, FontInfo & f)
252 string const s = ascii_lowercase(siz);
254 while (LyXSizeNames[i] != s && LyXSizeNames[i] != string("error"))
256 if (s == LyXSizeNames[i]) {
257 f.setSize(FontSize(i));
259 lyxerr << "Font::setLyXSize: Unknown size `"
260 << s << '\'' << endl;
264 // Set size according to lyx format string
265 FontState Font::setLyXMisc(string const & siz)
267 string const s = ascii_lowercase(siz);
269 while (LyXMiscNames[i] != s &&
270 LyXMiscNames[i] != string("error")) ++i;
271 if (s == LyXMiscNames[i])
273 lyxerr << "Font::setLyXMisc: Unknown misc flag `"
274 << s << '\'' << endl;
279 /// Sets color after LyX text format
280 void setLyXColor(string const & col, FontInfo & f)
282 f.setColor(lcolor.getFromLyXName(col));
286 // Returns size in latex format
287 string const Font::latexSize() const
289 return LaTeXSizeNames[bits_.size()];
293 // Read a font definition from given file in lyx format
295 FontInfo lyxRead(Lexer & lex, FontInfo const & fi)
299 bool finished = false;
300 while (!finished && lex.isOK() && !error) {
302 string const tok = ascii_lowercase(lex.getString());
306 } else if (tok == "endfont") {
308 } else if (tok == "family") {
310 string const ttok = lex.getString();
311 setLyXFamily(ttok, f);
312 } else if (tok == "series") {
314 string const ttok = lex.getString();
315 setLyXSeries(ttok, f);
316 } else if (tok == "shape") {
318 string const ttok = lex.getString();
319 setLyXShape(ttok, f);
320 } else if (tok == "size") {
322 string const ttok = lex.getString();
324 } else if (tok == "misc") {
326 string const ttok = ascii_lowercase(lex.getString());
328 if (ttok == "no_bar") {
329 f.setUnderbar(FONT_OFF);
330 } else if (ttok == "no_emph") {
332 } else if (ttok == "no_noun") {
334 } else if (ttok == "emph") {
336 } else if (ttok == "underbar") {
337 f.setUnderbar(FONT_ON);
338 } else if (ttok == "noun") {
341 lex.printError("Illegal misc type");
343 } else if (tok == "color") {
345 string const ttok = lex.getString();
346 setLyXColor(ttok, f);
348 lex.printError("Unknown tag");
356 /// Writes the changes from this font to orgfont in .lyx format in file
357 void Font::lyxWriteChanges(Font const & orgfont,
361 if (orgfont.fontInfo().family() != bits_.family())
362 os << "\\family " << LyXFamilyNames[bits_.family()] << "\n";
363 if (orgfont.fontInfo().series() != bits_.series())
364 os << "\\series " << LyXSeriesNames[bits_.series()] << "\n";
365 if (orgfont.fontInfo().shape() != bits_.shape())
366 os << "\\shape " << LyXShapeNames[bits_.shape()] << "\n";
367 if (orgfont.fontInfo().size() != bits_.size())
368 os << "\\size " << LyXSizeNames[bits_.size()] << "\n";
369 if (orgfont.fontInfo().emph() != bits_.emph())
370 os << "\\emph " << LyXMiscNames[bits_.emph()] << "\n";
371 if (orgfont.fontInfo().number() != bits_.number())
372 os << "\\numeric " << LyXMiscNames[bits_.number()] << "\n";
373 if (orgfont.fontInfo().underbar() != bits_.underbar()) {
374 // This is only for backwards compatibility
375 switch (bits_.underbar()) {
376 case FONT_OFF: os << "\\bar no\n"; break;
377 case FONT_ON: os << "\\bar under\n"; break;
378 case FONT_TOGGLE: lyxerr << "Font::lyxWriteFontChanges: "
379 "FONT_TOGGLE should not appear here!"
382 case FONT_INHERIT: os << "\\bar default\n"; break;
383 case FONT_IGNORE: lyxerr << "Font::lyxWriteFontChanges: "
384 "IGNORE should not appear here!"
389 if (orgfont.fontInfo().noun() != bits_.noun()) {
390 os << "\\noun " << LyXMiscNames[bits_.noun()] << "\n";
392 if (orgfont.fontInfo().color() != bits_.color())
393 os << "\\color " << lcolor.getLyXName(bits_.color()) << '\n';
394 // FIXME: uncomment this when we support background.
395 //if (orgfont.fontInfo().background() != bits_.background())
396 // os << "\\color " << lcolor.getLyXName(bits_.background()) << '\n';
397 if (orgfont.language() != language() &&
398 language() != latex_language) {
400 os << "\\lang " << language()->lang() << "\n";
402 os << "\\lang unknown\n";
407 /// Writes the head of the LaTeX needed to impose this font
408 // Returns number of chars written.
409 int Font::latexWriteStartChanges(odocstream & os, BufferParams const & bparams,
410 OutputParams const & runparams,
412 Font const & prev) const
417 if (language()->babel() != base.language()->babel() &&
418 language() != prev.language()) {
419 if (language()->lang() == "farsi") {
422 } else if (!isRightToLeft() &&
423 base.language()->lang() == "farsi") {
426 } else if (language()->lang() == "arabic_arabi") {
429 } else if (!isRightToLeft() &&
430 base.language()->lang() == "arabic_arabi") {
433 // currently the remaining RTL languages are arabic_arabtex and hebrew
434 } else if (isRightToLeft() != prev.isRightToLeft()) {
435 if (isRightToLeft()) {
442 } else if (!language()->babel().empty()) {
444 subst(lyxrc.language_command_local,
445 "$$lang", language()->babel());
446 os << from_ascii(tmp);
447 count += tmp.length();
454 if (language()->encoding()->package() == Encoding::CJK) {
455 pair<bool, int> const c = switchEncoding(os, bparams,
456 runparams, *(language()->encoding()));
458 open_encoding_ = true;
460 runparams.encoding = language()->encoding();
464 // When the current language is Hebrew, Arabic, or Farsi
465 // the numbers are written Left-to-Right. ArabTeX package
466 // reorders the number automatically but the packages used
467 // for Hebrew and Farsi (Arabi) do not.
468 if (bits_.number() == FONT_ON && prev.fontInfo().number() != FONT_ON
469 && (language()->lang() == "hebrew"
470 || language()->lang() == "farsi"
471 || language()->lang() == "arabic_arabi")) {
477 f.reduce(base.bits_);
479 if (f.family() != INHERIT_FAMILY) {
481 << LaTeXFamilyNames[f.family()]
483 count += strlen(LaTeXFamilyNames[f.family()]) + 2;
484 env = true; //We have opened a new environment
486 if (f.series() != INHERIT_SERIES) {
488 << LaTeXSeriesNames[f.series()]
490 count += strlen(LaTeXSeriesNames[f.series()]) + 2;
491 env = true; //We have opened a new environment
493 if (f.shape() != INHERIT_SHAPE) {
495 << LaTeXShapeNames[f.shape()]
497 count += strlen(LaTeXShapeNames[f.shape()]) + 2;
498 env = true; //We have opened a new environment
500 if (f.color() != Color_inherit && f.color() != Color_ignore) {
502 << from_ascii(lcolor.getLaTeXName(f.color()))
504 count += lcolor.getLaTeXName(f.color()).length() + 13;
505 env = true; //We have opened a new environment
507 // FIXME: uncomment this when we support background.
509 if (f.background() != Color_inherit && f.background() != Color_ignore) {
511 << from_ascii(lcolor.getLaTeXName(f.background()))
513 count += lcolor.getLaTeXName(f.background()).length() + 13;
514 env = true; //We have opened a new environment
517 if (f.emph() == FONT_ON) {
520 env = true; //We have opened a new environment
522 if (f.underbar() == FONT_ON) {
525 env = true; //We have opened a new environment
527 // \noun{} is a LyX special macro
528 if (f.noun() == FONT_ON) {
531 env = true; //We have opened a new environment
533 if (f.size() != FONT_SIZE_INHERIT) {
534 // If we didn't open an environment above, we open one here
540 << LaTeXSizeNames[f.size()]
542 count += strlen(LaTeXSizeNames[f.size()]) + 2;
548 /// Writes ending block of LaTeX needed to close use of this font
549 // Returns number of chars written
550 // This one corresponds to latexWriteStartChanges(). (Asger)
551 int Font::latexWriteEndChanges(odocstream & os, BufferParams const & bparams,
552 OutputParams const & runparams,
555 bool const & closeLanguage) const
560 // reduce the current font to changes against the base
561 // font (of the layout). We use a temporary for this to
562 // avoid changing this font instance, as that would break
564 f.reduce(base.bits_);
566 if (f.family() != INHERIT_FAMILY) {
569 env = true; // Size change need not bother about closing env.
571 if (f.series() != INHERIT_SERIES) {
574 env = true; // Size change need not bother about closing env.
576 if (f.shape() != INHERIT_SHAPE) {
579 env = true; // Size change need not bother about closing env.
581 if (f.color() != Color_inherit && f.color() != Color_ignore) {
584 env = true; // Size change need not bother about closing env.
586 if (f.emph() == FONT_ON) {
589 env = true; // Size change need not bother about closing env.
591 if (f.underbar() == FONT_ON) {
594 env = true; // Size change need not bother about closing env.
596 if (f.noun() == FONT_ON) {
599 env = true; // Size change need not bother about closing env.
601 if (f.size() != FONT_SIZE_INHERIT) {
602 // We only have to close if only size changed
609 // When the current language is Hebrew, Arabic, or Farsi
610 // the numbers are written Left-to-Right. ArabTeX package
611 // reorders the number automatically but the packages used
612 // for Hebrew and Farsi (Arabi) do not.
613 if (bits_.number() == FONT_ON && next.fontInfo().number() != FONT_ON
614 && (language()->lang() == "hebrew"
615 || language()->lang() == "farsi"
616 || language()->lang() == "arabic_arabi")) {
621 if (open_encoding_) {
622 // We need to close the encoding even if it does not change
623 // to do correct environment nesting
624 Encoding const * const ascii = encodings.fromLyXName("ascii");
625 pair<bool, int> const c = switchEncoding(os, bparams,
627 LASSERT(c.first, /**/);
629 runparams.encoding = ascii;
630 open_encoding_ = false;
634 language() != base.language() && language() != next.language()) {
643 string Font::toString(bool const toggle) const
645 string lang = "ignore";
647 lang = language()->lang();
650 os << "family " << bits_.family() << '\n'
651 << "series " << bits_.series() << '\n'
652 << "shape " << bits_.shape() << '\n'
653 << "size " << bits_.size() << '\n'
654 << "emph " << bits_.emph() << '\n'
655 << "underbar " << bits_.underbar() << '\n'
656 << "noun " << bits_.noun() << '\n'
657 << "number " << bits_.number() << '\n'
658 << "color " << bits_.color() << '\n'
659 << "language " << lang << '\n'
660 << "toggleall " << convert<string>(toggle);
665 bool Font::fromString(string const & data, bool & toggle)
667 istringstream is(data);
675 token = lex.getString();
677 if (token.empty() || !lex.next())
680 if (token == "family") {
681 int const next = lex.getInteger();
682 bits_.setFamily(FontFamily(next));
684 } else if (token == "series") {
685 int const next = lex.getInteger();
686 bits_.setSeries(FontSeries(next));
688 } else if (token == "shape") {
689 int const next = lex.getInteger();
690 bits_.setShape(FontShape(next));
692 } else if (token == "size") {
693 int const next = lex.getInteger();
694 bits_.setSize(FontSize(next));
696 } else if (token == "emph" || token == "underbar" ||
697 token == "noun" || token == "number") {
699 int const next = lex.getInteger();
700 FontState const misc = FontState(next);
704 else if (token == "underbar")
705 bits_.setUnderbar(misc);
706 else if (token == "noun")
708 else if (token == "number")
709 bits_.setNumber(misc);
711 } else if (token == "color") {
712 int const next = lex.getInteger();
713 bits_.setColor(ColorCode(next));
716 } else if (token == "background") {
717 int const next = lex.getInteger();
718 bits_.setBackground(ColorCode(next));
721 } else if (token == "language") {
722 string const next = lex.getString();
723 if (next == "ignore")
724 setLanguage(ignore_language);
726 setLanguage(languages.getLanguage(next));
728 } else if (token == "toggleall") {
729 toggle = lex.getBool();
732 // Unrecognised token
742 void Font::validate(LaTeXFeatures & features) const
744 BufferParams const & bparams = features.bufferParams();
745 Language const * doc_language = bparams.language;
747 if (bits_.noun() == FONT_ON) {
748 LYXERR(Debug::LATEX, "font.noun: " << bits_.noun());
749 features.require("noun");
750 LYXERR(Debug::LATEX, "Noun enabled. Font: " << to_utf8(stateText(0)));
752 switch (bits_.color()) {
756 // probably we should put here all interface colors used for
757 // font displaying! For now I just add this ones I know of (Jug)
759 case Color_notelabel:
762 features.require("color");
763 LYXERR(Debug::LATEX, "Color enabled. Font: " << to_utf8(stateText(0)));
766 // FIXME: Do something for background and soul package?
768 if (lang_->babel() != doc_language->babel() &&
769 lang_ != ignore_language &&
770 lang_ != latex_language)
772 features.useLanguage(lang_);
773 LYXERR(Debug::LATEX, "Found language " << lang_->lang());
778 ostream & operator<<(ostream & os, FontState fms)
780 return os << int(fms);
784 ostream & operator<<(ostream & os, FontInfo const & f)
787 << " family " << f.family()
788 << " series " << f.series()
789 << " shape " << f.shape()
790 << " size " << f.size()
791 << " color " << f.color()
792 // FIXME: uncomment this when we support background.
793 //<< " background " << f.background()
794 << " emph " << f.emph()
795 << " underbar " << f.underbar()
796 << " noun " << f.noun()
797 << " number " << f.number();
801 ostream & operator<<(ostream & os, Font const & font)
803 return os << font.bits_
804 << " lang: " << (font.lang_ ? font.lang_->lang() : 0);