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