~oif-team/ubuntu/natty/qt4-x11/xi2.1

« back to all changes in this revision

Viewing changes to src/3rdparty/freetype/src/raster/ftraster.c

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-08-24 04:09:09 UTC
  • Revision ID: james.westby@ubuntu.com-20050824040909-xmxe9jfr4a0w5671
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************/
 
2
/*                                                                         */
 
3
/*  ftraster.c                                                             */
 
4
/*                                                                         */
 
5
/*    The FreeType glyph rasterizer (body).                                */
 
6
/*                                                                         */
 
7
/*  Copyright 1996-2001, 2002, 2003 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 is a rewrite of the FreeType 1.x scan-line converter             */
 
21
  /*                                                                       */
 
22
  /*************************************************************************/
 
23
 
 
24
 
 
25
#include <ft2build.h>
 
26
#include "ftraster.h"
 
27
#include FT_INTERNAL_CALC_H   /* for FT_MulDiv only */
 
28
 
 
29
 
 
30
  /*************************************************************************/
 
31
  /*                                                                       */
 
32
  /* A simple technical note on how the raster works                       */
 
33
  /* -----------------------------------------------                       */
 
34
  /*                                                                       */
 
35
  /*   Converting an outline into a bitmap is achieved in several steps:   */
 
36
  /*                                                                       */
 
37
  /*   1 - Decomposing the outline into successive `profiles'.  Each       */
 
38
  /*       profile is simply an array of scanline intersections on a given */
 
39
  /*       dimension.  A profile's main attributes are                     */
 
40
  /*                                                                       */
 
41
  /*       o its scanline position boundaries, i.e. `Ymin' and `Ymax'.     */
 
42
  /*                                                                       */
 
43
  /*       o an array of intersection coordinates for each scanline        */
 
44
  /*         between `Ymin' and `Ymax'.                                    */
 
45
  /*                                                                       */
 
46
  /*       o a direction, indicating whether it was built going `up' or    */
 
47
  /*         `down', as this is very important for filling rules.          */
 
48
  /*                                                                       */
 
49
  /*   2 - Sweeping the target map's scanlines in order to compute segment */
 
50
  /*       `spans' which are then filled.  Additionally, this pass         */
 
51
  /*       performs drop-out control.                                      */
 
52
  /*                                                                       */
 
53
  /*   The outline data is parsed during step 1 only.  The profiles are    */
 
54
  /*   built from the bottom of the render pool, used as a stack.  The     */
 
55
  /*   following graphics shows the profile list under construction:       */
 
56
  /*                                                                       */
 
57
  /*     ____________________________________________________________ _ _  */
 
58
  /*    |         |                   |         |                 |        */
 
59
  /*    | profile | coordinates for   | profile | coordinates for |-->     */
 
60
  /*    |    1    |  profile 1        |    2    |  profile 2      |-->     */
 
61
  /*    |_________|___________________|_________|_________________|__ _ _  */
 
62
  /*                                                                       */
 
63
  /*    ^                                                         ^        */
 
64
  /*    |                                                         |        */
 
65
  /*  start of render pool                                       top       */
 
66
  /*                                                                       */
 
67
  /*   The top of the profile stack is kept in the `top' variable.         */
 
68
  /*                                                                       */
 
69
  /*   As you can see, a profile record is pushed on top of the render     */
 
70
  /*   pool, which is then followed by its coordinates/intersections.  If  */
 
71
  /*   a change of direction is detected in the outline, a new profile is  */
 
72
  /*   generated until the end of the outline.                             */
 
73
  /*                                                                       */
 
74
  /*   Note that when all profiles have been generated, the function       */
 
75
  /*   Finalize_Profile_Table() is used to record, for each profile, its   */
 
76
  /*   bottom-most scanline as well as the scanline above its upmost       */
 
77
  /*   boundary.  These positions are called `y-turns' because they (sort  */
 
78
  /*   of) correspond to local extrema.  They are stored in a sorted list  */
 
79
  /*   built from the top of the render pool as a downwards stack:         */
 
80
  /*                                                                       */
 
81
  /*      _ _ _______________________________________                      */
 
82
  /*                            |                    |                     */
 
83
  /*                         <--| sorted list of     |                     */
 
84
  /*                         <--|  extrema scanlines |                     */
 
85
  /*      _ _ __________________|____________________|                     */
 
86
  /*                                                                       */
 
87
  /*                            ^                    ^                     */
 
88
  /*                            |                    |                     */
 
89
  /*                         maxBuff           sizeBuff = end of pool      */
 
90
  /*                                                                       */
 
91
  /*   This list is later used during the sweep phase in order to          */
 
92
  /*   optimize performance (see technical note on the sweep below).       */
 
93
  /*                                                                       */
 
94
  /*   Of course, the raster detects whether the two stacks collide and    */
 
95
  /*   handles the situation propertly.                                    */
 
96
  /*                                                                       */
 
97
  /*************************************************************************/
 
98
 
 
99
 
 
100
  /*************************************************************************/
 
101
  /*************************************************************************/
 
102
  /**                                                                     **/
 
103
  /**  CONFIGURATION MACROS                                               **/
 
104
  /**                                                                     **/
 
105
  /*************************************************************************/
 
106
  /*************************************************************************/
 
107
 
 
108
  /* define DEBUG_RASTER if you want to compile a debugging version */
 
109
#define xxxDEBUG_RASTER
 
110
 
 
111
  /* The default render pool size in bytes */
 
112
#define RASTER_RENDER_POOL  8192
 
113
 
 
114
  /* undefine FT_RASTER_OPTION_ANTI_ALIASING if you do not want to support */
 
115
  /* 5-levels anti-aliasing                                                */
 
116
#ifdef FT_CONFIG_OPTION_5_GRAY_LEVELS
 
117
#define FT_RASTER_OPTION_ANTI_ALIASING
 
118
#endif
 
119
 
 
120
  /* The size of the two-lines intermediate bitmap used */
 
121
  /* for anti-aliasing, in bytes.                       */
 
122
#define RASTER_GRAY_LINES  2048
 
123
 
 
124
 
 
125
  /*************************************************************************/
 
126
  /*************************************************************************/
 
127
  /**                                                                     **/
 
128
  /**  OTHER MACROS (do not change)                                       **/
 
129
  /**                                                                     **/
 
130
  /*************************************************************************/
 
131
  /*************************************************************************/
 
132
 
 
133
  /*************************************************************************/
 
134
  /*                                                                       */
 
135
  /* The macro FT_COMPONENT is used in trace mode.  It is an implicit      */
 
136
  /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log  */
 
137
  /* messages during execution.                                            */
 
138
  /*                                                                       */
 
139
#undef  FT_COMPONENT
 
140
#define FT_COMPONENT  trace_raster
 
141
 
 
142
 
 
143
#ifdef _STANDALONE_
 
144
 
 
145
 
 
146
  /* This macro is used to indicate that a function parameter is unused. */
 
147
  /* Its purpose is simply to reduce compiler warnings.  Note also that  */
 
148
  /* simply defining it as `(void)x' doesn't avoid warnings with certain */
 
149
  /* ANSI compilers (e.g. LCC).                                          */
 
150
#define FT_UNUSED( x )  (x) = (x)
 
151
 
 
152
  /* Disable the tracing mechanism for simplicity -- developers can      */
 
153
  /* activate it easily by redefining these two macros.                  */
 
154
#ifndef FT_ERROR
 
155
#define FT_ERROR( x )  do ; while ( 0 )     /* nothing */
 
156
#endif
 
157
 
 
158
#ifndef FT_TRACE
 
159
#define FT_TRACE( x )  do ; while ( 0 )     /* nothing */
 
160
#endif
 
161
 
 
162
#define Raster_Err_None          0
 
163
#define Raster_Err_Not_Ini      -1
 
164
#define Raster_Err_Overflow     -2
 
165
#define Raster_Err_Neg_Height   -3
 
166
#define Raster_Err_Invalid      -4
 
167
#define Raster_Err_Unsupported  -5
 
168
 
 
169
 
 
170
#else /* _STANDALONE_ */
 
171
 
 
172
 
 
173
#include FT_INTERNAL_OBJECTS_H
 
174
#include FT_INTERNAL_DEBUG_H        /* for FT_TRACE() and FT_ERROR() */
 
175
 
 
176
#include "rasterrs.h"
 
177
 
 
178
#define Raster_Err_None         Raster_Err_Ok
 
179
#define Raster_Err_Not_Ini      Raster_Err_Raster_Uninitialized
 
180
#define Raster_Err_Overflow     Raster_Err_Raster_Overflow
 
181
#define Raster_Err_Neg_Height   Raster_Err_Raster_Negative_Height
 
182
#define Raster_Err_Invalid      Raster_Err_Invalid_Outline
 
183
#define Raster_Err_Unsupported  Raster_Err_Cannot_Render_Glyph
 
184
 
 
185
 
 
186
#endif /* _STANDALONE_ */
 
187
 
 
188
 
 
189
#ifndef FT_MEM_SET
 
190
#define FT_MEM_SET( d, s, c )  ft_memset( d, s, c )
 
191
#endif
 
192
 
 
193
 
 
194
  /* FMulDiv means `Fast MulDiv'; it is used in case where `b' is       */
 
195
  /* typically a small value and the result of a*b is known to fit into */
 
196
  /* 32 bits.                                                           */
 
197
#define FMulDiv( a, b, c )  ( (a) * (b) / (c) )
 
198
 
 
199
  /* On the other hand, SMulDiv means `Slow MulDiv', and is used typically */
 
200
  /* for clipping computations.  It simply uses the FT_MulDiv() function   */
 
201
  /* defined in `ftcalc.h'.                                                */
 
202
#define SMulDiv  FT_MulDiv
 
203
 
 
204
  /* The rasterizer is a very general purpose component; please leave */
 
205
  /* the following redefinitions there (you never know your target    */
 
206
  /* environment).                                                    */
 
207
 
 
208
#ifndef TRUE
 
209
#define TRUE   1
 
210
#endif
 
211
 
 
212
#ifndef FALSE
 
213
#define FALSE  0
 
214
#endif
 
215
 
 
216
#ifndef NULL
 
217
#define NULL  (void*)0
 
218
#endif
 
219
 
 
220
#ifndef SUCCESS
 
221
#define SUCCESS  0
 
222
#endif
 
223
 
 
224
#ifndef FAILURE
 
225
#define FAILURE  1
 
226
#endif
 
227
 
 
228
 
 
229
#define MaxBezier  32   /* The maximum number of stacked Bezier curves. */
 
230
                        /* Setting this constant to more than 32 is a   */
 
231
                        /* pure waste of space.                         */
 
232
 
 
233
#define Pixel_Bits  6   /* fractional bits of *input* coordinates */
 
234
 
 
235
 
 
236
  /*************************************************************************/
 
237
  /*************************************************************************/
 
238
  /**                                                                     **/
 
239
  /**  SIMPLE TYPE DECLARATIONS                                           **/
 
240
  /**                                                                     **/
 
241
  /*************************************************************************/
 
242
  /*************************************************************************/
 
243
 
 
244
  typedef int             Int;
 
245
  typedef unsigned int    UInt;
 
246
  typedef short           Short;
 
247
  typedef unsigned short  UShort, *PUShort;
 
248
  typedef long            Long, *PLong;
 
249
  typedef unsigned long   ULong;
 
250
 
 
251
  typedef unsigned char   Byte, *PByte;
 
252
  typedef char            Bool;
 
253
 
 
254
 
 
255
  typedef union  Alignment_
 
256
  {
 
257
    long    l;
 
258
    void*   p;
 
259
    void  (*f)(void);
 
260
 
 
261
  } Alignment, *PAlignment;
 
262
 
 
263
 
 
264
  typedef struct  TPoint_
 
265
  {
 
266
    Long  x;
 
267
    Long  y;
 
268
 
 
269
  } TPoint;
 
270
 
 
271
 
 
272
  typedef enum  TFlow_
 
273
  {
 
274
    Flow_None = 0,
 
275
    Flow_Up   = 1,
 
276
    Flow_Down = -1
 
277
 
 
278
  } TFlow;
 
279
 
 
280
 
 
281
  /* States of each line, arc, and profile */
 
282
  typedef enum  TStates_
 
283
  {
 
284
    Unknown_State,
 
285
    Ascending_State,
 
286
    Descending_State,
 
287
    Flat_State
 
288
 
 
289
  } TStates;
 
290
 
 
291
 
 
292
  typedef struct TProfile_  TProfile;
 
293
  typedef TProfile*         PProfile;
 
294
 
 
295
  struct  TProfile_
 
296
  {
 
297
    FT_F26Dot6  X;           /* current coordinate during sweep        */
 
298
    PProfile    link;        /* link to next profile - various purpose */
 
299
    PLong       offset;      /* start of profile's data in render pool */
 
300
    int         flow;        /* Profile orientation: Asc/Descending    */
 
301
    long        height;      /* profile's height in scanlines          */
 
302
    long        start;       /* profile's starting scanline            */
 
303
 
 
304
    unsigned    countL;      /* number of lines to step before this    */
 
305
                             /* profile becomes drawable               */
 
306
 
 
307
    PProfile    next;        /* next profile in same contour, used     */
 
308
                             /* during drop-out control                */
 
309
  };
 
310
 
 
311
  typedef PProfile   TProfileList;
 
312
  typedef PProfile*  PProfileList;
 
313
 
 
314
 
 
315
  /* Simple record used to implement a stack of bands, required */
 
316
  /* by the sub-banding mechanism                               */
 
317
  typedef struct  TBand_
 
318
  {
 
319
    Short  y_min;   /* band's minimum */
 
320
    Short  y_max;   /* band's maximum */
 
321
 
 
322
  } TBand;
 
323
 
 
324
 
 
325
#define AlignProfileSize \
 
326
  ( ( sizeof ( TProfile ) + sizeof ( Alignment ) - 1 ) / sizeof ( long ) )
 
327
 
 
328
 
 
329
#ifdef TT_STATIC_RASTER
 
330
 
 
331
 
 
332
#define RAS_ARGS       /* void */
 
333
#define RAS_ARG        /* void */
 
334
 
 
335
#define RAS_VARS       /* void */
 
336
#define RAS_VAR        /* void */
 
337
 
 
338
#define FT_UNUSED_RASTER  do ; while ( 0 )
 
339
 
 
340
 
 
341
#else /* TT_STATIC_RASTER */
 
342
 
 
343
 
 
344
#define RAS_ARGS       TRaster_Instance*  raster,
 
345
#define RAS_ARG        TRaster_Instance*  raster
 
346
 
 
347
#define RAS_VARS       raster,
 
348
#define RAS_VAR        raster
 
349
 
 
350
#define FT_UNUSED_RASTER  FT_UNUSED( raster )
 
351
 
 
352
 
 
353
#endif /* TT_STATIC_RASTER */
 
354
 
 
355
 
 
356
  typedef struct TRaster_Instance_  TRaster_Instance;
 
357
 
 
358
 
 
359
  /* prototypes used for sweep function dispatch */
 
360
  typedef void
 
361
  Function_Sweep_Init( RAS_ARGS Short*  min,
 
362
                                Short*  max );
 
363
 
 
364
  typedef void
 
365
  Function_Sweep_Span( RAS_ARGS Short       y,
 
366
                                FT_F26Dot6  x1,
 
367
                                FT_F26Dot6  x2,
 
368
                                PProfile    left,
 
369
                                PProfile    right );
 
370
 
 
371
  typedef void
 
372
  Function_Sweep_Step( RAS_ARG );
 
373
 
 
374
 
 
375
  /* NOTE: These operations are only valid on 2's complement processors */
 
376
 
 
377
#define FLOOR( x )    ( (x) & -ras.precision )
 
378
#define CEILING( x )  ( ( (x) + ras.precision - 1 ) & -ras.precision )
 
379
#define TRUNC( x )    ( (signed long)(x) >> ras.precision_bits )
 
380
#define FRAC( x )     ( (x) & ( ras.precision - 1 ) )
 
381
#define SCALED( x )   ( ( (x) << ras.scale_shift ) - ras.precision_half )
 
382
 
 
383
  /* Note that I have moved the location of some fields in the */
 
384
  /* structure to ensure that the most used variables are used */
 
385
  /* at the top.  Thus, their offset can be coded with less    */
 
386
  /* opcodes, and it results in a smaller executable.          */
 
387
 
 
388
  struct  TRaster_Instance_
 
389
  {
 
390
    Int       precision_bits;       /* precision related variables         */
 
391
    Int       precision;
 
392
    Int       precision_half;
 
393
    Long      precision_mask;
 
394
    Int       precision_shift;
 
395
    Int       precision_step;
 
396
    Int       precision_jitter;
 
397
 
 
398
    Int       scale_shift;          /* == precision_shift   for bitmaps    */
 
399
                                    /* == precision_shift+1 for pixmaps    */
 
400
 
 
401
    PLong     buff;                 /* The profiles buffer                 */
 
402
    PLong     sizeBuff;             /* Render pool size                    */
 
403
    PLong     maxBuff;              /* Profiles buffer size                */
 
404
    PLong     top;                  /* Current cursor in buffer            */
 
405
 
 
406
    FT_Error  error;
 
407
 
 
408
    Int       numTurns;             /* number of Y-turns in outline        */
 
409
 
 
410
    TPoint*   arc;                  /* current Bezier arc pointer          */
 
411
 
 
412
    UShort    bWidth;               /* target bitmap width                 */
 
413
    PByte     bTarget;              /* target bitmap buffer                */
 
414
    PByte     gTarget;              /* target pixmap buffer                */
 
415
 
 
416
    Long      lastX, lastY, minY, maxY;
 
417
 
 
418
    UShort    num_Profs;            /* current number of profiles          */
 
419
 
 
420
    Bool      fresh;                /* signals a fresh new profile which   */
 
421
                                    /* 'start' field must be completed     */
 
422
    Bool      joint;                /* signals that the last arc ended     */
 
423
                                    /* exactly on a scanline.  Allows      */
 
424
                                    /* removal of doublets                 */
 
425
    PProfile  cProfile;             /* current profile                     */
 
426
    PProfile  fProfile;             /* head of linked list of profiles     */
 
427
    PProfile  gProfile;             /* contour's first profile in case     */
 
428
                                    /* of impact                           */
 
429
 
 
430
    TStates   state;                /* rendering state                     */
 
431
 
 
432
    FT_Bitmap   target;             /* description of target bit/pixmap    */
 
433
    FT_Outline  outline;
 
434
 
 
435
    Long      traceOfs;             /* current offset in target bitmap     */
 
436
    Long      traceG;               /* current offset in target pixmap     */
 
437
 
 
438
    Short     traceIncr;            /* sweep's increment in target bitmap  */
 
439
 
 
440
    Short     gray_min_x;           /* current min x during gray rendering */
 
441
    Short     gray_max_x;           /* current max x during gray rendering */
 
442
 
 
443
    /* dispatch variables */
 
444
 
 
445
    Function_Sweep_Init*  Proc_Sweep_Init;
 
446
    Function_Sweep_Span*  Proc_Sweep_Span;
 
447
    Function_Sweep_Span*  Proc_Sweep_Drop;
 
448
    Function_Sweep_Step*  Proc_Sweep_Step;
 
449
 
 
450
    Byte      dropOutControl;       /* current drop_out control method     */
 
451
 
 
452
    Bool      second_pass;          /* indicates wether a horizontal pass  */
 
453
                                    /* should be performed to control      */
 
454
                                    /* drop-out accurately when calling    */
 
455
                                    /* Render_Glyph.  Note that there is   */
 
456
                                    /* no horizontal pass during gray      */
 
457
                                    /* rendering.                          */
 
458
 
 
459
    TPoint    arcs[3 * MaxBezier + 1]; /* The Bezier stack                 */
 
460
 
 
461
    TBand     band_stack[16];       /* band stack used for sub-banding     */
 
462
    Int       band_top;             /* band stack top                      */
 
463
 
 
464
    Int       count_table[256];     /* Look-up table used to quickly count */
 
465
                                    /* set bits in a gray 2x2 cell         */
 
466
 
 
467
    void*     memory;
 
468
 
 
469
#ifdef FT_RASTER_OPTION_ANTI_ALIASING
 
470
 
 
471
    Byte      grays[5];             /* Palette of gray levels used for     */
 
472
                                    /* render.                             */
 
473
 
 
474
    Byte      gray_lines[RASTER_GRAY_LINES];
 
475
                                /* Intermediate table used to render the   */
 
476
                                /* graylevels pixmaps.                     */
 
477
                                /* gray_lines is a buffer holding two      */
 
478
                                /* monochrome scanlines                    */
 
479
 
 
480
    Short     gray_width;       /* width in bytes of one monochrome        */
 
481
                                /* intermediate scanline of gray_lines.    */
 
482
                                /* Each gray pixel takes 2 bits long there */
 
483
 
 
484
                       /* The gray_lines must hold 2 lines, thus with size */
 
485
                       /* in bytes of at least `gray_width*2'.             */
 
486
 
 
487
#endif /* FT_RASTER_ANTI_ALIASING */
 
488
 
 
489
#if 0
 
490
    PByte       flags;              /* current flags table                 */
 
491
    PUShort     outs;               /* current outlines table              */
 
492
    FT_Vector*  coords;
 
493
 
 
494
    UShort      nPoints;            /* number of points in current glyph   */
 
495
    Short       nContours;          /* number of contours in current glyph */
 
496
#endif
 
497
 
 
498
  };
 
499
 
 
500
 
 
501
#ifdef FT_CONFIG_OPTION_STATIC_RASTER
 
502
 
 
503
  static TRaster_Instance  cur_ras;
 
504
#define ras  cur_ras
 
505
 
 
506
#else
 
507
 
 
508
#define ras  (*raster)
 
509
 
 
510
#endif /* FT_CONFIG_OPTION_STATIC_RASTER */
 
511
 
 
512
 
 
513
  /*************************************************************************/
 
514
  /*************************************************************************/
 
515
  /**                                                                     **/
 
516
  /**  PROFILES COMPUTATION                                               **/
 
517
  /**                                                                     **/
 
518
  /*************************************************************************/
 
519
  /*************************************************************************/
 
520
 
 
521
 
 
522
  /*************************************************************************/
 
523
  /*                                                                       */
 
524
  /* <Function>                                                            */
 
525
  /*    Set_High_Precision                                                 */
 
526
  /*                                                                       */
 
527
  /* <Description>                                                         */
 
528
  /*    Sets precision variables according to param flag.                  */
 
529
  /*                                                                       */
 
530
  /* <Input>                                                               */
 
531
  /*    High :: Set to True for high precision (typically for ppem < 18),  */
 
532
  /*            false otherwise.                                           */
 
533
  /*                                                                       */
 
534
  static void
 
535
  Set_High_Precision( RAS_ARGS Int  High )
 
536
  {
 
537
    if ( High )
 
538
    {
 
539
      ras.precision_bits   = 10;
 
540
      ras.precision_step   = 128;
 
541
      ras.precision_jitter = 24;
 
542
    }
 
543
    else
 
544
    {
 
545
      ras.precision_bits   = 6;
 
546
      ras.precision_step   = 32;
 
547
      ras.precision_jitter = 2;
 
548
    }
 
549
 
 
550
    FT_TRACE6(( "Set_High_Precision(%s)\n", High ? "true" : "false" ));
 
551
 
 
552
    ras.precision       = 1 << ras.precision_bits;
 
553
    ras.precision_half  = ras.precision / 2;
 
554
    ras.precision_shift = ras.precision_bits - Pixel_Bits;
 
555
    ras.precision_mask  = -ras.precision;
 
556
  }
 
557
 
 
558
 
 
559
  /*************************************************************************/
 
560
  /*                                                                       */
 
561
  /* <Function>                                                            */
 
562
  /*    New_Profile                                                        */
 
563
  /*                                                                       */
 
564
  /* <Description>                                                         */
 
565
  /*    Creates a new profile in the render pool.                          */
 
566
  /*                                                                       */
 
567
  /* <Input>                                                               */
 
568
  /*    aState :: The state/orientation of the new profile.                */
 
569
  /*                                                                       */
 
570
  /* <Return>                                                              */
 
571
  /*   SUCCESS on success.  FAILURE in case of overflow or of incoherent   */
 
572
  /*   profile.                                                            */
 
573
  /*                                                                       */
 
574
  static Bool
 
575
  New_Profile( RAS_ARGS TStates  aState )
 
576
  {
 
577
    if ( !ras.fProfile )
 
578
    {
 
579
      ras.cProfile  = (PProfile)ras.top;
 
580
      ras.fProfile  = ras.cProfile;
 
581
      ras.top      += AlignProfileSize;
 
582
    }
 
583
 
 
584
    if ( ras.top >= ras.maxBuff )
 
585
    {
 
586
      ras.error = Raster_Err_Overflow;
 
587
      return FAILURE;
 
588
    }
 
589
 
 
590
    switch ( aState )
 
591
    {
 
592
    case Ascending_State:
 
593
      ras.cProfile->flow = Flow_Up;
 
594
      FT_TRACE6(( "New ascending profile = %lx\n", (long)ras.cProfile ));
 
595
      break;
 
596
 
 
597
    case Descending_State:
 
598
      ras.cProfile->flow = Flow_Down;
 
599
      FT_TRACE6(( "New descending profile = %lx\n", (long)ras.cProfile ));
 
600
      break;
 
601
 
 
602
    default:
 
603
      FT_ERROR(( "New_Profile: invalid profile direction!\n" ));
 
604
      ras.error = Raster_Err_Invalid;
 
605
      return FAILURE;
 
606
    }
 
607
 
 
608
    ras.cProfile->start  = 0;
 
609
    ras.cProfile->height = 0;
 
610
    ras.cProfile->offset = ras.top;
 
611
    ras.cProfile->link   = (PProfile)0;
 
612
    ras.cProfile->next   = (PProfile)0;
 
613
 
 
614
    if ( !ras.gProfile )
 
615
      ras.gProfile = ras.cProfile;
 
616
 
 
617
    ras.state = aState;
 
618
    ras.fresh = TRUE;
 
619
    ras.joint = FALSE;
 
620
 
 
621
    return SUCCESS;
 
622
  }
 
623
 
 
624
 
 
625
  /*************************************************************************/
 
626
  /*                                                                       */
 
627
  /* <Function>                                                            */
 
628
  /*    End_Profile                                                        */
 
629
  /*                                                                       */
 
630
  /* <Description>                                                         */
 
631
  /*    Finalizes the current profile.                                     */
 
632
  /*                                                                       */
 
633
  /* <Return>                                                              */
 
634
  /*    SUCCESS on success.  FAILURE in case of overflow or incoherency.   */
 
635
  /*                                                                       */
 
636
  static Bool
 
637
  End_Profile( RAS_ARG )
 
638
  {
 
639
    Long      h;
 
640
    PProfile  oldProfile;
 
641
 
 
642
 
 
643
    h = (Long)( ras.top - ras.cProfile->offset );
 
644
 
 
645
    if ( h < 0 )
 
646
    {
 
647
      FT_ERROR(( "End_Profile: negative height encountered!\n" ));
 
648
      ras.error = Raster_Err_Neg_Height;
 
649
      return FAILURE;
 
650
    }
 
651
 
 
652
    if ( h > 0 )
 
653
    {
 
654
      FT_TRACE6(( "Ending profile %lx, start = %ld, height = %ld\n",
 
655
                  (long)ras.cProfile, ras.cProfile->start, h ));
 
656
 
 
657
      oldProfile           = ras.cProfile;
 
658
      ras.cProfile->height = h;
 
659
      ras.cProfile         = (PProfile)ras.top;
 
660
 
 
661
      ras.top             += AlignProfileSize;
 
662
 
 
663
      ras.cProfile->height = 0;
 
664
      ras.cProfile->offset = ras.top;
 
665
      oldProfile->next     = ras.cProfile;
 
666
      ras.num_Profs++;
 
667
    }
 
668
 
 
669
    if ( ras.top >= ras.maxBuff )
 
670
    {
 
671
      FT_TRACE1(( "overflow in End_Profile\n" ));
 
672
      ras.error = Raster_Err_Overflow;
 
673
      return FAILURE;
 
674
    }
 
675
 
 
676
    ras.joint = FALSE;
 
677
 
 
678
    return SUCCESS;
 
679
  }
 
680
 
 
681
 
 
682
  /*************************************************************************/
 
683
  /*                                                                       */
 
684
  /* <Function>                                                            */
 
685
  /*    Insert_Y_Turn                                                      */
 
686
  /*                                                                       */
 
687
  /* <Description>                                                         */
 
688
  /*    Inserts a salient into the sorted list placed on top of the render */
 
689
  /*    pool.                                                              */
 
690
  /*                                                                       */
 
691
  /* <Input>                                                               */
 
692
  /*    New y scanline position.                                           */
 
693
  /*                                                                       */
 
694
  /* <Return>                                                              */
 
695
  /*    SUCCESS on success.  FAILURE in case of overflow.                  */
 
696
  /*                                                                       */
 
697
  static Bool
 
698
  Insert_Y_Turn( RAS_ARGS Int  y )
 
699
  {
 
700
    PLong  y_turns;
 
701
    Int    y2, n;
 
702
 
 
703
 
 
704
    n       = ras.numTurns - 1;
 
705
    y_turns = ras.sizeBuff - ras.numTurns;
 
706
 
 
707
    /* look for first y value that is <= */
 
708
    while ( n >= 0 && y < y_turns[n] )
 
709
      n--;
 
710
 
 
711
    /* if it is <, simply insert it, ignore if == */
 
712
    if ( n >= 0 && y > y_turns[n] )
 
713
      while ( n >= 0 )
 
714
      {
 
715
        y2 = (Int)y_turns[n];
 
716
        y_turns[n] = y;
 
717
        y = y2;
 
718
        n--;
 
719
      }
 
720
 
 
721
    if ( n < 0 )
 
722
    {
 
723
      ras.maxBuff--;
 
724
      if ( ras.maxBuff <= ras.top )
 
725
      {
 
726
        ras.error = Raster_Err_Overflow;
 
727
        return FAILURE;
 
728
      }
 
729
      ras.numTurns++;
 
730
      ras.sizeBuff[-ras.numTurns] = y;
 
731
    }
 
732
 
 
733
    return SUCCESS;
 
734
  }
 
735
 
 
736
 
 
737
  /*************************************************************************/
 
738
  /*                                                                       */
 
739
  /* <Function>                                                            */
 
740
  /*    Finalize_Profile_Table                                             */
 
741
  /*                                                                       */
 
742
  /* <Description>                                                         */
 
743
  /*    Adjusts all links in the profiles list.                            */
 
744
  /*                                                                       */
 
745
  /* <Return>                                                              */
 
746
  /*    SUCCESS on success.  FAILURE in case of overflow.                  */
 
747
  /*                                                                       */
 
748
  static Bool
 
749
  Finalize_Profile_Table( RAS_ARG )
 
750
  {
 
751
    Int       bottom, top;
 
752
    UShort    n;
 
753
    PProfile  p;
 
754
 
 
755
 
 
756
    n = ras.num_Profs;
 
757
 
 
758
    if ( n > 1 )
 
759
    {
 
760
      p = ras.fProfile;
 
761
      while ( n > 0 )
 
762
      {
 
763
        if ( n > 1 )
 
764
          p->link = (PProfile)( p->offset + p->height );
 
765
        else
 
766
          p->link = NULL;
 
767
 
 
768
        switch ( p->flow )
 
769
        {
 
770
        case Flow_Down:
 
771
          bottom     = (Int)( p->start - p->height + 1 );
 
772
          top        = (Int)p->start;
 
773
          p->start   = bottom;
 
774
          p->offset += p->height - 1;
 
775
          break;
 
776
 
 
777
        case Flow_Up:
 
778
        default:
 
779
          bottom = (Int)p->start;
 
780
          top    = (Int)( p->start + p->height - 1 );
 
781
        }
 
782
 
 
783
        if ( Insert_Y_Turn( RAS_VARS bottom )   ||
 
784
             Insert_Y_Turn( RAS_VARS top + 1 )  )
 
785
          return FAILURE;
 
786
 
 
787
        p = p->link;
 
788
        n--;
 
789
      }
 
790
    }
 
791
    else
 
792
      ras.fProfile = NULL;
 
793
 
 
794
    return SUCCESS;
 
795
  }
 
796
 
 
797
 
 
798
  /*************************************************************************/
 
799
  /*                                                                       */
 
800
  /* <Function>                                                            */
 
801
  /*    Split_Conic                                                        */
 
802
  /*                                                                       */
 
803
  /* <Description>                                                         */
 
804
  /*    Subdivides one conic Bezier into two joint sub-arcs in the Bezier  */
 
805
  /*    stack.                                                             */
 
806
  /*                                                                       */
 
807
  /* <Input>                                                               */
 
808
  /*    None (subdivided Bezier is taken from the top of the stack).       */
 
809
  /*                                                                       */
 
810
  /* <Note>                                                                */
 
811
  /*    This routine is the `beef' of this component.  It is  _the_ inner  */
 
812
  /*    loop that should be optimized to hell to get the best performance. */
 
813
  /*                                                                       */
 
814
  static void
 
815
  Split_Conic( TPoint*  base )
 
816
  {
 
817
    Long  a, b;
 
818
 
 
819
 
 
820
    base[4].x = base[2].x;
 
821
    b = base[1].x;
 
822
    a = base[3].x = ( base[2].x + b ) / 2;
 
823
    b = base[1].x = ( base[0].x + b ) / 2;
 
824
    base[2].x = ( a + b ) / 2;
 
825
 
 
826
    base[4].y = base[2].y;
 
827
    b = base[1].y;
 
828
    a = base[3].y = ( base[2].y + b ) / 2;
 
829
    b = base[1].y = ( base[0].y + b ) / 2;
 
830
    base[2].y = ( a + b ) / 2;
 
831
 
 
832
    /* hand optimized.  gcc doesn't seem to be too good at common      */
 
833
    /* expression substitution and instruction scheduling ;-)          */
 
834
  }
 
835
 
 
836
 
 
837
  /*************************************************************************/
 
838
  /*                                                                       */
 
839
  /* <Function>                                                            */
 
840
  /*    Split_Cubic                                                        */
 
841
  /*                                                                       */
 
842
  /* <Description>                                                         */
 
843
  /*    Subdivides a third-order Bezier arc into two joint sub-arcs in the */
 
844
  /*    Bezier stack.                                                      */
 
845
  /*                                                                       */
 
846
  /* <Note>                                                                */
 
847
  /*    This routine is the `beef' of the component.  It is one of _the_   */
 
848
  /*    inner loops that should be optimized like hell to get the best     */
 
849
  /*    performance.                                                       */
 
850
  /*                                                                       */
 
851
  static void
 
852
  Split_Cubic( TPoint*  base )
 
853
  {
 
854
    Long  a, b, c, d;
 
855
 
 
856
 
 
857
    base[6].x = base[3].x;
 
858
    c = base[1].x;
 
859
    d = base[2].x;
 
860
    base[1].x = a = ( base[0].x + c + 1 ) >> 1;
 
861
    base[5].x = b = ( base[3].x + d + 1 ) >> 1;
 
862
    c = ( c + d + 1 ) >> 1;
 
863
    base[2].x = a = ( a + c + 1 ) >> 1;
 
864
    base[4].x = b = ( b + c + 1 ) >> 1;
 
865
    base[3].x = ( a + b + 1 ) >> 1;
 
866
 
 
867
    base[6].y = base[3].y;
 
868
    c = base[1].y;
 
869
    d = base[2].y;
 
870
    base[1].y = a = ( base[0].y + c + 1 ) >> 1;
 
871
    base[5].y = b = ( base[3].y + d + 1 ) >> 1;
 
872
    c = ( c + d + 1 ) >> 1;
 
873
    base[2].y = a = ( a + c + 1 ) >> 1;
 
874
    base[4].y = b = ( b + c + 1 ) >> 1;
 
875
    base[3].y = ( a + b + 1 ) >> 1;
 
876
  }
 
877
 
 
878
 
 
879
  /*************************************************************************/
 
880
  /*                                                                       */
 
881
  /* <Function>                                                            */
 
882
  /*    Line_Up                                                            */
 
883
  /*                                                                       */
 
884
  /* <Description>                                                         */
 
885
  /*    Computes the x-coordinates of an ascending line segment and stores */
 
886
  /*    them in the render pool.                                           */
 
887
  /*                                                                       */
 
888
  /* <Input>                                                               */
 
889
  /*    x1   :: The x-coordinate of the segment's start point.             */
 
890
  /*                                                                       */
 
891
  /*    y1   :: The y-coordinate of the segment's start point.             */
 
892
  /*                                                                       */
 
893
  /*    x2   :: The x-coordinate of the segment's end point.               */
 
894
  /*                                                                       */
 
895
  /*    y2   :: The y-coordinate of the segment's end point.               */
 
896
  /*                                                                       */
 
897
  /*    miny :: A lower vertical clipping bound value.                     */
 
898
  /*                                                                       */
 
899
  /*    maxy :: An upper vertical clipping bound value.                    */
 
900
  /*                                                                       */
 
901
  /* <Return>                                                              */
 
902
  /*    SUCCESS on success, FAILURE on render pool overflow.               */
 
903
  /*                                                                       */
 
904
  static Bool
 
905
  Line_Up( RAS_ARGS Long  x1,
 
906
                    Long  y1,
 
907
                    Long  x2,
 
908
                    Long  y2,
 
909
                    Long  miny,
 
910
                    Long  maxy )
 
911
  {
 
912
    Long   Dx, Dy;
 
913
    Int    e1, e2, f1, f2, size;     /* XXX: is `Short' sufficient? */
 
914
    Long   Ix, Rx, Ax;
 
915
 
 
916
    PLong  top;
 
917
 
 
918
 
 
919
    Dx = x2 - x1;
 
920
    Dy = y2 - y1;
 
921
 
 
922
    if ( Dy <= 0 || y2 < miny || y1 > maxy )
 
923
      return SUCCESS;
 
924
 
 
925
    if ( y1 < miny )
 
926
    {
 
927
      /* Take care: miny-y1 can be a very large value; we use     */
 
928
      /*            a slow MulDiv function to avoid clipping bugs */
 
929
      x1 += SMulDiv( Dx, miny - y1, Dy );
 
930
      e1  = (Int)TRUNC( miny );
 
931
      f1  = 0;
 
932
    }
 
933
    else
 
934
    {
 
935
      e1 = (Int)TRUNC( y1 );
 
936
      f1 = (Int)FRAC( y1 );
 
937
    }
 
938
 
 
939
    if ( y2 > maxy )
 
940
    {
 
941
      /* x2 += FMulDiv( Dx, maxy - y2, Dy );  UNNECESSARY */
 
942
      e2  = (Int)TRUNC( maxy );
 
943
      f2  = 0;
 
944
    }
 
945
    else
 
946
    {
 
947
      e2 = (Int)TRUNC( y2 );
 
948
      f2 = (Int)FRAC( y2 );
 
949
    }
 
950
 
 
951
    if ( f1 > 0 )
 
952
    {
 
953
      if ( e1 == e2 )
 
954
        return SUCCESS;
 
955
      else
 
956
      {
 
957
        x1 += FMulDiv( Dx, ras.precision - f1, Dy );
 
958
        e1 += 1;
 
959
      }
 
960
    }
 
961
    else
 
962
      if ( ras.joint )
 
963
      {
 
964
        ras.top--;
 
965
        ras.joint = FALSE;
 
966
      }
 
967
 
 
968
    ras.joint = (char)( f2 == 0 );
 
969
 
 
970
    if ( ras.fresh )
 
971
    {
 
972
      ras.cProfile->start = e1;
 
973
      ras.fresh           = FALSE;
 
974
    }
 
975
 
 
976
    size = e2 - e1 + 1;
 
977
    if ( ras.top + size >= ras.maxBuff )
 
978
    {
 
979
      ras.error = Raster_Err_Overflow;
 
980
      return FAILURE;
 
981
    }
 
982
 
 
983
    if ( Dx > 0 )
 
984
    {
 
985
      Ix = ( ras.precision * Dx ) / Dy;
 
986
      Rx = ( ras.precision * Dx ) % Dy;
 
987
      Dx = 1;
 
988
    }
 
989
    else
 
990
    {
 
991
      Ix = -( ( ras.precision * -Dx ) / Dy );
 
992
      Rx =    ( ras.precision * -Dx ) % Dy;
 
993
      Dx = -1;
 
994
    }
 
995
 
 
996
    Ax  = -Dy;
 
997
    top = ras.top;
 
998
 
 
999
    while ( size > 0 )
 
1000
    {
 
1001
      *top++ = x1;
 
1002
 
 
1003
      x1 += Ix;
 
1004
      Ax += Rx;
 
1005
      if ( Ax >= 0 )
 
1006
      {
 
1007
        Ax -= Dy;
 
1008
        x1 += Dx;
 
1009
      }
 
1010
      size--;
 
1011
    }
 
1012
 
 
1013
    ras.top = top;
 
1014
    return SUCCESS;
 
1015
  }
 
1016
 
 
1017
 
 
1018
  /*************************************************************************/
 
1019
  /*                                                                       */
 
1020
  /* <Function>                                                            */
 
1021
  /*    Line_Down                                                          */
 
1022
  /*                                                                       */
 
1023
  /* <Description>                                                         */
 
1024
  /*    Computes the x-coordinates of an descending line segment and       */
 
1025
  /*    stores them in the render pool.                                    */
 
1026
  /*                                                                       */
 
1027
  /* <Input>                                                               */
 
1028
  /*    x1   :: The x-coordinate of the segment's start point.             */
 
1029
  /*                                                                       */
 
1030
  /*    y1   :: The y-coordinate of the segment's start point.             */
 
1031
  /*                                                                       */
 
1032
  /*    x2   :: The x-coordinate of the segment's end point.               */
 
1033
  /*                                                                       */
 
1034
  /*    y2   :: The y-coordinate of the segment's end point.               */
 
1035
  /*                                                                       */
 
1036
  /*    miny :: A lower vertical clipping bound value.                     */
 
1037
  /*                                                                       */
 
1038
  /*    maxy :: 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
    Bool  result, fresh;
 
1052
 
 
1053
 
 
1054
    fresh  = ras.fresh;
 
1055
 
 
1056
    result = Line_Up( RAS_VARS x1, -y1, x2, -y2, -maxy, -miny );
 
1057
 
 
1058
    if ( fresh && !ras.fresh )
 
1059
      ras.cProfile->start = -ras.cProfile->start;
 
1060
 
 
1061
    return result;
 
1062
  }
 
1063
 
 
1064
 
 
1065
  /* A function type describing the functions used to split Bezier arcs */
 
1066
  typedef void  (*TSplitter)( TPoint*  base );
 
1067
 
 
1068
 
 
1069
  /*************************************************************************/
 
1070
  /*                                                                       */
 
1071
  /* <Function>                                                            */
 
1072
  /*    Bezier_Up                                                          */
 
1073
  /*                                                                       */
 
1074
  /* <Description>                                                         */
 
1075
  /*    Computes the x-coordinates of an ascending Bezier arc and stores   */
 
1076
  /*    them in the render pool.                                           */
 
1077
  /*                                                                       */
 
1078
  /* <Input>                                                               */
 
1079
  /*    degree   :: The degree of the Bezier arc (either 2 or 3).          */
 
1080
  /*                                                                       */
 
1081
  /*    splitter :: The function to split Bezier arcs.                     */
 
1082
  /*                                                                       */
 
1083
  /*    miny     :: A lower vertical clipping bound value.                 */
 
1084
  /*                                                                       */
 
1085
  /*    maxy     :: An upper vertical clipping bound value.                */
 
1086
  /*                                                                       */
 
1087
  /* <Return>                                                              */
 
1088
  /*    SUCCESS on success, FAILURE on render pool overflow.               */
 
1089
  /*                                                                       */
 
1090
  static Bool
 
1091
  Bezier_Up( RAS_ARGS Int        degree,
 
1092
                      TSplitter  splitter,
 
1093
                      Long       miny,
 
1094
                      Long       maxy )
 
1095
  {
 
1096
    Long   y1, y2, e, e2, e0;
 
1097
    Short  f1;
 
1098
 
 
1099
    TPoint*  arc;
 
1100
    TPoint*  start_arc;
 
1101
 
 
1102
    PLong top;
 
1103
 
 
1104
 
 
1105
    arc = ras.arc;
 
1106
    y1  = arc[degree].y;
 
1107
    y2  = arc[0].y;
 
1108
    top = ras.top;
 
1109
 
 
1110
    if ( y2 < miny || y1 > maxy )
 
1111
      goto Fin;
 
1112
 
 
1113
    e2 = FLOOR( y2 );
 
1114
 
 
1115
    if ( e2 > maxy )
 
1116
      e2 = maxy;
 
1117
 
 
1118
    e0 = miny;
 
1119
 
 
1120
    if ( y1 < miny )
 
1121
      e = miny;
 
1122
    else
 
1123
    {
 
1124
      e  = CEILING( y1 );
 
1125
      f1 = (Short)( FRAC( y1 ) );
 
1126
      e0 = e;
 
1127
 
 
1128
      if ( f1 == 0 )
 
1129
      {
 
1130
        if ( ras.joint )
 
1131
        {
 
1132
          top--;
 
1133
          ras.joint = FALSE;
 
1134
        }
 
1135
 
 
1136
        *top++ = arc[degree].x;
 
1137
 
 
1138
        e += ras.precision;
 
1139
      }
 
1140
    }
 
1141
 
 
1142
    if ( ras.fresh )
 
1143
    {
 
1144
      ras.cProfile->start = TRUNC( e0 );
 
1145
      ras.fresh = FALSE;
 
1146
    }
 
1147
 
 
1148
    if ( e2 < e )
 
1149
      goto Fin;
 
1150
 
 
1151
    if ( ( top + TRUNC( e2 - e ) + 1 ) >= ras.maxBuff )
 
1152
    {
 
1153
      ras.top   = top;
 
1154
      ras.error = Raster_Err_Overflow;
 
1155
      return FAILURE;
 
1156
    }
 
1157
 
 
1158
    start_arc = arc;
 
1159
 
 
1160
    while ( arc >= start_arc && e <= e2 )
 
1161
    {
 
1162
      ras.joint = FALSE;
 
1163
 
 
1164
      y2 = arc[0].y;
 
1165
 
 
1166
      if ( y2 > e )
 
1167
      {
 
1168
        y1 = arc[degree].y;
 
1169
        if ( y2 - y1 >= ras.precision_step )
 
1170
        {
 
1171
          splitter( arc );
 
1172
          arc += degree;
 
1173
        }
 
1174
        else
 
1175
        {
 
1176
          *top++ = arc[degree].x + FMulDiv( arc[0].x-arc[degree].x,
 
1177
                                            e - y1, y2 - y1 );
 
1178
          arc -= degree;
 
1179
          e   += ras.precision;
 
1180
        }
 
1181
      }
 
1182
      else
 
1183
      {
 
1184
        if ( y2 == e )
 
1185
        {
 
1186
          ras.joint  = TRUE;
 
1187
          *top++     = arc[0].x;
 
1188
 
 
1189
          e += ras.precision;
 
1190
        }
 
1191
        arc -= degree;
 
1192
      }
 
1193
    }
 
1194
 
 
1195
  Fin:
 
1196
    ras.top  = top;
 
1197
    ras.arc -= degree;
 
1198
    return SUCCESS;
 
1199
  }
 
1200
 
 
1201
 
 
1202
  /*************************************************************************/
 
1203
  /*                                                                       */
 
1204
  /* <Function>                                                            */
 
1205
  /*    Bezier_Down                                                        */
 
1206
  /*                                                                       */
 
1207
  /* <Description>                                                         */
 
1208
  /*    Computes the x-coordinates of an descending Bezier arc and stores  */
 
1209
  /*    them in the render pool.                                           */
 
1210
  /*                                                                       */
 
1211
  /* <Input>                                                               */
 
1212
  /*    degree   :: The degree of the Bezier arc (either 2 or 3).          */
 
1213
  /*                                                                       */
 
1214
  /*    splitter :: The function to split Bezier arcs.                     */
 
1215
  /*                                                                       */
 
1216
  /*    miny     :: A lower vertical clipping bound value.                 */
 
1217
  /*                                                                       */
 
1218
  /*    maxy     :: An upper vertical clipping bound value.                */
 
1219
  /*                                                                       */
 
1220
  /* <Return>                                                              */
 
1221
  /*    SUCCESS on success, FAILURE on render pool overflow.               */
 
1222
  /*                                                                       */
 
1223
  static Bool
 
1224
  Bezier_Down( RAS_ARGS Int        degree,
 
1225
                        TSplitter  splitter,
 
1226
                        Long       miny,
 
1227
                        Long       maxy )
 
1228
  {
 
1229
    TPoint*  arc = ras.arc;
 
1230
    Bool     result, fresh;
 
1231
 
 
1232
 
 
1233
    arc[0].y = -arc[0].y;
 
1234
    arc[1].y = -arc[1].y;
 
1235
    arc[2].y = -arc[2].y;
 
1236
    if ( degree > 2 )
 
1237
      arc[3].y = -arc[3].y;
 
1238
 
 
1239
    fresh = ras.fresh;
 
1240
 
 
1241
    result = Bezier_Up( RAS_VARS degree, splitter, -maxy, -miny );
 
1242
 
 
1243
    if ( fresh && !ras.fresh )
 
1244
      ras.cProfile->start = -ras.cProfile->start;
 
1245
 
 
1246
    arc[0].y = -arc[0].y;
 
1247
    return result;
 
1248
  }
 
1249
 
 
1250
 
 
1251
  /*************************************************************************/
 
1252
  /*                                                                       */
 
1253
  /* <Function>                                                            */
 
1254
  /*    Line_To                                                            */
 
1255
  /*                                                                       */
 
1256
  /* <Description>                                                         */
 
1257
  /*    Injects a new line segment and adjusts Profiles list.              */
 
1258
  /*                                                                       */
 
1259
  /* <Input>                                                               */
 
1260
  /*   x :: The x-coordinate of the segment's end point (its start point   */
 
1261
  /*        is stored in `LastX').                                         */
 
1262
  /*                                                                       */
 
1263
  /*   y :: The y-coordinate of the segment's end point (its start point   */
 
1264
  /*        is stored in `LastY').                                         */
 
1265
  /*                                                                       */
 
1266
  /* <Return>                                                              */
 
1267
  /*   SUCCESS on success, FAILURE on render pool overflow or incorrect    */
 
1268
  /*   profile.                                                            */
 
1269
  /*                                                                       */
 
1270
  static Bool
 
1271
  Line_To( RAS_ARGS Long  x,
 
1272
                    Long  y )
 
1273
  {
 
1274
    /* First, detect a change of direction */
 
1275
 
 
1276
    switch ( ras.state )
 
1277
    {
 
1278
    case Unknown_State:
 
1279
      if ( y > ras.lastY )
 
1280
      {
 
1281
        if ( New_Profile( RAS_VARS Ascending_State ) )
 
1282
          return FAILURE;
 
1283
      }
 
1284
      else
 
1285
      {
 
1286
        if ( y < ras.lastY )
 
1287
          if ( New_Profile( RAS_VARS Descending_State ) )
 
1288
            return FAILURE;
 
1289
      }
 
1290
      break;
 
1291
 
 
1292
    case Ascending_State:
 
1293
      if ( y < ras.lastY )
 
1294
      {
 
1295
        if ( End_Profile( RAS_VAR )                   ||
 
1296
             New_Profile( RAS_VARS Descending_State ) )
 
1297
          return FAILURE;
 
1298
      }
 
1299
      break;
 
1300
 
 
1301
    case Descending_State:
 
1302
      if ( y > ras.lastY )
 
1303
      {
 
1304
        if ( End_Profile( RAS_VAR )                  ||
 
1305
             New_Profile( RAS_VARS Ascending_State ) )
 
1306
          return FAILURE;
 
1307
      }
 
1308
      break;
 
1309
 
 
1310
    default:
 
1311
      ;
 
1312
    }
 
1313
 
 
1314
    /* Then compute the lines */
 
1315
 
 
1316
    switch ( ras.state )
 
1317
    {
 
1318
    case Ascending_State:
 
1319
      if ( Line_Up( RAS_VARS ras.lastX, ras.lastY,
 
1320
                    x, y, ras.minY, ras.maxY ) )
 
1321
        return FAILURE;
 
1322
      break;
 
1323
 
 
1324
    case Descending_State:
 
1325
      if ( Line_Down( RAS_VARS ras.lastX, ras.lastY,
 
1326
                      x, y, ras.minY, ras.maxY ) )
 
1327
        return FAILURE;
 
1328
      break;
 
1329
 
 
1330
    default:
 
1331
      ;
 
1332
    }
 
1333
 
 
1334
    ras.lastX = x;
 
1335
    ras.lastY = y;
 
1336
 
 
1337
    return SUCCESS;
 
1338
  }
 
1339
 
 
1340
 
 
1341
  /*************************************************************************/
 
1342
  /*                                                                       */
 
1343
  /* <Function>                                                            */
 
1344
  /*    Conic_To                                                           */
 
1345
  /*                                                                       */
 
1346
  /* <Description>                                                         */
 
1347
  /*    Injects a new conic arc and adjusts the profile list.              */
 
1348
  /*                                                                       */
 
1349
  /* <Input>                                                               */
 
1350
  /*   cx :: The x-coordinate of the arc's new control point.              */
 
1351
  /*                                                                       */
 
1352
  /*   cy :: The y-coordinate of the arc's new control point.              */
 
1353
  /*                                                                       */
 
1354
  /*   x  :: The x-coordinate of the arc's end point (its start point is   */
 
1355
  /*         stored in `LastX').                                           */
 
1356
  /*                                                                       */
 
1357
  /*   y  :: The y-coordinate of the arc's end point (its start point is   */
 
1358
  /*         stored in `LastY').                                           */
 
1359
  /*                                                                       */
 
1360
  /* <Return>                                                              */
 
1361
  /*   SUCCESS on success, FAILURE on render pool overflow or incorrect    */
 
1362
  /*   profile.                                                            */
 
1363
  /*                                                                       */
 
1364
  static Bool
 
1365
  Conic_To( RAS_ARGS Long  cx,
 
1366
                     Long  cy,
 
1367
                     Long  x,
 
1368
                     Long  y )
 
1369
  {
 
1370
    Long     y1, y2, y3, x3, ymin, ymax;
 
1371
    TStates  state_bez;
 
1372
 
 
1373
 
 
1374
    ras.arc      = ras.arcs;
 
1375
    ras.arc[2].x = ras.lastX;
 
1376
    ras.arc[2].y = ras.lastY;
 
1377
    ras.arc[1].x = cx; ras.arc[1].y = cy;
 
1378
    ras.arc[0].x = x;  ras.arc[0].y = y;
 
1379
 
 
1380
    do
 
1381
    {
 
1382
      y1 = ras.arc[2].y;
 
1383
      y2 = ras.arc[1].y;
 
1384
      y3 = ras.arc[0].y;
 
1385
      x3 = ras.arc[0].x;
 
1386
 
 
1387
      /* first, categorize the Bezier arc */
 
1388
 
 
1389
      if ( y1 <= y3 )
 
1390
      {
 
1391
        ymin = y1;
 
1392
        ymax = y3;
 
1393
      }
 
1394
      else
 
1395
      {
 
1396
        ymin = y3;
 
1397
        ymax = y1;
 
1398
      }
 
1399
 
 
1400
      if ( y2 < ymin || y2 > ymax )
 
1401
      {
 
1402
        /* this arc has no given direction, split it! */
 
1403
        Split_Conic( ras.arc );
 
1404
        ras.arc += 2;
 
1405
      }
 
1406
      else if ( y1 == y3 )
 
1407
      {
 
1408
        /* this arc is flat, ignore it and pop it from the Bezier stack */
 
1409
        ras.arc -= 2;
 
1410
      }
 
1411
      else
 
1412
      {
 
1413
        /* the arc is y-monotonous, either ascending or descending */
 
1414
        /* detect a change of direction                            */
 
1415
        state_bez = y1 < y3 ? Ascending_State : Descending_State;
 
1416
        if ( ras.state != state_bez )
 
1417
        {
 
1418
          /* finalize current profile if any */
 
1419
          if ( ras.state != Unknown_State   &&
 
1420
               End_Profile( RAS_VAR ) )
 
1421
            goto Fail;
 
1422
 
 
1423
          /* create a new profile */
 
1424
          if ( New_Profile( RAS_VARS state_bez ) )
 
1425
            goto Fail;
 
1426
        }
 
1427
 
 
1428
        /* now call the appropriate routine */
 
1429
        if ( state_bez == Ascending_State )
 
1430
        {
 
1431
          if ( Bezier_Up( RAS_VARS 2, Split_Conic, ras.minY, ras.maxY ) )
 
1432
            goto Fail;
 
1433
        }
 
1434
        else
 
1435
          if ( Bezier_Down( RAS_VARS 2, Split_Conic, ras.minY, ras.maxY ) )
 
1436
            goto Fail;
 
1437
      }
 
1438
 
 
1439
    } while ( ras.arc >= ras.arcs );
 
1440
 
 
1441
    ras.lastX = x3;
 
1442
    ras.lastY = y3;
 
1443
 
 
1444
    return SUCCESS;
 
1445
 
 
1446
  Fail:
 
1447
    return FAILURE;
 
1448
  }
 
1449
 
 
1450
 
 
1451
  /*************************************************************************/
 
1452
  /*                                                                       */
 
1453
  /* <Function>                                                            */
 
1454
  /*    Cubic_To                                                           */
 
1455
  /*                                                                       */
 
1456
  /* <Description>                                                         */
 
1457
  /*    Injects a new cubic arc and adjusts the profile list.              */
 
1458
  /*                                                                       */
 
1459
  /* <Input>                                                               */
 
1460
  /*   cx1 :: The x-coordinate of the arc's first new control point.       */
 
1461
  /*                                                                       */
 
1462
  /*   cy1 :: The y-coordinate of the arc's first new control point.       */
 
1463
  /*                                                                       */
 
1464
  /*   cx2 :: The x-coordinate of the arc's second new control point.      */
 
1465
  /*                                                                       */
 
1466
  /*   cy2 :: The y-coordinate of the arc's second new control point.      */
 
1467
  /*                                                                       */
 
1468
  /*   x   :: The x-coordinate of the arc's end point (its start point is  */
 
1469
  /*          stored in `LastX').                                          */
 
1470
  /*                                                                       */
 
1471
  /*   y   :: The y-coordinate of the arc's end point (its start point is  */
 
1472
  /*          stored in `LastY').                                          */
 
1473
  /*                                                                       */
 
1474
  /* <Return>                                                              */
 
1475
  /*   SUCCESS on success, FAILURE on render pool overflow or incorrect    */
 
1476
  /*   profile.                                                            */
 
1477
  /*                                                                       */
 
1478
  static Bool
 
1479
  Cubic_To( RAS_ARGS Long  cx1,
 
1480
                     Long  cy1,
 
1481
                     Long  cx2,
 
1482
                     Long  cy2,
 
1483
                     Long  x,
 
1484
                     Long  y )
 
1485
  {
 
1486
    Long     y1, y2, y3, y4, x4, ymin1, ymax1, ymin2, ymax2;
 
1487
    TStates  state_bez;
 
1488
 
 
1489
 
 
1490
    ras.arc      = ras.arcs;
 
1491
    ras.arc[3].x = ras.lastX;
 
1492
    ras.arc[3].y = ras.lastY;
 
1493
    ras.arc[2].x = cx1; ras.arc[2].y = cy1;
 
1494
    ras.arc[1].x = cx2; ras.arc[1].y = cy2;
 
1495
    ras.arc[0].x = x;   ras.arc[0].y = y;
 
1496
 
 
1497
    do
 
1498
    {
 
1499
      y1 = ras.arc[3].y;
 
1500
      y2 = ras.arc[2].y;
 
1501
      y3 = ras.arc[1].y;
 
1502
      y4 = ras.arc[0].y;
 
1503
      x4 = ras.arc[0].x;
 
1504
 
 
1505
      /* first, categorize the Bezier arc */
 
1506
 
 
1507
      if ( y1 <= y4 )
 
1508
      {
 
1509
        ymin1 = y1;
 
1510
        ymax1 = y4;
 
1511
      }
 
1512
      else
 
1513
      {
 
1514
        ymin1 = y4;
 
1515
        ymax1 = y1;
 
1516
      }
 
1517
 
 
1518
      if ( y2 <= y3 )
 
1519
      {
 
1520
        ymin2 = y2;
 
1521
        ymax2 = y3;
 
1522
      }
 
1523
      else
 
1524
      {
 
1525
        ymin2 = y3;
 
1526
        ymax2 = y2;
 
1527
      }
 
1528
 
 
1529
      if ( ymin2 < ymin1 || ymax2 > ymax1 )
 
1530
      {
 
1531
        /* this arc has no given direction, split it! */
 
1532
        Split_Cubic( ras.arc );
 
1533
        ras.arc += 3;
 
1534
      }
 
1535
      else if ( y1 == y4 )
 
1536
      {
 
1537
        /* this arc is flat, ignore it and pop it from the Bezier stack */
 
1538
        ras.arc -= 3;
 
1539
      }
 
1540
      else
 
1541
      {
 
1542
        state_bez = ( y1 <= y4 ) ? Ascending_State : Descending_State;
 
1543
 
 
1544
        /* detect a change of direction */
 
1545
        if ( ras.state != state_bez )
 
1546
        {
 
1547
          if ( ras.state != Unknown_State   &&
 
1548
               End_Profile( RAS_VAR ) )
 
1549
            goto Fail;
 
1550
 
 
1551
          if ( New_Profile( RAS_VARS state_bez ) )
 
1552
            goto Fail;
 
1553
        }
 
1554
 
 
1555
        /* compute intersections */
 
1556
        if ( state_bez == Ascending_State )
 
1557
        {
 
1558
          if ( Bezier_Up( RAS_VARS 3, Split_Cubic, ras.minY, ras.maxY ) )
 
1559
            goto Fail;
 
1560
        }
 
1561
        else
 
1562
          if ( Bezier_Down( RAS_VARS 3, Split_Cubic, ras.minY, ras.maxY ) )
 
1563
            goto Fail;
 
1564
      }
 
1565
 
 
1566
    } while ( ras.arc >= ras.arcs );
 
1567
 
 
1568
    ras.lastX = x4;
 
1569
    ras.lastY = y4;
 
1570
 
 
1571
    return SUCCESS;
 
1572
 
 
1573
  Fail:
 
1574
    return FAILURE;
 
1575
  }
 
1576
 
 
1577
 
 
1578
#undef  SWAP_
 
1579
#define SWAP_( x, y )  do                \
 
1580
                       {                 \
 
1581
                         Long  swap = x; \
 
1582
                                         \
 
1583
                                         \
 
1584
                         x = y;          \
 
1585
                         y = swap;       \
 
1586
                       } while ( 0 )
 
1587
 
 
1588
 
 
1589
  /*************************************************************************/
 
1590
  /*                                                                       */
 
1591
  /* <Function>                                                            */
 
1592
  /*    Decompose_Curve                                                    */
 
1593
  /*                                                                       */
 
1594
  /* <Description>                                                         */
 
1595
  /*    Scans the outline arays in order to emit individual segments and   */
 
1596
  /*    Beziers by calling Line_To() and Bezier_To().  It handles all      */
 
1597
  /*    weird cases, like when the first point is off the curve, or when   */
 
1598
  /*    there are simply no `on' points in the contour!                    */
 
1599
  /*                                                                       */
 
1600
  /* <Input>                                                               */
 
1601
  /*    first   :: The index of the first point in the contour.            */
 
1602
  /*                                                                       */
 
1603
  /*    last    :: The index of the last point in the contour.             */
 
1604
  /*                                                                       */
 
1605
  /*    flipped :: If set, flip the direction of the curve.                */
 
1606
  /*                                                                       */
 
1607
  /* <Return>                                                              */
 
1608
  /*    SUCCESS on success, FAILURE on error.                              */
 
1609
  /*                                                                       */
 
1610
  static Bool
 
1611
  Decompose_Curve( RAS_ARGS UShort  first,
 
1612
                            UShort  last,
 
1613
                            int     flipped )
 
1614
  {
 
1615
    FT_Vector   v_last;
 
1616
    FT_Vector   v_control;
 
1617
    FT_Vector   v_start;
 
1618
 
 
1619
    FT_Vector*  points;
 
1620
    FT_Vector*  point;
 
1621
    FT_Vector*  limit;
 
1622
    char*       tags;
 
1623
 
 
1624
    unsigned    tag;       /* current point's state           */
 
1625
 
 
1626
 
 
1627
    points = ras.outline.points;
 
1628
    limit  = points + last;
 
1629
 
 
1630
    v_start.x = SCALED( points[first].x );
 
1631
    v_start.y = SCALED( points[first].y );
 
1632
    v_last.x  = SCALED( points[last].x );
 
1633
    v_last.y  = SCALED( points[last].y );
 
1634
 
 
1635
    if ( flipped )
 
1636
    {
 
1637
      SWAP_( v_start.x, v_start.y );
 
1638
      SWAP_( v_last.x, v_last.y );
 
1639
    }
 
1640
 
 
1641
    v_control = v_start;
 
1642
 
 
1643
    point = points + first;
 
1644
    tags  = ras.outline.tags  + first;
 
1645
    tag   = FT_CURVE_TAG( tags[0] );
 
1646
 
 
1647
    /* A contour cannot start with a cubic control point! */
 
1648
    if ( tag == FT_CURVE_TAG_CUBIC )
 
1649
      goto Invalid_Outline;
 
1650
 
 
1651
    /* check first point to determine origin */
 
1652
    if ( tag == FT_CURVE_TAG_CONIC )
 
1653
    {
 
1654
      /* first point is conic control.  Yes, this happens. */
 
1655
      if ( FT_CURVE_TAG( ras.outline.tags[last] ) == FT_CURVE_TAG_ON )
 
1656
      {
 
1657
        /* start at last point if it is on the curve */
 
1658
        v_start = v_last;
 
1659
        limit--;
 
1660
      }
 
1661
      else
 
1662
      {
 
1663
        /* if both first and last points are conic,         */
 
1664
        /* start at their middle and record its position    */
 
1665
        /* for closure                                      */
 
1666
        v_start.x = ( v_start.x + v_last.x ) / 2;
 
1667
        v_start.y = ( v_start.y + v_last.y ) / 2;
 
1668
 
 
1669
        v_last = v_start;
 
1670
      }
 
1671
      point--;
 
1672
      tags--;
 
1673
    }
 
1674
 
 
1675
    ras.lastX = v_start.x;
 
1676
    ras.lastY = v_start.y;
 
1677
 
 
1678
    while ( point < limit )
 
1679
    {
 
1680
      point++;
 
1681
      tags++;
 
1682
 
 
1683
      tag = FT_CURVE_TAG( tags[0] );
 
1684
 
 
1685
      switch ( tag )
 
1686
      {
 
1687
      case FT_CURVE_TAG_ON:  /* emit a single line_to */
 
1688
        {
 
1689
          Long  x, y;
 
1690
 
 
1691
 
 
1692
          x = SCALED( point->x );
 
1693
          y = SCALED( point->y );
 
1694
          if ( flipped )
 
1695
            SWAP_( x, y );
 
1696
 
 
1697
          if ( Line_To( RAS_VARS x, y ) )
 
1698
            goto Fail;
 
1699
          continue;
 
1700
        }
 
1701
 
 
1702
      case FT_CURVE_TAG_CONIC:  /* consume conic arcs */
 
1703
        v_control.x = SCALED( point[0].x );
 
1704
        v_control.y = SCALED( point[0].y );
 
1705
 
 
1706
        if ( flipped )
 
1707
          SWAP_( v_control.x, v_control.y );
 
1708
 
 
1709
      Do_Conic:
 
1710
        if ( point < limit )
 
1711
        {
 
1712
          FT_Vector  v_middle;
 
1713
          Long       x, y;
 
1714
 
 
1715
 
 
1716
          point++;
 
1717
          tags++;
 
1718
          tag = FT_CURVE_TAG( tags[0] );
 
1719
 
 
1720
          x = SCALED( point[0].x );
 
1721
          y = SCALED( point[0].y );
 
1722
 
 
1723
          if ( flipped )
 
1724
            SWAP_( x, y );
 
1725
 
 
1726
          if ( tag == FT_CURVE_TAG_ON )
 
1727
          {
 
1728
            if ( Conic_To( RAS_VARS v_control.x, v_control.y, x, y ) )
 
1729
              goto Fail;
 
1730
            continue;
 
1731
          }
 
1732
 
 
1733
          if ( tag != FT_CURVE_TAG_CONIC )
 
1734
            goto Invalid_Outline;
 
1735
 
 
1736
          v_middle.x = ( v_control.x + x ) / 2;
 
1737
          v_middle.y = ( v_control.y + y ) / 2;
 
1738
 
 
1739
          if ( Conic_To( RAS_VARS v_control.x, v_control.y,
 
1740
                                  v_middle.x,  v_middle.y ) )
 
1741
            goto Fail;
 
1742
 
 
1743
          v_control.x = x;
 
1744
          v_control.y = y;
 
1745
 
 
1746
          goto Do_Conic;
 
1747
        }
 
1748
 
 
1749
        if ( Conic_To( RAS_VARS v_control.x, v_control.y,
 
1750
                                v_start.x,   v_start.y ) )
 
1751
          goto Fail;
 
1752
 
 
1753
        goto Close;
 
1754
 
 
1755
      default:  /* FT_CURVE_TAG_CUBIC */
 
1756
        {
 
1757
          Long  x1, y1, x2, y2, x3, y3;
 
1758
 
 
1759
 
 
1760
          if ( point + 1 > limit                             ||
 
1761
               FT_CURVE_TAG( tags[1] ) != FT_CURVE_TAG_CUBIC )
 
1762
            goto Invalid_Outline;
 
1763
 
 
1764
          point += 2;
 
1765
          tags  += 2;
 
1766
 
 
1767
          x1 = SCALED( point[-2].x );
 
1768
          y1 = SCALED( point[-2].y );
 
1769
          x2 = SCALED( point[-1].x );
 
1770
          y2 = SCALED( point[-1].y );
 
1771
          x3 = SCALED( point[ 0].x );
 
1772
          y3 = SCALED( point[ 0].y );
 
1773
 
 
1774
          if ( flipped )
 
1775
          {
 
1776
            SWAP_( x1, y1 );
 
1777
            SWAP_( x2, y2 );
 
1778
            SWAP_( x3, y3 );
 
1779
          }
 
1780
 
 
1781
          if ( point <= limit )
 
1782
          {
 
1783
            if ( Cubic_To( RAS_VARS x1, y1, x2, y2, x3, y3 ) )
 
1784
              goto Fail;
 
1785
            continue;
 
1786
          }
 
1787
 
 
1788
          if ( Cubic_To( RAS_VARS x1, y1, x2, y2, v_start.x, v_start.y ) )
 
1789
            goto Fail;
 
1790
          goto Close;
 
1791
        }
 
1792
      }
 
1793
    }
 
1794
 
 
1795
    /* close the contour with a line segment */
 
1796
    if ( Line_To( RAS_VARS v_start.x, v_start.y ) )
 
1797
      goto Fail;
 
1798
 
 
1799
  Close:
 
1800
    return SUCCESS;
 
1801
 
 
1802
  Invalid_Outline:
 
1803
    ras.error = Raster_Err_Invalid;
 
1804
 
 
1805
  Fail:
 
1806
    return FAILURE;
 
1807
  }
 
1808
 
 
1809
 
 
1810
  /*************************************************************************/
 
1811
  /*                                                                       */
 
1812
  /* <Function>                                                            */
 
1813
  /*    Convert_Glyph                                                      */
 
1814
  /*                                                                       */
 
1815
  /* <Description>                                                         */
 
1816
  /*    Converts a glyph into a series of segments and arcs and makes a    */
 
1817
  /*    profiles list with them.                                           */
 
1818
  /*                                                                       */
 
1819
  /* <Input>                                                               */
 
1820
  /*    flipped :: 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
    unsigned  start;
 
1831
 
 
1832
    PProfile  lastProfile;
 
1833
 
 
1834
 
 
1835
    ras.fProfile = NULL;
 
1836
    ras.joint    = FALSE;
 
1837
    ras.fresh    = FALSE;
 
1838
 
 
1839
    ras.maxBuff  = ras.sizeBuff - AlignProfileSize;
 
1840
 
 
1841
    ras.numTurns = 0;
 
1842
 
 
1843
    ras.cProfile         = (PProfile)ras.top;
 
1844
    ras.cProfile->offset = ras.top;
 
1845
    ras.num_Profs        = 0;
 
1846
 
 
1847
    start = 0;
 
1848
 
 
1849
    for ( i = 0; i < ras.outline.n_contours; i++ )
 
1850
    {
 
1851
      ras.state    = Unknown_State;
 
1852
      ras.gProfile = NULL;
 
1853
 
 
1854
      if ( Decompose_Curve( RAS_VARS (unsigned short)start,
 
1855
                            ras.outline.contours[i],
 
1856
                            flipped ) )
 
1857
        return FAILURE;
 
1858
 
 
1859
      start = ras.outline.contours[i] + 1;
 
1860
 
 
1861
      /* We must now see whether the extreme arcs join or not */
 
1862
      if ( FRAC( ras.lastY ) == 0 &&
 
1863
           ras.lastY >= ras.minY  &&
 
1864
           ras.lastY <= ras.maxY  )
 
1865
        if ( ras.gProfile && ras.gProfile->flow == ras.cProfile->flow )
 
1866
          ras.top--;
 
1867
        /* Note that ras.gProfile can be nil if the contour was too small */
 
1868
        /* to be drawn.                                                   */
 
1869
 
 
1870
      lastProfile = ras.cProfile;
 
1871
      if ( End_Profile( RAS_VAR ) )
 
1872
        return FAILURE;
 
1873
 
 
1874
      /* close the `next profile in contour' linked list */
 
1875
      if ( ras.gProfile )
 
1876
        lastProfile->next = ras.gProfile;
 
1877
    }
 
1878
 
 
1879
    if ( Finalize_Profile_Table( RAS_VAR ) )
 
1880
      return FAILURE;
 
1881
 
 
1882
    return (Bool)( ras.top < ras.maxBuff ? SUCCESS : FAILURE );
 
1883
  }
 
1884
 
 
1885
 
 
1886
  /*************************************************************************/
 
1887
  /*************************************************************************/
 
1888
  /**                                                                     **/
 
1889
  /**  SCAN-LINE SWEEPS AND DRAWING                                       **/
 
1890
  /**                                                                     **/
 
1891
  /*************************************************************************/
 
1892
  /*************************************************************************/
 
1893
 
 
1894
 
 
1895
  /*************************************************************************/
 
1896
  /*                                                                       */
 
1897
  /*  Init_Linked                                                          */
 
1898
  /*                                                                       */
 
1899
  /*    Initializes an empty linked list.                                  */
 
1900
  /*                                                                       */
 
1901
  static void
 
1902
  Init_Linked( TProfileList*  l )
 
1903
  {
 
1904
    *l = NULL;
 
1905
  }
 
1906
 
 
1907
 
 
1908
  /*************************************************************************/
 
1909
  /*                                                                       */
 
1910
  /*  InsNew                                                               */
 
1911
  /*                                                                       */
 
1912
  /*    Inserts a new profile in a linked list.                            */
 
1913
  /*                                                                       */
 
1914
  static void
 
1915
  InsNew( PProfileList  list,
 
1916
          PProfile      profile )
 
1917
  {
 
1918
    PProfile  *old, current;
 
1919
    Long       x;
 
1920
 
 
1921
 
 
1922
    old     = list;
 
1923
    current = *old;
 
1924
    x       = profile->X;
 
1925
 
 
1926
    while ( current )
 
1927
    {
 
1928
      if ( x < current->X )
 
1929
        break;
 
1930
      old     = &current->link;
 
1931
      current = *old;
 
1932
    }
 
1933
 
 
1934
    profile->link = current;
 
1935
    *old          = profile;
 
1936
  }
 
1937
 
 
1938
 
 
1939
  /*************************************************************************/
 
1940
  /*                                                                       */
 
1941
  /*  DelOld                                                               */
 
1942
  /*                                                                       */
 
1943
  /*    Removes an old profile from a linked list.                         */
 
1944
  /*                                                                       */
 
1945
  static void
 
1946
  DelOld( PProfileList  list,
 
1947
          PProfile      profile )
 
1948
  {
 
1949
    PProfile  *old, current;
 
1950
 
 
1951
 
 
1952
    old     = list;
 
1953
    current = *old;
 
1954
 
 
1955
    while ( current )
 
1956
    {
 
1957
      if ( current == profile )
 
1958
      {
 
1959
        *old = current->link;
 
1960
        return;
 
1961
      }
 
1962
 
 
1963
      old     = &current->link;
 
1964
      current = *old;
 
1965
    }
 
1966
 
 
1967
    /* we should never get there, unless the profile was not part of */
 
1968
    /* the list.                                                     */
 
1969
  }
 
1970
 
 
1971
 
 
1972
  /*************************************************************************/
 
1973
  /*                                                                       */
 
1974
  /*  Sort                                                                 */
 
1975
  /*                                                                       */
 
1976
  /*    Sorts a trace list.  In 95%, the list is already sorted.  We need  */
 
1977
  /*    an algorithm which is fast in this case.  Bubble sort is enough    */
 
1978
  /*    and simple.                                                        */
 
1979
  /*                                                                       */
 
1980
  static void
 
1981
  Sort( PProfileList  list )
 
1982
  {
 
1983
    PProfile  *old, current, next;
 
1984
 
 
1985
 
 
1986
    /* First, set the new X coordinate of each profile */
 
1987
    current = *list;
 
1988
    while ( current )
 
1989
    {
 
1990
      current->X       = *current->offset;
 
1991
      current->offset += current->flow;
 
1992
      current->height--;
 
1993
      current = current->link;
 
1994
    }
 
1995
 
 
1996
    /* Then sort them */
 
1997
    old     = list;
 
1998
    current = *old;
 
1999
 
 
2000
    if ( !current )
 
2001
      return;
 
2002
 
 
2003
    next = current->link;
 
2004
 
 
2005
    while ( next )
 
2006
    {
 
2007
      if ( current->X <= next->X )
 
2008
      {
 
2009
        old     = &current->link;
 
2010
        current = *old;
 
2011
 
 
2012
        if ( !current )
 
2013
          return;
 
2014
      }
 
2015
      else
 
2016
      {
 
2017
        *old          = next;
 
2018
        current->link = next->link;
 
2019
        next->link    = current;
 
2020
 
 
2021
        old     = list;
 
2022
        current = *old;
 
2023
      }
 
2024
 
 
2025
      next = current->link;
 
2026
    }
 
2027
  }
 
2028
 
 
2029
 
 
2030
  /*************************************************************************/
 
2031
  /*                                                                       */
 
2032
  /*  Vertical Sweep Procedure Set                                         */
 
2033
  /*                                                                       */
 
2034
  /*  These four routines are used during the vertical black/white sweep   */
 
2035
  /*  phase by the generic Draw_Sweep() function.                          */
 
2036
  /*                                                                       */
 
2037
  /*************************************************************************/
 
2038
 
 
2039
  static void
 
2040
  Vertical_Sweep_Init( RAS_ARGS Short*  min,
 
2041
                                Short*  max )
 
2042
  {
 
2043
    Long  pitch = ras.target.pitch;
 
2044
 
 
2045
    FT_UNUSED( max );
 
2046
 
 
2047
 
 
2048
    ras.traceIncr = (Short)-pitch;
 
2049
    ras.traceOfs  = -*min * pitch;
 
2050
    if ( pitch > 0 )
 
2051
      ras.traceOfs += ( ras.target.rows - 1 ) * pitch;
 
2052
 
 
2053
    ras.gray_min_x = 0;
 
2054
    ras.gray_max_x = 0;
 
2055
  }
 
2056
 
 
2057
 
 
2058
  static void
 
2059
  Vertical_Sweep_Span( RAS_ARGS Short       y,
 
2060
                                FT_F26Dot6  x1,
 
2061
                                FT_F26Dot6  x2,
 
2062
                                PProfile    left,
 
2063
                                PProfile    right )
 
2064
  {
 
2065
    Long   e1, e2;
 
2066
    int    c1, c2;
 
2067
    Byte   f1, f2;
 
2068
    Byte*  target;
 
2069
 
 
2070
    FT_UNUSED( y );
 
2071
    FT_UNUSED( left );
 
2072
    FT_UNUSED( right );
 
2073
 
 
2074
 
 
2075
    /* Drop-out control */
 
2076
 
 
2077
    e1 = TRUNC( CEILING( x1 ) );
 
2078
 
 
2079
    if ( x2 - x1 - ras.precision <= ras.precision_jitter )
 
2080
      e2 = e1;
 
2081
    else
 
2082
      e2 = TRUNC( FLOOR( x2 ) );
 
2083
 
 
2084
    if ( e2 >= 0 && e1 < ras.bWidth )
 
2085
    {
 
2086
      if ( e1 < 0 )
 
2087
        e1 = 0;
 
2088
      if ( e2 >= ras.bWidth )
 
2089
        e2 = ras.bWidth - 1;
 
2090
 
 
2091
      c1 = (Short)( e1 >> 3 );
 
2092
      c2 = (Short)( e2 >> 3 );
 
2093
 
 
2094
      f1 = (Byte)  ( 0xFF >> ( e1 & 7 ) );
 
2095
      f2 = (Byte) ~( 0x7F >> ( e2 & 7 ) );
 
2096
 
 
2097
      if ( ras.gray_min_x > c1 ) ras.gray_min_x = (short)c1;
 
2098
      if ( ras.gray_max_x < c2 ) ras.gray_max_x = (short)c2;
 
2099
 
 
2100
      target = ras.bTarget + ras.traceOfs + c1;
 
2101
      c2 -= c1;
 
2102
 
 
2103
      if ( c2 > 0 )
 
2104
      {
 
2105
        target[0] |= f1;
 
2106
 
 
2107
        /* memset() is slower than the following code on many platforms. */
 
2108
        /* This is due to the fact that, in the vast majority of cases,  */
 
2109
        /* the span length in bytes is relatively small.                 */
 
2110
        c2--;
 
2111
        while ( c2 > 0 )
 
2112
        {
 
2113
          *(++target) = 0xFF;
 
2114
          c2--;
 
2115
        }
 
2116
        target[1] |= f2;
 
2117
      }
 
2118
      else
 
2119
        *target |= ( f1 & f2 );
 
2120
    }
 
2121
  }
 
2122
 
 
2123
 
 
2124
  static void
 
2125
  Vertical_Sweep_Drop( RAS_ARGS Short       y,
 
2126
                                FT_F26Dot6  x1,
 
2127
                                FT_F26Dot6  x2,
 
2128
                                PProfile    left,
 
2129
                                PProfile    right )
 
2130
  {
 
2131
    Long   e1, e2;
 
2132
    Short  c1, f1;
 
2133
 
 
2134
 
 
2135
    /* Drop-out control */
 
2136
 
 
2137
    e1 = CEILING( x1 );
 
2138
    e2 = FLOOR  ( x2 );
 
2139
 
 
2140
    if ( e1 > e2 )
 
2141
    {
 
2142
      if ( e1 == e2 + ras.precision )
 
2143
      {
 
2144
        switch ( ras.dropOutControl )
 
2145
        {
 
2146
        case 1:
 
2147
          e1 = e2;
 
2148
          break;
 
2149
 
 
2150
        case 4:
 
2151
          e1 = CEILING( (x1 + x2 + 1) / 2 );
 
2152
          break;
 
2153
 
 
2154
        case 2:
 
2155
        case 5:
 
2156
          /* Drop-out Control Rule #4 */
 
2157
 
 
2158
          /* The spec is not very clear regarding rule #4.  It      */
 
2159
          /* presents a method that is way too costly to implement  */
 
2160
          /* while the general idea seems to get rid of `stubs'.    */
 
2161
          /*                                                        */
 
2162
          /* Here, we only get rid of stubs recognized if:          */
 
2163
          /*                                                        */
 
2164
          /*  upper stub:                                           */
 
2165
          /*                                                        */
 
2166
          /*   - P_Left and P_Right are in the same contour         */
 
2167
          /*   - P_Right is the successor of P_Left in that contour */
 
2168
          /*   - y is the top of P_Left and P_Right                 */
 
2169
          /*                                                        */
 
2170
          /*  lower stub:                                           */
 
2171
          /*                                                        */
 
2172
          /*   - P_Left and P_Right are in the same contour         */
 
2173
          /*   - P_Left is the successor of P_Right in that contour */
 
2174
          /*   - y is the bottom of P_Left                          */
 
2175
          /*                                                        */
 
2176
 
 
2177
          /* FIXXXME: uncommenting this line solves the disappearing */
 
2178
          /*          bit problem in the `7' of verdana 10pts, but   */
 
2179
          /*          makes a new one in the `C' of arial 14pts      */
 
2180
 
 
2181
#if 0
 
2182
          if ( x2 - x1 < ras.precision_half )
 
2183
#endif
 
2184
          {
 
2185
            /* upper stub test */
 
2186
            if ( left->next == right && left->height <= 0 )
 
2187
              return;
 
2188
 
 
2189
            /* lower stub test */
 
2190
            if ( right->next == left && left->start == y )
 
2191
              return;
 
2192
          }
 
2193
 
 
2194
          /* check that the rightmost pixel isn't set */
 
2195
 
 
2196
          e1 = TRUNC( e1 );
 
2197
 
 
2198
          c1 = (Short)( e1 >> 3 );
 
2199
          f1 = (Short)( e1 &  7 );
 
2200
 
 
2201
          if ( e1 >= 0 && e1 < ras.bWidth                      &&
 
2202
               ras.bTarget[ras.traceOfs + c1] & ( 0x80 >> f1 ) )
 
2203
            return;
 
2204
 
 
2205
          if ( ras.dropOutControl == 2 )
 
2206
            e1 = e2;
 
2207
          else
 
2208
            e1 = CEILING( ( x1 + x2 + 1 ) / 2 );
 
2209
 
 
2210
          break;
 
2211
 
 
2212
        default:
 
2213
          return;  /* unsupported mode */
 
2214
        }
 
2215
      }
 
2216
      else
 
2217
        return;
 
2218
    }
 
2219
 
 
2220
    e1 = TRUNC( e1 );
 
2221
 
 
2222
    if ( e1 >= 0 && e1 < ras.bWidth )
 
2223
    {
 
2224
      c1 = (Short)( e1 >> 3 );
 
2225
      f1 = (Short)( e1 & 7 );
 
2226
 
 
2227
      if ( ras.gray_min_x > c1 ) ras.gray_min_x = c1;
 
2228
      if ( ras.gray_max_x < c1 ) ras.gray_max_x = c1;
 
2229
 
 
2230
      ras.bTarget[ras.traceOfs + c1] |= (char)( 0x80 >> f1 );
 
2231
    }
 
2232
  }
 
2233
 
 
2234
 
 
2235
  static void
 
2236
  Vertical_Sweep_Step( RAS_ARG )
 
2237
  {
 
2238
    ras.traceOfs += ras.traceIncr;
 
2239
  }
 
2240
 
 
2241
 
 
2242
  /***********************************************************************/
 
2243
  /*                                                                     */
 
2244
  /*  Horizontal Sweep Procedure Set                                     */
 
2245
  /*                                                                     */
 
2246
  /*  These four routines are used during the horizontal black/white     */
 
2247
  /*  sweep phase by the generic Draw_Sweep() function.                  */
 
2248
  /*                                                                     */
 
2249
  /***********************************************************************/
 
2250
 
 
2251
  static void
 
2252
  Horizontal_Sweep_Init( RAS_ARGS Short*  min,
 
2253
                                  Short*  max )
 
2254
  {
 
2255
    /* nothing, really */
 
2256
    FT_UNUSED( raster );
 
2257
    FT_UNUSED( min );
 
2258
    FT_UNUSED( max );
 
2259
  }
 
2260
 
 
2261
 
 
2262
  static void
 
2263
  Horizontal_Sweep_Span( RAS_ARGS Short       y,
 
2264
                                  FT_F26Dot6  x1,
 
2265
                                  FT_F26Dot6  x2,
 
2266
                                  PProfile    left,
 
2267
                                  PProfile    right )
 
2268
  {
 
2269
    Long   e1, e2;
 
2270
    PByte  bits;
 
2271
    Byte   f1;
 
2272
 
 
2273
    FT_UNUSED( left );
 
2274
    FT_UNUSED( right );
 
2275
 
 
2276
 
 
2277
    if ( x2 - x1 < ras.precision )
 
2278
    {
 
2279
      e1 = CEILING( x1 );
 
2280
      e2 = FLOOR  ( x2 );
 
2281
 
 
2282
      if ( e1 == e2 )
 
2283
      {
 
2284
        bits = ras.bTarget + ( y >> 3 );
 
2285
        f1   = (Byte)( 0x80 >> ( y & 7 ) );
 
2286
 
 
2287
        e1 = TRUNC( e1 );
 
2288
 
 
2289
        if ( e1 >= 0 && e1 < ras.target.rows )
 
2290
        {
 
2291
          PByte  p;
 
2292
 
 
2293
 
 
2294
          p = bits - e1*ras.target.pitch;
 
2295
          if ( ras.target.pitch > 0 )
 
2296
            p += ( ras.target.rows - 1 ) * ras.target.pitch;
 
2297
 
 
2298
          p[0] |= f1;
 
2299
        }
 
2300
      }
 
2301
    }
 
2302
  }
 
2303
 
 
2304
 
 
2305
  static void
 
2306
  Horizontal_Sweep_Drop( RAS_ARGS Short       y,
 
2307
                                  FT_F26Dot6  x1,
 
2308
                                  FT_F26Dot6  x2,
 
2309
                                  PProfile    left,
 
2310
                                  PProfile    right )
 
2311
  {
 
2312
    Long   e1, e2;
 
2313
    PByte  bits;
 
2314
    Byte   f1;
 
2315
 
 
2316
 
 
2317
    /* During the horizontal sweep, we only take care of drop-outs */
 
2318
 
 
2319
    e1 = CEILING( x1 );
 
2320
    e2 = FLOOR  ( x2 );
 
2321
 
 
2322
    if ( e1 > e2 )
 
2323
    {
 
2324
      if ( e1 == e2 + ras.precision )
 
2325
      {
 
2326
        switch ( ras.dropOutControl )
 
2327
        {
 
2328
        case 1:
 
2329
          e1 = e2;
 
2330
          break;
 
2331
 
 
2332
        case 4:
 
2333
          e1 = CEILING( ( x1 + x2 + 1 ) / 2 );
 
2334
          break;
 
2335
 
 
2336
        case 2:
 
2337
        case 5:
 
2338
 
 
2339
          /* Drop-out Control Rule #4 */
 
2340
 
 
2341
          /* The spec is not very clear regarding rule #4.  It      */
 
2342
          /* presents a method that is way too costly to implement  */
 
2343
          /* while the general idea seems to get rid of `stubs'.    */
 
2344
          /*                                                        */
 
2345
 
 
2346
          /* rightmost stub test */
 
2347
          if ( left->next == right && left->height <= 0 )
 
2348
            return;
 
2349
 
 
2350
          /* leftmost stub test */
 
2351
          if ( right->next == left && left->start == y )
 
2352
            return;
 
2353
 
 
2354
          /* check that the rightmost pixel isn't set */
 
2355
 
 
2356
          e1 = TRUNC( e1 );
 
2357
 
 
2358
          bits = ras.bTarget + ( y >> 3 );
 
2359
          f1   = (Byte)( 0x80 >> ( y & 7 ) );
 
2360
 
 
2361
          bits -= e1 * ras.target.pitch;
 
2362
          if ( ras.target.pitch > 0 )
 
2363
            bits += ( ras.target.rows - 1 ) * ras.target.pitch;
 
2364
 
 
2365
          if ( e1 >= 0              &&
 
2366
               e1 < ras.target.rows &&
 
2367
               *bits & f1 )
 
2368
            return;
 
2369
 
 
2370
          if ( ras.dropOutControl == 2 )
 
2371
            e1 = e2;
 
2372
          else
 
2373
            e1 = CEILING( ( x1 + x2 + 1 ) / 2 );
 
2374
 
 
2375
          break;
 
2376
 
 
2377
        default:
 
2378
          return;  /* unsupported mode */
 
2379
        }
 
2380
      }
 
2381
      else
 
2382
        return;
 
2383
    }
 
2384
 
 
2385
    bits = ras.bTarget + ( y >> 3 );
 
2386
    f1   = (Byte)( 0x80 >> ( y & 7 ) );
 
2387
 
 
2388
    e1 = TRUNC( e1 );
 
2389
 
 
2390
    if ( e1 >= 0 && e1 < ras.target.rows )
 
2391
    {
 
2392
      bits -= e1 * ras.target.pitch;
 
2393
      if ( ras.target.pitch > 0 )
 
2394
        bits += ( ras.target.rows - 1 ) * ras.target.pitch;
 
2395
 
 
2396
      bits[0] |= f1;
 
2397
    }
 
2398
  }
 
2399
 
 
2400
 
 
2401
  static void
 
2402
  Horizontal_Sweep_Step( RAS_ARG )
 
2403
  {
 
2404
    /* Nothing, really */
 
2405
    FT_UNUSED( raster );
 
2406
  }
 
2407
 
 
2408
 
 
2409
#ifdef FT_RASTER_OPTION_ANTI_ALIASING
 
2410
 
 
2411
 
 
2412
  /*************************************************************************/
 
2413
  /*                                                                       */
 
2414
  /*  Vertical Gray Sweep Procedure Set                                    */
 
2415
  /*                                                                       */
 
2416
  /*  These two routines are used during the vertical gray-levels sweep    */
 
2417
  /*  phase by the generic Draw_Sweep() function.                          */
 
2418
  /*                                                                       */
 
2419
  /*  NOTES                                                                */
 
2420
  /*                                                                       */
 
2421
  /*  - The target pixmap's width *must* be a multiple of 4.               */
 
2422
  /*                                                                       */
 
2423
  /*  - You have to use the function Vertical_Sweep_Span() for the gray    */
 
2424
  /*    span call.                                                         */
 
2425
  /*                                                                       */
 
2426
  /*************************************************************************/
 
2427
 
 
2428
  static void
 
2429
  Vertical_Gray_Sweep_Init( RAS_ARGS Short*  min,
 
2430
                                     Short*  max )
 
2431
  {
 
2432
    Long  pitch, byte_len;
 
2433
 
 
2434
 
 
2435
    *min = *min & -2;
 
2436
    *max = ( *max + 3 ) & -2;
 
2437
 
 
2438
    ras.traceOfs  = 0;
 
2439
    pitch         = ras.target.pitch;
 
2440
    byte_len      = -pitch;
 
2441
    ras.traceIncr = (Short)byte_len;
 
2442
    ras.traceG    = ( *min / 2 ) * byte_len;
 
2443
 
 
2444
    if ( pitch > 0 )
 
2445
    {
 
2446
      ras.traceG += ( ras.target.rows - 1 ) * pitch;
 
2447
      byte_len    = -byte_len;
 
2448
    }
 
2449
 
 
2450
    ras.gray_min_x =  (Short)byte_len;
 
2451
    ras.gray_max_x = -(Short)byte_len;
 
2452
  }
 
2453
 
 
2454
 
 
2455
  static void
 
2456
  Vertical_Gray_Sweep_Step( RAS_ARG )
 
2457
  {
 
2458
    Int    c1, c2;
 
2459
    PByte  pix, bit, bit2;
 
2460
    Int*   count = ras.count_table;
 
2461
    Byte*  grays;
 
2462
 
 
2463
 
 
2464
    ras.traceOfs += ras.gray_width;
 
2465
 
 
2466
    if ( ras.traceOfs > ras.gray_width )
 
2467
    {
 
2468
      pix   = ras.gTarget + ras.traceG + ras.gray_min_x * 4;
 
2469
      grays = ras.grays;
 
2470
 
 
2471
      if ( ras.gray_max_x >= 0 )
 
2472
      {
 
2473
        Long   last_pixel = ras.target.width - 1;
 
2474
        Int    last_cell  = last_pixel >> 2;
 
2475
        Int    last_bit   = last_pixel & 3;
 
2476
        Bool   over       = 0;
 
2477
 
 
2478
 
 
2479
        if ( ras.gray_max_x >= last_cell && last_bit != 3 )
 
2480
        {
 
2481
          ras.gray_max_x = last_cell - 1;
 
2482
          over = 1;
 
2483
        }
 
2484
 
 
2485
        if ( ras.gray_min_x < 0 )
 
2486
          ras.gray_min_x = 0;
 
2487
 
 
2488
        bit   = ras.bTarget + ras.gray_min_x;
 
2489
        bit2  = bit + ras.gray_width;
 
2490
 
 
2491
        c1 = ras.gray_max_x - ras.gray_min_x;
 
2492
 
 
2493
        while ( c1 >= 0 )
 
2494
        {
 
2495
          c2 = count[*bit] + count[*bit2];
 
2496
 
 
2497
          if ( c2 )
 
2498
          {
 
2499
            pix[0] = grays[(c2 >> 12) & 0x000F];
 
2500
            pix[1] = grays[(c2 >> 8 ) & 0x000F];
 
2501
            pix[2] = grays[(c2 >> 4 ) & 0x000F];
 
2502
            pix[3] = grays[ c2        & 0x000F];
 
2503
 
 
2504
            *bit  = 0;
 
2505
            *bit2 = 0;
 
2506
          }
 
2507
 
 
2508
          bit++;
 
2509
          bit2++;
 
2510
          pix += 4;
 
2511
          c1--;
 
2512
        }
 
2513
 
 
2514
        if ( over )
 
2515
        {
 
2516
          c2 = count[*bit] + count[*bit2];
 
2517
          if ( c2 )
 
2518
          {
 
2519
            switch ( last_bit )
 
2520
            {
 
2521
            case 2:
 
2522
              pix[2] = grays[(c2 >> 4 ) & 0x000F];
 
2523
            case 1:
 
2524
              pix[1] = grays[(c2 >> 8 ) & 0x000F];
 
2525
            default:
 
2526
              pix[0] = grays[(c2 >> 12) & 0x000F];
 
2527
            }
 
2528
 
 
2529
            *bit  = 0;
 
2530
            *bit2 = 0;
 
2531
          }
 
2532
        }
 
2533
      }
 
2534
 
 
2535
      ras.traceOfs = 0;
 
2536
      ras.traceG  += ras.traceIncr;
 
2537
 
 
2538
      ras.gray_min_x =  32000;
 
2539
      ras.gray_max_x = -32000;
 
2540
    }
 
2541
  }
 
2542
 
 
2543
 
 
2544
  static void
 
2545
  Horizontal_Gray_Sweep_Span( RAS_ARGS Short       y,
 
2546
                                       FT_F26Dot6  x1,
 
2547
                                       FT_F26Dot6  x2,
 
2548
                                       PProfile    left,
 
2549
                                       PProfile    right )
 
2550
  {
 
2551
    /* nothing, really */
 
2552
    FT_UNUSED( raster );
 
2553
    FT_UNUSED( y );
 
2554
    FT_UNUSED( x1 );
 
2555
    FT_UNUSED( x2 );
 
2556
    FT_UNUSED( left );
 
2557
    FT_UNUSED( right );
 
2558
  }
 
2559
 
 
2560
 
 
2561
  static void
 
2562
  Horizontal_Gray_Sweep_Drop( RAS_ARGS Short       y,
 
2563
                                       FT_F26Dot6  x1,
 
2564
                                       FT_F26Dot6  x2,
 
2565
                                       PProfile    left,
 
2566
                                       PProfile    right )
 
2567
  {
 
2568
    Long   e1, e2;
 
2569
    PByte  pixel;
 
2570
    Byte   color;
 
2571
 
 
2572
 
 
2573
    /* During the horizontal sweep, we only take care of drop-outs */
 
2574
    e1 = CEILING( x1 );
 
2575
    e2 = FLOOR  ( x2 );
 
2576
 
 
2577
    if ( e1 > e2 )
 
2578
    {
 
2579
      if ( e1 == e2 + ras.precision )
 
2580
      {
 
2581
        switch ( ras.dropOutControl )
 
2582
        {
 
2583
        case 1:
 
2584
          e1 = e2;
 
2585
          break;
 
2586
 
 
2587
        case 4:
 
2588
          e1 = CEILING( ( x1 + x2 + 1 ) / 2 );
 
2589
          break;
 
2590
 
 
2591
        case 2:
 
2592
        case 5:
 
2593
 
 
2594
          /* Drop-out Control Rule #4 */
 
2595
 
 
2596
          /* The spec is not very clear regarding rule #4.  It      */
 
2597
          /* presents a method that is way too costly to implement  */
 
2598
          /* while the general idea seems to get rid of `stubs'.    */
 
2599
          /*                                                        */
 
2600
 
 
2601
          /* rightmost stub test */
 
2602
          if ( left->next == right && left->height <= 0 )
 
2603
            return;
 
2604
 
 
2605
          /* leftmost stub test */
 
2606
          if ( right->next == left && left->start == y )
 
2607
            return;
 
2608
 
 
2609
          if ( ras.dropOutControl == 2 )
 
2610
            e1 = e2;
 
2611
          else
 
2612
            e1 = CEILING( ( x1 + x2 + 1 ) / 2 );
 
2613
 
 
2614
          break;
 
2615
 
 
2616
        default:
 
2617
          return;  /* unsupported mode */
 
2618
        }
 
2619
      }
 
2620
      else
 
2621
        return;
 
2622
    }
 
2623
 
 
2624
    if ( e1 >= 0 )
 
2625
    {
 
2626
      if ( x2 - x1 >= ras.precision_half )
 
2627
        color = ras.grays[2];
 
2628
      else
 
2629
        color = ras.grays[1];
 
2630
 
 
2631
      e1 = TRUNC( e1 ) / 2;
 
2632
      if ( e1 < ras.target.rows )
 
2633
      {
 
2634
        pixel = ras.gTarget - e1 * ras.target.pitch + y / 2;
 
2635
        if ( ras.target.pitch > 0 )
 
2636
          pixel += ( ras.target.rows - 1 ) * ras.target.pitch;
 
2637
 
 
2638
        if ( pixel[0] == ras.grays[0] )
 
2639
          pixel[0] = color;
 
2640
      }
 
2641
    }
 
2642
  }
 
2643
 
 
2644
 
 
2645
#endif /* FT_RASTER_OPTION_ANTI_ALIASING */
 
2646
 
 
2647
 
 
2648
  /*************************************************************************/
 
2649
  /*                                                                       */
 
2650
  /*  Generic Sweep Drawing routine                                        */
 
2651
  /*                                                                       */
 
2652
  /*************************************************************************/
 
2653
 
 
2654
  static Bool
 
2655
  Draw_Sweep( RAS_ARG )
 
2656
  {
 
2657
    Short         y, y_change, y_height;
 
2658
 
 
2659
    PProfile      P, Q, P_Left, P_Right;
 
2660
 
 
2661
    Short         min_Y, max_Y, top, bottom, dropouts;
 
2662
 
 
2663
    Long          x1, x2, xs, e1, e2;
 
2664
 
 
2665
    TProfileList  waiting;
 
2666
    TProfileList  draw_left, draw_right;
 
2667
 
 
2668
 
 
2669
    /* Init empty linked lists */
 
2670
 
 
2671
    Init_Linked( &waiting );
 
2672
 
 
2673
    Init_Linked( &draw_left  );
 
2674
    Init_Linked( &draw_right );
 
2675
 
 
2676
    /* first, compute min and max Y */
 
2677
 
 
2678
    P     = ras.fProfile;
 
2679
    max_Y = (Short)TRUNC( ras.minY );
 
2680
    min_Y = (Short)TRUNC( ras.maxY );
 
2681
 
 
2682
    while ( P )
 
2683
    {
 
2684
      Q = P->link;
 
2685
 
 
2686
      bottom = (Short)P->start;
 
2687
      top    = (Short)( P->start + P->height - 1 );
 
2688
 
 
2689
      if ( min_Y > bottom ) min_Y = bottom;
 
2690
      if ( max_Y < top    ) max_Y = top;
 
2691
 
 
2692
      P->X = 0;
 
2693
      InsNew( &waiting, P );
 
2694
 
 
2695
      P = Q;
 
2696
    }
 
2697
 
 
2698
    /* Check the Y-turns */
 
2699
    if ( ras.numTurns == 0 )
 
2700
    {
 
2701
      ras.error = Raster_Err_Invalid;
 
2702
      return FAILURE;
 
2703
    }
 
2704
 
 
2705
    /* Now inits the sweep */
 
2706
 
 
2707
    ras.Proc_Sweep_Init( RAS_VARS &min_Y, &max_Y );
 
2708
 
 
2709
    /* Then compute the distance of each profile from min_Y */
 
2710
 
 
2711
    P = waiting;
 
2712
 
 
2713
    while ( P )
 
2714
    {
 
2715
      P->countL = (UShort)( P->start - min_Y );
 
2716
      P = P->link;
 
2717
    }
 
2718
 
 
2719
    /* Let's go */
 
2720
 
 
2721
    y        = min_Y;
 
2722
    y_height = 0;
 
2723
 
 
2724
    if ( ras.numTurns > 0 &&
 
2725
         ras.sizeBuff[-ras.numTurns] == min_Y )
 
2726
      ras.numTurns--;
 
2727
 
 
2728
    while ( ras.numTurns > 0 )
 
2729
    {
 
2730
      /* look in the waiting list for new activations */
 
2731
 
 
2732
      P = waiting;
 
2733
 
 
2734
      while ( P )
 
2735
      {
 
2736
        Q = P->link;
 
2737
        P->countL -= y_height;
 
2738
        if ( P->countL == 0 )
 
2739
        {
 
2740
          DelOld( &waiting, P );
 
2741
 
 
2742
          switch ( P->flow )
 
2743
          {
 
2744
          case Flow_Up:
 
2745
            InsNew( &draw_left,  P );
 
2746
            break;
 
2747
 
 
2748
          case Flow_Down:
 
2749
            InsNew( &draw_right, P );
 
2750
            break;
 
2751
          }
 
2752
        }
 
2753
 
 
2754
        P = Q;
 
2755
      }
 
2756
 
 
2757
      /* Sort the drawing lists */
 
2758
 
 
2759
      Sort( &draw_left );
 
2760
      Sort( &draw_right );
 
2761
 
 
2762
      y_change = (Short)ras.sizeBuff[-ras.numTurns--];
 
2763
      y_height = (Short)( y_change - y );
 
2764
 
 
2765
      while ( y < y_change )
 
2766
      {
 
2767
        /* Let's trace */
 
2768
 
 
2769
        dropouts = 0;
 
2770
 
 
2771
        P_Left  = draw_left;
 
2772
        P_Right = draw_right;
 
2773
 
 
2774
        while ( P_Left )
 
2775
        {
 
2776
          x1 = P_Left ->X;
 
2777
          x2 = P_Right->X;
 
2778
 
 
2779
          if ( x1 > x2 )
 
2780
          {
 
2781
            xs = x1;
 
2782
            x1 = x2;
 
2783
            x2 = xs;
 
2784
          }
 
2785
 
 
2786
          if ( x2 - x1 <= ras.precision )
 
2787
          {
 
2788
            e1 = FLOOR( x1 );
 
2789
            e2 = CEILING( x2 );
 
2790
 
 
2791
            if ( ras.dropOutControl != 0                 &&
 
2792
                 ( e1 > e2 || e2 == e1 + ras.precision ) )
 
2793
            {
 
2794
              /* a drop out was detected */
 
2795
 
 
2796
              P_Left ->X = x1;
 
2797
              P_Right->X = x2;
 
2798
 
 
2799
              /* mark profile for drop-out processing */
 
2800
              P_Left->countL = 1;
 
2801
              dropouts++;
 
2802
 
 
2803
              goto Skip_To_Next;
 
2804
            }
 
2805
          }
 
2806
 
 
2807
          ras.Proc_Sweep_Span( RAS_VARS y, x1, x2, P_Left, P_Right );
 
2808
 
 
2809
        Skip_To_Next:
 
2810
 
 
2811
          P_Left  = P_Left->link;
 
2812
          P_Right = P_Right->link;
 
2813
        }
 
2814
 
 
2815
        /* now perform the dropouts _after_ the span drawing -- */
 
2816
        /* drop-outs processing has been moved out of the loop  */
 
2817
        /* for performance tuning                               */
 
2818
        if ( dropouts > 0 )
 
2819
          goto Scan_DropOuts;
 
2820
 
 
2821
      Next_Line:
 
2822
 
 
2823
        ras.Proc_Sweep_Step( RAS_VAR );
 
2824
 
 
2825
        y++;
 
2826
 
 
2827
        if ( y < y_change )
 
2828
        {
 
2829
          Sort( &draw_left  );
 
2830
          Sort( &draw_right );
 
2831
        }
 
2832
      }
 
2833
 
 
2834
      /* Now finalize the profiles that needs it */
 
2835
 
 
2836
      P = draw_left;
 
2837
      while ( P )
 
2838
      {
 
2839
        Q = P->link;
 
2840
        if ( P->height == 0 )
 
2841
          DelOld( &draw_left, P );
 
2842
        P = Q;
 
2843
      }
 
2844
 
 
2845
      P = draw_right;
 
2846
      while ( P )
 
2847
      {
 
2848
        Q = P->link;
 
2849
        if ( P->height == 0 )
 
2850
          DelOld( &draw_right, P );
 
2851
        P = Q;
 
2852
      }
 
2853
    }
 
2854
 
 
2855
    /* for gray-scaling, flushes the bitmap scanline cache */
 
2856
    while ( y <= max_Y )
 
2857
    {
 
2858
      ras.Proc_Sweep_Step( RAS_VAR );
 
2859
      y++;
 
2860
    }
 
2861
 
 
2862
    return SUCCESS;
 
2863
 
 
2864
  Scan_DropOuts:
 
2865
 
 
2866
    P_Left  = draw_left;
 
2867
    P_Right = draw_right;
 
2868
 
 
2869
    while ( P_Left )
 
2870
    {
 
2871
      if ( P_Left->countL )
 
2872
      {
 
2873
        P_Left->countL = 0;
 
2874
#if 0
 
2875
        dropouts--;  /* -- this is useful when debugging only */
 
2876
#endif
 
2877
        ras.Proc_Sweep_Drop( RAS_VARS y,
 
2878
                                      P_Left->X,
 
2879
                                      P_Right->X,
 
2880
                                      P_Left,
 
2881
                                      P_Right );
 
2882
      }
 
2883
 
 
2884
      P_Left  = P_Left->link;
 
2885
      P_Right = P_Right->link;
 
2886
    }
 
2887
 
 
2888
    goto Next_Line;
 
2889
  }
 
2890
 
 
2891
 
 
2892
  /*************************************************************************/
 
2893
  /*                                                                       */
 
2894
  /* <Function>                                                            */
 
2895
  /*    Render_Single_Pass                                                 */
 
2896
  /*                                                                       */
 
2897
  /* <Description>                                                         */
 
2898
  /*    Performs one sweep with sub-banding.                               */
 
2899
  /*                                                                       */
 
2900
  /* <Input>                                                               */
 
2901
  /*    flipped :: If set, flip the direction of the outline.              */
 
2902
  /*                                                                       */
 
2903
  /* <Return>                                                              */
 
2904
  /*    Renderer error code.                                               */
 
2905
  /*                                                                       */
 
2906
  static int
 
2907
  Render_Single_Pass( RAS_ARGS Bool  flipped )
 
2908
  {
 
2909
    Short  i, j, k;
 
2910
 
 
2911
 
 
2912
    while ( ras.band_top >= 0 )
 
2913
    {
 
2914
      ras.maxY = (Long)ras.band_stack[ras.band_top].y_max * ras.precision;
 
2915
      ras.minY = (Long)ras.band_stack[ras.band_top].y_min * ras.precision;
 
2916
 
 
2917
      ras.top = ras.buff;
 
2918
 
 
2919
      ras.error = Raster_Err_None;
 
2920
 
 
2921
      if ( Convert_Glyph( RAS_VARS flipped ) )
 
2922
      {
 
2923
        if ( ras.error != Raster_Err_Overflow )
 
2924
          return FAILURE;
 
2925
 
 
2926
        ras.error = Raster_Err_None;
 
2927
 
 
2928
        /* sub-banding */
 
2929
 
 
2930
#ifdef DEBUG_RASTER
 
2931
        ClearBand( RAS_VARS TRUNC( ras.minY ), TRUNC( ras.maxY ) );
 
2932
#endif
 
2933
 
 
2934
        i = ras.band_stack[ras.band_top].y_min;
 
2935
        j = ras.band_stack[ras.band_top].y_max;
 
2936
 
 
2937
        k = (Short)( ( i + j ) / 2 );
 
2938
 
 
2939
        if ( ras.band_top >= 7 || k < i )
 
2940
        {
 
2941
          ras.band_top = 0;
 
2942
          ras.error    = Raster_Err_Invalid;
 
2943
 
 
2944
          return ras.error;
 
2945
        }
 
2946
 
 
2947
        ras.band_stack[ras.band_top + 1].y_min = k;
 
2948
        ras.band_stack[ras.band_top + 1].y_max = j;
 
2949
 
 
2950
        ras.band_stack[ras.band_top].y_max = (Short)( k - 1 );
 
2951
 
 
2952
        ras.band_top++;
 
2953
      }
 
2954
      else
 
2955
      {
 
2956
        if ( ras.fProfile )
 
2957
          if ( Draw_Sweep( RAS_VAR ) )
 
2958
             return ras.error;
 
2959
        ras.band_top--;
 
2960
      }
 
2961
    }
 
2962
 
 
2963
    return SUCCESS;
 
2964
  }
 
2965
 
 
2966
 
 
2967
  /*************************************************************************/
 
2968
  /*                                                                       */
 
2969
  /* <Function>                                                            */
 
2970
  /*    Render_Glyph                                                       */
 
2971
  /*                                                                       */
 
2972
  /* <Description>                                                         */
 
2973
  /*    Renders a glyph in a bitmap.  Sub-banding if needed.               */
 
2974
  /*                                                                       */
 
2975
  /* <Return>                                                              */
 
2976
  /*    FreeType error code.  0 means success.                             */
 
2977
  /*                                                                       */
 
2978
  FT_LOCAL_DEF( FT_Error )
 
2979
  Render_Glyph( RAS_ARG )
 
2980
  {
 
2981
    FT_Error  error;
 
2982
 
 
2983
 
 
2984
    Set_High_Precision( RAS_VARS ras.outline.flags &
 
2985
                        FT_OUTLINE_HIGH_PRECISION );
 
2986
    ras.scale_shift    = ras.precision_shift;
 
2987
    ras.dropOutControl = 2;
 
2988
    ras.second_pass    = (FT_Byte)( !( ras.outline.flags &
 
2989
                                       FT_OUTLINE_SINGLE_PASS ) );
 
2990
 
 
2991
    /* Vertical Sweep */
 
2992
    ras.Proc_Sweep_Init = Vertical_Sweep_Init;
 
2993
    ras.Proc_Sweep_Span = Vertical_Sweep_Span;
 
2994
    ras.Proc_Sweep_Drop = Vertical_Sweep_Drop;
 
2995
    ras.Proc_Sweep_Step = Vertical_Sweep_Step;
 
2996
 
 
2997
    ras.band_top            = 0;
 
2998
    ras.band_stack[0].y_min = 0;
 
2999
    ras.band_stack[0].y_max = (short)( ras.target.rows - 1 );
 
3000
 
 
3001
    ras.bWidth  = (unsigned short)ras.target.width;
 
3002
    ras.bTarget = (Byte*)ras.target.buffer;
 
3003
 
 
3004
    if ( ( error = Render_Single_Pass( RAS_VARS 0 ) ) != 0 )
 
3005
      return error;
 
3006
 
 
3007
    /* Horizontal Sweep */
 
3008
    if ( ras.second_pass && ras.dropOutControl != 0 )
 
3009
    {
 
3010
      ras.Proc_Sweep_Init = Horizontal_Sweep_Init;
 
3011
      ras.Proc_Sweep_Span = Horizontal_Sweep_Span;
 
3012
      ras.Proc_Sweep_Drop = Horizontal_Sweep_Drop;
 
3013
      ras.Proc_Sweep_Step = Horizontal_Sweep_Step;
 
3014
 
 
3015
      ras.band_top            = 0;
 
3016
      ras.band_stack[0].y_min = 0;
 
3017
      ras.band_stack[0].y_max = (short)( ras.target.width - 1 );
 
3018
 
 
3019
      if ( ( error = Render_Single_Pass( RAS_VARS 1 ) ) != 0 )
 
3020
        return error;
 
3021
    }
 
3022
 
 
3023
    return Raster_Err_Ok;
 
3024
  }
 
3025
 
 
3026
 
 
3027
#ifdef FT_RASTER_OPTION_ANTI_ALIASING
 
3028
 
 
3029
 
 
3030
  /*************************************************************************/
 
3031
  /*                                                                       */
 
3032
  /* <Function>                                                            */
 
3033
  /*    Render_Gray_Glyph                                                  */
 
3034
  /*                                                                       */
 
3035
  /* <Description>                                                         */
 
3036
  /*    Renders a glyph with grayscaling.  Sub-banding if needed.          */
 
3037
  /*                                                                       */
 
3038
  /* <Return>                                                              */
 
3039
  /*    FreeType error code.  0 means success.                             */
 
3040
  /*                                                                       */
 
3041
  FT_LOCAL_DEF( FT_Error )
 
3042
  Render_Gray_Glyph( RAS_ARG )
 
3043
  {
 
3044
    Long      pixel_width;
 
3045
    FT_Error  error;
 
3046
 
 
3047
 
 
3048
    Set_High_Precision( RAS_VARS ras.outline.flags &
 
3049
                        FT_OUTLINE_HIGH_PRECISION );
 
3050
    ras.scale_shift    = ras.precision_shift + 1;
 
3051
    ras.dropOutControl = 2;
 
3052
    ras.second_pass    = !( ras.outline.flags & FT_OUTLINE_SINGLE_PASS );
 
3053
 
 
3054
    /* Vertical Sweep */
 
3055
 
 
3056
    ras.band_top            = 0;
 
3057
    ras.band_stack[0].y_min = 0;
 
3058
    ras.band_stack[0].y_max = 2 * ras.target.rows - 1;
 
3059
 
 
3060
    ras.bWidth  = ras.gray_width;
 
3061
    pixel_width = 2 * ( ( ras.target.width + 3 ) >> 2 );
 
3062
 
 
3063
    if ( ras.bWidth > pixel_width )
 
3064
      ras.bWidth = pixel_width;
 
3065
 
 
3066
    ras.bWidth  = ras.bWidth * 8;
 
3067
    ras.bTarget = (Byte*)ras.gray_lines;
 
3068
    ras.gTarget = (Byte*)ras.target.buffer;
 
3069
 
 
3070
    ras.Proc_Sweep_Init = Vertical_Gray_Sweep_Init;
 
3071
    ras.Proc_Sweep_Span = Vertical_Sweep_Span;
 
3072
    ras.Proc_Sweep_Drop = Vertical_Sweep_Drop;
 
3073
    ras.Proc_Sweep_Step = Vertical_Gray_Sweep_Step;
 
3074
 
 
3075
    error = Render_Single_Pass( RAS_VARS 0 );
 
3076
    if ( error )
 
3077
      return error;
 
3078
 
 
3079
    /* Horizontal Sweep */
 
3080
    if ( ras.second_pass && ras.dropOutControl != 0 )
 
3081
    {
 
3082
      ras.Proc_Sweep_Init = Horizontal_Sweep_Init;
 
3083
      ras.Proc_Sweep_Span = Horizontal_Gray_Sweep_Span;
 
3084
      ras.Proc_Sweep_Drop = Horizontal_Gray_Sweep_Drop;
 
3085
      ras.Proc_Sweep_Step = Horizontal_Sweep_Step;
 
3086
 
 
3087
      ras.band_top            = 0;
 
3088
      ras.band_stack[0].y_min = 0;
 
3089
      ras.band_stack[0].y_max = ras.target.width * 2 - 1;
 
3090
 
 
3091
      error = Render_Single_Pass( RAS_VARS 1 );
 
3092
      if ( error )
 
3093
        return error;
 
3094
    }
 
3095
 
 
3096
    return Raster_Err_Ok;
 
3097
  }
 
3098
 
 
3099
#else /* !FT_RASTER_OPTION_ANTI_ALIASING */
 
3100
 
 
3101
  FT_LOCAL_DEF( FT_Error )
 
3102
  Render_Gray_Glyph( RAS_ARG )
 
3103
  {
 
3104
    FT_UNUSED_RASTER;
 
3105
 
 
3106
    return Raster_Err_Cannot_Render_Glyph;
 
3107
  }
 
3108
 
 
3109
#endif /* !FT_RASTER_OPTION_ANTI_ALIASING */
 
3110
 
 
3111
 
 
3112
  static void
 
3113
  ft_black_init( TRaster_Instance*  raster )
 
3114
  {
 
3115
    FT_UInt  n;
 
3116
    FT_ULong c;
 
3117
 
 
3118
 
 
3119
    /* setup count table */
 
3120
    for ( n = 0; n < 256; n++ )
 
3121
    {
 
3122
      c = ( n & 0x55 ) + ( ( n & 0xAA ) >> 1 );
 
3123
 
 
3124
      c = ( ( c << 6 ) & 0x3000 ) |
 
3125
          ( ( c << 4 ) & 0x0300 ) |
 
3126
          ( ( c << 2 ) & 0x0030 ) |
 
3127
                   (c  & 0x0003 );
 
3128
 
 
3129
      raster->count_table[n] = (UInt)c;
 
3130
    }
 
3131
 
 
3132
#ifdef FT_RASTER_OPTION_ANTI_ALIASING
 
3133
 
 
3134
    /* set default 5-levels gray palette */
 
3135
    for ( n = 0; n < 5; n++ )
 
3136
      raster->grays[n] = n * 255 / 4;
 
3137
 
 
3138
    raster->gray_width = RASTER_GRAY_LINES / 2;
 
3139
 
 
3140
#endif
 
3141
  }
 
3142
 
 
3143
 
 
3144
  /**** RASTER OBJECT CREATION: In standalone mode, we simply use *****/
 
3145
  /****                         a static object.                  *****/
 
3146
 
 
3147
 
 
3148
#ifdef _STANDALONE_
 
3149
 
 
3150
 
 
3151
  static int
 
3152
  ft_black_new( void*      memory,
 
3153
                FT_Raster  *araster )
 
3154
  {
 
3155
     static FT_RasterRec_  the_raster;
 
3156
 
 
3157
 
 
3158
     *araster = &the_raster;
 
3159
     FT_MEM_ZERO( &the_raster, sizeof ( the_raster ) );
 
3160
     ft_black_init( &the_raster );
 
3161
 
 
3162
     return 0;
 
3163
  }
 
3164
 
 
3165
 
 
3166
  static void
 
3167
  ft_black_done( FT_Raster  raster )
 
3168
  {
 
3169
    /* nothing */
 
3170
    raster->init = 0;
 
3171
  }
 
3172
 
 
3173
 
 
3174
#else /* _STANDALONE_ */
 
3175
 
 
3176
 
 
3177
  static int
 
3178
  ft_black_new( FT_Memory           memory,
 
3179
                TRaster_Instance**  araster )
 
3180
  {
 
3181
    FT_Error           error;
 
3182
    TRaster_Instance*  raster;
 
3183
 
 
3184
 
 
3185
    *araster = 0;
 
3186
    if ( !FT_NEW( raster ) )
 
3187
    {
 
3188
      raster->memory = memory;
 
3189
      ft_black_init( raster );
 
3190
 
 
3191
      *araster = raster;
 
3192
    }
 
3193
 
 
3194
    return error;
 
3195
  }
 
3196
 
 
3197
 
 
3198
  static void
 
3199
  ft_black_done( TRaster_Instance*  raster )
 
3200
  {
 
3201
    FT_Memory  memory = (FT_Memory)raster->memory;
 
3202
    FT_FREE( raster );
 
3203
  }
 
3204
 
 
3205
 
 
3206
#endif /* _STANDALONE_ */
 
3207
 
 
3208
 
 
3209
  static void
 
3210
  ft_black_reset( TRaster_Instance*  raster,
 
3211
                  const char*        pool_base,
 
3212
                  long               pool_size )
 
3213
  {
 
3214
    if ( raster && pool_base && pool_size >= 4096 )
 
3215
    {
 
3216
      /* save the pool */
 
3217
      raster->buff     = (PLong)pool_base;
 
3218
      raster->sizeBuff = raster->buff + pool_size / sizeof ( Long );
 
3219
    }
 
3220
  }
 
3221
 
 
3222
 
 
3223
  static void
 
3224
  ft_black_set_mode( TRaster_Instance*  raster,
 
3225
                     unsigned long      mode,
 
3226
                     const char*        palette )
 
3227
  {
 
3228
#ifdef FT_RASTER_OPTION_ANTI_ALIASING
 
3229
 
 
3230
    if ( mode == FT_MAKE_TAG( 'p', 'a', 'l', '5' ) )
 
3231
    {
 
3232
      /* set 5-levels gray palette */
 
3233
      raster->grays[0] = palette[0];
 
3234
      raster->grays[1] = palette[1];
 
3235
      raster->grays[2] = palette[2];
 
3236
      raster->grays[3] = palette[3];
 
3237
      raster->grays[4] = palette[4];
 
3238
    }
 
3239
 
 
3240
#else
 
3241
 
 
3242
    FT_UNUSED( raster );
 
3243
    FT_UNUSED( mode );
 
3244
    FT_UNUSED( palette );
 
3245
 
 
3246
#endif
 
3247
  }
 
3248
 
 
3249
 
 
3250
  static int
 
3251
  ft_black_render( TRaster_Instance*  raster,
 
3252
                   FT_Raster_Params*  params )
 
3253
  {
 
3254
    FT_Outline*  outline    = (FT_Outline*)params->source;
 
3255
    FT_Bitmap*   target_map = params->target;
 
3256
 
 
3257
 
 
3258
    if ( !raster || !raster->buff || !raster->sizeBuff )
 
3259
      return Raster_Err_Not_Ini;
 
3260
 
 
3261
    /* return immediately if the outline is empty */
 
3262
    if ( outline->n_points == 0 || outline->n_contours <= 0 )
 
3263
      return Raster_Err_None;
 
3264
 
 
3265
    if ( !outline || !outline->contours || !outline->points )
 
3266
      return Raster_Err_Invalid;
 
3267
 
 
3268
    if ( outline->n_points != outline->contours[outline->n_contours - 1] + 1 )
 
3269
      return Raster_Err_Invalid;
 
3270
 
 
3271
    /* this version of the raster does not support direct rendering, sorry */
 
3272
    if ( params->flags & FT_RASTER_FLAG_DIRECT )
 
3273
      return Raster_Err_Unsupported;
 
3274
 
 
3275
    if ( !target_map || !target_map->buffer )
 
3276
      return Raster_Err_Invalid;
 
3277
 
 
3278
    ras.outline  = *outline;
 
3279
    ras.target   = *target_map;
 
3280
 
 
3281
    return ( ( params->flags & FT_RASTER_FLAG_AA )
 
3282
               ? Render_Gray_Glyph( raster )
 
3283
               : Render_Glyph( raster ) );
 
3284
  }
 
3285
 
 
3286
 
 
3287
  const FT_Raster_Funcs  ft_standard_raster =
 
3288
  {
 
3289
    FT_GLYPH_FORMAT_OUTLINE,
 
3290
    (FT_Raster_New_Func)     ft_black_new,
 
3291
    (FT_Raster_Reset_Func)   ft_black_reset,
 
3292
    (FT_Raster_Set_Mode_Func)ft_black_set_mode,
 
3293
    (FT_Raster_Render_Func)  ft_black_render,
 
3294
    (FT_Raster_Done_Func)    ft_black_done
 
3295
  };
 
3296
 
 
3297
 
 
3298
/* END */