summaryrefslogtreecommitdiff
path: root/gs/base/gxfapiu.c
blob: 95cd9f1b41f1b4d138de942f2d4298d1ff5cd7e9 (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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
/* Copyright (C) 2001-2006 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 that
   license.  Refer to licensing information at http://www.artifex.com/
   or contact Artifex Software, Inc.,  7 Mt. Lassen Drive - Suite A-134,
   San Rafael, CA  94903, U.S.A., +1(415)492-9861, for further information.
*/

/* $Id$ */
/* Font API support : UFST common initialization */

/* GS includes : */
#include "std.h"
#include "gx.h"
#include "strmio.h"
#include "gsmalloc.h"

/* UFST includes : */
#undef frac_bits
#include "cgconfig.h"
#include "ufstport.h"
#include "shareinc.h"
/* GS includes : */
#include "gxfapiu.h"

#define MAX_STATIC_FCO_COUNT 4

/* Slightly nasty: UFST doesn't provide for data to be
   passed through it, so we've got to store a reference
   to our context here.
   When we fix the UFST_REENTRANT option, there will be
   provision for passing the context through the UFST.
*/
#if UFST_REENTRANT
/* not currently supported */
#else
static gs_memory_t *gs_mem_ctx = NULL;
#endif

/* -------------------- UFST callback dispatcher ------------- */

/* This code provides dispatching UFST callbacks to the PDL */

struct IF_STATE;

static LPUB8 stub_PCLEO_charptr(FSP LPUB8 pfont_hdr, UW16  sym_code)
{   return NULL;
}

static LPUB8 stub_PCLchId2ptr(FSP UW16 chId)
{   return NULL;
}

static LPUB8 stub_PCLglyphID2Ptr(FSP UW16 glyphID)
{   return NULL;
}

/* This global is defined here because the UFST needs it to link against. */
/* set to 1 to display UFST debugging statements */
GLOBAL const SW16 trace_sw = 0;

/*
    The following 4 variables are defined statically

They could be stored in the gs_lib_ctx but that would require casting the types
to avoid including ufst's typedefs.
*/

static LPUB8 (*m_PCLEO_charptr)(FSP LPUB8 pfont_hdr, UW16  sym_code) = stub_PCLEO_charptr;
static LPUB8 (*m_PCLchId2ptr)(FSP UW16 chId) = stub_PCLchId2ptr;
static LPUB8 (*m_PCLglyphID2Ptr)(FSP UW16 glyphID) = stub_PCLglyphID2Ptr;
#if !UFST_REENTRANT
static fco_list_elem static_fco_list[MAX_STATIC_FCO_COUNT] = {0, 0, 0, 0};
static char static_fco_paths[MAX_STATIC_FCO_COUNT][gp_file_name_sizeof];
static int static_fco_count = 0;
static bool ufst_initialized = FALSE;
#endif

LPUB8 PCLEO_charptr(FSP LPUB8 pfont_hdr, UW16  sym_code)
{   return m_PCLEO_charptr(FSA pfont_hdr, sym_code);
}

LPUB8 PCLchId2ptr(FSP UW16 chId)
{   return m_PCLchId2ptr(FSA chId);
}

LPUB8 PCLglyphID2Ptr(FSP UW16 glyphID)
{   return m_PCLglyphID2Ptr(FSA glyphID);
}
/* File handling intermediates */

FILE * FAPIU_fopen (char *path, char *mode)
{
    if (!gs_mem_ctx)
        return NULL;

    return((FILE *)sfopen(path, mode, gs_mem_ctx));
}

int FAPIU_fread (void *ptr, int size, int count, FILE *s)
{
    return(sfread(ptr, size, count, (stream *)(s)));
}

int FAPIU_fgetc (FILE *s)
{
    return(sfgetc((stream *)(s)));
}

int FAPIU_fseek (FILE *s, int offset, int whence)
{
    return(sfseek((stream *)(s), offset, whence));
}

int FAPIU_frewind (FILE *s)
{
    return(srewind((stream *)(s)));
}

int FAPIU_ftell (FILE *s)
{
    return(sftell((stream *)(s)));
}

int FAPIU_feof (FILE *s)
{
    return(sfeof((stream *)(s)));
}

int FAPIU_ferror (FILE *s)
{
    return(sferror((stream *)(s)));
}

int FAPIU_fclose (FILE *s)
{
    return(sfclose((stream *)(s)));
}

void * FAPIU_open (char *path, int mode)
{
    if (!gs_mem_ctx)
        return NULL;

    /* FIXME: "mode" needs work */
    return(sfopen(path, "r+", gs_mem_ctx));
}

int FAPIU_read (void *s, void *ptr, int count)
{
    return(sfread(ptr, 1, count, (stream *)(s)));
}

int FAPIU_lseek (void *s, int offset, int whence)
{
    int pos = sfseek (s, offset, whence);

    if (pos >= 0)
    {
        pos = sftell(s);
    }
    return(pos);
}

int fapi_ufseek (stream *s, long offset, int whence)
{
    int pos = sfseek (s, offset, whence);

    if (pos >= 0)
    {
        pos = sftell(s);
    }
    return(pos);
}

int FAPIU_close (void *s)
{
    return(sfclose((stream *)(s)));
}

#if UFST_VERSION_MAJOR >= 6 && UFST_VERSION_MINOR >= 2

GLOBAL VOID MEMinit(FSP0)
{
   if_state.pserver->mem_avail[CACHE_POOL]  = 16 * 1024 * 1024;
   if_state.pserver->mem_fund[CACHE_POOL]   = 16 * 1024 * 1024;

   if_state.pserver->mem_avail[BUFFER_POOL] = 4 * 1024 * 1024;
   if_state.pserver->mem_fund[BUFFER_POOL]  = 4 * 1024 * 1024;

   if_state.pserver->mem_avail[CHARGEN_POOL]  = 16 * 1024 * 1024;
   if_state.pserver->mem_fund[CHARGEN_POOL]   = 16 * 1024 * 1024;

}
#ifndef UFST_MEMORY_CHECKING
#define UFST_MEMORY_CHECKING 0
#endif

#if UFST_MEMORY_CHECKING
static unsigned long curmem = 0;
unsigned long maxmem = 0;
#endif

GLOBAL MEM_HANDLE MEMalloc(FSP UW16 pool, SL32 size)
{

    void *ptr;
#if UFST_MEMORY_CHECKING
    void *ptr2;

    size += sizeof(long) + 2 * sizeof(void *);
#endif

    size += sizeof(long);
    ptr = gs_malloc (gs_mem_ctx, size, 1, "UFST MEMalloc");
    if(!ptr) {
        return(NIL_MH);
    }
    else {
        *((long *)ptr) = size;

#if UFST_MEMORY_CHECKING
        *((long *)(ptr + size - sizeof(long))) = size;
        ptr2 = ptr;
#endif

        ptr += sizeof(long);

#if UFST_MEMORY_CHECKING
        if ((curmem += size) > maxmem) {
            maxmem = curmem;
        }
        size -= 2 * sizeof(long);
        *((char *)ptr) = ptr2;
        *((char *)(ptr + size - sizeof(void *))) = ptr2;
        ptr += sizeof(void *);

#endif

        return((MEM_HANDLE)ptr);
    }
}

GLOBAL VOID MEMfree(FSP UW16 pool, MEM_HANDLE ptr)
{
    int size = 0;
    void *ptr1;
#if UFST_MEMORY_CHECKING
    int size1;
    void *ptr2;

    size = sizeof(void *);
#endif

    if (!ptr) {
        return;
    }

    ptr -= (sizeof(long) + size);
    size = *((long *)ptr);

#if UFST_MEMORY_CHECKING
    ptr1 = ptr;
    size1 = size;
    curmem -= size;

    if (size1 != *((long *)(ptr + size - sizeof(long)))) {
        dprintf("Memory length corrupt!\n");
    }

    ptr1 += sizeof(long);
    size1 -= sizeof(long);
    if (*((char *)ptr) != *((char *)(ptr + size - sizeof(void *)))) {
        dprintf("Memory pointer record corrupt!\n");
    }
#endif

    memset (ptr, 0x00, size);
    gs_free (gs_mem_ctx, ptr, 0, 0, "UFST MEMfree");
}

#endif

/* Set UFST callbacks. Each PDL will want it's own character build function and must set the callbacks
 * upon language entry/initialization.
 */
/* Warning : this function may cause a reentrancy problem
     due to a modification of static variables.
     Nevertheless this problem isn't important in a
     single interpreter build, because the values
     really change on the first demand only.
     See also a comment in gs_fapiufst_finit.
   */
void gx_set_UFST_Callbacks(LPUB8 (*p_PCLEO_charptr)(FSP LPUB8 pfont_hdr, UW16  sym_code),
                           LPUB8 (*p_PCLchId2ptr)(FSP UW16 chId),
                           LPUB8 (*p_PCLglyphID2Ptr)(FSP UW16 glyphID))
{   m_PCLEO_charptr = (p_PCLEO_charptr != NULL ? p_PCLEO_charptr : stub_PCLEO_charptr);
    m_PCLchId2ptr = (p_PCLchId2ptr != NULL ? p_PCLchId2ptr : stub_PCLchId2ptr);
    m_PCLglyphID2Ptr = (p_PCLglyphID2Ptr != NULL ? p_PCLglyphID2Ptr : stub_PCLglyphID2Ptr);
}

#define MAX_OPEN_LIBRARIES  5   /* NB */
#define BITMAP_WIDTH        1   /* must be 1, 2, 4 or 8 */

/* returns negative on error,
 * 1 = "I just initialized for the first time and you might want to as well."
 * 0 = "I've already initialized but its ok to call me."
 * <0 = error.
 */
int
gx_UFST_init(gs_memory_t *mem, const UB8 *ufst_root_dir)
{
    IFCONFIG            config_block;
    int status;

#if !UFST_REENTRANT
    if (ufst_initialized)
        return 0;
    gs_mem_ctx = mem;
#endif
    strcpy(config_block.ufstPath, ufst_root_dir);
    strcpy(config_block.typePath, ufst_root_dir);
    config_block.num_files = MAX_OPEN_LIBRARIES;  /* max open library files */
    config_block.bit_map_width = BITMAP_WIDTH;    /* bitmap width 1, 2 or 4 */

    /* These parameters were set in open_UFST() (fapiufst.c) but were left
       uninitialized in pl_load_built_in_fonts() (plulfont.c). */
    config_block.typePath[0] = 0;

    if ((status = CGIFinit(FSA0)) != 0) {
        dprintf1("CGIFinit() error: %d\n", status);
        gs_mem_ctx = NULL;
        return status;
    }
    if ((status = CGIFconfig(FSA &config_block)) != 0) {
        dprintf1("CGIFconfig() error: %d\n", status);
        gs_mem_ctx = NULL;
        return status;
    }
    if ((status = CGIFenter(FSA0)) != 0) {
        dprintf1("CGIFenter() error: %u\n",status);
        gs_mem_ctx = NULL;
        return status;
    }
#if !UFST_REENTRANT
    ufst_initialized = TRUE;
#endif
    return 1; /* first time, caller may have more initialization to do */
}

int
gx_UFST_fini(void)
{
    CGIFexit(FSA0);
#if !UFST_REENTRANT
    ufst_initialized = FALSE;
    gs_mem_ctx = NULL;
#endif
    return 0;
}

/* Access to the static FCO list for the language switching project. */

fco_list_elem *gx_UFST_find_static_fco(const char *font_file_path)
{
#if !UFST_REENTRANT
    int i;

    for (i = 0; i < static_fco_count; i++)
        if (!strcmp(static_fco_list[i].file_path, font_file_path))
            return &static_fco_list[i];
#endif
    return NULL;
}

fco_list_elem *gx_UFST_find_static_fco_handle(SW16 fcHandle)
{
#if !UFST_REENTRANT
    int i;

    for (i = 0; i < static_fco_count; i++)
        if (static_fco_list[i].fcHandle == fcHandle)
            return &static_fco_list[i];
#endif
    return NULL;
}

SW16 gx_UFST_find_fco_handle_by_name(const char *font_file_path)
{
#if !UFST_REENTRANT
    fco_list_elem *fco = gx_UFST_find_static_fco(font_file_path);

    if (fco)
        return fco->fcHandle;
    return 0;
#endif
}

UW16 gx_UFST_open_static_fco(const char *font_file_path, SW16 *result_fcHandle)
{
#if !UFST_REENTRANT
    SW16 fcHandle;
    UW16 code;
    fco_list_elem *e;

    if (static_fco_count >= MAX_STATIC_FCO_COUNT)
        return ERR_fco_NoMem;
    code = CGIFfco_Open(FSA (UB8 *)font_file_path, &fcHandle);
    if (code != 0)
        return code;
    e = &static_fco_list[static_fco_count];
    strncpy(static_fco_paths[static_fco_count], font_file_path,
            sizeof(static_fco_paths[static_fco_count]));
    e->file_path = static_fco_paths[static_fco_count];
    e->fcHandle = fcHandle;
    e->open_count = -1; /* Unused for static FCOs. */
    static_fco_count++;
    *result_fcHandle = fcHandle;
    return 0;
#else
    **result_fcHandle = -1;
    return ERR_fco_NoMem;
#endif
}

UW16 gx_UFST_close_static_fco(SW16 fcHandle)
{
#if !UFST_REENTRANT
    int i;

    for (i = 0; i < static_fco_count; i++)
        if (static_fco_list[i].fcHandle == fcHandle)
            break;
    if (i >= static_fco_count)
        return ERR_fco_NoMem;
    CGIFfco_Close(FSA fcHandle);
    for (i++; i < static_fco_count; i++) {
        static_fco_list[i - 1] = static_fco_list[i];
        strcpy(static_fco_paths[i - 1], static_fco_paths[i]);
    }
    static_fco_count--;
#endif
    return 0;
}

void gx_UFST_close_static_fcos()
{
#if !UFST_REENTRANT
    for(; static_fco_count; )
        gx_UFST_close_static_fco(static_fco_list[0].fcHandle);
#endif
}

#if GRAYSCALING
/* ------------------------------- BLACKPIX --------------------------------
 * Description: Called by UFST gichar function.
 * Returns:     VOID
 * Notes:       This is a stub implementation for graymap.
 */
GLOBAL VOID BLACKPIX(FSP SW16 x, SW16 y )
{
        return;
}

/* -------------------------------- GRAYPIX --------------------------------
 * Description: Called by UFST gichar function.
 * Returns:     VOID
 * Notes:       This is a stub implementation for graymap.
 */
GLOBAL VOID GRAYPIX(FSP SW16 x, SW16 y, SW16 v )
{
        return;
}
#endif	/* GRAYSCALING */