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