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