2 * \file xfont_metrics.C
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
9 * Full author contact details are available in file CREDITS
15 #pragma implementation "frontends/font_metrics.h"
16 #pragma implementation "frontends/xfont_metrics.h"
19 #include "support/lstrings.h"
20 #include "xfont_metrics.h"
21 #include "xfont_loader.h"
26 #include <boost/scoped_array.hpp>
31 XFontStruct * getXFontstruct(LyXFont const & f)
33 return fontloader.load
34 (f.family(), f.series(),
35 f.realShape(), f.size());
40 XID getFontID(LyXFont const & f)
42 return getXFontstruct(f)->fid;
48 namespace font_metrics {
50 int maxAscent(LyXFont const & f)
52 return getXFontstruct(f)->ascent;
56 int maxDescent(LyXFont const & f)
58 return getXFontstruct(f)->descent;
62 int ascent(char c, LyXFont const & f)
64 XFontStruct * finfo = getXFontstruct(f);
65 unsigned int uc = static_cast<unsigned char>(c);
67 && uc >= finfo->min_char_or_byte2
68 && uc <= finfo->max_char_or_byte2+256*finfo->max_byte1)
69 return finfo->per_char[uc - finfo->min_char_or_byte2].ascent;
75 int descent(char c, LyXFont const & f)
77 XFontStruct * finfo = getXFontstruct(f);
78 unsigned int uc = static_cast<unsigned char>(c);
80 && uc >= finfo->min_char_or_byte2
81 && uc <= finfo->max_char_or_byte2+256*finfo->max_byte1)
82 return finfo->per_char[uc - finfo->min_char_or_byte2].descent;
84 return finfo->descent;
88 int lbearing(char c, LyXFont const & f)
90 XFontStruct * finfo = getXFontstruct(f);
91 unsigned int uc = static_cast<unsigned char>(c);
93 && uc >= finfo->min_char_or_byte2
94 && uc <= finfo->max_char_or_byte2+256*finfo->max_byte1)
95 return finfo->per_char[uc - finfo->min_char_or_byte2].lbearing;
101 int rbearing(char c, LyXFont const & f)
103 XFontStruct * finfo = getXFontstruct(f);
104 unsigned int uc = static_cast<unsigned char>(c);
106 && uc >= finfo->min_char_or_byte2
107 && uc <= finfo->max_char_or_byte2+256*finfo->max_byte1)
108 return finfo->per_char[uc - finfo->min_char_or_byte2].rbearing;
114 int width(char const * s, size_t n, LyXFont const & f)
119 if (lyxrc.font_norm_type == LyXRC::ISO_10646_1) {
120 boost::scoped_array<XChar2b> xs(new XChar2b[n]);
121 Encoding const * encoding = f.language()->encoding();
123 if (f.isSymbolFont()) {
124 #ifdef USE_UNICODE_FOR_SYMBOLS
125 font.setFamily(LyXFont::ROMAN_FAMILY);
126 font.setShape(LyXFont::UP_SHAPE);
128 encoding = encodings.symbol_encoding();
130 for (size_t i = 0; i < n; ++i) {
131 Uchar c = encoding->ucs(s[i]);
132 xs[i].byte1 = c >> 8;
133 xs[i].byte2 = c & 0xff;
135 int result = xfont_metrics::width(xs.get(), n, font);
139 if (f.realShape() != LyXFont::SMALLCAPS_SHAPE) {
140 return ::XTextWidth(getXFontstruct(f), s, n);
142 // emulate smallcaps since X doesn't support this
144 LyXFont smallfont(f);
145 smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE);
146 for (size_t i = 0; i < n; ++i) {
147 char const c = uppercase(s[i]);
149 result += ::XTextWidth(getXFontstruct(smallfont), &c, 1);
151 result += ::XTextWidth(getXFontstruct(f), &c, 1);
159 int signedWidth(string const & s, LyXFont const & f)
164 return -width(s.substr(1, s.length() - 1), f);
170 void rectText(string const & str, LyXFont const & font,
175 static int const d = 2;
176 width = font_metrics::width(str, font) + d * 2 + 2;
177 ascent = font_metrics::maxAscent(font) + d;
178 descent = font_metrics::maxDescent(font) + d;
183 void buttonText(string const & str, LyXFont const & font,
188 static int const d = 3;
190 width = font_metrics::width(str, font) + d * 2 + 2;
191 ascent = font_metrics::maxAscent(font) + d;
192 descent = font_metrics::maxDescent(font) + d;
195 } // namespace font_metrics
197 namespace xfont_metrics {
199 int width(XChar2b const * s, int n, LyXFont const & f)
204 if (f.realShape() != LyXFont::SMALLCAPS_SHAPE) {
205 return ::XTextWidth16(getXFontstruct(f), s, n);
207 // emulate smallcaps since X doesn't support this
210 LyXFont smallfont(f);
211 smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE);
212 for (int i = 0; i < n; ++i) {
216 c.byte1 = s[i].byte1;
217 c.byte2 = uppercase(s[i].byte2);
219 if (c.byte2 != s[i].byte2) {
220 result += ::XTextWidth16(getXFontstruct(smallfont), &c, 1);
222 result += ::XTextWidth16(getXFontstruct(f), &s[i], 1);
230 int XTextWidth(LyXFont const & f, char const * str, int count)
232 return ::XTextWidth(getXFontstruct(f), str, count);
236 int XTextWidth16(LyXFont const & f, XChar2b const * str, int count)
238 return ::XTextWidth16(getXFontstruct(f), str, count);
242 /// hmm, not a metric !
243 void XSetFont(Display * display, GC gc, LyXFont const & f)
245 ::XSetFont(display, gc, getFontID(f));
248 } // namespace xfont_metrics