📄 smooth.c
字号:
static void
gray_split_cubic( FT_Vector* base )
{
TPos a, b, c, d;
base[6].x = base[3].x;
c = base[1].x;
d = base[2].x;
base[1].x = a = ( base[0].x + c ) / 2;
base[5].x = b = ( base[3].x + d ) / 2;
c = ( c + d ) / 2;
base[2].x = a = ( a + c ) / 2;
base[4].x = b = ( b + c ) / 2;
base[3].x = ( a + b ) / 2;
base[6].y = base[3].y;
c = base[1].y;
d = base[2].y;
base[1].y = a = ( base[0].y + c ) / 2;
base[5].y = b = ( base[3].y + d ) / 2;
c = ( c + d ) / 2;
base[2].y = a = ( a + c ) / 2;
base[4].y = b = ( b + c ) / 2;
base[3].y = ( a + b ) / 2;
}
static void
gray_render_cubic( RAS_ARG_ const FT_Vector* control1,
const FT_Vector* control2,
const FT_Vector* to )
{
TPos dx, dy, da, db;
int top, level;
int* levels;
FT_Vector* arc;
dx = DOWNSCALE( ras.x ) + to->x - ( control1->x << 1 );
if ( dx < 0 )
dx = -dx;
dy = DOWNSCALE( ras.y ) + to->y - ( control1->y << 1 );
if ( dy < 0 )
dy = -dy;
if ( dx < dy )
dx = dy;
da = dx;
dx = DOWNSCALE( ras.x ) + to->x - 3 * ( control1->x + control2->x );
if ( dx < 0 )
dx = -dx;
dy = DOWNSCALE( ras.y ) + to->y - 3 * ( control1->x + control2->y );
if ( dy < 0 )
dy = -dy;
if ( dx < dy )
dx = dy;
db = dx;
level = 1;
da = da / ras.cubic_level;
db = db / ras.conic_level;
while ( da > 0 || db > 0 )
{
da >>= 2;
db >>= 3;
level++;
}
if ( level <= 1 )
{
TPos to_x, to_y, mid_x, mid_y;
to_x = UPSCALE( to->x );
to_y = UPSCALE( to->y );
mid_x = ( ras.x + to_x +
3 * UPSCALE( control1->x + control2->x ) ) / 8;
mid_y = ( ras.y + to_y +
3 * UPSCALE( control1->y + control2->y ) ) / 8;
gray_render_line( RAS_VAR_ mid_x, mid_y );
gray_render_line( RAS_VAR_ to_x, to_y );
return;
}
arc = ras.bez_stack;
arc[0].x = UPSCALE( to->x );
arc[0].y = UPSCALE( to->y );
arc[1].x = UPSCALE( control2->x );
arc[1].y = UPSCALE( control2->y );
arc[2].x = UPSCALE( control1->x );
arc[2].y = UPSCALE( control1->y );
arc[3].x = ras.x;
arc[3].y = ras.y;
levels = ras.lev_stack;
top = 0;
levels[0] = level;
while ( top >= 0 )
{
level = levels[top];
if ( level > 1 )
{
/* check that the arc crosses the current band */
TPos min, max, y;
min = max = arc[0].y;
y = arc[1].y;
if ( y < min ) min = y;
if ( y > max ) max = y;
y = arc[2].y;
if ( y < min ) min = y;
if ( y > max ) max = y;
y = arc[3].y;
if ( y < min ) min = y;
if ( y > max ) max = y;
if ( TRUNC( min ) >= ras.max_ey || TRUNC( max ) < 0 )
goto Draw;
gray_split_cubic( arc );
arc += 3;
top ++;
levels[top] = levels[top - 1] = level - 1;
continue;
}
Draw:
{
TPos to_x, to_y, mid_x, mid_y;
to_x = arc[0].x;
to_y = arc[0].y;
mid_x = ( ras.x + to_x + 3 * ( arc[1].x + arc[2].x ) ) / 8;
mid_y = ( ras.y + to_y + 3 * ( arc[1].y + arc[2].y ) ) / 8;
gray_render_line( RAS_VAR_ mid_x, mid_y );
gray_render_line( RAS_VAR_ to_x, to_y );
top --;
arc -= 3;
}
}
return;
}
/* a macro comparing two cell pointers. Returns true if a <= b. */
#if 1
#define PACK( a ) ( ( (long)(a)->y << 16 ) + (a)->x )
#define LESS_THAN( a, b ) ( PACK( a ) < PACK( b ) )
#else /* 1 */
#define LESS_THAN( a, b ) ( (a)->y < (b)->y || \
( (a)->y == (b)->y && (a)->x < (b)->x ) )
#endif /* 1 */
#define SWAP_CELLS( a, b, temp ) do \
{ \
temp = *(a); \
*(a) = *(b); \
*(b) = temp; \
} while ( 0 )
#define DEBUG_SORT
#define QUICK_SORT
#ifdef SHELL_SORT
/* a simple shell sort algorithm that works directly on our */
/* cells table */
static void
gray_shell_sort ( PCell cells,
int count )
{
PCell i, j, limit = cells + count;
TCell temp;
int gap;
/* compute initial gap */
for ( gap = 0; ++gap < count; gap *= 3 )
;
while ( gap /= 3 )
{
for ( i = cells + gap; i < limit; i++ )
{
for ( j = i - gap; ; j -= gap )
{
PCell k = j + gap;
if ( LESS_THAN( j, k ) )
break;
SWAP_CELLS( j, k, temp );
if ( j < cells + gap )
break;
}
}
}
}
#endif /* SHELL_SORT */
#ifdef QUICK_SORT
/* This is a non-recursive quicksort that directly process our cells */
/* array. It should be faster than calling the stdlib qsort(), and we */
/* can even tailor our insertion threshold... */
#define QSORT_THRESHOLD 9 /* below this size, a sub-array will be sorted */
/* through a normal insertion sort */
static void
gray_quick_sort( PCell cells,
int count )
{
PCell stack[40]; /* should be enough ;-) */
PCell* top; /* top of stack */
PCell base, limit;
TCell temp;
limit = cells + count;
base = cells;
top = stack;
for (;;)
{
int len = (int)( limit - base );
PCell i, j, pivot;
if ( len > QSORT_THRESHOLD )
{
/* we use base + len/2 as the pivot */
pivot = base + len / 2;
SWAP_CELLS( base, pivot, temp );
i = base + 1;
j = limit - 1;
/* now ensure that *i <= *base <= *j */
if ( LESS_THAN( j, i ) )
SWAP_CELLS( i, j, temp );
if ( LESS_THAN( base, i ) )
SWAP_CELLS( base, i, temp );
if ( LESS_THAN( j, base ) )
SWAP_CELLS( base, j, temp );
for (;;)
{
do i++; while ( LESS_THAN( i, base ) );
do j--; while ( LESS_THAN( base, j ) );
if ( i > j )
break;
SWAP_CELLS( i, j, temp );
}
SWAP_CELLS( base, j, temp );
/* now, push the largest sub-array */
if ( j - base > limit - i )
{
top[0] = base;
top[1] = j;
base = i;
}
else
{
top[0] = i;
top[1] = limit;
limit = j;
}
top += 2;
}
else
{
/* the sub-array is small, perform insertion sort */
j = base;
i = j + 1;
for ( ; i < limit; j = i, i++ )
{
for ( ; LESS_THAN( j + 1, j ); j-- )
{
SWAP_CELLS( j + 1, j, temp );
if ( j == base )
break;
}
}
if ( top > stack )
{
top -= 2;
base = top[0];
limit = top[1];
}
else
break;
}
}
}
#endif /* QUICK_SORT */
#ifdef DEBUG_GRAYS
#ifdef DEBUG_SORT
static int
gray_check_sort( PCell cells,
int count )
{
PCell p, q;
for ( p = cells + count - 2; p >= cells; p-- )
{
q = p + 1;
if ( !LESS_THAN( p, q ) )
return 0;
}
return 1;
}
#endif /* DEBUG_SORT */
#endif /* DEBUG_GRAYS */
static int
gray_move_to( const FT_Vector* to,
FT_Raster raster )
{
TPos x, y;
/* record current cell, if any */
gray_record_cell( (PRaster)raster );
/* start to a new position */
x = UPSCALE( to->x );
y = UPSCALE( to->y );
gray_start_cell( (PRaster)raster, TRUNC( x ), TRUNC( y ) );
((PRaster)raster)->x = x;
((PRaster)raster)->y = y;
return 0;
}
static int
gray_line_to( const FT_Vector* to,
FT_Raster raster )
{
gray_render_line( (PRaster)raster,
UPSCALE( to->x ), UPSCALE( to->y ) );
return 0;
}
static int
gray_conic_to( const FT_Vector* control,
const FT_Vector* to,
FT_Raster raster )
{
gray_render_conic( (PRaster)raster, control, to );
return 0;
}
static int
gray_cubic_to( const FT_Vector* control1,
const FT_Vector* control2,
const FT_Vector* to,
FT_Raster raster )
{
gray_render_cubic( (PRaster)raster, control1, control2, to );
return 0;
}
static void
gray_render_span( int y,
int count,
const FT_Span* spans,
PRaster raster )
{
unsigned char* p;
FT_Bitmap* map = &raster->target;
/* first of all, compute the scanline offset */
p = (unsigned char*)map->buffer - y * map->pitch;
if ( map->pitch >= 0 )
p += ( map->rows - 1 ) * map->pitch;
for ( ; count > 0; count--, spans++ )
{
unsigned char coverage = spans->coverage;
#ifdef GRAYS_USE_GAMMA
coverage = raster->gamma[coverage];
#endif
if ( coverage )
#if 1
FT_MEM_SET( p + spans->x, (unsigned char)coverage, spans->len );
#else /* 1 */
{
q = p + spans->x;
limit = q + spans->len;
for ( ; q < limit; q++ )
q[0] = (unsigned char)coverage;
}
#endif /* 1 */
}
}
#ifdef DEBUG_GRAYS
#include <stdio.h>
static void
gray_dump_cells( RAS_ARG )
{
PCell cell, limit;
int y = -1;
cell = ras.cells;
limit = cell + ras.num_cells;
for ( ; cell < limit; cell++ )
{
if ( cell->y != y )
{
fprintf( stderr, "\n%2d: ", cell->y );
y = cell->y;
}
fprintf( stderr, "[%d %d %d]",
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -