]> git.lyx.org Git - lyx.git/blob - src/font.C
Angus insetindex patch + protect patch from Dekel
[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-2000 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
24 // namespace {
25 static inline
26 XFontStruct * getXFontstruct(LyXFont const & f)
27 {
28         return fontloader.load(f.family(), f.series(),
29                                f.realShape(), f.size());
30 }
31
32
33 static inline
34 XID getFontID(LyXFont const & f)
35 {
36         return getXFontstruct(f)->fid;
37 }
38 // } // end of anon namespace
39
40 int lyxfont::maxAscent(LyXFont const & f)
41 {
42         return getXFontstruct(f)->ascent;
43 }
44
45
46 int lyxfont::maxDescent(LyXFont const & f)
47 {
48         return getXFontstruct(f)->descent;
49 }
50
51
52 int lyxfont::ascent(char c, LyXFont const & f)
53 {
54         XFontStruct * finfo = getXFontstruct(f);
55         unsigned int uc = static_cast<unsigned char>(c);
56         if (finfo->per_char
57             && uc >= finfo->min_char_or_byte2
58             && uc <= finfo->max_char_or_byte2) 
59                 return finfo->per_char[uc - finfo->min_char_or_byte2].ascent;
60         else
61                 return finfo->ascent;
62 }
63
64
65 int lyxfont::descent(char c, LyXFont const & f)
66 {
67         XFontStruct * finfo = getXFontstruct(f);
68         unsigned int uc = static_cast<unsigned char>(c);
69         if (finfo->per_char
70             && uc >= finfo->min_char_or_byte2
71             && uc <= finfo->max_char_or_byte2) 
72                 return finfo->per_char[uc - finfo->min_char_or_byte2].descent;
73         else
74                 return finfo->descent;
75 }
76
77
78 int lyxfont::lbearing(char c, LyXFont const & f)
79 {
80         XFontStruct * finfo = getXFontstruct(f);
81         unsigned int uc = static_cast<unsigned char>(c);
82         if (finfo->per_char
83             && uc >= finfo->min_char_or_byte2
84             && uc <= finfo->max_char_or_byte2) 
85                 return finfo->per_char[uc - finfo->min_char_or_byte2].lbearing;
86         else
87                 return 0;
88 }
89
90
91 int lyxfont::rbearing(char c, LyXFont const & f)
92 {
93         XFontStruct * finfo = getXFontstruct(f);
94         unsigned int uc = static_cast<unsigned char>(c);
95         if (finfo->per_char
96             && uc >= finfo->min_char_or_byte2
97             && uc <= finfo->max_char_or_byte2) 
98                 return finfo->per_char[uc - finfo->min_char_or_byte2].rbearing;
99         else
100                 return width(c, f);
101 }
102
103
104 int lyxfont::width(char const * s, int n, LyXFont const & f)
105 {
106         if (!lyxrc.use_gui)
107                 return n;
108
109         if (lyxrc.font_norm_type == LyXRC::ISO_10646_1) {
110                 XChar2b * xs = new XChar2b[n];
111                 Encoding const * encoding = f.language()->encoding();
112                 LyXFont const * font = &f; 
113                 if (f.family() == LyXFont::SYMBOL_FAMILY) {
114 #ifdef USE_UNICODE_FOR_SYMBOLS
115                         LyXFont font2 = f;
116                         font2.setFamily(LyXFont::ROMAN_FAMILY);
117                         font2.setShape(LyXFont::UP_SHAPE);
118                         font = &font2;
119 #endif
120                         encoding = &symbol_encoding;
121                 }
122                 for (int i = 0; i < n; ++i) {
123                         Uchar c = encoding->ucs(s[i]);
124                         xs[i].byte1 = c >> 8;
125                         xs[i].byte2 = c & 0xff;
126                 }
127                 int result = width(xs, n, *font);
128                 delete[] xs;
129                 return result;
130         }
131
132         if (f.realShape() != LyXFont::SMALLCAPS_SHAPE) {
133                 return ::XTextWidth(getXFontstruct(f), s, n);
134         } else {
135                 // emulate smallcaps since X doesn't support this
136                 unsigned int result = 0;
137                 char c;
138                 LyXFont smallfont(f);
139                 smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE);
140                 for (int i = 0; i < n; ++i) {
141                         c = s[i];
142                         // when islower is a macro, the cast is needed (JMarc)
143                         if (islower(static_cast<unsigned char>(c))) {
144                                 c = toupper(c);
145                                 result += ::XTextWidth(getXFontstruct(smallfont), &c, 1);
146                         } else {
147                                 result += ::XTextWidth(getXFontstruct(f), &c, 1);
148                         }
149                 }
150                 return result;
151         }
152 }
153
154
155 int lyxfont::signedWidth(string const & s, LyXFont const & f)
156 {
157         if (s.empty()) return 0;
158         if (s.c_str()[0] == '-')
159                 return -width(s.c_str() + 1, s.length() - 1, f);
160         else
161                 return width(s.c_str(), s.length(), f);
162 }
163
164
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 = {0, 0};
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 == 0 && islower(s[i].byte2)) {
180                                 c.byte2 = toupper(s[i].byte2);
181                                 result += ::XTextWidth16(getXFontstruct(smallfont), &c, 1);
182                         } else {
183                                 result += ::XTextWidth16(getXFontstruct(f), &s[i], 1);
184                         }
185                 }
186                 return result;
187         }
188 }
189
190 int lyxfont::XTextWidth(LyXFont const & f, char * str, int count)
191 {
192         return ::XTextWidth(getXFontstruct(f), str, count);
193 }
194
195
196 int lyxfont::XTextWidth16(LyXFont const & f, XChar2b * str, int count)
197 {
198         return ::XTextWidth16(getXFontstruct(f), str, count);
199 }
200
201
202 void lyxfont::XSetFont(Display * display, GC gc, LyXFont const & f) 
203 {
204         ::XSetFont(display, gc, getFontID(f));
205 }
206
207 //} // end of namespace font
208 //} // end of namespace lyx