]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/xfont_metrics.C
some reworking to allow Qt to successfully choose a serif default font
[lyx.git] / src / frontends / xforms / xfont_metrics.C
1 /**
2  * \file xfont_metrics.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author unknown
7  * \author John Levon
8  *
9  * Full author contact details are available in file CREDITS
10  */
11
12 #include <config.h>
13
14 #include "support/lstrings.h"
15 #include "xfont_metrics.h"
16 #include "xfont_loader.h"
17 #include "lyxrc.h"
18 #include "encoding.h"
19 #include "language.h"
20
21 #include <boost/scoped_array.hpp>
22
23 namespace {
24
25 inline
26 XFontStruct * getXFontstruct(LyXFont const & f)
27 {
28         return fontloader.load
29                 (f.family(), f.series(),
30                 f.realShape(), f.size());
31 }
32
33
34 inline
35 XID getFontID(LyXFont const & f)
36 {
37         return getXFontstruct(f)->fid;
38 }
39
40 } // namespace anon
41
42
43 namespace font_metrics {
44
45 int maxAscent(LyXFont const & f)
46 {
47         return getXFontstruct(f)->ascent;
48 }
49
50
51 int maxDescent(LyXFont const & f)
52 {
53         return getXFontstruct(f)->descent;
54 }
55
56
57 int ascent(char c, LyXFont const & f)
58 {
59         XFontStruct * finfo = getXFontstruct(f);
60         unsigned int uc = static_cast<unsigned char>(c);
61         if (finfo->per_char
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;
65         else
66                 return finfo->ascent;
67 }
68
69
70 int descent(char c, LyXFont const & f)
71 {
72         XFontStruct * finfo = getXFontstruct(f);
73         unsigned int uc = static_cast<unsigned char>(c);
74         if (finfo->per_char
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;
78         else
79                 return finfo->descent;
80 }
81
82
83 int lbearing(char c, LyXFont const & f)
84 {
85         XFontStruct * finfo = getXFontstruct(f);
86         unsigned int uc = static_cast<unsigned char>(c);
87         if (finfo->per_char
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;
91         else
92                 return 0;
93 }
94
95
96 int rbearing(char c, LyXFont const & f)
97 {
98         XFontStruct * finfo = getXFontstruct(f);
99         unsigned int uc = static_cast<unsigned char>(c);
100         if (finfo->per_char
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;
104         else
105                 return width(c, f);
106 }
107
108
109 int width(char const * s, size_t n, LyXFont const & f)
110 {
111         if (!lyx_gui::use_gui)
112                 return n;
113
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();
117                 LyXFont font(f);
118                 if (f.isSymbolFont()) {
119 #ifdef USE_UNICODE_FOR_SYMBOLS
120                         font.setFamily(LyXFont::ROMAN_FAMILY);
121                         font.setShape(LyXFont::UP_SHAPE);
122 #endif
123                         encoding = encodings.symbol_encoding();
124                 }
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;
129                 }
130                 int result = xfont_metrics::width(xs.get(), n, font);
131                 return result;
132         }
133
134         if (f.realShape() != LyXFont::SMALLCAPS_SHAPE) {
135                 return ::XTextWidth(getXFontstruct(f), s, n);
136         } else {
137                 // emulate smallcaps since X doesn't support this
138                 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]);
143                         if (c != s[i]) {
144                                 result += ::XTextWidth(getXFontstruct(smallfont), &c, 1);
145                         } else {
146                                 result += ::XTextWidth(getXFontstruct(f), &c, 1);
147                         }
148                 }
149                 return result;
150         }
151 }
152
153
154 int signedWidth(string const & s, LyXFont const & f)
155 {
156         if (s.empty())
157                 return 0;
158         if (s[0] == '-')
159                 return -width(s.substr(1, s.length() - 1), f);
160         else
161                 return width(s, f);
162 }
163
164
165 void rectText(string const & str, LyXFont const & font,
166         int & width,
167         int & ascent,
168         int & descent)
169 {
170         static int const d = 2;
171         width = font_metrics::width(str, font) + d * 2 + 2;
172         ascent = font_metrics::maxAscent(font) + d;
173         descent = font_metrics::maxDescent(font) + d;
174 }
175
176
177
178 void buttonText(string const & str, LyXFont const & font,
179         int & width,
180         int & ascent,
181         int & descent)
182 {
183         static int const d = 3;
184
185         width = font_metrics::width(str, font) + d * 2 + 2;
186         ascent = font_metrics::maxAscent(font) + d;
187         descent = font_metrics::maxDescent(font) + d;
188 }
189
190 } // namespace font_metrics
191
192 namespace xfont_metrics {
193
194 int width(XChar2b const * s, int n, LyXFont const & f)
195 {
196         if (!lyx_gui::use_gui)
197                 return n;
198
199         if (f.realShape() != LyXFont::SMALLCAPS_SHAPE) {
200                 return ::XTextWidth16(getXFontstruct(f), s, n);
201         } else {
202                 // emulate smallcaps since X doesn't support this
203                 int result = 0;
204                 static XChar2b c;
205                 LyXFont smallfont(f);
206                 smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE);
207                 for (int i = 0; i < n; ++i) {
208                         if (s[i].byte1)
209                                 c = s[i];
210                         else {
211                                 c.byte1 = s[i].byte1;
212                                 c.byte2 = uppercase(s[i].byte2);
213                         }
214                         if (c.byte2 != s[i].byte2) {
215                                 result += ::XTextWidth16(getXFontstruct(smallfont), &c, 1);
216                         } else {
217                                 result += ::XTextWidth16(getXFontstruct(f), &s[i], 1);
218                 }
219                 }
220                 return result;
221         }
222 }
223
224
225 int XTextWidth(LyXFont const & f, char const * str, int count)
226 {
227         return ::XTextWidth(getXFontstruct(f), str, count);
228 }
229
230
231 int XTextWidth16(LyXFont const & f, XChar2b const * str, int count)
232 {
233         return ::XTextWidth16(getXFontstruct(f), str, count);
234 }
235
236
237 /// hmm, not a metric !
238 void XSetFont(Display * display, GC gc, LyXFont const & f)
239 {
240         ::XSetFont(display, gc, getFontID(f));
241 }
242
243 } // namespace xfont_metrics