]> git.lyx.org Git - lyx.git/blob - src/font.C
1aae9a6e43569a54828bf0afebd3b84673ad29c3
[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 * encoding = f.language()->encoding();
98                 LyXFont const * font = &f; 
99                 if (f.family() == LyXFont::SYMBOL_FAMILY) {
100 #ifdef USE_UNICODE_FOR_SYMBOLS
101                         LyXFont font2 = f;
102                         font2.setFamily(LyXFont::ROMAN_FAMILY);
103                         font2.setShape(LyXFont::UP_SHAPE);
104                         font = &font2;
105 #endif
106                         encoding = &symbol_encoding;
107                 }
108                 for (int i = 0; i < n; ++i) {
109                         Uchar c = encoding->ucs(s[i]);
110                         xs[i].byte1 = c >> 8;
111                         xs[i].byte2 = c & 0xff;
112                 }
113                 int result = width(xs, n, *font);
114                 delete[] xs;
115                 return result;
116         }
117
118         if (f.realShape() != LyXFont::SMALLCAPS_SHAPE) {
119                 return ::XTextWidth(getXFontstruct(f), s, n);
120         } else {
121                 // emulate smallcaps since X doesn't support this
122                 unsigned int result = 0;
123                 char c;
124                 LyXFont smallfont(f);
125                 smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE);
126                 for (int i = 0; i < n; ++i) {
127                         c = s[i];
128                         // when islower is a macro, the cast is needed (JMarc)
129                         if (islower(static_cast<unsigned char>(c))) {
130                                 c = toupper(c);
131                                 result += ::XTextWidth(getXFontstruct(smallfont), &c, 1);
132                         } else {
133                                 result += ::XTextWidth(getXFontstruct(f), &c, 1);
134                         }
135                 }
136                 return result;
137         }
138 }
139
140
141 int lyxfont::signedWidth(string const & s, LyXFont const & f)
142 {
143         if (s.empty()) return 0;
144         if (s.c_str()[0] == '-')
145                 return -width(s.c_str() + 1, s.length() - 1, f);
146         else
147                 return width(s.c_str(), s.length(), f);
148 }
149
150
151 int lyxfont::width(XChar2b const * s, int n, LyXFont const & f)
152 {
153         if (!lyxrc.use_gui)
154                 return n;
155         
156         if (f.realShape() != LyXFont::SMALLCAPS_SHAPE) {
157                 return ::XTextWidth16(getXFontstruct(f), s, n);
158         } else {
159                 // emulate smallcaps since X doesn't support this
160                 unsigned int result = 0;
161                 static XChar2b c = {0, 0};
162                 LyXFont smallfont(f);
163                 smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE);
164                 for (int i = 0; i < n; ++i) {
165                         if (s[i].byte1 == 0 && islower(s[i].byte2)) {
166                                 c.byte2 = toupper(s[i].byte2);
167                                 result += ::XTextWidth16(getXFontstruct(smallfont), &c, 1);
168                         } else {
169                                 result += ::XTextWidth16(getXFontstruct(f), &s[i], 1);
170                         }
171                 }
172                 return result;
173         }
174 }
175
176 int lyxfont::XTextWidth(LyXFont const & f, char * str, int count)
177 {
178         return ::XTextWidth(getXFontstruct(f), str, count);
179 }
180
181
182 int lyxfont::XTextWidth16(LyXFont const & f, XChar2b * str, int count)
183 {
184         return ::XTextWidth16(getXFontstruct(f), str, count);
185 }
186
187
188 void lyxfont::XSetFont(Display * display, GC gc, LyXFont const & f) 
189 {
190         ::XSetFont(display, gc, getFontID(f));
191 }
192
193 //} // end of namespace font
194 //} // end of namespace lyx