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

« back to all changes in this revision

Viewing changes to src/gui/painting/qblackraster.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
/*  qblackraster.c, derived from 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, ../../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 is a rewrite of the FreeType 1.x scan-line converter             */
 
21
/*                                                                       */
 
22
/*************************************************************************/
 
23
 
 
24
/* #define Q_RASTER_DEBUG */
 
25
 
 
26
#include <qglobal.h>
 
27
 
 
28
typedef long QT_FT_F26Dot6;
 
29
typedef int QT_FT_Error;
 
30
typedef int QT_FT_Int;
 
31
typedef unsigned int QT_FT_UInt;
 
32
 
 
33
#if defined(Q_WS_WIN64)
 
34
typedef __int64          QT_FT_Long;
 
35
typedef unsigned __int64 QT_FT_ULong;
 
36
#else
 
37
typedef long             QT_FT_Long;
 
38
typedef unsigned long    QT_FT_ULong;
 
39
#endif
 
40
 
 
41
#define QT_FT_Int64  qint64
 
42
#define QT_FT_Byte uchar
 
43
 
 
44
#define QT_FT_TRACE1 if (0) printf
 
45
#define QT_FT_TRACE6 if (0) printf
 
46
 
 
47
#define Raster_Err_Ok 0
 
48
#define Raster_Err_Cannot_Render_Glyph 1
 
49
 
 
50
#define QT_FT_LOCAL_DEF(x) static x
 
51
 
 
52
#define QT_FT_BEGIN_HEADER
 
53
#define QT_FT_END_HEADER
 
54
#include <private/qrasterdefs_p.h>
 
55
#include <private/qblackraster_p.h>
 
56
 
 
57
static QT_FT_Long QT_FT_MulDiv(QT_FT_Long  a, QT_FT_Long  b, QT_FT_Long  c)
 
58
{
 
59
    QT_FT_Int   s;
 
60
    QT_FT_Long  d;
 
61
 
 
62
    s = 1;
 
63
    if ( a < 0 ) { a = -a; s = -1; }
 
64
    if ( b < 0 ) { b = -b; s = -s; }
 
65
    if ( c < 0 ) { c = -c; s = -s; }
 
66
 
 
67
    d = (QT_FT_Long)( c > 0 ? ( (QT_FT_Int64)a * b + ( c >> 1 ) ) / c
 
68
                   : 0x7FFFFFFFL );
 
69
 
 
70
    return ( s > 0 ) ? d : -d;
 
71
}
 
72
 
 
73
#include <string.h>
 
74
#define QT_FT_MEM_ZERO(x, len) memset(x, 0, len);
 
75
#include <stdio.h>
 
76
 
 
77
 
 
78
#define MAX(x, y) (x > y ? x : y)
 
79
#define MIN(x, y) (x < y ? x : y)
 
80
#define MAX_SPANS 32
 
81
 
 
82
/*************************************************************************/
 
83
/*                                                                       */
 
84
/* A simple technical note on how the raster works                       */
 
85
/* -----------------------------------------------                       */
 
86
/*                                                                       */
 
87
/*   Converting an outline into a bitmap is achieved in several steps:   */
 
88
/*                                                                       */
 
89
/*   1 - Decomposing the outline into successive `profiles'.  Each       */
 
90
/*       profile is simply an array of scanline intersections on a given */
 
91
/*       dimension.  A profile's main attributes are                     */
 
92
/*                                                                       */
 
93
/*       o its scanline position boundaries, i.e. `Ymin' and `Ymax'.     */
 
94
/*                                                                       */
 
95
/*       o an array of intersection coordinates for each scanline        */
 
96
/*         between `Ymin' and `Ymax'.                                    */
 
97
/*                                                                       */
 
98
/*       o a direction, indicating whether it was built going `up' or    */
 
99
/*         `down', as this is very important for filling rules.          */
 
100
/*                                                                       */
 
101
/*   2 - Sweeping the target map's scanlines in order to compute segment */
 
102
/*       `spans' which are then filled.  Additionally, this pass         */
 
103
/*       performs drop-out control.                                      */
 
104
/*                                                                       */
 
105
/*   The outline data is parsed during step 1 only.  The profiles are    */
 
106
/*   built from the bottom of the render pool, used as a stack.  The     */
 
107
/*   following graphics shows the profile list under construction:       */
 
108
/*                                                                       */
 
109
/*     ____________________________________________________________ _ _  */
 
110
/*    |         |                   |         |                 |        */
 
111
/*    | profile | coordinates for   | profile | coordinates for |-->     */
 
112
/*    |    1    |  profile 1        |    2    |  profile 2      |-->     */
 
113
/*    |_________|___________________|_________|_________________|__ _ _  */
 
114
/*                                                                       */
 
115
/*    ^                                                         ^        */
 
116
/*    |                                                         |        */
 
117
/*  start of render pool                                       top       */
 
118
/*                                                                       */
 
119
/*   The top of the profile stack is kept in the `top' variable.         */
 
120
/*                                                                       */
 
121
/*   As you can see, a profile record is pushed on top of the render     */
 
122
/*   pool, which is then followed by its coordinates/intersections.  If  */
 
123
/*   a change of direction is detected in the outline, a new profile is  */
 
124
/*   generated until the end of the outline.                             */
 
125
/*                                                                       */
 
126
/*   Note that when all profiles have been generated, the function       */
 
127
/*   Finalize_Profile_Table() is used to record, for each profile, its   */
 
128
/*   bottom-most scanline as well as the scanline above its upmost       */
 
129
/*   boundary.  These positions are called `y-turns' because they (sort  */
 
130
/*   of) correspond to local extrema.  They are stored in a sorted list  */
 
131
/*   built from the top of the render pool as a downwards stack:         */
 
132
/*                                                                       */
 
133
/*      _ _ _______________________________________                      */
 
134
/*                            |                    |                     */
 
135
/*                         <--| sorted list of     |                     */
 
136
/*                         <--|  extrema scanlines |                     */
 
137
/*      _ _ __________________|____________________|                     */
 
138
/*                                                                       */
 
139
/*                            ^                    ^                     */
 
140
/*                            |                    |                     */
 
141
/*                         maxBuff           sizeBuff = end of pool      */
 
142
/*                                                                       */
 
143
/*   This list is later used during the sweep phase in order to          */
 
144
/*   optimize performance (see technical note on the sweep below).       */
 
145
/*                                                                       */
 
146
/*   Of course, the raster detects whether the two stacks collide and    */
 
147
/*   handles the situation propertly.                                    */
 
148
/*                                                                       */
 
149
/*************************************************************************/
 
150
 
 
151
 
 
152
/*************************************************************************/
 
153
/*************************************************************************/
 
154
/**                                                                     **/
 
155
/**  CONFIGURATION MACROS                                               **/
 
156
/**                                                                     **/
 
157
/*************************************************************************/
 
158
/*************************************************************************/
 
159
 
 
160
/* define DEBUG_RASTER if you want to compile a debugging version */
 
161
#define xxxDEBUG_RASTER
 
162
 
 
163
/* The default render pool size in bytes */
 
164
#define RASTER_RENDER_POOL  8192
 
165
 
 
166
 
 
167
/* The size of the two-lines intermediate bitmap used */
 
168
/* for anti-aliasing, in bytes.                       */
 
169
#define RASTER_GRAY_LINES  2048
 
170
 
 
171
 
 
172
/*************************************************************************/
 
173
/*************************************************************************/
 
174
/**                                                                     **/
 
175
/**  OTHER MACROS (do not change)                                       **/
 
176
/**                                                                     **/
 
177
/*************************************************************************/
 
178
/*************************************************************************/
 
179
 
 
180
/*************************************************************************/
 
181
/*                                                                       */
 
182
/* The macro QT_FT_COMPONENT is used in trace mode.  It is an implicit      */
 
183
/* parameter of the QT_FT_TRACE() and QT_FT_ERROR() macros, used to print/log  */
 
184
/* messages during execution.                                            */
 
185
/*                                                                       */
 
186
#undef  QT_FT_COMPONENT
 
187
#define QT_FT_COMPONENT  trace_raster
 
188
 
 
189
 
 
190
/* This macro is used to indicate that a function parameter is unused. */
 
191
/* Its purpose is simply to reduce compiler warnings.  Note also that  */
 
192
/* simply defining it as `(void)x' doesn't avoid warnings with certain */
 
193
/* ANSI compilers (e.g. LCC).                                          */
 
194
#define QT_FT_UNUSED( x )  (x) = (x)
 
195
 
 
196
/* Disable the tracing mechanism for simplicity -- developers can      */
 
197
/* activate it easily by redefining these two macros.                  */
 
198
#ifndef QT_FT_ERROR
 
199
#define QT_FT_ERROR( x )  do ; while ( 0 )     /* nothing */
 
200
#endif
 
201
 
 
202
#ifndef QT_FT_TRACE
 
203
#define QT_FT_TRACE( x )  do ; while ( 0 )     /* nothing */
 
204
#endif
 
205
 
 
206
#define Raster_Err_None          0
 
207
#define Raster_Err_Not_Ini      -1
 
208
#define Raster_Err_Overflow     -2
 
209
#define Raster_Err_Neg_Height   -3
 
210
#define Raster_Err_Invalid      -4
 
211
#define Raster_Err_Unsupported  -5
 
212
 
 
213
 
 
214
 
 
215
#ifndef QT_FT_MEM_SET
 
216
#define QT_FT_MEM_SET( d, s, c )  qt_ft_memset( d, s, c )
 
217
#endif
 
218
 
 
219
 
 
220
/* FMulDiv means `Fast MulDiv'; it is used in case where `b' is       */
 
221
/* typically a small value and the result of a*b is known to fit into */
 
222
/* 32 bits.                                                           */
 
223
#define FMulDiv( a, b, c )  ( (a) * (b) / (c) )
 
224
 
 
225
/* On the other hand, SMulDiv means `Slow MulDiv', and is used typically */
 
226
/* for clipping computations.  It simply uses the QT_FT_MulDiv() function   */
 
227
/* defined in `ftcalc.h'.                                                */
 
228
#define SMulDiv  QT_FT_MulDiv
 
229
 
 
230
/* The rasterizer is a very general purpose component; please leave */
 
231
/* the following redefinitions there (you never know your target    */
 
232
/* environment).                                                    */
 
233
 
 
234
#ifndef TRUE
 
235
#define TRUE   1
 
236
#endif
 
237
 
 
238
#ifndef FALSE
 
239
#define FALSE  0
 
240
#endif
 
241
 
 
242
#ifndef NULL
 
243
#define NULL  (void*)0
 
244
#endif
 
245
 
 
246
#ifndef SUCCESS
 
247
#define SUCCESS  0
 
248
#endif
 
249
 
 
250
#ifndef FAILURE
 
251
#define FAILURE  1
 
252
#endif
 
253
 
 
254
 
 
255
#define MaxBezier  32   /* The maximum number of stacked Bezier curves. */
 
256
                        /* Setting this constant to more than 32 is a   */
 
257
                        /* pure waste of space.                         */
 
258
 
 
259
#define Pixel_Bits  6   /* fractional bits of *input* coordinates */
 
260
 
 
261
 
 
262
/*************************************************************************/
 
263
/*************************************************************************/
 
264
/**                                                                     **/
 
265
/**  SIMPLE TYPE DECLARATIONS                                           **/
 
266
/**                                                                     **/
 
267
/*************************************************************************/
 
268
/*************************************************************************/
 
269
 
 
270
typedef int             Int;
 
271
typedef unsigned int    UInt;
 
272
typedef short           Short;
 
273
typedef unsigned short  UShort, *PUShort;
 
274
 
 
275
#if defined(Q_WS_WIN64)
 
276
typedef __int64          Long, *PLong;
 
277
typedef unsigned __int64 ULong;
 
278
#else
 
279
typedef long             Long, *PLong;
 
280
typedef unsigned long    ULong;
 
281
#endif
 
282
 
 
283
typedef unsigned char   Byte, *PByte;
 
284
typedef char            Bool;
 
285
 
 
286
 
 
287
typedef union  Alignment_
 
288
{
 
289
    long    l;
 
290
    void*   p;
 
291
    void  (*f)(void);
 
292
 
 
293
} Alignment, *PAlignment;
 
294
 
 
295
 
 
296
typedef struct  TPoint_
 
297
{
 
298
    Long  x;
 
299
    Long  y;
 
300
 
 
301
} TPoint;
 
302
 
 
303
 
 
304
typedef enum  TFlow_
 
305
{
 
306
    Flow_None = 0,
 
307
    Flow_Up   = 1,
 
308
    Flow_Down = -1
 
309
 
 
310
} TFlow;
 
311
 
 
312
 
 
313
/* States of each line, arc, and profile */
 
314
typedef enum  TStates_
 
315
{
 
316
    Unknown_State,
 
317
    Ascending_State,
 
318
    Descending_State,
 
319
    Flat_State
 
320
 
 
321
} TStates;
 
322
 
 
323
 
 
324
typedef struct TProfile_  TProfile;
 
325
typedef TProfile*         PProfile;
 
326
 
 
327
struct  TProfile_
 
328
{
 
329
    QT_FT_F26Dot6  X;           /* current coordinate during sweep        */
 
330
    PProfile    link;        /* link to next profile - various purpose */
 
331
    PLong       offset;      /* start of profile's data in render pool */
 
332
    int         flow;        /* Profile orientation: Asc/Descending    */
 
333
    long        height;      /* profile's height in scanlines          */
 
334
    long        start;       /* profile's starting scanline            */
 
335
 
 
336
    unsigned    countL;      /* number of lines to step before this    */
 
337
                             /* profile becomes drawable               */
 
338
};
 
339
 
 
340
typedef PProfile   TProfileList;
 
341
typedef PProfile*  PProfileList;
 
342
 
 
343
 
 
344
/* Simple record used to implement a stack of bands, required */
 
345
/* by the sub-banding mechanism                               */
 
346
typedef struct  TBand_
 
347
{
 
348
    Short  y_min;   /* band's minimum */
 
349
    Short  y_max;   /* band's maximum */
 
350
 
 
351
} TBand;
 
352
 
 
353
 
 
354
#define AlignProfileSize                                                \
 
355
    ( ( sizeof ( TProfile ) + ( sizeof ( TProfile ) % sizeof ( Alignment ) ) ) / sizeof ( Long ) )
 
356
 
 
357
 
 
358
#ifdef TT_STATIC_RASTER
 
359
 
 
360
 
 
361
#define RAS_ARGS       /* void */
 
362
#define RAS_ARG        /* void */
 
363
 
 
364
#define RAS_VARS       /* void */
 
365
#define RAS_VAR        /* void */
 
366
 
 
367
#define QT_FT_UNUSED_RASTER  do ; while ( 0 )
 
368
 
 
369
 
 
370
#else /* TT_STATIC_RASTER */
 
371
 
 
372
 
 
373
#define RAS_ARGS       TRaster_Instance*  raster,
 
374
#define RAS_ARG        TRaster_Instance*  raster
 
375
 
 
376
#define RAS_VARS       raster,
 
377
#define RAS_VAR        raster
 
378
 
 
379
#define QT_FT_UNUSED_RASTER  QT_FT_UNUSED( raster )
 
380
 
 
381
 
 
382
#endif /* TT_STATIC_RASTER */
 
383
 
 
384
 
 
385
typedef struct TRaster_Instance_  TRaster_Instance;
 
386
 
 
387
 
 
388
/* prototypes used for sweep function dispatch */
 
389
typedef void
 
390
Function_Sweep_Init( RAS_ARGS Short*  min,
 
391
                     Short*  max );
 
392
 
 
393
typedef void
 
394
Function_Sweep_Span( RAS_ARGS Short       y,
 
395
                     QT_FT_F26Dot6  x1,
 
396
                     QT_FT_F26Dot6  x2,
 
397
                     PProfile    left,
 
398
                     PProfile    right );
 
399
 
 
400
typedef void
 
401
Function_Sweep_Step( RAS_ARG );
 
402
 
 
403
 
 
404
/* NOTE: These operations are only valid on 2's complement processors */
 
405
 
 
406
#define FLOOR( x )    ( (x) & -ras.precision )
 
407
#define CEILING( x )  ( ( (x) + ras.precision - 1 ) & -ras.precision )
 
408
#define TRUNC( x )    ( (signed long)(x) >> ras.precision_bits )
 
409
#define FRAC( x )     ( (x) & ( ras.precision - 1 ) )
 
410
#define SCALED( x )   ( ( (x) << ras.scale_shift ) )
 
411
 
 
412
/* Note that I have moved the location of some fields in the */
 
413
/* structure to ensure that the most used variables are used */
 
414
/* at the top.  Thus, their offset can be coded with less    */
 
415
/* opcodes, and it results in a smaller executable.          */
 
416
 
 
417
struct  TRaster_Instance_
 
418
{
 
419
    Int       precision_bits;       /* precision related variables         */
 
420
    Int       precision;
 
421
    Int       precision_half;
 
422
    Long      precision_mask;
 
423
    Int       precision_shift;
 
424
    Int       precision_step;
 
425
    Int       precision_jitter;
 
426
 
 
427
    Int       scale_shift;          /* == precision_shift   for bitmaps    */
 
428
                                    /* == precision_shift+1 for pixmaps    */
 
429
 
 
430
    PLong     buff;                 /* The profiles buffer                 */
 
431
    PLong     sizeBuff;             /* Render pool size                    */
 
432
    PLong     maxBuff;              /* Profiles buffer size                */
 
433
    PLong     top;                  /* Current cursor in buffer            */
 
434
 
 
435
    QT_FT_Error  error;
 
436
 
 
437
    Int       numTurns;             /* number of Y-turns in outline        */
 
438
 
 
439
    TPoint*   arc;                  /* current Bezier arc pointer          */
 
440
 
 
441
    UShort    bWidth;               /* target bitmap width                 */
 
442
    PByte     bTarget;              /* target bitmap buffer                */
 
443
    PByte     gTarget;              /* target pixmap buffer                */
 
444
 
 
445
    Long      lastX, lastY, minY, maxY;
 
446
    Short     minX_dev, maxX_dev;   /* vertical bounds in device coords    */
 
447
 
 
448
    UShort    num_Profs;            /* current number of profiles          */
 
449
 
 
450
    Bool      fresh;                /* signals a fresh new profile which   */
 
451
                                    /* 'start' field must be completed     */
 
452
    PProfile  cProfile;             /* current profile                     */
 
453
    PProfile  fProfile;             /* head of linked list of profiles     */
 
454
    PProfile  gProfile;             /* contour's first profile in case     */
 
455
                                    /* of impact                           */
 
456
 
 
457
    TStates   state;                /* rendering state                     */
 
458
 
 
459
    QT_FT_Outline  outline;
 
460
 
 
461
    Long      traceOfs;             /* current offset in target bitmap     */
 
462
    Long      traceG;               /* current offset in target pixmap     */
 
463
 
 
464
    Short     traceIncr;            /* sweep's increment in target bitmap  */
 
465
 
 
466
    Short     gray_min_x;           /* current min x during gray rendering */
 
467
    Short     gray_max_x;           /* current max x during gray rendering */
 
468
 
 
469
    /* dispatch variables */
 
470
 
 
471
    Function_Sweep_Init*  Proc_Sweep_Init;
 
472
    Function_Sweep_Span*  Proc_Sweep_Span;
 
473
    Function_Sweep_Step*  Proc_Sweep_Step;
 
474
 
 
475
    TPoint    arcs[3 * MaxBezier + 1]; /* The Bezier stack                 */
 
476
 
 
477
    TBand     band_stack[16];       /* band stack used for sub-banding     */
 
478
    Int       band_top;             /* band stack top                      */
 
479
 
 
480
    void*     memory;
 
481
 
 
482
#if 0
 
483
    PByte       flags;              /* current flags table                 */
 
484
    PUShort     outs;               /* current outlines table              */
 
485
    QT_FT_Vector*  coords;
 
486
 
 
487
    UShort      nPoints;            /* number of points in current glyph   */
 
488
    Short       nContours;          /* number of contours in current glyph */
 
489
#endif
 
490
 
 
491
    QT_FT_SpanFunc black_spans;
 
492
    void *      user_data;
 
493
    Bool        odd_even;           /* True for odd even fills             */
 
494
    QT_FT_BBox     clip_box;           /* The clipping box */
 
495
};
 
496
 
 
497
 
 
498
#ifdef QT_FT_CONFIG_OPTION_STATIC_RASTER
 
499
 
 
500
static TRaster_Instance  cur_ras;
 
501
#define ras  cur_ras
 
502
 
 
503
#else
 
504
 
 
505
#define ras  (*raster)
 
506
 
 
507
#endif /* QT_FT_CONFIG_OPTION_STATIC_RASTER */
 
508
 
 
509
 
 
510
/*************************************************************************/
 
511
/*************************************************************************/
 
512
/**                                                                     **/
 
513
/**  PROFILES COMPUTATION                                               **/
 
514
/**                                                                     **/
 
515
/*************************************************************************/
 
516
/*************************************************************************/
 
517
 
 
518
 
 
519
/*************************************************************************/
 
520
/*                                                                       */
 
521
/* <Function>                                                            */
 
522
/*    Set_High_Precision                                                 */
 
523
/*                                                                       */
 
524
/* <Description>                                                         */
 
525
/*    Sets precision variables according to param flag.                  */
 
526
/*                                                                       */
 
527
/* <Input>                                                               */
 
528
/*    High :: Set to True for high precision (typically for ppem < 18),  */
 
529
/*            false otherwise.                                           */
 
530
/*                                                                       */
 
531
static void
 
532
Set_High_Precision( RAS_ARGS Int  High )
 
533
{
 
534
    if ( High )
 
535
    {
 
536
        ras.precision_bits   = 10;
 
537
        ras.precision_step   = 128;
 
538
        ras.precision_jitter = 24;
 
539
    }
 
540
    else
 
541
    {
 
542
        ras.precision_bits   = 6;
 
543
        ras.precision_step   = 32;
 
544
        ras.precision_jitter = 2;
 
545
    }
 
546
 
 
547
    QT_FT_TRACE6( "Set_High_Precision(%s)\n", High ? "true" : "false" );
 
548
 
 
549
    ras.precision       = 1 << ras.precision_bits;
 
550
    ras.precision_half  = ras.precision / 2;
 
551
    ras.precision_shift = ras.precision_bits - Pixel_Bits;
 
552
    ras.precision_mask  = -ras.precision;
 
553
}
 
554
 
 
555
 
 
556
/*************************************************************************/
 
557
/*                                                                       */
 
558
/* <Function>                                                            */
 
559
/*    New_Profile                                                        */
 
560
/*                                                                       */
 
561
/* <Description>                                                         */
 
562
/*    Creates a new profile in the render pool.                          */
 
563
/*                                                                       */
 
564
/* <Input>                                                               */
 
565
/*    aState :: The state/orientation of the new profile.                */
 
566
/*                                                                       */
 
567
/* <Return>                                                              */
 
568
/*   SUCCESS on success.  FAILURE in case of overflow or of incoherent   */
 
569
/*   profile.                                                            */
 
570
/*                                                                       */
 
571
static Bool
 
572
New_Profile( RAS_ARGS TStates  aState )
 
573
{
 
574
    if ( !ras.fProfile )
 
575
    {
 
576
        ras.cProfile  = (PProfile)ras.top;
 
577
        ras.fProfile  = ras.cProfile;
 
578
        ras.top      += AlignProfileSize;
 
579
    }
 
580
 
 
581
    if ( ras.top >= ras.maxBuff )
 
582
    {
 
583
        ras.error = Raster_Err_Overflow;
 
584
        return FAILURE;
 
585
    }
 
586
 
 
587
    switch ( aState )
 
588
    {
 
589
    case Ascending_State:
 
590
        ras.cProfile->flow = Flow_Up;
 
591
        QT_FT_TRACE6( "\nNew ascending profile = %lx\n", (long)ras.cProfile );
 
592
        break;
 
593
 
 
594
    case Descending_State:
 
595
        ras.cProfile->flow = Flow_Down;
 
596
        QT_FT_TRACE6( "\nNew descending profile = %lx\n", (long)ras.cProfile );
 
597
        break;
 
598
 
 
599
    default:
 
600
        QT_FT_ERROR(( "New_Profile: invalid profile direction!\n" ));
 
601
        ras.error = Raster_Err_Invalid;
 
602
        return FAILURE;
 
603
    }
 
604
 
 
605
    ras.cProfile->start  = 0;
 
606
    ras.cProfile->height = 0;
 
607
    ras.cProfile->offset = ras.top;
 
608
    ras.cProfile->link   = (PProfile)0;
 
609
 
 
610
    if ( !ras.gProfile )
 
611
        ras.gProfile = ras.cProfile;
 
612
 
 
613
    ras.state = aState;
 
614
    ras.fresh = TRUE;
 
615
 
 
616
    return SUCCESS;
 
617
}
 
618
 
 
619
 
 
620
/*************************************************************************/
 
621
/*                                                                       */
 
622
/* <Function>                                                            */
 
623
/*    End_Profile                                                        */
 
624
/*                                                                       */
 
625
/* <Description>                                                         */
 
626
/*    Finalizes the current profile.                                     */
 
627
/*                                                                       */
 
628
/* <Return>                                                              */
 
629
/*    SUCCESS on success.  FAILURE in case of overflow or incoherency.   */
 
630
/*                                                                       */
 
631
static Bool
 
632
End_Profile( RAS_ARG )
 
633
{
 
634
    Long      h;
 
635
    PProfile  oldProfile;
 
636
    
 
637
    h = (Long)( ras.top - ras.cProfile->offset );
 
638
 
 
639
    if ( h < 0 )
 
640
    {
 
641
        QT_FT_ERROR(( "End_Profile: negative height encountered!\n" ));
 
642
        ras.error = Raster_Err_Neg_Height;
 
643
        return FAILURE;
 
644
    }
 
645
 
 
646
    if ( h > 0 )
 
647
    {
 
648
        QT_FT_TRACE6( "Ending profile %lx, start = %ld, height = %ld top=%p\n\n",
 
649
                   (long)ras.cProfile, ras.cProfile->start, h,ras.top );
 
650
 
 
651
        oldProfile           = ras.cProfile;
 
652
        ras.cProfile->height = h;
 
653
        ras.cProfile         = (PProfile)ras.top;
 
654
 
 
655
        ras.top             += AlignProfileSize;
 
656
 
 
657
        ras.cProfile->height = 0;
 
658
        ras.cProfile->offset = ras.top;
 
659
        ras.num_Profs++;
 
660
    }
 
661
 
 
662
    if ( ras.top >= ras.maxBuff )
 
663
    {
 
664
        QT_FT_TRACE1( "overflow in End_Profile\n" );
 
665
        ras.error = Raster_Err_Overflow;
 
666
        return FAILURE;
 
667
    }
 
668
 
 
669
    return SUCCESS;
 
670
}
 
671
 
 
672
 
 
673
/*************************************************************************/
 
674
/*                                                                       */
 
675
/* <Function>                                                            */
 
676
/*    Insert_Y_Turn                                                      */
 
677
/*                                                                       */
 
678
/* <Description>                                                         */
 
679
/*    Inserts a salient into the sorted list placed on top of the render */
 
680
/*    pool.                                                              */
 
681
/*                                                                       */
 
682
/* <Input>                                                               */
 
683
/*    New y scanline position.                                           */
 
684
/*                                                                       */
 
685
/* <Return>                                                              */
 
686
/*    SUCCESS on success.  FAILURE in case of overflow.                  */
 
687
/*                                                                       */
 
688
static Bool
 
689
Insert_Y_Turn( RAS_ARGS Int  y )
 
690
{
 
691
    PLong  y_turns;
 
692
    Int    y2, n;
 
693
 
 
694
 
 
695
    n       = ras.numTurns - 1;
 
696
    y_turns = ras.sizeBuff - ras.numTurns;
 
697
 
 
698
    /* look for first y value that is <= */
 
699
    while ( n >= 0 && y < y_turns[n] )
 
700
        n--;
 
701
 
 
702
    /* if it is <, simply insert it, ignore if == */
 
703
    if ( n >= 0 && y > y_turns[n] )
 
704
        while ( n >= 0 )
 
705
        {
 
706
            y2 = (Int)y_turns[n];
 
707
            y_turns[n] = y;
 
708
            y = y2;
 
709
            n--;
 
710
        }
 
711
 
 
712
    if ( n < 0 )
 
713
    {
 
714
        ras.maxBuff--;
 
715
        if ( ras.maxBuff <= ras.top )
 
716
        {
 
717
            ras.error = Raster_Err_Overflow;
 
718
            return FAILURE;
 
719
        }
 
720
        ras.numTurns++;
 
721
        ras.sizeBuff[-ras.numTurns] = y;
 
722
    }
 
723
 
 
724
    return SUCCESS;
 
725
}
 
726
 
 
727
 
 
728
/*************************************************************************/
 
729
/*                                                                       */
 
730
/* <Function>                                                            */
 
731
/*    Finalize_Profile_Table                                             */
 
732
/*                                                                       */
 
733
/* <Description>                                                         */
 
734
/*    Adjusts all links in the profiles list.                            */
 
735
/*                                                                       */
 
736
/* <Return>                                                              */
 
737
/*    SUCCESS on success.  FAILURE in case of overflow.                  */
 
738
/*                                                                       */
 
739
static Bool
 
740
Finalize_Profile_Table( RAS_ARG )
 
741
{
 
742
    Int       bottom, top;
 
743
    UShort    n;
 
744
    PProfile  p;
 
745
 
 
746
 
 
747
    n = ras.num_Profs;
 
748
 
 
749
    if ( n > 1 )
 
750
    {
 
751
        p = ras.fProfile;
 
752
        while ( n > 0 )
 
753
        {
 
754
            if ( n > 1 )
 
755
                p->link = (PProfile)( p->offset + p->height );
 
756
            else
 
757
                p->link = NULL;
 
758
 
 
759
            switch ( p->flow )
 
760
            {
 
761
            case Flow_Down:
 
762
                bottom     = (Int)( p->start - p->height + 1 );
 
763
                top        = (Int)p->start;
 
764
                p->start   = bottom;
 
765
                p->offset += p->height - 1;
 
766
                break;
 
767
 
 
768
            case Flow_Up:
 
769
            default:
 
770
                bottom = (Int)p->start;
 
771
                top    = (Int)( p->start + p->height - 1 );
 
772
            }
 
773
 
 
774
            if ( Insert_Y_Turn( RAS_VARS bottom )   ||
 
775
                 Insert_Y_Turn( RAS_VARS top + 1 )  )
 
776
                return FAILURE;
 
777
 
 
778
            p = p->link;
 
779
            n--;
 
780
        }
 
781
    }
 
782
    else
 
783
        ras.fProfile = NULL;
 
784
 
 
785
    return SUCCESS;
 
786
}
 
787
 
 
788
 
 
789
/*************************************************************************/
 
790
/*                                                                       */
 
791
/* <Function>                                                            */
 
792
/*    Split_Conic                                                        */
 
793
/*                                                                       */
 
794
/* <Description>                                                         */
 
795
/*    Subdivides one conic Bezier into two joint sub-arcs in the Bezier  */
 
796
/*    stack.                                                             */
 
797
/*                                                                       */
 
798
/* <Input>                                                               */
 
799
/*    None (subdivided Bezier is taken from the top of the stack).       */
 
800
/*                                                                       */
 
801
/* <Note>                                                                */
 
802
/*    This routine is the `beef' of this component.  It is  _the_ inner  */
 
803
/*    loop that should be optimized to hell to get the best performance. */
 
804
/*                                                                       */
 
805
static void
 
806
Split_Conic( TPoint*  base )
 
807
{
 
808
    Long  a, b;
 
809
 
 
810
 
 
811
    base[4].x = base[2].x;
 
812
    b = base[1].x;
 
813
    a = base[3].x = ( base[2].x + b ) / 2;
 
814
    b = base[1].x = ( base[0].x + b ) / 2;
 
815
    base[2].x = ( a + b ) / 2;
 
816
 
 
817
    base[4].y = base[2].y;
 
818
    b = base[1].y;
 
819
    a = base[3].y = ( base[2].y + b ) / 2;
 
820
    b = base[1].y = ( base[0].y + b ) / 2;
 
821
    base[2].y = ( a + b ) / 2;
 
822
 
 
823
    /* hand optimized.  gcc doesn't seem to be too good at common      */
 
824
    /* expression substitution and instruction scheduling ;-)          */
 
825
}
 
826
 
 
827
 
 
828
/*************************************************************************/
 
829
/*                                                                       */
 
830
/* <Function>                                                            */
 
831
/*    Split_Cubic                                                        */
 
832
/*                                                                       */
 
833
/* <Description>                                                         */
 
834
/*    Subdivides a third-order Bezier arc into two joint sub-arcs in the */
 
835
/*    Bezier stack.                                                      */
 
836
/*                                                                       */
 
837
/* <Note>                                                                */
 
838
/*    This routine is the `beef' of the component.  It is one of _the_   */
 
839
/*    inner loops that should be optimized like hell to get the best     */
 
840
/*    performance.                                                       */
 
841
/*                                                                       */
 
842
static void
 
843
Split_Cubic( TPoint*  base )
 
844
{
 
845
    Long  a, b, c, d;
 
846
 
 
847
 
 
848
    base[6].x = base[3].x;
 
849
    c = base[1].x;
 
850
    d = base[2].x;
 
851
    base[1].x = a = ( base[0].x + c + 1 ) >> 1;
 
852
    base[5].x = b = ( base[3].x + d + 1 ) >> 1;
 
853
    c = ( c + d + 1 ) >> 1;
 
854
    base[2].x = a = ( a + c + 1 ) >> 1;
 
855
    base[4].x = b = ( b + c + 1 ) >> 1;
 
856
    base[3].x = ( a + b + 1 ) >> 1;
 
857
 
 
858
    base[6].y = base[3].y;
 
859
    c = base[1].y;
 
860
    d = base[2].y;
 
861
    base[1].y = a = ( base[0].y + c + 1 ) >> 1;
 
862
    base[5].y = b = ( base[3].y + d + 1 ) >> 1;
 
863
    c = ( c + d + 1 ) >> 1;
 
864
    base[2].y = a = ( a + c + 1 ) >> 1;
 
865
    base[4].y = b = ( b + c + 1 ) >> 1;
 
866
    base[3].y = ( a + b + 1 ) >> 1;
 
867
}
 
868
 
 
869
 
 
870
/*************************************************************************/
 
871
/*                                                                       */
 
872
/* <Function>                                                            */
 
873
/*    Line_Up                                                            */
 
874
/*                                                                       */
 
875
/* <Description>                                                         */
 
876
/*    Computes the x-coordinates of an ascending line segment and stores */
 
877
/*    them in the render pool.                                           */
 
878
/*                                                                       */
 
879
/* <Input>                                                               */
 
880
/*    x1   :: The x-coordinate of the segment's start point.             */
 
881
/*                                                                       */
 
882
/*    y1   :: The y-coordinate of the segment's start point.             */
 
883
/*                                                                       */
 
884
/*    x2   :: The x-coordinate of the segment's end point.               */
 
885
/*                                                                       */
 
886
/*    y2   :: The y-coordinate of the segment's end point.               */
 
887
/*                                                                       */
 
888
/*    miny :: A lower vertical clipping bound value.                     */
 
889
/*                                                                       */
 
890
/*    maxy :: An upper vertical clipping bound value.                    */
 
891
/*                                                                       */
 
892
/* <Return>                                                              */
 
893
/*    SUCCESS on success, FAILURE on render pool overflow.               */
 
894
/*                                                                       */
 
895
static Bool
 
896
Line_Up( RAS_ARGS Long  x1,
 
897
         Long  y1,
 
898
         Long  x2,
 
899
         Long  y2,
 
900
         Long  miny,
 
901
         Long  maxy )
 
902
{
 
903
    Long   Dx, Dy;
 
904
    Int    e1, e2, f1, size;     /* XXX: is `Short' sufficient? */
 
905
    Long   Ix, Rx, Ax;
 
906
    Int clipped;
 
907
    PLong  top;
 
908
 
 
909
    clipped = 0;
 
910
 
 
911
    Dx = x2 - x1;
 
912
    Dy = y2 - y1;
 
913
 
 
914
    if ( Dy <= 0 || y2 < miny || y1 > maxy )
 
915
        return SUCCESS;
 
916
 
 
917
    if ( y1 < miny )
 
918
    {
 
919
        /* Take care: miny-y1 can be a very large value; we use     */
 
920
        /*            a slow MulDiv function to avoid clipping bugs */
 
921
        x1 += SMulDiv( Dx, miny - y1, Dy );
 
922
        e1  = (Int)TRUNC( CEILING(miny) );
 
923
        f1  = 0;
 
924
        clipped = 1;
 
925
    }
 
926
    else
 
927
    {
 
928
        e1 = (Int)TRUNC( CEILING(y1) );
 
929
        f1 = (Int)FRAC( y1 );
 
930
    }
 
931
 
 
932
    if ( y2 > maxy )
 
933
    {
 
934
        /* x2 += FMulDiv( Dx, maxy - y2, Dy );  UNNECESSARY */
 
935
        e2  = (Int)TRUNC( FLOOR(maxy) );
 
936
    }
 
937
    else
 
938
    {
 
939
        e2 = (Int)TRUNC( FLOOR(y2) );
 
940
        if (FRAC(y2) == 0 && ras.cProfile->flow == Flow_Up)
 
941
            --e2;
 
942
    }
 
943
    QT_FT_TRACE6("Line_Up y1=%f, y2=%f, e1=%d, e2=%d f1=%d\n", y1/64.,  y2/64., e1, e2, f1);
 
944
 
 
945
    if ( f1 > 0 )
 
946
    {
 
947
        x1 += FMulDiv( Dx, ras.precision - f1, Dy );
 
948
    } else if (ras.cProfile->flow == Flow_Down && !clipped ) {
 
949
        e1++;
 
950
        x1 += FMulDiv( Dx, ras.precision, Dy);
 
951
    }
 
952
 
 
953
    if ( ras.fresh )
 
954
    {
 
955
        ras.cProfile->start = e1;
 
956
        ras.fresh           = FALSE;
 
957
    }
 
958
    QT_FT_TRACE6("e1 = start=%d, e2=%d\n", e1, e2);
 
959
 
 
960
    size = e2 - e1 + 1;
 
961
    if ( ras.top + size >= ras.maxBuff )
 
962
    {
 
963
        ras.error = Raster_Err_Overflow;
 
964
        return FAILURE;
 
965
    }
 
966
 
 
967
    if ( Dx > 0 )
 
968
    {
 
969
        Ix = ( ras.precision * Dx ) / Dy;
 
970
        Rx = ( ras.precision * Dx ) % Dy;
 
971
        Dx = 1;
 
972
    }
 
973
    else
 
974
    {
 
975
        Ix = -( ( ras.precision * -Dx ) / Dy );
 
976
        Rx =    ( ras.precision * -Dx ) % Dy;
 
977
        Dx = -1;
 
978
    }
 
979
 
 
980
    Ax  = -Dy;
 
981
    top = ras.top;
 
982
 
 
983
    QT_FT_TRACE6("line_up (%f/%f)->(%f/%f), flow=%s\n",
 
984
              x1/64., y1/64., x2/64., y2/64.,
 
985
              ras.cProfile->flow == Flow_Up ? "Flow_Up" : "Flow_Down");
 
986
 
 
987
    while ( size > 0 )
 
988
    {
 
989
        *top++ = x1;
 
990
        QT_FT_TRACE6("    x=%f y=%d\n", x1/64., e2+1-size);
 
991
 
 
992
        x1 += Ix;
 
993
        Ax += Rx;
 
994
        if ( Ax >= 0 )
 
995
        {
 
996
            Ax -= Dy;
 
997
            x1 += Dx;
 
998
        }
 
999
        size--;
 
1000
    }
 
1001
 
 
1002
    ras.top = top;
 
1003
 
 
1004
    return SUCCESS;
 
1005
}
 
1006
 
 
1007
 
 
1008
/*************************************************************************/
 
1009
/*                                                                       */
 
1010
/* <Function>                                                            */
 
1011
/*    Line_Down                                                          */
 
1012
/*                                                                       */
 
1013
/* <Description>                                                         */
 
1014
/*    Computes the x-coordinates of an descending line segment and       */
 
1015
/*    stores them in the render pool.                                    */
 
1016
/*                                                                       */
 
1017
/* <Input>                                                               */
 
1018
/*    x1   :: The x-coordinate of the segment's start point.             */
 
1019
/*                                                                       */
 
1020
/*    y1   :: The y-coordinate of the segment's start point.             */
 
1021
/*                                                                       */
 
1022
/*    x2   :: The x-coordinate of the segment's end point.               */
 
1023
/*                                                                       */
 
1024
/*    y2   :: The y-coordinate of the segment's end point.               */
 
1025
/*                                                                       */
 
1026
/*    miny :: A lower vertical clipping bound value.                     */
 
1027
/*                                                                       */
 
1028
/*    maxy :: An upper vertical clipping bound value.                    */
 
1029
/*                                                                       */
 
1030
/* <Return>                                                              */
 
1031
/*    SUCCESS on success, FAILURE on render pool overflow.               */
 
1032
/*                                                                       */
 
1033
static Bool
 
1034
Line_Down( RAS_ARGS Long  x1,
 
1035
           Long  y1,
 
1036
           Long  x2,
 
1037
           Long  y2,
 
1038
           Long  miny,
 
1039
           Long  maxy )
 
1040
{
 
1041
    Bool  result, fresh;
 
1042
 
 
1043
 
 
1044
    fresh  = ras.fresh;
 
1045
 
 
1046
    result = Line_Up( RAS_VARS x1, -y1, x2, -y2, -maxy, -miny );
 
1047
 
 
1048
    if ( fresh && !ras.fresh )
 
1049
        ras.cProfile->start = -ras.cProfile->start;
 
1050
 
 
1051
    return result;
 
1052
}
 
1053
 
 
1054
 
 
1055
/* A function type describing the functions used to split Bezier arcs */
 
1056
typedef void  (*TSplitter)( TPoint*  base );
 
1057
 
 
1058
 
 
1059
/*************************************************************************/
 
1060
/*                                                                       */
 
1061
/* <Function>                                                            */
 
1062
/*    Bezier_Up                                                          */
 
1063
/*                                                                       */
 
1064
/* <Description>                                                         */
 
1065
/*    Computes the x-coordinates of an ascending Bezier arc and stores   */
 
1066
/*    them in the render pool.                                           */
 
1067
/*                                                                       */
 
1068
/* <Input>                                                               */
 
1069
/*    degree   :: The degree of the Bezier arc (either 2 or 3).          */
 
1070
/*                                                                       */
 
1071
/*    splitter :: The function to split Bezier arcs.                     */
 
1072
/*                                                                       */
 
1073
/*    miny     :: A lower vertical clipping bound value.                 */
 
1074
/*                                                                       */
 
1075
/*    maxy     :: An upper vertical clipping bound value.                */
 
1076
/*                                                                       */
 
1077
/* <Return>                                                              */
 
1078
/*    SUCCESS on success, FAILURE on render pool overflow.               */
 
1079
/*                                                                       */
 
1080
static Bool
 
1081
Bezier_Up( RAS_ARGS Int        degree,
 
1082
           TSplitter  splitter,
 
1083
           Long       miny,
 
1084
           Long       maxy )
 
1085
{
 
1086
    Long   y1, y2, e, e2;
 
1087
 
 
1088
    TPoint*  arc;
 
1089
    TPoint*  start_arc;
 
1090
 
 
1091
    PLong top;
 
1092
 
 
1093
 
 
1094
    arc = ras.arc;
 
1095
    y1  = arc[degree].y;
 
1096
    y2  = arc[0].y;
 
1097
    top = ras.top;
 
1098
 
 
1099
    if ( y2 < miny || y1 > maxy )
 
1100
        goto Fin;
 
1101
 
 
1102
    e2 = FLOOR( y2 );
 
1103
    if ( e2 > maxy )
 
1104
        e2 = FLOOR(maxy);
 
1105
    if (FRAC(y2) == 0 && ras.cProfile->flow == Flow_Up)
 
1106
        e2 -= ras.precision;
 
1107
 
 
1108
    if ( y1 < miny )
 
1109
        e = CEILING(miny);
 
1110
    else
 
1111
    {
 
1112
        e  = CEILING( y1 );
 
1113
        if (FRAC(y1) == 0 && ras.cProfile->flow == Flow_Down)
 
1114
            e += ras.precision;
 
1115
    }
 
1116
 
 
1117
    if ( ras.fresh )
 
1118
    {
 
1119
        ras.cProfile->start = TRUNC( e );
 
1120
        ras.fresh = FALSE;
 
1121
    }
 
1122
 
 
1123
    QT_FT_TRACE6("bezier_up: y1=%f, y2=%f, e1=%f, e2=%f\n", y1/64.,  y2/64., e/64., e2/64.);
 
1124
 
 
1125
    if ( e2 < e )
 
1126
        goto Fin;
 
1127
 
 
1128
    if ( ( top + TRUNC( e2 - e ) + 1 ) >= ras.maxBuff )
 
1129
    {
 
1130
        ras.top   = top;
 
1131
        ras.error = Raster_Err_Overflow;
 
1132
        return FAILURE;
 
1133
    }
 
1134
 
 
1135
    QT_FT_TRACE6("       Flow = %s start=%ld, e=%f, e2=%f y1=%f, y2=%f\n",
 
1136
              ras.cProfile->flow == Flow_Up ? "Flow_Up" : "Flow_Down",
 
1137
              ras.cProfile->start, e/64., e2/64.,y1/64.,y2/64.);
 
1138
 
 
1139
    start_arc = arc;
 
1140
 
 
1141
    while ( arc >= start_arc && e <= e2 )
 
1142
    {
 
1143
        y2 = arc[0].y;
 
1144
 
 
1145
        if ( y2 > e )
 
1146
        {
 
1147
            y1 = arc[degree].y;
 
1148
            if ( y2 - y1 >= ras.precision_step )
 
1149
            {
 
1150
                splitter( arc );
 
1151
                arc += degree;
 
1152
            }
 
1153
            else
 
1154
            {
 
1155
                *top++ = arc[degree].x + FMulDiv( arc[0].x-arc[degree].x,
 
1156
                                                  e - y1, y2 - y1 );
 
1157
                QT_FT_TRACE6("  x=%f y=%f\n",
 
1158
                          (arc[degree].x + FMulDiv( arc[0].x-arc[degree].x,
 
1159
                                                    e - y1, y2 - y1 ))/64., e/64.);
 
1160
                arc -= degree;
 
1161
                e   += ras.precision;
 
1162
            }
 
1163
        }
 
1164
        else
 
1165
        {
 
1166
            if ( y2 == e )
 
1167
            {
 
1168
                *top++     = arc[0].x;
 
1169
                QT_FT_TRACE6("  x=%f y=%f\n", arc[0].x/64., e/64.);
 
1170
 
 
1171
                e += ras.precision;
 
1172
            }
 
1173
            arc -= degree;
 
1174
        }
 
1175
    }
 
1176
 
 
1177
Fin:
 
1178
    ras.top  = top;
 
1179
    ras.arc -= degree;
 
1180
    QT_FT_TRACE6("    currently %d points in profile, start=%ld top=%p\n", (int)(ras.top - ras.cProfile->offset), ras.cProfile->start, ras.top);
 
1181
    return SUCCESS;
 
1182
}
 
1183
 
 
1184
 
 
1185
/*************************************************************************/
 
1186
/*                                                                       */
 
1187
/* <Function>                                                            */
 
1188
/*    Bezier_Down                                                        */
 
1189
/*                                                                       */
 
1190
/* <Description>                                                         */
 
1191
/*    Computes the x-coordinates of an descending Bezier arc and stores  */
 
1192
/*    them in the render pool.                                           */
 
1193
/*                                                                       */
 
1194
/* <Input>                                                               */
 
1195
/*    degree   :: The degree of the Bezier arc (either 2 or 3).          */
 
1196
/*                                                                       */
 
1197
/*    splitter :: The function to split Bezier arcs.                     */
 
1198
/*                                                                       */
 
1199
/*    miny     :: A lower vertical clipping bound value.                 */
 
1200
/*                                                                       */
 
1201
/*    maxy     :: An upper vertical clipping bound value.                */
 
1202
/*                                                                       */
 
1203
/* <Return>                                                              */
 
1204
/*    SUCCESS on success, FAILURE on render pool overflow.               */
 
1205
/*                                                                       */
 
1206
static Bool
 
1207
Bezier_Down( RAS_ARGS Int        degree,
 
1208
             TSplitter  splitter,
 
1209
             Long       miny,
 
1210
             Long       maxy )
 
1211
{
 
1212
    TPoint*  arc = ras.arc;
 
1213
    Bool     result, fresh;
 
1214
 
 
1215
 
 
1216
    arc[0].y = -arc[0].y;
 
1217
    arc[1].y = -arc[1].y;
 
1218
    arc[2].y = -arc[2].y;
 
1219
    if ( degree > 2 )
 
1220
        arc[3].y = -arc[3].y;
 
1221
 
 
1222
    fresh = ras.fresh;
 
1223
 
 
1224
    result = Bezier_Up( RAS_VARS degree, splitter, -maxy, -miny );
 
1225
 
 
1226
    if ( fresh && !ras.fresh )
 
1227
        ras.cProfile->start = -ras.cProfile->start;
 
1228
 
 
1229
    arc[0].y = -arc[0].y;
 
1230
    return result;
 
1231
}
 
1232
 
 
1233
 
 
1234
/*************************************************************************/
 
1235
/*                                                                       */
 
1236
/* <Function>                                                            */
 
1237
/*    Line_To                                                            */
 
1238
/*                                                                       */
 
1239
/* <Description>                                                         */
 
1240
/*    Injects a new line segment and adjusts Profiles list.              */
 
1241
/*                                                                       */
 
1242
/* <Input>                                                               */
 
1243
/*   x :: The x-coordinate of the segment's end point (its start point   */
 
1244
/*        is stored in `LastX').                                         */
 
1245
/*                                                                       */
 
1246
/*   y :: The y-coordinate of the segment's end point (its start point   */
 
1247
/*        is stored in `LastY').                                         */
 
1248
/*                                                                       */
 
1249
/* <Return>                                                              */
 
1250
/*   SUCCESS on success, FAILURE on render pool overflow or incorrect    */
 
1251
/*   profile.                                                            */
 
1252
/*                                                                       */
 
1253
static Bool
 
1254
Line_To( RAS_ARGS Long  x,
 
1255
         Long  y )
 
1256
{
 
1257
    /* First, detect a change of direction */
 
1258
    QT_FT_TRACE6( "Line_To (%f/%f)->(%f/%f)\n", ras.lastX/64., ras.lastY/64., x/64., y/64. );
 
1259
 
 
1260
    switch ( ras.state )
 
1261
    {
 
1262
    case Unknown_State:
 
1263
        if ( y > ras.lastY )
 
1264
        {
 
1265
            if ( New_Profile( RAS_VARS Ascending_State ) )
 
1266
                return FAILURE;
 
1267
        }
 
1268
        else
 
1269
        {
 
1270
            if ( y < ras.lastY )
 
1271
                if ( New_Profile( RAS_VARS Descending_State ) )
 
1272
                    return FAILURE;
 
1273
        }
 
1274
        break;
 
1275
 
 
1276
    case Ascending_State:
 
1277
        if ( y < ras.lastY )
 
1278
        {
 
1279
            if ( End_Profile( RAS_VAR )                   ||
 
1280
                 New_Profile( RAS_VARS Descending_State ) )
 
1281
                return FAILURE;
 
1282
        }
 
1283
        break;
 
1284
 
 
1285
    case Descending_State:
 
1286
        if ( y > ras.lastY )
 
1287
        {
 
1288
            if ( End_Profile( RAS_VAR )                  ||
 
1289
                 New_Profile( RAS_VARS Ascending_State ) )
 
1290
                return FAILURE;
 
1291
        }
 
1292
        break;
 
1293
 
 
1294
    default:
 
1295
        ;
 
1296
    }
 
1297
 
 
1298
    /* Then compute the lines */
 
1299
 
 
1300
    switch ( ras.state )
 
1301
    {
 
1302
    case Ascending_State:
 
1303
        if ( Line_Up( RAS_VARS ras.lastX, ras.lastY,
 
1304
                      x, y, ras.minY, ras.maxY ) )
 
1305
            return FAILURE;
 
1306
        break;
 
1307
 
 
1308
    case Descending_State:
 
1309
        if ( Line_Down( RAS_VARS ras.lastX, ras.lastY,
 
1310
                        x, y, ras.minY, ras.maxY ) )
 
1311
            return FAILURE;
 
1312
        break;
 
1313
 
 
1314
    default:
 
1315
        ;
 
1316
    }
 
1317
 
 
1318
    ras.lastX = x;
 
1319
    ras.lastY = y;
 
1320
 
 
1321
    return SUCCESS;
 
1322
}
 
1323
 
 
1324
 
 
1325
/*************************************************************************/
 
1326
/*                                                                       */
 
1327
/* <Function>                                                            */
 
1328
/*    Conic_To                                                           */
 
1329
/*                                                                       */
 
1330
/* <Description>                                                         */
 
1331
/*    Injects a new conic arc and adjusts the profile list.              */
 
1332
/*                                                                       */
 
1333
/* <Input>                                                               */
 
1334
/*   cx :: The x-coordinate of the arc's new control point.              */
 
1335
/*                                                                       */
 
1336
/*   cy :: The y-coordinate of the arc's new control point.              */
 
1337
/*                                                                       */
 
1338
/*   x  :: The x-coordinate of the arc's end point (its start point is   */
 
1339
/*         stored in `LastX').                                           */
 
1340
/*                                                                       */
 
1341
/*   y  :: The y-coordinate of the arc's end point (its start point is   */
 
1342
/*         stored in `LastY').                                           */
 
1343
/*                                                                       */
 
1344
/* <Return>                                                              */
 
1345
/*   SUCCESS on success, FAILURE on render pool overflow or incorrect    */
 
1346
/*   profile.                                                            */
 
1347
/*                                                                       */
 
1348
static Bool
 
1349
Conic_To( RAS_ARGS Long  cx,
 
1350
          Long  cy,
 
1351
          Long  x,
 
1352
          Long  y )
 
1353
{
 
1354
    Long     y1, y2, y3, x3, ymin, ymax;
 
1355
    TStates  state_bez;
 
1356
 
 
1357
    QT_FT_TRACE6( "Conic_To (%f/%f)-(%f/%f)\n", cx/64., cy/64., x/64., y/64. );
 
1358
 
 
1359
    ras.arc      = ras.arcs;
 
1360
    ras.arc[2].x = ras.lastX;
 
1361
    ras.arc[2].y = ras.lastY;
 
1362
    ras.arc[1].x = cx; ras.arc[1].y = cy;
 
1363
    ras.arc[0].x = x;  ras.arc[0].y = y;
 
1364
 
 
1365
    do
 
1366
    {
 
1367
        y1 = ras.arc[2].y;
 
1368
        y2 = ras.arc[1].y;
 
1369
        y3 = ras.arc[0].y;
 
1370
        x3 = ras.arc[0].x;
 
1371
 
 
1372
        /* first, categorize the Bezier arc */
 
1373
 
 
1374
        if ( y1 <= y3 )
 
1375
        {
 
1376
            ymin = y1;
 
1377
            ymax = y3;
 
1378
        }
 
1379
        else
 
1380
        {
 
1381
            ymin = y3;
 
1382
            ymax = y1;
 
1383
        }
 
1384
 
 
1385
        if ( y2 < ymin || y2 > ymax )
 
1386
        {
 
1387
            /* this arc has no given direction, split it! */
 
1388
            Split_Conic( ras.arc );
 
1389
            ras.arc += 2;
 
1390
        }
 
1391
        else if ( y1 == y3 )
 
1392
        {
 
1393
            /* this arc is flat, ignore it and pop it from the Bezier stack */
 
1394
            ras.arc -= 2;
 
1395
        }
 
1396
        else
 
1397
        {
 
1398
            /* the arc is y-monotonous, either ascending or descending */
 
1399
            /* detect a change of direction                            */
 
1400
            state_bez = y1 < y3 ? Ascending_State : Descending_State;
 
1401
            if ( ras.state != state_bez )
 
1402
            {
 
1403
                /* finalize current profile if any */
 
1404
                if ( ras.state != Unknown_State   &&
 
1405
                     End_Profile( RAS_VAR ) )
 
1406
                    goto Fail;
 
1407
 
 
1408
                /* create a new profile */
 
1409
                if ( New_Profile( RAS_VARS state_bez ) )
 
1410
                    goto Fail;
 
1411
            }
 
1412
 
 
1413
            /* now call the appropriate routine */
 
1414
            if ( state_bez == Ascending_State )
 
1415
            {
 
1416
                if ( Bezier_Up( RAS_VARS 2, Split_Conic, ras.minY, ras.maxY ) )
 
1417
                    goto Fail;
 
1418
            }
 
1419
            else
 
1420
                if ( Bezier_Down( RAS_VARS 2, Split_Conic, ras.minY, ras.maxY ) )
 
1421
                    goto Fail;
 
1422
        }
 
1423
 
 
1424
    } while ( ras.arc >= ras.arcs );
 
1425
 
 
1426
    ras.lastX = x3;
 
1427
    ras.lastY = y3;
 
1428
 
 
1429
    return SUCCESS;
 
1430
 
 
1431
Fail:
 
1432
    return FAILURE;
 
1433
}
 
1434
 
 
1435
 
 
1436
/*************************************************************************/
 
1437
/*                                                                       */
 
1438
/* <Function>                                                            */
 
1439
/*    Cubic_To                                                           */
 
1440
/*                                                                       */
 
1441
/* <Description>                                                         */
 
1442
/*    Injects a new cubic arc and adjusts the profile list.              */
 
1443
/*                                                                       */
 
1444
/* <Input>                                                               */
 
1445
/*   cx1 :: The x-coordinate of the arc's first new control point.       */
 
1446
/*                                                                       */
 
1447
/*   cy1 :: The y-coordinate of the arc's first new control point.       */
 
1448
/*                                                                       */
 
1449
/*   cx2 :: The x-coordinate of the arc's second new control point.      */
 
1450
/*                                                                       */
 
1451
/*   cy2 :: The y-coordinate of the arc's second new control point.      */
 
1452
/*                                                                       */
 
1453
/*   x   :: The x-coordinate of the arc's end point (its start point is  */
 
1454
/*          stored in `LastX').                                          */
 
1455
/*                                                                       */
 
1456
/*   y   :: The y-coordinate of the arc's end point (its start point is  */
 
1457
/*          stored in `LastY').                                          */
 
1458
/*                                                                       */
 
1459
/* <Return>                                                              */
 
1460
/*   SUCCESS on success, FAILURE on render pool overflow or incorrect    */
 
1461
/*   profile.                                                            */
 
1462
/*                                                                       */
 
1463
static Bool
 
1464
Cubic_To( RAS_ARGS Long  cx1,
 
1465
          Long  cy1,
 
1466
          Long  cx2,
 
1467
          Long  cy2,
 
1468
          Long  x,
 
1469
          Long  y )
 
1470
{
 
1471
    Long     y1, y2, y3, y4, x4, ymin1, ymax1, ymin2, ymax2;
 
1472
    TStates  state_bez;
 
1473
 
 
1474
    QT_FT_TRACE6( "Cubic_To (%f/%f)-(%f/%f)-(%f/%f)-(%f/%f)\n",
 
1475
               ras.lastX/64., ras.lastY/64., cx1/64., cy1/64., cx2/64., cy2/64., x/64., y/64. );
 
1476
 
 
1477
    ras.arc      = ras.arcs;
 
1478
    ras.arc[3].x = ras.lastX;
 
1479
    ras.arc[3].y = ras.lastY;
 
1480
    ras.arc[2].x = cx1; ras.arc[2].y = cy1;
 
1481
    ras.arc[1].x = cx2; ras.arc[1].y = cy2;
 
1482
    ras.arc[0].x = x;   ras.arc[0].y = y;
 
1483
 
 
1484
    do
 
1485
    {
 
1486
        y1 = ras.arc[3].y;
 
1487
        y2 = ras.arc[2].y;
 
1488
        y3 = ras.arc[1].y;
 
1489
        y4 = ras.arc[0].y;
 
1490
        x4 = ras.arc[0].x;
 
1491
 
 
1492
        /* first, categorize the Bezier arc */
 
1493
 
 
1494
        if ( y1 <= y4 )
 
1495
        {
 
1496
            ymin1 = y1;
 
1497
            ymax1 = y4;
 
1498
        }
 
1499
        else
 
1500
        {
 
1501
            ymin1 = y4;
 
1502
            ymax1 = y1;
 
1503
        }
 
1504
 
 
1505
        if ( y2 <= y3 )
 
1506
        {
 
1507
            ymin2 = y2;
 
1508
            ymax2 = y3;
 
1509
        }
 
1510
        else
 
1511
        {
 
1512
            ymin2 = y3;
 
1513
            ymax2 = y2;
 
1514
        }
 
1515
 
 
1516
        if ( ymin2 < ymin1 || ymax2 > ymax1 )
 
1517
        {
 
1518
            /* this arc has no given direction, split it! */
 
1519
            Split_Cubic( ras.arc );
 
1520
            ras.arc += 3;
 
1521
        }
 
1522
        else if ( y1 == y4 )
 
1523
        {
 
1524
            /* this arc is flat, ignore it and pop it from the Bezier stack */
 
1525
            ras.arc -= 3;
 
1526
        }
 
1527
        else
 
1528
        {
 
1529
            state_bez = ( y1 <= y4 ) ? Ascending_State : Descending_State;
 
1530
 
 
1531
            /* detect a change of direction */
 
1532
            if ( ras.state != state_bez )
 
1533
            {
 
1534
                if ( ras.state != Unknown_State   &&
 
1535
                     End_Profile( RAS_VAR ) )
 
1536
                    goto Fail;
 
1537
 
 
1538
                if ( New_Profile( RAS_VARS state_bez ) )
 
1539
                    goto Fail;
 
1540
            }
 
1541
 
 
1542
            /* compute intersections */
 
1543
            if ( state_bez == Ascending_State )
 
1544
            {
 
1545
                if ( Bezier_Up( RAS_VARS 3, Split_Cubic, ras.minY, ras.maxY ) )
 
1546
                    goto Fail;
 
1547
            }
 
1548
            else
 
1549
                if ( Bezier_Down( RAS_VARS 3, Split_Cubic, ras.minY, ras.maxY ) )
 
1550
                    goto Fail;
 
1551
        }
 
1552
 
 
1553
    } while ( ras.arc >= ras.arcs );
 
1554
 
 
1555
    ras.lastX = x4;
 
1556
    ras.lastY = y4;
 
1557
 
 
1558
    return SUCCESS;
 
1559
 
 
1560
Fail:
 
1561
    return FAILURE;
 
1562
}
 
1563
 
 
1564
 
 
1565
#undef  SWAP_
 
1566
#define SWAP_( x, y )  do                       \
 
1567
    {                                           \
 
1568
        Long  swap = x;                         \
 
1569
                                                \
 
1570
                                                \
 
1571
        x = y;                                  \
 
1572
        y = swap;                               \
 
1573
    } while ( 0 )
 
1574
 
 
1575
 
 
1576
/*************************************************************************/
 
1577
/*                                                                       */
 
1578
/* <Function>                                                            */
 
1579
/*    Decompose_Curve                                                    */
 
1580
/*                                                                       */
 
1581
/* <Description>                                                         */
 
1582
/*    Scans the outline arays in order to emit individual segments and   */
 
1583
/*    Beziers by calling Line_To() and Bezier_To().  It handles all      */
 
1584
/*    weird cases, like when the first point is off the curve, or when   */
 
1585
/*    there are simply no `on' points in the contour!                    */
 
1586
/*                                                                       */
 
1587
/* <Input>                                                               */
 
1588
/*    first   :: The index of the first point in the contour.            */
 
1589
/*                                                                       */
 
1590
/*    last    :: The index of the last point in the contour.             */
 
1591
/*                                                                       */
 
1592
/*    flipped :: If set, flip the direction of the curve.                */
 
1593
/*                                                                       */
 
1594
/* <Return>                                                              */
 
1595
/*    SUCCESS on success, FAILURE on error.                              */
 
1596
/*                                                                       */
 
1597
static Bool
 
1598
Decompose_Curve( RAS_ARGS UShort  first,
 
1599
                 UShort  last,
 
1600
                 int     flipped )
 
1601
{
 
1602
    QT_FT_Vector   v_last;
 
1603
    QT_FT_Vector   v_control;
 
1604
    QT_FT_Vector   v_start;
 
1605
 
 
1606
    QT_FT_Vector*  points;
 
1607
    QT_FT_Vector*  point;
 
1608
    QT_FT_Vector*  limit;
 
1609
    char*       tags;
 
1610
 
 
1611
    unsigned    tag;       /* current point's state           */
 
1612
 
 
1613
 
 
1614
    points = ras.outline.points;
 
1615
    limit  = points + last;
 
1616
 
 
1617
    v_start.x = SCALED( points[first].x );
 
1618
    v_start.y = SCALED( points[first].y );
 
1619
    v_last.x  = SCALED( points[last].x );
 
1620
    v_last.y  = SCALED( points[last].y );
 
1621
 
 
1622
    if ( flipped )
 
1623
    {
 
1624
        SWAP_( v_start.x, v_start.y );
 
1625
        SWAP_( v_last.x, v_last.y );
 
1626
    }
 
1627
 
 
1628
    v_control = v_start;
 
1629
 
 
1630
    point = points + first;
 
1631
    tags  = ras.outline.tags  + first;
 
1632
    tag   = QT_FT_CURVE_TAG( tags[0] );
 
1633
 
 
1634
    /* A contour cannot start with a cubic control point! */
 
1635
    if ( tag == QT_FT_CURVE_TAG_CUBIC )
 
1636
        goto Invalid_Outline;
 
1637
 
 
1638
    /* check first point to determine origin */
 
1639
    if ( tag == QT_FT_CURVE_TAG_CONIC )
 
1640
    {
 
1641
        /* first point is conic control.  Yes, this happens. */
 
1642
        if ( QT_FT_CURVE_TAG( ras.outline.tags[last] ) == QT_FT_CURVE_TAG_ON )
 
1643
        {
 
1644
            /* start at last point if it is on the curve */
 
1645
            v_start = v_last;
 
1646
            limit--;
 
1647
        }
 
1648
        else
 
1649
        {
 
1650
            /* if both first and last points are conic,         */
 
1651
            /* start at their middle and record its position    */
 
1652
            /* for closure                                      */
 
1653
            v_start.x = ( v_start.x + v_last.x ) / 2;
 
1654
            v_start.y = ( v_start.y + v_last.y ) / 2;
 
1655
 
 
1656
            v_last = v_start;
 
1657
        }
 
1658
        point--;
 
1659
        tags--;
 
1660
    }
 
1661
 
 
1662
    ras.lastX = v_start.x;
 
1663
    ras.lastY = v_start.y;
 
1664
 
 
1665
    while ( point < limit )
 
1666
    {
 
1667
        point++;
 
1668
        tags++;
 
1669
 
 
1670
        tag = QT_FT_CURVE_TAG( tags[0] );
 
1671
 
 
1672
        switch ( tag )
 
1673
        {
 
1674
        case QT_FT_CURVE_TAG_ON:  /* emit a single line_to */
 
1675
        {
 
1676
            Long  x, y;
 
1677
 
 
1678
 
 
1679
            x = SCALED( point->x );
 
1680
            y = SCALED( point->y );
 
1681
            if ( flipped )
 
1682
                SWAP_( x, y );
 
1683
 
 
1684
            if ( Line_To( RAS_VARS x, y ) )
 
1685
                goto Fail;
 
1686
            continue;
 
1687
        }
 
1688
 
 
1689
        case QT_FT_CURVE_TAG_CONIC:  /* consume conic arcs */
 
1690
            v_control.x = SCALED( point[0].x );
 
1691
            v_control.y = SCALED( point[0].y );
 
1692
 
 
1693
            if ( flipped )
 
1694
                SWAP_( v_control.x, v_control.y );
 
1695
 
 
1696
        Do_Conic:
 
1697
            if ( point < limit )
 
1698
            {
 
1699
                QT_FT_Vector  v_middle;
 
1700
                Long       x, y;
 
1701
 
 
1702
 
 
1703
                point++;
 
1704
                tags++;
 
1705
                tag = QT_FT_CURVE_TAG( tags[0] );
 
1706
 
 
1707
                x = SCALED( point[0].x );
 
1708
                y = SCALED( point[0].y );
 
1709
 
 
1710
                if ( flipped )
 
1711
                    SWAP_( x, y );
 
1712
 
 
1713
                if ( tag == QT_FT_CURVE_TAG_ON )
 
1714
                {
 
1715
                    if ( Conic_To( RAS_VARS v_control.x, v_control.y, x, y ) )
 
1716
                        goto Fail;
 
1717
                    continue;
 
1718
                }
 
1719
 
 
1720
                if ( tag != QT_FT_CURVE_TAG_CONIC )
 
1721
                    goto Invalid_Outline;
 
1722
 
 
1723
                v_middle.x = ( v_control.x + x ) / 2;
 
1724
                v_middle.y = ( v_control.y + y ) / 2;
 
1725
 
 
1726
                if ( Conic_To( RAS_VARS v_control.x, v_control.y,
 
1727
                               v_middle.x,  v_middle.y ) )
 
1728
                    goto Fail;
 
1729
 
 
1730
                v_control.x = x;
 
1731
                v_control.y = y;
 
1732
 
 
1733
                goto Do_Conic;
 
1734
            }
 
1735
 
 
1736
            if ( Conic_To( RAS_VARS v_control.x, v_control.y,
 
1737
                           v_start.x,   v_start.y ) )
 
1738
                goto Fail;
 
1739
 
 
1740
            goto Close;
 
1741
 
 
1742
        default:  /* QT_FT_CURVE_TAG_CUBIC */
 
1743
        {
 
1744
            Long  x1, y1, x2, y2, x3, y3;
 
1745
 
 
1746
 
 
1747
            if ( point + 1 > limit                             ||
 
1748
                 QT_FT_CURVE_TAG( tags[1] ) != QT_FT_CURVE_TAG_CUBIC )
 
1749
                goto Invalid_Outline;
 
1750
 
 
1751
            point += 2;
 
1752
            tags  += 2;
 
1753
 
 
1754
            x1 = SCALED( point[-2].x );
 
1755
            y1 = SCALED( point[-2].y );
 
1756
            x2 = SCALED( point[-1].x );
 
1757
            y2 = SCALED( point[-1].y );
 
1758
            x3 = SCALED( point[ 0].x );
 
1759
            y3 = SCALED( point[ 0].y );
 
1760
 
 
1761
            if ( flipped )
 
1762
            {
 
1763
                SWAP_( x1, y1 );
 
1764
                SWAP_( x2, y2 );
 
1765
                SWAP_( x3, y3 );
 
1766
            }
 
1767
 
 
1768
            if ( point <= limit )
 
1769
            {
 
1770
                if ( Cubic_To( RAS_VARS x1, y1, x2, y2, x3, y3 ) )
 
1771
                    goto Fail;
 
1772
                continue;
 
1773
            }
 
1774
 
 
1775
            if ( Cubic_To( RAS_VARS x1, y1, x2, y2, v_start.x, v_start.y ) )
 
1776
                goto Fail;
 
1777
            goto Close;
 
1778
        }
 
1779
        }
 
1780
    }
 
1781
 
 
1782
    /* close the contour with a line segment */
 
1783
    if ( Line_To( RAS_VARS v_start.x, v_start.y ) )
 
1784
        goto Fail;
 
1785
 
 
1786
Close:
 
1787
    return SUCCESS;
 
1788
 
 
1789
Invalid_Outline:
 
1790
    ras.error = Raster_Err_Invalid;
 
1791
 
 
1792
Fail:
 
1793
    return FAILURE;
 
1794
}
 
1795
 
 
1796
 
 
1797
/*************************************************************************/
 
1798
/*                                                                       */
 
1799
/* <Function>                                                            */
 
1800
/*    Convert_Glyph                                                      */
 
1801
/*                                                                       */
 
1802
/* <Description>                                                         */
 
1803
/*    Converts a glyph into a series of segments and arcs and makes a    */
 
1804
/*    profiles list with them.                                           */
 
1805
/*                                                                       */
 
1806
/* <Input>                                                               */
 
1807
/*    flipped :: If set, flip the direction of curve.                    */
 
1808
/*                                                                       */
 
1809
/* <Return>                                                              */
 
1810
/*    SUCCESS on success, FAILURE if any error was encountered during    */
 
1811
/*    rendering.                                                         */
 
1812
/*                                                                       */
 
1813
static Bool
 
1814
Convert_Glyph( RAS_ARGS int  flipped )
 
1815
{
 
1816
    int       i;
 
1817
    unsigned  start;
 
1818
 
 
1819
    PProfile  lastProfile;
 
1820
 
 
1821
 
 
1822
    ras.fProfile = NULL;
 
1823
    ras.fresh    = FALSE;
 
1824
 
 
1825
    ras.maxBuff  = ras.sizeBuff - AlignProfileSize;
 
1826
 
 
1827
    ras.numTurns = 0;
 
1828
 
 
1829
    ras.cProfile         = (PProfile)ras.top;
 
1830
    ras.cProfile->offset = ras.top;
 
1831
    ras.num_Profs        = 0;
 
1832
 
 
1833
    start = 0;
 
1834
 
 
1835
    for ( i = 0; i < ras.outline.n_contours; i++ )
 
1836
    {
 
1837
        ras.state    = Unknown_State;
 
1838
        ras.gProfile = NULL;
 
1839
 
 
1840
        if ( Decompose_Curve( RAS_VARS (unsigned short)start,
 
1841
                              ras.outline.contours[i],
 
1842
                              flipped ) )
 
1843
            return FAILURE;
 
1844
 
 
1845
        start = ras.outline.contours[i] + 1;
 
1846
 
 
1847
        /* Note that ras.gProfile can be nil if the contour was too small */
 
1848
        /* to be drawn.                                                   */
 
1849
 
 
1850
        lastProfile = ras.cProfile;
 
1851
        if ( End_Profile( RAS_VAR ) )
 
1852
            return FAILURE;
 
1853
 
 
1854
    }
 
1855
 
 
1856
    if ( Finalize_Profile_Table( RAS_VAR ) )
 
1857
        return FAILURE;
 
1858
 
 
1859
    return (Bool)( ras.top < ras.maxBuff ? SUCCESS : FAILURE );
 
1860
}
 
1861
 
 
1862
 
 
1863
/*************************************************************************/
 
1864
/*************************************************************************/
 
1865
/**                                                                     **/
 
1866
/**  SCAN-LINE SWEEPS AND DRAWING                                       **/
 
1867
/**                                                                     **/
 
1868
/*************************************************************************/
 
1869
/*************************************************************************/
 
1870
 
 
1871
 
 
1872
/*************************************************************************/
 
1873
/*                                                                       */
 
1874
/*  Init_Linked                                                          */
 
1875
/*                                                                       */
 
1876
/*    Initializes an empty linked list.                                  */
 
1877
/*                                                                       */
 
1878
static void
 
1879
Init_Linked( TProfileList*  l )
 
1880
{
 
1881
    *l = NULL;
 
1882
}
 
1883
 
 
1884
 
 
1885
/*************************************************************************/
 
1886
/*                                                                       */
 
1887
/*  InsNew                                                               */
 
1888
/*                                                                       */
 
1889
/*    Inserts a new profile in a linked list.                            */
 
1890
/*                                                                       */
 
1891
static void
 
1892
InsNew( PProfileList  list,
 
1893
        PProfile      profile )
 
1894
{
 
1895
    PProfile  *old, current;
 
1896
    Long       x;
 
1897
 
 
1898
 
 
1899
    old     = list;
 
1900
    current = *old;
 
1901
    x       = profile->X;
 
1902
 
 
1903
    while ( current )
 
1904
    {
 
1905
        if ( x < current->X )
 
1906
            break;
 
1907
        old     = &current->link;
 
1908
        current = *old;
 
1909
    }
 
1910
 
 
1911
    profile->link = current;
 
1912
    *old          = profile;
 
1913
}
 
1914
 
 
1915
 
 
1916
/*************************************************************************/
 
1917
/*                                                                       */
 
1918
/*  DelOld                                                               */
 
1919
/*                                                                       */
 
1920
/*    Removes an old profile from a linked list.                         */
 
1921
/*                                                                       */
 
1922
static void
 
1923
DelOld( PProfileList  list,
 
1924
        PProfile      profile )
 
1925
{
 
1926
    PProfile  *old, current;
 
1927
 
 
1928
 
 
1929
    old     = list;
 
1930
    current = *old;
 
1931
 
 
1932
    while ( current )
 
1933
    {
 
1934
        if ( current == profile )
 
1935
        {
 
1936
            *old = current->link;
 
1937
            return;
 
1938
        }
 
1939
 
 
1940
        old     = &current->link;
 
1941
        current = *old;
 
1942
    }
 
1943
 
 
1944
    /* we should never get there, unless the profile was not part of */
 
1945
    /* the list.                                                     */
 
1946
}
 
1947
 
 
1948
 
 
1949
/*************************************************************************/
 
1950
/*                                                                       */
 
1951
/*  Sort                                                                 */
 
1952
/*                                                                       */
 
1953
/*    Sorts a trace list.  In 95%, the list is already sorted.  We need  */
 
1954
/*    an algorithm which is fast in this case.  Bubble sort is enough    */
 
1955
/*    and simple.                                                        */
 
1956
/*                                                                       */
 
1957
static void
 
1958
Sort( PProfileList  list )
 
1959
{
 
1960
    PProfile  *old, current, next;
 
1961
 
 
1962
 
 
1963
    /* First, set the new X coordinate of each profile */
 
1964
    current = *list;
 
1965
    while ( current )
 
1966
    {
 
1967
        current->X       = *current->offset;
 
1968
        current->offset += current->flow;
 
1969
        current->height--;
 
1970
        current = current->link;
 
1971
    }
 
1972
 
 
1973
    /* Then sort them */
 
1974
    old     = list;
 
1975
    current = *old;
 
1976
 
 
1977
    if ( !current )
 
1978
        return;
 
1979
 
 
1980
    next = current->link;
 
1981
 
 
1982
    while ( next )
 
1983
    {
 
1984
        if ( current->X <= next->X )
 
1985
        {
 
1986
            old     = &current->link;
 
1987
            current = *old;
 
1988
 
 
1989
            if ( !current )
 
1990
                return;
 
1991
        }
 
1992
        else
 
1993
        {
 
1994
            *old          = next;
 
1995
            current->link = next->link;
 
1996
            next->link    = current;
 
1997
 
 
1998
            old     = list;
 
1999
            current = *old;
 
2000
        }
 
2001
 
 
2002
        next = current->link;
 
2003
    }
 
2004
}
 
2005
 
 
2006
 
 
2007
/*************************************************************************/
 
2008
/*                                                                       */
 
2009
/*  Vertical Sweep Procedure Set                                         */
 
2010
/*                                                                       */
 
2011
/*  These four routines are used during the vertical black/white sweep   */
 
2012
/*  phase by the generic Draw_Sweep() function.                          */
 
2013
/*                                                                       */
 
2014
/*************************************************************************/
 
2015
 
 
2016
static void
 
2017
Vertical_Sweep_Init( RAS_ARGS Short*  min,
 
2018
                     Short*  max )
 
2019
{
 
2020
    QT_FT_UNUSED( min );
 
2021
    QT_FT_UNUSED( max );
 
2022
    QT_FT_UNUSED( raster );
 
2023
}
 
2024
 
 
2025
 
 
2026
static void
 
2027
Vertical_Sweep_Step( RAS_ARG )
 
2028
{
 
2029
    ras.traceOfs += ras.traceIncr;
 
2030
}
 
2031
 
 
2032
 
 
2033
/*************************************************************************/
 
2034
/*                                                                       */
 
2035
/*  Generic Sweep Drawing routine                                        */
 
2036
/*                                                                       */
 
2037
/*************************************************************************/
 
2038
 
 
2039
static Bool
 
2040
Draw_Sweep( RAS_ARG )
 
2041
{
 
2042
    Short         y, y_change, y_height;
 
2043
 
 
2044
    PProfile      P, Q, P_Left, P_Right;
 
2045
 
 
2046
    Short         min_Y, max_Y, top, bottom;
 
2047
 
 
2048
    Long          x1, x2, xs, e1, e2, ix1, ix2;
 
2049
 
 
2050
    short winding;
 
2051
 
 
2052
    TProfileList  waiting;
 
2053
/*     TProfileList  draw_left, draw_right; */
 
2054
 
 
2055
    TProfileList profile_list;
 
2056
 
 
2057
    QT_FT_Span spans[MAX_SPANS];
 
2058
    Int span_count;
 
2059
 
 
2060
    /* Init empty linked lists */
 
2061
 
 
2062
    Init_Linked( &waiting );
 
2063
 
 
2064
/*     Init_Linked( &draw_left  );*/
 
2065
/*     Init_Linked( &draw_right );*/
 
2066
 
 
2067
    Init_Linked( &profile_list );
 
2068
 
 
2069
    /* first, compute min and max Y */
 
2070
 
 
2071
    P     = ras.fProfile;
 
2072
    max_Y = (Short)TRUNC( ras.minY );
 
2073
    min_Y = (Short)TRUNC( ras.maxY );
 
2074
 
 
2075
    while ( P )
 
2076
    {
 
2077
        Q = P->link;
 
2078
 
 
2079
        bottom = (Short)P->start;
 
2080
        top    = (Short)( P->start + P->height - 1 );
 
2081
 
 
2082
        if ( min_Y > bottom ) min_Y = bottom;
 
2083
        if ( max_Y < top    ) max_Y = top;
 
2084
 
 
2085
        P->X = 0;
 
2086
        InsNew( &waiting, P );
 
2087
 
 
2088
        P = Q;
 
2089
    }
 
2090
 
 
2091
    /* Check the Y-turns */
 
2092
    if ( ras.numTurns == 0 )
 
2093
    {
 
2094
        ras.error = Raster_Err_Invalid;
 
2095
        return FAILURE;
 
2096
    }
 
2097
 
 
2098
    /* Now inits the sweep */
 
2099
 
 
2100
    ras.Proc_Sweep_Init( RAS_VARS &min_Y, &max_Y );
 
2101
 
 
2102
    /* Then compute the distance of each profile from min_Y */
 
2103
 
 
2104
    P = waiting;
 
2105
 
 
2106
    while ( P )
 
2107
    {
 
2108
        P->countL = (UShort)( P->start - min_Y );
 
2109
        P = P->link;
 
2110
    }
 
2111
 
 
2112
    /* Let's go */
 
2113
 
 
2114
    y        = min_Y;
 
2115
    y_height = 0;
 
2116
 
 
2117
    if ( ras.numTurns > 0 &&
 
2118
         ras.sizeBuff[-ras.numTurns] == min_Y )
 
2119
        ras.numTurns--;
 
2120
 
 
2121
    while ( ras.numTurns > 0 )
 
2122
    {
 
2123
        /* look in the waiting list for new activations */
 
2124
 
 
2125
        P = waiting;
 
2126
 
 
2127
        while ( P )
 
2128
        {
 
2129
            Q = P->link;
 
2130
            P->countL -= y_height;
 
2131
            if ( P->countL == 0 )
 
2132
            {
 
2133
                DelOld( &waiting, P );
 
2134
                if (P->flow) {
 
2135
                    InsNew( &profile_list, P );
 
2136
                }
 
2137
            }
 
2138
 
 
2139
            P = Q;
 
2140
        }
 
2141
 
 
2142
        /* Sort the drawing lists */
 
2143
        Sort(&profile_list);
 
2144
 
 
2145
        y_change = (Short)ras.sizeBuff[-ras.numTurns--];
 
2146
        y_height = (Short)( y_change - y );
 
2147
 
 
2148
        while ( y < y_change )
 
2149
        {
 
2150
            span_count = 0;
 
2151
 
 
2152
#ifdef Q_RASTER_DEBUG
 
2153
            Q = profile_list;
 
2154
            printf("y=%d ::", y);
 
2155
            while (Q) {
 
2156
                printf(" %.2f", Q->X / 64.);
 
2157
                Q = Q->link;
 
2158
            }
 
2159
            printf("\n");
 
2160
#endif
 
2161
 
 
2162
            /* Let's trace */
 
2163
            P_Left = profile_list;
 
2164
 
 
2165
            while (P_Left) {
 
2166
                winding = P_Left->flow;
 
2167
 
 
2168
                if (ras.odd_even) {
 
2169
                    P_Right = P_Left->link;
 
2170
                } else { /* Winding fill; */
 
2171
                    P_Right = P_Left;
 
2172
                    while (winding && P_Right->link) {
 
2173
                        P_Right = P_Right->link;
 
2174
                        winding += P_Right->flow;
 
2175
                    }
 
2176
                }
 
2177
 
 
2178
                /* We need an even number of intersections, otherwise there is something */
 
2179
                /* wrong... */
 
2180
                if (!P_Right)
 
2181
                    return Raster_Err_Invalid;
 
2182
 
 
2183
                x1 = P_Left ->X;
 
2184
                x2 = P_Right->X;
 
2185
 
 
2186
                if ( x1 > x2 ) {
 
2187
                    xs = x1;
 
2188
                    x1 = x2;
 
2189
                    x2 = xs;
 
2190
                }
 
2191
 
 
2192
                if ( x2 - x1 <= ras.precision ) {
 
2193
                    e1 = FLOOR( x1 );
 
2194
                    e2 = CEILING( x2 );
 
2195
                }
 
2196
 
 
2197
                ix1 = MAX(TRUNC(CEILING(x1)), ras.minX_dev);
 
2198
                ix2 = MIN(TRUNC(FLOOR(x2-1)) + 1, ras.maxX_dev);
 
2199
                if (ix2 > ix1) {
 
2200
                    spans[span_count].x = (short) ix1;
 
2201
                    spans[span_count].len = (short) (ix2 - ix1);
 
2202
                    spans[span_count].coverage = 255;
 
2203
                    ++span_count;
 
2204
 
 
2205
                    if (span_count == MAX_SPANS) {
 
2206
                        ras.black_spans(y, span_count, spans, ras.user_data);
 
2207
                        span_count = 0;
 
2208
                    }
 
2209
                }
 
2210
                P_Left = P_Right->link;
 
2211
            }
 
2212
            ras.Proc_Sweep_Step( RAS_VAR );
 
2213
 
 
2214
            if (span_count > 0)
 
2215
                ras.black_spans(y, span_count, spans, ras.user_data);
 
2216
 
 
2217
            y++;
 
2218
 
 
2219
            if (y < y_change)
 
2220
                Sort( &profile_list );
 
2221
        }
 
2222
 
 
2223
        /* Now finalize the profiles that needs it */
 
2224
 
 
2225
        P = profile_list;
 
2226
        while ( P )
 
2227
        {
 
2228
            Q = P->link;
 
2229
            if ( P->height == 0 )
 
2230
                DelOld( &profile_list, P );
 
2231
            P = Q;
 
2232
        }
 
2233
 
 
2234
    }
 
2235
 
 
2236
    /* for gray-scaling, flushes the bitmap scanline cache */
 
2237
    while ( y <= max_Y )
 
2238
    {
 
2239
        ras.Proc_Sweep_Step( RAS_VAR );
 
2240
        y++;
 
2241
    }
 
2242
 
 
2243
    return SUCCESS;
 
2244
}
 
2245
 
 
2246
 
 
2247
/*************************************************************************/
 
2248
/*                                                                       */
 
2249
/* <Function>                                                            */
 
2250
/*    Render_Single_Pass                                                 */
 
2251
/*                                                                       */
 
2252
/* <Description>                                                         */
 
2253
/*    Performs one sweep with sub-banding.                               */
 
2254
/*                                                                       */
 
2255
/* <Input>                                                               */
 
2256
/*    flipped :: If set, flip the direction of the outline.              */
 
2257
/*                                                                       */
 
2258
/* <Return>                                                              */
 
2259
/*    Renderer error code.                                               */
 
2260
/*                                                                       */
 
2261
static int
 
2262
Render_Single_Pass( RAS_ARGS Bool  flipped )
 
2263
{
 
2264
    Short  i, j, k;
 
2265
 
 
2266
 
 
2267
    while ( ras.band_top >= 0 )
 
2268
    {
 
2269
        ras.maxY = (Long)ras.band_stack[ras.band_top].y_max * ras.precision;
 
2270
        ras.minY = (Long)ras.band_stack[ras.band_top].y_min * ras.precision;
 
2271
 
 
2272
        ras.top = ras.buff;
 
2273
 
 
2274
        ras.error = Raster_Err_None;
 
2275
 
 
2276
        if ( Convert_Glyph( RAS_VARS flipped ) )
 
2277
        {
 
2278
            if ( ras.error != Raster_Err_Overflow )
 
2279
                return FAILURE;
 
2280
 
 
2281
            ras.error = Raster_Err_None;
 
2282
 
 
2283
            /* sub-banding */
 
2284
 
 
2285
#ifdef DEBUG_RASTER
 
2286
            ClearBand( RAS_VARS TRUNC( ras.minY ), TRUNC( ras.maxY ) );
 
2287
#endif
 
2288
 
 
2289
            i = ras.band_stack[ras.band_top].y_min;
 
2290
            j = ras.band_stack[ras.band_top].y_max;
 
2291
 
 
2292
            k = (Short)( ( i + j ) / 2 );
 
2293
 
 
2294
            if ( ras.band_top >= 7 || k < i )
 
2295
            {
 
2296
                ras.band_top = 0;
 
2297
                ras.error    = Raster_Err_Invalid;
 
2298
 
 
2299
                return ras.error;
 
2300
            }
 
2301
 
 
2302
            ras.band_stack[ras.band_top + 1].y_min = k;
 
2303
            ras.band_stack[ras.band_top + 1].y_max = j;
 
2304
 
 
2305
            ras.band_stack[ras.band_top].y_max = (Short)( k - 1 );
 
2306
 
 
2307
            ras.band_top++;
 
2308
        }
 
2309
        else
 
2310
        {
 
2311
            if ( ras.fProfile )
 
2312
                if ( Draw_Sweep( RAS_VAR ) )
 
2313
                    return ras.error;
 
2314
            ras.band_top--;
 
2315
        }
 
2316
    }
 
2317
 
 
2318
    return SUCCESS;
 
2319
}
 
2320
 
 
2321
 
 
2322
/*************************************************************************/
 
2323
/*                                                                       */
 
2324
/* <Function>                                                            */
 
2325
/*    Render_Glyph                                                       */
 
2326
/*                                                                       */
 
2327
/* <Description>                                                         */
 
2328
/*    Renders a glyph in a bitmap.  Sub-banding if needed.               */
 
2329
/*                                                                       */
 
2330
/* <Return>                                                              */
 
2331
/*    FreeType error code.  0 means success.                             */
 
2332
/*                                                                       */
 
2333
QT_FT_LOCAL_DEF( QT_FT_Error )
 
2334
    Render_Glyph( RAS_ARG )
 
2335
{
 
2336
    QT_FT_Error  error;
 
2337
 
 
2338
    QT_FT_TRACE6("Render_Glyph:\n");
 
2339
 
 
2340
    Set_High_Precision( RAS_VARS FALSE );
 
2341
    ras.scale_shift    = ras.precision_shift;
 
2342
 
 
2343
    /* Vertical Sweep */
 
2344
    ras.Proc_Sweep_Init = Vertical_Sweep_Init;
 
2345
    ras.Proc_Sweep_Step = Vertical_Sweep_Step;
 
2346
 
 
2347
    ras.band_top            = 0;
 
2348
    ras.band_stack[0].y_min = (short) (ras.clip_box.yMin);
 
2349
    ras.band_stack[0].y_max = (short) (ras.clip_box.yMax - ras.clip_box.yMin - 1);
 
2350
    ras.bWidth = (unsigned short) (ras.clip_box.xMax - ras.clip_box.xMin - 1);
 
2351
 
 
2352
    if ( ( error = Render_Single_Pass( RAS_VARS 0 ) ) != 0 )
 
2353
        return error;
 
2354
 
 
2355
    QT_FT_TRACE6("End Render_Glyph:\n\n");
 
2356
 
 
2357
    return Raster_Err_Ok;
 
2358
}
 
2359
 
 
2360
 
 
2361
 
 
2362
QT_FT_LOCAL_DEF( QT_FT_Error )
 
2363
    Render_Gray_Glyph( RAS_ARG )
 
2364
{
 
2365
    QT_FT_UNUSED_RASTER;
 
2366
 
 
2367
    return Raster_Err_Cannot_Render_Glyph;
 
2368
}
 
2369
 
 
2370
 
 
2371
/**** RASTER OBJECT CREATION: In standalone mode, we simply use *****/
 
2372
/****                         a static object.                  *****/
 
2373
 
 
2374
 
 
2375
/* #### a static instance might be a bad idea... */
 
2376
static int
 
2377
qt_ft_black_new( void*      memory,
 
2378
              QT_FT_Raster  *araster )
 
2379
{
 
2380
    static TRaster_Instance the_raster;
 
2381
    QT_FT_UNUSED( memory );
 
2382
 
 
2383
    *araster = (QT_FT_Raster) &the_raster;
 
2384
    QT_FT_MEM_ZERO( &the_raster, sizeof ( the_raster ) );
 
2385
 
 
2386
    return 0;
 
2387
}
 
2388
 
 
2389
 
 
2390
static void
 
2391
qt_ft_black_done( QT_FT_Raster  raster )
 
2392
{
 
2393
    QT_FT_UNUSED( raster );
 
2394
    /* nothing */
 
2395
/*     raster->init = 0; */
 
2396
}
 
2397
 
 
2398
 
 
2399
static void
 
2400
qt_ft_black_reset( TRaster_Instance*  raster,
 
2401
                const char*        pool_base,
 
2402
                long               pool_size )
 
2403
{
 
2404
    if ( raster && pool_base && pool_size >= 4096 )
 
2405
    {
 
2406
        /* save the pool */
 
2407
        raster->buff     = (PLong)pool_base;
 
2408
        raster->sizeBuff = raster->buff + pool_size / sizeof ( Long );
 
2409
    }
 
2410
}
 
2411
 
 
2412
 
 
2413
static void
 
2414
qt_ft_black_set_mode( TRaster_Instance*  raster,
 
2415
                   unsigned long      mode,
 
2416
                   const char*        palette )
 
2417
{
 
2418
 
 
2419
    QT_FT_UNUSED( raster );
 
2420
    QT_FT_UNUSED( mode );
 
2421
    QT_FT_UNUSED( palette );
 
2422
 
 
2423
}
 
2424
 
 
2425
 
 
2426
static int
 
2427
qt_ft_black_render( TRaster_Instance*  raster,
 
2428
                 QT_FT_Raster_Params*  params )
 
2429
{
 
2430
    QT_FT_Outline*  outline    = (QT_FT_Outline*)params->source;
 
2431
 
 
2432
    if ( !raster || !raster->buff || !raster->sizeBuff )
 
2433
        return Raster_Err_Not_Ini;
 
2434
 
 
2435
    /* return immediately if the outline is empty */
 
2436
    if ( outline->n_points == 0 || outline->n_contours <= 0 )
 
2437
        return Raster_Err_None;
 
2438
 
 
2439
    if ( !outline || !outline->contours || !outline->points )
 
2440
        return Raster_Err_Invalid;
 
2441
 
 
2442
    if ( outline->n_points != outline->contours[outline->n_contours - 1] + 1 )
 
2443
        return Raster_Err_Invalid;
 
2444
 
 
2445
    if (!params->black_spans)
 
2446
        return Raster_Err_Invalid;
 
2447
    ras.black_spans = params->black_spans;
 
2448
    ras.user_data = params->user;
 
2449
    ras.outline  = *outline;
 
2450
 
 
2451
    if (!(params->flags & QT_FT_RASTER_FLAG_CLIP))
 
2452
        return Raster_Err_Invalid;
 
2453
 
 
2454
    ras.minX_dev = (short)params->clip_box.xMin;
 
2455
    ras.maxX_dev = (short)params->clip_box.xMax;
 
2456
 
 
2457
    ras.clip_box = params->clip_box;
 
2458
 
 
2459
    ras.odd_even = (outline->flags & QT_FT_OUTLINE_EVEN_ODD_FILL);
 
2460
 
 
2461
    return ( ( params->flags & QT_FT_RASTER_FLAG_AA )
 
2462
             ? Render_Gray_Glyph( raster )
 
2463
             : Render_Glyph( raster ) );
 
2464
}
 
2465
 
 
2466
 
 
2467
const QT_FT_Raster_Funcs  qt_ft_standard_raster =
 
2468
{
 
2469
    QT_FT_GLYPH_FORMAT_OUTLINE,
 
2470
    (QT_FT_Raster_New_Func)     qt_ft_black_new,
 
2471
    (QT_FT_Raster_Reset_Func)   qt_ft_black_reset,
 
2472
    (QT_FT_Raster_Set_Mode_Func)qt_ft_black_set_mode,
 
2473
    (QT_FT_Raster_Render_Func)  qt_ft_black_render,
 
2474
    (QT_FT_Raster_Done_Func)    qt_ft_black_done
 
2475
};
 
2476
 
 
2477
 
 
2478
/* END */