Coverage Report

Created: 2025-06-24 07:01

/src/ghostpdl/psi/zfontenum.c
Line
Count
Source (jump to first uncovered line)
1
/* Copyright (C) 2001-2023 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
/* this is the ps interpreter interface to the native font
19
   enumeration code. it calls the platform-specific routines
20
   to obtain an additional set of entries that can be added
21
   to the Fontmap to reference fonts stored on the system.
22
 */
23
24
#include "memory_.h"
25
#include "string_.h"
26
#include <stdlib.h>
27
#include "ghost.h"
28
#include "oper.h"
29
#include "gsstruct.h"
30
#include "gsmalloc.h"
31
#include "ialloc.h"
32
#include "iname.h"
33
#include "iutil.h"
34
#include "store.h"
35
#include "gp.h"
36
37
typedef struct fontenum_s {
38
        char *fontname, *path;
39
        struct fontenum_s *next;
40
} fontenum_t;
41
42
/* .getnativefonts [ [<name> <path>] ... ] */
43
static int
44
z_fontenum(i_ctx_t *i_ctx_p)
45
8.79k
{
46
8.79k
    os_ptr op = osp;
47
8.79k
    void *enum_state = NULL;
48
8.79k
    int code = 0;
49
8.79k
    int e,elements, e2;
50
8.79k
    char *fontname, *path;
51
8.79k
    fontenum_t *results, **presults;
52
8.79k
    ref array;
53
8.79k
    uint length;
54
8.79k
    uint length2;
55
8.79k
    byte *nfname, *nfpath;
56
57
8.79k
    enum_state = gp_enumerate_fonts_init(imemory);
58
8.79k
    if (enum_state == NULL) {
59
      /* put false on the stack and return */
60
8.79k
      push(1);
61
8.79k
      make_bool(op, false);
62
8.79k
      goto all_done;
63
8.79k
    }
64
65
0
    presults = &results;
66
0
    results = NULL;
67
0
    elements = 0;
68
0
    while((code = gp_enumerate_fonts_next(enum_state, &fontname, &path )) > 0) {
69
0
        fontenum_t *r;
70
71
0
        if (fontname == NULL || path == NULL) {
72
0
            code = gs_note_error(gs_error_ioerror);
73
0
            goto done;
74
0
        }
75
0
        r = gs_malloc(imemory->non_gc_memory, 1, sizeof(fontenum_t), "fontenum list");
76
0
        if (r == NULL)
77
0
            break;
78
0
        length = strlen(fontname) + 1;
79
0
        r->fontname = gs_malloc(imemory->non_gc_memory, length, 1, "native font name");
80
0
        if (r->fontname) {
81
0
            memcpy(r->fontname, fontname, length);
82
0
            length2 = strlen(path) + 1;
83
0
            r->path = gs_malloc(imemory->non_gc_memory, length2, 1, "native font path");
84
0
            if (r->path) {
85
0
                memcpy(r->path, path, length2);
86
0
                *presults = r;
87
0
                presults = &r->next;
88
0
                r->next = 0;
89
0
                elements += 1;
90
0
            } else {
91
0
                gs_free(imemory->non_gc_memory, r->fontname, length, 1, "native font name");
92
0
                gs_free(imemory->non_gc_memory, r, sizeof(fontenum_t), 1, "fontenum list");
93
0
                break;
94
0
            }
95
0
        } else {
96
0
            gs_free(imemory->non_gc_memory, r, sizeof(fontenum_t), 1, "fontenum list");
97
0
            break;
98
0
        }
99
0
    }
100
101
0
    gp_enumerate_fonts_free(enum_state);
102
0
    enum_state = NULL;
103
104
0
    if ((code = ialloc_ref_array(&array, a_all | icurrent_space, elements, "native fontmap")) >= 0) {
105
0
        fontenum_t *r = results;
106
107
0
        for (e = e2 = 0; e < elements && r != NULL; e++) {
108
0
            ref mapping;
109
110
0
            if ((code = ialloc_ref_array(&mapping, a_all | icurrent_space, 2, "native font mapping")) >= 0) {
111
0
                length = strlen(r->fontname);
112
0
                length2 = strlen(r->path);
113
0
                nfname = ialloc_string(length, "native font name");
114
0
                nfpath = ialloc_string(length2, "native font path");
115
0
                if (nfname == NULL || nfpath == NULL) {
116
0
                    ifree_string(nfname, length, "native font name");
117
0
                    ifree_string(nfpath, length2, "native font name");
118
0
                }
119
0
                else {
120
0
                   memcpy(nfname, r->fontname, length);
121
0
                   make_string(&(mapping.value.refs[0]), a_all | icurrent_space, length, nfname);
122
0
                   memcpy(nfpath, r->path, length2);
123
0
                   make_string(&(mapping.value.refs[1]), a_all | icurrent_space, length2, nfpath);
124
125
0
                   ref_assign(&(array.value.refs[e2]), &mapping);
126
0
                   e2++;
127
0
                }
128
0
            }
129
0
            results = r;
130
0
            r = r->next;
131
132
0
            gs_free(imemory->non_gc_memory, results->fontname,
133
0
                    strlen(results->fontname) + 1, 1, "native font name");
134
0
            gs_free(imemory->non_gc_memory, results->path,
135
0
                    strlen(results->path) + 1, 1, "native font path");
136
0
            gs_free(imemory->non_gc_memory, results, 1,
137
0
                    sizeof(fontenum_t), "fontenum list");
138
0
        }
139
0
    }
140
0
    else {
141
0
        while (elements-- && results != NULL) {
142
0
            fontenum_t *r = results->next;
143
144
0
            gs_free(imemory->non_gc_memory, results->fontname,
145
0
                    strlen(results->fontname) + 1, 1, "native font name");
146
0
            gs_free(imemory->non_gc_memory, results->path,
147
0
                    strlen(results->path) + 1, 1, "native font path");
148
0
            gs_free(imemory->non_gc_memory, results, 1,
149
0
                    sizeof(fontenum_t), "fontenum list");
150
0
            results = r;
151
0
        }
152
0
    }
153
154
0
done:
155
0
    if (enum_state)
156
0
        gp_enumerate_fonts_free(enum_state);
157
0
    if (code >= 0) {
158
0
        push(2);
159
0
        ref_assign(op-1, &array);
160
0
        make_bool(op, true);
161
0
    }
162
8.79k
all_done:
163
8.79k
    return code;
164
0
}
165
166
/* Match the above routines to their postscript filter names.
167
   This is how our static routines get called externally. */
168
const op_def zfontenum_op_defs[] = {
169
    {"0.getnativefonts", z_fontenum},
170
    op_def_end(0)
171
};