1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1995 Matthias Ettrich
7 * Copyright 1995-2000 The LyX Team.
9 * ====================================================== */
14 #pragma implementation
20 #include "FontLoader.h"
26 XFontStruct * getXFontstruct(LyXFont const & f)
28 return fontloader.load(f.family(), f.series(),
29 f.realShape(), f.size());
34 XID getFontID(LyXFont const & f)
36 return getXFontstruct(f)->fid;
38 // } // end of anon namespace
40 int lyxfont::maxAscent(LyXFont const & f)
42 return getXFontstruct(f)->ascent;
46 int lyxfont::maxDescent(LyXFont const & f)
48 return getXFontstruct(f)->descent;
52 int lyxfont::ascent(char c, LyXFont const & f)
54 XFontStruct * finfo = getXFontstruct(f);
55 unsigned int uc = static_cast<unsigned char>(c);
57 && uc >= finfo->min_char_or_byte2
58 && uc <= finfo->max_char_or_byte2+256*finfo->max_byte1)
59 return finfo->per_char[uc - finfo->min_char_or_byte2].ascent;
65 int lyxfont::descent(char c, LyXFont const & f)
67 XFontStruct * finfo = getXFontstruct(f);
68 unsigned int uc = static_cast<unsigned char>(c);
70 && uc >= finfo->min_char_or_byte2
71 && uc <= finfo->max_char_or_byte2+256*finfo->max_byte1)
72 return finfo->per_char[uc - finfo->min_char_or_byte2].descent;
74 return finfo->descent;
78 int lyxfont::lbearing(char c, LyXFont const & f)
80 XFontStruct * finfo = getXFontstruct(f);
81 unsigned int uc = static_cast<unsigned char>(c);
83 && uc >= finfo->min_char_or_byte2
84 && uc <= finfo->max_char_or_byte2+256*finfo->max_byte1)
85 return finfo->per_char[uc - finfo->min_char_or_byte2].lbearing;
91 int lyxfont::rbearing(char c, LyXFont const & f)
93 XFontStruct * finfo = getXFontstruct(f);
94 unsigned int uc = static_cast<unsigned char>(c);
96 && uc >= finfo->min_char_or_byte2
97 && uc <= finfo->max_char_or_byte2+256*finfo->max_byte1)
98 return finfo->per_char[uc - finfo->min_char_or_byte2].rbearing;
104 int lyxfont::width(char const * s, size_t n, LyXFont const & f)
109 if (lyxrc.font_norm_type == LyXRC::ISO_10646_1) {
110 XChar2b * xs = new XChar2b[n];
111 Encoding const * encoding = f.language()->encoding();
112 //LyXFont const * font = &f;
114 if (f.family() == LyXFont::SYMBOL_FAMILY) {
115 #ifdef USE_UNICODE_FOR_SYMBOLS
117 font.setFamily(LyXFont::ROMAN_FAMILY);
118 font.setShape(LyXFont::UP_SHAPE);
121 encoding = encodings.symbol_encoding();
123 for (size_t i = 0; i < n; ++i) {
124 Uchar c = encoding->ucs(s[i]);
125 xs[i].byte1 = c >> 8;
126 xs[i].byte2 = c & 0xff;
128 int result = width(xs, n, font);
133 if (f.realShape() != LyXFont::SMALLCAPS_SHAPE) {
134 return ::XTextWidth(getXFontstruct(f), s, n);
136 // emulate smallcaps since X doesn't support this
137 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) {
143 // when islower is a macro, the cast is needed (JMarc)
144 if (islower(static_cast<unsigned char>(c))) {
146 result += ::XTextWidth(getXFontstruct(smallfont), &c, 1);
148 result += ::XTextWidth(getXFontstruct(f), &c, 1);
156 int lyxfont::signedWidth(string const & s, LyXFont const & f)
158 if (s.empty()) return 0;
160 return -width(s.substr(1, s.length() - 1), f);
166 //int lyxfont::width(wstring const & s, int n, LyXFont const & f)
167 int lyxfont::width(XChar2b const * s, int n, LyXFont const & f)
172 if (f.realShape() != LyXFont::SMALLCAPS_SHAPE) {
173 return ::XTextWidth16(getXFontstruct(f), s, n);
175 // emulate smallcaps since X doesn't support this
176 unsigned int result = 0;
177 static XChar2b c = {0, 0};
178 LyXFont smallfont(f);
179 smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE);
180 for (int i = 0; i < n; ++i) {
181 if (s[i].byte1 == 0 && islower(s[i].byte2)) {
182 c.byte2 = toupper(s[i].byte2);
183 result += ::XTextWidth16(getXFontstruct(smallfont), &c, 1);
185 result += ::XTextWidth16(getXFontstruct(f), &s[i], 1);
192 int lyxfont::XTextWidth(LyXFont const & f, char const * str, int count)
194 return ::XTextWidth(getXFontstruct(f), str, count);
198 int lyxfont::XTextWidth16(LyXFont const & f, XChar2b const * str, int count)
200 return ::XTextWidth16(getXFontstruct(f), str, count);
204 void lyxfont::XSetFont(Display * display, GC gc, LyXFont const & f)
206 ::XSetFont(display, gc, getFontID(f));
209 //} // end of namespace font
210 //} // end of namespace lyx