1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright (C) 1995 Matthias Ettrich
7 * Copyright (C) 1995-1999 The LyX Team.
9 * ======================================================*/
15 #pragma implementation "lyxfont.h"
20 #include "definitions.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 GUIColorNames[13] =
63 { N_("None"), N_("Black"), N_("White"), N_("Red"), N_("Green"), N_("Blue"),
64 N_("Cyan"), N_("Magenta"),
65 N_("Yellow"), N_("Math"), N_("Inset"), N_("Inherit"), N_("Ignore") };
68 // Strings used to read and write .lyx format files
70 string const LyXFamilyNames[6] =
71 { "roman", "sans", "typewriter", "symbol", "default", "error" };
73 string const LyXSeriesNames[4] =
74 { "medium", "bold", "default", "error" };
76 string const LyXShapeNames[6] =
77 { "up", "italic", "slanted", "smallcaps", "default", "error" };
79 string const LyXSizeNames[14] =
80 { "tiny", "scriptsize", "footnotesize", "small", "normal", "large",
81 "larger", "largest", "huge", "giant",
82 "increase-error", "decrease-error", "default", "error" };
84 string const LyXMiscNames[12] =
85 { "off", "on", "toggle", "default", "error" };
87 string const LyXColorNames[13] =
88 { "none", "black", "white", "red", "green", "blue", "cyan", "magenta",
89 "yellow", "matherror", "inseterror", "default", "error" };
92 // Strings used to write LaTeX files
95 string const LaTeXFamilyNames[6] =
96 { "textrm", "textsf", "texttt", "error1", "error2", "error3" };
98 string const LaTeXSeriesNames[4] =
99 { "textmd", "textbf", "error4", "error5" };
101 string const LaTeXShapeNames[6] =
102 { "textup", "textit", "textsl", "textsc", "error6", "error7" };
104 string const LaTeXSizeNames[14] =
105 { "tiny", "scriptsize", "footnotesize", "small", "normalsize", "large",
106 "Large", "LARGE", "huge", "Huge", "error8", "error9", "error10", "error11" };
108 string const LaTeXColorNames[13] =
109 { "none", "black", "white", "red", "green", "blue", "cyan", "magenta",
110 "yellow", "error12", "error13", "error14", "error15" };
112 /// Decreases font size by one
113 LyXFont& LyXFont::decSize()
116 case SIZE_HUGER: setSize(SIZE_HUGE); break;
117 case SIZE_HUGE: setSize(SIZE_LARGEST); break;
118 case SIZE_LARGEST: setSize(SIZE_LARGER); break;
119 case SIZE_LARGER: setSize(SIZE_LARGE); break;
120 case SIZE_LARGE: setSize(SIZE_NORMAL); break;
121 case SIZE_NORMAL: setSize(SIZE_SMALL); break;
122 case SIZE_SMALL: setSize(SIZE_FOOTNOTE); break;
123 case SIZE_FOOTNOTE: setSize(SIZE_SCRIPT); break;
124 case SIZE_SCRIPT: setSize(SIZE_TINY); break;
125 case SIZE_TINY: break;
127 lyxerr.print("Can't LyXFont::decSize on INCREASE_SIZE");
130 lyxerr.print("Can't LyXFont::decSize on DECREASE_SIZE");
133 lyxerr.print("Can't LyXFont::decSize on INHERIT_SIZE");
136 lyxerr.print("Can't LyXFont::decSize on IGNORE_SIZE");
143 /// Increases font size by one
144 LyXFont& LyXFont::incSize()
147 case SIZE_HUGER: break;
148 case SIZE_HUGE: setSize(SIZE_HUGER); break;
149 case SIZE_LARGEST: setSize(SIZE_HUGE); break;
150 case SIZE_LARGER: setSize(SIZE_LARGEST); break;
151 case SIZE_LARGE: setSize(SIZE_LARGER); break;
152 case SIZE_NORMAL: setSize(SIZE_LARGE); break;
153 case SIZE_SMALL: setSize(SIZE_NORMAL); break;
154 case SIZE_FOOTNOTE: setSize(SIZE_SMALL); break;
155 case SIZE_SCRIPT: setSize(SIZE_FOOTNOTE); break;
156 case SIZE_TINY: setSize(SIZE_SCRIPT); break;
158 lyxerr.print("Can't LyXFont::incSize on INCREASE_SIZE");
161 lyxerr.print("Can't LyXFont::incSize on DECREASE_SIZE");
164 lyxerr.print("Can't LyXFont::incSize on INHERIT_SIZE");
167 lyxerr.print("Can't LyXFont::incSize on IGNORE_SIZE");
174 /// Updates a misc setting according to request
175 LyXFont::FONT_MISC_STATE LyXFont::setMisc(FONT_MISC_STATE newfont,
178 if (newfont == TOGGLE) {
184 lyxerr.print("LyXFont::setMisc: Need state"
185 " ON or OFF to toggle. Setting to ON");
188 } else if (newfont == IGNORE)
195 /// Updates font settings according to request
196 void LyXFont::update(LyXFont const & newfont, bool toggleall)
198 if(newfont.family() == family() && toggleall)
199 setFamily(INHERIT_FAMILY); // toggle 'back'
200 else if (newfont.family() != IGNORE_FAMILY)
201 setFamily(newfont.family());
202 // else it's IGNORE_SHAPE
204 // "Old" behaviour: "Setting" bold will toggle bold on/off.
205 switch (newfont.series()) {
208 if (series() == BOLD_SERIES && toggleall)
209 setSeries(MEDIUM_SERIES);
211 setSeries(BOLD_SERIES);
215 setSeries(newfont.series());
221 if(newfont.shape() == shape() && toggleall)
222 setShape(INHERIT_SHAPE); // toggle 'back'
223 else if (newfont.shape() != IGNORE_SHAPE)
224 setShape(newfont.shape());
225 // else it's IGNORE_SHAPE
227 if (newfont.size() != IGNORE_SIZE) {
228 if (newfont.size() == INCREASE_SIZE)
230 else if (newfont.size() == DECREASE_SIZE)
232 else if (newfont.size() == size() && toggleall)
233 setSize(INHERIT_SIZE); // toggle 'back'
235 setSize(newfont.size());
238 setEmph(setMisc(newfont.emph(), emph()));
239 setUnderbar(setMisc(newfont.underbar(), underbar()));
240 setNoun(setMisc(newfont.noun(), noun()));
241 setLatex(setMisc(newfont.latex(), latex()));
243 if(newfont.color() == color() && toggleall)
244 setColor(INHERIT_COLOR); // toggle 'back'
245 else if (newfont.color() != IGNORE_COLOR)
246 setColor(newfont.color());
250 /// Reduce font to fall back to template where possible
251 void LyXFont::reduce(LyXFont const & tmplt)
253 if (family() == tmplt.family())
254 setFamily(INHERIT_FAMILY);
255 if (series() == tmplt.series())
256 setSeries(INHERIT_SERIES);
257 if (shape() == tmplt.shape())
258 setShape(INHERIT_SHAPE);
259 if (size() == tmplt.size())
260 setSize(INHERIT_SIZE);
261 if (emph() == tmplt.emph())
263 if (underbar() == tmplt.underbar())
264 setUnderbar(INHERIT);
265 if (noun() == tmplt.noun())
267 if (latex() == tmplt.latex())
269 if (color() == tmplt.color())
270 setColor(INHERIT_COLOR);
274 /// Realize font from a template
275 // This one is not pretty, but it's extremely fast (Asger)
276 LyXFont & LyXFont::realize(LyXFont const & tmplt)
278 if (bits == inherit) {
283 if ((bits & (Fam_Mask<<Fam_Pos)) == (ui32(INHERIT_FAMILY)<<Fam_Pos))
285 bits &= ~(Fam_Mask << Fam_Pos);
286 bits |= (tmplt.bits & Fam_Mask << Fam_Pos);
288 if ((bits & (Ser_Mask<<Ser_Pos)) == (ui32(INHERIT_SERIES)<<Ser_Pos))
290 bits &= ~(Ser_Mask << Ser_Pos);
291 bits |= (tmplt.bits & Ser_Mask << Ser_Pos);
293 if ((bits & (Sha_Mask << Sha_Pos)) == ui32(INHERIT_SHAPE) << Sha_Pos)
295 bits &= ~(Sha_Mask << Sha_Pos);
296 bits |= (tmplt.bits & Sha_Mask << Sha_Pos);
298 if ((bits & (Siz_Mask << Siz_Pos)) == ui32(INHERIT_SIZE) << Siz_Pos)
300 bits &= ~(Siz_Mask << Siz_Pos);
301 bits |= (tmplt.bits & Siz_Mask << Siz_Pos);
303 if ((bits & (Misc_Mask << Emp_Pos)) == ui32(INHERIT) << Emp_Pos)
305 bits &= ~(Misc_Mask << Emp_Pos);
306 bits |= (tmplt.bits & Misc_Mask << Emp_Pos);
308 if ((bits & (Misc_Mask << Und_Pos)) == ui32(INHERIT) << Und_Pos)
310 bits &= ~(Misc_Mask << Und_Pos);
311 bits |= (tmplt.bits & Misc_Mask << Und_Pos);
313 if ((bits & (Misc_Mask << Nou_Pos)) == ui32(INHERIT) << Nou_Pos)
315 bits &= ~(Misc_Mask << Nou_Pos);
316 bits |= (tmplt.bits & Misc_Mask << Nou_Pos);
318 if ((bits & (Misc_Mask << Lat_Pos)) == ui32(INHERIT) << Lat_Pos)
320 bits &= ~(Misc_Mask << Lat_Pos);
321 bits |= (tmplt.bits & Misc_Mask << Lat_Pos);
323 if ((bits & (Col_Mask << Col_Pos)) == ui32(INHERIT_COLOR) << Col_Pos)
325 bits &= ~(Col_Mask << Col_Pos);
326 bits |= (tmplt.bits & Col_Mask << Col_Pos);
332 /// Is font resolved?
333 bool LyXFont::resolved() const
335 return (family() != INHERIT_FAMILY && series() != INHERIT_SERIES &&
336 shape() != INHERIT_SHAPE && size() != INHERIT_SIZE &&
337 emph() != INHERIT && underbar() != INHERIT &&
338 noun() != INHERIT && latex() != INHERIT &&
339 color() != INHERIT_COLOR);
342 /// Build GUI description of font state
343 string LyXFont::stateText() const
346 if (family() != INHERIT_FAMILY)
347 buf += string(_(GUIFamilyNames[family()].c_str())) + ", ";
348 if (series() != INHERIT_SERIES)
349 buf += string(_(GUISeriesNames[series()].c_str())) + ", ";
350 if (shape() != INHERIT_SHAPE)
351 buf += string(_(GUIShapeNames[shape()].c_str())) + ", ";
352 if (size() != INHERIT_SIZE)
353 buf += string(_(GUISizeNames[size()].c_str())) + ", ";
354 if (color() != INHERIT_COLOR)
355 buf += string(_(GUIColorNames[color()].c_str())) + ", ";
357 if (emph() != INHERIT)
358 buf += string(_("Emphasis ")) + _(GUIMiscNames[emph()].c_str()) + ", ";
359 if (underbar() != INHERIT)
360 buf += string(_("Underline ")) + _(GUIMiscNames[underbar()].c_str()) + ", ";
361 if (noun() != INHERIT)
362 buf += string(_("Noun ")) + _(GUIMiscNames[noun()].c_str()) + ", ";
363 if (latex() != INHERIT)
364 buf += string(_("Latex ")) + _(GUIMiscNames[latex()].c_str()) + ", ";
367 buf = strip(buf, ' ');
368 buf = strip(buf, ',');
373 // Set family according to lyx format string
374 LyXFont& LyXFont::setLyXFamily(string const & fam)
376 string s = lowercase(fam);
379 while (s != LyXFamilyNames[i] && LyXFamilyNames[i] != "error") i++;
380 if (s == LyXFamilyNames[i]) {
381 setFamily(LyXFont::FONT_FAMILY(i));
383 lyxerr.print("LyXFont::setLyXFamily: Unknown family `"+s+'\'');
388 // Set series according to lyx format string
389 LyXFont& LyXFont::setLyXSeries(string const & ser)
391 string s = lowercase(ser);
394 while (s != LyXSeriesNames[i] && LyXSeriesNames[i] != "error") i++;
395 if (s == LyXSeriesNames[i]) {
396 setSeries(LyXFont::FONT_SERIES(i));
398 lyxerr.print("LyXFont::setLyXSeries: Unknown series `"+s+'\'');
403 // Set shape according to lyx format string
404 LyXFont& LyXFont::setLyXShape(string const & sha)
406 string s = lowercase(sha);
409 while (s != LyXShapeNames[i] && LyXShapeNames[i] != "error") i++;
410 if (s == LyXShapeNames[i]) {
411 setShape(LyXFont::FONT_SHAPE(i));
413 lyxerr.print("LyXFont::setLyXShape: Unknown shape `"+s+'\'');
418 // Set size according to lyx format string
419 LyXFont& LyXFont::setLyXSize(string const & siz)
421 string s = lowercase(siz);
423 while (s != LyXSizeNames[i] && LyXSizeNames[i] != "error") i++;
424 if (s == LyXSizeNames[i]) {
425 setSize(LyXFont::FONT_SIZE(i));
427 lyxerr.print("LyXFont::setLyXSize: Unknown size `"+s+'\'');
431 // Set size according to lyx format string
432 LyXFont::FONT_MISC_STATE LyXFont::setLyXMisc(string const & siz)
434 string s = lowercase(siz);
436 while (s != LyXMiscNames[i] && LyXMiscNames[i] != "error") i++;
437 if (s == LyXMiscNames[i])
438 return FONT_MISC_STATE(i);
439 lyxerr.print("LyXFont::setLyXMisc: Unknown misc flag `"+s+'\'');
443 /// Sets color after LyX text format
444 LyXFont& LyXFont::setLyXColor(string const & col)
446 string s = lowercase(col);
448 while (s != LyXColorNames[i] && LyXColorNames[i] != "error") i++;
449 if (s == LyXColorNames[i]) {
450 setColor(LyXFont::FONT_COLOR(i));
452 lyxerr.print("LyXFont::setLyXColor: Unknown Color `"+s+'\'');
457 /// Sets size after GUI name
458 LyXFont& LyXFont::setGUISize(string const & siz)
460 string s = lowercase(siz);
462 while (!lGUISizeNames[i].empty() &&
463 s != _(lGUISizeNames[i].c_str()))
465 if (s == _(lGUISizeNames[i].c_str())) {
466 setSize(LyXFont::FONT_SIZE(i));
468 lyxerr.print("LyXFont::setGUISize: Unknown Size `"+s+'\'');
473 // Returns size in latex format
474 string LyXFont::latexSize() const
476 return LaTeXSizeNames[size()];
480 // Read a font definition from given file in lyx format
482 LyXFont & LyXFont::lyxRead(LyXLex & lex)
485 bool finished = false;
486 while (!finished && lex.IsOK() && !error) {
488 string tok = lowercase(lex.GetString());
492 } else if (tok == "endfont") {
494 } else if (tok == "family") {
496 string tok = lex.GetString();
498 } else if (tok == "series") {
500 string tok = lex.GetString();
502 } else if (tok == "shape") {
504 string tok = lex.GetString();
506 } else if (tok == "size") {
508 string tok = lex.GetString();
510 } else if (tok == "latex") {
512 string tok = lowercase(lex.GetString());
514 if (tok == "no_latex") {
516 } else if (tok == "latex") {
519 lex.printError("Illegal LaTeX type`$$Token'");
521 } else if (tok == "misc") {
523 string tok = lowercase(lex.GetString());
525 if (tok == "no_bar") {
527 } else if (tok == "no_emph") {
529 } else if (tok == "no_noun") {
531 } else if (tok == "emph") {
533 } else if (tok == "underbar") {
535 } else if (tok == "noun") {
538 lex.printError("Illegal misc type `$$Token´");
540 } else if (tok == "color") {
542 string tok = lex.GetString();
545 lex.printError("Unknown tag `$$Token'");
553 /// Writes the changes from this font to orgfont in .lyx format in file
554 void LyXFont::lyxWriteChanges(LyXFont const & orgfont, FILE * file) const
557 if (orgfont.family() != family()) {
558 fprintf(file, "\\family %s \n",
559 LyXFamilyNames[family()].c_str());
561 if (orgfont.series() != series()) {
562 fprintf(file, "\\series %s \n",
563 LyXSeriesNames[series()].c_str());
565 if (orgfont.shape() != shape()) {
566 fprintf(file, "\\shape %s \n", LyXShapeNames[shape()].c_str());
568 if (orgfont.size() != size()) {
569 fprintf(file, "\\size %s \n", LyXSizeNames[size()].c_str());
571 if (orgfont.emph() != emph()) {
572 fprintf(file, "\\emph %s \n", LyXMiscNames[emph()].c_str());
574 if (orgfont.underbar() != underbar()) {
575 // This is only for backwards compatibility
576 switch (underbar()) {
577 case OFF: fprintf(file, "\\bar no \n"); break;
578 case ON: fprintf(file, "\\bar under \n"); break;
579 case TOGGLE: lyxerr.print("LyXFont::lyxWriteFontChanges: "
580 "TOGGLE should not appear here!");
582 case INHERIT: fprintf(file, "\\bar default \n"); break;
583 case IGNORE: lyxerr.print("LyXFont::lyxWriteFontChanges: "
584 "IGNORE should not appear here!");
588 if (orgfont.noun() != noun()) {
589 fprintf(file, "\\noun %s \n", LyXMiscNames[noun()].c_str());
591 if (orgfont.latex() != latex()) {
592 // This is only for backwards compatibility
594 case OFF: fprintf(file, "\\latex no_latex \n"); break;
595 case ON: fprintf(file, "\\latex latex \n"); break;
596 case TOGGLE: lyxerr.print("LyXFont::lyxWriteFontChanges: "
597 "TOGGLE should not appear here!");
599 case INHERIT: fprintf(file, "\\latex default \n"); break;
600 case IGNORE: lyxerr.print("LyXFont::lyxWriteFontChanges: "
601 "IGNORE should not appear here!");
605 if (orgfont.color() != color()) {
606 fprintf(file, "\\color %s\n", LyXColorNames[color()].c_str());
611 /// Writes the head of the LaTeX needed to impose this font
612 // Returns number of chars written.
613 int LyXFont::latexWriteStartChanges(FILE * file, LyXFont const & base) const
616 int count = latexWriteStartChanges(font, base);
617 fprintf(file, "%s", font.c_str());
622 /// Writes the head of the LaTeX needed to impose this font
623 // Returns number of chars written.
624 int LyXFont::latexWriteStartChanges(string & file, LyXFont const & base) const
629 if (f.bits == inherit)
635 if (f.family() != INHERIT_FAMILY) {
637 file += LaTeXFamilyNames[f.family()];
639 count += LaTeXFamilyNames[f.family()].length() + 2;
640 env = true; //We have opened a new environment
642 if (f.series() != INHERIT_SERIES) {
644 file += LaTeXSeriesNames[f.series()];
646 count += LaTeXSeriesNames[f.series()].length() + 2;
647 env = true; //We have opened a new environment
649 if (f.shape() != INHERIT_SHAPE) {
651 file += LaTeXShapeNames[f.shape()];
653 count += LaTeXShapeNames[f.shape()].length() + 2;
654 env = true; //We have opened a new environment
656 if (f.color() != INHERIT_COLOR) {
657 file += "\\textcolor{";
658 file += LaTeXColorNames[f.color()];
660 count += LaTeXColorNames[f.color()].length() + 13;
661 env = true; //We have opened a new environment
663 if (f.emph() == ON) {
666 env = true; //We have opened a new environment
668 if (f.underbar() == ON) {
669 file += "\\underbar{";
671 env = true; //We have opened a new environment
673 // \noun{} is a LyX special macro
674 if (f.noun() == ON) {
677 env = true; //We have opened a new environment
679 if (f.size() != INHERIT_SIZE) {
680 // If we didn't open an environment above, we open one here
686 file += LaTeXSizeNames[f.size()];
688 count += LaTeXSizeNames[f.size()].length() + 2;
694 /// Writes ending block of LaTeX needed to close use of this font
695 // Returns number of chars written
696 // This one corresponds to latexWriteStartChanges(). (Asger)
697 int LyXFont::latexWriteEndChanges(FILE * file, LyXFont const & base) const
700 int count = latexWriteEndChanges(ending, base);
701 fprintf(file, "%s", ending.c_str());
706 /// Writes ending block of LaTeX needed to close use of this font
707 // Returns number of chars written
708 // This one corresponds to latexWriteStartChanges(). (Asger)
709 int LyXFont::latexWriteEndChanges(string & file, LyXFont const & base) const
711 LyXFont f = *this; // why do you need this?
712 f.reduce(base); // why isn't this just "reduce(base);" (Lgb)
713 // Because this function is const. Everything breaks if this
714 // method changes the font it represents. There is no speed penalty
715 // by using the temporary. (Asger)
717 if (f.bits == inherit)
722 if (f.family() != INHERIT_FAMILY) {
725 env = true; // Size change need not bother about closing env.
727 if (f.series() != INHERIT_SERIES) {
730 env = true; // Size change need not bother about closing env.
732 if (f.shape() != INHERIT_SHAPE) {
735 env = true; // Size change need not bother about closing env.
737 if (f.color() != INHERIT_COLOR) {
740 env = true; // Size change need not bother about closing env.
742 if (f.emph() == ON) {
745 env = true; // Size change need not bother about closing env.
747 if (f.underbar() == ON) {
750 env = true; // Size change need not bother about closing env.
752 if (f.noun() == ON) {
755 env = true; // Size change need not bother about closing env.
757 if (f.size() != INHERIT_SIZE) {
758 // We only have to close if only size changed
769 // Convert logical attributes to concrete shape attribute
770 LyXFont::FONT_SHAPE LyXFont::realShape() const
772 register FONT_SHAPE s = shape();
786 GC LyXFont::getGC() const
790 gc = ::getGC(gc_latex);
793 gc = ::getGC(gc_copy);
794 else if (color() == MATH)
795 gc = ::getGC(gc_math);
796 else if (color() == INSET)
797 gc = ::getGC(gc_foot);
799 gc = ::GetColorGC(color());
802 XSetFont(fl_display, gc, getXFontstruct()->fid);
807 XFontStruct* LyXFont::getXFontstruct() const
809 return fontloader.load(family(),series(),realShape(),size());
813 int LyXFont::maxAscent() const
815 return getXFontstruct()->ascent;
819 int LyXFont::maxDescent() const
821 return getXFontstruct()->descent;
825 int LyXFont::ascent(char c) const
827 XFontStruct *finfo = getXFontstruct();
829 && (unsigned int) c >= finfo->min_char_or_byte2
830 && (unsigned int) c <= finfo->max_char_or_byte2) {
831 unsigned int index = (unsigned int) c - finfo->min_char_or_byte2;
832 return finfo->per_char[index].ascent;
834 return finfo->ascent;
838 int LyXFont::descent(char c) const
840 XFontStruct *finfo = getXFontstruct();
842 && (unsigned int) c >= finfo->min_char_or_byte2
843 && (unsigned int) c <= finfo->max_char_or_byte2) {
844 unsigned int index = (unsigned int) c - finfo->min_char_or_byte2;
845 return finfo->per_char[index].descent;
847 return finfo->descent;
851 // Specialized after profiling. (Asger)
852 int LyXFont::width(char c) const
854 if (realShape() != LyXFont::SMALLCAPS_SHAPE){
855 return XTextWidth(getXFontstruct(), &c, 1);
857 return textWidth(&c, 1);
862 int LyXFont::textWidth(char const *s, int n) const
864 if (realShape() != LyXFont::SMALLCAPS_SHAPE){
865 return XTextWidth(getXFontstruct(), s, n);
867 // emulate smallcaps since X doesn't support this
868 unsigned int result = 0;
870 LyXFont smallfont = *this;
873 smallfont.setShape(LyXFont::UP_SHAPE);
874 for (int i=0; i < n; i++){
876 if (islower((unsigned char) c)){
877 c = toupper( (unsigned char) c );
878 result += XTextWidth(smallfont.getXFontstruct(), &c, 1);
880 result += XTextWidth(getXFontstruct(), &c, 1);
888 int LyXFont::stringWidth(string const & s) const
890 if (s.empty()) return 0;
891 return textWidth(s.c_str(), s.length());
894 int LyXFont::signedStringWidth(string const & s) const
896 if (s.empty()) return 0;
897 if (s.c_str()[0] == '-')
898 return -textWidth(s.c_str()+1, s.length()-1);
900 return textWidth(s.c_str(), s.length());
904 int LyXFont::drawText(char const* s, int n, Pixmap pm,
905 int baseline, int x) const
907 if (realShape() != LyXFont::SMALLCAPS_SHAPE) {
908 XDrawString(fl_display,
914 return XTextWidth(getXFontstruct(), s, n);
917 // emulate smallcaps since X doesn't support this
921 LyXFont smallfont = *this;
924 smallfont.setShape(LyXFont::UP_SHAPE);
925 for (i=0; i < n; i++){
927 if (islower((unsigned char) c)){
928 c = toupper((unsigned char) c);
929 XDrawString(fl_display,
934 x += XTextWidth(smallfont.getXFontstruct(), &c, 1);
937 XDrawString(fl_display,
942 x += XTextWidth(getXFontstruct(), &c, 1);
951 int LyXFont::drawString(string const &s, Pixmap pm, int baseline, int x) const
953 return drawText(s.c_str(), s.length(), pm, baseline, x);
957 bool LyXFont::equalExceptLatex(LyXFont const &f) const
960 f1.setLatex(f.latex());