Coverage Report

Created: 2025-06-10 06:58

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