1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
|
#include "fitz_base.h"
#include "fitz_tree.h"
#include "fitz_draw.h" /* FIXME -- for glyph rendering callbacks */
#include <ft2build.h>
#include FT_FREETYPE_H
static void fz_finalizefreetype(void);
static fz_font *
fz_newfont(void)
{
fz_font *font;
font = fz_malloc(sizeof(fz_font));
font->refs = 1;
strcpy(font->name, "<unknown>");
font->ftface = nil;
font->ftsubstitute = 0;
font->fthint = 0;
font->t3matrix = fz_identity();
font->t3procs = nil;
font->bbox.x0 = 0;
font->bbox.y0 = 0;
font->bbox.x1 = 1000;
font->bbox.y1 = 1000;
return font;
}
fz_font *
fz_keepfont(fz_font *font)
{
font->refs ++;
return font;
}
void
fz_dropfont(fz_font *font)
{
int fterr;
int i;
if (font && --font->refs == 0)
{
if (font->t3procs)
{
for (i = 0; i < 256; i++)
if (font->t3procs[i])
fz_droptree(font->t3procs[i]);
fz_free(font->t3procs);
}
if (font->ftface)
{
fterr = FT_Done_Face((FT_Face)font->ftface);
if (fterr)
fz_warn("freetype finalizing face: %s", ft_errorstring(fterr));
fz_finalizefreetype();
}
fz_free(font);
}
}
void
fz_setfontbbox(fz_font *font, int xmin, int ymin, int xmax, int ymax)
{
font->bbox.x0 = xmin;
font->bbox.y0 = ymin;
font->bbox.x1 = xmax;
font->bbox.y1 = ymax;
}
/*
* Freetype hooks
*/
static FT_Library fz_ftlib = nil;
static int fz_ftlib_refs = 0;
#undef __FTERRORS_H__
#define FT_ERRORDEF(e, v, s) { (e), (s) },
#define FT_ERROR_START_LIST
#define FT_ERROR_END_LIST { 0, NULL }
struct ft_error
{
int err;
char *str;
};
static const struct ft_error ft_errors[] =
{
#include FT_ERRORS_H
};
char *ft_errorstring(int err)
{
const struct ft_error *e;
for (e = ft_errors; e->str != NULL; e++)
if (e->err == err)
return e->str;
return "Unknown error";
}
static fz_error
fz_initfreetype(void)
{
int fterr;
int maj, min, pat;
if (fz_ftlib)
{
fz_ftlib_refs++;
return fz_okay;
}
fterr = FT_Init_FreeType(&fz_ftlib);
if (fterr)
return fz_throw("cannot init freetype: %s", ft_errorstring(fterr));
FT_Library_Version(fz_ftlib, &maj, &min, &pat);
if (maj == 2 && min == 1 && pat < 7)
{
fterr = FT_Done_FreeType(fz_ftlib);
if (fterr)
fz_warn("freetype finalizing: %s", ft_errorstring(fterr));
return fz_throw("freetype version too old: %d.%d.%d", maj, min, pat);
}
fz_ftlib_refs++;
return fz_okay;
}
static void
fz_finalizefreetype(void)
{
int fterr;
if (--fz_ftlib_refs == 0)
{
fterr = FT_Done_FreeType(fz_ftlib);
if (fterr)
fz_warn("freetype finalizing: %s", ft_errorstring(fterr));
fz_ftlib = nil;
}
}
fz_error
fz_newfontfromfile(fz_font **fontp, char *path, int index)
{
fz_error error;
fz_font *font;
int fterr;
error = fz_initfreetype();
if (error)
return fz_rethrow(error, "cannot init freetype library");
font = fz_newfont();
if (!font)
return fz_rethrow(-1, "out of memory: font struct");
fterr = FT_New_Face(fz_ftlib, path, index, (FT_Face*)&font->ftface);
if (fterr)
{
fz_free(font);
return fz_throw("freetype: cannot load font: %s", ft_errorstring(fterr));
}
*fontp = font;
return fz_okay;
}
fz_error
fz_newfontfrombuffer(fz_font **fontp, unsigned char *data, int len, int index)
{
fz_error error;
fz_font *font;
int fterr;
error = fz_initfreetype();
if (error)
return fz_rethrow(error, "cannot init freetype library");
font = fz_newfont();
if (!font)
return fz_rethrow(-1, "out of memory: font struct");
fterr = FT_New_Memory_Face(fz_ftlib, data, len, index, (FT_Face*)&font->ftface);
if (fterr)
{
fz_free(font);
return fz_throw("freetype: cannot load font: %s", ft_errorstring(fterr));
}
*fontp = font;
return fz_okay;
}
fz_error
fz_renderftglyph(fz_glyph *glyph, fz_font *font, int gid, fz_matrix trm)
{
FT_Face face = font->ftface;
FT_Matrix m;
FT_Vector v;
FT_Error fterr;
int x, y;
#if 0
/* We lost this feature in refactoring.
* We can't access pdf_fontdesc metrics from fz_font.
* The pdf_fontdesc metrics are character based (cid),
* where the glyph being rendered is given by glyph (gid).
*/
if (font->ftsubstitute && font->wmode == 0)
{
fz_hmtx subw;
int realw;
float scale;
fterr = FT_Set_Char_Size(face, 1000, 1000, 72, 72);
if (fterr)
return fz_warn("freetype setting character size: %s", ft_errorstring(fterr));
fterr = FT_Load_Glyph(font->ftface, gid,
FT_LOAD_NO_HINTING | FT_LOAD_NO_BITMAP | FT_LOAD_IGNORE_TRANSFORM);
if (fterr)
return fz_throw("freetype failed to load glyph: %s", ft_errorstring(fterr));
realw = ((FT_Face)font->ftface)->glyph->advance.x;
subw = fz_gethmtx(font, cid); // <-- this is the offender
if (realw)
scale = (float) subw.w / realw;
else
scale = 1.0;
trm = fz_concat(fz_scale(scale, 1.0), trm);
}
#endif
glyph->w = 0;
glyph->h = 0;
glyph->x = 0;
glyph->y = 0;
glyph->samples = nil;
/* freetype mutilates complex glyphs if they are loaded
* with FT_Set_Char_Size 1.0. it rounds the coordinates
* before applying transformation. to get more precision in
* freetype, we shift part of the scale in the matrix
* into FT_Set_Char_Size instead
*/
m.xx = trm.a * 64; /* should be 65536 */
m.yx = trm.b * 64;
m.xy = trm.c * 64;
m.yy = trm.d * 64;
v.x = trm.e * 64;
v.y = trm.f * 64;
fterr = FT_Set_Char_Size(face, 65536, 65536, 72, 72); /* should be 64, 64 */
if (fterr)
fz_warn("freetype setting character size: %s", ft_errorstring(fterr));
FT_Set_Transform(face, &m, &v);
if (font->fthint)
{
/* Enable hinting, but keep the huge char size so that
* it is hinted for a character. This will in effect nullify
* the effect of grid fitting. This form of hinting should
* only be used for DynaLab and similar tricky TrueType fonts,
* so that we get the correct outline shape.
*/
#ifdef USE_HINTING
/* If you really want grid fitting, enable this code. */
float scale = fz_matrixexpansion(trm);
m.xx = trm.a * 65536 / scale;
m.xy = trm.b * 65536 / scale;
m.yx = trm.c * 65536 / scale;
m.yy = trm.d * 65536 / scale;
v.x = 0;
v.y = 0;
fterr = FT_Set_Char_Size(face, 64 * scale, 64 * scale, 72, 72);
if (fterr)
fz_warn("freetype setting character size: %s", ft_errorstring(fterr));
FT_Set_Transform(face, &m, &v);
#endif
fterr = FT_Load_Glyph(face, gid, FT_LOAD_NO_BITMAP);
if (fterr)
fz_warn("freetype load glyph (gid %d): %s", gid, ft_errorstring(fterr));
}
else
{
fterr = FT_Load_Glyph(face, gid, FT_LOAD_NO_BITMAP | FT_LOAD_NO_HINTING);
if (fterr)
fz_warn("freetype load glyph (gid %d): %s", gid, ft_errorstring(fterr));
}
fterr = FT_Render_Glyph(face->glyph, ft_render_mode_normal);
if (fterr)
fz_warn("freetype render glyph (gid %d): %s", gid, ft_errorstring(fterr));
glyph->w = face->glyph->bitmap.width;
glyph->h = face->glyph->bitmap.rows;
glyph->x = face->glyph->bitmap_left;
glyph->y = face->glyph->bitmap_top - glyph->h;
glyph->samples = face->glyph->bitmap.buffer;
for (y = 0; y < glyph->h / 2; y++)
{
for (x = 0; x < glyph->w; x++)
{
unsigned char a = glyph->samples[y * glyph->w + x ];
unsigned char b = glyph->samples[(glyph->h - y - 1) * glyph->w + x];
glyph->samples[y * glyph->w + x ] = b;
glyph->samples[(glyph->h - y - 1) * glyph->w + x] = a;
}
}
return fz_okay;
}
/*
* Type 3 fonts...
*/
fz_font *
fz_newtype3font(char *name, fz_matrix matrix)
{
fz_font *font;
int i;
font = fz_newfont();
font->t3procs = fz_malloc(sizeof(fz_tree*) * 256);
font->t3widths = fz_malloc(sizeof(float) * 256);
strlcpy(font->name, name, sizeof(font->name));
font->t3matrix = matrix;
for (i = 0; i < 256; i++)
{
font->t3procs[i] = nil;
font->t3widths[i] = 0;
}
return font;
}
/* XXX UGLY HACK XXX */
extern fz_colorspace *pdf_devicegray;
fz_error
fz_rendert3glyph(fz_glyph *glyph, fz_font *font, int gid, fz_matrix trm)
{
fz_error error;
fz_renderer *gc;
fz_tree *tree;
fz_matrix ctm;
fz_irect bbox;
/* TODO: make it reentrant */
static fz_pixmap *pixmap = nil;
if (pixmap)
{
fz_droppixmap(pixmap);
pixmap = nil;
}
if (gid < 0 || gid > 255)
return fz_throw("assert: glyph out of range");
tree = font->t3procs[gid];
if (!tree)
{
glyph->w = 0;
glyph->h = 0;
return fz_okay;
}
ctm = fz_concat(font->t3matrix, trm);
bbox = fz_roundrect(fz_boundtree(tree, ctm));
error = fz_newrenderer(&gc, pdf_devicegray, 1, 4096);
if (error)
return fz_rethrow(error, "cannot create renderer");
error = fz_rendertree(&pixmap, gc, tree, ctm, bbox, 0);
fz_droprenderer(gc);
if (error)
return fz_rethrow(error, "cannot render glyph");
assert(pixmap->n == 1);
glyph->x = pixmap->x;
glyph->y = pixmap->y;
glyph->w = pixmap->w;
glyph->h = pixmap->h;
glyph->samples = pixmap->samples;
return fz_okay;
}
void
fz_debugfont(fz_font *font)
{
printf("font '%s' {\n", font->name);
if (font->ftface)
{
printf(" freetype face %p\n", font->ftface);
if (font->ftsubstitute)
printf(" substitute font\n");
}
if (font->t3procs)
{
printf(" type3 matrix [%g %g %g %g]\n",
font->t3matrix.a, font->t3matrix.b,
font->t3matrix.c, font->t3matrix.d);
}
printf(" bbox [%d %d %d %d]\n",
font->bbox.x0, font->bbox.y0,
font->bbox.x1, font->bbox.y1);
printf("}\n");
}
|