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"
20 #include "frontends/lyx_gui.h"
22 #include "support/lstrings.h"
24 #include <boost/scoped_array.hpp>
26 using namespace lyx::support;
32 XFontStruct * getXFontstruct(LyXFont const & f)
34 return fontloader.load
35 (f.family(), f.series(),
36 f.realShape(), f.size());
41 XID getFontID(LyXFont const & f)
43 return getXFontstruct(f)->fid;
49 namespace font_metrics {
51 int maxAscent(LyXFont const & f)
53 return getXFontstruct(f)->ascent;
57 int maxDescent(LyXFont const & f)
59 return getXFontstruct(f)->descent;
63 int ascent(char c, LyXFont const & f)
65 XFontStruct * finfo = getXFontstruct(f);
66 unsigned int uc = static_cast<unsigned char>(c);
68 && uc >= finfo->min_char_or_byte2
69 && uc <= finfo->max_char_or_byte2+256*finfo->max_byte1)
70 return finfo->per_char[uc - finfo->min_char_or_byte2].ascent;
76 int descent(char c, LyXFont const & f)
78 XFontStruct * finfo = getXFontstruct(f);
79 unsigned int uc = static_cast<unsigned char>(c);
81 && uc >= finfo->min_char_or_byte2
82 && uc <= finfo->max_char_or_byte2+256*finfo->max_byte1)
83 return finfo->per_char[uc - finfo->min_char_or_byte2].descent;
85 return finfo->descent;
89 int lbearing(char c, LyXFont const & f)
91 XFontStruct * finfo = getXFontstruct(f);
92 unsigned int uc = static_cast<unsigned char>(c);
94 && uc >= finfo->min_char_or_byte2
95 && uc <= finfo->max_char_or_byte2+256*finfo->max_byte1)
96 return finfo->per_char[uc - finfo->min_char_or_byte2].lbearing;
102 int rbearing(char c, LyXFont const & f)
104 XFontStruct * finfo = getXFontstruct(f);
105 unsigned int uc = static_cast<unsigned char>(c);
107 && uc >= finfo->min_char_or_byte2
108 && uc <= finfo->max_char_or_byte2+256*finfo->max_byte1)
109 return finfo->per_char[uc - finfo->min_char_or_byte2].rbearing;
115 int width(char const * s, size_t n, LyXFont const & f)
117 if (!lyx_gui::use_gui)
120 if (lyxrc.font_norm_type == LyXRC::ISO_10646_1) {
121 boost::scoped_array<XChar2b> xs(new XChar2b[n]);
122 Encoding const * encoding = f.language()->encoding();
124 if (f.isSymbolFont()) {
125 #ifdef USE_UNICODE_FOR_SYMBOLS
126 font.setFamily(LyXFont::ROMAN_FAMILY);
127 font.setShape(LyXFont::UP_SHAPE);
129 encoding = encodings.symbol_encoding();
131 for (size_t i = 0; i < n; ++i) {
132 Uchar c = encoding->ucs(s[i]);
133 xs[i].byte1 = c >> 8;
134 xs[i].byte2 = c & 0xff;
136 int result = xfont_metrics::width(xs.get(), n, font);
140 if (f.realShape() != LyXFont::SMALLCAPS_SHAPE) {
141 return ::XTextWidth(getXFontstruct(f), s, n);
143 // emulate smallcaps since X doesn't support this
145 LyXFont smallfont(f);
146 smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE);
147 for (size_t i = 0; i < n; ++i) {
148 char const c = uppercase(s[i]);
150 result += ::XTextWidth(getXFontstruct(smallfont), &c, 1);
152 result += ::XTextWidth(getXFontstruct(f), &c, 1);
160 int signedWidth(string const & s, LyXFont const & f)
165 return -width(s.substr(1, s.length() - 1), f);
171 void rectText(string const & str, LyXFont const & font,
176 static int const d = 2;
177 width = font_metrics::width(str, font) + d * 2 + 2;
178 ascent = font_metrics::maxAscent(font) + d;
179 descent = font_metrics::maxDescent(font) + d;
184 void buttonText(string const & str, LyXFont const & font,
189 static int const d = 3;
191 width = font_metrics::width(str, font) + d * 2 + 2;
192 ascent = font_metrics::maxAscent(font) + d;
193 descent = font_metrics::maxDescent(font) + d;
196 } // namespace font_metrics
198 namespace xfont_metrics {
200 int width(XChar2b const * s, int n, LyXFont const & f)
202 if (!lyx_gui::use_gui)
205 if (f.realShape() != LyXFont::SMALLCAPS_SHAPE) {
206 return ::XTextWidth16(getXFontstruct(f), s, n);
208 // emulate smallcaps since X doesn't support this
211 LyXFont smallfont(f);
212 smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE);
213 for (int i = 0; i < n; ++i) {
217 c.byte1 = s[i].byte1;
218 c.byte2 = uppercase(s[i].byte2);
220 if (c.byte2 != s[i].byte2) {
221 result += ::XTextWidth16(getXFontstruct(smallfont), &c, 1);
223 result += ::XTextWidth16(getXFontstruct(f), &s[i], 1);
231 int XTextWidth(LyXFont const & f, char const * str, int count)
233 return ::XTextWidth(getXFontstruct(f), str, count);
237 int XTextWidth16(LyXFont const & f, XChar2b const * str, int count)
239 return ::XTextWidth16(getXFontstruct(f), str, count);
243 /// hmm, not a metric !
244 void XSetFont(Display * display, GC gc, LyXFont const & f)
246 ::XSetFont(display, gc, getFontID(f));
249 } // namespace xfont_metrics