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