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 "support/lstrings.h"
15 #include "xftFontLoader.h"
16 #include "font_metrics.h"
20 #include "codeConvert.h"
24 #include <boost/scoped_array.hpp>
34 inline XftFont * getXftFont(LyXFont const & f)
36 return fontLoader.load(f.family(), f.series(),
37 f.realShape(), f.size());
41 inline int XGlyphAscent(XGlyphInfo const & info)
47 inline int XGlyphDescent(XGlyphInfo const & info)
49 return info.height - info.y;
53 inline int XGlyphLbearing(XGlyphInfo const & info)
59 inline int XGlyphRbearing(XGlyphInfo const & info)
61 return -info.x + info.width;
65 inline int XGlyphLogWidth(XGlyphInfo const & info)
73 wchar_t wcs[2] = {0, 0};
76 mbstowcs(wcs, mbs, 2);
84 namespace font_metrics {
87 int maxAscent(LyXFont const & f)
89 XftFont * font = getXftFont(f);
94 int maxDescent(LyXFont const & f)
96 XftFont * font = getXftFont(f);
101 int ascent(wchar_t c,LyXFont const & f)
103 XftFont * font = getXftFont(f);
105 XftTextExtents32(getDisplay(), font,
106 wcsToXftChar32StrFast(&c),
109 return XGlyphAscent(glyph);
113 int ascent(char c, LyXFont const & f)
115 return ascent(C2WC(c), f);
119 int descent(wchar_t c,LyXFont const & f)
121 XftFont * font = getXftFont(f);
123 XftTextExtents32(getDisplay(), font,
124 wcsToXftChar32StrFast(&c),
127 return XGlyphDescent(glyph);
131 int descent(char c, LyXFont const & f)
133 return descent(C2WC(c), f);
137 int lbearing(wchar_t c,LyXFont const & f)
139 XftFont * font = getXftFont(f);
141 XftTextExtents32(getDisplay(), font,
142 wcsToXftChar32StrFast(&c),
145 return XGlyphLbearing(glyph);
149 int rbearing(wchar_t c,LyXFont const & f)
151 XftFont * font = getXftFont(f);
153 XftTextExtents32(getDisplay(), font,
154 wcsToXftChar32StrFast(&c),
157 return XGlyphRbearing(glyph);
161 int lbearing(char c, LyXFont const & f)
163 return lbearing(C2WC(c), f);
167 int rbearing(char c, LyXFont const & f)
169 return rbearing(C2WC(c), f);
173 int width(wchar_t const * s, size_t n, LyXFont const & f)
175 XftFont * font = getXftFont(f);
177 if (f.realShape() != LyXFont::SMALLCAPS_SHAPE){
178 XftTextExtents32(getDisplay(), font,
179 const_cast<XftChar32 *>(
180 wcsToXftChar32StrFast(s)),
183 return XGlyphLogWidth(glyph);
186 LyXFont smallfont(f);
187 smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE);
188 XftFont * fontS = getXftFont(smallfont);
189 for (size_t i = 0; i < n; ++i) {
190 wchar_t wc = lyx::support::uppercase(s[i]);
192 XftTextExtents32(getDisplay(), fontS,
193 wcsToXftChar32StrFast(&wc),
196 result += XGlyphLogWidth(glyph);
198 XftTextExtents32(getDisplay(), font,
199 wcsToXftChar32StrFast(&wc),
202 result += XGlyphLogWidth(glyph);
210 int width(wchar_t c,LyXFont const & f)
212 return width(&c, 1, f);
216 int width(char const * s, size_t n, LyXFont const & f)
218 boost::scoped_array<wchar_t> wcs(new wchar_t[n]);
220 if (fontLoader.isSpecial(f)) {
221 unsigned char const * us =
222 reinterpret_cast<unsigned char const *>(s);
224 std::copy(us, us + n, wcs.get());
226 len = mbstowcs(wcs.get(), s, n);
227 return width(wcs.get(), len, f);
231 int signedWidth(string const & s, LyXFont const & f)
235 boost::scoped_array<wchar_t> wcs(new wchar_t[s.length() + 1]);
236 int len = mbstowcs(wcs.get(), s.c_str(), s.length());
238 return width(wcs.get() + 1, len - 1, f);
240 return width(wcs.get(), len, f);
244 void rectText(string const & str, LyXFont const & font,
249 static int const d = 2;
250 width = font_metrics::width(str, font) + d * 2 + 2;
251 ascent = font_metrics::maxAscent(font) + d;
252 descent = font_metrics::maxDescent(font) + d;
256 void buttonText(string const & str, LyXFont const & font,
261 static int const d = 3;
263 width = font_metrics::width(str, font) + d * 2 + 2;
264 ascent = font_metrics::maxAscent(font) + d;
265 descent = font_metrics::maxDescent(font) + d;
269 } // namespace font_metrics