]> git.lyx.org Git - lyx.git/blob - src/font.C
Sall fix for reading DOS-encoded-lyx-files.
[lyx.git] / src / font.C
1 #include <config.h>
2
3 #include <cctype>
4
5 #include "font.h"
6 #include "FontLoader.h"
7 #include "lyxrc.h"
8 #include "encoding.h"
9
10 // namespace {
11 static inline
12 XFontStruct * getXFontstruct(LyXFont const & f)
13 {
14         return fontloader.load(f.family(), f.series(),
15                                f.realShape(), f.size());
16 }
17
18
19 static inline
20 XID getFontID(LyXFont const & f)
21 {
22         return getXFontstruct(f)->fid;
23 }
24 // } // end of anon namespace
25
26 int lyxfont::maxAscent(LyXFont const & f)
27 {
28         return getXFontstruct(f)->ascent;
29 }
30
31
32 int lyxfont::maxDescent(LyXFont const & f)
33 {
34         return getXFontstruct(f)->descent;
35 }
36
37
38 int lyxfont::ascent(char c, LyXFont const & f)
39 {
40         XFontStruct * finfo = getXFontstruct(f);
41         unsigned int uc = static_cast<unsigned char>(c);
42         if (finfo->per_char
43             && uc >= finfo->min_char_or_byte2
44             && uc <= finfo->max_char_or_byte2) 
45                 return finfo->per_char[uc - finfo->min_char_or_byte2].ascent;
46         else
47                 return finfo->ascent;
48 }
49
50
51 int lyxfont::descent(char c, LyXFont const & f)
52 {
53         XFontStruct * finfo = getXFontstruct(f);
54         unsigned int uc = static_cast<unsigned char>(c);
55         if (finfo->per_char
56             && uc >= finfo->min_char_or_byte2
57             && uc <= finfo->max_char_or_byte2) 
58                 return finfo->per_char[uc - finfo->min_char_or_byte2].descent;
59         else
60                 return finfo->descent;
61 }
62
63
64 int lyxfont::lbearing(char c, LyXFont const & f)
65 {
66         XFontStruct * finfo = getXFontstruct(f);
67         unsigned int uc = static_cast<unsigned char>(c);
68         if (finfo->per_char
69             && uc >= finfo->min_char_or_byte2
70             && uc <= finfo->max_char_or_byte2) 
71                 return finfo->per_char[uc - finfo->min_char_or_byte2].lbearing;
72         else
73                 return 0;
74 }
75
76
77 int lyxfont::rbearing(char c, LyXFont const & f)
78 {
79         XFontStruct * finfo = getXFontstruct(f);
80         unsigned int uc = static_cast<unsigned char>(c);
81         if (finfo->per_char
82             && uc >= finfo->min_char_or_byte2
83             && uc <= finfo->max_char_or_byte2) 
84                 return finfo->per_char[uc - finfo->min_char_or_byte2].rbearing;
85         else
86                 return width(c, f);
87 }
88
89
90 int lyxfont::width(char const * s, int n, LyXFont const & f)
91 {
92         if (!lyxrc.use_gui)
93                 return n;
94
95         if (lyxrc.font_norm_type == LyXRC::ISO_10646_1) {
96                 XChar2b * xs = new XChar2b[n];
97                 Encoding const * enc = f.language()->encoding();
98                 for (int i = 0; i < n; ++i) {
99                         Uchar c = enc->ucs(s[i]);
100                         xs[i].byte1 = c >> 8;
101                         xs[i].byte2 = c & 0xff;
102                 }
103                 int result = width(xs, n, f);
104                 delete[] xs;
105                 return result;
106         }
107
108         if (f.realShape() != LyXFont::SMALLCAPS_SHAPE) {
109                 return ::XTextWidth(getXFontstruct(f), s, n);
110         } else {
111                 // emulate smallcaps since X doesn't support this
112                 unsigned int result = 0;
113                 char c;
114                 LyXFont smallfont(f);
115                 smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE);
116                 for (int i = 0; i < n; ++i) {
117                         c = s[i];
118                         // when islower is a macro, the cast is needed (JMarc)
119                         if (islower(static_cast<unsigned char>(c))) {
120                                 c = toupper(c);
121                                 result += ::XTextWidth(getXFontstruct(smallfont), &c, 1);
122                         } else {
123                                 result += ::XTextWidth(getXFontstruct(f), &c, 1);
124                         }
125                 }
126                 return result;
127         }
128 }
129
130
131 int lyxfont::signedWidth(string const & s, LyXFont const & f)
132 {
133         if (s.empty()) return 0;
134         if (s.c_str()[0] == '-')
135                 return -width(s.c_str() + 1, s.length() - 1, f);
136         else
137                 return width(s.c_str(), s.length(), f);
138 }
139
140
141 int lyxfont::width(XChar2b const * s, int n, LyXFont const & f)
142 {
143         if (!lyxrc.use_gui)
144                 return n;
145         
146         if (f.realShape() != LyXFont::SMALLCAPS_SHAPE) {
147                 return ::XTextWidth16(getXFontstruct(f), s, n);
148         } else {
149                 // emulate smallcaps since X doesn't support this
150                 unsigned int result = 0;
151                 static XChar2b c = {0, 0};
152                 LyXFont smallfont(f);
153                 smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE);
154                 for (int i = 0; i < n; ++i) {
155                         if (s[i].byte1 == 0 && islower(s[i].byte2)) {
156                                 c.byte2 = toupper(s[i].byte2);
157                                 result += ::XTextWidth16(getXFontstruct(smallfont), &c, 1);
158                         } else {
159                                 result += ::XTextWidth16(getXFontstruct(f), &s[i], 1);
160                         }
161                 }
162                 return result;
163         }
164 }
165
166 int lyxfont::XTextWidth(LyXFont const & f, char * str, int count)
167 {
168         return ::XTextWidth(getXFontstruct(f), str, count);
169 }
170
171
172 int lyxfont::XTextWidth16(LyXFont const & f, XChar2b * str, int count)
173 {
174         return ::XTextWidth16(getXFontstruct(f), str, count);
175 }
176
177
178 void lyxfont::XSetFont(Display * display, GC gc, LyXFont const & f) 
179 {
180         ::XSetFont(display, gc, getFontID(f));
181 }
182
183 //} // end of namespace font
184 //} // end of namespace lyx