summaryrefslogtreecommitdiff
path: root/svg/svgtop.c
blob: ac11a4af005fd5f03530018d0b0dbf9cdd3f4e02 (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
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
/* Copyright (C) 2008 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.
*/

/* Top-level API implementation of Scalable Vector Graphics (SVG) */

/* Language wrapper implementation (see pltop.h) */

#include "ghostsvg.h"

#include "pltop.h"

#include "gxdevice.h" /* so we can include gxht.h below */
#include "gxht.h" /* gsht1.h is incomplete, we need storage size of gs_halftone */
#include "gsht1.h"

static int svg_install_halftone(svg_context_t *ctx, gx_device *pdevice);

#define SVG_PARSER_MIN_INPUT_SIZE 8192

/*
 * The SVG interpeter is identical to pl_interp_t.
 * The SVG interpreter instance is derived from pl_interp_instance_t.
 */

typedef struct svg_interp_instance_s svg_interp_instance_t;

struct svg_interp_instance_s
{
    pl_interp_instance_t pl;		/* common part: must be first */

    pl_page_action_t pre_page_action;   /* action before page out */
    void *pre_page_closure;		/* closure to call pre_page_action with */
    pl_page_action_t post_page_action;  /* action before page out */
    void *post_page_closure;		/* closure to call post_page_action with */

    svg_context_t *ctx;
};

/* version and build date are not currently used */
#define SVG_VERSION NULL
#define SVG_BUILD_DATE NULL

static const pl_interp_characteristics_t *
svg_imp_characteristics(const pl_interp_implementation_t *pimpl)
{
    static pl_interp_characteristics_t svg_characteristics =
    {
        "SVG",
        "<?xml", /* string to recognize SVG files */
        "Artifex",
        SVG_VERSION,
        SVG_BUILD_DATE,
        SVG_PARSER_MIN_INPUT_SIZE, /* Minimum input size */
    };
    return &svg_characteristics;
}

static int
svg_imp_allocate_interp(pl_interp_t **ppinterp,
        const pl_interp_implementation_t *pimpl,
        gs_memory_t *pmem)
{
    static pl_interp_t interp; /* there's only one interpreter */
    *ppinterp = &interp;
    return 0;
}

/* Do per-instance interpreter allocation/init. No device is set yet */
static int
svg_imp_allocate_interp_instance(pl_interp_instance_t **ppinstance,
        pl_interp_t *pinterp,
        gs_memory_t *pmem)
{
    svg_interp_instance_t *instance;
    svg_context_t *ctx;
    gs_state *pgs;

    dputs("-- svg_imp_allocate_interp_instance --\n");

    instance = (svg_interp_instance_t *) gs_alloc_bytes(pmem,
            sizeof(svg_interp_instance_t), "svg_imp_allocate_interp_instance");

    ctx = (svg_context_t *) gs_alloc_bytes(pmem,
            sizeof(svg_context_t), "svg_imp_allocate_interp_instance");

    pgs = gs_state_alloc(pmem);
    gsicc_init_iccmanager(pgs);

    memset(ctx, 0, sizeof(svg_context_t));

    if (!instance || !ctx || !pgs)
    {
        if (instance)
            gs_free_object(pmem, instance, "svg_imp_allocate_interp_instance");
        if (ctx)
            gs_free_object(pmem, ctx, "svg_imp_allocate_interp_instance");
        if (pgs)
            gs_state_free(pgs);
        return gs_error_VMerror;
    }

    ctx->instance = instance;
    ctx->memory = pmem;
    ctx->pgs = pgs;
    ctx->fontdir = NULL;

    /* TODO: load some builtin ICC profiles here */
    ctx->srgb = gs_cspace_new_DeviceRGB(ctx->memory);

    ctx->fill_rule = 1; /* 0=evenodd, 1=nonzero */

    ctx->fill_is_set = 0;
    ctx->fill_color[0] = 0.0;
    ctx->fill_color[1] = 0.0;
    ctx->fill_color[2] = 0.0;

    ctx->stroke_is_set = 0;
    ctx->stroke_color[0] = 0.0;
    ctx->stroke_color[1] = 0.0;
    ctx->stroke_color[2] = 0.0;

    instance->pre_page_action = 0;
    instance->pre_page_closure = 0;
    instance->post_page_action = 0;
    instance->post_page_closure = 0;

    instance->ctx = ctx;

    // XXX - svg_init_font_cache(ctx);

    *ppinstance = (pl_interp_instance_t *)instance;

    return 0;
}

/* Set a client language into an interperter instance */
static int
svg_imp_set_client_instance(pl_interp_instance_t *pinstance,
        pl_interp_instance_t *pclient,
        pl_interp_instance_clients_t which_client)
{
    /* ignore */
    return 0;
}

static int
svg_imp_set_pre_page_action(pl_interp_instance_t *pinstance,
        pl_page_action_t action, void *closure)
{
    svg_interp_instance_t *instance = (svg_interp_instance_t *)pinstance;
    instance->pre_page_action = action;
    instance->pre_page_closure = closure;
    return 0;
}

static int
svg_imp_set_post_page_action(pl_interp_instance_t *pinstance,
        pl_page_action_t action, void *closure)
{
    svg_interp_instance_t *instance = (svg_interp_instance_t *)pinstance;
    instance->post_page_action = action;
    instance->post_page_closure = closure;
    return 0;
}

static int
svg_imp_set_device(pl_interp_instance_t *pinstance, gx_device *pdevice)
{
    svg_interp_instance_t *instance = (svg_interp_instance_t *)pinstance;
    svg_context_t *ctx = instance->ctx;
    int code;

    dputs("-- svg_imp_set_device --\n");

    gs_opendevice(pdevice);

    code = gsicc_init_device_profile(ctx->pgs, pdevice);
    if (code < 0)
        return code;

    code = gs_setdevice_no_erase(ctx->pgs, pdevice);
    if (code < 0)
        goto cleanup_setdevice;

    gs_setaccuratecurves(ctx->pgs, true);  /* NB not sure */

    /* gsave and grestore (among other places) assume that */
    /* there are at least 2 gstates on the graphics stack. */
    /* Ensure that now. */
    code = gs_gsave(ctx->pgs);
    if (code < 0)
        goto cleanup_gsave;

    code = gs_erasepage(ctx->pgs);
    if (code < 0)
        goto cleanup_erase;

    code = svg_install_halftone(ctx, pdevice);
    if (code < 0)
        goto cleanup_halftone;

    return 0;

cleanup_halftone:
cleanup_erase:
    /* undo gsave */
    gs_grestore_only(ctx->pgs);     /* destroys gs_save stack */

cleanup_gsave:
    /* undo setdevice */
    gs_nulldevice(ctx->pgs);

cleanup_setdevice:
    /* nothing to undo */
    return code;
}

static int
svg_imp_get_device_memory(pl_interp_instance_t *pinstance, gs_memory_t **ppmem)
{
    /* huh? we do nothing here */
    return 0;
}

static int
svg_imp_process_file(pl_interp_instance_t *pinstance, char *filename)
{
    svg_interp_instance_t *instance = (svg_interp_instance_t *)pinstance;
    svg_context_t *ctx = instance->ctx;
    svg_item_t *root;
    FILE *file;
    int code;
    char buf[4096];
    int n;

    dprintf1("svg_imp_process_file %s\n", filename);

    file = fopen(filename, "rb");
    if (!file)
        return gs_error_ioerror;

    while (!feof(file))
    {
        n = fread(buf, 1, sizeof buf, file);
        if (n == 0)
            if (ferror(file))
                return gs_error_ioerror;
        code = svg_feed_xml_parser(ctx, buf, n);
        if (code < 0)
        {
            gs_catch(code, "cannot parse SVG document");
            fclose(file);
            return code;
        }
    }

    root = svg_close_xml_parser(ctx);
    if (!root)
    {
        gs_catch(-1, "cannot parse xml document");
        fclose(file);
        return -1;
    }

    code = svg_parse_document(ctx, root);
    if (code < 0)
        gs_catch(code, "cannot parse SVG document");

    svg_free_item(ctx, root);
    fclose(file);
    return code;
}

/* Parse a cursor-full of data */
static int
svg_imp_process(pl_interp_instance_t *pinstance, stream_cursor_read *buf)
{
    svg_interp_instance_t *instance = (svg_interp_instance_t *)pinstance;
    svg_context_t *ctx = instance->ctx;
    int code;

    code = svg_feed_xml_parser(ctx, (const char*)buf->ptr + 1, buf->limit - buf->ptr);
    if (code < 0)
        gs_catch(code, "cannot parse SVG document");

    buf->ptr = buf->limit;

    return code;
}

/* Skip to end of job.
 * Return 1 if done, 0 ok but EOJ not found, else negative error code.
 */
static int
svg_imp_flush_to_eoj(pl_interp_instance_t *pinstance, stream_cursor_read *pcursor)
{
    /* assume SVG cannot be pjl embedded */
    pcursor->ptr = pcursor->limit;
    return 0;
}

/* Parser action for end-of-file */
static int
svg_imp_process_eof(pl_interp_instance_t *pinstance)
{
    svg_interp_instance_t *instance = (svg_interp_instance_t *)pinstance;
    svg_context_t *ctx = instance->ctx;
    svg_item_t *root;
    int code;

    dputs("-- svg_imp_process_eof --\n");

    root = svg_close_xml_parser(ctx);
    if (!root)
        return gs_rethrow(-1, "cannot parse xml document");

    code = svg_parse_document(ctx, root);
    if (code < 0)
        gs_catch(code, "cannot parse SVG document");

    svg_free_item(ctx, root);

    return code;
}

/* Report any errors after running a job */
static int
svg_imp_report_errors(pl_interp_instance_t *pinstance,
        int code,           /* prev termination status */
        long file_position, /* file position of error, -1 if unknown */
        bool force_to_cout  /* force errors to cout */
        )
{
    return 0;
}

/* Prepare interp instance for the next "job" */
static int
svg_imp_init_job(pl_interp_instance_t *pinstance)
{
    svg_interp_instance_t *instance = (svg_interp_instance_t *)pinstance;
    svg_context_t *ctx = instance->ctx;

    dputs("-- svg_imp_init_job --\n");

    return svg_open_xml_parser(ctx);
}

/* Wrap up interp instance after a "job" */
static int
svg_imp_dnit_job(pl_interp_instance_t *pinstance)
{
    dputs("-- svg_imp_dnit_job --\n");

    return 0;
}

/* Remove a device from an interperter instance */
static int
svg_imp_remove_device(pl_interp_instance_t *pinstance)
{
    svg_interp_instance_t *instance = (svg_interp_instance_t *)pinstance;
    svg_context_t *ctx = instance->ctx;

    int code = 0;       /* first error status encountered */
    int error;

    dputs("-- svg_imp_remove_device --\n");

    /* return to original gstate  */
    gs_grestore_only(ctx->pgs);        /* destroys gs_save stack */

    /* Deselect device */
    /* NB */
    error = gs_nulldevice(ctx->pgs);
    if (code >= 0)
        code = error;

    return code;
}

/* Deallocate a interpreter instance */
static int
svg_imp_deallocate_interp_instance(pl_interp_instance_t *pinstance)
{
    svg_interp_instance_t *instance = (svg_interp_instance_t *)pinstance;
    svg_context_t *ctx = instance->ctx;
    gs_memory_t *mem = ctx->memory;

    dputs("-- svg_imp_deallocate_interp_instance --\n");

    /* language clients don't free the font cache machinery */

    // free gstate?
    gs_free_object(mem, ctx, "svg_imp_deallocate_interp_instance");
    gs_free_object(mem, instance, "svg_imp_deallocate_interp_instance");

    return 0;
}

/* Do static deinit of SVG interpreter */
static int
svg_imp_deallocate_interp(pl_interp_t *pinterp)
{
    /* nothing to do */
    return 0;
}

/* Parser implementation descriptor */
const pl_interp_implementation_t svg_implementation =
{
    svg_imp_characteristics,
    svg_imp_allocate_interp,
    svg_imp_allocate_interp_instance,
    svg_imp_set_client_instance,
    svg_imp_set_pre_page_action,
    svg_imp_set_post_page_action,
    svg_imp_set_device,
    svg_imp_init_job,
    svg_imp_process_file,
    svg_imp_process,
    svg_imp_flush_to_eoj,
    svg_imp_process_eof,
    svg_imp_report_errors,
    svg_imp_dnit_job,
    svg_imp_remove_device,
    svg_imp_deallocate_interp_instance,
    svg_imp_deallocate_interp,
    svg_imp_get_device_memory,
};

/*
 * End-of-page function called by SVG parser.
 */
int
svg_show_page(svg_context_t *ctx, int num_copies, int flush)
{
    pl_interp_instance_t *pinstance = ctx->instance;
    svg_interp_instance_t *instance = ctx->instance;

    int code = 0;

    /* do pre-page action */
    if (instance->pre_page_action)
    {
        code = instance->pre_page_action(pinstance, instance->pre_page_closure);
        if (code < 0)
            return code;
        if (code != 0)
            return 0;    /* code > 0 means abort w/no error */
    }

    /* output the page */
    code = gs_output_page(ctx->pgs, num_copies, flush);
    if (code < 0)
        return code;

    /* do post-page action */
    if (instance->post_page_action)
    {
        code = instance->post_page_action(pinstance, instance->post_page_closure);
        if (code < 0)
            return code;
    }

    return 0;
}

/*
 * We need to install a halftone ourselves, this is not
 * done automatically.
 */

static float
identity_transfer(floatp tint, const gx_transfer_map *ignore_map)
{
    return tint;
}

/* The following is a 45 degree spot screen with the spots enumerated
 * in a defined order. */
static byte order16x16[256] = {
    38, 11, 14, 32, 165, 105, 90, 171, 38, 12, 14, 33, 161, 101, 88, 167,
    30, 6, 0, 16, 61, 225, 231, 125, 30, 6, 1, 17, 63, 222, 227, 122,
    27, 3, 8, 19, 71, 242, 205, 110, 28, 4, 9, 20, 74, 246, 208, 106,
    35, 24, 22, 40, 182, 46, 56, 144, 36, 25, 22, 41, 186, 48, 58, 148,
    152, 91, 81, 174, 39, 12, 15, 34, 156, 95, 84, 178, 40, 13, 16, 34,
    69, 212, 235, 129, 31, 7, 2, 18, 66, 216, 239, 133, 32, 8, 2, 18,
    79, 254, 203, 114, 28, 4, 10, 20, 76, 250, 199, 118, 29, 5, 10, 21,
    193, 44, 54, 142, 36, 26, 23, 42, 189, 43, 52, 139, 37, 26, 24, 42,
    39, 12, 15, 33, 159, 99, 87, 169, 38, 11, 14, 33, 163, 103, 89, 172,
    31, 7, 1, 17, 65, 220, 229, 123, 30, 6, 1, 17, 62, 223, 233, 127,
    28, 4, 9, 20, 75, 248, 210, 108, 27, 3, 9, 19, 72, 244, 206, 112,
    36, 25, 23, 41, 188, 49, 60, 150, 35, 25, 22, 41, 184, 47, 57, 146,
    157, 97, 85, 180, 40, 13, 16, 35, 154, 93, 83, 176, 39, 13, 15, 34,
    67, 218, 240, 135, 32, 8, 3, 19, 70, 214, 237, 131, 31, 7, 2, 18,
    78, 252, 197, 120, 29, 5, 11, 21, 80, 255, 201, 116, 29, 5, 10, 21,
    191, 43, 51, 137, 37, 27, 24, 43, 195, 44, 53, 140, 37, 26, 23, 42
};

#define source_phase_x 4
#define source_phase_y 0

static int
svg_install_halftone(svg_context_t *ctx, gx_device *pdevice)
{
    gs_halftone ht;
    gs_string thresh;
    int code;

    int width = 16;
    int height = 16;
    thresh.data = order16x16;
    thresh.size = width * height;

    if (gx_device_must_halftone(pdevice))
    {
        ht.type = ht_type_threshold;
        ht.params.threshold.width = width;
        ht.params.threshold.height = height;
        ht.params.threshold.thresholds.data = thresh.data;
        ht.params.threshold.thresholds.size = thresh.size;
        ht.params.threshold.transfer = 0;
        ht.params.threshold.transfer_closure.proc = 0;

        gs_settransfer(ctx->pgs, identity_transfer);

        code = gs_sethalftone(ctx->pgs, &ht);
        if (code < 0)
            return gs_throw(code, "could not install halftone");

        code = gs_sethalftonephase(ctx->pgs, 0, 0);
        if (code < 0)
            return gs_throw(code, "could not set halftone phase");
    }

    return 0;
}