1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1995 Matthias Ettrich
7 * Copyright 1995-2001 The LyX Team.
9 * ====================================================== */
14 #pragma implementation
17 #include "support/lstrings.h"
19 #include "FontLoader.h"
24 #include <boost/smart_ptr.hpp>
30 XFontStruct * getXFontstruct(LyXFont const & f)
32 return fontloader.load(f.family(), f.series(),
33 f.realShape(), f.size());
38 XID getFontID(LyXFont const & f)
40 return getXFontstruct(f)->fid;
45 int lyxfont::maxAscent(LyXFont const & f)
47 return getXFontstruct(f)->ascent;
51 int lyxfont::maxDescent(LyXFont const & f)
53 return getXFontstruct(f)->descent;
57 int lyxfont::ascent(char c, LyXFont const & f)
59 XFontStruct * finfo = getXFontstruct(f);
60 unsigned int uc = static_cast<unsigned char>(c);
62 && uc >= finfo->min_char_or_byte2
63 && uc <= finfo->max_char_or_byte2+256*finfo->max_byte1)
64 return finfo->per_char[uc - finfo->min_char_or_byte2].ascent;
70 int lyxfont::descent(char c, LyXFont const & f)
72 XFontStruct * finfo = getXFontstruct(f);
73 unsigned int uc = static_cast<unsigned char>(c);
75 && uc >= finfo->min_char_or_byte2
76 && uc <= finfo->max_char_or_byte2+256*finfo->max_byte1)
77 return finfo->per_char[uc - finfo->min_char_or_byte2].descent;
79 return finfo->descent;
83 int lyxfont::lbearing(char c, LyXFont const & f)
85 XFontStruct * finfo = getXFontstruct(f);
86 unsigned int uc = static_cast<unsigned char>(c);
88 && uc >= finfo->min_char_or_byte2
89 && uc <= finfo->max_char_or_byte2+256*finfo->max_byte1)
90 return finfo->per_char[uc - finfo->min_char_or_byte2].lbearing;
96 int lyxfont::rbearing(char c, LyXFont const & f)
98 XFontStruct * finfo = getXFontstruct(f);
99 unsigned int uc = static_cast<unsigned char>(c);
101 && uc >= finfo->min_char_or_byte2
102 && uc <= finfo->max_char_or_byte2+256*finfo->max_byte1)
103 return finfo->per_char[uc - finfo->min_char_or_byte2].rbearing;
109 int lyxfont::width(char const * s, size_t n, LyXFont const & f)
114 if (lyxrc.font_norm_type == LyXRC::ISO_10646_1) {
115 boost::scoped_array<XChar2b> xs(new XChar2b[n]);
116 Encoding const * encoding = f.language()->encoding();
118 if (f.isSymbolFont()) {
119 #ifdef USE_UNICODE_FOR_SYMBOLS
120 font.setFamily(LyXFont::ROMAN_FAMILY);
121 font.setShape(LyXFont::UP_SHAPE);
123 encoding = encodings.symbol_encoding();
125 for (size_t i = 0; i < n; ++i) {
126 Uchar c = encoding->ucs(s[i]);
127 xs[i].byte1 = c >> 8;
128 xs[i].byte2 = c & 0xff;
130 int result = width(xs.get(), n, font);
134 if (f.realShape() != LyXFont::SMALLCAPS_SHAPE) {
135 return ::XTextWidth(getXFontstruct(f), s, n);
137 // emulate smallcaps since X doesn't support this
138 unsigned int result = 0;
139 LyXFont smallfont(f);
140 smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE);
141 for (size_t i = 0; i < n; ++i) {
142 char const c = uppercase(s[i]);
144 result += ::XTextWidth(getXFontstruct(smallfont), &c, 1);
146 result += ::XTextWidth(getXFontstruct(f), &c, 1);
154 int lyxfont::signedWidth(string const & s, LyXFont const & f)
159 return -width(s.substr(1, s.length() - 1), f);
165 //int lyxfont::width(wstring const & s, int n, LyXFont const & f)
166 int lyxfont::width(XChar2b const * s, int n, LyXFont const & f)
171 if (f.realShape() != LyXFont::SMALLCAPS_SHAPE) {
172 return ::XTextWidth16(getXFontstruct(f), s, n);
174 // emulate smallcaps since X doesn't support this
175 unsigned int result = 0;
177 LyXFont smallfont(f);
178 smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE);
179 for (int i = 0; i < n; ++i) {
183 c.byte1 = s[i].byte1;
184 c.byte2 = uppercase(s[i].byte2);
186 if (c.byte2 != s[i].byte2) {
187 result += ::XTextWidth16(getXFontstruct(smallfont), &c, 1);
189 result += ::XTextWidth16(getXFontstruct(f), &s[i], 1);
196 int lyxfont::XTextWidth(LyXFont const & f, char const * str, int count)
198 return ::XTextWidth(getXFontstruct(f), str, count);
202 int lyxfont::XTextWidth16(LyXFont const & f, XChar2b const * str, int count)
204 return ::XTextWidth16(getXFontstruct(f), str, count);
208 void lyxfont::XSetFont(Display * display, GC gc, LyXFont const & f)
210 ::XSetFont(display, gc, getFontID(f));
214 void lyxfont::rectText(string const & str, LyXFont const & font,
215 int & width, int & ascent, int & descent)
217 static int const d = 2;
218 width = lyxfont::width(str, font) + d * 2 + 2;
219 ascent = lyxfont::maxAscent(font) + d;
220 descent = lyxfont::maxDescent(font) + d;
225 void lyxfont::buttonText(string const & str, LyXFont const & font,
226 int & width, int & ascent, int & descent)
228 static int const d = 3;
230 width = lyxfont::width(str, font) + d * 2 + 2;
231 ascent = lyxfont::maxAscent(font) + d;
232 descent = lyxfont::maxDescent(font) + d;
236 //} // end of namespace font
237 //} // end of namespace lyx