summaryrefslogtreecommitdiff
path: root/pxl/pxpthr.c
blob: 055cd0326b8400db969ae291ff569fb944957749 (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
/* Portions Copyright (C) 2007 artofcode LLC.
   Portions Copyright (C) 2007 Artifex Software Inc.
   Portions Copyright (C) 2007 Aladdin Enterprises.
   This software is based in part on the work of the Independent JPEG Group.
   All Rights Reserved.

   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., 101 Lucas Valley Road #110,
   San Rafael, CA  94903, (415)492-9861, for further information. */
/*$Id$ */

/* pxpthr.c.c */
/* PCL XL passtrough mode */
#include "stdio_.h"                     /* std.h + NULL */
#include "gsdevice.h"
#include "gstypes.h"
#include "gspath.h"
#include "gscoord.h"
#include "gsstate.h"
#include "gsicc_manage.h"
#include "pcommand.h"
#include "pgmand.h"
#include "pcstate.h"
#include "pcfont.h"
#include "pcparse.h"
#include "pctop.h"
#include "pcpage.h"
#include "pcdraw.h"
#include "pxoper.h"
#include "pxstate.h"
#include "pxgstate.h"
#include "pxpthr.h"
#include "pxparse.h"
#include "plfont.h"

const byte apxPassthrough[] = {0, 0};

/* NB - globals needing cleanup
 */
static pcl_state_t *global_pcs = NULL;
static pcl_parser_state_t global_pcl_parser_state;
static hpgl_parser_state_t global_gl_parser_state;

/* if this is a contiguous passthrough meaning that 2 passtrough
   operators have been given back to back and pxl should not regain
   control. */
static bool global_this_pass_contiguous = false;

/* this is the first passthrough on this page */
static bool global_pass_first = true;

/* store away the current font attributes PCL can't set these,
 * they persist for XL, rotation is missing */
gs_point global_char_shear;
gs_point global_char_scale;
float global_char_bold_value;

/* forward decl */
void pxpcl_release(void);
void pxpcl_pagestatereset(void);



/* NB: tests for this function are used to flag pxl snippet mode
 */
static int
pcl_end_page_noop(pcl_state_t *pcs, int num_copies, int flush)
{
    return pxPassThrough;
}

/* set variables other that setting the page device that do not
   default to pcl reset values */
void
pxPassthrough_pcl_state_nonpage_exceptions(px_state_t *pxs)
{
    /* xl cursor -> pcl cursor position */
    gs_point xlcp, pclcp, dp;
    int code1 = 0;
    int code2 = 0;
    int code3 = 0;

    /* make the pcl ctm active, after resets the hpgl/2 ctm is
       active. */
    pcl_set_graphics_state(global_pcs);
    /* xl current point -> device point -> pcl current
       point.  If anything fails we assume the current
       point is not valid and use the cap from the pcl
       state initialization - pcl's origin */
    if ((code1 = gs_currentpoint(pxs->pgs, &xlcp)) ||
        (code2 = gs_transform(pxs->pgs, xlcp.x, xlcp.y, &dp)) ||
        (code3 = gs_itransform(global_pcs->pgs, dp.x, dp.y, &pclcp)) ) {
        global_pcs->cap.x = 0;
        global_pcs->cap.y = inch2coord(2.0/6.0); /* 1/6" off by 2x in resolution. */
        if (gs_debug_c('i'))
            dprintf2("passthrough: changing cap NO currentpoint (%d, %d) \n",
                     global_pcs->cap.x, global_pcs->cap.y);
    } else {
      if (gs_debug_c('i'))
        dprintf8("passthrough: changing cap from (%d,%d) (%d,%d) (%d, %d) (%d, %d) \n",
                 global_pcs->cap.x, global_pcs->cap.y,
                 (coord)xlcp.x, (coord)xlcp.y,
                 (coord)dp.x, (coord)dp.y,
                 (coord)pclcp.x, (coord)pclcp.y);
      global_pcs->cap.x = (coord)pclcp.x;
      global_pcs->cap.y = (coord)pclcp.y;
    }
    if (global_pcs->underline_enabled)
        global_pcs->underline_start = global_pcs->cap;

    /* Hacked copy of font state; rotation doesn't copy? */
    if ( pxs->pxgs->char_shear.x || pxs->pxgs->char_shear.y ) {
        global_char_shear.x = pxs->pxgs->char_shear.x;
        global_char_shear.y = pxs->pxgs->char_shear.y;
    }
    if ( pxs->pxgs->char_scale.x != 1.0 || pxs->pxgs->char_scale.y != 1.0 ) {
        global_char_scale.x = pxs->pxgs->char_scale.x;
        global_char_scale.y = pxs->pxgs->char_scale.y;
    }
    if ( pxs->pxgs->char_bold_value > 0.001 )
        global_char_bold_value = pxs->pxgs->char_bold_value;

}

/* retrieve the current pcl state and initialize pcl */
static int
pxPassthrough_init(px_state_t *pxs)
{
    int code;

    if (gs_debug_c('i'))
        dprintf("passthrough: initializing global pcl state\n");
    global_pcs = pcl_get_gstate(pxs->pcls);

    /* default to pcl5c */
    global_pcs->personality = 0;
    /* for now we do not support intepolation in XL passthrough mode. */
    global_pcs->interpolate = false;

    /* do an initial reset to set up a permanent reset.  The
       motivation here is to avoid tracking down a slew of memory
       leaks */
    global_pcs->xfm_state.paper_size = pcl_get_default_paper(global_pcs); /* pxl paper ? */
    pcl_do_resets(global_pcs, pcl_reset_initial);
    pcl_do_resets(global_pcs, pcl_reset_permanent);

    /* mark the page as blank and install xl's page device in
       pcl's state */
    global_pcs->page_marked = 0;
    code = gs_setdevice_no_erase(global_pcs->pgs, gs_currentdevice(pxs->pgs));
    if ( code < 0 )
        return code;
    /* Also, check if the device profile was set int the global_pcs pgs.
       If not then initialize. Fix for seg fault with T427.BIN */
#ifdef ICCBRANCH
    gsicc_init_device_profile(global_pcs->pgs, gs_currentdevice(pxs->pgs));
#endif
    /* yet another reset with the new page device */
    pcl_do_resets(global_pcs, pcl_reset_initial);
    /* set the parser state and initialize the pcl parser */
    global_pcl_parser_state.definitions = global_pcs->pcl_commands;
    global_pcl_parser_state.hpgl_parser_state=&global_gl_parser_state;
    pcl_process_init(&global_pcl_parser_state);
    /* default 600 to match XL allow PCL to override */
    global_pcs->uom_cp = 7200L / 600L;
    return 0;
}

void
pxPassthrough_setpagestate(px_state_t *pxs)
{
    /* by definition we are in "snippet mode" if pxl has dirtied
       the page */
    if ( pxs->have_page ) {
        if (gs_debug_c('i'))
            dprintf("passthrough: snippet mode\n");
        /* disable an end page in pcl, also used to flag in snippet mode */
        global_pcs->end_page = pcl_end_page_noop;
        /* set the page size and orientation.  Really just sets
           the page tranformation does not feed a page (see noop
           above) */
        pcl_new_logical_page_for_passthrough(global_pcs, (int)pxs->orientation,
                                             &pxs->media_dims);

        if (gs_debug_c('i'))
            dprintf2("passthrough: snippet mode changing orientation from %d to %d\n",
                     global_pcs->xfm_state.lp_orient, (int)pxs->orientation);

    } else { /* not snippet mode - full page mode */
        /* pcl can feed the page and presumedely pcl commands will
           be used to set pcl's state. */
        global_pcs->end_page = pcl_end_page_top;
        /* clean the pcl page if it was marked by a previous snippet
           and set to full page mode. */
        global_pcs->page_marked = 0;
        pcl_new_logical_page_for_passthrough(global_pcs, (int)pxs->orientation, &pxs->media_dims);
        if (gs_debug_c('i'))
            dprintf("passthrough: full page mode\n");
    }
}

int
pxPassthrough(px_args_t *par, px_state_t *pxs)
{

    stream_cursor_read r;
    int code = 0;
    uint used;

    /* apparently if there is no open data source we open one.  By the
       spec this should already be open, in practice it is not. */
    if ( !pxs->data_source_open ) {
        if (gs_debug_c('i'))
            dprintf("passthrough: data source not open upon entry\n");
        pxs->data_source_open = true;
        pxs->data_source_big_endian = true;
    }

    /* source available is part of the equation to determine if this
       operator is being called for the first time */
    if ( par->source.available == 0 ) {
        if (par->source.phase == 0) {
            if (gs_debug_c('i'))
                dprintf("passthrough starting getting more data\n");

            if ( !global_pcs )
                pxPassthrough_init(pxs);

            /* this is the first passthrough on this page */
            if ( global_pass_first ) {
                pxPassthrough_setpagestate(pxs);
                pxPassthrough_pcl_state_nonpage_exceptions(pxs);
                global_pass_first = false;
            } else {
                /* there was a previous passthrough check if there were
                   any intervening XL commands */
                if ( global_this_pass_contiguous == false )
                    pxPassthrough_pcl_state_nonpage_exceptions(pxs);
            }
            par->source.phase = 1;
        }
        return pxNeedData;
    }

    /* NB cie does not work yet.  I am not sure if this state value
       should come from xl or pjl */
    global_pcs->useciecolor = 0;

    /* set pcl data stream pointers to xl's and process this batch of data. */
    r.ptr = par->source.data - 1;
    r.limit = par->source.data + par->source.available - 1;
    code = pcl_process(&global_pcl_parser_state, global_pcs, &r);
    /* updata xl's parser position to reflect what pcl has consumed. */
    used = (r.ptr + 1 - par->source.data);
    par->source.available -= used;
    par->source.data = r.ptr + 1;

    if ( code < 0 ) {
        dprintf1("passthrough: error return %d\n", code);
        return code;
    }
    /* always return need data and we exit at the top when the data is
       exhausted. */
    {
        if (used > px_parser_data_left(par->parser)) {
            dprintf("error: read past end of stream\n");
            return -1;
        } else if (used < px_parser_data_left(par->parser)) {
            return pxNeedData;
        } else {
            /* end of operator and data */
            return 0;
        }
    }
}


void
pxpcl_pagestatereset()
{
    global_pass_first = true;
    if ( global_pcs ) {
        global_pcs->xfm_state.left_offset_cp = 0.0;
        global_pcs->xfm_state.top_offset_cp = 0.0;
        global_pcs->margins.top = 0;
        global_pcs->margins.left = 0;
    }
}

void
pxpcl_release(void)
{
    if (global_pcs) {
        if (gs_debug_c('i'))
            dprintf("passthrough: releasing global pcl state\n");
        pcl_grestore(global_pcs);
        gs_grestore_only(global_pcs->pgs);
        gs_nulldevice(global_pcs->pgs);
        pcl_do_resets(global_pcs, pcl_reset_permanent);
        global_pcs->end_page = pcl_end_page_top; /* pcl_end_page handling */
        pxpcl_pagestatereset();
        global_pcs = NULL;
        global_this_pass_contiguous = false;
        global_pass_first = true;
        global_char_shear.x = 0;
        global_char_shear.y = 0;
        global_char_scale.x = 1.0;
        global_char_scale.y = 1.0;
        global_char_bold_value = 0.0;
    }
}

/* the pxl parser must give us this information */
void
pxpcl_passthroughcontiguous(bool cont)
{
    global_this_pass_contiguous = cont;
}

/* copy state from pcl to pxl after a non-snippet passthrough
 */
void
pxpcl_endpassthroughcontiguous(px_state_t *pxs)
{
    if ( global_pcs->end_page == pcl_end_page_top &&
         global_pcs->page_marked &&
         pxs->orientation != global_pcs->xfm_state.lp_orient ) {

        /* end of pcl whole job; need to reflect pcl orientation changes */
        pxs->orientation = global_pcs->xfm_state.lp_orient;
        pxBeginPageFromPassthrough(pxs);
    }

    pxs->pxgs->char_shear.x = global_char_shear.x;
    pxs->pxgs->char_shear.y = global_char_shear.y;
    pxs->pxgs->char_scale.x = global_char_scale.x;
    pxs->pxgs->char_scale.y = global_char_scale.y;
    pxs->pxgs->char_bold_value = global_char_bold_value;
}
int pxpcl_selectfont(px_args_t *par, px_state_t *pxs)
{
    int code;
    stream_cursor_read r;
    const px_value_t *pstr = par->pv[3];
    const byte *str = (const byte *)pstr->value.array.data;
    uint len = pstr->value.array.size;
    px_gstate_t *pxgs = pxs->pxgs;
    pcl_font_selection_t *pfp;

    if ( !global_pcs )
        pxPassthrough_init(pxs);

    /* this is the first passthrough on this page */
    if ( global_pass_first ) {
        pxPassthrough_setpagestate(pxs);
        pxPassthrough_pcl_state_nonpage_exceptions(pxs);
        global_pass_first = false;
    } else {
        /* there was a previous passthrough check if there were
           any intervening XL commands */
        if ( global_this_pass_contiguous == false )
            pxPassthrough_pcl_state_nonpage_exceptions(pxs);
    }
    r.ptr = str - 1;
    r.limit = str + len - 1;
    code = pcl_process(&global_pcl_parser_state, global_pcs, &r);

    pcl_recompute_font(global_pcs, false);       /* select font */
    code = gs_setfont(pxs->pgs, global_pcs->font->pfont);
    if (code < 0)
        return code;
    pfp = &global_pcs->font_selection[global_pcs->font_selected];

    {
#define CP_PER_INCH         (7200.0)
#define CP_PER_MM           (7200.0/25.4)
#define CP_PER_10ths_of_MM  (CP_PER_MM/10.0)

        static const floatp centipoints_per_measure[4] = {
            CP_PER_INCH,          /* eInch */
            CP_PER_MM,            /* eMillimeter */
            CP_PER_10ths_of_MM,   /* eTenthsOfAMillimeter */
            1                     /* pxeMeasure_next, won't reach */
        };

        gs_point sz;
        pcl_font_scale(global_pcs, &sz);
        pxgs->char_size = sz.x /
            centipoints_per_measure[pxs->measure] *
            pxs->units_per_measure.x;
    }
    pxgs->symbol_set = pfp->params.symbol_set;

    if (pcl_downloaded_and_bound(global_pcs->font)) {
        pxgs->symbol_map = 0;
    } else {
        set_symbol_map(pxs, global_pcs->font->font_type == plft_16bit);
    }

    {
        pl_font_t *plf = global_pcs->font;


        /* unfortunately the storage identifier is inconsistent
           between PCL and PCL XL, NB we should use the pxfont.h
           enumerations pxfsInternal and pxfsDownloaded but there is a
           foolish type redefinition in that header that need to be
           fixed. */

        if (plf->storage == 4)
            plf->storage = 1;
        else
            plf->storage = 0;

        pxgs->base_font = (px_font_t *)plf;
    }
    pxgs->char_matrix_set = false;
    return 0;
}