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 _GLIBCPP_CONCEPT_CHECKS
15 #undef _GLIBCPP_CONCEPT_CHECKS
19 #include "xftFontLoader.h"
20 #include "font_metrics.h"
24 #include "codeConvert.h"
26 #include "support/lstrings.h"
30 #include <boost/scoped_array.hpp>
40 inline XftFont * getXftFont(LyXFont const & f)
42 return fontLoader.load(f.family(), f.series(),
43 f.realShape(), f.size());
47 inline int XGlyphAscent(XGlyphInfo const & info)
53 inline int XGlyphDescent(XGlyphInfo const & info)
55 return info.height - info.y;
59 inline int XGlyphLbearing(XGlyphInfo const & info)
65 inline int XGlyphRbearing(XGlyphInfo const & info)
67 return -info.x + info.width;
71 inline int XGlyphLogWidth(XGlyphInfo const & info)
79 wchar_t wcs[2] = {0, 0};
82 mbstowcs(wcs, mbs, 2);
90 namespace font_metrics {
93 int maxAscent(LyXFont const & f)
95 XftFont * font = getXftFont(f);
100 int maxDescent(LyXFont const & f)
102 XftFont * font = getXftFont(f);
103 return font->descent;
107 int ascent(wchar_t c,LyXFont const & f)
109 XftFont * font = getXftFont(f);
111 XftTextExtents32(getDisplay(), font,
112 wcsToXftChar32StrFast(&c),
115 return XGlyphAscent(glyph);
119 int ascent(char c, LyXFont const & f)
121 return ascent(C2WC(c), f);
125 int descent(wchar_t c,LyXFont const & f)
127 XftFont * font = getXftFont(f);
129 XftTextExtents32(getDisplay(), font,
130 wcsToXftChar32StrFast(&c),
133 return XGlyphDescent(glyph);
137 int descent(char c, LyXFont const & f)
139 return descent(C2WC(c), f);
143 int lbearing(wchar_t c,LyXFont const & f)
145 XftFont * font = getXftFont(f);
147 XftTextExtents32(getDisplay(), font,
148 wcsToXftChar32StrFast(&c),
151 return XGlyphLbearing(glyph);
155 int rbearing(wchar_t c,LyXFont const & f)
157 XftFont * font = getXftFont(f);
159 XftTextExtents32(getDisplay(), font,
160 wcsToXftChar32StrFast(&c),
163 return XGlyphRbearing(glyph);
167 int lbearing(char c, LyXFont const & f)
169 return lbearing(C2WC(c), f);
173 int rbearing(char c, LyXFont const & f)
175 return rbearing(C2WC(c), f);
179 int width(wchar_t const * s, size_t n, LyXFont const & f)
181 XftFont * font = getXftFont(f);
183 if (f.realShape() != LyXFont::SMALLCAPS_SHAPE){
184 XftTextExtents32(getDisplay(), font,
185 const_cast<XftChar32 *>(
186 wcsToXftChar32StrFast(s)),
189 return XGlyphLogWidth(glyph);
192 LyXFont smallfont(f);
193 smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE);
194 XftFont * fontS = getXftFont(smallfont);
195 for (size_t i = 0; i < n; ++i) {
196 wchar_t wc = lyx::support::uppercase(s[i]);
198 XftTextExtents32(getDisplay(), fontS,
199 wcsToXftChar32StrFast(&wc),
202 result += XGlyphLogWidth(glyph);
204 XftTextExtents32(getDisplay(), font,
205 wcsToXftChar32StrFast(&wc),
208 result += XGlyphLogWidth(glyph);
216 int width(wchar_t c,LyXFont const & f)
218 return width(&c, 1, f);
222 int width(char const * s, size_t n, LyXFont const & f)
224 boost::scoped_array<wchar_t> wcs(new wchar_t[n]);
226 if (fontLoader.isSpecial(f)) {
227 unsigned char const * us =
228 reinterpret_cast<unsigned char const *>(s);
230 std::copy(us, us + n, wcs.get());
232 len = mbstowcs(wcs.get(), s, n);
233 return width(wcs.get(), len, f);
237 int signedWidth(string const & s, LyXFont const & f)
241 boost::scoped_array<wchar_t> wcs(new wchar_t[s.length() + 1]);
242 int len = mbstowcs(wcs.get(), s.c_str(), s.length());
244 return width(wcs.get() + 1, len - 1, f);
246 return width(wcs.get(), len, f);
250 void rectText(string const & str, LyXFont const & font,
255 static int const d = 2;
256 width = font_metrics::width(str, font) + d * 2 + 2;
257 ascent = font_metrics::maxAscent(font) + d;
258 descent = font_metrics::maxDescent(font) + d;
262 void buttonText(string const & str, LyXFont const & font,
267 static int const d = 3;
269 width = font_metrics::width(str, font) + d * 2 + 2;
270 ascent = font_metrics::maxAscent(font) + d;
271 descent = font_metrics::maxDescent(font) + d;
275 } // namespace font_metrics