Coverage Report

Created: 2025-06-24 07:01

/src/ghostpdl/psi/zcharx.c
Line
Count
Source (jump to first uncovered line)
1
/* Copyright (C) 2001-2024 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
/* Level 2 character operators */
18
#include "ghost.h"
19
#include "oper.h"
20
#include "gsmatrix.h"   /* for gxfont.h */
21
#include "gstext.h"
22
#include "gxfixed.h"    /* for gxfont.h */
23
#include "gxfont.h"
24
#include "gxtext.h"
25
#include "ialloc.h"
26
#include "ichar.h"
27
#include "igstate.h"
28
#include "iname.h"
29
#include "ibnum.h"
30
#include "estack.h"
31
#include "memory_.h"
32
33
/* Common setup for glyphshow and .glyphwidth. */
34
static int
35
glyph_show_setup(i_ctx_t *i_ctx_p, gs_glyph *pglyph)
36
5
{
37
5
    os_ptr op = osp;
38
39
5
    check_op(1);
40
41
2
    switch (gs_currentfont(igs)->FontType) {
42
0
        case ft_CID_encrypted:
43
0
        case ft_CID_user_defined:
44
0
        case ft_CID_TrueType:
45
0
        case ft_CID_bitmap:
46
0
            check_int_leu(*op, GS_MAX_GLYPH - GS_MIN_CID_GLYPH);
47
0
            *pglyph = (gs_glyph) op->value.intval + GS_MIN_CID_GLYPH;
48
0
            break;
49
2
        default:
50
2
            check_type(*op, t_name);
51
0
            *pglyph = name_index(imemory, op);
52
2
    }
53
0
    return op_show_enum_setup(i_ctx_p);
54
2
}
55
56
/* <charname> glyphshow - */
57
static int
58
zglyphshow(i_ctx_t *i_ctx_p)
59
5
{
60
5
    es_ptr ep = esp;        /* Save in case of error */
61
5
    gs_glyph glyph = GS_NO_GLYPH;
62
5
    gs_text_enum_t *penum;
63
5
    int code;
64
65
5
    if ((code = glyph_show_setup(i_ctx_p, &glyph)) != 0)
66
5
        return code;
67
0
    if ((code = gs_glyphshow_begin(igs, glyph, imemory_local, &penum)) < 0)
68
0
        return code;
69
0
    *(op_proc_t *)&penum->enum_client_data = zglyphshow;
70
0
    if ((code = op_show_finish_setup(i_ctx_p, penum, 1, NULL)) < 0) {
71
0
        ifree_object(penum, "zglyphshow");
72
0
        return code;
73
0
    }
74
0
    code = op_show_continue_pop(i_ctx_p, 1);
75
0
    if (code < 0) {
76
        /* We must restore the exec stack pointer back to the point where we entered, in case
77
         * we 'retry' the operation (eg having increased the operand stack).
78
         * We'll rely on gc to handle the enumerator.
79
         * Bug #700618/#707485
80
         */
81
0
        esp = ep;
82
0
    }
83
0
    return code;
84
0
}
85
86
/* <string> <numarray|numstring> xshow - */
87
/* <string> <numarray|numstring> yshow - */
88
/* <string> <numarray|numstring> xyshow - */
89
static int
90
moveshow(i_ctx_t *i_ctx_p, bool have_x, bool have_y)
91
57
{
92
57
    os_ptr op = osp;
93
57
    gs_text_enum_t *penum = NULL;
94
57
    int code;
95
57
    int format;
96
57
    uint i, size, widths_needed;
97
57
    float *values;
98
57
    bool CPSI_mode = gs_currentcpsimode(imemory);
99
100
57
    check_op(2);
101
21
    code = op_show_setup(i_ctx_p, op - 1);
102
21
    if (code != 0)
103
17
        return code;
104
4
    format = num_array_format(op);
105
4
    if (format < 0)
106
4
        return format;
107
0
    size = num_array_size(op, format);
108
0
    values = (float *)ialloc_byte_array(size, sizeof(float), "moveshow");
109
0
    if (values == 0)
110
0
        return_error(gs_error_VMerror);
111
0
    if (CPSI_mode)
112
0
        memset(values, 0, size * sizeof(values[0])); /* Safety. */
113
0
    if ((code = gs_xyshow_begin(igs, op[-1].value.bytes, r_size(op - 1),
114
0
                                (have_x ? values : (float *)0),
115
0
                                (have_y ? values : (float *)0),
116
0
                                size, imemory_local, &penum)) < 0) {
117
0
        ifree_object(values, "moveshow");
118
0
        if (penum) /* if there was an error, the text_enum may not have been allocated */
119
0
            penum->text.x_widths = penum->text.y_widths = NULL;
120
0
        return code;
121
0
    }
122
0
    if (CPSI_mode) {
123
        /* CET 13-29.PS page 2 defines a longer width array
124
           then the text requires, and CPSI silently ignores extra elements.
125
           So we need to compute exact number of characters
126
           to know how many elements to load and type check. */
127
0
        code = gs_text_count_chars(igs, gs_get_text_params(penum), imemory);
128
0
        if (code < 0)
129
0
            return code;
130
0
        widths_needed = code;
131
0
        if (have_x && have_y)
132
0
            widths_needed <<= 1;
133
0
    } else
134
0
        widths_needed = size;
135
0
    for (i = 0; i < widths_needed; ++i) {
136
0
        ref value;
137
138
0
        switch (code = num_array_get(imemory, op, format, i, &value)) {
139
0
        case t_integer:
140
0
            values[i] = (float)value.value.intval; break;
141
0
        case t_real:
142
0
            values[i] = value.value.realval; break;
143
0
        case t_null:
144
0
            code = gs_note_error(gs_error_rangecheck);
145
            /* falls through */
146
0
        default:
147
0
            ifree_object(values, "moveshow");
148
0
        penum->text.x_widths = penum->text.y_widths = NULL;
149
0
            return code;
150
0
        }
151
0
    }
152
0
    if ((code = op_show_finish_setup(i_ctx_p, penum, 2, NULL)) < 0) {
153
0
        ifree_object(values, "moveshow");
154
0
        penum->text.x_widths = penum->text.y_widths = NULL;
155
0
        return code;
156
0
    }
157
0
    pop(2);
158
0
    return op_show_continue(i_ctx_p);
159
0
}
160
static int
161
zxshow(i_ctx_t *i_ctx_p)
162
17
{
163
17
    return moveshow(i_ctx_p, true, false);
164
17
}
165
static int
166
zyshow(i_ctx_t *i_ctx_p)
167
15
{
168
15
    return moveshow(i_ctx_p, false, true);
169
15
}
170
static int
171
zxyshow(i_ctx_t *i_ctx_p)
172
25
{
173
25
    return moveshow(i_ctx_p, true, true);
174
25
}
175
176
/* ------ Initialization procedure ------ */
177
178
const op_def zcharx_op_defs[] =
179
{
180
    op_def_begin_level2(),
181
    {"1glyphshow", zglyphshow},
182
    {"2xshow", zxshow},
183
    {"2xyshow", zxyshow},
184
    {"2yshow", zyshow},
185
    op_def_end(0)
186
};