Coverage Report

Created: 2026-04-01 07:17

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