1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1995 Matthias Ettrich
7 * Copyright 1995-2000 The LyX Team.
9 * ====================================================== */
15 #pragma implementation "lyxfont.h"
25 #include "FontLoader.h"
26 #include "support/lstrings.h"
27 #include "bufferparams.h" // stateText
37 char const * GUIFamilyNames[6] =
38 { N_("Roman"), N_("Sans serif"), N_("Typewriter"), N_("Symbol"), N_("Inherit"),
42 char const * GUISeriesNames[4] =
43 { N_("Medium"), N_("Bold"), N_("Inherit"), N_("Ignore") };
46 char const * GUIShapeNames[6] =
47 { N_("Upright"), N_("Italic"), N_("Slanted"), N_("Smallcaps"), N_("Inherit"),
51 char const * GUISizeNames[14] =
52 { N_("Tiny"), N_("Smallest"), N_("Smaller"), N_("Small"), N_("Normal"), N_("Large"),
53 N_("Larger"), N_("Largest"), N_("Huge"), N_("Huger"), N_("Increase"), N_("Decrease"),
54 N_("Inherit"), N_("Ignore") };
57 char const * GUIMiscNames[5] =
58 { N_("Off"), N_("On"), N_("Toggle"), N_("Inherit"), N_("Ignore") };
62 // Strings used to read and write .lyx format files
65 char const * LyXFamilyNames[6] =
66 { "roman", "sans", "typewriter", "symbol", "default", "error" };
69 char const * LyXSeriesNames[4] =
70 { "medium", "bold", "default", "error" };
73 char const * LyXShapeNames[6] =
74 { "up", "italic", "slanted", "smallcaps", "default", "error" };
77 char const * LyXSizeNames[14] =
78 { "tiny", "scriptsize", "footnotesize", "small", "normal", "large",
79 "larger", "largest", "huge", "giant",
80 "increase-error", "decrease-error", "default", "error" };
83 char const * LyXMiscNames[5] =
84 { "off", "on", "toggle", "default", "error" };
87 // Strings used to write LaTeX files
90 char const * LaTeXFamilyNames[6] =
91 { "textrm", "textsf", "texttt", "error1", "error2", "error3" };
94 char const * LaTeXSeriesNames[4] =
95 { "textmd", "textbf", "error4", "error5" };
98 char const * LaTeXShapeNames[6] =
99 { "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" };
107 LyXFont::FontBits LyXFont::sane = {
118 LyXFont::FontBits LyXFont::inherit = {
129 LyXFont::FontBits LyXFont::ignore = {
141 bool LyXFont::FontBits::operator==(LyXFont::FontBits const & fb1) const
143 return fb1.family == family &&
144 fb1.series == series &&
145 fb1.shape == shape &&
147 fb1.color == color &&
149 fb1.underbar == underbar &&
155 bool LyXFont::FontBits::operator!=(LyXFont::FontBits const & fb1) const
157 return !(fb1 == *this);
161 /// Decreases font size by one
162 LyXFont & LyXFont::decSize()
165 case SIZE_HUGER: setSize(SIZE_HUGE); break;
166 case SIZE_HUGE: setSize(SIZE_LARGEST); break;
167 case SIZE_LARGEST: setSize(SIZE_LARGER); break;
168 case SIZE_LARGER: setSize(SIZE_LARGE); break;
169 case SIZE_LARGE: setSize(SIZE_NORMAL); break;
170 case SIZE_NORMAL: setSize(SIZE_SMALL); break;
171 case SIZE_SMALL: setSize(SIZE_FOOTNOTE); break;
172 case SIZE_FOOTNOTE: setSize(SIZE_SCRIPT); break;
173 case SIZE_SCRIPT: setSize(SIZE_TINY); break;
174 case SIZE_TINY: break;
176 lyxerr << "Can't LyXFont::decSize on INCREASE_SIZE" << endl;
179 lyxerr <<"Can't LyXFont::decSize on DECREASE_SIZE" << endl;
182 lyxerr <<"Can't LyXFont::decSize on INHERIT_SIZE" << endl;
185 lyxerr <<"Can't LyXFont::decSize on IGNORE_SIZE" << endl;
192 /// Increases font size by one
193 LyXFont & LyXFont::incSize()
196 case SIZE_HUGER: break;
197 case SIZE_HUGE: setSize(SIZE_HUGER); break;
198 case SIZE_LARGEST: setSize(SIZE_HUGE); break;
199 case SIZE_LARGER: setSize(SIZE_LARGEST); break;
200 case SIZE_LARGE: setSize(SIZE_LARGER); break;
201 case SIZE_NORMAL: setSize(SIZE_LARGE); break;
202 case SIZE_SMALL: setSize(SIZE_NORMAL); break;
203 case SIZE_FOOTNOTE: setSize(SIZE_SMALL); break;
204 case SIZE_SCRIPT: setSize(SIZE_FOOTNOTE); break;
205 case SIZE_TINY: setSize(SIZE_SCRIPT); break;
207 lyxerr <<"Can't LyXFont::incSize on INCREASE_SIZE" << endl;
210 lyxerr <<"Can't LyXFont::incSize on DECREASE_SIZE" << endl;
213 lyxerr <<"Can't LyXFont::incSize on INHERIT_SIZE" << endl;
216 lyxerr <<"Can't LyXFont::incSize on IGNORE_SIZE" << endl;
223 /// Updates a misc setting according to request
224 LyXFont::FONT_MISC_STATE LyXFont::setMisc(FONT_MISC_STATE newfont,
227 if (newfont == TOGGLE) {
233 lyxerr <<"LyXFont::setMisc: Need state"
234 " ON or OFF to toggle. Setting to ON" << endl;
237 } else if (newfont == IGNORE)
244 /// Updates font settings according to request
245 void LyXFont::update(LyXFont const & newfont,
246 Language const * document_language, bool toggleall)
248 if(newfont.family() == family() && toggleall)
249 setFamily(INHERIT_FAMILY); // toggle 'back'
250 else if (newfont.family() != IGNORE_FAMILY)
251 setFamily(newfont.family());
252 // else it's IGNORE_SHAPE
254 // "Old" behaviour: "Setting" bold will toggle bold on/off.
255 switch (newfont.series()) {
258 if (series() == BOLD_SERIES && toggleall)
259 setSeries(MEDIUM_SERIES);
261 setSeries(BOLD_SERIES);
265 setSeries(newfont.series());
271 if(newfont.shape() == shape() && toggleall)
272 setShape(INHERIT_SHAPE); // toggle 'back'
273 else if (newfont.shape() != IGNORE_SHAPE)
274 setShape(newfont.shape());
275 // else it's IGNORE_SHAPE
277 if (newfont.size() != IGNORE_SIZE) {
278 if (newfont.size() == INCREASE_SIZE)
280 else if (newfont.size() == DECREASE_SIZE)
282 else if (newfont.size() == size() && toggleall)
283 setSize(INHERIT_SIZE); // toggle 'back'
285 setSize(newfont.size());
288 setEmph(setMisc(newfont.emph(), emph()));
289 setUnderbar(setMisc(newfont.underbar(), underbar()));
290 setNoun(setMisc(newfont.noun(), noun()));
291 setLatex(setMisc(newfont.latex(), latex()));
293 if (newfont.language() == language() && toggleall)
294 if (language() == document_language)
295 setLanguage(default_language);
297 setLanguage(document_language);
298 else if (newfont.language() != ignore_language)
299 setLanguage(newfont.language());
301 if (newfont.color() == color() && toggleall)
302 setColor(LColor::inherit); // toggle 'back'
303 else if (newfont.color() != LColor::ignore)
304 setColor(newfont.color());
308 /// Reduce font to fall back to template where possible
309 void LyXFont::reduce(LyXFont const & tmplt)
311 if (family() == tmplt.family())
312 setFamily(INHERIT_FAMILY);
313 if (series() == tmplt.series())
314 setSeries(INHERIT_SERIES);
315 if (shape() == tmplt.shape())
316 setShape(INHERIT_SHAPE);
317 if (size() == tmplt.size())
318 setSize(INHERIT_SIZE);
319 if (emph() == tmplt.emph())
321 if (underbar() == tmplt.underbar())
322 setUnderbar(INHERIT);
323 if (noun() == tmplt.noun())
325 if (latex() == tmplt.latex())
327 if (color() == tmplt.color())
328 setColor(LColor::inherit);
332 /// Realize font from a template
333 LyXFont & LyXFont::realize(LyXFont const & tmplt)
335 if (bits == inherit) {
340 if (bits.family == INHERIT_FAMILY) {
341 bits.family = tmplt.bits.family;
343 if (bits.series == INHERIT_SERIES) {
344 bits.series = tmplt.bits.series;
346 if (bits.shape == INHERIT_SHAPE) {
347 bits.shape = tmplt.bits.shape;
349 if (bits.size == INHERIT_SIZE) {
350 bits.size = tmplt.bits.size;
352 if (bits.emph == INHERIT) {
353 bits.emph = tmplt.bits.emph;
355 if (bits.underbar == INHERIT) {
356 bits.underbar = tmplt.bits.underbar;
358 if (bits.noun == INHERIT) {
359 bits.noun = tmplt.bits.noun;
361 if (bits.latex == INHERIT) {
362 bits.latex = tmplt.bits.latex;
364 if (bits.color == LColor::inherit) {
365 bits.color = tmplt.bits.color;
372 /// Is font resolved?
373 bool LyXFont::resolved() const
375 return (family() != INHERIT_FAMILY && series() != INHERIT_SERIES &&
376 shape() != INHERIT_SHAPE && size() != INHERIT_SIZE &&
377 emph() != INHERIT && underbar() != INHERIT &&
378 noun() != INHERIT && latex() != INHERIT &&
379 color() != LColor::inherit);
383 /// Build GUI description of font state
384 string const LyXFont::stateText(BufferParams * params) const
386 std::ostringstream ost;
387 if (family() != INHERIT_FAMILY)
388 ost << _(GUIFamilyNames[family()]) << ", ";
389 if (series() != INHERIT_SERIES)
390 ost << _(GUISeriesNames[series()]) << ", ";
391 if (shape() != INHERIT_SHAPE)
392 ost << _(GUIShapeNames[shape()]) << ", ";
393 if (size() != INHERIT_SIZE)
394 ost << _(GUISizeNames[size()]) << ", ";
395 if (color() != LColor::inherit)
396 ost << lcolor.getGUIName(color()) << ", ";
397 if (emph() != INHERIT)
398 ost << _("Emphasis ")
399 << _(GUIMiscNames[emph()]) << ", ";
400 if (underbar() != INHERIT)
401 ost << _("Underline ")
402 << _(GUIMiscNames[underbar()]) << ", ";
403 if (noun() != INHERIT)
404 ost << _("Noun ") << _(GUIMiscNames[noun()]) << ", ";
405 if (latex() != INHERIT)
406 ost << _("Latex ") << _(GUIMiscNames[latex()]) << ", ";
408 ost << _("Default") << ", ";
409 if (!params || (language() != params->language_info &&
410 language()->lang() != "default"))
411 ost << _("Language: ") << _(language()->display().c_str());
413 string buf(ost.str().c_str());
414 buf = strip(buf, ' ');
415 buf = strip(buf, ',');
420 // Set family according to lyx format string
421 LyXFont & LyXFont::setLyXFamily(string const & fam)
423 string s = lowercase(fam);
426 while (s != LyXFamilyNames[i] && LyXFamilyNames[i] != "error") ++i;
427 if (s == LyXFamilyNames[i]) {
428 setFamily(LyXFont::FONT_FAMILY(i));
430 lyxerr << "LyXFont::setLyXFamily: Unknown family `"
431 << s << '\'' << endl;
436 // Set series according to lyx format string
437 LyXFont & LyXFont::setLyXSeries(string const & ser)
439 string s = lowercase(ser);
442 while (s != LyXSeriesNames[i] && LyXSeriesNames[i] != "error") ++i;
443 if (s == LyXSeriesNames[i]) {
444 setSeries(LyXFont::FONT_SERIES(i));
446 lyxerr << "LyXFont::setLyXSeries: Unknown series `"
447 << s << '\'' << endl;
452 // Set shape according to lyx format string
453 LyXFont & LyXFont::setLyXShape(string const & sha)
455 string s = lowercase(sha);
458 while (s != LyXShapeNames[i] && LyXShapeNames[i] != "error") ++i;
459 if (s == LyXShapeNames[i]) {
460 setShape(LyXFont::FONT_SHAPE(i));
462 lyxerr << "LyXFont::setLyXShape: Unknown shape `"
463 << s << '\'' << endl;
468 // Set size according to lyx format string
469 LyXFont & LyXFont::setLyXSize(string const & siz)
471 string s = lowercase(siz);
473 while (s != LyXSizeNames[i] && LyXSizeNames[i] != "error") ++i;
474 if (s == LyXSizeNames[i]) {
475 setSize(LyXFont::FONT_SIZE(i));
477 lyxerr << "LyXFont::setLyXSize: Unknown size `"
478 << s << '\'' << endl;
483 // Set size according to lyx format string
484 LyXFont::FONT_MISC_STATE LyXFont::setLyXMisc(string const & siz)
486 string s = lowercase(siz);
488 while (s != LyXMiscNames[i] && LyXMiscNames[i] != "error") ++i;
489 if (s == LyXMiscNames[i])
490 return FONT_MISC_STATE(i);
491 lyxerr << "LyXFont::setLyXMisc: Unknown misc flag `"
492 << s << '\'' << endl;
497 /// Sets color after LyX text format
498 LyXFont & LyXFont::setLyXColor(string const & col)
500 setColor(lcolor.getFromLyXName(col));
505 /// Sets size after GUI name
506 LyXFont & LyXFont::setGUISize(string const & siz)
508 // CHECK this might be wrong.
510 // this is how it was how it was done in the lyx repository...
511 // but this does not make sense.
512 setColor(lcolor.getFromGUIName(siz));
517 // Returns size in latex format
518 string const LyXFont::latexSize() const
520 return LaTeXSizeNames[size()];
524 // Read a font definition from given file in lyx format
526 LyXFont & LyXFont::lyxRead(LyXLex & lex)
529 bool finished = false;
530 while (!finished && lex.IsOK() && !error) {
532 string tok = lowercase(lex.GetString());
536 } else if (tok == "endfont") {
538 } else if (tok == "family") {
540 string tok = lex.GetString();
542 } else if (tok == "series") {
544 string tok = lex.GetString();
546 } else if (tok == "shape") {
548 string tok = lex.GetString();
550 } else if (tok == "size") {
552 string tok = lex.GetString();
554 } else if (tok == "latex") {
556 string tok = lowercase(lex.GetString());
558 if (tok == "no_latex") {
560 } else if (tok == "latex") {
563 lex.printError("Illegal LaTeX type`$$Token'");
565 } else if (tok == "misc") {
567 string tok = lowercase(lex.GetString());
569 if (tok == "no_bar") {
571 } else if (tok == "no_emph") {
573 } else if (tok == "no_noun") {
575 } else if (tok == "emph") {
577 } else if (tok == "underbar") {
579 } else if (tok == "noun") {
582 lex.printError("Illegal misc type `$$Token´");
584 } else if (tok == "color") {
586 string tok = lex.GetString();
589 lex.printError("Unknown tag `$$Token'");
597 /// Writes the changes from this font to orgfont in .lyx format in file
598 void LyXFont::lyxWriteChanges(LyXFont const & orgfont, ostream & os) const
601 if (orgfont.family() != family()) {
602 os << "\\family " << LyXFamilyNames[family()] << " \n";
604 if (orgfont.series() != series()) {
605 os << "\\series " << LyXSeriesNames[series()] << " \n";
607 if (orgfont.shape() != shape()) {
608 os << "\\shape " << LyXShapeNames[shape()] << " \n";
610 if (orgfont.size() != size()) {
611 os << "\\size " << LyXSizeNames[size()] << " \n";
613 if (orgfont.emph() != emph()) {
614 os << "\\emph " << LyXMiscNames[emph()] << " \n";
616 if (orgfont.underbar() != underbar()) {
617 // This is only for backwards compatibility
618 switch (underbar()) {
619 case OFF: os << "\\bar no \n"; break;
620 case ON: os << "\\bar under \n"; break;
621 case TOGGLE: lyxerr << "LyXFont::lyxWriteFontChanges: "
622 "TOGGLE should not appear here!"
625 case INHERIT: os << "\\bar default \n"; break;
626 case IGNORE: lyxerr << "LyXFont::lyxWriteFontChanges: "
627 "IGNORE should not appear here!"
632 if (orgfont.noun() != noun()) {
633 os << "\\noun " << LyXMiscNames[noun()] << " \n";
635 if (orgfont.latex() != latex()) {
636 // This is only for backwards compatibility
638 case OFF: os << "\\latex no_latex \n"; break;
639 case ON: os << "\\latex latex \n"; break;
640 case TOGGLE: lyxerr << "LyXFont::lyxWriteFontChanges: "
641 "TOGGLE should not appear here!"
644 case INHERIT: os << "\\latex default \n"; break;
645 case IGNORE: lyxerr << "LyXFont::lyxWriteFontChanges: "
646 "IGNORE should not appear here!"
651 if (orgfont.color() != color()) {
652 // To make us file compatible with older
653 // lyx versions we emit "default" instead
655 string col_str(lcolor.getLyXName(color()));
656 if (col_str == "inherit") col_str = "default";
657 os << "\\color " << col_str << "\n";
659 if (orgfont.language() != language() &&
660 language()->lang() != "default") {
662 os << "\\lang " << language()->lang() << "\n";
664 os << "\\lang unknown\n";
669 /// Writes the head of the LaTeX needed to impose this font
670 // Returns number of chars written.
671 int LyXFont::latexWriteStartChanges(ostream & os, LyXFont const & base,
672 LyXFont const & prev) const
677 if (language() != base.language() && language() != prev.language() &&
678 language()->lang() != "default") {
679 if (isRightToLeft() != prev.isRightToLeft()) {
680 if (isRightToLeft()) {
683 env = true; //We have opened a new environment
687 env = true; //We have opened a new environment
691 subst(lyxrc.language_command_begin,
692 "$$lang", language()->lang());
694 count += tmp.length();
695 env = true; //We have opened a new environment
702 if (f.family() != INHERIT_FAMILY) {
704 << LaTeXFamilyNames[f.family()]
706 count += strlen(LaTeXFamilyNames[f.family()]) + 2;
707 env = true; //We have opened a new environment
709 if (f.series() != INHERIT_SERIES) {
711 << LaTeXSeriesNames[f.series()]
713 count += strlen(LaTeXSeriesNames[f.series()]) + 2;
714 env = true; //We have opened a new environment
716 if (f.shape() != INHERIT_SHAPE) {
718 << LaTeXShapeNames[f.shape()]
720 count += strlen(LaTeXShapeNames[f.shape()]) + 2;
721 env = true; //We have opened a new environment
723 if (f.color() != LColor::inherit && f.color() != LColor::ignore) {
725 << lcolor.getLaTeXName(f.color())
727 count += lcolor.getLaTeXName(f.color()).length() + 13;
728 env = true; //We have opened a new environment
730 if (f.emph() == ON) {
733 env = true; //We have opened a new environment
735 if (f.underbar() == ON) {
738 env = true; //We have opened a new environment
740 // \noun{} is a LyX special macro
741 if (f.noun() == ON) {
744 env = true; //We have opened a new environment
746 if (f.size() != INHERIT_SIZE) {
747 // If we didn't open an environment above, we open one here
753 << LaTeXSizeNames[f.size()]
755 count += strlen(LaTeXSizeNames[f.size()]) + 2;
761 /// Writes ending block of LaTeX needed to close use of this font
762 // Returns number of chars written
763 // This one corresponds to latexWriteStartChanges(). (Asger)
764 int LyXFont::latexWriteEndChanges(ostream & os, LyXFont const & base,
765 LyXFont const & next) const
770 if (language() != base.language() && language() != next.language()
771 && language()->lang() != "default") {
774 env = true; // Size change need not bother about closing env.
777 LyXFont f = *this; // why do you need this?
778 f.reduce(base); // why isn't this just "reduce(base);" (Lgb)
779 // Because this function is const. Everything breaks if this
780 // method changes the font it represents. There is no speed penalty
781 // by using the temporary. (Asger)
783 if (f.family() != INHERIT_FAMILY) {
786 env = true; // Size change need not bother about closing env.
788 if (f.series() != INHERIT_SERIES) {
791 env = true; // Size change need not bother about closing env.
793 if (f.shape() != INHERIT_SHAPE) {
796 env = true; // Size change need not bother about closing env.
798 if (f.color() != LColor::inherit && f.color() != LColor::ignore) {
801 env = true; // Size change need not bother about closing env.
803 if (f.emph() == ON) {
806 env = true; // Size change need not bother about closing env.
808 if (f.underbar() == ON) {
811 env = true; // Size change need not bother about closing env.
813 if (f.noun() == ON) {
816 env = true; // Size change need not bother about closing env.
818 if (f.size() != INHERIT_SIZE) {
819 // We only have to close if only size changed
830 LColor::color LyXFont::realColor() const
833 return LColor::latex;
834 if (color() == LColor::none)
835 return LColor::foreground;
840 // Convert logical attributes to concrete shape attribute
841 LyXFont::FONT_SHAPE LyXFont::realShape() const
843 register FONT_SHAPE s = shape();
857 bool LyXFont::equalExceptLatex(LyXFont const & f) const
860 f1.setLatex(f.latex());
865 ostream & operator<<(ostream & o, LyXFont::FONT_MISC_STATE fms)
867 return o << int(fms);