Sync with trunk r63343.
[reactos.git] / lib / 3rdparty / freetype / src / smooth / ftgrays.c
1 /***************************************************************************/
2 /* */
3 /* ftgrays.c */
4 /* */
5 /* A new `perfect' anti-aliasing renderer (body). */
6 /* */
7 /* Copyright 2000-2003, 2005-2014 by */
8 /* David Turner, Robert Wilhelm, and Werner Lemberg. */
9 /* */
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. */
15 /* */
16 /***************************************************************************/
17
18 /*************************************************************************/
19 /* */
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 */
24 /* */
25 /* - copy `src/smooth/ftgrays.c' (this file) to your current directory */
26 /* */
27 /* - copy `include/ftimage.h' and `src/smooth/ftgrays.h' to the same */
28 /* directory */
29 /* */
30 /* - compile `ftgrays' with the _STANDALONE_ macro defined, as in */
31 /* */
32 /* cc -c -D_STANDALONE_ ftgrays.c */
33 /* */
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'. */
37 /* */
38 /* See the comments and documentation in the file `ftimage.h' for more */
39 /* details on how the raster works. */
40 /* */
41 /*************************************************************************/
42
43 /*************************************************************************/
44 /* */
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. */
49 /* */
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). */
55 /* */
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. */
60 /* */
61 /* This renderer has the following advantages: */
62 /* */
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 */
67 /* callback. */
68 /* */
69 /* - A perfect anti-aliaser, i.e., it computes the _exact_ coverage on */
70 /* each pixel cell. */
71 /* */
72 /* - It performs a single pass on the outline (the `standard' FT2 */
73 /* renderer makes two passes). */
74 /* */
75 /* - It can easily be modified to render to _any_ number of gray levels */
76 /* cheaply. */
77 /* */
78 /* - For small (< 20) pixel sizes, it is faster than the standard */
79 /* renderer. */
80 /* */
81 /*************************************************************************/
82
83
84 /*************************************************************************/
85 /* */
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. */
89 /* */
90 #undef FT_COMPONENT
91 #define FT_COMPONENT trace_smooth
92
93
94 #ifdef _STANDALONE_
95
96
97 /* Auxiliary macros for token concatenation. */
98 #define FT_ERR_XCAT( x, y ) x ## y
99 #define FT_ERR_CAT( x, y ) FT_ERR_XCAT( x, y )
100
101
102 /* define this to dump debugging information */
103 /* #define FT_DEBUG_LEVEL_TRACE */
104
105
106 #ifdef FT_DEBUG_LEVEL_TRACE
107 #include <stdio.h>
108 #include <stdarg.h>
109 #endif
110
111 #include <stddef.h>
112 #include <string.h>
113 #include <setjmp.h>
114 #include <limits.h>
115 #define FT_UINT_MAX UINT_MAX
116 #define FT_INT_MAX INT_MAX
117
118 #define ft_memset memset
119
120 #define ft_setjmp setjmp
121 #define ft_longjmp longjmp
122 #define ft_jmp_buf jmp_buf
123
124 typedef ptrdiff_t FT_PtrDist;
125
126
127 #define ErrRaster_Invalid_Mode -2
128 #define ErrRaster_Invalid_Outline -1
129 #define ErrRaster_Invalid_Argument -3
130 #define ErrRaster_Memory_Overflow -4
131
132 #define FT_BEGIN_HEADER
133 #define FT_END_HEADER
134
135 #include "ftimage.h"
136 #include "ftgrays.h"
137
138
139 /* This macro is used to indicate that a function parameter is unused. */
140 /* Its purpose is simply to reduce compiler warnings. Note also that */
141 /* simply defining it as `(void)x' doesn't avoid warnings with certain */
142 /* ANSI compilers (e.g. LCC). */
143 #define FT_UNUSED( x ) (x) = (x)
144
145
146 /* we only use level 5 & 7 tracing messages; cf. ftdebug.h */
147
148 #ifdef FT_DEBUG_LEVEL_TRACE
149
150 void
151 FT_Message( const char* fmt,
152 ... )
153 {
154 va_list ap;
155
156
157 va_start( ap, fmt );
158 vfprintf( stderr, fmt, ap );
159 va_end( ap );
160 }
161
162
163 /* empty function useful for setting a breakpoint to catch errors */
164 int
165 FT_Throw( int error,
166 int line,
167 const char* file )
168 {
169 FT_UNUSED( error );
170 FT_UNUSED( line );
171 FT_UNUSED( file );
172
173 return 0;
174 }
175
176
177 /* we don't handle tracing levels in stand-alone mode; */
178 #ifndef FT_TRACE5
179 #define FT_TRACE5( varformat ) FT_Message varformat
180 #endif
181 #ifndef FT_TRACE7
182 #define FT_TRACE7( varformat ) FT_Message varformat
183 #endif
184 #ifndef FT_ERROR
185 #define FT_ERROR( varformat ) FT_Message varformat
186 #endif
187
188 #define FT_THROW( e ) \
189 ( FT_Throw( FT_ERR_CAT( ErrRaster, e ), \
190 __LINE__, \
191 __FILE__ ) | \
192 FT_ERR_CAT( ErrRaster, e ) )
193
194 #else /* !FT_DEBUG_LEVEL_TRACE */
195
196 #define FT_TRACE5( x ) do { } while ( 0 ) /* nothing */
197 #define FT_TRACE7( x ) do { } while ( 0 ) /* nothing */
198 #define FT_ERROR( x ) do { } while ( 0 ) /* nothing */
199 #define FT_THROW( e ) FT_ERR_CAT( ErrRaster_, e )
200
201
202 #endif /* !FT_DEBUG_LEVEL_TRACE */
203
204
205 #define FT_DEFINE_OUTLINE_FUNCS( class_, \
206 move_to_, line_to_, \
207 conic_to_, cubic_to_, \
208 shift_, delta_ ) \
209 static const FT_Outline_Funcs class_ = \
210 { \
211 move_to_, \
212 line_to_, \
213 conic_to_, \
214 cubic_to_, \
215 shift_, \
216 delta_ \
217 };
218
219 #define FT_DEFINE_RASTER_FUNCS( class_, glyph_format_, \
220 raster_new_, raster_reset_, \
221 raster_set_mode_, raster_render_, \
222 raster_done_ ) \
223 const FT_Raster_Funcs class_ = \
224 { \
225 glyph_format_, \
226 raster_new_, \
227 raster_reset_, \
228 raster_set_mode_, \
229 raster_render_, \
230 raster_done_ \
231 };
232
233
234 #else /* !_STANDALONE_ */
235
236
237 #include <ft2build.h>
238 #include "ftgrays.h"
239 #include FT_INTERNAL_OBJECTS_H
240 #include FT_INTERNAL_DEBUG_H
241 #include FT_OUTLINE_H
242
243 #include "ftsmerrs.h"
244
245 #include "ftspic.h"
246
247 #define Smooth_Err_Invalid_Mode Smooth_Err_Cannot_Render_Glyph
248 #define Smooth_Err_Memory_Overflow Smooth_Err_Out_Of_Memory
249 #define ErrRaster_Memory_Overflow Smooth_Err_Out_Of_Memory
250
251
252 #endif /* !_STANDALONE_ */
253
254
255 #ifndef FT_MEM_SET
256 #define FT_MEM_SET( d, s, c ) ft_memset( d, s, c )
257 #endif
258
259 #ifndef FT_MEM_ZERO
260 #define FT_MEM_ZERO( dest, count ) FT_MEM_SET( dest, 0, count )
261 #endif
262
263 /* as usual, for the speed hungry :-) */
264
265 #undef RAS_ARG
266 #undef RAS_ARG_
267 #undef RAS_VAR
268 #undef RAS_VAR_
269
270 #ifndef FT_STATIC_RASTER
271
272 #define RAS_ARG gray_PWorker worker
273 #define RAS_ARG_ gray_PWorker worker,
274
275 #define RAS_VAR worker
276 #define RAS_VAR_ worker,
277
278 #else /* FT_STATIC_RASTER */
279
280 #define RAS_ARG /* empty */
281 #define RAS_ARG_ /* empty */
282 #define RAS_VAR /* empty */
283 #define RAS_VAR_ /* empty */
284
285 #endif /* FT_STATIC_RASTER */
286
287
288 /* must be at least 6 bits! */
289 #define PIXEL_BITS 8
290
291 #undef FLOOR
292 #undef CEILING
293 #undef TRUNC
294 #undef SCALED
295
296 #define ONE_PIXEL ( 1L << PIXEL_BITS )
297 #define PIXEL_MASK ( -1L << PIXEL_BITS )
298 #define TRUNC( x ) ( (TCoord)( (x) >> PIXEL_BITS ) )
299 #define SUBPIXELS( x ) ( (TPos)(x) << PIXEL_BITS )
300 #define FLOOR( x ) ( (x) & -ONE_PIXEL )
301 #define CEILING( x ) ( ( (x) + ONE_PIXEL - 1 ) & -ONE_PIXEL )
302 #define ROUND( x ) ( ( (x) + ONE_PIXEL / 2 ) & -ONE_PIXEL )
303
304 #if PIXEL_BITS >= 6
305 #define UPSCALE( x ) ( (x) << ( PIXEL_BITS - 6 ) )
306 #define DOWNSCALE( x ) ( (x) >> ( PIXEL_BITS - 6 ) )
307 #else
308 #define UPSCALE( x ) ( (x) >> ( 6 - PIXEL_BITS ) )
309 #define DOWNSCALE( x ) ( (x) << ( 6 - PIXEL_BITS ) )
310 #endif
311
312
313 /* Compute `dividend / divisor' and return both its quotient and */
314 /* remainder, cast to a specific type. This macro also ensures that */
315 /* the remainder is always positive. */
316 #define FT_DIV_MOD( type, dividend, divisor, quotient, remainder ) \
317 FT_BEGIN_STMNT \
318 (quotient) = (type)( (dividend) / (divisor) ); \
319 (remainder) = (type)( (dividend) % (divisor) ); \
320 if ( (remainder) < 0 ) \
321 { \
322 (quotient)--; \
323 (remainder) += (type)(divisor); \
324 } \
325 FT_END_STMNT
326
327 #ifdef __arm__
328 /* Work around a bug specific to GCC which make the compiler fail to */
329 /* optimize a division and modulo operation on the same parameters */
330 /* into a single call to `__aeabi_idivmod'. See */
331 /* */
332 /* http://gcc.gnu.org/bugzilla/show_bug.cgi?id=43721 */
333 #undef FT_DIV_MOD
334 #define FT_DIV_MOD( type, dividend, divisor, quotient, remainder ) \
335 FT_BEGIN_STMNT \
336 (quotient) = (type)( (dividend) / (divisor) ); \
337 (remainder) = (type)( (dividend) - (quotient) * (divisor) ); \
338 if ( (remainder) < 0 ) \
339 { \
340 (quotient)--; \
341 (remainder) += (type)(divisor); \
342 } \
343 FT_END_STMNT
344 #endif /* __arm__ */
345
346
347 /*************************************************************************/
348 /* */
349 /* TYPE DEFINITIONS */
350 /* */
351
352 /* don't change the following types to FT_Int or FT_Pos, since we might */
353 /* need to define them to "float" or "double" when experimenting with */
354 /* new algorithms */
355
356 typedef long TCoord; /* integer scanline/pixel coordinate */
357 typedef long TPos; /* sub-pixel coordinate */
358
359 /* determine the type used to store cell areas. This normally takes at */
360 /* least PIXEL_BITS*2 + 1 bits. On 16-bit systems, we need to use */
361 /* `long' instead of `int', otherwise bad things happen */
362
363 #if PIXEL_BITS <= 7
364
365 typedef int TArea;
366
367 #else /* PIXEL_BITS >= 8 */
368
369 /* approximately determine the size of integers using an ANSI-C header */
370 #if FT_UINT_MAX == 0xFFFFU
371 typedef long TArea;
372 #else
373 typedef int TArea;
374 #endif
375
376 #endif /* PIXEL_BITS >= 8 */
377
378
379 /* maximum number of gray spans in a call to the span callback */
380 #define FT_MAX_GRAY_SPANS 32
381
382
383 typedef struct TCell_* PCell;
384
385 typedef struct TCell_
386 {
387 TPos x; /* same with gray_TWorker.ex */
388 TCoord cover; /* same with gray_TWorker.cover */
389 TArea area;
390 PCell next;
391
392 } TCell;
393
394
395 #if defined( _MSC_VER ) /* Visual C++ (and Intel C++) */
396 /* We disable the warning `structure was padded due to */
397 /* __declspec(align())' in order to compile cleanly with */
398 /* the maximum level of warnings. */
399 #pragma warning( push )
400 #pragma warning( disable : 4324 )
401 #endif /* _MSC_VER */
402
403 typedef struct gray_TWorker_
404 {
405 TCoord ex, ey;
406 TPos min_ex, max_ex;
407 TPos min_ey, max_ey;
408 TPos count_ex, count_ey;
409
410 TArea area;
411 TCoord cover;
412 int invalid;
413
414 PCell cells;
415 FT_PtrDist max_cells;
416 FT_PtrDist num_cells;
417
418 TCoord cx, cy;
419 TPos x, y;
420
421 TPos last_ey;
422
423 FT_Vector bez_stack[32 * 3 + 1];
424 int lev_stack[32];
425
426 FT_Outline outline;
427 FT_Bitmap target;
428 FT_BBox clip_box;
429
430 FT_Span gray_spans[FT_MAX_GRAY_SPANS];
431 int num_gray_spans;
432
433 FT_Raster_Span_Func render_span;
434 void* render_span_data;
435 int span_y;
436
437 int band_size;
438 int band_shoot;
439
440 ft_jmp_buf jump_buffer;
441
442 void* buffer;
443 long buffer_size;
444
445 PCell* ycells;
446 TPos ycount;
447
448 } gray_TWorker, *gray_PWorker;
449
450 #if defined( _MSC_VER )
451 #pragma warning( pop )
452 #endif
453
454
455 #ifndef FT_STATIC_RASTER
456 #define ras (*worker)
457 #else
458 static gray_TWorker ras;
459 #endif
460
461
462 typedef struct gray_TRaster_
463 {
464 void* buffer;
465 long buffer_size;
466 int band_size;
467 void* memory;
468 gray_PWorker worker;
469
470 } gray_TRaster, *gray_PRaster;
471
472
473
474 /*************************************************************************/
475 /* */
476 /* Initialize the cells table. */
477 /* */
478 static void
479 gray_init_cells( RAS_ARG_ void* buffer,
480 long byte_size )
481 {
482 ras.buffer = buffer;
483 ras.buffer_size = byte_size;
484
485 ras.ycells = (PCell*) buffer;
486 ras.cells = NULL;
487 ras.max_cells = 0;
488 ras.num_cells = 0;
489 ras.area = 0;
490 ras.cover = 0;
491 ras.invalid = 1;
492 }
493
494
495 /*************************************************************************/
496 /* */
497 /* Compute the outline bounding box. */
498 /* */
499 static void
500 gray_compute_cbox( RAS_ARG )
501 {
502 FT_Outline* outline = &ras.outline;
503 FT_Vector* vec = outline->points;
504 FT_Vector* limit = vec + outline->n_points;
505
506
507 if ( outline->n_points <= 0 )
508 {
509 ras.min_ex = ras.max_ex = 0;
510 ras.min_ey = ras.max_ey = 0;
511 return;
512 }
513
514 ras.min_ex = ras.max_ex = vec->x;
515 ras.min_ey = ras.max_ey = vec->y;
516
517 vec++;
518
519 for ( ; vec < limit; vec++ )
520 {
521 TPos x = vec->x;
522 TPos y = vec->y;
523
524
525 if ( x < ras.min_ex ) ras.min_ex = x;
526 if ( x > ras.max_ex ) ras.max_ex = x;
527 if ( y < ras.min_ey ) ras.min_ey = y;
528 if ( y > ras.max_ey ) ras.max_ey = y;
529 }
530
531 /* truncate the bounding box to integer pixels */
532 ras.min_ex = ras.min_ex >> 6;
533 ras.min_ey = ras.min_ey >> 6;
534 ras.max_ex = ( ras.max_ex + 63 ) >> 6;
535 ras.max_ey = ( ras.max_ey + 63 ) >> 6;
536 }
537
538
539 /*************************************************************************/
540 /* */
541 /* Record the current cell in the table. */
542 /* */
543 static PCell
544 gray_find_cell( RAS_ARG )
545 {
546 PCell *pcell, cell;
547 TPos x = ras.ex;
548
549
550 if ( x > ras.count_ex )
551 x = ras.count_ex;
552
553 pcell = &ras.ycells[ras.ey];
554 for (;;)
555 {
556 cell = *pcell;
557 if ( cell == NULL || cell->x > x )
558 break;
559
560 if ( cell->x == x )
561 goto Exit;
562
563 pcell = &cell->next;
564 }
565
566 if ( ras.num_cells >= ras.max_cells )
567 ft_longjmp( ras.jump_buffer, 1 );
568
569 cell = ras.cells + ras.num_cells++;
570 cell->x = x;
571 cell->area = 0;
572 cell->cover = 0;
573
574 cell->next = *pcell;
575 *pcell = cell;
576
577 Exit:
578 return cell;
579 }
580
581
582 static void
583 gray_record_cell( RAS_ARG )
584 {
585 if ( ras.area | ras.cover )
586 {
587 PCell cell = gray_find_cell( RAS_VAR );
588
589
590 cell->area += ras.area;
591 cell->cover += ras.cover;
592 }
593 }
594
595
596 /*************************************************************************/
597 /* */
598 /* Set the current cell to a new position. */
599 /* */
600 static void
601 gray_set_cell( RAS_ARG_ TCoord ex,
602 TCoord ey )
603 {
604 /* Move the cell pointer to a new position. We set the `invalid' */
605 /* flag to indicate that the cell isn't part of those we're interested */
606 /* in during the render phase. This means that: */
607 /* */
608 /* . the new vertical position must be within min_ey..max_ey-1. */
609 /* . the new horizontal position must be strictly less than max_ex */
610 /* */
611 /* Note that if a cell is to the left of the clipping region, it is */
612 /* actually set to the (min_ex-1) horizontal position. */
613
614 /* All cells that are on the left of the clipping region go to the */
615 /* min_ex - 1 horizontal position. */
616 ey -= ras.min_ey;
617
618 if ( ex > ras.max_ex )
619 ex = ras.max_ex;
620
621 ex -= ras.min_ex;
622 if ( ex < 0 )
623 ex = -1;
624
625 /* are we moving to a different cell ? */
626 if ( ex != ras.ex || ey != ras.ey )
627 {
628 /* record the current one if it is valid */
629 if ( !ras.invalid )
630 gray_record_cell( RAS_VAR );
631
632 ras.area = 0;
633 ras.cover = 0;
634 ras.ex = ex;
635 ras.ey = ey;
636 }
637
638 ras.invalid = ( (unsigned)ey >= (unsigned)ras.count_ey ||
639 ex >= ras.count_ex );
640 }
641
642
643 /*************************************************************************/
644 /* */
645 /* Start a new contour at a given cell. */
646 /* */
647 static void
648 gray_start_cell( RAS_ARG_ TCoord ex,
649 TCoord ey )
650 {
651 if ( ex > ras.max_ex )
652 ex = (TCoord)( ras.max_ex );
653
654 if ( ex < ras.min_ex )
655 ex = (TCoord)( ras.min_ex - 1 );
656
657 ras.area = 0;
658 ras.cover = 0;
659 ras.ex = ex - ras.min_ex;
660 ras.ey = ey - ras.min_ey;
661 ras.last_ey = SUBPIXELS( ey );
662 ras.invalid = 0;
663
664 gray_set_cell( RAS_VAR_ ex, ey );
665 }
666
667
668 /*************************************************************************/
669 /* */
670 /* Render a scanline as one or more cells. */
671 /* */
672 static void
673 gray_render_scanline( RAS_ARG_ TCoord ey,
674 TPos x1,
675 TCoord y1,
676 TPos x2,
677 TCoord y2 )
678 {
679 TCoord ex1, ex2, fx1, fx2, delta, mod;
680 long p, first, dx;
681 int incr;
682
683
684 dx = x2 - x1;
685
686 ex1 = TRUNC( x1 );
687 ex2 = TRUNC( x2 );
688 fx1 = (TCoord)( x1 - SUBPIXELS( ex1 ) );
689 fx2 = (TCoord)( x2 - SUBPIXELS( ex2 ) );
690
691 /* trivial case. Happens often */
692 if ( y1 == y2 )
693 {
694 gray_set_cell( RAS_VAR_ ex2, ey );
695 return;
696 }
697
698 /* everything is located in a single cell. That is easy! */
699 /* */
700 if ( ex1 == ex2 )
701 {
702 delta = y2 - y1;
703 ras.area += (TArea)(( fx1 + fx2 ) * delta);
704 ras.cover += delta;
705 return;
706 }
707
708 /* ok, we'll have to render a run of adjacent cells on the same */
709 /* scanline... */
710 /* */
711 p = ( ONE_PIXEL - fx1 ) * ( y2 - y1 );
712 first = ONE_PIXEL;
713 incr = 1;
714
715 if ( dx < 0 )
716 {
717 p = fx1 * ( y2 - y1 );
718 first = 0;
719 incr = -1;
720 dx = -dx;
721 }
722
723 FT_DIV_MOD( TCoord, p, dx, delta, mod );
724
725 ras.area += (TArea)(( fx1 + first ) * delta);
726 ras.cover += delta;
727
728 ex1 += incr;
729 gray_set_cell( RAS_VAR_ ex1, ey );
730 y1 += delta;
731
732 if ( ex1 != ex2 )
733 {
734 TCoord lift, rem;
735
736
737 p = ONE_PIXEL * ( y2 - y1 + delta );
738 FT_DIV_MOD( TCoord, p, dx, lift, rem );
739
740 mod -= (int)dx;
741
742 while ( ex1 != ex2 )
743 {
744 delta = lift;
745 mod += rem;
746 if ( mod >= 0 )
747 {
748 mod -= (TCoord)dx;
749 delta++;
750 }
751
752 ras.area += (TArea)(ONE_PIXEL * delta);
753 ras.cover += delta;
754 y1 += delta;
755 ex1 += incr;
756 gray_set_cell( RAS_VAR_ ex1, ey );
757 }
758 }
759
760 delta = y2 - y1;
761 ras.area += (TArea)(( fx2 + ONE_PIXEL - first ) * delta);
762 ras.cover += delta;
763 }
764
765
766 /*************************************************************************/
767 /* */
768 /* Render a given line as a series of scanlines. */
769 /* */
770 static void
771 gray_render_line( RAS_ARG_ TPos to_x,
772 TPos to_y )
773 {
774 TCoord ey1, ey2, fy1, fy2, mod;
775 TPos dx, dy, x, x2;
776 long p, first;
777 int delta, rem, lift, incr;
778
779
780 ey1 = TRUNC( ras.last_ey );
781 ey2 = TRUNC( to_y ); /* if (ey2 >= ras.max_ey) ey2 = ras.max_ey-1; */
782 fy1 = (TCoord)( ras.y - ras.last_ey );
783 fy2 = (TCoord)( to_y - SUBPIXELS( ey2 ) );
784
785 dx = to_x - ras.x;
786 dy = to_y - ras.y;
787
788 /* perform vertical clipping */
789 {
790 TCoord min, max;
791
792
793 min = ey1;
794 max = ey2;
795 if ( ey1 > ey2 )
796 {
797 min = ey2;
798 max = ey1;
799 }
800 if ( min >= ras.max_ey || max < ras.min_ey )
801 goto End;
802 }
803
804 /* everything is on a single scanline */
805 if ( ey1 == ey2 )
806 {
807 gray_render_scanline( RAS_VAR_ ey1, ras.x, fy1, to_x, fy2 );
808 goto End;
809 }
810
811 /* vertical line - avoid calling gray_render_scanline */
812 incr = 1;
813
814 if ( dx == 0 )
815 {
816 TCoord ex = TRUNC( ras.x );
817 TCoord two_fx = (TCoord)( ( ras.x - SUBPIXELS( ex ) ) << 1 );
818 TArea area;
819
820
821 first = ONE_PIXEL;
822 if ( dy < 0 )
823 {
824 first = 0;
825 incr = -1;
826 }
827
828 delta = (int)( first - fy1 );
829 ras.area += (TArea)two_fx * delta;
830 ras.cover += delta;
831 ey1 += incr;
832
833 gray_set_cell( RAS_VAR_ ex, ey1 );
834
835 delta = (int)( first + first - ONE_PIXEL );
836 area = (TArea)two_fx * delta;
837 while ( ey1 != ey2 )
838 {
839 ras.area += area;
840 ras.cover += delta;
841 ey1 += incr;
842
843 gray_set_cell( RAS_VAR_ ex, ey1 );
844 }
845
846 delta = (int)( fy2 - ONE_PIXEL + first );
847 ras.area += (TArea)two_fx * delta;
848 ras.cover += delta;
849
850 goto End;
851 }
852
853 /* ok, we have to render several scanlines */
854 p = ( ONE_PIXEL - fy1 ) * dx;
855 first = ONE_PIXEL;
856 incr = 1;
857
858 if ( dy < 0 )
859 {
860 p = fy1 * dx;
861 first = 0;
862 incr = -1;
863 dy = -dy;
864 }
865
866 FT_DIV_MOD( int, p, dy, delta, mod );
867
868 x = ras.x + delta;
869 gray_render_scanline( RAS_VAR_ ey1, ras.x, fy1, x, (TCoord)first );
870
871 ey1 += incr;
872 gray_set_cell( RAS_VAR_ TRUNC( x ), ey1 );
873
874 if ( ey1 != ey2 )
875 {
876 p = ONE_PIXEL * dx;
877 FT_DIV_MOD( int, p, dy, lift, rem );
878 mod -= (int)dy;
879
880 while ( ey1 != ey2 )
881 {
882 delta = lift;
883 mod += rem;
884 if ( mod >= 0 )
885 {
886 mod -= (int)dy;
887 delta++;
888 }
889
890 x2 = x + delta;
891 gray_render_scanline( RAS_VAR_ ey1, x,
892 (TCoord)( ONE_PIXEL - first ), x2,
893 (TCoord)first );
894 x = x2;
895
896 ey1 += incr;
897 gray_set_cell( RAS_VAR_ TRUNC( x ), ey1 );
898 }
899 }
900
901 gray_render_scanline( RAS_VAR_ ey1, x,
902 (TCoord)( ONE_PIXEL - first ), to_x,
903 fy2 );
904
905 End:
906 ras.x = to_x;
907 ras.y = to_y;
908 ras.last_ey = SUBPIXELS( ey2 );
909 }
910
911
912 static void
913 gray_split_conic( FT_Vector* base )
914 {
915 TPos a, b;
916
917
918 base[4].x = base[2].x;
919 b = base[1].x;
920 a = base[3].x = ( base[2].x + b ) / 2;
921 b = base[1].x = ( base[0].x + b ) / 2;
922 base[2].x = ( a + b ) / 2;
923
924 base[4].y = base[2].y;
925 b = base[1].y;
926 a = base[3].y = ( base[2].y + b ) / 2;
927 b = base[1].y = ( base[0].y + b ) / 2;
928 base[2].y = ( a + b ) / 2;
929 }
930
931
932 static void
933 gray_render_conic( RAS_ARG_ const FT_Vector* control,
934 const FT_Vector* to )
935 {
936 TPos dx, dy;
937 TPos min, max, y;
938 int top, level;
939 int* levels;
940 FT_Vector* arc;
941
942
943 levels = ras.lev_stack;
944
945 arc = ras.bez_stack;
946 arc[0].x = UPSCALE( to->x );
947 arc[0].y = UPSCALE( to->y );
948 arc[1].x = UPSCALE( control->x );
949 arc[1].y = UPSCALE( control->y );
950 arc[2].x = ras.x;
951 arc[2].y = ras.y;
952 top = 0;
953
954 dx = FT_ABS( arc[2].x + arc[0].x - 2 * arc[1].x );
955 dy = FT_ABS( arc[2].y + arc[0].y - 2 * arc[1].y );
956 if ( dx < dy )
957 dx = dy;
958
959 if ( dx < ONE_PIXEL / 4 )
960 goto Draw;
961
962 /* short-cut the arc that crosses the current band */
963 min = max = arc[0].y;
964
965 y = arc[1].y;
966 if ( y < min ) min = y;
967 if ( y > max ) max = y;
968
969 y = arc[2].y;
970 if ( y < min ) min = y;
971 if ( y > max ) max = y;
972
973 if ( TRUNC( min ) >= ras.max_ey || TRUNC( max ) < ras.min_ey )
974 goto Draw;
975
976 level = 0;
977 do
978 {
979 dx >>= 2;
980 level++;
981 } while ( dx > ONE_PIXEL / 4 );
982
983 levels[0] = level;
984
985 do
986 {
987 level = levels[top];
988 if ( level > 0 )
989 {
990 gray_split_conic( arc );
991 arc += 2;
992 top++;
993 levels[top] = levels[top - 1] = level - 1;
994 continue;
995 }
996
997 Draw:
998 gray_render_line( RAS_VAR_ arc[0].x, arc[0].y );
999 top--;
1000 arc -= 2;
1001
1002 } while ( top >= 0 );
1003 }
1004
1005
1006 static void
1007 gray_split_cubic( FT_Vector* base )
1008 {
1009 TPos a, b, c, d;
1010
1011
1012 base[6].x = base[3].x;
1013 c = base[1].x;
1014 d = base[2].x;
1015 base[1].x = a = ( base[0].x + c ) / 2;
1016 base[5].x = b = ( base[3].x + d ) / 2;
1017 c = ( c + d ) / 2;
1018 base[2].x = a = ( a + c ) / 2;
1019 base[4].x = b = ( b + c ) / 2;
1020 base[3].x = ( a + b ) / 2;
1021
1022 base[6].y = base[3].y;
1023 c = base[1].y;
1024 d = base[2].y;
1025 base[1].y = a = ( base[0].y + c ) / 2;
1026 base[5].y = b = ( base[3].y + d ) / 2;
1027 c = ( c + d ) / 2;
1028 base[2].y = a = ( a + c ) / 2;
1029 base[4].y = b = ( b + c ) / 2;
1030 base[3].y = ( a + b ) / 2;
1031 }
1032
1033
1034 static void
1035 gray_render_cubic( RAS_ARG_ const FT_Vector* control1,
1036 const FT_Vector* control2,
1037 const FT_Vector* to )
1038 {
1039 FT_Vector* arc;
1040 TPos min, max, y;
1041
1042
1043 arc = ras.bez_stack;
1044 arc[0].x = UPSCALE( to->x );
1045 arc[0].y = UPSCALE( to->y );
1046 arc[1].x = UPSCALE( control2->x );
1047 arc[1].y = UPSCALE( control2->y );
1048 arc[2].x = UPSCALE( control1->x );
1049 arc[2].y = UPSCALE( control1->y );
1050 arc[3].x = ras.x;
1051 arc[3].y = ras.y;
1052
1053 /* Short-cut the arc that crosses the current band. */
1054 min = max = arc[0].y;
1055
1056 y = arc[1].y;
1057 if ( y < min )
1058 min = y;
1059 if ( y > max )
1060 max = y;
1061
1062 y = arc[2].y;
1063 if ( y < min )
1064 min = y;
1065 if ( y > max )
1066 max = y;
1067
1068 y = arc[3].y;
1069 if ( y < min )
1070 min = y;
1071 if ( y > max )
1072 max = y;
1073
1074 if ( TRUNC( min ) >= ras.max_ey || TRUNC( max ) < ras.min_ey )
1075 goto Draw;
1076
1077 for (;;)
1078 {
1079 /* Decide whether to split or draw. See `Rapid Termination */
1080 /* Evaluation for Recursive Subdivision of Bezier Curves' by Thomas */
1081 /* F. Hain, at */
1082 /* http://www.cis.southalabama.edu/~hain/general/Publications/Bezier/Camera-ready%20CISST02%202.pdf */
1083
1084 {
1085 TPos dx, dy, dx_, dy_;
1086 TPos dx1, dy1, dx2, dy2;
1087 TPos L, s, s_limit;
1088
1089
1090 /* dx and dy are x and y components of the P0-P3 chord vector. */
1091 dx = arc[3].x - arc[0].x;
1092 dy = arc[3].y - arc[0].y;
1093
1094 /* L is an (under)estimate of the Euclidean distance P0-P3. */
1095 /* */
1096 /* If dx >= dy, then r = sqrt(dx^2 + dy^2) can be overestimated */
1097 /* with least maximum error by */
1098 /* */
1099 /* r_upperbound = dx + (sqrt(2) - 1) * dy , */
1100 /* */
1101 /* where sqrt(2) - 1 can be (over)estimated by 107/256, giving an */
1102 /* error of no more than 8.4%. */
1103 /* */
1104 /* Similarly, some elementary calculus shows that r can be */
1105 /* underestimated with least maximum error by */
1106 /* */
1107 /* r_lowerbound = sqrt(2 + sqrt(2)) / 2 * dx */
1108 /* + sqrt(2 - sqrt(2)) / 2 * dy . */
1109 /* */
1110 /* 236/256 and 97/256 are (under)estimates of the two algebraic */
1111 /* numbers, giving an error of no more than 8.1%. */
1112
1113 dx_ = FT_ABS( dx );
1114 dy_ = FT_ABS( dy );
1115
1116 /* This is the same as */
1117 /* */
1118 /* L = ( 236 * FT_MAX( dx_, dy_ ) */
1119 /* + 97 * FT_MIN( dx_, dy_ ) ) >> 8; */
1120 L = ( dx_ > dy_ ? 236 * dx_ + 97 * dy_
1121 : 97 * dx_ + 236 * dy_ ) >> 8;
1122
1123 /* Avoid possible arithmetic overflow below by splitting. */
1124 if ( L > 32767 )
1125 goto Split;
1126
1127 /* Max deviation may be as much as (s/L) * 3/4 (if Hain's v = 1). */
1128 s_limit = L * (TPos)( ONE_PIXEL / 6 );
1129
1130 /* s is L * the perpendicular distance from P1 to the line P0-P3. */
1131 dx1 = arc[1].x - arc[0].x;
1132 dy1 = arc[1].y - arc[0].y;
1133 s = FT_ABS( dy * dx1 - dx * dy1 );
1134
1135 if ( s > s_limit )
1136 goto Split;
1137
1138 /* s is L * the perpendicular distance from P2 to the line P0-P3. */
1139 dx2 = arc[2].x - arc[0].x;
1140 dy2 = arc[2].y - arc[0].y;
1141 s = FT_ABS( dy * dx2 - dx * dy2 );
1142
1143 if ( s > s_limit )
1144 goto Split;
1145
1146 /* Split super curvy segments where the off points are so far
1147 from the chord that the angles P0-P1-P3 or P0-P2-P3 become
1148 acute as detected by appropriate dot products. */
1149 if ( dx1 * ( dx1 - dx ) + dy1 * ( dy1 - dy ) > 0 ||
1150 dx2 * ( dx2 - dx ) + dy2 * ( dy2 - dy ) > 0 )
1151 goto Split;
1152
1153 /* No reason to split. */
1154 goto Draw;
1155 }
1156
1157 Split:
1158 gray_split_cubic( arc );
1159 arc += 3;
1160 continue;
1161
1162 Draw:
1163 gray_render_line( RAS_VAR_ arc[0].x, arc[0].y );
1164
1165 if ( arc == ras.bez_stack )
1166 return;
1167
1168 arc -= 3;
1169 }
1170 }
1171
1172
1173 static int
1174 gray_move_to( const FT_Vector* to,
1175 gray_PWorker worker )
1176 {
1177 TPos x, y;
1178
1179
1180 /* record current cell, if any */
1181 if ( !ras.invalid )
1182 gray_record_cell( RAS_VAR );
1183
1184 /* start to a new position */
1185 x = UPSCALE( to->x );
1186 y = UPSCALE( to->y );
1187
1188 gray_start_cell( RAS_VAR_ TRUNC( x ), TRUNC( y ) );
1189
1190 worker->x = x;
1191 worker->y = y;
1192 return 0;
1193 }
1194
1195
1196 static int
1197 gray_line_to( const FT_Vector* to,
1198 gray_PWorker worker )
1199 {
1200 gray_render_line( RAS_VAR_ UPSCALE( to->x ), UPSCALE( to->y ) );
1201 return 0;
1202 }
1203
1204
1205 static int
1206 gray_conic_to( const FT_Vector* control,
1207 const FT_Vector* to,
1208 gray_PWorker worker )
1209 {
1210 gray_render_conic( RAS_VAR_ control, to );
1211 return 0;
1212 }
1213
1214
1215 static int
1216 gray_cubic_to( const FT_Vector* control1,
1217 const FT_Vector* control2,
1218 const FT_Vector* to,
1219 gray_PWorker worker )
1220 {
1221 gray_render_cubic( RAS_VAR_ control1, control2, to );
1222 return 0;
1223 }
1224
1225
1226 static void
1227 gray_render_span( int y,
1228 int count,
1229 const FT_Span* spans,
1230 gray_PWorker worker )
1231 {
1232 unsigned char* p;
1233 FT_Bitmap* map = &worker->target;
1234
1235
1236 /* first of all, compute the scanline offset */
1237 p = (unsigned char*)map->buffer - y * map->pitch;
1238 if ( map->pitch >= 0 )
1239 p += (unsigned)( ( map->rows - 1 ) * map->pitch );
1240
1241 for ( ; count > 0; count--, spans++ )
1242 {
1243 unsigned char coverage = spans->coverage;
1244
1245
1246 if ( coverage )
1247 {
1248 /* For small-spans it is faster to do it by ourselves than
1249 * calling `memset'. This is mainly due to the cost of the
1250 * function call.
1251 */
1252 if ( spans->len >= 8 )
1253 FT_MEM_SET( p + spans->x, (unsigned char)coverage, spans->len );
1254 else
1255 {
1256 unsigned char* q = p + spans->x;
1257
1258
1259 switch ( spans->len )
1260 {
1261 case 7: *q++ = (unsigned char)coverage;
1262 case 6: *q++ = (unsigned char)coverage;
1263 case 5: *q++ = (unsigned char)coverage;
1264 case 4: *q++ = (unsigned char)coverage;
1265 case 3: *q++ = (unsigned char)coverage;
1266 case 2: *q++ = (unsigned char)coverage;
1267 case 1: *q = (unsigned char)coverage;
1268 default:
1269 ;
1270 }
1271 }
1272 }
1273 }
1274 }
1275
1276
1277 static void
1278 gray_hline( RAS_ARG_ TCoord x,
1279 TCoord y,
1280 TPos area,
1281 TCoord acount )
1282 {
1283 int coverage;
1284
1285
1286 /* compute the coverage line's coverage, depending on the */
1287 /* outline fill rule */
1288 /* */
1289 /* the coverage percentage is area/(PIXEL_BITS*PIXEL_BITS*2) */
1290 /* */
1291 coverage = (int)( area >> ( PIXEL_BITS * 2 + 1 - 8 ) );
1292 /* use range 0..256 */
1293 if ( coverage < 0 )
1294 coverage = -coverage;
1295
1296 if ( ras.outline.flags & FT_OUTLINE_EVEN_ODD_FILL )
1297 {
1298 coverage &= 511;
1299
1300 if ( coverage > 256 )
1301 coverage = 512 - coverage;
1302 else if ( coverage == 256 )
1303 coverage = 255;
1304 }
1305 else
1306 {
1307 /* normal non-zero winding rule */
1308 if ( coverage >= 256 )
1309 coverage = 255;
1310 }
1311
1312 y += (TCoord)ras.min_ey;
1313 x += (TCoord)ras.min_ex;
1314
1315 /* FT_Span.x is a 16-bit short, so limit our coordinates appropriately */
1316 if ( x >= 32767 )
1317 x = 32767;
1318
1319 /* FT_Span.y is an integer, so limit our coordinates appropriately */
1320 if ( y >= FT_INT_MAX )
1321 y = FT_INT_MAX;
1322
1323 if ( coverage )
1324 {
1325 FT_Span* span;
1326 int count;
1327
1328
1329 /* see whether we can add this span to the current list */
1330 count = ras.num_gray_spans;
1331 span = ras.gray_spans + count - 1;
1332 if ( count > 0 &&
1333 ras.span_y == y &&
1334 (int)span->x + span->len == (int)x &&
1335 span->coverage == coverage )
1336 {
1337 span->len = (unsigned short)( span->len + acount );
1338 return;
1339 }
1340
1341 if ( ras.span_y != y || count >= FT_MAX_GRAY_SPANS )
1342 {
1343 if ( ras.render_span && count > 0 )
1344 ras.render_span( ras.span_y, count, ras.gray_spans,
1345 ras.render_span_data );
1346
1347 #ifdef FT_DEBUG_LEVEL_TRACE
1348
1349 if ( count > 0 )
1350 {
1351 int n;
1352
1353
1354 FT_TRACE7(( "y = %3d ", ras.span_y ));
1355 span = ras.gray_spans;
1356 for ( n = 0; n < count; n++, span++ )
1357 FT_TRACE7(( "[%d..%d]:%02x ",
1358 span->x, span->x + span->len - 1, span->coverage ));
1359 FT_TRACE7(( "\n" ));
1360 }
1361
1362 #endif /* FT_DEBUG_LEVEL_TRACE */
1363
1364 ras.num_gray_spans = 0;
1365 ras.span_y = (int)y;
1366
1367 span = ras.gray_spans;
1368 }
1369 else
1370 span++;
1371
1372 /* add a gray span to the current list */
1373 span->x = (short)x;
1374 span->len = (unsigned short)acount;
1375 span->coverage = (unsigned char)coverage;
1376
1377 ras.num_gray_spans++;
1378 }
1379 }
1380
1381
1382 #ifdef FT_DEBUG_LEVEL_TRACE
1383
1384 /* to be called while in the debugger -- */
1385 /* this function causes a compiler warning since it is unused otherwise */
1386 static void
1387 gray_dump_cells( RAS_ARG )
1388 {
1389 int yindex;
1390
1391
1392 for ( yindex = 0; yindex < ras.ycount; yindex++ )
1393 {
1394 PCell cell;
1395
1396
1397 printf( "%3d:", yindex );
1398
1399 for ( cell = ras.ycells[yindex]; cell != NULL; cell = cell->next )
1400 printf( " (%3ld, c:%4ld, a:%6d)", cell->x, cell->cover, cell->area );
1401 printf( "\n" );
1402 }
1403 }
1404
1405 #endif /* FT_DEBUG_LEVEL_TRACE */
1406
1407
1408 static void
1409 gray_sweep( RAS_ARG_ const FT_Bitmap* target )
1410 {
1411 int yindex;
1412
1413 FT_UNUSED( target );
1414
1415
1416 if ( ras.num_cells == 0 )
1417 return;
1418
1419 ras.num_gray_spans = 0;
1420
1421 FT_TRACE7(( "gray_sweep: start\n" ));
1422
1423 for ( yindex = 0; yindex < ras.ycount; yindex++ )
1424 {
1425 PCell cell = ras.ycells[yindex];
1426 TCoord cover = 0;
1427 TCoord x = 0;
1428
1429
1430 for ( ; cell != NULL; cell = cell->next )
1431 {
1432 TPos area;
1433
1434
1435 if ( cell->x > x && cover != 0 )
1436 gray_hline( RAS_VAR_ x, yindex, cover * ( ONE_PIXEL * 2 ),
1437 cell->x - x );
1438
1439 cover += cell->cover;
1440 area = cover * ( ONE_PIXEL * 2 ) - cell->area;
1441
1442 if ( area != 0 && cell->x >= 0 )
1443 gray_hline( RAS_VAR_ cell->x, yindex, area, 1 );
1444
1445 x = cell->x + 1;
1446 }
1447
1448 if ( cover != 0 )
1449 gray_hline( RAS_VAR_ x, yindex, cover * ( ONE_PIXEL * 2 ),
1450 ras.count_ex - x );
1451 }
1452
1453 if ( ras.render_span && ras.num_gray_spans > 0 )
1454 ras.render_span( ras.span_y, ras.num_gray_spans,
1455 ras.gray_spans, ras.render_span_data );
1456
1457 #ifdef FT_DEBUG_LEVEL_TRACE
1458
1459 if ( ras.num_gray_spans > 0 )
1460 {
1461 FT_Span* span;
1462 int n;
1463
1464
1465 FT_TRACE7(( "y = %3d ", ras.span_y ));
1466 span = ras.gray_spans;
1467 for ( n = 0; n < ras.num_gray_spans; n++, span++ )
1468 FT_TRACE7(( "[%d..%d]:%02x ",
1469 span->x, span->x + span->len - 1, span->coverage ));
1470 FT_TRACE7(( "\n" ));
1471 }
1472
1473 FT_TRACE7(( "gray_sweep: end\n" ));
1474
1475 #endif /* FT_DEBUG_LEVEL_TRACE */
1476
1477 }
1478
1479
1480 #ifdef _STANDALONE_
1481
1482 /*************************************************************************/
1483 /* */
1484 /* The following function should only compile in stand-alone mode, */
1485 /* i.e., when building this component without the rest of FreeType. */
1486 /* */
1487 /*************************************************************************/
1488
1489 /*************************************************************************/
1490 /* */
1491 /* <Function> */
1492 /* FT_Outline_Decompose */
1493 /* */
1494 /* <Description> */
1495 /* Walk over an outline's structure to decompose it into individual */
1496 /* segments and Bézier arcs. This function is also able to emit */
1497 /* `move to' and `close to' operations to indicate the start and end */
1498 /* of new contours in the outline. */
1499 /* */
1500 /* <Input> */
1501 /* outline :: A pointer to the source target. */
1502 /* */
1503 /* func_interface :: A table of `emitters', i.e., function pointers */
1504 /* called during decomposition to indicate path */
1505 /* operations. */
1506 /* */
1507 /* <InOut> */
1508 /* user :: A typeless pointer which is passed to each */
1509 /* emitter during the decomposition. It can be */
1510 /* used to store the state during the */
1511 /* decomposition. */
1512 /* */
1513 /* <Return> */
1514 /* Error code. 0 means success. */
1515 /* */
1516 static int
1517 FT_Outline_Decompose( const FT_Outline* outline,
1518 const FT_Outline_Funcs* func_interface,
1519 void* user )
1520 {
1521 #undef SCALED
1522 #define SCALED( x ) ( ( (x) << shift ) - delta )
1523
1524 FT_Vector v_last;
1525 FT_Vector v_control;
1526 FT_Vector v_start;
1527
1528 FT_Vector* point;
1529 FT_Vector* limit;
1530 char* tags;
1531
1532 int error;
1533
1534 int n; /* index of contour in outline */
1535 int first; /* index of first point in contour */
1536 char tag; /* current point's state */
1537
1538 int shift;
1539 TPos delta;
1540
1541
1542 if ( !outline || !func_interface )
1543 return FT_THROW( Invalid_Argument );
1544
1545 shift = func_interface->shift;
1546 delta = func_interface->delta;
1547 first = 0;
1548
1549 for ( n = 0; n < outline->n_contours; n++ )
1550 {
1551 int last; /* index of last point in contour */
1552
1553
1554 FT_TRACE5(( "FT_Outline_Decompose: Outline %d\n", n ));
1555
1556 last = outline->contours[n];
1557 if ( last < 0 )
1558 goto Invalid_Outline;
1559 limit = outline->points + last;
1560
1561 v_start = outline->points[first];
1562 v_start.x = SCALED( v_start.x );
1563 v_start.y = SCALED( v_start.y );
1564
1565 v_last = outline->points[last];
1566 v_last.x = SCALED( v_last.x );
1567 v_last.y = SCALED( v_last.y );
1568
1569 v_control = v_start;
1570
1571 point = outline->points + first;
1572 tags = outline->tags + first;
1573 tag = FT_CURVE_TAG( tags[0] );
1574
1575 /* A contour cannot start with a cubic control point! */
1576 if ( tag == FT_CURVE_TAG_CUBIC )
1577 goto Invalid_Outline;
1578
1579 /* check first point to determine origin */
1580 if ( tag == FT_CURVE_TAG_CONIC )
1581 {
1582 /* first point is conic control. Yes, this happens. */
1583 if ( FT_CURVE_TAG( outline->tags[last] ) == FT_CURVE_TAG_ON )
1584 {
1585 /* start at last point if it is on the curve */
1586 v_start = v_last;
1587 limit--;
1588 }
1589 else
1590 {
1591 /* if both first and last points are conic, */
1592 /* start at their middle and record its position */
1593 /* for closure */
1594 v_start.x = ( v_start.x + v_last.x ) / 2;
1595 v_start.y = ( v_start.y + v_last.y ) / 2;
1596
1597 v_last = v_start;
1598 }
1599 point--;
1600 tags--;
1601 }
1602
1603 FT_TRACE5(( " move to (%.2f, %.2f)\n",
1604 v_start.x / 64.0, v_start.y / 64.0 ));
1605 error = func_interface->move_to( &v_start, user );
1606 if ( error )
1607 goto Exit;
1608
1609 while ( point < limit )
1610 {
1611 point++;
1612 tags++;
1613
1614 tag = FT_CURVE_TAG( tags[0] );
1615 switch ( tag )
1616 {
1617 case FT_CURVE_TAG_ON: /* emit a single line_to */
1618 {
1619 FT_Vector vec;
1620
1621
1622 vec.x = SCALED( point->x );
1623 vec.y = SCALED( point->y );
1624
1625 FT_TRACE5(( " line to (%.2f, %.2f)\n",
1626 vec.x / 64.0, vec.y / 64.0 ));
1627 error = func_interface->line_to( &vec, user );
1628 if ( error )
1629 goto Exit;
1630 continue;
1631 }
1632
1633 case FT_CURVE_TAG_CONIC: /* consume conic arcs */
1634 v_control.x = SCALED( point->x );
1635 v_control.y = SCALED( point->y );
1636
1637 Do_Conic:
1638 if ( point < limit )
1639 {
1640 FT_Vector vec;
1641 FT_Vector v_middle;
1642
1643
1644 point++;
1645 tags++;
1646 tag = FT_CURVE_TAG( tags[0] );
1647
1648 vec.x = SCALED( point->x );
1649 vec.y = SCALED( point->y );
1650
1651 if ( tag == FT_CURVE_TAG_ON )
1652 {
1653 FT_TRACE5(( " conic to (%.2f, %.2f)"
1654 " with control (%.2f, %.2f)\n",
1655 vec.x / 64.0, vec.y / 64.0,
1656 v_control.x / 64.0, v_control.y / 64.0 ));
1657 error = func_interface->conic_to( &v_control, &vec, user );
1658 if ( error )
1659 goto Exit;
1660 continue;
1661 }
1662
1663 if ( tag != FT_CURVE_TAG_CONIC )
1664 goto Invalid_Outline;
1665
1666 v_middle.x = ( v_control.x + vec.x ) / 2;
1667 v_middle.y = ( v_control.y + vec.y ) / 2;
1668
1669 FT_TRACE5(( " conic to (%.2f, %.2f)"
1670 " with control (%.2f, %.2f)\n",
1671 v_middle.x / 64.0, v_middle.y / 64.0,
1672 v_control.x / 64.0, v_control.y / 64.0 ));
1673 error = func_interface->conic_to( &v_control, &v_middle, user );
1674 if ( error )
1675 goto Exit;
1676
1677 v_control = vec;
1678 goto Do_Conic;
1679 }
1680
1681 FT_TRACE5(( " conic to (%.2f, %.2f)"
1682 " with control (%.2f, %.2f)\n",
1683 v_start.x / 64.0, v_start.y / 64.0,
1684 v_control.x / 64.0, v_control.y / 64.0 ));
1685 error = func_interface->conic_to( &v_control, &v_start, user );
1686 goto Close;
1687
1688 default: /* FT_CURVE_TAG_CUBIC */
1689 {
1690 FT_Vector vec1, vec2;
1691
1692
1693 if ( point + 1 > limit ||
1694 FT_CURVE_TAG( tags[1] ) != FT_CURVE_TAG_CUBIC )
1695 goto Invalid_Outline;
1696
1697 point += 2;
1698 tags += 2;
1699
1700 vec1.x = SCALED( point[-2].x );
1701 vec1.y = SCALED( point[-2].y );
1702
1703 vec2.x = SCALED( point[-1].x );
1704 vec2.y = SCALED( point[-1].y );
1705
1706 if ( point <= limit )
1707 {
1708 FT_Vector vec;
1709
1710
1711 vec.x = SCALED( point->x );
1712 vec.y = SCALED( point->y );
1713
1714 FT_TRACE5(( " cubic to (%.2f, %.2f)"
1715 " with controls (%.2f, %.2f) and (%.2f, %.2f)\n",
1716 vec.x / 64.0, vec.y / 64.0,
1717 vec1.x / 64.0, vec1.y / 64.0,
1718 vec2.x / 64.0, vec2.y / 64.0 ));
1719 error = func_interface->cubic_to( &vec1, &vec2, &vec, user );
1720 if ( error )
1721 goto Exit;
1722 continue;
1723 }
1724
1725 FT_TRACE5(( " cubic to (%.2f, %.2f)"
1726 " with controls (%.2f, %.2f) and (%.2f, %.2f)\n",
1727 v_start.x / 64.0, v_start.y / 64.0,
1728 vec1.x / 64.0, vec1.y / 64.0,
1729 vec2.x / 64.0, vec2.y / 64.0 ));
1730 error = func_interface->cubic_to( &vec1, &vec2, &v_start, user );
1731 goto Close;
1732 }
1733 }
1734 }
1735
1736 /* close the contour with a line segment */
1737 FT_TRACE5(( " line to (%.2f, %.2f)\n",
1738 v_start.x / 64.0, v_start.y / 64.0 ));
1739 error = func_interface->line_to( &v_start, user );
1740
1741 Close:
1742 if ( error )
1743 goto Exit;
1744
1745 first = last + 1;
1746 }
1747
1748 FT_TRACE5(( "FT_Outline_Decompose: Done\n", n ));
1749 return 0;
1750
1751 Exit:
1752 FT_TRACE5(( "FT_Outline_Decompose: Error %d\n", error ));
1753 return error;
1754
1755 Invalid_Outline:
1756 return FT_THROW( Invalid_Outline );
1757 }
1758
1759 #endif /* _STANDALONE_ */
1760
1761
1762 typedef struct gray_TBand_
1763 {
1764 TPos min, max;
1765
1766 } gray_TBand;
1767
1768 FT_DEFINE_OUTLINE_FUNCS(func_interface,
1769 (FT_Outline_MoveTo_Func) gray_move_to,
1770 (FT_Outline_LineTo_Func) gray_line_to,
1771 (FT_Outline_ConicTo_Func)gray_conic_to,
1772 (FT_Outline_CubicTo_Func)gray_cubic_to,
1773 0,
1774 0
1775 )
1776
1777 static int
1778 gray_convert_glyph_inner( RAS_ARG )
1779 {
1780
1781 volatile int error = 0;
1782
1783 #ifdef FT_CONFIG_OPTION_PIC
1784 FT_Outline_Funcs func_interface;
1785 Init_Class_func_interface(&func_interface);
1786 #endif
1787
1788 if ( ft_setjmp( ras.jump_buffer ) == 0 )
1789 {
1790 error = FT_Outline_Decompose( &ras.outline, &func_interface, &ras );
1791 if ( !ras.invalid )
1792 gray_record_cell( RAS_VAR );
1793 }
1794 else
1795 error = FT_THROW( Memory_Overflow );
1796
1797 return error;
1798 }
1799
1800
1801 static int
1802 gray_convert_glyph( RAS_ARG )
1803 {
1804 gray_TBand bands[40];
1805 gray_TBand* volatile band;
1806 int volatile n, num_bands;
1807 TPos volatile min, max, max_y;
1808 FT_BBox* clip;
1809
1810
1811 /* Set up state in the raster object */
1812 gray_compute_cbox( RAS_VAR );
1813
1814 /* clip to target bitmap, exit if nothing to do */
1815 clip = &ras.clip_box;
1816
1817 if ( ras.max_ex <= clip->xMin || ras.min_ex >= clip->xMax ||
1818 ras.max_ey <= clip->yMin || ras.min_ey >= clip->yMax )
1819 return 0;
1820
1821 if ( ras.min_ex < clip->xMin ) ras.min_ex = clip->xMin;
1822 if ( ras.min_ey < clip->yMin ) ras.min_ey = clip->yMin;
1823
1824 if ( ras.max_ex > clip->xMax ) ras.max_ex = clip->xMax;
1825 if ( ras.max_ey > clip->yMax ) ras.max_ey = clip->yMax;
1826
1827 ras.count_ex = ras.max_ex - ras.min_ex;
1828 ras.count_ey = ras.max_ey - ras.min_ey;
1829
1830 /* set up vertical bands */
1831 num_bands = (int)( ( ras.max_ey - ras.min_ey ) / ras.band_size );
1832 if ( num_bands == 0 )
1833 num_bands = 1;
1834 if ( num_bands >= 39 )
1835 num_bands = 39;
1836
1837 ras.band_shoot = 0;
1838
1839 min = ras.min_ey;
1840 max_y = ras.max_ey;
1841
1842 for ( n = 0; n < num_bands; n++, min = max )
1843 {
1844 max = min + ras.band_size;
1845 if ( n == num_bands - 1 || max > max_y )
1846 max = max_y;
1847
1848 bands[0].min = min;
1849 bands[0].max = max;
1850 band = bands;
1851
1852 while ( band >= bands )
1853 {
1854 TPos bottom, top, middle;
1855 int error;
1856
1857 {
1858 PCell cells_max;
1859 int yindex;
1860 long cell_start, cell_end, cell_mod;
1861
1862
1863 ras.ycells = (PCell*)ras.buffer;
1864 ras.ycount = band->max - band->min;
1865
1866 cell_start = sizeof ( PCell ) * ras.ycount;
1867 cell_mod = cell_start % sizeof ( TCell );
1868 if ( cell_mod > 0 )
1869 cell_start += sizeof ( TCell ) - cell_mod;
1870
1871 cell_end = ras.buffer_size;
1872 cell_end -= cell_end % sizeof ( TCell );
1873
1874 cells_max = (PCell)( (char*)ras.buffer + cell_end );
1875 ras.cells = (PCell)( (char*)ras.buffer + cell_start );
1876 if ( ras.cells >= cells_max )
1877 goto ReduceBands;
1878
1879 ras.max_cells = cells_max - ras.cells;
1880 if ( ras.max_cells < 2 )
1881 goto ReduceBands;
1882
1883 for ( yindex = 0; yindex < ras.ycount; yindex++ )
1884 ras.ycells[yindex] = NULL;
1885 }
1886
1887 ras.num_cells = 0;
1888 ras.invalid = 1;
1889 ras.min_ey = band->min;
1890 ras.max_ey = band->max;
1891 ras.count_ey = band->max - band->min;
1892
1893 error = gray_convert_glyph_inner( RAS_VAR );
1894
1895 if ( !error )
1896 {
1897 gray_sweep( RAS_VAR_ &ras.target );
1898 band--;
1899 continue;
1900 }
1901 else if ( error != ErrRaster_Memory_Overflow )
1902 return 1;
1903
1904 ReduceBands:
1905 /* render pool overflow; we will reduce the render band by half */
1906 bottom = band->min;
1907 top = band->max;
1908 middle = bottom + ( ( top - bottom ) >> 1 );
1909
1910 /* This is too complex for a single scanline; there must */
1911 /* be some problems. */
1912 if ( middle == bottom )
1913 {
1914 #ifdef FT_DEBUG_LEVEL_TRACE
1915 FT_TRACE7(( "gray_convert_glyph: rotten glyph\n" ));
1916 #endif
1917 return 1;
1918 }
1919
1920 if ( bottom-top >= ras.band_size )
1921 ras.band_shoot++;
1922
1923 band[1].min = bottom;
1924 band[1].max = middle;
1925 band[0].min = middle;
1926 band[0].max = top;
1927 band++;
1928 }
1929 }
1930
1931 if ( ras.band_shoot > 8 && ras.band_size > 16 )
1932 ras.band_size = ras.band_size / 2;
1933
1934 return 0;
1935 }
1936
1937
1938 static int
1939 gray_raster_render( gray_PRaster raster,
1940 const FT_Raster_Params* params )
1941 {
1942 const FT_Outline* outline = (const FT_Outline*)params->source;
1943 const FT_Bitmap* target_map = params->target;
1944 gray_PWorker worker;
1945
1946
1947 if ( !raster || !raster->buffer || !raster->buffer_size )
1948 return FT_THROW( Invalid_Argument );
1949
1950 if ( !outline )
1951 return FT_THROW( Invalid_Outline );
1952
1953 /* return immediately if the outline is empty */
1954 if ( outline->n_points == 0 || outline->n_contours <= 0 )
1955 return 0;
1956
1957 if ( !outline->contours || !outline->points )
1958 return FT_THROW( Invalid_Outline );
1959
1960 if ( outline->n_points !=
1961 outline->contours[outline->n_contours - 1] + 1 )
1962 return FT_THROW( Invalid_Outline );
1963
1964 worker = raster->worker;
1965
1966 /* if direct mode is not set, we must have a target bitmap */
1967 if ( !( params->flags & FT_RASTER_FLAG_DIRECT ) )
1968 {
1969 if ( !target_map )
1970 return FT_THROW( Invalid_Argument );
1971
1972 /* nothing to do */
1973 if ( !target_map->width || !target_map->rows )
1974 return 0;
1975
1976 if ( !target_map->buffer )
1977 return FT_THROW( Invalid_Argument );
1978 }
1979
1980 /* this version does not support monochrome rendering */
1981 if ( !( params->flags & FT_RASTER_FLAG_AA ) )
1982 return FT_THROW( Invalid_Mode );
1983
1984 /* compute clipping box */
1985 if ( !( params->flags & FT_RASTER_FLAG_DIRECT ) )
1986 {
1987 /* compute clip box from target pixmap */
1988 ras.clip_box.xMin = 0;
1989 ras.clip_box.yMin = 0;
1990 ras.clip_box.xMax = target_map->width;
1991 ras.clip_box.yMax = target_map->rows;
1992 }
1993 else if ( params->flags & FT_RASTER_FLAG_CLIP )
1994 ras.clip_box = params->clip_box;
1995 else
1996 {
1997 ras.clip_box.xMin = -32768L;
1998 ras.clip_box.yMin = -32768L;
1999 ras.clip_box.xMax = 32767L;
2000 ras.clip_box.yMax = 32767L;
2001 }
2002
2003 gray_init_cells( RAS_VAR_ raster->buffer, raster->buffer_size );
2004
2005 ras.outline = *outline;
2006 ras.num_cells = 0;
2007 ras.invalid = 1;
2008 ras.band_size = raster->band_size;
2009 ras.num_gray_spans = 0;
2010
2011 if ( params->flags & FT_RASTER_FLAG_DIRECT )
2012 {
2013 ras.render_span = (FT_Raster_Span_Func)params->gray_spans;
2014 ras.render_span_data = params->user;
2015 }
2016 else
2017 {
2018 ras.target = *target_map;
2019 ras.render_span = (FT_Raster_Span_Func)gray_render_span;
2020 ras.render_span_data = &ras;
2021 }
2022
2023 return gray_convert_glyph( RAS_VAR );
2024 }
2025
2026
2027 /**** RASTER OBJECT CREATION: In stand-alone mode, we simply use *****/
2028 /**** a static object. *****/
2029
2030 #ifdef _STANDALONE_
2031
2032 static int
2033 gray_raster_new( void* memory,
2034 FT_Raster* araster )
2035 {
2036 static gray_TRaster the_raster;
2037
2038 FT_UNUSED( memory );
2039
2040
2041 *araster = (FT_Raster)&the_raster;
2042 FT_MEM_ZERO( &the_raster, sizeof ( the_raster ) );
2043
2044 return 0;
2045 }
2046
2047
2048 static void
2049 gray_raster_done( FT_Raster raster )
2050 {
2051 /* nothing */
2052 FT_UNUSED( raster );
2053 }
2054
2055 #else /* !_STANDALONE_ */
2056
2057 static int
2058 gray_raster_new( FT_Memory memory,
2059 FT_Raster* araster )
2060 {
2061 FT_Error error;
2062 gray_PRaster raster = NULL;
2063
2064
2065 *araster = 0;
2066 if ( !FT_ALLOC( raster, sizeof ( gray_TRaster ) ) )
2067 {
2068 raster->memory = memory;
2069 *araster = (FT_Raster)raster;
2070 }
2071
2072 return error;
2073 }
2074
2075
2076 static void
2077 gray_raster_done( FT_Raster raster )
2078 {
2079 FT_Memory memory = (FT_Memory)((gray_PRaster)raster)->memory;
2080
2081
2082 FT_FREE( raster );
2083 }
2084
2085 #endif /* !_STANDALONE_ */
2086
2087
2088 static void
2089 gray_raster_reset( FT_Raster raster,
2090 char* pool_base,
2091 long pool_size )
2092 {
2093 gray_PRaster rast = (gray_PRaster)raster;
2094
2095
2096 if ( raster )
2097 {
2098 if ( pool_base && pool_size >= (long)sizeof ( gray_TWorker ) + 2048 )
2099 {
2100 gray_PWorker worker = (gray_PWorker)pool_base;
2101
2102
2103 rast->worker = worker;
2104 rast->buffer = pool_base +
2105 ( ( sizeof ( gray_TWorker ) +
2106 sizeof ( TCell ) - 1 ) &
2107 ~( sizeof ( TCell ) - 1 ) );
2108 rast->buffer_size = (long)( ( pool_base + pool_size ) -
2109 (char*)rast->buffer ) &
2110 ~( sizeof ( TCell ) - 1 );
2111 rast->band_size = (int)( rast->buffer_size /
2112 ( sizeof ( TCell ) * 8 ) );
2113 }
2114 else
2115 {
2116 rast->buffer = NULL;
2117 rast->buffer_size = 0;
2118 rast->worker = NULL;
2119 }
2120 }
2121 }
2122
2123
2124 FT_DEFINE_RASTER_FUNCS(ft_grays_raster,
2125 FT_GLYPH_FORMAT_OUTLINE,
2126
2127 (FT_Raster_New_Func) gray_raster_new,
2128 (FT_Raster_Reset_Func) gray_raster_reset,
2129 (FT_Raster_Set_Mode_Func)0,
2130 (FT_Raster_Render_Func) gray_raster_render,
2131 (FT_Raster_Done_Func) gray_raster_done
2132 )
2133
2134
2135 /* END */
2136
2137
2138 /* Local Variables: */
2139 /* coding: utf-8 */
2140 /* End: */