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>
30 using lyx::support::uppercase;
31 using lyx::frontend::fontloader;
36 XFontStruct * getXFontstruct(LyXFont const & f)
38 return fontloader.load
39 (f.family(), f.series(),
40 f.realShape(), f.size());
45 XID getFontID(LyXFont const & f)
47 return getXFontstruct(f)->fid;
53 namespace font_metrics {
55 int maxAscent(LyXFont const & f)
57 return getXFontstruct(f)->ascent;
61 int maxDescent(LyXFont const & f)
63 return getXFontstruct(f)->descent;
67 int ascent(char c, LyXFont const & f)
69 XFontStruct * finfo = getXFontstruct(f);
70 unsigned int uc = static_cast<unsigned char>(c);
72 && uc >= finfo->min_char_or_byte2
73 && uc <= finfo->max_char_or_byte2+256*finfo->max_byte1)
74 return finfo->per_char[uc - finfo->min_char_or_byte2].ascent;
80 int descent(char c, LyXFont const & f)
82 XFontStruct * finfo = getXFontstruct(f);
83 unsigned int uc = static_cast<unsigned char>(c);
85 && uc >= finfo->min_char_or_byte2
86 && uc <= finfo->max_char_or_byte2+256*finfo->max_byte1)
87 return finfo->per_char[uc - finfo->min_char_or_byte2].descent;
89 return finfo->descent;
93 int lbearing(char c, LyXFont const & f)
95 XFontStruct * finfo = getXFontstruct(f);
96 unsigned int uc = static_cast<unsigned char>(c);
98 && uc >= finfo->min_char_or_byte2
99 && uc <= finfo->max_char_or_byte2+256*finfo->max_byte1)
100 return finfo->per_char[uc - finfo->min_char_or_byte2].lbearing;
106 int rbearing(char c, LyXFont const & f)
108 XFontStruct * finfo = getXFontstruct(f);
109 unsigned int uc = static_cast<unsigned char>(c);
111 && uc >= finfo->min_char_or_byte2
112 && uc <= finfo->max_char_or_byte2+256*finfo->max_byte1)
113 return finfo->per_char[uc - finfo->min_char_or_byte2].rbearing;
119 int width(char const * s, size_t n, LyXFont const & f)
121 if (!lyx_gui::use_gui)
124 if (lyxrc.font_norm_type == LyXRC::ISO_10646_1) {
125 boost::scoped_array<XChar2b> xs(new XChar2b[n]);
126 Encoding const * encoding = f.language()->encoding();
128 if (f.isSymbolFont()) {
129 #ifdef USE_UNICODE_FOR_SYMBOLS
130 font.setFamily(LyXFont::ROMAN_FAMILY);
131 font.setShape(LyXFont::UP_SHAPE);
133 encoding = encodings.symbol_encoding();
135 for (size_t i = 0; i < n; ++i) {
136 Uchar c = encoding->ucs(s[i]);
137 xs[i].byte1 = c >> 8;
138 xs[i].byte2 = c & 0xff;
140 int result = xfont_metrics::width(xs.get(), n, font);
144 if (f.realShape() != LyXFont::SMALLCAPS_SHAPE) {
145 return ::XTextWidth(getXFontstruct(f), s, n);
147 // emulate smallcaps since X doesn't support this
149 LyXFont smallfont(f);
150 smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE);
151 for (size_t i = 0; i < n; ++i) {
152 char const c = uppercase(s[i]);
154 result += ::XTextWidth(getXFontstruct(smallfont), &c, 1);
156 result += ::XTextWidth(getXFontstruct(f), &c, 1);
164 int signedWidth(string const & s, LyXFont const & f)
169 return -width(s.substr(1, s.length() - 1), f);
175 void rectText(string const & str, LyXFont const & font,
180 static int const d = 2;
181 width = font_metrics::width(str, font) + d * 2 + 2;
182 ascent = font_metrics::maxAscent(font) + d;
183 descent = font_metrics::maxDescent(font) + d;
188 void buttonText(string const & str, LyXFont const & font,
193 static int const d = 3;
195 width = font_metrics::width(str, font) + d * 2 + 2;
196 ascent = font_metrics::maxAscent(font) + d;
197 descent = font_metrics::maxDescent(font) + d;
200 } // namespace font_metrics
202 namespace xfont_metrics {
204 int width(XChar2b const * s, int n, LyXFont const & f)
206 if (!lyx_gui::use_gui)
209 if (f.realShape() != LyXFont::SMALLCAPS_SHAPE) {
210 return ::XTextWidth16(getXFontstruct(f), s, n);
212 // emulate smallcaps since X doesn't support this
215 LyXFont smallfont(f);
216 smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE);
217 for (int i = 0; i < n; ++i) {
221 c.byte1 = s[i].byte1;
222 c.byte2 = uppercase(s[i].byte2);
224 if (c.byte2 != s[i].byte2) {
225 result += ::XTextWidth16(getXFontstruct(smallfont), &c, 1);
227 result += ::XTextWidth16(getXFontstruct(f), &s[i], 1);
235 int XTextWidth(LyXFont const & f, char const * str, int count)
237 return ::XTextWidth(getXFontstruct(f), str, count);
241 int XTextWidth16(LyXFont const & f, XChar2b const * str, int count)
243 return ::XTextWidth16(getXFontstruct(f), str, count);
247 /// hmm, not a metric !
248 void XSetFont(Display * display, GC gc, LyXFont const & f)
250 ::XSetFont(display, gc, getFontID(f));
253 } // namespace xfont_metrics