2 * \file xfont_metrics.C
3 * Copyright 1995-2002 the LyX Team
4 * Read the file COPYING
7 * \author John Levon <moz@compsoc.man.ac.uk>
13 #pragma implementation "frontends/font_metrics.h"
14 #pragma implementation "frontends/xfont_metrics.h"
17 #include "support/lstrings.h"
18 #include "xfont_metrics.h"
19 #include "xfont_loader.h"
24 #include <boost/scoped_array.hpp>
29 XFontStruct * getXFontstruct(LyXFont const & f)
31 return fontloader.load
32 (f.family(), f.series(),
33 f.realShape(), f.size());
38 XID getFontID(LyXFont const & f)
40 return getXFontstruct(f)->fid;
46 namespace font_metrics {
48 int maxAscent(LyXFont const & f)
50 return getXFontstruct(f)->ascent;
54 int maxDescent(LyXFont const & f)
56 return getXFontstruct(f)->descent;
60 int ascent(char c, LyXFont const & f)
62 XFontStruct * finfo = getXFontstruct(f);
63 unsigned int uc = static_cast<unsigned char>(c);
65 && uc >= finfo->min_char_or_byte2
66 && uc <= finfo->max_char_or_byte2+256*finfo->max_byte1)
67 return finfo->per_char[uc - finfo->min_char_or_byte2].ascent;
73 int descent(char c, LyXFont const & f)
75 XFontStruct * finfo = getXFontstruct(f);
76 unsigned int uc = static_cast<unsigned char>(c);
78 && uc >= finfo->min_char_or_byte2
79 && uc <= finfo->max_char_or_byte2+256*finfo->max_byte1)
80 return finfo->per_char[uc - finfo->min_char_or_byte2].descent;
82 return finfo->descent;
86 int lbearing(char c, LyXFont const & f)
88 XFontStruct * finfo = getXFontstruct(f);
89 unsigned int uc = static_cast<unsigned char>(c);
91 && uc >= finfo->min_char_or_byte2
92 && uc <= finfo->max_char_or_byte2+256*finfo->max_byte1)
93 return finfo->per_char[uc - finfo->min_char_or_byte2].lbearing;
99 int rbearing(char c, LyXFont const & f)
101 XFontStruct * finfo = getXFontstruct(f);
102 unsigned int uc = static_cast<unsigned char>(c);
104 && uc >= finfo->min_char_or_byte2
105 && uc <= finfo->max_char_or_byte2+256*finfo->max_byte1)
106 return finfo->per_char[uc - finfo->min_char_or_byte2].rbearing;
112 int width(char const * s, size_t n, LyXFont const & f)
117 if (lyxrc.font_norm_type == LyXRC::ISO_10646_1) {
118 boost::scoped_array<XChar2b> xs(new XChar2b[n]);
119 Encoding const * encoding = f.language()->encoding();
121 if (f.isSymbolFont()) {
122 #ifdef USE_UNICODE_FOR_SYMBOLS
123 font.setFamily(LyXFont::ROMAN_FAMILY);
124 font.setShape(LyXFont::UP_SHAPE);
126 encoding = encodings.symbol_encoding();
128 for (size_t i = 0; i < n; ++i) {
129 Uchar c = encoding->ucs(s[i]);
130 xs[i].byte1 = c >> 8;
131 xs[i].byte2 = c & 0xff;
133 int result = xfont_metrics::width(xs.get(), n, font);
137 if (f.realShape() != LyXFont::SMALLCAPS_SHAPE) {
138 return ::XTextWidth(getXFontstruct(f), s, n);
140 // emulate smallcaps since X doesn't support this
142 LyXFont smallfont(f);
143 smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE);
144 for (size_t i = 0; i < n; ++i) {
145 char const c = uppercase(s[i]);
147 result += ::XTextWidth(getXFontstruct(smallfont), &c, 1);
149 result += ::XTextWidth(getXFontstruct(f), &c, 1);
157 int signedWidth(string const & s, LyXFont const & f)
162 return -width(s.substr(1, s.length() - 1), f);
168 void rectText(string const & str, LyXFont const & font,
173 static int const d = 2;
174 width = font_metrics::width(str, font) + d * 2 + 2;
175 ascent = font_metrics::maxAscent(font) + d;
176 descent = font_metrics::maxDescent(font) + d;
181 void buttonText(string const & str, LyXFont const & font,
186 static int const d = 3;
188 width = font_metrics::width(str, font) + d * 2 + 2;
189 ascent = font_metrics::maxAscent(font) + d;
190 descent = font_metrics::maxDescent(font) + d;
193 } // namespace font_metrics
195 namespace xfont_metrics {
197 int width(XChar2b const * s, int n, LyXFont const & f)
202 if (f.realShape() != LyXFont::SMALLCAPS_SHAPE) {
203 return ::XTextWidth16(getXFontstruct(f), s, n);
205 // emulate smallcaps since X doesn't support this
208 LyXFont smallfont(f);
209 smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE);
210 for (int i = 0; i < n; ++i) {
214 c.byte1 = s[i].byte1;
215 c.byte2 = uppercase(s[i].byte2);
217 if (c.byte2 != s[i].byte2) {
218 result += ::XTextWidth16(getXFontstruct(smallfont), &c, 1);
220 result += ::XTextWidth16(getXFontstruct(f), &s[i], 1);
228 int XTextWidth(LyXFont const & f, char const * str, int count)
230 return ::XTextWidth(getXFontstruct(f), str, count);
234 int XTextWidth16(LyXFont const & f, XChar2b const * str, int count)
236 return ::XTextWidth16(getXFontstruct(f), str, count);
240 /// hmm, not a metric !
241 void XSetFont(Display * display, GC gc, LyXFont const & f)
243 ::XSetFont(display, gc, getFontID(f));
246 } // namespace xfont_metrics