1 /***************************************************************************/
5 /* A new `perfect' anti-aliasing renderer (body). */
7 /* Copyright 2000-2001, 2002, 2003, 2005, 2006, 2007 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
98 #include <string.h> /* for ft_memcpy() */
101 #define FT_UINT_MAX UINT_MAX
103 #define ft_memset memset
105 #define ft_setjmp setjmp
106 #define ft_longjmp longjmp
107 #define ft_jmp_buf jmp_buf
110 #define ErrRaster_Invalid_Mode -2
111 #define ErrRaster_Invalid_Outline -1
112 #define ErrRaster_Invalid_Argument -3
113 #define ErrRaster_Memory_Overflow -4
115 #define FT_BEGIN_HEADER
116 #define FT_END_HEADER
121 /* This macro is used to indicate that a function parameter is unused. */
122 /* Its purpose is simply to reduce compiler warnings. Note also that */
123 /* simply defining it as `(void)x' doesn't avoid warnings with certain */
124 /* ANSI compilers (e.g. LCC). */
125 #define FT_UNUSED( x ) (x) = (x)
127 /* Disable the tracing mechanism for simplicity -- developers can */
128 /* activate it easily by redefining these two macros. */
130 #define FT_ERROR( x ) do ; while ( 0 ) /* nothing */
134 #define FT_TRACE( x ) do ; while ( 0 ) /* nothing */
137 #else /* !_STANDALONE_ */
139 #include <ft2build.h>
141 #include FT_INTERNAL_OBJECTS_H
142 #include FT_INTERNAL_DEBUG_H
143 #include FT_OUTLINE_H
145 #include "ftsmerrs.h"
147 #define ErrRaster_Invalid_Mode Smooth_Err_Cannot_Render_Glyph
148 #define ErrRaster_Invalid_Outline Smooth_Err_Invalid_Outline
149 #define ErrRaster_Memory_Overflow Smooth_Err_Out_Of_Memory
150 #define ErrRaster_Invalid_Argument Smooth_Err_Bad_Argument
152 #endif /* !_STANDALONE_ */
156 #define FT_MEM_SET( d, s, c ) ft_memset( d, s, c )
160 #define FT_MEM_ZERO( dest, count ) FT_MEM_SET( dest, 0, count )
163 /* define this to dump debugging information */
164 #define xxxDEBUG_GRAYS
167 /* as usual, for the speed hungry :-) */
169 #ifndef FT_STATIC_RASTER
172 #define RAS_ARG PWorker worker
173 #define RAS_ARG_ PWorker worker,
175 #define RAS_VAR worker
176 #define RAS_VAR_ worker,
178 #define ras (*worker)
181 #else /* FT_STATIC_RASTER */
184 #define RAS_ARG /* empty */
185 #define RAS_ARG_ /* empty */
186 #define RAS_VAR /* empty */
187 #define RAS_VAR_ /* empty */
192 #endif /* FT_STATIC_RASTER */
195 /* must be at least 6 bits! */
198 #define ONE_PIXEL ( 1L << PIXEL_BITS )
199 #define PIXEL_MASK ( -1L << PIXEL_BITS )
200 #define TRUNC( x ) ( (TCoord)( (x) >> PIXEL_BITS ) )
201 #define SUBPIXELS( x ) ( (TPos)(x) << PIXEL_BITS )
202 #define FLOOR( x ) ( (x) & -ONE_PIXEL )
203 #define CEILING( x ) ( ( (x) + ONE_PIXEL - 1 ) & -ONE_PIXEL )
204 #define ROUND( x ) ( ( (x) + ONE_PIXEL / 2 ) & -ONE_PIXEL )
207 #define UPSCALE( x ) ( (x) << ( PIXEL_BITS - 6 ) )
208 #define DOWNSCALE( x ) ( (x) >> ( PIXEL_BITS - 6 ) )
210 #define UPSCALE( x ) ( (x) >> ( 6 - PIXEL_BITS ) )
211 #define DOWNSCALE( x ) ( (x) << ( 6 - PIXEL_BITS ) )
215 /*************************************************************************/
217 /* TYPE DEFINITIONS */
220 /* don't change the following types to FT_Int or FT_Pos, since we might */
221 /* need to define them to "float" or "double" when experimenting with */
224 typedef int TCoord
; /* integer scanline/pixel coordinate */
225 typedef long TPos
; /* sub-pixel coordinate */
227 /* determine the type used to store cell areas. This normally takes at */
228 /* least PIXEL_BITS*2 + 1 bits. On 16-bit systems, we need to use */
229 /* `long' instead of `int', otherwise bad things happen */
235 #else /* PIXEL_BITS >= 8 */
237 /* approximately determine the size of integers using an ANSI-C header */
238 #if FT_UINT_MAX == 0xFFFFU
244 #endif /* PIXEL_BITS >= 8 */
247 /* maximal number of gray spans in a call to the span callback */
248 #define FT_MAX_GRAY_SPANS 32
251 typedef struct TCell_
* PCell
;
253 typedef struct TCell_
263 typedef struct TWorker_
268 TPos count_ex
, count_ey
;
283 FT_Vector bez_stack
[32 * 3 + 1];
290 FT_Span gray_spans
[FT_MAX_GRAY_SPANS
];
293 FT_Raster_Span_Func render_span
;
294 void* render_span_data
;
302 ft_jmp_buf jump_buffer
;
313 typedef struct TRaster_
325 /*************************************************************************/
327 /* Initialize the cells table. */
330 gray_init_cells( RAS_ARG_
void* buffer
,
334 ras
.buffer_size
= byte_size
;
336 ras
.ycells
= (PCell
*) buffer
;
346 /*************************************************************************/
348 /* Compute the outline bounding box. */
351 gray_compute_cbox( RAS_ARG
)
353 FT_Outline
* outline
= &ras
.outline
;
354 FT_Vector
* vec
= outline
->points
;
355 FT_Vector
* limit
= vec
+ outline
->n_points
;
358 if ( outline
->n_points
<= 0 )
360 ras
.min_ex
= ras
.max_ex
= 0;
361 ras
.min_ey
= ras
.max_ey
= 0;
365 ras
.min_ex
= ras
.max_ex
= vec
->x
;
366 ras
.min_ey
= ras
.max_ey
= vec
->y
;
370 for ( ; vec
< limit
; vec
++ )
376 if ( x
< ras
.min_ex
) ras
.min_ex
= x
;
377 if ( x
> ras
.max_ex
) ras
.max_ex
= x
;
378 if ( y
< ras
.min_ey
) ras
.min_ey
= y
;
379 if ( y
> ras
.max_ey
) ras
.max_ey
= y
;
382 /* truncate the bounding box to integer pixels */
383 ras
.min_ex
= ras
.min_ex
>> 6;
384 ras
.min_ey
= ras
.min_ey
>> 6;
385 ras
.max_ex
= ( ras
.max_ex
+ 63 ) >> 6;
386 ras
.max_ey
= ( ras
.max_ey
+ 63 ) >> 6;
390 /*************************************************************************/
392 /* Record the current cell in the table. */
395 gray_find_cell( RAS_ARG
)
401 if ( x
> ras
.max_ex
)
404 pcell
= &ras
.ycells
[ras
.ey
];
408 if ( cell
== NULL
|| cell
->x
> x
)
417 if ( ras
.num_cells
>= ras
.max_cells
)
418 ft_longjmp( ras
.jump_buffer
, 1 );
420 cell
= ras
.cells
+ ras
.num_cells
++;
434 gray_record_cell( RAS_ARG
)
436 if ( !ras
.invalid
&& ( ras
.area
| ras
.cover
) )
438 PCell cell
= gray_find_cell( RAS_VAR
);
441 cell
->area
+= ras
.area
;
442 cell
->cover
+= ras
.cover
;
447 /*************************************************************************/
449 /* Set the current cell to a new position. */
452 gray_set_cell( RAS_ARG_ TCoord ex
,
455 /* Move the cell pointer to a new position. We set the `invalid' */
456 /* flag to indicate that the cell isn't part of those we're interested */
457 /* in during the render phase. This means that: */
459 /* . the new vertical position must be within min_ey..max_ey-1. */
460 /* . the new horizontal position must be strictly less than max_ex */
462 /* Note that if a cell is to the left of the clipping region, it is */
463 /* actually set to the (min_ex-1) horizontal position. */
465 /* All cells that are on the left of the clipping region go to the */
466 /* min_ex - 1 horizontal position. */
469 if ( ex
> ras
.max_ex
)
476 /* are we moving to a different cell ? */
477 if ( ex
!= ras
.ex
|| ey
!= ras
.ey
)
479 /* record the current one if it is valid */
481 gray_record_cell( RAS_VAR
);
489 ras
.invalid
= ( (unsigned)ey
>= (unsigned)ras
.count_ey
||
490 ex
>= ras
.count_ex
);
494 /*************************************************************************/
496 /* Start a new contour at a given cell. */
499 gray_start_cell( RAS_ARG_ TCoord ex
,
502 if ( ex
> ras
.max_ex
)
503 ex
= (TCoord
)( ras
.max_ex
);
505 if ( ex
< ras
.min_ex
)
506 ex
= (TCoord
)( ras
.min_ex
- 1 );
510 ras
.ex
= ex
- ras
.min_ex
;
511 ras
.ey
= ey
- ras
.min_ey
;
512 ras
.last_ey
= SUBPIXELS( ey
);
515 gray_set_cell( RAS_VAR_ ex
, ey
);
519 /*************************************************************************/
521 /* Render a scanline as one or more cells. */
524 gray_render_scanline( RAS_ARG_ TCoord ey
,
530 TCoord ex1
, ex2
, fx1
, fx2
, delta
;
532 int incr
, lift
, mod
, rem
;
539 fx1
= (TCoord
)( x1
- SUBPIXELS( ex1
) );
540 fx2
= (TCoord
)( x2
- SUBPIXELS( ex2
) );
542 /* trivial case. Happens often */
545 gray_set_cell( RAS_VAR_ ex2
, ey
);
549 /* everything is located in a single cell. That is easy! */
554 ras
.area
+= (TArea
)( fx1
+ fx2
) * delta
;
559 /* ok, we'll have to render a run of adjacent cells on the same */
562 p
= ( ONE_PIXEL
- fx1
) * ( y2
- y1
);
568 p
= fx1
* ( y2
- y1
);
574 delta
= (TCoord
)( p
/ dx
);
575 mod
= (TCoord
)( p
% dx
);
582 ras
.area
+= (TArea
)( fx1
+ first
) * delta
;
586 gray_set_cell( RAS_VAR_ ex1
, ey
);
591 p
= ONE_PIXEL
* ( y2
- y1
+ delta
);
592 lift
= (TCoord
)( p
/ dx
);
593 rem
= (TCoord
)( p
% dx
);
612 ras
.area
+= (TArea
)ONE_PIXEL
* delta
;
616 gray_set_cell( RAS_VAR_ ex1
, ey
);
621 ras
.area
+= (TArea
)( fx2
+ ONE_PIXEL
- first
) * delta
;
626 /*************************************************************************/
628 /* Render a given line as a series of scanlines. */
631 gray_render_line( RAS_ARG_ TPos to_x
,
634 TCoord ey1
, ey2
, fy1
, fy2
;
637 int delta
, rem
, mod
, lift
, incr
;
640 ey1
= TRUNC( ras
.last_ey
);
641 ey2
= TRUNC( to_y
); /* if (ey2 >= ras.max_ey) ey2 = ras.max_ey-1; */
642 fy1
= (TCoord
)( ras
.y
- ras
.last_ey
);
643 fy2
= (TCoord
)( to_y
- SUBPIXELS( ey2
) );
648 /* XXX: we should do something about the trivial case where dx == 0, */
649 /* as it happens very often! */
651 /* perform vertical clipping */
663 if ( min
>= ras
.max_ey
|| max
< ras
.min_ey
)
667 /* everything is on a single scanline */
670 gray_render_scanline( RAS_VAR_ ey1
, ras
.x
, fy1
, to_x
, fy2
);
674 /* vertical line - avoid calling gray_render_scanline */
679 TCoord ex
= TRUNC( ras
.x
);
680 TCoord two_fx
= (TCoord
)( ( ras
.x
- SUBPIXELS( ex
) ) << 1 );
691 delta
= (int)( first
- fy1
);
692 ras
.area
+= (TArea
)two_fx
* delta
;
696 gray_set_cell( &ras
, ex
, ey1
);
698 delta
= (int)( first
+ first
- ONE_PIXEL
);
699 area
= (TArea
)two_fx
* delta
;
706 gray_set_cell( &ras
, ex
, ey1
);
709 delta
= (int)( fy2
- ONE_PIXEL
+ first
);
710 ras
.area
+= (TArea
)two_fx
* delta
;
716 /* ok, we have to render several scanlines */
717 p
= ( ONE_PIXEL
- fy1
) * dx
;
729 delta
= (int)( p
/ dy
);
730 mod
= (int)( p
% dy
);
738 gray_render_scanline( RAS_VAR_ ey1
, ras
.x
, fy1
, x
, (TCoord
)first
);
741 gray_set_cell( RAS_VAR_
TRUNC( x
), ey1
);
746 lift
= (int)( p
/ dy
);
747 rem
= (int)( p
% dy
);
766 gray_render_scanline( RAS_VAR_ ey1
, x
,
767 (TCoord
)( ONE_PIXEL
- first
), x2
,
772 gray_set_cell( RAS_VAR_
TRUNC( x
), ey1
);
776 gray_render_scanline( RAS_VAR_ ey1
, x
,
777 (TCoord
)( ONE_PIXEL
- first
), to_x
,
783 ras
.last_ey
= SUBPIXELS( ey2
);
788 gray_split_conic( FT_Vector
* base
)
793 base
[4].x
= base
[2].x
;
795 a
= base
[3].x
= ( base
[2].x
+ b
) / 2;
796 b
= base
[1].x
= ( base
[0].x
+ b
) / 2;
797 base
[2].x
= ( a
+ b
) / 2;
799 base
[4].y
= base
[2].y
;
801 a
= base
[3].y
= ( base
[2].y
+ b
) / 2;
802 b
= base
[1].y
= ( base
[0].y
+ b
) / 2;
803 base
[2].y
= ( a
+ b
) / 2;
808 gray_render_conic( RAS_ARG_
const FT_Vector
* control
,
809 const FT_Vector
* to
)
817 dx
= DOWNSCALE( ras
.x
) + to
->x
- ( control
->x
<< 1 );
820 dy
= DOWNSCALE( ras
.y
) + to
->y
- ( control
->y
<< 1 );
827 dx
= dx
/ ras
.conic_level
;
834 /* a shortcut to speed things up */
837 /* we compute the mid-point directly in order to avoid */
838 /* calling gray_split_conic() */
839 TPos to_x
, to_y
, mid_x
, mid_y
;
842 to_x
= UPSCALE( to
->x
);
843 to_y
= UPSCALE( to
->y
);
844 mid_x
= ( ras
.x
+ to_x
+ 2 * UPSCALE( control
->x
) ) / 4;
845 mid_y
= ( ras
.y
+ to_y
+ 2 * UPSCALE( control
->y
) ) / 4;
847 gray_render_line( RAS_VAR_ mid_x
, mid_y
);
848 gray_render_line( RAS_VAR_ to_x
, to_y
);
854 levels
= ras
.lev_stack
;
858 arc
[0].x
= UPSCALE( to
->x
);
859 arc
[0].y
= UPSCALE( to
->y
);
860 arc
[1].x
= UPSCALE( control
->x
);
861 arc
[1].y
= UPSCALE( control
->y
);
870 /* check that the arc crosses the current band */
874 min
= max
= arc
[0].y
;
877 if ( y
< min
) min
= y
;
878 if ( y
> max
) max
= y
;
881 if ( y
< min
) min
= y
;
882 if ( y
> max
) max
= y
;
884 if ( TRUNC( min
) >= ras
.max_ey
|| TRUNC( max
) < ras
.min_ey
)
887 gray_split_conic( arc
);
890 levels
[top
] = levels
[top
- 1] = level
- 1;
896 TPos to_x
, to_y
, mid_x
, mid_y
;
901 mid_x
= ( ras
.x
+ to_x
+ 2 * arc
[1].x
) / 4;
902 mid_y
= ( ras
.y
+ to_y
+ 2 * arc
[1].y
) / 4;
904 gray_render_line( RAS_VAR_ mid_x
, mid_y
);
905 gray_render_line( RAS_VAR_ to_x
, to_y
);
917 gray_split_cubic( FT_Vector
* base
)
922 base
[6].x
= base
[3].x
;
925 base
[1].x
= a
= ( base
[0].x
+ c
) / 2;
926 base
[5].x
= b
= ( base
[3].x
+ d
) / 2;
928 base
[2].x
= a
= ( a
+ c
) / 2;
929 base
[4].x
= b
= ( b
+ c
) / 2;
930 base
[3].x
= ( a
+ b
) / 2;
932 base
[6].y
= base
[3].y
;
935 base
[1].y
= a
= ( base
[0].y
+ c
) / 2;
936 base
[5].y
= b
= ( base
[3].y
+ d
) / 2;
938 base
[2].y
= a
= ( a
+ c
) / 2;
939 base
[4].y
= b
= ( b
+ c
) / 2;
940 base
[3].y
= ( a
+ b
) / 2;
945 gray_render_cubic( RAS_ARG_
const FT_Vector
* control1
,
946 const FT_Vector
* control2
,
947 const FT_Vector
* to
)
955 dx
= DOWNSCALE( ras
.x
) + to
->x
- ( control1
->x
<< 1 );
958 dy
= DOWNSCALE( ras
.y
) + to
->y
- ( control1
->y
<< 1 );
965 dx
= DOWNSCALE( ras
.x
) + to
->x
- 3 * ( control1
->x
+ control2
->x
);
968 dy
= DOWNSCALE( ras
.y
) + to
->y
- 3 * ( control1
->x
+ control2
->y
);
976 da
= da
/ ras
.cubic_level
;
977 db
= db
/ ras
.conic_level
;
978 while ( da
> 0 || db
> 0 )
987 TPos to_x
, to_y
, mid_x
, mid_y
;
990 to_x
= UPSCALE( to
->x
);
991 to_y
= UPSCALE( to
->y
);
992 mid_x
= ( ras
.x
+ to_x
+
993 3 * UPSCALE( control1
->x
+ control2
->x
) ) / 8;
994 mid_y
= ( ras
.y
+ to_y
+
995 3 * UPSCALE( control1
->y
+ control2
->y
) ) / 8;
997 gray_render_line( RAS_VAR_ mid_x
, mid_y
);
998 gray_render_line( RAS_VAR_ to_x
, to_y
);
1002 arc
= ras
.bez_stack
;
1003 arc
[0].x
= UPSCALE( to
->x
);
1004 arc
[0].y
= UPSCALE( to
->y
);
1005 arc
[1].x
= UPSCALE( control2
->x
);
1006 arc
[1].y
= UPSCALE( control2
->y
);
1007 arc
[2].x
= UPSCALE( control1
->x
);
1008 arc
[2].y
= UPSCALE( control1
->y
);
1012 levels
= ras
.lev_stack
;
1018 level
= levels
[top
];
1021 /* check that the arc crosses the current band */
1025 min
= max
= arc
[0].y
;
1027 if ( y
< min
) min
= y
;
1028 if ( y
> max
) max
= y
;
1030 if ( y
< min
) min
= y
;
1031 if ( y
> max
) max
= y
;
1033 if ( y
< min
) min
= y
;
1034 if ( y
> max
) max
= y
;
1035 if ( TRUNC( min
) >= ras
.max_ey
|| TRUNC( max
) < 0 )
1037 gray_split_cubic( arc
);
1040 levels
[top
] = levels
[top
- 1] = level
- 1;
1046 TPos to_x
, to_y
, mid_x
, mid_y
;
1051 mid_x
= ( ras
.x
+ to_x
+ 3 * ( arc
[1].x
+ arc
[2].x
) ) / 8;
1052 mid_y
= ( ras
.y
+ to_y
+ 3 * ( arc
[1].y
+ arc
[2].y
) ) / 8;
1054 gray_render_line( RAS_VAR_ mid_x
, mid_y
);
1055 gray_render_line( RAS_VAR_ to_x
, to_y
);
1067 gray_move_to( const FT_Vector
* to
,
1073 /* record current cell, if any */
1074 gray_record_cell( worker
);
1076 /* start to a new position */
1077 x
= UPSCALE( to
->x
);
1078 y
= UPSCALE( to
->y
);
1080 gray_start_cell( worker
, TRUNC( x
), TRUNC( y
) );
1089 gray_line_to( const FT_Vector
* to
,
1092 gray_render_line( worker
, UPSCALE( to
->x
), UPSCALE( to
->y
) );
1098 gray_conic_to( const FT_Vector
* control
,
1099 const FT_Vector
* to
,
1102 gray_render_conic( worker
, control
, to
);
1108 gray_cubic_to( const FT_Vector
* control1
,
1109 const FT_Vector
* control2
,
1110 const FT_Vector
* to
,
1113 gray_render_cubic( worker
, control1
, control2
, to
);
1119 gray_render_span( int y
,
1121 const FT_Span
* spans
,
1125 FT_Bitmap
* map
= &worker
->target
;
1128 /* first of all, compute the scanline offset */
1129 p
= (unsigned char*)map
->buffer
- y
* map
->pitch
;
1130 if ( map
->pitch
>= 0 )
1131 p
+= ( map
->rows
- 1 ) * map
->pitch
;
1133 for ( ; count
> 0; count
--, spans
++ )
1135 unsigned char coverage
= spans
->coverage
;
1140 /* For small-spans it is faster to do it by ourselves than
1141 * calling `memset'. This is mainly due to the cost of the
1144 if ( spans
->len
>= 8 )
1145 FT_MEM_SET( p
+ spans
->x
, (unsigned char)coverage
, spans
->len
);
1148 unsigned char* q
= p
+ spans
->x
;
1151 switch ( spans
->len
)
1153 case 7: *q
++ = (unsigned char)coverage
;
1154 case 6: *q
++ = (unsigned char)coverage
;
1155 case 5: *q
++ = (unsigned char)coverage
;
1156 case 4: *q
++ = (unsigned char)coverage
;
1157 case 3: *q
++ = (unsigned char)coverage
;
1158 case 2: *q
++ = (unsigned char)coverage
;
1159 case 1: *q
= (unsigned char)coverage
;
1170 gray_hline( RAS_ARG_ TCoord x
,
1180 /* compute the coverage line's coverage, depending on the */
1181 /* outline fill rule */
1183 /* the coverage percentage is area/(PIXEL_BITS*PIXEL_BITS*2) */
1185 coverage
= (int)( area
>> ( PIXEL_BITS
* 2 + 1 - 8 ) );
1186 /* use range 0..256 */
1188 coverage
= -coverage
;
1190 if ( ras
.outline
.flags
& FT_OUTLINE_EVEN_ODD_FILL
)
1194 if ( coverage
> 256 )
1195 coverage
= 512 - coverage
;
1196 else if ( coverage
== 256 )
1201 /* normal non-zero winding rule */
1202 if ( coverage
>= 256 )
1206 y
+= (TCoord
)ras
.min_ey
;
1207 x
+= (TCoord
)ras
.min_ex
;
1209 /* FT_Span.x is a 16-bit short, so limit our coordinates appropriately */
1215 /* see whether we can add this span to the current list */
1216 count
= ras
.num_gray_spans
;
1217 span
= ras
.gray_spans
+ count
- 1;
1220 (int)span
->x
+ span
->len
== (int)x
&&
1221 span
->coverage
== coverage
)
1223 span
->len
= (unsigned short)( span
->len
+ acount
);
1227 if ( ras
.span_y
!= y
|| count
>= FT_MAX_GRAY_SPANS
)
1229 if ( ras
.render_span
&& count
> 0 )
1230 ras
.render_span( ras
.span_y
, count
, ras
.gray_spans
,
1231 ras
.render_span_data
);
1232 /* ras.render_span( span->y, ras.gray_spans, count ); */
1236 if ( ras
.span_y
>= 0 )
1241 fprintf( stderr
, "y=%3d ", ras
.span_y
);
1242 span
= ras
.gray_spans
;
1243 for ( n
= 0; n
< count
; n
++, span
++ )
1244 fprintf( stderr
, "[%d..%d]:%02x ",
1245 span
->x
, span
->x
+ span
->len
- 1, span
->coverage
);
1246 fprintf( stderr
, "\n" );
1249 #endif /* DEBUG_GRAYS */
1251 ras
.num_gray_spans
= 0;
1255 span
= ras
.gray_spans
;
1260 /* add a gray span to the current list */
1262 span
->len
= (unsigned short)acount
;
1263 span
->coverage
= (unsigned char)coverage
;
1265 ras
.num_gray_spans
++;
1272 /* to be called while in the debugger */
1273 gray_dump_cells( RAS_ARG
)
1278 for ( yindex
= 0; yindex
< ras
.ycount
; yindex
++ )
1283 printf( "%3d:", yindex
);
1285 for ( cell
= ras
.ycells
[yindex
]; cell
!= NULL
; cell
= cell
->next
)
1286 printf( " (%3d, c:%4d, a:%6d)", cell
->x
, cell
->cover
, cell
->area
);
1291 #endif /* DEBUG_GRAYS */
1295 gray_sweep( RAS_ARG_
const FT_Bitmap
* target
)
1299 FT_UNUSED( target
);
1302 if ( ras
.num_cells
== 0 )
1305 ras
.num_gray_spans
= 0;
1307 for ( yindex
= 0; yindex
< ras
.ycount
; yindex
++ )
1309 PCell cell
= ras
.ycells
[yindex
];
1314 for ( ; cell
!= NULL
; cell
= cell
->next
)
1319 if ( cell
->x
> x
&& cover
!= 0 )
1320 gray_hline( RAS_VAR_ x
, yindex
, cover
* ( ONE_PIXEL
* 2 ),
1323 cover
+= cell
->cover
;
1324 area
= cover
* ( ONE_PIXEL
* 2 ) - cell
->area
;
1326 if ( area
!= 0 && cell
->x
>= 0 )
1327 gray_hline( RAS_VAR_ cell
->x
, yindex
, area
, 1 );
1333 gray_hline( RAS_VAR_ x
, yindex
, cover
* ( ONE_PIXEL
* 2 ),
1337 if ( ras
.render_span
&& ras
.num_gray_spans
> 0 )
1338 ras
.render_span( ras
.span_y
, ras
.num_gray_spans
,
1339 ras
.gray_spans
, ras
.render_span_data
);
1345 /*************************************************************************/
1347 /* The following function should only compile in stand_alone mode, */
1348 /* i.e., when building this component without the rest of FreeType. */
1350 /*************************************************************************/
1352 /*************************************************************************/
1355 /* FT_Outline_Decompose */
1358 /* Walks over an outline's structure to decompose it into individual */
1359 /* segments and Bezier arcs. This function is also able to emit */
1360 /* `move to' and `close to' operations to indicate the start and end */
1361 /* of new contours in the outline. */
1364 /* outline :: A pointer to the source target. */
1366 /* func_interface :: A table of `emitters', i.e,. function pointers */
1367 /* called during decomposition to indicate path */
1370 /* user :: A typeless pointer which is passed to each */
1371 /* emitter during the decomposition. It can be */
1372 /* used to store the state during the */
1373 /* decomposition. */
1376 /* Error code. 0 means success. */
1379 int FT_Outline_Decompose( const FT_Outline
* outline
,
1380 const FT_Outline_Funcs
* func_interface
,
1385 #define SCALED( x ) ( ( (x) << shift ) - delta )
1387 #define SCALED( x ) (x)
1391 FT_Vector v_control
;
1398 int n
; /* index of contour in outline */
1399 int first
; /* index of first point in contour */
1401 char tag
; /* current point's state */
1404 int shift
= func_interface
->shift
;
1405 TPos delta
= func_interface
->delta
;
1411 for ( n
= 0; n
< outline
->n_contours
; n
++ )
1413 int last
; /* index of last point in contour */
1416 last
= outline
->contours
[n
];
1417 limit
= outline
->points
+ last
;
1419 v_start
= outline
->points
[first
];
1420 v_last
= outline
->points
[last
];
1422 v_start
.x
= SCALED( v_start
.x
);
1423 v_start
.y
= SCALED( v_start
.y
);
1425 v_last
.x
= SCALED( v_last
.x
);
1426 v_last
.y
= SCALED( v_last
.y
);
1428 v_control
= v_start
;
1430 point
= outline
->points
+ first
;
1431 tags
= outline
->tags
+ first
;
1432 tag
= FT_CURVE_TAG( tags
[0] );
1434 /* A contour cannot start with a cubic control point! */
1435 if ( tag
== FT_CURVE_TAG_CUBIC
)
1436 goto Invalid_Outline
;
1438 /* check first point to determine origin */
1439 if ( tag
== FT_CURVE_TAG_CONIC
)
1441 /* first point is conic control. Yes, this happens. */
1442 if ( FT_CURVE_TAG( outline
->tags
[last
] ) == FT_CURVE_TAG_ON
)
1444 /* start at last point if it is on the curve */
1450 /* if both first and last points are conic, */
1451 /* start at their middle and record its position */
1453 v_start
.x
= ( v_start
.x
+ v_last
.x
) / 2;
1454 v_start
.y
= ( v_start
.y
+ v_last
.y
) / 2;
1462 error
= func_interface
->move_to( &v_start
, user
);
1466 while ( point
< limit
)
1471 tag
= FT_CURVE_TAG( tags
[0] );
1474 case FT_CURVE_TAG_ON
: /* emit a single line_to */
1479 vec
.x
= SCALED( point
->x
);
1480 vec
.y
= SCALED( point
->y
);
1482 error
= func_interface
->line_to( &vec
, user
);
1488 case FT_CURVE_TAG_CONIC
: /* consume conic arcs */
1490 v_control
.x
= SCALED( point
->x
);
1491 v_control
.y
= SCALED( point
->y
);
1494 if ( point
< limit
)
1502 tag
= FT_CURVE_TAG( tags
[0] );
1504 vec
.x
= SCALED( point
->x
);
1505 vec
.y
= SCALED( point
->y
);
1507 if ( tag
== FT_CURVE_TAG_ON
)
1509 error
= func_interface
->conic_to( &v_control
, &vec
,
1516 if ( tag
!= FT_CURVE_TAG_CONIC
)
1517 goto Invalid_Outline
;
1519 v_middle
.x
= ( v_control
.x
+ vec
.x
) / 2;
1520 v_middle
.y
= ( v_control
.y
+ vec
.y
) / 2;
1522 error
= func_interface
->conic_to( &v_control
, &v_middle
,
1531 error
= func_interface
->conic_to( &v_control
, &v_start
,
1536 default: /* FT_CURVE_TAG_CUBIC */
1538 FT_Vector vec1
, vec2
;
1541 if ( point
+ 1 > limit
||
1542 FT_CURVE_TAG( tags
[1] ) != FT_CURVE_TAG_CUBIC
)
1543 goto Invalid_Outline
;
1548 vec1
.x
= SCALED( point
[-2].x
);
1549 vec1
.y
= SCALED( point
[-2].y
);
1551 vec2
.x
= SCALED( point
[-1].x
);
1552 vec2
.y
= SCALED( point
[-1].y
);
1554 if ( point
<= limit
)
1559 vec
.x
= SCALED( point
->x
);
1560 vec
.y
= SCALED( point
->y
);
1562 error
= func_interface
->cubic_to( &vec1
, &vec2
, &vec
, user
);
1568 error
= func_interface
->cubic_to( &vec1
, &vec2
, &v_start
, user
);
1574 /* close the contour with a line segment */
1575 error
= func_interface
->line_to( &v_start
, user
);
1590 return ErrRaster_Invalid_Outline
;
1593 #endif /* _STANDALONE_ */
1596 typedef struct TBand_
1604 gray_convert_glyph_inner( RAS_ARG
)
1607 const FT_Outline_Funcs func_interface
=
1609 (FT_Outline_MoveTo_Func
) gray_move_to
,
1610 (FT_Outline_LineTo_Func
) gray_line_to
,
1611 (FT_Outline_ConicTo_Func
)gray_conic_to
,
1612 (FT_Outline_CubicTo_Func
)gray_cubic_to
,
1617 volatile int error
= 0;
1619 if ( ft_setjmp( ras
.jump_buffer
) == 0 )
1621 error
= FT_Outline_Decompose( &ras
.outline
, &func_interface
, &ras
);
1622 gray_record_cell( RAS_VAR
);
1626 error
= ErrRaster_Memory_Overflow
;
1634 gray_convert_glyph( RAS_ARG
)
1637 TBand
* volatile band
;
1638 int volatile n
, num_bands
;
1639 TPos
volatile min
, max
, max_y
;
1643 /* Set up state in the raster object */
1644 gray_compute_cbox( RAS_VAR
);
1646 /* clip to target bitmap, exit if nothing to do */
1647 clip
= &ras
.clip_box
;
1649 if ( ras
.max_ex
<= clip
->xMin
|| ras
.min_ex
>= clip
->xMax
||
1650 ras
.max_ey
<= clip
->yMin
|| ras
.min_ey
>= clip
->yMax
)
1653 if ( ras
.min_ex
< clip
->xMin
) ras
.min_ex
= clip
->xMin
;
1654 if ( ras
.min_ey
< clip
->yMin
) ras
.min_ey
= clip
->yMin
;
1656 if ( ras
.max_ex
> clip
->xMax
) ras
.max_ex
= clip
->xMax
;
1657 if ( ras
.max_ey
> clip
->yMax
) ras
.max_ey
= clip
->yMax
;
1659 ras
.count_ex
= ras
.max_ex
- ras
.min_ex
;
1660 ras
.count_ey
= ras
.max_ey
- ras
.min_ey
;
1662 /* simple heuristic used to speed-up the bezier decomposition -- see */
1663 /* the code in gray_render_conic() and gray_render_cubic() for more */
1665 ras
.conic_level
= 32;
1666 ras
.cubic_level
= 16;
1672 if ( ras
.count_ex
> 24 || ras
.count_ey
> 24 )
1674 if ( ras
.count_ex
> 120 || ras
.count_ey
> 120 )
1677 ras
.conic_level
<<= level
;
1678 ras
.cubic_level
<<= level
;
1681 /* setup vertical bands */
1682 num_bands
= (int)( ( ras
.max_ey
- ras
.min_ey
) / ras
.band_size
);
1683 if ( num_bands
== 0 ) num_bands
= 1;
1684 if ( num_bands
>= 39 ) num_bands
= 39;
1691 for ( n
= 0; n
< num_bands
; n
++, min
= max
)
1693 max
= min
+ ras
.band_size
;
1694 if ( n
== num_bands
- 1 || max
> max_y
)
1701 while ( band
>= bands
)
1703 TPos bottom
, top
, middle
;
1709 long cell_start
, cell_end
, cell_mod
;
1712 ras
.ycells
= (PCell
*)ras
.buffer
;
1713 ras
.ycount
= band
->max
- band
->min
;
1715 cell_start
= sizeof ( PCell
) * ras
.ycount
;
1716 cell_mod
= cell_start
% sizeof ( TCell
);
1718 cell_start
+= sizeof ( TCell
) - cell_mod
;
1720 cell_end
= ras
.buffer_size
;
1721 cell_end
-= cell_end
% sizeof( TCell
);
1723 cells_max
= (PCell
)( (char*)ras
.buffer
+ cell_end
);
1724 ras
.cells
= (PCell
)( (char*)ras
.buffer
+ cell_start
);
1725 if ( ras
.cells
>= cells_max
)
1728 ras
.max_cells
= cells_max
- ras
.cells
;
1729 if ( ras
.max_cells
< 2 )
1732 for ( yindex
= 0; yindex
< ras
.ycount
; yindex
++ )
1733 ras
.ycells
[yindex
] = NULL
;
1738 ras
.min_ey
= band
->min
;
1739 ras
.max_ey
= band
->max
;
1740 ras
.count_ey
= band
->max
- band
->min
;
1742 error
= gray_convert_glyph_inner( RAS_VAR
);
1746 gray_sweep( RAS_VAR_
&ras
.target
);
1750 else if ( error
!= ErrRaster_Memory_Overflow
)
1754 /* render pool overflow; we will reduce the render band by half */
1757 middle
= bottom
+ ( ( top
- bottom
) >> 1 );
1759 /* This is too complex for a single scanline; there must */
1760 /* be some problems. */
1761 if ( middle
== bottom
)
1764 fprintf( stderr
, "Rotten glyph!\n" );
1769 if ( bottom
-top
>= ras
.band_size
)
1772 band
[1].min
= bottom
;
1773 band
[1].max
= middle
;
1774 band
[0].min
= middle
;
1780 if ( ras
.band_shoot
> 8 && ras
.band_size
> 16 )
1781 ras
.band_size
= ras
.band_size
/ 2;
1788 gray_raster_render( PRaster raster
,
1789 const FT_Raster_Params
* params
)
1791 const FT_Outline
* outline
= (const FT_Outline
*)params
->source
;
1792 const FT_Bitmap
* target_map
= params
->target
;
1796 if ( !raster
|| !raster
->buffer
|| !raster
->buffer_size
)
1797 return ErrRaster_Invalid_Argument
;
1799 /* return immediately if the outline is empty */
1800 if ( outline
->n_points
== 0 || outline
->n_contours
<= 0 )
1803 if ( !outline
|| !outline
->contours
|| !outline
->points
)
1804 return ErrRaster_Invalid_Outline
;
1806 if ( outline
->n_points
!=
1807 outline
->contours
[outline
->n_contours
- 1] + 1 )
1808 return ErrRaster_Invalid_Outline
;
1810 worker
= raster
->worker
;
1812 /* if direct mode is not set, we must have a target bitmap */
1813 if ( ( params
->flags
& FT_RASTER_FLAG_DIRECT
) == 0 )
1816 return ErrRaster_Invalid_Argument
;
1819 if ( !target_map
->width
|| !target_map
->rows
)
1822 if ( !target_map
->buffer
)
1823 return ErrRaster_Invalid_Argument
;
1826 /* this version does not support monochrome rendering */
1827 if ( !( params
->flags
& FT_RASTER_FLAG_AA
) )
1828 return ErrRaster_Invalid_Mode
;
1830 /* compute clipping box */
1831 if ( ( params
->flags
& FT_RASTER_FLAG_DIRECT
) == 0 )
1833 /* compute clip box from target pixmap */
1834 ras
.clip_box
.xMin
= 0;
1835 ras
.clip_box
.yMin
= 0;
1836 ras
.clip_box
.xMax
= target_map
->width
;
1837 ras
.clip_box
.yMax
= target_map
->rows
;
1839 else if ( params
->flags
& FT_RASTER_FLAG_CLIP
)
1841 ras
.clip_box
= params
->clip_box
;
1845 ras
.clip_box
.xMin
= -32768L;
1846 ras
.clip_box
.yMin
= -32768L;
1847 ras
.clip_box
.xMax
= 32767L;
1848 ras
.clip_box
.yMax
= 32767L;
1851 gray_init_cells( worker
, raster
->buffer
, raster
->buffer_size
);
1853 ras
.outline
= *outline
;
1856 ras
.band_size
= raster
->band_size
;
1857 ras
.num_gray_spans
= 0;
1860 ras
.target
= *target_map
;
1862 ras
.render_span
= (FT_Raster_Span_Func
)gray_render_span
;
1863 ras
.render_span_data
= &ras
;
1865 if ( params
->flags
& FT_RASTER_FLAG_DIRECT
)
1867 ras
.render_span
= (FT_Raster_Span_Func
)params
->gray_spans
;
1868 ras
.render_span_data
= params
->user
;
1871 return gray_convert_glyph( worker
);
1875 /**** RASTER OBJECT CREATION: In standalone mode, we simply use *****/
1876 /**** a static object. *****/
1881 gray_raster_new( void* memory
,
1882 FT_Raster
* araster
)
1884 static TRaster the_raster
;
1886 FT_UNUSED( memory
);
1889 *araster
= (FT_Raster
)&the_raster
;
1890 FT_MEM_ZERO( &the_raster
, sizeof ( the_raster
) );
1897 gray_raster_done( FT_Raster raster
)
1900 FT_UNUSED( raster
);
1903 #else /* _STANDALONE_ */
1906 gray_raster_new( FT_Memory memory
,
1907 FT_Raster
* araster
)
1914 if ( !FT_ALLOC( raster
, sizeof ( TRaster
) ) )
1916 raster
->memory
= memory
;
1917 *araster
= (FT_Raster
)raster
;
1925 gray_raster_done( FT_Raster raster
)
1927 FT_Memory memory
= (FT_Memory
)((PRaster
)raster
)->memory
;
1933 #endif /* _STANDALONE_ */
1937 gray_raster_reset( FT_Raster raster
,
1941 PRaster rast
= (PRaster
)raster
;
1946 if ( pool_base
&& pool_size
>= (long)sizeof ( TWorker
) + 2048 )
1948 PWorker worker
= (PWorker
)pool_base
;
1951 rast
->worker
= worker
;
1952 rast
->buffer
= pool_base
+
1953 ( ( sizeof ( TWorker
) + sizeof ( TCell
) - 1 ) &
1954 ~( sizeof ( TCell
) - 1 ) );
1955 rast
->buffer_size
= (long)( ( pool_base
+ pool_size
) -
1956 (char*)rast
->buffer
) &
1957 ~( sizeof ( TCell
) - 1 );
1958 rast
->band_size
= (int)( rast
->buffer_size
/
1959 ( sizeof ( TCell
) * 8 ) );
1963 rast
->buffer
= NULL
;
1964 rast
->buffer_size
= 0;
1965 rast
->worker
= NULL
;
1971 const FT_Raster_Funcs ft_grays_raster
=
1973 FT_GLYPH_FORMAT_OUTLINE
,
1975 (FT_Raster_New_Func
) gray_raster_new
,
1976 (FT_Raster_Reset_Func
) gray_raster_reset
,
1977 (FT_Raster_Set_Mode_Func
)0,
1978 (FT_Raster_Render_Func
) gray_raster_render
,
1979 (FT_Raster_Done_Func
) gray_raster_done