Coverage Report

Created: 2025-06-10 07:15

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