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