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