Coverage Report

Created: 2022-10-31 07:00

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