/src/ghostpdl/base/ttfmain.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* Copyright (C) 2001-2023 Artifex Software, Inc. |
2 | | All Rights Reserved. |
3 | | |
4 | | This software is provided AS-IS with no warranty, either express or |
5 | | implied. |
6 | | |
7 | | This software is distributed under license and may not be copied, |
8 | | modified or distributed except as expressly authorized under the terms |
9 | | of the license contained in the file LICENSE in this distribution. |
10 | | |
11 | | Refer to licensing information at http://www.artifex.com or contact |
12 | | Artifex Software, Inc., 39 Mesa Street, Suite 108A, San Francisco, |
13 | | CA 94129, USA, for further information. |
14 | | */ |
15 | | |
16 | | |
17 | | /* A Free Type interface adapter. */ |
18 | | /* Uses code fragments from the FreeType project. */ |
19 | | |
20 | | #include "ttmisc.h" |
21 | | #include "ttfoutl.h" |
22 | | #include "ttfmemd.h" |
23 | | |
24 | | #include "ttfinp.h" |
25 | | #include "ttfsfnt.h" |
26 | | #include "ttobjs.h" |
27 | | #include "ttinterp.h" |
28 | | #include "ttcalc.h" |
29 | | |
30 | | static const bool skip_instructions = 0; /* Debug purpose only. */ |
31 | | |
32 | | typedef struct { |
33 | | TT_Fixed a, b, c, d, tx, ty; |
34 | | } FixMatrix; |
35 | | |
36 | | struct ttfSubGlyphUsage_s { |
37 | | FixMatrix m; |
38 | | int index; |
39 | | int flags; |
40 | | short arg1, arg2; |
41 | | }; |
42 | | |
43 | | /*------------------------------------------------------------------- */ |
44 | | |
45 | | static TT_Fixed AVE(F26Dot6 a, F26Dot6 b) |
46 | 0 | { return (a + b) / 2; |
47 | 0 | } |
48 | | |
49 | | static F26Dot6 shortToF26Dot6(short a) |
50 | 0 | { return (F26Dot6)a << 6; |
51 | 0 | } |
52 | | |
53 | | static F26Dot6 floatToF26Dot6(float a) |
54 | 0 | { return (F26Dot6)(a * (1 << 6) + 0.5); |
55 | 0 | } |
56 | | |
57 | | static TT_Fixed floatToF16Dot16(float a) |
58 | 0 | { return (F26Dot6)(a * (1 << 16) + 0.5); |
59 | 0 | } |
60 | | |
61 | | static void TransformF26Dot6PointFix(F26Dot6Point *pt, F26Dot6 dx, F26Dot6 dy, FixMatrix *m) |
62 | 0 | { pt->x = MulDiv(dx, m->a, 65536) + MulDiv(dy, m->c, 65536) + (m->tx >> 10); |
63 | 0 | pt->y = MulDiv(dx, m->b, 65536) + MulDiv(dy, m->d, 65536) + (m->ty >> 10); |
64 | 0 | } |
65 | | |
66 | | static void TransformF26Dot6PointFloat(FloatPoint *pt, F26Dot6 dx, F26Dot6 dy, FloatMatrix *m) |
67 | 0 | { pt->x = dx * m->a / 64 + dy * m->c / 64 + m->tx; |
68 | 0 | pt->y = dx * m->b / 64 + dy * m->d / 64 + m->ty; |
69 | 0 | } |
70 | | |
71 | | /*-------------------------------------------------------------------*/ |
72 | | |
73 | | static ttfPtrElem *ttfFont__get_table_ptr(ttfFont *f, char *id) |
74 | 0 | { |
75 | 0 | if (!memcmp(id, "cvt ", 4)) |
76 | 0 | return &f->t_cvt_; |
77 | 0 | if (!memcmp(id, "fpgm", 4)) |
78 | 0 | return &f->t_fpgm; |
79 | 0 | if (!memcmp(id, "glyf", 4)) |
80 | 0 | return &f->t_glyf; |
81 | 0 | if (!memcmp(id, "head", 4)) |
82 | 0 | return &f->t_head; |
83 | 0 | if (!memcmp(id, "hhea", 4)) |
84 | 0 | return &f->t_hhea; |
85 | 0 | if (!memcmp(id, "hmtx", 4)) |
86 | 0 | return &f->t_hmtx; |
87 | 0 | if (!memcmp(id, "vhea", 4)) |
88 | 0 | return &f->t_vhea; |
89 | 0 | if (!memcmp(id, "vmtx", 4)) |
90 | 0 | return &f->t_vmtx; |
91 | 0 | if (!memcmp(id, "loca", 4)) |
92 | 0 | return &f->t_loca; |
93 | 0 | if (!memcmp(id, "maxp", 4)) |
94 | 0 | return &f->t_maxp; |
95 | 0 | if (!memcmp(id, "prep", 4)) |
96 | 0 | return &f->t_prep; |
97 | 0 | if (!memcmp(id, "cmap", 4)) |
98 | 0 | return &f->t_cmap; |
99 | 0 | return 0; |
100 | 0 | } |
101 | | |
102 | | /*-------------------------------------------------------------------*/ |
103 | | |
104 | | TT_Error TT_Set_Instance_CharSizes(TT_Instance instance, |
105 | | TT_F26Dot6 charWidth, |
106 | | TT_F26Dot6 charHeight) |
107 | 0 | { |
108 | 0 | PInstance ins = instance.z; |
109 | |
|
110 | 0 | if ( !ins ) |
111 | 0 | return TT_Err_Invalid_Instance_Handle; |
112 | | |
113 | 0 | if (charWidth < 1*64) |
114 | 0 | charWidth = 1*64; |
115 | |
|
116 | 0 | if (charHeight < 1*64) |
117 | 0 | charHeight = 1*64; |
118 | |
|
119 | 0 | ins->metrics.x_scale1 = charWidth; |
120 | 0 | ins->metrics.y_scale1 = charHeight; |
121 | 0 | ins->metrics.x_scale2 = ins->face->font->nUnitsPerEm; |
122 | 0 | ins->metrics.y_scale2 = ins->face->font->nUnitsPerEm; |
123 | |
|
124 | 0 | if (ins->face->font->nFlags & 8) { |
125 | 0 | ins->metrics.x_scale1 = (ins->metrics.x_scale1+32) & -64; |
126 | 0 | ins->metrics.y_scale1 = (ins->metrics.y_scale1+32) & -64; |
127 | 0 | } |
128 | |
|
129 | 0 | ins->metrics.x_ppem = ins->metrics.x_scale1 / 64; |
130 | 0 | ins->metrics.y_ppem = ins->metrics.y_scale1 / 64; |
131 | |
|
132 | 0 | if (charWidth > charHeight) |
133 | 0 | ins->metrics.pointSize = charWidth; |
134 | 0 | else |
135 | 0 | ins->metrics.pointSize = charHeight; |
136 | |
|
137 | 0 | ins->valid = FALSE; |
138 | 0 | return Instance_Reset(ins, FALSE); |
139 | 0 | } |
140 | | |
141 | | /*-------------------------------------------------------------------*/ |
142 | | |
143 | | int ttfInterpreter__obtain(ttfMemory *mem, ttfInterpreter **ptti) |
144 | 0 | { |
145 | 0 | ttfInterpreter *tti; |
146 | |
|
147 | 0 | if (*ptti) { |
148 | 0 | (*ptti)->lock++; |
149 | 0 | return 0; |
150 | 0 | } |
151 | 0 | tti = mem->alloc_struct(mem, (const ttfMemoryDescriptor *)&st_ttfInterpreter, "ttfInterpreter__obtain"); |
152 | 0 | if (!tti) |
153 | 0 | return fMemoryError; |
154 | 0 | tti->usage = 0; |
155 | 0 | tti->usage_size = 0; |
156 | 0 | tti->ttf_memory = mem; |
157 | 0 | tti->lock = 1; |
158 | 0 | tti->exec = mem->alloc_struct(mem, (const ttfMemoryDescriptor *)&st_TExecution_Context, "ttfInterpreter__obtain"); |
159 | 0 | if (!tti->exec) { |
160 | 0 | mem->free(mem, tti, "ttfInterpreter__obtain"); |
161 | 0 | return fMemoryError; |
162 | 0 | } |
163 | 0 | memset(tti->exec, 0, sizeof(*tti->exec)); |
164 | 0 | *ptti = tti; |
165 | 0 | return 0; |
166 | 0 | } |
167 | | |
168 | | void ttfInterpreter__release(ttfInterpreter **ptti) |
169 | 0 | { |
170 | 0 | ttfInterpreter *tti = *ptti; |
171 | 0 | ttfMemory *mem = tti->ttf_memory; |
172 | |
|
173 | 0 | if(--tti->lock) |
174 | 0 | return; |
175 | 0 | mem->free(mem, tti->usage, "ttfInterpreter__release"); |
176 | 0 | mem->free(mem, tti->exec, "ttfInterpreter__release"); |
177 | 0 | mem->free(mem, *ptti, "ttfInterpreter__release"); |
178 | 0 | *ptti = 0; |
179 | 0 | } |
180 | | |
181 | | /*-------------------------------------------------------------------*/ |
182 | | |
183 | | void ttfFont__init(ttfFont *self, ttfMemory *mem, |
184 | | void (*DebugRepaint)(ttfFont *), |
185 | | int (*DebugPrint)(ttfFont *, const char *s, ...), |
186 | | const gs_memory_t *DebugMem) |
187 | 0 | { |
188 | 0 | memset(self, 0, sizeof(*self)); |
189 | 0 | self->DebugRepaint = DebugRepaint; |
190 | 0 | self->DebugPrint = DebugPrint; |
191 | 0 | self->DebugMem = DebugMem; |
192 | 0 | } |
193 | | |
194 | | void ttfFont__finit(ttfFont *self) |
195 | 0 | { ttfMemory *mem = self->tti->ttf_memory; |
196 | |
|
197 | 0 | if (self->exec) { |
198 | 0 | if (self->inst) |
199 | 0 | Context_Destroy(self->exec); |
200 | 0 | else { |
201 | | /* Context_Create was not called - see ttfFont__Open. |
202 | | Must not call Context_Destroy for proper 'lock' count. |
203 | | */ |
204 | 0 | } |
205 | 0 | } |
206 | 0 | self->exec = NULL; |
207 | 0 | if (self->inst) |
208 | 0 | Instance_Destroy(self->inst); |
209 | 0 | mem->free(mem, self->inst, "ttfFont__finit"); |
210 | 0 | self->inst = NULL; |
211 | 0 | if (self->face) |
212 | 0 | Face_Destroy(self->face); |
213 | 0 | mem->free(mem, self->face, "ttfFont__finit"); |
214 | 0 | self->face = NULL; |
215 | 0 | } |
216 | | |
217 | 0 | #define MAX_SUBGLYPH_NESTING 3 /* Arbitrary. We need this because we don't want |
218 | | a ttfOutliner__BuildGlyphOutline recursion |
219 | | while a glyph is loaded in ttfReader. */ |
220 | | |
221 | | FontError ttfFont__Open(ttfInterpreter *tti, ttfFont *self, ttfReader *r, |
222 | | unsigned int nTTC, float w, float h, |
223 | | bool design_grid) |
224 | 0 | { char sVersion[4], sVersion1[4] = {0, 1, 0, 0}; |
225 | 0 | char sVersion2[4] = {0, 2, 0, 0}; |
226 | 0 | unsigned int nNumTables, i; |
227 | 0 | TT_Error code, code1 = 0; |
228 | 0 | int k; |
229 | 0 | TT_Instance I; |
230 | 0 | ttfMemory *mem = tti->ttf_memory; |
231 | 0 | F26Dot6 ww, hh; |
232 | |
|
233 | 0 | self->tti = tti; |
234 | 0 | self->design_grid = design_grid; |
235 | 0 | r->Read(r, sVersion, 4); |
236 | 0 | if(!memcmp(sVersion, "ttcf", 4)) { |
237 | 0 | unsigned int nFonts; |
238 | 0 | unsigned int nPos = 0xbaadf00d; /* Quiet compiler. */ |
239 | |
|
240 | 0 | r->Read(r, sVersion, 4); |
241 | 0 | if(memcmp(sVersion, sVersion1, 4) && memcmp(sVersion, sVersion2, 4)) |
242 | 0 | return fUnimplemented; |
243 | 0 | nFonts = ttfReader__UInt(r); |
244 | 0 | if (nFonts == 0) |
245 | 0 | return fBadFontData; |
246 | 0 | if(nTTC >= nFonts) |
247 | 0 | return fTableNotFound; |
248 | 0 | for(i = 0; i <= nTTC; i++) |
249 | 0 | nPos = ttfReader__UInt(r); |
250 | 0 | r->Seek(r, nPos); |
251 | 0 | r->Read(r, sVersion, 4); |
252 | 0 | } |
253 | 0 | if(memcmp(sVersion, sVersion1, 4) && memcmp(sVersion, "true", 4)) |
254 | 0 | return fUnimplemented; |
255 | 0 | nNumTables = ttfReader__UShort(r); |
256 | 0 | ttfReader__UShort(r); /* nSearchRange */ |
257 | 0 | ttfReader__UShort(r); /* nEntrySelector */ |
258 | 0 | ttfReader__UShort(r); /* nRangeShift */ |
259 | 0 | for (i = 0; i < nNumTables; i++) { |
260 | 0 | char sTag[5]; |
261 | 0 | unsigned int nOffset, nLength; |
262 | 0 | ttfPtrElem *e; |
263 | |
|
264 | 0 | sTag[4] = 0; |
265 | 0 | r->Read(r, sTag, 4); |
266 | 0 | ttfReader__UInt(r); /* nCheckSum */ |
267 | 0 | nOffset = ttfReader__UInt(r); |
268 | 0 | nLength = ttfReader__UInt(r); |
269 | 0 | e = ttfFont__get_table_ptr(self, sTag); |
270 | 0 | if (e != NULL) { |
271 | 0 | e->nPos = nOffset; |
272 | 0 | e->nLen = nLength; |
273 | 0 | } |
274 | 0 | } |
275 | 0 | r->Seek(r, self->t_head.nPos + offset_of(sfnt_FontHeader, flags)); |
276 | 0 | self->nFlags = ttfReader__UShort(r); |
277 | 0 | r->Seek(r, self->t_head.nPos + offset_of(sfnt_FontHeader, unitsPerEm)); |
278 | 0 | self->nUnitsPerEm = ttfReader__UShort(r); |
279 | 0 | if (self->nUnitsPerEm <= 0) |
280 | 0 | self->nUnitsPerEm = 1024; |
281 | 0 | r->Seek(r, self->t_head.nPos + offset_of(sfnt_FontHeader, indexToLocFormat)); |
282 | 0 | self->nIndexToLocFormat = ttfReader__UShort(r); |
283 | 0 | r->Seek(r, self->t_maxp.nPos + offset_of(sfnt_maxProfileTable, numGlyphs)); |
284 | 0 | self->nNumGlyphs = ttfReader__UShort(r); |
285 | 0 | r->Seek(r, self->t_maxp.nPos + offset_of(sfnt_maxProfileTable, maxComponentElements)); |
286 | 0 | self->nMaxComponents = ttfReader__UShort(r); |
287 | 0 | if(self->nMaxComponents < 10) |
288 | 0 | self->nMaxComponents = 10; /* work around DynaLab bug in lgoth.ttf */ |
289 | 0 | r->Seek(r, self->t_hhea.nPos + offset_of(sfnt_MetricsHeader, numberLongMetrics)); |
290 | 0 | self->nLongMetricsHorz = ttfReader__UShort(r); |
291 | 0 | if (self->t_vhea.nPos != 0) { |
292 | 0 | r->Seek(r, self->t_vhea.nPos + offset_of(sfnt_MetricsHeader, numberLongMetrics)); |
293 | 0 | self->nLongMetricsVert = ttfReader__UShort(r); |
294 | 0 | } else |
295 | 0 | self->nLongMetricsVert = 0; |
296 | 0 | if (tti->usage_size < self->nMaxComponents * MAX_SUBGLYPH_NESTING) { |
297 | 0 | tti->ttf_memory->free(tti->ttf_memory, tti->usage, "ttfFont__Open"); |
298 | 0 | tti->usage_size = 0; |
299 | 0 | tti->usage = mem->alloc_bytes(mem, |
300 | 0 | sizeof(ttfSubGlyphUsage) * self->nMaxComponents * MAX_SUBGLYPH_NESTING, |
301 | 0 | "ttfFont__Open"); |
302 | 0 | if (tti->usage == NULL) |
303 | 0 | return fMemoryError; |
304 | 0 | tti->usage_size = self->nMaxComponents * MAX_SUBGLYPH_NESTING; |
305 | 0 | } |
306 | 0 | self->face = mem->alloc_struct(mem, (const ttfMemoryDescriptor *)&st_TFace, "ttfFont__Open"); |
307 | 0 | if (self->face == NULL) |
308 | 0 | return fMemoryError; |
309 | 0 | memset(self->face, 0, sizeof(*self->face)); |
310 | 0 | self->face->r = r; |
311 | 0 | self->face->font = self; |
312 | 0 | self->exec = tti->exec; |
313 | 0 | code = Face_Create(self->face); |
314 | 0 | if (code) |
315 | 0 | return fMemoryError; |
316 | 0 | code = r->Error(r); |
317 | 0 | if (code < 0) |
318 | 0 | return fBadFontData; |
319 | 0 | self->inst = mem->alloc_struct(mem, (const ttfMemoryDescriptor *)&st_TInstance, "ttfFont__Open"); |
320 | 0 | if (self->inst == NULL) |
321 | 0 | return fMemoryError; |
322 | 0 | memset(self->inst, 0, sizeof(*self->inst)); |
323 | 0 | code = Context_Create(self->exec, self->face); /* See comment in the implementation of Context_Create. */ |
324 | 0 | if (code == TT_Err_Out_Of_Memory) |
325 | 0 | return fMemoryError; |
326 | 0 | code = Instance_Create(self->inst, self->face); |
327 | 0 | if (code == TT_Err_Out_Of_Memory) |
328 | 0 | return fMemoryError; |
329 | 0 | if (code) |
330 | 0 | return fBadFontData; |
331 | 0 | for(k = 0; k < self->face->cvtSize; k++) |
332 | 0 | self->inst->cvt[k] = shortToF26Dot6(self->face->cvt[k]); |
333 | 0 | code = Instance_Init(self->inst); |
334 | 0 | if (code == TT_Err_Out_Of_Memory) |
335 | 0 | return fMemoryError; |
336 | 0 | if (code >= TT_Err_Invalid_Opcode && code <= TT_Err_Invalid_Displacement) |
337 | 0 | code1 = fBadInstruction; |
338 | 0 | else if (code) |
339 | 0 | return fBadFontData; |
340 | 0 | I.z = self->inst; |
341 | 0 | if (design_grid) |
342 | 0 | ww = hh = shortToF26Dot6(self->nUnitsPerEm); |
343 | 0 | else { |
344 | | /* Round towards zero for a better view of mirrored characters : */ |
345 | 0 | ww = floatToF26Dot6(w); |
346 | 0 | hh = floatToF26Dot6(h); |
347 | 0 | } |
348 | 0 | code = TT_Set_Instance_CharSizes(I, ww, hh); |
349 | 0 | self->inst->metrics = self->exec->metrics; |
350 | 0 | if (code == TT_Err_Invalid_Engine) |
351 | 0 | return fPatented; |
352 | 0 | if (code == TT_Err_Out_Of_Memory) |
353 | 0 | return fMemoryError; |
354 | 0 | if (code >= TT_Err_Invalid_Opcode && code <= TT_Err_Invalid_Displacement) |
355 | 0 | return fBadInstruction; |
356 | 0 | if (code) |
357 | 0 | return fBadFontData; |
358 | 0 | if (code1) |
359 | 0 | return code1; |
360 | 0 | return code; |
361 | 0 | } |
362 | | |
363 | | static void ttfFont__StartGlyph(ttfFont *self) |
364 | 0 | { Context_Load( self->exec, self->inst ); |
365 | 0 | if ( self->inst->GS.instruct_control & 2 ) |
366 | 0 | self->exec->GS = Default_GraphicsState; |
367 | 0 | else |
368 | 0 | self->exec->GS = self->inst->GS; |
369 | 0 | self->tti->usage_top = 0; |
370 | 0 | } |
371 | | |
372 | | static void ttfFont__StopGlyph(ttfFont *self) |
373 | 0 | { |
374 | 0 | Context_Save(self->exec, self->inst); |
375 | 0 | } |
376 | | |
377 | | /*-------------------------------------------------------------------*/ |
378 | | |
379 | | static void mount_zone( PGlyph_Zone source, |
380 | | PGlyph_Zone target ) |
381 | 0 | { |
382 | 0 | Int np, nc; |
383 | |
|
384 | 0 | np = source->n_points; |
385 | 0 | nc = source->n_contours; |
386 | |
|
387 | 0 | target->org_x = source->org_x + np; |
388 | 0 | target->org_y = source->org_y + np; |
389 | 0 | target->cur_x = source->cur_x + np; |
390 | 0 | target->cur_y = source->cur_y + np; |
391 | 0 | target->touch = source->touch + np; |
392 | |
|
393 | 0 | target->contours = source->contours + nc; |
394 | |
|
395 | 0 | target->n_points = 0; |
396 | 0 | target->n_contours = 0; |
397 | 0 | } |
398 | | |
399 | | static void Init_Glyph_Component( PSubglyph_Record element, |
400 | | PSubglyph_Record original, |
401 | | PExecution_Context exec ) |
402 | 0 | { |
403 | 0 | element->index = -1; |
404 | 0 | element->is_scaled = FALSE; |
405 | 0 | element->is_hinted = FALSE; |
406 | |
|
407 | 0 | if (original) |
408 | 0 | mount_zone( &original->zone, &element->zone ); |
409 | 0 | else |
410 | 0 | element->zone = exec->pts; |
411 | |
|
412 | 0 | element->zone.n_contours = 0; |
413 | 0 | element->zone.n_points = 0; |
414 | |
|
415 | 0 | element->arg1 = 0; |
416 | 0 | element->arg2 = 0; |
417 | |
|
418 | 0 | element->element_flag = 0; |
419 | 0 | element->preserve_pps = FALSE; |
420 | |
|
421 | 0 | element->transform.xx = 1 << 16; |
422 | 0 | element->transform.xy = 0; |
423 | 0 | element->transform.yx = 0; |
424 | 0 | element->transform.yy = 1 << 16; |
425 | |
|
426 | 0 | element->transform.ox = 0; |
427 | 0 | element->transform.oy = 0; |
428 | |
|
429 | 0 | element->leftBearing = 0; |
430 | 0 | element->advanceWidth = 0; |
431 | 0 | } |
432 | | |
433 | | static void cur_to_org( Int n, PGlyph_Zone zone ) |
434 | 0 | { |
435 | 0 | Int k; |
436 | |
|
437 | 0 | for ( k = 0; k < n; k++ ) |
438 | 0 | zone->org_x[k] = zone->cur_x[k]; |
439 | |
|
440 | 0 | for ( k = 0; k < n; k++ ) |
441 | 0 | zone->org_y[k] = zone->cur_y[k]; |
442 | 0 | } |
443 | | |
444 | | static void org_to_cur( Int n, PGlyph_Zone zone ) |
445 | 0 | { |
446 | 0 | Int k; |
447 | |
|
448 | 0 | for ( k = 0; k < n; k++ ) |
449 | 0 | zone->cur_x[k] = zone->org_x[k]; |
450 | |
|
451 | 0 | for ( k = 0; k < n; k++ ) |
452 | 0 | zone->cur_y[k] = zone->org_y[k]; |
453 | 0 | } |
454 | | |
455 | | /*-------------------------------------------------------------------*/ |
456 | | |
457 | | void ttfOutliner__init(ttfOutliner *self, ttfFont *f, ttfReader *r, ttfExport *exp, |
458 | | bool bOutline, bool bFirst, bool bVertical) |
459 | 0 | { |
460 | 0 | self->r = r; |
461 | 0 | self->bOutline = bOutline; |
462 | 0 | self->bFirst = bFirst; |
463 | 0 | self->pFont = f; |
464 | 0 | self->bVertical = bVertical; |
465 | 0 | self->exp = exp; |
466 | 0 | } |
467 | | |
468 | | static void MoveGlyphOutline(TGlyph_Zone *pts, int nOffset, ttfGlyphOutline *out, FixMatrix *m) |
469 | 0 | { F26Dot6* x = pts->org_x + nOffset; |
470 | 0 | F26Dot6* y = pts->org_y + nOffset; |
471 | 0 | short count = out->pointCount; |
472 | 0 | F26Dot6Point p; |
473 | |
|
474 | 0 | if (m->a == 65536 && m->b == 0 && |
475 | 0 | m->c == 0 && m->d == 65536 && |
476 | 0 | m->tx == 0 && m->ty == 0) |
477 | 0 | return; |
478 | 0 | for (; count != 0; --count) { |
479 | 0 | TransformF26Dot6PointFix(&p, *x, *y, m); |
480 | 0 | *x++ = p.x; |
481 | 0 | *y++ = p.y; |
482 | 0 | } |
483 | 0 | } |
484 | | |
485 | | static FontError ttfOutliner__BuildGlyphOutlineAux(ttfOutliner *self, int glyphIndex, |
486 | | FixMatrix *m_orig, ttfGlyphOutline* gOutline) |
487 | 0 | { ttfFont *pFont = self->pFont; |
488 | 0 | ttfReader *r = self->r; |
489 | 0 | ttfInterpreter *tti = pFont->tti; |
490 | 0 | short sideBearing; |
491 | 0 | FontError error = fNoError; |
492 | 0 | short arg1, arg2; |
493 | 0 | short count; |
494 | 0 | unsigned int i; |
495 | 0 | unsigned short nAdvance; |
496 | 0 | unsigned int nPosBeg; |
497 | 0 | TExecution_Context *exec = pFont->exec; |
498 | 0 | TGlyph_Zone *pts = &exec->pts; |
499 | 0 | TSubglyph_Record subglyph; |
500 | 0 | ttfSubGlyphUsage *usage = tti->usage + tti->usage_top; |
501 | 0 | const byte *glyph = NULL; |
502 | 0 | int glyph_size; |
503 | 0 | bool execute_bytecode = true; |
504 | 0 | int nPoints = 0; |
505 | |
|
506 | 0 | retry: |
507 | 0 | if (r->get_metrics(r, glyphIndex, self->bVertical, &sideBearing, &nAdvance) < 0) { |
508 | | /* fixme: the error code is missing due to interface restrictions. */ |
509 | 0 | goto errex; |
510 | 0 | } |
511 | 0 | gOutline->sideBearing = shortToF26Dot6(sideBearing); |
512 | 0 | gOutline->advance.x = shortToF26Dot6(nAdvance); |
513 | 0 | gOutline->advance.y = 0; |
514 | 0 | self->bFirst = FALSE; |
515 | |
|
516 | 0 | if (!self->bOutline) |
517 | 0 | return fNoError; |
518 | 0 | if (!r->LoadGlyph(r, glyphIndex, &glyph, &glyph_size)) |
519 | 0 | return fGlyphNotFound; |
520 | 0 | if (r->Eof(r)) { |
521 | 0 | r->ReleaseGlyph(r, glyphIndex); |
522 | 0 | gOutline->xMinB = gOutline->yMinB = 0; |
523 | 0 | gOutline->xMaxB = gOutline->yMaxB = 0; |
524 | 0 | return fNoError; |
525 | 0 | } |
526 | 0 | if (r->Error(r)) |
527 | 0 | goto errex; |
528 | 0 | nPosBeg = r->Tell(r); |
529 | |
|
530 | 0 | gOutline->contourCount = ttfReader__Short(r); |
531 | 0 | subglyph.bbox.xMin = ttfReader__Short(r); |
532 | 0 | subglyph.bbox.yMin = ttfReader__Short(r); |
533 | 0 | subglyph.bbox.xMax = ttfReader__Short(r); |
534 | 0 | subglyph.bbox.yMax = ttfReader__Short(r); |
535 | |
|
536 | 0 | if (exec->metrics.x_scale1 == 0 || exec->metrics.x_scale2 == 0 |
537 | 0 | || exec->metrics.y_scale1 == 0 || exec->metrics.y_scale2 == 0) { |
538 | 0 | goto errex; |
539 | 0 | } |
540 | 0 | gOutline->xMinB = Scale_X(&exec->metrics, subglyph.bbox.xMin); |
541 | 0 | gOutline->yMinB = Scale_Y(&exec->metrics, subglyph.bbox.yMin); |
542 | 0 | gOutline->xMaxB = Scale_X(&exec->metrics, subglyph.bbox.xMax); |
543 | 0 | gOutline->yMaxB = Scale_Y(&exec->metrics, subglyph.bbox.yMax); |
544 | | |
545 | | /* FreeType stuff beg */ |
546 | 0 | Init_Glyph_Component(&subglyph, NULL, pFont->exec); |
547 | 0 | subglyph.leftBearing = sideBearing; |
548 | 0 | subglyph.advanceWidth = nAdvance; |
549 | 0 | subglyph.pp1.x = subglyph.bbox.xMin - sideBearing; |
550 | 0 | subglyph.pp1.y = 0; |
551 | 0 | subglyph.pp2.x = subglyph.pp1.x + nAdvance; |
552 | 0 | subglyph.pp2.y = 0; |
553 | | /* FreeType stuff end */ |
554 | |
|
555 | 0 | if (gOutline->contourCount == 0) |
556 | 0 | gOutline->pointCount = 0; |
557 | 0 | else if (gOutline->contourCount == -1) { |
558 | 0 | unsigned short flags, index, bHaveInstructions = 0; |
559 | 0 | unsigned int nUsage = 0; |
560 | 0 | unsigned int nPos; |
561 | 0 | unsigned int n_ins; |
562 | |
|
563 | 0 | gOutline->bCompound = TRUE; |
564 | 0 | if (tti->usage_top + pFont->nMaxComponents > tti->usage_size) |
565 | 0 | return fBadFontData; |
566 | 0 | gOutline->contourCount = gOutline->pointCount = 0; |
567 | 0 | do { |
568 | 0 | FixMatrix m; |
569 | 0 | ttfSubGlyphUsage *e; |
570 | |
|
571 | 0 | if (nUsage >= pFont->nMaxComponents) { |
572 | 0 | error = fMemoryError; goto ex; |
573 | 0 | } |
574 | 0 | flags = ttfReader__UShort(r); |
575 | 0 | index = ttfReader__UShort(r); |
576 | 0 | bHaveInstructions |= (flags & WE_HAVE_INSTRUCTIONS); |
577 | 0 | if (flags & ARG_1_AND_2_ARE_WORDS) { |
578 | 0 | arg1 = ttfReader__Short(r); |
579 | 0 | arg2 = ttfReader__Short(r); |
580 | 0 | } else { |
581 | 0 | if (flags & ARGS_ARE_XY_VALUES) { |
582 | | /* offsets are signed */ |
583 | 0 | arg1 = ttfReader__SignedByte(r); |
584 | 0 | arg2 = ttfReader__SignedByte(r); |
585 | 0 | } else { /* anchor points are unsigned */ |
586 | 0 | arg1 = ttfReader__Byte(r); |
587 | 0 | arg2 = ttfReader__Byte(r); |
588 | 0 | } |
589 | 0 | } |
590 | 0 | m.b = m.c = m.tx = m.ty = 0; |
591 | 0 | if (flags & WE_HAVE_A_SCALE) |
592 | 0 | m.a = m.d = (TT_Fixed)ttfReader__Short(r) << 2; |
593 | 0 | else if (flags & WE_HAVE_AN_X_AND_Y_SCALE) { |
594 | 0 | m.a = (TT_Fixed)ttfReader__Short(r) << 2; |
595 | 0 | m.d = (TT_Fixed)ttfReader__Short(r) << 2; |
596 | 0 | } else if (flags & WE_HAVE_A_TWO_BY_TWO) { |
597 | 0 | m.a = (TT_Fixed)ttfReader__Short(r)<<2; |
598 | 0 | m.b = (TT_Fixed)ttfReader__Short(r)<<2; |
599 | 0 | m.c = (TT_Fixed)ttfReader__Short(r)<<2; |
600 | 0 | m.d = (TT_Fixed)ttfReader__Short(r)<<2; |
601 | 0 | } else |
602 | 0 | m.a = m.d = 65536; |
603 | 0 | e = &usage[nUsage]; |
604 | 0 | e->m = m; |
605 | 0 | e->index = index; |
606 | 0 | e->arg1 = arg1; |
607 | 0 | e->arg2 = arg2; |
608 | 0 | e->flags = flags; |
609 | 0 | nUsage++; |
610 | 0 | } while (flags & MORE_COMPONENTS); |
611 | 0 | if (r->Error(r)) |
612 | 0 | goto errex; |
613 | 0 | nPos = r->Tell(r); |
614 | 0 | n_ins = ((!r->Eof(r) && (bHaveInstructions)) ? ttfReader__UShort(r) : 0); |
615 | 0 | nPos = r->Tell(r); |
616 | 0 | r->ReleaseGlyph(r, glyphIndex); |
617 | 0 | glyph = NULL; |
618 | 0 | for (i = 0; i < nUsage; i++) { |
619 | 0 | ttfGlyphOutline out; |
620 | 0 | ttfSubGlyphUsage *e = &usage[i]; |
621 | 0 | int j; |
622 | 0 | TT_Error code; |
623 | 0 | int nPointsStored = gOutline->pointCount, nContoursStored = gOutline->contourCount; |
624 | |
|
625 | 0 | out.contourCount = 0; |
626 | 0 | out.pointCount = 0; |
627 | 0 | out.bCompound = FALSE; |
628 | 0 | pts->org_x += nPointsStored; |
629 | 0 | pts->org_y += nPointsStored; |
630 | 0 | pts->cur_x += nPointsStored; |
631 | 0 | pts->cur_y += nPointsStored; |
632 | 0 | pts->touch += nPointsStored; |
633 | 0 | pts->contours += nContoursStored; |
634 | 0 | tti->usage_top += nUsage; |
635 | 0 | code = ttfOutliner__BuildGlyphOutlineAux(self, e->index, m_orig, &out); |
636 | 0 | pts->org_x -= nPointsStored; |
637 | 0 | pts->org_y -= nPointsStored; |
638 | 0 | pts->cur_x -= nPointsStored; |
639 | 0 | pts->cur_y -= nPointsStored; |
640 | 0 | pts->touch -= nPointsStored; |
641 | 0 | tti->usage_top -= nUsage; |
642 | 0 | pts->contours -= nContoursStored; |
643 | 0 | if (code == fPatented) |
644 | 0 | error = code; |
645 | 0 | else if (code != fNoError) { |
646 | 0 | error = code; |
647 | 0 | goto ex; |
648 | 0 | } |
649 | 0 | if (flags & ARGS_ARE_XY_VALUES) { |
650 | 0 | e->m.tx = Scale_X( &exec->metrics, e->arg1 ) << 10; |
651 | 0 | e->m.ty = Scale_Y( &exec->metrics, e->arg2 ) << 10; |
652 | 0 | } else { |
653 | 0 | if (e->arg1 < 0 || e->arg1 > pts->n_points |
654 | 0 | || ((int)gOutline->pointCount + e->arg2) < 0 || (gOutline->pointCount + e->arg2) > pts->n_points) { |
655 | 0 | error = fBadFontData; |
656 | 0 | goto ex; |
657 | 0 | } |
658 | 0 | else { |
659 | 0 | e->m.tx = (pts->org_x[e->arg1] - pts->org_x[gOutline->pointCount + e->arg2]) << 10; |
660 | 0 | e->m.ty = (pts->org_y[e->arg1] - pts->org_y[gOutline->pointCount + e->arg2]) << 10; |
661 | 0 | } |
662 | 0 | } |
663 | 0 | MoveGlyphOutline(pts, nPointsStored, &out, &e->m); |
664 | 0 | for (j = nContoursStored; j < out.contourCount + nContoursStored; j++) |
665 | 0 | pts->contours[j] += nPointsStored; |
666 | 0 | gOutline->contourCount += out.contourCount; |
667 | 0 | gOutline->pointCount += out.pointCount; |
668 | 0 | if(e->flags & USE_MY_METRICS) { |
669 | 0 | gOutline->advance.x = out.advance.x; |
670 | 0 | gOutline->sideBearing = out.sideBearing; |
671 | 0 | } |
672 | 0 | } |
673 | 0 | if (execute_bytecode && !skip_instructions && n_ins && |
674 | 0 | !(pFont->inst->GS.instruct_control & 1)) { |
675 | 0 | TT_Error code; |
676 | |
|
677 | 0 | r->LoadGlyph(r, glyphIndex, &glyph, &glyph_size); |
678 | 0 | if (r->Error(r)) |
679 | 0 | goto errex; |
680 | 0 | if (nPos + n_ins > glyph_size) |
681 | 0 | goto errex; |
682 | 0 | code = Set_CodeRange(exec, TT_CodeRange_Glyph, (byte *)glyph + nPos, n_ins); |
683 | 0 | if (!code) { |
684 | 0 | int k; |
685 | 0 | F26Dot6 x; |
686 | |
|
687 | 0 | nPoints = gOutline->pointCount + 2; |
688 | 0 | exec->pts = subglyph.zone; |
689 | 0 | pts->n_points = nPoints; |
690 | 0 | pts->n_contours = gOutline->contourCount; |
691 | | /* add phantom points : */ |
692 | 0 | pts->org_x[nPoints - 2] = Scale_X(&exec->metrics, subglyph.pp1.x); |
693 | 0 | pts->org_y[nPoints - 2] = Scale_Y(&exec->metrics, subglyph.pp1.y); |
694 | 0 | pts->org_x[nPoints - 1] = Scale_X(&exec->metrics, subglyph.pp2.x); |
695 | 0 | pts->org_y[nPoints - 1] = Scale_Y(&exec->metrics, subglyph.pp2.y); |
696 | 0 | pts->touch[nPoints - 1] = 0; |
697 | 0 | pts->touch[nPoints - 2] = 0; |
698 | | /* if hinting, round the phantom points (not sure) : */ |
699 | 0 | x = pts->org_x[nPoints - 2]; |
700 | 0 | x = ((x + 32) & -64) - x; |
701 | 0 | if (x) |
702 | 0 | for (k = 0; k < nPoints; k++) |
703 | 0 | pts->org_x[k] += x; |
704 | 0 | pts->cur_x[nPoints - 1] = (pts->cur_x[nPoints - 1] + 32) & -64; |
705 | 0 | for (k = 0; k < nPoints; k++) |
706 | 0 | pts->touch[k] = pts->touch[k] & TT_Flag_On_Curve; |
707 | 0 | org_to_cur(nPoints, pts); |
708 | 0 | exec->is_composite = TRUE; |
709 | 0 | if (pFont->patented) |
710 | 0 | code = TT_Err_Invalid_Engine; |
711 | 0 | else |
712 | 0 | code = Context_Run(exec, FALSE); |
713 | 0 | if (!code) |
714 | 0 | cur_to_org(nPoints, pts); |
715 | 0 | else if (code == TT_Err_Invalid_Engine) |
716 | 0 | error = fPatented; |
717 | 0 | else { |
718 | | /* We have a range of errors that can be caused by |
719 | | * bad bytecode |
720 | | */ |
721 | 0 | if ((int)code >= TT_Err_Invalid_Opcode |
722 | 0 | && (int)code <= TT_Err_Invalid_Displacement) { |
723 | 0 | error = fBadInstruction; |
724 | 0 | } |
725 | 0 | else { |
726 | 0 | error = fBadFontData; |
727 | 0 | } |
728 | 0 | } |
729 | 0 | } |
730 | 0 | Unset_CodeRange(exec); |
731 | 0 | Clear_CodeRange(exec, TT_CodeRange_Glyph); |
732 | 0 | } |
733 | 0 | } else if (gOutline->contourCount > 0) { |
734 | 0 | int i; |
735 | 0 | bool bInsOK; |
736 | 0 | byte *onCurve, *stop, flag; |
737 | 0 | short *endPoints; |
738 | 0 | unsigned int nPos; |
739 | 0 | unsigned int n_ins; |
740 | |
|
741 | 0 | if (self->nContoursTotal + gOutline->contourCount > exec->n_contours) { |
742 | 0 | error = fBadFontData; goto ex; |
743 | 0 | } |
744 | 0 | endPoints = pts->contours; |
745 | 0 | for (i = 0; i < gOutline->contourCount; i++) |
746 | 0 | endPoints[i] = ttfReader__Short(r); |
747 | 0 | for (i = 1; i < gOutline->contourCount; i++) |
748 | 0 | if (endPoints[i - 1] < 0 || endPoints[i - 1] >= endPoints[i]) { |
749 | 0 | error = fBadFontData; goto ex; |
750 | 0 | } |
751 | 0 | nPoints = gOutline->pointCount = endPoints[gOutline->contourCount - 1] + 1; |
752 | 0 | if (nPoints < 0 || self->nPointsTotal + nPoints + 2 > exec->n_points) { |
753 | 0 | error = fBadFontData; goto ex; |
754 | 0 | } |
755 | 0 | n_ins = ttfReader__Short(r); |
756 | 0 | nPos = r->Tell(r); |
757 | 0 | r->Seek(r, nPos + n_ins); |
758 | 0 | if (r->Error(r)) |
759 | 0 | goto errex; |
760 | 0 | bInsOK = !Set_CodeRange(exec, TT_CodeRange_Glyph, (byte *)glyph + nPos, n_ins); |
761 | 0 | onCurve = pts->touch; |
762 | 0 | stop = onCurve + gOutline->pointCount; |
763 | |
|
764 | 0 | while (onCurve < stop) { |
765 | 0 | *onCurve++ = flag = ttfReader__Byte(r); |
766 | 0 | if (flag & REPEAT_FLAGS) { |
767 | 0 | count = ttfReader__Byte(r); |
768 | 0 | for (--count; count >= 0 && onCurve < stop; --count) |
769 | 0 | *onCurve++ = flag; |
770 | 0 | } |
771 | 0 | } |
772 | | /* Lets do X */ |
773 | 0 | { short coord = (self->bVertical ? 0 : sideBearing - subglyph.bbox.xMin); |
774 | 0 | F26Dot6* x = pts->org_x; |
775 | 0 | onCurve = pts->touch; |
776 | 0 | while (onCurve < stop) { |
777 | 0 | if ((flag = *onCurve++) & XSHORT) { |
778 | 0 | if (flag & SHORT_X_IS_POS) |
779 | 0 | coord += ttfReader__Byte(r); |
780 | 0 | else |
781 | 0 | coord -= ttfReader__Byte(r); |
782 | 0 | } else if (!(flag & NEXT_X_IS_ZERO)) |
783 | 0 | coord += ttfReader__Short(r); |
784 | 0 | *x++ = Scale_X(&exec->metrics, coord); |
785 | 0 | } |
786 | 0 | } |
787 | | /* Lets do Y */ |
788 | 0 | { short coord = 0; |
789 | 0 | F26Dot6* y = pts->org_y; |
790 | 0 | onCurve = pts->touch; |
791 | 0 | while (onCurve < stop) { |
792 | 0 | if((flag = *onCurve) & YSHORT) |
793 | 0 | if ( flag & SHORT_Y_IS_POS ) |
794 | 0 | coord += ttfReader__Byte(r); |
795 | 0 | else |
796 | 0 | coord -= ttfReader__Byte(r); |
797 | 0 | else if(!(flag & NEXT_Y_IS_ZERO)) |
798 | 0 | coord += ttfReader__Short(r); |
799 | 0 | *y++ = Scale_Y( &exec->metrics, coord ); |
800 | | |
801 | | /* Filter off the extra bits */ |
802 | 0 | *onCurve++ = flag & ONCURVE; |
803 | 0 | } |
804 | 0 | } |
805 | 0 | MoveGlyphOutline(pts, 0, gOutline, m_orig); |
806 | 0 | self->nContoursTotal += gOutline->contourCount; |
807 | 0 | self->nPointsTotal += nPoints; |
808 | 0 | if (execute_bytecode && !skip_instructions && |
809 | 0 | !r->Error(r) && n_ins && bInsOK && !(pFont->inst->GS.instruct_control & 1)) { |
810 | 0 | TGlyph_Zone *pts = &exec->pts; |
811 | 0 | int k; |
812 | 0 | F26Dot6 x; |
813 | 0 | TT_Error code; |
814 | |
|
815 | 0 | exec->is_composite = FALSE; |
816 | | /* add phantom points : */ |
817 | 0 | pts->org_x[nPoints ] = Scale_X(&exec->metrics, subglyph.pp1.x); |
818 | 0 | pts->org_y[nPoints ] = Scale_Y(&exec->metrics, subglyph.pp1.y); |
819 | 0 | pts->org_x[nPoints + 1] = Scale_X(&exec->metrics, subglyph.pp2.x); |
820 | 0 | pts->org_y[nPoints + 1] = Scale_Y(&exec->metrics, subglyph.pp2.y); |
821 | 0 | pts->touch[nPoints ] = 0; |
822 | 0 | pts->touch[nPoints + 1] = 0; |
823 | 0 | pts->n_points = nPoints + 2; |
824 | 0 | pts->n_contours = gOutline->contourCount; |
825 | | /* if hinting, round the phantom points (not sure) : */ |
826 | 0 | x = pts->org_x[nPoints]; |
827 | 0 | x = ((x + 32) & -64) - x; |
828 | 0 | if (x) |
829 | 0 | for (k = 0; k < nPoints + 2; k++) |
830 | 0 | pts->org_x[k] += x; |
831 | 0 | org_to_cur(nPoints + 2, pts); |
832 | 0 | exec->is_composite = FALSE; |
833 | 0 | for (k = 0; k < nPoints + 2; k++) |
834 | 0 | pts->touch[k] &= TT_Flag_On_Curve; |
835 | 0 | if (pFont->patented) |
836 | 0 | code = TT_Err_Invalid_Engine; |
837 | 0 | else |
838 | 0 | code = Context_Run(exec, FALSE ); |
839 | 0 | if (!code) |
840 | 0 | cur_to_org(nPoints + 2, pts); |
841 | 0 | else if (code == TT_Err_Invalid_Engine) |
842 | 0 | error = fPatented; |
843 | 0 | else |
844 | 0 | error = fBadInstruction; |
845 | 0 | gOutline->sideBearing = subglyph.bbox.xMin - subglyph.pp1.x; |
846 | 0 | gOutline->advance.x = subglyph.pp2.x - subglyph.pp1.x; |
847 | 0 | } |
848 | 0 | Unset_CodeRange(exec); |
849 | 0 | Clear_CodeRange(exec, TT_CodeRange_Glyph); |
850 | 0 | } else |
851 | 0 | error = fBadFontData; |
852 | 0 | goto ex; |
853 | 0 | errex:; |
854 | 0 | error = fBadFontData; |
855 | 0 | ex:; |
856 | 0 | r->ReleaseGlyph(r, glyphIndex); |
857 | |
|
858 | 0 | if (error == fBadInstruction && execute_bytecode) { |
859 | | /* reset a load of stuff so we can try again without hinting */ |
860 | 0 | exec = pFont->exec; |
861 | 0 | pts = &exec->pts; |
862 | 0 | usage = tti->usage + tti->usage_top; |
863 | 0 | glyph = NULL; |
864 | 0 | self->nPointsTotal -= (nPoints + 2); |
865 | 0 | nPoints = 0; |
866 | 0 | self->nContoursTotal -= gOutline->contourCount; |
867 | 0 | error = fNoError; |
868 | 0 | execute_bytecode = false; |
869 | 0 | r->Seek(r, nPosBeg); |
870 | 0 | goto retry; |
871 | 0 | } |
872 | | |
873 | 0 | return error; |
874 | 0 | } |
875 | | |
876 | | static FontError ttfOutliner__BuildGlyphOutline(ttfOutliner *self, int glyphIndex, |
877 | | float orig_x, float orig_y, ttfGlyphOutline* gOutline) |
878 | 0 | { |
879 | 0 | FixMatrix m_orig = {1 << 16, 0, 0, 1 << 16, 0, 0}; |
880 | | |
881 | | /* Round towards zero like old character coordinate conversions do. */ |
882 | 0 | m_orig.tx = floatToF16Dot16(orig_x); |
883 | 0 | m_orig.ty = floatToF16Dot16(orig_y); |
884 | 0 | return ttfOutliner__BuildGlyphOutlineAux(self, glyphIndex, &m_orig, gOutline); |
885 | 0 | } |
886 | | |
887 | | #define AVECTOR_BUG 1 /* Work around a bug in AVector fonts. */ |
888 | | |
889 | | int ttfOutliner__DrawGlyphOutline(ttfOutliner *self) |
890 | 0 | { ttfGlyphOutline* out = &self->out; |
891 | 0 | FloatMatrix *m = &self->post_transform; |
892 | 0 | ttfFont *pFont = self->pFont; |
893 | 0 | ttfExport *exp = self->exp; |
894 | 0 | TExecution_Context *exec = pFont->exec; |
895 | 0 | TGlyph_Zone *epts = &exec->pts; |
896 | 0 | short* endP = epts->contours; |
897 | 0 | byte* onCurve = epts->touch; |
898 | 0 | F26Dot6* x = epts->org_x; |
899 | 0 | F26Dot6* y = epts->org_y; |
900 | 0 | F26Dot6 px, py; |
901 | 0 | short sp, ctr; |
902 | 0 | FloatPoint p0, p1, p2, p3; |
903 | 0 | # if AVECTOR_BUG |
904 | 0 | F26Dot6 expand_x; |
905 | 0 | F26Dot6 expand_y; |
906 | 0 | F26Dot6 xMin, xMax; |
907 | 0 | F26Dot6 yMin, yMax; |
908 | | |
909 | |
|
910 | 0 | if (exec->metrics.x_scale1 == 0 || exec->metrics.x_scale2 == 0 |
911 | 0 | || exec->metrics.y_scale1 == 0 || exec->metrics.y_scale2 == 0) { |
912 | 0 | return_error(gs_error_invalidfont); |
913 | 0 | } |
914 | | |
915 | 0 | expand_x = Scale_X(&exec->metrics, pFont->nUnitsPerEm * 2); |
916 | 0 | expand_y = Scale_Y(&exec->metrics, pFont->nUnitsPerEm * 2); |
917 | 0 | xMin = out->xMinB - expand_x; |
918 | 0 | xMax = out->xMaxB + expand_x; |
919 | 0 | yMin = out->yMinB - expand_y; |
920 | 0 | yMax = out->yMaxB + expand_y; |
921 | 0 | # endif |
922 | |
|
923 | 0 | TransformF26Dot6PointFloat(&p1, out->advance.x, out->advance.y, m); |
924 | 0 | p1.x -= self->post_transform.tx; |
925 | 0 | p1.y -= self->post_transform.ty; |
926 | 0 | exp->SetWidth(exp, &p1); |
927 | 0 | sp = -1; |
928 | 0 | for (ctr = out->contourCount; ctr != 0; --ctr) { |
929 | 0 | short pt, pts = *endP - sp; |
930 | 0 | short ep = pts - 1; |
931 | |
|
932 | 0 | if (pts < 3) { |
933 | 0 | x += pts; |
934 | 0 | y += pts; |
935 | 0 | onCurve += pts; |
936 | 0 | sp = *endP++; |
937 | 0 | continue; /* skip 1 and 2 point contours */ |
938 | 0 | } |
939 | | |
940 | 0 | if (exp->bPoints) { |
941 | 0 | for (pt = 0; pt <= ep; pt++) { |
942 | 0 | px = x[pt], py = y[pt]; |
943 | 0 | # if AVECTOR_BUG |
944 | 0 | if (x[pt] < xMin || xMax < x[pt] || y[pt] < yMin || yMax < y[pt]) { |
945 | 0 | short prevIndex = pt == 0 ? ep : pt - 1; |
946 | 0 | short nextIndex = pt == ep ? 0 : pt + 1; |
947 | 0 | if (nextIndex > ep) |
948 | 0 | nextIndex = 0; |
949 | 0 | px=AVE(x[prevIndex], x[nextIndex]); |
950 | 0 | py=AVE(y[prevIndex], y[nextIndex]); |
951 | 0 | } |
952 | 0 | # endif |
953 | 0 | TransformF26Dot6PointFloat(&p0, px, py, m); |
954 | 0 | exp->Point(exp, &p0, onCurve[pt], !pt); |
955 | 0 | } |
956 | 0 | } |
957 | |
|
958 | 0 | if (exp->bOutline) { |
959 | 0 | pt = 0; |
960 | 0 | if(onCurve[ep] & 1) { |
961 | 0 | px = x[ep]; |
962 | 0 | py = y[ep]; |
963 | 0 | } else if (onCurve[0] & 1) { |
964 | 0 | px = x[0]; |
965 | 0 | py = y[0]; |
966 | 0 | pt = 1; |
967 | 0 | } else { |
968 | 0 | px = AVE(x[0], x[ep]); |
969 | 0 | py = AVE(y[0], y[ep]); |
970 | 0 | } |
971 | 0 | self->ppx = px; self->ppy = py; |
972 | 0 | TransformF26Dot6PointFloat(&p0, px, py, m); |
973 | 0 | exp->MoveTo(exp, &p0); |
974 | |
|
975 | 0 | for (; pt <= ep; pt++) { |
976 | 0 | short prevIndex = pt == 0 ? ep : pt - 1; |
977 | 0 | short nextIndex = pt == ep ? 0 : pt + 1; |
978 | 0 | if (onCurve[pt] & 1) { |
979 | 0 | if (onCurve[prevIndex] & 1) { |
980 | 0 | px = x[pt]; |
981 | 0 | py = y[pt]; |
982 | 0 | if (self->ppx != px || self->ppy != py) { |
983 | 0 | TransformF26Dot6PointFloat(&p1, px, py, m); |
984 | 0 | exp->LineTo(exp, &p1); |
985 | 0 | self->ppx = px; self->ppy = py; |
986 | 0 | p0 = p1; |
987 | 0 | } |
988 | 0 | } |
989 | 0 | } else { |
990 | 0 | F26Dot6 prevX, prevY, nextX, nextY; |
991 | |
|
992 | 0 | px = x[pt]; |
993 | 0 | py = y[pt]; |
994 | 0 | # if AVECTOR_BUG |
995 | 0 | if(x[pt] < xMin || xMax < x[pt] || y[pt] < yMin || yMax < y[pt]) { |
996 | 0 | px=AVE(x[prevIndex], x[nextIndex]); |
997 | 0 | py=AVE(y[prevIndex], y[nextIndex]); |
998 | 0 | } |
999 | 0 | # endif |
1000 | 0 | if (onCurve[prevIndex] & 1) { |
1001 | 0 | prevX = x[prevIndex]; |
1002 | 0 | prevY = y[prevIndex]; |
1003 | 0 | } else { |
1004 | 0 | prevX = AVE(x[prevIndex], px); |
1005 | 0 | prevY = AVE(y[prevIndex], py); |
1006 | 0 | } |
1007 | 0 | if (onCurve[nextIndex] & 1) { |
1008 | 0 | nextX = x[nextIndex]; |
1009 | 0 | nextY = y[nextIndex]; |
1010 | 0 | } else { |
1011 | 0 | nextX = AVE(px, x[nextIndex]); |
1012 | 0 | nextY = AVE(py, y[nextIndex]); |
1013 | 0 | } |
1014 | 0 | if (self->ppx != nextX || self->ppy != nextY) { |
1015 | 0 | double dx1, dy1, dx2, dy2, dx3, dy3; |
1016 | 0 | const double prec = 1e-6; |
1017 | |
|
1018 | 0 | TransformF26Dot6PointFloat(&p1, (prevX + (px << 1)) / 3, (prevY + (py << 1)) / 3, m); |
1019 | 0 | TransformF26Dot6PointFloat(&p2, (nextX + (px << 1)) / 3, (nextY + (py << 1)) / 3, m); |
1020 | 0 | TransformF26Dot6PointFloat(&p3, nextX, nextY, m); |
1021 | 0 | dx1 = p1.x - p0.x, dy1 = p1.y - p0.y; |
1022 | 0 | dx2 = p2.x - p0.x, dy2 = p2.y - p0.y; |
1023 | 0 | dx3 = p3.x - p0.x, dy3 = p3.y - p0.y; |
1024 | 0 | if (fabs(dx1 * dy3 - dy1 * dx3) > prec * fabs(dx1 * dx3 - dy1 * dy3) || |
1025 | 0 | fabs(dx2 * dy3 - dy2 * dx3) > prec * fabs(dx2 * dx3 - dy2 * dy3)) |
1026 | 0 | exp->CurveTo(exp, &p1, &p2, &p3); |
1027 | 0 | else |
1028 | 0 | exp->LineTo(exp, &p3); |
1029 | 0 | self->ppx = nextX; self->ppy = nextY; |
1030 | 0 | p0 = p3; |
1031 | 0 | } |
1032 | 0 | } |
1033 | 0 | } |
1034 | 0 | exp->Close(exp); |
1035 | 0 | } |
1036 | 0 | x += pts; |
1037 | 0 | y += pts; |
1038 | 0 | onCurve += pts; |
1039 | 0 | sp = *endP++; |
1040 | 0 | } |
1041 | 0 | return 0; |
1042 | 0 | } |
1043 | | |
1044 | | FontError ttfOutliner__Outline(ttfOutliner *self, int glyphIndex, |
1045 | | float orig_x, float orig_y, FloatMatrix *m1) |
1046 | 0 | { ttfFont *pFont = self->pFont; |
1047 | 0 | FontError error; |
1048 | |
|
1049 | 0 | self->post_transform = *m1; |
1050 | 0 | self->out.contourCount = 0; |
1051 | 0 | self->out.pointCount = 0; |
1052 | 0 | self->out.bCompound = FALSE; |
1053 | 0 | self->nPointsTotal = 0; |
1054 | 0 | self->nContoursTotal = 0; |
1055 | 0 | self->out.advance.x = self->out.advance.y = 0; |
1056 | 0 | ttfFont__StartGlyph(pFont); |
1057 | 0 | error = ttfOutliner__BuildGlyphOutline(self, glyphIndex, orig_x, orig_y, &self->out); |
1058 | 0 | ttfFont__StopGlyph(pFont); |
1059 | 0 | if (pFont->nUnitsPerEm <= 0) |
1060 | 0 | pFont->nUnitsPerEm = 1024; |
1061 | 0 | if (pFont->design_grid) { |
1062 | 0 | self->post_transform.a /= pFont->nUnitsPerEm; |
1063 | 0 | self->post_transform.b /= pFont->nUnitsPerEm; |
1064 | 0 | self->post_transform.c /= pFont->nUnitsPerEm; |
1065 | 0 | self->post_transform.d /= pFont->nUnitsPerEm; |
1066 | 0 | } |
1067 | 0 | return error; |
1068 | 0 | } |