home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Fresh Fish 5
/
FreshFish_July-August1994.bin
/
bbs
/
gnu
/
gs-2.6.1.4-src.lha
/
src
/
amiga
/
gs-2.6.1.4
/
gxstroke.c
< prev
next >
Wrap
C/C++ Source or Header
|
1994-01-27
|
26KB
|
761 lines
/* Copyright (C) 1989, 1992, 1993 Aladdin Enterprises. All rights reserved.
This file is part of Ghostscript.
Ghostscript is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY. No author or distributor accepts responsibility
to anyone for the consequences of using it or for whether it serves any
particular purpose or works at all, unless he says so in writing. Refer
to the Ghostscript General Public License for full details.
Everyone is granted permission to copy, modify and redistribute
Ghostscript, but only under the conditions described in the Ghostscript
General Public License. A copy of this license is supposed to have been
given to you along with Ghostscript so you can know your rights and
responsibilities. It should be in a file named COPYING. Among other
things, the copyright notice and this notice must be preserved on all
copies. */
/* gxstroke.c */
/* Path stroking procedures for Ghostscript library */
#include "math_.h"
#include "gx.h"
#include "gpcheck.h"
#include "gserrors.h"
#include "gxfixed.h"
#include "gxarith.h"
#include "gxmatrix.h"
#include "gscoord.h"
#include "gzstate.h"
#include "gzdevice.h"
#include "gzcolor.h" /* requires gxdevice.h */
#include "gzline.h"
#include "gzpath.h"
/* Define the filling adjustment that actually produces no adjustment. */
#define fill_no_adjust ((fixed)1)
/*
* Structure for a partial line (passed to the drawing routine).
* Two of these are required to do joins right.
* Each endpoint includes the two ends of the cap as well,
* and the deltas for square and round cap computation.
*
* The deltas (co, cdelta, ce) are in clockwise order in device space
* around the endpoint p: they are one-half the line width (suitably
* transformed) at 90 degrees counter-clockwise, straight ahead,
* and 90 degrees clockwise from the oriented line o->e,
* where "90 degrees" is measured in *user* coordinates.
* Note that the values at o are the negatives of the values at e.
*
* Initially, only o.p, e.p, e.cdelta, width, and thin are set.
* compute_caps fills in the rest.
*/
typedef gs_fixed_point _ss *p_ptr;
typedef struct endpoint_s {
gs_fixed_point p; /* the end of the line */
gs_fixed_point co, ce; /* ends of the cap, p +/- width */
gs_fixed_point cdelta; /* +/- cap length */
} endpoint;
typedef endpoint _ss *ep_ptr;
typedef struct partial_line_s {
endpoint o; /* starting coordinate */
endpoint e; /* ending coordinate */
gs_fixed_point width; /* one-half line width, see above */
int thin; /* true if minimum-width line */
} partial_line;
typedef partial_line _ss *pl_ptr;
/* Procedures that stroke a partial_line (the first pl_ptr argument). */
/* If both partial_lines are non-null, the procedure creates */
/* an appropriate join; otherwise, the procedure creates an */
/* end cap. If the first int is 0, the procedure also starts with */
/* an appropriate cap. */
private int near stroke_add(P5(gx_path *, int, pl_ptr, pl_ptr, gs_state *));
private int near stroke_fill(P5(gx_path *, int, pl_ptr, pl_ptr, gs_state *));
/* Other forward declarations */
private int near stroke(P4(const gx_path *, gx_path *,
int near (*)(P5(gx_path *, int, pl_ptr, pl_ptr, gs_state *)),
gs_state *));
private void near adjust_stroke(P2(pl_ptr, gs_state *));
private int near expand_dashes(P3(const subpath *, gx_path *, gs_state *));
private void near compute_caps(P1(pl_ptr));
private int near add_capped(P4(gx_path *, gs_line_cap,
int (*)(P3(gx_path *, fixed, fixed)),
ep_ptr));
/* Stroke a path for drawing or saving */
int
gx_stroke_fill(const gx_path *ppath, gs_state *pgs)
{ return stroke(ppath, (gx_path *)0, stroke_fill, pgs);
}
int
gx_stroke_add(const gx_path *ppath, gx_path *to_path, gs_state *pgs)
{ int code = stroke(ppath, to_path, stroke_add, pgs);
if ( code < 0 ) return code;
if ( !ppath->subpath_open && ppath->position_valid )
code = gx_path_add_point(to_path, ppath->position.x,
ppath->position.y);
return code;
}
/* Fill a partial stroked path. */
/* Free variables: code, to_path, ppath, stroke_path_body, pgs, exit (label). */
#define fill_stroke_path()\
if(to_path==&stroke_path_body && !gx_path_is_void_inline(&stroke_path_body))\
{ code = gx_fill_path(to_path, pgs->dev_color, pgs, gx_rule_winding_number,\
fill_no_adjust);\
gx_path_release(to_path);\
if ( code < 0 ) goto exit;\
gx_path_init(to_path, ppath->memory_procs);\
}
/* Stroke a path. Call line_proc (stroke_add or stroke_fill) */
/* for each line segment. */
private int near
stroke(const gx_path *ppath, gx_path *to_path,
int near (*line_proc)(P5(gx_path *, int, pl_ptr, pl_ptr, gs_state *)),
gs_state *pgs)
{ const subpath *psub;
const subpath *save_psub = 0;
int code = 0;
const line_params *lp = pgs->line_params;
int dash_count = lp->dash.pattern_size;
gx_path fpath, dpath;
gx_path stroke_path_body;
float xx = pgs->ctm.xx, xy = pgs->ctm.xy;
float yx = pgs->ctm.yx, yy = pgs->ctm.yy;
int skewed = !is_fzero2(xy, yx);
int uniform = (skewed ? 0 : xx == yy ? 1 : xx == -yy ? -1 : 0);
/*
* We are dealing with a reflected coordinate system
* if (1,0) is counter-clockwise from (0,1).
* See the note in stroke_add for the algorithm.
*/
int reflected =
(uniform ? uniform > 0 :
skewed ? xy * yx < xx * yy :
(xx < 0) == (yy < 0));
float line_width = lp->width; /* this is *half* the line width! */
int always_thin;
double line_width_and_scale, line_width_scale_xx;
#ifdef DEBUG
if ( gs_debug['o'] )
{ int count = lp->dash.pattern_size;
int i;
dprintf3("[o]half_width=%f, cap=%d, join=%d,\n",
lp->width, (int)lp->cap, (int)lp->join);
dprintf2(" miter_limit=%f, miter_check=%f,\n",
lp->miter_limit, lp->miter_check);
dprintf1(" dash pattern=%d", count);
for ( i = 0; i < count; i++ )
dprintf1(",%f", lp->dash.pattern[i]);
dprintf4(",\n offset=%f, init(ink_on=%d, index=%d, dist_left=%f)\n",
lp->dash.offset, lp->dash.init_ink_on, lp->dash.init_index,
lp->dash.init_dist_left);
}
#endif
if ( line_width < 0 ) line_width = -line_width;
if ( is_fzero(line_width) )
always_thin = 1;
else if ( !skewed )
{ float xxa = xx, yya = yy;
if ( xxa < 0 ) xxa = -xxa;
if ( yya < 0 ) yya = -yya;
always_thin = (max(xxa, yya) * line_width < 0.5);
}
else
{ /* The check is more complicated, but it's worth it. */
float xsq = xx * xx + xy * xy;
float ysq = yx * yx + yy * yy;
float cross = xx * yx + xy * yy;
if ( cross < 0 ) cross = 0;
always_thin =
((max(xsq, ysq) + cross) * line_width * line_width < 0.5);
}
line_width_and_scale = line_width * (double)int2fixed(1);
if ( !always_thin && uniform )
{ /* Precompute a value we'll need later. */
line_width_scale_xx = line_width_and_scale * xx;
if ( line_width_scale_xx < 0 )
line_width_scale_xx = -line_width_scale_xx;
}
if_debug5('o', "[o]ctm=(%g,%g,%g,%g) thin=%d\n",
xx, xy, yx, yy, always_thin);
/* Start by flattening the path. We should do this on-the-fly.... */
if ( !ppath->curve_count ) /* don't need to flatten */
{ psub = ppath->first_subpath;
if ( !psub ) return 0;
}
else
{ if ( (code = gx_path_flatten(ppath, &fpath, pgs->flatness, (int)pgs->in_cachedevice)) < 0 )
return code;
psub = fpath.first_subpath;
}
if ( dash_count )
gx_path_init(&dpath, ppath->memory_procs);
if ( to_path == 0 )
{ /* We might try to defer this if it's expensive.... */
to_path = &stroke_path_body;
gx_path_init(to_path, ppath->memory_procs);
}
for ( ; ; )
{ const line_segment *pline;
fixed x, y;
partial_line pl, pl_prev, pl_first;
int first = 0;
int index = 0;
if ( !psub )
{ /* Might just be the end of a dash expansion. */
if ( save_psub )
{ gx_path_release(&dpath);
psub = (const subpath *)save_psub->last->next;
if ( !psub ) break;
gx_path_init(&dpath, ppath->memory_procs);
save_psub = 0;
}
else /* all done */
break;
}
if ( dash_count && !save_psub )
{ code = expand_dashes(psub, &dpath, pgs);
if ( code < 0 ) goto exit;
save_psub = (subpath *)psub;
psub = dpath.first_subpath;
continue; /* psub might be null */
}
pline = (const line_segment *)(psub->n