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 "xfont_loader.h"
21 #include "frontends/lyx_gui.h"
23 #include "support/lstrings.h"
25 #include <boost/scoped_array.hpp>
27 using namespace lyx::support;
33 XFontStruct * getXFontstruct(LyXFont const & f)
35 return fontloader.load
36 (f.family(), f.series(),
37 f.realShape(), f.size());
42 XID getFontID(LyXFont const & f)
44 return getXFontstruct(f)->fid;
50 namespace font_metrics {
52 int maxAscent(LyXFont const & f)
54 return getXFontstruct(f)->ascent;
58 int maxDescent(LyXFont const & f)
60 return getXFontstruct(f)->descent;
64 int ascent(char c, LyXFont const & f)
66 XFontStruct * finfo = getXFontstruct(f);
67 unsigned int uc = static_cast<unsigned char>(c);
69 && uc >= finfo->min_char_or_byte2
70 && uc <= finfo->max_char_or_byte2+256*finfo->max_byte1)
71 return finfo->per_char[uc - finfo->min_char_or_byte2].ascent;
77 int descent(char c, LyXFont const & f)
79 XFontStruct * finfo = getXFontstruct(f);
80 unsigned int uc = static_cast<unsigned char>(c);
82 && uc >= finfo->min_char_or_byte2
83 && uc <= finfo->max_char_or_byte2+256*finfo->max_byte1)
84 return finfo->per_char[uc - finfo->min_char_or_byte2].descent;
86 return finfo->descent;
90 int lbearing(char c, LyXFont const & f)
92 XFontStruct * finfo = getXFontstruct(f);
93 unsigned int uc = static_cast<unsigned char>(c);
95 && uc >= finfo->min_char_or_byte2
96 && uc <= finfo->max_char_or_byte2+256*finfo->max_byte1)
97 return finfo->per_char[uc - finfo->min_char_or_byte2].lbearing;
103 int rbearing(char c, LyXFont const & f)
105 XFontStruct * finfo = getXFontstruct(f);
106 unsigned int uc = static_cast<unsigned char>(c);
108 && uc >= finfo->min_char_or_byte2
109 && uc <= finfo->max_char_or_byte2+256*finfo->max_byte1)
110 return finfo->per_char[uc - finfo->min_char_or_byte2].rbearing;
116 int width(char const * s, size_t n, LyXFont const & f)
118 if (!lyx_gui::use_gui)
121 if (lyxrc.font_norm_type == LyXRC::ISO_10646_1) {
122 boost::scoped_array<XChar2b> xs(new XChar2b[n]);
123 Encoding const * encoding = f.language()->encoding();
125 if (f.isSymbolFont()) {
126 #ifdef USE_UNICODE_FOR_SYMBOLS
127 font.setFamily(LyXFont::ROMAN_FAMILY);
128 font.setShape(LyXFont::UP_SHAPE);
130 encoding = encodings.symbol_encoding();
132 for (size_t i = 0; i < n; ++i) {
133 Uchar c = encoding->ucs(s[i]);
134 xs[i].byte1 = c >> 8;
135 xs[i].byte2 = c & 0xff;
137 int result = xfont_metrics::width(xs.get(), n, font);
141 if (f.realShape() != LyXFont::SMALLCAPS_SHAPE) {
142 return ::XTextWidth(getXFontstruct(f), s, n);
144 // emulate smallcaps since X doesn't support this
146 LyXFont smallfont(f);
147 smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE);
148 for (size_t i = 0; i < n; ++i) {
149 char const c = uppercase(s[i]);
151 result += ::XTextWidth(getXFontstruct(smallfont), &c, 1);
153 result += ::XTextWidth(getXFontstruct(f), &c, 1);
161 int signedWidth(string const & s, LyXFont const & f)
166 return -width(s.substr(1, s.length() - 1), f);
172 void rectText(string const & str, LyXFont const & font,
177 static int const d = 2;
178 width = font_metrics::width(str, font) + d * 2 + 2;
179 ascent = font_metrics::maxAscent(font) + d;
180 descent = font_metrics::maxDescent(font) + d;
185 void buttonText(string const & str, LyXFont const & font,
190 static int const d = 3;
192 width = font_metrics::width(str, font) + d * 2 + 2;
193 ascent = font_metrics::maxAscent(font) + d;
194 descent = font_metrics::maxDescent(font) + d;
197 } // namespace font_metrics
199 namespace xfont_metrics {
201 int width(XChar2b const * s, int n, LyXFont const & f)
203 if (!lyx_gui::use_gui)
206 if (f.realShape() != LyXFont::SMALLCAPS_SHAPE) {
207 return ::XTextWidth16(getXFontstruct(f), s, n);
209 // emulate smallcaps since X doesn't support this
212 LyXFont smallfont(f);
213 smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE);
214 for (int i = 0; i < n; ++i) {
218 c.byte1 = s[i].byte1;
219 c.byte2 = uppercase(s[i].byte2);
221 if (c.byte2 != s[i].byte2) {
222 result += ::XTextWidth16(getXFontstruct(smallfont), &c, 1);
224 result += ::XTextWidth16(getXFontstruct(f), &s[i], 1);
232 int XTextWidth(LyXFont const & f, char const * str, int count)
234 return ::XTextWidth(getXFontstruct(f), str, count);
238 int XTextWidth16(LyXFont const & f, XChar2b const * str, int count)
240 return ::XTextWidth16(getXFontstruct(f), str, count);
244 /// hmm, not a metric !
245 void XSetFont(Display * display, GC gc, LyXFont const & f)
247 ::XSetFont(display, gc, getFontID(f));
250 } // namespace xfont_metrics