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"
36 char const * GUIFamilyNames[6] =
37 { N_("Roman"), N_("Sans serif"), N_("Typewriter"), N_("Symbol"), N_("Inherit"),
41 char const * GUISeriesNames[4] =
42 { N_("Medium"), N_("Bold"), N_("Inherit"), N_("Ignore") };
45 char const * GUIShapeNames[6] =
46 { N_("Upright"), N_("Italic"), N_("Slanted"), N_("Smallcaps"), N_("Inherit"),
50 char const * GUISizeNames[14] =
51 { N_("Tiny"), N_("Smallest"), N_("Smaller"), N_("Small"), N_("Normal"), N_("Large"),
52 N_("Larger"), N_("Largest"), N_("Huge"), N_("Huger"), N_("Increase"), N_("Decrease"),
53 N_("Inherit"), N_("Ignore") };
56 char const * GUIMiscNames[5] =
57 { N_("Off"), N_("On"), N_("Toggle"), N_("Inherit"), N_("Ignore") };
61 // Strings used to read and write .lyx format files
64 char const * LyXFamilyNames[6] =
65 { "roman", "sans", "typewriter", "symbol", "default", "error" };
68 char const * LyXSeriesNames[4] =
69 { "medium", "bold", "default", "error" };
72 char const * LyXShapeNames[6] =
73 { "up", "italic", "slanted", "smallcaps", "default", "error" };
76 char const * LyXSizeNames[14] =
77 { "tiny", "scriptsize", "footnotesize", "small", "normal", "large",
78 "larger", "largest", "huge", "giant",
79 "increase-error", "decrease-error", "default", "error" };
82 char const * LyXMiscNames[5] =
83 { "off", "on", "toggle", "default", "error" };
86 // Strings used to write LaTeX files
89 char const * LaTeXFamilyNames[6] =
90 { "textrm", "textsf", "texttt", "error1", "error2", "error3" };
93 char const * LaTeXSeriesNames[4] =
94 { "textmd", "textbf", "error4", "error5" };
97 char const * LaTeXShapeNames[6] =
98 { "textup", "textit", "textsl", "textsc", "error6", "error7" };
101 char const * LaTeXSizeNames[14] =
102 { "tiny", "scriptsize", "footnotesize", "small", "normalsize", "large",
103 "Large", "LARGE", "huge", "Huge", "error8", "error9", "error10", "error11" };
106 LyXFont::FontBits LyXFont::sane = {
117 LyXFont::FontBits LyXFont::inherit = {
128 LyXFont::FontBits LyXFont::ignore = {
140 bool LyXFont::FontBits::operator==(LyXFont::FontBits const & fb1) const
142 return fb1.family == family &&
143 fb1.series == series &&
144 fb1.shape == shape &&
146 fb1.color == color &&
148 fb1.underbar == underbar &&
154 bool LyXFont::FontBits::operator!=(LyXFont::FontBits const & fb1) const
156 return !(fb1 == *this);
160 /// Decreases font size by one
161 LyXFont & LyXFont::decSize()
164 case SIZE_HUGER: setSize(SIZE_HUGE); break;
165 case SIZE_HUGE: setSize(SIZE_LARGEST); break;
166 case SIZE_LARGEST: setSize(SIZE_LARGER); break;
167 case SIZE_LARGER: setSize(SIZE_LARGE); break;
168 case SIZE_LARGE: setSize(SIZE_NORMAL); break;
169 case SIZE_NORMAL: setSize(SIZE_SMALL); break;
170 case SIZE_SMALL: setSize(SIZE_FOOTNOTE); break;
171 case SIZE_FOOTNOTE: setSize(SIZE_SCRIPT); break;
172 case SIZE_SCRIPT: setSize(SIZE_TINY); break;
173 case SIZE_TINY: break;
175 lyxerr << "Can't LyXFont::decSize on INCREASE_SIZE" << endl;
178 lyxerr <<"Can't LyXFont::decSize on DECREASE_SIZE" << endl;
181 lyxerr <<"Can't LyXFont::decSize on INHERIT_SIZE" << endl;
184 lyxerr <<"Can't LyXFont::decSize on IGNORE_SIZE" << endl;
191 /// Increases font size by one
192 LyXFont & LyXFont::incSize()
195 case SIZE_HUGER: break;
196 case SIZE_HUGE: setSize(SIZE_HUGER); break;
197 case SIZE_LARGEST: setSize(SIZE_HUGE); break;
198 case SIZE_LARGER: setSize(SIZE_LARGEST); break;
199 case SIZE_LARGE: setSize(SIZE_LARGER); break;
200 case SIZE_NORMAL: setSize(SIZE_LARGE); break;
201 case SIZE_SMALL: setSize(SIZE_NORMAL); break;
202 case SIZE_FOOTNOTE: setSize(SIZE_SMALL); break;
203 case SIZE_SCRIPT: setSize(SIZE_FOOTNOTE); break;
204 case SIZE_TINY: setSize(SIZE_SCRIPT); break;
206 lyxerr <<"Can't LyXFont::incSize on INCREASE_SIZE" << endl;
209 lyxerr <<"Can't LyXFont::incSize on DECREASE_SIZE" << endl;
212 lyxerr <<"Can't LyXFont::incSize on INHERIT_SIZE" << endl;
215 lyxerr <<"Can't LyXFont::incSize on IGNORE_SIZE" << endl;
222 /// Updates a misc setting according to request
223 LyXFont::FONT_MISC_STATE LyXFont::setMisc(FONT_MISC_STATE newfont,
226 if (newfont == TOGGLE) {
232 lyxerr <<"LyXFont::setMisc: Need state"
233 " ON or OFF to toggle. Setting to ON" << endl;
236 } else if (newfont == IGNORE)
243 /// Updates font settings according to request
244 void LyXFont::update(LyXFont const & newfont,
245 Language const * document_language, bool toggleall)
247 if(newfont.family() == family() && toggleall)
248 setFamily(INHERIT_FAMILY); // toggle 'back'
249 else if (newfont.family() != IGNORE_FAMILY)
250 setFamily(newfont.family());
251 // else it's IGNORE_SHAPE
253 // "Old" behaviour: "Setting" bold will toggle bold on/off.
254 switch (newfont.series()) {
257 if (series() == BOLD_SERIES && toggleall)
258 setSeries(MEDIUM_SERIES);
260 setSeries(BOLD_SERIES);
264 setSeries(newfont.series());
270 if(newfont.shape() == shape() && toggleall)
271 setShape(INHERIT_SHAPE); // toggle 'back'
272 else if (newfont.shape() != IGNORE_SHAPE)
273 setShape(newfont.shape());
274 // else it's IGNORE_SHAPE
276 if (newfont.size() != IGNORE_SIZE) {
277 if (newfont.size() == INCREASE_SIZE)
279 else if (newfont.size() == DECREASE_SIZE)
281 else if (newfont.size() == size() && toggleall)
282 setSize(INHERIT_SIZE); // toggle 'back'
284 setSize(newfont.size());
287 setEmph(setMisc(newfont.emph(), emph()));
288 setUnderbar(setMisc(newfont.underbar(), underbar()));
289 setNoun(setMisc(newfont.noun(), noun()));
290 setLatex(setMisc(newfont.latex(), latex()));
292 if (newfont.language() == language() && toggleall)
293 if (language() == document_language)
294 setLanguage(default_language);
296 setLanguage(document_language);
297 else if (newfont.language() != ignore_language)
298 setLanguage(newfont.language());
300 if (newfont.color() == color() && toggleall)
301 setColor(LColor::inherit); // toggle 'back'
302 else if (newfont.color() != LColor::ignore)
303 setColor(newfont.color());
307 /// Reduce font to fall back to template where possible
308 void LyXFont::reduce(LyXFont const & tmplt)
310 if (family() == tmplt.family())
311 setFamily(INHERIT_FAMILY);
312 if (series() == tmplt.series())
313 setSeries(INHERIT_SERIES);
314 if (shape() == tmplt.shape())
315 setShape(INHERIT_SHAPE);
316 if (size() == tmplt.size())
317 setSize(INHERIT_SIZE);
318 if (emph() == tmplt.emph())
320 if (underbar() == tmplt.underbar())
321 setUnderbar(INHERIT);
322 if (noun() == tmplt.noun())
324 if (latex() == tmplt.latex())
326 if (color() == tmplt.color())
327 setColor(LColor::inherit);
331 /// Realize font from a template
332 LyXFont & LyXFont::realize(LyXFont const & tmplt)
334 if (bits == inherit) {
339 if (bits.family == INHERIT_FAMILY) {
340 bits.family = tmplt.bits.family;
342 if (bits.series == INHERIT_SERIES) {
343 bits.series = tmplt.bits.series;
345 if (bits.shape == INHERIT_SHAPE) {
346 bits.shape = tmplt.bits.shape;
348 if (bits.size == INHERIT_SIZE) {
349 bits.size = tmplt.bits.size;
351 if (bits.emph == INHERIT) {
352 bits.emph = tmplt.bits.emph;
354 if (bits.underbar == INHERIT) {
355 bits.underbar = tmplt.bits.underbar;
357 if (bits.noun == INHERIT) {
358 bits.noun = tmplt.bits.noun;
360 if (bits.latex == INHERIT) {
361 bits.latex = tmplt.bits.latex;
363 if (bits.color == LColor::inherit) {
364 bits.color = tmplt.bits.color;
371 /// Is font resolved?
372 bool LyXFont::resolved() const
374 return (family() != INHERIT_FAMILY && series() != INHERIT_SERIES &&
375 shape() != INHERIT_SHAPE && size() != INHERIT_SIZE &&
376 emph() != INHERIT && underbar() != INHERIT &&
377 noun() != INHERIT && latex() != INHERIT &&
378 color() != LColor::inherit);
382 /// Build GUI description of font state
383 string LyXFont::stateText() const
386 std::ostringstream ost;
389 ostrstream ost(str, 1024);
391 if (family() != INHERIT_FAMILY)
392 ost << _(GUIFamilyNames[family()]) << ", ";
393 if (series() != INHERIT_SERIES)
394 ost << _(GUISeriesNames[series()]) << ", ";
395 if (shape() != INHERIT_SHAPE)
396 ost << _(GUIShapeNames[shape()]) << ", ";
397 if (size() != INHERIT_SIZE)
398 ost << _(GUISizeNames[size()]) << ", ";
399 if (color() != LColor::inherit)
400 ost << lcolor.getGUIName(color()) << ", ";
401 if (emph() != INHERIT)
402 ost << _("Emphasis ")
403 << _(GUIMiscNames[emph()]) << ", ";
404 if (underbar() != INHERIT)
405 ost << _("Underline ")
406 << _(GUIMiscNames[underbar()]) << ", ";
407 if (noun() != INHERIT)
408 ost << _("Noun ") << _(GUIMiscNames[noun()]) << ", ";
409 if (latex() != INHERIT)
410 ost << _("Latex ") << _(GUIMiscNames[latex()]) << ", ";
412 ost << _("Default") << ", ";
413 ost << _("Language: ") << _(language()->display.c_str());
415 string buf(ost.str().c_str());
418 string buf(ost.str());
420 buf = strip(buf, ' ');
421 buf = strip(buf, ',');
426 // Set family according to lyx format string
427 LyXFont & LyXFont::setLyXFamily(string const & fam)
429 string s = lowercase(fam);
432 while (s != LyXFamilyNames[i] && LyXFamilyNames[i] != "error") ++i;
433 if (s == LyXFamilyNames[i]) {
434 setFamily(LyXFont::FONT_FAMILY(i));
436 lyxerr << "LyXFont::setLyXFamily: Unknown family `"
437 << s << '\'' << endl;
442 // Set series according to lyx format string
443 LyXFont & LyXFont::setLyXSeries(string const & ser)
445 string s = lowercase(ser);
448 while (s != LyXSeriesNames[i] && LyXSeriesNames[i] != "error") ++i;
449 if (s == LyXSeriesNames[i]) {
450 setSeries(LyXFont::FONT_SERIES(i));
452 lyxerr << "LyXFont::setLyXSeries: Unknown series `"
453 << s << '\'' << endl;
458 // Set shape according to lyx format string
459 LyXFont & LyXFont::setLyXShape(string const & sha)
461 string s = lowercase(sha);
464 while (s != LyXShapeNames[i] && LyXShapeNames[i] != "error") ++i;
465 if (s == LyXShapeNames[i]) {
466 setShape(LyXFont::FONT_SHAPE(i));
468 lyxerr << "LyXFont::setLyXShape: Unknown shape `"
469 << s << '\'' << endl;
474 // Set size according to lyx format string
475 LyXFont & LyXFont::setLyXSize(string const & siz)
477 string s = lowercase(siz);
479 while (s != LyXSizeNames[i] && LyXSizeNames[i] != "error") ++i;
480 if (s == LyXSizeNames[i]) {
481 setSize(LyXFont::FONT_SIZE(i));
483 lyxerr << "LyXFont::setLyXSize: Unknown size `"
484 << s << '\'' << endl;
489 // Set size according to lyx format string
490 LyXFont::FONT_MISC_STATE LyXFont::setLyXMisc(string const & siz)
492 string s = lowercase(siz);
494 while (s != LyXMiscNames[i] && LyXMiscNames[i] != "error") ++i;
495 if (s == LyXMiscNames[i])
496 return FONT_MISC_STATE(i);
497 lyxerr << "LyXFont::setLyXMisc: Unknown misc flag `"
498 << s << '\'' << endl;
503 /// Sets color after LyX text format
504 LyXFont & LyXFont::setLyXColor(string const & col)
506 setColor(lcolor.getFromLyXName(col));
511 /// Sets size after GUI name
512 LyXFont & LyXFont::setGUISize(string const & siz)
514 // CHECK this might be wrong.
516 // this is how it was how it was done in the lyx repository...
517 // but this does not make sense.
518 setColor(lcolor.getFromGUIName(siz));
523 // Returns size in latex format
524 string LyXFont::latexSize() const
526 return LaTeXSizeNames[size()];
530 // Read a font definition from given file in lyx format
532 LyXFont & LyXFont::lyxRead(LyXLex & lex)
535 bool finished = false;
536 while (!finished && lex.IsOK() && !error) {
538 string tok = lowercase(lex.GetString());
542 } else if (tok == "endfont") {
544 } else if (tok == "family") {
546 string tok = lex.GetString();
548 } else if (tok == "series") {
550 string tok = lex.GetString();
552 } else if (tok == "shape") {
554 string tok = lex.GetString();
556 } else if (tok == "size") {
558 string tok = lex.GetString();
560 } else if (tok == "latex") {
562 string tok = lowercase(lex.GetString());
564 if (tok == "no_latex") {
566 } else if (tok == "latex") {
569 lex.printError("Illegal LaTeX type`$$Token'");
571 } else if (tok == "misc") {
573 string tok = lowercase(lex.GetString());
575 if (tok == "no_bar") {
577 } else if (tok == "no_emph") {
579 } else if (tok == "no_noun") {
581 } else if (tok == "emph") {
583 } else if (tok == "underbar") {
585 } else if (tok == "noun") {
588 lex.printError("Illegal misc type `$$Token´");
590 } else if (tok == "color") {
592 string tok = lex.GetString();
595 lex.printError("Unknown tag `$$Token'");
603 /// Writes the changes from this font to orgfont in .lyx format in file
604 void LyXFont::lyxWriteChanges(LyXFont const & orgfont, ostream & os) const
607 if (orgfont.family() != family()) {
608 os << "\\family " << LyXFamilyNames[family()] << " \n";
610 if (orgfont.series() != series()) {
611 os << "\\series " << LyXSeriesNames[series()] << " \n";
613 if (orgfont.shape() != shape()) {
614 os << "\\shape " << LyXShapeNames[shape()] << " \n";
616 if (orgfont.size() != size()) {
617 os << "\\size " << LyXSizeNames[size()] << " \n";
619 if (orgfont.emph() != emph()) {
620 os << "\\emph " << LyXMiscNames[emph()] << " \n";
622 if (orgfont.underbar() != underbar()) {
623 // This is only for backwards compatibility
624 switch (underbar()) {
625 case OFF: os << "\\bar no \n"; break;
626 case ON: os << "\\bar under \n"; break;
627 case TOGGLE: lyxerr << "LyXFont::lyxWriteFontChanges: "
628 "TOGGLE should not appear here!"
631 case INHERIT: os << "\\bar default \n"; break;
632 case IGNORE: lyxerr << "LyXFont::lyxWriteFontChanges: "
633 "IGNORE should not appear here!"
638 if (orgfont.noun() != noun()) {
639 os << "\\noun " << LyXMiscNames[noun()] << " \n";
641 if (orgfont.latex() != latex()) {
642 // This is only for backwards compatibility
644 case OFF: os << "\\latex no_latex \n"; break;
645 case ON: os << "\\latex latex \n"; break;
646 case TOGGLE: lyxerr << "LyXFont::lyxWriteFontChanges: "
647 "TOGGLE should not appear here!"
650 case INHERIT: os << "\\latex default \n"; break;
651 case IGNORE: lyxerr << "LyXFont::lyxWriteFontChanges: "
652 "IGNORE should not appear here!"
657 if (orgfont.color() != color()) {
658 os << "\\color " << lcolor.getLyXName(color()) << "\n";
660 if (orgfont.language() != language()) {
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 if (isRightToLeft() != prev.isRightToLeft()) {
679 if (isRightToLeft()) {
682 env = true; //We have opened a new environment
686 env = true; //We have opened a new environment
690 subst(lyxrc.language_command_begin,
691 "$$lang", language()->lang);
693 count += tmp.length();
694 env = true; //We have opened a new environment
701 if (f.family() != INHERIT_FAMILY) {
703 << LaTeXFamilyNames[f.family()]
705 count += strlen(LaTeXFamilyNames[f.family()]) + 2;
706 env = true; //We have opened a new environment
708 if (f.series() != INHERIT_SERIES) {
710 << LaTeXSeriesNames[f.series()]
712 count += strlen(LaTeXSeriesNames[f.series()]) + 2;
713 env = true; //We have opened a new environment
715 if (f.shape() != INHERIT_SHAPE) {
717 << LaTeXShapeNames[f.shape()]
719 count += strlen(LaTeXShapeNames[f.shape()]) + 2;
720 env = true; //We have opened a new environment
722 if (f.color() != LColor::inherit) {
724 << lcolor.getLaTeXName(f.color())
726 count += lcolor.getLaTeXName(f.color()).length() + 13;
727 env = true; //We have opened a new environment
729 if (f.emph() == ON) {
732 env = true; //We have opened a new environment
734 if (f.underbar() == ON) {
737 env = true; //We have opened a new environment
739 // \noun{} is a LyX special macro
740 if (f.noun() == ON) {
743 env = true; //We have opened a new environment
745 if (f.size() != INHERIT_SIZE) {
746 // If we didn't open an environment above, we open one here
752 << LaTeXSizeNames[f.size()]
754 count += strlen(LaTeXSizeNames[f.size()]) + 2;
760 /// Writes ending block of LaTeX needed to close use of this font
761 // Returns number of chars written
762 // This one corresponds to latexWriteStartChanges(). (Asger)
763 int LyXFont::latexWriteEndChanges(ostream & os, LyXFont const & base,
764 LyXFont const & next) const
769 if (language() != base.language() && language() != next.language()) {
772 env = true; // Size change need not bother about closing env.
775 LyXFont f = *this; // why do you need this?
776 f.reduce(base); // why isn't this just "reduce(base);" (Lgb)
777 // Because this function is const. Everything breaks if this
778 // method changes the font it represents. There is no speed penalty
779 // by using the temporary. (Asger)
781 if (f.family() != INHERIT_FAMILY) {
784 env = true; // Size change need not bother about closing env.
786 if (f.series() != INHERIT_SERIES) {
789 env = true; // Size change need not bother about closing env.
791 if (f.shape() != INHERIT_SHAPE) {
794 env = true; // Size change need not bother about closing env.
796 if (f.color() != LColor::inherit) {
799 env = true; // Size change need not bother about closing env.
801 if (f.emph() == ON) {
804 env = true; // Size change need not bother about closing env.
806 if (f.underbar() == ON) {
809 env = true; // Size change need not bother about closing env.
811 if (f.noun() == ON) {
814 env = true; // Size change need not bother about closing env.
816 if (f.size() != INHERIT_SIZE) {
817 // We only have to close if only size changed
828 LColor::color LyXFont::realColor() const
831 return LColor::latex;
832 if (color() == LColor::none)
833 return LColor::foreground;
838 // Convert logical attributes to concrete shape attribute
839 LyXFont::FONT_SHAPE LyXFont::realShape() const
841 register FONT_SHAPE s = shape();
855 bool LyXFont::equalExceptLatex(LyXFont const & f) const
858 f1.setLatex(f.latex());
863 ostream & operator<<(ostream & o, LyXFont::FONT_MISC_STATE fms)
865 return o << int(fms);