1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1995 Matthias Ettrich
7 * Copyright 1995-1999 The LyX Team.
9 * ====================================================== */
15 #pragma implementation "lyxfont.h"
25 #include "FontLoader.h"
26 #include "support/lstrings.h"
30 // The global fontloader
31 FontLoader fontloader;
37 string const GUIFamilyNames[6] =
38 { N_("Roman"), N_("Sans serif"), N_("Typewriter"), N_("Symbol"), N_("Inherit"),
41 string const GUISeriesNames[4] =
42 { N_("Medium"), N_("Bold"), N_("Inherit"), N_("Ignore") };
44 string const GUIShapeNames[6] =
45 { N_("Upright"), N_("Italic"), N_("Slanted"), N_("Smallcaps"), N_("Inherit"),
48 string const GUISizeNames[14] =
49 { N_("Tiny"), N_("Smallest"), N_("Smaller"), N_("Small"), N_("Normal"), N_("Large"),
50 N_("Larger"), N_("Largest"), N_("Huge"), N_("Huger"), N_("Increase"), N_("Decrease"),
51 N_("Inherit"), N_("Ignore") };
53 string const lGUISizeNames[15] =
54 { N_("tiny"), N_("smallest"), N_("smaller"), N_("small"), N_("normal"), N_("large"),
55 N_("larger"), N_("largest"), N_("huge"), N_("huger"), N_("increase"), N_("decrease"),
56 N_("inherit"), N_("ignore"), string() };
58 string const GUIMiscNames[5] =
59 { N_("Off"), N_("On"), N_("Toggle"), N_("Inherit"), N_("Ignore") };
61 string const GUIDirectionNames[5] =
62 { N_("LTR"), N_("RTL"), N_("Toggle"), N_("Inherit"), N_("Ignore") };
66 // Strings used to read and write .lyx format files
68 string const LyXFamilyNames[6] =
69 { "roman", "sans", "typewriter", "symbol", "default", "error" };
71 string const LyXSeriesNames[4] =
72 { "medium", "bold", "default", "error" };
74 string const LyXShapeNames[6] =
75 { "up", "italic", "slanted", "smallcaps", "default", "error" };
77 string const LyXSizeNames[14] =
78 { "tiny", "scriptsize", "footnotesize", "small", "normal", "large",
79 "larger", "largest", "huge", "giant",
80 "increase-error", "decrease-error", "default", "error" };
82 string const LyXMiscNames[12] =
83 { "off", "on", "toggle", "default", "error" };
86 // Strings used to write LaTeX files
89 string const LaTeXFamilyNames[6] =
90 { "textrm", "textsf", "texttt", "error1", "error2", "error3" };
92 string const LaTeXSeriesNames[4] =
93 { "textmd", "textbf", "error4", "error5" };
95 string const LaTeXShapeNames[6] =
96 { "textup", "textit", "textsl", "textsc", "error6", "error7" };
98 string const LaTeXSizeNames[14] =
99 { "tiny", "scriptsize", "footnotesize", "small", "normalsize", "large",
100 "Large", "LARGE", "huge", "Huge", "error8", "error9", "error10", "error11" };
103 LyXFont::FontBits LyXFont::sane = {
115 LyXFont::FontBits LyXFont::inherit = {
127 LyXFont::FontBits LyXFont::ignore = {
140 /// Decreases font size by one
141 LyXFont & LyXFont::decSize()
144 case SIZE_HUGER: setSize(SIZE_HUGE); break;
145 case SIZE_HUGE: setSize(SIZE_LARGEST); break;
146 case SIZE_LARGEST: setSize(SIZE_LARGER); break;
147 case SIZE_LARGER: setSize(SIZE_LARGE); break;
148 case SIZE_LARGE: setSize(SIZE_NORMAL); break;
149 case SIZE_NORMAL: setSize(SIZE_SMALL); break;
150 case SIZE_SMALL: setSize(SIZE_FOOTNOTE); break;
151 case SIZE_FOOTNOTE: setSize(SIZE_SCRIPT); break;
152 case SIZE_SCRIPT: setSize(SIZE_TINY); break;
153 case SIZE_TINY: break;
155 lyxerr << "Can't LyXFont::decSize on INCREASE_SIZE" << endl;
158 lyxerr <<"Can't LyXFont::decSize on DECREASE_SIZE" << endl;
161 lyxerr <<"Can't LyXFont::decSize on INHERIT_SIZE" << endl;
164 lyxerr <<"Can't LyXFont::decSize on IGNORE_SIZE" << endl;
171 /// Increases font size by one
172 LyXFont & LyXFont::incSize()
175 case SIZE_HUGER: break;
176 case SIZE_HUGE: setSize(SIZE_HUGER); break;
177 case SIZE_LARGEST: setSize(SIZE_HUGE); break;
178 case SIZE_LARGER: setSize(SIZE_LARGEST); break;
179 case SIZE_LARGE: setSize(SIZE_LARGER); break;
180 case SIZE_NORMAL: setSize(SIZE_LARGE); break;
181 case SIZE_SMALL: setSize(SIZE_NORMAL); break;
182 case SIZE_FOOTNOTE: setSize(SIZE_SMALL); break;
183 case SIZE_SCRIPT: setSize(SIZE_FOOTNOTE); break;
184 case SIZE_TINY: setSize(SIZE_SCRIPT); break;
186 lyxerr <<"Can't LyXFont::incSize on INCREASE_SIZE" << endl;
189 lyxerr <<"Can't LyXFont::incSize on DECREASE_SIZE" << endl;
192 lyxerr <<"Can't LyXFont::incSize on INHERIT_SIZE" << endl;
195 lyxerr <<"Can't LyXFont::incSize on IGNORE_SIZE" << endl;
202 /// Updates a misc setting according to request
203 LyXFont::FONT_MISC_STATE LyXFont::setMisc(FONT_MISC_STATE newfont,
206 if (newfont == TOGGLE) {
212 lyxerr <<"LyXFont::setMisc: Need state"
213 " ON or OFF to toggle. Setting to ON" << endl;
216 } else if (newfont == IGNORE)
223 /// Updates font settings according to request
224 void LyXFont::update(LyXFont const & newfont, bool toggleall)
226 if(newfont.family() == family() && toggleall)
227 setFamily(INHERIT_FAMILY); // toggle 'back'
228 else if (newfont.family() != IGNORE_FAMILY)
229 setFamily(newfont.family());
230 // else it's IGNORE_SHAPE
232 // "Old" behaviour: "Setting" bold will toggle bold on/off.
233 switch (newfont.series()) {
236 if (series() == BOLD_SERIES && toggleall)
237 setSeries(MEDIUM_SERIES);
239 setSeries(BOLD_SERIES);
243 setSeries(newfont.series());
249 if(newfont.shape() == shape() && toggleall)
250 setShape(INHERIT_SHAPE); // toggle 'back'
251 else if (newfont.shape() != IGNORE_SHAPE)
252 setShape(newfont.shape());
253 // else it's IGNORE_SHAPE
255 if (newfont.size() != IGNORE_SIZE) {
256 if (newfont.size() == INCREASE_SIZE)
258 else if (newfont.size() == DECREASE_SIZE)
260 else if (newfont.size() == size() && toggleall)
261 setSize(INHERIT_SIZE); // toggle 'back'
263 setSize(newfont.size());
266 setEmph(setMisc(newfont.emph(), emph()));
267 setUnderbar(setMisc(newfont.underbar(), underbar()));
268 setNoun(setMisc(newfont.noun(), noun()));
269 setLatex(setMisc(newfont.latex(), latex()));
271 switch(newfont.direction()) {
273 if (direction() == LTR_DIR)
274 setDirection(RTL_DIR);
276 setDirection(LTR_DIR);
281 setDirection(newfont.direction());
284 if(newfont.color() == color() && toggleall)
285 setColor(LColor::inherit); // toggle 'back'
286 else if (newfont.color() != LColor::ignore)
287 setColor(newfont.color());
291 /// Reduce font to fall back to template where possible
292 void LyXFont::reduce(LyXFont const & tmplt)
294 if (family() == tmplt.family())
295 setFamily(INHERIT_FAMILY);
296 if (series() == tmplt.series())
297 setSeries(INHERIT_SERIES);
298 if (shape() == tmplt.shape())
299 setShape(INHERIT_SHAPE);
300 if (size() == tmplt.size())
301 setSize(INHERIT_SIZE);
302 if (emph() == tmplt.emph())
304 if (underbar() == tmplt.underbar())
305 setUnderbar(INHERIT);
306 if (noun() == tmplt.noun())
308 if (latex() == tmplt.latex())
310 if (color() == tmplt.color())
311 setColor(LColor::inherit);
312 if (direction() == tmplt.direction())
313 setDirection(INHERIT_DIR);
317 /// Realize font from a template
318 LyXFont & LyXFont::realize(LyXFont const & tmplt)
320 if (bits == inherit) {
325 if (bits.family == INHERIT_FAMILY) {
326 bits.family = tmplt.bits.family;
328 if (bits.series == INHERIT_SERIES) {
329 bits.series = tmplt.bits.series;
331 if (bits.shape == INHERIT_SHAPE) {
332 bits.shape = tmplt.bits.shape;
334 if (bits.size == INHERIT_SIZE) {
335 bits.size = tmplt.bits.size;
337 if (bits.emph == INHERIT) {
338 bits.emph = tmplt.bits.emph;
340 if (bits.underbar == INHERIT) {
341 bits.underbar = tmplt.bits.underbar;
343 if (bits.noun == INHERIT) {
344 bits.noun = tmplt.bits.noun;
346 if (bits.latex == INHERIT) {
347 bits.latex = tmplt.bits.latex;
349 if (bits.color == LColor::inherit) {
350 bits.color = tmplt.bits.color;
352 if (bits.direction == INHERIT_DIR) {
353 bits.direction = tmplt.bits.direction;
360 /// Is font resolved?
361 bool LyXFont::resolved() const
363 return (family() != INHERIT_FAMILY && series() != INHERIT_SERIES &&
364 shape() != INHERIT_SHAPE && size() != INHERIT_SIZE &&
365 emph() != INHERIT && underbar() != INHERIT &&
366 noun() != INHERIT && latex() != INHERIT &&
367 color() != LColor::inherit &&
368 direction() != INHERIT_DIR);
372 /// Build GUI description of font state
373 string LyXFont::stateText() const
376 if (family() != INHERIT_FAMILY)
377 buf += string(_(GUIFamilyNames[family()].c_str())) + ", ";
378 if (series() != INHERIT_SERIES)
379 buf += string(_(GUISeriesNames[series()].c_str())) + ", ";
380 if (shape() != INHERIT_SHAPE)
381 buf += string(_(GUIShapeNames[shape()].c_str())) + ", ";
382 if (size() != INHERIT_SIZE)
383 buf += string(_(GUISizeNames[size()].c_str())) + ", ";
384 if (color() != LColor::inherit)
385 buf += lcolor.getGUIName(color()) + ", ";
386 if (emph() != INHERIT)
387 buf += string(_("Emphasis ")) + _(GUIMiscNames[emph()].c_str()) + ", ";
388 if (underbar() != INHERIT)
389 buf += string(_("Underline ")) + _(GUIMiscNames[underbar()].c_str()) + ", ";
390 if (noun() != INHERIT)
391 buf += string(_("Noun ")) + _(GUIMiscNames[noun()].c_str()) + ", ";
392 if (latex() != INHERIT)
393 buf += string(_("Latex ")) + _(GUIMiscNames[latex()].c_str()) + ", ";
394 if (direction() != INHERIT_DIR)
395 buf += string(_("Direction ")) + _(GUIDirectionNames[direction()].c_str()) + ", ";
398 buf = strip(buf, ' ');
399 buf = strip(buf, ',');
404 // Set family according to lyx format string
405 LyXFont & LyXFont::setLyXFamily(string const & fam)
407 string s = lowercase(fam);
410 while (s != LyXFamilyNames[i] && LyXFamilyNames[i] != "error") ++i;
411 if (s == LyXFamilyNames[i]) {
412 setFamily(LyXFont::FONT_FAMILY(i));
414 lyxerr << "LyXFont::setLyXFamily: Unknown family `"
415 << s << '\'' << endl;
420 // Set series according to lyx format string
421 LyXFont & LyXFont::setLyXSeries(string const & ser)
423 string s = lowercase(ser);
426 while (s != LyXSeriesNames[i] && LyXSeriesNames[i] != "error") ++i;
427 if (s == LyXSeriesNames[i]) {
428 setSeries(LyXFont::FONT_SERIES(i));
430 lyxerr << "LyXFont::setLyXSeries: Unknown series `"
431 << s << '\'' << endl;
436 // Set shape according to lyx format string
437 LyXFont & LyXFont::setLyXShape(string const & sha)
439 string s = lowercase(sha);
442 while (s != LyXShapeNames[i] && LyXShapeNames[i] != "error") ++i;
443 if (s == LyXShapeNames[i]) {
444 setShape(LyXFont::FONT_SHAPE(i));
446 lyxerr << "LyXFont::setLyXShape: Unknown shape `"
447 << s << '\'' << endl;
452 // Set size according to lyx format string
453 LyXFont & LyXFont::setLyXSize(string const & siz)
455 string s = lowercase(siz);
457 while (s != LyXSizeNames[i] && LyXSizeNames[i] != "error") ++i;
458 if (s == LyXSizeNames[i]) {
459 setSize(LyXFont::FONT_SIZE(i));
461 lyxerr << "LyXFont::setLyXSize: Unknown size `"
462 << s << '\'' << endl;
466 // Set size according to lyx format string
467 LyXFont::FONT_MISC_STATE LyXFont::setLyXMisc(string const & siz)
469 string s = lowercase(siz);
471 while (s != LyXMiscNames[i] && LyXMiscNames[i] != "error") ++i;
472 if (s == LyXMiscNames[i])
473 return FONT_MISC_STATE(i);
474 lyxerr << "LyXFont::setLyXMisc: Unknown misc flag `"
475 << s << '\'' << endl;
479 /// Sets color after LyX text format
480 LyXFont & LyXFont::setLyXColor(string const & col)
482 setColor(lcolor.getFromLyXName(col));
487 /// Sets size after GUI name
488 LyXFont & LyXFont::setGUISize(string const & siz)
490 #warning this might be wrong.
492 // this is how it was how it was done in the lyx repository...
493 // but this does not make sense.
494 setColor(lcolor.getFromGUIName(siz));
499 // Returns size in latex format
500 string LyXFont::latexSize() const
502 return LaTeXSizeNames[size()];
506 // Read a font definition from given file in lyx format
508 LyXFont & LyXFont::lyxRead(LyXLex & lex)
511 bool finished = false;
512 while (!finished && lex.IsOK() && !error) {
514 string tok = lowercase(lex.GetString());
518 } else if (tok == "endfont") {
520 } else if (tok == "family") {
522 string tok = lex.GetString();
524 } else if (tok == "series") {
526 string tok = lex.GetString();
528 } else if (tok == "shape") {
530 string tok = lex.GetString();
532 } else if (tok == "size") {
534 string tok = lex.GetString();
536 } else if (tok == "latex") {
538 string tok = lowercase(lex.GetString());
540 if (tok == "no_latex") {
542 } else if (tok == "latex") {
545 lex.printError("Illegal LaTeX type`$$Token'");
547 } else if (tok == "misc") {
549 string tok = lowercase(lex.GetString());
551 if (tok == "no_bar") {
553 } else if (tok == "no_emph") {
555 } else if (tok == "no_noun") {
557 } else if (tok == "emph") {
559 } else if (tok == "underbar") {
561 } else if (tok == "noun") {
564 lex.printError("Illegal misc type `$$Token´");
566 } else if (tok == "color") {
568 string tok = lex.GetString();
570 } else if (tok == "direction") {
572 string tok = lowercase(lex.GetString());
575 setDirection(LTR_DIR);
576 } else if (tok == "rtl") {
577 setDirection(RTL_DIR);
579 lex.printError("Illegal type`$$Token'");
582 lex.printError("Unknown tag `$$Token'");
590 /// Writes the changes from this font to orgfont in .lyx format in file
591 void LyXFont::lyxWriteChanges(LyXFont const & orgfont, ostream & os) const
594 if (orgfont.family() != family()) {
595 os << "\\family " << LyXFamilyNames[family()] << " \n";
597 if (orgfont.series() != series()) {
598 os << "\\series " << LyXSeriesNames[series()] << " \n";
600 if (orgfont.shape() != shape()) {
601 os << "\\shape " << LyXShapeNames[shape()] << " \n";
603 if (orgfont.size() != size()) {
604 os << "\\size " << LyXSizeNames[size()] << " \n";
606 if (orgfont.emph() != emph()) {
607 os << "\\emph " << LyXMiscNames[emph()] << " \n";
609 if (orgfont.underbar() != underbar()) {
610 // This is only for backwards compatibility
611 switch (underbar()) {
612 case OFF: os << "\\bar no \n"; break;
613 case ON: os << "\\bar under \n"; break;
614 case TOGGLE: lyxerr << "LyXFont::lyxWriteFontChanges: "
615 "TOGGLE should not appear here!"
618 case INHERIT: os << "\\bar default \n"; break;
619 case IGNORE: lyxerr << "LyXFont::lyxWriteFontChanges: "
620 "IGNORE should not appear here!"
625 if (orgfont.noun() != noun()) {
626 os << "\\noun " << LyXMiscNames[noun()] << " \n";
628 if (orgfont.latex() != latex()) {
629 // This is only for backwards compatibility
631 case OFF: os << "\\latex no_latex \n"; break;
632 case ON: os << "\\latex latex \n"; break;
633 case TOGGLE: lyxerr << "LyXFont::lyxWriteFontChanges: "
634 "TOGGLE should not appear here!"
637 case INHERIT: os << "\\latex default \n"; break;
638 case IGNORE: lyxerr << "LyXFont::lyxWriteFontChanges: "
639 "IGNORE should not appear here!"
644 if (orgfont.color() != color()) {
645 os << "\\color " << lcolor.getLyXName(color()) << "\n";
647 if (orgfont.direction() != direction()) {
648 switch (direction()) {
649 case RTL_DIR: os << "\\direction rtl \n"; break;
650 case LTR_DIR: os << "\\direction ltr\n"; break;
651 case TOGGLE_DIR: lyxerr << "LyXFont::lyxWriteFontChanges: "
652 "TOGGLE should not appear here!"
654 case INHERIT_DIR: os << "\\direction default \n"; break;
655 case IGNORE_DIR: lyxerr << "LyXFont::lyxWriteFontChanges: "
656 "IGNORE should not appear here!"
664 /// Writes the head of the LaTeX needed to impose this font
665 // Returns number of chars written.
666 #ifdef USE_OSTREAM_ONLY
667 int LyXFont::latexWriteStartChanges(ostream & os, LyXFont const & base,
668 LyXFont const & prev) const
673 if (f.bits == inherit)
679 FONT_DIRECTION direction = f.direction();
680 if (direction != prev.direction()) {
681 if (direction == LTR_DIR) {
684 env = true; //We have opened a new environment
686 if (direction == RTL_DIR) {
689 env = true; //We have opened a new environment
693 if (f.family() != INHERIT_FAMILY) {
695 << LaTeXFamilyNames[f.family()]
697 << LaTeXFamilyNames[f.family()].length() + 2;
698 env = true; //We have opened a new environment
700 if (f.series() != INHERIT_SERIES) {
702 << LaTeXSeriesNames[f.series()]
704 count += LaTeXSeriesNames[f.series()].length() + 2;
705 env = true; //We have opened a new environment
707 if (f.shape() != INHERIT_SHAPE) {
709 << LaTeXShapeNames[f.shape()]
711 count += LaTeXShapeNames[f.shape()].length() + 2;
712 env = true; //We have opened a new environment
714 if (f.color() != LColor::inherit) {
716 << lcolor.getLaTeXName(f.color())
718 count += lcolor.getLaTeXName(f.color()).length() + 13;
719 env = true; //We have opened a new environment
721 if (f.emph() == ON) {
724 env = true; //We have opened a new environment
726 if (f.underbar() == ON) {
729 env = true; //We have opened a new environment
731 // \noun{} is a LyX special macro
732 if (f.noun() == ON) {
735 env = true; //We have opened a new environment
737 if (f.size() != INHERIT_SIZE) {
738 // If we didn't open an environment above, we open one here
744 << LaTeXSizeNames[f.size()]
746 count += LaTeXSizeNames[f.size()].length() + 2;
751 int LyXFont::latexWriteStartChanges(string & file, LyXFont const & base,
752 LyXFont const & prev) const
757 if (f.bits == inherit)
763 FONT_DIRECTION direction = f.direction();
764 if (direction != prev.direction()) {
765 if (direction == LTR_DIR) {
768 env = true; //We have opened a new environment
770 if (direction == RTL_DIR) {
773 env = true; //We have opened a new environment
777 if (f.family() != INHERIT_FAMILY) {
779 file += LaTeXFamilyNames[f.family()];
781 count += LaTeXFamilyNames[f.family()].length() + 2;
782 env = true; //We have opened a new environment
784 if (f.series() != INHERIT_SERIES) {
786 file += LaTeXSeriesNames[f.series()];
788 count += LaTeXSeriesNames[f.series()].length() + 2;
789 env = true; //We have opened a new environment
791 if (f.shape() != INHERIT_SHAPE) {
793 file += LaTeXShapeNames[f.shape()];
795 count += LaTeXShapeNames[f.shape()].length() + 2;
796 env = true; //We have opened a new environment
798 if (f.color() != LColor::inherit) {
799 file += "\\textcolor{";
800 file += lcolor.getLaTeXName(f.color());
802 count += lcolor.getLaTeXName(f.color()).length() + 13;
803 env = true; //We have opened a new environment
805 if (f.emph() == ON) {
808 env = true; //We have opened a new environment
810 if (f.underbar() == ON) {
811 file += "\\underbar{";
813 env = true; //We have opened a new environment
815 // \noun{} is a LyX special macro
816 if (f.noun() == ON) {
819 env = true; //We have opened a new environment
821 if (f.size() != INHERIT_SIZE) {
822 // If we didn't open an environment above, we open one here
828 file += LaTeXSizeNames[f.size()];
830 count += LaTeXSizeNames[f.size()].length() + 2;
837 #ifndef USE_OSTREAM_ONLY
838 /// Writes ending block of LaTeX needed to close use of this font
839 // Returns number of chars written
840 // This one corresponds to latexWriteStartChanges(). (Asger)
841 int LyXFont::latexWriteEndChanges(string & file, LyXFont const & base,
842 LyXFont const & next) const
844 LyXFont f = *this; // why do you need this?
845 f.reduce(base); // why isn't this just "reduce(base);" (Lgb)
846 // Because this function is const. Everything breaks if this
847 // method changes the font it represents. There is no speed penalty
848 // by using the temporary. (Asger)
850 if (f.bits == inherit)
856 FONT_DIRECTION direction = f.direction();
857 if ( direction != next.direction()
858 && (direction == RTL_DIR || direction == LTR_DIR) ) {
861 env = true; // Size change need not bother about closing env.
864 if (f.family() != INHERIT_FAMILY) {
867 env = true; // Size change need not bother about closing env.
869 if (f.series() != INHERIT_SERIES) {
872 env = true; // Size change need not bother about closing env.
874 if (f.shape() != INHERIT_SHAPE) {
877 env = true; // Size change need not bother about closing env.
879 if (f.color() != LColor::inherit) {
882 env = true; // Size change need not bother about closing env.
884 if (f.emph() == ON) {
887 env = true; // Size change need not bother about closing env.
889 if (f.underbar() == ON) {
892 env = true; // Size change need not bother about closing env.
894 if (f.noun() == ON) {
897 env = true; // Size change need not bother about closing env.
899 if (f.size() != INHERIT_SIZE) {
900 // We only have to close if only size changed
909 /// Writes ending block of LaTeX needed to close use of this font
910 // Returns number of chars written
911 // This one corresponds to latexWriteStartChanges(). (Asger)
912 int LyXFont::latexWriteEndChanges(ostream & os, LyXFont const & base,
913 LyXFont const & next) const
915 LyXFont f = *this; // why do you need this?
916 f.reduce(base); // why isn't this just "reduce(base);" (Lgb)
917 // Because this function is const. Everything breaks if this
918 // method changes the font it represents. There is no speed penalty
919 // by using the temporary. (Asger)
921 if (f.bits == inherit)
927 FONT_DIRECTION direction = f.direction();
928 if ( direction != next.direction()
929 && (direction == RTL_DIR || direction == LTR_DIR) ) {
932 env = true; // Size change need not bother about closing env.
935 if (f.family() != INHERIT_FAMILY) {
938 env = true; // Size change need not bother about closing env.
940 if (f.series() != INHERIT_SERIES) {
943 env = true; // Size change need not bother about closing env.
945 if (f.shape() != INHERIT_SHAPE) {
948 env = true; // Size change need not bother about closing env.
950 if (f.color() != LColor::inherit) {
953 env = true; // Size change need not bother about closing env.
955 if (f.emph() == ON) {
958 env = true; // Size change need not bother about closing env.
960 if (f.underbar() == ON) {
963 env = true; // Size change need not bother about closing env.
965 if (f.noun() == ON) {
968 env = true; // Size change need not bother about closing env.
970 if (f.size() != INHERIT_SIZE) {
971 // We only have to close if only size changed
982 LColor::color LyXFont::realColor() const
985 return LColor::latex;
986 if (color() == LColor::none)
987 return LColor::foreground;
992 // Convert logical attributes to concrete shape attribute
993 LyXFont::FONT_SHAPE LyXFont::realShape() const
995 register FONT_SHAPE s = shape();
1004 s = SMALLCAPS_SHAPE;
1009 XFontStruct * LyXFont::getXFontstruct() const
1011 return fontloader.load(family(), series(), realShape(), size());
1015 int LyXFont::maxAscent() const
1017 return getXFontstruct()->ascent;
1021 int LyXFont::maxDescent() const
1023 return getXFontstruct()->descent;
1027 int LyXFont::ascent(char c) const
1029 XFontStruct * finfo = getXFontstruct();
1030 unsigned int uc = static_cast<unsigned char>(c);
1032 && uc >= finfo->min_char_or_byte2
1033 && uc <= finfo->max_char_or_byte2)
1034 return finfo->per_char[uc - finfo->min_char_or_byte2].ascent;
1036 return finfo->ascent;
1040 int LyXFont::descent(char c) const
1042 XFontStruct * finfo = getXFontstruct();
1043 unsigned int uc = static_cast<unsigned char>(c);
1045 && uc >= finfo->min_char_or_byte2
1046 && uc <= finfo->max_char_or_byte2)
1047 return finfo->per_char[uc - finfo->min_char_or_byte2].descent;
1049 return finfo->descent;
1053 // Specialized after profiling. (Asger)
1054 int LyXFont::width(char c) const
1056 if (realShape() != LyXFont::SMALLCAPS_SHAPE){
1057 return XTextWidth(getXFontstruct(), &c, 1);
1059 return textWidth(&c, 1);
1064 int LyXFont::lbearing(char c) const
1066 XFontStruct * finfo = getXFontstruct();
1067 unsigned int uc = static_cast<unsigned char>(c);
1069 && uc >= finfo->min_char_or_byte2
1070 && uc <= finfo->max_char_or_byte2)
1071 return finfo->per_char[uc - finfo->min_char_or_byte2].lbearing;
1077 int LyXFont::rbearing(char c) const
1079 XFontStruct * finfo = getXFontstruct();
1080 unsigned int uc = static_cast<unsigned char>(c);
1082 && uc >= finfo->min_char_or_byte2
1083 && uc <= finfo->max_char_or_byte2)
1084 return finfo->per_char[uc - finfo->min_char_or_byte2].rbearing;
1090 int LyXFont::textWidth(char const * s, int n) const
1092 if (realShape() != LyXFont::SMALLCAPS_SHAPE){
1093 return XTextWidth(getXFontstruct(), s, n);
1095 // emulate smallcaps since X doesn't support this
1096 unsigned int result = 0;
1098 LyXFont smallfont = *this;
1099 smallfont.decSize();
1100 smallfont.decSize();
1101 smallfont.setShape(LyXFont::UP_SHAPE);
1102 for (int i = 0; i < n; ++i) {
1104 // when islower is a macro, the cast is needed (JMarc)
1105 if (islower(static_cast<unsigned char>(c))){
1107 result += XTextWidth(smallfont.getXFontstruct(), &c, 1);
1109 result += XTextWidth(getXFontstruct(), &c, 1);
1117 int LyXFont::stringWidth(string const & s) const
1119 if (s.empty()) return 0;
1120 return textWidth(s.c_str(), s.length());
1124 int LyXFont::signedStringWidth(string const & s) const
1126 if (s.empty()) return 0;
1127 if (s.c_str()[0] == '-')
1128 return -textWidth(s.c_str()+1, s.length()-1);
1130 return textWidth(s.c_str(), s.length());
1134 int LyXFont::drawText(char const * s, int n, Pixmap,
1137 if (realShape() != LyXFont::SMALLCAPS_SHAPE) {
1138 /* XDrawString(fl_display,
1143 XFlush(fl_display); */
1144 return XTextWidth(getXFontstruct(), s, n);
1147 // emulate smallcaps since X doesn't support this
1150 LyXFont smallfont = *this;
1151 smallfont.decSize();
1152 smallfont.decSize();
1153 smallfont.setShape(LyXFont::UP_SHAPE);
1154 for (int i = 0; i < n; ++i) {
1156 if (islower(static_cast<unsigned char>(c))){
1158 /* XDrawString(fl_display,
1163 x += XTextWidth(smallfont.getXFontstruct(),
1165 //XFlush(fl_display);
1167 /* XDrawString(fl_display,
1172 x += XTextWidth(getXFontstruct(), &c, 1);
1173 //XFlush(fl_display);
1181 int LyXFont::drawString(string const & s, Pixmap pm, int baseline, int x) const
1183 return drawText(s.c_str(), s.length(), pm, baseline, x);
1187 bool LyXFont::equalExceptLatex(LyXFont const & f) const
1190 f1.setLatex(f.latex());
1195 LyXDirection LyXFont::getFontDirection() const
1197 if (lyxrc->rtl_support
1198 && direction() == LyXFont::RTL_DIR
1199 && latex() != LyXFont::ON)
1200 return LYX_DIR_RIGHT_TO_LEFT;
1202 return LYX_DIR_LEFT_TO_RIGHT;
1206 ostream & operator<<(ostream & o, LyXFont::FONT_MISC_STATE fms)
1208 return o << int(fms);