Debian Codegraph @

src/fontenc.c

raw
1 /*
2 Copyright (c) 1998-2001 by Juliusz Chroboczek
3
4 Permission is hereby granted, free of charge, to any person obtaining a copy
5 of this software and associated documentation files (the "Software"), to deal
6 in the Software without restriction, including without limitation the rights
7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 copies of the Software, and to permit persons to whom the Software is
9 furnished to do so, subject to the following conditions:
10
11 The above copyright notice and this permission notice shall be included in
12 all copies or substantial portions of the Software.
13
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 THE SOFTWARE.
21 */
22
23 /* Backend-independent encoding code */
24
25 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
28
29 #include <string.h>
30 #include <strings.h>
31 #include <stdlib.h>
32
33 #define FALSE 0
34 #define TRUE 1
35 #define MAXFONTNAMELEN 1024
36 #define MAXFONTFILENAMELEN 1024
37
38 #include <X11/fonts/fontenc.h>
39 #include "fontencI.h"
40 #include "reallocarray.h"
41
42 /* Functions local to this file */
43
44 static FontEncPtr FontEncLoad(const char *, const char *);
45
46 /* Early versions of this code only knew about hardwired encodings,
47 hence the following data. Now that the code knows how to load an
48 encoding from a file, most of these tables could go away. */
49
50 /* At any rate, no new hardcoded encodings will be added. */
51
52 static FontMapRec iso10646[] = {
53 {FONT_ENCODING_UNICODE, 0, 0, NULL, NULL, NULL, NULL, NULL},
54 {0, 0, 0, NULL, NULL, NULL, NULL, NULL}
55 };
56
57 /* Notice that the Apple encodings do not have all the characters in
58 the corresponding ISO 8859, and therefore the table has some holes.
59 There's not much more we can do with fonts without a Unicode cmap
60 unless we are willing to combine cmaps (which we are not). */
61
62 static const unsigned short
63 iso8859_1_apple_roman[] = {
64 0xCA, 0xC1, 0xA2, 0xA3, 0xDB, 0xB4, 0x00, 0xA4,
65 0xAC, 0xA9, 0xBB, 0xC7, 0xC2, 0x00, 0xA8, 0xF8,
66 0xA1, 0xB1, 0x00, 0x00, 0xAB, 0xB5, 0xA6, 0xE1,
67 0xFC, 0x00, 0xBC, 0xC8, 0x00, 0x00, 0x00, 0xC0,
68 0xCB, 0xE7, 0xE5, 0xCC, 0x80, 0x81, 0xAE, 0x82,
69 0xE9, 0x83, 0xE6, 0xE8, 0xED, 0xEA, 0xEB, 0xEC,
70 0x00, 0x84, 0xF1, 0xEE, 0xEF, 0xCD, 0x85, 0x00,
71 0xAF, 0xF4, 0xF2, 0xF3, 0x86, 0x00, 0x00, 0xA7,
72 0x88, 0x87, 0x89, 0x8B, 0x8A, 0x8C, 0xBE, 0x8D,
73 0x8F, 0x8E, 0x90, 0x91, 0x93, 0x92, 0x94, 0x95,
74 0x00, 0x96, 0x98, 0x97, 0x99, 0x9B, 0x9A, 0xD6,
75 0xBF, 0x9D, 0x9C, 0x9E, 0x9F, 0x00, 0x00, 0xD8
76 };
77
78 /* Cannot use simple_recode because need to eliminate 0x80<=code<0xA0 */
79 static unsigned
80 iso8859_1_to_apple_roman(unsigned isocode, void *client_data)
81 {
82 if (isocode <= 0x80)
83 return isocode;
84 else if (isocode >= 0xA0)
85 return iso8859_1_apple_roman[isocode - 0xA0];
86 else
87 return 0;
88 }
89
90 static FontMapRec iso8859_1[] = {
91 {FONT_ENCODING_TRUETYPE, 2, 2, NULL, NULL, NULL, NULL, NULL}, /* ISO 8859-1 */
92 {FONT_ENCODING_UNICODE, 0, 0, NULL, NULL, NULL, NULL, NULL}, /* ISO 8859-1 coincides with Unicode */
93 {FONT_ENCODING_TRUETYPE, 1, 0, iso8859_1_to_apple_roman, NULL, NULL, NULL,
94 NULL},
95 {0, 0, 0, NULL, NULL, NULL, NULL, NULL}
96 };
97
98 static const unsigned short iso8859_2_tophalf[] = {
99 0x00A0, 0x0104, 0x02D8, 0x0141, 0x00A4, 0x013D, 0x015A, 0x00A7,
100 0x00A8, 0x0160, 0x015E, 0x0164, 0x0179, 0x00AD, 0x017D, 0x017B,
101 0x00B0, 0x0105, 0x02DB, 0x0142, 0x00B4, 0x013E, 0x015B, 0x02C7,
102 0x00B8, 0x0161, 0x015F, 0x0165, 0x017A, 0x02DD, 0x017E, 0x017C,
103 0x0154, 0x00C1, 0x00C2, 0x0102, 0x00C4, 0x0139, 0x0106, 0x00C7,
104 0x010C, 0x00C9, 0x0118, 0x00CB, 0x011A, 0x00CD, 0x00CE, 0x010E,
105 0x0110, 0x0143, 0x0147, 0x00D3, 0x00D4, 0x0150, 0x00D6, 0x00D7,
106 0x0158, 0x016E, 0x00DA, 0x0170, 0x00DC, 0x00DD, 0x0162, 0x00DF,
107 0x0155, 0x00E1, 0x00E2, 0x0103, 0x00E4, 0x013A, 0x0107, 0x00E7,
108 0x010D, 0x00E9, 0x0119, 0x00EB, 0x011B, 0x00ED, 0x00EE, 0x010F,
109 0x0111, 0x0144, 0x0148, 0x00F3, 0x00F4, 0x0151, 0x00F6, 0x00F7,
110 0x0159, 0x016F, 0x00FA, 0x0171, 0x00FC, 0x00FD, 0x0163, 0x02D9
111 };
112
113 static FontEncSimpleMapRec iso8859_2_to_unicode_map =
114 { 0x60, 0, 0xA0, iso8859_2_tophalf };
115
116 static const unsigned short iso8859_2_apple_centeuro[] = {
117 0xCA, 0x84, 0x00, 0xFC, 0x00, 0xBB, 0xE5, 0xA4,
118 0xAC, 0xE1, 0x00, 0xE8, 0x8F, 0x00, 0xEB, 0xFB,
119 0xA1, 0x88, 0x00, 0xB8, 0x00, 0xBC, 0xE6, 0xFF,
120 0x00, 0xE4, 0x00, 0xE9, 0x90, 0x00, 0xEC, 0xFD,
121 0xD9, 0xE7, 0x00, 0x00, 0x80, 0xBD, 0x8C, 0x00,
122 0x89, 0x83, 0xA2, 0x00, 0x9D, 0xEA, 0x00, 0x91,
123 0x00, 0xC1, 0xC5, 0xEE, 0xEF, 0xCC, 0x85, 0x00,
124 0xDB, 0xF1, 0xF2, 0xF4, 0x86, 0xF8, 0x00, 0xA7,
125 0xDA, 0x87, 0x00, 0x00, 0x8A, 0xBE, 0x8D, 0x00,
126 0x8B, 0x8E, 0xAB, 0x00, 0x9E, 0x92, 0x00, 0x93,
127 0x00, 0xC4, 0xCB, 0x97, 0x99, 0xCE, 0x9A, 0xD6,
128 0xDE, 0xF3, 0x9C, 0xF5, 0x9F, 0xF9, 0x00, 0x00
129 };
130
131 static unsigned
132 iso8859_2_to_apple_centeuro(unsigned isocode, void *client_data)
133 {
134 if (isocode <= 0x80)
135 return isocode;
136 else if (isocode >= 0xA0)
137 return iso8859_2_apple_centeuro[isocode - 0xA0];
138 else
139 return 0;
140 }
141
142 static FontMapRec iso8859_2[] = {
143 {FONT_ENCODING_UNICODE, 0, 0,
144 FontEncSimpleRecode, NULL, &iso8859_2_to_unicode_map, NULL, NULL},
145 {FONT_ENCODING_TRUETYPE, 1, 29, iso8859_2_to_apple_centeuro, NULL, NULL,
146 NULL, NULL},
147 {0, 0, 0, NULL, NULL, NULL, NULL, NULL}
148 };
149
150 static const unsigned short iso8859_3_tophalf[] = {
151 0x00A0, 0x0126, 0x02D8, 0x00A3, 0x00A4, 0x0000, 0x0124, 0x00A7,
152 0x00A8, 0x0130, 0x015E, 0x011E, 0x0134, 0x00AD, 0x0000, 0x017B,
153 0x00B0, 0x0127, 0x00B2, 0x00B3, 0x00B4, 0x00B5, 0x0125, 0x00B7,
154 0x00B8, 0x0131, 0x015F, 0x011F, 0x0135, 0x00BD, 0x0000, 0x017C,
155 0x00C0, 0x00C1, 0x00C2, 0x0000, 0x00C4, 0x010A, 0x0108, 0x00C7,
156 0x00C8, 0x00C9, 0x00CA, 0x00CB, 0x00CC, 0x00CD, 0x00CE, 0x00CF,
157 0x0000, 0x00D1, 0x00D2, 0x00D3, 0x00D4, 0x0120, 0x00D6, 0x00D7,
158 0x011C, 0x00D9, 0x00DA, 0x00DB, 0x00DC, 0x016C, 0x015C, 0x00DF,
159 0x00E0, 0x00E1, 0x00E2, 0x0000, 0x00E4, 0x010B, 0x0109, 0x00E7,
160 0x00E8, 0x00E9, 0x00EA, 0x00EB, 0x00EC, 0x00ED, 0x00EE, 0x00EF,
161 0x0000, 0x00F1, 0x00F2, 0x00F3, 0x00F4, 0x0121, 0x00F6, 0x00F7,
162 0x011D, 0x00F9, 0x00FA, 0x00FB, 0x00FC, 0x016D, 0x015D, 0x02D9
163 };
164
165 static FontEncSimpleMapRec iso8859_3_to_unicode_map =
166 { 0x60, 0, 0xA0, iso8859_3_tophalf };
167
168 static FontMapRec iso8859_3[] = {
169 {FONT_ENCODING_UNICODE, 0, 0,
170 FontEncSimpleRecode, NULL, &iso8859_3_to_unicode_map, NULL, NULL},
171 {0, 0, 0, NULL, NULL, NULL, NULL, NULL}
172 };
173
174 static const unsigned short iso8859_4_tophalf[] = {
175 0x00A0, 0x0104, 0x0138, 0x0156, 0x00A4, 0x0128, 0x013B, 0x00A7,
176 0x00A8, 0x0160, 0x0112, 0x0122, 0x0166, 0x00AD, 0x017D, 0x00AF,
177 0x00B0, 0x0105, 0x02DB, 0x0157, 0x00B4, 0x0129, 0x013C, 0x02C7,
178 0x00B8, 0x0161, 0x0113, 0x0123, 0x0167, 0x014A, 0x017E, 0x014B,
179 0x0100, 0x00C1, 0x00C2, 0x00C3, 0x00C4, 0x00C5, 0x00C6, 0x012E,
180 0x010C, 0x00C9, 0x0118, 0x00CB, 0x0116, 0x00CD, 0x00CE, 0x012A,
181 0x0110, 0x0145, 0x014C, 0x0136, 0x00D4, 0x00D5, 0x00D6, 0x00D7,
182 0x00D8, 0x0172, 0x00DA, 0x00DB, 0x00DC, 0x0168, 0x016A, 0x00DF,
183 0x0101, 0x00E1, 0x00E2, 0x00E3, 0x00E4, 0x00E5, 0x00E6, 0x012F,
184 0x010D, 0x00E9, 0x0119, 0x00EB, 0x0117, 0x00ED, 0x00EE, 0x012B,
185 0x0111, 0x0146, 0x014D, 0x0137, 0x00F4, 0x00F5, 0x00F6, 0x00F7,
186 0x00F8, 0x0173, 0x00FA, 0x00FB, 0x00FC, 0x0169, 0x016B, 0x02D9,
187 };
188
189 static FontEncSimpleMapRec iso8859_4_to_unicode_map =
190 { 0x60, 0, 0xA0, iso8859_4_tophalf };
191
192 static FontMapRec iso8859_4[] = {
193 {FONT_ENCODING_UNICODE, 0, 0, FontEncSimpleRecode, NULL,
194 &iso8859_4_to_unicode_map, NULL, NULL},
195 {0, 0, 0, NULL, NULL, NULL, NULL, NULL}
196 };
197
198 static const unsigned short iso8859_5_tophalf[] = {
199 0x00A0, 0x0401, 0x0402, 0x0403, 0x0404, 0x0405, 0x0406, 0x0407,
200 0x0408, 0x0409, 0x040A, 0x040B, 0x040C, 0x00AD, 0x040E, 0x040F,
201 0x0410, 0x0411, 0x0412, 0x0413, 0x0414, 0x0415, 0x0416, 0x0417,
202 0x0418, 0x0419, 0x041A, 0x041B, 0x041C, 0x041D, 0x041E, 0x041F,
203 0x0420, 0x0421, 0x0422, 0x0423, 0x0424, 0x0425, 0x0426, 0x0427,
204 0x0428, 0x0429, 0x042A, 0x042B, 0x042C, 0x042D, 0x042E, 0x042F,
205 0x0430, 0x0431, 0x0432, 0x0433, 0x0434, 0x0435, 0x0436, 0x0437,
206 0x0438, 0x0439, 0x043A, 0x043B, 0x043C, 0x043D, 0x043E, 0x043F,
207 0x0440, 0x0441, 0x0442, 0x0443, 0x0444, 0x0445, 0x0446, 0x0447,
208 0x0448, 0x0449, 0x044A, 0x044B, 0x044C, 0x044D, 0x044E, 0x044F,
209 0x2116, 0x0451, 0x0452, 0x0453, 0x0454, 0x0455, 0x0456, 0x0457,
210 0x0458, 0x0459, 0x045A, 0x045B, 0x045C, 0x00A7, 0x045E, 0x045F
211 };
212
213 static FontEncSimpleMapRec iso8859_5_to_unicode_map =
214 { 0x60, 0, 0xA0, iso8859_5_tophalf };
215
216 static const unsigned short iso8859_5_apple_cyrillic[] = {
217 0xCA, 0xDD, 0xAB, 0xAE, 0xB8, 0xC1, 0xA7, 0xBA,
218 0xB7, 0xBC, 0xBE, 0xCB, 0xCD, 0x00, 0xD8, 0xDA,
219 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
220 0x88, 0x89, 0x8A, 0x8B, 0x8C, 0x8D, 0x8E, 0x8F,
221 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
222 0x98, 0x99, 0x9A, 0x9B, 0x9C, 0x9D, 0x9E, 0x9F,
223 0xE0, 0xE1, 0xE2, 0xE3, 0xE4, 0xE5, 0xE6, 0xE7,
224 0xE8, 0xE9, 0xEA, 0xEB, 0xEC, 0xED, 0xEE, 0xEF,
225 0xF0, 0xF1, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7,
226 0xF8, 0xF9, 0xFA, 0xFB, 0xFC, 0xFD, 0xFE, 0xDF,
227 0xDC, 0xDE, 0xAC, 0xAF, 0xB9, 0xCF, 0xB4, 0xBB,
228 0xC0, 0xBD, 0xBF, 0xCC, 0xCE, 0xA4, 0xD9, 0xDB
229 };
230
231 static unsigned
232 iso8859_5_to_apple_cyrillic(unsigned isocode, void *client_data)
233 {
234 if (isocode <= 0x80)
235 return isocode;
236 else if (isocode >= 0xA0)
237 return iso8859_5_apple_cyrillic[isocode - 0x80];
238 else
239 return 0;
240 }
241
242 static FontMapRec iso8859_5[] = {
243 {FONT_ENCODING_UNICODE, 0, 0, FontEncSimpleRecode, NULL,
244 &iso8859_5_to_unicode_map, NULL, NULL},
245 {FONT_ENCODING_TRUETYPE, 1, 7, iso8859_5_to_apple_cyrillic, NULL, NULL,
246 NULL, NULL},
247 {0, 0, 0, NULL, NULL, NULL, NULL, NULL}
248 };
249
250 /* ISO 8859-6 seems useless for serving fonts (not enough presentation
251 * forms). What do Arabic-speakers use? */
252
253 static unsigned
254 iso8859_6_to_unicode(unsigned isocode, void *client_data)
255 {
256 if (isocode <= 0xA0 || isocode == 0xA4 || isocode == 0xAD)
257 return isocode;
258 else if (isocode == 0xAC || isocode == 0xBB ||
259 isocode == 0xBF ||
260 (isocode >= 0xC1 && isocode <= 0xDA) ||
261 (isocode >= 0xE0 && isocode <= 0xEF) ||
262 (isocode >= 0xF0 && isocode <= 0xF2))
263 return isocode - 0xA0 + 0x0600;
264 else
265 return 0;
266 }
267
268 static FontMapRec iso8859_6[] = {
269 {FONT_ENCODING_UNICODE, 0, 0, iso8859_6_to_unicode, NULL, NULL, NULL, NULL},
270 {0, 0, 0, NULL, NULL, NULL, NULL, NULL}
271 };
272
273 static unsigned
274 iso8859_7_to_unicode(unsigned isocode, void *client_data)
275 {
276 if (isocode <= 0xA0 ||
277 (isocode >= 0xA3 && isocode <= 0xAD) ||
278 (isocode >= 0xB0 && isocode <= 0xB3) ||
279 isocode == 0xB7 || isocode == 0xBB || isocode == 0xBD)
280 return isocode;
281 else if (isocode == 0xA1)
282 return 0x2018;
283 else if (isocode == 0xA2)
284 return 0x2019;
285 else if (isocode == 0xAF)
286 return 0x2015;
287 else if (isocode == 0xD2) /* unassigned */
288 return 0;
289 else if (isocode >= 0xB4 && isocode <= 0xFE)
290 return isocode - 0xA0 + 0x0370;
291 else
292 return 0;
293 }
294
295 static FontMapRec iso8859_7[] = {
296 {FONT_ENCODING_UNICODE, 0, 0, iso8859_7_to_unicode, NULL, NULL, NULL, NULL},
297 {0, 0, 0, NULL, NULL, NULL, NULL, NULL}
298 };
299
300 static unsigned
301 iso8859_8_to_unicode(unsigned isocode, void *client_data)
302 {
303 if (isocode == 0xA1)
304 return 0;
305 else if (isocode < 0xBF)
306 return isocode;
307 else if (isocode == 0xDF)
308 return 0x2017;
309 else if (isocode >= 0xE0 && isocode <= 0xFA)
310 return isocode + 0x04F0;
311 else
312 return 0;
313 }
314
315 static FontMapRec iso8859_8[] = {
316 {FONT_ENCODING_UNICODE, 0, 0, iso8859_8_to_unicode, NULL, NULL, NULL, NULL},
317 {0, 0, 0, NULL, NULL, NULL, NULL, NULL}
318 };
319
320 static unsigned
321 iso8859_9_to_unicode(unsigned isocode, void *client_data)
322 {
323 switch (isocode) {
324 case 0xD0:
325 return 0x011E;
326 case 0xDD:
327 return 0x0130;
328 case 0xDE:
329 return 0x015E;
330 case 0xF0:
331 return 0x011F;
332 case 0xFD:
333 return 0x0131;
334 case 0xFE:
335 return 0x015F;
336 default:
337 return isocode;
338 }
339 }
340
341 static FontMapRec iso8859_9[] = {
342 {FONT_ENCODING_UNICODE, 0, 0, iso8859_9_to_unicode, NULL, NULL, NULL, NULL},
343 {0, 0, 0, NULL, NULL, NULL, NULL, NULL}
344 };
345
346 static const unsigned short iso8859_10_tophalf[] = {
347 0x00A0, 0x0104, 0x0112, 0x0122, 0x012A, 0x0128, 0x0136, 0x00A7,
348 0x013B, 0x0110, 0x0160, 0x0166, 0x017D, 0x00AD, 0x016A, 0x014A,
349 0x00B0, 0x0105, 0x0113, 0x0123, 0x012B, 0x0129, 0x0137, 0x00B7,
350 0x013C, 0x0111, 0x0161, 0x0167, 0x017E, 0x2014, 0x016B, 0x014B,
351 0x0100, 0x00C1, 0x00C2, 0x00C3, 0x00C4, 0x00C5, 0x00C6, 0x012E,
352 0x010C, 0x00C9, 0x0118, 0x00CB, 0x0116, 0x00CD, 0x00CE, 0x00CF,
353 0x00D0, 0x0145, 0x014C, 0x00D3, 0x00D4, 0x00D5, 0x00D6, 0x0168,
354 0x00D8, 0x0172, 0x00DA, 0x00DB, 0x00DC, 0x00DD, 0x00DE, 0x00DF,
355 0x0101, 0x00E1, 0x00E2, 0x00E3, 0x00E4, 0x00E5, 0x00E6, 0x012F,
356 0x010D, 0x00E9, 0x0119, 0x00EB, 0x0117, 0x00ED, 0x00EE, 0x00EF,
357 0x00F0, 0x0146, 0x014D, 0x00F3, 0x00F4, 0x00F5, 0x00F6, 0x0169,
358 0x00F8, 0x0173, 0x00FA, 0x00FB, 0x00FC, 0x00FD, 0x00FE, 0x0138
359 };
360
361 static FontEncSimpleMapRec iso8859_10_to_unicode_map =
362 { 0x60, 0, 0xA0, iso8859_10_tophalf };
363
364 static FontMapRec iso8859_10[] = {
365 {FONT_ENCODING_UNICODE, 0, 0, FontEncSimpleRecode, NULL,
366 &iso8859_10_to_unicode_map, NULL, NULL},
367 {0, 0, 0, NULL, NULL, NULL, NULL, NULL}
368 };
369
370 static unsigned
371 iso8859_15_to_unicode(unsigned isocode, void *client_data)
372 {
373 switch (isocode) {
374 case 0xA4:
375 return 0x20AC;
376 case 0xA6:
377 return 0x0160;
378 case 0xA8:
379 return 0x0161;
380 case 0xB4:
381 return 0x017D;
382 case 0xB8:
383 return 0x017E;
384 case 0xBC:
385 return 0x0152;
386 case 0xBD:
387 return 0x0153;
388 case 0xBE:
389 return 0x0178;
390 default:
391 return isocode;
392 }
393 }
394
395 static FontMapRec iso8859_15[] = {
396 {FONT_ENCODING_UNICODE, 0, 0, iso8859_15_to_unicode, NULL, NULL, NULL,
397 NULL},
398 {0, 0, 0, NULL, NULL, NULL, NULL, NULL}
399 };
400
401 static const unsigned short koi8_r_tophalf[] = {
402 0x2500, 0x2502, 0x250C, 0x2510, 0x2514, 0x2518, 0x251C, 0x2524,
403 0x252C, 0x2534, 0x253C, 0x2580, 0x2584, 0x2588, 0x258C, 0x2590,
404 0x2591, 0x2592, 0x2593, 0x2320, 0x25A0, 0x2022, 0x221A, 0x2248,
405 0x2264, 0x2265, 0x00A0, 0x2321, 0x00B0, 0x00B2, 0x00B7, 0x00F7,
406 0x2550, 0x2551, 0x2552, 0x0451, 0x2553, 0x2554, 0x2555, 0x2556,
407 0x2557, 0x2558, 0x2559, 0x255A, 0x255B, 0x255C, 0x255D, 0x255E,
408 0x255F, 0x2560, 0x2561, 0x0401, 0x2562, 0x2563, 0x2564, 0x2565,
409 0x2566, 0x2567, 0x2568, 0x2569, 0x256A, 0x256B, 0x256C, 0x00A9,
410 0x044E, 0x0430, 0x0431, 0x0446, 0x0434, 0x0435, 0x0444, 0x0433,
411 0x0445, 0x0438, 0x0439, 0x043A, 0x043B, 0x043C, 0x043D, 0x043E,
412 0x043F, 0x044F, 0x0440, 0x0441, 0x0442, 0x0443, 0x0436, 0x0432,
413 0x044C, 0x044B, 0x0437, 0x0448, 0x044D, 0x0449, 0x0447, 0x044A,
414 0x042E, 0x0410, 0x0411, 0x0426, 0x0414, 0x0415, 0x0424, 0x0413,
415 0x0425, 0x0418, 0x0419, 0x041A, 0x041B, 0x041C, 0x041D, 0x041E,
416 0x041F, 0x042F, 0x0420, 0x0421, 0x0422, 0x0423, 0x0416, 0x0412,
417 0x042C, 0x042B, 0x0417, 0x0428, 0x042D, 0x0429, 0x0427, 0x042A
418 };
419
420 static FontEncSimpleMapRec koi8_r_to_unicode_map =
421 { 0x80, 0, 0x80, koi8_r_tophalf };
422
423 static FontMapRec koi8_r[] = {
424 {FONT_ENCODING_UNICODE, 0, 0, FontEncSimpleRecode, NULL,
425 &koi8_r_to_unicode_map, NULL, NULL},
426 {0, 0, 0, NULL, NULL, NULL, NULL, NULL}
427 };
428
429 static unsigned
430 koi8_ru_to_unicode(unsigned koicode, void *client_data)
431 {
432 switch (koicode) {
433 case 0x93:
434 return 0x201C;
435 case 0x96:
436 return 0x201D;
437 case 0x97:
438 return 0x2014;
439 case 0x98:
440 return 0x2116;
441 case 0x99:
442 return 0x2122;
443 case 0x9B:
444 return 0x00BB;
445 case 0x9C:
446 return 0x00AE;
447 case 0x9D:
448 return 0x00AB;
449 case 0x9F:
450 return 0x00A4;
451 case 0xA4:
452 return 0x0454;
453 case 0xA6:
454 return 0x0456;
455 case 0xA7:
456 return 0x0457;
457 case 0xAD:
458 return 0x0491;
459 case 0xAE:
460 return 0x045E;
461 case 0xB4:
462 return 0x0404;
463 case 0xB6:
464 return 0x0406;
465 case 0xB7:
466 return 0x0407;
467 case 0xBD:
468 return 0x0490;
469 case 0xBE:
470 return 0x040E;
471 default:
472 return FontEncSimpleRecode(koicode, &koi8_r_to_unicode_map);
473 }
474 }
475
476 static FontMapRec koi8_ru[] = {
477 {FONT_ENCODING_UNICODE, 0, 0, koi8_ru_to_unicode, NULL, NULL, NULL, NULL},
478 {0, 0, 0, NULL, NULL, NULL, NULL, NULL}
479 };
480
481 /* koi8-e, ISO-IR-111 or ECMA-Cyrillic */
482
483 static const unsigned short koi8_e_A0_BF[] = {
484 0x00A0, 0x0452, 0x0453, 0x0451, 0x0454, 0x0455, 0x0456, 0x0457,
485 0x0458, 0x0459, 0x045A, 0x045B, 0x045C, 0x00AD, 0x045E, 0x045F,
486 0x2116, 0x0402, 0x0403, 0x0401, 0x0404, 0x0405, 0x0406, 0x0407,
487 0x0408, 0x0409, 0x040A, 0x040B, 0x040C, 0x00A4, 0x040E, 0x040F
488 };
489
490 static unsigned
491 koi8_e_to_unicode(unsigned koicode, void *client_data)
492 {
493 if (koicode < 0xA0)
494 return koicode;
495 else if (koicode < 0xC0)
496 return koi8_e_A0_BF[koicode - 0xA0];
497 else
498 return FontEncSimpleRecode(koicode, &koi8_r_to_unicode_map);
499 }
500
501 static FontMapRec koi8_e[] = {
502 {FONT_ENCODING_UNICODE, 0, 0, koi8_e_to_unicode, NULL, NULL, NULL, NULL},
503 {0, 0, 0, NULL, NULL, NULL, NULL, NULL}
504 };
505
506 /* Koi8 unified */
507
508 static const unsigned short koi8_uni_80_BF[] = {
509 0x2500, 0x2502, 0x250C, 0x2510, 0x2514, 0x2518, 0x251C, 0x2524,
510 0x252C, 0x2534, 0x253C, 0x2580, 0x2584, 0x2588, 0x258C, 0x2590,
511 0x2591, 0x2018, 0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014,
512 0x00A9, 0x2122, 0x00A0, 0x00BB, 0x00AE, 0x00AB, 0x00B7, 0x00A4,
513 0x00A0, 0x0452, 0x0453, 0x0451, 0x0454, 0x0455, 0x0456, 0x0457,
514 0x0458, 0x0459, 0x045A, 0x045B, 0x045C, 0x0491, 0x045E, 0x045F,
515 0x2116, 0x0402, 0x0403, 0x0401, 0x0404, 0x0405, 0x0406, 0x0407,
516 0x0408, 0x0409, 0x040A, 0x040B, 0x040C, 0x0490, 0x040E, 0x040F
517 };
518
519 static unsigned
520 koi8_uni_to_unicode(unsigned koicode, void *client_data)
521 {
522 if (koicode < 0x80)
523 return koicode;
524 else if (koicode < 0xC0)
525 return koi8_uni_80_BF[koicode - 0x80];
526 else
527 return FontEncSimpleRecode(koicode, &koi8_r_to_unicode_map);
528 }
529
530 static FontMapRec koi8_uni[] = {
531 {FONT_ENCODING_UNICODE, 0, 0, koi8_uni_to_unicode, NULL, NULL, NULL, NULL},
532 {0, 0, 0, NULL, NULL, NULL, NULL, NULL}
533 };
534
535 /* Ukrainian variant of Koi8-R; see RFC 2319 */
536
537 static unsigned
538 koi8_u_to_unicode(unsigned koicode, void *client_data)
539 {
540 switch (koicode) {
541 case 0xA4:
542 return 0x0454;
543 case 0xA6:
544 return 0x0456;
545 case 0xA7:
546 return 0x0457;
547 case 0xAD:
548 return 0x0491;
549 case 0xB4:
550 return 0x0404;
551 case 0xB6:
552 return 0x0406;
553 case 0xB7:
554 return 0x0407;
555 case 0xBD:
556 return 0x0490;
557 default:
558 return FontEncSimpleRecode(koicode, &koi8_r_to_unicode_map);
559 }
560 }
561
562 static FontMapRec koi8_u[] = {
563 {FONT_ENCODING_UNICODE, 0, 0, koi8_u_to_unicode, NULL, NULL, NULL},
564 {0, 0, 0, NULL, NULL, NULL, NULL, NULL}
565 };
566
567 /* Microsoft Symbol, which is only meaningful for TrueType fonts, is
568 treated specially in ftenc.c, where we add usFirstCharIndex-0x20 to
569 the glyph index before applying the cmap. Lovely design. */
570
571 static FontMapRec microsoft_symbol[] = {
572 {FONT_ENCODING_TRUETYPE, 3, 0, NULL, NULL, NULL, NULL, NULL},
573 /* You never know */
574 {FONT_ENCODING_TRUETYPE, 3, 1, NULL, NULL, NULL, NULL, NULL},
575 {0, 0, 0, NULL, NULL, NULL, NULL, NULL}
576 };
577
578 static FontMapRec apple_roman[] = {
579 {FONT_ENCODING_TRUETYPE, 1, 0, NULL, NULL, NULL, NULL, NULL},
580 {0, 0, 0, NULL, NULL, NULL, NULL, NULL}
581 };
582
583 /* The data for recodings */
584
585 /* For compatibility with X11R6.4. Losers. */
586 static char *iso8859_15_aliases[2] = { "fcd8859-15", NULL };
587
588 static FontEncRec initial_encodings[] = {
589 {"iso10646-1", NULL, 256 * 256, 0, iso10646, NULL, 0, 0}, /* Unicode */
590 {"iso8859-1", NULL, 256, 0, iso8859_1, NULL, 0, 0}, /* Latin 1 (West European) */
591 {"iso8859-2", NULL, 256, 0, iso8859_2, NULL, 0, 0}, /* Latin 2 (East European) */
592 {"iso8859-3", NULL, 256, 0, iso8859_3, NULL, 0, 0}, /* Latin 3 (South European) */
593 {"iso8859-4", NULL, 256, 0, iso8859_4, NULL, 0, 0}, /* Latin 4 (North European) */
594 {"iso8859-5", NULL, 256, 0, iso8859_5, NULL, 0, 0}, /* Cyrillic */
595 {"iso8859-6", NULL, 256, 0, iso8859_6, NULL, 0, 0}, /* Arabic */
596 {"iso8859-7", NULL, 256, 0, iso8859_7, NULL, 0, 0}, /* Greek */
597 {"iso8859-8", NULL, 256, 0, iso8859_8, NULL, 0, 0}, /* Hebrew */
598 {"iso8859-9", NULL, 256, 0, iso8859_9, NULL, 0, 0}, /* Latin 5 (Turkish) */
599 {"iso8859-10", NULL, 256, 0, iso8859_10, NULL, 0, 0}, /* Latin 6 (Nordic) */
600 {"iso8859-15", iso8859_15_aliases, 256, 0, iso8859_15, NULL, 0, 0}, /* Latin 9 */
601 {"koi8-r", NULL, 256, 0, koi8_r, NULL, 0, 0}, /* Russian */
602 {"koi8-ru", NULL, 256, 0, koi8_ru, NULL, 0, 0}, /* Ukrainian */
603 {"koi8-uni", NULL, 256, 0, koi8_uni, NULL, 0, 0}, /* Russian/Ukrainian/Bielorussian */
604 {"koi8-e", NULL, 256, 0, koi8_e, NULL, 0, 0}, /* ``European'' */
605 {"koi8-u", NULL, 256, 0, koi8_u, NULL, 0, 0}, /* Ukrainian too */
606 {"microsoft-symbol", NULL, 256, 0, microsoft_symbol, NULL, 0, 0},
607 {"apple-roman", NULL, 256, 0, apple_roman, NULL, 0, 0},
608 {NULL, NULL, 0, 0, NULL, NULL, 0, 0}
609 };
610
611 static FontEncPtr font_encodings = NULL;
612
613 static void
614 define_initial_encoding_info(void)
615 {
616 FontEncPtr encoding;
617 FontMapPtr mapping;
618
619 font_encodings = initial_encodings;
620 for (encoding = font_encodings; ; encoding++) {
621 encoding->next = encoding + 1;
622 for (mapping = encoding->mappings; ; mapping++) {
623 mapping->next = mapping + 1;
624 mapping->encoding = encoding;
625 if (mapping->next->type == 0) {
626 mapping->next = NULL;
627 break;
628 }
629 }
630 if (!encoding->next->name) {
631 encoding->next = NULL;
632 break;
633 }
634 }
635 }
636
637 char *
638 FontEncFromXLFD(const char *name, int length)
639 {
640 const char *p;
641 char *q;
642 static char charset[MAXFONTNAMELEN];
643 int len;
644
645 if (length > MAXFONTNAMELEN - 1)
646 return NULL;
647
648 if (name == NULL)
649 p = NULL;
650 else {
651 p = name + length - 1;
652 while (p > name && *p != '-')
653 p--;
654 p--;
655 while (p >= name && *p != '-')
656 p--;
657 if (p <= name)
658 p = NULL;
659 }
660
661 /* now p either is null or points at the '-' before the charset registry */
662
663 if (p == NULL)
664 return NULL;
665
666 len = length - (p - name) - 1;
667 memcpy(charset, p + 1, len);
668 charset[len] = 0;
669
670 /* check for a subset specification */
671 if ((q = strchr(charset, (int) '[')))
672 *q = 0;
673
674 return charset;
675 }
676
677 unsigned
678 FontEncRecode(unsigned code, FontMapPtr mapping)
679 {
680 FontEncPtr encoding = mapping->encoding;
681
682 if (encoding && mapping->recode) {
683 if (encoding->row_size == 0) {
684 /* linear encoding */
685 if (code < encoding->first || code >= encoding->size)
686 return 0;
687 }
688 else {
689 /* matrix encoding */
690 int row = code / 0x100, col = code & 0xFF;
691
692 if (row < encoding->first || row >= encoding->size ||
693 col < encoding->first_col || col >= encoding->row_size)
694 return 0;
695 }
696 return (*mapping->recode) (code, mapping->client_data);
697 }
698 else
699 return code;
700 }
701
702 char *
703 FontEncName(unsigned code, FontMapPtr mapping)
704 {
705 FontEncPtr encoding = mapping->encoding;
706
707 if (encoding && mapping->name) {
708 if ((encoding->row_size == 0 && code >= encoding->size) ||
709 (encoding->row_size != 0 &&
710 (code / 0x100 >= encoding->size ||
711 (code & 0xFF) >= encoding->row_size)))
712 return NULL;
713 return (*mapping->name) (code, mapping->client_data);
714 }
715 else
716 return NULL;
717 }
718
719 FontEncPtr
720 FontEncFind(const char *encoding_name, const char *filename)
721 {
722 FontEncPtr encoding;
723 char **alias;
724
725 if (font_encodings == NULL)
726 define_initial_encoding_info();
727
728 for (encoding = font_encodings; encoding; encoding = encoding->next) {
729 if (!strcasecmp(encoding->name, encoding_name))
730 return encoding;
731 if (encoding->aliases)
732 for (alias = encoding->aliases; *alias; alias++)
733 if (!strcasecmp(*alias, encoding_name))
734 return encoding;
735 }
736
737 /* Unknown charset, try to load a definition file */
738 return FontEncLoad(encoding_name, filename);
739 }
740
741 FontMapPtr
742 FontMapFind(FontEncPtr encoding, int type, int pid, int eid)
743 {
744 FontMapPtr mapping;
745
746 if (encoding == NULL)
747 return NULL;
748
749 for (mapping = encoding->mappings; mapping; mapping = mapping->next) {
750 if (mapping->type != type)
751 continue;
752 if (pid > 0 && mapping->pid != pid)
753 continue;
754 if (eid > 0 && mapping->eid != eid)
755 continue;
756 return mapping;
757 }
758 return NULL;
759 }
760
761 FontMapPtr
762 FontEncMapFind(const char *encoding_name, int type, int pid, int eid,
763 const char *filename)
764 {
765 FontEncPtr encoding;
766 FontMapPtr mapping;
767
768 encoding = FontEncFind(encoding_name, filename);
769 if (encoding == NULL)
770 return NULL;
771 mapping = FontMapFind(encoding, type, pid, eid);
772 return mapping;
773 }
774
775 static FontEncPtr
776 FontEncLoad(const char *encoding_name, const char *filename)
777 {
778 FontEncPtr encoding;
779
780 encoding = FontEncReallyLoad(encoding_name, filename);
781 if (encoding == NULL) {
782 return NULL;
783 }
784 else {
785 char **alias;
786 int found = 0;
787
788 /* Check whether the name is already known for this encoding */
789 if (strcasecmp(encoding->name, encoding_name) == 0) {
790 found = 1;
791 }
792 else {
793 if (encoding->aliases) {
794 for (alias = encoding->aliases; *alias; alias++)
795 if (!strcasecmp(*alias, encoding_name)) {
796 found = 1;
797 break;
798 }
799 }
800 }
801
802 if (!found) {
803 /* Add a new alias. This works because we know that this
804 particular encoding has been allocated dynamically */
805 char **new_aliases;
806 char *new_name;
807 int numaliases = 0;
808
809 new_name = strdup(encoding_name);
810 if (new_name == NULL)
811 return NULL;
812 if (encoding->aliases) {
813 for (alias = encoding->aliases; *alias; alias++)
814 numaliases++;
815 }
816 new_aliases = Xmallocarray(numaliases + 2, sizeof(char *));
817 if (new_aliases == NULL) {
818 free(new_name);
819 return NULL;
820 }
821 if (encoding->aliases) {
822 memcpy(new_aliases, encoding->aliases,
823 numaliases * sizeof(char *));
824 free(encoding->aliases);
825 }
826 new_aliases[numaliases] = new_name;
827 new_aliases[numaliases + 1] = NULL;
828 encoding->aliases = new_aliases;
829 }
830
831 /* register the new encoding */
832 encoding->next = font_encodings;
833 font_encodings = encoding;
834
835 return encoding;
836 }
837 }
838
839 unsigned
840 FontEncSimpleRecode(unsigned code, void *client_data)
841 {
842 FontEncSimpleMapPtr map;
843 unsigned index;
844
845 map = client_data;
846
847 if (code > 0xFFFF || (map->row_size && (code & 0xFF) >= map->row_size))
848 return 0;
849
850 if (map->row_size)
851 index = (code & 0xFF) + (code >> 8) * map->row_size;
852 else
853 index = code;
854
855 if (map->map && index >= map->first && index < map->first + map->len)
856 return map->map[index - map->first];
857 else
858 return code;
859 }
860
861 char *
862 FontEncSimpleName(unsigned code, void *client_data)
863 {
864 FontEncSimpleNamePtr map;
865
866 map = client_data;
867 if (map && code >= map->first && code < map->first + map->len)
868 return map->map[code - map->first];
869 else
870 return NULL;
871 }
872
873 unsigned
874 FontEncUndefinedRecode(unsigned code, void *client_data)
875 {
876 return code;
877 }
878
879 char *
880 FontEncUndefinedName(unsigned code, void *client_data)
881 {
882 return NULL;
883 }
884
885 #define FONTENC_SEGMENT_SIZE 256
886 #define FONTENC_SEGMENTS 256
887 #define FONTENC_INVERSE_CODES (FONTENC_SEGMENT_SIZE * FONTENC_SEGMENTS)
888
889 static unsigned int
890 reverse_reverse(unsigned i, void *data)
891 {
892 int s, j;
893 unsigned **map = (unsigned **) data;
894
895 if (i >= FONTENC_INVERSE_CODES)
896 return 0;
897
898 if (map == NULL)
899 return 0;
900
901 s = i / FONTENC_SEGMENT_SIZE;
902 j = i % FONTENC_SEGMENT_SIZE;
903
904 if (map[s] == NULL)
905 return 0;
906 else
907 return map[s][j];
908 }
909
910 static int
911 tree_set(unsigned int **map, unsigned int i, unsigned int j)
912 {
913 int s, c;
914
915 if (i >= FONTENC_INVERSE_CODES)
916 return FALSE;
917
918 s = i / FONTENC_SEGMENT_SIZE;
919 c = i % FONTENC_SEGMENT_SIZE;
920
921 if (map[s] == NULL) {
922 map[s] = calloc(FONTENC_SEGMENT_SIZE, sizeof(int));
923 if (map[s] == NULL)
924 return FALSE;
925 }
926
927 map[s][c] = j;
928 return TRUE;
929 }
930
931 FontMapReversePtr
932 FontMapReverse(FontMapPtr mapping)
933 {
934 FontEncPtr encoding = mapping->encoding;
935 FontMapReversePtr reverse = NULL;
936 unsigned int **map = NULL;
937 int i, j, k;
938
939 if (encoding == NULL)
940 goto bail;
941
942 map = calloc(FONTENC_SEGMENTS, sizeof(int *));
943 if (map == NULL)
944 goto bail;
945
946 if (encoding->row_size == 0) {
947 for (i = encoding->first; i < encoding->size; i++) {
948 k = FontEncRecode(i, mapping);
949 if (k != 0)
950 if (!tree_set(map, k, i))
951 goto bail;
952 }
953 }
954 else {
955 for (i = encoding->first; i < encoding->size; i++) {
956 for (j = encoding->first_col; j < encoding->row_size; j++) {
957 k = FontEncRecode(i * 256 + j, mapping);
958 if (k != 0)
959 if (!tree_set(map, k, i * 256 + j))
960 goto bail;
961 }
962 }
963 }
964
965 reverse = malloc(sizeof(FontMapReverseRec));
966 if (!reverse)
967 goto bail;
968
969 reverse->reverse = reverse_reverse;
970 reverse->data = map;
971 return reverse;
972
973 bail:
974 free(map);
975 free(reverse);
976 return NULL;
977 }
978
979 void
980 FontMapReverseFree(FontMapReversePtr delendum)
981 {
982 unsigned int **map = (unsigned int **) delendum;
983 int i;
984
985 if (map == NULL)
986 return;
987
988 for (i = 0; i < FONTENC_SEGMENTS; i++)
989 free(map[i]);
990
991 free(map);
992 return;
993 }