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 * enc = f.language()->encoding();
98 for (int i = 0; i < n; ++i) {
99 Uchar c = enc->ucs(s[i]);
100 xs[i].byte1 = c >> 8;
101 xs[i].byte2 = c & 0xff;
103 int result = width(xs, n, f);
108 if (f.realShape() != LyXFont::SMALLCAPS_SHAPE) {
109 return ::XTextWidth(getXFontstruct(f), s, n);
111 // emulate smallcaps since X doesn't support this
112 unsigned int result = 0;
114 LyXFont smallfont(f);
115 smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE);
116 for (int i = 0; i < n; ++i) {
118 // when islower is a macro, the cast is needed (JMarc)
119 if (islower(static_cast<unsigned char>(c))) {
121 result += ::XTextWidth(getXFontstruct(smallfont), &c, 1);
123 result += ::XTextWidth(getXFontstruct(f), &c, 1);
131 int lyxfont::signedWidth(string const & s, LyXFont const & f)
133 if (s.empty()) return 0;
134 if (s.c_str()[0] == '-')
135 return -width(s.c_str() + 1, s.length() - 1, f);
137 return width(s.c_str(), s.length(), f);
141 int lyxfont::width(XChar2b const * s, int n, LyXFont const & f)
146 if (f.realShape() != LyXFont::SMALLCAPS_SHAPE) {
147 return ::XTextWidth16(getXFontstruct(f), s, n);
149 // emulate smallcaps since X doesn't support this
150 unsigned int result = 0;
151 static XChar2b c = {0, 0};
152 LyXFont smallfont(f);
153 smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE);
154 for (int i = 0; i < n; ++i) {
155 if (s[i].byte1 == 0 && islower(s[i].byte2)) {
156 c.byte2 = toupper(s[i].byte2);
157 result += ::XTextWidth16(getXFontstruct(smallfont), &c, 1);
159 result += ::XTextWidth16(getXFontstruct(f), &s[i], 1);
166 int lyxfont::XTextWidth(LyXFont const & f, char * str, int count)
168 return ::XTextWidth(getXFontstruct(f), str, count);
172 int lyxfont::XTextWidth16(LyXFont const & f, XChar2b * str, int count)
174 return ::XTextWidth16(getXFontstruct(f), str, count);
178 void lyxfont::XSetFont(Display * display, GC gc, LyXFont const & f)
180 ::XSetFont(display, gc, getFontID(f));
183 //} // end of namespace font
184 //} // end of namespace lyx