1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright (C) 1995 Matthias Ettrich
7 * Copyright (C) 1995-1998 The LyX Team.
9 *======================================================*/
15 #pragma implementation "lyxfont.h"
20 #include "definitions.h"
26 #include "FontLoader.h"
28 // $Id: lyxfont.C,v 1.1 1999/09/27 18:44:37 larsbj Exp $
30 #if !defined(lint) && !defined(WITH_WARNINGS)
31 static char vcid[] = "$Id: lyxfont.C,v 1.1 1999/09/27 18:44:37 larsbj Exp $";
36 // The global fontloader
37 FontLoader fontloader;
43 LString const GUIFamilyNames[6] =
44 { N_("Roman"), N_("Sans serif"), N_("Typewriter"), N_("Symbol"), N_("Inherit"),
47 LString const GUISeriesNames[4] =
48 { N_("Medium"), N_("Bold"), N_("Inherit"), N_("Ignore") };
50 LString const GUIShapeNames[6] =
51 { N_("Upright"), N_("Italic"), N_("Slanted"), N_("Smallcaps"), N_("Inherit"),
54 LString 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 LString const lGUISizeNames[15] =
60 { N_("tiny"), N_("smallest"), N_("smaller"), N_("small"), N_("normal"), N_("large"),
61 N_("larger"), N_("largest"), N_("huge"), N_("huger"), N_("increase"), N_("decrease"),
62 N_("inherit"), N_("ignore"), LString() };
64 LString const GUIMiscNames[5] =
65 { N_("Off"), N_("On"), N_("Toggle"), N_("Inherit"), N_("Ignore") };
67 LString const GUIColorNames[13] =
68 { N_("None"), N_("Black"), N_("White"), N_("Red"), N_("Green"), N_("Blue"),
69 N_("Cyan"), N_("Magenta"),
70 N_("Yellow"), N_("Math"), N_("Inset"), N_("Inherit"), N_("Ignore") };
73 // Strings used to read and write .lyx format files
75 LString const LyXFamilyNames[6] =
76 { "roman", "sans", "typewriter", "symbol", "default", "error" };
78 LString const LyXSeriesNames[4] =
79 { "medium", "bold", "default", "error" };
81 LString const LyXShapeNames[6] =
82 { "up", "italic", "slanted", "smallcaps", "default", "error" };
84 LString const LyXSizeNames[14] =
85 { "tiny", "scriptsize", "footnotesize", "small", "normal", "large",
86 "larger", "largest", "huge", "giant",
87 "increase-error", "decrease-error", "default", "error" };
89 LString const LyXMiscNames[12] =
90 { "off", "on", "toggle", "default", "error" };
92 LString const LyXColorNames[13] =
93 { "none", "black", "white", "red", "green", "blue", "cyan", "magenta",
94 "yellow", "matherror", "inseterror", "default", "error" };
97 // Strings used to write LaTeX files
100 LString const LaTeXFamilyNames[6] =
101 { "textrm", "textsf", "texttt", "error1", "error2", "error3" };
103 LString const LaTeXSeriesNames[4] =
104 { "textmd", "textbf", "error4", "error5" };
106 LString const LaTeXShapeNames[6] =
107 { "textup", "textit", "textsl", "textsc", "error6", "error7" };
109 LString const LaTeXSizeNames[14] =
110 { "tiny", "scriptsize", "footnotesize", "small", "normalsize", "large",
111 "Large", "LARGE", "huge", "Huge", "error8", "error9", "error10", "error11" };
113 LString const LaTeXColorNames[13] =
114 { "none", "black", "white", "red", "green", "blue", "cyan", "magenta",
115 "yellow", "error12", "error13", "error14", "error15" };
117 /// Decreases font size by one
118 LyXFont& LyXFont::decSize()
121 case SIZE_HUGER: setSize(SIZE_HUGE); break;
122 case SIZE_HUGE: setSize(SIZE_LARGEST); break;
123 case SIZE_LARGEST: setSize(SIZE_LARGER); break;
124 case SIZE_LARGER: setSize(SIZE_LARGE); break;
125 case SIZE_LARGE: setSize(SIZE_NORMAL); break;
126 case SIZE_NORMAL: setSize(SIZE_SMALL); break;
127 case SIZE_SMALL: setSize(SIZE_FOOTNOTE); break;
128 case SIZE_FOOTNOTE: setSize(SIZE_SCRIPT); break;
129 case SIZE_SCRIPT: setSize(SIZE_TINY); break;
130 case SIZE_TINY: break;
132 lyxerr.print("Can't LyXFont::decSize on INCREASE_SIZE");
135 lyxerr.print("Can't LyXFont::decSize on DECREASE_SIZE");
138 lyxerr.print("Can't LyXFont::decSize on INHERIT_SIZE");
141 lyxerr.print("Can't LyXFont::decSize on IGNORE_SIZE");
148 /// Increases font size by one
149 LyXFont& LyXFont::incSize()
152 case SIZE_HUGER: break;
153 case SIZE_HUGE: setSize(SIZE_HUGER); break;
154 case SIZE_LARGEST: setSize(SIZE_HUGE); break;
155 case SIZE_LARGER: setSize(SIZE_LARGEST); break;
156 case SIZE_LARGE: setSize(SIZE_LARGER); break;
157 case SIZE_NORMAL: setSize(SIZE_LARGE); break;
158 case SIZE_SMALL: setSize(SIZE_NORMAL); break;
159 case SIZE_FOOTNOTE: setSize(SIZE_SMALL); break;
160 case SIZE_SCRIPT: setSize(SIZE_FOOTNOTE); break;
161 case SIZE_TINY: setSize(SIZE_SCRIPT); break;
163 lyxerr.print("Can't LyXFont::incSize on INCREASE_SIZE");
166 lyxerr.print("Can't LyXFont::incSize on DECREASE_SIZE");
169 lyxerr.print("Can't LyXFont::incSize on INHERIT_SIZE");
172 lyxerr.print("Can't LyXFont::incSize on IGNORE_SIZE");
179 /// Updates a misc setting according to request
180 LyXFont::FONT_MISC_STATE LyXFont::setMisc(FONT_MISC_STATE newfont,
183 if (newfont == TOGGLE) {
189 lyxerr.print("LyXFont::setMisc: Need state"
190 " ON or OFF to toggle. Setting to ON");
193 } else if (newfont == IGNORE)
200 /// Updates font settings according to request
201 void LyXFont::update(LyXFont const & newfont, bool toggleall)
203 if(newfont.family() == family() && toggleall)
204 setFamily(INHERIT_FAMILY); // toggle 'back'
205 else if (newfont.family() != IGNORE_FAMILY)
206 setFamily(newfont.family());
207 // else it's IGNORE_SHAPE
209 // "Old" behaviour: "Setting" bold will toggle bold on/off.
210 switch (newfont.series()) {
213 if (series() == BOLD_SERIES && toggleall)
214 setSeries(MEDIUM_SERIES);
216 setSeries(BOLD_SERIES);
220 setSeries(newfont.series());
226 if(newfont.shape() == shape() && toggleall)
227 setShape(INHERIT_SHAPE); // toggle 'back'
228 else if (newfont.shape() != IGNORE_SHAPE)
229 setShape(newfont.shape());
230 // else it's IGNORE_SHAPE
232 if (newfont.size() != IGNORE_SIZE) {
233 if (newfont.size() == INCREASE_SIZE)
235 else if (newfont.size() == DECREASE_SIZE)
237 else if (newfont.size() == size() && toggleall)
238 setSize(INHERIT_SIZE); // toggle 'back'
240 setSize(newfont.size());
243 setEmph(setMisc(newfont.emph(), emph()));
244 setUnderbar(setMisc(newfont.underbar(), underbar()));
245 setNoun(setMisc(newfont.noun(), noun()));
246 setLatex(setMisc(newfont.latex(), latex()));
248 if(newfont.color() == color() && toggleall)
249 setColor(INHERIT_COLOR); // toggle 'back'
250 else if (newfont.color() != IGNORE_COLOR)
251 setColor(newfont.color());
255 /// Reduce font to fall back to template where possible
256 void LyXFont::reduce(LyXFont const & tmplt)
258 if (family() == tmplt.family())
259 setFamily(INHERIT_FAMILY);
260 if (series() == tmplt.series())
261 setSeries(INHERIT_SERIES);
262 if (shape() == tmplt.shape())
263 setShape(INHERIT_SHAPE);
264 if (size() == tmplt.size())
265 setSize(INHERIT_SIZE);
266 if (emph() == tmplt.emph())
268 if (underbar() == tmplt.underbar())
269 setUnderbar(INHERIT);
270 if (noun() == tmplt.noun())
272 if (latex() == tmplt.latex())
274 if (color() == tmplt.color())
275 setColor(INHERIT_COLOR);
279 /// Realize font from a template
280 // This one is not pretty, but it's extremely fast (Asger)
281 LyXFont & LyXFont::realize(LyXFont const & tmplt)
283 if (bits == inherit) {
288 if ((bits & (Fam_Mask<<Fam_Pos)) == (ui32(INHERIT_FAMILY)<<Fam_Pos))
290 bits &= ~(Fam_Mask << Fam_Pos);
291 bits |= (tmplt.bits & Fam_Mask << Fam_Pos);
293 if ((bits & (Ser_Mask<<Ser_Pos)) == (ui32(INHERIT_SERIES)<<Ser_Pos))
295 bits &= ~(Ser_Mask << Ser_Pos);
296 bits |= (tmplt.bits & Ser_Mask << Ser_Pos);
298 if ((bits & (Sha_Mask << Sha_Pos)) == ui32(INHERIT_SHAPE) << Sha_Pos)
300 bits &= ~(Sha_Mask << Sha_Pos);
301 bits |= (tmplt.bits & Sha_Mask << Sha_Pos);
303 if ((bits & (Siz_Mask << Siz_Pos)) == ui32(INHERIT_SIZE) << Siz_Pos)
305 bits &= ~(Siz_Mask << Siz_Pos);
306 bits |= (tmplt.bits & Siz_Mask << Siz_Pos);
308 if ((bits & (Misc_Mask << Emp_Pos)) == ui32(INHERIT) << Emp_Pos)
310 bits &= ~(Misc_Mask << Emp_Pos);
311 bits |= (tmplt.bits & Misc_Mask << Emp_Pos);
313 if ((bits & (Misc_Mask << Und_Pos)) == ui32(INHERIT) << Und_Pos)
315 bits &= ~(Misc_Mask << Und_Pos);
316 bits |= (tmplt.bits & Misc_Mask << Und_Pos);
318 if ((bits & (Misc_Mask << Nou_Pos)) == ui32(INHERIT) << Nou_Pos)
320 bits &= ~(Misc_Mask << Nou_Pos);
321 bits |= (tmplt.bits & Misc_Mask << Nou_Pos);
323 if ((bits & (Misc_Mask << Lat_Pos)) == ui32(INHERIT) << Lat_Pos)
325 bits &= ~(Misc_Mask << Lat_Pos);
326 bits |= (tmplt.bits & Misc_Mask << Lat_Pos);
328 if ((bits & (Col_Mask << Col_Pos)) == ui32(INHERIT_COLOR) << Col_Pos)
330 bits &= ~(Col_Mask << Col_Pos);
331 bits |= (tmplt.bits & Col_Mask << Col_Pos);
337 /// Is font resolved?
338 bool LyXFont::resolved() const
340 return (family() != INHERIT_FAMILY && series() != INHERIT_SERIES &&
341 shape() != INHERIT_SHAPE && size() != INHERIT_SIZE &&
342 emph() != INHERIT && underbar() != INHERIT &&
343 noun() != INHERIT && latex() != INHERIT &&
344 color() != INHERIT_COLOR);
347 /// Build GUI description of font state
348 LString LyXFont::stateText() const
351 if (family() != INHERIT_FAMILY)
352 buf += LString(_(GUIFamilyNames[family()].c_str())) + ", ";
353 if (series() != INHERIT_SERIES)
354 buf += LString(_(GUISeriesNames[series()].c_str())) + ", ";
355 if (shape() != INHERIT_SHAPE)
356 buf += LString(_(GUIShapeNames[shape()].c_str())) + ", ";
357 if (size() != INHERIT_SIZE)
358 buf += LString(_(GUISizeNames[size()].c_str())) + ", ";
359 if (color() != INHERIT_COLOR)
360 buf += LString(_(GUIColorNames[color()].c_str())) + ", ";
362 if (emph() != INHERIT)
363 buf += LString(_("Emphasis ")) + _(GUIMiscNames[emph()].c_str()) + ", ";
364 if (underbar() != INHERIT)
365 buf += LString(_("Underline ")) + _(GUIMiscNames[underbar()].c_str()) + ", ";
366 if (noun() != INHERIT)
367 buf += LString(_("Noun ")) + _(GUIMiscNames[noun()].c_str()) + ", ";
368 if (latex() != INHERIT)
369 buf += LString(_("Latex ")) + _(GUIMiscNames[latex()].c_str()) + ", ";
378 // Set family according to lyx format string
379 LyXFont& LyXFont::setLyXFamily(LString const & fam)
384 while (s != LyXFamilyNames[i] && LyXFamilyNames[i] != "error") i++;
385 if (s == LyXFamilyNames[i]) {
386 setFamily(LyXFont::FONT_FAMILY(i));
388 lyxerr.print("LyXFont::setLyXFamily: Unknown family `"+s+'\'');
393 // Set series according to lyx format string
394 LyXFont& LyXFont::setLyXSeries(LString const & ser)
399 while (s != LyXSeriesNames[i] && LyXSeriesNames[i] != "error") i++;
400 if (s == LyXSeriesNames[i]) {
401 setSeries(LyXFont::FONT_SERIES(i));
403 lyxerr.print("LyXFont::setLyXSeries: Unknown series `"+s+'\'');
408 // Set shape according to lyx format string
409 LyXFont& LyXFont::setLyXShape(LString const & sha)
414 while (s != LyXShapeNames[i] && LyXShapeNames[i] != "error") i++;
415 if (s == LyXShapeNames[i]) {
416 setShape(LyXFont::FONT_SHAPE(i));
418 lyxerr.print("LyXFont::setLyXShape: Unknown shape `"+s+'\'');
423 // Set size according to lyx format string
424 LyXFont& LyXFont::setLyXSize(LString const & siz)
429 while (s != LyXSizeNames[i] && LyXSizeNames[i] != "error") i++;
430 if (s == LyXSizeNames[i]) {
431 setSize(LyXFont::FONT_SIZE(i));
433 lyxerr.print("LyXFont::setLyXSize: Unknown size `"+s+'\'');
437 // Set size according to lyx format string
438 LyXFont::FONT_MISC_STATE LyXFont::setLyXMisc(LString const & siz)
443 while (s != LyXMiscNames[i] && LyXMiscNames[i] != "error") i++;
444 if (s == LyXMiscNames[i])
445 return FONT_MISC_STATE(i);
446 lyxerr.print("LyXFont::setLyXMisc: Unknown misc flag `"+s+'\'');
450 /// Sets color after LyX text format
451 LyXFont& LyXFont::setLyXColor(LString const & col)
456 while (s != LyXColorNames[i] && LyXColorNames[i] != "error") i++;
457 if (s == LyXColorNames[i]) {
458 setColor(LyXFont::FONT_COLOR(i));
460 lyxerr.print("LyXFont::setLyXColor: Unknown Color `"+s+'\'');
465 /// Sets size after GUI name
466 LyXFont& LyXFont::setGUISize(LString const & siz)
471 while (!lGUISizeNames[i].empty() &&
472 s != _(lGUISizeNames[i].c_str()))
474 if (s == _(lGUISizeNames[i].c_str())) {
475 setSize(LyXFont::FONT_SIZE(i));
477 lyxerr.print("LyXFont::setGUISize: Unknown Size `"+s+'\'');
482 // Returns size in latex format
483 LString LyXFont::latexSize() const
485 return LaTeXSizeNames[size()];
489 // Read a font definition from given file in lyx format
491 LyXFont & LyXFont::lyxRead(LyXLex & lex)
494 bool finished = false;
495 while (!finished && lex.IsOK() && !error) {
497 LString tok = lex.GetString();
502 } else if (tok == "endfont") {
504 } else if (tok == "family") {
506 LString tok = lex.GetString();
508 } else if (tok == "series") {
510 LString tok = lex.GetString();
512 } else if (tok == "shape") {
514 LString tok = lex.GetString();
516 } else if (tok == "size") {
518 LString tok = lex.GetString();
520 } else if (tok == "latex") {
522 LString tok = lex.GetString();
525 if (tok == "no_latex") {
527 } else if (tok == "latex") {
530 lex.printError("Illegal LaTeX type`$$Token'");
532 } else if (tok == "misc") {
534 LString tok = lex.GetString();
537 if (tok == "no_bar") {
539 } else if (tok == "no_emph") {
541 } else if (tok == "no_noun") {
543 } else if (tok == "emph") {
545 } else if (tok == "underbar") {
547 } else if (tok == "noun") {
550 lex.printError("Illegal misc type `$$Token´");
552 } else if (tok == "color") {
554 LString tok = lex.GetString();
557 lex.printError("Unknown tag `$$Token'");
565 /// Writes the changes from this font to orgfont in .lyx format in file
566 void LyXFont::lyxWriteChanges(LyXFont const & orgfont, FILE * file) const
569 if (orgfont.family() != family()) {
570 fprintf(file, "\\family %s \n",
571 LyXFamilyNames[family()].c_str());
573 if (orgfont.series() != series()) {
574 fprintf(file, "\\series %s \n",
575 LyXSeriesNames[series()].c_str());
577 if (orgfont.shape() != shape()) {
578 fprintf(file, "\\shape %s \n", LyXShapeNames[shape()].c_str());
580 if (orgfont.size() != size()) {
581 fprintf(file, "\\size %s \n", LyXSizeNames[size()].c_str());
583 if (orgfont.emph() != emph()) {
584 fprintf(file, "\\emph %s \n", LyXMiscNames[emph()].c_str());
586 if (orgfont.underbar() != underbar()) {
587 // This is only for backwards compatibility
588 switch (underbar()) {
589 case OFF: fprintf(file, "\\bar no \n"); break;
590 case ON: fprintf(file, "\\bar under \n"); break;
591 case TOGGLE: lyxerr.print("LyXFont::lyxWriteFontChanges: "
592 "TOGGLE should not appear here!");
594 case INHERIT: fprintf(file, "\\bar default \n"); break;
595 case IGNORE: lyxerr.print("LyXFont::lyxWriteFontChanges: "
596 "IGNORE should not appear here!");
600 if (orgfont.noun() != noun()) {
601 fprintf(file, "\\noun %s \n", LyXMiscNames[noun()].c_str());
603 if (orgfont.latex() != latex()) {
604 // This is only for backwards compatibility
606 case OFF: fprintf(file, "\\latex no_latex \n"); break;
607 case ON: fprintf(file, "\\latex latex \n"); break;
608 case TOGGLE: lyxerr.print("LyXFont::lyxWriteFontChanges: "
609 "TOGGLE should not appear here!");
611 case INHERIT: fprintf(file, "\\latex default \n"); break;
612 case IGNORE: lyxerr.print("LyXFont::lyxWriteFontChanges: "
613 "IGNORE should not appear here!");
617 if (orgfont.color() != color()) {
618 fprintf(file, "\\color %s\n", LyXColorNames[color()].c_str());
623 /// Writes the head of the LaTeX needed to impose this font
624 // Returns number of chars written.
625 int LyXFont::latexWriteStartChanges(FILE * file, LyXFont const & base) const
628 int count = latexWriteStartChanges(font, base);
629 fprintf(file, "%s", font.c_str());
634 /// Writes the head of the LaTeX needed to impose this font
635 // Returns number of chars written.
636 int LyXFont::latexWriteStartChanges(LString & file, LyXFont const & base) const
641 if (f.bits == inherit)
647 if (f.family() != INHERIT_FAMILY) {
649 file += LaTeXFamilyNames[f.family()];
651 count += LaTeXFamilyNames[f.family()].length() + 2;
652 env = true; //We have opened a new environment
654 if (f.series() != INHERIT_SERIES) {
656 file += LaTeXSeriesNames[f.series()];
658 count += LaTeXSeriesNames[f.series()].length() + 2;
659 env = true; //We have opened a new environment
661 if (f.shape() != INHERIT_SHAPE) {
663 file += LaTeXShapeNames[f.shape()];
665 count += LaTeXShapeNames[f.shape()].length() + 2;
666 env = true; //We have opened a new environment
668 if (f.color() != INHERIT_COLOR) {
669 file += "\\textcolor{";
670 file += LaTeXColorNames[f.color()];
672 count += LaTeXColorNames[f.color()].length() + 13;
673 env = true; //We have opened a new environment
675 if (f.emph() == ON) {
678 env = true; //We have opened a new environment
680 if (f.underbar() == ON) {
681 file += "\\underbar{";
683 env = true; //We have opened a new environment
685 // \noun{} is a LyX special macro
686 if (f.noun() == ON) {
689 env = true; //We have opened a new environment
691 if (f.size() != INHERIT_SIZE) {
692 // If we didn't open an environment above, we open one here
698 file += LaTeXSizeNames[f.size()];
700 count += LaTeXSizeNames[f.size()].length() + 2;
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(FILE * file, LyXFont const & base) const
712 int count = latexWriteEndChanges(ending, base);
713 fprintf(file, "%s", ending.c_str());
718 /// Writes ending block of LaTeX needed to close use of this font
719 // Returns number of chars written
720 // This one corresponds to latexWriteStartChanges(). (Asger)
721 int LyXFont::latexWriteEndChanges(LString & file, LyXFont const & base) const
723 LyXFont f = *this; // why do you need this?
724 f.reduce(base); // why isn't this just "reduce(base);" (Lgb)
725 // Because this function is const. Everything breaks if this
726 // method changes the font it represents. There is no speed penalty
727 // by using the temporary. (Asger)
729 if (f.bits == inherit)
734 if (f.family() != INHERIT_FAMILY) {
737 env = true; // Size change need not bother about closing env.
739 if (f.series() != INHERIT_SERIES) {
742 env = true; // Size change need not bother about closing env.
744 if (f.shape() != INHERIT_SHAPE) {
747 env = true; // Size change need not bother about closing env.
749 if (f.color() != INHERIT_COLOR) {
752 env = true; // Size change need not bother about closing env.
754 if (f.emph() == ON) {
757 env = true; // Size change need not bother about closing env.
759 if (f.underbar() == ON) {
762 env = true; // Size change need not bother about closing env.
764 if (f.noun() == ON) {
767 env = true; // Size change need not bother about closing env.
769 if (f.size() != INHERIT_SIZE) {
770 // We only have to close if only size changed
781 // Convert logical attributes to concrete shape attribute
782 LyXFont::FONT_SHAPE LyXFont::realShape() const
784 register FONT_SHAPE s = shape();
798 GC LyXFont::getGC() const
802 gc = ::getGC(gc_latex);
805 gc = ::getGC(gc_copy);
806 else if (color() == MATH)
807 gc = ::getGC(gc_math);
808 else if (color() == INSET)
809 gc = ::getGC(gc_foot);
811 gc = ::GetColorGC(color());
814 XSetFont(fl_display, gc, getXFontstruct()->fid);
819 XFontStruct* LyXFont::getXFontstruct() const
821 return fontloader.load(family(),series(),realShape(),size());
825 int LyXFont::maxAscent() const
827 return getXFontstruct()->ascent;
831 int LyXFont::maxDescent() const
833 return getXFontstruct()->descent;
837 int LyXFont::ascent(char c) const
839 XFontStruct *finfo = getXFontstruct();
841 && (unsigned int) c >= finfo->min_char_or_byte2
842 && (unsigned int) c <= finfo->max_char_or_byte2) {
843 unsigned int index = (unsigned int) c - finfo->min_char_or_byte2;
844 return finfo->per_char[index].ascent;
846 return finfo->ascent;
850 int LyXFont::descent(char c) const
852 XFontStruct *finfo = getXFontstruct();
854 && (unsigned int) c >= finfo->min_char_or_byte2
855 && (unsigned int) c <= finfo->max_char_or_byte2) {
856 unsigned int index = (unsigned int) c - finfo->min_char_or_byte2;
857 return finfo->per_char[index].descent;
859 return finfo->descent;
863 // Specialized after profiling. (Asger)
864 int LyXFont::width(char c) const
866 if (realShape() != LyXFont::SMALLCAPS_SHAPE){
867 return XTextWidth(getXFontstruct(), &c, 1);
869 return textWidth(&c, 1);
874 int LyXFont::textWidth(char const *s, int n) const
876 if (realShape() != LyXFont::SMALLCAPS_SHAPE){
877 return XTextWidth(getXFontstruct(), s, n);
879 // emulate smallcaps since X doesn't support this
880 unsigned int result = 0;
882 LyXFont smallfont = *this;
885 smallfont.setShape(LyXFont::UP_SHAPE);
886 for (int i=0; i < n; i++){
888 if (islower((unsigned char) c)){
889 c = toupper( (unsigned char) c );
890 result += XTextWidth(smallfont.getXFontstruct(), &c, 1);
892 result += XTextWidth(getXFontstruct(), &c, 1);
900 int LyXFont::stringWidth(LString const & s) const
902 if (s.empty()) return 0;
903 return textWidth(s.c_str(), s.length());
906 int LyXFont::signedStringWidth(LString const & s) const
908 if (s.empty()) return 0;
909 if (s.c_str()[0] == '-')
910 return -textWidth(s.c_str()+1, s.length()-1);
912 return textWidth(s.c_str(), s.length());
916 int LyXFont::drawText(char const* s, int n, Pixmap pm,
917 int baseline, int x) const
919 if (realShape() != LyXFont::SMALLCAPS_SHAPE) {
920 XDrawString(fl_display,
926 return XTextWidth(getXFontstruct(), s, n);
929 // emulate smallcaps since X doesn't support this
933 LyXFont smallfont = *this;
936 smallfont.setShape(LyXFont::UP_SHAPE);
937 for (i=0; i < n; i++){
939 if (islower((unsigned char) c)){
940 c = toupper((unsigned char) c);
941 XDrawString(fl_display,
946 x += XTextWidth(smallfont.getXFontstruct(), &c, 1);
949 XDrawString(fl_display,
954 x += XTextWidth(getXFontstruct(), &c, 1);
963 int LyXFont::drawString(LString const &s, Pixmap pm, int baseline, int x) const
965 return drawText(s.c_str(), s.length(), pm, baseline, x);
969 bool LyXFont::equalExceptLatex(LyXFont const &f) const
972 f1.setLatex(f.latex());