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

« back to all changes in this revision

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