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 wcsToFcChar32StrFast(&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 wcsToFcChar32StrFast(&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 wcsToFcChar32StrFast(&c),
143 return XGlyphLbearing(glyph);
147 int rbearing(wchar_t c,LyXFont const & f)
149 XftFont * font = getXftFont(f);
151 XftTextExtents32(getDisplay(), font,
152 wcsToFcChar32StrFast(&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 wcsToFcChar32StrFast(s),
180 return XGlyphLogWidth(glyph);
183 LyXFont smallfont(f);
184 smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE);
185 XftFont * fontS = getXftFont(smallfont);
186 for (size_t i = 0; i < n; ++i) {
187 wchar_t wc = lyx::support::uppercase(s[i]);
189 XftTextExtents32(getDisplay(), fontS,
190 wcsToFcChar32StrFast(&wc),
193 result += XGlyphLogWidth(glyph);
195 XftTextExtents32(getDisplay(), font,
196 wcsToFcChar32StrFast(&wc),
199 result += XGlyphLogWidth(glyph);
207 int width(wchar_t c,LyXFont const & f)
209 return width(&c, 1, f);
213 int width(char const * s, size_t n,LyXFont const & f)
215 boost::scoped_array<wchar_t> wcs(new wchar_t[n]);
217 if (fontLoader.isSpecial(f)) {
218 unsigned char const * us =
219 reinterpret_cast<unsigned char const *>(s);
221 std::copy(us, us + n, wcs.get());
223 len = mbstowcs(wcs.get(), s, n);
224 return width(wcs.get(), len, f);
228 int signedWidth(string const & s, LyXFont const & f)
232 boost::scoped_array<wchar_t> wcs(new wchar_t[s.length() + 1]);
233 int len = mbstowcs(wcs.get(), s.c_str(), s.length());
235 return width(wcs.get() + 1, len - 1, f);
237 return width(wcs.get(), len, f);
241 void rectText(string const & str, LyXFont const & font,
246 static int const d = 2;
247 width = font_metrics::width(str, font) + d * 2 + 2;
248 ascent = font_metrics::maxAscent(font) + d;
249 descent = font_metrics::maxDescent(font) + d;
253 void buttonText(string const & str, LyXFont const & font,
258 static int const d = 3;
260 width = font_metrics::width(str, font) + d * 2 + 2;
261 ascent = font_metrics::maxAscent(font) + d;
262 descent = font_metrics::maxDescent(font) + d;
266 } // namespace font_metrics