summaryrefslogtreecommitdiff
path: root/pcl/pxl/pxtop.c
blob: b9a6291f335cd65f3b2f0487f32ba3e34e22806f (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
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
/* Copyright (C) 2001-2022 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.
*/


/* pxtop.c */
/* Top-level API implementation of PCL/XL */

#include "stdio_.h"
#include "string_.h"
#include "gdebug.h"
#include "gserrors.h"
#include "gstypes.h"
#include "gsmemory.h"
#include "gsstate.h"            /* must precede gsdevice.h */
#include "gsdevice.h"
#include "gsstruct.h"           /* for gxalloc.h */
#include "gspaint.h"
#include "gsfont.h"
#include "gxalloc.h"
#include "gxstate.h"
#include "gxdevice.h"
#include "plparse.h"
#include "pxattr.h"             /* for pxparse.h */
#include "pxerrors.h"
#include "pxoper.h"
#include "pxstate.h"
#include "pxfont.h"
#include "pxvalue.h"            /* for pxparse.h */
#include "pxparse.h"
#include "pxptable.h"
#include "pxstate.h"
#include "pltop.h"
#include "plmain.h"
#include "gsicc_manage.h"
#include "gxdevsop.h"

/* Imported operators */
px_operator_proc(pxEndPage);
px_operator_proc(pxBeginSession);

/* Forward decls */
static int pxl_end_page_top(px_state_t * pxs, int num_copies, int flush);

static int px_top_init(px_parser_state_t * st, px_state_t * pxs,
                       bool big_endian);

/* ------------ PCXL stream header processor ------- */
/* State used to process an XL stream header */
typedef struct px_stream_header_process_s
{
    enum { PSHPReady, PSHPSkipping, PSHPDone } state;
    px_parser_state_t *st;      /* parser state to refer to */
    px_state_t *pxs;            /* xl state to refer to */
} px_stream_header_process_t;

/* Initialize stream header processor */
static void
px_stream_header_init(px_stream_header_process_t * process,
                      px_parser_state_t * st,
                      px_state_t * pxs)
{
    process->state = PSHPReady;
    process->st = st;
    process->pxs = pxs;
}

/* Process stream header input */
/* ret -ve error, 0 if needs more input, 1 if done successfully */
static int
px_stream_header_process(px_stream_header_process_t * process,
                         stream_cursor_read * cursor)
{
    while (cursor->ptr != cursor->limit) {
        switch (process->state) {
        case PSHPReady:
            process->state = PSHPSkipping;  /* switch to next state */
            switch ((*++cursor->ptr)) {
            case '(':
                px_top_init(process->st, process->pxs, true);
                break;
            case ')':
                px_top_init(process->st, process->pxs, false);
                break;
            default:
                /* Initialize state to avoid confusion */
                px_top_init(process->st, process->pxs, true);
                return gs_note_error(errorUnsupportedBinding);
            }
            break;
        case PSHPSkipping:
            if ((*++cursor->ptr) == '\n') {
                process->state = PSHPDone;
                return 1;
            }
            break;
        case PSHPDone:
        default:
            /* Shouldn't ever come here */
            return gs_note_error(errorIllegalStreamHeader);
        }
    }
    return 0;                   /* need more input */
}

/* De-initialize stream header processor */
static void
px_stream_header_dnit(px_stream_header_process_t * process)
{
    /* empty proc */
}

/*
 * PXL interpreter instance: derived from pl_interp_implementation_t
 */
typedef struct pxl_interp_instance_s
{
    gs_memory_t *memory;        /* memory allocator to use */
    px_parser_state_t *st;      /* parser state */
    px_state_t *pxs;            /* interp state */
    gs_gstate *pgs;             /* graphics state */
    enum {
        PSHeader,
        PSXL,
        PSDone
    } processState;             /* interp's processing state */
    px_stream_header_process_t headerState;        /* used to decode stream header */
} pxl_interp_instance_t;

static int
pxl_detect_language(const char *s, int len)
{
   if (len < 11)
       return 0;
   if (memcmp(s, ") HP-PCL XL", 11) == 0)
       return 100;
   return 0;
}

/* Get implementation's characteristics */
static const pl_interp_characteristics_t *      /* always returns a descriptor */
pxl_impl_characteristics(const pl_interp_implementation_t * impl)        /* implementation of interpreter to alloc */
{
    /* version and build date are not currently used */
#define PXLVERSION NULL
#define PXLBUILDDATE NULL
    static pl_interp_characteristics_t pxl_characteristics = {
        "PCLXL",
        pxl_detect_language,
    };
    return &pxl_characteristics;
}

/* Do per-instance interpreter allocation/init. No device is set yet */
static int                      /* ret 0 ok, else -ve error code */
pxl_impl_allocate_interp_instance(pl_interp_implementation_t *impl,
                                  gs_memory_t * mem)
{
    /* Allocate everything up front */
    pxl_interp_instance_t *pxli      /****** SHOULD HAVE A STRUCT DESCRIPTOR ******/
        = (pxl_interp_instance_t *) gs_alloc_bytes(mem,
                                                   sizeof
                                                   (pxl_interp_instance_t),
                                                   "pxl_allocate_interp_instance(pxl_interp_instance_t)");
    gs_gstate *pgs = gs_gstate_alloc(mem);
    px_parser_state_t *st = px_process_alloc(mem);      /* parser init, cheap */
    px_state_t *pxs = px_state_alloc(mem);      /* inits interp state, potentially expensive */

    /* If allocation error, deallocate & return */
    if (!pxli || !pgs || !st || !pxs) {
        if (pxli)
            gs_free_object(mem, pxli,
                           "pxl_impl_allocate_interp_instance(pxl_interp_instance_t)");
        if (pgs)
            gs_gstate_free(pgs);
        if (st)
            px_process_release(st);
        if (pxs)
            px_state_release(pxs);
        return gs_error_VMerror;
    }
    gsicc_init_iccmanager(pgs);

    /* Setup pointers to allocated mem within instance */
    pxli->memory = mem;
    pxli->pgs = pgs;
    pxli->pxs = pxs;
    pxli->st = st;

    /* General init of pxl_state */
    px_state_init(pxs, pgs);    /*pgs only needed to set pxs as pgs' client */
    pxs->client_data = pxli;
    pxs->end_page = pxl_end_page_top;   /* after px_state_init */

    pxs->pjls = pl_main_get_pjl_instance(mem);

    /* The PCL instance is needed for PassThrough mode */
    pxs->pcls = pl_main_get_pcl_instance(mem);

    /* Return success */
    impl->interp_client_data = pxli;
    return 0;
}

static int
pxl_set_icc_params(pl_interp_implementation_t * impl, gs_gstate * pgs)
{
    pxl_interp_instance_t *pxli  = impl->interp_client_data;
    return pl_set_icc_params(pxli->memory, pgs);
}

/*
 * Set a Boolean parameter.
 */
static int
put_param_bool(pxl_interp_instance_t *pxli, gs_param_name pkey, bool value)
{
    gs_c_param_list list;
    int code;

    gs_c_param_list_write(&list, pxli->memory);
    code = param_write_bool((gs_param_list *) & list, pkey, &value);
    if (code >= 0) {
        gs_c_param_list_read(&list);

        code = gs_gstate_putdeviceparams(pxli->pgs,
                                         gs_currentdevice(pxli->pgs),
                                         (gs_param_list *) &list);
    }

    gs_c_param_list_release(&list);
    return code;
}

/* Prepare interp instance for the next "job" */
/* ret 0 ok, else -ve error code */
static int
pxl_impl_init_job(pl_interp_implementation_t * impl,
                  gx_device                  * device)
{
    int code = 0;
    pxl_interp_instance_t *pxli = impl->interp_client_data;
    px_state_t *pxs = pxli->pxs;
    gs_memory_t *mem = pxli->memory;

    enum { Sbegin, Ssetdevice, Sinitg, Sgsave, Serase, Sdone } stage;

    px_reset_errors(pxli->pxs);
    px_process_init(pxli->st, true);

    /* set input status to: expecting stream header */
    px_stream_header_init(&pxli->headerState, pxli->st, pxli->pxs);
    pxli->processState = PSHeader;

    stage = Sbegin;

    pxs->interpolate = pl_main_get_interpolate(mem);
    pxs->nocache = pl_main_get_nocache(mem);
    gs_setscanconverter(pxli->pgs, pl_main_get_scanconverter(mem));

    if (pxs->nocache)
        gs_setcachelimit(pxs->font_dir, 0);

    /* Set the device into the gstate */
    stage = Ssetdevice;
    if ((code = gs_setdevice_no_erase(pxli->pgs, device)) < 0)  /* can't erase yet */
        goto pisdEnd;

    /* Warn the device that PXL uses ROPs. */
    if (code == 0) {
        code = put_param_bool(pxli, "LanguageUsesROPs", true);

        if (!device->is_open)
            code = gs_opendevice(device);
    }

    /* Init XL graphics */
    stage = Sinitg;
    if ((code = px_initgraphics(pxli->pxs)) < 0)
        goto pisdEnd;

    code = pxl_set_icc_params(impl, pxli->pgs);
    if (code < 0)
        goto pisdEnd;

    /* Do inits of gstate that may be reset by setdevice */
    gs_setaccuratecurves(pxli->pgs, true);      /* All H-P languages want accurate curves. */

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

    stage = Serase;
    if ((code = gs_erasepage(pxli->pgs)) < 0)
        goto pisdEnd;

    stage = Sdone;              /* success */
    /* Unwind any errors */
  pisdEnd:
    switch (stage) {
        case Sdone:            /* don't undo success */
            break;

        case Serase:           /* gs_erasepage failed */
            /* undo  gsave */
            gs_grestore_only(pxli->pgs);        /* destroys gs_save stack */
            /* fall thru to next */
        case Sgsave:           /* gsave failed */
        case Sinitg:
            /* undo setdevice */
            gs_nulldevice(pxli->pgs);
            /* fall thru to next */

        case Ssetdevice:       /* gs_setdevice failed */
        case Sbegin:           /* nothing left to undo */
            break;
    }

    return code;
}

/* Do any setup for parser per-cursor */
static int
pxl_impl_process_begin(pl_interp_implementation_t * impl)
{
    return 0;
}

/* Parse a cursor-full of data */
/* ret 0 or +ve if ok, else -ve error code */
static int
pxl_impl_process(pl_interp_implementation_t * impl,
                 stream_cursor_read * cursor)
{
    pxl_interp_instance_t *pxli = impl->interp_client_data;
    int code = 0;

    /* Process some input */
    switch (pxli->processState) {
    case PSDone:
        return e_ExitLanguage;
    case PSHeader:         /* Input stream header */
        code = px_stream_header_process(&pxli->headerState, cursor);
        if (code == 0)
            break;          /* need more input later */
        else if (code < 0) {        /* stream header termination */
            pxli->processState = PSDone;
            return code;    /* return error */
        } else {
            pxli->processState = PSXL;
        }
        /* fall thru to PSXL */
    case PSXL:             /* PCL XL */
        code = px_process(pxli->st, pxli->pxs, cursor);
        if (code == e_ExitLanguage) {
            pxli->processState = PSDone;
        } else if (code == errorWarningsReported) {
            /* The parser doesn't skip over the EndSession */
            /* operator, because an "error" occurred. */
            cursor->ptr++;
        } else if (code < 0)
            /* Map library error codes to PCL XL codes when possible. */
            switch (code) {
            case gs_error_invalidfont:
                code = errorIllegalFontData;
                break;
            case gs_error_limitcheck:
                code = errorInternalOverflow;
                break;
            case gs_error_nocurrentpoint:
                code = errorCurrentCursorUndefined;
                break;
            case gs_error_rangecheck:
                code = errorIllegalAttributeValue;
                break;
            case gs_error_VMerror:
                code = errorInsufficientMemory;
                break;
            }
        break;              /* may need more input later */
    }
    return code;
}

static int
pxl_impl_process_end(pl_interp_implementation_t * impl)
{
    return 0;
}


/* Skip to end of job ret 1 if done, 0 ok but EOJ not found, else -ve error code */
static int
pxl_impl_flush_to_eoj(pl_interp_implementation_t * impl,
                      stream_cursor_read * cursor)
{
    const byte *p = cursor->ptr;
    const byte *rlimit = cursor->limit;

    /* Skip to, but leave UEL in buffer for PJL to find later */
    for (; p < rlimit; ++p)
        if (p[1] == '\033') {
            uint avail = rlimit - p;

            if (memcmp(p + 1, "\033%-12345X", min(avail, 9)))
                continue;
            if (avail < 9)
                break;
            cursor->ptr = p;
            return 1;           /* found eoj */
        }
    cursor->ptr = p;
    return 0;                   /* need more */
}

/* Parser action for end-of-file */
/* ret 0 or +ve if ok, else -ve error code */
static int
pxl_impl_process_eof(pl_interp_implementation_t * impl)
{
    pxl_interp_instance_t *pxli = impl->interp_client_data;

    px_state_cleanup(pxli->pxs);
    px_process_init(pxli->st, true);

    return 0;
}

/* Report any errors after running a job */
/* ret 0 ok, else -ve error code */
static int
pxl_impl_report_errors(pl_interp_implementation_t * impl,
                       int code,
                       long file_position,
                       bool force_to_cout)
{
    pxl_interp_instance_t *pxli = impl->interp_client_data;
    px_parser_state_t *st = pxli->st;
    px_state_t *pxs = pxli->pxs;
    int report = pxs->error_report;
    const char *subsystem = (code <= px_error_next ? "KERNEL" : "GRAPHICS");
    char message[px_max_error_line + 1];
    int N = 0;
    int y = 0;

    if (code >= 0)
        return code;            /* not really an error */
    if (report & eErrorPage) {
        int ecode = px_begin_error_page(pxs, &y);
        if (ecode < 0) {
            return ecode;
        }
    }
    while ((N = px_error_message_line(message, N, subsystem,
                                      code, st, pxs)) >= 0) {
        if ((report & eBackChannel) || force_to_cout)
            errprintf(pxli->memory, "%s", message);
        if (report & eErrorPage)
            y = px_error_page_show(message, y, pxs);
    }
    if (((report & pxeErrorReport_next) && file_position != -1L)
        || force_to_cout)
        errprintf(pxli->memory, "file position of error = %ld\n",
                  file_position);
    if (report & eErrorPage) {
        px_args_t args;

        args.pv[0] = 0;
        pxEndPage(&args, pxs);
    }
    px_reset_errors(pxs);

    return code;
}

/* Wrap up interp instance after a "job" */
/* ret 0 ok, else -ve error code */
static int
pxl_impl_dnit_job(pl_interp_implementation_t * impl)
{
    int code;
    pxl_interp_instance_t *pxli = impl->interp_client_data;
    gx_device *device = gs_currentdevice(pxli->pgs);

    px_stream_header_dnit(&pxli->headerState);
    px_state_cleanup(pxli->pxs);
    px_process_init(pxli->st, true);
    /* return to original gstate  */
    code = gs_grestore_only(pxli->pgs);        /* destroys gs_save stack */

    /* Warn the device that ROP usage has come to an end */
    if (code >= 0) {
        code = put_param_bool(pxli, "LanguageUsesROPs", false);

        if (!device->is_open)
            code = gs_opendevice(device);
    }

    return code;
}

/* Deallocate a interpreter instance */
/* ret 0 ok, else -ve error code */
static int
pxl_impl_deallocate_interp_instance(pl_interp_implementation_t * impl)
{
    pxl_interp_instance_t *pxli = impl->interp_client_data;
    gs_memory_t *mem = pxli->memory;

    px_dict_release(&pxli->pxs->font_dict);
    px_dict_release(&pxli->pxs->builtin_font_dict);
    /* do total dnit of interp state */
    px_state_finit(pxli->pxs);
    /* free halftone cache */
    gs_gstate_free(pxli->pgs);
    px_process_release(pxli->st);
    px_state_release(pxli->pxs);
    gs_free_object(mem, pxli,
                   "pxl_impl_deallocate_interp_instance(pxl_interp_instance_t)");
    return 0;
}

/*
 * End-of-page called back by PXL
 */
static int
pxl_end_page_top(px_state_t * pxls, int num_copies, int flush)
{
    return pl_finish_page(pxls->memory->gs_lib_ctx->top_of_system,
                          pxls->pgs, num_copies, flush);
}

/* Parser implementation descriptor */
pl_interp_implementation_t pxl_implementation = {
    pxl_impl_characteristics,
    pxl_impl_allocate_interp_instance,
    NULL,                       /* get_device_memory */
    NULL,                       /* set_param */
    NULL,                       /* add_path */
    NULL,                       /* post_args_init */
    pxl_impl_init_job,
    NULL,                       /* run_prefix_commands */
    NULL,                       /* process_file */
    pxl_impl_process_begin,
    pxl_impl_process,
    pxl_impl_process_end,
    pxl_impl_flush_to_eoj,
    pxl_impl_process_eof,
    pxl_impl_report_errors,
    pxl_impl_dnit_job,
    pxl_impl_deallocate_interp_instance,
    NULL,                       /* pxl_impl_reset */
    NULL                        /* interp_client_data */
};

/* ---------- Utility Procs ----------- */
/* Initialize the parser state, and session parameters in case we get */
/* an error before the session is established. */
static int
px_top_init(px_parser_state_t * st, px_state_t * pxs, bool big_endian)
{
    px_args_t args;
    px_value_t v[3];

    px_process_init(st, big_endian);

    /* Measure */
    v[0].type = pxd_scalar | pxd_ubyte;
    v[0].value.i = eInch;
    args.pv[0] = &v[0];
    /* UnitsPerMeasure */
    v[1].type = pxd_xy | pxd_uint16;
    v[1].value.ia[0] = v[1].value.ia[1] = 100;  /* arbitrary */
    args.pv[1] = &v[1];
    /* ErrorReporting */
    v[2].type = pxd_scalar | pxd_ubyte;
    v[2].value.i = eErrorPage;
    args.pv[2] = &v[2];
    {
        int code = pxBeginSession(&args, pxs);

        if (code < 0)
            return code;
    }
    return 0;
}