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"
27 XFontStruct * getXFontstruct(LyXFont const & f)
29 return fontloader.load(f.family(), f.series(),
30 f.realShape(), f.size());
35 XID getFontID(LyXFont const & f)
37 return getXFontstruct(f)->fid;
42 int lyxfont::maxAscent(LyXFont const & f)
44 return getXFontstruct(f)->ascent;
48 int lyxfont::maxDescent(LyXFont const & f)
50 return getXFontstruct(f)->descent;
54 int lyxfont::ascent(char c, LyXFont const & f)
56 XFontStruct * finfo = getXFontstruct(f);
57 unsigned int uc = static_cast<unsigned char>(c);
59 && uc >= finfo->min_char_or_byte2
60 && uc <= finfo->max_char_or_byte2+256*finfo->max_byte1)
61 return finfo->per_char[uc - finfo->min_char_or_byte2].ascent;
67 int lyxfont::descent(char c, LyXFont const & f)
69 XFontStruct * finfo = getXFontstruct(f);
70 unsigned int uc = static_cast<unsigned char>(c);
72 && uc >= finfo->min_char_or_byte2
73 && uc <= finfo->max_char_or_byte2+256*finfo->max_byte1)
74 return finfo->per_char[uc - finfo->min_char_or_byte2].descent;
76 return finfo->descent;
80 int lyxfont::lbearing(char c, LyXFont const & f)
82 XFontStruct * finfo = getXFontstruct(f);
83 unsigned int uc = static_cast<unsigned char>(c);
85 && uc >= finfo->min_char_or_byte2
86 && uc <= finfo->max_char_or_byte2+256*finfo->max_byte1)
87 return finfo->per_char[uc - finfo->min_char_or_byte2].lbearing;
93 int lyxfont::rbearing(char c, LyXFont const & f)
95 XFontStruct * finfo = getXFontstruct(f);
96 unsigned int uc = static_cast<unsigned char>(c);
98 && uc >= finfo->min_char_or_byte2
99 && uc <= finfo->max_char_or_byte2+256*finfo->max_byte1)
100 return finfo->per_char[uc - finfo->min_char_or_byte2].rbearing;
106 int lyxfont::width(char const * s, size_t n, LyXFont const & f)
111 if (lyxrc.font_norm_type == LyXRC::ISO_10646_1) {
112 XChar2b * xs = new XChar2b[n];
113 Encoding const * encoding = f.language()->encoding();
114 //LyXFont const * font = &f;
116 if (f.family() == LyXFont::SYMBOL_FAMILY) {
117 #ifdef USE_UNICODE_FOR_SYMBOLS
119 font.setFamily(LyXFont::ROMAN_FAMILY);
120 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, n, font);
135 if (f.realShape() != LyXFont::SMALLCAPS_SHAPE) {
136 return ::XTextWidth(getXFontstruct(f), s, n);
138 // emulate smallcaps since X doesn't support this
139 unsigned int result = 0;
141 LyXFont smallfont(f);
142 smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE);
143 for (size_t i = 0; i < n; ++i) {
145 // when islower is a macro, the cast is needed (JMarc)
146 if (islower(static_cast<unsigned char>(c))) {
148 result += ::XTextWidth(getXFontstruct(smallfont), &c, 1);
150 result += ::XTextWidth(getXFontstruct(f), &c, 1);
158 int lyxfont::signedWidth(string const & s, LyXFont const & f)
160 if (s.empty()) return 0;
162 return -width(s.substr(1, s.length() - 1), f);
168 //int lyxfont::width(wstring const & s, int n, LyXFont const & f)
169 int lyxfont::width(XChar2b const * s, int n, LyXFont const & f)
174 if (f.realShape() != LyXFont::SMALLCAPS_SHAPE) {
175 return ::XTextWidth16(getXFontstruct(f), s, n);
177 // emulate smallcaps since X doesn't support this
178 unsigned int result = 0;
179 static XChar2b c = {0, 0};
180 LyXFont smallfont(f);
181 smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE);
182 for (int i = 0; i < n; ++i) {
183 if (s[i].byte1 == 0 && islower(s[i].byte2)) {
184 c.byte2 = toupper(s[i].byte2);
185 result += ::XTextWidth16(getXFontstruct(smallfont), &c, 1);
187 result += ::XTextWidth16(getXFontstruct(f), &s[i], 1);
194 int lyxfont::XTextWidth(LyXFont const & f, char const * str, int count)
196 return ::XTextWidth(getXFontstruct(f), str, count);
200 int lyxfont::XTextWidth16(LyXFont const & f, XChar2b const * str, int count)
202 return ::XTextWidth16(getXFontstruct(f), str, count);
206 void lyxfont::XSetFont(Display * display, GC gc, LyXFont const & f)
208 ::XSetFont(display, gc, getFontID(f));
211 //} // end of namespace font
212 //} // end of namespace lyx