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
436
437
438
439
440
441
442
443
444
445
446
447
|
#include "fitz.h"
#define LINEDIST 0.9f
#define SPACEDIST 0.2f
#include <ft2build.h>
#include FT_FREETYPE_H
#if ((FREETYPE_MAJOR == 2) && (FREETYPE_MINOR == 1)) || \
((FREETYPE_MAJOR == 2) && (FREETYPE_MINOR == 2)) || \
((FREETYPE_MAJOR == 2) && (FREETYPE_MINOR == 3) && (FREETYPE_PATCH < 8))
int
FT_Get_Advance(FT_Face face, int gid, int masks, FT_Fixed *out)
{
int err;
err = FT_Load_Glyph(face, gid, masks);
if (err)
return err;
if (masks & FT_LOAD_VERTICAL_LAYOUT)
*out = face->glyph->metrics.vertAdvance << 10;
else
*out = face->glyph->metrics.horiAdvance << 10;
return 0;
}
#else
#include FT_ADVANCES_H
#endif
typedef struct fz_textdevice_s fz_textdevice;
struct fz_textdevice_s
{
fz_point point;
fz_textspan *head;
fz_textspan *span;
};
fz_textspan *
fz_newtextspan(void)
{
fz_textspan *span;
span = fz_malloc(sizeof(fz_textspan));
span->font = nil;
span->wmode = 0;
span->size = 0;
span->len = 0;
span->cap = 0;
span->text = nil;
span->next = nil;
span->eol = 0;
return span;
}
void
fz_freetextspan(fz_textspan *span)
{
if (span->font)
fz_dropfont(span->font);
if (span->next)
fz_freetextspan(span->next);
fz_free(span->text);
fz_free(span);
}
static void
fz_addtextcharimp(fz_textspan *span, int c, fz_bbox bbox)
{
if (span->len + 1 >= span->cap)
{
span->cap = span->cap > 1 ? (span->cap * 3) / 2 : 80;
span->text = fz_realloc(span->text, span->cap, sizeof(fz_textchar));
}
span->text[span->len].c = c;
span->text[span->len].bbox = bbox;
span->len ++;
}
static fz_bbox
fz_splitbbox(fz_bbox bbox, int i, int n)
{
float w = (float)(bbox.x1 - bbox.x0) / n;
float x0 = bbox.x0;
bbox.x0 = x0 + i * w;
bbox.x1 = x0 + (i + 1) * w;
return bbox;
}
static void
fz_addtextchar(fz_textspan **last, fz_font *font, float size, int wmode, int c, fz_bbox bbox)
{
fz_textspan *span = *last;
if (!span->font)
{
span->font = fz_keepfont(font);
span->size = size;
}
if (span->font != font || span->size != size || span->wmode != wmode)
{
span = fz_newtextspan();
span->font = fz_keepfont(font);
span->size = size;
span->wmode = wmode;
(*last)->next = span;
*last = span;
}
switch (c)
{
case -1: /* ignore when one unicode character maps to multiple glyphs */
break;
case 0xFB00: /* ff */
fz_addtextcharimp(span, 'f', fz_splitbbox(bbox, 0, 2));
fz_addtextcharimp(span, 'f', fz_splitbbox(bbox, 1, 2));
break;
case 0xFB01: /* fi */
fz_addtextcharimp(span, 'f', fz_splitbbox(bbox, 0, 2));
fz_addtextcharimp(span, 'i', fz_splitbbox(bbox, 1, 2));
break;
case 0xFB02: /* fl */
fz_addtextcharimp(span, 'f', fz_splitbbox(bbox, 0, 2));
fz_addtextcharimp(span, 'l', fz_splitbbox(bbox, 1, 2));
break;
case 0xFB03: /* ffi */
fz_addtextcharimp(span, 'f', fz_splitbbox(bbox, 0, 3));
fz_addtextcharimp(span, 'f', fz_splitbbox(bbox, 1, 3));
fz_addtextcharimp(span, 'i', fz_splitbbox(bbox, 2, 3));
break;
case 0xFB04: /* ffl */
fz_addtextcharimp(span, 'f', fz_splitbbox(bbox, 0, 3));
fz_addtextcharimp(span, 'f', fz_splitbbox(bbox, 1, 3));
fz_addtextcharimp(span, 'l', fz_splitbbox(bbox, 2, 3));
break;
case 0xFB05: /* long st */
case 0xFB06: /* st */
fz_addtextcharimp(span, 's', fz_splitbbox(bbox, 0, 2));
fz_addtextcharimp(span, 't', fz_splitbbox(bbox, 1, 2));
break;
default:
fz_addtextcharimp(span, c, bbox);
break;
}
}
static void
fz_dividetextchars(fz_textspan **last, int n, fz_bbox bbox)
{
fz_textspan *span = *last;
int i, x;
x = span->len - n;
if (x >= 0)
for (i = 0; i < n; i++)
span->text[x + i].bbox = fz_splitbbox(bbox, i, n);
}
static void
fz_addtextnewline(fz_textspan **last, fz_font *font, float size, int wmode)
{
fz_textspan *span;
span = fz_newtextspan();
span->font = fz_keepfont(font);
span->size = size;
span->wmode = wmode;
(*last)->eol = 1;
(*last)->next = span;
*last = span;
}
void
fz_debugtextspanxml(fz_textspan *span)
{
char buf[10];
int c, n, k, i;
printf("<span font=\"%s\" size=\"%g\" wmode=\"%d\" eol=\"%d\">\n",
span->font ? span->font->name : "NULL", span->size, span->wmode, span->eol);
for (i = 0; i < span->len; i++)
{
printf("\t<char ucs=\"");
c = span->text[i].c;
if (c < 128)
putchar(c);
else
{
n = runetochar(buf, &c);
for (k = 0; k < n; k++)
putchar(buf[k]);
}
printf("\" bbox=\"[%d %d %d %d]\">\n",
span->text[i].bbox.x0,
span->text[i].bbox.y0,
span->text[i].bbox.x1,
span->text[i].bbox.y1);
}
printf("</span>\n");
if (span->next)
fz_debugtextspanxml(span->next);
}
void
fz_debugtextspan(fz_textspan *span)
{
char buf[10];
int c, n, k, i;
for (i = 0; i < span->len; i++)
{
c = span->text[i].c;
if (c < 128)
putchar(c);
else
{
n = runetochar(buf, &c);
for (k = 0; k < n; k++)
putchar(buf[k]);
}
}
if (span->eol)
putchar('\n');
if (span->next)
fz_debugtextspan(span->next);
}
static void
fz_textextractspan(fz_textspan **last, fz_text *text, fz_matrix ctm, fz_point *pen)
{
fz_font *font = text->font;
FT_Face face = font->ftface;
fz_matrix tm = text->trm;
fz_matrix trm;
float size;
float adv;
fz_rect rect;
fz_point dir, ndir;
fz_point delta, ndelta;
float dist, dot;
float ascender = 1;
float descender = 0;
int multi;
int i, err;
if (text->len == 0)
return;
if (font->ftface)
{
err = FT_Set_Char_Size(font->ftface, 64, 64, 72, 72);
if (err)
fz_warn("freetype set character size: %s", ft_errorstring(err));
ascender = (float)face->ascender / face->units_per_EM;
descender = (float)face->descender / face->units_per_EM;
}
rect = fz_emptyrect;
if (text->wmode == 0)
{
dir.x = 1;
dir.y = 0;
}
else
{
dir.x = 0;
dir.y = 1;
}
tm.e = 0;
tm.f = 0;
trm = fz_concat(tm, ctm);
dir = fz_transformvector(trm, dir);
dist = sqrtf(dir.x * dir.x + dir.y * dir.y);
ndir.x = dir.x / dist;
ndir.y = dir.y / dist;
size = fz_matrixexpansion(trm);
multi = 1;
for (i = 0; i < text->len; i++)
{
if (text->els[i].gid < 0)
{
fz_addtextchar(last, font, size, text->wmode, text->els[i].ucs, fz_roundrect(rect));
multi ++;
fz_dividetextchars(last, multi, fz_roundrect(rect));
continue;
}
multi = 1;
/* Calculate new pen location and delta */
tm.e = text->els[i].x;
tm.f = text->els[i].y;
trm = fz_concat(tm, ctm);
delta.x = pen->x - trm.e;
delta.y = pen->y - trm.f;
if (pen->x == -1 && pen->y == -1)
delta.x = delta.y = 0;
dist = sqrtf(delta.x * delta.x + delta.y * delta.y);
/* Add space and newlines based on pen movement */
if (dist > 0)
{
ndelta.x = delta.x / dist;
ndelta.y = delta.y / dist;
dot = ndelta.x * ndir.x + ndelta.y * ndir.y;
if (dist > size * LINEDIST)
{
fz_addtextnewline(last, font, size, text->wmode);
}
else if (fabsf(dot) > 0.95f && dist > size * SPACEDIST)
{
if ((*last)->len > 0 && (*last)->text[(*last)->len - 1].c != ' ')
{
fz_rect spacerect;
spacerect.x0 = -0.2f;
spacerect.y0 = 0;
spacerect.x1 = 0;
spacerect.y1 = 1;
spacerect = fz_transformrect(trm, spacerect);
fz_addtextchar(last, font, size, text->wmode, ' ', fz_roundrect(spacerect));
}
}
}
/* Calculate bounding box and new pen position based on font metrics */
if (font->ftface)
{
FT_Fixed ftadv = 0;
int mask = FT_LOAD_NO_BITMAP | FT_LOAD_NO_HINTING | FT_LOAD_IGNORE_TRANSFORM;
if (text->wmode)
mask |= FT_LOAD_VERTICAL_LAYOUT;
FT_Get_Advance(font->ftface, text->els[i].gid, mask, &ftadv);
adv = ftadv / 65536.0f;
if (text->wmode)
{
adv = -1; /* TODO: freetype returns broken vertical metrics */
rect.x0 = 0;
rect.y0 = 0;
rect.x1 = 1;
rect.y1 = adv;
}
else
{
rect.x0 = 0;
rect.y0 = descender;
rect.x1 = adv;
rect.y1 = ascender;
}
}
else
{
adv = font->t3widths[text->els[i].gid];
rect.x0 = 0;
rect.y0 = descender;
rect.x1 = adv;
rect.y1 = ascender;
}
rect = fz_transformrect(trm, rect);
pen->x = trm.e + dir.x * adv;
pen->y = trm.f + dir.y * adv;
fz_addtextchar(last, font, size, text->wmode, text->els[i].ucs, fz_roundrect(rect));
}
}
static void
fz_textfilltext(void *user, fz_text *text, fz_matrix ctm,
fz_colorspace *colorspace, float *color, float alpha)
{
fz_textdevice *tdev = user;
fz_textextractspan(&tdev->span, text, ctm, &tdev->point);
}
static void
fz_textstroketext(void *user, fz_text *text, fz_strokestate *stroke, fz_matrix ctm,
fz_colorspace *colorspace, float *color, float alpha)
{
fz_textdevice *tdev = user;
fz_textextractspan(&tdev->span, text, ctm, &tdev->point);
}
static void
fz_textcliptext(void *user, fz_text *text, fz_matrix ctm, int accumulate)
{
fz_textdevice *tdev = user;
fz_textextractspan(&tdev->span, text, ctm, &tdev->point);
}
static void
fz_textclipstroketext(void *user, fz_text *text, fz_strokestate *stroke, fz_matrix ctm)
{
fz_textdevice *tdev = user;
fz_textextractspan(&tdev->span, text, ctm, &tdev->point);
}
static void
fz_textignoretext(void *user, fz_text *text, fz_matrix ctm)
{
fz_textdevice *tdev = user;
fz_textextractspan(&tdev->span, text, ctm, &tdev->point);
}
static void
fz_textfreeuser(void *user)
{
fz_textdevice *tdev = user;
tdev->span->eol = 1;
/* TODO: unicode NFC normalization */
/* TODO: bidi logical reordering */
fz_free(tdev);
}
fz_device *
fz_newtextdevice(fz_textspan *root)
{
fz_device *dev;
fz_textdevice *tdev = fz_malloc(sizeof(fz_textdevice));
tdev->head = root;
tdev->span = root;
tdev->point.x = -1;
tdev->point.y = -1;
dev = fz_newdevice(tdev);
dev->hints = FZ_IGNOREIMAGE | FZ_IGNORESHADE;
dev->freeuser = fz_textfreeuser;
dev->filltext = fz_textfilltext;
dev->stroketext = fz_textstroketext;
dev->cliptext = fz_textcliptext;
dev->clipstroketext = fz_textclipstroketext;
dev->ignoretext = fz_textignoretext;
return dev;
}
|