2 * \file xfont_metrics.C
3 * Read the file COPYING
8 * Full author contact details are available in file CREDITS
14 #pragma implementation "frontends/font_metrics.h"
15 #pragma implementation "frontends/xfont_metrics.h"
18 #include "support/lstrings.h"
19 #include "xfont_metrics.h"
20 #include "xfont_loader.h"
25 #include <boost/scoped_array.hpp>
30 XFontStruct * getXFontstruct(LyXFont const & f)
32 return fontloader.load
33 (f.family(), f.series(),
34 f.realShape(), f.size());
39 XID getFontID(LyXFont const & f)
41 return getXFontstruct(f)->fid;
47 namespace font_metrics {
49 int maxAscent(LyXFont const & f)
51 return getXFontstruct(f)->ascent;
55 int maxDescent(LyXFont const & f)
57 return getXFontstruct(f)->descent;
61 int ascent(char c, LyXFont const & f)
63 XFontStruct * finfo = getXFontstruct(f);
64 unsigned int uc = static_cast<unsigned char>(c);
66 && uc >= finfo->min_char_or_byte2
67 && uc <= finfo->max_char_or_byte2+256*finfo->max_byte1)
68 return finfo->per_char[uc - finfo->min_char_or_byte2].ascent;
74 int descent(char c, LyXFont const & f)
76 XFontStruct * finfo = getXFontstruct(f);
77 unsigned int uc = static_cast<unsigned char>(c);
79 && uc >= finfo->min_char_or_byte2
80 && uc <= finfo->max_char_or_byte2+256*finfo->max_byte1)
81 return finfo->per_char[uc - finfo->min_char_or_byte2].descent;
83 return finfo->descent;
87 int lbearing(char c, LyXFont const & f)
89 XFontStruct * finfo = getXFontstruct(f);
90 unsigned int uc = static_cast<unsigned char>(c);
92 && uc >= finfo->min_char_or_byte2
93 && uc <= finfo->max_char_or_byte2+256*finfo->max_byte1)
94 return finfo->per_char[uc - finfo->min_char_or_byte2].lbearing;
100 int rbearing(char c, LyXFont const & f)
102 XFontStruct * finfo = getXFontstruct(f);
103 unsigned int uc = static_cast<unsigned char>(c);
105 && uc >= finfo->min_char_or_byte2
106 && uc <= finfo->max_char_or_byte2+256*finfo->max_byte1)
107 return finfo->per_char[uc - finfo->min_char_or_byte2].rbearing;
113 int width(char const * s, size_t n, LyXFont const & f)
118 if (lyxrc.font_norm_type == LyXRC::ISO_10646_1) {
119 boost::scoped_array<XChar2b> xs(new XChar2b[n]);
120 Encoding const * encoding = f.language()->encoding();
122 if (f.isSymbolFont()) {
123 #ifdef USE_UNICODE_FOR_SYMBOLS
124 font.setFamily(LyXFont::ROMAN_FAMILY);
125 font.setShape(LyXFont::UP_SHAPE);
127 encoding = encodings.symbol_encoding();
129 for (size_t i = 0; i < n; ++i) {
130 Uchar c = encoding->ucs(s[i]);
131 xs[i].byte1 = c >> 8;
132 xs[i].byte2 = c & 0xff;
134 int result = xfont_metrics::width(xs.get(), n, font);
138 if (f.realShape() != LyXFont::SMALLCAPS_SHAPE) {
139 return ::XTextWidth(getXFontstruct(f), s, n);
141 // emulate smallcaps since X doesn't support this
143 LyXFont smallfont(f);
144 smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE);
145 for (size_t i = 0; i < n; ++i) {
146 char const c = uppercase(s[i]);
148 result += ::XTextWidth(getXFontstruct(smallfont), &c, 1);
150 result += ::XTextWidth(getXFontstruct(f), &c, 1);
158 int signedWidth(string const & s, LyXFont const & f)
163 return -width(s.substr(1, s.length() - 1), f);
169 void rectText(string const & str, LyXFont const & font,
174 static int const d = 2;
175 width = font_metrics::width(str, font) + d * 2 + 2;
176 ascent = font_metrics::maxAscent(font) + d;
177 descent = font_metrics::maxDescent(font) + d;
182 void buttonText(string const & str, LyXFont const & font,
187 static int const d = 3;
189 width = font_metrics::width(str, font) + d * 2 + 2;
190 ascent = font_metrics::maxAscent(font) + d;
191 descent = font_metrics::maxDescent(font) + d;
194 } // namespace font_metrics
196 namespace xfont_metrics {
198 int width(XChar2b const * s, int n, LyXFont const & f)
203 if (f.realShape() != LyXFont::SMALLCAPS_SHAPE) {
204 return ::XTextWidth16(getXFontstruct(f), s, n);
206 // emulate smallcaps since X doesn't support this
209 LyXFont smallfont(f);
210 smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE);
211 for (int i = 0; i < n; ++i) {
215 c.byte1 = s[i].byte1;
216 c.byte2 = uppercase(s[i].byte2);
218 if (c.byte2 != s[i].byte2) {
219 result += ::XTextWidth16(getXFontstruct(smallfont), &c, 1);
221 result += ::XTextWidth16(getXFontstruct(f), &s[i], 1);
229 int XTextWidth(LyXFont const & f, char const * str, int count)
231 return ::XTextWidth(getXFontstruct(f), str, count);
235 int XTextWidth16(LyXFont const & f, XChar2b const * str, int count)
237 return ::XTextWidth16(getXFontstruct(f), str, count);
241 /// hmm, not a metric !
242 void XSetFont(Display * display, GC gc, LyXFont const & f)
244 ::XSetFont(display, gc, getFontID(f));
247 } // namespace xfont_metrics