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"
26 #include "FontLoader.h"
27 #include "support/lstrings.h"
31 // The global fontloader
32 FontLoader fontloader;
38 string const GUIFamilyNames[6] =
39 { N_("Roman"), N_("Sans serif"), N_("Typewriter"), N_("Symbol"), N_("Inherit"),
42 string const GUISeriesNames[4] =
43 { N_("Medium"), N_("Bold"), N_("Inherit"), N_("Ignore") };
45 string const GUIShapeNames[6] =
46 { N_("Upright"), N_("Italic"), N_("Slanted"), N_("Smallcaps"), N_("Inherit"),
49 string const GUISizeNames[14] =
50 { N_("Tiny"), N_("Smallest"), N_("Smaller"), N_("Small"), N_("Normal"), N_("Large"),
51 N_("Larger"), N_("Largest"), N_("Huge"), N_("Huger"), N_("Increase"), N_("Decrease"),
52 N_("Inherit"), N_("Ignore") };
54 string const lGUISizeNames[15] =
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"), string() };
59 string const GUIMiscNames[5] =
60 { N_("Off"), N_("On"), N_("Toggle"), N_("Inherit"), N_("Ignore") };
62 string const GUIDirectionNames[5] =
63 { N_("LTR"), N_("RTL"), N_("Toggle"), N_("Inherit"), N_("Ignore") };
65 string const GUIColorNames[13] =
66 { N_("None"), N_("Black"), N_("White"), N_("Red"), N_("Green"), N_("Blue"),
67 N_("Cyan"), N_("Magenta"),
68 N_("Yellow"), N_("Math"), N_("Inset"), N_("Inherit"), N_("Ignore") };
71 // Strings used to read and write .lyx format files
73 string const LyXFamilyNames[6] =
74 { "roman", "sans", "typewriter", "symbol", "default", "error" };
76 string const LyXSeriesNames[4] =
77 { "medium", "bold", "default", "error" };
79 string const LyXShapeNames[6] =
80 { "up", "italic", "slanted", "smallcaps", "default", "error" };
82 string const LyXSizeNames[14] =
83 { "tiny", "scriptsize", "footnotesize", "small", "normal", "large",
84 "larger", "largest", "huge", "giant",
85 "increase-error", "decrease-error", "default", "error" };
87 string const LyXMiscNames[12] =
88 { "off", "on", "toggle", "default", "error" };
90 string const LyXColorNames[13] =
91 { "none", "black", "white", "red", "green", "blue", "cyan", "magenta",
92 "yellow", "matherror", "inseterror", "default", "error" };
95 // Strings used to write LaTeX files
98 string const LaTeXFamilyNames[6] =
99 { "textrm", "textsf", "texttt", "error1", "error2", "error3" };
101 string const LaTeXSeriesNames[4] =
102 { "textmd", "textbf", "error4", "error5" };
104 string const LaTeXShapeNames[6] =
105 { "textup", "textit", "textsl", "textsc", "error6", "error7" };
107 string const LaTeXSizeNames[14] =
108 { "tiny", "scriptsize", "footnotesize", "small", "normalsize", "large",
109 "Large", "LARGE", "huge", "Huge", "error8", "error9", "error10", "error11" };
111 string const LaTeXColorNames[13] =
112 { "none", "black", "white", "red", "green", "blue", "cyan", "magenta",
113 "yellow", "error12", "error13", "error14", "error15" };
115 /// Decreases font size by one
116 LyXFont & LyXFont::decSize()
119 case SIZE_HUGER: setSize(SIZE_HUGE); break;
120 case SIZE_HUGE: setSize(SIZE_LARGEST); break;
121 case SIZE_LARGEST: setSize(SIZE_LARGER); break;
122 case SIZE_LARGER: setSize(SIZE_LARGE); break;
123 case SIZE_LARGE: setSize(SIZE_NORMAL); break;
124 case SIZE_NORMAL: setSize(SIZE_SMALL); break;
125 case SIZE_SMALL: setSize(SIZE_FOOTNOTE); break;
126 case SIZE_FOOTNOTE: setSize(SIZE_SCRIPT); break;
127 case SIZE_SCRIPT: setSize(SIZE_TINY); break;
128 case SIZE_TINY: break;
130 lyxerr << "Can't LyXFont::decSize on INCREASE_SIZE" << endl;
133 lyxerr <<"Can't LyXFont::decSize on DECREASE_SIZE" << endl;
136 lyxerr <<"Can't LyXFont::decSize on INHERIT_SIZE" << endl;
139 lyxerr <<"Can't LyXFont::decSize on IGNORE_SIZE" << endl;
146 /// Increases font size by one
147 LyXFont & LyXFont::incSize()
150 case SIZE_HUGER: break;
151 case SIZE_HUGE: setSize(SIZE_HUGER); break;
152 case SIZE_LARGEST: setSize(SIZE_HUGE); break;
153 case SIZE_LARGER: setSize(SIZE_LARGEST); break;
154 case SIZE_LARGE: setSize(SIZE_LARGER); break;
155 case SIZE_NORMAL: setSize(SIZE_LARGE); break;
156 case SIZE_SMALL: setSize(SIZE_NORMAL); break;
157 case SIZE_FOOTNOTE: setSize(SIZE_SMALL); break;
158 case SIZE_SCRIPT: setSize(SIZE_FOOTNOTE); break;
159 case SIZE_TINY: setSize(SIZE_SCRIPT); break;
161 lyxerr <<"Can't LyXFont::incSize on INCREASE_SIZE" << endl;
164 lyxerr <<"Can't LyXFont::incSize on DECREASE_SIZE" << endl;
167 lyxerr <<"Can't LyXFont::incSize on INHERIT_SIZE" << endl;
170 lyxerr <<"Can't LyXFont::incSize on IGNORE_SIZE" << endl;
177 /// Updates a misc setting according to request
178 LyXFont::FONT_MISC_STATE LyXFont::setMisc(FONT_MISC_STATE newfont,
181 if (newfont == TOGGLE) {
187 lyxerr <<"LyXFont::setMisc: Need state"
188 " ON or OFF to toggle. Setting to ON" << endl;
191 } else if (newfont == IGNORE)
198 /// Updates font settings according to request
199 void LyXFont::update(LyXFont const & newfont, bool toggleall)
201 if(newfont.family() == family() && toggleall)
202 setFamily(INHERIT_FAMILY); // toggle 'back'
203 else if (newfont.family() != IGNORE_FAMILY)
204 setFamily(newfont.family());
205 // else it's IGNORE_SHAPE
207 // "Old" behaviour: "Setting" bold will toggle bold on/off.
208 switch (newfont.series()) {
211 if (series() == BOLD_SERIES && toggleall)
212 setSeries(MEDIUM_SERIES);
214 setSeries(BOLD_SERIES);
218 setSeries(newfont.series());
224 if(newfont.shape() == shape() && toggleall)
225 setShape(INHERIT_SHAPE); // toggle 'back'
226 else if (newfont.shape() != IGNORE_SHAPE)
227 setShape(newfont.shape());
228 // else it's IGNORE_SHAPE
230 if (newfont.size() != IGNORE_SIZE) {
231 if (newfont.size() == INCREASE_SIZE)
233 else if (newfont.size() == DECREASE_SIZE)
235 else if (newfont.size() == size() && toggleall)
236 setSize(INHERIT_SIZE); // toggle 'back'
238 setSize(newfont.size());
241 setEmph(setMisc(newfont.emph(), emph()));
242 setUnderbar(setMisc(newfont.underbar(), underbar()));
243 setNoun(setMisc(newfont.noun(), noun()));
244 setLatex(setMisc(newfont.latex(), latex()));
246 switch(newfont.direction()) {
248 if (direction() == LTR_DIR)
249 setDirection(RTL_DIR);
251 setDirection(LTR_DIR);
256 setDirection(newfont.direction());
259 if(newfont.color() == color() && toggleall)
260 setColor(INHERIT_COLOR); // toggle 'back'
261 else if (newfont.color() != IGNORE_COLOR)
262 setColor(newfont.color());
266 /// Reduce font to fall back to template where possible
267 void LyXFont::reduce(LyXFont const & tmplt)
269 if (family() == tmplt.family())
270 setFamily(INHERIT_FAMILY);
271 if (series() == tmplt.series())
272 setSeries(INHERIT_SERIES);
273 if (shape() == tmplt.shape())
274 setShape(INHERIT_SHAPE);
275 if (size() == tmplt.size())
276 setSize(INHERIT_SIZE);
277 if (emph() == tmplt.emph())
279 if (underbar() == tmplt.underbar())
280 setUnderbar(INHERIT);
281 if (noun() == tmplt.noun())
283 if (latex() == tmplt.latex())
285 if (color() == tmplt.color())
286 setColor(INHERIT_COLOR);
287 if (direction() == tmplt.direction())
288 setDirection(INHERIT_DIR);
292 /// Realize font from a template
293 // This one is not pretty, but it's extremely fast (Asger)
294 LyXFont & LyXFont::realize(LyXFont const & tmplt)
296 if (bits == inherit) {
301 if ((bits & (Fam_Mask<<Fam_Pos)) == (ui32(INHERIT_FAMILY)<<Fam_Pos))
303 bits &= ~(Fam_Mask << Fam_Pos);
304 bits |= (tmplt.bits & Fam_Mask << Fam_Pos);
306 if ((bits & (Ser_Mask<<Ser_Pos)) == (ui32(INHERIT_SERIES)<<Ser_Pos))
308 bits &= ~(Ser_Mask << Ser_Pos);
309 bits |= (tmplt.bits & Ser_Mask << Ser_Pos);
311 if ((bits & (Sha_Mask << Sha_Pos)) == ui32(INHERIT_SHAPE) << Sha_Pos)
313 bits &= ~(Sha_Mask << Sha_Pos);
314 bits |= (tmplt.bits & Sha_Mask << Sha_Pos);
316 if ((bits & (Siz_Mask << Siz_Pos)) == ui32(INHERIT_SIZE) << Siz_Pos)
318 bits &= ~(Siz_Mask << Siz_Pos);
319 bits |= (tmplt.bits & Siz_Mask << Siz_Pos);
321 if ((bits & (Misc_Mask << Emp_Pos)) == ui32(INHERIT) << Emp_Pos)
323 bits &= ~(Misc_Mask << Emp_Pos);
324 bits |= (tmplt.bits & Misc_Mask << Emp_Pos);
326 if ((bits & (Misc_Mask << Und_Pos)) == ui32(INHERIT) << Und_Pos)
328 bits &= ~(Misc_Mask << Und_Pos);
329 bits |= (tmplt.bits & Misc_Mask << Und_Pos);
331 if ((bits & (Misc_Mask << Nou_Pos)) == ui32(INHERIT) << Nou_Pos)
333 bits &= ~(Misc_Mask << Nou_Pos);
334 bits |= (tmplt.bits & Misc_Mask << Nou_Pos);
336 if ((bits & (Misc_Mask << Lat_Pos)) == ui32(INHERIT) << Lat_Pos)
338 bits &= ~(Misc_Mask << Lat_Pos);
339 bits |= (tmplt.bits & Misc_Mask << Lat_Pos);
341 if ((bits & (Col_Mask << Col_Pos)) == ui32(INHERIT_COLOR) << Col_Pos)
343 bits &= ~(Col_Mask << Col_Pos);
344 bits |= (tmplt.bits & Col_Mask << Col_Pos);
346 if ((bits & (Dir_Mask << Dir_Pos)) == ui32(INHERIT_DIR) << Dir_Pos)
348 bits &= ~(Dir_Mask << Dir_Pos);
349 bits |= (tmplt.bits & Dir_Mask << Dir_Pos);
356 /// Is font resolved?
357 bool LyXFont::resolved() const
359 return (family() != INHERIT_FAMILY && series() != INHERIT_SERIES &&
360 shape() != INHERIT_SHAPE && size() != INHERIT_SIZE &&
361 emph() != INHERIT && underbar() != INHERIT &&
362 noun() != INHERIT && latex() != INHERIT &&
363 color() != INHERIT_COLOR &&
364 direction() != INHERIT_DIR);
368 /// Build GUI description of font state
369 string LyXFont::stateText() const
372 if (family() != INHERIT_FAMILY)
373 buf += string(_(GUIFamilyNames[family()].c_str())) + ", ";
374 if (series() != INHERIT_SERIES)
375 buf += string(_(GUISeriesNames[series()].c_str())) + ", ";
376 if (shape() != INHERIT_SHAPE)
377 buf += string(_(GUIShapeNames[shape()].c_str())) + ", ";
378 if (size() != INHERIT_SIZE)
379 buf += string(_(GUISizeNames[size()].c_str())) + ", ";
380 if (color() != INHERIT_COLOR)
381 buf += string(_(GUIColorNames[color()].c_str())) + ", ";
383 if (emph() != INHERIT)
384 buf += string(_("Emphasis ")) + _(GUIMiscNames[emph()].c_str()) + ", ";
385 if (underbar() != INHERIT)
386 buf += string(_("Underline ")) + _(GUIMiscNames[underbar()].c_str()) + ", ";
387 if (noun() != INHERIT)
388 buf += string(_("Noun ")) + _(GUIMiscNames[noun()].c_str()) + ", ";
389 if (latex() != INHERIT)
390 buf += string(_("Latex ")) + _(GUIMiscNames[latex()].c_str()) + ", ";
391 if (direction() != INHERIT_DIR)
392 buf += string(_("Direction ")) + _(GUIDirectionNames[direction()].c_str()) + ", ";
395 buf = strip(buf, ' ');
396 buf = strip(buf, ',');
401 // Set family according to lyx format string
402 LyXFont & LyXFont::setLyXFamily(string const & fam)
404 string s = lowercase(fam);
407 while (s != LyXFamilyNames[i] && LyXFamilyNames[i] != "error") ++i;
408 if (s == LyXFamilyNames[i]) {
409 setFamily(LyXFont::FONT_FAMILY(i));
411 lyxerr << "LyXFont::setLyXFamily: Unknown family `"
412 << s << '\'' << endl;
417 // Set series according to lyx format string
418 LyXFont & LyXFont::setLyXSeries(string const & ser)
420 string s = lowercase(ser);
423 while (s != LyXSeriesNames[i] && LyXSeriesNames[i] != "error") ++i;
424 if (s == LyXSeriesNames[i]) {
425 setSeries(LyXFont::FONT_SERIES(i));
427 lyxerr << "LyXFont::setLyXSeries: Unknown series `"
428 << s << '\'' << endl;
433 // Set shape according to lyx format string
434 LyXFont & LyXFont::setLyXShape(string const & sha)
436 string s = lowercase(sha);
439 while (s != LyXShapeNames[i] && LyXShapeNames[i] != "error") ++i;
440 if (s == LyXShapeNames[i]) {
441 setShape(LyXFont::FONT_SHAPE(i));
443 lyxerr << "LyXFont::setLyXShape: Unknown shape `"
444 << s << '\'' << endl;
449 // Set size according to lyx format string
450 LyXFont & LyXFont::setLyXSize(string const & siz)
452 string s = lowercase(siz);
454 while (s != LyXSizeNames[i] && LyXSizeNames[i] != "error") ++i;
455 if (s == LyXSizeNames[i]) {
456 setSize(LyXFont::FONT_SIZE(i));
458 lyxerr << "LyXFont::setLyXSize: Unknown size `"
459 << s << '\'' << endl;
463 // Set size according to lyx format string
464 LyXFont::FONT_MISC_STATE LyXFont::setLyXMisc(string const & siz)
466 string s = lowercase(siz);
468 while (s != LyXMiscNames[i] && LyXMiscNames[i] != "error") ++i;
469 if (s == LyXMiscNames[i])
470 return FONT_MISC_STATE(i);
471 lyxerr << "LyXFont::setLyXMisc: Unknown misc flag `"
472 << s << '\'' << endl;
476 /// Sets color after LyX text format
477 LyXFont & LyXFont::setLyXColor(string const & col)
479 string s = lowercase(col);
481 while (s != LyXColorNames[i] && LyXColorNames[i] != "error") ++i;
482 if (s == LyXColorNames[i]) {
483 setColor(LyXFont::FONT_COLOR(i));
485 lyxerr << "LyXFont::setLyXColor: Unknown Color `"
486 << s << '\'' << endl;
491 /// Sets size after GUI name
492 LyXFont & LyXFont::setGUISize(string const & siz)
494 string s = lowercase(siz);
496 while (!lGUISizeNames[i].empty() &&
497 s != _(lGUISizeNames[i].c_str()))
499 if (s == _(lGUISizeNames[i].c_str())) {
500 setSize(LyXFont::FONT_SIZE(i));
502 lyxerr << "LyXFont::setGUISize: Unknown Size `"
503 << s << '\'' << endl;
508 // Returns size in latex format
509 string LyXFont::latexSize() const
511 return LaTeXSizeNames[size()];
515 // Read a font definition from given file in lyx format
517 LyXFont & LyXFont::lyxRead(LyXLex & lex)
520 bool finished = false;
521 while (!finished && lex.IsOK() && !error) {
523 string tok = lowercase(lex.GetString());
527 } else if (tok == "endfont") {
529 } else if (tok == "family") {
531 string tok = lex.GetString();
533 } else if (tok == "series") {
535 string tok = lex.GetString();
537 } else if (tok == "shape") {
539 string tok = lex.GetString();
541 } else if (tok == "size") {
543 string tok = lex.GetString();
545 } else if (tok == "latex") {
547 string tok = lowercase(lex.GetString());
549 if (tok == "no_latex") {
551 } else if (tok == "latex") {
554 lex.printError("Illegal LaTeX type`$$Token'");
556 } else if (tok == "misc") {
558 string tok = lowercase(lex.GetString());
560 if (tok == "no_bar") {
562 } else if (tok == "no_emph") {
564 } else if (tok == "no_noun") {
566 } else if (tok == "emph") {
568 } else if (tok == "underbar") {
570 } else if (tok == "noun") {
573 lex.printError("Illegal misc type `$$Token´");
575 } else if (tok == "color") {
577 string tok = lex.GetString();
579 } else if (tok == "direction") {
581 string tok = lowercase(lex.GetString());
584 setDirection(LTR_DIR);
585 } else if (tok == "rtl") {
586 setDirection(RTL_DIR);
588 lex.printError("Illegal type`$$Token'");
591 lex.printError("Unknown tag `$$Token'");
599 /// Writes the changes from this font to orgfont in .lyx format in file
600 void LyXFont::lyxWriteChanges(LyXFont const & orgfont, ostream & os) const
603 if (orgfont.family() != family()) {
604 os << "\\family " << LyXFamilyNames[family()] << " \n";
606 if (orgfont.series() != series()) {
607 os << "\\series " << LyXSeriesNames[series()] << " \n";
609 if (orgfont.shape() != shape()) {
610 os << "\\shape " << LyXShapeNames[shape()] << " \n";
612 if (orgfont.size() != size()) {
613 os << "\\size " << LyXSizeNames[size()] << " \n";
615 if (orgfont.emph() != emph()) {
616 os << "\\emph " << LyXMiscNames[emph()] << " \n";
618 if (orgfont.underbar() != underbar()) {
619 // This is only for backwards compatibility
620 switch (underbar()) {
621 case OFF: os << "\\bar no \n"; break;
622 case ON: os << "\\bar under \n"; break;
623 case TOGGLE: lyxerr << "LyXFont::lyxWriteFontChanges: "
624 "TOGGLE should not appear here!"
627 case INHERIT: os << "\\bar default \n"; break;
628 case IGNORE: lyxerr << "LyXFont::lyxWriteFontChanges: "
629 "IGNORE should not appear here!"
634 if (orgfont.noun() != noun()) {
635 os << "\\noun " << LyXMiscNames[noun()] << " \n";
637 if (orgfont.latex() != latex()) {
638 // This is only for backwards compatibility
640 case OFF: os << "\\latex no_latex \n"; break;
641 case ON: os << "\\latex latex \n"; break;
642 case TOGGLE: lyxerr << "LyXFont::lyxWriteFontChanges: "
643 "TOGGLE should not appear here!"
646 case INHERIT: os << "\\latex default \n"; break;
647 case IGNORE: lyxerr << "LyXFont::lyxWriteFontChanges: "
648 "IGNORE should not appear here!"
653 if (orgfont.color() != color()) {
654 os << "\\color " << LyXColorNames[color()] << "\n";
656 if (orgfont.direction() != direction()) {
657 switch (direction()) {
658 case RTL_DIR: os << "\\direction rtl \n"; break;
659 case LTR_DIR: os << "\\direction ltr\n"; break;
660 case TOGGLE_DIR: lyxerr << "LyXFont::lyxWriteFontChanges: "
661 "TOGGLE should not appear here!"
663 case INHERIT_DIR: os << "\\direction default \n"; break;
664 case IGNORE_DIR: lyxerr << "LyXFont::lyxWriteFontChanges: "
665 "IGNORE should not appear here!"
673 /// Writes the head of the LaTeX needed to impose this font
674 // Returns number of chars written.
675 int LyXFont::latexWriteStartChanges(string & file, LyXFont const & base, LyXFont const & prev) const
680 if (f.bits == inherit)
686 FONT_DIRECTION direction = f.direction();
687 if (direction != prev.direction()) {
688 if (direction == LTR_DIR) {
691 env = true; //We have opened a new environment
693 if (direction == RTL_DIR) {
696 env = true; //We have opened a new environment
700 if (f.family() != INHERIT_FAMILY) {
702 file += LaTeXFamilyNames[f.family()];
704 count += LaTeXFamilyNames[f.family()].length() + 2;
705 env = true; //We have opened a new environment
707 if (f.series() != INHERIT_SERIES) {
709 file += LaTeXSeriesNames[f.series()];
711 count += LaTeXSeriesNames[f.series()].length() + 2;
712 env = true; //We have opened a new environment
714 if (f.shape() != INHERIT_SHAPE) {
716 file += LaTeXShapeNames[f.shape()];
718 count += LaTeXShapeNames[f.shape()].length() + 2;
719 env = true; //We have opened a new environment
721 if (f.color() != INHERIT_COLOR) {
722 file += "\\textcolor{";
723 file += LaTeXColorNames[f.color()];
725 count += LaTeXColorNames[f.color()].length() + 13;
726 env = true; //We have opened a new environment
728 if (f.emph() == ON) {
731 env = true; //We have opened a new environment
733 if (f.underbar() == ON) {
734 file += "\\underbar{";
736 env = true; //We have opened a new environment
738 // \noun{} is a LyX special macro
739 if (f.noun() == ON) {
742 env = true; //We have opened a new environment
744 if (f.size() != INHERIT_SIZE) {
745 // If we didn't open an environment above, we open one here
751 file += LaTeXSizeNames[f.size()];
753 count += LaTeXSizeNames[f.size()].length() + 2;
759 /// Writes ending block of LaTeX needed to close use of this font
760 // Returns number of chars written
761 // This one corresponds to latexWriteStartChanges(). (Asger)
762 int LyXFont::latexWriteEndChanges(string & file, LyXFont const & base, LyXFont const & next) const
764 LyXFont f = *this; // why do you need this?
765 f.reduce(base); // why isn't this just "reduce(base);" (Lgb)
766 // Because this function is const. Everything breaks if this
767 // method changes the font it represents. There is no speed penalty
768 // by using the temporary. (Asger)
770 if (f.bits == inherit)
776 FONT_DIRECTION direction = f.direction();
777 if ( direction != next.direction()
778 && (direction == RTL_DIR || direction == LTR_DIR) ) {
781 env = true; // Size change need not bother about closing env.
784 if (f.family() != INHERIT_FAMILY) {
787 env = true; // Size change need not bother about closing env.
789 if (f.series() != INHERIT_SERIES) {
792 env = true; // Size change need not bother about closing env.
794 if (f.shape() != INHERIT_SHAPE) {
797 env = true; // Size change need not bother about closing env.
799 if (f.color() != INHERIT_COLOR) {
802 env = true; // Size change need not bother about closing env.
804 if (f.emph() == ON) {
807 env = true; // Size change need not bother about closing env.
809 if (f.underbar() == ON) {
812 env = true; // Size change need not bother about closing env.
814 if (f.noun() == ON) {
817 env = true; // Size change need not bother about closing env.
819 if (f.size() != INHERIT_SIZE) {
820 // We only have to close if only size changed
831 // Convert logical attributes to concrete shape attribute
832 LyXFont::FONT_SHAPE LyXFont::realShape() const
834 register FONT_SHAPE s = shape();
848 GC LyXFont::getGC() const
852 gc = ::getGC(gc_latex);
855 gc = ::getGC(gc_copy);
856 else if (color() == MATH)
857 gc = ::getGC(gc_math);
858 else if (color() == INSET)
859 gc = ::getGC(gc_foot);
861 gc = ::GetColorGC(color());
864 XSetFont(fl_display, gc, getXFontstruct()->fid);
869 XFontStruct * LyXFont::getXFontstruct() const
871 return fontloader.load(family(), series(), realShape(), size());
875 int LyXFont::maxAscent() const
877 return getXFontstruct()->ascent;
881 int LyXFont::maxDescent() const
883 return getXFontstruct()->descent;
887 int LyXFont::ascent(char c) const
889 XFontStruct * finfo = getXFontstruct();
890 unsigned int uc = static_cast<unsigned char>(c);
892 && uc >= finfo->min_char_or_byte2
893 && uc <= finfo->max_char_or_byte2)
894 return finfo->per_char[uc - finfo->min_char_or_byte2].ascent;
896 return finfo->ascent;
900 int LyXFont::descent(char c) const
902 XFontStruct * finfo = getXFontstruct();
903 unsigned int uc = static_cast<unsigned char>(c);
905 && uc >= finfo->min_char_or_byte2
906 && uc <= finfo->max_char_or_byte2)
907 return finfo->per_char[uc - finfo->min_char_or_byte2].descent;
909 return finfo->descent;
913 // Specialized after profiling. (Asger)
914 int LyXFont::width(char c) const
916 if (realShape() != LyXFont::SMALLCAPS_SHAPE){
917 return XTextWidth(getXFontstruct(), &c, 1);
919 return textWidth(&c, 1);
924 int LyXFont::lbearing(char c) const
926 XFontStruct * finfo = getXFontstruct();
927 unsigned int uc = static_cast<unsigned char>(c);
929 && uc >= finfo->min_char_or_byte2
930 && uc <= finfo->max_char_or_byte2)
931 return finfo->per_char[uc - finfo->min_char_or_byte2].lbearing;
937 int LyXFont::rbearing(char c) const
939 XFontStruct * finfo = getXFontstruct();
940 unsigned int uc = static_cast<unsigned char>(c);
942 && uc >= finfo->min_char_or_byte2
943 && uc <= finfo->max_char_or_byte2)
944 return finfo->per_char[uc - finfo->min_char_or_byte2].rbearing;
950 int LyXFont::textWidth(char const * s, int n) const
952 if (realShape() != LyXFont::SMALLCAPS_SHAPE){
953 return XTextWidth(getXFontstruct(), s, n);
955 // emulate smallcaps since X doesn't support this
956 unsigned int result = 0;
958 LyXFont smallfont = *this;
961 smallfont.setShape(LyXFont::UP_SHAPE);
962 for (int i = 0; i < n; ++i) {
964 // when islower is a macro, the cast is needed (JMarc)
965 if (islower(static_cast<unsigned char>(c))){
967 result += XTextWidth(smallfont.getXFontstruct(), &c, 1);
969 result += XTextWidth(getXFontstruct(), &c, 1);
977 int LyXFont::stringWidth(string const & s) const
979 if (s.empty()) return 0;
980 return textWidth(s.c_str(), s.length());
984 int LyXFont::signedStringWidth(string const & s) const
986 if (s.empty()) return 0;
987 if (s.c_str()[0] == '-')
988 return -textWidth(s.c_str()+1, s.length()-1);
990 return textWidth(s.c_str(), s.length());
994 int LyXFont::drawText(char const * s, int n, Pixmap pm,
995 int baseline, int x) const
997 if (realShape() != LyXFont::SMALLCAPS_SHAPE) {
998 XDrawString(fl_display,
1004 return XTextWidth(getXFontstruct(), s, n);
1007 // emulate smallcaps since X doesn't support this
1010 LyXFont smallfont = *this;
1011 smallfont.decSize();
1012 smallfont.decSize();
1013 smallfont.setShape(LyXFont::UP_SHAPE);
1014 for (int i = 0; i < n; ++i) {
1016 if (islower(static_cast<unsigned char>(c))){
1018 XDrawString(fl_display,
1023 x += XTextWidth(smallfont.getXFontstruct(),
1027 XDrawString(fl_display,
1032 x += XTextWidth(getXFontstruct(), &c, 1);
1041 int LyXFont::drawString(string const & s, Pixmap pm, int baseline, int x) const
1043 return drawText(s.c_str(), s.length(), pm, baseline, x);
1047 bool LyXFont::equalExceptLatex(LyXFont const & f) const
1050 f1.setLatex(f.latex());
1055 ostream & operator<<(ostream & o, LyXFont::FONT_MISC_STATE fms)
1057 return o << int(fms);