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.
8 * Full author contact details are available in file CREDITS.
14 #include "xftFontLoader.h"
15 #include "font_metrics.h"
19 #include "codeConvert.h"
21 #include "support/lstrings.h"
25 #include <boost/scoped_array.hpp>
35 inline XftFont * getXftFont(LyXFont const & f)
37 return fontLoader.load(f.family(), f.series(),
38 f.realShape(), f.size());
42 inline int XGlyphAscent(XGlyphInfo const & info)
48 inline int XGlyphDescent(XGlyphInfo const & info)
50 return info.height - info.y;
54 inline int XGlyphLbearing(XGlyphInfo const & info)
60 inline int XGlyphRbearing(XGlyphInfo const & info)
62 return -info.x + info.width;
66 inline int XGlyphLogWidth(XGlyphInfo const & info)
74 wchar_t wcs[2] = {0, 0};
77 mbstowcs(wcs, mbs, 2);
85 namespace font_metrics {
88 int maxAscent(LyXFont const & f)
90 XftFont * font = getXftFont(f);
95 int maxDescent(LyXFont const & f)
97 XftFont * font = getXftFont(f);
102 int ascent(wchar_t c,LyXFont const & f)
104 XftFont * font = getXftFont(f);
106 XftTextExtents32(getDisplay(), font,
107 wcsToXftChar32StrFast(&c),
110 return XGlyphAscent(glyph);
114 int ascent(char c, LyXFont const & f)
116 return ascent(C2WC(c), f);
120 int descent(wchar_t c,LyXFont const & f)
122 XftFont * font = getXftFont(f);
124 XftTextExtents32(getDisplay(), font,
125 wcsToXftChar32StrFast(&c),
128 return XGlyphDescent(glyph);
132 int descent(char c, LyXFont const & f)
134 return descent(C2WC(c), f);
138 int lbearing(wchar_t c,LyXFont const & f)
140 XftFont * font = getXftFont(f);
142 XftTextExtents32(getDisplay(), font,
143 wcsToXftChar32StrFast(&c),
146 return XGlyphLbearing(glyph);
150 int rbearing(wchar_t c,LyXFont const & f)
152 XftFont * font = getXftFont(f);
154 XftTextExtents32(getDisplay(), font,
155 wcsToXftChar32StrFast(&c),
158 return XGlyphRbearing(glyph);
162 int lbearing(char c, LyXFont const & f)
164 return lbearing(C2WC(c), f);
168 int rbearing(char c, LyXFont const & f)
170 return rbearing(C2WC(c), f);
174 int width(wchar_t const * s, size_t n, LyXFont const & f)
176 XftFont * font = getXftFont(f);
178 if (f.realShape() != LyXFont::SMALLCAPS_SHAPE){
179 XftTextExtents32(getDisplay(), font,
180 const_cast<XftChar32 *>(
181 wcsToXftChar32StrFast(s)),
184 return XGlyphLogWidth(glyph);
187 LyXFont smallfont(f);
188 smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE);
189 XftFont * fontS = getXftFont(smallfont);
190 for (size_t i = 0; i < n; ++i) {
191 wchar_t wc = lyx::support::uppercase(s[i]);
193 XftTextExtents32(getDisplay(), fontS,
194 wcsToXftChar32StrFast(&wc),
197 result += XGlyphLogWidth(glyph);
199 XftTextExtents32(getDisplay(), font,
200 wcsToXftChar32StrFast(&wc),
203 result += XGlyphLogWidth(glyph);
211 int width(wchar_t c,LyXFont const & f)
213 return width(&c, 1, f);
217 int width(char const * s, size_t n, LyXFont const & f)
219 boost::scoped_array<wchar_t> wcs(new wchar_t[n]);
221 if (fontLoader.isSpecial(f)) {
222 unsigned char const * us =
223 reinterpret_cast<unsigned char const *>(s);
225 std::copy(us, us + n, wcs.get());
227 len = mbstowcs(wcs.get(), s, n);
228 return width(wcs.get(), len, f);
232 int signedWidth(string const & s, LyXFont const & f)
236 boost::scoped_array<wchar_t> wcs(new wchar_t[s.length() + 1]);
237 int len = mbstowcs(wcs.get(), s.c_str(), s.length());
239 return width(wcs.get() + 1, len - 1, f);
241 return width(wcs.get(), len, f);
245 void rectText(string const & str, LyXFont const & font,
250 static int const d = 2;
251 width = font_metrics::width(str, font) + d * 2 + 2;
252 ascent = font_metrics::maxAscent(font) + d;
253 descent = font_metrics::maxDescent(font) + d;
257 void buttonText(string const & str, LyXFont const & font,
262 static int const d = 3;
264 width = font_metrics::width(str, font) + d * 2 + 2;
265 ascent = font_metrics::maxAscent(font) + d;
266 descent = font_metrics::maxDescent(font) + d;
270 } // namespace font_metrics