6 #include "FontLoader.h"
12 XFontStruct * getXFontstruct(LyXFont const & f)
14 return fontloader.load(f.family(), f.series(),
15 f.realShape(), f.size());
20 XID getFontID(LyXFont const & f)
22 return getXFontstruct(f)->fid;
24 // } // end of anon namespace
26 int lyxfont::maxAscent(LyXFont const & f)
28 return getXFontstruct(f)->ascent;
32 int lyxfont::maxDescent(LyXFont const & f)
34 return getXFontstruct(f)->descent;
38 int lyxfont::ascent(char c, LyXFont const & f)
40 XFontStruct * finfo = getXFontstruct(f);
41 unsigned int uc = static_cast<unsigned char>(c);
43 && uc >= finfo->min_char_or_byte2
44 && uc <= finfo->max_char_or_byte2)
45 return finfo->per_char[uc - finfo->min_char_or_byte2].ascent;
51 int lyxfont::descent(char c, LyXFont const & f)
53 XFontStruct * finfo = getXFontstruct(f);
54 unsigned int uc = static_cast<unsigned char>(c);
56 && uc >= finfo->min_char_or_byte2
57 && uc <= finfo->max_char_or_byte2)
58 return finfo->per_char[uc - finfo->min_char_or_byte2].descent;
60 return finfo->descent;
64 int lyxfont::lbearing(char c, LyXFont const & f)
66 XFontStruct * finfo = getXFontstruct(f);
67 unsigned int uc = static_cast<unsigned char>(c);
69 && uc >= finfo->min_char_or_byte2
70 && uc <= finfo->max_char_or_byte2)
71 return finfo->per_char[uc - finfo->min_char_or_byte2].lbearing;
77 int lyxfont::rbearing(char c, LyXFont const & f)
79 XFontStruct * finfo = getXFontstruct(f);
80 unsigned int uc = static_cast<unsigned char>(c);
82 && uc >= finfo->min_char_or_byte2
83 && uc <= finfo->max_char_or_byte2)
84 return finfo->per_char[uc - finfo->min_char_or_byte2].rbearing;
90 int lyxfont::width(char const * s, int n, LyXFont const & f)
95 if (lyxrc.font_norm_type == LyXRC::ISO_10646_1) {
96 XChar2b * xs = new XChar2b[n];
97 Encoding const * encoding = f.language()->encoding();
98 LyXFont const * font = &f;
99 if (f.family() == LyXFont::SYMBOL_FAMILY) {
100 #ifdef USE_UNICODE_FOR_SYMBOLS
102 font2.setFamily(LyXFont::ROMAN_FAMILY);
103 font2.setShape(LyXFont::UP_SHAPE);
106 encoding = &symbol_encoding;
108 for (int i = 0; i < n; ++i) {
109 Uchar c = encoding->ucs(s[i]);
110 xs[i].byte1 = c >> 8;
111 xs[i].byte2 = c & 0xff;
113 int result = width(xs, n, *font);
118 if (f.realShape() != LyXFont::SMALLCAPS_SHAPE) {
119 return ::XTextWidth(getXFontstruct(f), s, n);
121 // emulate smallcaps since X doesn't support this
122 unsigned int result = 0;
124 LyXFont smallfont(f);
125 smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE);
126 for (int i = 0; i < n; ++i) {
128 // when islower is a macro, the cast is needed (JMarc)
129 if (islower(static_cast<unsigned char>(c))) {
131 result += ::XTextWidth(getXFontstruct(smallfont), &c, 1);
133 result += ::XTextWidth(getXFontstruct(f), &c, 1);
141 int lyxfont::signedWidth(string const & s, LyXFont const & f)
143 if (s.empty()) return 0;
144 if (s.c_str()[0] == '-')
145 return -width(s.c_str() + 1, s.length() - 1, f);
147 return width(s.c_str(), s.length(), f);
151 int lyxfont::width(XChar2b const * s, int n, LyXFont const & f)
156 if (f.realShape() != LyXFont::SMALLCAPS_SHAPE) {
157 return ::XTextWidth16(getXFontstruct(f), s, n);
159 // emulate smallcaps since X doesn't support this
160 unsigned int result = 0;
161 static XChar2b c = {0, 0};
162 LyXFont smallfont(f);
163 smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE);
164 for (int i = 0; i < n; ++i) {
165 if (s[i].byte1 == 0 && islower(s[i].byte2)) {
166 c.byte2 = toupper(s[i].byte2);
167 result += ::XTextWidth16(getXFontstruct(smallfont), &c, 1);
169 result += ::XTextWidth16(getXFontstruct(f), &s[i], 1);
176 int lyxfont::XTextWidth(LyXFont const & f, char * str, int count)
178 return ::XTextWidth(getXFontstruct(f), str, count);
182 int lyxfont::XTextWidth16(LyXFont const & f, XChar2b * str, int count)
184 return ::XTextWidth16(getXFontstruct(f), str, count);
188 void lyxfont::XSetFont(Display * display, GC gc, LyXFont const & f)
190 ::XSetFont(display, gc, getFontID(f));
193 //} // end of namespace font
194 //} // end of namespace lyx