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 "support/lstrings.h"
15 #include "frontends/lyx_gui.h"
16 #include "xfont_metrics.h"
17 #include "xfont_loader.h"
22 #include <boost/scoped_array.hpp>
27 XFontStruct * getXFontstruct(LyXFont const & f)
29 return fontloader.load
30 (f.family(), f.series(),
31 f.realShape(), f.size());
36 XID getFontID(LyXFont const & f)
38 return getXFontstruct(f)->fid;
44 namespace font_metrics {
46 int maxAscent(LyXFont const & f)
48 return getXFontstruct(f)->ascent;
52 int maxDescent(LyXFont const & f)
54 return getXFontstruct(f)->descent;
58 int ascent(char c, LyXFont const & f)
60 XFontStruct * finfo = getXFontstruct(f);
61 unsigned int uc = static_cast<unsigned char>(c);
63 && uc >= finfo->min_char_or_byte2
64 && uc <= finfo->max_char_or_byte2+256*finfo->max_byte1)
65 return finfo->per_char[uc - finfo->min_char_or_byte2].ascent;
71 int descent(char c, LyXFont const & f)
73 XFontStruct * finfo = getXFontstruct(f);
74 unsigned int uc = static_cast<unsigned char>(c);
76 && uc >= finfo->min_char_or_byte2
77 && uc <= finfo->max_char_or_byte2+256*finfo->max_byte1)
78 return finfo->per_char[uc - finfo->min_char_or_byte2].descent;
80 return finfo->descent;
84 int lbearing(char c, LyXFont const & f)
86 XFontStruct * finfo = getXFontstruct(f);
87 unsigned int uc = static_cast<unsigned char>(c);
89 && uc >= finfo->min_char_or_byte2
90 && uc <= finfo->max_char_or_byte2+256*finfo->max_byte1)
91 return finfo->per_char[uc - finfo->min_char_or_byte2].lbearing;
97 int rbearing(char c, LyXFont const & f)
99 XFontStruct * finfo = getXFontstruct(f);
100 unsigned int uc = static_cast<unsigned char>(c);
102 && uc >= finfo->min_char_or_byte2
103 && uc <= finfo->max_char_or_byte2+256*finfo->max_byte1)
104 return finfo->per_char[uc - finfo->min_char_or_byte2].rbearing;
110 int width(char const * s, size_t n, LyXFont const & f)
112 if (!lyx_gui::use_gui)
115 if (lyxrc.font_norm_type == LyXRC::ISO_10646_1) {
116 boost::scoped_array<XChar2b> xs(new XChar2b[n]);
117 Encoding const * encoding = f.language()->encoding();
119 if (f.isSymbolFont()) {
120 #ifdef USE_UNICODE_FOR_SYMBOLS
121 font.setFamily(LyXFont::ROMAN_FAMILY);
122 font.setShape(LyXFont::UP_SHAPE);
124 encoding = encodings.symbol_encoding();
126 for (size_t i = 0; i < n; ++i) {
127 Uchar c = encoding->ucs(s[i]);
128 xs[i].byte1 = c >> 8;
129 xs[i].byte2 = c & 0xff;
131 int result = xfont_metrics::width(xs.get(), n, font);
135 if (f.realShape() != LyXFont::SMALLCAPS_SHAPE) {
136 return ::XTextWidth(getXFontstruct(f), s, n);
138 // emulate smallcaps since X doesn't support this
140 LyXFont smallfont(f);
141 smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE);
142 for (size_t i = 0; i < n; ++i) {
143 char const c = uppercase(s[i]);
145 result += ::XTextWidth(getXFontstruct(smallfont), &c, 1);
147 result += ::XTextWidth(getXFontstruct(f), &c, 1);
155 int signedWidth(string const & s, LyXFont const & f)
160 return -width(s.substr(1, s.length() - 1), f);
166 void rectText(string const & str, LyXFont const & font,
171 static int const d = 2;
172 width = font_metrics::width(str, font) + d * 2 + 2;
173 ascent = font_metrics::maxAscent(font) + d;
174 descent = font_metrics::maxDescent(font) + d;
179 void buttonText(string const & str, LyXFont const & font,
184 static int const d = 3;
186 width = font_metrics::width(str, font) + d * 2 + 2;
187 ascent = font_metrics::maxAscent(font) + d;
188 descent = font_metrics::maxDescent(font) + d;
191 } // namespace font_metrics
193 namespace xfont_metrics {
195 int width(XChar2b const * s, int n, LyXFont const & f)
197 if (!lyx_gui::use_gui)
200 if (f.realShape() != LyXFont::SMALLCAPS_SHAPE) {
201 return ::XTextWidth16(getXFontstruct(f), s, n);
203 // emulate smallcaps since X doesn't support this
206 LyXFont smallfont(f);
207 smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE);
208 for (int i = 0; i < n; ++i) {
212 c.byte1 = s[i].byte1;
213 c.byte2 = uppercase(s[i].byte2);
215 if (c.byte2 != s[i].byte2) {
216 result += ::XTextWidth16(getXFontstruct(smallfont), &c, 1);
218 result += ::XTextWidth16(getXFontstruct(f), &s[i], 1);
226 int XTextWidth(LyXFont const & f, char const * str, int count)
228 return ::XTextWidth(getXFontstruct(f), str, count);
232 int XTextWidth16(LyXFont const & f, XChar2b const * str, int count)
234 return ::XTextWidth16(getXFontstruct(f), str, count);
238 /// hmm, not a metric !
239 void XSetFont(Display * display, GC gc, LyXFont const & f)
241 ::XSetFont(display, gc, getFontID(f));
244 } // namespace xfont_metrics