]> git.lyx.org Git - lyx.git/blob - src/font.C
form para crash fix from John
[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 <cctype>
18
19 #include "font.h"
20 #include "FontLoader.h"
21 #include "lyxrc.h"
22 #include "encoding.h"
23 #include "language.h"
24
25 namespace {
26
27 inline
28 XFontStruct * getXFontstruct(LyXFont const & f)
29 {
30         return fontloader.load(f.family(), f.series(),
31                                f.realShape(), f.size());
32 }
33
34
35 inline
36 XID getFontID(LyXFont const & f)
37 {
38         return getXFontstruct(f)->fid;
39 }
40
41 } // namespace anon
42
43 int lyxfont::maxAscent(LyXFont const & f)
44 {
45         return getXFontstruct(f)->ascent;
46 }
47
48
49 int lyxfont::maxDescent(LyXFont const & f)
50 {
51         return getXFontstruct(f)->descent;
52 }
53
54
55 int lyxfont::ascent(char c, LyXFont const & f)
56 {
57         XFontStruct * finfo = getXFontstruct(f);
58         unsigned int uc = static_cast<unsigned char>(c);
59         if (finfo->per_char
60             && uc >= finfo->min_char_or_byte2
61             && uc <= finfo->max_char_or_byte2+256*finfo->max_byte1) 
62                 return finfo->per_char[uc - finfo->min_char_or_byte2].ascent;
63         else
64                 return finfo->ascent;
65 }
66
67
68 int lyxfont::descent(char c, LyXFont const & f)
69 {
70         XFontStruct * finfo = getXFontstruct(f);
71         unsigned int uc = static_cast<unsigned char>(c);
72         if (finfo->per_char
73             && uc >= finfo->min_char_or_byte2
74             && uc <= finfo->max_char_or_byte2+256*finfo->max_byte1) 
75                 return finfo->per_char[uc - finfo->min_char_or_byte2].descent;
76         else
77                 return finfo->descent;
78 }
79
80
81 int lyxfont::lbearing(char c, LyXFont const & f)
82 {
83         XFontStruct * finfo = getXFontstruct(f);
84         unsigned int uc = static_cast<unsigned char>(c);
85         if (finfo->per_char
86             && uc >= finfo->min_char_or_byte2
87             && uc <= finfo->max_char_or_byte2+256*finfo->max_byte1) 
88                 return finfo->per_char[uc - finfo->min_char_or_byte2].lbearing;
89         else
90                 return 0;
91 }
92
93
94 int lyxfont::rbearing(char c, LyXFont const & f)
95 {
96         XFontStruct * finfo = getXFontstruct(f);
97         unsigned int uc = static_cast<unsigned char>(c);
98         if (finfo->per_char
99             && uc >= finfo->min_char_or_byte2
100             && uc <= finfo->max_char_or_byte2+256*finfo->max_byte1) 
101                 return finfo->per_char[uc - finfo->min_char_or_byte2].rbearing;
102         else
103                 return width(c, f);
104 }
105
106
107 int lyxfont::width(char const * s, size_t n, LyXFont const & f)
108 {
109         if (!lyxrc.use_gui)
110                 return n;
111
112         if (lyxrc.font_norm_type == LyXRC::ISO_10646_1) {
113                 XChar2b * xs = new XChar2b[n];
114                 Encoding const * encoding = f.language()->encoding();
115                 //LyXFont const * font = &f;
116                 LyXFont font(f);
117                 if (f.isSymbolFont()) {
118 #ifdef USE_UNICODE_FOR_SYMBOLS
119                         //LyXFont font2 = f;
120                         font.setFamily(LyXFont::ROMAN_FAMILY);
121                         font.setShape(LyXFont::UP_SHAPE);
122                         //font = &font2;
123 #endif
124                         encoding = encodings.symbol_encoding();
125                 }
126                 for (size_t i = 0; i < n; ++i) {
127                         Uchar c = encoding->ucs(s[i]);
128                         xs[i].byte1 = c >> 8;
129                         xs[i].byte2 = c & 0xff;
130                 }
131                 int result = width(xs, n, font);
132                 delete[] xs;
133                 return result;
134         }
135
136         if (f.realShape() != LyXFont::SMALLCAPS_SHAPE) {
137                 return ::XTextWidth(getXFontstruct(f), s, n);
138         } else {
139                 // emulate smallcaps since X doesn't support this
140                 unsigned int result = 0;
141                 char c;
142                 LyXFont smallfont(f);
143                 smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE);
144                 for (size_t i = 0; i < n; ++i) {
145                         c = s[i];
146                         // when islower is a macro, the cast is needed (JMarc)
147                         if (islower(static_cast<unsigned char>(c))) {
148                                 c = toupper(c);
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 lyxfont::signedWidth(string const & s, LyXFont const & f)
160 {
161         if (s.empty()) 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 //int lyxfont::width(wstring const & s, int n, LyXFont const & f)
170 int lyxfont::width(XChar2b const * s, int n, LyXFont const & f)
171 {
172         if (!lyxrc.use_gui)
173                 return n;
174         
175         if (f.realShape() != LyXFont::SMALLCAPS_SHAPE) {
176                 return ::XTextWidth16(getXFontstruct(f), s, n);
177         } else {
178                 // emulate smallcaps since X doesn't support this
179                 unsigned int result = 0;
180                 static XChar2b c;
181                 LyXFont smallfont(f);
182                 smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE);
183                 for (int i = 0; i < n; ++i) {
184                         if (s[i].byte1 == 0 && islower(s[i].byte2)) {
185                                 c.byte2 = toupper(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