📄 smooth.c
字号:
const FT_Outline_Funcs func_interface =
{
(FT_Outline_MoveTo_Func) gray_move_to,
(FT_Outline_LineTo_Func) gray_line_to,
(FT_Outline_ConicTo_Func)gray_conic_to,
(FT_Outline_CubicTo_Func)gray_cubic_to,
0,
0
};
volatile int error = 0;
if ( ft_setjmp( ras.jump_buffer ) == 0 )
{
error = FT_Outline_Decompose( &ras.outline, &func_interface, &ras );
gray_record_cell( RAS_VAR );
}
else
{
error = ErrRaster_MemoryOverflow;
}
return error;
}
static int
gray_convert_glyph( RAS_ARG )
{
TBand bands[40];
TBand* volatile band;
int volatile n, num_bands;
TPos volatile min, max, max_y;
FT_BBox* clip;
/* Set up state in the raster object */
gray_compute_cbox( RAS_VAR );
/* clip to target bitmap, exit if nothing to do */
clip = &ras.clip_box;
if ( ras.max_ex <= clip->xMin || ras.min_ex >= clip->xMax ||
ras.max_ey <= clip->yMin || ras.min_ey >= clip->yMax )
return 0;
if ( ras.min_ex < clip->xMin ) ras.min_ex = clip->xMin;
if ( ras.min_ey < clip->yMin ) ras.min_ey = clip->yMin;
if ( ras.max_ex > clip->xMax ) ras.max_ex = clip->xMax;
if ( ras.max_ey > clip->yMax ) ras.max_ey = clip->yMax;
/* simple heuristic used to speed-up the bezier decomposition -- see */
/* the code in gray_render_conic() and gray_render_cubic() for more */
/* details */
ras.conic_level = 32;
ras.cubic_level = 16;
{
int level = 0;
if ( ras.max_ex > 24 || ras.max_ey > 24 )
level++;
if ( ras.max_ex > 120 || ras.max_ey > 120 )
level++;
ras.conic_level <<= level;
ras.cubic_level <<= level;
}
/* setup vertical bands */
num_bands = (int)( ( ras.max_ey - ras.min_ey ) / ras.band_size );
if ( num_bands == 0 ) num_bands = 1;
if ( num_bands >= 39 ) num_bands = 39;
ras.band_shoot = 0;
min = ras.min_ey;
max_y = ras.max_ey;
for ( n = 0; n < num_bands; n++, min = max )
{
max = min + ras.band_size;
if ( n == num_bands - 1 || max > max_y )
max = max_y;
bands[0].min = min;
bands[0].max = max;
band = bands;
while ( band >= bands )
{
TPos bottom, top, middle;
int error;
ras.num_cells = 0;
ras.invalid = 1;
ras.min_ey = band->min;
ras.max_ey = band->max;
#if 1
error = gray_convert_glyph_inner( RAS_VAR );
#else
error = FT_Outline_Decompose( outline, &func_interface, &ras ) ||
gray_record_cell( RAS_VAR );
#endif
if ( !error )
{
#ifdef SHELL_SORT
gray_shell_sort( ras.cells, ras.num_cells );
#else
gray_quick_sort( ras.cells, ras.num_cells );
#endif
#ifdef DEBUG_GRAYS
gray_check_sort( ras.cells, ras.num_cells );
gray_dump_cells( RAS_VAR );
#endif
gray_sweep( RAS_VAR_ &ras.target );
band--;
continue;
}
else if ( error != ErrRaster_MemoryOverflow )
return 1;
/* render pool overflow, we will reduce the render band by half */
bottom = band->min;
top = band->max;
middle = bottom + ( ( top - bottom ) >> 1 );
/* waoow! This is too complex for a single scanline, something */
/* must be really rotten here! */
if ( middle == bottom )
{
#ifdef DEBUG_GRAYS
fprintf( stderr, "Rotten glyph!\n" );
#endif
return 1;
}
if ( bottom-top >= ras.band_size )
ras.band_shoot++;
band[1].min = bottom;
band[1].max = middle;
band[0].min = middle;
band[0].max = top;
band++;
}
}
if ( ras.band_shoot > 8 && ras.band_size > 16 )
ras.band_size = ras.band_size / 2;
return 0;
}
static int
gray_raster_render( PRaster raster,
const FT_Raster_Params* params )
{
const FT_Outline* outline = (const FT_Outline*)params->source;
const FT_Bitmap* target_map = params->target;
if ( !raster || !raster->cells || !raster->max_cells )
return -1;
/* return immediately if the outline is empty */
if ( outline->n_points == 0 || outline->n_contours <= 0 )
return 0;
if ( !outline || !outline->contours || !outline->points )
return ErrRaster_Invalid_Outline;
if ( outline->n_points !=
outline->contours[outline->n_contours - 1] + 1 )
return ErrRaster_Invalid_Outline;
/* if direct mode is not set, we must have a target bitmap */
if ( ( params->flags & FT_RASTER_FLAG_DIRECT ) == 0 &&
( !target_map || !target_map->buffer ) )
return -1;
/* this version does not support monochrome rendering */
if ( !( params->flags & FT_RASTER_FLAG_AA ) )
return ErrRaster_Invalid_Mode;
/* compute clipping box */
if ( ( params->flags & FT_RASTER_FLAG_DIRECT ) == 0 )
{
/* compute clip box from target pixmap */
ras.clip_box.xMin = 0;
ras.clip_box.yMin = 0;
ras.clip_box.xMax = target_map->width;
ras.clip_box.yMax = target_map->rows;
}
else if ( params->flags & FT_RASTER_FLAG_CLIP )
{
ras.clip_box = params->clip_box;
}
else
{
ras.clip_box.xMin = -32768L;
ras.clip_box.yMin = -32768L;
ras.clip_box.xMax = 32767L;
ras.clip_box.yMax = 32767L;
}
ras.outline = *outline;
ras.num_cells = 0;
ras.invalid = 1;
if ( target_map )
ras.target = *target_map;
ras.render_span = (FT_Raster_Span_Func)gray_render_span;
ras.render_span_data = &ras;
if ( params->flags & FT_RASTER_FLAG_DIRECT )
{
ras.render_span = (FT_Raster_Span_Func)params->gray_spans;
ras.render_span_data = params->user;
}
return gray_convert_glyph( (PRaster)raster );
}
/**** RASTER OBJECT CREATION: In standalone mode, we simply use *****/
/**** a static object. *****/
#ifdef GRAYS_USE_GAMMA
/* initialize the "gamma" table. Yes, this is really a crummy function */
/* but the results look pretty good for something that simple. */
/* */
#define M_MAX 255
#define M_X 128
#define M_Y 192
static void
grays_init_gamma( PRaster raster )
{
unsigned int x, a;
for ( x = 0; x < 256; x++ )
{
if ( x <= M_X )
a = ( x * M_Y + M_X / 2) / M_X;
else
a = M_Y + ( ( x - M_X ) * ( M_MAX - M_Y ) +
( M_MAX - M_X ) / 2 ) / ( M_MAX - M_X );
raster->gamma[x] = (unsigned char)a;
}
}
#endif /* GRAYS_USE_GAMMA */
#ifdef _STANDALONE_
static int
gray_raster_new( void* memory,
FT_Raster* araster )
{
static TRaster the_raster;
FT_UNUSED( memory );
*araster = (FT_Raster)&the_raster;
FT_MEM_ZERO( &the_raster, sizeof ( the_raster ) );
#ifdef GRAYS_USE_GAMMA
grays_init_gamma( (PRaster)*araster );
#endif
return 0;
}
static void
gray_raster_done( FT_Raster raster )
{
/* nothing */
FT_UNUSED( raster );
}
#else /* _STANDALONE_ */
static int
gray_raster_new( FT_Memory memory,
FT_Raster* araster )
{
FT_Error error;
PRaster raster;
*araster = 0;
if ( !FT_ALLOC( raster, sizeof ( TRaster ) ) )
{
raster->memory = memory;
*araster = (FT_Raster)raster;
#ifdef GRAYS_USE_GAMMA
grays_init_gamma( raster );
#endif
}
return error;
}
static void
gray_raster_done( FT_Raster raster )
{
FT_Memory memory = (FT_Memory)((PRaster)raster)->memory;
FT_FREE( raster );
}
#endif /* _STANDALONE_ */
static void
gray_raster_reset( FT_Raster raster,
char* pool_base,
long pool_size )
{
PRaster rast = (PRaster)raster;
if ( raster && pool_base && pool_size >= 4096 )
gray_init_cells( rast, (char*)pool_base, pool_size );
rast->band_size = (int)( ( pool_size / sizeof ( TCell ) ) / 8 );
}
const FT_Raster_Funcs ft_grays_raster =
{
FT_GLYPH_FORMAT_OUTLINE,
(FT_Raster_New_Func) gray_raster_new,
(FT_Raster_Reset_Func) gray_raster_reset,
(FT_Raster_Set_Mode_Func)0,
(FT_Raster_Render_Func) gray_raster_render,
(FT_Raster_Done_Func) gray_raster_done
};
/* END */
/***************************************************************************/
/* */
/* ftsmooth.c */
/* */
/* Anti-aliasing renderer interface (body). */
/* */
/* Copyright 2000-2001, 2002, 2003, 2004, 2005 by */
/* David Turner, Robert Wilhelm, and Werner Lemberg. */
/* */
/* This file is part of the FreeType project, and may only be used, */
/* modified, and distributed under the terms of the FreeType project */
/* license, LICENSE.TXT. By continuing to use, modify, or distribute */
/* this file you indicate that you have read the license and */
/* understand and accept it fully. */
/* */
/***************************************************************************/
#include "ft2build.h"
#include FT_INTERNAL_OBJECTS_H
#include FT_OUTLINE_H
#include "ftsmooth.h"
#include "ftgrays.h"
#include "ftsmerrs.h"
/* initialize renderer -- init its raster */
static FT_Error
ft_smooth_init( FT_Renderer render )
{
FT_Library library = FT_MODULE_LIBRARY( render );
render->clazz->raster_class->raster_reset( render->raster,
library->raster_pool,
library->raster_pool_size );
return 0;
}
/* sets render-specific mode */
static FT_Error
ft_smooth_set_mode( FT_Renderer render,
FT_ULong mode_tag,
FT_Pointer data )
{
/* we simply pass it to the raster */
return render->clazz->raster_class->raster_set_mode( render->raster,
mode_tag,
data );
}
/* transform a given glyph image */
static FT_Error
ft_smooth_transform( FT_Renderer render,
FT_GlyphSlot slot,
const FT_Matrix* matrix,
const FT_Vector* delta )
{
FT_Error error = Smooth_Err_Ok;
if ( slot->format != render->glyph_format )
{
error = Smooth_Err_Invalid_Argument;
goto Exit;
}
if ( matrix )
FT_Outline_Transform( &slot->outline, matrix );
if ( delta )
FT_Outline_Translate( &slot->outline, delta->x, delta->y );
Exit:
return error;
}
/* return the glyph's control box */
static void
ft_smooth_get_cbox( FT_Renderer render,
FT_GlyphSlot slot,
FT_BBox* cbox )
{
FT_MEM_ZERO( cbox, sizeof ( *cbox ) );
if ( slot->format == render->glyph_format )
FT_Outline_Get_CBox( &slot->outline, cbox );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -