summaryrefslogtreecommitdiff
path: root/base/spsdf.c
blob: 156421e6b7dad9be14eff48f8b450a3fd38d6845 (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
/* Copyright (C) 2001-2023 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.,  39 Mesa Street, Suite 108A, San Francisco,
   CA 94129, USA, for further information.
*/


/* Common utilities for PostScript and PDF format printing */
#include "stdio_.h"		/* for stream.h */
#include "string_.h"
#include "gstypes.h"
#include "gsmemory.h"
#include "gserrors.h"
#include "spprint.h"
#include "spsdf.h"
#include "stream.h"
#include "strimpl.h"
#include "sa85x.h"
#include "sstring.h"
#include "scanchar.h"

/*
 * Write a string in its shortest form ( () or <> ).  Note that
 * this form is different depending on whether binary data are allowed.
 * Currently we don't support ASCII85 strings ( <~ ~> ).
 */
void
s_write_ps_string(stream * s, const byte * str, uint size, int print_ok)
{
    uint added = 0;
    uint i;
    const stream_template *templat;
    stream_AXE_state state;
    stream_state *st = NULL;

    if (print_ok & PRINT_BINARY_OK) {
        /* Only need to escape (, ), \, CR, EOL. */
        stream_putc(s, '(');
        for (i = 0; i < size; ++i) {
            byte ch = str[i];

            switch (ch) {
                case char_CR:
                    stream_puts(s, "\\r");
                    continue;
                case char_EOL:
                    stream_puts(s, "\\n");
                    continue;
                case '(':
                case ')':
                case '\\':
                    stream_putc(s, '\\');
            }
            stream_putc(s, ch);
        }
        stream_putc(s, ')');
        return;
    }
    for (i = 0; i < size; ++i) {
        byte ch = str[i];

        if (ch == 0 || ch >= 127)
            added += 3;
        else if (strchr("()\\\n\r\t\b\f", ch) != 0)
            ++added;
        else if (ch < 32)
            added += 3;
    }

    if (added < size || (print_ok & PRINT_HEX_NOT_OK)) {
        /* More efficient, or mandatory, to represent as PostScript string. */
        templat = &s_PSSE_template;
        stream_putc(s, '(');
    } else {
        /* More efficient, and permitted, to represent as hex string. */
        templat = &s_AXE_template;
        st = (stream_state *) & state;
        s_AXE_init_inline(&state);
        stream_putc(s, '<');
    }

    {
        byte buf[100];		/* size is arbitrary */
        stream_cursor_read r;
        stream_cursor_write w;
        int status;

        stream_cursor_read_init(&r, str, size);
        do {
            stream_cursor_write_init(&w, buf, sizeof(buf));

            status = (*templat->process) (st, &r, &w, true);
            stream_write(s, buf, (uint) (w.ptr + 1 - buf));
        }
        while (status == 1);
    }
}

/* Set up a write stream that just keeps track of the position. */
int
s_alloc_position_stream(stream ** ps, gs_memory_t * mem)
{
    stream *s = *ps = s_alloc(mem, "s_alloc_position_stream");

    if (s == 0)
        return_error(gs_error_VMerror);
    swrite_position_only(s);
    return 0;
}

/* ---------------- Parameter printing ---------------- */

private_st_printer_param_list();
const param_printer_params_t param_printer_params_default = {
    param_printer_params_default_values
};

/* We'll implement the other printers later if we have to. */
static param_proc_xmit_typed(param_print_typed);
/*static param_proc_begin_xmit_collection(param_print_begin_collection);*/
/*static param_proc_end_xmit_collection(param_print_end_collection);*/
static const gs_param_list_procs printer_param_list_procs = {
    param_print_typed,
    NULL /* begin_collection */ ,
    NULL /* end_collection */ ,
    NULL /* get_next_key */ ,
    gs_param_request_default,
    gs_param_requested_default
};

int
s_init_param_printer(printer_param_list_t *prlist,
                     const param_printer_params_t * ppp, stream * s)
{
    gs_param_list_init((gs_param_list *)prlist, &printer_param_list_procs,
                       NULL);
    prlist->strm = s;
    prlist->params = *ppp;
    prlist->any = false;
    return 0;
}
int
s_alloc_param_printer(gs_param_list ** pplist,
                      const param_printer_params_t * ppp, stream * s,
                      gs_memory_t * mem)
{
    printer_param_list_t *prlist =
        gs_alloc_struct(mem, printer_param_list_t, &st_printer_param_list,
                        "s_alloc_param_printer");
    int code;

    *pplist = (gs_param_list *)prlist;
    if (prlist == 0)
        return_error(gs_error_VMerror);
    code = s_init_param_printer(prlist, ppp, s);
    prlist->memory = mem;
    return code;
}

void
s_release_param_printer(printer_param_list_t *prlist)
{
    if (prlist) {
        if (prlist->any && prlist->params.suffix)
            stream_puts(prlist->strm, prlist->params.suffix);
    }
}
void
s_free_param_printer(gs_param_list * plist)
{
    if (plist) {
        printer_param_list_t *const prlist = (printer_param_list_t *) plist;

        s_release_param_printer(prlist);
        gs_free_object(prlist->memory, plist, "s_free_param_printer");
    }
}

static int
param_print_typed(gs_param_list * plist, gs_param_name pkey,
                  gs_param_typed_value * pvalue)
{
    printer_param_list_t *const prlist = (printer_param_list_t *)plist;
    stream *s = prlist->strm;

    if (!prlist->any) {
        if (prlist->params.prefix)
            stream_puts(s, prlist->params.prefix);
        prlist->any = true;
    }
    if (prlist->params.item_prefix)
        stream_puts(s, prlist->params.item_prefix);
    pprints1(s, "/%s", pkey);
    switch (pvalue->type) {
        case gs_param_type_null:
            stream_puts(s, " null");
            break;
        case gs_param_type_bool:
            stream_puts(s, (pvalue->value.b ? " true" : " false"));
            break;
        case gs_param_type_int:
            pprintd1(s, " %d", pvalue->value.i);
            break;
        case gs_param_type_long:
            pprintld1(s, " %ld", pvalue->value.l);
            break;
        case gs_param_type_size_t:
            pprintzd1(s, " %"PRIdSIZE, pvalue->value.z);
            break;
        case gs_param_type_i64:
            pprinti64d1(s, " %"PRId64, pvalue->value.i64);
            break;
        case gs_param_type_float:
            pprintg1(s, " %g", pvalue->value.f);
            break;
        case gs_param_type_string:
            s_write_ps_string(s, pvalue->value.s.data, pvalue->value.s.size,
                              prlist->params.print_ok);
            break;
        case gs_param_type_name:
            /****** SHOULD USE #-ESCAPES FOR PDF ******/
            stream_putc(s, '/');
            stream_write(s, pvalue->value.n.data, pvalue->value.n.size);
            break;
        case gs_param_type_int_array:
            {
                uint i;
                char sepr = (pvalue->value.ia.size <= 10 ? ' ' : '\n');

                stream_putc(s, '[');
                for (i = 0; i < pvalue->value.ia.size; ++i) {
                    pprintd1(s, "%d", pvalue->value.ia.data[i]);
                    stream_putc(s, sepr);
                }
                stream_putc(s, ']');
            }
            break;
        case gs_param_type_float_array:
            {
                uint i;
                char sepr = (pvalue->value.fa.size <= 10 ? ' ' : '\n');

                stream_putc(s, '[');
                for (i = 0; i < pvalue->value.fa.size; ++i) {
                    pprintg1(s, "%g", pvalue->value.fa.data[i]);
                    stream_putc(s, sepr);
                }
                stream_putc(s, ']');
            }
            break;
            /*case gs_param_type_string_array: */
            /*case gs_param_type_name_array: */
        default:
            return_error(gs_error_typecheck);
    }
    if (prlist->params.item_suffix)
        stream_puts(s, prlist->params.item_suffix);
    return 0;
}