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