Skip to content
0.2.0-dev Download ZIP
2775 lines ·71.0 KB Raw
1/****************************************************************************
2 *
3 * ftraster.c
4 *
5 * The FreeType glyph rasterizer (body).
6 *
7 * Copyright (C) 1996-2026 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 `ftimage.h' and `ftmisc.h' into the $(incdir)
23 * directory. Typically, you should do something like
24 *
25 * - copy `src/raster/ftraster.c' (this file) to your current directory
26 *
27 * - copy `include/freetype/ftimage.h' and `src/raster/ftmisc.h' to your
28 * current directory
29 *
30 * - compile `ftraster' with the STANDALONE_ macro defined, as in
31 *
32 * cc -c -DSTANDALONE_ ftraster.c
33 *
34 * The renderer can be initialized with a call to
35 * `ft_standard_raster.raster_new'; a bitmap can be generated
36 * with a call to `ft_standard_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 *
46 * This is a rewrite of the FreeType 1.x scan-line converter
47 *
48 */
49
50#ifdef STANDALONE_
51
52 /* The size in bytes of the render pool used by the scan-line converter */
53 /* to do all of its work. */
54#define FT_RENDER_POOL_SIZE 16384L
55
56#define FT_CONFIG_STANDARD_LIBRARY_H <stdlib.h>
57
58#include <string.h> /* for memset */
59
60#include "ftmisc.h"
61#include "ftimage.h"
62
63#else /* !STANDALONE_ */
64
65#include "ftraster.h"
66#include <freetype/internal/ftcalc.h> /* for FT_MulDiv_No_Round */
67
68#endif /* !STANDALONE_ */
69
70
71 /**************************************************************************
72 *
73 * A simple technical note on how the raster works
74 * -----------------------------------------------
75 *
76 * Converting an outline into a bitmap is achieved in several steps:
77 *
78 * 1 - Decomposing the outline into successive `profiles'. Each
79 * profile is simply an array of scanline intersections on a given
80 * dimension. A profile's main attributes are
81 *
82 * o its scanline position boundaries, i.e. `Ymin' and `Ymax'
83 *
84 * o an array of intersection coordinates for each scanline
85 * between `Ymin' and `Ymax'
86 *
87 * o a direction, indicating whether it was built going `up' or
88 * `down', as this is very important for filling rules
89 *
90 * o its drop-out mode
91 *
92 * 2 - Sweeping the target map's scanlines in order to compute segment
93 * `spans' which are then filled. Additionally, this pass
94 * performs drop-out control.
95 *
96 * The outline data is parsed during step 1 only. The profiles are
97 * built from the bottom of the render pool, used as a stack. The
98 * following graphics shows the profile list under construction:
99 *
100 * __________________________________________________________ _ _
101 * | | | | |
102 * | profile | coordinates for | profile | coordinates for |-->
103 * | 1 | profile 1 | 2 | profile 2 |-->
104 * |_________|_________________|_________|_________________|__ _ _
105 *
106 * ^ ^
107 * | |
108 * start of render pool top
109 *
110 * The top of the profile stack is kept in the `top' variable.
111 *
112 * As you can see, a profile record is pushed on top of the render
113 * pool, which is then followed by its coordinates/intersections. If
114 * a change of direction is detected in the outline, a new profile is
115 * generated until the end of the outline.
116 *
117 * Note that, for all generated profiles, the function End_Profile()
118 * is used to record all their bottom-most scanlines as well as the
119 * scanline above their upmost boundary. These positions are called
120 * `y-turns' because they (sort of) correspond to local extrema.
121 * They are stored in a sorted list built from the top of the render
122 * pool as a downwards stack:
123 *
124 * _ _ _______________________________________
125 * | |
126 * <--| sorted list of |
127 * <--| extrema scanlines |
128 * _ _ __________________|____________________|
129 *
130 * ^ ^
131 * | |
132 * maxBuff sizeBuff = end of pool
133 *
134 * This list is later used during the sweep phase in order to
135 * optimize performance (see technical note on the sweep below).
136 *
137 * Of course, the raster detects whether the two stacks collide and
138 * handles the situation by bisecting the job and restarting.
139 *
140 */
141
142
143 /*************************************************************************/
144 /*************************************************************************/
145 /** **/
146 /** CONFIGURATION MACROS **/
147 /** **/
148 /*************************************************************************/
149 /*************************************************************************/
150
151
152 /*************************************************************************/
153 /*************************************************************************/
154 /** **/
155 /** OTHER MACROS (do not change) **/
156 /** **/
157 /*************************************************************************/
158 /*************************************************************************/
159
160 /**************************************************************************
161 *
162 * The macro FT_COMPONENT is used in trace mode. It is an implicit
163 * parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log
164 * messages during execution.
165 */
166#undef FT_COMPONENT
167#define FT_COMPONENT raster
168
169
170#ifdef STANDALONE_
171
172 /* Auxiliary macros for token concatenation. */
173#define FT_ERR_XCAT( x, y ) x ## y
174#define FT_ERR_CAT( x, y ) FT_ERR_XCAT( x, y )
175
176 /* This macro is used to indicate that a function parameter is unused. */
177 /* Its purpose is simply to reduce compiler warnings. Note also that */
178 /* simply defining it as `(void)x' doesn't avoid warnings with certain */
179 /* ANSI compilers (e.g. LCC). */
180#define FT_UNUSED( x ) (x) = (x)
181
182 /* Disable the tracing mechanism for simplicity -- developers can */
183 /* activate it easily by redefining these macros. */
184#ifndef FT_ERROR
185#define FT_ERROR( x ) do { } while ( 0 ) /* nothing */
186#endif
187
188#ifndef FT_TRACE
189#define FT_TRACE( x ) do { } while ( 0 ) /* nothing */
190#define FT_TRACE1( x ) do { } while ( 0 ) /* nothing */
191#define FT_TRACE6( x ) do { } while ( 0 ) /* nothing */
192#define FT_TRACE7( x ) do { } while ( 0 ) /* nothing */
193#endif
194
195#ifndef FT_THROW
196#define FT_THROW( e ) FT_ERR_CAT( Raster_Err_, e )
197#endif
198
199#define Raster_Err_Ok 0
200#define Raster_Err_Invalid_Outline -1
201#define Raster_Err_Cannot_Render_Glyph -2
202#define Raster_Err_Invalid_Argument -3
203#define Raster_Err_Raster_Overflow -4
204#define Raster_Err_Raster_Uninitialized -5
205#define Raster_Err_Raster_Negative_Height -6
206
207#define ft_memset memset
208
209#define FT_DEFINE_RASTER_FUNCS( class_, glyph_format_, raster_new_, \
210 raster_reset_, raster_set_mode_, \
211 raster_render_, raster_done_ ) \
212 const FT_Raster_Funcs class_ = \
213 { \
214 glyph_format_, \
215 raster_new_, \
216 raster_reset_, \
217 raster_set_mode_, \
218 raster_render_, \
219 raster_done_ \
220 };
221
222#else /* !STANDALONE_ */
223
224
225#include <freetype/internal/ftobjs.h>
226#include <freetype/internal/ftdebug.h> /* for FT_TRACE, FT_ERROR, and FT_THROW */
227
228#include "rasterrs.h"
229
230
231#endif /* !STANDALONE_ */
232
233
234#ifndef FT_MEM_SET
235#define FT_MEM_SET( d, s, c ) ft_memset( d, s, c )
236#endif
237
238#ifndef FT_MEM_ZERO
239#define FT_MEM_ZERO( dest, count ) FT_MEM_SET( dest, 0, count )
240#endif
241
242#ifndef FT_ZERO
243#define FT_ZERO( p ) FT_MEM_ZERO( p, sizeof ( *(p) ) )
244#endif
245
246 /* FMulDiv means `Fast MulDiv'; it is used in case where `b' is */
247 /* typically a small value and the result of a*b is known to fit into */
248 /* 32 bits. */
249#define FMulDiv( a, b, c ) ( (a) * (b) / (c) )
250
251 /* On the other hand, SMulDiv means `Slow MulDiv', and is used typically */
252 /* for clipping computations. It simply uses the FT_MulDiv() function */
253 /* defined in `ftcalc.h'. */
254#ifdef FT_INT64
255#define SMulDiv( a, b, c ) (Long)( (FT_Int64)(a) * (b) / (c) )
256#else
257#define SMulDiv FT_MulDiv_No_Round
258#endif
259
260 /* The rasterizer is a very general purpose component; please leave */
261 /* the following redefinitions there (you never know your target */
262 /* environment). */
263
264#ifndef TRUE
265#define TRUE 1
266#endif
267
268#ifndef FALSE
269#define FALSE 0
270#endif
271
272#ifndef NULL
273#define NULL (void*)0
274#endif
275
276#ifndef SUCCESS
277#define SUCCESS 0
278#endif
279
280#ifndef FAILURE
281#define FAILURE 1
282#endif
283
284
285#define MaxBezier 32 /* The maximum number of stacked Bezier curves. */
286 /* Setting this constant to more than 32 is a */
287 /* pure waste of space. */
288
289#define Pixel_Bits 6 /* fractional bits of *input* coordinates */
290
291
292 /*************************************************************************/
293 /*************************************************************************/
294 /** **/
295 /** SIMPLE TYPE DECLARATIONS **/
296 /** **/
297 /*************************************************************************/
298 /*************************************************************************/
299
300 typedef int Int;
301 typedef unsigned int UInt;
302 typedef short Short;
303 typedef unsigned short UShort, *PUShort;
304 typedef long Long, *PLong;
305 typedef unsigned long ULong;
306
307 typedef unsigned char Byte, *PByte;
308 typedef char Bool;
309
310 typedef struct TPoint_
311 {
312 Long x;
313 Long y;
314
315 } TPoint;
316
317
318 /* values for the `flags' bit field */
319#define Flow_Up 0x08U
320#define Overshoot_Top 0x10U
321#define Overshoot_Bottom 0x20U
322#define Dropout 0x40U
323
324
325 /* States of each line, arc, and profile */
326 typedef enum TStates_
327 {
328 Unknown_State,
329 Ascending_State,
330 Descending_State,
331 Flat_State
332
333 } TStates;
334
335
336 typedef struct TProfile_ TProfile;
337 typedef TProfile* PProfile;
338
339 struct TProfile_
340 {
341 PProfile link; /* link to next profile (various purposes) */
342 PProfile next; /* next profile in same contour, used */
343 /* during drop-out control */
344 Int offset; /* bottom or currently scanned array index */
345 Int height; /* profile's height in scanlines */
346 Int start; /* profile's starting scanline, also use */
347 /* as activation counter */
348 UShort flags; /* Bit 0-2: drop-out mode */
349 /* Bit 3: profile orientation (up/down) */
350 /* Bit 4: is top profile? */
351 /* Bit 5: is bottom profile? */
352 /* Bit 6: dropout detected */
353
354 FT_F26Dot6 X; /* current coordinate during sweep */
355 Long x[1]; /* actually variable array of scanline */
356 /* intersections with `height` elements */
357 };
358
359 typedef PProfile TProfileList;
360 typedef PProfile* PProfileList;
361
362
363#undef RAS_ARG
364#undef RAS_ARGS
365#undef RAS_VAR
366#undef RAS_VARS
367
368#ifdef FT_STATIC_RASTER
369
370
371#define RAS_ARGS /* void */
372#define RAS_ARG void
373
374#define RAS_VARS /* void */
375#define RAS_VAR /* void */
376
377#define FT_UNUSED_RASTER do { } while ( 0 )
378
379
380#else /* !FT_STATIC_RASTER */
381
382
383#define RAS_ARGS black_PWorker worker,
384#define RAS_ARG black_PWorker worker
385
386#define RAS_VARS worker,
387#define RAS_VAR worker
388
389#define FT_UNUSED_RASTER FT_UNUSED( worker )
390
391
392#endif /* !FT_STATIC_RASTER */
393
394
395 typedef struct black_TWorker_ black_TWorker, *black_PWorker;
396
397
398 /* prototypes used for sweep function dispatch */
399 typedef void
400 Function_Sweep_Init( RAS_ARGS Int min,
401 Int max );
402
403 typedef void
404 Function_Sweep_Span( RAS_ARGS Int y,
405 FT_F26Dot6 x1,
406 FT_F26Dot6 x2 );
407
408 typedef void
409 Function_Sweep_Step( RAS_ARG );
410
411
412 /* NOTE: These operations are only valid on 2's complement processors */
413#undef FLOOR
414#undef CEILING
415#undef TRUNC
416#undef SCALED
417
418#define FLOOR( x ) ( (x) & -ras.precision )
419#define CEILING( x ) ( ( (x) + ras.precision - 1 ) & -ras.precision )
420#define TRUNC( x ) ( (Long)(x) >> ras.precision_bits )
421#define FRAC( x ) ( (x) & ( ras.precision - 1 ) )
422
423 /* scale and shift grid to pixel centers */
424#define SCALED( x ) ( (x) * ras.precision_scale - ras.precision_half )
425
426#define IS_BOTTOM_OVERSHOOT( x ) \
427 (Bool)( CEILING( x ) - x >= ras.precision_half )
428#define IS_TOP_OVERSHOOT( x ) \
429 (Bool)( x - FLOOR( x ) >= ras.precision_half )
430
431 /* Smart dropout rounding to find which pixel is closer to span ends. */
432 /* To mimic Windows, symmetric cases do not depend on the precision. */
433#define SMART( p, q ) FLOOR( ( (p) + (q) + ras.precision * 63 / 64 ) >> 1 )
434
435#if FT_RENDER_POOL_SIZE > 2048
436#define FT_MAX_BLACK_POOL ( FT_RENDER_POOL_SIZE / sizeof ( Long ) )
437#else
438#define FT_MAX_BLACK_POOL ( 2048 / sizeof ( Long ) )
439#endif
440
441 /* The most used variables are positioned at the top of the structure. */
442 /* Thus, their offset can be coded with less opcodes, resulting in a */
443 /* smaller executable. */
444
445 struct black_TWorker_
446 {
447 Int precision_bits; /* precision related variables */
448 Int precision;
449 Int precision_half;
450 Int precision_scale;
451 Int precision_step;
452
453 PLong buff; /* The profiles buffer */
454 PLong sizeBuff; /* Render pool size */
455 PLong maxBuff; /* Profiles buffer size */
456 PLong top; /* Current cursor in buffer */
457
458 FT_Error error;
459
460 Byte dropOutControl; /* current drop_out control method */
461
462 Long lastX, lastY;
463 Long minY, maxY;
464
465 UShort num_Profs; /* current number of profiles */
466 Int numTurns; /* number of Y-turns in outline */
467
468 PProfile cProfile; /* current profile */
469 PProfile fProfile; /* head of linked list of profiles */
470 PProfile gProfile; /* contour's first profile in case */
471 /* of impact */
472
473 TStates state; /* rendering state */
474
475 FT_Outline outline;
476
477 Int bTop; /* target bitmap max line index */
478 Int bRight; /* target bitmap rightmost index */
479 Int bPitch; /* target bitmap pitch */
480 PByte bOrigin; /* target bitmap bottom-left origin */
481 PByte bLine; /* target bitmap current line */
482
483 /* dispatch variables */
484
485 Function_Sweep_Init* Proc_Sweep_Init;
486 Function_Sweep_Span* Proc_Sweep_Span;
487 Function_Sweep_Span* Proc_Sweep_Drop;
488 Function_Sweep_Step* Proc_Sweep_Step;
489
490 };
491
492
493 typedef struct black_TRaster_
494 {
495 void* memory;
496
497 } black_TRaster, *black_PRaster;
498
499#ifdef FT_STATIC_RASTER
500
501 static black_TWorker ras;
502
503#else /* !FT_STATIC_RASTER */
504
505#define ras (*worker)
506
507#endif /* !FT_STATIC_RASTER */
508
509
510 /*************************************************************************/
511 /*************************************************************************/
512 /** **/
513 /** PROFILES COMPUTATION **/
514 /** **/
515 /*************************************************************************/
516 /*************************************************************************/
517
518
519 /**************************************************************************
520 *
521 * @Function:
522 * Set_High_Precision
523 *
524 * @Description:
525 * Set precision variables according to param flag.
526 *
527 * @Input:
528 * High ::
529 * Set to True for high precision (typically for ppem < 24),
530 * false otherwise.
531 */
532 static void
533 Set_High_Precision( RAS_ARGS Int High )
534 {
535 /*
536 * `precision_step' is used in `Bezier_Up' to decide when to split a
537 * given y-monotonous Bezier arc that crosses a scanline before
538 * approximating it as a straight segment. The default value of 32 (for
539 * low accuracy) corresponds to
540 *
541 * 32 / 64 == 0.5 pixels,
542 *
543 * while for the high accuracy case we have
544 *
545 * 256 / (1 << 12) = 0.0625 pixels.
546 *
547 */
548
549 if ( High && ras.bTop + ras.bRight < 256 )
550 {
551 ras.precision_bits = 12;
552 ras.precision_step = 256;
553 }
554 else
555 {
556 ras.precision_bits = 6;
557 ras.precision_step = 32;
558 }
559
560 ras.precision = 1 << ras.precision_bits;
561 ras.precision_half = ras.precision >> 1;
562 ras.precision_scale = ras.precision >> Pixel_Bits;
563 }
564
565
566 /**************************************************************************
567 *
568 * @Function:
569 * Insert_Y_Turn
570 *
571 * @Description:
572 * Insert a salient into the sorted list placed on top of the render
573 * pool.
574 *
575 * @Input:
576 * New y scanline position.
577 *
578 * @Return:
579 * SUCCESS on success. FAILURE in case of overflow.
580 */
581 static Bool
582 Insert_Y_Turns( RAS_ARGS Int y,
583 Int top )
584 {
585 Int n = ras.numTurns;
586 PLong y_turns = ras.maxBuff;
587
588
589 /* update top value */
590 if ( n == 0 || top > y_turns[n] )
591 y_turns[n] = top;
592
593 /* look for first y value that is <= */
594 while ( n-- && y < y_turns[n] )
595 ;
596
597 /* if it is <, simply insert it, ignore if == */
598 if ( n < 0 || y > y_turns[n] )
599 {
600 ras.maxBuff--;
601 if ( ras.maxBuff <= ras.top )
602 {
603 ras.error = FT_THROW( Raster_Overflow );
604 return FAILURE;
605 }
606
607 do
608 {
609 Int y2 = (Int)y_turns[n];
610
611
612 y_turns[n] = y;
613 y = y2;
614 } while ( n-- >= 0 );
615
616 ras.numTurns++;
617 }
618
619 return SUCCESS;
620 }
621
622
623 /**************************************************************************
624 *
625 * @Function:
626 * New_Profile
627 *
628 * @Description:
629 * Create a new profile in the render pool.
630 *
631 * @Input:
632 * aState ::
633 * The state/orientation of the new profile.
634 *
635 * @Return:
636 * SUCCESS on success. FAILURE in case of overflow or of incoherent
637 * profile.
638 */
639 static Bool
640 New_Profile( RAS_ARGS TStates aState )
641 {
642 Long e;
643
644
645 if ( !ras.cProfile || ras.cProfile->height )
646 {
647 ras.cProfile = (PProfile)ras.top;
648 ras.top = ras.cProfile->x;
649
650 if ( ras.top >= ras.maxBuff )
651 {
652 FT_TRACE1(( "overflow in New_Profile\n" ));
653 ras.error = FT_THROW( Raster_Overflow );
654 return FAILURE;
655 }
656
657 ras.cProfile->height = 0;
658 }
659
660 ras.cProfile->flags = ras.dropOutControl;
661
662 switch ( aState )
663 {
664 case Ascending_State:
665 ras.cProfile->flags |= Flow_Up;
666 if ( IS_BOTTOM_OVERSHOOT( ras.lastY ) )
667 ras.cProfile->flags |= Overshoot_Bottom;
668
669 e = CEILING( ras.lastY );
670 break;
671
672 case Descending_State:
673 if ( IS_TOP_OVERSHOOT( ras.lastY ) )
674 ras.cProfile->flags |= Overshoot_Top;
675
676 e = FLOOR( ras.lastY );
677 break;
678
679 default:
680 FT_ERROR(( "New_Profile: invalid profile direction\n" ));
681 ras.error = FT_THROW( Invalid_Outline );
682 return FAILURE;
683 }
684
685 if ( e > ras.maxY )
686 e = ras.maxY;
687 if ( e < ras.minY )
688 e = ras.minY;
689 ras.cProfile->start = (Int)TRUNC( e );
690
691 FT_TRACE7(( " new %s profile = %p, start = %d\n",
692 aState == Ascending_State ? "ascending" : "descending",
693 (void *)ras.cProfile, ras.cProfile->start ));
694
695 if ( ras.lastY == e )
696 *ras.top++ = ras.lastX;
697
698 ras.state = aState;
699
700 return SUCCESS;
701 }
702
703
704 /**************************************************************************
705 *
706 * @Function:
707 * End_Profile
708 *
709 * @Description:
710 * Finalize the current profile and record y-turns.
711 *
712 * @Return:
713 * SUCCESS on success. FAILURE in case of overflow or incoherency.
714 */
715 static Bool
716 End_Profile( RAS_ARG )
717 {
718 PProfile p = ras.cProfile;
719 Int h = (Int)( ras.top - p->x );
720 Int bottom, top;
721
722
723 if ( h < 0 )
724 {
725 FT_ERROR(( "End_Profile: negative height encountered\n" ));
726 ras.error = FT_THROW( Raster_Negative_Height );
727 return FAILURE;
728 }
729
730 if ( h > 0 )
731 {
732 FT_TRACE7(( " ending profile %p, start = %2d, height = %+3d\n",
733 (void *)p, p->start, p->flags & Flow_Up ? h : -h ));
734
735 p->height = h;
736
737 if ( p->flags & Flow_Up )
738 {
739 if ( IS_TOP_OVERSHOOT( ras.lastY ) )
740 p->flags |= Overshoot_Top;
741
742 bottom = p->start;
743 top = bottom + h;
744 p->offset = 0;
745 p->X = p->x[0];
746 }
747 else
748 {
749 if ( IS_BOTTOM_OVERSHOOT( ras.lastY ) )
750 p->flags |= Overshoot_Bottom;
751
752 top = p->start + 1;
753 bottom = top - h;
754 p->start = bottom;
755 p->offset = h - 1;
756 p->X = p->x[h - 1];
757 }
758
759 if ( Insert_Y_Turns( RAS_VARS bottom, top ) )
760 return FAILURE;
761
762 if ( !ras.gProfile )
763 ras.gProfile = p;
764
765 /* preliminary values to be finalized */
766 p->next = ras.gProfile;
767 p->link = (PProfile)ras.top;
768
769 ras.num_Profs++;
770 }
771
772 return SUCCESS;
773 }
774
775
776 /**************************************************************************
777 *
778 * @Function:
779 * Finalize_Profile_Table
780 *
781 * @Description:
782 * Adjust all links in the profiles list.
783 */
784 static void
785 Finalize_Profile_Table( RAS_ARG )
786 {
787 UShort n = ras.num_Profs;
788 PProfile p = ras.fProfile;
789 PProfile q;
790
791
792 /* there should be at least two profiles, up and down */
793 while ( --n )
794 {
795 q = p->link;
796
797 /* fix the contour loop */
798 if ( q->next == p->next )
799 p->next = q;
800
801 p = q;
802 }
803
804 /* null-terminate */
805 p->link = NULL;
806 }
807
808
809 /**************************************************************************
810 *
811 * @Function:
812 * Split_Conic
813 *
814 * @Description:
815 * Subdivide one conic Bezier into two joint sub-arcs in the Bezier
816 * stack.
817 *
818 * @Input:
819 * None (subdivided Bezier is taken from the top of the stack).
820 *
821 * @Note:
822 * This routine is the `beef' of this component. It is _the_ inner
823 * loop that should be optimized to hell to get the best performance.
824 */
825 static void
826 Split_Conic( TPoint* base )
827 {
828 Long a, b;
829
830
831 base[4].x = base[2].x;
832 a = base[0].x + base[1].x;
833 b = base[1].x + base[2].x;
834 base[3].x = b >> 1;
835 base[2].x = ( a + b ) >> 2;
836 base[1].x = a >> 1;
837
838 base[4].y = base[2].y;
839 a = base[0].y + base[1].y;
840 b = base[1].y + base[2].y;
841 base[3].y = b >> 1;
842 base[2].y = ( a + b ) >> 2;
843 base[1].y = a >> 1;
844
845 /* hand optimized. gcc doesn't seem to be too good at common */
846 /* expression substitution and instruction scheduling ;-) */
847 }
848
849
850 /**************************************************************************
851 *
852 * @Function:
853 * Split_Cubic
854 *
855 * @Description:
856 * Subdivide a third-order Bezier arc into two joint sub-arcs in the
857 * Bezier stack.
858 *
859 * @Note:
860 * This routine is the `beef' of the component. It is one of _the_
861 * inner loops that should be optimized like hell to get the best
862 * performance.
863 */
864 static void
865 Split_Cubic( TPoint* base )
866 {
867 Long a, b, c;
868
869
870 base[6].x = base[3].x;
871 a = base[0].x + base[1].x;
872 b = base[1].x + base[2].x;
873 c = base[2].x + base[3].x;
874 base[5].x = c >> 1;
875 c += b;
876 base[4].x = c >> 2;
877 base[1].x = a >> 1;
878 a += b;
879 base[2].x = a >> 2;
880 base[3].x = ( a + c ) >> 3;
881
882 base[6].y = base[3].y;
883 a = base[0].y + base[1].y;
884 b = base[1].y + base[2].y;
885 c = base[2].y + base[3].y;
886 base[5].y = c >> 1;
887 c += b;
888 base[4].y = c >> 2;
889 base[1].y = a >> 1;
890 a += b;
891 base[2].y = a >> 2;
892 base[3].y = ( a + c ) >> 3;
893 }
894
895
896 /**************************************************************************
897 *
898 * @Function:
899 * Line_Up
900 *
901 * @Description:
902 * Compute the x-coordinates of an ascending line segment and store
903 * them in the render pool.
904 *
905 * @Input:
906 * x1 ::
907 * The x-coordinate of the segment's start point.
908 *
909 * y1 ::
910 * The y-coordinate of the segment's start point.
911 *
912 * x2 ::
913 * The x-coordinate of the segment's end point.
914 *
915 * y2 ::
916 * The y-coordinate of the segment's end point.
917 *
918 * miny ::
919 * A lower vertical clipping bound value.
920 *
921 * maxy ::
922 * An upper vertical clipping bound value.
923 *
924 * @Return:
925 * SUCCESS on success, FAILURE on render pool overflow.
926 */
927 static Bool
928 Line_Up( RAS_ARGS Long x1,
929 Long y1,
930 Long x2,
931 Long y2,
932 Long miny,
933 Long maxy )
934 {
935 Long e, e2, Dx, Dy;
936 Long Ix, Rx, Ax;
937 Int size;
938
939 PLong top;
940
941
942 if ( y2 < miny || y1 > maxy )
943 return SUCCESS;
944
945 e2 = y2 > maxy ? maxy : FLOOR( y2 );
946 e = y1 < miny ? miny : CEILING( y1 );
947
948 if ( y1 == e )
949 e += ras.precision;
950
951 if ( e2 < e ) /* nothing to do */
952 return SUCCESS;
953
954 size = (Int)TRUNC( e2 - e ) + 1;
955 top = ras.top;
956
957 if ( top + size >= ras.maxBuff )
958 {
959 ras.error = FT_THROW( Raster_Overflow );
960 return FAILURE;
961 }
962
963 Dx = x2 - x1;
964 Dy = y2 - y1;
965
966 if ( Dx == 0 ) /* very easy */
967 {
968 do
969 *top++ = x1;
970 while ( --size );
971 goto Fin;
972 }
973
974 Ix = SMulDiv( e - y1, Dx, Dy );
975 x1 += Ix;
976 *top++ = x1;
977
978 if ( --size )
979 {
980 Ax = Dx * ( e - y1 ) - Dy * Ix; /* remainder */
981 Ix = SMulDiv( ras.precision, Dx, Dy );
982 Rx = Dx * ras.precision - Dy * Ix; /* remainder */
983 Dx = 1;
984
985 if ( x2 < x1 )
986 {
987 Ax = -Ax;
988 Rx = -Rx;
989 Dx = -Dx;
990 }
991
992 do
993 {
994 x1 += Ix;
995 Ax += Rx;
996 if ( Ax >= Dy )
997 {
998 Ax -= Dy;
999 x1 += Dx;
1000 }
1001 *top++ = x1;
1002 }
1003 while ( --size );
1004 }
1005
1006 Fin:
1007 ras.top = top;
1008 return SUCCESS;
1009 }
1010
1011
1012 /**************************************************************************
1013 *
1014 * @Function:
1015 * Line_Down
1016 *
1017 * @Description:
1018 * Compute the x-coordinates of an descending line segment and store
1019 * them in the render pool.
1020 *
1021 * @Input:
1022 * x1 ::
1023 * The x-coordinate of the segment's start point.
1024 *
1025 * y1 ::
1026 * The y-coordinate of the segment's start point.
1027 *
1028 * x2 ::
1029 * The x-coordinate of the segment's end point.
1030 *
1031 * y2 ::
1032 * The y-coordinate of the segment's end point.
1033 *
1034 * miny ::
1035 * A lower vertical clipping bound value.
1036 *
1037 * maxy ::
1038 * An upper vertical clipping bound value.
1039 *
1040 * @Return:
1041 * SUCCESS on success, FAILURE on render pool overflow.
1042 */
1043 static Bool
1044 Line_Down( RAS_ARGS Long x1,
1045 Long y1,
1046 Long x2,
1047 Long y2,
1048 Long miny,
1049 Long maxy )
1050 {
1051 return Line_Up( RAS_VARS x1, -y1, x2, -y2, -maxy, -miny );
1052 }
1053
1054
1055 /* A function type describing the functions used to split Bezier arcs */
1056 typedef void (*TSplitter)( TPoint* base );
1057
1058
1059 /**************************************************************************
1060 *
1061 * @Function:
1062 * Bezier_Up
1063 *
1064 * @Description:
1065 * Compute the x-coordinates of an ascending Bezier arc and store
1066 * them in the render pool.
1067 *
1068 * @Input:
1069 * degree ::
1070 * The degree of the Bezier arc (either 2 or 3).
1071 *
1072 * splitter ::
1073 * The function to split Bezier arcs.
1074 *
1075 * miny ::
1076 * A lower vertical clipping bound value.
1077 *
1078 * maxy ::
1079 * An upper vertical clipping bound value.
1080 *
1081 * @Return:
1082 * SUCCESS on success, FAILURE on render pool overflow.
1083 */
1084 static Bool
1085 Bezier_Up( RAS_ARGS Int degree,
1086 TPoint* arc,
1087 TSplitter splitter,
1088 Long miny,
1089 Long maxy )
1090 {
1091 Long y1, y2, e, e2, dy;
1092 Long dx, x2;
1093
1094 PLong top;
1095
1096
1097 y1 = arc[degree].y;
1098 y2 = arc[0].y;
1099
1100 if ( y2 < miny || y1 > maxy )
1101 return SUCCESS;
1102
1103 e2 = y2 > maxy ? maxy : FLOOR( y2 );
1104 e = y1 < miny ? miny : CEILING( y1 );
1105
1106 if ( y1 == e )
1107 e += ras.precision;
1108
1109 if ( e2 < e ) /* nothing to do */
1110 return SUCCESS;
1111
1112 top = ras.top;
1113
1114 if ( ( top + TRUNC( e2 - e ) + 1 ) >= ras.maxBuff )
1115 {
1116 ras.error = FT_THROW( Raster_Overflow );
1117 return FAILURE;
1118 }
1119
1120 do
1121 {
1122 y2 = arc[0].y;
1123 x2 = arc[0].x;
1124
1125 if ( y2 > e )
1126 {
1127 dy = y2 - arc[degree].y;
1128 dx = x2 - arc[degree].x;
1129
1130 /* split condition should be invariant of direction */
1131 if ( dy > ras.precision_step ||
1132 dx > ras.precision_step ||
1133 -dx > ras.precision_step )
1134 {
1135 splitter( arc );
1136 arc += degree;
1137 }
1138 else
1139 {
1140 *top++ = x2 - FMulDiv( y2 - e, dx, dy );
1141 e += ras.precision;
1142 arc -= degree;
1143 }
1144 }
1145 else
1146 {
1147 if ( y2 == e )
1148 {
1149 *top++ = x2;
1150 e += ras.precision;
1151 }
1152 arc -= degree;
1153 }
1154 }
1155 while ( e <= e2 );
1156
1157 ras.top = top;
1158 return SUCCESS;
1159 }
1160
1161
1162 /**************************************************************************
1163 *
1164 * @Function:
1165 * Bezier_Down
1166 *
1167 * @Description:
1168 * Compute the x-coordinates of an descending Bezier arc and store
1169 * them in the render pool.
1170 *
1171 * @Input:
1172 * degree ::
1173 * The degree of the Bezier arc (either 2 or 3).
1174 *
1175 * splitter ::
1176 * The function to split Bezier arcs.
1177 *
1178 * miny ::
1179 * A lower vertical clipping bound value.
1180 *
1181 * maxy ::
1182 * An upper vertical clipping bound value.
1183 *
1184 * @Return:
1185 * SUCCESS on success, FAILURE on render pool overflow.
1186 */
1187 static Bool
1188 Bezier_Down( RAS_ARGS Int degree,
1189 TPoint* arc,
1190 TSplitter splitter,
1191 Long miny,
1192 Long maxy )
1193 {
1194 Bool result;
1195
1196
1197 arc[0].y = -arc[0].y;
1198 arc[1].y = -arc[1].y;
1199 arc[2].y = -arc[2].y;
1200 if ( degree > 2 )
1201 arc[3].y = -arc[3].y;
1202
1203 result = Bezier_Up( RAS_VARS degree, arc, splitter, -maxy, -miny );
1204
1205 arc[0].y = -arc[0].y;
1206 return result;
1207 }
1208
1209
1210 /**************************************************************************
1211 *
1212 * @Function:
1213 * Line_To
1214 *
1215 * @Description:
1216 * Inject a new line segment and adjust the Profiles list.
1217 *
1218 * @Input:
1219 * x ::
1220 * The x-coordinate of the segment's end point (its start point
1221 * is stored in `lastX').
1222 *
1223 * y ::
1224 * The y-coordinate of the segment's end point (its start point
1225 * is stored in `lastY').
1226 *
1227 * @Return:
1228 * SUCCESS on success, FAILURE on render pool overflow or incorrect
1229 * profile.
1230 */
1231 static Bool
1232 Line_To( RAS_ARGS Long x,
1233 Long y )
1234 {
1235 TStates state;
1236
1237
1238 if ( y == ras.lastY )
1239 goto Fin;
1240
1241 /* First, detect a change of direction */
1242
1243 state = ras.lastY < y ? Ascending_State : Descending_State;
1244
1245 if ( ras.state != state )
1246 {
1247 /* finalize current profile if any */
1248 if ( ras.state != Unknown_State &&
1249 End_Profile( RAS_VAR ) )
1250 goto Fail;
1251
1252 /* create a new profile */
1253 if ( New_Profile( RAS_VARS state ) )
1254 goto Fail;
1255 }
1256
1257 /* Then compute the lines */
1258
1259 if ( state == Ascending_State )
1260 {
1261 if ( Line_Up( RAS_VARS ras.lastX, ras.lastY,
1262 x, y, ras.minY, ras.maxY ) )
1263 goto Fail;
1264 }
1265 else
1266 {
1267 if ( Line_Down( RAS_VARS ras.lastX, ras.lastY,
1268 x, y, ras.minY, ras.maxY ) )
1269 goto Fail;
1270 }
1271
1272 Fin:
1273 ras.lastX = x;
1274 ras.lastY = y;
1275 return SUCCESS;
1276
1277 Fail:
1278 return FAILURE;
1279 }
1280
1281
1282 /**************************************************************************
1283 *
1284 * @Function:
1285 * Conic_To
1286 *
1287 * @Description:
1288 * Inject a new conic arc and adjust the profile list.
1289 *
1290 * @Input:
1291 * cx ::
1292 * The x-coordinate of the arc's new control point.
1293 *
1294 * cy ::
1295 * The y-coordinate of the arc's new control point.
1296 *
1297 * x ::
1298 * The x-coordinate of the arc's end point (its start point is
1299 * stored in `lastX').
1300 *
1301 * y ::
1302 * The y-coordinate of the arc's end point (its start point is
1303 * stored in `lastY').
1304 *
1305 * @Return:
1306 * SUCCESS on success, FAILURE on render pool overflow or incorrect
1307 * profile.
1308 */
1309 static Bool
1310 Conic_To( RAS_ARGS Long cx,
1311 Long cy,
1312 Long x,
1313 Long y )
1314 {
1315 Long y1, y2, y3, x3, ymin, ymax;
1316 TStates state_bez;
1317 TPoint arcs[2 * MaxBezier + 1]; /* The Bezier stack */
1318 TPoint* arc; /* current Bezier arc pointer */
1319
1320
1321 arc = arcs;
1322 arc[2].x = ras.lastX;
1323 arc[2].y = ras.lastY;
1324 arc[1].x = cx;
1325 arc[1].y = cy;
1326 arc[0].x = x;
1327 arc[0].y = y;
1328
1329 do
1330 {
1331 y1 = arc[2].y;
1332 y2 = arc[1].y;
1333 y3 = arc[0].y;
1334 x3 = arc[0].x;
1335
1336 /* first, categorize the Bezier arc */
1337
1338 if ( y1 <= y3 )
1339 {
1340 ymin = y1;
1341 ymax = y3;
1342 }
1343 else
1344 {
1345 ymin = y3;
1346 ymax = y1;
1347 }
1348
1349 if ( y2 < FLOOR( ymin ) || y2 > CEILING( ymax ) )
1350 {
1351 /* this arc has no given direction, split it! */
1352 Split_Conic( arc );
1353 arc += 2;
1354 continue;
1355 }
1356 else if ( y1 != y3 )
1357 {
1358 /* the arc is y-monotonous, either ascending or descending */
1359 /* detect a change of direction */
1360 state_bez = y1 < y3 ? Ascending_State : Descending_State;
1361 if ( ras.state != state_bez )
1362 {
1363 /* finalize current profile if any */
1364 if ( ras.state != Unknown_State &&
1365 End_Profile( RAS_VAR ) )
1366 goto Fail;
1367
1368 /* create a new profile */
1369 if ( New_Profile( RAS_VARS state_bez ) )
1370 goto Fail;
1371 }
1372
1373 /* now call the appropriate routine */
1374 if ( state_bez == Ascending_State )
1375 {
1376 if ( Bezier_Up( RAS_VARS 2, arc, Split_Conic,
1377 ras.minY, ras.maxY ) )
1378 goto Fail;
1379 }
1380 else
1381 if ( Bezier_Down( RAS_VARS 2, arc, Split_Conic,
1382 ras.minY, ras.maxY ) )
1383 goto Fail;
1384 }
1385
1386 ras.lastX = x3;
1387 ras.lastY = y3;
1388
1389 if ( arc == arcs )
1390 break;
1391 arc -= 2;
1392
1393 } while ( 1 );
1394
1395 return SUCCESS;
1396
1397 Fail:
1398 return FAILURE;
1399 }
1400
1401
1402 /**************************************************************************
1403 *
1404 * @Function:
1405 * Cubic_To
1406 *
1407 * @Description:
1408 * Inject a new cubic arc and adjust the profile list.
1409 *
1410 * @Input:
1411 * cx1 ::
1412 * The x-coordinate of the arc's first new control point.
1413 *
1414 * cy1 ::
1415 * The y-coordinate of the arc's first new control point.
1416 *
1417 * cx2 ::
1418 * The x-coordinate of the arc's second new control point.
1419 *
1420 * cy2 ::
1421 * The y-coordinate of the arc's second new control point.
1422 *
1423 * x ::
1424 * The x-coordinate of the arc's end point (its start point is
1425 * stored in `lastX').
1426 *
1427 * y ::
1428 * The y-coordinate of the arc's end point (its start point is
1429 * stored in `lastY').
1430 *
1431 * @Return:
1432 * SUCCESS on success, FAILURE on render pool overflow or incorrect
1433 * profile.
1434 */
1435 static Bool
1436 Cubic_To( RAS_ARGS Long cx1,
1437 Long cy1,
1438 Long cx2,
1439 Long cy2,
1440 Long x,
1441 Long y )
1442 {
1443 Long y1, y2, y3, y4, x4, ymin1, ymax1, ymin2, ymax2;
1444 TStates state_bez;
1445 TPoint arcs[3 * MaxBezier + 1]; /* The Bezier stack */
1446 TPoint* arc; /* current Bezier arc pointer */
1447
1448
1449 arc = arcs;
1450 arc[3].x = ras.lastX;
1451 arc[3].y = ras.lastY;
1452 arc[2].x = cx1;
1453 arc[2].y = cy1;
1454 arc[1].x = cx2;
1455 arc[1].y = cy2;
1456 arc[0].x = x;
1457 arc[0].y = y;
1458
1459 do
1460 {
1461 y1 = arc[3].y;
1462 y2 = arc[2].y;
1463 y3 = arc[1].y;
1464 y4 = arc[0].y;
1465 x4 = arc[0].x;
1466
1467 /* first, categorize the Bezier arc */
1468
1469 if ( y1 <= y4 )
1470 {
1471 ymin1 = y1;
1472 ymax1 = y4;
1473 }
1474 else
1475 {
1476 ymin1 = y4;
1477 ymax1 = y1;
1478 }
1479
1480 if ( y2 <= y3 )
1481 {
1482 ymin2 = y2;
1483 ymax2 = y3;
1484 }
1485 else
1486 {
1487 ymin2 = y3;
1488 ymax2 = y2;
1489 }
1490
1491 if ( ymin2 < FLOOR( ymin1 ) || ymax2 > CEILING( ymax1 ) )
1492 {
1493 /* this arc has no given direction, split it! */
1494 Split_Cubic( arc );
1495 arc += 3;
1496 continue;
1497 }
1498 else if ( y1 != y4 )
1499 {
1500 state_bez = y1 < y4 ? Ascending_State : Descending_State;
1501
1502 /* detect a change of direction */
1503 if ( ras.state != state_bez )
1504 {
1505 /* finalize current profile if any */
1506 if ( ras.state != Unknown_State &&
1507 End_Profile( RAS_VAR ) )
1508 goto Fail;
1509
1510 if ( New_Profile( RAS_VARS state_bez ) )
1511 goto Fail;
1512 }
1513
1514 /* compute intersections */
1515 if ( state_bez == Ascending_State )
1516 {
1517 if ( Bezier_Up( RAS_VARS 3, arc, Split_Cubic,
1518 ras.minY, ras.maxY ) )
1519 goto Fail;
1520 }
1521 else
1522 if ( Bezier_Down( RAS_VARS 3, arc, Split_Cubic,
1523 ras.minY, ras.maxY ) )
1524 goto Fail;
1525 }
1526
1527 ras.lastX = x4;
1528 ras.lastY = y4;
1529
1530 if ( arc == arcs )
1531 break;
1532 arc -= 3;
1533
1534 } while ( 1 );
1535
1536 return SUCCESS;
1537
1538 Fail:
1539 return FAILURE;
1540 }
1541
1542
1543#undef SWAP_
1544#define SWAP_( x, y ) do \
1545 { \
1546 Long swap = x; \
1547 \
1548 \
1549 x = y; \
1550 y = swap; \
1551 } while ( 0 )
1552
1553
1554 /**************************************************************************
1555 *
1556 * @Function:
1557 * Decompose_Curve
1558 *
1559 * @Description:
1560 * Scan the outline arrays in order to emit individual segments and
1561 * Beziers by calling Line_To() and Bezier_To(). It handles all
1562 * weird cases, like when the first point is off the curve, or when
1563 * there are simply no `on' points in the contour!
1564 *
1565 * @Input:
1566 * first ::
1567 * The index of the first point in the contour.
1568 *
1569 * last ::
1570 * The index of the last point in the contour.
1571 *
1572 * flipped ::
1573 * If set, flip the direction of the curve.
1574 *
1575 * @Return:
1576 * SUCCESS on success, FAILURE on error.
1577 *
1578 * @Note:
1579 * Unlike FT_Outline_Decompose(), this function handles the scanmode
1580 * dropout tags in the individual contours. Therefore, it cannot be
1581 * replaced.
1582 */
1583 static Bool
1584 Decompose_Curve( RAS_ARGS Int first,
1585 Int last,
1586 Int flipped )
1587 {
1588 FT_Vector v_last;
1589 FT_Vector v_control;
1590 FT_Vector v_start;
1591
1592 FT_Vector* points;
1593 FT_Vector* point;
1594 FT_Vector* limit;
1595 FT_Byte* tags;
1596
1597 UInt tag; /* current point's state */
1598
1599
1600 points = ras.outline.points;
1601 limit = points + last;
1602
1603 v_start.x = SCALED( points[first].x );
1604 v_start.y = SCALED( points[first].y );
1605 v_last.x = SCALED( points[last].x );
1606 v_last.y = SCALED( points[last].y );
1607
1608 if ( flipped )
1609 {
1610 SWAP_( v_start.x, v_start.y );
1611 SWAP_( v_last.x, v_last.y );
1612 }
1613
1614 v_control = v_start;
1615
1616 point = points + first;
1617 tags = ras.outline.tags + first;
1618
1619 /* set scan mode if necessary */
1620 if ( tags[0] & FT_CURVE_TAG_HAS_SCANMODE )
1621 ras.dropOutControl = (Byte)tags[0] >> 5;
1622
1623 tag = FT_CURVE_TAG( tags[0] );
1624
1625 /* check first point to determine origin */
1626 if ( tag != FT_CURVE_TAG_ON )
1627 {
1628 UInt last_tag = FT_CURVE_TAG( ras.outline.tags[last] );
1629
1630
1631 /* first point is a control point. Yes, this happens. */
1632 if ( last_tag == FT_CURVE_TAG_ON )
1633 {
1634 /* start at last point if it is on the curve */
1635 v_start = v_last;
1636 limit--;
1637 }
1638 else
1639 {
1640 if ( last_tag != tag )
1641 goto Invalid_Outline;
1642
1643 /* if both first and last points are controls, */
1644 /* start at their middle and record its position */
1645 /* for closure */
1646 v_start.x = ( v_start.x + v_last.x ) / 2;
1647 v_start.y = ( v_start.y + v_last.y ) / 2;
1648
1649 /* v_last = v_start; */
1650 }
1651 point--;
1652 tags--;
1653 }
1654
1655 ras.lastX = v_start.x;
1656 ras.lastY = v_start.y;
1657
1658 while ( point < limit )
1659 {
1660 point++;
1661 tags++;
1662
1663 tag = FT_CURVE_TAG( tags[0] );
1664
1665 switch ( tag )
1666 {
1667 case FT_CURVE_TAG_ON: /* emit a single line_to */
1668 {
1669 Long x, y;
1670
1671
1672 x = SCALED( point->x );
1673 y = SCALED( point->y );
1674 if ( flipped )
1675 SWAP_( x, y );
1676
1677 if ( Line_To( RAS_VARS x, y ) )
1678 goto Fail;
1679 continue;
1680 }
1681
1682 case FT_CURVE_TAG_CONIC: /* consume conic arcs */
1683 v_control.x = SCALED( point[0].x );
1684 v_control.y = SCALED( point[0].y );
1685
1686 if ( flipped )
1687 SWAP_( v_control.x, v_control.y );
1688
1689 Do_Conic:
1690 if ( point < limit )
1691 {
1692 FT_Vector v_middle;
1693 Long x, y;
1694
1695
1696 point++;
1697 tags++;
1698 tag = FT_CURVE_TAG( tags[0] );
1699
1700 x = SCALED( point[0].x );
1701 y = SCALED( point[0].y );
1702
1703 if ( flipped )
1704 SWAP_( x, y );
1705
1706 if ( tag == FT_CURVE_TAG_ON )
1707 {
1708 if ( Conic_To( RAS_VARS v_control.x, v_control.y, x, y ) )
1709 goto Fail;
1710 continue;
1711 }
1712
1713 if ( tag != FT_CURVE_TAG_CONIC )
1714 goto Invalid_Outline;
1715
1716 v_middle.x = ( v_control.x + x ) / 2;
1717 v_middle.y = ( v_control.y + y ) / 2;
1718
1719 if ( Conic_To( RAS_VARS v_control.x, v_control.y,
1720 v_middle.x, v_middle.y ) )
1721 goto Fail;
1722
1723 v_control.x = x;
1724 v_control.y = y;
1725
1726 goto Do_Conic;
1727 }
1728
1729 if ( Conic_To( RAS_VARS v_control.x, v_control.y,
1730 v_start.x, v_start.y ) )
1731 goto Fail;
1732
1733 goto Close;
1734
1735 default: /* FT_CURVE_TAG_CUBIC */
1736 {
1737 Long x1, y1, x2, y2, x3, y3;
1738
1739
1740 if ( point + 1 > limit ||
1741 FT_CURVE_TAG( tags[1] ) != FT_CURVE_TAG_CUBIC )
1742 goto Invalid_Outline;
1743
1744 point += 2;
1745 tags += 2;
1746
1747 x1 = SCALED( point[-2].x );
1748 y1 = SCALED( point[-2].y );
1749 x2 = SCALED( point[-1].x );
1750 y2 = SCALED( point[-1].y );
1751
1752 if ( flipped )
1753 {
1754 SWAP_( x1, y1 );
1755 SWAP_( x2, y2 );
1756 }
1757
1758 if ( point <= limit )
1759 {
1760 tag = FT_CURVE_TAG( tags[0] );
1761
1762 x3 = SCALED( point[0].x );
1763 y3 = SCALED( point[0].y );
1764
1765 if ( flipped )
1766 SWAP_( x3, y3 );
1767
1768 if ( tag == FT_CURVE_TAG_CUBIC )
1769 {
1770 x3 = ( x2 + x3 ) / 2;
1771 y3 = ( y2 + y3 ) / 2;
1772 }
1773 else if ( tag != FT_CURVE_TAG_ON )
1774 goto Invalid_Outline;
1775
1776 if ( Cubic_To( RAS_VARS x1, y1, x2, y2, x3, y3 ) )
1777 goto Fail;
1778
1779 if ( tag == FT_CURVE_TAG_CUBIC )
1780 {
1781 point--;
1782 tags--;
1783 }
1784 continue;
1785 }
1786
1787 if ( Cubic_To( RAS_VARS x1, y1, x2, y2, v_start.x, v_start.y ) )
1788 goto Fail;
1789 goto Close;
1790 }
1791 }
1792 }
1793
1794 /* close the contour with a line segment */
1795 if ( Line_To( RAS_VARS v_start.x, v_start.y ) )
1796 goto Fail;
1797
1798 Close:
1799 return SUCCESS;
1800
1801 Invalid_Outline:
1802 ras.error = FT_THROW( Invalid_Outline );
1803
1804 Fail:
1805 return FAILURE;
1806 }
1807
1808
1809 /**************************************************************************
1810 *
1811 * @Function:
1812 * Convert_Glyph
1813 *
1814 * @Description:
1815 * Convert a glyph into a series of segments and arcs and make a
1816 * profiles list with them.
1817 *
1818 * @Input:
1819 * flipped ::
1820 * If set, flip the direction of curve.
1821 *
1822 * @Return:
1823 * SUCCESS on success, FAILURE if any error was encountered during
1824 * rendering.
1825 */
1826 static Bool
1827 Convert_Glyph( RAS_ARGS Int flipped )
1828 {
1829 Int i;
1830 Int first, last;
1831
1832
1833 ras.fProfile = NULL;
1834 ras.cProfile = NULL;
1835
1836 ras.top = ras.buff;
1837 ras.maxBuff = ras.sizeBuff - 1; /* top reserve */
1838
1839 ras.numTurns = 0;
1840 ras.num_Profs = 0;
1841
1842 last = -1;
1843 for ( i = 0; i < ras.outline.n_contours; i++ )
1844 {
1845 ras.state = Unknown_State;
1846 ras.gProfile = NULL;
1847
1848 first = last + 1;
1849 last = ras.outline.contours[i];
1850
1851 if ( Decompose_Curve( RAS_VARS first, last, flipped ) )
1852 return FAILURE;
1853
1854 /* Note that ras.gProfile can stay nil if the contour was */
1855 /* too small to be drawn or degenerate. */
1856 if ( !ras.gProfile )
1857 continue;
1858
1859 /* we must now check whether the extreme arcs join or not */
1860 if ( FRAC( ras.lastY ) == 0 &&
1861 ras.lastY >= ras.minY &&
1862 ras.lastY <= ras.maxY )
1863 if ( ( ras.gProfile->flags & Flow_Up ) ==
1864 ( ras.cProfile->flags & Flow_Up ) )
1865 ras.top--;
1866
1867 if ( End_Profile( RAS_VAR ) )
1868 return FAILURE;
1869
1870 if ( !ras.fProfile )
1871 ras.fProfile = ras.gProfile;
1872 }
1873
1874 if ( ras.fProfile )
1875 Finalize_Profile_Table( RAS_VAR );
1876
1877 return SUCCESS;
1878 }
1879
1880
1881 /*************************************************************************/
1882 /*************************************************************************/
1883 /** **/
1884 /** SCAN-LINE SWEEPS AND DRAWING **/
1885 /** **/
1886 /*************************************************************************/
1887 /*************************************************************************/
1888
1889
1890 /**************************************************************************
1891 *
1892 * InsNew
1893 *
1894 * Inserts a new profile in a linked list, sorted by coordinate.
1895 */
1896 static void
1897 InsNew( PProfileList list,
1898 PProfile profile )
1899 {
1900 PProfile *old, current;
1901 Long x;
1902
1903
1904 old = list;
1905 current = *old;
1906 x = profile->X;
1907
1908 while ( current && current->X < x )
1909 {
1910 old = &current->link;
1911 current = *old;
1912 }
1913
1914 profile->link = current;
1915 *old = profile;
1916 }
1917
1918
1919 /**************************************************************************
1920 *
1921 * Increment
1922 *
1923 * Advances all profile in the list to the next scanline. It also
1924 * sorts the trace list in the unlikely case of profile crossing.
1925 * The profiles are inserted in sorted order. We might need a single
1926 * swap to fix it when profiles (contours) cross.
1927 * Bubble sort with immediate restart is good enough and simple.
1928 */
1929 static void
1930 Increment( PProfileList list,
1931 Int flow )
1932 {
1933 PProfile *old, current, next;
1934
1935
1936 /* First, set the new X coordinates and remove exhausted profiles */
1937 old = list;
1938 while ( *old )
1939 {
1940 current = *old;
1941 if ( --current->height )
1942 {
1943 current->offset += flow;
1944 current->X = current->x[current->offset];
1945 old = &current->link;
1946 }
1947 else
1948 *old = current->link; /* remove */
1949 }
1950
1951 /* Then make sure the list remains sorted */
1952 old = list;
1953 current = *old;
1954
1955 if ( !current )
1956 return;
1957
1958 while ( current->link )
1959 {
1960 next = current->link;
1961
1962 if ( current->X <= next->X )
1963 {
1964 old = &current->link;
1965 current = next;
1966 }
1967 else
1968 {
1969 *old = next;
1970 current->link = next->link;
1971 next->link = current;
1972
1973 /* this is likely the only necessary swap -- restart */
1974 old = list;
1975 current = *old;
1976 }
1977 }
1978 }
1979
1980
1981 /**************************************************************************
1982 *
1983 * Vertical Sweep Procedure Set
1984 *
1985 * These four routines are used during the vertical black/white sweep
1986 * phase by the generic Draw_Sweep() function.
1987 *
1988 */
1989
1990 static void
1991 Vertical_Sweep_Init( RAS_ARGS Int min,
1992 Int max )
1993 {
1994 FT_UNUSED( max );
1995
1996
1997 ras.bLine = ras.bOrigin - min * ras.bPitch;
1998 }
1999
2000
2001 static void
2002 Vertical_Sweep_Span( RAS_ARGS Int y,
2003 FT_F26Dot6 x1,
2004 FT_F26Dot6 x2 )
2005 {
2006 Int e1 = (Int)TRUNC( CEILING( x1 ) );
2007 Int e2 = (Int)TRUNC( FLOOR( x2 ) );
2008
2009 FT_UNUSED( y );
2010
2011
2012 FT_TRACE7(( " y=%d x=[% .*f;% .*f]",
2013 y,
2014 ras.precision_bits, (double)x1 / (double)ras.precision,
2015 ras.precision_bits, (double)x2 / (double)ras.precision ));
2016
2017 if ( e2 >= 0 && e1 <= ras.bRight )
2018 {
2019 PByte target;
2020
2021 Int c1, f1, c2, f2;
2022
2023
2024 if ( e1 < 0 )
2025 e1 = 0;
2026 if ( e2 > ras.bRight )
2027 e2 = ras.bRight;
2028
2029 FT_TRACE7(( " -> x=[%d;%d]", e1, e2 ));
2030
2031 c1 = e1 >> 3;
2032 c2 = e2 >> 3;
2033
2034 f1 = 0xFF >> ( e1 & 7 );
2035 f2 = ~0x7F >> ( e2 & 7 );
2036
2037 target = ras.bLine + c1;
2038 c2 -= c1;
2039
2040 if ( c2 > 0 )
2041 {
2042 target[0] |= f1;
2043
2044 /* memset() is slower than the following code on many platforms. */
2045 /* This is due to the fact that, in the vast majority of cases, */
2046 /* the span length in bytes is relatively small. */
2047 while ( --c2 > 0 )
2048 *( ++target ) = 0xFF;
2049
2050 target[1] |= f2;
2051 }
2052 else
2053 *target |= ( f1 & f2 );
2054 }
2055
2056 FT_TRACE7(( "\n" ));
2057 }
2058
2059
2060 static void
2061 Vertical_Sweep_Drop( RAS_ARGS Int y,
2062 FT_F26Dot6 x1,
2063 FT_F26Dot6 x2 )
2064 {
2065 Int e1 = (Int)TRUNC( x1 );
2066 Int e2 = (Int)TRUNC( x2 );
2067 Int c1, f1;
2068
2069 FT_UNUSED( y );
2070
2071
2072 /* undocumented but confirmed: If the drop-out would result in a */
2073 /* pixel outside of the bounding box, use the pixel inside of the */
2074 /* bounding box instead */
2075 if ( e1 < 0 || e1 > ras.bRight )
2076 e1 = e2;
2077
2078 /* otherwise check that the other pixel isn't set */
2079 else if ( e2 >=0 && e2 <= ras.bRight )
2080 {
2081 c1 = e2 >> 3;
2082 f1 = 0x80 >> ( e2 & 7 );
2083
2084 if ( ras.bLine[c1] & f1 )
2085 return;
2086 }
2087
2088 if ( e1 >= 0 && e1 <= ras.bRight )
2089 {
2090 c1 = e1 >> 3;
2091 f1 = 0x80 >> ( e1 & 7 );
2092
2093 FT_TRACE7(( " y=%d x=%d%s\n", y, e1,
2094 ras.bLine[c1] & f1 ? " redundant" : "" ));
2095
2096 ras.bLine[c1] |= f1;
2097 }
2098 }
2099
2100
2101 static void
2102 Vertical_Sweep_Step( RAS_ARG )
2103 {
2104 ras.bLine -= ras.bPitch;
2105 }
2106
2107
2108 /************************************************************************
2109 *
2110 * Horizontal Sweep Procedure Set
2111 *
2112 * These four routines are used during the horizontal black/white
2113 * sweep phase by the generic Draw_Sweep() function.
2114 *
2115 */
2116
2117 static void
2118 Horizontal_Sweep_Init( RAS_ARGS Int min,
2119 Int max )
2120 {
2121 /* nothing, really */
2122 FT_UNUSED_RASTER;
2123 FT_UNUSED( min );
2124 FT_UNUSED( max );
2125 }
2126
2127
2128 static void
2129 Horizontal_Sweep_Span( RAS_ARGS Int y,
2130 FT_F26Dot6 x1,
2131 FT_F26Dot6 x2 )
2132 {
2133 Long e1 = CEILING( x1 );
2134 Long e2 = FLOOR( x2 );
2135
2136
2137 FT_TRACE7(( " x=%d y=[% .*f;% .*f]",
2138 y,
2139 ras.precision_bits, (double)x1 / (double)ras.precision,
2140 ras.precision_bits, (double)x2 / (double)ras.precision ));
2141
2142 /* We should not need this procedure but the vertical sweep */
2143 /* mishandles horizontal lines through pixel centers. So we */
2144 /* have to check perfectly aligned span edges here. */
2145 /* */
2146 /* XXX: Can we handle horizontal lines better and drop this? */
2147
2148 if ( x1 == e1 )
2149 {
2150 e1 = TRUNC( e1 );
2151
2152 if ( e1 >= 0 && e1 <= ras.bTop )
2153 {
2154 Int f1;
2155 PByte bits;
2156
2157
2158 bits = ras.bOrigin + ( y >> 3 ) - e1 * ras.bPitch;
2159 f1 = 0x80 >> ( y & 7 );
2160
2161 FT_TRACE7(( bits[0] & f1 ? " redundant"
2162 : " -> y=%ld edge", e1 ));
2163
2164 bits[0] |= f1;
2165 }
2166 }
2167
2168 if ( x2 == e2 )
2169 {
2170 e2 = TRUNC( e2 );
2171
2172 if ( e2 >= 0 && e2 <= ras.bTop )
2173 {
2174 Int f1;
2175 PByte bits;
2176
2177
2178 bits = ras.bOrigin + ( y >> 3 ) - e2 * ras.bPitch;
2179 f1 = 0x80 >> ( y & 7 );
2180
2181 FT_TRACE7(( bits[0] & f1 ? " redundant"
2182 : " -> y=%ld edge", e2 ));
2183
2184 bits[0] |= f1;
2185 }
2186 }
2187
2188 FT_TRACE7(( "\n" ));
2189 }
2190
2191
2192 static void
2193 Horizontal_Sweep_Drop( RAS_ARGS Int y,
2194 FT_F26Dot6 x1,
2195 FT_F26Dot6 x2 )
2196 {
2197 Int e1 = (Int)TRUNC( x1 );
2198 Int e2 = (Int)TRUNC( x2 );
2199 PByte bits;
2200 Int f1;
2201
2202
2203 /* undocumented but confirmed: If the drop-out would result in a */
2204 /* pixel outside of the bounding box, use the pixel inside of the */
2205 /* bounding box instead */
2206 if ( e1 < 0 || e1 > ras.bTop )
2207 e1 = e2;
2208
2209 /* otherwise check that the other pixel isn't set */
2210 else if ( e2 >=0 && e2 <= ras.bTop )
2211 {
2212 bits = ras.bOrigin + ( y >> 3 ) - e2 * ras.bPitch;
2213 f1 = 0x80 >> ( y & 7 );
2214
2215 if ( *bits & f1 )
2216 return;
2217 }
2218
2219 if ( e1 >= 0 && e1 <= ras.bTop )
2220 {
2221 bits = ras.bOrigin + ( y >> 3 ) - e1 * ras.bPitch;
2222 f1 = 0x80 >> ( y & 7 );
2223
2224 FT_TRACE7(( " x=%d y=%d%s\n", y, e1,
2225 *bits & f1 ? " redundant" : "" ));
2226
2227 *bits |= f1;
2228 }
2229 }
2230
2231
2232 static void
2233 Horizontal_Sweep_Step( RAS_ARG )
2234 {
2235 /* Nothing, really */
2236 FT_UNUSED_RASTER;
2237 }
2238
2239
2240 /**************************************************************************
2241 *
2242 * Generic Sweep Drawing routine
2243 *
2244 * Note that this routine is executed with the pool containing at least
2245 * two valid profiles (up and down) and two y-turns (top and bottom).
2246 *
2247 */
2248
2249 static void
2250 Draw_Sweep( RAS_ARG )
2251 {
2252 Int min_Y, max_Y, dropouts;
2253 Int y, y_turn;
2254
2255 PProfile *Q, P, P_Left, P_Right;
2256
2257 TProfileList waiting = ras.fProfile;
2258 TProfileList draw_left = NULL;
2259 TProfileList draw_right = NULL;
2260
2261
2262 /* use y_turns to set the drawing range */
2263
2264 min_Y = (Int)ras.maxBuff[0];
2265 max_Y = (Int)ras.maxBuff[ras.numTurns] - 1;
2266
2267 /* now initialize the sweep */
2268
2269 ras.Proc_Sweep_Init( RAS_VARS min_Y, max_Y );
2270
2271 /* let's go */
2272
2273 for ( y = min_Y; y <= max_Y; )
2274 {
2275 /* check waiting list for new profile activations */
2276
2277 Q = &waiting;
2278 while ( *Q )
2279 {
2280 P = *Q;
2281 if ( P->start == y )
2282 {
2283 *Q = P->link; /* remove */
2284
2285 /* each active list contains profiles with the same flow */
2286 /* left and right are arbitrary, correspond to TrueType */
2287 if ( P->flags & Flow_Up )
2288 InsNew( &draw_left, P );
2289 else
2290 InsNew( &draw_right, P );
2291 }
2292 else
2293 Q = &P->link;
2294 }
2295
2296 y_turn = (Int)*++ras.maxBuff;
2297
2298 do
2299 {
2300 /* let's trace */
2301
2302 dropouts = 0;
2303
2304 P_Left = draw_left;
2305 P_Right = draw_right;
2306
2307 while ( P_Left && P_Right )
2308 {
2309 Long x1 = P_Left ->X;
2310 Long x2 = P_Right->X;
2311 Long xs;
2312
2313
2314 /* TrueType should have x2 > x1, but can be opposite */
2315 /* by mistake or in CFF/Type1, fix it then */
2316 if ( x1 > x2 )
2317 {
2318 xs = x1;
2319 x1 = x2;
2320 x2 = xs;
2321 }
2322
2323 if ( CEILING( x1 ) <= FLOOR( x2 ) )
2324 ras.Proc_Sweep_Span( RAS_VARS y, x1, x2 );
2325
2326 /* otherwise, bottom ceiling > top floor, it is a drop-out */
2327 else
2328 {
2329 Int dropOutControl = P_Left->flags & 7;
2330
2331
2332 /* Drop-out control */
2333
2334 /* e2 x2 x1 e1 */
2335 /* */
2336 /* ^ | */
2337 /* | | */
2338 /* +-------------+---------------------+------------+ */
2339 /* | | */
2340 /* | v */
2341 /* */
2342 /* pixel contour contour pixel */
2343 /* center center */
2344
2345 /* drop-out mode scan conversion rules (OpenType specs) */
2346 /* ------------------------------------------------------- */
2347 /* bit 0 exclude stubs if set */
2348 /* bit 1 ignore drop-outs if set */
2349 /* bit 2 smart rounding if set */
2350
2351 if ( dropOutControl & 2 )
2352 goto Next_Pair;
2353
2354 /* The specification neither provides an exact definition */
2355 /* of a `stub' nor gives exact rules to exclude them. */
2356 /* */
2357 /* Here the constraints we use to recognize a stub. */
2358 /* */
2359 /* upper stub: */
2360 /* */
2361 /* - P_Left and P_Right are in the same contour */
2362 /* - P_Right is the successor of P_Left in that contour */
2363 /* - y is the top of P_Left and P_Right */
2364 /* */
2365 /* lower stub: */
2366 /* */
2367 /* - P_Left and P_Right are in the same contour */
2368 /* - P_Left is the successor of P_Right in that contour */
2369 /* - y is the bottom of P_Left */
2370 /* */
2371 /* We draw a stub if the following constraints are met. */
2372 /* */
2373 /* - for an upper or lower stub, there is top or bottom */
2374 /* overshoot, respectively */
2375 /* - the covered interval is greater or equal to a half */
2376 /* pixel */
2377
2378 if ( dropOutControl & 1 )
2379 {
2380 /* upper stub test */
2381 if ( P_Left->height == 1 &&
2382 P_Left->next == P_Right &&
2383 !( P_Left->flags & Overshoot_Top &&
2384 x2 - x1 >= ras.precision_half ) )
2385 goto Next_Pair;
2386
2387 /* lower stub test */
2388 if ( P_Left->offset == 0 &&
2389 P_Right->next == P_Left &&
2390 !( P_Left->flags & Overshoot_Bottom &&
2391 x2 - x1 >= ras.precision_half ) )
2392 goto Next_Pair;
2393 }
2394
2395 /* select the pixel to set and the other pixel */
2396 if ( dropOutControl & 4 )
2397 {
2398 x2 = SMART( x1, x2 );
2399 x1 = x1 > x2 ? x2 + ras.precision : x2 - ras.precision;
2400 }
2401 else
2402 {
2403 x2 = FLOOR ( x2 );
2404 x1 = CEILING( x1 );
2405 }
2406
2407 P_Left ->X = x2;
2408 P_Right->X = x1;
2409
2410 /* mark profile for drop-out processing */
2411 P_Left->flags |= Dropout;
2412 dropouts++;
2413 }
2414
2415 Next_Pair:
2416 P_Left = P_Left->link;
2417 P_Right = P_Right->link;
2418 }
2419
2420 /* handle drop-outs _after_ the span drawing */
2421 P_Left = draw_left;
2422 P_Right = draw_right;
2423
2424 while ( dropouts )
2425 {
2426 if ( P_Left->flags & Dropout )
2427 {
2428 ras.Proc_Sweep_Drop( RAS_VARS y, P_Left->X, P_Right->X );
2429
2430 P_Left->flags &= ~Dropout;
2431 dropouts--;
2432 }
2433
2434 P_Left = P_Left->link;
2435 P_Right = P_Right->link;
2436 }
2437
2438 ras.Proc_Sweep_Step( RAS_VAR );
2439
2440 Increment( &draw_left, 1 );
2441 Increment( &draw_right, -1 );
2442 }
2443 while ( ++y < y_turn );
2444 }
2445 }
2446
2447
2448 /**************************************************************************
2449 *
2450 * @Function:
2451 * Render_Single_Pass
2452 *
2453 * @Description:
2454 * Perform one sweep with sub-banding.
2455 *
2456 * @Input:
2457 * flipped ::
2458 * If set, flip the direction of the outline.
2459 *
2460 * @Return:
2461 * Renderer error code.
2462 */
2463 static int
2464 Render_Single_Pass( RAS_ARGS Bool flipped,
2465 Int y_min,
2466 Int y_max )
2467 {
2468 Int band_stack[32]; /* enough to bisect 32-bit int bands */
2469 Int* band = band_stack;
2470
2471
2472 FT_TRACE6(( "%s pass [%d..%d]\n",
2473 flipped ? "Horizontal" : "Vertical",
2474 y_min, y_max ));
2475
2476 while ( 1 )
2477 {
2478 ras.minY = (Long)y_min * ras.precision;
2479 ras.maxY = (Long)y_max * ras.precision;
2480
2481 ras.error = Raster_Err_Ok;
2482
2483 if ( Convert_Glyph( RAS_VARS flipped ) )
2484 {
2485 if ( ras.error != Raster_Err_Raster_Overflow )
2486 return ras.error;
2487
2488 /* sub-banding */
2489
2490 if ( y_min == y_max )
2491 return ras.error; /* still Raster_Overflow */
2492
2493 FT_TRACE6(( "band [%d..%d]: to be bisected\n",
2494 y_min, y_max ));
2495
2496 *band++ = y_min;
2497 y_min = ( y_min + y_max + 1 ) >> 1;
2498 }
2499 else
2500 {
2501 FT_TRACE6(( "band [%d..%d]: %hd profiles; %td bytes remaining\n",
2502 y_min, y_max, ras.num_Profs,
2503 (char*)ras.maxBuff - (char*)ras.top ));
2504
2505 if ( ras.fProfile )
2506 Draw_Sweep( RAS_VAR );
2507
2508 if ( band == band_stack )
2509 break; /* done */
2510
2511 y_max = y_min - 1;
2512 y_min = *--band;
2513 }
2514 }
2515
2516 return Raster_Err_Ok;
2517 }
2518
2519
2520 /**************************************************************************
2521 *
2522 * @Function:
2523 * Render_Glyph
2524 *
2525 * @Description:
2526 * Render a glyph in a bitmap. Sub-banding if needed.
2527 *
2528 * @Return:
2529 * FreeType error code. 0 means success.
2530 */
2531 static FT_Error
2532 Render_Glyph( RAS_ARG )
2533 {
2534 FT_Error error;
2535
2536
2537 Set_High_Precision( RAS_VARS ras.outline.flags &
2538 FT_OUTLINE_HIGH_PRECISION );
2539
2540 ras.dropOutControl = 0;
2541
2542 if ( ras.outline.flags & FT_OUTLINE_IGNORE_DROPOUTS )
2543 ras.dropOutControl |= 2;
2544
2545 if ( ras.outline.flags & FT_OUTLINE_SMART_DROPOUTS )
2546 ras.dropOutControl |= 4;
2547
2548 if ( !( ras.outline.flags & FT_OUTLINE_INCLUDE_STUBS ) )
2549 ras.dropOutControl |= 1;
2550
2551 FT_TRACE6(( "BW Raster: precision 1/%d, dropout mode %d\n",
2552 ras.precision, ras.dropOutControl ));
2553
2554 /* Vertical Sweep */
2555 ras.Proc_Sweep_Init = Vertical_Sweep_Init;
2556 ras.Proc_Sweep_Span = Vertical_Sweep_Span;
2557 ras.Proc_Sweep_Drop = Vertical_Sweep_Drop;
2558 ras.Proc_Sweep_Step = Vertical_Sweep_Step;
2559
2560 error = Render_Single_Pass( RAS_VARS 0, 0, ras.bTop );
2561 if ( error )
2562 return error;
2563
2564 /* Horizontal Sweep */
2565 if ( !( ras.outline.flags & FT_OUTLINE_SINGLE_PASS ) )
2566 {
2567 ras.Proc_Sweep_Init = Horizontal_Sweep_Init;
2568 ras.Proc_Sweep_Span = Horizontal_Sweep_Span;
2569 ras.Proc_Sweep_Drop = Horizontal_Sweep_Drop;
2570 ras.Proc_Sweep_Step = Horizontal_Sweep_Step;
2571
2572 error = Render_Single_Pass( RAS_VARS 1, 0, ras.bRight );
2573 if ( error )
2574 return error;
2575 }
2576
2577 return Raster_Err_Ok;
2578 }
2579
2580
2581 /**** RASTER OBJECT CREATION: In standalone mode, we simply use *****/
2582 /**** a static object. *****/
2583
2584
2585#ifdef STANDALONE_
2586
2587
2588 static int
2589 ft_black_new( void* memory,
2590 FT_Raster *araster )
2591 {
2592 static black_TRaster the_raster;
2593 FT_UNUSED( memory );
2594
2595
2596 *araster = (FT_Raster)&the_raster;
2597 FT_ZERO( &the_raster );
2598
2599 return 0;
2600 }
2601
2602
2603 static void
2604 ft_black_done( FT_Raster raster )
2605 {
2606 /* nothing */
2607 FT_UNUSED( raster );
2608 }
2609
2610
2611#else /* !STANDALONE_ */
2612
2613
2614 static int
2615 ft_black_new( void* memory_, /* FT_Memory */
2616 FT_Raster *araster_ ) /* black_PRaster */
2617 {
2618 FT_Memory memory = (FT_Memory)memory_;
2619 black_PRaster *araster = (black_PRaster*)araster_;
2620
2621 FT_Error error;
2622 black_PRaster raster = NULL;
2623
2624
2625 if ( !FT_NEW( raster ) )
2626 raster->memory = memory;
2627
2628 *araster = raster;
2629
2630 return error;
2631 }
2632
2633
2634 static void
2635 ft_black_done( FT_Raster raster_ ) /* black_PRaster */
2636 {
2637 black_PRaster raster = (black_PRaster)raster_;
2638 FT_Memory memory = (FT_Memory)raster->memory;
2639
2640
2641 FT_FREE( raster );
2642 }
2643
2644
2645#endif /* !STANDALONE_ */
2646
2647
2648 static void
2649 ft_black_reset( FT_Raster raster,
2650 PByte pool_base,
2651 ULong pool_size )
2652 {
2653 FT_UNUSED( raster );
2654 FT_UNUSED( pool_base );
2655 FT_UNUSED( pool_size );
2656 }
2657
2658
2659 static int
2660 ft_black_set_mode( FT_Raster raster,
2661 ULong mode,
2662 void* args )
2663 {
2664 FT_UNUSED( raster );
2665 FT_UNUSED( mode );
2666 FT_UNUSED( args );
2667
2668 return 0;
2669 }
2670
2671
2672 static int
2673 ft_black_render( FT_Raster raster,
2674 const FT_Raster_Params* params )
2675 {
2676 const FT_Outline* outline = (const FT_Outline*)params->source;
2677 const FT_Bitmap* target_map = params->target;
2678
2679 FT_ULong estimate;
2680 int ret;
2681
2682#ifndef FT_STATIC_RASTER
2683 black_TWorker worker[1];
2684#endif
2685
2686
2687 if ( !raster )
2688 return FT_THROW( Raster_Uninitialized );
2689
2690 if ( !outline )
2691 return FT_THROW( Invalid_Outline );
2692
2693 /* return immediately if the outline is empty */
2694 if ( outline->n_points == 0 || outline->n_contours == 0 )
2695 return Raster_Err_Ok;
2696
2697 if ( !outline->contours || !outline->points )
2698 return FT_THROW( Invalid_Outline );
2699
2700 if ( outline->n_points !=
2701 outline->contours[outline->n_contours - 1] + 1 )
2702 return FT_THROW( Invalid_Outline );
2703
2704 /* this version of the raster does not support direct rendering, sorry */
2705 if ( params->flags & FT_RASTER_FLAG_DIRECT ||
2706 params->flags & FT_RASTER_FLAG_AA )
2707 return FT_THROW( Cannot_Render_Glyph );
2708
2709 if ( !target_map )
2710 return FT_THROW( Invalid_Argument );
2711
2712 /* nothing to do */
2713 if ( !target_map->width || !target_map->rows )
2714 return Raster_Err_Ok;
2715
2716 if ( !target_map->buffer )
2717 return FT_THROW( Invalid_Argument );
2718
2719 ras.outline = *outline;
2720
2721 ras.bTop = (Int)target_map->rows - 1;
2722 ras.bRight = (Int)target_map->width - 1;
2723 ras.bPitch = (Int)target_map->pitch;
2724 ras.bOrigin = (PByte)target_map->buffer;
2725
2726 if ( ras.bPitch > 0 )
2727 ras.bOrigin += ras.bTop * ras.bPitch;
2728
2729 /* allocate memory based on empirical estimate from CJK fonts */
2730 estimate = ( ras.bTop + ras.bRight ) * 8UL +
2731 80UL * sizeof ( TProfile ) / sizeof ( Long );
2732 if ( estimate > FT_MAX_BLACK_POOL )
2733 {
2734 FT_Error error;
2735 FT_Memory memory = (FT_Memory)((black_PRaster)raster)->memory;
2736
2737
2738 if ( FT_QNEW_ARRAY( ras.buff, estimate ) )
2739 ret = error;
2740 else
2741 {
2742 ras.sizeBuff = ras.buff + estimate;
2743 ret = Render_Glyph( RAS_VAR );
2744 FT_FREE( ras.buff );
2745 }
2746 }
2747 else
2748 {
2749 Long buffer[FT_MAX_BLACK_POOL]; /* stack allocation */
2750
2751
2752 ras.buff = buffer;
2753 ras.sizeBuff = (&buffer)[1]; /* Points to right after buffer. */
2754
2755 ret = Render_Glyph( RAS_VAR );
2756 }
2757
2758 return ret;
2759 }
2760
2761
2762 FT_DEFINE_RASTER_FUNCS(
2763 ft_standard_raster,
2764
2765 FT_GLYPH_FORMAT_OUTLINE,
2766
2767 ft_black_new, /* FT_Raster_New_Func raster_new */
2768 ft_black_reset, /* FT_Raster_Reset_Func raster_reset */
2769 ft_black_set_mode, /* FT_Raster_Set_Mode_Func raster_set_mode */
2770 ft_black_render, /* FT_Raster_Render_Func raster_render */
2771 ft_black_done /* FT_Raster_Done_Func raster_done */
2772 )
2773
2774
2775/* END */