]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/xfont_metrics.C
font loader / color handler move
[lyx.git] / src / frontends / xforms / xfont_metrics.C
1 /* This file is part of
2  * ======================================================
3  *
4  *           LyX, The Document Processor
5  *
6  *           Copyright 1995 Matthias Ettrich
7  *           Copyright 1995-2001 The LyX Team.
8  *
9  * ====================================================== */
10
11 #include <config.h>
12
13 #ifdef __GNUG__
14 #pragma implementation
15 #endif
16
17 #include "support/lstrings.h"
18 #include "xfont_metrics.h"
19 #include "xfont_loader.h"
20 #include "lyxrc.h"
21 #include "encoding.h"
22 #include "language.h"
23
24 #include <boost/scoped_array.hpp>
25
26 namespace {
27
28 inline
29 XFontStruct * getXFontstruct(LyXFont const & f)
30 {
31         return fontloader.load(f.family(), f.series(),
32                                f.realShape(), f.size());
33 }
34
35
36 inline
37 XID getFontID(LyXFont const & f)
38 {
39         return getXFontstruct(f)->fid;
40 }
41
42 } // namespace anon
43
44 int font_metrics::maxAscent(LyXFont const & f)
45 {
46         return getXFontstruct(f)->ascent;
47 }
48
49
50 int font_metrics::maxDescent(LyXFont const & f)
51 {
52         return getXFontstruct(f)->descent;
53 }
54
55
56 int font_metrics::ascent(char c, LyXFont const & f)
57 {
58         XFontStruct * finfo = getXFontstruct(f);
59         unsigned int uc = static_cast<unsigned char>(c);
60         if (finfo->per_char
61             && uc >= finfo->min_char_or_byte2
62             && uc <= finfo->max_char_or_byte2+256*finfo->max_byte1)
63                 return finfo->per_char[uc - finfo->min_char_or_byte2].ascent;
64         else
65                 return finfo->ascent;
66 }
67
68
69 int font_metrics::descent(char c, LyXFont const & f)
70 {
71         XFontStruct * finfo = getXFontstruct(f);
72         unsigned int uc = static_cast<unsigned char>(c);
73         if (finfo->per_char
74             && uc >= finfo->min_char_or_byte2
75             && uc <= finfo->max_char_or_byte2+256*finfo->max_byte1)
76                 return finfo->per_char[uc - finfo->min_char_or_byte2].descent;
77         else
78                 return finfo->descent;
79 }
80
81
82 int font_metrics::lbearing(char c, LyXFont const & f)
83 {
84         XFontStruct * finfo = getXFontstruct(f);
85         unsigned int uc = static_cast<unsigned char>(c);
86         if (finfo->per_char
87             && uc >= finfo->min_char_or_byte2
88             && uc <= finfo->max_char_or_byte2+256*finfo->max_byte1)
89                 return finfo->per_char[uc - finfo->min_char_or_byte2].lbearing;
90         else
91                 return 0;
92 }
93
94
95 int font_metrics::rbearing(char c, LyXFont const & f)
96 {
97         XFontStruct * finfo = getXFontstruct(f);
98         unsigned int uc = static_cast<unsigned char>(c);
99         if (finfo->per_char
100             && uc >= finfo->min_char_or_byte2
101             && uc <= finfo->max_char_or_byte2+256*finfo->max_byte1)
102                 return finfo->per_char[uc - finfo->min_char_or_byte2].rbearing;
103         else
104                 return width(c, f);
105 }
106
107
108 int font_metrics::width(char c, LyXFont const & f)
109
110         return width(&c, 1, f);
111 }
112
113
114 int font_metrics::width(string const & s, LyXFont const & f)
115 {
116         if (s.empty()) return 0;
117         return width(s.data(), s.length(), f);
118 }
119  
120  
121 int font_metrics::width(char const * s, size_t n, LyXFont const & f)
122 {
123         if (!lyxrc.use_gui)
124                 return n;
125
126         if (lyxrc.font_norm_type == LyXRC::ISO_10646_1) {
127                 boost::scoped_array<XChar2b> xs(new XChar2b[n]);
128                 Encoding const * encoding = f.language()->encoding();
129                 LyXFont font(f);
130                 if (f.isSymbolFont()) {
131 #ifdef USE_UNICODE_FOR_SYMBOLS
132                         font.setFamily(LyXFont::ROMAN_FAMILY);
133                         font.setShape(LyXFont::UP_SHAPE);
134 #endif
135                         encoding = encodings.symbol_encoding();
136                 }
137                 for (size_t i = 0; i < n; ++i) {
138                         Uchar c = encoding->ucs(s[i]);
139                         xs[i].byte1 = c >> 8;
140                         xs[i].byte2 = c & 0xff;
141                 }
142                 int result = width(xs.get(), n, font);
143                 return result;
144         }
145
146         if (f.realShape() != LyXFont::SMALLCAPS_SHAPE) {
147                 return ::XTextWidth(getXFontstruct(f), s, n);
148         } else {
149                 // emulate smallcaps since X doesn't support this
150                 unsigned int result = 0;
151                 LyXFont smallfont(f);
152                 smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE);
153                 for (size_t i = 0; i < n; ++i) {
154                         char const c = uppercase(s[i]);
155                         if (c != s[i]) {
156                                 result += ::XTextWidth(getXFontstruct(smallfont), &c, 1);
157                         } else {
158                                 result += ::XTextWidth(getXFontstruct(f), &c, 1);
159                         }
160                 }
161                 return result;
162         }
163 }
164
165
166 int font_metrics::signedWidth(string const & s, LyXFont const & f)
167 {
168         if (s.empty())
169                 return 0;
170         if (s[0] == '-')
171                 return -width(s.substr(1, s.length() - 1), f);
172         else
173                 return width(s, f);
174 }
175
176
177 //int font_metrics::width(wstring const & s, int n, LyXFont const & f)
178 int font_metrics::width(XChar2b const * s, int n, LyXFont const & f)
179 {
180         if (!lyxrc.use_gui)
181                 return n;
182
183         if (f.realShape() != LyXFont::SMALLCAPS_SHAPE) {
184                 return ::XTextWidth16(getXFontstruct(f), s, n);
185         } else {
186                 // emulate smallcaps since X doesn't support this
187                 unsigned int result = 0;
188                 static XChar2b c;
189                 LyXFont smallfont(f);
190                 smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE);
191                 for (int i = 0; i < n; ++i) {
192                         if (s[i].byte1)
193                                 c = s[i];
194                         else {
195                                 c.byte1 = s[i].byte1;
196                                 c.byte2 = uppercase(s[i].byte2);
197                         }
198                         if (c.byte2 != s[i].byte2) {
199                                 result += ::XTextWidth16(getXFontstruct(smallfont), &c, 1);
200                         } else {
201                                 result += ::XTextWidth16(getXFontstruct(f), &s[i], 1);
202                 }
203                 }
204                 return result;
205         }
206 }
207
208 int font_metrics::XTextWidth(LyXFont const & f, char const * str, int count)
209 {
210         return ::XTextWidth(getXFontstruct(f), str, count);
211 }
212
213
214 int font_metrics::XTextWidth16(LyXFont const & f, XChar2b const * str, int count)
215 {
216         return ::XTextWidth16(getXFontstruct(f), str, count);
217 }
218
219
220 void font_metrics::XSetFont(Display * display, GC gc, LyXFont const & f)
221 {
222         ::XSetFont(display, gc, getFontID(f));
223 }
224
225
226 void font_metrics::rectText(string const & str, LyXFont const & font,
227               int & width, int & ascent, int & descent)
228 {
229         static int const d = 2;
230         width = font_metrics::width(str, font) + d * 2 + 2;
231         ascent = font_metrics::maxAscent(font) + d;
232         descent = font_metrics::maxDescent(font) + d;
233 }
234
235
236
237 void font_metrics::buttonText(string const & str, LyXFont const & font,
238                 int & width, int & ascent, int & descent)
239 {
240         static int const d = 3;
241
242         width = font_metrics::width(str, font) + d * 2 + 2;
243         ascent = font_metrics::maxAscent(font) + d;
244         descent = font_metrics::maxDescent(font) + d;
245 }
246
247
248 //} // end of namespace font
249 //} // end of namespace lyx