1 /***************************************************************************/
5 /* A new `perfect' anti-aliasing renderer (body). */
7 /* Copyright 2000-2003, 2005-2012 by */
8 /* David Turner, Robert Wilhelm, and Werner Lemberg. */
10 /* This file is part of the FreeType project, and may only be used, */
11 /* modified, and distributed under the terms of the FreeType project */
12 /* license, LICENSE.TXT. By continuing to use, modify, or distribute */
13 /* this file you indicate that you have read the license and */
14 /* understand and accept it fully. */
16 /***************************************************************************/
18 /*************************************************************************/
20 /* This file can be compiled without the rest of the FreeType engine, by */
21 /* defining the _STANDALONE_ macro when compiling it. You also need to */
22 /* put the files `ftgrays.h' and `ftimage.h' into the current */
23 /* compilation directory. Typically, you could do something like */
25 /* - copy `src/smooth/ftgrays.c' (this file) to your current directory */
27 /* - copy `include/freetype/ftimage.h' and `src/smooth/ftgrays.h' to the */
30 /* - compile `ftgrays' with the _STANDALONE_ macro defined, as in */
32 /* cc -c -D_STANDALONE_ ftgrays.c */
34 /* The renderer can be initialized with a call to */
35 /* `ft_gray_raster.raster_new'; an anti-aliased bitmap can be generated */
36 /* with a call to `ft_gray_raster.raster_render'. */
38 /* See the comments and documentation in the file `ftimage.h' for more */
39 /* details on how the raster works. */
41 /*************************************************************************/
43 /*************************************************************************/
45 /* This is a new anti-aliasing scan-converter for FreeType 2. The */
46 /* algorithm used here is _very_ different from the one in the standard */
47 /* `ftraster' module. Actually, `ftgrays' computes the _exact_ */
48 /* coverage of the outline on each pixel cell. */
50 /* It is based on ideas that I initially found in Raph Levien's */
51 /* excellent LibArt graphics library (see http://www.levien.com/libart */
52 /* for more information, though the web pages do not tell anything */
53 /* about the renderer; you'll have to dive into the source code to */
54 /* understand how it works). */
56 /* Note, however, that this is a _very_ different implementation */
57 /* compared to Raph's. Coverage information is stored in a very */
58 /* different way, and I don't use sorted vector paths. Also, it doesn't */
59 /* use floating point values. */
61 /* This renderer has the following advantages: */
63 /* - It doesn't need an intermediate bitmap. Instead, one can supply a */
64 /* callback function that will be called by the renderer to draw gray */
65 /* spans on any target surface. You can thus do direct composition on */
66 /* any kind of bitmap, provided that you give the renderer the right */
69 /* - A perfect anti-aliaser, i.e., it computes the _exact_ coverage on */
70 /* each pixel cell. */
72 /* - It performs a single pass on the outline (the `standard' FT2 */
73 /* renderer makes two passes). */
75 /* - It can easily be modified to render to _any_ number of gray levels */
78 /* - For small (< 20) pixel sizes, it is faster than the standard */
81 /*************************************************************************/
84 /*************************************************************************/
86 /* The macro FT_COMPONENT is used in trace mode. It is an implicit */
87 /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */
88 /* messages during execution. */
91 #define FT_COMPONENT trace_smooth
97 /* define this to dump debugging information */
98 /* #define FT_DEBUG_LEVEL_TRACE */
101 #ifdef FT_DEBUG_LEVEL_TRACE
110 #define FT_UINT_MAX UINT_MAX
111 #define FT_INT_MAX INT_MAX
113 #define ft_memset memset
115 #define ft_setjmp setjmp
116 #define ft_longjmp longjmp
117 #define ft_jmp_buf jmp_buf
119 typedef ptrdiff_t FT_PtrDist;
122 #define ErrRaster_Invalid_Mode -2
123 #define ErrRaster_Invalid_Outline -1
124 #define ErrRaster_Invalid_Argument -3
125 #define ErrRaster_Memory_Overflow -4
127 #define FT_BEGIN_HEADER
128 #define FT_END_HEADER
134 /* This macro is used to indicate that a function parameter is unused. */
135 /* Its purpose is simply to reduce compiler warnings. Note also that */
136 /* simply defining it as `(void)x' doesn't avoid warnings with certain */
137 /* ANSI compilers (e.g. LCC). */
138 #define FT_UNUSED( x ) (x) = (x)
141 /* we only use level 5 & 7 tracing messages; cf. ftdebug.h */
143 #ifdef FT_DEBUG_LEVEL_TRACE
146 FT_Message( const char* fmt,
153 vfprintf( stderr, fmt, ap );
157 /* we don't handle tracing levels in stand-alone mode; */
159 #define FT_TRACE5( varformat ) FT_Message varformat
162 #define FT_TRACE7( varformat ) FT_Message varformat
165 #define FT_ERROR( varformat ) FT_Message varformat
168 #else /* !FT_DEBUG_LEVEL_TRACE */
170 #define FT_TRACE5( x ) do { } while ( 0 ) /* nothing */
171 #define FT_TRACE7( x ) do { } while ( 0 ) /* nothing */
172 #define FT_ERROR( x ) do { } while ( 0 ) /* nothing */
174 #endif /* !FT_DEBUG_LEVEL_TRACE */
177 #define FT_DEFINE_OUTLINE_FUNCS( class_, \
178 move_to_, line_to_, \
179 conic_to_, cubic_to_, \
181 static const FT_Outline_Funcs class_ = \
191 #define FT_DEFINE_RASTER_FUNCS( class_, glyph_format_, \
192 raster_new_, raster_reset_, \
193 raster_set_mode_, raster_render_, \
195 const FT_Raster_Funcs class_ = \
205 #else /* !_STANDALONE_ */
208 #include <ft2build.h>
210 #include FT_INTERNAL_OBJECTS_H
211 #include FT_INTERNAL_DEBUG_H
212 #include FT_OUTLINE_H
214 #include "ftsmerrs.h"
218 #define ErrRaster_Invalid_Mode Smooth_Err_Cannot_Render_Glyph
219 #define ErrRaster_Invalid_Outline Smooth_Err_Invalid_Outline
220 #define ErrRaster_Memory_Overflow Smooth_Err_Out_Of_Memory
221 #define ErrRaster_Invalid_Argument Smooth_Err_Invalid_Argument
223 #endif /* !_STANDALONE_ */
226 #define FT_MEM_SET( d, s, c ) ft_memset( d, s, c )
230 #define FT_MEM_ZERO( dest, count ) FT_MEM_SET( dest, 0, count )
233 /* as usual, for the speed hungry :-) */
240 #ifndef FT_STATIC_RASTER
242 #define RAS_ARG gray_PWorker worker
243 #define RAS_ARG_ gray_PWorker worker,
245 #define RAS_VAR worker
246 #define RAS_VAR_ worker,
248 #else /* FT_STATIC_RASTER */
250 #define RAS_ARG /* empty */
251 #define RAS_ARG_ /* empty */
252 #define RAS_VAR /* empty */
253 #define RAS_VAR_ /* empty */
255 #endif /* FT_STATIC_RASTER */
258 /* must be at least 6 bits! */
266 #define ONE_PIXEL ( 1L << PIXEL_BITS )
267 #define PIXEL_MASK ( -1L << PIXEL_BITS )
268 #define TRUNC( x ) ( (TCoord)( (x) >> PIXEL_BITS ) )
269 #define SUBPIXELS( x ) ( (TPos)(x) << PIXEL_BITS )
270 #define FLOOR( x ) ( (x) & -ONE_PIXEL )
271 #define CEILING( x ) ( ( (x) + ONE_PIXEL - 1 ) & -ONE_PIXEL )
272 #define ROUND( x ) ( ( (x) + ONE_PIXEL / 2 ) & -ONE_PIXEL )
275 #define UPSCALE( x ) ( (x) << ( PIXEL_BITS - 6 ) )
276 #define DOWNSCALE( x ) ( (x) >> ( PIXEL_BITS - 6 ) )
278 #define UPSCALE( x ) ( (x) >> ( 6 - PIXEL_BITS ) )
279 #define DOWNSCALE( x ) ( (x) << ( 6 - PIXEL_BITS ) )
283 /*************************************************************************/
285 /* TYPE DEFINITIONS */
288 /* don't change the following types to FT_Int or FT_Pos, since we might */
289 /* need to define them to "float" or "double" when experimenting with */
292 typedef long TCoord; /* integer scanline/pixel coordinate */
293 typedef long TPos; /* sub-pixel coordinate */
295 /* determine the type used to store cell areas. This normally takes at */
296 /* least PIXEL_BITS*2 + 1 bits. On 16-bit systems, we need to use */
297 /* `long' instead of `int', otherwise bad things happen */
303 #else /* PIXEL_BITS >= 8 */
305 /* approximately determine the size of integers using an ANSI-C header */
306 #if FT_UINT_MAX == 0xFFFFU
312 #endif /* PIXEL_BITS >= 8 */
315 /* maximal number of gray spans in a call to the span callback */
316 #define FT_MAX_GRAY_SPANS 32
319 typedef struct TCell_* PCell;
321 typedef struct TCell_
323 TPos x; /* same with gray_TWorker.ex */
324 TCoord cover; /* same with gray_TWorker.cover */
331 typedef struct gray_TWorker_
336 TPos count_ex, count_ey;
343 FT_PtrDist max_cells;
344 FT_PtrDist num_cells;
351 FT_Vector bez_stack[32 * 3 + 1];
358 FT_Span gray_spans[FT_MAX_GRAY_SPANS];
361 FT_Raster_Span_Func render_span;
362 void* render_span_data;
368 ft_jmp_buf jump_buffer;
376 } gray_TWorker, *gray_PWorker;
379 #ifndef FT_STATIC_RASTER
380 #define ras (*worker)
382 static gray_TWorker ras;
386 typedef struct gray_TRaster_
394 } gray_TRaster, *gray_PRaster;
398 /*************************************************************************/
400 /* Initialize the cells table. */
403 gray_init_cells( RAS_ARG_ void* buffer,
407 ras.buffer_size = byte_size;
409 ras.ycells = (PCell*) buffer;
419 /*************************************************************************/
421 /* Compute the outline bounding box. */
424 gray_compute_cbox( RAS_ARG )
426 FT_Outline* outline = &ras.outline;
427 FT_Vector* vec = outline->points;
428 FT_Vector* limit = vec + outline->n_points;
431 if ( outline->n_points <= 0 )
433 ras.min_ex = ras.max_ex = 0;
434 ras.min_ey = ras.max_ey = 0;
438 ras.min_ex = ras.max_ex = vec->x;
439 ras.min_ey = ras.max_ey = vec->y;
443 for ( ; vec < limit; vec++ )
449 if ( x < ras.min_ex ) ras.min_ex = x;
450 if ( x > ras.max_ex ) ras.max_ex = x;
451 if ( y < ras.min_ey ) ras.min_ey = y;
452 if ( y > ras.max_ey ) ras.max_ey = y;
455 /* truncate the bounding box to integer pixels */
456 ras.min_ex = ras.min_ex >> 6;
457 ras.min_ey = ras.min_ey >> 6;
458 ras.max_ex = ( ras.max_ex + 63 ) >> 6;
459 ras.max_ey = ( ras.max_ey + 63 ) >> 6;
463 /*************************************************************************/
465 /* Record the current cell in the table. */
468 gray_find_cell( RAS_ARG )
474 if ( x > ras.count_ex )
477 pcell = &ras.ycells[ras.ey];
481 if ( cell == NULL || cell->x > x )
490 if ( ras.num_cells >= ras.max_cells )
491 ft_longjmp( ras.jump_buffer, 1 );
493 cell = ras.cells + ras.num_cells++;
507 gray_record_cell( RAS_ARG )
509 if ( !ras.invalid && ( ras.area | ras.cover ) )
511 PCell cell = gray_find_cell( RAS_VAR );
514 cell->area += ras.area;
515 cell->cover += ras.cover;
520 /*************************************************************************/
522 /* Set the current cell to a new position. */
525 gray_set_cell( RAS_ARG_ TCoord ex,
528 /* Move the cell pointer to a new position. We set the `invalid' */
529 /* flag to indicate that the cell isn't part of those we're interested */
530 /* in during the render phase. This means that: */
532 /* . the new vertical position must be within min_ey..max_ey-1. */
533 /* . the new horizontal position must be strictly less than max_ex */
535 /* Note that if a cell is to the left of the clipping region, it is */
536 /* actually set to the (min_ex-1) horizontal position. */
538 /* All cells that are on the left of the clipping region go to the */
539 /* min_ex - 1 horizontal position. */
542 if ( ex > ras.max_ex )
549 /* are we moving to a different cell ? */
550 if ( ex != ras.ex || ey != ras.ey )
552 /* record the current one if it is valid */
554 gray_record_cell( RAS_VAR );
562 ras.invalid = ( (unsigned)ey >= (unsigned)ras.count_ey ||
563 ex >= ras.count_ex );
567 /*************************************************************************/
569 /* Start a new contour at a given cell. */
572 gray_start_cell( RAS_ARG_ TCoord ex,
575 if ( ex > ras.max_ex )
576 ex = (TCoord)( ras.max_ex );
578 if ( ex < ras.min_ex )
579 ex = (TCoord)( ras.min_ex - 1 );
583 ras.ex = ex - ras.min_ex;
584 ras.ey = ey - ras.min_ey;
585 ras.last_ey = SUBPIXELS( ey );
588 gray_set_cell( RAS_VAR_ ex, ey );
592 /*************************************************************************/
594 /* Render a scanline as one or more cells. */
597 gray_render_scanline( RAS_ARG_ TCoord ey,
603 TCoord ex1, ex2, fx1, fx2, delta, mod, lift, rem;
612 fx1 = (TCoord)( x1 - SUBPIXELS( ex1 ) );
613 fx2 = (TCoord)( x2 - SUBPIXELS( ex2 ) );
615 /* trivial case. Happens often */
618 gray_set_cell( RAS_VAR_ ex2, ey );
622 /* everything is located in a single cell. That is easy! */
627 ras.area += (TArea)(( fx1 + fx2 ) * delta);
632 /* ok, we'll have to render a run of adjacent cells on the same */
635 p = ( ONE_PIXEL - fx1 ) * ( y2 - y1 );
641 p = fx1 * ( y2 - y1 );
647 delta = (TCoord)( p / dx );
648 mod = (TCoord)( p % dx );
655 ras.area += (TArea)(( fx1 + first ) * delta);
659 gray_set_cell( RAS_VAR_ ex1, ey );
664 p = ONE_PIXEL * ( y2 - y1 + delta );
665 lift = (TCoord)( p / dx );
666 rem = (TCoord)( p % dx );
685 ras.area += (TArea)(ONE_PIXEL * delta);
689 gray_set_cell( RAS_VAR_ ex1, ey );
694 ras.area += (TArea)(( fx2 + ONE_PIXEL - first ) * delta);
699 /*************************************************************************/
701 /* Render a given line as a series of scanlines. */
704 gray_render_line( RAS_ARG_ TPos to_x,
707 TCoord ey1, ey2, fy1, fy2, mod;
710 int delta, rem, lift, incr;
713 ey1 = TRUNC( ras.last_ey );
714 ey2 = TRUNC( to_y ); /* if (ey2 >= ras.max_ey) ey2 = ras.max_ey-1; */
715 fy1 = (TCoord)( ras.y - ras.last_ey );
716 fy2 = (TCoord)( to_y - SUBPIXELS( ey2 ) );
721 /* XXX: we should do something about the trivial case where dx == 0, */
722 /* as it happens very often! */
724 /* perform vertical clipping */
736 if ( min >= ras.max_ey || max < ras.min_ey )
740 /* everything is on a single scanline */
743 gray_render_scanline( RAS_VAR_ ey1, ras.x, fy1, to_x, fy2 );
747 /* vertical line - avoid calling gray_render_scanline */
752 TCoord ex = TRUNC( ras.x );
753 TCoord two_fx = (TCoord)( ( ras.x - SUBPIXELS( ex ) ) << 1 );
764 delta = (int)( first - fy1 );
765 ras.area += (TArea)two_fx * delta;
769 gray_set_cell( RAS_VAR_ ex, ey1 );
771 delta = (int)( first + first - ONE_PIXEL );
772 area = (TArea)two_fx * delta;
779 gray_set_cell( RAS_VAR_ ex, ey1 );
782 delta = (int)( fy2 - ONE_PIXEL + first );
783 ras.area += (TArea)two_fx * delta;
789 /* ok, we have to render several scanlines */
790 p = ( ONE_PIXEL - fy1 ) * dx;
802 delta = (int)( p / dy );
803 mod = (int)( p % dy );
811 gray_render_scanline( RAS_VAR_ ey1, ras.x, fy1, x, (TCoord)first );
814 gray_set_cell( RAS_VAR_ TRUNC( x ), ey1 );
819 lift = (int)( p / dy );
820 rem = (int)( p % dy );
839 gray_render_scanline( RAS_VAR_ ey1, x,
840 (TCoord)( ONE_PIXEL - first ), x2,
845 gray_set_cell( RAS_VAR_ TRUNC( x ), ey1 );
849 gray_render_scanline( RAS_VAR_ ey1, x,
850 (TCoord)( ONE_PIXEL - first ), to_x,
856 ras.last_ey = SUBPIXELS( ey2 );
861 gray_split_conic( FT_Vector* base )
866 base[4].x = base[2].x;
868 a = base[3].x = ( base[2].x + b ) / 2;
869 b = base[1].x = ( base[0].x + b ) / 2;
870 base[2].x = ( a + b ) / 2;
872 base[4].y = base[2].y;
874 a = base[3].y = ( base[2].y + b ) / 2;
875 b = base[1].y = ( base[0].y + b ) / 2;
876 base[2].y = ( a + b ) / 2;
881 gray_render_conic( RAS_ARG_ const FT_Vector* control,
882 const FT_Vector* to )
891 levels = ras.lev_stack;
894 arc[0].x = UPSCALE( to->x );
895 arc[0].y = UPSCALE( to->y );
896 arc[1].x = UPSCALE( control->x );
897 arc[1].y = UPSCALE( control->y );
902 dx = FT_ABS( arc[2].x + arc[0].x - 2 * arc[1].x );
903 dy = FT_ABS( arc[2].y + arc[0].y - 2 * arc[1].y );
907 if ( dx < ONE_PIXEL / 4 )
910 /* short-cut the arc that crosses the current band */
911 min = max = arc[0].y;
914 if ( y < min ) min = y;
915 if ( y > max ) max = y;
918 if ( y < min ) min = y;
919 if ( y > max ) max = y;
921 if ( TRUNC( min ) >= ras.max_ey || TRUNC( max ) < ras.min_ey )
929 } while ( dx > ONE_PIXEL / 4 );
938 gray_split_conic( arc );
941 levels[top] = levels[top - 1] = level - 1;
946 gray_render_line( RAS_VAR_ arc[0].x, arc[0].y );
950 } while ( top >= 0 );
955 gray_split_cubic( FT_Vector* base )
960 base[6].x = base[3].x;
963 base[1].x = a = ( base[0].x + c ) / 2;
964 base[5].x = b = ( base[3].x + d ) / 2;
966 base[2].x = a = ( a + c ) / 2;
967 base[4].x = b = ( b + c ) / 2;
968 base[3].x = ( a + b ) / 2;
970 base[6].y = base[3].y;
973 base[1].y = a = ( base[0].y + c ) / 2;
974 base[5].y = b = ( base[3].y + d ) / 2;
976 base[2].y = a = ( a + c ) / 2;
977 base[4].y = b = ( b + c ) / 2;
978 base[3].y = ( a + b ) / 2;
983 gray_render_cubic( RAS_ARG_ const FT_Vector* control1,
984 const FT_Vector* control2,
985 const FT_Vector* to )
992 arc[0].x = UPSCALE( to->x );
993 arc[0].y = UPSCALE( to->y );
994 arc[1].x = UPSCALE( control2->x );
995 arc[1].y = UPSCALE( control2->y );
996 arc[2].x = UPSCALE( control1->x );
997 arc[2].y = UPSCALE( control1->y );
1001 /* Short-cut the arc that crosses the current band. */
1002 min = max = arc[0].y;
1022 if ( TRUNC( min ) >= ras.max_ey || TRUNC( max ) < ras.min_ey )
1027 /* Decide whether to split or draw. See `Rapid Termination */
1028 /* Evaluation for Recursive Subdivision of Bezier Curves' by Thomas */
1030 /* http://www.cis.southalabama.edu/~hain/general/Publications/Bezier/Camera-ready%20CISST02%202.pdf */
1033 TPos dx, dy, dx_, dy_;
1034 TPos dx1, dy1, dx2, dy2;
1038 /* dx and dy are x and y components of the P0-P3 chord vector. */
1039 dx = arc[3].x - arc[0].x;
1040 dy = arc[3].y - arc[0].y;
1042 /* L is an (under)estimate of the Euclidean distance P0-P3. */
1044 /* If dx >= dy, then r = sqrt(dx^2 + dy^2) can be overestimated */
1045 /* with least maximum error by */
1047 /* r_upperbound = dx + (sqrt(2) - 1) * dy , */
1049 /* where sqrt(2) - 1 can be (over)estimated by 107/256, giving an */
1050 /* error of no more than 8.4%. */
1052 /* Similarly, some elementary calculus shows that r can be */
1053 /* underestimated with least maximum error by */
1055 /* r_lowerbound = sqrt(2 + sqrt(2)) / 2 * dx */
1056 /* + sqrt(2 - sqrt(2)) / 2 * dy . */
1058 /* 236/256 and 97/256 are (under)estimates of the two algebraic */
1059 /* numbers, giving an error of no more than 8.1%. */
1064 /* This is the same as */
1066 /* L = ( 236 * FT_MAX( dx_, dy_ ) */
1067 /* + 97 * FT_MIN( dx_, dy_ ) ) >> 8; */
1068 L = ( dx_ > dy_ ? 236 * dx_ + 97 * dy_
1069 : 97 * dx_ + 236 * dy_ ) >> 8;
1071 /* Avoid possible arithmetic overflow below by splitting. */
1075 /* Max deviation may be as much as (s/L) * 3/4 (if Hain's v = 1). */
1076 s_limit = L * (TPos)( ONE_PIXEL / 6 );
1078 /* s is L * the perpendicular distance from P1 to the line P0-P3. */
1079 dx1 = arc[1].x - arc[0].x;
1080 dy1 = arc[1].y - arc[0].y;
1081 s = FT_ABS( dy * dx1 - dx * dy1 );
1086 /* s is L * the perpendicular distance from P2 to the line P0-P3. */
1087 dx2 = arc[2].x - arc[0].x;
1088 dy2 = arc[2].y - arc[0].y;
1089 s = FT_ABS( dy * dx2 - dx * dy2 );
1094 /* If P1 or P2 is outside P0-P3, split the curve. */
1095 if ( dy * dy1 + dx * dx1 < 0 ||
1096 dy * dy2 + dx * dx2 < 0 ||
1097 dy * (arc[3].y - arc[1].y) + dx * (arc[3].x - arc[1].x) < 0 ||
1098 dy * (arc[3].y - arc[2].y) + dx * (arc[3].x - arc[2].x) < 0 )
1101 /* No reason to split. */
1106 gray_split_cubic( arc );
1111 gray_render_line( RAS_VAR_ arc[0].x, arc[0].y );
1113 if ( arc == ras.bez_stack )
1122 gray_move_to( const FT_Vector* to,
1123 gray_PWorker worker )
1128 /* record current cell, if any */
1129 gray_record_cell( RAS_VAR );
1131 /* start to a new position */
1132 x = UPSCALE( to->x );
1133 y = UPSCALE( to->y );
1135 gray_start_cell( RAS_VAR_ TRUNC( x ), TRUNC( y ) );
1144 gray_line_to( const FT_Vector* to,
1145 gray_PWorker worker )
1147 gray_render_line( RAS_VAR_ UPSCALE( to->x ), UPSCALE( to->y ) );
1153 gray_conic_to( const FT_Vector* control,
1154 const FT_Vector* to,
1155 gray_PWorker worker )
1157 gray_render_conic( RAS_VAR_ control, to );
1163 gray_cubic_to( const FT_Vector* control1,
1164 const FT_Vector* control2,
1165 const FT_Vector* to,
1166 gray_PWorker worker )
1168 gray_render_cubic( RAS_VAR_ control1, control2, to );
1174 gray_render_span( int y,
1176 const FT_Span* spans,
1177 gray_PWorker worker )
1180 FT_Bitmap* map = &worker->target;
1183 /* first of all, compute the scanline offset */
1184 p = (unsigned char*)map->buffer - y * map->pitch;
1185 if ( map->pitch >= 0 )
1186 p += (unsigned)( ( map->rows - 1 ) * map->pitch );
1188 for ( ; count > 0; count--, spans++ )
1190 unsigned char coverage = spans->coverage;
1195 /* For small-spans it is faster to do it by ourselves than
1196 * calling `memset'. This is mainly due to the cost of the
1199 if ( spans->len >= 8 )
1200 FT_MEM_SET( p + spans->x, (unsigned char)coverage, spans->len );
1203 unsigned char* q = p + spans->x;
1206 switch ( spans->len )
1208 case 7: *q++ = (unsigned char)coverage;
1209 case 6: *q++ = (unsigned char)coverage;
1210 case 5: *q++ = (unsigned char)coverage;
1211 case 4: *q++ = (unsigned char)coverage;
1212 case 3: *q++ = (unsigned char)coverage;
1213 case 2: *q++ = (unsigned char)coverage;
1214 case 1: *q = (unsigned char)coverage;
1225 gray_hline( RAS_ARG_ TCoord x,
1235 /* compute the coverage line's coverage, depending on the */
1236 /* outline fill rule */
1238 /* the coverage percentage is area/(PIXEL_BITS*PIXEL_BITS*2) */
1240 coverage = (int)( area >> ( PIXEL_BITS * 2 + 1 - 8 ) );
1241 /* use range 0..256 */
1243 coverage = -coverage;
1245 if ( ras.outline.flags & FT_OUTLINE_EVEN_ODD_FILL )
1249 if ( coverage > 256 )
1250 coverage = 512 - coverage;
1251 else if ( coverage == 256 )
1256 /* normal non-zero winding rule */
1257 if ( coverage >= 256 )
1261 y += (TCoord)ras.min_ey;
1262 x += (TCoord)ras.min_ex;
1264 /* FT_Span.x is a 16-bit short, so limit our coordinates appropriately */
1268 /* FT_Span.y is an integer, so limit our coordinates appropriately */
1269 if ( y >= FT_INT_MAX )
1274 /* see whether we can add this span to the current list */
1275 count = ras.num_gray_spans;
1276 span = ras.gray_spans + count - 1;
1279 (int)span->x + span->len == (int)x &&
1280 span->coverage == coverage )
1282 span->len = (unsigned short)( span->len + acount );
1286 if ( ras.span_y != y || count >= FT_MAX_GRAY_SPANS )
1288 if ( ras.render_span && count > 0 )
1289 ras.render_span( ras.span_y, count, ras.gray_spans,
1290 ras.render_span_data );
1292 #ifdef FT_DEBUG_LEVEL_TRACE
1299 FT_TRACE7(( "y = %3d ", ras.span_y ));
1300 span = ras.gray_spans;
1301 for ( n = 0; n < count; n++, span++ )
1302 FT_TRACE7(( "[%d..%d]:%02x ",
1303 span->x, span->x + span->len - 1, span->coverage ));
1304 FT_TRACE7(( "\n" ));
1307 #endif /* FT_DEBUG_LEVEL_TRACE */
1309 ras.num_gray_spans = 0;
1310 ras.span_y = (int)y;
1313 span = ras.gray_spans;
1318 /* add a gray span to the current list */
1320 span->len = (unsigned short)acount;
1321 span->coverage = (unsigned char)coverage;
1323 ras.num_gray_spans++;
1328 #ifdef FT_DEBUG_LEVEL_TRACE
1330 /* to be called while in the debugger -- */
1331 /* this function causes a compiler warning since it is unused otherwise */
1333 gray_dump_cells( RAS_ARG )
1338 for ( yindex = 0; yindex < ras.ycount; yindex++ )
1343 printf( "%3d:", yindex );
1345 for ( cell = ras.ycells[yindex]; cell != NULL; cell = cell->next )
1346 printf( " (%3ld, c:%4ld, a:%6d)", cell->x, cell->cover, cell->area );
1351 #endif /* FT_DEBUG_LEVEL_TRACE */
1355 gray_sweep( RAS_ARG_ const FT_Bitmap* target )
1359 FT_UNUSED( target );
1362 if ( ras.num_cells == 0 )
1365 ras.num_gray_spans = 0;
1367 FT_TRACE7(( "gray_sweep: start\n" ));
1369 for ( yindex = 0; yindex < ras.ycount; yindex++ )
1371 PCell cell = ras.ycells[yindex];
1376 for ( ; cell != NULL; cell = cell->next )
1381 if ( cell->x > x && cover != 0 )
1382 gray_hline( RAS_VAR_ x, yindex, cover * ( ONE_PIXEL * 2 ),
1385 cover += cell->cover;
1386 area = cover * ( ONE_PIXEL * 2 ) - cell->area;
1388 if ( area != 0 && cell->x >= 0 )
1389 gray_hline( RAS_VAR_ cell->x, yindex, area, 1 );
1395 gray_hline( RAS_VAR_ x, yindex, cover * ( ONE_PIXEL * 2 ),
1399 if ( ras.render_span && ras.num_gray_spans > 0 )
1400 ras.render_span( ras.span_y, ras.num_gray_spans,
1401 ras.gray_spans, ras.render_span_data );
1403 FT_TRACE7(( "gray_sweep: end\n" ));
1409 /*************************************************************************/
1411 /* The following function should only compile in stand-alone mode, */
1412 /* i.e., when building this component without the rest of FreeType. */
1414 /*************************************************************************/
1416 /*************************************************************************/
1419 /* FT_Outline_Decompose */
1422 /* Walk over an outline's structure to decompose it into individual */
1423 /* segments and Bézier arcs. This function is also able to emit */
1424 /* `move to' and `close to' operations to indicate the start and end */
1425 /* of new contours in the outline. */
1428 /* outline :: A pointer to the source target. */
1430 /* func_interface :: A table of `emitters', i.e., function pointers */
1431 /* called during decomposition to indicate path */
1435 /* user :: A typeless pointer which is passed to each */
1436 /* emitter during the decomposition. It can be */
1437 /* used to store the state during the */
1438 /* decomposition. */
1441 /* Error code. 0 means success. */
1444 FT_Outline_Decompose( const FT_Outline* outline,
1445 const FT_Outline_Funcs* func_interface,
1449 #define SCALED( x ) ( ( (x) << shift ) - delta )
1452 FT_Vector v_control;
1461 int n; /* index of contour in outline */
1462 int first; /* index of first point in contour */
1463 char tag; /* current point's state */
1469 if ( !outline || !func_interface )
1470 return ErrRaster_Invalid_Argument;
1472 shift = func_interface->shift;
1473 delta = func_interface->delta;
1476 for ( n = 0; n < outline->n_contours; n++ )
1478 int last; /* index of last point in contour */
1481 FT_TRACE5(( "FT_Outline_Decompose: Outline %d\n", n ));
1483 last = outline->contours[n];
1485 goto Invalid_Outline;
1486 limit = outline->points + last;
1488 v_start = outline->points[first];
1489 v_start.x = SCALED( v_start.x );
1490 v_start.y = SCALED( v_start.y );
1492 v_last = outline->points[last];
1493 v_last.x = SCALED( v_last.x );
1494 v_last.y = SCALED( v_last.y );
1496 v_control = v_start;
1498 point = outline->points + first;
1499 tags = outline->tags + first;
1500 tag = FT_CURVE_TAG( tags[0] );
1502 /* A contour cannot start with a cubic control point! */
1503 if ( tag == FT_CURVE_TAG_CUBIC )
1504 goto Invalid_Outline;
1506 /* check first point to determine origin */
1507 if ( tag == FT_CURVE_TAG_CONIC )
1509 /* first point is conic control. Yes, this happens. */
1510 if ( FT_CURVE_TAG( outline->tags[last] ) == FT_CURVE_TAG_ON )
1512 /* start at last point if it is on the curve */
1518 /* if both first and last points are conic, */
1519 /* start at their middle and record its position */
1521 v_start.x = ( v_start.x + v_last.x ) / 2;
1522 v_start.y = ( v_start.y + v_last.y ) / 2;
1530 FT_TRACE5(( " move to (%.2f, %.2f)\n",
1531 v_start.x / 64.0, v_start.y / 64.0 ));
1532 error = func_interface->move_to( &v_start, user );
1536 while ( point < limit )
1541 tag = FT_CURVE_TAG( tags[0] );
1544 case FT_CURVE_TAG_ON: /* emit a single line_to */
1549 vec.x = SCALED( point->x );
1550 vec.y = SCALED( point->y );
1552 FT_TRACE5(( " line to (%.2f, %.2f)\n",
1553 vec.x / 64.0, vec.y / 64.0 ));
1554 error = func_interface->line_to( &vec, user );
1560 case FT_CURVE_TAG_CONIC: /* consume conic arcs */
1561 v_control.x = SCALED( point->x );
1562 v_control.y = SCALED( point->y );
1565 if ( point < limit )
1573 tag = FT_CURVE_TAG( tags[0] );
1575 vec.x = SCALED( point->x );
1576 vec.y = SCALED( point->y );
1578 if ( tag == FT_CURVE_TAG_ON )
1580 FT_TRACE5(( " conic to (%.2f, %.2f)"
1581 " with control (%.2f, %.2f)\n",
1582 vec.x / 64.0, vec.y / 64.0,
1583 v_control.x / 64.0, v_control.y / 64.0 ));
1584 error = func_interface->conic_to( &v_control, &vec, user );
1590 if ( tag != FT_CURVE_TAG_CONIC )
1591 goto Invalid_Outline;
1593 v_middle.x = ( v_control.x + vec.x ) / 2;
1594 v_middle.y = ( v_control.y + vec.y ) / 2;
1596 FT_TRACE5(( " conic to (%.2f, %.2f)"
1597 " with control (%.2f, %.2f)\n",
1598 v_middle.x / 64.0, v_middle.y / 64.0,
1599 v_control.x / 64.0, v_control.y / 64.0 ));
1600 error = func_interface->conic_to( &v_control, &v_middle, user );
1608 FT_TRACE5(( " conic to (%.2f, %.2f)"
1609 " with control (%.2f, %.2f)\n",
1610 v_start.x / 64.0, v_start.y / 64.0,
1611 v_control.x / 64.0, v_control.y / 64.0 ));
1612 error = func_interface->conic_to( &v_control, &v_start, user );
1615 default: /* FT_CURVE_TAG_CUBIC */
1617 FT_Vector vec1, vec2;
1620 if ( point + 1 > limit ||
1621 FT_CURVE_TAG( tags[1] ) != FT_CURVE_TAG_CUBIC )
1622 goto Invalid_Outline;
1627 vec1.x = SCALED( point[-2].x );
1628 vec1.y = SCALED( point[-2].y );
1630 vec2.x = SCALED( point[-1].x );
1631 vec2.y = SCALED( point[-1].y );
1633 if ( point <= limit )
1638 vec.x = SCALED( point->x );
1639 vec.y = SCALED( point->y );
1641 FT_TRACE5(( " cubic to (%.2f, %.2f)"
1642 " with controls (%.2f, %.2f) and (%.2f, %.2f)\n",
1643 vec.x / 64.0, vec.y / 64.0,
1644 vec1.x / 64.0, vec1.y / 64.0,
1645 vec2.x / 64.0, vec2.y / 64.0 ));
1646 error = func_interface->cubic_to( &vec1, &vec2, &vec, user );
1652 FT_TRACE5(( " cubic to (%.2f, %.2f)"
1653 " with controls (%.2f, %.2f) and (%.2f, %.2f)\n",
1654 v_start.x / 64.0, v_start.y / 64.0,
1655 vec1.x / 64.0, vec1.y / 64.0,
1656 vec2.x / 64.0, vec2.y / 64.0 ));
1657 error = func_interface->cubic_to( &vec1, &vec2, &v_start, user );
1663 /* close the contour with a line segment */
1664 FT_TRACE5(( " line to (%.2f, %.2f)\n",
1665 v_start.x / 64.0, v_start.y / 64.0 ));
1666 error = func_interface->line_to( &v_start, user );
1675 FT_TRACE5(( "FT_Outline_Decompose: Done\n", n ));
1679 FT_TRACE5(( "FT_Outline_Decompose: Error %d\n", error ));
1683 return ErrRaster_Invalid_Outline;
1686 #endif /* _STANDALONE_ */
1689 typedef struct gray_TBand_
1695 FT_DEFINE_OUTLINE_FUNCS(func_interface,
1696 (FT_Outline_MoveTo_Func) gray_move_to,
1697 (FT_Outline_LineTo_Func) gray_line_to,
1698 (FT_Outline_ConicTo_Func)gray_conic_to,
1699 (FT_Outline_CubicTo_Func)gray_cubic_to,
1705 gray_convert_glyph_inner( RAS_ARG )
1708 volatile int error = 0;
1710 #ifdef FT_CONFIG_OPTION_PIC
1711 FT_Outline_Funcs func_interface;
1712 Init_Class_func_interface(&func_interface);
1715 if ( ft_setjmp( ras.jump_buffer ) == 0 )
1717 error = FT_Outline_Decompose( &ras.outline, &func_interface, &ras );
1718 gray_record_cell( RAS_VAR );
1721 error = ErrRaster_Memory_Overflow;
1728 gray_convert_glyph( RAS_ARG )
1730 gray_TBand bands[40];
1731 gray_TBand* volatile band;
1732 int volatile n, num_bands;
1733 TPos volatile min, max, max_y;
1737 /* Set up state in the raster object */
1738 gray_compute_cbox( RAS_VAR );
1740 /* clip to target bitmap, exit if nothing to do */
1741 clip = &ras.clip_box;
1743 if ( ras.max_ex <= clip->xMin || ras.min_ex >= clip->xMax ||
1744 ras.max_ey <= clip->yMin || ras.min_ey >= clip->yMax )
1747 if ( ras.min_ex < clip->xMin ) ras.min_ex = clip->xMin;
1748 if ( ras.min_ey < clip->yMin ) ras.min_ey = clip->yMin;
1750 if ( ras.max_ex > clip->xMax ) ras.max_ex = clip->xMax;
1751 if ( ras.max_ey > clip->yMax ) ras.max_ey = clip->yMax;
1753 ras.count_ex = ras.max_ex - ras.min_ex;
1754 ras.count_ey = ras.max_ey - ras.min_ey;
1756 /* set up vertical bands */
1757 num_bands = (int)( ( ras.max_ey - ras.min_ey ) / ras.band_size );
1758 if ( num_bands == 0 )
1760 if ( num_bands >= 39 )
1768 for ( n = 0; n < num_bands; n++, min = max )
1770 max = min + ras.band_size;
1771 if ( n == num_bands - 1 || max > max_y )
1778 while ( band >= bands )
1780 TPos bottom, top, middle;
1786 long cell_start, cell_end, cell_mod;
1789 ras.ycells = (PCell*)ras.buffer;
1790 ras.ycount = band->max - band->min;
1792 cell_start = sizeof ( PCell ) * ras.ycount;
1793 cell_mod = cell_start % sizeof ( TCell );
1795 cell_start += sizeof ( TCell ) - cell_mod;
1797 cell_end = ras.buffer_size;
1798 cell_end -= cell_end % sizeof ( TCell );
1800 cells_max = (PCell)( (char*)ras.buffer + cell_end );
1801 ras.cells = (PCell)( (char*)ras.buffer + cell_start );
1802 if ( ras.cells >= cells_max )
1805 ras.max_cells = cells_max - ras.cells;
1806 if ( ras.max_cells < 2 )
1809 for ( yindex = 0; yindex < ras.ycount; yindex++ )
1810 ras.ycells[yindex] = NULL;
1815 ras.min_ey = band->min;
1816 ras.max_ey = band->max;
1817 ras.count_ey = band->max - band->min;
1819 error = gray_convert_glyph_inner( RAS_VAR );
1823 gray_sweep( RAS_VAR_ &ras.target );
1827 else if ( error != ErrRaster_Memory_Overflow )
1831 /* render pool overflow; we will reduce the render band by half */
1834 middle = bottom + ( ( top - bottom ) >> 1 );
1836 /* This is too complex for a single scanline; there must */
1837 /* be some problems. */
1838 if ( middle == bottom )
1840 #ifdef FT_DEBUG_LEVEL_TRACE
1841 FT_TRACE7(( "gray_convert_glyph: rotten glyph\n" ));
1846 if ( bottom-top >= ras.band_size )
1849 band[1].min = bottom;
1850 band[1].max = middle;
1851 band[0].min = middle;
1857 if ( ras.band_shoot > 8 && ras.band_size > 16 )
1858 ras.band_size = ras.band_size / 2;
1865 gray_raster_render( gray_PRaster raster,
1866 const FT_Raster_Params* params )
1868 const FT_Outline* outline = (const FT_Outline*)params->source;
1869 const FT_Bitmap* target_map = params->target;
1870 gray_PWorker worker;
1873 if ( !raster || !raster->buffer || !raster->buffer_size )
1874 return ErrRaster_Invalid_Argument;
1877 return ErrRaster_Invalid_Outline;
1879 /* return immediately if the outline is empty */
1880 if ( outline->n_points == 0 || outline->n_contours <= 0 )
1883 if ( !outline->contours || !outline->points )
1884 return ErrRaster_Invalid_Outline;
1886 if ( outline->n_points !=
1887 outline->contours[outline->n_contours - 1] + 1 )
1888 return ErrRaster_Invalid_Outline;
1890 worker = raster->worker;
1892 /* if direct mode is not set, we must have a target bitmap */
1893 if ( !( params->flags & FT_RASTER_FLAG_DIRECT ) )
1896 return ErrRaster_Invalid_Argument;
1899 if ( !target_map->width || !target_map->rows )
1902 if ( !target_map->buffer )
1903 return ErrRaster_Invalid_Argument;
1906 /* this version does not support monochrome rendering */
1907 if ( !( params->flags & FT_RASTER_FLAG_AA ) )
1908 return ErrRaster_Invalid_Mode;
1910 /* compute clipping box */
1911 if ( !( params->flags & FT_RASTER_FLAG_DIRECT ) )
1913 /* compute clip box from target pixmap */
1914 ras.clip_box.xMin = 0;
1915 ras.clip_box.yMin = 0;
1916 ras.clip_box.xMax = target_map->width;
1917 ras.clip_box.yMax = target_map->rows;
1919 else if ( params->flags & FT_RASTER_FLAG_CLIP )
1920 ras.clip_box = params->clip_box;
1923 ras.clip_box.xMin = -32768L;
1924 ras.clip_box.yMin = -32768L;
1925 ras.clip_box.xMax = 32767L;
1926 ras.clip_box.yMax = 32767L;
1929 gray_init_cells( RAS_VAR_ raster->buffer, raster->buffer_size );
1931 ras.outline = *outline;
1934 ras.band_size = raster->band_size;
1935 ras.num_gray_spans = 0;
1937 if ( params->flags & FT_RASTER_FLAG_DIRECT )
1939 ras.render_span = (FT_Raster_Span_Func)params->gray_spans;
1940 ras.render_span_data = params->user;
1944 ras.target = *target_map;
1945 ras.render_span = (FT_Raster_Span_Func)gray_render_span;
1946 ras.render_span_data = &ras;
1949 return gray_convert_glyph( RAS_VAR );
1953 /**** RASTER OBJECT CREATION: In stand-alone mode, we simply use *****/
1954 /**** a static object. *****/
1959 gray_raster_new( void* memory,
1960 FT_Raster* araster )
1962 static gray_TRaster the_raster;
1964 FT_UNUSED( memory );
1967 *araster = (FT_Raster)&the_raster;
1968 FT_MEM_ZERO( &the_raster, sizeof ( the_raster ) );
1975 gray_raster_done( FT_Raster raster )
1978 FT_UNUSED( raster );
1981 #else /* !_STANDALONE_ */
1984 gray_raster_new( FT_Memory memory,
1985 FT_Raster* araster )
1988 gray_PRaster raster = NULL;
1992 if ( !FT_ALLOC( raster, sizeof ( gray_TRaster ) ) )
1994 raster->memory = memory;
1995 *araster = (FT_Raster)raster;
2003 gray_raster_done( FT_Raster raster )
2005 FT_Memory memory = (FT_Memory)((gray_PRaster)raster)->memory;
2011 #endif /* !_STANDALONE_ */
2015 gray_raster_reset( FT_Raster raster,
2019 gray_PRaster rast = (gray_PRaster)raster;
2024 if ( pool_base && pool_size >= (long)sizeof ( gray_TWorker ) + 2048 )
2026 gray_PWorker worker = (gray_PWorker)pool_base;
2029 rast->worker = worker;
2030 rast->buffer = pool_base +
2031 ( ( sizeof ( gray_TWorker ) +
2032 sizeof ( TCell ) - 1 ) &
2033 ~( sizeof ( TCell ) - 1 ) );
2034 rast->buffer_size = (long)( ( pool_base + pool_size ) -
2035 (char*)rast->buffer ) &
2036 ~( sizeof ( TCell ) - 1 );
2037 rast->band_size = (int)( rast->buffer_size /
2038 ( sizeof ( TCell ) * 8 ) );
2042 rast->buffer = NULL;
2043 rast->buffer_size = 0;
2044 rast->worker = NULL;
2050 FT_DEFINE_RASTER_FUNCS(ft_grays_raster,
2051 FT_GLYPH_FORMAT_OUTLINE,
2053 (FT_Raster_New_Func) gray_raster_new,
2054 (FT_Raster_Reset_Func) gray_raster_reset,
2055 (FT_Raster_Set_Mode_Func)0,
2056 (FT_Raster_Render_Func) gray_raster_render,
2057 (FT_Raster_Done_Func) gray_raster_done
2064 /* Local Variables: */