summaryrefslogtreecommitdiff
path: root/pcl/pcl/pcfrgrnd.c
blob: 4a21b039ba12eb213a5d309aeee6b35a4680d365 (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
/* Copyright (C) 2001-2021 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.
*/


/* pcfrgrnd.c - PCL foreground object implementation */
#include "gx.h"
#include "pcommand.h"
#include "pcfont.h"
#include "pcfrgrnd.h"

/* RC routines */
gs_private_st_simple(st_frgrnd_t, pcl_frgrnd_t, "pcl foreground object");

/*
 * Free a pcl foreground object.
 */
static void
free_foreground(gs_memory_t * pmem, void *pvfrgrnd, client_name_t cname)
{
    pcl_frgrnd_t *pfrgrnd = (pcl_frgrnd_t *) pvfrgrnd;

    if (pfrgrnd->pbase != 0)
        pcl_cs_base_release(pfrgrnd->pbase);
    if (pfrgrnd->pht != 0)
        pcl_ht_release(pfrgrnd->pht);
    gs_free_object(pmem, pvfrgrnd, cname);
}

/*
 * Allocate a pcl foreground object.
 *
 * Returns 0 on success, < 0 in the event of an error.
 */
static int
alloc_foreground(pcl_state_t * pcs,
                 pcl_frgrnd_t ** ppfrgrnd, gs_memory_t * pmem)
{
    pcl_frgrnd_t *pfrgrnd = 0;

    rc_alloc_struct_1(pfrgrnd,
                      pcl_frgrnd_t,
                      &st_frgrnd_t,
                      pmem,
                      return e_Memory, "allocate pcl foreground object");
    pfrgrnd->rc.free = free_foreground;
    pfrgrnd->id = pcl_next_id(pcs);
    pfrgrnd->pbase = 0;
    pfrgrnd->pht = 0;
    *ppfrgrnd = pfrgrnd;
    return 0;
}

/*
 * Build a foreground object from a palette object, releasing any exsiting
 * foreground object.
 *
 * There is no "unshare" routine for foreground, as a foreground object cannot
 * be modified.
 *
 * Returns 0 on success, < 0 in the event of an error
 */
static int
build_foreground(pcl_state_t * pcs,
                 pcl_frgrnd_t ** ppfrgrnd,
                 const pcl_palette_t * ppalet,
                 int pal_entry, gs_memory_t * pmem)
{
    pcl_frgrnd_t *pfrgrnd = *ppfrgrnd;
    const pcl_cs_indexed_t *pindexed = ppalet->pindexed;
    int num_entries;
    bool is_default = false;
    int code = 0;

    if (!pindexed) {
        return_error(gs_error_invalidaccess);
    }

    /*
     * Check for a request for the default foreground. Since there are only
     * three fixed palettes, it is sufficient to check that the palette provided
     * is fixed and has two entries. The default foreground is black, which is
     * the second of the two entries.
     */
    if ((pindexed->pfixed) && (pindexed->num_entries == 2) && (pal_entry == 1)) {
        is_default = true;
        if (pcs->pdflt_frgrnd != 0) {
            pcl_frgrnd_copy_from(*ppfrgrnd, pcs->pdflt_frgrnd);
            return 0;
        }
    }

    num_entries = pindexed->num_entries;

    /* release the existing foreground */
    if (pfrgrnd != 0) {
        rc_decrement(pfrgrnd, "build pcl foreground");
        *ppfrgrnd = 0;
    }

    if ((code = alloc_foreground(pcs, ppfrgrnd, pmem)) < 0)
        return code;
    pfrgrnd = *ppfrgrnd;

    /* pal_entry is interpreted modulo the current palette size */
    if ((pal_entry < 0) || (pal_entry >= num_entries)) {
        if (pindexed->is_GL) {
            int max_pen = num_entries - 1;

            while (pal_entry > max_pen)
                pal_entry -= max_pen;
        } else {
            pal_entry %= num_entries;
            if (pal_entry < 0)
                pal_entry += num_entries;
        }
    }

    pfrgrnd->is_cmy = (ppalet->pindexed->original_cspace == 1);
    pfrgrnd->color[0] = pindexed->palette.data[3 * pal_entry];
    pfrgrnd->color[1] = pindexed->palette.data[3 * pal_entry + 1];
    pfrgrnd->color[2] = pindexed->palette.data[3 * pal_entry + 2];
    pcl_cs_base_init_from(pfrgrnd->pbase, ppalet->pindexed->pbase);
    pcl_ht_init_from(pfrgrnd->pht, ppalet->pht);

    if (is_default)
        pcl_frgrnd_init_from(pcs->pdflt_frgrnd, pfrgrnd);

    return 0;
}

/*
 * Build the default foreground. This should be called by the reset function
 * for palettes, and should only be called when the current palette is the
 * default 2-entry palette.
 */
int
pcl_frgrnd_set_default_foreground(pcl_state_t * pcs)
{
    int code = 0;

    /* check that the palette is complete */
    if ((code = pcl_palette_check_complete(pcs)) < 0)
        return code;

    return build_foreground(pcs,
                            &(pcs->pfrgrnd), pcs->ppalet, 1, pcs->memory);
}

/*
 * ESC * v # S
 *
 * Set foreground. It is not clear the handling of negative values is accurate.
 */
static int
set_foreground(pcl_args_t * pargs, pcl_state_t * pcs)
{
    int code;

    if (pcs->personality == pcl5e || pcs->raster_state.graphics_mode)
        return 0;

    /* check that the palette is complete */
    if ((code = pcl_palette_check_complete(pcs)) < 0)
        return code;

    return build_foreground(pcs,
                            &(pcs->pfrgrnd),
                            pcs->ppalet, int_arg(pargs), pcs->memory);
}

/*
 * Initialization, reset, and copy procedures.
 *
 * There is no reset procedure for this module, as the function is accomplished
 * by the palette module.
 */
static int
frgrnd_do_registration(pcl_parser_state_t * pcl_parser_state,
                       gs_memory_t * mem)
{
    DEFINE_CLASS('*') {
        'v', 'S',
            PCL_COMMAND("Set Foreground", set_foreground,
                        pca_neg_ok | pca_raster_graphics)
    }, END_CLASS return 0;
}

static int
frgrnd_do_reset(pcl_state_t * pcs, pcl_reset_type_t type)
{
    if (type & (pcl_reset_permanent)) {
        rc_decrement(pcs->pfrgrnd, "foreground reset pfrgrnd");
        rc_decrement(pcs->pdflt_frgrnd, "foreground reset pdflt_frgrnd");
        rc_decrement(pcs->pwhite_cs, "foreground reset p_white_cs");
    }
    return 0;
}

static int
frgrnd_do_copy(pcl_state_t * psaved,
               const pcl_state_t * pcs, pcl_copy_operation_t operation)
{
    if ((operation & (pcl_copy_before_call | pcl_copy_before_overlay)) != 0)
        pcl_frgrnd_init_from(psaved->pfrgrnd, pcs->pfrgrnd);
    else if ((operation & pcl_copy_after) != 0)
        pcl_frgrnd_release(((pcl_state_t *) pcs)->pfrgrnd);
    return 0;
}

/* (white pattern or white foreground color) and transparent pattern
 * is a NOP
 */
bool
is_invisible_pattern(pcl_state_t * pcs)
{
    if (pcs->pattern_transparent) {
        if (pcs->pattern_type == pcl_pattern_solid_white)
            return true;
        if (pcs->pfrgrnd->color[0] == 0xff &&
            pcs->pfrgrnd->color[1] == 0xff && pcs->pfrgrnd->color[2] == 0xff)
            return true;        /* NB: depends on CMY conversion to internal RGB */
    }
    return false;
}

const pcl_init_t pcl_frgrnd_init =
    { frgrnd_do_registration, frgrnd_do_reset, frgrnd_do_copy };