Coverage Report

Created: 2026-07-24 07:44

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
215M
{
132
215M
    gs_memory_t *mem = (gs_memory_t *) memory->user;
133
134
215M
    return (gs_malloc(mem, size, 1, "FF_alloc"));
135
215M
}
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.72M
        return (block);
145
2.72M
    }
146
147
7.03M
    tmp = gs_malloc(mem, new_size, 1, "FF_realloc");
148
7.03M
    if (tmp && block) {
149
7.03M
        memcpy(tmp, block, min(cur_size, new_size));
150
151
7.03M
        gs_free(mem, block, 0, 0, "FF_realloc");
152
7.03M
    }
153
154
7.03M
    return (tmp);
155
9.75M
}
156
157
static void
158
    FF_free(FT_Memory memory, void *block)
159
222M
{
160
222M
    gs_memory_t *mem = (gs_memory_t *) memory->user;
161
162
222M
    gs_free(mem, block, 0, 0, "FF_free");
163
222M
}
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
87.9M
        status = sgets(ps, buffer, count, &rlen);
183
184
87.9M
        if (status < 0 && status != EOFC)
185
0
            return_error (-1);
186
87.9M
    }
187
144M
    return (rlen);
188
144M
}
189
190
static void
191
FF_stream_close(FT_Stream str)
192
1.95M
{
193
1.95M
    stream *ps = (stream *) str->descriptor.pointer;
194
1.95M
    gs_memory_t *mem = ps->memory;
195
196
1.95M
    (void)sclose(ps);
197
1.95M
    gs_free_object(mem, ps, "FF_stream_close");
198
1.95M
}
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.95M
{
205
1.95M
    int code = 0;
206
1.95M
    gs_parsed_file_name_t pfn;
207
1.95M
    stream *ps = (stream *)NULL;
208
1.95M
    gs_offset_t length;
209
1.95M
    FT_Stream ftstrm = NULL;
210
211
1.95M
    code = gs_parse_file_name(&pfn, (const char *)fname, strlen(fname), mem);
212
1.95M
    if (code < 0) {
213
0
        goto error_out;
214
0
    }
215
216
1.95M
    if (!pfn.fname) {
217
0
        code = gs_error_undefinedfilename;
218
0
        goto error_out;
219
0
    }
220
221
1.95M
    if (pfn.iodev == NULL) {
222
0
        pfn.iodev = iodev_default(mem);
223
0
    }
224
225
1.95M
    if (pfn.iodev) {
226
1.95M
        gx_io_device *const iodev = pfn.iodev;
227
228
1.95M
        iodev_proc_open_file((*open_file)) = iodev->procs.open_file;
229
230
1.95M
        if (open_file) {
231
1.95M
            code = open_file(iodev, pfn.fname, pfn.len, "r", &ps, mem);
232
1.95M
            if (code < 0) {
233
0
                goto error_out;
234
0
            }
235
1.95M
        }
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.95M
    }
246
0
    else {
247
0
        goto error_out;
248
0
    }
249
250
1.95M
    if ((code = savailable(ps, &length)) < 0) {
251
0
        goto error_out;
252
0
    }
253
254
1.95M
    ftstrm = gs_malloc(mem, sizeof(FT_StreamRec), 1, "FF_open_read_stream");
255
1.95M
    if (!ftstrm) {
256
0
        code = gs_error_VMerror;
257
0
        goto error_out;
258
0
    }
259
1.95M
    memset(ftstrm, 0x00, sizeof(FT_StreamRec));
260
261
1.95M
    ftstrm->descriptor.pointer = ps;
262
1.95M
    ftstrm->read = FF_stream_read;
263
1.95M
    ftstrm->close = FF_stream_close;
264
1.95M
    ftstrm->size = (long)length;
265
1.95M
    *fts = ftstrm;
266
267
1.95M
  error_out:
268
1.95M
    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.95M
    return (code);
275
1.95M
}
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.62M
{
283
2.62M
    ff_server *s = (ff_server *) a_server;
284
285
2.62M
    ff_face *face = (ff_face *) FF_alloc(s->ftmemory, sizeof(ff_face));
286
287
2.62M
    if (face) {
288
2.62M
        face->ft_face = a_ft_face;
289
2.62M
        face->ft_inc_int = a_ft_inc_int;
290
2.62M
        face->font_data = a_font_data;
291
2.62M
        face->font_data_len = a_font_data_len;
292
2.62M
        face->data_owned = data_owned;
293
2.62M
        face->ftstrm = ftstrm;
294
2.62M
        face->server = (ff_server *) a_server;
295
2.62M
    }
296
2.62M
    return face;
297
2.62M
}
298
299
static void
300
delete_face(gs_fapi_server * a_server, ff_face * a_face)
301
2.62M
{
302
2.62M
    if (a_face) {
303
2.62M
        ff_server *s = (ff_server *) a_server;
304
2.62M
        if (a_face->ft_inc_int) {
305
655k
            FT_Incremental a_info = a_face->ft_inc_int->object;
306
307
655k
            if (a_info->glyph_data) {
308
345k
                gs_free(s->mem, a_info->glyph_data, 0, 0, "delete_face");
309
345k
            }
310
655k
            a_info->glyph_data = NULL;
311
655k
            a_info->glyph_data_length = 0;
312
655k
            delete_inc_int(a_server, a_face->ft_inc_int);
313
655k
            a_face->ft_inc_int = NULL;
314
655k
        }
315
2.62M
        FT_Done_Face(a_face->ft_face);
316
317
2.62M
        FF_free(s->ftmemory, a_face->ft_inc_int);
318
2.62M
        if (a_face->data_owned)
319
2.60M
            FF_free(s->ftmemory, a_face->font_data);
320
2.62M
        if (a_face->ftstrm) {
321
1.95M
            FF_free(s->ftmemory, a_face->ftstrm);
322
1.95M
        }
323
2.62M
        FF_free(s->ftmemory, a_face);
324
2.62M
    }
325
2.62M
}
326
327
static FT_IncrementalRec *
328
new_inc_int_info(gs_fapi_server * a_server, gs_fapi_font * a_fapi_font)
329
658k
{
330
658k
    ff_server *s = (ff_server *) a_server;
331
332
658k
    FT_IncrementalRec *info =
333
658k
        (FT_IncrementalRec *) FF_alloc(s->ftmemory,
334
658k
                                       sizeof(FT_IncrementalRec));
335
658k
    if (info) {
336
658k
        info->fapi_font = a_fapi_font;
337
658k
        info->glyph_data = NULL;
338
658k
        info->glyph_data_length = 0;
339
658k
        info->glyph_data_in_use = false;
340
658k
        info->glyph_metrics_index = 0xFFFFFFFF;
341
658k
        info->metrics_type = gs_fapi_metrics_notdef;
342
658k
    }
343
658k
    return info;
344
658k
}
345
346
static void
347
delete_inc_int_info(gs_fapi_server * a_server,
348
                    FT_IncrementalRec * a_inc_int_info)
349
658k
{
350
658k
    ff_server *s = (ff_server *) a_server;
351
352
658k
    if (a_inc_int_info) {
353
658k
        FF_free(s->ftmemory, a_inc_int_info->glyph_data);
354
658k
        FF_free(s->ftmemory, a_inc_int_info);
355
658k
    }
356
658k
}
357
358
static FT_Error
359
get_fapi_glyph_data(FT_Incremental a_info, FT_UInt a_index, FT_Data * a_data)
360
10.6M
{
361
10.6M
    gs_fapi_font *ff = a_info->fapi_font;
362
10.6M
    int length = 0;
363
10.6M
    ff_face *face = (ff_face *) ff->server_font_data;
364
10.6M
    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.6M
    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.6M
    if (a_info->glyph_data_in_use) {
373
26.8k
        unsigned char *buffer = NULL;
374
375
26.8k
        length = ff->get_glyph(ff, a_index, NULL, 0);
376
26.8k
        if (length == gs_fapi_glyph_invalid_format
377
26.8k
            || length == gs_fapi_glyph_invalid_index)
378
0
            return FT_Err_Invalid_Glyph_Index;
379
380
26.8k
        buffer = gs_malloc(mem, length, 1, "get_fapi_glyph_data");
381
26.8k
        if (!buffer)
382
1.17k
            return FT_Err_Out_Of_Memory;
383
384
25.7k
        length = ff->get_glyph(ff, a_index, buffer, length);
385
25.7k
        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
25.7k
        a_data->pointer = buffer;
391
25.7k
    }
392
10.5M
    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.5M
        const void *saved_char_data = ff->char_data;
398
399
        /* Get as much of the glyph data as possible into the buffer */
400
10.5M
        length =
401
10.5M
            ff->get_glyph(ff, a_index, a_info->glyph_data,
402
10.5M
                          (ushort) a_info->glyph_data_length);
403
10.5M
        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.5M
        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.5M
        if (length > a_info->glyph_data_length) {
415
823k
            if (a_info->glyph_data) {
416
476k
                gs_free((gs_memory_t *) mem,
417
476k
                        a_info->glyph_data, 0, 0, "get_fapi_glyph_data");
418
476k
            }
419
420
823k
            a_info->glyph_data =
421
823k
                gs_malloc(mem, length, 1, "get_fapi_glyph_data");
422
423
823k
            if (!a_info->glyph_data) {
424
1.26k
                a_info->glyph_data_length = 0;
425
1.26k
                return FT_Err_Out_Of_Memory;
426
1.26k
            }
427
822k
            a_info->glyph_data_length = length;
428
822k
            ff->char_data = saved_char_data;
429
822k
            length = ff->get_glyph(ff, a_index, a_info->glyph_data, length);
430
822k
            if (length == gs_fapi_glyph_invalid_format)
431
0
                return FT_Err_Unknown_File_Format;
432
822k
            if (length == gs_fapi_glyph_invalid_index)
433
0
                return FT_Err_Invalid_Glyph_Index;
434
822k
        }
435
436
        /* Set the returned pointer and length. */
437
10.5M
        a_data->pointer = a_info->glyph_data;
438
439
10.5M
        a_info->glyph_data_in_use = true;
440
10.5M
    }
441
442
10.6M
    a_data->length = length;
443
10.6M
    return 0;
444
10.6M
}
445
446
static void
447
free_fapi_glyph_data(FT_Incremental a_info, FT_Data * a_data)
448
10.6M
{
449
10.6M
    gs_fapi_font *ff = a_info->fapi_font;
450
10.6M
    ff_face *face = (ff_face *) ff->server_font_data;
451
10.6M
    gs_memory_t *mem = (gs_memory_t *) face->server->mem;
452
453
10.6M
    if (a_data->pointer == (const FT_Byte *)a_info->glyph_data)
454
10.5M
        a_info->glyph_data_in_use = false;
455
25.7k
    else
456
10.6M
        gs_free(mem, (FT_Byte *) a_data->pointer, 0, 0, "free_fapi_glyph_data");
457
10.6M
}
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.4M
{
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.4M
    if (bVertical && !a_info->fapi_font->is_type1)
469
841k
        a_metrics->advance = 0;
470
471
11.4M
    if (a_info->glyph_metrics_index == a_glyph_index) {
472
438k
        switch (a_info->metrics_type) {
473
438k
            case gs_fapi_metrics_add:
474
438k
                a_metrics->advance += a_info->glyph_metrics.advance;
475
438k
                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
438k
        }
488
438k
    }
489
11.4M
    return 0;
490
11.4M
}
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
658k
{
501
658k
    ff_server *s = (ff_server *) a_server;
502
503
658k
    FT_Incremental_InterfaceRec *i =
504
658k
        (FT_Incremental_InterfaceRec *) FF_alloc(s->ftmemory,
505
658k
                                                 sizeof
506
658k
                                                 (FT_Incremental_InterfaceRec));
507
658k
    if (i) {
508
658k
        i->funcs = &TheFAPIIncrementalInterfaceFuncs;
509
658k
        i->object = (FT_Incremental) new_inc_int_info(a_server, a_fapi_font);
510
511
658k
        if (!i->object) {
512
0
            FF_free(s->ftmemory, i);
513
0
            i = NULL;
514
0
        }
515
658k
    }
516
658k
    return i;
517
658k
}
518
519
static void
520
delete_inc_int(gs_fapi_server * a_server,
521
               FT_Incremental_InterfaceRec * a_inc_int)
522
658k
{
523
658k
    ff_server *s = (ff_server *) a_server;
524
525
658k
    if (a_inc_int) {
526
658k
        delete_inc_int_info(a_server, a_inc_int->object);
527
658k
        FF_free(s->ftmemory, a_inc_int);
528
658k
    }
529
658k
}
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
16.2M
{
537
16.2M
    if (a_error) {
538
211k
        if (a_error == FT_Err_Out_Of_Memory)
539
0
            return_error(gs_error_VMerror);
540
211k
        else
541
211k
            return_error(gs_error_unknownerror);
542
211k
    }
543
16.0M
    return 0;
544
16.2M
}
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.6M
{
554
14.6M
    ff_server *s = (ff_server *) a_server;
555
14.6M
    FT_Error ft_error = 0;
556
14.6M
    FT_Error ft_error_fb = 1;
557
14.6M
    ff_face *face = (ff_face *) a_fapi_font->server_font_data;
558
14.6M
    FT_Face ft_face = face->ft_face;
559
14.6M
    int index = a_char_ref->char_codes[0];
560
14.6M
    FT_Long w;
561
14.6M
    FT_Long h;
562
14.6M
    FT_Long fflags;
563
14.6M
    FT_Int32 load_flags = 0;
564
14.6M
    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.6M
    const void *saved_char_data = a_fapi_font->char_data;
571
14.6M
    const int saved_char_data_len = a_fapi_font->char_data_len;
572
573
14.6M
    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.6M
    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.6M
    if (!a_char_ref->is_glyph_index) {
585
10.0M
        if (ft_face->num_charmaps)
586
9.99M
            index = FT_Get_Char_Index(ft_face, index);
587
61.3k
        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.3k
            if (a_fapi_font->is_type1)
600
0
                index = 0;
601
61.3k
            else
602
61.3k
                index = a_char_ref->char_codes[0];
603
61.3k
        }
604
10.0M
    }
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.08M
            (index == 0 ||
613
3.08M
            (a_char_ref->client_char_code != gs_no_char &&
614
3.08M
            FT_Get_Char_Index(ft_face, a_char_ref->client_char_code) <= 0))) {
615
63.6k
            int tmp_ind;
616
617
63.6k
            if ((tmp_ind = FT_Get_Char_Index(ft_face, 32)) > 0) {
618
0
                index = tmp_ind;
619
0
            }
620
63.6k
        }
621
4.57M
    }
622
    /* Refresh the pointer to the FAPI_font held by the incremental interface. */
623
14.6M
    if (face->ft_inc_int)
624
10.4M
        face->ft_inc_int->object->fapi_font = a_fapi_font;
625
626
    /* Store the overriding metrics if they have been supplied. */
627
14.6M
    if (face->ft_inc_int && a_char_ref->metrics_type != gs_fapi_metrics_notdef) {
628
629
439k
        FT_Incremental_MetricsRec *m = &face->ft_inc_int->object->glyph_metrics;
630
631
439k
        m->bearing_x = a_char_ref->sb_x >> 16;
632
439k
        m->bearing_y = a_char_ref->sb_y >> 16;
633
439k
        m->advance = a_char_ref->aw_x >> 16;
634
635
439k
        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
439k
        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
439k
        else {
654
439k
            face->ft_inc_int->object->metrics_type = a_char_ref->metrics_type;
655
439k
        }
656
439k
    }
657
14.1M
    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
10.0M
        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.6M
    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.6M
        a_fapi_font->char_data = saved_char_data;
667
14.6M
        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.98M
            if (a_server->grid_fit == 0) {
673
0
                load_flags = FT_LOAD_NO_HINTING | FT_LOAD_NO_AUTOHINT;
674
0
            }
675
4.98M
            else if (a_server->grid_fit == 2) {
676
0
                load_flags = FT_LOAD_FORCE_AUTOHINT;
677
0
            }
678
4.98M
            load_flags |= FT_LOAD_MONOCHROME | FT_LOAD_NO_BITMAP | FT_LOAD_LINEAR_DESIGN | FT_LOAD_PEDANTIC;
679
4.98M
        }
680
9.64M
        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.64M
            load_flags |= FT_LOAD_MONOCHROME | FT_LOAD_NO_HINTING | FT_LOAD_NO_BITMAP | FT_LOAD_LINEAR_DESIGN;
684
9.64M
        }
685
686
14.6M
        ft_error = FT_Load_Glyph(ft_face, index, load_flags);
687
14.6M
        if (ft_error == FT_Err_Unknown_File_Format) {
688
0
            return index + 1;
689
0
        }
690
14.6M
    }
691
692
14.6M
    if (ft_error == FT_Err_Invalid_Argument
693
14.6M
        || ft_error == FT_Err_Invalid_Reference
694
14.5M
        || ft_error == FT_Err_Invalid_Glyph_Index
695
14.5M
        || ft_error == FT_Err_DEF_In_Glyf_Bytecode
696
14.5M
        || (ft_error >= FT_Err_Invalid_Opcode
697
555k
            && ft_error <= FT_Err_Too_Many_Instruction_Defs)) {
698
699
555k
        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
555k
        fflags = ft_face->face_flags;
703
555k
        ft_face->face_flags &= ~FT_FACE_FLAG_TRICKY;
704
555k
        load_flags |= FT_LOAD_NO_HINTING;
705
555k
        ft_error = FT_Load_Glyph(ft_face, index, load_flags);
706
707
555k
        ft_face->face_flags = fflags;
708
555k
    }
709
710
14.6M
    if (ft_error == FT_Err_Out_Of_Memory
711
14.6M
        || ft_error == FT_Err_Array_Too_Large) {
712
1.36k
        return (gs_error_VMerror);
713
1.36k
    }
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.6M
    if (ft_error) {
717
155k
        gs_string notdef_str;
718
719
155k
        notdef_str.data = (byte *)".notdef";
720
155k
        notdef_str.size = 7;
721
722
155k
        a_fapi_font->char_data = (void *)(&notdef_str);
723
155k
        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
155k
        fflags = ft_face->face_flags;
727
155k
        ft_face->face_flags &= ~FT_FACE_FLAG_TRICKY;
728
729
155k
        ft_error_fb = FT_Load_Glyph(ft_face, 0, load_flags);
730
731
155k
        ft_face->face_flags = fflags;
732
733
155k
        a_fapi_font->char_data = saved_char_data;
734
155k
        a_fapi_font->char_data_len = saved_char_data_len;
735
155k
    }
736
737
14.6M
    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.6M
    if ((!ft_error || !ft_error_fb) && a_metrics) {
745
14.6M
        FT_Long hx;
746
14.6M
        FT_Long hy;
747
14.6M
        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.6M
        hx = (FT_Long) (((double)ft_face->glyph->metrics.horiBearingX *
753
14.6M
                         ft_face->units_per_EM * 72.0) /
754
14.6M
                        ((double)face->width * face->horz_res))  + (a_fapi_font->is_mtx_skipped == 1 ? 0 : a_char_ref->sb_x >> 16);
755
14.6M
        hy = (FT_Long) (((double)ft_face->glyph->metrics.horiBearingY *
756
14.6M
                         ft_face->units_per_EM * 72.0) /
757
14.6M
                        ((double)face->height * face->vert_res)) + (a_fapi_font->is_mtx_skipped == 1 ? 0 : a_char_ref->sb_y >> 16);
758
759
14.6M
        w = (FT_Long) (((double)ft_face->glyph->metrics.width *
760
14.6M
                        ft_face->units_per_EM * 72.0) / ((double)face->width *
761
14.6M
                                                         face->horz_res));
762
14.6M
        h = (FT_Long) (((double)ft_face->glyph->metrics.height *
763
14.6M
                        ft_face->units_per_EM * 72.0) /
764
14.6M
                       ((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.6M
        if (a_fapi_font->is_type1
772
4.97M
           || ((a_fapi_font->full_font_buf || a_fapi_font->font_file_path)
773
9.64M
           && a_fapi_font->is_vertical &&  FT_HAS_VERTICAL(ft_face))) {
774
775
9.64M
            vadv = ft_face->glyph->linearVertAdvance;
776
9.64M
        }
777
4.97M
        else {
778
4.97M
            vadv = 0;
779
4.97M
        }
780
781
14.6M
        a_metrics->bbox_x0 = hx;
782
14.6M
        a_metrics->bbox_y0 = hy - h;
783
14.6M
        a_metrics->bbox_x1 = a_metrics->bbox_x0 + w;
784
14.6M
        a_metrics->bbox_y1 = a_metrics->bbox_y0 + h;
785
14.6M
        a_metrics->escapement = ft_face->glyph->linearHoriAdvance;
786
14.6M
        a_metrics->v_escapement = vadv;
787
14.6M
        a_metrics->em_x = ft_face->units_per_EM;
788
14.6M
        a_metrics->em_y = ft_face->units_per_EM;
789
14.6M
    }
790
791
14.6M
    if ((!ft_error || !ft_error_fb)) {
792
793
14.6M
        FT_BBox cbox;
794
795
        /* compute the control box, and grid fit it - lifted from ft_raster1_render() */
796
14.6M
        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.6M
        cbox.xMin = ((cbox.xMin) & ~63);        /* FT_PIX_FLOOR( cbox.xMin ) */
803
14.6M
        cbox.yMin = ((cbox.yMin) & ~63);
804
14.6M
        cbox.xMax = (((cbox.xMax) + 63) & ~63);
805
14.6M
        cbox.yMax = (((cbox.yMax) + 63) & ~63); /* FT_PIX_CEIL( cbox.yMax ) */
806
807
14.6M
        w = (FT_UInt) ((cbox.xMax - cbox.xMin) >> 6);
808
14.6M
        h = (FT_UInt) ((cbox.yMax - cbox.yMin) >> 6);
809
810
14.6M
        if (!a_fapi_font->metrics_only && a_bitmap == true && ft_face->glyph->format != FT_GLYPH_FORMAT_BITMAP
811
7.92M
            && ft_face->glyph->format != FT_GLYPH_FORMAT_COMPOSITE) {
812
7.92M
            if ((bitmap_raster(w) * h) < max_bitmap) {
813
5.08M
                FT_Render_Mode mode = FT_RENDER_MODE_MONO;
814
5.08M
                FT_UInt save_x_ppem = ft_face->glyph->face->size->metrics.x_ppem;
815
5.08M
                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.08M
                if (a_fapi_font->is_type1) {
819
3.18M
                    ft_face->glyph->face->size->metrics.x_ppem = ft_face->glyph->face->size->metrics.y_ppem = 1000;
820
3.18M
                }
821
1.89M
                else {
822
1.89M
                    ft_face->glyph->face->size->metrics.x_ppem = ft_face->glyph->face->size->metrics.y_ppem = 2048;
823
1.89M
                }
824
5.08M
                ft_error = FT_Render_Glyph(ft_face->glyph, mode);
825
826
5.08M
                ft_face->glyph->face->size->metrics.x_ppem = save_x_ppem;
827
5.08M
                ft_face->glyph->face->size->metrics.y_ppem = save_y_ppem;
828
829
5.08M
                if (ft_error != 0) {
830
75
                    (*a_glyph) = NULL;
831
75
                    return (gs_error_VMerror);
832
75
                }
833
5.08M
            }
834
2.84M
            else {
835
2.84M
                (*a_glyph) = NULL;
836
2.84M
                return (gs_error_VMerror);
837
2.84M
            }
838
7.92M
        }
839
14.6M
    }
840
841
11.7M
    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.56M
        ft_face->glyph->advance.x = ft_face->glyph->advance.y = 0;
848
9.56M
        if ((!ft_error || !ft_error_fb) && a_glyph) {
849
9.55M
            ft_error = FT_Get_Glyph(ft_face->glyph, a_glyph);
850
9.55M
        }
851
11.2k
        else {
852
11.2k
            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.2k
            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.2k
        }
871
9.56M
    }
872
873
11.7M
    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
246
        if (!ft_error_fb)
887
0
            ft_error = 0;
888
246
    }
889
11.7M
    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.65k
        if (!ft_error_fb)
903
0
            ft_error = 0;
904
3.65k
    }
905
11.7M
    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.7M
    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.7M
    return ft_to_gs_error(ft_error);
938
14.6M
}
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.63M
{
949
2.63M
    ff_server *s = (ff_server *) a_server;
950
2.63M
    FT_UInt tt_ins_version = TT_INTERPRETER_VERSION_35;
951
2.63M
    FT_Error ft_error;
952
953
2.63M
    if (s->freetype_library)
954
2.55M
        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
75.6k
    s->ftmemory->user = s->mem;
960
75.6k
    s->ftmemory->alloc = FF_alloc;
961
75.6k
    s->ftmemory->free = FF_free;
962
75.6k
    s->ftmemory->realloc = FF_realloc;
963
964
75.6k
    ft_error = FT_New_Library(s->ftmemory, &s->freetype_library);
965
75.6k
    if (ft_error)
966
0
        return ft_to_gs_error(ft_error);
967
968
75.6k
    FT_Add_Default_Modules(s->freetype_library);
969
75.6k
    FT_Property_Set( s->freetype_library, "truetype", "interpreter-version", &tt_ins_version);
970
971
75.6k
    return 0;
972
75.6k
}
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.49M
{
1012
8.49M
    double scalex, scaley, fact = 1.0;
1013
8.49M
    double factx = 1.0, facty = 1.0;
1014
8.49M
    FT_Matrix ftscale_mat;
1015
8.49M
    FT_UInt xres;
1016
8.49M
    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.49M
    double upe = units_per_EM > 512 ? (float)units_per_EM : 512.0;
1023
1024
8.49M
    scalex = hypot((double)a_transform->xx, (double)a_transform->xy);
1025
8.49M
    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.49M
    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.49M
    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.49M
    else {
1071
        /* Life is considerably easier when square resolutions are in use! */
1072
8.49M
        xres = (FT_UInt)(*xresp / factx);
1073
8.49M
        yres = (FT_UInt)(*yresp / facty);
1074
8.49M
    }
1075
1076
8.49M
    scalex *= 1.0 / 65536.0;
1077
8.49M
    scaley *= 1.0 / 65536.0;
1078
1079
8.49M
    if (scalex < scaley) {
1080
79.2k
        scaley = scalex;
1081
79.2k
    }
1082
8.41M
    else if (scalex > scaley) {
1083
46.5k
        scalex = scaley;
1084
46.5k
    }
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.49M
    if (scalex < 1.0) {
1097
512k
        fact = 1.016 / scalex;
1098
512k
        scalex = scalex * fact;
1099
512k
        scaley = scaley * fact;
1100
512k
    }
1101
1102
    /* see above */
1103
8.49M
    if (scalex * xres < 2268.0 / 64.0) {
1104
2.50k
        fact = (2400.0 / 64.0) / (xres * scalex);
1105
2.50k
        scaley *= fact;
1106
2.50k
        scalex *= fact;
1107
2.50k
    }
1108
1109
    /* see above */
1110
8.49M
    fact = 1.0;
1111
12.5M
    while (scaley * yres > (double)upe * 72.0 && (xres > 0 && yres > 0)
1112
4.01M
           && (scalex > 0.0 && scaley > 0.0)) {
1113
4.01M
        if (scaley < yres) {
1114
1.97k
            xres >>= 1;
1115
1.97k
            yres >>= 1;
1116
1.97k
            fact *= 2.0;
1117
1.97k
        }
1118
4.01M
        else {
1119
4.01M
            scalex /= 1.25;
1120
4.01M
            scaley /= 1.25;
1121
4.01M
        }
1122
4.01M
    }
1123
1124
8.49M
    ftscale_mat.xx = (FT_Fixed) ((65536.0 / scalex) * fact);
1125
8.49M
    ftscale_mat.xy = 0;
1126
8.49M
    ftscale_mat.yx = 0;
1127
8.49M
    ftscale_mat.yy = (FT_Fixed) ((65536.0 / scaley) * fact);
1128
1129
8.49M
    FT_Matrix_Multiply(a_transform, &ftscale_mat);
1130
8.49M
    memcpy(a_transform, &ftscale_mat, sizeof(FT_Matrix));
1131
1132
8.49M
    *xresp = xres;
1133
8.49M
    *yresp = yres;
1134
    /* Return values ready scaled for FT */
1135
8.49M
    *a_x_scale = (FT_Fixed) (scalex * 64);
1136
8.49M
    *a_y_scale = (FT_Fixed) (scaley * 64);
1137
8.49M
}
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.49M
{
1147
8.49M
    ff_server *s = (ff_server *) a_server;
1148
8.49M
    ff_face *face = (ff_face *) a_font->server_font_data;
1149
8.49M
    FT_Error ft_error = 0;
1150
8.49M
    int i, j, code;
1151
8.49M
    FT_CharMap cmap = NULL;
1152
8.49M
    bool data_owned = true;
1153
1154
8.49M
    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.49M
    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.49M
    if (a_font->is_cid && a_font->is_type1 && a_font->font_file_path == NULL
1173
6.72k
        && (a_descendant_code == gs_fapi_toplevel_begin
1174
5.15k
            || a_descendant_code == gs_fapi_toplevel_complete)) {
1175
        /* dpf("gs_fapi_ft_get_scaled_font return 0\n"); */
1176
3.09k
        return 0;
1177
3.09k
    }
1178
1179
    /* Create the face if it doesn't already exist. */
1180
8.49M
    if (!face) {
1181
2.62M
        FT_Face ft_face = NULL;
1182
2.62M
        FT_Parameter ft_param;
1183
2.62M
        FT_Incremental_InterfaceRec *ft_inc_int = NULL;
1184
2.62M
        unsigned char *own_font_data = NULL;
1185
2.62M
        int own_font_data_len = -1;
1186
2.62M
        FT_Stream ft_strm = NULL;
1187
1188
        /* dpf("gs_fapi_ft_get_scaled_font creating face\n"); */
1189
1190
2.62M
        if (a_font->full_font_buf) {
1191
1192
12.9k
            own_font_data =
1193
12.9k
                gs_malloc(((gs_memory_t *) (s->ftmemory->user)),
1194
12.9k
                          a_font->full_font_buf_len, 1,
1195
12.9k
                          "gs_fapi_ft_get_scaled_font - full font buf");
1196
12.9k
            if (!own_font_data) {
1197
0
                return_error(gs_error_VMerror);
1198
0
            }
1199
1200
12.9k
            own_font_data_len = a_font->full_font_buf_len;
1201
12.9k
            memcpy(own_font_data, a_font->full_font_buf,
1202
12.9k
                   a_font->full_font_buf_len);
1203
1204
12.9k
            ft_error =
1205
12.9k
                FT_New_Memory_Face(s->freetype_library,
1206
12.9k
                                   (const FT_Byte *)own_font_data,
1207
12.9k
                                   own_font_data_len, a_font->subfont,
1208
12.9k
                                   &ft_face);
1209
1210
12.9k
            if (ft_error) {
1211
886
                gs_memory_t *mem = (gs_memory_t *) s->ftmemory->user;
1212
886
                gs_free(mem, own_font_data, 0, 0, "gs_fapi_ft_get_scaled_font");
1213
886
                return ft_to_gs_error(ft_error);
1214
886
            }
1215
12.9k
        }
1216
        /* Load a typeface from a file. */
1217
2.61M
        else if (a_font->font_file_path) {
1218
1.95M
            FT_Open_Args args;
1219
1220
1.95M
            memset(&args, 0x00, sizeof(args));
1221
1222
1.95M
            if ((code =
1223
1.95M
                 FF_open_read_stream((gs_memory_t *) (s->ftmemory->user),
1224
1.95M
                                     (char *)a_font->font_file_path,
1225
1.95M
                                     &ft_strm)) < 0) {
1226
0
                return (code);
1227
0
            }
1228
1229
1.95M
            args.flags = FT_OPEN_STREAM;
1230
1.95M
            args.stream = ft_strm;
1231
1232
1.95M
            ft_error =
1233
1.95M
                FT_Open_Face(s->freetype_library, &args, a_font->subfont,
1234
1.95M
                             &ft_face);
1235
1.95M
            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.95M
        }
1243
1244
        /* Load a typeface from a representation in GhostScript's memory. */
1245
658k
        else {
1246
658k
            FT_Open_Args open_args;
1247
1248
658k
            open_args.flags = FT_OPEN_MEMORY;
1249
658k
            open_args.stream = NULL;
1250
1251
658k
            if (a_font->is_type1) {
1252
636k
                long length;
1253
636k
                unsigned short type = 0;
1254
1255
636k
                code = a_font->get_word(a_font, gs_fapi_font_feature_FontType, 0, &type);
1256
636k
                if (code < 0) {
1257
0
                    return code;
1258
0
                }
1259
1260
                /* Tell the FAPI interface that we need to decrypt the /Subrs data. */
1261
636k
                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
636k
                if (type == 1)
1268
625k
                    length = gs_fapi_serialize_type1_font(a_font, NULL, 0);
1269
10.5k
                else
1270
10.5k
                    length = gs_fapi_serialize_type2_font(a_font, NULL, 0);
1271
1272
636k
                open_args.memory_base = own_font_data =
1273
636k
                    FF_alloc(s->ftmemory, length);
1274
636k
                if (!open_args.memory_base)
1275
58
                    return_error(gs_error_VMerror);
1276
636k
                own_font_data_len = length;
1277
636k
                if (type == 1)
1278
625k
                    open_args.memory_size =
1279
625k
                        gs_fapi_serialize_type1_font(a_font, own_font_data, length);
1280
10.5k
                else
1281
10.5k
                    open_args.memory_size =
1282
10.5k
                        gs_fapi_serialize_type2_font(a_font, own_font_data, length);
1283
1284
636k
                if (open_args.memory_size != length)
1285
0
                    return_error(gs_error_unregistered);        /* Must not happen. */
1286
1287
636k
                ft_inc_int = new_inc_int(a_server, a_font);
1288
636k
                if (!ft_inc_int) {
1289
0
                    FF_free(s->ftmemory, own_font_data);
1290
0
                    return_error(gs_error_VMerror);
1291
0
                }
1292
636k
            }
1293
1294
            /* It must be type 42 (see code in FAPI_FF_get_glyph in zfapi.c). */
1295
21.5k
            else {
1296
                /* Get the length of the TrueType data. */
1297
1298
21.5k
                if (a_font->retrieve_tt_font != NULL) {
1299
21.4k
                    unsigned int ms;
1300
21.4k
                    code = a_font->retrieve_tt_font(a_font, (void **)&own_font_data, &ms);
1301
21.4k
                    if (code == 0) {
1302
21.4k
                        data_owned = false;
1303
21.4k
                        open_args.memory_base = own_font_data;
1304
21.4k
                        open_args.memory_size = own_font_data_len = ms;
1305
21.4k
                    }
1306
21.4k
                }
1307
126
                else
1308
126
                    code = gs_error_unregistered;
1309
1310
21.5k
                if (code < 0) {
1311
126
                    unsigned long lms;
1312
126
                    code = a_font->get_long(a_font, gs_fapi_font_feature_TT_size, 0, &lms);
1313
126
                    if (code < 0)
1314
0
                        return code;
1315
126
                    if (lms == 0)
1316
0
                        return_error(gs_error_invalidfont);
1317
1318
126
                    open_args.memory_size = (FT_Long)lms;
1319
1320
                    /* Load the TrueType data into a single buffer. */
1321
126
                    open_args.memory_base = own_font_data =
1322
126
                        FF_alloc(s->ftmemory, open_args.memory_size);
1323
126
                    if (!own_font_data)
1324
0
                        return_error(gs_error_VMerror);
1325
1326
126
                    own_font_data_len = open_args.memory_size;
1327
1328
126
                    code = a_font->serialize_tt_font(a_font, own_font_data,
1329
126
                                          open_args.memory_size);
1330
126
                    if (code < 0) {
1331
40
                        FF_free(s->ftmemory, own_font_data);
1332
40
                        return code;
1333
40
                    }
1334
126
                }
1335
1336
                /* We always load incrementally. */
1337
21.5k
                ft_inc_int = new_inc_int(a_server, a_font);
1338
21.5k
                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
21.5k
            }
1344
1345
658k
            if (ft_inc_int) {
1346
658k
                open_args.flags =
1347
658k
                    (FT_UInt) (open_args.flags | FT_OPEN_PARAMS);
1348
658k
                ft_param.tag = FT_PARAM_TAG_INCREMENTAL;
1349
658k
                ft_param.data = ft_inc_int;
1350
658k
                open_args.num_params = 1;
1351
658k
                open_args.params = &ft_param;
1352
658k
            }
1353
658k
            ft_error =
1354
658k
                FT_Open_Face(s->freetype_library, &open_args, a_font->subfont,
1355
658k
                             &ft_face);
1356
658k
            if (ft_error) {
1357
2.13k
                delete_inc_int (a_server, ft_inc_int);
1358
2.13k
                if (data_owned)
1359
62
                    FF_free(s->ftmemory, own_font_data);
1360
2.13k
                return ft_to_gs_error(ft_error);
1361
2.13k
            }
1362
658k
        }
1363
1364
2.62M
        if (ft_face) {
1365
2.62M
            face =
1366
2.62M
                new_face(a_server, ft_face, ft_inc_int, ft_strm,
1367
2.62M
                         own_font_data, own_font_data_len, data_owned);
1368
2.62M
            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.62M
            a_font->server_font_data = face;
1376
1377
2.62M
            if (!a_font->is_type1) {
1378
5.95M
                for (i = 0; i < GS_FAPI_NUM_TTF_CMAP_REQ && !cmap; i++) {
1379
4.03M
                    if (a_font->ttf_cmap_req[i].platform_id > 0) {
1380
13.6M
                        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.93M
                             && 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.96M
                    }
1390
63.6k
                    else {
1391
63.6k
                        break;
1392
63.6k
                    }
1393
4.03M
                }
1394
1.98M
                if (cmap) {
1395
1.92M
                    (void)FT_Set_Charmap(face->ft_face, cmap);
1396
1.92M
                }
1397
63.6k
                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.98M
                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.9k
                else {
1410
                    /* Just in case */
1411
62.9k
                    a_font->ttf_cmap_selected.platform_id = -1;
1412
62.9k
                    a_font->ttf_cmap_selected.encoding_id = -1;
1413
62.9k
                }
1414
1.98M
            }
1415
636k
            else {
1416
                /* Just in case */
1417
636k
                a_font->ttf_cmap_selected.platform_id = -1;
1418
636k
                a_font->ttf_cmap_selected.encoding_id = -1;
1419
636k
            }
1420
2.62M
        }
1421
0
        else
1422
0
            a_font->server_font_data = NULL;
1423
2.62M
    }
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.49M
    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.49M
        face->ft_transform.xx = a_font_scale->matrix[0];
1435
8.49M
        face->ft_transform.xy = a_font_scale->matrix[2];
1436
8.49M
        face->ft_transform.yx = a_font_scale->matrix[1];
1437
8.49M
        face->ft_transform.yy = a_font_scale->matrix[3];
1438
1439
8.49M
        face->horz_res = a_font_scale->HWResolution[0] >> 16;
1440
8.49M
        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.49M
        transform_decompose(&face->ft_transform, &face->horz_res,
1446
8.49M
                            &face->vert_res, &face->width, &face->height, face->ft_face->units_per_EM);
1447
1448
8.49M
        ft_error = FT_Set_Char_Size(face->ft_face, face->width, face->height,
1449
8.49M
                                    face->horz_res, face->vert_res);
1450
1451
8.49M
        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.49M
        FT_Set_Transform(face->ft_face, &face->ft_transform, NULL);
1467
1468
8.49M
    }
1469
1470
    /* dpf("gs_fapi_ft_get_scaled_font return %d\n", a_font->server_font_data ? 0 : -1); */
1471
8.49M
    return a_font->server_font_data ? 0 : -1;
1472
8.49M
}
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
12.1k
{
1484
12.1k
    *a_decoding_id = "Unicode";
1485
12.1k
    return 0;
1486
12.1k
}
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.52M
{
1494
2.52M
    ff_face *face = (ff_face *) a_font->server_font_data;
1495
1496
2.52M
    a_box[0] = face->ft_face->bbox.xMin;
1497
2.52M
    a_box[1] = face->ft_face->bbox.yMin;
1498
2.52M
    a_box[2] = face->ft_face->bbox.xMax;
1499
2.52M
    a_box[3] = face->ft_face->bbox.yMax;
1500
1501
2.52M
    unitsPerEm[0] = unitsPerEm[1] = face->ft_face->units_per_EM;
1502
1503
2.52M
    return 0;
1504
2.52M
}
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.7M
{
1552
    /* Replace metrics only if the metrics are supplied in font units. */
1553
11.7M
    *a_result = 1;
1554
11.7M
    return 0;
1555
11.7M
}
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
22.7M
{
1574
22.7M
    m->xx = 1.0;
1575
22.7M
    m->xy = 0.0;
1576
22.7M
    m->yx = 0.0;
1577
22.7M
    m->yy = 1.0;
1578
22.7M
    m->tx = 0.0;
1579
22.7M
    m->ty = 0.0;
1580
22.7M
    return 0;
1581
22.7M
}
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.93M
{
1593
7.93M
    ff_server *s = (ff_server *) a_server;
1594
7.93M
    gs_fapi_retcode error =
1595
7.93M
        load_glyph(a_server, a_font, a_char_ref, a_metrics,
1596
7.93M
                   (FT_Glyph *) & s->bitmap_glyph, true,
1597
7.93M
                   a_server->max_bitmap);
1598
7.93M
    return error;
1599
7.93M
}
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.43M
{
1607
9.43M
    ff_server *s = (ff_server *) a_server;
1608
1609
9.43M
    if (!s->bitmap_glyph)
1610
4.35M
        return(gs_error_unregistered);
1611
5.08M
    a_raster->p = s->bitmap_glyph->bitmap.buffer;
1612
5.08M
    a_raster->width = s->bitmap_glyph->bitmap.width;
1613
5.08M
    a_raster->height = s->bitmap_glyph->bitmap.rows;
1614
5.08M
    a_raster->line_step = s->bitmap_glyph->bitmap.pitch;
1615
5.08M
    a_raster->orig_x = s->bitmap_glyph->left * 16;
1616
5.08M
    a_raster->orig_y = s->bitmap_glyph->top * 16;
1617
5.08M
    a_raster->left_indent = a_raster->top_indent = a_raster->black_height =
1618
5.08M
        a_raster->black_width = 0;
1619
5.08M
    return 0;
1620
9.43M
}
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.69M
{
1632
6.69M
    ff_server *s = (ff_server *) a_server;
1633
1634
6.69M
    return load_glyph(a_server, a_font, a_char_ref, a_metrics,
1635
6.69M
                      (FT_Glyph *) & s->outline_glyph, false,
1636
6.69M
                      a_server->max_bitmap);
1637
6.69M
}
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
53.3M
{
1650
53.3M
    if (p1->x == p2->x && p1->y == p2->y)
1651
49.4k
        return 1;
1652
53.3M
    else
1653
53.3M
        return 0;
1654
53.3M
}
1655
1656
static int
1657
move_to(const FT_Vector * aTo, void *aObject)
1658
5.09M
{
1659
5.09M
    FF_path_info *p = (FF_path_info *) aObject;
1660
1661
5.09M
    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
5.09M
    p->x = ((int64_t) aTo->x) << 26;
1671
5.09M
    p->y = ((int64_t) aTo->y) << 26;
1672
1673
5.09M
    return p->path->moveto(p->path, p->x, p->y) ? -1 : 0;
1674
5.09M
}
1675
1676
static int
1677
line_to(const FT_Vector * aTo, void *aObject)
1678
22.5M
{
1679
22.5M
    FF_path_info *p = (FF_path_info *) aObject;
1680
1681
22.5M
    if (!FF_points_equal(&p->currentp, aTo)) {
1682
22.5M
        p->currentp = *aTo;
1683
1684
        /* See move_to() above */
1685
22.5M
        p->x = ((int64_t) aTo->x) << 26;
1686
22.5M
        p->y = ((int64_t) aTo->y) << 26;
1687
1688
22.5M
        return p->path->lineto(p->path, p->x, p->y) ? -1 : 0;
1689
22.5M
    }
1690
21.6k
    return 0;
1691
22.5M
}
1692
1693
static int
1694
conic_to(const FT_Vector * aControl, const FT_Vector * aTo, void *aObject)
1695
6.56M
{
1696
6.56M
    FF_path_info *p = (FF_path_info *) aObject;
1697
6.56M
    double x, y, Controlx, Controly;
1698
6.56M
    int64_t Control1x, Control1y, Control2x, Control2y;
1699
6.56M
    double sx, sy;
1700
1701
6.56M
    if (!FF_points_equal(&p->currentp, aControl) ||
1702
5.74k
        !FF_points_equal(&p->currentp, aTo) ||
1703
6.56M
        !FF_points_equal(aControl, aTo)) {
1704
6.56M
        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.56M
        sx = (double) (p->x >> 32);
1723
6.56M
        sy = (double) (p->y >> 32);
1724
1725
6.56M
        x = aTo->x / 64.0;
1726
6.56M
        p->x = ((int64_t) float2fixed(x)) << 24;
1727
6.56M
        y = aTo->y / 64.0;
1728
6.56M
        p->y = ((int64_t) float2fixed(y)) << 24;
1729
6.56M
        Controlx = aControl->x / 64.0;
1730
6.56M
        Controly = aControl->y / 64.0;
1731
1732
6.56M
        Control1x = ((int64_t) float2fixed((sx + Controlx * 2) / 3)) << 24;
1733
6.56M
        Control1y = ((int64_t) float2fixed((sy + Controly * 2) / 3)) << 24;
1734
6.56M
        Control2x = ((int64_t) float2fixed((x + Controlx * 2) / 3)) << 24;
1735
6.56M
        Control2y = ((int64_t) float2fixed((y + Controly * 2) / 3)) << 24;
1736
1737
6.56M
        return p->path->curveto(p->path, Control1x,
1738
6.56M
                                Control1y,
1739
6.56M
                                Control2x, Control2y, p->x, p->y) ? -1 : 0;
1740
6.56M
    }
1741
3.13k
    return 0;
1742
6.56M
}
1743
1744
static int
1745
cubic_to(const FT_Vector * aControl1, const FT_Vector * aControl2,
1746
         const FT_Vector * aTo, void *aObject)
1747
24.1M
{
1748
24.1M
    FF_path_info *p = (FF_path_info *) aObject;
1749
24.1M
    int64_t Control1x, Control1y, Control2x, Control2y;
1750
1751
24.1M
    if (!FF_points_equal(&p->currentp, aControl1) ||
1752
6.09k
        !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
24.1M
        !FF_points_equal(aControl2, aTo)) {
1757
24.1M
        p->currentp = *aTo;
1758
1759
        /* See move_to() above */
1760
24.1M
        p->x = ((int64_t) aTo->x) << 26;
1761
24.1M
        p->y = ((int64_t) aTo->y) << 26;
1762
1763
24.1M
        Control1x = ((int64_t) aControl1->x) << 26;
1764
24.1M
        Control1y = ((int64_t) aControl1->y) << 26;
1765
24.1M
        Control2x = ((int64_t) aControl2->x) << 26;
1766
24.1M
        Control2y = ((int64_t) aControl2->y) << 26;
1767
24.1M
        return p->path->curveto(p->path, Control1x, Control1y, Control2x,
1768
24.1M
                                Control2y, p->x, p->y) ? -1 : 0;
1769
24.1M
    }
1770
1.71k
    return 0;
1771
24.1M
}
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.45M
{
1788
4.45M
    ff_server *s = (ff_server *) a_server;
1789
4.45M
    FF_path_info p;
1790
4.45M
    FT_Error ft_error = 0;
1791
1792
4.45M
    p.path = a_path;
1793
4.45M
    p.x = 0;
1794
4.45M
    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.45M
    if (s->outline_glyph) {
1799
4.45M
        ft_error =
1800
4.45M
            FT_Outline_Decompose(&s->outline_glyph->outline, &TheFtOutlineFuncs,
1801
4.45M
                                 &p);
1802
4.45M
    }
1803
0
    else {
1804
0
        a_path->moveto(a_path, 0, 0);
1805
0
    }
1806
1807
4.45M
    if (a_path->gs_error == 0)
1808
4.26M
        a_path->closepath(a_path);
1809
4.45M
    return ft_to_gs_error(ft_error);
1810
4.45M
}
1811
1812
static gs_fapi_retcode
1813
gs_fapi_ft_release_char_data(gs_fapi_server * a_server)
1814
11.7M
{
1815
11.7M
    ff_server *s = (ff_server *) a_server;
1816
1817
11.7M
    if (s->outline_glyph) {
1818
4.47M
        FT_Outline_Done(s->freetype_library, &s->outline_glyph->outline);
1819
4.47M
        FF_free(s->ftmemory, s->outline_glyph);
1820
4.47M
    }
1821
1822
11.7M
    if (s->bitmap_glyph) {
1823
5.08M
        FT_Bitmap_Done(s->freetype_library, &s->bitmap_glyph->bitmap);
1824
5.08M
        FF_free(s->ftmemory, s->bitmap_glyph);
1825
5.08M
    }
1826
1827
11.7M
    s->outline_glyph = NULL;
1828
11.7M
    s->bitmap_glyph = NULL;
1829
11.7M
    return 0;
1830
11.7M
}
1831
1832
static gs_fapi_retcode
1833
gs_fapi_ft_release_typeface(gs_fapi_server * a_server, void *a_server_font_data)
1834
2.62M
{
1835
2.62M
    ff_face *face = (ff_face *) a_server_font_data;
1836
1837
2.62M
    delete_face(a_server, face);
1838
2.62M
    return 0;
1839
2.62M
}
1840
1841
static gs_fapi_retcode
1842
gs_fapi_ft_check_cmap_for_GID(gs_fapi_server * server, uint * index)
1843
13.6M
{
1844
13.6M
    ff_face *face = (ff_face *) (server->ff.server_font_data);
1845
13.6M
    FT_Face ft_face = face->ft_face;
1846
1847
13.6M
    *index = FT_Get_Char_Index(ft_face, *index);
1848
13.6M
    return 0;
1849
13.6M
}
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
190k
{
1933
190k
    ff_server *serv;
1934
190k
    int code = 0;
1935
190k
    gs_memory_t *cmem = mem->non_gc_memory;
1936
1937
190k
    code = gs_memory_chunk_wrap(&(cmem), mem);
1938
190k
    if (code != 0) {
1939
0
        return (code);
1940
0
    }
1941
1942
1943
190k
    serv = (ff_server *) gs_alloc_bytes_immovable(cmem, sizeof(ff_server), "gs_fapi_ft_init");
1944
190k
    if (!serv) {
1945
0
        gs_memory_chunk_release(cmem);
1946
0
        return_error(gs_error_VMerror);
1947
0
    }
1948
190k
    memset(serv, 0, sizeof(*serv));
1949
190k
    serv->mem = cmem;
1950
190k
    serv->fapi_server = freetypeserver;
1951
1952
190k
    serv->ftmemory = (FT_Memory) (&(serv->ftmemory_rec));
1953
1954
190k
    (*server) = (gs_fapi_server *) serv;
1955
190k
    return (0);
1956
190k
}
1957
1958
1959
void
1960
gs_fapi_freetype_destroy(gs_fapi_server ** serv)
1961
190k
{
1962
190k
    ff_server *server = (ff_server *) * serv;
1963
190k
    gs_memory_t *cmem = server->mem;
1964
1965
190k
    FT_Done_Glyph(&server->outline_glyph->root);
1966
190k
    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
190k
    FT_Done_Library(server->freetype_library);
1973
190k
    gs_free(cmem, *serv, 0, 0, "gs_fapi_freetype_destroy: ff_server");
1974
    *serv = NULL;
1975
190k
    gs_memory_chunk_release(cmem);
1976
190k
}