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