Coverage Report

Created: 2026-08-08 08:00

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ghostpdl/base/fapi_ft.c
Line
Count
Source
1
/* Copyright (C) 2001-2026 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
/*
18
   GhostScript Font API plug-in that allows fonts to be rendered by FreeType.
19
   Started by Graham Asher, 6th June 2002.
20
 */
21
22
/* GhostScript headers. */
23
#include "stdio_.h"
24
#include "malloc_.h"
25
#include "write_t1.h"
26
#include "write_t2.h"
27
#include "math_.h"
28
#include "gserrors.h"
29
#include "gsmemory.h"
30
#include "gsmalloc.h"
31
#include "gxfixed.h"
32
#include "gdebug.h"
33
#include "gxbitmap.h"
34
#include "gsmchunk.h"
35
#include "gxfont.h"
36
#include "gxfont1.h"
37
38
#include "stream.h"
39
#include "gxiodev.h"            /* must come after stream.h */
40
41
#include "gsfname.h"
42
43
#include "gxfapi.h"
44
45
46
/* FreeType headers */
47
#include <ft2build.h>
48
#include FT_FREETYPE_H
49
#include FT_INCREMENTAL_H
50
#include FT_GLYPH_H
51
#include FT_SYSTEM_H
52
#include FT_MODULE_H
53
#include FT_TRIGONOMETRY_H
54
#include FT_BBOX_H
55
#include FT_OUTLINE_H
56
#include FT_IMAGE_H
57
#include FT_BITMAP_H
58
#include FT_TRUETYPE_DRIVER_H
59
#include FT_TRUETYPE_TABLES_H
60
#include FT_MULTIPLE_MASTERS_H
61
#include FT_TYPE1_TABLES_H
62
63
/* Note: structure definitions here start with FF_, which stands for 'FAPI FreeType". */
64
65
#define ft_emprintf(m,s) { outflush(m); emprintf(m, s); outflush(m); }
66
#define ft_emprintf1(m,s,d) { outflush(m); emprintf1(m, s, d); outflush(m); }
67
68
typedef struct ff_server_s
69
{
70
    gs_fapi_server fapi_server;
71
    FT_Library freetype_library;
72
    FT_OutlineGlyph outline_glyph;
73
    FT_BitmapGlyph bitmap_glyph;
74
    gs_memory_t *mem;
75
    FT_Memory ftmemory;
76
    struct FT_MemoryRec_ ftmemory_rec;
77
} ff_server;
78
79
80
81
typedef struct ff_face_s
82
{
83
    FT_Face ft_face;
84
85
    /* Currently in force scaling/transform for this face */
86
    FT_Matrix ft_transform;
87
    FT_F26Dot6 width, height;
88
    FT_UInt horz_res;
89
    FT_UInt vert_res;
90
91
    /* If non-null, the incremental interface object passed to FreeType. */
92
    FT_Incremental_InterfaceRec *ft_inc_int;
93
    /* If non-null, we're using a custom stream object for Freetype to read the font file */
94
    FT_Stream ftstrm;
95
    /* Non-null if font data is owned by this object. */
96
    unsigned char *font_data;
97
    int font_data_len;
98
    bool data_owned;
99
    ff_server *server;
100
} ff_face;
101
102
/* Here we define the struct FT_Incremental that is used as an opaque type
103
 * inside FreeType. This structure has to have the tag FT_IncrementalRec_
104
 * to be compatible with the functions defined in FT_Incremental_FuncsRec.
105
 */
106
typedef struct FT_IncrementalRec_
107
{
108
    gs_fapi_font *fapi_font;    /* The font. */
109
110
    /* If it is already in use glyph data is allocated on the heap. */
111
    unsigned char *glyph_data;  /* A one-shot buffer for glyph data. */
112
    size_t glyph_data_length;   /* Length in bytes of glyph_data. */
113
    bool glyph_data_in_use;     /* True if glyph_data is already in use. */
114
115
    FT_Incremental_MetricsRec glyph_metrics;    /* Incremental glyph metrics supplied by Ghostscript. */
116
    unsigned long glyph_metrics_index;  /* contains data for this glyph index unless it is 0xFFFFFFFF. */
117
    gs_fapi_metrics_type metrics_type;  /* determines whether metrics are replaced, added, etc. */
118
} FT_IncrementalRec;
119
120
121
static void
122
delete_inc_int(gs_fapi_server * a_server,
123
               FT_Incremental_InterfaceRec * a_inc_int);
124
125
static void
126
delete_inc_int_info(gs_fapi_server * a_server,
127
                    FT_IncrementalRec * a_inc_int_info);
128
129
static void *
130
FF_alloc(FT_Memory memory, long size)
131
211M
{
132
211M
    gs_memory_t *mem = (gs_memory_t *) memory->user;
133
134
211M
    return (gs_malloc(mem, size, 1, "FF_alloc"));
135
211M
}
136
137
static void *
138
    FF_realloc(FT_Memory memory, long cur_size, long new_size, void *block)
139
9.75M
{
140
9.75M
    gs_memory_t *mem = (gs_memory_t *) memory->user;
141
9.75M
    void *tmp;
142
143
9.75M
    if (cur_size == new_size) {
144
2.74M
        return (block);
145
2.74M
    }
146
147
7.00M
    tmp = gs_malloc(mem, new_size, 1, "FF_realloc");
148
7.00M
    if (tmp && block) {
149
7.00M
        memcpy(tmp, block, min(cur_size, new_size));
150
151
7.00M
        gs_free(mem, block, 0, 0, "FF_realloc");
152
7.00M
    }
153
154
7.00M
    return (tmp);
155
9.75M
}
156
157
static void
158
    FF_free(FT_Memory memory, void *block)
159
218M
{
160
218M
    gs_memory_t *mem = (gs_memory_t *) memory->user;
161
162
218M
    gs_free(mem, block, 0, 0, "FF_free");
163
218M
}
164
165
/* The following three functions are used in providing a custom stream
166
 * object to Freetype, so file access happens through Ghostscript's
167
 * file i/o. Most importantly, this gives Freetype direct access to
168
 * files in the romfs
169
 */
170
static FT_ULong
171
FF_stream_read(FT_Stream str, unsigned long offset, unsigned char *buffer,
172
               unsigned long count)
173
144M
{
174
144M
    stream *ps = (stream *) str->descriptor.pointer;
175
144M
    unsigned int rlen = 0;
176
144M
    int status = 0;
177
178
144M
    if (sseek(ps, (gs_offset_t)offset) < 0)
179
0
        return_error(-1);
180
181
144M
    if (count) {
182
88.1M
        status = sgets(ps, buffer, count, &rlen);
183
184
88.1M
        if (status < 0 && status != EOFC)
185
0
            return_error (-1);
186
88.1M
    }
187
144M
    return (rlen);
188
144M
}
189
190
static void
191
FF_stream_close(FT_Stream str)
192
1.96M
{
193
1.96M
    stream *ps = (stream *) str->descriptor.pointer;
194
1.96M
    gs_memory_t *mem = ps->memory;
195
196
1.96M
    (void)sclose(ps);
197
1.96M
    gs_free_object(mem, ps, "FF_stream_close");
198
1.96M
}
199
200
extern const uint file_default_buffer_size;
201
202
static int
203
FF_open_read_stream(gs_memory_t * mem, char *fname, FT_Stream * fts)
204
1.96M
{
205
1.96M
    int code = 0;
206
1.96M
    gs_parsed_file_name_t pfn;
207
1.96M
    stream *ps = (stream *)NULL;
208
1.96M
    gs_offset_t length;
209
1.96M
    FT_Stream ftstrm = NULL;
210
211
1.96M
    code = gs_parse_file_name(&pfn, (const char *)fname, strlen(fname), mem);
212
1.96M
    if (code < 0) {
213
0
        goto error_out;
214
0
    }
215
216
1.96M
    if (!pfn.fname) {
217
0
        code = gs_error_undefinedfilename;
218
0
        goto error_out;
219
0
    }
220
221
1.96M
    if (pfn.iodev == NULL) {
222
0
        pfn.iodev = iodev_default(mem);
223
0
    }
224
225
1.96M
    if (pfn.iodev) {
226
1.96M
        gx_io_device *const iodev = pfn.iodev;
227
228
1.96M
        iodev_proc_open_file((*open_file)) = iodev->procs.open_file;
229
230
1.96M
        if (open_file) {
231
1.96M
            code = open_file(iodev, pfn.fname, pfn.len, "r", &ps, mem);
232
1.96M
            if (code < 0) {
233
0
                goto error_out;
234
0
            }
235
1.96M
        }
236
0
        else {
237
0
            code =
238
0
                file_open_stream(pfn.fname, pfn.len, "r",
239
0
                                 file_default_buffer_size, &ps, pfn.iodev,
240
0
                                 pfn.iodev->procs.gp_fopen, mem);
241
0
            if (code < 0) {
242
0
                goto error_out;
243
0
            }
244
0
        }
245
1.96M
    }
246
0
    else {
247
0
        goto error_out;
248
0
    }
249
250
1.96M
    if ((code = savailable(ps, &length)) < 0) {
251
0
        goto error_out;
252
0
    }
253
254
1.96M
    ftstrm = gs_malloc(mem, sizeof(FT_StreamRec), 1, "FF_open_read_stream");
255
1.96M
    if (!ftstrm) {
256
0
        code = gs_error_VMerror;
257
0
        goto error_out;
258
0
    }
259
1.96M
    memset(ftstrm, 0x00, sizeof(FT_StreamRec));
260
261
1.96M
    ftstrm->descriptor.pointer = ps;
262
1.96M
    ftstrm->read = FF_stream_read;
263
1.96M
    ftstrm->close = FF_stream_close;
264
1.96M
    ftstrm->size = (long)length;
265
1.96M
    *fts = ftstrm;
266
267
1.96M
  error_out:
268
1.96M
    if (code < 0) {
269
0
        if (ps)
270
0
            (void)sclose(ps);
271
0
        if (ftstrm)
272
0
            gs_free(mem, ftstrm, 0, 0, "FF_open_read_stream");
273
0
    }
274
1.96M
    return (code);
275
1.96M
}
276
277
278
static ff_face *
279
new_face(gs_fapi_server * a_server, FT_Face a_ft_face,
280
         FT_Incremental_InterfaceRec * a_ft_inc_int, FT_Stream ftstrm,
281
         unsigned char *a_font_data, int a_font_data_len, bool data_owned)
282
2.55M
{
283
2.55M
    ff_server *s = (ff_server *) a_server;
284
285
2.55M
    ff_face *face = (ff_face *) FF_alloc(s->ftmemory, sizeof(ff_face));
286
287
2.55M
    if (face) {
288
2.55M
        face->ft_face = a_ft_face;
289
2.55M
        face->ft_inc_int = a_ft_inc_int;
290
2.55M
        face->font_data = a_font_data;
291
2.55M
        face->font_data_len = a_font_data_len;
292
2.55M
        face->data_owned = data_owned;
293
2.55M
        face->ftstrm = ftstrm;
294
2.55M
        face->server = (ff_server *) a_server;
295
2.55M
    }
296
2.55M
    return face;
297
2.55M
}
298
299
static void
300
delete_face(gs_fapi_server * a_server, ff_face * a_face)
301
2.55M
{
302
2.55M
    if (a_face) {
303
2.55M
        ff_server *s = (ff_server *) a_server;
304
2.55M
        if (a_face->ft_inc_int) {
305
578k
            FT_Incremental a_info = a_face->ft_inc_int->object;
306
307
578k
            if (a_info->glyph_data) {
308
341k
                gs_free(s->mem, a_info->glyph_data, 0, 0, "delete_face");
309
341k
            }
310
578k
            a_info->glyph_data = NULL;
311
578k
            a_info->glyph_data_length = 0;
312
578k
            delete_inc_int(a_server, a_face->ft_inc_int);
313
578k
            a_face->ft_inc_int = NULL;
314
578k
        }
315
2.55M
        FT_Done_Face(a_face->ft_face);
316
317
2.55M
        FF_free(s->ftmemory, a_face->ft_inc_int);
318
2.55M
        if (a_face->data_owned)
319
2.53M
            FF_free(s->ftmemory, a_face->font_data);
320
2.55M
        if (a_face->ftstrm) {
321
1.96M
            FF_free(s->ftmemory, a_face->ftstrm);
322
1.96M
        }
323
2.55M
        FF_free(s->ftmemory, a_face);
324
2.55M
    }
325
2.55M
}
326
327
static FT_IncrementalRec *
328
new_inc_int_info(gs_fapi_server * a_server, gs_fapi_font * a_fapi_font)
329
580k
{
330
580k
    ff_server *s = (ff_server *) a_server;
331
332
580k
    FT_IncrementalRec *info =
333
580k
        (FT_IncrementalRec *) FF_alloc(s->ftmemory,
334
580k
                                       sizeof(FT_IncrementalRec));
335
580k
    if (info) {
336
580k
        info->fapi_font = a_fapi_font;
337
580k
        info->glyph_data = NULL;
338
580k
        info->glyph_data_length = 0;
339
580k
        info->glyph_data_in_use = false;
340
580k
        info->glyph_metrics_index = 0xFFFFFFFF;
341
580k
        info->metrics_type = gs_fapi_metrics_notdef;
342
580k
    }
343
580k
    return info;
344
580k
}
345
346
static void
347
delete_inc_int_info(gs_fapi_server * a_server,
348
                    FT_IncrementalRec * a_inc_int_info)
349
580k
{
350
580k
    ff_server *s = (ff_server *) a_server;
351
352
580k
    if (a_inc_int_info) {
353
580k
        FF_free(s->ftmemory, a_inc_int_info->glyph_data);
354
580k
        FF_free(s->ftmemory, a_inc_int_info);
355
580k
    }
356
580k
}
357
358
static FT_Error
359
get_fapi_glyph_data(FT_Incremental a_info, FT_UInt a_index, FT_Data * a_data)
360
10.4M
{
361
10.4M
    gs_fapi_font *ff = a_info->fapi_font;
362
10.4M
    int length = 0;
363
10.4M
    ff_face *face = (ff_face *) ff->server_font_data;
364
10.4M
    gs_memory_t *mem = (gs_memory_t *) face->server->mem;
365
366
    /* Tell the FAPI interface that we need to decrypt the glyph data. */
367
10.4M
    ff->need_decrypt = true;
368
369
    /* If glyph_data is already in use (as will happen for composite glyphs)
370
     * create a new buffer on the heap.
371
     */
372
10.4M
    if (a_info->glyph_data_in_use) {
373
27.3k
        unsigned char *buffer = NULL;
374
375
27.3k
        length = ff->get_glyph(ff, a_index, NULL, 0);
376
27.3k
        if (length == gs_fapi_glyph_invalid_format
377
27.3k
            || length == gs_fapi_glyph_invalid_index)
378
0
            return FT_Err_Invalid_Glyph_Index;
379
380
27.3k
        buffer = gs_malloc(mem, length, 1, "get_fapi_glyph_data");
381
27.3k
        if (!buffer)
382
1.15k
            return FT_Err_Out_Of_Memory;
383
384
26.2k
        length = ff->get_glyph(ff, a_index, buffer, length);
385
26.2k
        if (length == gs_fapi_glyph_invalid_format) {
386
0
            gs_free((gs_memory_t *) mem, buffer, 0, 0,
387
0
                    "get_fapi_glyph_data");
388
0
            return FT_Err_Invalid_Glyph_Index;
389
0
        }
390
26.2k
        a_data->pointer = buffer;
391
26.2k
    }
392
10.4M
    else {
393
        /* Save ff->char_data, which is set to null by FAPI_FF_get_glyph as part of a hack to
394
         * make the deprecated Type 2 endchar ('seac') work, so that it can be restored
395
         * if we need to try again with a longer buffer.
396
         */
397
10.4M
        const void *saved_char_data = ff->char_data;
398
399
        /* Get as much of the glyph data as possible into the buffer */
400
10.4M
        length =
401
10.4M
            ff->get_glyph(ff, a_index, a_info->glyph_data,
402
10.4M
                          (ushort) a_info->glyph_data_length);
403
10.4M
        if (length == gs_fapi_glyph_invalid_format) {
404
0
            ff->char_data = saved_char_data;
405
0
            return FT_Err_Unknown_File_Format;
406
0
        }
407
408
10.4M
        if (length == gs_fapi_glyph_invalid_index) {
409
0
            ff->char_data = saved_char_data;
410
0
            return FT_Err_Invalid_Glyph_Index;
411
0
        }
412
413
        /* If the buffer was too small enlarge it and try again. */
414
10.4M
        if (length > a_info->glyph_data_length) {
415
814k
            if (a_info->glyph_data) {
416
471k
                gs_free((gs_memory_t *) mem,
417
471k
                        a_info->glyph_data, 0, 0, "get_fapi_glyph_data");
418
471k
            }
419
420
814k
            a_info->glyph_data =
421
814k
                gs_malloc(mem, length, 1, "get_fapi_glyph_data");
422
423
814k
            if (!a_info->glyph_data) {
424
1.23k
                a_info->glyph_data_length = 0;
425
1.23k
                return FT_Err_Out_Of_Memory;
426
1.23k
            }
427
812k
            a_info->glyph_data_length = length;
428
812k
            ff->char_data = saved_char_data;
429
812k
            length = ff->get_glyph(ff, a_index, a_info->glyph_data, length);
430
812k
            if (length == gs_fapi_glyph_invalid_format)
431
0
                return FT_Err_Unknown_File_Format;
432
812k
            if (length == gs_fapi_glyph_invalid_index)
433
0
                return FT_Err_Invalid_Glyph_Index;
434
812k
        }
435
436
        /* Set the returned pointer and length. */
437
10.4M
        a_data->pointer = a_info->glyph_data;
438
439
10.4M
        a_info->glyph_data_in_use = true;
440
10.4M
    }
441
442
10.4M
    a_data->length = length;
443
10.4M
    return 0;
444
10.4M
}
445
446
static void
447
free_fapi_glyph_data(FT_Incremental a_info, FT_Data * a_data)
448
10.4M
{
449
10.4M
    gs_fapi_font *ff = a_info->fapi_font;
450
10.4M
    ff_face *face = (ff_face *) ff->server_font_data;
451
10.4M
    gs_memory_t *mem = (gs_memory_t *) face->server->mem;
452
453
10.4M
    if (a_data->pointer == (const FT_Byte *)a_info->glyph_data)
454
10.4M
        a_info->glyph_data_in_use = false;
455
26.2k
    else
456
10.4M
        gs_free(mem, (FT_Byte *) a_data->pointer, 0, 0, "free_fapi_glyph_data");
457
10.4M
}
458
459
static FT_Error
460
get_fapi_glyph_metrics(FT_Incremental a_info, FT_UInt a_glyph_index,
461
                       FT_Bool bVertical,
462
                       FT_Incremental_MetricsRec * a_metrics)
463
11.2M
{
464
    /* FreeType will create synthetic vertical metrics, including a vertical
465
     * advance, if none is present. We don't want this, so if the font uses Truetype outlines
466
     * and the WMode is not 1 (vertical) we ignore the advance by setting it to 0
467
     */
468
11.2M
    if (bVertical && !a_info->fapi_font->is_type1)
469
852k
        a_metrics->advance = 0;
470
471
11.2M
    if (a_info->glyph_metrics_index == a_glyph_index) {
472
447k
        switch (a_info->metrics_type) {
473
447k
            case gs_fapi_metrics_add:
474
447k
                a_metrics->advance += a_info->glyph_metrics.advance;
475
447k
                break;
476
0
            case gs_fapi_metrics_replace_width:
477
0
                a_metrics->advance = a_info->glyph_metrics.advance;
478
0
                break;
479
0
            case gs_fapi_metrics_replace:
480
0
                *a_metrics = a_info->glyph_metrics;
481
                /* We are replacing the horizontal metrics, so the vertical must be 0 */
482
0
                a_metrics->advance_v = 0;
483
0
                break;
484
0
            default:
485
                /* This can't happen. */
486
0
                return FT_Err_Invalid_Argument;
487
447k
        }
488
447k
    }
489
11.2M
    return 0;
490
11.2M
}
491
492
static const FT_Incremental_FuncsRec TheFAPIIncrementalInterfaceFuncs = {
493
    get_fapi_glyph_data,
494
    free_fapi_glyph_data,
495
    get_fapi_glyph_metrics
496
};
497
498
static FT_Incremental_InterfaceRec *
499
new_inc_int(gs_fapi_server * a_server, gs_fapi_font * a_fapi_font)
500
580k
{
501
580k
    ff_server *s = (ff_server *) a_server;
502
503
580k
    FT_Incremental_InterfaceRec *i =
504
580k
        (FT_Incremental_InterfaceRec *) FF_alloc(s->ftmemory,
505
580k
                                                 sizeof
506
580k
                                                 (FT_Incremental_InterfaceRec));
507
580k
    if (i) {
508
580k
        i->funcs = &TheFAPIIncrementalInterfaceFuncs;
509
580k
        i->object = (FT_Incremental) new_inc_int_info(a_server, a_fapi_font);
510
511
580k
        if (!i->object) {
512
0
            FF_free(s->ftmemory, i);
513
0
            i = NULL;
514
0
        }
515
580k
    }
516
580k
    return i;
517
580k
}
518
519
static void
520
delete_inc_int(gs_fapi_server * a_server,
521
               FT_Incremental_InterfaceRec * a_inc_int)
522
580k
{
523
580k
    ff_server *s = (ff_server *) a_server;
524
525
580k
    if (a_inc_int) {
526
580k
        delete_inc_int_info(a_server, a_inc_int->object);
527
580k
        FF_free(s->ftmemory, a_inc_int);
528
580k
    }
529
580k
}
530
531
/* Convert FreeType error codes to GhostScript ones.
532
 * Very rudimentary because most don't correspond.
533
 */
534
static int
535
ft_to_gs_error(FT_Error a_error)
536
15.8M
{
537
15.8M
    if (a_error) {
538
208k
        if (a_error == FT_Err_Out_Of_Memory)
539
0
            return_error(gs_error_VMerror);
540
208k
        else
541
208k
            return_error(gs_error_unknownerror);
542
208k
    }
543
15.6M
    return 0;
544
15.8M
}
545
546
/* Load a glyph and optionally rasterize it. Return its metrics in a_metrics.
547
 * If a_bitmap is true convert the glyph to a bitmap.
548
 */
549
static gs_fapi_retcode
550
load_glyph(gs_fapi_server * a_server, gs_fapi_font * a_fapi_font,
551
           const gs_fapi_char_ref * a_char_ref, gs_fapi_metrics * a_metrics,
552
           FT_Glyph * a_glyph, bool a_bitmap, int max_bitmap)
553
14.4M
{
554
14.4M
    ff_server *s = (ff_server *) a_server;
555
14.4M
    FT_Error ft_error = 0;
556
14.4M
    FT_Error ft_error_fb = 1;
557
14.4M
    ff_face *face = (ff_face *) a_fapi_font->server_font_data;
558
14.4M
    FT_Face ft_face = face->ft_face;
559
14.4M
    int index = a_char_ref->char_codes[0];
560
14.4M
    FT_Long w;
561
14.4M
    FT_Long h;
562
14.4M
    FT_Long fflags;
563
14.4M
    FT_Int32 load_flags = 0;
564
14.4M
    FT_Vector  delta = {0,0};
565
566
    /* Save a_fapi_font->char_data, which is set to null by FAPI_FF_get_glyph as part of a hack to
567
     * make the deprecated Type 2 endchar ('seac') work, so that it can be restored
568
     * after the first call to FT_Load_Glyph.
569
     */
570
14.4M
    const void *saved_char_data = a_fapi_font->char_data;
571
14.4M
    const int saved_char_data_len = a_fapi_font->char_data_len;
572
573
14.4M
    if (s->bitmap_glyph) {
574
0
        FT_Bitmap_Done(s->freetype_library, &s->bitmap_glyph->bitmap);
575
0
        FF_free(s->ftmemory, s->bitmap_glyph);
576
0
        s->bitmap_glyph = NULL;
577
0
    }
578
14.4M
    if (s->outline_glyph) {
579
0
        FT_Outline_Done(s->freetype_library, &s->outline_glyph->outline);
580
0
        FF_free(s->ftmemory, s->outline_glyph);
581
0
        s->outline_glyph = NULL;
582
0
    }
583
584
14.4M
    if (!a_char_ref->is_glyph_index) {
585
9.90M
        if (ft_face->num_charmaps)
586
9.84M
            index = FT_Get_Char_Index(ft_face, index);
587
61.4k
        else {
588
            /* If there are no character maps and no glyph index, loading the glyph will still work
589
             * properly if both glyph data and metrics are supplied by the incremental interface.
590
             * In that case we use a dummy glyph index which will be passed
591
             * back to FAPI_FF_get_glyph by get_fapi_glyph_data.
592
             *
593
             * Type 1 fonts don't use the code and can appear to FreeType to have only one glyph,
594
             * so we have to set the index to 0.
595
             *
596
             * For other font types, FAPI_FF_get_glyph requires the character code
597
             * when getting data.
598
             */
599
61.4k
            if (a_fapi_font->is_type1)
600
0
                index = 0;
601
61.4k
            else
602
61.4k
                index = a_char_ref->char_codes[0];
603
61.4k
        }
604
9.90M
    }
605
4.57M
    else {
606
        /* This is a heuristic to try to avoid using the TTF notdef (empty rectangle), and replace it
607
           with a non-marking glyph instead. This is only required for fonts where we don't use the
608
           FT incremental interface - when we are using the incremental interface, we handle it in
609
           our own glyph lookup code.
610
         */
611
4.57M
        if (!a_fapi_font->is_cid && !face->ft_inc_int &&
612
3.06M
            (index == 0 ||
613
3.06M
            (a_char_ref->client_char_code != gs_no_char &&
614
3.06M
            FT_Get_Char_Index(ft_face, a_char_ref->client_char_code) <= 0))) {
615
63.0k
            int tmp_ind;
616
617
63.0k
            if ((tmp_ind = FT_Get_Char_Index(ft_face, 32)) > 0) {
618
0
                index = tmp_ind;
619
0
            }
620
63.0k
        }
621
4.57M
    }
622
    /* Refresh the pointer to the FAPI_font held by the incremental interface. */
623
14.4M
    if (face->ft_inc_int)
624
10.3M
        face->ft_inc_int->object->fapi_font = a_fapi_font;
625
626
    /* Store the overriding metrics if they have been supplied. */
627
14.4M
    if (face->ft_inc_int && a_char_ref->metrics_type != gs_fapi_metrics_notdef) {
628
629
448k
        FT_Incremental_MetricsRec *m = &face->ft_inc_int->object->glyph_metrics;
630
631
448k
        m->bearing_x = a_char_ref->sb_x >> 16;
632
448k
        m->bearing_y = a_char_ref->sb_y >> 16;
633
448k
        m->advance = a_char_ref->aw_x >> 16;
634
635
448k
        face->ft_inc_int->object->glyph_metrics_index = index;
636
637
        /* For most font types, the original metrics come directly from the font, and
638
           what we have here are customized (such as a Matrics dict in Postscript). We
639
           only want to use the width, in that case, because other metrics can mess up
640
           the hinting in Freetype. We'll apply custom lsb outselves, using the "delta"
641
           stuff below.
642
           The exception here is PCL/XL embedded TTF fonts, where the h/vmtx tables can
643
           be missing, and we *have* to use the explicit metrics from the PCL/XL glyph
644
           data. (NOTE: if those do not match the original font's metrics, again, the hinting
645
           can be distorted)
646
         */
647
448k
        if (a_char_ref->metrics_type == gs_fapi_metrics_replace && !a_fapi_font->is_mtx_skipped) {
648
0
            face->ft_inc_int->object->glyph_metrics_index = 0xFFFFFFFF;
649
0
            delta.x = FT_MulFix(a_char_ref->sb_x >> 16, ft_face->size->metrics.x_scale);
650
0
            delta.y = FT_MulFix(a_char_ref->sb_y >> 16, ft_face->size->metrics.y_scale);
651
0
            FT_Vector_Transform( &delta, &face->ft_transform);
652
0
        }
653
448k
        else {
654
448k
            face->ft_inc_int->object->metrics_type = a_char_ref->metrics_type;
655
448k
        }
656
448k
    }
657
14.0M
    else if (face->ft_inc_int)
658
        /* Make sure we don't leave this set to the last value, as we may then use inappropriate metrics values */
659
9.90M
        face->ft_inc_int->object->glyph_metrics_index = 0xFFFFFFFF;
660
661
    /* We have to load the glyph, scale it correctly, and render it if we need a bitmap. */
662
14.4M
    if (!ft_error) {
663
        /* We disable loading bitmaps because if we allow it then FreeType invents metrics for them, which messes up our glyph positioning */
664
        /* Also the bitmaps tend to look somewhat different (though more readable) than FreeType's rendering. By disabling them we */
665
        /* maintain consistency better.  (FT_LOAD_NO_BITMAP) */
666
14.4M
        a_fapi_font->char_data = saved_char_data;
667
14.4M
        if (!a_fapi_font->is_mtx_skipped && !a_fapi_font->is_type1) {
668
            /* grid_fit == 1 is the default - use font's native hints
669
             * with freetype, 1 & 3 are, in practice, the same.
670
             */
671
672
4.97M
            if (a_server->grid_fit == 0) {
673
0
                load_flags = FT_LOAD_NO_HINTING | FT_LOAD_NO_AUTOHINT;
674
0
            }
675
4.97M
            else if (a_server->grid_fit == 2) {
676
0
                load_flags = FT_LOAD_FORCE_AUTOHINT;
677
0
            }
678
4.97M
            load_flags |= FT_LOAD_MONOCHROME | FT_LOAD_NO_BITMAP | FT_LOAD_LINEAR_DESIGN | FT_LOAD_PEDANTIC;
679
4.97M
        }
680
9.49M
        else {
681
            /* Current FreeType hinting for type 1 fonts is so poor we are actually better off without it (fewer files render incorrectly) (FT_LOAD_NO_HINTING) */
682
            /* We also need to disable hinting for XL format embedded truetypes */
683
9.49M
            load_flags |= FT_LOAD_MONOCHROME | FT_LOAD_NO_HINTING | FT_LOAD_NO_BITMAP | FT_LOAD_LINEAR_DESIGN;
684
9.49M
        }
685
686
14.4M
        ft_error = FT_Load_Glyph(ft_face, index, load_flags);
687
14.4M
        if (ft_error == FT_Err_Unknown_File_Format) {
688
0
            return index + 1;
689
0
        }
690
14.4M
    }
691
692
14.4M
    if (ft_error == FT_Err_Invalid_Argument
693
14.4M
        || ft_error == FT_Err_Invalid_Reference
694
14.3M
        || ft_error == FT_Err_Invalid_Glyph_Index
695
14.3M
        || ft_error == FT_Err_DEF_In_Glyf_Bytecode
696
14.3M
        || (ft_error >= FT_Err_Invalid_Opcode
697
554k
            && ft_error <= FT_Err_Too_Many_Instruction_Defs)) {
698
699
554k
        a_fapi_font->char_data = saved_char_data;
700
701
        /* We want to prevent hinting, even for a "tricky" font - it shouldn't matter for the notdef */
702
554k
        fflags = ft_face->face_flags;
703
554k
        ft_face->face_flags &= ~FT_FACE_FLAG_TRICKY;
704
554k
        load_flags |= FT_LOAD_NO_HINTING;
705
554k
        ft_error = FT_Load_Glyph(ft_face, index, load_flags);
706
707
554k
        ft_face->face_flags = fflags;
708
554k
    }
709
710
14.4M
    if (ft_error == FT_Err_Out_Of_Memory
711
14.4M
        || ft_error == FT_Err_Array_Too_Large) {
712
1.33k
        return (gs_error_VMerror);
713
1.33k
    }
714
715
    /* If FT gives us an error, try to fall back to the notdef - if that doesn't work, we'll throw an error over to Ghostscript */
716
14.4M
    if (ft_error) {
717
151k
        gs_string notdef_str;
718
719
151k
        notdef_str.data = (byte *)".notdef";
720
151k
        notdef_str.size = 7;
721
722
151k
        a_fapi_font->char_data = (void *)(&notdef_str);
723
151k
        a_fapi_font->char_data_len = 0;
724
725
        /* We want to prevent hinting, even for a "tricky" font - it shouldn't matter for the notdef */
726
151k
        fflags = ft_face->face_flags;
727
151k
        ft_face->face_flags &= ~FT_FACE_FLAG_TRICKY;
728
729
151k
        ft_error_fb = FT_Load_Glyph(ft_face, 0, load_flags);
730
731
151k
        ft_face->face_flags = fflags;
732
733
151k
        a_fapi_font->char_data = saved_char_data;
734
151k
        a_fapi_font->char_data_len = saved_char_data_len;
735
151k
    }
736
737
14.4M
    if ((!ft_error || !ft_error_fb) && (delta.x != 0 || delta.y != 0)) {
738
0
        FT_Outline_Translate( &ft_face->glyph->outline, delta.x, delta.y);
739
0
    }
740
741
    /* Previously we interpreted the glyph unscaled, and derived the metrics from that. Now we only interpret it
742
     * once, and work out the metrics from the scaled/hinted outline.
743
     */
744
14.4M
    if ((!ft_error || !ft_error_fb) && a_metrics) {
745
14.4M
        FT_Long hx;
746
14.4M
        FT_Long hy;
747
14.4M
        FT_Long vadv;
748
749
        /* In order to get the metrics in the form we need them, we have to remove the size scaling
750
         * the resolution scaling, and convert to points.
751
         */
752
14.4M
        hx = (FT_Long) (((double)ft_face->glyph->metrics.horiBearingX *
753
14.4M
                         ft_face->units_per_EM * 72.0) /
754
14.4M
                        ((double)face->width * face->horz_res))  + (a_fapi_font->is_mtx_skipped == 1 ? 0 : a_char_ref->sb_x >> 16);
755
14.4M
        hy = (FT_Long) (((double)ft_face->glyph->metrics.horiBearingY *
756
14.4M
                         ft_face->units_per_EM * 72.0) /
757
14.4M
                        ((double)face->height * face->vert_res)) + (a_fapi_font->is_mtx_skipped == 1 ? 0 : a_char_ref->sb_y >> 16);
758
759
14.4M
        w = (FT_Long) (((double)ft_face->glyph->metrics.width *
760
14.4M
                        ft_face->units_per_EM * 72.0) / ((double)face->width *
761
14.4M
                                                         face->horz_res));
762
14.4M
        h = (FT_Long) (((double)ft_face->glyph->metrics.height *
763
14.4M
                        ft_face->units_per_EM * 72.0) /
764
14.4M
                       ((double)face->height * face->vert_res));
765
766
        /* Ugly. FreeType creates verticla metrics for TT fonts, normally we override them in the
767
         * metrics callbacks, but those only work for incremental interface fonts, and TrueType fonts
768
         * loaded as CIDFont replacements are not incrementally handled. So here, if its a CIDFont, and
769
         * its not type 1 outlines, and its not a vertical mode fotn, ignore the advance.
770
         */
771
14.4M
        if (a_fapi_font->is_type1
772
4.96M
           || ((a_fapi_font->full_font_buf || a_fapi_font->font_file_path)
773
9.49M
           && a_fapi_font->is_vertical &&  FT_HAS_VERTICAL(ft_face))) {
774
775
9.49M
            vadv = ft_face->glyph->linearVertAdvance;
776
9.49M
        }
777
4.96M
        else {
778
4.96M
            vadv = 0;
779
4.96M
        }
780
781
14.4M
        a_metrics->bbox_x0 = hx;
782
14.4M
        a_metrics->bbox_y0 = hy - h;
783
14.4M
        a_metrics->bbox_x1 = a_metrics->bbox_x0 + w;
784
14.4M
        a_metrics->bbox_y1 = a_metrics->bbox_y0 + h;
785
14.4M
        a_metrics->escapement = ft_face->glyph->linearHoriAdvance;
786
14.4M
        a_metrics->v_escapement = vadv;
787
14.4M
        a_metrics->em_x = ft_face->units_per_EM;
788
14.4M
        a_metrics->em_y = ft_face->units_per_EM;
789
14.4M
    }
790
791
14.4M
    if ((!ft_error || !ft_error_fb)) {
792
793
14.4M
        FT_BBox cbox;
794
795
        /* compute the control box, and grid fit it - lifted from ft_raster1_render() */
796
14.4M
        FT_Outline_Get_CBox(&ft_face->glyph->outline, &cbox);
797
798
        /* These round operations are only to preserve behaviour compared to the 9.00 release
799
           which used the bitmap dimensions as calculated by Freetype.
800
           But FT_PIX_FLOOR/FT_PIX_CEIL aren't public.
801
         */
802
14.4M
        cbox.xMin = ((cbox.xMin) & ~63);        /* FT_PIX_FLOOR( cbox.xMin ) */
803
14.4M
        cbox.yMin = ((cbox.yMin) & ~63);
804
14.4M
        cbox.xMax = (((cbox.xMax) + 63) & ~63);
805
14.4M
        cbox.yMax = (((cbox.yMax) + 63) & ~63); /* FT_PIX_CEIL( cbox.yMax ) */
806
807
14.4M
        w = (FT_UInt) ((cbox.xMax - cbox.xMin) >> 6);
808
14.4M
        h = (FT_UInt) ((cbox.yMax - cbox.yMin) >> 6);
809
810
14.4M
        if (!a_fapi_font->metrics_only && a_bitmap == true && ft_face->glyph->format != FT_GLYPH_FORMAT_BITMAP
811
7.96M
            && ft_face->glyph->format != FT_GLYPH_FORMAT_COMPOSITE) {
812
7.96M
            if ((bitmap_raster(w) * h) < max_bitmap) {
813
5.12M
                FT_Render_Mode mode = FT_RENDER_MODE_MONO;
814
5.12M
                FT_UInt save_x_ppem = ft_face->glyph->face->size->metrics.x_ppem;
815
5.12M
                FT_UInt save_y_ppem = ft_face->glyph->face->size->metrics.y_ppem;
816
817
                /* Workaround so we have more control over size of the glyph that freetype will render for us */
818
5.12M
                if (a_fapi_font->is_type1) {
819
3.20M
                    ft_face->glyph->face->size->metrics.x_ppem = ft_face->glyph->face->size->metrics.y_ppem = 1000;
820
3.20M
                }
821
1.92M
                else {
822
1.92M
                    ft_face->glyph->face->size->metrics.x_ppem = ft_face->glyph->face->size->metrics.y_ppem = 2048;
823
1.92M
                }
824
5.12M
                ft_error = FT_Render_Glyph(ft_face->glyph, mode);
825
826
5.12M
                ft_face->glyph->face->size->metrics.x_ppem = save_x_ppem;
827
5.12M
                ft_face->glyph->face->size->metrics.y_ppem = save_y_ppem;
828
829
5.12M
                if (ft_error != 0) {
830
75
                    (*a_glyph) = NULL;
831
75
                    return (gs_error_VMerror);
832
75
                }
833
5.12M
            }
834
2.84M
            else {
835
2.84M
                (*a_glyph) = NULL;
836
2.84M
                return (gs_error_VMerror);
837
2.84M
            }
838
7.96M
        }
839
14.4M
    }
840
841
11.6M
    if (!a_fapi_font->metrics_only) {
842
        /* The following works around the fact that at the scales we deal with
843
         * these values may not fit in a 16.16 fixed point value, and thus cause
844
         * freetype to error due to overflow - but we don't use these values
845
         * and neither does freetype, we can set them to zero and avoid the error
846
         */
847
9.36M
        ft_face->glyph->advance.x = ft_face->glyph->advance.y = 0;
848
9.36M
        if ((!ft_error || !ft_error_fb) && a_glyph) {
849
9.35M
            ft_error = FT_Get_Glyph(ft_face->glyph, a_glyph);
850
9.35M
        }
851
11.0k
        else {
852
11.0k
            if (ft_face->glyph->format == FT_GLYPH_FORMAT_BITMAP) {
853
0
                FT_BitmapGlyph bmg;
854
855
0
                ft_error = FT_Get_Glyph(ft_face->glyph, (FT_Glyph *) & bmg);
856
0
                if (!ft_error) {
857
0
                    FT_Bitmap_Done(s->freetype_library, &bmg->bitmap);
858
0
                    FF_free(s->ftmemory, bmg);
859
0
                }
860
0
            }
861
11.0k
            else if (ft_face->glyph->format == FT_GLYPH_FORMAT_OUTLINE) {
862
0
                FT_OutlineGlyph olg;
863
864
0
                ft_error = FT_Get_Glyph(ft_face->glyph, (FT_Glyph *) & olg);
865
0
                if (!ft_error) {
866
0
                    FT_Outline_Done(s->freetype_library, &olg->outline);
867
0
                    FF_free(s->ftmemory, olg);
868
0
                }
869
0
            }
870
11.0k
        }
871
9.36M
    }
872
873
11.6M
    if (ft_error == FT_Err_Too_Many_Hints) {
874
#ifdef DEBUG
875
        if (gs_debug_c('1')) {
876
            ft_emprintf1(a_fapi_font->memory,
877
                      "TrueType glyph %"PRId64" uses more instructions than the declared maximum in the font.",
878
                      a_char_ref->char_codes[0]);
879
880
            if (!ft_error_fb) {
881
                ft_emprintf(a_fapi_font->memory,
882
                         " Continuing, falling back to notdef\n\n");
883
            }
884
        }
885
#endif
886
258
        if (!ft_error_fb)
887
0
            ft_error = 0;
888
258
    }
889
11.6M
    if (ft_error == FT_Err_Invalid_Argument) {
890
#ifdef DEBUG
891
        if (gs_debug_c('1')) {
892
            ft_emprintf1(a_fapi_font->memory,
893
                      "TrueType parsing error in glyph %"PRId64" in the font.",
894
                      a_char_ref->char_codes[0]);
895
896
            if (!ft_error_fb) {
897
                ft_emprintf(a_fapi_font->memory,
898
                         " Continuing, falling back to notdef\n\n");
899
            }
900
        }
901
#endif
902
3.45k
        if (!ft_error_fb)
903
0
            ft_error = 0;
904
3.45k
    }
905
11.6M
    if (ft_error == FT_Err_Too_Many_Function_Defs) {
906
#ifdef DEBUG
907
        if (gs_debug_c('1')) {
908
            ft_emprintf1(a_fapi_font->memory,
909
                      "TrueType instruction error in glyph %"PRId64" in the font.",
910
                      a_char_ref->char_codes[0]);
911
912
            if (!ft_error_fb) {
913
                ft_emprintf(a_fapi_font->memory,
914
                         " Continuing, falling back to notdef\n\n");
915
            }
916
        }
917
#endif
918
0
        if (!ft_error_fb)
919
0
            ft_error = 0;
920
0
    }
921
11.6M
    if (ft_error == FT_Err_Invalid_Glyph_Index) {
922
#ifdef DEBUG
923
        if (gs_debug_c('1')) {
924
            ft_emprintf1(a_fapi_font->memory,
925
                      "FreeType is unable to find the glyph %"PRId64" in the font.",
926
                      a_char_ref->char_codes[0]);
927
928
            if (!ft_error_fb) {
929
                ft_emprintf(a_fapi_font->memory,
930
                         " Continuing, falling back to notdef\n\n");
931
            }
932
        }
933
#endif
934
0
        if (!ft_error_fb)
935
0
            ft_error = 0;
936
0
    }
937
11.6M
    return ft_to_gs_error(ft_error);
938
14.4M
}
939
940
/*
941
 * Ensure that the rasterizer is open.
942
 *
943
 * In the case of FreeType this means creating the FreeType library object.
944
 */
945
static gs_fapi_retcode
946
gs_fapi_ft_ensure_open(gs_fapi_server * a_server, const char * server_param,
947
                       int server_param_size)
948
2.56M
{
949
2.56M
    ff_server *s = (ff_server *) a_server;
950
2.56M
    FT_UInt tt_ins_version = TT_INTERPRETER_VERSION_35;
951
2.56M
    FT_Error ft_error;
952
953
2.56M
    if (s->freetype_library)
954
2.49M
        return 0;
955
956
    /* As we want FT to use our memory management, we cannot use the convenience of
957
     * FT_Init_FreeType(), we have to do each stage "manually"
958
     */
959
69.1k
    s->ftmemory->user = s->mem;
960
69.1k
    s->ftmemory->alloc = FF_alloc;
961
69.1k
    s->ftmemory->free = FF_free;
962
69.1k
    s->ftmemory->realloc = FF_realloc;
963
964
69.1k
    ft_error = FT_New_Library(s->ftmemory, &s->freetype_library);
965
69.1k
    if (ft_error)
966
0
        return ft_to_gs_error(ft_error);
967
968
69.1k
    FT_Add_Default_Modules(s->freetype_library);
969
69.1k
    FT_Property_Set( s->freetype_library, "truetype", "interpreter-version", &tt_ins_version);
970
971
69.1k
    return 0;
972
69.1k
}
973
974
#if 0                           /* Not currently used */
975
static void
976
transform_concat(FT_Matrix * a_A, const FT_Matrix * a_B)
977
{
978
    FT_Matrix result = *a_B;
979
980
    FT_Matrix_Multiply(a_A, &result);
981
    *a_A = result;
982
}
983
984
/* Create a transform representing an angle defined as a vector. */
985
static void
986
make_rotation(FT_Matrix * a_transform, const FT_Vector * a_vector)
987
{
988
    FT_Fixed length, cos, sin;
989
990
    if (a_vector->x >= 0 && a_vector->y == 0) {
991
        a_transform->xx = a_transform->yy = 65536;
992
        a_transform->xy = a_transform->yx = 0;
993
        return;
994
    }
995
996
    length = FT_Vector_Length((FT_Vector *) a_vector);
997
    cos = FT_DivFix(a_vector->x, length);
998
    sin = FT_DivFix(a_vector->y, length);
999
    a_transform->xx = a_transform->yy = cos;
1000
    a_transform->xy = -sin;
1001
    a_transform->yx = sin;
1002
}
1003
#endif /* Not currently used */
1004
1005
/* Divide a transformation into a scaling part and a rotation-and-shear part.
1006
 * The scaling part is used for setting the pixel size for hinting.
1007
 */
1008
static void
1009
transform_decompose(FT_Matrix * a_transform, FT_UInt * xresp, FT_UInt * yresp,
1010
                    FT_Fixed * a_x_scale, FT_Fixed * a_y_scale, int units_per_EM)
1011
8.33M
{
1012
8.33M
    double scalex, scaley, fact = 1.0;
1013
8.33M
    double factx = 1.0, facty = 1.0;
1014
8.33M
    FT_Matrix ftscale_mat;
1015
8.33M
    FT_UInt xres;
1016
8.33M
    FT_UInt yres;
1017
    /* We have to account for units_per_EM as we fiddle with the scaling
1018
     * in order to avoid underflow (mostly in the TTF hinting code), but
1019
     * we also want to clamp to a lower value (512, admittedly arrived at
1020
     * via experimentation) in order to preserve the fidelity of the outlines.
1021
     */
1022
8.33M
    double upe = units_per_EM > 512 ? (float)units_per_EM : 512.0;
1023
1024
8.33M
    scalex = hypot((double)a_transform->xx, (double)a_transform->xy);
1025
8.33M
    scaley = hypot((double)a_transform->yx, (double)a_transform->yy);
1026
1027
    /* In addition to all the wrangling below, we have to make sure that
1028
     * that the contents of a_transform can also be understood by Freetype.
1029
     */
1030
8.33M
    if (scalex < 64.0 || scaley < 64.0) {
1031
4.90k
        factx = 64.0/scalex;
1032
4.90k
        facty = 64.0/scaley;
1033
1034
4.90k
        ftscale_mat.xx = (FT_Fixed)(a_transform->xx * factx);
1035
4.90k
        ftscale_mat.xy = (FT_Fixed)(a_transform->xy * facty);
1036
4.90k
        ftscale_mat.yx = (FT_Fixed)(a_transform->yx * factx);
1037
4.90k
        ftscale_mat.yy = (FT_Fixed)(a_transform->yy * facty);
1038
4.90k
        memcpy(a_transform, &ftscale_mat, sizeof(ftscale_mat));
1039
4.90k
        scalex = hypot((double)a_transform->xx, (double)a_transform->xy);
1040
4.90k
        scaley = hypot((double)a_transform->yx, (double)a_transform->yy);
1041
4.90k
    }
1042
1043
8.33M
    if (*xresp != *yresp) {
1044
        /* To get good results, we have to pull the implicit scaling from
1045
         * non-square resolutions, and apply it in the matrix. This means
1046
         * we get the correct "shearing" effect for rotated glyphs.
1047
         * The previous solution was only effective for for glyphs whose
1048
         * axes were coincident with the axes of the page.
1049
         */
1050
0
        bool use_x = true;
1051
1052
0
        if (*xresp < *yresp) {
1053
0
            use_x = false;
1054
0
        }
1055
1056
0
        ftscale_mat.xx =
1057
0
            (int)(((double)(*xresp) /
1058
0
                   ((double)(use_x ? (*xresp) : (*yresp)))) * 65536);
1059
0
        ftscale_mat.xy = ftscale_mat.yx = 0;
1060
0
        ftscale_mat.yy =
1061
0
            (int)(((double)(*yresp) /
1062
0
                   ((double)(use_x ? (*xresp) : (*yresp)))) * 65536);
1063
1064
0
        FT_Matrix_Multiply(&ftscale_mat, a_transform);
1065
1066
0
        xres = yres = (use_x ? (*xresp) : (*yresp));
1067
0
        xres = (FT_UInt)(xres / factx);
1068
0
        yres = (FT_UInt)(yres / facty);
1069
0
    }
1070
8.33M
    else {
1071
        /* Life is considerably easier when square resolutions are in use! */
1072
8.33M
        xres = (FT_UInt)(*xresp / factx);
1073
8.33M
        yres = (FT_UInt)(*yresp / facty);
1074
8.33M
    }
1075
1076
8.33M
    scalex *= 1.0 / 65536.0;
1077
8.33M
    scaley *= 1.0 / 65536.0;
1078
1079
8.33M
    if (scalex < scaley) {
1080
83.6k
        scaley = scalex;
1081
83.6k
    }
1082
8.25M
    else if (scalex > scaley) {
1083
46.9k
        scalex = scaley;
1084
46.9k
    }
1085
1086
    /* FT clamps the width and height to a lower limit of 1.0 units
1087
     * (note: as FT stores it in 64ths of a unit, that is 64)
1088
     * So if either the width or the height are <1.0 here, we scale
1089
     * the width and height appropriately, and then compensate using
1090
     * the "final" matrix for FT
1091
     */
1092
    /* We use 1 1/64th to calculate the scale, so that we *guarantee* the
1093
     * scalex/y we calculate will be >64 after rounding.
1094
     */
1095
1096
8.33M
    if (scalex < 1.0) {
1097
515k
        fact = 1.016 / scalex;
1098
515k
        scalex = scalex * fact;
1099
515k
        scaley = scaley * fact;
1100
515k
    }
1101
1102
    /* see above */
1103
8.33M
    if (scalex * xres < 2268.0 / 64.0) {
1104
2.54k
        fact = (2400.0 / 64.0) / (xres * scalex);
1105
2.54k
        scaley *= fact;
1106
2.54k
        scalex *= fact;
1107
2.54k
    }
1108
1109
    /* see above */
1110
8.33M
    fact = 1.0;
1111
11.8M
    while (scaley * yres > (double)upe * 72.0 && (xres > 0 && yres > 0)
1112
3.53M
           && (scalex > 0.0 && scaley > 0.0)) {
1113
3.53M
        if (scaley < yres) {
1114
1.97k
            xres >>= 1;
1115
1.97k
            yres >>= 1;
1116
1.97k
            fact *= 2.0;
1117
1.97k
        }
1118
3.53M
        else {
1119
3.53M
            scalex /= 1.25;
1120
3.53M
            scaley /= 1.25;
1121
3.53M
        }
1122
3.53M
    }
1123
1124
8.33M
    ftscale_mat.xx = (FT_Fixed) ((65536.0 / scalex) * fact);
1125
8.33M
    ftscale_mat.xy = 0;
1126
8.33M
    ftscale_mat.yx = 0;
1127
8.33M
    ftscale_mat.yy = (FT_Fixed) ((65536.0 / scaley) * fact);
1128
1129
8.33M
    FT_Matrix_Multiply(a_transform, &ftscale_mat);
1130
8.33M
    memcpy(a_transform, &ftscale_mat, sizeof(FT_Matrix));
1131
1132
8.33M
    *xresp = xres;
1133
8.33M
    *yresp = yres;
1134
    /* Return values ready scaled for FT */
1135
8.33M
    *a_x_scale = (FT_Fixed) (scalex * 64);
1136
8.33M
    *a_y_scale = (FT_Fixed) (scaley * 64);
1137
8.33M
}
1138
1139
/*
1140
 * Open a font and set its size.
1141
 */
1142
static gs_fapi_retcode
1143
gs_fapi_ft_get_scaled_font(gs_fapi_server * a_server, gs_fapi_font * a_font,
1144
                const gs_fapi_font_scale * a_font_scale,
1145
                const char *a_map, gs_fapi_descendant_code a_descendant_code)
1146
8.34M
{
1147
8.34M
    ff_server *s = (ff_server *) a_server;
1148
8.34M
    ff_face *face = (ff_face *) a_font->server_font_data;
1149
8.34M
    FT_Error ft_error = 0;
1150
8.34M
    int i, j, code;
1151
8.34M
    FT_CharMap cmap = NULL;
1152
8.34M
    bool data_owned = true;
1153
1154
8.34M
    if (s->bitmap_glyph) {
1155
0
        FT_Bitmap_Done(s->freetype_library, &s->bitmap_glyph->bitmap);
1156
0
        FF_free(s->ftmemory, s->bitmap_glyph);
1157
0
        s->bitmap_glyph = NULL;
1158
0
    }
1159
8.34M
    if (s->outline_glyph) {
1160
0
        FT_Outline_Done(s->freetype_library, &s->outline_glyph->outline);
1161
0
        FF_free(s->ftmemory, s->outline_glyph);
1162
0
        s->outline_glyph = NULL;
1163
0
    }
1164
1165
    /* dpf("gs_fapi_ft_get_scaled_font enter: is_type1=%d is_cid=%d font_file_path='%s' a_descendant_code=%d\n",
1166
       a_font->is_type1, a_font->is_cid, a_font->font_file_path ? a_font->font_file_path : "", a_descendant_code); */
1167
1168
    /* If this font is the top level font of an embedded CID type 0 font (font type 9)
1169
     * do nothing. See the comment in FAPI_prepare_font. The descendant fonts are
1170
     * passed in individually.
1171
     */
1172
8.34M
    if (a_font->is_cid && a_font->is_type1 && a_font->font_file_path == NULL
1173
6.14k
        && (a_descendant_code == gs_fapi_toplevel_begin
1174
4.69k
            || a_descendant_code == gs_fapi_toplevel_complete)) {
1175
        /* dpf("gs_fapi_ft_get_scaled_font return 0\n"); */
1176
2.87k
        return 0;
1177
2.87k
    }
1178
1179
    /* Create the face if it doesn't already exist. */
1180
8.34M
    if (!face) {
1181
2.55M
        FT_Face ft_face = NULL;
1182
2.55M
        FT_Parameter ft_param;
1183
2.55M
        FT_Incremental_InterfaceRec *ft_inc_int = NULL;
1184
2.55M
        unsigned char *own_font_data = NULL;
1185
2.55M
        int own_font_data_len = -1;
1186
2.55M
        FT_Stream ft_strm = NULL;
1187
1188
        /* dpf("gs_fapi_ft_get_scaled_font creating face\n"); */
1189
1190
2.55M
        if (a_font->full_font_buf) {
1191
1192
11.8k
            own_font_data =
1193
11.8k
                gs_malloc(((gs_memory_t *) (s->ftmemory->user)),
1194
11.8k
                          a_font->full_font_buf_len, 1,
1195
11.8k
                          "gs_fapi_ft_get_scaled_font - full font buf");
1196
11.8k
            if (!own_font_data) {
1197
0
                return_error(gs_error_VMerror);
1198
0
            }
1199
1200
11.8k
            own_font_data_len = a_font->full_font_buf_len;
1201
11.8k
            memcpy(own_font_data, a_font->full_font_buf,
1202
11.8k
                   a_font->full_font_buf_len);
1203
1204
11.8k
            ft_error =
1205
11.8k
                FT_New_Memory_Face(s->freetype_library,
1206
11.8k
                                   (const FT_Byte *)own_font_data,
1207
11.8k
                                   own_font_data_len, a_font->subfont,
1208
11.8k
                                   &ft_face);
1209
1210
11.8k
            if (ft_error) {
1211
794
                gs_memory_t *mem = (gs_memory_t *) s->ftmemory->user;
1212
794
                gs_free(mem, own_font_data, 0, 0, "gs_fapi_ft_get_scaled_font");
1213
794
                return ft_to_gs_error(ft_error);
1214
794
            }
1215
11.8k
        }
1216
        /* Load a typeface from a file. */
1217
2.54M
        else if (a_font->font_file_path) {
1218
1.96M
            FT_Open_Args args;
1219
1220
1.96M
            memset(&args, 0x00, sizeof(args));
1221
1222
1.96M
            if ((code =
1223
1.96M
                 FF_open_read_stream((gs_memory_t *) (s->ftmemory->user),
1224
1.96M
                                     (char *)a_font->font_file_path,
1225
1.96M
                                     &ft_strm)) < 0) {
1226
0
                return (code);
1227
0
            }
1228
1229
1.96M
            args.flags = FT_OPEN_STREAM;
1230
1.96M
            args.stream = ft_strm;
1231
1232
1.96M
            ft_error =
1233
1.96M
                FT_Open_Face(s->freetype_library, &args, a_font->subfont,
1234
1.96M
                             &ft_face);
1235
1.96M
            if (ft_error) {
1236
                /* in the event of an error, Freetype should cleanup the stream */
1237
                /* But not the ft container */
1238
0
                gs_memory_t *mem = (gs_memory_t *) s->ftmemory->user;
1239
0
                gs_free(mem, ft_strm, 0, 0, "gs_fapi_ft_get_scaled_font");
1240
0
                return ft_to_gs_error(ft_error);
1241
0
            }
1242
1.96M
        }
1243
1244
        /* Load a typeface from a representation in GhostScript's memory. */
1245
580k
        else {
1246
580k
            FT_Open_Args open_args;
1247
1248
580k
            open_args.flags = FT_OPEN_MEMORY;
1249
580k
            open_args.stream = NULL;
1250
1251
580k
            if (a_font->is_type1) {
1252
560k
                long length;
1253
560k
                unsigned short type = 0;
1254
1255
560k
                code = a_font->get_word(a_font, gs_fapi_font_feature_FontType, 0, &type);
1256
560k
                if (code < 0) {
1257
0
                    return code;
1258
0
                }
1259
1260
                /* Tell the FAPI interface that we need to decrypt the /Subrs data. */
1261
560k
                a_font->need_decrypt = true;
1262
1263
                /*
1264
                   Serialise a type 1 font in PostScript source form, or
1265
                   a Type 2 font in binary form, so that FreeType can read it.
1266
                 */
1267
560k
                if (type == 1)
1268
551k
                    length = gs_fapi_serialize_type1_font(a_font, NULL, 0);
1269
9.56k
                else
1270
9.56k
                    length = gs_fapi_serialize_type2_font(a_font, NULL, 0);
1271
1272
560k
                open_args.memory_base = own_font_data =
1273
560k
                    FF_alloc(s->ftmemory, length);
1274
560k
                if (!open_args.memory_base)
1275
60
                    return_error(gs_error_VMerror);
1276
560k
                own_font_data_len = length;
1277
560k
                if (type == 1)
1278
551k
                    open_args.memory_size =
1279
551k
                        gs_fapi_serialize_type1_font(a_font, own_font_data, length);
1280
9.50k
                else
1281
9.50k
                    open_args.memory_size =
1282
9.50k
                        gs_fapi_serialize_type2_font(a_font, own_font_data, length);
1283
1284
560k
                if (open_args.memory_size != length)
1285
0
                    return_error(gs_error_unregistered);        /* Must not happen. */
1286
1287
560k
                ft_inc_int = new_inc_int(a_server, a_font);
1288
560k
                if (!ft_inc_int) {
1289
0
                    FF_free(s->ftmemory, own_font_data);
1290
0
                    return_error(gs_error_VMerror);
1291
0
                }
1292
560k
            }
1293
1294
            /* It must be type 42 (see code in FAPI_FF_get_glyph in zfapi.c). */
1295
20.1k
            else {
1296
                /* Get the length of the TrueType data. */
1297
1298
20.1k
                if (a_font->retrieve_tt_font != NULL) {
1299
19.9k
                    unsigned int ms;
1300
19.9k
                    code = a_font->retrieve_tt_font(a_font, (void **)&own_font_data, &ms);
1301
19.9k
                    if (code == 0) {
1302
19.9k
                        data_owned = false;
1303
19.9k
                        open_args.memory_base = own_font_data;
1304
19.9k
                        open_args.memory_size = own_font_data_len = ms;
1305
19.9k
                    }
1306
19.9k
                }
1307
121
                else
1308
121
                    code = gs_error_unregistered;
1309
1310
20.1k
                if (code < 0) {
1311
121
                    unsigned long lms;
1312
121
                    code = a_font->get_long(a_font, gs_fapi_font_feature_TT_size, 0, &lms);
1313
121
                    if (code < 0)
1314
0
                        return code;
1315
121
                    if (lms == 0)
1316
0
                        return_error(gs_error_invalidfont);
1317
1318
121
                    open_args.memory_size = (FT_Long)lms;
1319
1320
                    /* Load the TrueType data into a single buffer. */
1321
121
                    open_args.memory_base = own_font_data =
1322
121
                        FF_alloc(s->ftmemory, open_args.memory_size);
1323
121
                    if (!own_font_data)
1324
0
                        return_error(gs_error_VMerror);
1325
1326
121
                    own_font_data_len = open_args.memory_size;
1327
1328
121
                    code = a_font->serialize_tt_font(a_font, own_font_data,
1329
121
                                          open_args.memory_size);
1330
121
                    if (code < 0) {
1331
28
                        FF_free(s->ftmemory, own_font_data);
1332
28
                        return code;
1333
28
                    }
1334
121
                }
1335
1336
                /* We always load incrementally. */
1337
20.0k
                ft_inc_int = new_inc_int(a_server, a_font);
1338
20.0k
                if (!ft_inc_int) {
1339
0
                    if (data_owned)
1340
0
                        FF_free(s->ftmemory, own_font_data);
1341
0
                    return_error(gs_error_VMerror);
1342
0
                }
1343
20.0k
            }
1344
1345
580k
            if (ft_inc_int) {
1346
580k
                open_args.flags =
1347
580k
                    (FT_UInt) (open_args.flags | FT_OPEN_PARAMS);
1348
580k
                ft_param.tag = FT_PARAM_TAG_INCREMENTAL;
1349
580k
                ft_param.data = ft_inc_int;
1350
580k
                open_args.num_params = 1;
1351
580k
                open_args.params = &ft_param;
1352
580k
            }
1353
580k
            ft_error =
1354
580k
                FT_Open_Face(s->freetype_library, &open_args, a_font->subfont,
1355
580k
                             &ft_face);
1356
580k
            if (ft_error) {
1357
1.92k
                delete_inc_int (a_server, ft_inc_int);
1358
1.92k
                if (data_owned)
1359
62
                    FF_free(s->ftmemory, own_font_data);
1360
1.92k
                return ft_to_gs_error(ft_error);
1361
1.92k
            }
1362
580k
        }
1363
1364
2.55M
        if (ft_face) {
1365
2.55M
            face =
1366
2.55M
                new_face(a_server, ft_face, ft_inc_int, ft_strm,
1367
2.55M
                         own_font_data, own_font_data_len, data_owned);
1368
2.55M
            if (!face) {
1369
0
                if (data_owned)
1370
0
                    FF_free(s->ftmemory, own_font_data);
1371
0
                FT_Done_Face(ft_face);
1372
0
                delete_inc_int(a_server, ft_inc_int);
1373
0
                return_error(gs_error_VMerror);
1374
0
            }
1375
2.55M
            a_font->server_font_data = face;
1376
1377
2.55M
            if (!a_font->is_type1) {
1378
5.97M
                for (i = 0; i < GS_FAPI_NUM_TTF_CMAP_REQ && !cmap; i++) {
1379
4.04M
                    if (a_font->ttf_cmap_req[i].platform_id > 0) {
1380
13.7M
                        for (j = 0; j < face->ft_face->num_charmaps; j++) {
1381
11.6M
                            if (FT_Get_CMap_Format(face->ft_face->charmaps[j]) >= 0
1382
11.6M
                             && face->ft_face->charmaps[j]->platform_id == a_font->ttf_cmap_req[i].platform_id
1383
3.95M
                             && face->ft_face->charmaps[j]->encoding_id == a_font->ttf_cmap_req[i].encoding_id) {
1384
1385
1.92M
                                cmap = face->ft_face->charmaps[j];
1386
1.92M
                                break;
1387
1.92M
                            }
1388
11.6M
                        }
1389
3.97M
                    }
1390
63.4k
                    else {
1391
63.4k
                        break;
1392
63.4k
                    }
1393
4.04M
                }
1394
1.99M
                if (cmap) {
1395
1.92M
                    (void)FT_Set_Charmap(face->ft_face, cmap);
1396
1.92M
                }
1397
63.4k
                else if (a_font->full_font_buf != NULL || a_font->font_file_path != NULL) {
1398
                    /* If we've passed a complete TTF to Freetype, but *haven't* requested a
1399
                     * specific cmap table above, try to use a Unicode one
1400
                     * If that doesn't work, just leave the default in place.
1401
                     */
1402
61.0k
                    (void)FT_Select_Charmap(face->ft_face, ft_encoding_unicode);
1403
61.0k
                }
1404
                /* For PDF, we have to know which cmap table actually was selected */
1405
1.99M
                if (face->ft_face->charmap != NULL) {
1406
1.92M
                    a_font->ttf_cmap_selected.platform_id = face->ft_face->charmap->platform_id;
1407
1.92M
                    a_font->ttf_cmap_selected.encoding_id = face->ft_face->charmap->encoding_id;
1408
1.92M
                }
1409
62.7k
                else {
1410
                    /* Just in case */
1411
62.7k
                    a_font->ttf_cmap_selected.platform_id = -1;
1412
62.7k
                    a_font->ttf_cmap_selected.encoding_id = -1;
1413
62.7k
                }
1414
1.99M
            }
1415
560k
            else {
1416
                /* Just in case */
1417
560k
                a_font->ttf_cmap_selected.platform_id = -1;
1418
560k
                a_font->ttf_cmap_selected.encoding_id = -1;
1419
560k
            }
1420
2.55M
        }
1421
0
        else
1422
0
            a_font->server_font_data = NULL;
1423
2.55M
    }
1424
1425
    /* Set the point size and transformation.
1426
     * The matrix is scaled by the shift specified in the server, 16,
1427
     * so we divide by 65536 when converting to a gs_matrix.
1428
     */
1429
8.33M
    if (face) {
1430
        /* Convert the GS transform into an FT transform.
1431
         * Ignore the translation elements because they contain very large values
1432
         * derived from the current transformation matrix and so are of no use.
1433
         */
1434
8.33M
        face->ft_transform.xx = a_font_scale->matrix[0];
1435
8.33M
        face->ft_transform.xy = a_font_scale->matrix[2];
1436
8.33M
        face->ft_transform.yx = a_font_scale->matrix[1];
1437
8.33M
        face->ft_transform.yy = a_font_scale->matrix[3];
1438
1439
8.33M
        face->horz_res = a_font_scale->HWResolution[0] >> 16;
1440
8.33M
        face->vert_res = a_font_scale->HWResolution[1] >> 16;
1441
1442
        /* Split the transform into scale factors and a rotation-and-shear
1443
         * transform.
1444
         */
1445
8.33M
        transform_decompose(&face->ft_transform, &face->horz_res,
1446
8.33M
                            &face->vert_res, &face->width, &face->height, face->ft_face->units_per_EM);
1447
1448
8.33M
        ft_error = FT_Set_Char_Size(face->ft_face, face->width, face->height,
1449
8.33M
                                    face->horz_res, face->vert_res);
1450
1451
8.33M
        if (ft_error) {
1452
            /* The code originally cleaned up the face data here, but the "top level"
1453
               font object still has references to the face data, and we've no way
1454
               to tell it it's gone. So we defer releasing the data until the garbage
1455
               collector collects the font object, and the font's finalize call will
1456
               free the data correctly for us.
1457
             */
1458
151
            return ft_to_gs_error(ft_error);
1459
151
        }
1460
1461
        /* Concatenate the transform to a reflection around (y=0) so that it
1462
         * produces a glyph that is upside down in FreeType terms, with its
1463
         * first row at the bottom. That is what GhostScript needs.
1464
         */
1465
1466
8.33M
        FT_Set_Transform(face->ft_face, &face->ft_transform, NULL);
1467
1468
8.33M
    }
1469
1470
    /* dpf("gs_fapi_ft_get_scaled_font return %d\n", a_font->server_font_data ? 0 : -1); */
1471
8.33M
    return a_font->server_font_data ? 0 : -1;
1472
8.33M
}
1473
1474
/*
1475
 * Return the name of a resource which maps names to character codes. Do this
1476
 * by setting a_decoding_id to point to a null-terminated string. The resource
1477
 * is in the 'decoding' directory in the directory named by /GenericResourceDir
1478
 * in lib/gs_res.ps.
1479
 */
1480
static gs_fapi_retcode
1481
gs_fapi_ft_get_decodingID(gs_fapi_server * a_server, gs_fapi_font * a_font,
1482
               const char **a_decoding_id)
1483
9.92k
{
1484
9.92k
    *a_decoding_id = "Unicode";
1485
9.92k
    return 0;
1486
9.92k
}
1487
1488
/*
1489
 * Get the font bounding box in font units.
1490
 */
1491
static gs_fapi_retcode
1492
gs_fapi_ft_get_font_bbox(gs_fapi_server * a_server, gs_fapi_font * a_font, int a_box[4], int unitsPerEm[2])
1493
2.47M
{
1494
2.47M
    ff_face *face = (ff_face *) a_font->server_font_data;
1495
1496
2.47M
    a_box[0] = face->ft_face->bbox.xMin;
1497
2.47M
    a_box[1] = face->ft_face->bbox.yMin;
1498
2.47M
    a_box[2] = face->ft_face->bbox.xMax;
1499
2.47M
    a_box[3] = face->ft_face->bbox.yMax;
1500
1501
2.47M
    unitsPerEm[0] = unitsPerEm[1] = face->ft_face->units_per_EM;
1502
1503
2.47M
    return 0;
1504
2.47M
}
1505
1506
/*
1507
 * Return a boolean value in a_proportional stating whether the font is proportional
1508
 * or fixed-width.
1509
 */
1510
static gs_fapi_retcode
1511
gs_fapi_ft_get_font_proportional_feature(gs_fapi_server * a_server,
1512
                              gs_fapi_font * a_font, bool * a_proportional)
1513
0
{
1514
0
    *a_proportional = true;
1515
0
    return 0;
1516
0
}
1517
1518
/* Convert the character name in a_char_ref.char_name to a character code or
1519
 * glyph index and put it in a_char_ref.char_code, setting
1520
 * a_char_ref.is_glyph_index as appropriate. If this is possible set a_result
1521
 * to true, otherwise set it to false.  The return value is a standard error
1522
 * return code.
1523
 */
1524
static gs_fapi_retcode
1525
gs_fapi_ft_can_retrieve_char_by_name(gs_fapi_server * a_server, gs_fapi_font * a_font,
1526
                          gs_fapi_char_ref * a_char_ref, bool * a_result)
1527
0
{
1528
0
    ff_face *face = (ff_face *) a_font->server_font_data;
1529
0
    char name[128];
1530
1531
0
    if (FT_HAS_GLYPH_NAMES(face->ft_face)
1532
0
        && a_char_ref->char_name_length < sizeof(name)) {
1533
0
        memcpy(name, a_char_ref->char_name, a_char_ref->char_name_length);
1534
0
        name[a_char_ref->char_name_length] = 0;
1535
0
        a_char_ref->char_codes[0] = FT_Get_Name_Index(face->ft_face, name);
1536
0
        *a_result = a_char_ref->char_codes[0] != 0;
1537
0
        if (*a_result)
1538
0
            a_char_ref->is_glyph_index = true;
1539
0
    }
1540
0
    else
1541
0
        *a_result = false;
1542
0
    return 0;
1543
0
}
1544
1545
/*
1546
 * Return non-zero if the metrics can be replaced.
1547
 */
1548
static gs_fapi_retcode
1549
gs_fapi_ft_can_replace_metrics(gs_fapi_server * a_server, gs_fapi_font * a_font,
1550
                    gs_fapi_char_ref * a_char_ref, int *a_result)
1551
11.6M
{
1552
    /* Replace metrics only if the metrics are supplied in font units. */
1553
11.6M
    *a_result = 1;
1554
11.6M
    return 0;
1555
11.6M
}
1556
1557
/*
1558
 * Retrieve the metrics of a_char_ref and put them in a_metrics.
1559
 */
1560
static gs_fapi_retcode
1561
gs_fapi_ft_get_char_width(gs_fapi_server * a_server, gs_fapi_font * a_font,
1562
               gs_fapi_char_ref * a_char_ref, gs_fapi_metrics * a_metrics)
1563
0
{
1564
0
    ff_server *s = (ff_server *) a_server;
1565
1566
0
    return load_glyph(a_server, a_font, a_char_ref, a_metrics,
1567
0
                      (FT_Glyph *) & s->outline_glyph,
1568
0
                      false, a_server->max_bitmap);
1569
0
}
1570
1571
static gs_fapi_retcode
1572
gs_fapi_ft_get_fontmatrix(gs_fapi_server * server, gs_matrix * m)
1573
21.6M
{
1574
21.6M
    m->xx = 1.0;
1575
21.6M
    m->xy = 0.0;
1576
21.6M
    m->yx = 0.0;
1577
21.6M
    m->yy = 1.0;
1578
21.6M
    m->tx = 0.0;
1579
21.6M
    m->ty = 0.0;
1580
21.6M
    return 0;
1581
21.6M
}
1582
1583
/*
1584
 * Rasterize the character a_char and return its metrics. Do not return the
1585
 * bitmap but store this. It can be retrieved by a subsequent call to
1586
 * gs_fapi_ft_get_char_raster.
1587
 */
1588
static gs_fapi_retcode
1589
gs_fapi_ft_get_char_raster_metrics(gs_fapi_server * a_server, gs_fapi_font * a_font,
1590
                        gs_fapi_char_ref * a_char_ref,
1591
                        gs_fapi_metrics * a_metrics)
1592
7.98M
{
1593
7.98M
    ff_server *s = (ff_server *) a_server;
1594
7.98M
    gs_fapi_retcode error =
1595
7.98M
        load_glyph(a_server, a_font, a_char_ref, a_metrics,
1596
7.98M
                   (FT_Glyph *) & s->bitmap_glyph, true,
1597
7.98M
                   a_server->max_bitmap);
1598
7.98M
    return error;
1599
7.98M
}
1600
1601
/*
1602
 * Return the bitmap created by the last call to gs_fapi_ft_get_char_raster_metrics.
1603
 */
1604
static gs_fapi_retcode
1605
gs_fapi_ft_get_char_raster(gs_fapi_server * a_server, gs_fapi_raster * a_raster)
1606
9.31M
{
1607
9.31M
    ff_server *s = (ff_server *) a_server;
1608
1609
9.31M
    if (!s->bitmap_glyph)
1610
4.18M
        return(gs_error_unregistered);
1611
5.12M
    a_raster->p = s->bitmap_glyph->bitmap.buffer;
1612
5.12M
    a_raster->width = s->bitmap_glyph->bitmap.width;
1613
5.12M
    a_raster->height = s->bitmap_glyph->bitmap.rows;
1614
5.12M
    a_raster->line_step = s->bitmap_glyph->bitmap.pitch;
1615
5.12M
    a_raster->orig_x = s->bitmap_glyph->left * 16;
1616
5.12M
    a_raster->orig_y = s->bitmap_glyph->top * 16;
1617
5.12M
    a_raster->left_indent = a_raster->top_indent = a_raster->black_height =
1618
5.12M
        a_raster->black_width = 0;
1619
5.12M
    return 0;
1620
9.31M
}
1621
1622
/*
1623
 * Create an outline for the character a_char and return its metrics. Do not
1624
 * return the outline but store this.
1625
 * It can be retrieved by a subsequent call to gs_fapi_ft_get_char_outline.
1626
 */
1627
static gs_fapi_retcode
1628
gs_fapi_ft_get_char_outline_metrics(gs_fapi_server * a_server, gs_fapi_font * a_font,
1629
                         gs_fapi_char_ref * a_char_ref,
1630
                         gs_fapi_metrics * a_metrics)
1631
6.49M
{
1632
6.49M
    ff_server *s = (ff_server *) a_server;
1633
1634
6.49M
    return load_glyph(a_server, a_font, a_char_ref, a_metrics,
1635
6.49M
                      (FT_Glyph *) & s->outline_glyph, false,
1636
6.49M
                      a_server->max_bitmap);
1637
6.49M
}
1638
1639
typedef struct FF_path_info_s
1640
{
1641
    gs_fapi_path *path;
1642
    int64_t x;
1643
    int64_t y;
1644
    FT_Vector currentp;
1645
} FF_path_info;
1646
1647
static inline int
1648
FF_points_equal(const FT_Vector *p1, const FT_Vector *p2)
1649
50.2M
{
1650
50.2M
    if (p1->x == p2->x && p1->y == p2->y)
1651
47.2k
        return 1;
1652
50.1M
    else
1653
50.1M
        return 0;
1654
50.2M
}
1655
1656
static int
1657
move_to(const FT_Vector * aTo, void *aObject)
1658
4.76M
{
1659
4.76M
    FF_path_info *p = (FF_path_info *) aObject;
1660
1661
4.76M
    p->currentp = *aTo;
1662
1663
    /* FAPI expects that co-ordinates will be as implied by frac_shift
1664
     * in our case 16.16 fixed precision. True for 'low level' FT
1665
     * routines (apparently), it isn't true for these routines where
1666
     * FT returns a 26.6 format. Rescale to 16.16 so that FAPI will
1667
     * be able to convert to GS co-ordinates properly.
1668
     */
1669
    /* FAPI now expects these coordinates in 32.32 */
1670
4.76M
    p->x = ((int64_t) aTo->x) << 26;
1671
4.76M
    p->y = ((int64_t) aTo->y) << 26;
1672
1673
4.76M
    return p->path->moveto(p->path, p->x, p->y) ? -1 : 0;
1674
4.76M
}
1675
1676
static int
1677
line_to(const FT_Vector * aTo, void *aObject)
1678
21.2M
{
1679
21.2M
    FF_path_info *p = (FF_path_info *) aObject;
1680
1681
21.2M
    if (!FF_points_equal(&p->currentp, aTo)) {
1682
21.1M
        p->currentp = *aTo;
1683
1684
        /* See move_to() above */
1685
21.1M
        p->x = ((int64_t) aTo->x) << 26;
1686
21.1M
        p->y = ((int64_t) aTo->y) << 26;
1687
1688
21.1M
        return p->path->lineto(p->path, p->x, p->y) ? -1 : 0;
1689
21.1M
    }
1690
19.3k
    return 0;
1691
21.2M
}
1692
1693
static int
1694
conic_to(const FT_Vector * aControl, const FT_Vector * aTo, void *aObject)
1695
6.38M
{
1696
6.38M
    FF_path_info *p = (FF_path_info *) aObject;
1697
6.38M
    double x, y, Controlx, Controly;
1698
6.38M
    int64_t Control1x, Control1y, Control2x, Control2y;
1699
6.38M
    double sx, sy;
1700
1701
6.38M
    if (!FF_points_equal(&p->currentp, aControl) ||
1702
5.83k
        !FF_points_equal(&p->currentp, aTo) ||
1703
6.38M
        !FF_points_equal(aControl, aTo)) {
1704
6.38M
        p->currentp = *aTo;
1705
1706
        /* More complicated than above, we need to do arithmetic on the
1707
         * co-ordinates, so we want them as floats and we will convert the
1708
         * result into 16.16 fixed precision for FAPI
1709
         *
1710
         * NB this code is funcitonally the same as the original, but I don't believe
1711
         * the comment (below) to be what the code is actually doing....
1712
         *
1713
         * NB2: the comment below was wrong, even though the code was correct(!!)
1714
         * The comment has now been amended.
1715
         *
1716
         * Convert a quadratic spline to a cubic. Do this by changing the three points
1717
         * A, B and C to A, 2/3(B,A), 2/3(B,C), C - that is, the two cubic control points are
1718
         * a third of the way from the single quadratic control point to the end points. This
1719
         * gives the same curve as the original quadratic.
1720
         */
1721
1722
6.38M
        sx = (double) (p->x >> 32);
1723
6.38M
        sy = (double) (p->y >> 32);
1724
1725
6.38M
        x = aTo->x / 64.0;
1726
6.38M
        p->x = ((int64_t) float2fixed(x)) << 24;
1727
6.38M
        y = aTo->y / 64.0;
1728
6.38M
        p->y = ((int64_t) float2fixed(y)) << 24;
1729
6.38M
        Controlx = aControl->x / 64.0;
1730
6.38M
        Controly = aControl->y / 64.0;
1731
1732
6.38M
        Control1x = ((int64_t) float2fixed((sx + Controlx * 2) / 3)) << 24;
1733
6.38M
        Control1y = ((int64_t) float2fixed((sy + Controly * 2) / 3)) << 24;
1734
6.38M
        Control2x = ((int64_t) float2fixed((x + Controlx * 2) / 3)) << 24;
1735
6.38M
        Control2y = ((int64_t) float2fixed((y + Controly * 2) / 3)) << 24;
1736
1737
6.38M
        return p->path->curveto(p->path, Control1x,
1738
6.38M
                                Control1y,
1739
6.38M
                                Control2x, Control2y, p->x, p->y) ? -1 : 0;
1740
6.38M
    }
1741
3.19k
    return 0;
1742
6.38M
}
1743
1744
static int
1745
cubic_to(const FT_Vector * aControl1, const FT_Vector * aControl2,
1746
         const FT_Vector * aTo, void *aObject)
1747
22.6M
{
1748
22.6M
    FF_path_info *p = (FF_path_info *) aObject;
1749
22.6M
    int64_t Control1x, Control1y, Control2x, Control2y;
1750
1751
22.6M
    if (!FF_points_equal(&p->currentp, aControl1) ||
1752
6.04k
        !FF_points_equal(&p->currentp, aControl2) ||
1753
2.83k
        !FF_points_equal(&p->currentp, aTo) ||
1754
1.71k
        !FF_points_equal(aControl1, aControl2) ||
1755
1.71k
        !FF_points_equal(aControl1, aTo) ||
1756
22.6M
        !FF_points_equal(aControl2, aTo)) {
1757
22.6M
        p->currentp = *aTo;
1758
1759
        /* See move_to() above */
1760
22.6M
        p->x = ((int64_t) aTo->x) << 26;
1761
22.6M
        p->y = ((int64_t) aTo->y) << 26;
1762
1763
22.6M
        Control1x = ((int64_t) aControl1->x) << 26;
1764
22.6M
        Control1y = ((int64_t) aControl1->y) << 26;
1765
22.6M
        Control2x = ((int64_t) aControl2->x) << 26;
1766
22.6M
        Control2y = ((int64_t) aControl2->y) << 26;
1767
22.6M
        return p->path->curveto(p->path, Control1x, Control1y, Control2x,
1768
22.6M
                                Control2y, p->x, p->y) ? -1 : 0;
1769
22.6M
    }
1770
1.71k
    return 0;
1771
22.6M
}
1772
1773
static const FT_Outline_Funcs TheFtOutlineFuncs = {
1774
    move_to,
1775
    line_to,
1776
    conic_to,
1777
    cubic_to,
1778
    0,
1779
    0
1780
};
1781
1782
/*
1783
 * Return the outline created by the last call to gs_fapi_ft_get_char_outline_metrics.
1784
 */
1785
static gs_fapi_retcode
1786
gs_fapi_ft_get_char_outline(gs_fapi_server * a_server, gs_fapi_path * a_path)
1787
4.21M
{
1788
4.21M
    ff_server *s = (ff_server *) a_server;
1789
4.21M
    FF_path_info p;
1790
4.21M
    FT_Error ft_error = 0;
1791
1792
4.21M
    p.path = a_path;
1793
4.21M
    p.x = 0;
1794
4.21M
    p.y = 0;
1795
    /* If we got an error during glyph creation, we can get
1796
     * here with s->outline_glyph == NULL
1797
     */
1798
4.21M
    if (s->outline_glyph) {
1799
4.21M
        ft_error =
1800
4.21M
            FT_Outline_Decompose(&s->outline_glyph->outline, &TheFtOutlineFuncs,
1801
4.21M
                                 &p);
1802
4.21M
    }
1803
0
    else {
1804
0
        a_path->moveto(a_path, 0, 0);
1805
0
    }
1806
1807
4.21M
    if (a_path->gs_error == 0)
1808
4.01M
        a_path->closepath(a_path);
1809
4.21M
    return ft_to_gs_error(ft_error);
1810
4.21M
}
1811
1812
static gs_fapi_retcode
1813
gs_fapi_ft_release_char_data(gs_fapi_server * a_server)
1814
11.6M
{
1815
11.6M
    ff_server *s = (ff_server *) a_server;
1816
1817
11.6M
    if (s->outline_glyph) {
1818
4.22M
        FT_Outline_Done(s->freetype_library, &s->outline_glyph->outline);
1819
4.22M
        FF_free(s->ftmemory, s->outline_glyph);
1820
4.22M
    }
1821
1822
11.6M
    if (s->bitmap_glyph) {
1823
5.12M
        FT_Bitmap_Done(s->freetype_library, &s->bitmap_glyph->bitmap);
1824
5.12M
        FF_free(s->ftmemory, s->bitmap_glyph);
1825
5.12M
    }
1826
1827
11.6M
    s->outline_glyph = NULL;
1828
11.6M
    s->bitmap_glyph = NULL;
1829
11.6M
    return 0;
1830
11.6M
}
1831
1832
static gs_fapi_retcode
1833
gs_fapi_ft_release_typeface(gs_fapi_server * a_server, void *a_server_font_data)
1834
2.55M
{
1835
2.55M
    ff_face *face = (ff_face *) a_server_font_data;
1836
1837
2.55M
    delete_face(a_server, face);
1838
2.55M
    return 0;
1839
2.55M
}
1840
1841
static gs_fapi_retcode
1842
gs_fapi_ft_check_cmap_for_GID(gs_fapi_server * server, uint * index)
1843
11.2M
{
1844
11.2M
    ff_face *face = (ff_face *) (server->ff.server_font_data);
1845
11.2M
    FT_Face ft_face = face->ft_face;
1846
1847
11.2M
    *index = FT_Get_Char_Index(ft_face, *index);
1848
11.2M
    return 0;
1849
11.2M
}
1850
1851
static gs_fapi_retcode
1852
gs_fapi_ft_set_mm_weight_vector(gs_fapi_server *server, gs_fapi_font *ff, float *wvector, int length)
1853
0
{
1854
#if defined(SHARE_FT) && SHARE_FT == 1 && \
1855
    FREETYPE_MAJOR <= 2 && FREETYPE_MINOR <= 9 && FREETYPE_PATCH <= 1
1856
1857
    return gs_error_invalidaccess;
1858
#else
1859
0
    ff_face *face = (ff_face *) ff->server_font_data;
1860
0
    FT_Fixed nwv[16] = {0};
1861
0
    FT_Fixed cwv[16] = {0};
1862
0
    FT_UInt len = 16;
1863
0
    int i;
1864
0
    bool setit = false;
1865
0
    FT_Error ft_error;
1866
0
    (void)server;
1867
1868
0
    ft_error = FT_Get_MM_WeightVector(face->ft_face, &len, cwv);
1869
0
    if (ft_error != 0) return_error(gs_error_invalidaccess);
1870
1871
0
    for (i = 0; i < length; i++) {
1872
0
        nwv[i] = (FT_Fixed)(wvector[i] * 65536.0);
1873
0
        if (nwv[i] != cwv[i]) {
1874
0
            setit = true;
1875
0
        }
1876
0
    }
1877
1878
0
    if (setit == true) {
1879
0
        ft_error = FT_Set_MM_WeightVector(face->ft_face, length, nwv);
1880
0
        if (ft_error != 0) return_error(gs_error_invalidaccess);
1881
0
    }
1882
1883
0
    return 0;
1884
0
#endif
1885
0
}
1886
1887
static void gs_fapi_freetype_destroy(gs_fapi_server ** serv);
1888
1889
static const gs_fapi_server_descriptor freetypedescriptor = {
1890
    (const char *)"FAPI",
1891
    (const char *)"FreeType",
1892
    gs_fapi_freetype_destroy
1893
};
1894
1895
static const gs_fapi_server freetypeserver = {
1896
    {&freetypedescriptor},
1897
    NULL,                       /* client_ctx_p */
1898
    16,                         /* frac_shift */
1899
    {gs_no_id},
1900
    {0},
1901
    0,
1902
    false,
1903
    false,
1904
    {1, 0, 0, 1, 0, 0},
1905
    1,
1906
    {1, 0, 0, 1, 0, 0},
1907
    gs_fapi_ft_ensure_open,
1908
    gs_fapi_ft_get_scaled_font,
1909
    gs_fapi_ft_get_decodingID,
1910
    gs_fapi_ft_get_font_bbox,
1911
    gs_fapi_ft_get_font_proportional_feature,
1912
    gs_fapi_ft_can_retrieve_char_by_name,
1913
    gs_fapi_ft_can_replace_metrics,
1914
    NULL,                       /* can_simulate_style */
1915
    gs_fapi_ft_get_fontmatrix,
1916
    gs_fapi_ft_get_char_width,
1917
    gs_fapi_ft_get_char_raster_metrics,
1918
    gs_fapi_ft_get_char_raster,
1919
    gs_fapi_ft_get_char_outline_metrics,
1920
    gs_fapi_ft_get_char_outline,
1921
    gs_fapi_ft_release_char_data,
1922
    gs_fapi_ft_release_typeface,
1923
    gs_fapi_ft_check_cmap_for_GID,
1924
    NULL,                        /* get_font_info */
1925
    gs_fapi_ft_set_mm_weight_vector,
1926
};
1927
1928
int gs_fapi_ft_init(gs_memory_t * mem, gs_fapi_server ** server);
1929
1930
int
1931
gs_fapi_ft_init(gs_memory_t * mem, gs_fapi_server ** server)
1932
170k
{
1933
170k
    ff_server *serv;
1934
170k
    int code = 0;
1935
170k
    gs_memory_t *cmem = mem->non_gc_memory;
1936
1937
170k
    code = gs_memory_chunk_wrap(&(cmem), mem);
1938
170k
    if (code != 0) {
1939
0
        return (code);
1940
0
    }
1941
1942
1943
170k
    serv = (ff_server *) gs_alloc_bytes_immovable(cmem, sizeof(ff_server), "gs_fapi_ft_init");
1944
170k
    if (!serv) {
1945
0
        gs_memory_chunk_release(cmem);
1946
0
        return_error(gs_error_VMerror);
1947
0
    }
1948
170k
    memset(serv, 0, sizeof(*serv));
1949
170k
    serv->mem = cmem;
1950
170k
    serv->fapi_server = freetypeserver;
1951
1952
170k
    serv->ftmemory = (FT_Memory) (&(serv->ftmemory_rec));
1953
1954
170k
    (*server) = (gs_fapi_server *) serv;
1955
170k
    return (0);
1956
170k
}
1957
1958
1959
void
1960
gs_fapi_freetype_destroy(gs_fapi_server ** serv)
1961
170k
{
1962
170k
    ff_server *server = (ff_server *) * serv;
1963
170k
    gs_memory_t *cmem = server->mem;
1964
1965
170k
    FT_Done_Glyph(&server->outline_glyph->root);
1966
170k
    FT_Done_Glyph(&server->bitmap_glyph->root);
1967
1968
    /* As with initialization: since we're supplying memory management to
1969
     * FT, we cannot just to use FT_Done_FreeType (), we have to use
1970
     * FT_Done_Library () and then discard the memory ourselves
1971
     */
1972
170k
    FT_Done_Library(server->freetype_library);
1973
170k
    gs_free(cmem, *serv, 0, 0, "gs_fapi_freetype_destroy: ff_server");
1974
    *serv = NULL;
1975
170k
    gs_memory_chunk_release(cmem);
1976
170k
}