summaryrefslogtreecommitdiff
path: root/gs/base/gspath.c
blob: 4964b2a08ad32ec5b49eca66c14f15e312151760 (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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
/* Copyright (C) 2001-2006 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 that
   license.  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.
*/

/* $Id$ */
/* Basic path routines for Ghostscript library */
#include "gx.h"
#include "math_.h"
#include "gserrors.h"
#include "gxfixed.h"
#include "gxmatrix.h"
#include "gscoord.h"		/* requires gsmatrix.h */
#include "gspath.h"		/* for checking prototypes */
#include "gzstate.h"
#include "gzpath.h"
#include "gxdevice.h"		/* for gxcpath.h */
#include "gxdevmem.h"		/* for gs_device_is_memory */
#include "gzcpath.h"

/* ------ Miscellaneous ------ */

int
gs_newpath(gs_state * pgs)
{
    pgs->current_point_valid = false;
    return gx_path_new(pgs->path);
}

int
gs_closepath(gs_state * pgs)
{
    gx_path *ppath = pgs->path;
    int code = gx_path_close_subpath(ppath);

    if (code < 0)
        return code;
    pgs->current_point = pgs->subpath_start;
    return code;
}

int
gs_upmergepath(gs_state * pgs)
{
    /*
     * We really should be able to implement this as simply
     *   return gx_path_add_path(pgs->saved->path, pgs->path);
     * But because of the current_point members in the imager state,
     * we can't.
     */
    gs_state *saved = pgs->saved;
    int code;

    code = gx_path_add_path(saved->path, pgs->path);
    if (code < 0)
        return code;
    if (pgs->current_point_valid) {
        saved->current_point = pgs->current_point;
        saved->subpath_start = pgs->subpath_start;
        saved->current_point_valid = true;
    }
    return code;
}

/* Get the current path (for internal use only). */
gx_path *
gx_current_path(const gs_state * pgs)
{
    return pgs->path;
}

/* ------ Points and lines ------ */

static inline void
clamp_point(gs_fixed_point * ppt, floatp x, floatp y)
{
    ppt->x = clamp_coord(x);
    ppt->y = clamp_coord(y);
}

int
gs_currentpoint(gs_state * pgs, gs_point * ppt)
{
    if (!pgs->current_point_valid)
        return_error(gs_error_nocurrentpoint);
    return gs_itransform(pgs, pgs->current_point.x,
                              pgs->current_point.y, ppt);
}

static inline int
gs_point_transform_compat(floatp x, floatp y, const gs_matrix_fixed *m, gs_point *pt)
{
#if !PRECISE_CURRENTPOINT
    gs_fixed_point p;
    int code = gs_point_transform2fixed(m, x, y, &p);

    if (code < 0)
        return code;
    pt->x = fixed2float(p.x);
    pt->y = fixed2float(p.y);
    return 0;
#else
    return gs_point_transform(x, y, (const gs_matrix *)m, pt);
#endif
}

static inline int
gs_distance_transform_compat(floatp x, floatp y, const gs_matrix_fixed *m, gs_point *pt)
{
#if !PRECISE_CURRENTPOINT
    gs_fixed_point p;
    int code = gs_distance_transform2fixed(m, x, y, &p);

    if (code < 0)
        return code;
    pt->x = fixed2float(p.x);
    pt->y = fixed2float(p.y);
    return 0;
#else
    return gs_distance_transform(x, y, (const gs_matrix *)m, pt);
#endif
}

static inline int
clamp_point_aux(bool clamp_coordinates, gs_fixed_point *ppt, floatp x, floatp y)
{
    if (!f_fits_in_bits(x, fixed_int_bits) || !f_fits_in_bits(y, fixed_int_bits)) {
        if (!clamp_coordinates)
            return_error(gs_error_limitcheck);
        clamp_point(ppt, x, y);
    } else {
        /* 181-01.ps" fails with no rounding in
           "Verify as last element of a userpath and effect on setbbox." */
        ppt->x = float2fixed_rounded(x);
        ppt->y = float2fixed_rounded(y);
    }
    return 0;
}

int
gs_moveto_aux(gs_imager_state *pis, gx_path *ppath, floatp x, floatp y)
{
    gs_fixed_point pt;
    int code;

    code = clamp_point_aux(pis->clamp_coordinates, &pt, x, y);
    if (code < 0)
        return code;
    code = gx_path_add_point(ppath, pt.x, pt.y);
    if (code < 0)
        return code;
    ppath->start_flags = ppath->state_flags;
    gx_setcurrentpoint(pis, x, y);
    pis->subpath_start = pis->current_point;
    pis->current_point_valid = true;
    return 0;
}

int
gs_moveto(gs_state * pgs, floatp x, floatp y)
{
    gs_point pt;
    int code = gs_point_transform_compat(x, y, &pgs->ctm, &pt);

    if (code < 0)
        return code;
    return gs_moveto_aux((gs_imager_state *)pgs, pgs->path, pt.x, pt.y);
}

int
gs_rmoveto(gs_state * pgs, floatp x, floatp y)
{
    gs_point dd;
    int code;

    if (!pgs->current_point_valid)
        return_error(gs_error_nocurrentpoint);
    code = gs_distance_transform_compat(x, y, &pgs->ctm, &dd);
    if (code < 0)
        return code;
    /* fixme : check in range. */
    return gs_moveto_aux((gs_imager_state *)pgs, pgs->path,
                dd.x + pgs->current_point.x, dd.y + pgs->current_point.y);
}

static inline int
gs_lineto_aux(gs_state * pgs, floatp x, floatp y)
{
    gx_path *ppath = pgs->path;
    gs_fixed_point pt;
    int code;

    code = clamp_point_aux(pgs->clamp_coordinates, &pt, x, y);
    if (code < 0)
        return code;
    code = gx_path_add_line(ppath, pt.x, pt.y);
    if (code < 0)
        return code;
    gx_setcurrentpoint(pgs, x, y);
    return 0;
}

int
gs_lineto(gs_state * pgs, floatp x, floatp y)
{
    gs_point pt;
    int code = gs_point_transform_compat(x, y, &pgs->ctm, &pt);

    if (code < 0)
        return code;
    return gs_lineto_aux(pgs, pt.x, pt.y);
}

int
gs_rlineto(gs_state * pgs, floatp x, floatp y)
{
    gs_point dd;
    int code;

    if (!pgs->current_point_valid)
        return_error(gs_error_nocurrentpoint);
    code = gs_distance_transform_compat(x, y, &pgs->ctm, &dd);
    if (code < 0)
        return code;
    /* fixme : check in range. */
    return gs_lineto_aux(pgs, dd.x + pgs->current_point.x,
                              dd.y + pgs->current_point.y);
}

/* ------ Curves ------ */

static inline int
gs_curveto_aux(gs_state * pgs,
           floatp x1, floatp y1, floatp x2, floatp y2, floatp x3, floatp y3)
{
    gs_fixed_point p1, p2, p3;
    int code;
    gx_path *ppath = pgs->path;

    code = clamp_point_aux(pgs->clamp_coordinates, &p1, x1, y1);
    if (code < 0)
        return code;
    code = clamp_point_aux(pgs->clamp_coordinates, &p2, x2, y2);
    if (code < 0)
        return code;
    code = clamp_point_aux(pgs->clamp_coordinates, &p3, x3, y3);
    if (code < 0)
        return code;
    code = gx_path_add_curve(ppath, p1.x, p1.y, p2.x, p2.y, p3.x, p3.y);
    if (code < 0)
        return code;
    gx_setcurrentpoint(pgs, x3, y3);
    return 0;
}

int
gs_curveto(gs_state * pgs,
           floatp x1, floatp y1, floatp x2, floatp y2, floatp x3, floatp y3)
{
    gs_point pt1, pt2, pt3;
    int code;

    code = gs_point_transform_compat(x1, y1, &pgs->ctm, &pt1);
    if (code < 0)
        return code;
    code = gs_point_transform_compat(x2, y2, &pgs->ctm, &pt2);
    if (code < 0)
        return code;
    code = gs_point_transform_compat(x3, y3, &pgs->ctm, &pt3);
    if (code < 0)
        return code;
    return gs_curveto_aux(pgs,   pt1.x, pt1.y,   pt2.x, pt2.y,   pt3.x, pt3.y);
}

int
gs_rcurveto(gs_state * pgs,
     floatp dx1, floatp dy1, floatp dx2, floatp dy2, floatp dx3, floatp dy3)
{
    gs_point dd1, dd2, dd3;
    int code;

    if (!pgs->current_point_valid)
        return_error(gs_error_nocurrentpoint);
    code = gs_distance_transform_compat(dx1, dy1, &pgs->ctm, &dd1);
    if (code < 0)
        return code;
    code = gs_distance_transform_compat(dx2, dy2, &pgs->ctm, &dd2);
    if (code < 0)
        return code;
    code = gs_distance_transform_compat(dx3, dy3, &pgs->ctm, &dd3);
    if (code < 0)
        return code;
    /* fixme : check in range. */
    return gs_curveto_aux(pgs, dd1.x + pgs->current_point.x, dd1.y + pgs->current_point.y,
                               dd2.x + pgs->current_point.x, dd2.y + pgs->current_point.y,
                               dd3.x + pgs->current_point.x, dd3.y + pgs->current_point.y);
}

/* ------ Clipping ------ */

/* Forward references */
static int common_clip(gs_state *, int);

/*
 * Return the effective clipping path of a graphics state.  Sometimes this
 * is the intersection of the clip path and the view clip path; sometimes it
 * is just the clip path.  We aren't sure what the correct algorithm is for
 * this: for now, we use view clipping unless the current device is a memory
 * device.  This takes care of the most important case, where the current
 * device is a cache device.
 */
int
gx_effective_clip_path(gs_state * pgs, gx_clip_path ** ppcpath)
{
    gs_id view_clip_id =
        (pgs->view_clip == 0 || pgs->view_clip->rule == 0 ? gs_no_id :
         pgs->view_clip->id);

    if (gs_device_is_memory(pgs->device)) {
        *ppcpath = pgs->clip_path;
        return 0;
    }
    if (pgs->effective_clip_id == pgs->clip_path->id &&
        pgs->effective_view_clip_id == view_clip_id
        ) {
        *ppcpath = pgs->effective_clip_path;
        return 0;
    }
    /* Update the cache. */
    if (view_clip_id == gs_no_id) {
        if (!pgs->effective_clip_shared)
            gx_cpath_free(pgs->effective_clip_path, "gx_effective_clip_path");
        pgs->effective_clip_path = pgs->clip_path;
        pgs->effective_clip_shared = true;
    } else {
        gs_fixed_rect cbox, vcbox;

        gx_cpath_inner_box(pgs->clip_path, &cbox);
        gx_cpath_outer_box(pgs->view_clip, &vcbox);
        if (rect_within(vcbox, cbox)) {
            if (!pgs->effective_clip_shared)
                gx_cpath_free(pgs->effective_clip_path,
                              "gx_effective_clip_path");
            pgs->effective_clip_path = pgs->view_clip;
            pgs->effective_clip_shared = true;
        } else {
            /* Construct the intersection of the two clip paths. */
            int code;
            gx_clip_path ipath;
            gx_path vpath;
            gx_clip_path *npath = pgs->effective_clip_path;

            if (pgs->effective_clip_shared) {
                npath = gx_cpath_alloc(pgs->memory, "gx_effective_clip_path");
                if (npath == 0)
                    return_error(gs_error_VMerror);
            }
            gx_cpath_init_local(&ipath, pgs->memory);
            code = gx_cpath_assign_preserve(&ipath, pgs->clip_path);
            if (code < 0)
                return code;
            gx_path_init_local(&vpath, pgs->memory);
            code = gx_cpath_to_path(pgs->view_clip, &vpath);
            if (code < 0 ||
                (code = gx_cpath_clip(pgs, &ipath, &vpath,
                                      gx_rule_winding_number)) < 0 ||
                (code = gx_cpath_assign_free(npath, &ipath)) < 0
                )
                DO_NOTHING;
            gx_path_free(&vpath, "gx_effective_clip_path");
            gx_cpath_free(&ipath, "gx_effective_clip_path");
            if (code < 0)
                return code;
            pgs->effective_clip_path = npath;
            pgs->effective_clip_shared = false;
        }
    }
    pgs->effective_clip_id = pgs->effective_clip_path->id;
    pgs->effective_view_clip_id = view_clip_id;
    *ppcpath = pgs->effective_clip_path;
    return 0;
}

#ifdef DEBUG
/* Note that we just set the clipping path (internal). */
static void
note_set_clip_path(const gs_state * pgs)
{
    if (gs_debug_c('P')) {
        dlprintf("[P]Clipping path:\n");
        gx_cpath_print(pgs->clip_path);
    }
}
#else
#  define note_set_clip_path(pgs) DO_NOTHING
#endif

int
gs_clippath(gs_state * pgs)
{
    gx_path cpath;
    int code;

    gx_path_init_local(&cpath, pgs->path->memory);
    code = gx_cpath_to_path(pgs->clip_path, &cpath);
    if (code >= 0) {
        code = gx_path_assign_free(pgs->path, &cpath);
        pgs->current_point.x = fixed2float(pgs->path->position.x);
        pgs->current_point.y = fixed2float(pgs->path->position.y);
        pgs->current_point_valid = true;
    }
    if (code < 0)
        gx_path_free(&cpath, "gs_clippath");
    return code;
}

int
gs_initclip(gs_state * pgs)
{
    gs_fixed_rect box;
    int code = gx_default_clip_box(pgs, &box);

    if (code < 0)
        return code;
    return gx_clip_to_rectangle(pgs, &box);
}

int
gs_clip(gs_state * pgs)
{
    return common_clip(pgs, gx_rule_winding_number);
}

int
gs_eoclip(gs_state * pgs)
{
    return common_clip(pgs, gx_rule_even_odd);
}

static int
common_clip(gs_state * pgs, int rule)
{
    int code = gx_cpath_clip(pgs, pgs->clip_path, pgs->path, rule);
    if (code < 0)
        return code;
    pgs->clip_path->rule = rule;
    note_set_clip_path(pgs);
    return 0;
}

/* Establish a rectangle as the clipping path. */
/* Used by initclip and by the character and Pattern cache logic. */
int
gx_clip_to_rectangle(gs_state * pgs, gs_fixed_rect * pbox)
{
    int code = gx_cpath_from_rectangle(pgs->clip_path, pbox);

    if (code < 0)
        return code;
    pgs->clip_path->rule = gx_rule_winding_number;
    note_set_clip_path(pgs);
    return 0;
}

/* Set the clipping path to the current path, without intersecting. */
/* This is very inefficient right now. */
int
gx_clip_to_path(gs_state * pgs)
{
    gs_fixed_rect bbox;
    int code;

    if ((code = gx_path_bbox(pgs->path, &bbox)) < 0 ||
        (code = gx_clip_to_rectangle(pgs, &bbox)) < 0 ||
        (code = gs_clip(pgs)) < 0
        )
        return code;
    note_set_clip_path(pgs);
    return 0;
}

/* Get the default clipping box. */
int
gx_default_clip_box(const gs_state * pgs, gs_fixed_rect * pbox)
{
    register gx_device *dev = gs_currentdevice(pgs);
    gs_rect bbox;
    gs_matrix imat;
    int code;

    if (dev->ImagingBBox_set) {	/* Use the ImagingBBox, relative to default user space. */
        gs_defaultmatrix(pgs, &imat);
        bbox.p.x = dev->ImagingBBox[0];
        bbox.p.y = dev->ImagingBBox[1];
        bbox.q.x = dev->ImagingBBox[2];
        bbox.q.y = dev->ImagingBBox[3];
    } else {			/* Use the MediaSize indented by the HWMargins, */
        /* relative to unrotated user space adjusted by */
        /* the Margins.  (We suspect this isn't quite right, */
        /* but the whole issue of "margins" is such a mess that */
        /* we don't think we can do any better.) */
        (*dev_proc(dev, get_initial_matrix)) (dev, &imat);
        /* Adjust for the Margins. */
        imat.tx += dev->Margins[0] * dev->HWResolution[0] /
            dev->MarginsHWResolution[0];
        imat.ty += dev->Margins[1] * dev->HWResolution[1] /
            dev->MarginsHWResolution[1];
        bbox.p.x = dev->HWMargins[0];
        bbox.p.y = dev->HWMargins[1];
        bbox.q.x = dev->MediaSize[0] - dev->HWMargins[2];
        bbox.q.y = dev->MediaSize[1] - dev->HWMargins[3];
    }
    code = gs_bbox_transform(&bbox, &imat, &bbox);
    if (code < 0)
        return code;
    /* Round the clipping box so that it doesn't get ceilinged. */
    pbox->p.x = fixed_rounded(float2fixed(bbox.p.x));
    pbox->p.y = fixed_rounded(float2fixed(bbox.p.y));
    pbox->q.x = fixed_rounded(float2fixed(bbox.q.x));
    pbox->q.y = fixed_rounded(float2fixed(bbox.q.y));
    return 0;
}