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.
9 * Full author contact details are available in file CREDITS.
14 #include "xfont_metrics.h"
15 #include "font_metrics.h"
16 #include "xfont_loader.h"
22 #include "frontends/lyx_gui.h"
24 #include "support/lstrings.h"
26 #include <boost/scoped_array.hpp>
28 using namespace lyx::support;
34 XFontStruct * getXFontstruct(LyXFont const & f)
36 return fontloader.load
37 (f.family(), f.series(),
38 f.realShape(), f.size());
43 XID getFontID(LyXFont const & f)
45 return getXFontstruct(f)->fid;
51 namespace font_metrics {
53 int maxAscent(LyXFont const & f)
55 return getXFontstruct(f)->ascent;
59 int maxDescent(LyXFont const & f)
61 return getXFontstruct(f)->descent;
65 int ascent(char c, LyXFont const & f)
67 XFontStruct * finfo = getXFontstruct(f);
68 unsigned int uc = static_cast<unsigned char>(c);
70 && uc >= finfo->min_char_or_byte2
71 && uc <= finfo->max_char_or_byte2+256*finfo->max_byte1)
72 return finfo->per_char[uc - finfo->min_char_or_byte2].ascent;
78 int descent(char c, LyXFont const & f)
80 XFontStruct * finfo = getXFontstruct(f);
81 unsigned int uc = static_cast<unsigned char>(c);
83 && uc >= finfo->min_char_or_byte2
84 && uc <= finfo->max_char_or_byte2+256*finfo->max_byte1)
85 return finfo->per_char[uc - finfo->min_char_or_byte2].descent;
87 return finfo->descent;
91 int lbearing(char c, LyXFont const & f)
93 XFontStruct * finfo = getXFontstruct(f);
94 unsigned int uc = static_cast<unsigned char>(c);
96 && uc >= finfo->min_char_or_byte2
97 && uc <= finfo->max_char_or_byte2+256*finfo->max_byte1)
98 return finfo->per_char[uc - finfo->min_char_or_byte2].lbearing;
104 int rbearing(char c, LyXFont const & f)
106 XFontStruct * finfo = getXFontstruct(f);
107 unsigned int uc = static_cast<unsigned char>(c);
109 && uc >= finfo->min_char_or_byte2
110 && uc <= finfo->max_char_or_byte2+256*finfo->max_byte1)
111 return finfo->per_char[uc - finfo->min_char_or_byte2].rbearing;
117 int width(char const * s, size_t n, LyXFont const & f)
119 if (!lyx_gui::use_gui)
122 if (lyxrc.font_norm_type == LyXRC::ISO_10646_1) {
123 boost::scoped_array<XChar2b> xs(new XChar2b[n]);
124 Encoding const * encoding = f.language()->encoding();
126 if (f.isSymbolFont()) {
127 #ifdef USE_UNICODE_FOR_SYMBOLS
128 font.setFamily(LyXFont::ROMAN_FAMILY);
129 font.setShape(LyXFont::UP_SHAPE);
131 encoding = encodings.symbol_encoding();
133 for (size_t i = 0; i < n; ++i) {
134 Uchar c = encoding->ucs(s[i]);
135 xs[i].byte1 = c >> 8;
136 xs[i].byte2 = c & 0xff;
138 int result = xfont_metrics::width(xs.get(), n, font);
142 if (f.realShape() != LyXFont::SMALLCAPS_SHAPE) {
143 return ::XTextWidth(getXFontstruct(f), s, n);
145 // emulate smallcaps since X doesn't support this
147 LyXFont smallfont(f);
148 smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE);
149 for (size_t i = 0; i < n; ++i) {
150 char const c = uppercase(s[i]);
152 result += ::XTextWidth(getXFontstruct(smallfont), &c, 1);
154 result += ::XTextWidth(getXFontstruct(f), &c, 1);
162 int signedWidth(string const & s, LyXFont const & f)
167 return -width(s.substr(1, s.length() - 1), f);
173 void rectText(string const & str, LyXFont const & font,
178 static int const d = 2;
179 width = font_metrics::width(str, font) + d * 2 + 2;
180 ascent = font_metrics::maxAscent(font) + d;
181 descent = font_metrics::maxDescent(font) + d;
186 void buttonText(string const & str, LyXFont const & font,
191 static int const d = 3;
193 width = font_metrics::width(str, font) + d * 2 + 2;
194 ascent = font_metrics::maxAscent(font) + d;
195 descent = font_metrics::maxDescent(font) + d;
198 } // namespace font_metrics
200 namespace xfont_metrics {
202 int width(XChar2b const * s, int n, LyXFont const & f)
204 if (!lyx_gui::use_gui)
207 if (f.realShape() != LyXFont::SMALLCAPS_SHAPE) {
208 return ::XTextWidth16(getXFontstruct(f), s, n);
210 // emulate smallcaps since X doesn't support this
213 LyXFont smallfont(f);
214 smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE);
215 for (int i = 0; i < n; ++i) {
219 c.byte1 = s[i].byte1;
220 c.byte2 = uppercase(s[i].byte2);
222 if (c.byte2 != s[i].byte2) {
223 result += ::XTextWidth16(getXFontstruct(smallfont), &c, 1);
225 result += ::XTextWidth16(getXFontstruct(f), &s[i], 1);
233 int XTextWidth(LyXFont const & f, char const * str, int count)
235 return ::XTextWidth(getXFontstruct(f), str, count);
239 int XTextWidth16(LyXFont const & f, XChar2b const * str, int count)
241 return ::XTextWidth16(getXFontstruct(f), str, count);
245 /// hmm, not a metric !
246 void XSetFont(Display * display, GC gc, LyXFont const & f)
248 ::XSetFont(display, gc, getFontID(f));
251 } // namespace xfont_metrics