1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1995 Matthias Ettrich
7 * Copyright 1995-2001 The LyX Team.
9 * ====================================================== */
16 #pragma implementation
26 #include "support/lstrings.h"
27 #include "bufferparams.h" // stateText
32 #ifndef CXX_GLOBAL_CSTD
42 char const * GUIFamilyNames[LyXFont::NUM_FAMILIES + 2 /* default & error */] =
43 { N_("Roman"), N_("Sans serif"), N_("Typewriter"), N_("Symbol"),
44 "cmr", "cmsy", "cmm", "cmex", "msa", "msb", "eufrak", "wasy",
45 N_("Inherit"), N_("Ignore") };
47 char const * GUISeriesNames[4] =
48 { N_("Medium"), N_("Bold"), N_("Inherit"), N_("Ignore") };
50 char const * GUIShapeNames[6] =
51 { N_("Upright"), N_("Italic"), N_("Slanted"), N_("Smallcaps"), N_("Inherit"),
54 char const * GUISizeNames[14] =
55 { N_("Tiny"), N_("Smallest"), N_("Smaller"), N_("Small"), N_("Normal"), N_("Large"),
56 N_("Larger"), N_("Largest"), N_("Huge"), N_("Huger"), N_("Increase"), N_("Decrease"),
57 N_("Inherit"), N_("Ignore") };
59 char const * GUIMiscNames[5] =
60 { N_("Off"), N_("On"), N_("Toggle"), N_("Inherit"), N_("Ignore") };
64 // Strings used to read and write .lyx format files
66 char const * LyXFamilyNames[LyXFont::NUM_FAMILIES + 2 /* default & error */] =
67 { "roman", "sans", "typewriter", "symbol",
68 "cmr", "cmsy", "cmm", "cmex", "msa", "msb", "eufrak", "wasy",
71 char const * LyXSeriesNames[4] =
72 { "medium", "bold", "default", "error" };
74 char const * LyXShapeNames[6] =
75 { "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", "decrease", "default", "error" };
82 char const * LyXMiscNames[5] =
83 { "off", "on", "toggle", "default", "error" };
86 // Strings used to write LaTeX files
88 char const * LaTeXFamilyNames[6] =
89 { "textrm", "textsf", "texttt", "error1", "error2", "error3" };
91 char const * LaTeXSeriesNames[4] =
92 { "textmd", "textbf", "error4", "error5" };
94 char const * LaTeXShapeNames[6] =
95 { "textup", "textit", "textsl", "textsc", "error6", "error7" };
97 char const * LaTeXSizeNames[14] =
98 { "tiny", "scriptsize", "footnotesize", "small", "normalsize", "large",
99 "Large", "LARGE", "huge", "Huge", "error8", "error9", "error10", "error11" };
104 // Initialize static member
105 LyXFont::FontBits LyXFont::sane = {
116 // Initialize static member
117 LyXFont::FontBits LyXFont::inherit = {
128 // Initialize static member
129 LyXFont::FontBits LyXFont::ignore = {
141 bool operator==(LyXFont::FontBits const & lhs,
142 LyXFont::FontBits const & rhs)
144 return lhs.family == rhs.family &&
145 lhs.series == rhs.series &&
146 lhs.shape == rhs.shape &&
147 lhs.size == rhs.size &&
148 lhs.color == rhs.color &&
149 lhs.emph == rhs.emph &&
150 lhs.underbar == rhs.underbar &&
151 lhs.noun == rhs.noun &&
152 lhs.number == rhs.number;
157 : bits(sane), lang(default_language)
161 LyXFont::LyXFont(LyXFont::FONT_INIT1)
162 : bits(inherit), lang(default_language)
166 LyXFont::LyXFont(LyXFont::FONT_INIT2)
167 : bits(ignore), lang(ignore_language)
171 LyXFont::LyXFont(LyXFont::FONT_INIT3)
172 : bits(sane), lang(default_language)
176 LyXFont::LyXFont(LyXFont::FONT_INIT1, Language const * l)
177 : bits(inherit), lang(l)
181 LyXFont::LyXFont(LyXFont::FONT_INIT2, Language const * l)
182 : bits(ignore), lang(l)
186 LyXFont::LyXFont(LyXFont::FONT_INIT3, Language const * l)
187 : bits(sane), lang(l)
191 LyXFont::FONT_MISC_STATE LyXFont::underbar() const
193 return bits.underbar;
197 LColor::color LyXFont::color() const
203 Language const * LyXFont::language() const
209 LyXFont::FONT_MISC_STATE LyXFont::number() const
215 bool LyXFont::isRightToLeft() const
217 return lang->RightToLeft();
221 bool LyXFont::isVisibleRightToLeft() const
223 return (lang->RightToLeft() &&
228 LyXFont & LyXFont::setFamily(LyXFont::FONT_FAMILY f)
235 LyXFont & LyXFont::setSeries(LyXFont::FONT_SERIES s)
242 LyXFont & LyXFont::setShape(LyXFont::FONT_SHAPE s)
249 LyXFont & LyXFont::setSize(LyXFont::FONT_SIZE s)
256 LyXFont & LyXFont::setEmph(LyXFont::FONT_MISC_STATE e)
263 LyXFont & LyXFont::setUnderbar(LyXFont::FONT_MISC_STATE u)
270 LyXFont & LyXFont::setNoun(LyXFont::FONT_MISC_STATE n)
277 LyXFont & LyXFont::setColor(LColor::color c)
284 LyXFont & LyXFont::setLanguage(Language const * l)
291 LyXFont & LyXFont::setNumber(LyXFont::FONT_MISC_STATE n)
298 /// Decreases font size by one
299 LyXFont & LyXFont::decSize()
302 case SIZE_HUGER: setSize(SIZE_HUGE); break;
303 case SIZE_HUGE: setSize(SIZE_LARGEST); break;
304 case SIZE_LARGEST: setSize(SIZE_LARGER); break;
305 case SIZE_LARGER: setSize(SIZE_LARGE); break;
306 case SIZE_LARGE: setSize(SIZE_NORMAL); break;
307 case SIZE_NORMAL: setSize(SIZE_SMALL); break;
308 case SIZE_SMALL: setSize(SIZE_FOOTNOTE); break;
309 case SIZE_FOOTNOTE: setSize(SIZE_SCRIPT); break;
310 case SIZE_SCRIPT: setSize(SIZE_TINY); break;
311 case SIZE_TINY: break;
313 lyxerr << "Can't LyXFont::decSize on INCREASE_SIZE" << endl;
316 lyxerr <<"Can't LyXFont::decSize on DECREASE_SIZE" << endl;
319 lyxerr <<"Can't LyXFont::decSize on INHERIT_SIZE" << endl;
322 lyxerr <<"Can't LyXFont::decSize on IGNORE_SIZE" << endl;
329 /// Increases font size by one
330 LyXFont & LyXFont::incSize()
333 case SIZE_HUGER: break;
334 case SIZE_HUGE: setSize(SIZE_HUGER); break;
335 case SIZE_LARGEST: setSize(SIZE_HUGE); break;
336 case SIZE_LARGER: setSize(SIZE_LARGEST); break;
337 case SIZE_LARGE: setSize(SIZE_LARGER); break;
338 case SIZE_NORMAL: setSize(SIZE_LARGE); break;
339 case SIZE_SMALL: setSize(SIZE_NORMAL); break;
340 case SIZE_FOOTNOTE: setSize(SIZE_SMALL); break;
341 case SIZE_SCRIPT: setSize(SIZE_FOOTNOTE); break;
342 case SIZE_TINY: setSize(SIZE_SCRIPT); break;
344 lyxerr <<"Can't LyXFont::incSize on INCREASE_SIZE" << endl;
347 lyxerr <<"Can't LyXFont::incSize on DECREASE_SIZE" << endl;
350 lyxerr <<"Can't LyXFont::incSize on INHERIT_SIZE" << endl;
353 lyxerr <<"Can't LyXFont::incSize on IGNORE_SIZE" << endl;
360 /// Updates a misc setting according to request
361 LyXFont::FONT_MISC_STATE LyXFont::setMisc(FONT_MISC_STATE newfont,
364 if (newfont == TOGGLE) {
370 lyxerr <<"LyXFont::setMisc: Need state"
371 " ON or OFF to toggle. Setting to ON" << endl;
374 } else if (newfont == IGNORE)
381 /// Updates font settings according to request
382 void LyXFont::update(LyXFont const & newfont,
383 Language const * document_language,
386 if (newfont.family() == family() && toggleall)
387 setFamily(INHERIT_FAMILY); // toggle 'back'
388 else if (newfont.family() != IGNORE_FAMILY)
389 setFamily(newfont.family());
390 // else it's IGNORE_SHAPE
392 // "Old" behaviour: "Setting" bold will toggle bold on/off.
393 switch (newfont.series()) {
396 if (series() == BOLD_SERIES && toggleall)
397 setSeries(MEDIUM_SERIES);
399 setSeries(BOLD_SERIES);
403 setSeries(newfont.series());
409 if (newfont.shape() == shape() && toggleall)
410 setShape(INHERIT_SHAPE); // toggle 'back'
411 else if (newfont.shape() != IGNORE_SHAPE)
412 setShape(newfont.shape());
413 // else it's IGNORE_SHAPE
415 if (newfont.size() != IGNORE_SIZE) {
416 if (newfont.size() == INCREASE_SIZE)
418 else if (newfont.size() == DECREASE_SIZE)
421 setSize(newfont.size());
424 setEmph(setMisc(newfont.emph(), emph()));
425 setUnderbar(setMisc(newfont.underbar(), underbar()));
426 setNoun(setMisc(newfont.noun(), noun()));
428 setNumber(setMisc(newfont.number(), number()));
429 if (newfont.language() == language() && toggleall)
430 if (language() == document_language)
431 setLanguage(default_language);
433 setLanguage(document_language);
434 else if (newfont.language() != ignore_language)
435 setLanguage(newfont.language());
437 if (newfont.color() == color() && toggleall)
438 setColor(LColor::inherit); // toggle 'back'
439 else if (newfont.color() != LColor::ignore)
440 setColor(newfont.color());
444 /// Reduce font to fall back to template where possible
445 void LyXFont::reduce(LyXFont const & tmplt)
447 if (family() == tmplt.family())
448 setFamily(INHERIT_FAMILY);
449 if (series() == tmplt.series())
450 setSeries(INHERIT_SERIES);
451 if (shape() == tmplt.shape())
452 setShape(INHERIT_SHAPE);
453 if (size() == tmplt.size())
454 setSize(INHERIT_SIZE);
455 if (emph() == tmplt.emph())
457 if (underbar() == tmplt.underbar())
458 setUnderbar(INHERIT);
459 if (noun() == tmplt.noun())
461 if (color() == tmplt.color())
462 setColor(LColor::inherit);
466 /// Realize font from a template
467 LyXFont & LyXFont::realize(LyXFont const & tmplt)
469 if (bits == inherit) {
474 if (bits.family == INHERIT_FAMILY) {
475 bits.family = tmplt.bits.family;
477 if (bits.series == INHERIT_SERIES) {
478 bits.series = tmplt.bits.series;
480 if (bits.shape == INHERIT_SHAPE) {
481 bits.shape = tmplt.bits.shape;
483 if (bits.size == INHERIT_SIZE) {
484 bits.size = tmplt.bits.size;
486 if (bits.emph == INHERIT) {
487 bits.emph = tmplt.bits.emph;
489 if (bits.underbar == INHERIT) {
490 bits.underbar = tmplt.bits.underbar;
492 if (bits.noun == INHERIT) {
493 bits.noun = tmplt.bits.noun;
495 if (bits.color == LColor::inherit) {
496 bits.color = tmplt.bits.color;
502 /// Is font resolved?
503 bool LyXFont::resolved() const
505 return (family() != INHERIT_FAMILY && series() != INHERIT_SERIES &&
506 shape() != INHERIT_SHAPE && size() != INHERIT_SIZE &&
507 emph() != INHERIT && underbar() != INHERIT &&
509 color() != LColor::inherit);
513 /// Build GUI description of font state
514 string const LyXFont::stateText(BufferParams * params) const
517 if (family() != INHERIT_FAMILY)
518 ost << _(GUIFamilyNames[family()]) << ", ";
519 if (series() != INHERIT_SERIES)
520 ost << _(GUISeriesNames[series()]) << ", ";
521 if (shape() != INHERIT_SHAPE)
522 ost << _(GUIShapeNames[shape()]) << ", ";
523 if (size() != INHERIT_SIZE)
524 ost << _(GUISizeNames[size()]) << ", ";
525 if (color() != LColor::inherit)
526 ost << lcolor.getGUIName(color()) << ", ";
527 if (emph() != INHERIT)
528 ost << _("Emphasis ")
529 << _(GUIMiscNames[emph()]) << ", ";
530 if (underbar() != INHERIT)
531 ost << _("Underline ")
532 << _(GUIMiscNames[underbar()]) << ", ";
533 if (noun() != INHERIT)
534 ost << _("Noun ") << _(GUIMiscNames[noun()]) << ", ";
536 ost << _("Default") << ", ";
537 if (!params || (language() != params->language))
538 ost << _("Language: ") << _(language()->display()) << ", ";
540 ost << _(" Number ") << _(GUIMiscNames[number()]);
542 string buf(STRCONV(ost.str()));
543 buf = rtrim(buf, ", ");
548 // Set family according to lyx format string
549 LyXFont & LyXFont::setLyXFamily(string const & fam)
551 string const s = ascii_lowercase(fam);
554 while (s != LyXFamilyNames[i] && LyXFamilyNames[i] != "error") ++i;
555 if (s == LyXFamilyNames[i]) {
556 setFamily(LyXFont::FONT_FAMILY(i));
558 lyxerr << "LyXFont::setLyXFamily: Unknown family `"
559 << s << '\'' << endl;
564 // Set series according to lyx format string
565 LyXFont & LyXFont::setLyXSeries(string const & ser)
567 string const s = ascii_lowercase(ser);
570 while (s != LyXSeriesNames[i] && LyXSeriesNames[i] != "error") ++i;
571 if (s == LyXSeriesNames[i]) {
572 setSeries(LyXFont::FONT_SERIES(i));
574 lyxerr << "LyXFont::setLyXSeries: Unknown series `"
575 << s << '\'' << endl;
580 // Set shape according to lyx format string
581 LyXFont & LyXFont::setLyXShape(string const & sha)
583 string const s = ascii_lowercase(sha);
586 while (s != LyXShapeNames[i] && LyXShapeNames[i] != "error") ++i;
587 if (s == LyXShapeNames[i]) {
588 setShape(LyXFont::FONT_SHAPE(i));
590 lyxerr << "LyXFont::setLyXShape: Unknown shape `"
591 << s << '\'' << endl;
596 // Set size according to lyx format string
597 LyXFont & LyXFont::setLyXSize(string const & siz)
599 string const s = ascii_lowercase(siz);
601 while (s != LyXSizeNames[i] && LyXSizeNames[i] != "error") ++i;
602 if (s == LyXSizeNames[i]) {
603 setSize(LyXFont::FONT_SIZE(i));
605 lyxerr << "LyXFont::setLyXSize: Unknown size `"
606 << s << '\'' << endl;
611 // Set size according to lyx format string
612 LyXFont::FONT_MISC_STATE LyXFont::setLyXMisc(string const & siz)
614 string const s = ascii_lowercase(siz);
616 while (s != LyXMiscNames[i] && LyXMiscNames[i] != "error") ++i;
617 if (s == LyXMiscNames[i])
618 return FONT_MISC_STATE(i);
619 lyxerr << "LyXFont::setLyXMisc: Unknown misc flag `"
620 << s << '\'' << endl;
625 /// Sets color after LyX text format
626 LyXFont & LyXFont::setLyXColor(string const & col)
628 setColor(lcolor.getFromLyXName(col));
633 // Returns size in latex format
634 string const LyXFont::latexSize() const
636 return LaTeXSizeNames[size()];
640 // Read a font definition from given file in lyx format
642 LyXFont & LyXFont::lyxRead(LyXLex & lex)
645 bool finished = false;
646 while (!finished && lex.isOK() && !error) {
648 string const tok = ascii_lowercase(lex.getString());
652 } else if (tok == "endfont") {
654 } else if (tok == "family") {
656 string const ttok = lex.getString();
658 } else if (tok == "series") {
660 string const ttok = lex.getString();
662 } else if (tok == "shape") {
664 string const ttok = lex.getString();
666 } else if (tok == "size") {
668 string const ttok = lex.getString();
670 } else if (tok == "misc") {
672 string const ttok = ascii_lowercase(lex.getString());
674 if (ttok == "no_bar") {
676 } else if (ttok == "no_emph") {
678 } else if (ttok == "no_noun") {
680 } else if (ttok == "emph") {
682 } else if (ttok == "underbar") {
684 } else if (ttok == "noun") {
687 lex.printError("Illegal misc type `$$Token´");
689 } else if (tok == "color") {
691 string const ttok = lex.getString();
694 lex.printError("Unknown tag `$$Token'");
702 /// Writes the changes from this font to orgfont in .lyx format in file
703 void LyXFont::lyxWriteChanges(LyXFont const & orgfont,
707 if (orgfont.family() != family()) {
708 os << "\\family " << LyXFamilyNames[family()] << " \n";
710 if (orgfont.series() != series()) {
711 os << "\\series " << LyXSeriesNames[series()] << " \n";
713 if (orgfont.shape() != shape()) {
714 os << "\\shape " << LyXShapeNames[shape()] << " \n";
716 if (orgfont.size() != size()) {
717 os << "\\size " << LyXSizeNames[size()] << " \n";
719 if (orgfont.emph() != emph()) {
720 os << "\\emph " << LyXMiscNames[emph()] << " \n";
722 if (orgfont.number() != number()) {
723 os << "\\numeric " << LyXMiscNames[number()] << " \n";
725 if (orgfont.underbar() != underbar()) {
726 // This is only for backwards compatibility
727 switch (underbar()) {
728 case OFF: os << "\\bar no \n"; break;
729 case ON: os << "\\bar under \n"; break;
730 case TOGGLE: lyxerr << "LyXFont::lyxWriteFontChanges: "
731 "TOGGLE should not appear here!"
734 case INHERIT: os << "\\bar default \n"; break;
735 case IGNORE: lyxerr << "LyXFont::lyxWriteFontChanges: "
736 "IGNORE should not appear here!"
741 if (orgfont.noun() != noun()) {
742 os << "\\noun " << LyXMiscNames[noun()] << " \n";
744 if (orgfont.color() != color()) {
745 // To make us file compatible with older
746 // lyx versions we emit "default" instead
748 string col_str(lcolor.getLyXName(color()));
749 if (col_str == "inherit") col_str = "default";
750 os << "\\color " << col_str << "\n";
752 if (orgfont.language() != language()) {
754 os << "\\lang " << language()->lang() << "\n";
756 os << "\\lang unknown\n";
761 /// Writes the head of the LaTeX needed to impose this font
762 // Returns number of chars written.
763 int LyXFont::latexWriteStartChanges(ostream & os, LyXFont const & base,
764 LyXFont const & prev) const
769 if (language()->babel() != base.language()->babel() &&
770 language() != prev.language()) {
771 if (isRightToLeft() != prev.isRightToLeft()) {
772 if (isRightToLeft()) {
781 subst(lyxrc.language_command_local,
782 "$$lang", language()->babel());
784 count += tmp.length();
788 if (number() == ON && prev.number() != ON &&
789 language()->lang() == "hebrew") {
797 if (f.family() != INHERIT_FAMILY) {
799 << LaTeXFamilyNames[f.family()]
801 count += strlen(LaTeXFamilyNames[f.family()]) + 2;
802 env = true; //We have opened a new environment
804 if (f.series() != INHERIT_SERIES) {
806 << LaTeXSeriesNames[f.series()]
808 count += strlen(LaTeXSeriesNames[f.series()]) + 2;
809 env = true; //We have opened a new environment
811 if (f.shape() != INHERIT_SHAPE) {
813 << LaTeXShapeNames[f.shape()]
815 count += strlen(LaTeXShapeNames[f.shape()]) + 2;
816 env = true; //We have opened a new environment
818 if (f.color() != LColor::inherit && f.color() != LColor::ignore) {
820 << lcolor.getLaTeXName(f.color())
822 count += lcolor.getLaTeXName(f.color()).length() + 13;
823 env = true; //We have opened a new environment
825 if (f.emph() == ON) {
828 env = true; //We have opened a new environment
830 if (f.underbar() == ON) {
833 env = true; //We have opened a new environment
835 // \noun{} is a LyX special macro
836 if (f.noun() == ON) {
839 env = true; //We have opened a new environment
841 if (f.size() != INHERIT_SIZE) {
842 // If we didn't open an environment above, we open one here
848 << LaTeXSizeNames[f.size()]
850 count += strlen(LaTeXSizeNames[f.size()]) + 2;
856 /// Writes ending block of LaTeX needed to close use of this font
857 // Returns number of chars written
858 // This one corresponds to latexWriteStartChanges(). (Asger)
859 int LyXFont::latexWriteEndChanges(ostream & os, LyXFont const & base,
860 LyXFont const & next) const
865 // reduce the current font to changes against the base
866 // font (of the layout). We use a temporary for this to
867 // avoid changing this font instance, as that would break
871 if (f.family() != INHERIT_FAMILY) {
874 env = true; // Size change need not bother about closing env.
876 if (f.series() != INHERIT_SERIES) {
879 env = true; // Size change need not bother about closing env.
881 if (f.shape() != INHERIT_SHAPE) {
884 env = true; // Size change need not bother about closing env.
886 if (f.color() != LColor::inherit && f.color() != LColor::ignore) {
889 env = true; // Size change need not bother about closing env.
891 if (f.emph() == ON) {
894 env = true; // Size change need not bother about closing env.
896 if (f.underbar() == ON) {
899 env = true; // Size change need not bother about closing env.
901 if (f.noun() == ON) {
904 env = true; // Size change need not bother about closing env.
906 if (f.size() != INHERIT_SIZE) {
907 // We only have to close if only size changed
914 if (number() == ON && next.number() != ON &&
915 language()->lang() == "hebrew") {
920 if (language() != base.language() && language() != next.language()) {
929 LColor::color LyXFont::realColor() const
931 if (color() == LColor::none)
932 return LColor::foreground;
937 // Convert logical attributes to concrete shape attribute
938 LyXFont::FONT_SHAPE LyXFont::realShape() const
940 register FONT_SHAPE s = shape();
954 ostream & operator<<(ostream & o, LyXFont::FONT_MISC_STATE fms)
956 return o << int(fms);