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() != ignore_language)
293 if (language() == newfont.language())
294 if (language() == document_language)
295 setLanguage(default_language);
297 setLanguage(document_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 LyXFont::stateText() const
387 std::ostringstream ost;
390 ostrstream ost(str, 1024);
392 if (family() != INHERIT_FAMILY)
393 ost << _(GUIFamilyNames[family()]) << ", ";
394 if (series() != INHERIT_SERIES)
395 ost << _(GUISeriesNames[series()]) << ", ";
396 if (shape() != INHERIT_SHAPE)
397 ost << _(GUIShapeNames[shape()]) << ", ";
398 if (size() != INHERIT_SIZE)
399 ost << _(GUISizeNames[size()]) << ", ";
400 if (color() != LColor::inherit)
401 ost << lcolor.getGUIName(color()) << ", ";
402 if (emph() != INHERIT)
403 ost << _("Emphasis ")
404 << _(GUIMiscNames[emph()]) << ", ";
405 if (underbar() != INHERIT)
406 ost << _("Underline ")
407 << _(GUIMiscNames[underbar()]) << ", ";
408 if (noun() != INHERIT)
409 ost << _("Noun ") << _(GUIMiscNames[noun()]) << ", ";
410 if (latex() != INHERIT)
411 ost << _("Latex ") << _(GUIMiscNames[latex()]) << ", ";
413 // ost << _("Default");
414 unsigned int opos = ost.tellp();
415 //lyxerr << "Opos: " << opos << endl;
417 ost << _("Default") << ", ";
418 ost << _("Language: ") << _(language()->display.c_str());
420 string buf(ost.str().c_str());
423 string buf(ost.str());
425 buf = strip(buf, ' ');
426 buf = strip(buf, ',');
431 // Set family according to lyx format string
432 LyXFont & LyXFont::setLyXFamily(string const & fam)
434 string s = lowercase(fam);
437 while (s != LyXFamilyNames[i] && LyXFamilyNames[i] != "error") ++i;
438 if (s == LyXFamilyNames[i]) {
439 setFamily(LyXFont::FONT_FAMILY(i));
441 lyxerr << "LyXFont::setLyXFamily: Unknown family `"
442 << s << '\'' << endl;
447 // Set series according to lyx format string
448 LyXFont & LyXFont::setLyXSeries(string const & ser)
450 string s = lowercase(ser);
453 while (s != LyXSeriesNames[i] && LyXSeriesNames[i] != "error") ++i;
454 if (s == LyXSeriesNames[i]) {
455 setSeries(LyXFont::FONT_SERIES(i));
457 lyxerr << "LyXFont::setLyXSeries: Unknown series `"
458 << s << '\'' << endl;
463 // Set shape according to lyx format string
464 LyXFont & LyXFont::setLyXShape(string const & sha)
466 string s = lowercase(sha);
469 while (s != LyXShapeNames[i] && LyXShapeNames[i] != "error") ++i;
470 if (s == LyXShapeNames[i]) {
471 setShape(LyXFont::FONT_SHAPE(i));
473 lyxerr << "LyXFont::setLyXShape: Unknown shape `"
474 << s << '\'' << endl;
479 // Set size according to lyx format string
480 LyXFont & LyXFont::setLyXSize(string const & siz)
482 string s = lowercase(siz);
484 while (s != LyXSizeNames[i] && LyXSizeNames[i] != "error") ++i;
485 if (s == LyXSizeNames[i]) {
486 setSize(LyXFont::FONT_SIZE(i));
488 lyxerr << "LyXFont::setLyXSize: Unknown size `"
489 << s << '\'' << endl;
494 // Set size according to lyx format string
495 LyXFont::FONT_MISC_STATE LyXFont::setLyXMisc(string const & siz)
497 string s = lowercase(siz);
499 while (s != LyXMiscNames[i] && LyXMiscNames[i] != "error") ++i;
500 if (s == LyXMiscNames[i])
501 return FONT_MISC_STATE(i);
502 lyxerr << "LyXFont::setLyXMisc: Unknown misc flag `"
503 << s << '\'' << endl;
508 /// Sets color after LyX text format
509 LyXFont & LyXFont::setLyXColor(string const & col)
511 setColor(lcolor.getFromLyXName(col));
516 /// Sets size after GUI name
517 LyXFont & LyXFont::setGUISize(string const & siz)
519 #warning this might be wrong.
521 // this is how it was how it was done in the lyx repository...
522 // but this does not make sense.
523 setColor(lcolor.getFromGUIName(siz));
528 // Returns size in latex format
529 string LyXFont::latexSize() const
531 return LaTeXSizeNames[size()];
535 // Read a font definition from given file in lyx format
537 LyXFont & LyXFont::lyxRead(LyXLex & lex)
540 bool finished = false;
541 while (!finished && lex.IsOK() && !error) {
543 string tok = lowercase(lex.GetString());
547 } else if (tok == "endfont") {
549 } else if (tok == "family") {
551 string tok = lex.GetString();
553 } else if (tok == "series") {
555 string tok = lex.GetString();
557 } else if (tok == "shape") {
559 string tok = lex.GetString();
561 } else if (tok == "size") {
563 string tok = lex.GetString();
565 } else if (tok == "latex") {
567 string tok = lowercase(lex.GetString());
569 if (tok == "no_latex") {
571 } else if (tok == "latex") {
574 lex.printError("Illegal LaTeX type`$$Token'");
576 } else if (tok == "misc") {
578 string tok = lowercase(lex.GetString());
580 if (tok == "no_bar") {
582 } else if (tok == "no_emph") {
584 } else if (tok == "no_noun") {
586 } else if (tok == "emph") {
588 } else if (tok == "underbar") {
590 } else if (tok == "noun") {
593 lex.printError("Illegal misc type `$$Token´");
595 } else if (tok == "color") {
597 string tok = lex.GetString();
600 lex.printError("Unknown tag `$$Token'");
608 /// Writes the changes from this font to orgfont in .lyx format in file
609 void LyXFont::lyxWriteChanges(LyXFont const & orgfont, ostream & os) const
612 if (orgfont.family() != family()) {
613 os << "\\family " << LyXFamilyNames[family()] << " \n";
615 if (orgfont.series() != series()) {
616 os << "\\series " << LyXSeriesNames[series()] << " \n";
618 if (orgfont.shape() != shape()) {
619 os << "\\shape " << LyXShapeNames[shape()] << " \n";
621 if (orgfont.size() != size()) {
622 os << "\\size " << LyXSizeNames[size()] << " \n";
624 if (orgfont.emph() != emph()) {
625 os << "\\emph " << LyXMiscNames[emph()] << " \n";
627 if (orgfont.underbar() != underbar()) {
628 // This is only for backwards compatibility
629 switch (underbar()) {
630 case OFF: os << "\\bar no \n"; break;
631 case ON: os << "\\bar under \n"; break;
632 case TOGGLE: lyxerr << "LyXFont::lyxWriteFontChanges: "
633 "TOGGLE should not appear here!"
636 case INHERIT: os << "\\bar default \n"; break;
637 case IGNORE: lyxerr << "LyXFont::lyxWriteFontChanges: "
638 "IGNORE should not appear here!"
643 if (orgfont.noun() != noun()) {
644 os << "\\noun " << LyXMiscNames[noun()] << " \n";
646 if (orgfont.latex() != latex()) {
647 // This is only for backwards compatibility
649 case OFF: os << "\\latex no_latex \n"; break;
650 case ON: os << "\\latex latex \n"; break;
651 case TOGGLE: lyxerr << "LyXFont::lyxWriteFontChanges: "
652 "TOGGLE should not appear here!"
655 case INHERIT: os << "\\latex default \n"; break;
656 case IGNORE: lyxerr << "LyXFont::lyxWriteFontChanges: "
657 "IGNORE should not appear here!"
662 if (orgfont.color() != color()) {
663 os << "\\color " << lcolor.getLyXName(color()) << "\n";
665 if (orgfont.language() != language()) {
667 os << "\\lang " << language()->lang << "\n";
669 os << "\\lang unknown\n";
674 /// Writes the head of the LaTeX needed to impose this font
675 // Returns number of chars written.
676 int LyXFont::latexWriteStartChanges(ostream & os, LyXFont const & base,
677 LyXFont const & prev) const
682 if (language() != base.language() && language() != prev.language()) {
683 if (isRightToLeft() != prev.isRightToLeft()) {
684 if (isRightToLeft()) {
687 env = true; //We have opened a new environment
691 env = true; //We have opened a new environment
695 subst(lyxrc.language_command_begin,
696 "$$lang", language()->lang);
698 count += tmp.length();
699 env = true; //We have opened a new environment
706 if (f.family() != INHERIT_FAMILY) {
708 << LaTeXFamilyNames[f.family()]
710 count += strlen(LaTeXFamilyNames[f.family()]) + 2;
711 env = true; //We have opened a new environment
713 if (f.series() != INHERIT_SERIES) {
715 << LaTeXSeriesNames[f.series()]
717 count += strlen(LaTeXSeriesNames[f.series()]) + 2;
718 env = true; //We have opened a new environment
720 if (f.shape() != INHERIT_SHAPE) {
722 << LaTeXShapeNames[f.shape()]
724 count += strlen(LaTeXShapeNames[f.shape()]) + 2;
725 env = true; //We have opened a new environment
727 if (f.color() != LColor::inherit) {
729 << lcolor.getLaTeXName(f.color())
731 count += lcolor.getLaTeXName(f.color()).length() + 13;
732 env = true; //We have opened a new environment
734 if (f.emph() == ON) {
737 env = true; //We have opened a new environment
739 if (f.underbar() == ON) {
742 env = true; //We have opened a new environment
744 // \noun{} is a LyX special macro
745 if (f.noun() == ON) {
748 env = true; //We have opened a new environment
750 if (f.size() != INHERIT_SIZE) {
751 // If we didn't open an environment above, we open one here
757 << LaTeXSizeNames[f.size()]
759 count += strlen(LaTeXSizeNames[f.size()]) + 2;
765 /// Writes ending block of LaTeX needed to close use of this font
766 // Returns number of chars written
767 // This one corresponds to latexWriteStartChanges(). (Asger)
768 int LyXFont::latexWriteEndChanges(ostream & os, LyXFont const & base,
769 LyXFont const & next) const
774 if (language() != base.language() && language() != next.language()) {
777 env = true; // Size change need not bother about closing env.
780 LyXFont f = *this; // why do you need this?
781 f.reduce(base); // why isn't this just "reduce(base);" (Lgb)
782 // Because this function is const. Everything breaks if this
783 // method changes the font it represents. There is no speed penalty
784 // by using the temporary. (Asger)
786 if (f.family() != INHERIT_FAMILY) {
789 env = true; // Size change need not bother about closing env.
791 if (f.series() != INHERIT_SERIES) {
794 env = true; // Size change need not bother about closing env.
796 if (f.shape() != INHERIT_SHAPE) {
799 env = true; // Size change need not bother about closing env.
801 if (f.color() != LColor::inherit) {
804 env = true; // Size change need not bother about closing env.
806 if (f.emph() == ON) {
809 env = true; // Size change need not bother about closing env.
811 if (f.underbar() == ON) {
814 env = true; // Size change need not bother about closing env.
816 if (f.noun() == ON) {
819 env = true; // Size change need not bother about closing env.
821 if (f.size() != INHERIT_SIZE) {
822 // We only have to close if only size changed
833 LColor::color LyXFont::realColor() const
836 return LColor::latex;
837 if (color() == LColor::none)
838 return LColor::foreground;
843 // Convert logical attributes to concrete shape attribute
844 LyXFont::FONT_SHAPE LyXFont::realShape() const
846 register FONT_SHAPE s = shape();
860 bool LyXFont::equalExceptLatex(LyXFont const & f) const
863 f1.setLatex(f.latex());
868 ostream & operator<<(ostream & o, LyXFont::FONT_MISC_STATE fms)
870 return o << int(fms);