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>
30 inline XftFont * getXftFont(LyXFont const & f)
32 return fontLoader.load(f.family(), f.series(),
33 f.realShape(), f.size());
37 inline int XGlyphAscent(XGlyphInfo const & info)
43 inline int XGlyphDescent(XGlyphInfo const & info)
45 return info.height - info.y;
49 inline int XGlyphLbearing(XGlyphInfo const & info)
55 inline int XGlyphRbearing(XGlyphInfo const & info)
57 return -info.x + info.width;
61 inline int XGlyphLogWidth(XGlyphInfo const & info)
69 wchar_t wcs[2] = {0, 0};
72 mbstowcs(wcs, mbs, 2);
80 namespace font_metrics {
83 int maxAscent(LyXFont const & f)
85 XftFont * font = getXftFont(f);
90 int maxDescent(LyXFont const & f)
92 XftFont * font = getXftFont(f);
97 int ascent(wchar_t c,LyXFont const & f)
99 XftFont * font = getXftFont(f);
101 XftTextExtents32(getDisplay(), font,
102 wcsToFcChar32StrFast(&c),
105 return XGlyphAscent(glyph);
109 int ascent(char c, LyXFont const & f)
111 return ascent(C2WC(c), f);
115 int descent(wchar_t c,LyXFont const & f)
117 XftFont * font = getXftFont(f);
119 XftTextExtents32(getDisplay(), font,
120 wcsToFcChar32StrFast(&c),
123 return XGlyphDescent(glyph);
127 int descent(char c, LyXFont const & f)
129 return descent(C2WC(c), f);
133 int lbearing(wchar_t c,LyXFont const & f)
135 XftFont * font = getXftFont(f);
137 XftTextExtents32(getDisplay(), font,
138 wcsToFcChar32StrFast(&c),
141 return XGlyphLbearing(glyph);
145 int rbearing(wchar_t c,LyXFont const & f)
147 XftFont * font = getXftFont(f);
149 XftTextExtents32(getDisplay(), font,
150 wcsToFcChar32StrFast(&c),
153 return XGlyphRbearing(glyph);
157 int lbearing(char c, LyXFont const & f)
159 return lbearing(C2WC(c), f);
163 int rbearing(char c, LyXFont const & f)
165 return rbearing(C2WC(c), f);
169 int width(wchar_t const * s, size_t n, LyXFont const & f)
171 XftFont * font = getXftFont(f);
173 if (f.realShape() != LyXFont::SMALLCAPS_SHAPE){
174 XftTextExtents32(getDisplay(), font,
175 wcsToFcChar32StrFast(s),
178 return XGlyphLogWidth(glyph);
181 LyXFont smallfont(f);
182 smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE);
183 XftFont * fontS = getXftFont(smallfont);
184 for (size_t i = 0; i < n; ++i) {
185 wchar_t wc = lyx::support::uppercase(s[i]);
187 XftTextExtents32(getDisplay(), fontS,
188 wcsToFcChar32StrFast(&wc),
191 result += XGlyphLogWidth(glyph);
193 XftTextExtents32(getDisplay(), font,
194 wcsToFcChar32StrFast(&wc),
197 result += XGlyphLogWidth(glyph);
205 int width(wchar_t c,LyXFont const & f)
207 return width(&c, 1, f);
211 int width(char const * s, size_t n,LyXFont const & f)
213 boost::scoped_array<wchar_t> wcs(new wchar_t[n]);
215 if (fontLoader.isSpecial(f)) {
216 unsigned char const * us =
217 reinterpret_cast<unsigned char const *>(s);
219 std::copy(us, us + n, wcs.get());
221 len = mbstowcs(wcs.get(), s, n);
222 return width(wcs.get(), len, f);
226 int signedWidth(string const & s, LyXFont const & f)
230 boost::scoped_array<wchar_t> wcs(new wchar_t[s.length() + 1]);
231 int len = mbstowcs(wcs.get(), s.c_str(), s.length());
233 return width(wcs.get() + 1, len - 1, f);
235 return width(wcs.get(), len, f);
239 void rectText(string const & str, LyXFont const & font,
244 static int const d = 2;
245 width = font_metrics::width(str, font) + d * 2 + 2;
246 ascent = font_metrics::maxAscent(font) + d;
247 descent = font_metrics::maxDescent(font) + d;
251 void buttonText(string const & str, LyXFont const & font,
256 static int const d = 3;
258 width = font_metrics::width(str, font) + d * 2 + 2;
259 ascent = font_metrics::maxAscent(font) + d;
260 descent = font_metrics::maxDescent(font) + d;
264 } // namespace font_metrics