4 #include "FontLoader.h"
11 XFontStruct * getXFontstruct(LyXFont const & f)
13 return fontloader.load(f.family(), f.series(),
14 f.realShape(), f.size());
19 XID getFontID(LyXFont const & f)
21 return getXFontstruct(f)->fid;
23 // } // end of anon namespace
25 int lyxfont::maxAscent(LyXFont const & f)
27 return getXFontstruct(f)->ascent;
31 int lyxfont::maxDescent(LyXFont const & f)
33 return getXFontstruct(f)->descent;
37 int lyxfont::ascent(char c, LyXFont const & f)
39 XFontStruct * finfo = getXFontstruct(f);
40 unsigned int uc = static_cast<unsigned char>(c);
42 && uc >= finfo->min_char_or_byte2
43 && uc <= finfo->max_char_or_byte2)
44 return finfo->per_char[uc - finfo->min_char_or_byte2].ascent;
50 int lyxfont::descent(char c, LyXFont const & f)
52 XFontStruct * finfo = getXFontstruct(f);
53 unsigned int uc = static_cast<unsigned char>(c);
55 && uc >= finfo->min_char_or_byte2
56 && uc <= finfo->max_char_or_byte2)
57 return finfo->per_char[uc - finfo->min_char_or_byte2].descent;
59 return finfo->descent;
63 int lyxfont::lbearing(char c, LyXFont const & f)
65 XFontStruct * finfo = getXFontstruct(f);
66 unsigned int uc = static_cast<unsigned char>(c);
68 && uc >= finfo->min_char_or_byte2
69 && uc <= finfo->max_char_or_byte2)
70 return finfo->per_char[uc - finfo->min_char_or_byte2].lbearing;
76 int lyxfont::rbearing(char c, LyXFont const & f)
78 XFontStruct * finfo = getXFontstruct(f);
79 unsigned int uc = static_cast<unsigned char>(c);
81 && uc >= finfo->min_char_or_byte2
82 && uc <= finfo->max_char_or_byte2)
83 return finfo->per_char[uc - finfo->min_char_or_byte2].rbearing;
89 int lyxfont::width(char c, LyXFont const & f)
91 if (f.realShape() != LyXFont::SMALLCAPS_SHAPE) {
92 return lyxrc.use_gui ? ::XTextWidth(getXFontstruct(f), &c, 1)
96 smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE);
97 if (islower(static_cast<unsigned char>(c))) {
99 return ::XTextWidth(getXFontstruct(smallfont), &c, 1);
101 return ::XTextWidth(getXFontstruct(f), &c, 1);
107 int lyxfont::width(char const * s, int n, LyXFont const & f)
112 if (f.realShape() != LyXFont::SMALLCAPS_SHAPE) {
113 return ::XTextWidth(getXFontstruct(f), s, n);
115 // emulate smallcaps since X doesn't support this
116 unsigned int result = 0;
118 LyXFont smallfont(f);
119 smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE);
120 for (int i = 0; i < n; ++i) {
122 // when islower is a macro, the cast is needed (JMarc)
123 if (islower(static_cast<unsigned char>(c))) {
125 result += ::XTextWidth(getXFontstruct(smallfont), &c, 1);
127 result += ::XTextWidth(getXFontstruct(f), &c, 1);
135 int lyxfont::signedWidth(string const & s, LyXFont const & f)
137 if (s.empty()) return 0;
138 if (s.c_str()[0] == '-')
139 return -width(s.c_str() + 1, s.length() - 1, f);
141 return width(s.c_str(), s.length(), f);
145 int lyxfont::XTextWidth(LyXFont const & f, char * str, int count)
147 return ::XTextWidth(getXFontstruct(f), str, count);
151 void lyxfont::XSetFont(Display * display, GC gc, LyXFont const & f)
153 ::XSetFont(display, gc, getFontID(f));
156 //} // end of namespace font
157 //} // end of namespace lyx