Coverage Report

Created: 2026-11-12 07:09

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ghostpdl/pcl/pcl/pgchar.c
Line
Count
Source
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
/* pgchar.c */
18
/* HP-GL/2 font and character commands */
19
#include "math_.h"
20
#include "stdio_.h"             /* for gdebug.h */
21
#include "gdebug.h"
22
#include "pcparse.h"
23
#include "pgmand.h"
24
#include "pgdraw.h"
25
#include "pginit.h"
26
#include "pggeom.h"
27
#include "pgmisc.h"
28
#include "pcfsel.h"
29
#include "pcpalet.h"
30
31
/* ------ Internal procedures ------ */
32
33
/* Define font parameters (AD, SD). */
34
static int
35
hpgl_font_definition(hpgl_args_t * pargs, hpgl_state_t * pgls, int index)
36
144k
{
37
    /*
38
     * Since these commands take an arbitrary number of arguments, we
39
     * reset the argument bookkeeping after each group.  We reset
40
     * phase to 1, 2, or 3 after seeing the first pair, so we can tell
41
     * whether there were any arguments at all.  (1 means no parameter
42
     * changed, >1 means some parameter changed.)
43
     */
44
45
144k
    pcl_font_selection_t *pfs = &pgls->g.font_selection[index];
46
144k
#define pfp (&pfs->params)
47
144k
    int kind;
48
49
144k
    pfs->selected_id = (uint) - 1;
50
158k
    for (; hpgl_arg_c_int(pgls->memory, pargs, &kind); pargs->phase |= 1)
51
14.3k
        switch (kind) {
52
2.95k
            case 1:            /* symbol set */
53
2.95k
                {
54
2.95k
                    int32 sset;
55
56
2.95k
                    if (!hpgl_arg_int(pgls->memory, pargs, &sset))
57
8
                        return e_Range;
58
2.94k
                    if (pfp->symbol_set != (uint) sset)
59
2.75k
                        pfp->symbol_set = (uint) sset, pargs->phase |= 2;
60
2.94k
                }
61
0
                break;
62
2.90k
            case 2:            /* spacing */
63
2.90k
                {
64
2.90k
                    int spacing;
65
66
2.90k
                    if (!hpgl_arg_c_int(pgls->memory, pargs, &spacing))
67
38
                        return e_Range;
68
2.87k
                    if (((spacing == 1) || (spacing == 0))
69
2.86k
                        && (pfp->proportional_spacing != spacing))
70
2.67k
                        pfp->proportional_spacing = spacing, pargs->phase |=
71
2.67k
                            2;
72
2.87k
                }
73
0
                break;
74
4
            case 3:            /* pitch */
75
4
                {
76
4
                    hpgl_real_t pitch;
77
78
4
                    if (!hpgl_arg_c_real(pgls->memory, pargs, &pitch))
79
3
                        return e_Range;
80
1
                    if ((pl_fp_pitch_per_inch(pfp) != pitch) &&
81
1
                        (pitch >= 0) && (pitch < 32768.0)) {
82
1
                        pl_fp_set_pitch_per_inch(pfp,
83
1
                                                 pitch >
84
1
                                                 7200.0 ? 7200.0 : pitch);
85
1
                        pargs->phase |= 2;
86
1
                    }
87
88
1
                }
89
0
                break;
90
2.80k
            case 4:            /* height */
91
2.80k
                {
92
2.80k
                    hpgl_real_t height;
93
94
2.80k
                    if (!hpgl_arg_c_real(pgls->memory, pargs, &height))
95
19
                        return e_Range;
96
2.78k
                    if ((pfp->height_4ths != (uint) (height * 4))
97
2.65k
                        && (height >= 0)) {
98
                        /* minimum height for practical purposes is one
99
                           quarter point.  The HP Spec says 0 is legal
100
                           but does not specify what a height of zero
101
                           means.  The previous code truncated height,
102
                           it probably should be rounded as in pcl but
103
                           doing so would change a lot of files for no
104
                           compelling reason so for now truncate. */
105
2.65k
                        uint trunc_height_4ths = (uint) (height * 4);
106
107
2.65k
                        pfp->height_4ths =
108
2.65k
                            (trunc_height_4ths == 0 ? 1 : trunc_height_4ths);
109
2.65k
                        pargs->phase |= 2;
110
2.65k
                    }
111
2.78k
                }
112
0
                break;
113
285
            case 5:            /* posture */
114
285
                {
115
285
                    int posture;
116
117
285
                    if (!hpgl_arg_c_int(pgls->memory, pargs, &posture))
118
4
                        return e_Range;
119
281
                    if (pfp->style != posture)
120
2
                        pfp->style = posture, pargs->phase |= 2;
121
122
281
                }
123
0
                break;
124
2.75k
            case 6:            /* stroke weight */
125
2.75k
                {
126
2.75k
                    int weight;
127
128
2.75k
                    if (!hpgl_arg_c_int(pgls->memory, pargs, &weight))
129
161
                        return e_Range;
130
2.59k
                    if (pfp->stroke_weight != weight)
131
2.13k
                        if (((weight >= -7) && (weight <= 7))
132
9
                            || (weight == 9999))
133
2.12k
                            pfp->stroke_weight = weight, pargs->phase |= 2;
134
2.59k
                }
135
0
                break;
136
2.56k
            case 7:            /* typeface */
137
2.56k
                {
138
2.56k
                    int32 face;
139
140
2.56k
                    if (!hpgl_arg_int(pgls->memory, pargs, &face))
141
367
                        return e_Range;
142
2.20k
                    if (pfp->typeface_family != (uint) face)
143
2.07k
                        pfp->typeface_family = (uint) face, pargs->phase |= 2;
144
2.20k
                }
145
0
                break;
146
57
            default:
147
57
                return e_Range;
148
14.3k
        }
149
    /* If there were no arguments at all, default all values. */
150
143k
    if (!pargs->phase)
151
141k
        hpgl_default_font_params(pfs);
152
143k
    if (pargs->phase != 1) {    /* A value changed, or we are defaulting.  Decache the font. */
153
143k
        pfs->font = 0;
154
143k
        if (index == pgls->g.font_selected)
155
70.9k
            pgls->g.font = 0;
156
143k
    }
157
143k
    return 0;
158
144k
}
159
/* Define label drawing direction (DI, DR). */
160
static int
161
hpgl_label_direction(hpgl_args_t * pargs, hpgl_state_t * pgls, bool relative)
162
71.2k
{
163
71.2k
    hpgl_real_t run = 1, rise = 0;
164
165
71.2k
    if (hpgl_arg_c_real(pgls->memory, pargs, &run)) {
166
69.6k
        if (!hpgl_arg_c_real(pgls->memory, pargs, &rise)
167
69.5k
            || (run == 0 && rise == 0))
168
14
            return e_Range;
169
69.5k
        {
170
69.5k
            double hyp = hypot(run, rise);
171
172
69.5k
            run /= hyp;
173
69.5k
            rise /= hyp;
174
69.5k
        }
175
69.5k
    }
176
71.2k
    pgls->g.character.direction.x = run;
177
71.2k
    pgls->g.character.direction.y = rise;
178
71.2k
    pgls->g.character.direction_relative = relative;
179
71.2k
    hpgl_call(hpgl_update_carriage_return_pos(pgls));
180
71.2k
    return 0;
181
71.2k
}
182
183
/* forward declaration */
184
static int hpgl_select_font(hpgl_state_t * pgls, int index);
185
186
/* Select font by ID (FI, FN). */
187
static int
188
hpgl_select_font_by_id(hpgl_args_t * pargs, hpgl_state_t * pgls, int index)
189
2.94k
{
190
2.94k
    pcl_font_selection_t *pfs = &pgls->g.font_selection[index];
191
2.94k
    int32 id;
192
2.94k
    int code;
193
194
2.94k
    if (!hpgl_arg_c_int(pgls->memory, pargs, &id) || id < 0)
195
2.94k
        return e_Range;
196
8
    code = pcl_select_font_by_id(pfs, id, pgls /****** NOTA BENE ******/ );
197
8
    switch (code) {
198
0
        default:               /* error */
199
0
            return code;
200
8
        case 1:                /* ID not found, no effect */
201
8
            return 0;
202
0
        case 0:                /* ID found */
203
0
            break;
204
8
    }
205
0
    pgls->g.font_selection[index].font = pfs->font;
206
0
    pgls->g.font_selection[index].map = pfs->map;
207
    /*
208
     * If we just selected a bitmap font, force the equivalent of SB1.
209
     * See TRM 23-65 and 23-81.
210
     */
211
0
    if (pfs->font->scaling_technology == plfst_bitmap) {
212
0
        hpgl_args_t args;
213
214
0
        hpgl_args_setup(&args);
215
0
        hpgl_args_add_int(&args, 1);
216
0
        hpgl_call(hpgl_SB(&args, pgls));
217
0
    }
218
    /* note pcltrm 23-54 - only select if the table (primary or
219
       secondary) matches the currently selected table. */
220
0
    if (index == pgls->g.font_selected) {
221
0
        hpgl_select_font(pgls, index);
222
0
    }
223
0
    return 0;
224
0
}
225
226
/* Select font (SA, SS). */
227
static int
228
hpgl_select_font(hpgl_state_t * pgls, int index)
229
70.5k
{
230
70.5k
    pgls->g.font_selected = index;
231
70.5k
    pgls->g.font = pgls->g.font_selection[index].font;
232
70.5k
    pgls->g.map = pgls->g.font_selection[index].map;
233
70.5k
    return 0;
234
70.5k
}
235
236
/* ------ Commands ------ */
237
238
/* AD [kind,value...]; */
239
int
240
hpgl_AD(hpgl_args_t * pargs, hpgl_state_t * pgls)
241
73.2k
{
242
73.2k
    return hpgl_font_definition(pargs, pgls, 1);
243
73.2k
}
244
245
589k
#define CHAR_EDGE_PEN_UNSET -1
246
247
/* return the current edge pen based on whethere or not the interpreter specifically set the pen */
248
int32
249
hpgl_get_character_edge_pen(hpgl_state_t * pgls)
250
589k
{
251
    /* if the character edge pen has not been set then we return the
252
       current pen number, otherwise the state value as set in the CF
253
       command is used.  (see hpgl_CF) */
254
589k
    return (pgls->g.character.edge_pen == CHAR_EDGE_PEN_UNSET ?
255
589k
            hpgl_get_selected_pen(pgls) : pgls->g.character.edge_pen);
256
257
589k
}
258
259
/*
260
 * CF [mode[,pen]];
261
 */
262
int
263
hpgl_CF(hpgl_args_t * pargs, hpgl_state_t * pgls)
264
70.0k
{
265
70.0k
    int mode = 0;
266
70.0k
    int npen = pcl_palette_get_num_entries(pgls->ppalet);
267
70.0k
    int32 pen = 0;
268
269
70.0k
    if (hpgl_arg_c_int(pgls->memory, pargs, &mode)) {
270
359
        if ((mode & ~3) != 0)
271
1
            return e_Range;
272
        /* With only 1 argument, we "unset" the current pen.  This
273
           causes the drawing machinery to use the current pen when
274
           the stroke is rendered (i.e. a subsequent SP will change
275
           the character edge pen */
276
358
        if (hpgl_arg_int(pgls->memory, pargs, &pen)) {
277
356
            if ((pen < 0) || (pen >= npen))
278
0
                return e_Range;
279
356
        } else
280
2
            pen = CHAR_EDGE_PEN_UNSET;
281
358
    }
282
70.0k
    pgls->g.character.fill_mode = mode;
283
70.0k
    pgls->g.character.edge_pen = pen;
284
70.0k
    return 0;
285
70.0k
}
286
287
#undef CHAR_EDGE_PEN_UNSET
288
289
/* DI [run,rise]; */
290
int
291
hpgl_DI(hpgl_args_t * pargs, hpgl_state_t * pgls)
292
71.1k
{
293
71.1k
    return hpgl_label_direction(pargs, pgls, false);
294
71.1k
}
295
296
/* DR [run,rise]; */
297
int
298
hpgl_DR(hpgl_args_t * pargs, hpgl_state_t * pgls)
299
107
{
300
107
    return hpgl_label_direction(pargs, pgls, true);
301
107
}
302
303
/* DT terminator[,mode]; */
304
int
305
hpgl_DT(hpgl_args_t * pargs, hpgl_state_t * pgls)
306
1.93k
{
307
1.93k
    const byte *p = pargs->source.ptr;
308
1.93k
    const byte *rlimit = pargs->source.limit;
309
1.93k
    byte ch = (byte) pargs->phase;
310
1.93k
    int mode = 1;
311
312
    /* We use phase to remember the terminator character */
313
    /* in case we had to restart execution. */
314
1.93k
    if (p >= rlimit)
315
2
        return gs_error_NeedInput;
316
1.93k
    if (!ch)
317
1.93k
        switch ((ch = *++p)) {
318
1
            case ';':
319
1
                pargs->source.ptr = p;
320
1
                pgls->g.label.terminator = 3;
321
1
                pgls->g.label.print_terminator = false;
322
1
                return 0;
323
4
            case 0:
324
4
            case 5:
325
4
            case 27:
326
4
                return e_Range;
327
1.92k
            default:
328
1.92k
                if (p >= rlimit)
329
0
                    return gs_error_NeedInput;
330
1.92k
                if (*++p == ',') {
331
448
                    pargs->source.ptr = p;
332
448
                    pargs->phase = ch;
333
448
                    if_debug1m('i', pgls->memory, "%c", ch);
334
448
                }
335
1.93k
        }
336
1.92k
    if (hpgl_arg_c_int(pgls->memory, pargs, &mode) && (mode & ~1))
337
20
        return e_Range;
338
1.90k
    pgls->g.label.terminator = ch;
339
1.90k
    pgls->g.label.print_terminator = !mode;
340
1.90k
    return 0;
341
1.92k
}
342
343
/* DV [path[,line]]; */
344
int
345
hpgl_DV(hpgl_args_t * pargs, hpgl_state_t * pgls)
346
72.4k
{
347
72.4k
    int path;
348
72.4k
    int line;
349
350
72.4k
    if (!hpgl_arg_c_int(pgls->memory, pargs, &path))
351
69.6k
        path = 0;
352
353
72.4k
    if (!hpgl_arg_c_int(pgls->memory, pargs, &line))
354
72.3k
        line = 0;
355
356
72.4k
    if ((path & ~3) | (line & ~1))
357
1
        return e_Range;
358
72.4k
    pgls->g.character.text_path = path;
359
72.4k
    pgls->g.character.line_feed_direction = (line ? -1 : 1);
360
72.4k
    hpgl_call(hpgl_update_carriage_return_pos(pgls));
361
72.4k
    return 0;
362
72.4k
}
363
364
/* ES [width[,height]]; */
365
int
366
hpgl_ES(hpgl_args_t * pargs, hpgl_state_t * pgls)
367
72.6k
{
368
72.6k
    hpgl_real_t width = 0, height = 0;
369
370
72.6k
    hpgl_arg_c_real(pgls->memory, pargs, &width);
371
72.6k
    hpgl_arg_c_real(pgls->memory, pargs, &height);
372
72.6k
    pgls->g.character.extra_space.x = width;
373
72.6k
    pgls->g.character.extra_space.y = height;
374
72.6k
    return 0;
375
72.6k
}
376
377
/* FI fontid; */
378
int
379
hpgl_FI(hpgl_args_t * pargs, hpgl_state_t * pgls)
380
1.37k
{
381
1.37k
    return hpgl_select_font_by_id(pargs, pgls, 0);
382
1.37k
}
383
384
/* FN fontid; */
385
int
386
hpgl_FN(hpgl_args_t * pargs, hpgl_state_t * pgls)
387
1.57k
{
388
1.57k
    return hpgl_select_font_by_id(pargs, pgls, 1);
389
1.57k
}
390
391
/* The following is an extension documented in the Comparison Guide. */
392
/* LM [mode[, row number]]; */
393
int
394
hpgl_LM(hpgl_args_t * pargs, hpgl_state_t * pgls)
395
70.5k
{
396
70.5k
    int mode;
397
70.5k
    int row_number;
398
70.5k
    int old_mode =
399
70.5k
        (pgls->g.label.double_byte ? 1 : 0) +
400
70.5k
        (pgls->g.label.write_vertical ? 2 : 0);
401
402
70.5k
    if (!hpgl_arg_c_int(pgls->memory, pargs, &mode))
403
70.4k
        mode = 0;
404
405
70.5k
    if (!hpgl_arg_c_int(pgls->memory, pargs, &row_number))
406
70.5k
        row_number = 0;
407
408
70.5k
    pgls->g.label.row_offset =
409
70.5k
        (row_number < 0 ? 0 : row_number > 255 ? 255 : row_number) << 8;
410
70.5k
    mode &= 3;
411
70.5k
    pgls->g.label.double_byte = (mode & 1) != 0;
412
70.5k
    pgls->g.label.write_vertical = (mode & 2) != 0;
413
    /*
414
     * The documentation says "When LM switches modes, it turns off
415
     * symbol mode."  We take this literally: LM only turns off
416
     * symbol mode if the new label mode differs from the old one.
417
     */
418
70.5k
    if (mode != old_mode)
419
79
        pgls->g.symbol_mode = 0;
420
70.5k
    return 0;
421
70.5k
}
422
423
/* LO [origin]; */
424
int
425
hpgl_LO(hpgl_args_t * pargs, hpgl_state_t * pgls)
426
117k
{
427
117k
    int origin;
428
429
117k
    if (!hpgl_arg_c_int(pgls->memory, pargs, &origin))
430
1.63k
        origin = 1;
431
432
117k
    if (origin < 1 || origin == 10 || origin == 20 || origin > 21)
433
288
        return e_Range;
434
116k
    pgls->g.label.origin = origin;
435
116k
    hpgl_call(hpgl_update_carriage_return_pos(pgls));
436
116k
    return 0;
437
116k
}
438
439
/* SA; */
440
int
441
hpgl_SA(hpgl_args_t * pargs, hpgl_state_t * pgls)
442
360
{
443
360
    return hpgl_select_font(pgls, 1);
444
360
}
445
446
/* SB [mode]; */
447
int
448
hpgl_SB(hpgl_args_t * pargs, hpgl_state_t * pgls)
449
70.0k
{
450
70.0k
    int mode = 0;
451
452
70.0k
    if (hpgl_arg_c_int(pgls->memory, pargs, &mode) && (mode & ~1))
453
25
        return e_Range;
454
70.0k
    {
455
70.0k
        int i;
456
457
70.0k
        pgls->g.bitmap_fonts_allowed = mode;
458
        /*
459
         * A different set of fonts is now available for consideration.
460
         * Decache any affected font(s): those selected by parameter,
461
         * and bitmap fonts selected by ID if bitmap fonts are now
462
         * disallowed.
463
         */
464
210k
        for (i = 0; i < countof(pgls->g.font_selection); ++i) {
465
140k
            pcl_font_selection_t *pfs = &pgls->g.font_selection[i];
466
467
140k
            if (((int)pfs->selected_id < 0) ||
468
0
                (!mode && pfs->font != 0 &&
469
0
                 pfs->font->scaling_technology == plfst_bitmap)
470
140k
                ) {
471
140k
                pfs->font = 0;
472
140k
            }
473
140k
        }
474
70.0k
        pgls->g.font = 0;
475
70.0k
        pgls->g.map = 0;
476
70.0k
    }
477
70.0k
    return 0;
478
70.0k
}
479
480
/* SD [kind,value...]; */
481
int
482
hpgl_SD(hpgl_args_t * pargs, hpgl_state_t * pgls)
483
71.3k
{
484
71.3k
    return hpgl_font_definition(pargs, pgls, 0);
485
71.3k
}
486
487
/* SI [width,height]; */
488
int
489
hpgl_SI(hpgl_args_t * pargs, hpgl_state_t * pgls)
490
74.3k
{
491
74.3k
    hpgl_real_t width_cm, height_cm;
492
493
74.3k
    if (hpgl_arg_c_real(pgls->memory, pargs, &width_cm)) {
494
188
        if (!hpgl_arg_c_real(pgls->memory, pargs, &height_cm))
495
22
            return e_Range;
496
        /* this isn't documented but HP seems to ignore the
497
           command (retains previous value) if either parameter is
498
           zero. NB probably should use epsilon have not tested. */
499
166
        if (width_cm == 0.0 || height_cm == 0.0)
500
6
            return e_Range;
501
160
        pgls->g.character.size.x = mm_2_plu(width_cm * 10);
502
160
        pgls->g.character.size.y = mm_2_plu(height_cm * 10);
503
160
        pgls->g.character.size_mode = hpgl_size_absolute;
504
160
    } else
505
74.1k
        pgls->g.character.size_mode = hpgl_size_not_set;
506
74.3k
    return 0;
507
74.3k
}
508
509
139k
#define MAX_SL_TANGENT 114.5887
510
/* SL [slant]; */
511
int
512
hpgl_SL(hpgl_args_t * pargs, hpgl_state_t * pgls)
513
69.7k
{
514
69.7k
    hpgl_real_t slant = 0;
515
516
69.7k
    hpgl_arg_c_real(pgls->memory, pargs, &slant);
517
    /* clamp to 89.5 degrees of char slant, avoids math issues around
518
     * tan 90degrees == infinity.  Visually close to HP,
519
     * performance decrease as slant approaches tan(90).
520
     */
521
522
69.7k
    pgls->g.character.slant = slant > MAX_SL_TANGENT ?
523
69.6k
        MAX_SL_TANGENT : slant < -MAX_SL_TANGENT ? -MAX_SL_TANGENT : slant;
524
69.7k
    return 0;
525
69.7k
}
526
#undef MAX_SL_TANGENT
527
528
/* SR [width,height]; */
529
int
530
hpgl_SR(hpgl_args_t * pargs, hpgl_state_t * pgls)
531
298
{
532
298
    hpgl_real_t width_pct, height_pct;
533
534
298
    if (hpgl_arg_c_real(pgls->memory, pargs, &width_pct)) {
535
2
        if (!hpgl_arg_c_real(pgls->memory, pargs, &height_pct))
536
2
            return e_Range;
537
        /* this isn't documented but HP seems to ignore the
538
           command (retains previous value) if either parameter is
539
           zero. NB probably should use epsilon have not tested. */
540
0
        if (width_pct == 0.0 || height_pct == 0.0)
541
0
            return e_Range;
542
0
        pgls->g.character.size.x = width_pct / 100;
543
0
        pgls->g.character.size.y = height_pct / 100;
544
296
    } else {
545
296
        pgls->g.character.size.x = 0.0075;
546
296
        pgls->g.character.size.y = 0.015;
547
296
    }
548
296
    pgls->g.character.size_mode = hpgl_size_relative;
549
296
    return 0;
550
298
}
551
552
/* SS; */
553
int
554
hpgl_SS(hpgl_args_t * pargs, hpgl_state_t * pgls)
555
70.1k
{
556
70.1k
    return hpgl_select_font(pgls, 0);
557
70.1k
}
558
559
/* TD [mode]; */
560
int
561
hpgl_TD(hpgl_args_t * pargs, hpgl_state_t * pgls)
562
72.4k
{
563
72.4k
    int mode = 0;
564
565
72.4k
    if (hpgl_arg_c_int(pgls->memory, pargs, &mode) && (mode & ~1))
566
29
        return e_Range;
567
72.3k
    pgls->g.transparent_data = mode;
568
72.3k
    return 0;
569
72.4k
}
570
571
/* Add a command to the DL table - pen up or pen down coordinates.
572
   Resizing the table as appropriate. */
573
static int
574
hpgl_add_dl_char_data(hpgl_state_t * pgls, hpgl_dl_cdata_t * pcdata,
575
                      short cc_data)
576
0
{
577
    /* characters stored as short */
578
0
    int csz = sizeof(short);
579
580
0
    if (pcdata->index == -1) {
581
        /* first element - allocate a small array which will be resized as needed */
582
0
        pcdata->data =
583
0
            (short *)gs_alloc_bytes(pgls->memory, 2 * csz, "DL data");
584
0
        if (pcdata->data == 0)
585
0
            return e_Memory;
586
0
    } else if (gs_object_size(pgls->memory, pcdata->data) ==
587
0
               (pcdata->index + 1) * csz) {
588
        /* new character doesn't fit - double the size of the array */
589
0
        short *new_cdata =
590
0
            (short *)gs_resize_object(pgls->memory, pcdata->data,
591
0
                                      (pcdata->index + 1) * csz * 2,
592
0
                                      "DL data resize");
593
594
0
        if (new_cdata == 0) {
595
0
            return e_Memory;
596
0
        }
597
0
        pcdata->data = new_cdata;
598
0
    }
599
0
    pcdata->data[++pcdata->index] = cc_data;
600
0
    return 0;
601
0
}
602
603
static void
604
hpgl_dl_dict_free_value(gs_memory_t * mem, void *value, client_name_t cname)
605
0
{
606
0
    hpgl_dl_cdata_t *cdata = (hpgl_dl_cdata_t *) value;
607
608
0
    if (cdata->data)
609
0
        gs_free_object(mem, cdata->data, cname);
610
0
    gs_free_object(mem, cdata, cname);
611
0
}
612
613
614
static void
615
hpgl_clear_dl_chars(hpgl_state_t * pgls)
616
2.71k
{
617
    /* NB get rid of the 531 dictionary */
618
2.71k
    return;
619
2.71k
}
620
621
/* DL [Character Code, Coords] */
622
int
623
hpgl_DL(hpgl_args_t * pargs, hpgl_state_t * pgls)
624
2.71k
{
625
2.71k
    int code;
626
2.71k
    hpgl_dl_cdata_t *cdata;
627
628
    /* first call */
629
2.71k
    if (pargs->phase == 0) {
630
2.71k
        int32 cc;
631
632
        /* double byte characters are not yet supported */
633
2.71k
        if (pgls->g.label.double_byte)
634
0
            return e_Unimplemented;
635
        /* parse the character code, no arguments clears all defined
636
           characters */
637
2.71k
        if (!hpgl_arg_c_int(pgls->memory, pargs, &cc)) {
638
2.71k
            hpgl_clear_dl_chars(pgls);
639
2.71k
            return 0;
640
2.71k
        }
641
642
1
        if (cc < 0 || cc > 255)
643
0
            return e_Range;
644
645
1
        cdata =
646
1
            (hpgl_dl_cdata_t *) gs_alloc_bytes(pgls->memory,
647
1
                                               sizeof(hpgl_dl_cdata_t),
648
1
                                               "DL header");
649
1
        if (cdata == 0)
650
0
            return e_Memory;
651
1
        cdata->index = -1;
652
1
        cdata->data = NULL;
653
1
        id_set_value(pgls->g.current_dl_char_id, (ushort) cc);
654
1
        code = pl_dict_put(&pgls->g.dl_531_fontdict,
655
1
                    id_key(pgls->g.current_dl_char_id), 2, cdata);
656
1
        if (code < 0)
657
0
            return code;
658
1
        hpgl_args_init(pargs);
659
1
        pargs->phase = 1;
660
1
    }
661
662
1
    if (!pl_dict_find
663
1
        (&pgls->g.dl_531_fontdict, id_key(pgls->g.current_dl_char_id), 2,
664
1
         (void **)&cdata))
665
        /* this should be a failed assertion */
666
0
        return -1;
667
1
    do {
668
1
        int c;
669
670
1
        if (!hpgl_arg_c_int(pgls->memory, pargs, &c))
671
1
            break;
672
0
        code = hpgl_add_dl_char_data(pgls, cdata, c);
673
0
        if (code < 0) {
674
0
            pl_dict_undef(&pgls->g.dl_531_fontdict,
675
0
                          id_key(pgls->g.current_dl_char_id), 2);
676
0
            return code;
677
0
        }
678
0
        hpgl_args_init(pargs);
679
0
    } while (1);
680
1
    return 0;
681
1
}
682
683
/* Initialization */
684
static int
685
pgchar_do_registration(pcl_parser_state_t * pcl_parser_state,
686
                       gs_memory_t * mem)
687
18.4k
{                               /* Register commands */
688
18.4k
    DEFINE_HPGL_COMMANDS(mem)
689
18.4k
        HPGL_COMMAND('A', 'D', hpgl_AD, hpgl_cdf_pcl_rtl_both), /* kind/value pairs */
690
18.4k
        HPGL_COMMAND('C', 'F', hpgl_CF, hpgl_cdf_pcl_rtl_both),
691
18.4k
        HPGL_COMMAND('D', 'I', hpgl_DI, hpgl_cdf_pcl_rtl_both),
692
18.4k
        HPGL_COMMAND('D', 'L', hpgl_DL, hpgl_cdf_pcl_rtl_both),
693
18.4k
        HPGL_COMMAND('D', 'R', hpgl_DR, hpgl_cdf_pcl_rtl_both),
694
        /* DT has special argument parsing, so it must handle skipping */
695
        /* in polygon mode itself. */
696
18.4k
        HPGL_COMMAND('D', 'T', hpgl_DT, hpgl_cdf_polygon | hpgl_cdf_pcl_rtl_both),
697
18.4k
        HPGL_COMMAND('D', 'V', hpgl_DV, hpgl_cdf_pcl_rtl_both),
698
18.4k
        HPGL_COMMAND('E', 'S', hpgl_ES, hpgl_cdf_pcl_rtl_both),
699
18.4k
        HPGL_COMMAND('F', 'I', hpgl_FI, hpgl_cdf_pcl),
700
18.4k
        HPGL_COMMAND('F', 'N', hpgl_FN, hpgl_cdf_pcl),
701
18.4k
        HPGL_COMMAND('L', 'M', hpgl_LM, hpgl_cdf_pcl_rtl_both),
702
18.4k
        HPGL_COMMAND('L', 'O', hpgl_LO, hpgl_cdf_pcl_rtl_both),
703
18.4k
        HPGL_COMMAND('S', 'A', hpgl_SA, hpgl_cdf_pcl_rtl_both),
704
18.4k
        HPGL_COMMAND('S', 'B', hpgl_SB, hpgl_cdf_pcl),
705
18.4k
        HPGL_COMMAND('S', 'D', hpgl_SD, hpgl_cdf_pcl_rtl_both), /* kind/value pairs */
706
18.4k
        HPGL_COMMAND('S', 'I', hpgl_SI, hpgl_cdf_pcl_rtl_both),
707
18.4k
        HPGL_COMMAND('S', 'L', hpgl_SL, hpgl_cdf_pcl_rtl_both),
708
18.4k
        HPGL_COMMAND('S', 'R', hpgl_SR, hpgl_cdf_pcl_rtl_both),
709
18.4k
        HPGL_COMMAND('S', 'S', hpgl_SS, hpgl_cdf_pcl_rtl_both),
710
18.4k
        HPGL_COMMAND('T', 'D', hpgl_TD, hpgl_cdf_pcl_rtl_both),
711
18.4k
    END_HPGL_COMMANDS
712
18.4k
    return 0;
713
18.4k
}
714
715
static int
716
pgchar_do_reset(pcl_state_t * pcs, pcl_reset_type_t type)
717
62.4k
{
718
62.4k
    if (type & pcl_reset_initial)
719
18.4k
        pl_dict_init(&pcs->g.dl_531_fontdict, pcs->memory,
720
18.4k
                     hpgl_dl_dict_free_value);
721
62.4k
    if (type & pcl_reset_permanent)
722
0
        pl_dict_release(&pcs->g.dl_531_fontdict);
723
62.4k
    return 0;
724
62.4k
}
725
726
const pcl_init_t pgchar_init = {
727
    pgchar_do_registration, pgchar_do_reset, 0
728
};