1 /****************************************************************************
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the QtGui module of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 /***************************************************************************/
44 /* qgrayraster.c, derived from ftgrays.c */
46 /* A new `perfect' anti-aliasing renderer (body). */
48 /* Copyright 2000-2001, 2002, 2003 by */
49 /* David Turner, Robert Wilhelm, and Werner Lemberg. */
51 /* This file is part of the FreeType project, and may only be used, */
52 /* modified, and distributed under the terms of the FreeType project */
53 /* license, ../../3rdparty/freetype/docs/FTL.TXT. By continuing to use, */
54 /* modify, or distribute this file you indicate that you have read */
55 /* the license and understand and accept it fully. */
57 /***************************************************************************/
59 /*************************************************************************/
61 /* This file can be compiled without the rest of the FreeType engine, by */
62 /* defining the _STANDALONE_ macro when compiling it. You also need to */
63 /* put the files `ftgrays.h' and `ftimage.h' into the current */
64 /* compilation directory. Typically, you could do something like */
66 /* - copy `src/smooth/ftgrays.c' (this file) to your current directory */
68 /* - copy `include/freetype/ftimage.h' and `src/smooth/ftgrays.h' to the */
71 /* - compile `ftgrays' with the _STANDALONE_ macro defined, as in */
73 /* cc -c -D_STANDALONE_ ftgrays.c */
75 /* The renderer can be initialized with a call to */
76 /* `qt_ft_gray_raster.raster_new'; an anti-aliased bitmap can be generated */
77 /* with a call to `qt_ft_gray_raster.raster_render'. */
79 /* See the comments and documentation in the file `ftimage.h' for more */
80 /* details on how the raster works. */
82 /*************************************************************************/
84 /*************************************************************************/
86 /* This is a new anti-aliasing scan-converter for FreeType 2. The */
87 /* algorithm used here is _very_ different from the one in the standard */
88 /* `ftraster' module. Actually, `ftgrays' computes the _exact_ */
89 /* coverage of the outline on each pixel cell. */
91 /* It is based on ideas that I initially found in Raph Levien's */
92 /* excellent LibArt graphics library (see http://www.levien.com/libart */
93 /* for more information, though the web pages do not tell anything */
94 /* about the renderer; you'll have to dive into the source code to */
95 /* understand how it works). */
97 /* Note, however, that this is a _very_ different implementation */
98 /* compared to Raph's. Coverage information is stored in a very */
99 /* different way, and I don't use sorted vector paths. Also, it doesn't */
100 /* use floating point values. */
102 /* This renderer has the following advantages: */
104 /* - It doesn't need an intermediate bitmap. Instead, one can supply a */
105 /* callback function that will be called by the renderer to draw gray */
106 /* spans on any target surface. You can thus do direct composition on */
107 /* any kind of bitmap, provided that you give the renderer the right */
110 /* - A perfect anti-aliaser, i.e., it computes the _exact_ coverage on */
111 /* each pixel cell. */
113 /* - It performs a single pass on the outline (the `standard' FT2 */
114 /* renderer makes two passes). */
116 /* - It can easily be modified to render to _any_ number of gray levels */
119 /* - For small (< 20) pixel sizes, it is faster than the standard */
122 /*************************************************************************/
124 /* experimental support for gamma correction within the rasterizer */
125 #define xxxGRAYS_USE_GAMMA
128 /*************************************************************************/
130 /* The macro QT_FT_COMPONENT is used in trace mode. It is an implicit */
131 /* parameter of the QT_FT_TRACE() and QT_FT_ERROR() macros, used to print/log */
132 /* messages during execution. */
134 #undef QT_FT_COMPONENT
135 #define QT_FT_COMPONENT trace_smooth
138 #define ErrRaster_MemoryOverflow -4
141 # include <vxWorksCommon.h> /* needed for setjmp.h */
143 #include <string.h> /* for qt_ft_memcpy() */
147 #define QT_FT_UINT_MAX UINT_MAX
149 #define qt_ft_memset memset
151 #define qt_ft_setjmp setjmp
152 #define qt_ft_longjmp longjmp
153 #define qt_ft_jmp_buf jmp_buf
155 #define ErrRaster_Invalid_Mode -2
156 #define ErrRaster_Invalid_Outline -1
157 #define ErrRaster_Invalid_Argument -3
158 #define ErrRaster_Memory_Overflow -4
159 #define ErrRaster_OutOfMemory -6
161 #define QT_FT_BEGIN_HEADER
162 #define QT_FT_END_HEADER
164 #include <private/qrasterdefs_p.h>
165 #include <private/qgrayraster_p.h>
170 /* This macro is used to indicate that a function parameter is unused. */
171 /* Its purpose is simply to reduce compiler warnings. Note also that */
172 /* simply defining it as `(void)x' doesn't avoid warnings with certain */
173 /* ANSI compilers (e.g. LCC). */
174 #define QT_FT_UNUSED( x ) (x) = (x)
176 /* Disable the tracing mechanism for simplicity -- developers can */
177 /* activate it easily by redefining these two macros. */
179 #define QT_FT_ERROR( x ) do ; while ( 0 ) /* nothing */
183 #define QT_FT_TRACE( x ) do ; while ( 0 ) /* nothing */
186 #ifndef QT_FT_MEM_SET
187 #define QT_FT_MEM_SET( d, s, c ) qt_ft_memset( d, s, c )
190 #ifndef QT_FT_MEM_ZERO
191 #define QT_FT_MEM_ZERO( dest, count ) QT_FT_MEM_SET( dest, 0, count )
194 /* define this to dump debugging information */
195 #define xxxDEBUG_GRAYS
198 #define RAS_ARG PWorker worker
199 #define RAS_ARG_ PWorker worker,
201 #define RAS_VAR worker
202 #define RAS_VAR_ worker,
204 #define ras (*worker)
207 /* must be at least 6 bits! */
210 #define ONE_PIXEL ( 1L << PIXEL_BITS )
211 #define PIXEL_MASK ( -1L << PIXEL_BITS )
212 #define TRUNC( x ) ( (TCoord)( (x) >> PIXEL_BITS ) )
213 #define SUBPIXELS( x ) ( (TPos)(x) << PIXEL_BITS )
214 #define FLOOR( x ) ( (x) & -ONE_PIXEL )
215 #define CEILING( x ) ( ( (x) + ONE_PIXEL - 1 ) & -ONE_PIXEL )
216 #define ROUND( x ) ( ( (x) + ONE_PIXEL / 2 ) & -ONE_PIXEL )
219 #define UPSCALE( x ) ( (x) << ( PIXEL_BITS - 6 ) )
220 #define DOWNSCALE( x ) ( (x) >> ( PIXEL_BITS - 6 ) )
222 #define UPSCALE( x ) ( (x) >> ( 6 - PIXEL_BITS ) )
223 #define DOWNSCALE( x ) ( (x) << ( 6 - PIXEL_BITS ) )
226 /*************************************************************************/
228 /* TYPE DEFINITIONS */
231 /* don't change the following types to QT_FT_Int or QT_FT_Pos, since we might */
232 /* need to define them to "float" or "double" when experimenting with */
235 typedef int TCoord; /* integer scanline/pixel coordinate */
236 typedef int TPos; /* sub-pixel coordinate */
238 /* determine the type used to store cell areas. This normally takes at */
239 /* least PIXEL_BITS*2 + 1 bits. On 16-bit systems, we need to use */
240 /* `long' instead of `int', otherwise bad things happen */
246 #else /* PIXEL_BITS >= 8 */
248 /* approximately determine the size of integers using an ANSI-C header */
249 #if QT_FT_UINT_MAX == 0xFFFFU
255 #endif /* PIXEL_BITS >= 8 */
258 /* maximal number of gray spans in a call to the span callback */
259 #define QT_FT_MAX_GRAY_SPANS 256
262 typedef struct TCell_* PCell;
264 typedef struct TCell_
274 typedef struct TWorker_
279 TPos count_ex, count_ey;
294 QT_FT_Vector bez_stack[32 * 3 + 1];
297 QT_FT_Outline outline;
301 QT_FT_Span gray_spans[QT_FT_MAX_GRAY_SPANS];
304 QT_FT_Raster_Span_Func render_span;
305 void* render_span_data;
312 qt_ft_jmp_buf jump_buffer;
324 typedef struct TRaster_
328 long buffer_allocated_size;
335 int q_gray_rendered_spans(TRaster *raster)
337 if ( raster && raster->worker )
338 return raster->worker->skip_spans > 0 ? 0 : -raster->worker->skip_spans;
342 /*************************************************************************/
344 /* Initialize the cells table. */
347 gray_init_cells( RAS_ARG_ void* buffer,
351 ras.buffer_size = byte_size;
353 ras.ycells = (PCell*) buffer;
363 /*************************************************************************/
365 /* Compute the outline bounding box. */
368 gray_compute_cbox( RAS_ARG )
370 QT_FT_Outline* outline = &ras.outline;
371 QT_FT_Vector* vec = outline->points;
372 QT_FT_Vector* limit = vec + outline->n_points;
375 if ( outline->n_points <= 0 )
377 ras.min_ex = ras.max_ex = 0;
378 ras.min_ey = ras.max_ey = 0;
382 ras.min_ex = ras.max_ex = vec->x;
383 ras.min_ey = ras.max_ey = vec->y;
387 for ( ; vec < limit; vec++ )
393 if ( x < ras.min_ex ) ras.min_ex = x;
394 if ( x > ras.max_ex ) ras.max_ex = x;
395 if ( y < ras.min_ey ) ras.min_ey = y;
396 if ( y > ras.max_ey ) ras.max_ey = y;
399 /* truncate the bounding box to integer pixels */
400 ras.min_ex = ras.min_ex >> 6;
401 ras.min_ey = ras.min_ey >> 6;
402 ras.max_ex = ( ras.max_ex + 63 ) >> 6;
403 ras.max_ey = ( ras.max_ey + 63 ) >> 6;
407 /*************************************************************************/
409 /* Record the current cell in the table. */
412 gray_record_cell( RAS_ARG )
417 if ( ras.invalid || !( ras.area | ras.cover ) )
420 if ( x > ras.max_ex )
423 pcell = &ras.ycells[ras.ey];
428 if ( cell == NULL || cell->x > x )
431 if ( cell->x == x ) {
432 cell->area += ras.area;
433 cell->cover += ras.cover;
440 if ( ras.num_cells >= ras.max_cells )
441 qt_ft_longjmp( ras.jump_buffer, 1 );
443 cell = ras.cells + ras.num_cells++;
445 cell->area = ras.area;
446 cell->cover = ras.cover;
453 /*************************************************************************/
455 /* Set the current cell to a new position. */
458 gray_set_cell( RAS_ARG_ TCoord ex,
461 /* Move the cell pointer to a new position. We set the `invalid' */
462 /* flag to indicate that the cell isn't part of those we're interested */
463 /* in during the render phase. This means that: */
465 /* . the new vertical position must be within min_ey..max_ey-1. */
466 /* . the new horizontal position must be strictly less than max_ex */
468 /* Note that if a cell is to the left of the clipping region, it is */
469 /* actually set to the (min_ex-1) horizontal position. */
471 /* All cells that are on the left of the clipping region go to the */
472 /* min_ex - 1 horizontal position. */
475 if ( ex > ras.max_ex )
482 /* are we moving to a different cell ? */
483 if ( ex != ras.ex || ey != ras.ey )
485 /* record the current one if it is valid */
487 gray_record_cell( RAS_VAR );
495 ras.invalid = ( (unsigned)ey >= (unsigned)ras.count_ey ||
496 ex >= ras.count_ex );
500 /*************************************************************************/
502 /* Start a new contour at a given cell. */
505 gray_start_cell( RAS_ARG_ TCoord ex,
508 if ( ex > ras.max_ex )
509 ex = (TCoord)( ras.max_ex );
511 if ( ex < ras.min_ex )
512 ex = (TCoord)( ras.min_ex - 1 );
516 ras.ex = ex - ras.min_ex;
517 ras.ey = ey - ras.min_ey;
518 ras.last_ey = SUBPIXELS( ey );
521 gray_set_cell( RAS_VAR_ ex, ey );
525 /*************************************************************************/
527 /* Render a scanline as one or more cells. */
530 gray_render_scanline( RAS_ARG_ TCoord ey,
536 TCoord ex1, ex2, fx1, fx2, delta;
538 int incr, lift, mod, rem;
545 fx1 = (TCoord)( x1 - SUBPIXELS( ex1 ) );
546 fx2 = (TCoord)( x2 - SUBPIXELS( ex2 ) );
548 /* trivial case. Happens often */
551 gray_set_cell( RAS_VAR_ ex2, ey );
555 /* everything is located in a single cell. That is easy! */
560 ras.area += (TArea)( fx1 + fx2 ) * delta;
565 /* ok, we'll have to render a run of adjacent cells on the same */
568 p = ( ONE_PIXEL - fx1 ) * ( y2 - y1 );
574 p = fx1 * ( y2 - y1 );
580 delta = (TCoord)( p / dx );
581 mod = (TCoord)( p % dx );
588 ras.area += (TArea)( fx1 + first ) * delta;
592 gray_set_cell( RAS_VAR_ ex1, ey );
597 p = ONE_PIXEL * ( y2 - y1 + delta );
598 lift = (TCoord)( p / dx );
599 rem = (TCoord)( p % dx );
618 ras.area += (TArea)ONE_PIXEL * delta;
622 gray_set_cell( RAS_VAR_ ex1, ey );
627 ras.area += (TArea)( fx2 + ONE_PIXEL - first ) * delta;
632 /*************************************************************************/
634 /* Render a given line as a series of scanlines. */
637 gray_render_line( RAS_ARG_ TPos to_x,
640 TCoord ey1, ey2, fy1, fy2;
643 int delta, rem, mod, lift, incr;
646 ey1 = TRUNC( ras.last_ey );
647 ey2 = TRUNC( to_y ); /* if (ey2 >= ras.max_ey) ey2 = ras.max_ey-1; */
648 fy1 = (TCoord)( ras.y - ras.last_ey );
649 fy2 = (TCoord)( to_y - SUBPIXELS( ey2 ) );
654 /* XXX: we should do something about the trivial case where dx == 0, */
655 /* as it happens very often! */
657 /* perform vertical clipping */
669 if ( min >= ras.max_ey || max < ras.min_ey )
673 /* everything is on a single scanline */
676 gray_render_scanline( RAS_VAR_ ey1, ras.x, fy1, to_x, fy2 );
680 /* vertical line - avoid calling gray_render_scanline */
685 TCoord ex = TRUNC( ras.x );
686 TCoord two_fx = (TCoord)( ( ras.x - SUBPIXELS( ex ) ) << 1 );
697 delta = (int)( first - fy1 );
698 ras.area += (TArea)two_fx * delta;
702 gray_set_cell( &ras, ex, ey1 );
704 delta = (int)( first + first - ONE_PIXEL );
705 area = (TArea)two_fx * delta;
712 gray_set_cell( &ras, ex, ey1 );
715 delta = (int)( fy2 - ONE_PIXEL + first );
716 ras.area += (TArea)two_fx * delta;
722 /* ok, we have to render several scanlines */
723 p = ( ONE_PIXEL - fy1 ) * dx;
735 delta = (int)( p / dy );
736 mod = (int)( p % dy );
744 gray_render_scanline( RAS_VAR_ ey1, ras.x, fy1, x, (TCoord)first );
747 gray_set_cell( RAS_VAR_ TRUNC( x ), ey1 );
752 lift = (int)( p / dy );
753 rem = (int)( p % dy );
772 gray_render_scanline( RAS_VAR_ ey1, x,
773 (TCoord)( ONE_PIXEL - first ), x2,
778 gray_set_cell( RAS_VAR_ TRUNC( x ), ey1 );
782 gray_render_scanline( RAS_VAR_ ey1, x,
783 (TCoord)( ONE_PIXEL - first ), to_x,
789 ras.last_ey = SUBPIXELS( ey2 );
794 gray_split_conic( QT_FT_Vector* base )
799 base[4].x = base[2].x;
801 a = base[3].x = ( base[2].x + b ) / 2;
802 b = base[1].x = ( base[0].x + b ) / 2;
803 base[2].x = ( a + b ) / 2;
805 base[4].y = base[2].y;
807 a = base[3].y = ( base[2].y + b ) / 2;
808 b = base[1].y = ( base[0].y + b ) / 2;
809 base[2].y = ( a + b ) / 2;
814 gray_render_conic( RAS_ARG_ const QT_FT_Vector* control,
815 const QT_FT_Vector* to )
823 dx = DOWNSCALE( ras.x ) + to->x - ( control->x << 1 );
826 dy = DOWNSCALE( ras.y ) + to->y - ( control->y << 1 );
833 dx = dx / ras.conic_level;
840 /* a shortcut to speed things up */
843 /* we compute the mid-point directly in order to avoid */
844 /* calling gray_split_conic() */
845 TPos to_x, to_y, mid_x, mid_y;
848 to_x = UPSCALE( to->x );
849 to_y = UPSCALE( to->y );
850 mid_x = ( ras.x + to_x + 2 * UPSCALE( control->x ) ) / 4;
851 mid_y = ( ras.y + to_y + 2 * UPSCALE( control->y ) ) / 4;
853 gray_render_line( RAS_VAR_ mid_x, mid_y );
854 gray_render_line( RAS_VAR_ to_x, to_y );
860 levels = ras.lev_stack;
864 arc[0].x = UPSCALE( to->x );
865 arc[0].y = UPSCALE( to->y );
866 arc[1].x = UPSCALE( control->x );
867 arc[1].y = UPSCALE( control->y );
876 /* check that the arc crosses the current band */
880 min = max = arc[0].y;
883 if ( y < min ) min = y;
884 if ( y > max ) max = y;
887 if ( y < min ) min = y;
888 if ( y > max ) max = y;
890 if ( TRUNC( min ) >= ras.max_ey || TRUNC( max ) < ras.min_ey )
893 gray_split_conic( arc );
896 levels[top] = levels[top - 1] = level - 1;
902 TPos to_x, to_y, mid_x, mid_y;
907 mid_x = ( ras.x + to_x + 2 * arc[1].x ) / 4;
908 mid_y = ( ras.y + to_y + 2 * arc[1].y ) / 4;
910 gray_render_line( RAS_VAR_ mid_x, mid_y );
911 gray_render_line( RAS_VAR_ to_x, to_y );
923 gray_split_cubic( QT_FT_Vector* base )
928 base[6].x = base[3].x;
931 base[1].x = a = ( base[0].x + c ) / 2;
932 base[5].x = b = ( base[3].x + d ) / 2;
934 base[2].x = a = ( a + c ) / 2;
935 base[4].x = b = ( b + c ) / 2;
936 base[3].x = ( a + b ) / 2;
938 base[6].y = base[3].y;
941 base[1].y = a = ( base[0].y + c ) / 2;
942 base[5].y = b = ( base[3].y + d ) / 2;
944 base[2].y = a = ( a + c ) / 2;
945 base[4].y = b = ( b + c ) / 2;
946 base[3].y = ( a + b ) / 2;
951 gray_render_cubic( RAS_ARG_ const QT_FT_Vector* control1,
952 const QT_FT_Vector* control2,
953 const QT_FT_Vector* to )
961 dx = DOWNSCALE( ras.x ) + to->x - ( control1->x << 1 );
964 dy = DOWNSCALE( ras.y ) + to->y - ( control1->y << 1 );
971 dx = DOWNSCALE( ras.x ) + to->x - 3 * ( control1->x + control2->x );
974 dy = DOWNSCALE( ras.y ) + to->y - 3 * ( control1->y + control2->y );
982 da = da / ras.cubic_level;
983 db = db / ras.conic_level;
984 while ( da > 0 || db > 0 )
993 TPos to_x, to_y, mid_x, mid_y;
996 to_x = UPSCALE( to->x );
997 to_y = UPSCALE( to->y );
998 mid_x = ( ras.x + to_x +
999 3 * UPSCALE( control1->x + control2->x ) ) / 8;
1000 mid_y = ( ras.y + to_y +
1001 3 * UPSCALE( control1->y + control2->y ) ) / 8;
1003 gray_render_line( RAS_VAR_ mid_x, mid_y );
1004 gray_render_line( RAS_VAR_ to_x, to_y );
1008 arc = ras.bez_stack;
1009 arc[0].x = UPSCALE( to->x );
1010 arc[0].y = UPSCALE( to->y );
1011 arc[1].x = UPSCALE( control2->x );
1012 arc[1].y = UPSCALE( control2->y );
1013 arc[2].x = UPSCALE( control1->x );
1014 arc[2].y = UPSCALE( control1->y );
1018 levels = ras.lev_stack;
1024 level = levels[top];
1027 /* check that the arc crosses the current band */
1031 min = max = arc[0].y;
1033 if ( y < min ) min = y;
1034 if ( y > max ) max = y;
1036 if ( y < min ) min = y;
1037 if ( y > max ) max = y;
1039 if ( y < min ) min = y;
1040 if ( y > max ) max = y;
1041 if ( TRUNC( min ) >= ras.max_ey || TRUNC( max ) < 0 )
1043 gray_split_cubic( arc );
1046 levels[top] = levels[top - 1] = level - 1;
1052 TPos to_x, to_y, mid_x, mid_y;
1057 mid_x = ( ras.x + to_x + 3 * ( arc[1].x + arc[2].x ) ) / 8;
1058 mid_y = ( ras.y + to_y + 3 * ( arc[1].y + arc[2].y ) ) / 8;
1060 gray_render_line( RAS_VAR_ mid_x, mid_y );
1061 gray_render_line( RAS_VAR_ to_x, to_y );
1073 gray_move_to( const QT_FT_Vector* to,
1079 /* record current cell, if any */
1080 gray_record_cell( worker );
1082 /* start to a new position */
1083 x = UPSCALE( to->x );
1084 y = UPSCALE( to->y );
1086 gray_start_cell( worker, TRUNC( x ), TRUNC( y ) );
1095 gray_line_to( const QT_FT_Vector* to,
1098 gray_render_line( worker, UPSCALE( to->x ), UPSCALE( to->y ) );
1104 gray_conic_to( const QT_FT_Vector* control,
1105 const QT_FT_Vector* to,
1108 gray_render_conic( worker, control, to );
1114 gray_cubic_to( const QT_FT_Vector* control1,
1115 const QT_FT_Vector* control2,
1116 const QT_FT_Vector* to,
1119 gray_render_cubic( worker, control1, control2, to );
1125 gray_render_span( int count,
1126 const QT_FT_Span* spans,
1130 QT_FT_Bitmap* map = &worker->target;
1132 for ( ; count > 0; count--, spans++ )
1134 unsigned char coverage = spans->coverage;
1136 /* first of all, compute the scanline offset */
1137 p = (unsigned char*)map->buffer - spans->y * map->pitch;
1138 if ( map->pitch >= 0 )
1139 p += ( map->rows - 1 ) * map->pitch;
1144 /* For small-spans it is faster to do it by ourselves than
1145 * calling `memset'. This is mainly due to the cost of the
1148 if ( spans->len >= 8 )
1149 QT_FT_MEM_SET( p + spans->x, (unsigned char)coverage, spans->len );
1152 unsigned char* q = p + spans->x;
1155 switch ( spans->len )
1157 case 7: *q++ = (unsigned char)coverage;
1158 case 6: *q++ = (unsigned char)coverage;
1159 case 5: *q++ = (unsigned char)coverage;
1160 case 4: *q++ = (unsigned char)coverage;
1161 case 3: *q++ = (unsigned char)coverage;
1162 case 2: *q++ = (unsigned char)coverage;
1163 case 1: *q = (unsigned char)coverage;
1174 gray_hline( RAS_ARG_ TCoord x,
1184 /* compute the coverage line's coverage, depending on the */
1185 /* outline fill rule */
1187 /* the coverage percentage is area/(PIXEL_BITS*PIXEL_BITS*2) */
1189 coverage = (int)( area >> ( PIXEL_BITS * 2 + 1 - 8 ) );
1190 /* use range 0..256 */
1192 coverage = -coverage;
1194 if ( ras.outline.flags & QT_FT_OUTLINE_EVEN_ODD_FILL )
1198 if ( coverage > 256 )
1199 coverage = 512 - coverage;
1200 else if ( coverage == 256 )
1205 /* normal non-zero winding rule */
1206 if ( coverage >= 256 )
1210 y += (TCoord)ras.min_ey;
1211 x += (TCoord)ras.min_ex;
1213 /* QT_FT_Span.x is a 16-bit short, so limit our coordinates appropriately */
1219 /* see whether we can add this span to the current list */
1220 span = ras.gray_spans + ras.num_gray_spans - 1;
1221 if ( ras.num_gray_spans > 0 &&
1223 (int)span->x + span->len == (int)x &&
1224 span->coverage == coverage )
1226 span->len = (unsigned short)( span->len + acount );
1230 if ( ras.num_gray_spans >= QT_FT_MAX_GRAY_SPANS )
1232 if ( ras.render_span && ras.num_gray_spans > ras.skip_spans )
1234 skip = ras.skip_spans > 0 ? ras.skip_spans : 0;
1235 ras.render_span( ras.num_gray_spans - skip,
1236 ras.gray_spans + skip,
1237 ras.render_span_data );
1240 ras.skip_spans -= ras.num_gray_spans;
1242 /* ras.render_span( span->y, ras.gray_spans, count ); */
1251 fprintf( stderr, "y=%3d ", y );
1252 span = ras.gray_spans;
1253 for ( n = 0; n < count; n++, span++ )
1254 fprintf( stderr, "[%d..%d]:%02x ",
1255 span->x, span->x + span->len - 1, span->coverage );
1256 fprintf( stderr, "\n" );
1259 #endif /* DEBUG_GRAYS */
1261 ras.num_gray_spans = 0;
1263 span = ras.gray_spans;
1268 /* add a gray span to the current list */
1270 span->len = (unsigned short)acount;
1272 span->coverage = (unsigned char)coverage;
1274 ras.num_gray_spans++;
1281 /* to be called while in the debugger */
1282 gray_dump_cells( RAS_ARG )
1287 for ( yindex = 0; yindex < ras.ycount; yindex++ )
1292 printf( "%3d:", yindex );
1294 for ( cell = ras.ycells[yindex]; cell != NULL; cell = cell->next )
1295 printf( " (%3d, c:%4d, a:%6d)", cell->x, cell->cover, cell->area );
1300 #endif /* DEBUG_GRAYS */
1304 gray_sweep( RAS_ARG_ const QT_FT_Bitmap* target )
1308 QT_FT_UNUSED( target );
1311 if ( ras.num_cells == 0 )
1314 for ( yindex = 0; yindex < ras.ycount; yindex++ )
1316 PCell cell = ras.ycells[yindex];
1321 for ( ; cell != NULL; cell = cell->next )
1326 if ( cell->x > x && cover != 0 )
1327 gray_hline( RAS_VAR_ x, yindex, cover * ( ONE_PIXEL * 2 ),
1330 cover += cell->cover;
1331 area = cover * ( ONE_PIXEL * 2 ) - cell->area;
1333 if ( area != 0 && cell->x >= 0 )
1334 gray_hline( RAS_VAR_ cell->x, yindex, area, 1 );
1339 if ( ras.count_ex > x && cover != 0 )
1340 gray_hline( RAS_VAR_ x, yindex, cover * ( ONE_PIXEL * 2 ),
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 /* QT_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 /* user :: A typeless pointer which is passed to each */
1367 /* emitter during the decomposition. It can be */
1368 /* used to store the state during the */
1369 /* decomposition. */
1372 /* Error code. 0 means success. */
1375 int QT_FT_Outline_Decompose( const QT_FT_Outline* outline,
1379 #define SCALED( x ) (x)
1381 QT_FT_Vector v_last;
1382 QT_FT_Vector v_control;
1383 QT_FT_Vector v_start;
1385 QT_FT_Vector* point;
1386 QT_FT_Vector* limit;
1389 int n; /* index of contour in outline */
1390 int first; /* index of first point in contour */
1392 char tag; /* current point's state */
1396 for ( n = 0; n < outline->n_contours; n++ )
1398 int last; /* index of last point in contour */
1401 last = outline->contours[n];
1402 limit = outline->points + last;
1404 v_start = outline->points[first];
1405 v_last = outline->points[last];
1407 v_start.x = SCALED( v_start.x );
1408 v_start.y = SCALED( v_start.y );
1410 v_last.x = SCALED( v_last.x );
1411 v_last.y = SCALED( v_last.y );
1413 v_control = v_start;
1415 point = outline->points + first;
1416 tags = outline->tags + first;
1417 tag = QT_FT_CURVE_TAG( tags[0] );
1419 /* A contour cannot start with a cubic control point! */
1420 if ( tag == QT_FT_CURVE_TAG_CUBIC )
1421 goto Invalid_Outline;
1423 /* check first point to determine origin */
1424 if ( tag == QT_FT_CURVE_TAG_CONIC )
1426 /* first point is conic control. Yes, this happens. */
1427 if ( QT_FT_CURVE_TAG( outline->tags[last] ) == QT_FT_CURVE_TAG_ON )
1429 /* start at last point if it is on the curve */
1435 /* if both first and last points are conic, */
1436 /* start at their middle and record its position */
1438 v_start.x = ( v_start.x + v_last.x ) / 2;
1439 v_start.y = ( v_start.y + v_last.y ) / 2;
1447 error = gray_move_to( &v_start, user );
1451 while ( point < limit )
1456 tag = QT_FT_CURVE_TAG( tags[0] );
1459 case QT_FT_CURVE_TAG_ON: /* emit a single line_to */
1464 vec.x = SCALED( point->x );
1465 vec.y = SCALED( point->y );
1467 error = gray_line_to( &vec, user );
1473 case QT_FT_CURVE_TAG_CONIC: /* consume conic arcs */
1475 v_control.x = SCALED( point->x );
1476 v_control.y = SCALED( point->y );
1479 if ( point < limit )
1482 QT_FT_Vector v_middle;
1487 tag = QT_FT_CURVE_TAG( tags[0] );
1489 vec.x = SCALED( point->x );
1490 vec.y = SCALED( point->y );
1492 if ( tag == QT_FT_CURVE_TAG_ON )
1494 error = gray_conic_to( &v_control, &vec,
1501 if ( tag != QT_FT_CURVE_TAG_CONIC )
1502 goto Invalid_Outline;
1504 v_middle.x = ( v_control.x + vec.x ) / 2;
1505 v_middle.y = ( v_control.y + vec.y ) / 2;
1507 error = gray_conic_to( &v_control, &v_middle,
1516 error = gray_conic_to( &v_control, &v_start,
1521 default: /* QT_FT_CURVE_TAG_CUBIC */
1523 QT_FT_Vector vec1, vec2;
1526 if ( point + 1 > limit ||
1527 QT_FT_CURVE_TAG( tags[1] ) != QT_FT_CURVE_TAG_CUBIC )
1528 goto Invalid_Outline;
1533 vec1.x = SCALED( point[-2].x );
1534 vec1.y = SCALED( point[-2].y );
1536 vec2.x = SCALED( point[-1].x );
1537 vec2.y = SCALED( point[-1].y );
1539 if ( point <= limit )
1544 vec.x = SCALED( point->x );
1545 vec.y = SCALED( point->y );
1547 error = gray_cubic_to( &vec1, &vec2, &vec, user );
1553 error = gray_cubic_to( &vec1, &vec2, &v_start, user );
1559 /* close the contour with a line segment */
1560 error = gray_line_to( &v_start, user );
1575 return ErrRaster_Invalid_Outline;
1578 typedef struct TBand_
1586 gray_convert_glyph_inner( RAS_ARG )
1588 volatile int error = 0;
1590 if ( qt_ft_setjmp( ras.jump_buffer ) == 0 )
1592 error = QT_FT_Outline_Decompose( &ras.outline, &ras );
1593 gray_record_cell( RAS_VAR );
1597 error = ErrRaster_Memory_Overflow;
1605 gray_convert_glyph( RAS_ARG )
1608 TBand* volatile band;
1609 int volatile n, num_bands;
1610 TPos volatile min, max, max_y;
1614 ras.num_gray_spans = 0;
1616 /* Set up state in the raster object */
1617 gray_compute_cbox( RAS_VAR );
1619 /* clip to target bitmap, exit if nothing to do */
1620 clip = &ras.clip_box;
1622 if ( ras.max_ex <= clip->xMin || ras.min_ex >= clip->xMax ||
1623 ras.max_ey <= clip->yMin || ras.min_ey >= clip->yMax )
1626 if ( ras.min_ex < clip->xMin ) ras.min_ex = clip->xMin;
1627 if ( ras.min_ey < clip->yMin ) ras.min_ey = clip->yMin;
1629 if ( ras.max_ex > clip->xMax ) ras.max_ex = clip->xMax;
1630 if ( ras.max_ey > clip->yMax ) ras.max_ey = clip->yMax;
1632 ras.count_ex = ras.max_ex - ras.min_ex;
1633 ras.count_ey = ras.max_ey - ras.min_ey;
1635 /* simple heuristic used to speed-up the bezier decomposition -- see */
1636 /* the code in gray_render_conic() and gray_render_cubic() for more */
1638 ras.conic_level = 32;
1639 ras.cubic_level = 16;
1645 if ( ras.count_ex > 24 || ras.count_ey > 24 )
1647 if ( ras.count_ex > 120 || ras.count_ey > 120 )
1650 ras.conic_level <<= level;
1651 ras.cubic_level <<= level;
1654 /* setup vertical bands */
1655 num_bands = (int)( ( ras.max_ey - ras.min_ey ) / ras.band_size );
1656 if ( num_bands == 0 ) num_bands = 1;
1657 if ( num_bands >= 39 ) num_bands = 39;
1664 for ( n = 0; n < num_bands; n++, min = max )
1666 max = min + ras.band_size;
1667 if ( n == num_bands - 1 || max > max_y )
1674 while ( band >= bands )
1676 TPos bottom, top, middle;
1682 int cell_start, cell_end, cell_mod;
1685 ras.ycells = (PCell*)ras.buffer;
1686 ras.ycount = band->max - band->min;
1688 cell_start = sizeof ( PCell ) * ras.ycount;
1689 cell_mod = cell_start % sizeof ( TCell );
1691 cell_start += sizeof ( TCell ) - cell_mod;
1693 cell_end = ras.buffer_size;
1694 cell_end -= cell_end % sizeof( TCell );
1696 cells_max = (PCell)( (char*)ras.buffer + cell_end );
1697 ras.cells = (PCell)( (char*)ras.buffer + cell_start );
1698 if ( ras.cells >= cells_max )
1701 ras.max_cells = (int)(cells_max - ras.cells);
1702 if ( ras.max_cells < 2 )
1705 for ( yindex = 0; yindex < ras.ycount; yindex++ )
1706 ras.ycells[yindex] = NULL;
1711 ras.min_ey = band->min;
1712 ras.max_ey = band->max;
1713 ras.count_ey = band->max - band->min;
1715 error = gray_convert_glyph_inner( RAS_VAR );
1719 gray_sweep( RAS_VAR_ &ras.target );
1723 else if ( error != ErrRaster_Memory_Overflow )
1727 /* render pool overflow; we will reduce the render band by half */
1730 middle = bottom + ( ( top - bottom ) >> 1 );
1732 /* This is too complex for a single scanline; there must */
1733 /* be some problems. */
1734 if ( middle == bottom )
1737 fprintf( stderr, "Rotten glyph!\n" );
1739 return ErrRaster_OutOfMemory;
1742 if ( bottom-top >= ras.band_size )
1745 band[1].min = bottom;
1746 band[1].max = middle;
1747 band[0].min = middle;
1753 if ( ras.render_span && ras.num_gray_spans > ras.skip_spans )
1755 skip = ras.skip_spans > 0 ? ras.skip_spans : 0;
1756 ras.render_span( ras.num_gray_spans - skip,
1757 ras.gray_spans + skip,
1758 ras.render_span_data );
1761 ras.skip_spans -= ras.num_gray_spans;
1763 if ( ras.band_shoot > 8 && ras.band_size > 16 )
1764 ras.band_size = ras.band_size / 2;
1771 gray_raster_render( QT_FT_Raster raster,
1772 const QT_FT_Raster_Params* params )
1774 const QT_FT_Outline* outline = (const QT_FT_Outline*)params->source;
1775 const QT_FT_Bitmap* target_map = params->target;
1779 if ( !raster || !raster->buffer || !raster->buffer_size )
1780 return ErrRaster_Invalid_Argument;
1782 if ( raster->worker )
1783 raster->worker->skip_spans = params->skip_spans;
1785 // If raster object and raster buffer are allocated, but
1786 // raster size isn't of the minimum size, indicate out of
1788 if (raster->buffer_allocated_size < MINIMUM_POOL_SIZE )
1789 return ErrRaster_OutOfMemory;
1791 /* return immediately if the outline is empty */
1792 if ( outline->n_points == 0 || outline->n_contours <= 0 )
1795 if ( !outline || !outline->contours || !outline->points )
1796 return ErrRaster_Invalid_Outline;
1798 if ( outline->n_points !=
1799 outline->contours[outline->n_contours - 1] + 1 )
1800 return ErrRaster_Invalid_Outline;
1802 worker = raster->worker;
1804 /* if direct mode is not set, we must have a target bitmap */
1805 if ( ( params->flags & QT_FT_RASTER_FLAG_DIRECT ) == 0 )
1808 return ErrRaster_Invalid_Argument;
1811 if ( !target_map->width || !target_map->rows )
1814 if ( !target_map->buffer )
1815 return ErrRaster_Invalid_Argument;
1818 /* this version does not support monochrome rendering */
1819 if ( !( params->flags & QT_FT_RASTER_FLAG_AA ) )
1820 return ErrRaster_Invalid_Mode;
1822 /* compute clipping box */
1823 if ( ( params->flags & QT_FT_RASTER_FLAG_DIRECT ) == 0 )
1825 /* compute clip box from target pixmap */
1826 ras.clip_box.xMin = 0;
1827 ras.clip_box.yMin = 0;
1828 ras.clip_box.xMax = target_map->width;
1829 ras.clip_box.yMax = target_map->rows;
1831 else if ( params->flags & QT_FT_RASTER_FLAG_CLIP )
1833 ras.clip_box = params->clip_box;
1837 ras.clip_box.xMin = -32768L;
1838 ras.clip_box.yMin = -32768L;
1839 ras.clip_box.xMax = 32767L;
1840 ras.clip_box.yMax = 32767L;
1843 gray_init_cells( worker, raster->buffer, raster->buffer_size );
1845 ras.outline = *outline;
1848 ras.band_size = raster->band_size;
1851 ras.target = *target_map;
1853 ras.render_span = (QT_FT_Raster_Span_Func)gray_render_span;
1854 ras.render_span_data = &ras;
1856 if ( params->flags & QT_FT_RASTER_FLAG_DIRECT )
1858 ras.render_span = (QT_FT_Raster_Span_Func)params->gray_spans;
1859 ras.render_span_data = params->user;
1862 return gray_convert_glyph( worker );
1866 /**** RASTER OBJECT CREATION: In standalone mode, we simply use *****/
1867 /**** a static object. *****/
1870 gray_raster_new( QT_FT_Raster* araster )
1872 *araster = malloc(sizeof(TRaster));
1875 return ErrRaster_Memory_Overflow;
1877 QT_FT_MEM_ZERO(*araster, sizeof(TRaster));
1884 gray_raster_done( QT_FT_Raster raster )
1891 gray_raster_reset( QT_FT_Raster raster,
1895 PRaster rast = (PRaster)raster;
1899 if ( pool_base && ( pool_size >= MINIMUM_POOL_SIZE ) )
1901 PWorker worker = (PWorker)pool_base;
1904 rast->worker = worker;
1905 rast->buffer = pool_base +
1906 ( ( sizeof ( TWorker ) + sizeof ( TCell ) - 1 ) &
1907 ~( sizeof ( TCell ) - 1 ) );
1908 rast->buffer_size = (long)( ( pool_base + pool_size ) -
1909 (char*)rast->buffer ) &
1910 ~( sizeof ( TCell ) - 1 );
1911 rast->band_size = (int)( rast->buffer_size /
1912 ( sizeof ( TCell ) * 8 ) );
1914 else if ( pool_base)
1915 { // Case when there is a raster pool allocated, but it
1916 // doesn't have the minimum size (and so memory will be reallocated)
1917 rast->buffer = pool_base;
1918 rast->worker = NULL;
1919 rast->buffer_size = pool_size;
1923 rast->buffer = NULL;
1924 rast->buffer_size = 0;
1925 rast->worker = NULL;
1927 rast->buffer_allocated_size = pool_size;
1931 const QT_FT_Raster_Funcs qt_ft_grays_raster =
1933 QT_FT_GLYPH_FORMAT_OUTLINE,
1935 (QT_FT_Raster_New_Func) gray_raster_new,
1936 (QT_FT_Raster_Reset_Func) gray_raster_reset,
1937 (QT_FT_Raster_Set_Mode_Func)0,
1938 (QT_FT_Raster_Render_Func) gray_raster_render,
1939 (QT_FT_Raster_Done_Func) gray_raster_done