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.
16 #include "support/lstrings.h"
17 #include "xftFontLoader.h"
18 #include "font_metrics.h"
22 #include "codeConvert.h"
24 #include <boost/scoped_array.hpp>
32 inline XftFont * getXftFont(LyXFont const & f)
34 return fontLoader.load(f.family(), f.series(),
35 f.realShape(), f.size());
39 inline int XGlyphAscent(XGlyphInfo const & info)
45 inline int XGlyphDescent(XGlyphInfo const & info)
47 return info.height - info.y;
51 inline int XGlyphLbearing(XGlyphInfo const & info)
57 inline int XGlyphRbearing(XGlyphInfo const & info)
59 return -info.x + info.width;
63 inline int XGlyphLogWidth(XGlyphInfo const & info)
71 wchar_t wcs[2] = {0, 0};
74 mbstowcs(wcs, mbs, 2);
82 namespace font_metrics {
85 int maxAscent(LyXFont const & f)
87 XftFont * font = getXftFont(f);
92 int maxDescent(LyXFont const & f)
94 XftFont * font = getXftFont(f);
99 int ascent(wchar_t c,LyXFont const & f)
101 XftFont * font = getXftFont(f);
103 XftTextExtents32(getDisplay(), font,
104 wcsToXftChar32StrFast(&c),
107 return XGlyphAscent(glyph);
111 int ascent(char c, LyXFont const & f)
113 return ascent(C2WC(c), f);
117 int descent(wchar_t c,LyXFont const & f)
119 XftFont * font = getXftFont(f);
121 XftTextExtents32(getDisplay(), font,
122 wcsToXftChar32StrFast(&c),
125 return XGlyphDescent(glyph);
129 int descent(char c, LyXFont const & f)
131 return descent(C2WC(c), f);
135 int lbearing(wchar_t c,LyXFont const & f)
137 XftFont * font = getXftFont(f);
139 XftTextExtents32(getDisplay(), font,
140 wcsToXftChar32StrFast(&c),
143 return XGlyphLbearing(glyph);
147 int rbearing(wchar_t c,LyXFont const & f)
149 XftFont * font = getXftFont(f);
151 XftTextExtents32(getDisplay(), font,
152 wcsToXftChar32StrFast(&c),
155 return XGlyphRbearing(glyph);
159 int lbearing(char c, LyXFont const & f)
161 return lbearing(C2WC(c), f);
165 int rbearing(char c, LyXFont const & f)
167 return rbearing(C2WC(c), f);
171 int width(wchar_t const * s, size_t n, LyXFont const & f)
173 XftFont * font = getXftFont(f);
175 if (f.realShape() != LyXFont::SMALLCAPS_SHAPE){
176 XftTextExtents32(getDisplay(), font,
177 const_cast<XftChar32*>(
178 wcsToXftChar32StrFast(s)),
181 return XGlyphLogWidth(glyph);
184 LyXFont smallfont(f);
185 smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE);
186 XftFont * fontS = getXftFont(smallfont);
187 for (size_t i = 0; i < n; ++i) {
188 wchar_t wc = lyx::support::uppercase(s[i]);
190 XftTextExtents32(getDisplay(), fontS,
191 wcsToXftChar32StrFast(&wc),
194 result += XGlyphLogWidth(glyph);
196 XftTextExtents32(getDisplay(), font,
197 wcsToXftChar32StrFast(&wc),
200 result += XGlyphLogWidth(glyph);
208 int width(wchar_t c,LyXFont const & f)
210 return width(&c, 1, f);
214 int width(char const * s, size_t n,LyXFont const & f)
216 boost::scoped_array<wchar_t> wcs(new wchar_t[n]);
218 if (fontLoader.isSpecial(f)) {
219 unsigned char const * us =
220 reinterpret_cast<unsigned char const *>(s);
222 std::copy(us, us + n, wcs.get());
224 len = mbstowcs(wcs.get(), s, n);
225 return width(wcs.get(), len, f);
229 int signedWidth(string const & s, LyXFont const & f)
233 boost::scoped_array<wchar_t> wcs(new wchar_t[s.length() + 1]);
234 int len = mbstowcs(wcs.get(), s.c_str(), s.length());
236 return width(wcs.get() + 1, len - 1, f);
238 return width(wcs.get(), len, f);
242 void rectText(string const & str, LyXFont const & font,
247 static int const d = 2;
248 width = font_metrics::width(str, font) + d * 2 + 2;
249 ascent = font_metrics::maxAscent(font) + d;
250 descent = font_metrics::maxDescent(font) + d;
254 void buttonText(string const & str, LyXFont const & font,
259 static int const d = 3;
261 width = font_metrics::width(str, font) + d * 2 + 2;
262 ascent = font_metrics::maxAscent(font) + d;
263 descent = font_metrics::maxDescent(font) + d;
267 } // namespace font_metrics