Debian Codegraph @

texk/xdvik/ft.c

raw
1 /*========================================================================*\
2
3 Copyright (c) 2013 Paul Vojta
4
5 Permission is hereby granted, free of charge, to any person obtaining a copy
6 of this software and associated documentation files (the "Software"), to
7 deal in the Software without restriction, including without limitation the
8 rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
9 sell copies of the Software, and to permit persons to whom the Software is
10 furnished to do so, subject to the following conditions:
11
12 The above copyright notice and this permission notice shall be included in
13 all copies or substantial portions of the Software.
14
15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 PAUL VOJTA BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19 IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
22 \*========================================================================*/
23
24 /*
25 * FreeType (PostScript Type 1 and TrueType) font reading routines.
26 * Public routines are load_ft_font and read_ft_char.
27 * The prototype for load_ft_font is in dvi-init.h.
28 */
29
30 #include "xdvi-config.h"
31 #include "xdvi.h"
32 #include "dvi-init.h"
33 #include "dvi-draw.h"
34 #include "util.h"
35 #include "font-open.h"
36
37 #include <ctype.h>
38 #include <math.h>
39
40 #include <ft2build.h>
41 #include FT_FREETYPE_H
42 #include FT_SIZES_H
43
44 static FT_Library library = NULL;
45
46 static struct avl_enc *enc_head = NULL;
47
48 /* From xc/lib/X11/PutImage.c */
49
50 #if !WORDS_BIGENDIAN
51 static const unsigned char reverse_byte[0x100] = {
52 0x00, 0x80, 0x40, 0xc0, 0x20, 0xa0, 0x60, 0xe0,
53 0x10, 0x90, 0x50, 0xd0, 0x30, 0xb0, 0x70, 0xf0,
54 0x08, 0x88, 0x48, 0xc8, 0x28, 0xa8, 0x68, 0xe8,
55 0x18, 0x98, 0x58, 0xd8, 0x38, 0xb8, 0x78, 0xf8,
56 0x04, 0x84, 0x44, 0xc4, 0x24, 0xa4, 0x64, 0xe4,
57 0x14, 0x94, 0x54, 0xd4, 0x34, 0xb4, 0x74, 0xf4,
58 0x0c, 0x8c, 0x4c, 0xcc, 0x2c, 0xac, 0x6c, 0xec,
59 0x1c, 0x9c, 0x5c, 0xdc, 0x3c, 0xbc, 0x7c, 0xfc,
60 0x02, 0x82, 0x42, 0xc2, 0x22, 0xa2, 0x62, 0xe2,
61 0x12, 0x92, 0x52, 0xd2, 0x32, 0xb2, 0x72, 0xf2,
62 0x0a, 0x8a, 0x4a, 0xca, 0x2a, 0xaa, 0x6a, 0xea,
63 0x1a, 0x9a, 0x5a, 0xda, 0x3a, 0xba, 0x7a, 0xfa,
64 0x06, 0x86, 0x46, 0xc6, 0x26, 0xa6, 0x66, 0xe6,
65 0x16, 0x96, 0x56, 0xd6, 0x36, 0xb6, 0x76, 0xf6,
66 0x0e, 0x8e, 0x4e, 0xce, 0x2e, 0xae, 0x6e, 0xee,
67 0x1e, 0x9e, 0x5e, 0xde, 0x3e, 0xbe, 0x7e, 0xfe,
68 0x01, 0x81, 0x41, 0xc1, 0x21, 0xa1, 0x61, 0xe1,
69 0x11, 0x91, 0x51, 0xd1, 0x31, 0xb1, 0x71, 0xf1,
70 0x09, 0x89, 0x49, 0xc9, 0x29, 0xa9, 0x69, 0xe9,
71 0x19, 0x99, 0x59, 0xd9, 0x39, 0xb9, 0x79, 0xf9,
72 0x05, 0x85, 0x45, 0xc5, 0x25, 0xa5, 0x65, 0xe5,
73 0x15, 0x95, 0x55, 0xd5, 0x35, 0xb5, 0x75, 0xf5,
74 0x0d, 0x8d, 0x4d, 0xcd, 0x2d, 0xad, 0x6d, 0xed,
75 0x1d, 0x9d, 0x5d, 0xdd, 0x3d, 0xbd, 0x7d, 0xfd,
76 0x03, 0x83, 0x43, 0xc3, 0x23, 0xa3, 0x63, 0xe3,
77 0x13, 0x93, 0x53, 0xd3, 0x33, 0xb3, 0x73, 0xf3,
78 0x0b, 0x8b, 0x4b, 0xcb, 0x2b, 0xab, 0x6b, 0xeb,
79 0x1b, 0x9b, 0x5b, 0xdb, 0x3b, 0xbb, 0x7b, 0xfb,
80 0x07, 0x87, 0x47, 0xc7, 0x27, 0xa7, 0x67, 0xe7,
81 0x17, 0x97, 0x57, 0xd7, 0x37, 0xb7, 0x77, 0xf7,
82 0x0f, 0x8f, 0x4f, 0xcf, 0x2f, 0xaf, 0x6f, 0xef,
83 0x1f, 0x9f, 0x5f, 0xdf, 0x3f, 0xbf, 0x7f, 0xff
84 };
85 #endif /* !WORDS_BIGENDIAN */
86
87
88 /*
89 * FreeType I/O stream functions
90 */
91
92 static unsigned long
93 xdvi_stream_read(FT_Stream stream,
94 unsigned long offset,
95 unsigned char *buffer,
96 unsigned long count)
97 {
98 struct font *fontp;
99
100 fontp = ((struct ftfont *) stream->descriptor.pointer)->first_size;
101 open_font_file(fontp);
102 fseek(fontp->file, offset, SEEK_SET);
103 return (unsigned long) fread(buffer, 1, count, fontp->file);
104 }
105
106 static void
107 xdvi_stream_close(FT_Stream stream)
108 {
109 /* do nothing */
110 }
111
112
113 /*
114 * Public routines
115 */
116
117 static void
118 read_ft_char(struct font *fontp, wide_ubyte ch)
119 {
120 struct glyph *g;
121 FT_Face face;
122 FT_GlyphSlot slot;
123 int bmTypeT_wide;
124 int err;
125
126 if (globals.debug & DBG_PK)
127 printf("Loading FreeType char %d", ch);
128
129 /*
130 * Load it unscaled first, so that we can get a more accurate
131 * (non-hinted) value of the horizontal advance.
132 */
133
134 g = &fontp->glyph[ch];
135 face = fontp->ft->face;
136 FT_Activate_Size(fontp->size);
137 err = FT_Load_Glyph(face, g->addr, FT_LOAD_NO_SCALE);
138 if (err != 0)
139 XDVI_FATAL((stderr, "FT_Load_Glyph: error = %d", err));
140
141 g->dvi_adv = face->glyph->metrics.horiAdvance * fontp->ft->expn
142 * fontp->pixsize * (1 << 16) / face->units_per_EM + 0.5;
143
144 /* Now do the real loading. */
145
146 err = FT_Load_Glyph(face, g->addr, FT_LOAD_RENDER | FT_LOAD_MONOCHROME);
147 if (err != 0)
148 XDVI_FATAL((stderr, "FT_Load_Glyph: error = %d", err));
149
150 slot = face->glyph;
151 g->bitmap.w = slot->bitmap.width;
152 g->bitmap.h = slot->bitmap.rows;
153
154 if (globals.debug & DBG_PK)
155 printf(", size=%dx%d, dvi_adv=%ld\n", g->bitmap.w, g->bitmap.h,
156 g->dvi_adv);
157
158 alloc_bitmap(&g->bitmap);
159 bmTypeT_wide = ROUNDUP((int) g->bitmap.w, BMBITS);
160
161 if (slot->bitmap.width > 0) {
162 int i;
163 unsigned char *p;
164 bmUnitT *q;
165
166 p = slot->bitmap.buffer;
167 q = (bmUnitT *) g->bitmap.bits;
168 for (i = g->bitmap.h; i > 0; --i) {
169 #if WORDS_BIGENDIAN
170 memcpy(q, p, (slot->bitmap.width + 7) / 8);
171 #else
172 unsigned char *p1 = p;
173 bmUnitT *q1 = q;
174 bmUnitT data = 0;
175 int shift = 0;
176 int j;
177
178 for (j = (slot->bitmap.width + 7) / 8; j > 0; --j) {
179 if (shift >= BMBITS) {
180 *q1++ = data;
181 data = 0;
182 shift = 0;
183 }
184 data |= reverse_byte[*p1++] << shift;
185 shift += 8;
186 }
187 *q1 = data;
188 #endif
189 p += slot->bitmap.pitch;
190 q += bmTypeT_wide;
191 }
192 }
193
194 /* The offset of (-1,-1) is from comparing bitmaps of 'R' and 'a'
195 between cmr10.pfb and cmr10.300pk. */
196 g->x = -1 - slot->bitmap_left;
197 g->y = slot->bitmap_top - 1;
198 }
199
200 /*
201 * Do the ScaleFont, etc. directives.
202 */
203
204 static Boolean
205 set_transform(struct ftfont *ftp,
206 const char *str)
207 {
208 double x = 1.0;
209 double y = 0.0;
210
211 for (;;) {
212 while (isspace((unsigned char)*str)) ++str;
213 if (*str == '\0')
214 break;
215
216 if (isdigit((unsigned char)*str) || *str == '.' || *str == '-') {
217 double arg = strtod(str, (char **) &str);
218
219 while (isspace((unsigned char)*str)) ++str;
220
221 if (memcmp(str, "ObliqueSlant", 12) == 0
222 && (isspace((unsigned char)str[12]) || str[12] == '\0')) {
223 arg = -tan(arg);
224 str += 12;
225 while (isspace((unsigned char)*str)) ++str;
226 }
227
228 if (memcmp(str, "SlantFont", 9) == 0
229 && (isspace((unsigned char)str[9]) || str[9] == '\0')) {
230 y += arg;
231 str += 9;
232 }
233 else if (memcmp(str, "ExtendFont", 10) == 0
234 && (isspace((unsigned char)str[10]) || str[10] == '\0')) {
235 x *= arg;
236 str += 10;
237 }
238 else return False;
239 }
240 else { /* other characters; presume encoding name */
241 while (!isspace((unsigned char)*str) && *str != '\0') ++str;
242 while (isspace((unsigned char)*str)) ++str;
243 if (memcmp(str, "ReEncodeFont", 12) == 0
244 && (isspace((unsigned char)str[12]) || str[12] == '\0'))
245 str += 12;
246 else
247 return False;
248 }
249 }
250
251 if (x != 1.0 || y != 0.0) {
252 FT_Matrix mat;
253
254 mat.xx = (FT_Fixed) (x * 0x10000 + 0.5);
255 mat.xy = (FT_Fixed) (y * 0x10000 + 0.5);
256 mat.yx = 0;
257 mat.yy = 0x10000;
258
259 FT_Set_Transform(ftp->face, &mat, NULL);
260 ftp->expn = x;
261 }
262
263 return True;
264 }
265
266
267 /*
268 * When a document uses many freetype fonts, it can take a while to read
269 * them all (due mostly to disk access issues). So, we delay loading
270 * freetype fonts until they're needed.
271 *
272 * Therefore, this routine is not called until it is time to render a
273 * character from the font (see set_ft_char() in dvi-draw.c).
274 *
275 * It loads and sets up the font.
276 */
277
278 Boolean
279 load_ft_font(struct font *fontp)
280 {
281 struct ftfont *ftp;
282 struct avl_t1 *t1p;
283 struct font *fontp2;
284 FT_Face face;
285 FT_Size size;
286 int err;
287 const char *path;
288 FILE *f;
289
290 ftp = fontp->ft;
291 t1p = ftp->t1;
292 fontp2 = ftp->first_size;
293
294 if (!resource.freetype) {
295 TRACE_FT((stderr,
296 "load_ft_font returning: freetype has been turned off"));
297 return False;
298 }
299
300 if (t1p->bad) {
301 TRACE_FT((stderr, "Font %s was flagged as bad; skipping (size %d)",
302 fontp->fontname, (int) (fontp->fsize + 0.5)));
303 return False;
304 }
305
306 face = ftp->face;
307 if (face != NULL) { /* if this face is already in use */
308 err = FT_New_Size(face, &size);
309 if (err != 0) {
310 /* This error is probably better kept at stderr instead of
311 a popup (too annoying) or the statusline (might disappear
312 too fast) ... */
313 XDVI_ERROR((stderr, "Could not load FreeType font %s at %d: "
314 "FreeType FT_New_Size error = %d.\n"
315 "Will try pixel version instead.\n"
316 "Please see the FreeType documentation for details "
317 "about this.",
318 fontp->fontname, (int) (fontp->fsize + 0.5), err));
319 return False;
320 }
321 }
322 else {
323 FT_Open_Args args;
324
325 f = open_t1_font(t1p, &path);
326 if (f == NULL)
327 return False;
328
329 if (library == NULL) {
330 err = FT_Init_FreeType(&library);
331 if (err != 0) {
332 XDVI_ERROR((stderr,
333 "Could not initialize FreeType library: "
334 "FreeType FT_Init_FreeType error = %d.\n"
335 "Turning off use of FreeType.\n"
336 "Please see the FreeType documentation for details "
337 "about this.",
338 err));
339 resource.freetype = False;
340 free((char *) path);
341 fclose(f);
342 return False;
343 }
344 }
345
346 fontp2->filename = path;
347 fontp2->file = f;
348
349 fseek(f, 0L, SEEK_END);
350 ftp->stream.size = ftell(f);
351 fseek(f, 0L, SEEK_SET); /* this might not be necessary */
352
353 ftp->stream.descriptor.pointer = ftp;
354 ftp->stream.read = xdvi_stream_read;
355 ftp->stream.close = xdvi_stream_close;
356
357 args.flags = FT_OPEN_STREAM;
358 args.stream = &ftp->stream;
359 err = FT_Open_Face(library, &args, 0, &face);
360 if (err != 0) {
361 /* This error is probably better kept at stderr instead of
362 a popup (too annoying) or the statusline (might disappear
363 too fast) ... */
364 XDVI_ERROR((stderr, "Could not load FreeType font %s: "
365 "FreeType FT_Open_Face error = %d.\n"
366 "Will try pixel version instead.\n"
367 "Please see the FreeType documentation for details "
368 "about this.",
369 fontp->fontname, err));
370 free((char *) path);
371 fclose(f);
372 fontp2->file = NULL;
373 fontp2->filename = NULL;
374 return False;
375 }
376 ftp->face = face;
377
378 if (!FT_IS_SCALABLE(face)) {
379 TRACE_FT((stderr, "Font %s is not scalable.", fontp->fontname));
380 FT_Done_Face(face);
381 free((char *) path);
382 fclose(f);
383 fontp2->file = NULL;
384 fontp2->filename = NULL;
385 return False;
386 }
387
388 ftp->expn = 1.0;
389 if (t1p->addinfo != NULL)
390 if (!set_transform(ftp, t1p->addinfo)) {
391 FT_Done_Face(face);
392 free((char *) path);
393 fclose(f);
394 fontp2->file = NULL;
395 fontp2->filename = NULL;
396 return False;
397 }
398
399 if (face->charmap == NULL) {
400 if (face->num_charmaps > 0) {
401 if ((globals.debug & DBG_PK) && face->num_charmaps > 1)
402 printf("Choosing the first of %d charmaps.\n",
403 face->num_charmaps);
404 }
405 FT_Set_Charmap(face, face->charmaps[0]);
406 }
407 else if (face->charmap->encoding == ft_encoding_unicode
408 && FT_Select_Charmap(face, ft_encoding_adobe_custom) != 0
409 && FT_Select_Charmap(face, ft_encoding_adobe_standard) != 0
410 && FT_Select_Charmap(face, ft_encoding_adobe_expert) != 0) {
411 if (globals.debug & DBG_PK)
412 puts("Using unicode charmap for font.");
413 fputs("Using unicode charmap\n", stderr); /* ||| */
414 }
415
416 ftp->enc = NULL;
417 if (t1p->encname != NULL) { /* if there's an encoding */
418 struct avl_enc *encp;
419
420 encp = (struct avl_enc *) avladd(t1p->encname,
421 strlen(t1p->encname),
422 (struct avl **) &enc_head, sizeof *encp);
423 if (encp->key == t1p->encname) /* if new record */
424 read_encoding(encp);
425 if (encp->valid)
426 ftp->enc = encp;
427 }
428
429 size = face->size;
430 }
431
432 /* Get character indices (store them in addr) */
433
434 if (globals.debug & DBG_PK)
435 printf("Character indices for %s:\n", fontp->fontname);
436
437 FT_Activate_Size(size);
438
439 err = FT_Set_Char_Size(face,
440 (int) (fontp->pixsize * (72<<6) / resource.pixels_per_inch + 0.5), 0,
441 resource.pixels_per_inch, resource.pixels_per_inch);
442 if (err != 0)
443 XDVI_FATAL((stderr, "FT_Set_Char_Size: error = %d\n", err));
444
445 /* Look for already-computed character indices */
446 for (fontp2 = ftp->first_size;; fontp2 = fontp2->next_size) {
447 int i;
448
449 if (fontp2 == NULL) { /* if not found */
450 struct avl_enc *encp = ftp->enc;
451
452 for (i = 0; i < 256; ++i) {
453 int ci;
454
455 if (encp == NULL)
456 ci = FT_Get_Char_Index(face, i);
457 else {
458 const char *glyphname = encp->vec[i];
459
460 if (glyphname == NULL)
461 ci = 0;
462 else
463 ci = FT_Get_Name_Index(face,
464 (FT_String *) glyphname);
465 }
466
467 fontp->glyph[i].addr = ci;
468 if (globals.debug & DBG_PK)
469 printf("%3d->%3d%s", i, ci, (i + 1) % 8 ? " " : "\n");
470 }
471 break;
472 }
473 if (fontp2->size != NULL) { /* found the information */
474 for (i = 0; i < 256; ++i)
475 fontp->glyph[i].addr = fontp2->glyph[i].addr;
476 break;
477 }
478 }
479
480 fontp->size = size; /* it needed to be NULL in the above loop */
481 fontp->read_char = read_ft_char;
482
483 return True;
484 }