summaryrefslogtreecommitdiff
path: root/pcl/pgpoly.c
blob: a54f25ca3970aac6a8184f8b7cf8a928156ca36f (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
/* Copyright (C) 2001-2012 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.,  7 Mt. Lassen Drive - Suite A-134, San Rafael,
   CA  94903, U.S.A., +1(415)492-9861, for further information.
*/


/* pgpoly.c -  HP-GL/2 polygon commands */

#include "std.h"
#include "pcparse.h"
#include "pgmand.h"
#include "pgdraw.h"
#include "pggeom.h"
#include "pgmisc.h"
#include "pcpatrn.h"
#include "gspath.h"
#include "gscoord.h"

/* ------ Internal procedures ------ */

/* Define fill/edge and absolute/relative flags. */
#define DO_EDGE 1
#define DO_RELATIVE 2

/* clear the polygon buffer by entering and exiting polygon mode. */
static int
hpgl_clear_polygon_buffer(hpgl_args_t *pargs, hpgl_state_t *pgls)
{
    hpgl_args_setup(pargs);
    hpgl_call(hpgl_PM(pargs, pgls));
    hpgl_args_set_int(pargs,2);
    hpgl_call(hpgl_PM(pargs, pgls));
    return 0;
}

/* Build a rectangle in polygon mode used by (EA, ER, RA, RR). */
static int
hpgl_rectangle(hpgl_args_t *pargs,  hpgl_state_t *pgls, int flags, bool do_poly)
{	hpgl_real_t x2, y2;
        if ( !hpgl_arg_units(pgls->memory, pargs, &x2) ||
             !hpgl_arg_units(pgls->memory, pargs, &y2) ||
             current_units_out_of_range(x2) ||
             current_units_out_of_range(y2) ) {
            hpgl_call(hpgl_clear_polygon_buffer(pargs, pgls));
            return e_Range;
        }

        if ( flags & DO_RELATIVE )
          {
            x2 += pgls->g.pos.x;
            y2 += pgls->g.pos.y;
          }

        if ( do_poly ) {
            hpgl_args_setup(pargs);
            /* enter polygon mode. */
            hpgl_call(hpgl_PM(pargs, pgls));
        }

        /* do the rectangle */
        {
          hpgl_real_t x1 = pgls->g.pos.x;
          hpgl_real_t y1 = pgls->g.pos.y;

          hpgl_call(hpgl_add_point_to_path(pgls, x1, y1, hpgl_plot_move_absolute, true));
          hpgl_call(hpgl_add_point_to_path(pgls, x2, y1, hpgl_plot_draw_absolute, true));
          hpgl_call(hpgl_add_point_to_path(pgls, x2, y2, hpgl_plot_draw_absolute, true));
          hpgl_call(hpgl_add_point_to_path(pgls, x1, y2, hpgl_plot_draw_absolute, true));
          hpgl_call(hpgl_close_current_path(pgls));
        }

        /* exit polygon mode PM2 */
        if ( do_poly ) {
            hpgl_args_set_int(pargs,2);
            hpgl_call(hpgl_PM(pargs, pgls));
        }
        return 0;
}

/* Fill or edge a wedge (EW, WG). */
static int
hpgl_wedge(hpgl_args_t *pargs, hpgl_state_t *pgls)
{	hpgl_real_t radius, start, sweep, chord = 5.0;

        if ( !hpgl_arg_units(pgls->memory, pargs, &radius) ||
             !hpgl_arg_c_real(pgls->memory, pargs, &start) ||
             !hpgl_arg_c_real(pgls->memory, pargs, &sweep) || sweep < -360 || sweep > 360
             ) {
            hpgl_call(hpgl_clear_polygon_buffer(pargs, pgls));
            return e_Range;
        }

        hpgl_arg_c_real(pgls->memory, pargs, &chord);

        /* enter polygon mode */
        hpgl_args_setup(pargs);
        hpgl_call(hpgl_PM(pargs, pgls));

        if ( sweep > 359.9 || sweep < -359.9) {
            floatp num_chordsf = 360 / chord;
            /* match hp 4600 rounding, precompute since regular arc rounding is different. */
            floatp intpart;
            int num_chords = (int)((modf(num_chordsf, &intpart) < 0.06) ? intpart : intpart+1);
            floatp integral_chord_angle = fabs(sweep / num_chords);

            hpgl_call(hpgl_add_arc_to_path(pgls, pgls->g.pos.x, pgls->g.pos.y,
                                           radius, start, 360.0, integral_chord_angle, true,
                                           hpgl_plot_draw_absolute, true));
        }
        else
        /* draw the 2 lines and the arc using 3 point this does seem
           convoluted but it does guarantee that the endpoint lines
           for the vectors and the arc endpoints are coincident. */
          {
            hpgl_real_t x1, y1, x2, y2, x3, y3;
            hpgl_compute_vector_endpoints(radius, pgls->g.pos.x, pgls->g.pos.y,
                                          start, &x1, &y1);
            hpgl_compute_vector_endpoints(radius, pgls->g.pos.x, pgls->g.pos.y,
                                          (start + (sweep / 2.0)), &x2, &y2);
            hpgl_compute_vector_endpoints(radius, pgls->g.pos.x, pgls->g.pos.y,
                                          (start + sweep), &x3, &y3);
            hpgl_call(hpgl_add_point_to_path(pgls, pgls->g.pos.x, pgls->g.pos.y,
                                             hpgl_plot_move_absolute, true));
            hpgl_call(hpgl_add_point_to_path(pgls, x1, y1,
                                             hpgl_plot_draw_absolute, true));
            hpgl_call(hpgl_add_arc_3point_to_path(pgls,
                                                  x1, y1,
                                                  x2, y2,
                                                  x3, y3, chord,
                                                  hpgl_plot_draw_absolute));
          }

        hpgl_call(hpgl_close_current_path(pgls));
        hpgl_args_set_int(pargs,2);
        hpgl_call(hpgl_PM(pargs, pgls));

        return 0;
}

/* ------ Commands ------ */

/* EA x,y; */
int
hpgl_EA(hpgl_args_t *pargs, hpgl_state_t *pgls)
{
        hpgl_call(hpgl_rectangle(pargs, pgls, DO_EDGE, true));
        hpgl_call(hpgl_copy_polygon_buffer_to_current_path(pgls));
        hpgl_set_hpgl_path_mode(pgls, true);
        hpgl_call(hpgl_draw_current_path(pgls, hpgl_rm_vector));
        hpgl_set_hpgl_path_mode(pgls, false);
        return 0;
}

/* EP; */
int
hpgl_EP(hpgl_args_t *pargs, hpgl_state_t *pgls)
{
        /* preserve the current path and copy the polygon buffer to
           the current path */
        hpgl_call(hpgl_gsave(pgls));
        hpgl_call(hpgl_copy_polygon_buffer_to_current_path(pgls));
        hpgl_set_hpgl_path_mode(pgls, true);
        hpgl_call(hpgl_draw_current_path(pgls, hpgl_rm_vector_no_close));
        hpgl_set_hpgl_path_mode(pgls, false);
        hpgl_call(hpgl_grestore(pgls));
        return 0;
}

/* ER dx,dy; */
int
hpgl_ER(hpgl_args_t *pargs, hpgl_state_t *pgls)
{
        hpgl_call(hpgl_rectangle(pargs, pgls, DO_RELATIVE, true));
        hpgl_call(hpgl_copy_polygon_buffer_to_current_path(pgls));
        hpgl_set_hpgl_path_mode(pgls, true);
        hpgl_call(hpgl_draw_current_path(pgls, hpgl_rm_vector));
        hpgl_set_hpgl_path_mode(pgls, false);
        return 0;
}

/* EW radius,astart,asweep[,achord]; */
int
hpgl_EW(hpgl_args_t *pargs, hpgl_state_t *pgls)
{
        hpgl_call(hpgl_wedge(pargs, pgls));
        hpgl_call(hpgl_copy_polygon_buffer_to_current_path(pgls));
        hpgl_set_hpgl_path_mode(pgls, true);
        hpgl_call(hpgl_draw_current_path(pgls, hpgl_rm_vector));
        hpgl_set_hpgl_path_mode(pgls, false);
        return 0;
}

static hpgl_rendering_mode_t
hpgl_get_poly_render_mode(
    hpgl_state_t *              pgls
)
{
    hpgl_FT_pattern_source_t    type = pgls->g.fill.type;

    return ( ((type == hpgl_FT_pattern_one_line) ||
              (type == hpgl_FT_pattern_two_lines)  )
                ? hpgl_rm_clip_and_fill_polygon
                : hpgl_rm_polygon );
}

/* FP method; */
/* FP; */
int
hpgl_FP(hpgl_args_t *pargs, hpgl_state_t *pgls)
{	int method = 0;

    if ( hpgl_arg_c_int(pgls->memory, pargs, &method) && (method & ~1) ) {
        hpgl_call(hpgl_clear_polygon_buffer(pargs, pgls));
        return e_Range;
    }
    pgls->g.fill_type = (method == 0) ?
        hpgl_even_odd_rule : hpgl_winding_number_rule;
    hpgl_call(hpgl_copy_polygon_buffer_to_current_path(pgls));
    hpgl_set_hpgl_path_mode(pgls, true);
    hpgl_call(hpgl_draw_current_path(pgls,
                                     hpgl_get_poly_render_mode(pgls)));
    hpgl_set_hpgl_path_mode(pgls, false);
    return 0;
}

/* close a subpolygon; PM1 or CI inside of a polygon */
int
hpgl_close_subpolygon(hpgl_state_t *pgls)
{
    hpgl_call(hpgl_close_current_path(pgls));
    pgls->g.subpolygon_started = true;
    return 0;
}

/* PM op; */
int
hpgl_PM(hpgl_args_t *pargs, hpgl_state_t *pgls)
{	int op;

        if ( hpgl_arg_c_int(pgls->memory, pargs, &op) == 0 )
          op = 0;

        switch( op )
          {
          case 0 :
            /* draw the current path if there is one */
            hpgl_call(hpgl_draw_current_path(pgls, hpgl_rm_vector));
            /* clear the polygon buffer as well */
            gx_path_new(&pgls->g.polygon.buffer.path);
            /* global flag to indicate that we are in polygon mode */
            pgls->g.polygon_mode = true;
            /* save the pen state, to be restored by PM2 */
            hpgl_save_pen_state(pgls,
                                &pgls->g.polygon.pen_state,
                                hpgl_pen_down | hpgl_pen_pos);
            hpgl_set_hpgl_path_mode(pgls, true);
            break;
          case 1 :
              hpgl_call(hpgl_close_subpolygon(pgls));
              break;
          case 2 :
              if ( pgls->g.polygon_mode ) {
                  /* explicitly close the path if the pen is down */
                  if ( pgls->g.move_or_draw == hpgl_pen_down
                       && pgls->g.have_drawn_in_path )
                      hpgl_call(hpgl_close_current_path(pgls));
                  /* make a copy of the path and clear the current path */
                  hpgl_call(hpgl_copy_current_path_to_polygon_buffer(pgls));
                  hpgl_call(hpgl_clear_current_path(pgls));
                  /* return to vector mode */
                  pgls->g.polygon_mode = false;
                  /* restore the pen state */
                  hpgl_restore_pen_state(pgls,
                                         &pgls->g.polygon.pen_state,
                                         hpgl_pen_down | hpgl_pen_pos);
              }
              hpgl_set_hpgl_path_mode(pgls, false);
              break;
          default:
            return e_Range;
          }
        return 0;
}

/* RA x,y; */
int
hpgl_RA(hpgl_args_t *pargs, hpgl_state_t *pgls)
{
        hpgl_call(hpgl_rectangle(pargs, pgls, 0, true));
        hpgl_call(hpgl_copy_polygon_buffer_to_current_path(pgls));
        hpgl_set_hpgl_path_mode(pgls, true);
        hpgl_call(hpgl_draw_current_path(pgls,
                                         hpgl_get_poly_render_mode(pgls)));
        hpgl_set_hpgl_path_mode(pgls, false);
        return 0;
}

/* RR dx,dy;*/
int
hpgl_RR(hpgl_args_t *pargs, hpgl_state_t *pgls)
{
        hpgl_call(hpgl_rectangle(pargs, pgls, DO_RELATIVE, true));
        hpgl_call(hpgl_copy_polygon_buffer_to_current_path(pgls));
        hpgl_set_hpgl_path_mode(pgls, true);
        hpgl_call(hpgl_draw_current_path(pgls,
                                         hpgl_get_poly_render_mode(pgls)));
        hpgl_set_hpgl_path_mode(pgls, false);
        return 0;
}

/* RQ dx,dy;*/
int
hpgl_RQ(hpgl_args_t *pargs, hpgl_state_t *pgls)
{
    /* contary to the specification HP uses default pixel placement
       with RQ */
    byte save_pp = pgls->pp_mode;
    pgls->pp_mode = 0;
    hpgl_call(hpgl_rectangle(pargs, pgls, DO_RELATIVE, false));
    hpgl_set_hpgl_path_mode(pgls, true);
    hpgl_call(hpgl_draw_current_path(pgls,
                                     hpgl_get_poly_render_mode(pgls)));
    /* restore saved pixel placement mode */
    hpgl_set_hpgl_path_mode(pgls, false);
    pgls->pp_mode = save_pp;
    return 0;
}

/* WG radius,astart,asweep[,achord]; */
int
hpgl_WG(hpgl_args_t *pargs, hpgl_state_t *pgls)
{
        hpgl_call(hpgl_wedge(pargs, pgls));
        hpgl_call(hpgl_copy_polygon_buffer_to_current_path(pgls));
        hpgl_set_hpgl_path_mode(pgls, true);
        hpgl_call(hpgl_draw_current_path(pgls,
                                         hpgl_get_poly_render_mode(pgls)));
        hpgl_set_hpgl_path_mode(pgls, false);
        return 0;
}

/* Initialization */
static int
pgpoly_do_registration(
    pcl_parser_state_t *pcl_parser_state,
    gs_memory_t *mem)
{		/* Register commands */
    DEFINE_HPGL_COMMANDS(mem)
          HPGL_COMMAND('E', 'A', hpgl_EA, hpgl_cdf_lost_mode_cleared|hpgl_cdf_pcl_rtl_both),
          HPGL_COMMAND('E', 'P', hpgl_EP, hpgl_cdf_pcl_rtl_both),
          HPGL_COMMAND('E', 'R', hpgl_ER, hpgl_cdf_lost_mode_cleared|hpgl_cdf_pcl_rtl_both),
          HPGL_COMMAND('E', 'W', hpgl_EW, hpgl_cdf_lost_mode_cleared|hpgl_cdf_pcl_rtl_both),
          HPGL_COMMAND('F', 'P', hpgl_FP, hpgl_cdf_pcl_rtl_both),
          HPGL_COMMAND('P', 'M', hpgl_PM, hpgl_cdf_polygon|hpgl_cdf_lost_mode_cleared|hpgl_cdf_pcl_rtl_both),
          HPGL_COMMAND('R', 'A', hpgl_RA, hpgl_cdf_lost_mode_cleared|hpgl_cdf_pcl_rtl_both),
          HPGL_COMMAND('R', 'R', hpgl_RR, hpgl_cdf_lost_mode_cleared|hpgl_cdf_pcl_rtl_both),
          HPGL_COMMAND('R', 'Q', hpgl_RQ, hpgl_cdf_lost_mode_cleared|hpgl_cdf_pcl_rtl_both),
          HPGL_COMMAND('W', 'G', hpgl_WG, hpgl_cdf_lost_mode_cleared|hpgl_cdf_pcl_rtl_both),
        END_HPGL_COMMANDS
        return 0;
}
const pcl_init_t pgpoly_init = {
  pgpoly_do_registration, 0
};