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.
13 // Too hard to make concept checks work with this file
14 #ifdef _GLIBCXX_CONCEPT_CHECKS
15 #undef _GLIBCXX_CONCEPT_CHECKS
17 #ifdef _GLIBCPP_CONCEPT_CHECKS
18 #undef _GLIBCPP_CONCEPT_CHECKS
22 #include "xftFontLoader.h"
23 #include "font_metrics.h"
27 #include "codeConvert.h"
29 #include "support/lstrings.h"
33 #include <boost/scoped_array.hpp>
43 inline XftFont * getXftFont(LyXFont const & f)
45 return fontLoader.load(f.family(), f.series(),
46 f.realShape(), f.size());
50 inline int XGlyphAscent(XGlyphInfo const & info)
56 inline int XGlyphDescent(XGlyphInfo const & info)
58 return info.height - info.y;
62 inline int XGlyphLbearing(XGlyphInfo const & info)
68 inline int XGlyphRbearing(XGlyphInfo const & info)
70 return -info.x + info.width;
74 inline int XGlyphLogWidth(XGlyphInfo const & info)
82 wchar_t wcs[2] = {0, 0};
85 mbstowcs(wcs, mbs, 2);
93 namespace font_metrics {
96 int maxAscent(LyXFont const & f)
98 XftFont * font = getXftFont(f);
103 int maxDescent(LyXFont const & f)
105 XftFont * font = getXftFont(f);
106 return font->descent;
110 int ascent(wchar_t c,LyXFont const & f)
112 XftFont * font = getXftFont(f);
114 XftTextExtents32(getDisplay(), font,
115 wcsToXftChar32StrFast(&c),
118 return XGlyphAscent(glyph);
122 int ascent(char c, LyXFont const & f)
124 return ascent(C2WC(c), f);
128 int descent(wchar_t c,LyXFont const & f)
130 XftFont * font = getXftFont(f);
132 XftTextExtents32(getDisplay(), font,
133 wcsToXftChar32StrFast(&c),
136 return XGlyphDescent(glyph);
140 int descent(char c, LyXFont const & f)
142 return descent(C2WC(c), f);
146 int lbearing(wchar_t c,LyXFont const & f)
148 XftFont * font = getXftFont(f);
150 XftTextExtents32(getDisplay(), font,
151 wcsToXftChar32StrFast(&c),
154 return XGlyphLbearing(glyph);
158 int rbearing(wchar_t c,LyXFont const & f)
160 XftFont * font = getXftFont(f);
162 XftTextExtents32(getDisplay(), font,
163 wcsToXftChar32StrFast(&c),
166 return XGlyphRbearing(glyph);
170 int lbearing(char c, LyXFont const & f)
172 return lbearing(C2WC(c), f);
176 int rbearing(char c, LyXFont const & f)
178 return rbearing(C2WC(c), f);
182 int width(wchar_t const * s, size_t n, LyXFont const & f)
184 XftFont * font = getXftFont(f);
186 if (f.realShape() != LyXFont::SMALLCAPS_SHAPE){
187 XftTextExtents32(getDisplay(), font,
188 const_cast<XftChar32 *>(
189 wcsToXftChar32StrFast(s)),
192 return XGlyphLogWidth(glyph);
195 LyXFont smallfont(f);
196 smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE);
197 XftFont * fontS = getXftFont(smallfont);
198 for (size_t i = 0; i < n; ++i) {
199 wchar_t wc = lyx::support::uppercase(s[i]);
201 XftTextExtents32(getDisplay(), fontS,
202 wcsToXftChar32StrFast(&wc),
205 result += XGlyphLogWidth(glyph);
207 XftTextExtents32(getDisplay(), font,
208 wcsToXftChar32StrFast(&wc),
211 result += XGlyphLogWidth(glyph);
219 int width(wchar_t c,LyXFont const & f)
221 return width(&c, 1, f);
225 int width(char const * s, size_t n, LyXFont const & f)
227 boost::scoped_array<wchar_t> wcs(new wchar_t[n]);
229 if (fontLoader.isSpecial(f)) {
230 unsigned char const * us =
231 reinterpret_cast<unsigned char const *>(s);
233 std::copy(us, us + n, wcs.get());
235 len = mbstowcs(wcs.get(), s, n);
236 return width(wcs.get(), len, f);
240 int signedWidth(string const & s, LyXFont const & f)
244 boost::scoped_array<wchar_t> wcs(new wchar_t[s.length() + 1]);
245 int len = mbstowcs(wcs.get(), s.c_str(), s.length());
247 return width(wcs.get() + 1, len - 1, f);
249 return width(wcs.get(), len, f);
253 void rectText(string const & str, LyXFont const & font,
258 static int const d = 2;
259 width = font_metrics::width(str, font) + d * 2 + 2;
260 ascent = font_metrics::maxAscent(font) + d;
261 descent = font_metrics::maxDescent(font) + d;
265 void buttonText(string const & str, LyXFont const & font,
270 static int const d = 3;
272 width = font_metrics::width(str, font) + d * 2 + 2;
273 ascent = font_metrics::maxAscent(font) + d;
274 descent = font_metrics::maxDescent(font) + d;
278 } // namespace font_metrics