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 setLanguage(document_language);
295 else if (newfont.language() != ignore_language)
296 setLanguage(newfont.language());
298 if (newfont.color() == color() && toggleall)
299 setColor(LColor::inherit); // toggle 'back'
300 else if (newfont.color() != LColor::ignore)
301 setColor(newfont.color());
305 /// Reduce font to fall back to template where possible
306 void LyXFont::reduce(LyXFont const & tmplt)
308 if (family() == tmplt.family())
309 setFamily(INHERIT_FAMILY);
310 if (series() == tmplt.series())
311 setSeries(INHERIT_SERIES);
312 if (shape() == tmplt.shape())
313 setShape(INHERIT_SHAPE);
314 if (size() == tmplt.size())
315 setSize(INHERIT_SIZE);
316 if (emph() == tmplt.emph())
318 if (underbar() == tmplt.underbar())
319 setUnderbar(INHERIT);
320 if (noun() == tmplt.noun())
322 if (latex() == tmplt.latex())
324 if (color() == tmplt.color())
325 setColor(LColor::inherit);
329 /// Realize font from a template
330 LyXFont & LyXFont::realize(LyXFont const & tmplt)
332 if (bits == inherit) {
337 if (bits.family == INHERIT_FAMILY) {
338 bits.family = tmplt.bits.family;
340 if (bits.series == INHERIT_SERIES) {
341 bits.series = tmplt.bits.series;
343 if (bits.shape == INHERIT_SHAPE) {
344 bits.shape = tmplt.bits.shape;
346 if (bits.size == INHERIT_SIZE) {
347 bits.size = tmplt.bits.size;
349 if (bits.emph == INHERIT) {
350 bits.emph = tmplt.bits.emph;
352 if (bits.underbar == INHERIT) {
353 bits.underbar = tmplt.bits.underbar;
355 if (bits.noun == INHERIT) {
356 bits.noun = tmplt.bits.noun;
358 if (bits.latex == INHERIT) {
359 bits.latex = tmplt.bits.latex;
361 if (bits.color == LColor::inherit) {
362 bits.color = tmplt.bits.color;
369 /// Is font resolved?
370 bool LyXFont::resolved() const
372 return (family() != INHERIT_FAMILY && series() != INHERIT_SERIES &&
373 shape() != INHERIT_SHAPE && size() != INHERIT_SIZE &&
374 emph() != INHERIT && underbar() != INHERIT &&
375 noun() != INHERIT && latex() != INHERIT &&
376 color() != LColor::inherit);
380 /// Build GUI description of font state
381 string const LyXFont::stateText(BufferParams * params) const
383 std::ostringstream ost;
384 if (family() != INHERIT_FAMILY)
385 ost << _(GUIFamilyNames[family()]) << ", ";
386 if (series() != INHERIT_SERIES)
387 ost << _(GUISeriesNames[series()]) << ", ";
388 if (shape() != INHERIT_SHAPE)
389 ost << _(GUIShapeNames[shape()]) << ", ";
390 if (size() != INHERIT_SIZE)
391 ost << _(GUISizeNames[size()]) << ", ";
392 if (color() != LColor::inherit)
393 ost << lcolor.getGUIName(color()) << ", ";
394 if (emph() != INHERIT)
395 ost << _("Emphasis ")
396 << _(GUIMiscNames[emph()]) << ", ";
397 if (underbar() != INHERIT)
398 ost << _("Underline ")
399 << _(GUIMiscNames[underbar()]) << ", ";
400 if (noun() != INHERIT)
401 ost << _("Noun ") << _(GUIMiscNames[noun()]) << ", ";
402 if (latex() != INHERIT)
403 ost << _("Latex ") << _(GUIMiscNames[latex()]) << ", ";
405 ost << _("Default") << ", ";
406 if (!params || (language() != params->language_info))
407 ost << _("Language: ") << _(language()->display().c_str());
409 string buf(ost.str().c_str());
410 buf = strip(buf, ' ');
411 buf = strip(buf, ',');
416 // Set family according to lyx format string
417 LyXFont & LyXFont::setLyXFamily(string const & fam)
419 string s = lowercase(fam);
422 while (s != LyXFamilyNames[i] && LyXFamilyNames[i] != "error") ++i;
423 if (s == LyXFamilyNames[i]) {
424 setFamily(LyXFont::FONT_FAMILY(i));
426 lyxerr << "LyXFont::setLyXFamily: Unknown family `"
427 << s << '\'' << endl;
432 // Set series according to lyx format string
433 LyXFont & LyXFont::setLyXSeries(string const & ser)
435 string s = lowercase(ser);
438 while (s != LyXSeriesNames[i] && LyXSeriesNames[i] != "error") ++i;
439 if (s == LyXSeriesNames[i]) {
440 setSeries(LyXFont::FONT_SERIES(i));
442 lyxerr << "LyXFont::setLyXSeries: Unknown series `"
443 << s << '\'' << endl;
448 // Set shape according to lyx format string
449 LyXFont & LyXFont::setLyXShape(string const & sha)
451 string s = lowercase(sha);
454 while (s != LyXShapeNames[i] && LyXShapeNames[i] != "error") ++i;
455 if (s == LyXShapeNames[i]) {
456 setShape(LyXFont::FONT_SHAPE(i));
458 lyxerr << "LyXFont::setLyXShape: Unknown shape `"
459 << s << '\'' << endl;
464 // Set size according to lyx format string
465 LyXFont & LyXFont::setLyXSize(string const & siz)
467 string s = lowercase(siz);
469 while (s != LyXSizeNames[i] && LyXSizeNames[i] != "error") ++i;
470 if (s == LyXSizeNames[i]) {
471 setSize(LyXFont::FONT_SIZE(i));
473 lyxerr << "LyXFont::setLyXSize: Unknown size `"
474 << s << '\'' << endl;
479 // Set size according to lyx format string
480 LyXFont::FONT_MISC_STATE LyXFont::setLyXMisc(string const & siz)
482 string s = lowercase(siz);
484 while (s != LyXMiscNames[i] && LyXMiscNames[i] != "error") ++i;
485 if (s == LyXMiscNames[i])
486 return FONT_MISC_STATE(i);
487 lyxerr << "LyXFont::setLyXMisc: Unknown misc flag `"
488 << s << '\'' << endl;
493 /// Sets color after LyX text format
494 LyXFont & LyXFont::setLyXColor(string const & col)
496 setColor(lcolor.getFromLyXName(col));
501 /// Sets size after GUI name
502 LyXFont & LyXFont::setGUISize(string const & siz)
504 // CHECK this might be wrong.
506 // this is how it was how it was done in the lyx repository...
507 // but this does not make sense.
508 setColor(lcolor.getFromGUIName(siz));
513 // Returns size in latex format
514 string const LyXFont::latexSize() const
516 return LaTeXSizeNames[size()];
520 // Read a font definition from given file in lyx format
522 LyXFont & LyXFont::lyxRead(LyXLex & lex)
525 bool finished = false;
526 while (!finished && lex.IsOK() && !error) {
528 string tok = lowercase(lex.GetString());
532 } else if (tok == "endfont") {
534 } else if (tok == "family") {
536 string tok = lex.GetString();
538 } else if (tok == "series") {
540 string tok = lex.GetString();
542 } else if (tok == "shape") {
544 string tok = lex.GetString();
546 } else if (tok == "size") {
548 string tok = lex.GetString();
550 } else if (tok == "latex") {
552 string tok = lowercase(lex.GetString());
554 if (tok == "no_latex") {
556 } else if (tok == "latex") {
559 lex.printError("Illegal LaTeX type`$$Token'");
561 } else if (tok == "misc") {
563 string tok = lowercase(lex.GetString());
565 if (tok == "no_bar") {
567 } else if (tok == "no_emph") {
569 } else if (tok == "no_noun") {
571 } else if (tok == "emph") {
573 } else if (tok == "underbar") {
575 } else if (tok == "noun") {
578 lex.printError("Illegal misc type `$$Token´");
580 } else if (tok == "color") {
582 string tok = lex.GetString();
585 lex.printError("Unknown tag `$$Token'");
593 /// Writes the changes from this font to orgfont in .lyx format in file
594 void LyXFont::lyxWriteChanges(LyXFont const & orgfont, ostream & os) const
597 if (orgfont.family() != family()) {
598 os << "\\family " << LyXFamilyNames[family()] << " \n";
600 if (orgfont.series() != series()) {
601 os << "\\series " << LyXSeriesNames[series()] << " \n";
603 if (orgfont.shape() != shape()) {
604 os << "\\shape " << LyXShapeNames[shape()] << " \n";
606 if (orgfont.size() != size()) {
607 os << "\\size " << LyXSizeNames[size()] << " \n";
609 if (orgfont.emph() != emph()) {
610 os << "\\emph " << LyXMiscNames[emph()] << " \n";
612 if (orgfont.underbar() != underbar()) {
613 // This is only for backwards compatibility
614 switch (underbar()) {
615 case OFF: os << "\\bar no \n"; break;
616 case ON: os << "\\bar under \n"; break;
617 case TOGGLE: lyxerr << "LyXFont::lyxWriteFontChanges: "
618 "TOGGLE should not appear here!"
621 case INHERIT: os << "\\bar default \n"; break;
622 case IGNORE: lyxerr << "LyXFont::lyxWriteFontChanges: "
623 "IGNORE should not appear here!"
628 if (orgfont.noun() != noun()) {
629 os << "\\noun " << LyXMiscNames[noun()] << " \n";
631 if (orgfont.latex() != latex()) {
632 // This is only for backwards compatibility
634 case OFF: os << "\\latex no_latex \n"; break;
635 case ON: os << "\\latex latex \n"; break;
636 case TOGGLE: lyxerr << "LyXFont::lyxWriteFontChanges: "
637 "TOGGLE should not appear here!"
640 case INHERIT: os << "\\latex default \n"; break;
641 case IGNORE: lyxerr << "LyXFont::lyxWriteFontChanges: "
642 "IGNORE should not appear here!"
647 if (orgfont.color() != color()) {
648 // To make us file compatible with older
649 // lyx versions we emit "default" instead
651 string col_str(lcolor.getLyXName(color()));
652 if (col_str == "inherit") col_str = "default";
653 os << "\\color " << col_str << "\n";
655 if (orgfont.language() != language()) {
657 os << "\\lang " << language()->lang() << "\n";
659 os << "\\lang unknown\n";
664 /// Writes the head of the LaTeX needed to impose this font
665 // Returns number of chars written.
666 int LyXFont::latexWriteStartChanges(ostream & os, LyXFont const & base,
667 LyXFont const & prev) const
672 if (language() != base.language() && language() != prev.language()) {
673 if (isRightToLeft() != prev.isRightToLeft()) {
674 if (isRightToLeft()) {
677 env = true; //We have opened a new environment
681 env = true; //We have opened a new environment
685 subst(lyxrc.language_command_begin,
686 "$$lang", language()->lang());
688 count += tmp.length();
689 env = true; //We have opened a new environment
696 if (f.family() != INHERIT_FAMILY) {
698 << LaTeXFamilyNames[f.family()]
700 count += strlen(LaTeXFamilyNames[f.family()]) + 2;
701 env = true; //We have opened a new environment
703 if (f.series() != INHERIT_SERIES) {
705 << LaTeXSeriesNames[f.series()]
707 count += strlen(LaTeXSeriesNames[f.series()]) + 2;
708 env = true; //We have opened a new environment
710 if (f.shape() != INHERIT_SHAPE) {
712 << LaTeXShapeNames[f.shape()]
714 count += strlen(LaTeXShapeNames[f.shape()]) + 2;
715 env = true; //We have opened a new environment
717 if (f.color() != LColor::inherit && f.color() != LColor::ignore) {
719 << lcolor.getLaTeXName(f.color())
721 count += lcolor.getLaTeXName(f.color()).length() + 13;
722 env = true; //We have opened a new environment
724 if (f.emph() == ON) {
727 env = true; //We have opened a new environment
729 if (f.underbar() == ON) {
732 env = true; //We have opened a new environment
734 // \noun{} is a LyX special macro
735 if (f.noun() == ON) {
738 env = true; //We have opened a new environment
740 if (f.size() != INHERIT_SIZE) {
741 // If we didn't open an environment above, we open one here
747 << LaTeXSizeNames[f.size()]
749 count += strlen(LaTeXSizeNames[f.size()]) + 2;
755 /// Writes ending block of LaTeX needed to close use of this font
756 // Returns number of chars written
757 // This one corresponds to latexWriteStartChanges(). (Asger)
758 int LyXFont::latexWriteEndChanges(ostream & os, LyXFont const & base,
759 LyXFont const & next) const
764 if (language() != base.language() && language() != next.language()) {
767 env = true; // Size change need not bother about closing env.
770 LyXFont f = *this; // why do you need this?
771 f.reduce(base); // why isn't this just "reduce(base);" (Lgb)
772 // Because this function is const. Everything breaks if this
773 // method changes the font it represents. There is no speed penalty
774 // by using the temporary. (Asger)
776 if (f.family() != INHERIT_FAMILY) {
779 env = true; // Size change need not bother about closing env.
781 if (f.series() != INHERIT_SERIES) {
784 env = true; // Size change need not bother about closing env.
786 if (f.shape() != INHERIT_SHAPE) {
789 env = true; // Size change need not bother about closing env.
791 if (f.color() != LColor::inherit && f.color() != LColor::ignore) {
794 env = true; // Size change need not bother about closing env.
796 if (f.emph() == ON) {
799 env = true; // Size change need not bother about closing env.
801 if (f.underbar() == ON) {
804 env = true; // Size change need not bother about closing env.
806 if (f.noun() == ON) {
809 env = true; // Size change need not bother about closing env.
811 if (f.size() != INHERIT_SIZE) {
812 // We only have to close if only size changed
823 LColor::color LyXFont::realColor() const
826 return LColor::latex;
827 if (color() == LColor::none)
828 return LColor::foreground;
833 // Convert logical attributes to concrete shape attribute
834 LyXFont::FONT_SHAPE LyXFont::realShape() const
836 register FONT_SHAPE s = shape();
850 bool LyXFont::equalExceptLatex(LyXFont const & f) const
853 f1.setLatex(f.latex());
858 ostream & operator<<(ostream & o, LyXFont::FONT_MISC_STATE fms)
860 return o << int(fms);