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"
28 #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 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(char c,LyXFont const & f)
101 XftFont * font = getXftFont(f);
103 XftTextExtents8(getDisplay(), font,
104 reinterpret_cast<XftChar8 *>(&c),
107 return XGlyphAscent(glyph);
111 int descent(char c,LyXFont const & f)
113 XftFont * font = getXftFont(f);
115 XftTextExtents8(getDisplay(), font,
116 reinterpret_cast<XftChar8 *>(&c),
119 return XGlyphDescent(glyph);
123 int lbearing(char c,LyXFont const & f)
125 XftFont * font = getXftFont(f);
127 XftTextExtents8(getDisplay(), font,
128 reinterpret_cast<XftChar8 *>(&c),
131 return XGlyphLbearing(glyph);
135 int rbearing(char c,LyXFont const & f)
137 XftFont * font = getXftFont(f);
139 XftTextExtents8(getDisplay(), font,
140 reinterpret_cast<XftChar8 *>(&c),
143 return XGlyphRbearing(glyph);
147 int width(char const * s, size_t n, LyXFont const & f)
149 XftFont * font = getXftFont(f);
151 if (f.realShape() != LyXFont::SMALLCAPS_SHAPE){
152 XftTextExtents8(getDisplay(), font,
153 reinterpret_cast<XftChar8 *>(const_cast<char *>(s)), n, &glyph);
154 return XGlyphLogWidth(glyph);
157 LyXFont smallfont(f);
158 smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE);
159 XftFont * fontS = getXftFont(smallfont);
160 for (size_t i = 0; i < n; ++i) {
161 char c = lyx::support::uppercase(s[i]);
163 XftTextExtents8(getDisplay(), fontS,
164 reinterpret_cast<XftChar8 *>(&c),
167 result += XGlyphLogWidth(glyph);
169 XftTextExtents8(getDisplay(), font,
170 reinterpret_cast<XftChar8 *>(&c),
173 result += XGlyphLogWidth(glyph);
181 int signedWidth(string const & s, LyXFont const & f)
186 return width(s.c_str() + 1, s.size() - 1, f);
188 return width(s.c_str(), s.size(), f);
192 void rectText(string const & str, LyXFont const & font,
197 static int const d = 2;
198 width = font_metrics::width(str, font) + d * 2 + 2;
199 ascent = font_metrics::maxAscent(font) + d;
200 descent = font_metrics::maxDescent(font) + d;
204 void buttonText(string const & str, LyXFont const & font,
209 static int const d = 3;
211 width = font_metrics::width(str, font) + d * 2 + 2;
212 ascent = font_metrics::maxAscent(font) + d;
213 descent = font_metrics::maxDescent(font) + d;
217 } // namespace font_metrics