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)
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)
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)
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)
98 return finfo->per_char[uc - finfo->min_char_or_byte2].rbearing;
104 int lyxfont::width(char const * s, int 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;
113 if (f.family() == LyXFont::SYMBOL_FAMILY) {
114 #ifdef USE_UNICODE_FOR_SYMBOLS
116 font2.setFamily(LyXFont::ROMAN_FAMILY);
117 font2.setShape(LyXFont::UP_SHAPE);
120 encoding = &symbol_encoding;
122 for (int i = 0; i < n; ++i) {
123 Uchar c = encoding->ucs(s[i]);
124 xs[i].byte1 = c >> 8;
125 xs[i].byte2 = c & 0xff;
127 int result = width(xs, n, *font);
132 if (f.realShape() != LyXFont::SMALLCAPS_SHAPE) {
133 return ::XTextWidth(getXFontstruct(f), s, n);
135 // emulate smallcaps since X doesn't support this
136 unsigned int result = 0;
138 LyXFont smallfont(f);
139 smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE);
140 for (int i = 0; i < n; ++i) {
142 // when islower is a macro, the cast is needed (JMarc)
143 if (islower(static_cast<unsigned char>(c))) {
145 result += ::XTextWidth(getXFontstruct(smallfont), &c, 1);
147 result += ::XTextWidth(getXFontstruct(f), &c, 1);
155 int lyxfont::signedWidth(string const & s, LyXFont const & f)
157 if (s.empty()) return 0;
158 if (s.c_str()[0] == '-')
159 return -width(s.c_str() + 1, s.length() - 1, f);
161 return width(s.c_str(), s.length(), f);
165 int lyxfont::width(XChar2b const * s, int n, LyXFont const & f)
170 if (f.realShape() != LyXFont::SMALLCAPS_SHAPE) {
171 return ::XTextWidth16(getXFontstruct(f), s, n);
173 // emulate smallcaps since X doesn't support this
174 unsigned int result = 0;
175 static XChar2b c = {0, 0};
176 LyXFont smallfont(f);
177 smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE);
178 for (int i = 0; i < n; ++i) {
179 if (s[i].byte1 == 0 && islower(s[i].byte2)) {
180 c.byte2 = toupper(s[i].byte2);
181 result += ::XTextWidth16(getXFontstruct(smallfont), &c, 1);
183 result += ::XTextWidth16(getXFontstruct(f), &s[i], 1);
190 int lyxfont::XTextWidth(LyXFont const & f, char * str, int count)
192 return ::XTextWidth(getXFontstruct(f), str, count);
196 int lyxfont::XTextWidth16(LyXFont const & f, XChar2b * str, int count)
198 return ::XTextWidth16(getXFontstruct(f), str, count);
202 void lyxfont::XSetFont(Display * display, GC gc, LyXFont const & f)
204 ::XSetFont(display, gc, getFontID(f));
207 //} // end of namespace font
208 //} // end of namespace lyx