Coverage Report

Created: 2026-04-09 07:06

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