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
/
gxpcopy.c
< prev
next >
Wrap
C/C++ Source or Header
|
1994-01-27
|
17KB
|
501 lines
/* Copyright (C) 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. */
/* gxpcopy.c */
/* Path copying and flattening for Ghostscript library */
#include "math_.h"
#include "gx.h"
#include "gserrors.h"
#include "gxfixed.h"
#include "gxarith.h"
#include "gzpath.h"
/* Forward declarations */
private int copy_path(P4(const gx_path *, gx_path *, fixed, int));
private int flatten_recur(P8(gx_path *,
fixed, fixed, fixed, fixed, fixed, fixed, fixed));
private int flatten_sample(P8(gx_path *, int,
fixed, fixed, fixed, fixed, fixed, fixed));
/* Copy a path */
int
gx_path_copy(const gx_path *ppath_old, gx_path *ppath, int init)
{ return copy_path(ppath_old, ppath, max_fixed, init);
}
/* Flatten a path. */
/* If flatness is zero, use sampling rather than subdivision: */
/* this is important for Type 1 fonts. */
private fixed
scale_flatness(floatp flatness)
{ fixed scaled_flat = float2fixed(flatness);
return (scaled_flat > int2fixed(100) ? int2fixed(100) :
scaled_flat < fixed_half ? fixed_0 :
scaled_flat);
}
int
gx_path_flatten(const gx_path *ppath_old, gx_path *ppath, floatp flatness, int in_BuildCharGlyph)
{ return copy_path(ppath_old, ppath,
(in_BuildCharGlyph ? fixed_0 : scale_flatness(flatness)),
1);
}
/* Add a flattened curve to a path. */
int
gx_path_add_flattened_curve(gx_path *ppath,
fixed x1, fixed y1, fixed x2, fixed y2, fixed x3, fixed y3,
floatp flatness)
{ return flatten_recur(ppath, x1, y1, x2, y2, x3, y3,
scale_flatness(flatness));
}
/* Copy a path, optionally flattening it. */
/* If the copy fails, free the new path. */
private int
copy_path(const gx_path *ppath_old, gx_path *ppath, fixed scaled_flat,
int init)
{ gx_path old;
const segment *pseg;
int code;
#ifdef DEBUG
if ( gs_debug['p'] )
gx_dump_path(ppath_old, "before copy_path");
#endif
old = *ppath_old;
if ( init )
gx_path_init(ppath, ppath_old->memory_procs);
pseg = (const segment *)(old.first_subpath);
while ( pseg )
{ switch ( pseg->type )
{
case s_start:
code = gx_path_add_point(ppath, pseg->pt.x, pseg->pt.y);
break;
case s_curve:
{ curve_segment *pc = (curve_segment *)pseg;
if ( scaled_flat == max_fixed ) /* don't flatten */
code = gx_path_add_curve(ppath,
pc->p1.x, pc->p1.y,
pc->p2.x, pc->p2.y,
pc->pt.x, pc->pt.y);
else
code = flatten_recur(ppath,
pc->p1.x, pc->p1.y,
pc->p2.x, pc->p2.y,
pc->pt.x, pc->pt.y,
scaled_flat);
break;
}
case s_line:
code = gx_path_add_line(ppath, pseg->pt.x, pseg->pt.y);
break;
case s_line_close:
code = gx_path_close_subpath(ppath);
break;
}
if ( code )
{ gx_path_release(ppath);
if ( ppath == ppath_old )
*ppath = old;
return code;
}
pseg = pseg->next;
}
if ( !old.subpath_open && old.position_valid )
gx_path_add_point(ppath, old.position.x, old.position.y);
#ifdef DEBUG
if ( gs_debug['p'] )
gx_dump_path(ppath, "after copy_path");
#endif
return 0;
}
/* Internal routine to flatten a curve. */
/* This calls itself recursively, using binary subdivision, */
/* until the approximation is good enough to satisfy the */
/* flatness requirement. The starting point is ppath->position, */
/* which gets updated as line segments are added. */
/* Maximum number of points for sampling if we want accurate rasterizing. */
/* (num_sample_max - 1)^3 must fit into an int with a bit to spare. */
#if arch_sizeof_int == 2
# define num_sample_max 26
#else
# define num_sample_max (1 << ((size_of(int) * 8 - 2) / 3))
#endif
/* Table of f(i) = 256 * sqrt(1 + (i/64)^2). */
/* This is good to within 1% or better. */
#define sqrt_index_shift 6 /* scaling of index */
#define sqrt_value_shift 8 /* scaling of value */
private int scaled_sqrt_tab[65] =
{ 256, 256, 256, 256, 256, 256, 257, 257,
257, 258, 259, 259, 260, 261, 262, 262,
263, 264, 265, 267, 268, 269, 270, 272,
273, 274, 276, 277, 279, 281, 282, 284,
286, 288, 289, 291, 293, 295, 297, 299,
301, 304, 306, 308, 310, 312, 315, 317,
320, 322, 324, 327, 329, 332, 334, 337,
340, 342, 345, 348, 350, 353, 356, 359,
362
};
private int
flatten_recur(gx_path *ppath,
fixed x1, fixed y1, fixed x2, fixed y2, fixed x3, fixed y3,
fixed scaled_flat)
{ fixed
x0 = ppath->position.x,
y0 = ppath->position.y;
top:
#ifdef DEBUG
if ( gs_debug['2'] )
dprintf4("[2]x0=%f y0=%f x1=%f y1=%f\n",
fixed2float(x0), fixed2float(y0),
fixed2float(x1), fixed2float(y1)),
dprintf4(" x2=%f y2=%f x3=%f y3=%f\n",
fixed2float(x2), fixed2float(y2),
fixed2float(x3), fixed2float(y3));
#endif
/*
* Compute the maximum distance of the curve from
* the line (x0,y0)->(x3,y3). We do this conservatively
* by observing that the curve is enclosed by the
* quadrilateral of its control points, so we simply
* compute the distances of (x1,y1) and (x2,y2)
* from the line. Letting dx = x3-x0 and dy = y3-y0,
* the distance of (xp,yp) from the line is
* abs(N)/sqrt(D), where N = dy*(xp-x0)-dx*(yp-y0) and
* D = dx*dx+dy*dy; hence we want to test abs(N) <= sqrt(D)*F,
* where F is the flatness parameter from the graphics state.
* We can do this more efficiently by letting t=dy/dx, and
* testing abs(N1) <= sqrt(D1)*f, where N1=t*(xp-x0)-(yp-y0) and
* D1 = 1+t*t. If dx < dy, we swap x and y for this
* computation. This guarantees abs(t) <= 1, which allows us to
* compute sqrt(1+t*t) by table lookup on the high bits of abs(t).
*
* To avoid replacing shallow curves by short straight lines,
* we halve the flatness if the curve is very short.
* We don't do anything about long, nearly flat curves.
*
* Note that if scaled_flat is very small, we don't do any of this;
* instead, we just check whether abs(dx) and abs(dy) are
* small enough to switch over to sampling rather than dividing.
*/
{ fixed dx3 = x3 - x0;
fixed adx3 = any_abs(dx3);
fixed dy3 = y3 - y0;
fixed ady3 = any_abs(dy3);
fixed flat = scaled_flat;
/* We have to be quite careful to ensure that */
/* none of the multiplications will overflow. */
#define short_max 0x7ff0L
#define reduce_3(ad3, maxv)\
while ( ad3 > maxv )\
adx3 >>= 1, ady3 >>= 1,\
dx3 = arith_rshift_1(dx3), dy3 = arith_rshift_1(dy3)
#define reduce_d(d)\
for ( shift = 0; (d < 0 ? d < -short_max : d > short_max); shift++ )\
d = arith_rshift_1(d)
if ( adx3 > ady3 )
{ fixed d, dx, dy, dist;
int shift;
if ( scaled_flat == 0 )
{ if ( adx3 < int2fixed(num_sample_max - 1) / 2 )
{ int n = fixed2int_var_rounded(adx3) * 2 + 1;
int code = flatten_sample(ppath, max(n, 3), x1, y1,
x2, y2, x3, y3);
if ( code <= 0 ) return code;
}
goto sub;
}
if ( adx3 < 16 ) flat >>= 1;
reduce_3(ady3, short_max);
d = (scaled_sqrt_tab[(ady3 << sqrt_index_shift) / adx3] * flat) >> sqrt_value_shift;
dx = x1 - x0, dy = y1 - y0;
reduce_d(dx);
if ( ((dist = ((dx * dy3 / dx3) << shift) - dy) < 0 ?
-dist : dist) > d )
goto sub; /* not flat enough */
dx = x2 - x0, dy = y2 - y0;
reduce_d(dx);
if ( ((dist = ((dx * dy3 / dx3) << shift) - dy) < 0 ?
-dist : dist) > d )
goto sub; /* not flat enough */
}
else if ( ady3 != 0 )
{ fixed d, dy, dx, dist;
int shift;
if ( scaled_flat == 0 )
{ if ( ady3 < int2fixed(num_sample_max - 1) / 2 )
{ int n = fixed2int_var_rounded(ady3) * 2 + 1;
int c