]> git.lyx.org Git - lyx.git/blob - src/font.C
Small fixes noticed by Jean-Marc.
[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+256*finfo->max_byte1) 
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+256*finfo->max_byte1) 
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+256*finfo->max_byte1) 
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+256*finfo->max_byte1) 
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, size_t 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                 LyXFont font(f);
114                 if (f.family() == LyXFont::SYMBOL_FAMILY) {
115 #ifdef USE_UNICODE_FOR_SYMBOLS
116                         //LyXFont font2 = f;
117                         font.setFamily(LyXFont::ROMAN_FAMILY);
118                         font.setShape(LyXFont::UP_SHAPE);
119                         //font = &font2;
120 #endif
121                         encoding = encodings.symbol_encoding();
122                 }
123                 for (size_t i = 0; i < n; ++i) {
124                         Uchar c = encoding->ucs(s[i]);
125                         xs[i].byte1 = c >> 8;
126                         xs[i].byte2 = c & 0xff;
127                 }
128                 int result = width(xs, n, font);
129                 delete[] xs;
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                 char c;
139                 LyXFont smallfont(f);
140                 smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE);
141                 for (size_t i = 0; i < n; ++i) {
142                         c = s[i];
143                         // when islower is a macro, the cast is needed (JMarc)
144                         if (islower(static_cast<unsigned char>(c))) {
145                                 c = toupper(c);
146                                 result += ::XTextWidth(getXFontstruct(smallfont), &c, 1);
147                         } else {
148                                 result += ::XTextWidth(getXFontstruct(f), &c, 1);
149                         }
150                 }
151                 return result;
152         }
153 }
154
155
156 int lyxfont::signedWidth(string const & s, LyXFont const & f)
157 {
158         if (s.empty()) return 0;
159         if (s[0] == '-')
160                 return -width(s.substr(1, s.length() - 1), f);
161         else
162                 return width(s, f);
163 }
164
165
166 //int lyxfont::width(wstring const & s, int n, LyXFont const & f)
167 int lyxfont::width(XChar2b const * s, int n, LyXFont const & f)
168 {
169         if (!lyxrc.use_gui)
170                 return n;
171         
172         if (f.realShape() != LyXFont::SMALLCAPS_SHAPE) {
173                 return ::XTextWidth16(getXFontstruct(f), s, n);
174         } else {
175                 // emulate smallcaps since X doesn't support this
176                 unsigned int result = 0;
177                 static XChar2b c = {0, 0};
178                 LyXFont smallfont(f);
179                 smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE);
180                 for (int i = 0; i < n; ++i) {
181                         if (s[i].byte1 == 0 && islower(s[i].byte2)) {
182                                 c.byte2 = toupper(s[i].byte2);
183                                 result += ::XTextWidth16(getXFontstruct(smallfont), &c, 1);
184                         } else {
185                                 result += ::XTextWidth16(getXFontstruct(f), &s[i], 1);
186                         }
187                 }
188                 return result;
189         }
190 }
191
192 int lyxfont::XTextWidth(LyXFont const & f, char const * str, int count)
193 {
194         return ::XTextWidth(getXFontstruct(f), str, count);
195 }
196
197
198 int lyxfont::XTextWidth16(LyXFont const & f, XChar2b const * str, int count)
199 {
200         return ::XTextWidth16(getXFontstruct(f), str, count);
201 }
202
203
204 void lyxfont::XSetFont(Display * display, GC gc, LyXFont const & f) 
205 {
206         ::XSetFont(display, gc, getFontID(f));
207 }
208
209 //} // end of namespace font
210 //} // end of namespace lyx