summaryrefslogtreecommitdiff
blob: 73c1d08f28c600b820ced85945288fc5867bd0ae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
/* Copyright (C) 2001-2019 Artifex Software, Inc.
   All Rights Reserved.

   This software is provided AS-IS with no warranty, either express or
   implied.

   This software is distributed under license and may not be copied,
   modified or distributed except as expressly authorized under the terms
   of the license contained in the file LICENSE in this distribution.

   Refer to licensing information at http://www.artifex.com or contact
   Artifex Software, Inc.,  1305 Grant Avenue - Suite 200, Novato,
   CA 94945, U.S.A., +1(415)492-9861, for further information.
*/


/* $Id: fapibstm.c 11056 2010-04-13 09:38:22Z chrisl $ */
/* Bitstream font engine plugin */

/* GS includes : */
#include "stdio_.h"
#include "memory_.h"
#include "math_.h"
#include "gserrors.h"
#include "iplugin.h"
#include "ifapi.h"
#include "strmio.h"
#include "gsmalloc.h"

#include "config.h"
#include "t2k.h"

typedef struct fapi_bitstream_server_s fapi_bitstream_server;

typedef struct fapi_bitstream_server_s
{
    FAPI_server If;
    int bInitialized;
    FAPI_font *ff;
    i_plugin_client_memory client_mem;
} Bitstream_server;

typedef struct Bitstream_face_s
{
    tsiMemObject *MemObject;
    InputStream *Input;
    sfntClass *sfnt;
    T2K *T2K;
    unsigned int HRes, VRes;
    T2K_TRANS_MATRIX trans;
    unsigned int Initialised;

    /* Non-null if font data is owned by this object. */
    unsigned char *font_data;
} Bitstream_face;

static int
InitFont(Bitstream_server * server, FAPI_font * ff)
{
    unsigned char *own_font_data = NULL, *temp;
    gs_memory_t *mem = (gs_memory_t *) server->client_mem.client_data;
    Bitstream_face *face = (Bitstream_face *) ff->server_font_data;
    long length, written;
    int error, type;

    if (ff->font_file_path) {
    }
    /* Load a typeface from a representation in GhostScript's memory. */
    else {
        if (ff->is_type1) {
            type = ff->get_word(ff, FAPI_FONT_FEATURE_FontType, 0);

            /* Tell the FAPI interface that we need to *not* decrypt the /Subrs data. */
            ff->need_decrypt = false;

            /*
               Serialise a type 1 font in PostScript source form, or
               a Type 2 font in binary form, so that FreeType can read it.
             */
            if (type == 1)
                length = FF_serialize_type1_font_complete(ff, NULL, 0);
            else
                length = FF_serialize_type2_font(ff, NULL, 0);
            own_font_data = gs_malloc(mem, 1, length, "Type 1 font copy");
            if (type == 1)
                written =
                    FF_serialize_type1_font_complete(ff, own_font_data,
                                                     length);
            else
                written = FF_serialize_type2_font(ff, own_font_data, length);
            if (written != length)
                return (gs_error_unregistered);        /* Must not happen. */
        }
        /* It must be type 42 (see code in FAPI_FF_get_glyph in zfapi.c). */
        else {
            /* Get the length of the TrueType data. */
            long length = ff->get_long(ff, FAPI_FONT_FEATURE_TT_size, 0);

            if (length == 0)
                return = gs_error_invalidfont;

            /* Load the TrueType data into a single buffer. */
            own_font_data = gs_malloc(mem, 1, length, "Type 42 fotn copy");
            if (!own_font_data)
                return_error(gs_error_VMerror);
            if (ff->serialize_tt_font(ff, own_font_data, length))
                return_error(gs_error_invalidfont);
        }
    }
    face->font_data = own_font_data;
    face->Input =
        New_InputStream3(face->MemObject, own_font_data, length, &error);
    if (ff->is_type1) {
        if (type == 1)
            face->sfnt =
                FF_New_sfntClass(face->MemObject, FONT_TYPE_1, 1, face->Input,
                                 NULL, NULL, &error);
        else
            face->sfnt =
                FF_New_sfntClass(face->MemObject, FONT_TYPE_2, 1, face->Input,
                                 NULL, NULL, &error);
    }
    else {
        face->sfnt =
            FF_New_sfntClass(face->MemObject, FONT_TYPE_TT_OR_T2K, 1,
                             face->Input, NULL, NULL, &error);
    }
    face->T2K = NewT2K(face->MemObject, face->sfnt, &error);
    face->Initialised = 1;
    return 0;
}

static FAPI_retcode
ensure_open(FAPI_server * server, const byte * server_param,
            int server_param_size)
{
    return 0;
}

static FAPI_retcode
get_scaled_font(FAPI_server * server, FAPI_font * ff,
                const FAPI_font_scale * font_scale, const char *xlatmap,
                FAPI_descendant_code dc)
{
    Bitstream_server *Bserver = (Bitstream_server *) server;
    Bitstream_face *face = (Bitstream_face *) ff->server_font_data;

    if (face == NULL) {
        gs_memory_t *mem = (gs_memory_t *) Bserver->client_mem.client_data;
        int error;

        face =
            (Bitstream_face *) gs_malloc(mem, 1, sizeof(Bitstream_face),
                                         "Bitstream_face alloc");
        face->MemObject = tsi_NewMemhandler(&error);
        if (error)
            return -1;
        face->Initialised = 0;
        ff->server_font_data = face;
        Bserver->ff = ff;
    }
    face->HRes = font_scale->HWResolution[0] >> 16;
    face->VRes = font_scale->HWResolution[1] >> 16;
    face->trans.t00 = ONE16Dot16 * 50;  /* font_scale->matrix[0]; */
    face->trans.t01 = 0;        /*font_scale->matrix[1]; */
    face->trans.t10 = 0;        /*font_scale->matrix[2]; */
    face->trans.t11 = ONE16Dot16 * 50;  /*font_scale->matrix[3]; */
    return 0;
}

static FAPI_retcode
get_decodingID(FAPI_server * server, FAPI_font * ff,
               const char **decodingID_result)
{
    return 0;
}

static FAPI_retcode
get_font_bbox(FAPI_server * server, FAPI_font * ff, int BBox[4], int unitsPerEm[2])
{
    return 0;
}

static FAPI_retcode
get_font_proportional_feature(FAPI_server * server, FAPI_font * ff,
                              bool * bProportional)
{
    return 0;
}

static FAPI_retcode
can_retrieve_char_by_name(FAPI_server * server, FAPI_font * ff,
                          FAPI_char_ref * c, int *result)
{
    *result = 1;
    return 0;
}

static FAPI_retcode
can_replace_metrics(FAPI_server * server, FAPI_font * ff, FAPI_char_ref * c,
                    int *result)
{
    *result = 1;
    return 0;
}

static FAPI_retcode
get_fontmatrix(FAPI_server * I, gs_matrix * m)
{
    gs_matrix *base_font_matrix = &I->initial_FontMatrix;

    m->xx = I->initial_FontMatrix.xx;
    m->xy = I->initial_FontMatrix.xy;
    m->yx = I->initial_FontMatrix.yx;
    m->yy = I->initial_FontMatrix.yy;
    m->tx = I->initial_FontMatrix.tx;
    m->ty = I->initial_FontMatrix.ty;
    return 0;
}

static FAPI_retcode
get_char_width(FAPI_server * server, FAPI_font * ff, FAPI_char_ref * c,
               FAPI_metrics * metrics)
{
    return 0;
}

static FAPI_retcode
get_char_raster_metrics(FAPI_server * server, FAPI_font * ff,
                        FAPI_char_ref * c, FAPI_metrics * metrics)
{
    Bitstream_face *face = (Bitstream_face *) ff->server_font_data;
    int error;

    if (!face->Initialised)
        InitFont((Bitstream_server *) server, ff);

    T2K_NewTransformation(face->T2K, 1, face->HRes, face->VRes, &face->trans,
                          false, &error);
    T2K_RenderGlyph(face->T2K, c->char_codes[0], 0, 0, BLACK_AND_WHITE_BITMAP,
                    T2K_NAT_GRID_FIT | T2K_RETURN_OUTLINES | T2K_SCAN_CONVERT,
                    &error);

    metrics->bbox_x0 = face->T2K->fLeft26Dot6 >> 6;
    metrics->bbox_y0 = 0;
    metrics->bbox_x1 =
        metrics->bbox_x0 + face->T2K->xLinearAdvanceWidth16Dot16 >> 16;
    metrics->bbox_y1 = face->T2K->fTop26Dot6 >> 6;
    metrics->escapement = face->T2K->xLinearAdvanceWidth16Dot16 >> 16;
    metrics->v_escapement = face->T2K->yLinearAdvanceWidth16Dot16 >> 16;
    metrics->em_x = face->T2K->xPixelsPerEm;
    metrics->em_y = face->T2K->yPixelsPerEm;
    return 0;
}

static FAPI_retcode
get_char_raster(FAPI_server * server, FAPI_raster * rast)
{
    FAPI_font *ff = &server->ff;
    Bitstream_face *face = (Bitstream_face *) ff->server_font_data;

    rast->p = face->T2K->baseAddr;
    rast->width = face->T2K->width;
    rast->height = face->T2K->height;
    rast->line_step = face->T2K->rowBytes;
    rast->orig_x = 0;
    rast->orig_y = 0;
    rast->left_indent = rast->top_indent = rast->black_height =
        rast->black_width = 0;
    return 0;
}

static FAPI_retcode
get_char_outline_metrics(FAPI_server * server, FAPI_font * ff,
                         FAPI_char_ref * c, FAPI_metrics * metrics)
{
    return 0;
}

static FAPI_retcode
get_char_outline(FAPI_server * server, FAPI_path * p)
{
    return 0;
}

static FAPI_retcode
release_char_data(FAPI_server * server)
{
    FAPI_font *ff = &server->ff;
    Bitstream_face *face = (Bitstream_face *) ff->server_font_data;
    int error;

    T2K_PurgeMemory(face->T2K, 1, &error);
    return 0;
}

static FAPI_retcode
release_typeface(FAPI_server * server, void *font_data)
{
    int error;
    Bitstream_server *Bserver = (Bitstream_server *) server;
    Bitstream_face *face = (Bitstream_face *) font_data;
    gs_memory_t *mem = (gs_memory_t *) Bserver->client_mem.client_data;

    if (face->Initialised) {
        DeleteT2K(face->T2K, &error);
        FF_Delete_sfntClass(face->sfnt, &error);
        Delete_InputStream(face->Input, &error);
        gs_free(mem, face->font_data, 0, 0, "free font copy");
        tsi_DeleteMemhandler(face->MemObject);
    }
    return 0;
}

static FAPI_retcode
check_cmap_for_GID(FAPI_server * server, uint index)
{
    return 0;
}

static FAPI_retcode
gs_fapi_bstm_set_mm_weight_vector(gs_fapi_server *server, gs_fapi_font *ff, float *wvector, int length)
{
    (void)server;
    (void)ff;
    (void)wvector;
    (void)length;
    
    return gs_error_invalidaccess;
}

static void gs_fapibstm_finit(i_plugin_instance * instance,
                              i_plugin_client_memory * mem);

static const i_plugin_descriptor bitstream_descriptor = {
    "FAPI",
    "BITSTREAM",
    gs_fapibstm_finit
};

static const FAPI_server If0 = {
    {&bitstream_descriptor},
    16,                         /* frac_shift */
    {gs_no_id},
    {0},
    0,
    false,
    1,
    {1, 0, 0, 1, 0, 0},
    ensure_open,
    get_scaled_font,
    get_decodingID,
    get_font_bbox,
    get_font_proportional_feature,
    can_retrieve_char_by_name,
    can_replace_metrics,
    NULL,                       /* can_simulate_style */
    get_fontmatrix,
    get_char_width,
    get_char_raster_metrics,
    get_char_raster,
    get_char_outline_metrics,
    get_char_outline,
    release_char_data,
    release_typeface,
    check_cmap_for_GID,
    NULL,     /* get_font_info */
    gs_fapi_bstm_set_mm_weight_vector
};

plugin_instantiation_proc(gs_fapibstm_instantiate);     /* check prototype */

int
gs_fapibstm_instantiate(i_plugin_client_memory * client_mem,
                        i_plugin_instance ** p_instance)
{
    int error;
    tsiMemObject *mem = NULL;
    fapi_bitstream_server *r;

    r = (fapi_bitstream_server *) client_mem->alloc(client_mem,
                                                    sizeof
                                                    (fapi_bitstream_server),
                                                    "fapi_bitstream_server");
    if (r == 0)
        return_error(gs_error_VMerror);
    memset(r, 0, sizeof(*r));
    r->If = If0;
    r->client_mem = *client_mem;
    *p_instance = &r->If.ig;

    return 0;
}

static void
gs_fapibstm_finit(i_plugin_instance * this, i_plugin_client_memory * mem)
{
    fapi_bitstream_server *r = (fapi_bitstream_server *) this;

    if (r->If.ig.d != &bitstream_descriptor)
        return;                 /* safety */
    mem->free(mem, r, "fapi_bitstream_server");
}