< prev index next >

src/java.desktop/share/native/libfreetype/src/smooth/ftgrays.c

Print this page




  28    *   same directory
  29    *
  30    * - compile `ftgrays' with the STANDALONE_ macro defined, as in
  31    *
  32    *     cc -c -DSTANDALONE_ ftgrays.c
  33    *
  34    * The renderer can be initialized with a call to
  35    * `ft_gray_raster.raster_new'; an anti-aliased bitmap can be generated
  36    * with a call to `ft_gray_raster.raster_render'.
  37    *
  38    * See the comments and documentation in the file `ftimage.h' for more
  39    * details on how the raster works.
  40    *
  41    */
  42 
  43   /**************************************************************************
  44    *
  45    * This is a new anti-aliasing scan-converter for FreeType 2.  The
  46    * algorithm used here is _very_ different from the one in the standard
  47    * `ftraster' module.  Actually, `ftgrays' computes the _exact_
  48    * coverage of the outline on each pixel cell.
  49    *
  50    * It is based on ideas that I initially found in Raph Levien's
  51    * excellent LibArt graphics library (see https://www.levien.com/libart
  52    * for more information, though the web pages do not tell anything
  53    * about the renderer; you'll have to dive into the source code to
  54    * understand how it works).
  55    *
  56    * Note, however, that this is a _very_ different implementation
  57    * compared to Raph's.  Coverage information is stored in a very
  58    * different way, and I don't use sorted vector paths.  Also, it doesn't
  59    * use floating point values.
  60    *








  61    * This renderer has the following advantages:
  62    *
  63    * - It doesn't need an intermediate bitmap.  Instead, one can supply a
  64    *   callback function that will be called by the renderer to draw gray
  65    *   spans on any target surface.  You can thus do direct composition on
  66    *   any kind of bitmap, provided that you give the renderer the right
  67    *   callback.
  68    *
  69    * - A perfect anti-aliaser, i.e., it computes the _exact_ coverage on
  70    *   each pixel cell.
  71    *
  72    * - It performs a single pass on the outline (the `standard' FT2
  73    *   renderer makes two passes).
  74    *
  75    * - It can easily be modified to render to _any_ number of gray levels
  76    *   cheaply.
  77    *
  78    * - For small (< 20) pixel sizes, it is faster than the standard
  79    *   renderer.
  80    *
  81    */
  82 
  83 
  84   /**************************************************************************
  85    *
  86    * The macro FT_COMPONENT is used in trace mode.  It is an implicit
  87    * parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log
  88    * messages during execution.
  89    */
  90 #undef  FT_COMPONENT
  91 #define FT_COMPONENT  smooth
  92 
  93 
  94 #ifdef STANDALONE_
  95 
  96 
  97   /* The size in bytes of the render pool used by the scan-line converter  */
  98   /* to do all of its work.                                                */


 310 
 311 #define RAS_ARG   gray_PWorker  worker
 312 #define RAS_ARG_  gray_PWorker  worker,
 313 
 314 #define RAS_VAR   worker
 315 #define RAS_VAR_  worker,
 316 
 317 #else /* FT_STATIC_RASTER */
 318 
 319 #define RAS_ARG   void
 320 #define RAS_ARG_  /* empty */
 321 #define RAS_VAR   /* empty */
 322 #define RAS_VAR_  /* empty */
 323 
 324 #endif /* FT_STATIC_RASTER */
 325 
 326 
 327   /* must be at least 6 bits! */
 328 #define PIXEL_BITS  8
 329 
 330 #undef FLOOR
 331 #undef CEILING
 332 #undef TRUNC
 333 #undef SCALED
 334 
 335 #define ONE_PIXEL       ( 1 << PIXEL_BITS )
 336 #define TRUNC( x )      ( (TCoord)( (x) >> PIXEL_BITS ) )
 337 #define SUBPIXELS( x )  ( (TPos)(x) * ONE_PIXEL )
 338 #define FLOOR( x )      ( (x) & -ONE_PIXEL )
 339 #define CEILING( x )    ( ( (x) + ONE_PIXEL - 1 ) & -ONE_PIXEL )
 340 #define ROUND( x )      ( ( (x) + ONE_PIXEL / 2 ) & -ONE_PIXEL )
 341 
 342 #if PIXEL_BITS >= 6
 343 #define UPSCALE( x )    ( (x) * ( ONE_PIXEL >> 6 ) )
 344 #define DOWNSCALE( x )  ( (x) >> ( PIXEL_BITS - 6 ) )
 345 #else
 346 #define UPSCALE( x )    ( (x) >> ( 6 - PIXEL_BITS ) )
 347 #define DOWNSCALE( x )  ( (x) * ( 64 >> PIXEL_BITS ) )
 348 #endif
 349 
 350 
 351   /* Compute `dividend / divisor' and return both its quotient and     */
 352   /* remainder, cast to a specific type.  This macro also ensures that */
 353   /* the remainder is always positive.  We use the remainder to keep   */
 354   /* track of accumulating errors and compensate for them.             */
 355 #define FT_DIV_MOD( type, dividend, divisor, quotient, remainder ) \
 356   FT_BEGIN_STMNT                                                   \
 357     (quotient)  = (type)( (dividend) / (divisor) );                \
 358     (remainder) = (type)( (dividend) % (divisor) );                \
 359     if ( (remainder) < 0 )                                         \
 360     {                                                              \


 372 #undef FT_DIV_MOD
 373 #define FT_DIV_MOD( type, dividend, divisor, quotient, remainder ) \
 374   FT_BEGIN_STMNT                                                   \
 375     (quotient)  = (type)( (dividend) / (divisor) );                \
 376     (remainder) = (type)( (dividend) - (quotient) * (divisor) );   \
 377     if ( (remainder) < 0 )                                         \
 378     {                                                              \
 379       (quotient)--;                                                \
 380       (remainder) += (type)(divisor);                              \
 381     }                                                              \
 382   FT_END_STMNT
 383 #endif /* __arm__ */
 384 
 385 
 386   /* These macros speed up repetitive divisions by replacing them */
 387   /* with multiplications and right shifts.                       */
 388 #define FT_UDIVPREP( c, b )                                        \
 389   long  b ## _r = c ? (long)( FT_ULONG_MAX >> PIXEL_BITS ) / ( b ) \
 390                     : 0
 391 #define FT_UDIV( a, b )                                        \
 392   ( ( (unsigned long)( a ) * (unsigned long)( b ## _r ) ) >>   \
 393     ( sizeof( long ) * FT_CHAR_BIT - PIXEL_BITS ) )
 394 
 395 
 396   /**************************************************************************
 397    *
 398    * TYPE DEFINITIONS
 399    */
 400 
 401   /* don't change the following types to FT_Int or FT_Pos, since we might */
 402   /* need to define them to "float" or "double" when experimenting with   */
 403   /* new algorithms                                                       */
 404 
 405   typedef long  TPos;     /* subpixel coordinate               */
 406   typedef int   TCoord;   /* integer scanline/pixel coordinate */
 407   typedef int   TArea;    /* cell areas, coordinate products   */
 408 
 409 
 410   typedef struct TCell_*  PCell;
 411 
 412   typedef struct  TCell_


 415     TCoord  cover; /* same with gray_TWorker.cover */
 416     TArea   area;
 417     PCell   next;
 418 
 419   } TCell;
 420 
 421   typedef struct TPixmap_
 422   {
 423     unsigned char*  origin;  /* pixmap origin at the bottom-left */
 424     int             pitch;   /* pitch to go down one row */
 425 
 426   } TPixmap;
 427 
 428   /* maximum number of gray cells in the buffer */
 429 #if FT_RENDER_POOL_SIZE > 2048
 430 #define FT_MAX_GRAY_POOL  ( FT_RENDER_POOL_SIZE / sizeof ( TCell ) )
 431 #else
 432 #define FT_MAX_GRAY_POOL  ( 2048 / sizeof ( TCell ) )
 433 #endif
 434 



 435 
 436 #if defined( _MSC_VER )      /* Visual C++ (and Intel C++) */
 437   /* We disable the warning `structure was padded due to   */
 438   /* __declspec(align())' in order to compile cleanly with */
 439   /* the maximum level of warnings.                        */
 440 #pragma warning( push )
 441 #pragma warning( disable : 4324 )
 442 #endif /* _MSC_VER */
 443 
 444   typedef struct  gray_TWorker_
 445   {
 446     ft_jmp_buf  jump_buffer;
 447 
 448     TCoord  ex, ey;
 449     TCoord  min_ex, max_ex;
 450     TCoord  min_ey, max_ey;
 451 
 452     TArea   area;
 453     TCoord  cover;
 454     int     invalid;
 455 
 456     PCell*      ycells;
 457     PCell       cells;
 458     FT_PtrDist  max_cells;
 459     FT_PtrDist  num_cells;
 460 
 461     TPos    x,  y;
 462 
 463     FT_Outline  outline;
 464     TPixmap     target;
 465 
 466     FT_Raster_Span_Func  render_span;
 467     void*                render_span_data;


 468 
 469   } gray_TWorker, *gray_PWorker;
 470 
 471 #if defined( _MSC_VER )
 472 #pragma warning( pop )
 473 #endif
 474 
 475 
 476 #ifndef FT_STATIC_RASTER
 477 #define ras  (*worker)
 478 #else
 479   static gray_TWorker  ras;
 480 #endif
 481 
 482 
 483   typedef struct gray_TRaster_
 484   {
 485     void*         memory;
 486 
 487   } gray_TRaster, *gray_PRaster;


 499 
 500     for ( y = ras.min_ey; y < ras.max_ey; y++ )
 501     {
 502       PCell  cell = ras.ycells[y - ras.min_ey];
 503 
 504 
 505       printf( "%3d:", y );
 506 
 507       for ( ; cell != NULL; cell = cell->next )
 508         printf( " (%3d, c:%4d, a:%6d)",
 509                 cell->x, cell->cover, cell->area );
 510       printf( "\n" );
 511     }
 512   }
 513 
 514 #endif /* FT_DEBUG_LEVEL_TRACE */
 515 
 516 
 517   /**************************************************************************
 518    *
 519    * Record the current cell in the table.
 520    */
 521   static void
 522   gray_record_cell( RAS_ARG )
 523   {
 524     PCell  *pcell, cell;
 525     TCoord  x = ras.ex;
 526 
 527 
 528     pcell = &ras.ycells[ras.ey - ras.min_ey];
 529     for (;;)
 530     {
 531       cell = *pcell;
 532       if ( !cell || cell->x > x )
 533         break;
 534 
 535       if ( cell->x == x )
 536         goto Found;
 537 
 538       pcell = &cell->next;
 539     }
 540 
 541     if ( ras.num_cells >= ras.max_cells )
 542       ft_longjmp( ras.jump_buffer, 1 );
 543 
 544     /* insert new cell */
 545     cell        = ras.cells + ras.num_cells++;
 546     cell->x     = x;
 547     cell->area  = ras.area;
 548     cell->cover = ras.cover;
 549 
 550     cell->next  = *pcell;
 551     *pcell      = cell;
 552 


 560 
 561 
 562   /**************************************************************************
 563    *
 564    * Set the current cell to a new position.
 565    */
 566   static void
 567   gray_set_cell( RAS_ARG_ TCoord  ex,
 568                           TCoord  ey )
 569   {
 570     /* Move the cell pointer to a new position.  We set the `invalid'      */
 571     /* flag to indicate that the cell isn't part of those we're interested */
 572     /* in during the render phase.  This means that:                       */
 573     /*                                                                     */
 574     /* . the new vertical position must be within min_ey..max_ey-1.        */
 575     /* . the new horizontal position must be strictly less than max_ex     */
 576     /*                                                                     */
 577     /* Note that if a cell is to the left of the clipping region, it is    */
 578     /* actually set to the (min_ex-1) horizontal position.                 */
 579 
 580     if ( ex < ras.min_ex )
 581       ex = ras.min_ex - 1;
 582 
 583     /* record the current one if it is valid and substantial */
 584     if ( !ras.invalid && ( ras.area || ras.cover ) )
 585       gray_record_cell( RAS_VAR );
 586 
 587     ras.area  = 0;
 588     ras.cover = 0;
 589     ras.ex    = ex;
 590     ras.ey    = ey;
 591 
 592     ras.invalid = ( ey >= ras.max_ey || ey < ras.min_ey ||
 593                     ex >= ras.max_ex );
 594   }
 595 
 596 
 597 #ifndef FT_LONG64
 598 
 599   /**************************************************************************
 600    *
 601    * Render a scanline as one or more cells.
 602    */
 603   static void
 604   gray_render_scanline( RAS_ARG_ TCoord  ey,
 605                                  TPos    x1,
 606                                  TCoord  y1,
 607                                  TPos    x2,
 608                                  TCoord  y2 )
 609   {
 610     TCoord  ex1, ex2, fx1, fx2, first, dy, delta, mod;
 611     TPos    p, dx;
 612     int     incr;
 613 
 614 
 615     ex1 = TRUNC( x1 );
 616     ex2 = TRUNC( x2 );
 617 
 618     /* trivial case.  Happens often */
 619     if ( y1 == y2 )
 620     {
 621       gray_set_cell( RAS_VAR_ ex2, ey );
 622       return;
 623     }
 624 
 625     fx1   = (TCoord)( x1 - SUBPIXELS( ex1 ) );
 626     fx2   = (TCoord)( x2 - SUBPIXELS( ex2 ) );
 627 
 628     /* everything is located in a single cell.  That is easy! */
 629     /*                                                        */
 630     if ( ex1 == ex2 )
 631       goto End;
 632 
 633     /* ok, we'll have to render a run of adjacent cells on the same */
 634     /* scanline...                                                  */
 635     /*                                                              */
 636     dx = x2 - x1;
 637     dy = y2 - y1;
 638 
 639     if ( dx > 0 )
 640     {
 641       p     = ( ONE_PIXEL - fx1 ) * dy;
 642       first = ONE_PIXEL;
 643       incr  = 1;
 644     }
 645     else
 646     {
 647       p     = fx1 * dy;
 648       first = 0;
 649       incr  = -1;
 650       dx    = -dx;
 651     }
 652 



 653     FT_DIV_MOD( TCoord, p, dx, delta, mod );
 654 
 655     ras.area  += (TArea)( ( fx1 + first ) * delta );
 656     ras.cover += delta;
 657     y1        += delta;
 658     ex1       += incr;
 659     gray_set_cell( RAS_VAR_ ex1, ey );
 660 
 661     if ( ex1 != ex2 )
 662     {
 663       TCoord  lift, rem;
 664 
 665 
 666       p = ONE_PIXEL * dy;
 667       FT_DIV_MOD( TCoord, p, dx, lift, rem );
 668 
 669       do
 670       {
 671         delta = lift;
 672         mod  += rem;


 698    *
 699    * Render a given line as a series of scanlines.
 700    */
 701   static void
 702   gray_render_line( RAS_ARG_ TPos  to_x,
 703                              TPos  to_y )
 704   {
 705     TCoord  ey1, ey2, fy1, fy2, first, delta, mod;
 706     TPos    p, dx, dy, x, x2;
 707     int     incr;
 708 
 709 
 710     ey1 = TRUNC( ras.y );
 711     ey2 = TRUNC( to_y );     /* if (ey2 >= ras.max_ey) ey2 = ras.max_ey-1; */
 712 
 713     /* perform vertical clipping */
 714     if ( ( ey1 >= ras.max_ey && ey2 >= ras.max_ey ) ||
 715          ( ey1 <  ras.min_ey && ey2 <  ras.min_ey ) )
 716       goto End;
 717 
 718     fy1 = (TCoord)( ras.y - SUBPIXELS( ey1 ) );
 719     fy2 = (TCoord)( to_y - SUBPIXELS( ey2 ) );
 720 
 721     /* everything is on a single scanline */
 722     if ( ey1 == ey2 )
 723     {
 724       gray_render_scanline( RAS_VAR_ ey1, ras.x, fy1, to_x, fy2 );
 725       goto End;
 726     }
 727 
 728     dx = to_x - ras.x;
 729     dy = to_y - ras.y;
 730 
 731     /* vertical line - avoid calling gray_render_scanline */
 732     if ( dx == 0 )
 733     {
 734       TCoord  ex     = TRUNC( ras.x );
 735       TCoord  two_fx = (TCoord)( ( ras.x - SUBPIXELS( ex ) ) << 1 );
 736       TArea   area;
 737 
 738 
 739       if ( dy > 0)
 740       {
 741         first = ONE_PIXEL;
 742         incr  = 1;
 743       }
 744       else
 745       {
 746         first = 0;
 747         incr  = -1;
 748       }
 749 
 750       delta      = first - fy1;
 751       ras.area  += (TArea)two_fx * delta;
 752       ras.cover += delta;
 753       ey1       += incr;
 754 
 755       gray_set_cell( RAS_VAR_ ex, ey1 );


 770       ras.cover += delta;
 771 
 772       goto End;
 773     }
 774 
 775     /* ok, we have to render several scanlines */
 776     if ( dy > 0)
 777     {
 778       p     = ( ONE_PIXEL - fy1 ) * dx;
 779       first = ONE_PIXEL;
 780       incr  = 1;
 781     }
 782     else
 783     {
 784       p     = fy1 * dx;
 785       first = 0;
 786       incr  = -1;
 787       dy    = -dy;
 788     }
 789 


 790     FT_DIV_MOD( TCoord, p, dy, delta, mod );
 791 
 792     x = ras.x + delta;
 793     gray_render_scanline( RAS_VAR_ ey1, ras.x, fy1, x, first );
 794 
 795     ey1 += incr;
 796     gray_set_cell( RAS_VAR_ TRUNC( x ), ey1 );
 797 
 798     if ( ey1 != ey2 )
 799     {
 800       TCoord  lift, rem;
 801 
 802 
 803       p    = ONE_PIXEL * dx;
 804       FT_DIV_MOD( TCoord, p, dy, lift, rem );
 805 
 806       do
 807       {
 808         delta = lift;
 809         mod  += rem;


 826 
 827     gray_render_scanline( RAS_VAR_ ey1,
 828                                    x, ONE_PIXEL - first,
 829                                    to_x, fy2 );
 830 
 831   End:
 832     ras.x       = to_x;
 833     ras.y       = to_y;
 834   }
 835 
 836 #else
 837 
 838   /**************************************************************************
 839    *
 840    * Render a straight line across multiple cells in any direction.
 841    */
 842   static void
 843   gray_render_line( RAS_ARG_ TPos  to_x,
 844                              TPos  to_y )
 845   {
 846     TPos    dx, dy, fx1, fy1, fx2, fy2;
 847     TCoord  ex1, ex2, ey1, ey2;

 848 
 849 
 850     ey1 = TRUNC( ras.y );
 851     ey2 = TRUNC( to_y );
 852 
 853     /* perform vertical clipping */
 854     if ( ( ey1 >= ras.max_ey && ey2 >= ras.max_ey ) ||
 855          ( ey1 <  ras.min_ey && ey2 <  ras.min_ey ) )
 856       goto End;
 857 
 858     ex1 = TRUNC( ras.x );
 859     ex2 = TRUNC( to_x );
 860 
 861     fx1 = ras.x - SUBPIXELS( ex1 );
 862     fy1 = ras.y - SUBPIXELS( ey1 );
 863 
 864     dx = to_x - ras.x;
 865     dy = to_y - ras.y;
 866 
 867     if ( ex1 == ex2 && ey1 == ey2 )       /* inside one cell */
 868       ;
 869     else if ( dy == 0 ) /* ex1 != ex2 */  /* any horizontal line */
 870     {
 871       ex1 = ex2;
 872       gray_set_cell( RAS_VAR_ ex1, ey1 );
 873     }
 874     else if ( dx == 0 )
 875     {
 876       if ( dy > 0 )                       /* vertical line up */
 877         do
 878         {
 879           fy2 = ONE_PIXEL;
 880           ras.cover += ( fy2 - fy1 );
 881           ras.area  += ( fy2 - fy1 ) * fx1 * 2;
 882           fy1 = 0;
 883           ey1++;
 884           gray_set_cell( RAS_VAR_ ex1, ey1 );
 885         } while ( ey1 != ey2 );
 886       else                                /* vertical line down */
 887         do
 888         {
 889           fy2 = 0;
 890           ras.cover += ( fy2 - fy1 );
 891           ras.area  += ( fy2 - fy1 ) * fx1 * 2;
 892           fy1 = ONE_PIXEL;
 893           ey1--;
 894           gray_set_cell( RAS_VAR_ ex1, ey1 );
 895         } while ( ey1 != ey2 );
 896     }
 897     else                                  /* any other line */
 898     {
 899       TPos  prod = dx * fy1 - dy * fx1;
 900       FT_UDIVPREP( ex1 != ex2, dx );
 901       FT_UDIVPREP( ey1 != ey2, dy );
 902 
 903 
 904       /* The fundamental value `prod' determines which side and the  */
 905       /* exact coordinate where the line exits current cell.  It is  */
 906       /* also easily updated when moving from one cell to the next.  */
 907       do
 908       {
 909         if      ( prod                                   <= 0 &&
 910                   prod - dx * ONE_PIXEL                  >  0 ) /* left */
 911         {
 912           fx2 = 0;
 913           fy2 = (TPos)FT_UDIV( -prod, -dx );
 914           prod -= dy * ONE_PIXEL;
 915           ras.cover += ( fy2 - fy1 );
 916           ras.area  += ( fy2 - fy1 ) * ( fx1 + fx2 );
 917           fx1 = ONE_PIXEL;
 918           fy1 = fy2;
 919           ex1--;
 920         }
 921         else if ( prod - dx * ONE_PIXEL                  <= 0 &&
 922                   prod - dx * ONE_PIXEL + dy * ONE_PIXEL >  0 ) /* up */
 923         {
 924           prod -= dx * ONE_PIXEL;
 925           fx2 = (TPos)FT_UDIV( -prod, dy );
 926           fy2 = ONE_PIXEL;
 927           ras.cover += ( fy2 - fy1 );
 928           ras.area  += ( fy2 - fy1 ) * ( fx1 + fx2 );
 929           fx1 = fx2;
 930           fy1 = 0;
 931           ey1++;
 932         }
 933         else if ( prod - dx * ONE_PIXEL + dy * ONE_PIXEL <= 0 &&
 934                   prod                  + dy * ONE_PIXEL >= 0 ) /* right */
 935         {
 936           prod += dy * ONE_PIXEL;
 937           fx2 = ONE_PIXEL;
 938           fy2 = (TPos)FT_UDIV( prod, dx );
 939           ras.cover += ( fy2 - fy1 );
 940           ras.area  += ( fy2 - fy1 ) * ( fx1 + fx2 );
 941           fx1 = 0;
 942           fy1 = fy2;
 943           ex1++;
 944         }
 945         else /* ( prod                  + dy * ONE_PIXEL <  0 &&
 946                   prod                                   >  0 )    down */
 947         {
 948           fx2 = (TPos)FT_UDIV( prod, -dy );
 949           fy2 = 0;
 950           prod += dx * ONE_PIXEL;
 951           ras.cover += ( fy2 - fy1 );
 952           ras.area  += ( fy2 - fy1 ) * ( fx1 + fx2 );
 953           fx1 = fx2;
 954           fy1 = ONE_PIXEL;
 955           ey1--;
 956         }
 957 
 958         gray_set_cell( RAS_VAR_ ex1, ey1 );
 959       } while ( ex1 != ex2 || ey1 != ey2 );
 960     }
 961 
 962     fx2 = to_x - SUBPIXELS( ex2 );
 963     fy2 = to_y - SUBPIXELS( ey2 );
 964 
 965     ras.cover += ( fy2 - fy1 );
 966     ras.area  += ( fy2 - fy1 ) * ( fx1 + fx2 );
 967 
 968   End:
 969     ras.x       = to_x;
 970     ras.y       = to_y;
 971   }
 972 
 973 #endif
 974 
 975   static void
 976   gray_split_conic( FT_Vector*  base )
 977   {
 978     TPos  a, b;
 979 
 980 
 981     base[4].x = base[2].x;
 982     b = base[1].x;
 983     a = base[3].x = ( base[2].x + b ) / 2;
 984     b = base[1].x = ( base[0].x + b ) / 2;
 985     base[2].x = ( a + b ) / 2;

 986 
 987     base[4].y = base[2].y;
 988     b = base[1].y;
 989     a = base[3].y = ( base[2].y + b ) / 2;
 990     b = base[1].y = ( base[0].y + b ) / 2;
 991     base[2].y = ( a + b ) / 2;

 992   }
 993 
 994 
 995   static void
 996   gray_render_conic( RAS_ARG_ const FT_Vector*  control,
 997                               const FT_Vector*  to )
 998   {
 999     FT_Vector   bez_stack[16 * 2 + 1];  /* enough to accommodate bisections */
1000     FT_Vector*  arc = bez_stack;
1001     TPos        dx, dy;
1002     int         draw, split;
1003 
1004 
1005     arc[0].x = UPSCALE( to->x );
1006     arc[0].y = UPSCALE( to->y );
1007     arc[1].x = UPSCALE( control->x );
1008     arc[1].y = UPSCALE( control->y );
1009     arc[2].x = ras.x;
1010     arc[2].y = ras.y;
1011 


1025     dx = FT_ABS( arc[2].x + arc[0].x - 2 * arc[1].x );
1026     dy = FT_ABS( arc[2].y + arc[0].y - 2 * arc[1].y );
1027     if ( dx < dy )
1028       dx = dy;
1029 
1030     /* We can calculate the number of necessary bisections because  */
1031     /* each bisection predictably reduces deviation exactly 4-fold. */
1032     /* Even 32-bit deviation would vanish after 16 bisections.      */
1033     draw = 1;
1034     while ( dx > ONE_PIXEL / 4 )
1035     {
1036       dx   >>= 2;
1037       draw <<= 1;
1038     }
1039 
1040     /* We use decrement counter to count the total number of segments */
1041     /* to draw starting from 2^level. Before each draw we split as    */
1042     /* many times as there are trailing zeros in the counter.         */
1043     do
1044     {
1045       split = 1;
1046       while ( ( draw & split ) == 0 )
1047       {
1048         gray_split_conic( arc );
1049         arc += 2;
1050         split <<= 1;
1051       }
1052 
1053       gray_render_line( RAS_VAR_ arc[0].x, arc[0].y );
1054       arc -= 2;
1055 
1056     } while ( --draw );
1057   }
1058 
1059 
1060   static void
1061   gray_split_cubic( FT_Vector*  base )
1062   {
1063     TPos  a, b, c, d;
1064 
1065 
1066     base[6].x = base[3].x;
1067     c = base[1].x;
1068     d = base[2].x;
1069     base[1].x = a = ( base[0].x + c ) / 2;
1070     base[5].x = b = ( base[3].x + d ) / 2;
1071     c = ( c + d ) / 2;
1072     base[2].x = a = ( a + c ) / 2;
1073     base[4].x = b = ( b + c ) / 2;
1074     base[3].x = ( a + b ) / 2;


1075 
1076     base[6].y = base[3].y;
1077     c = base[1].y;
1078     d = base[2].y;
1079     base[1].y = a = ( base[0].y + c ) / 2;
1080     base[5].y = b = ( base[3].y + d ) / 2;
1081     c = ( c + d ) / 2;
1082     base[2].y = a = ( a + c ) / 2;
1083     base[4].y = b = ( b + c ) / 2;
1084     base[3].y = ( a + b ) / 2;


1085   }
1086 
1087 
1088   static void
1089   gray_render_cubic( RAS_ARG_ const FT_Vector*  control1,
1090                               const FT_Vector*  control2,
1091                               const FT_Vector*  to )
1092   {
1093     FT_Vector   bez_stack[16 * 3 + 1];  /* enough to accommodate bisections */
1094     FT_Vector*  arc = bez_stack;
1095     TPos        dx, dy, dx_, dy_;
1096     TPos        dx1, dy1, dx2, dy2;
1097     TPos        L, s, s_limit;
1098 
1099 
1100     arc[0].x = UPSCALE( to->x );
1101     arc[0].y = UPSCALE( to->y );
1102     arc[1].x = UPSCALE( control2->x );
1103     arc[1].y = UPSCALE( control2->y );
1104     arc[2].x = UPSCALE( control1->x );
1105     arc[2].y = UPSCALE( control1->y );
1106     arc[3].x = ras.x;
1107     arc[3].y = ras.y;
1108 
1109     /* short-cut the arc that crosses the current band */
1110     if ( ( TRUNC( arc[0].y ) >= ras.max_ey &&
1111            TRUNC( arc[1].y ) >= ras.max_ey &&
1112            TRUNC( arc[2].y ) >= ras.max_ey &&
1113            TRUNC( arc[3].y ) >= ras.max_ey ) ||
1114          ( TRUNC( arc[0].y ) <  ras.min_ey &&
1115            TRUNC( arc[1].y ) <  ras.min_ey &&
1116            TRUNC( arc[2].y ) <  ras.min_ey &&
1117            TRUNC( arc[3].y ) <  ras.min_ey ) )
1118     {
1119       ras.x = arc[0].x;
1120       ras.y = arc[0].y;
1121       return;
1122     }
1123 
1124     for (;;)
1125     {
1126       /* Decide whether to split or draw. See `Rapid Termination          */
1127       /* Evaluation for Recursive Subdivision of Bezier Curves' by Thomas */
1128       /* F. Hain, at                                                      */
1129       /* http://www.cis.southalabama.edu/~hain/general/Publications/Bezier/Camera-ready%20CISST02%202.pdf */
1130 
1131       /* dx and dy are x and y components of the P0-P3 chord vector. */
1132       dx = dx_ = arc[3].x - arc[0].x;
1133       dy = dy_ = arc[3].y - arc[0].y;
1134 
1135       L = FT_HYPOT( dx_, dy_ );
1136 
1137       /* Avoid possible arithmetic overflow below by splitting. */
1138       if ( L > 32767 )
1139         goto Split;
1140 
1141       /* Max deviation may be as much as (s/L) * 3/4 (if Hain's v = 1). */
1142       s_limit = L * (TPos)( ONE_PIXEL / 6 );
1143 
1144       /* s is L * the perpendicular distance from P1 to the line P0-P3. */
1145       dx1 = arc[1].x - arc[0].x;
1146       dy1 = arc[1].y - arc[0].y;
1147       s = FT_ABS( SUB_LONG( MUL_LONG( dy, dx1 ), MUL_LONG( dx, dy1 ) ) );
1148 
1149       if ( s > s_limit )
1150         goto Split;
1151 
1152       /* s is L * the perpendicular distance from P2 to the line P0-P3. */
1153       dx2 = arc[2].x - arc[0].x;
1154       dy2 = arc[2].y - arc[0].y;
1155       s = FT_ABS( SUB_LONG( MUL_LONG( dy, dx2 ), MUL_LONG( dx, dy2 ) ) );
1156 
1157       if ( s > s_limit )
1158         goto Split;
1159 
1160       /* Split super curvy segments where the off points are so far
1161          from the chord that the angles P0-P1-P3 or P0-P2-P3 become
1162          acute as detected by appropriate dot products. */
1163       if ( dx1 * ( dx1 - dx ) + dy1 * ( dy1 - dy ) > 0 ||
1164            dx2 * ( dx2 - dx ) + dy2 * ( dy2 - dy ) > 0 )
1165         goto Split;
1166 
1167       gray_render_line( RAS_VAR_ arc[0].x, arc[0].y );
1168 
1169       if ( arc == bez_stack )
1170         return;
1171 
1172       arc -= 3;
1173       continue;
1174 
1175     Split:
1176       gray_split_cubic( arc );
1177       arc += 3;
1178     }
1179   }
1180 
1181 
1182   static int
1183   gray_move_to( const FT_Vector*  to,
1184                 gray_PWorker      worker )


1219 
1220   static int
1221   gray_cubic_to( const FT_Vector*  control1,
1222                  const FT_Vector*  control2,
1223                  const FT_Vector*  to,
1224                  gray_PWorker      worker )
1225   {
1226     gray_render_cubic( RAS_VAR_ control1, control2, to );
1227     return 0;
1228   }
1229 
1230 
1231   static void
1232   gray_hline( RAS_ARG_ TCoord  x,
1233                        TCoord  y,
1234                        TArea   coverage,
1235                        TCoord  acount )
1236   {
1237     /* scale the coverage from 0..(ONE_PIXEL*ONE_PIXEL*2) to 0..256  */
1238     coverage >>= PIXEL_BITS * 2 + 1 - 8;
1239     if ( coverage < 0 )
1240       coverage = -coverage - 1;
1241 
1242     /* compute the line's coverage depending on the outline fill rule */
1243     if ( ras.outline.flags & FT_OUTLINE_EVEN_ODD_FILL )
1244     {
1245       coverage &= 511;
1246 
1247       if ( coverage >= 256 )
1248         coverage = 511 - coverage;
1249     }
1250     else
1251     {
1252       /* normal non-zero winding rule */


1253       if ( coverage >= 256 )
1254         coverage = 255;
1255     }
1256 
1257     if ( ras.render_span )  /* for FT_RASTER_FLAG_DIRECT only */
1258     {
1259       FT_Span  span;
1260 
1261 
1262       span.x        = (short)x;
1263       span.len      = (unsigned short)acount;
1264       span.coverage = (unsigned char)coverage;
1265 
1266       ras.render_span( y, 1, &span, ras.render_span_data );





1267     }
1268     else
1269     {
1270       unsigned char*  q = ras.target.origin - ras.target.pitch * y + x;
1271       unsigned char   c = (unsigned char)coverage;
1272 
1273 
1274       /* For small-spans it is faster to do it by ourselves than
1275        * calling `memset'.  This is mainly due to the cost of the
1276        * function call.
1277        */
1278       switch ( acount )
1279       {
1280       case 7: *q++ = c;
1281       case 6: *q++ = c;
1282       case 5: *q++ = c;
1283       case 4: *q++ = c;
1284       case 3: *q++ = c;
1285       case 2: *q++ = c;
1286       case 1: *q   = c;
1287       case 0: break;















1288       default:
1289         FT_MEM_SET( q, c, acount );
1290       }
1291     }
1292   }
1293 
1294 
1295   static void
1296   gray_sweep( RAS_ARG )
1297   {
1298     int  y;
1299 
1300 
1301     for ( y = ras.min_ey; y < ras.max_ey; y++ )
1302     {
1303       PCell   cell  = ras.ycells[y - ras.min_ey];
1304       TCoord  x     = ras.min_ex;
1305       TArea   cover = 0;
1306       TArea   area;
1307 
1308 
1309       for ( ; cell != NULL; cell = cell->next )
1310       {
1311         if ( cover != 0 && cell->x > x )
1312           gray_hline( RAS_VAR_ x, y, cover, cell->x - x );
1313 
1314         cover += (TArea)cell->cover * ( ONE_PIXEL * 2 );
1315         area   = cover - cell->area;
1316 
1317         if ( area != 0 && cell->x >= ras.min_ex )
1318           gray_hline( RAS_VAR_ cell->x, y, area, 1 );
1319 
1320         x = cell->x + 1;
1321       }
1322 
1323       if ( cover != 0 )
1324         gray_hline( RAS_VAR_ x, y, cover, ras.max_ex - x );







1325     }
1326   }
1327 
1328 
1329 #ifdef STANDALONE_
1330 
1331   /**************************************************************************
1332    *
1333    * The following functions should only compile in stand-alone mode,
1334    * i.e., when building this component without the rest of FreeType.
1335    *
1336    */
1337 
1338   /**************************************************************************
1339    *
1340    * @Function:
1341    *   FT_Outline_Decompose
1342    *
1343    * @Description:
1344    *   Walk over an outline's structure to decompose it into individual


1354    *     A table of `emitters', i.e., function pointers
1355    *     called during decomposition to indicate path
1356    *     operations.
1357    *
1358    * @InOut:
1359    *   user ::
1360    *     A typeless pointer which is passed to each
1361    *     emitter during the decomposition.  It can be
1362    *     used to store the state during the
1363    *     decomposition.
1364    *
1365    * @Return:
1366    *   Error code.  0 means success.
1367    */
1368   static int
1369   FT_Outline_Decompose( const FT_Outline*        outline,
1370                         const FT_Outline_Funcs*  func_interface,
1371                         void*                    user )
1372   {
1373 #undef SCALED
1374 #define SCALED( x )  ( ( (x) << shift ) - delta )
1375 
1376     FT_Vector   v_last;
1377     FT_Vector   v_control;
1378     FT_Vector   v_start;
1379 
1380     FT_Vector*  point;
1381     FT_Vector*  limit;
1382     char*       tags;
1383 
1384     int         error;
1385 
1386     int   n;         /* index of contour in outline     */
1387     int   first;     /* index of first point in contour */
1388     char  tag;       /* current point's state           */
1389 
1390     int   shift;
1391     TPos  delta;
1392 
1393 
1394     if ( !outline )


1614 #endif /* STANDALONE_ */
1615 
1616 
1617   FT_DEFINE_OUTLINE_FUNCS(
1618     func_interface,
1619 
1620     (FT_Outline_MoveTo_Func) gray_move_to,   /* move_to  */
1621     (FT_Outline_LineTo_Func) gray_line_to,   /* line_to  */
1622     (FT_Outline_ConicTo_Func)gray_conic_to,  /* conic_to */
1623     (FT_Outline_CubicTo_Func)gray_cubic_to,  /* cubic_to */
1624 
1625     0,                                       /* shift    */
1626     0                                        /* delta    */
1627   )
1628 
1629 
1630   static int
1631   gray_convert_glyph_inner( RAS_ARG,
1632                             int  continued )
1633   {
1634     volatile int  error = 0;
1635 
1636 
1637     if ( ft_setjmp( ras.jump_buffer ) == 0 )
1638     {
1639       if ( continued )
1640         FT_Trace_Disable();
1641       error = FT_Outline_Decompose( &ras.outline, &func_interface, &ras );
1642       if ( continued )
1643         FT_Trace_Enable();
1644 
1645       if ( !ras.invalid )
1646         gray_record_cell( RAS_VAR );
1647 
1648       FT_TRACE7(( "band [%d..%d]: %d cell%s\n",
1649                   ras.min_ey,
1650                   ras.max_ey,
1651                   ras.num_cells,
1652                   ras.num_cells == 1 ? "" : "s" ));
1653     }
1654     else


1738           FT_TRACE7(( "gray_convert_glyph: rotten glyph\n" ));
1739           return 1;
1740         }
1741 
1742         band++;
1743         band[1]  = band[0];
1744         band[0] += width;
1745       } while ( band >= bands );
1746     }
1747 
1748     return 0;
1749   }
1750 
1751 
1752   static int
1753   gray_raster_render( FT_Raster                raster,
1754                       const FT_Raster_Params*  params )
1755   {
1756     const FT_Outline*  outline    = (const FT_Outline*)params->source;
1757     const FT_Bitmap*   target_map = params->target;
1758     FT_BBox            clip;
1759 
1760 #ifndef FT_STATIC_RASTER
1761     gray_TWorker  worker[1];
1762 #endif
1763 
1764 
1765     if ( !raster )
1766       return FT_THROW( Invalid_Argument );
1767 
1768     /* this version does not support monochrome rendering */
1769     if ( !( params->flags & FT_RASTER_FLAG_AA ) )
1770       return FT_THROW( Invalid_Mode );
1771 
1772     if ( !outline )
1773       return FT_THROW( Invalid_Outline );
1774 
1775     /* return immediately if the outline is empty */
1776     if ( outline->n_points == 0 || outline->n_contours <= 0 )
1777       return 0;
1778 
1779     if ( !outline->contours || !outline->points )
1780       return FT_THROW( Invalid_Outline );
1781 
1782     if ( outline->n_points !=
1783            outline->contours[outline->n_contours - 1] + 1 )
1784       return FT_THROW( Invalid_Outline );
1785 
1786     ras.outline = *outline;
1787 
1788     if ( params->flags & FT_RASTER_FLAG_DIRECT )
1789     {
1790       if ( !params->gray_spans )
1791         return 0;
1792 
1793       ras.render_span      = (FT_Raster_Span_Func)params->gray_spans;
1794       ras.render_span_data = params->user;






1795     }
1796     else
1797     {
1798       /* if direct mode is not set, we must have a target bitmap */
1799       if ( !target_map )
1800         return FT_THROW( Invalid_Argument );
1801 
1802       /* nothing to do */
1803       if ( !target_map->width || !target_map->rows )
1804         return 0;
1805 
1806       if ( !target_map->buffer )
1807         return FT_THROW( Invalid_Argument );
1808 
1809       if ( target_map->pitch < 0 )
1810         ras.target.origin = target_map->buffer;
1811       else
1812         ras.target.origin = target_map->buffer
1813               + ( target_map->rows - 1 ) * (unsigned int)target_map->pitch;
1814 
1815       ras.target.pitch = target_map->pitch;
1816 
1817       ras.render_span      = (FT_Raster_Span_Func)NULL;
1818       ras.render_span_data = NULL;
1819     }
1820 
1821     /* compute clipping box */
1822     if ( params->flags & FT_RASTER_FLAG_DIRECT &&
1823          params->flags & FT_RASTER_FLAG_CLIP   )
1824       clip = params->clip_box;
1825     else
1826     {
1827       /* compute clip box from target pixmap */
1828       clip.xMin = 0;
1829       clip.yMin = 0;
1830       clip.xMax = (FT_Pos)target_map->width;
1831       clip.yMax = (FT_Pos)target_map->rows;
1832     }
1833 
1834     /* clip to target bitmap, exit if nothing to do */
1835     ras.min_ex = clip.xMin;
1836     ras.min_ey = clip.yMin;
1837     ras.max_ex = clip.xMax;
1838     ras.max_ey = clip.yMax;
1839 
1840     if ( ras.max_ex <= ras.min_ex || ras.max_ey <= ras.min_ey )
1841       return 0;
1842 
1843     return gray_convert_glyph( RAS_VAR );
1844   }
1845 
1846 
1847   /**** RASTER OBJECT CREATION: In stand-alone mode, we simply use *****/
1848   /****                         a static object.                   *****/
1849 
1850 #ifdef STANDALONE_
1851 
1852   static int
1853   gray_raster_new( void*       memory,
1854                    FT_Raster*  araster )
1855   {
1856     static gray_TRaster  the_raster;
1857 
1858     FT_UNUSED( memory );
1859 




  28    *   same directory
  29    *
  30    * - compile `ftgrays' with the STANDALONE_ macro defined, as in
  31    *
  32    *     cc -c -DSTANDALONE_ ftgrays.c
  33    *
  34    * The renderer can be initialized with a call to
  35    * `ft_gray_raster.raster_new'; an anti-aliased bitmap can be generated
  36    * with a call to `ft_gray_raster.raster_render'.
  37    *
  38    * See the comments and documentation in the file `ftimage.h' for more
  39    * details on how the raster works.
  40    *
  41    */
  42 
  43   /**************************************************************************
  44    *
  45    * This is a new anti-aliasing scan-converter for FreeType 2.  The
  46    * algorithm used here is _very_ different from the one in the standard
  47    * `ftraster' module.  Actually, `ftgrays' computes the _exact_
  48    * coverage of the outline on each pixel cell by straight segments.
  49    *
  50    * It is based on ideas that I initially found in Raph Levien's
  51    * excellent LibArt graphics library (see https://www.levien.com/libart
  52    * for more information, though the web pages do not tell anything
  53    * about the renderer; you'll have to dive into the source code to
  54    * understand how it works).
  55    *
  56    * Note, however, that this is a _very_ different implementation
  57    * compared to Raph's.  Coverage information is stored in a very
  58    * different way, and I don't use sorted vector paths.  Also, it doesn't
  59    * use floating point values.
  60    *
  61    * Bézier segments are flattened by splitting them until their deviation
  62    * from straight line becomes much smaller than a pixel.  Therefore, the
  63    * pixel coverage by a Bézier curve is calculated approximately.  To
  64    * estimate the deviation, we use the distance from the control point
  65    * to the conic chord centre or the cubic chord trisection.  These
  66    * distances vanish fast after each split.  In the conic case, they vanish
  67    * predictably and the number of necessary splits can be calculated.
  68    *
  69    * This renderer has the following advantages:
  70    *
  71    * - It doesn't need an intermediate bitmap.  Instead, one can supply a
  72    *   callback function that will be called by the renderer to draw gray
  73    *   spans on any target surface.  You can thus do direct composition on
  74    *   any kind of bitmap, provided that you give the renderer the right
  75    *   callback.
  76    *
  77    * - A perfect anti-aliaser, i.e., it computes the _exact_ coverage on
  78    *   each pixel cell by straight segments.
  79    *
  80    * - It performs a single pass on the outline (the `standard' FT2
  81    *   renderer makes two passes).
  82    *
  83    * - It can easily be modified to render to _any_ number of gray levels
  84    *   cheaply.
  85    *
  86    * - For small (< 80) pixel sizes, it is faster than the standard
  87    *   renderer.
  88    *
  89    */
  90 
  91 
  92   /**************************************************************************
  93    *
  94    * The macro FT_COMPONENT is used in trace mode.  It is an implicit
  95    * parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log
  96    * messages during execution.
  97    */
  98 #undef  FT_COMPONENT
  99 #define FT_COMPONENT  smooth
 100 
 101 
 102 #ifdef STANDALONE_
 103 
 104 
 105   /* The size in bytes of the render pool used by the scan-line converter  */
 106   /* to do all of its work.                                                */


 318 
 319 #define RAS_ARG   gray_PWorker  worker
 320 #define RAS_ARG_  gray_PWorker  worker,
 321 
 322 #define RAS_VAR   worker
 323 #define RAS_VAR_  worker,
 324 
 325 #else /* FT_STATIC_RASTER */
 326 
 327 #define RAS_ARG   void
 328 #define RAS_ARG_  /* empty */
 329 #define RAS_VAR   /* empty */
 330 #define RAS_VAR_  /* empty */
 331 
 332 #endif /* FT_STATIC_RASTER */
 333 
 334 
 335   /* must be at least 6 bits! */
 336 #define PIXEL_BITS  8
 337 





 338 #define ONE_PIXEL       ( 1 << PIXEL_BITS )
 339 #define TRUNC( x )      (TCoord)( (x) >> PIXEL_BITS )
 340 #define FRACT( x )      (TCoord)( (x) & ( ONE_PIXEL - 1 ) )



 341 
 342 #if PIXEL_BITS >= 6
 343 #define UPSCALE( x )    ( (x) * ( ONE_PIXEL >> 6 ) )
 344 #define DOWNSCALE( x )  ( (x) >> ( PIXEL_BITS - 6 ) )
 345 #else
 346 #define UPSCALE( x )    ( (x) >> ( 6 - PIXEL_BITS ) )
 347 #define DOWNSCALE( x )  ( (x) * ( 64 >> PIXEL_BITS ) )
 348 #endif
 349 
 350 
 351   /* Compute `dividend / divisor' and return both its quotient and     */
 352   /* remainder, cast to a specific type.  This macro also ensures that */
 353   /* the remainder is always positive.  We use the remainder to keep   */
 354   /* track of accumulating errors and compensate for them.             */
 355 #define FT_DIV_MOD( type, dividend, divisor, quotient, remainder ) \
 356   FT_BEGIN_STMNT                                                   \
 357     (quotient)  = (type)( (dividend) / (divisor) );                \
 358     (remainder) = (type)( (dividend) % (divisor) );                \
 359     if ( (remainder) < 0 )                                         \
 360     {                                                              \


 372 #undef FT_DIV_MOD
 373 #define FT_DIV_MOD( type, dividend, divisor, quotient, remainder ) \
 374   FT_BEGIN_STMNT                                                   \
 375     (quotient)  = (type)( (dividend) / (divisor) );                \
 376     (remainder) = (type)( (dividend) - (quotient) * (divisor) );   \
 377     if ( (remainder) < 0 )                                         \
 378     {                                                              \
 379       (quotient)--;                                                \
 380       (remainder) += (type)(divisor);                              \
 381     }                                                              \
 382   FT_END_STMNT
 383 #endif /* __arm__ */
 384 
 385 
 386   /* These macros speed up repetitive divisions by replacing them */
 387   /* with multiplications and right shifts.                       */
 388 #define FT_UDIVPREP( c, b )                                        \
 389   long  b ## _r = c ? (long)( FT_ULONG_MAX >> PIXEL_BITS ) / ( b ) \
 390                     : 0
 391 #define FT_UDIV( a, b )                                                \
 392   (TCoord)( ( (unsigned long)( a ) * (unsigned long)( b ## _r ) ) >>   \
 393             ( sizeof( long ) * FT_CHAR_BIT - PIXEL_BITS ) )
 394 
 395 
 396   /**************************************************************************
 397    *
 398    * TYPE DEFINITIONS
 399    */
 400 
 401   /* don't change the following types to FT_Int or FT_Pos, since we might */
 402   /* need to define them to "float" or "double" when experimenting with   */
 403   /* new algorithms                                                       */
 404 
 405   typedef long  TPos;     /* subpixel coordinate               */
 406   typedef int   TCoord;   /* integer scanline/pixel coordinate */
 407   typedef int   TArea;    /* cell areas, coordinate products   */
 408 
 409 
 410   typedef struct TCell_*  PCell;
 411 
 412   typedef struct  TCell_


 415     TCoord  cover; /* same with gray_TWorker.cover */
 416     TArea   area;
 417     PCell   next;
 418 
 419   } TCell;
 420 
 421   typedef struct TPixmap_
 422   {
 423     unsigned char*  origin;  /* pixmap origin at the bottom-left */
 424     int             pitch;   /* pitch to go down one row */
 425 
 426   } TPixmap;
 427 
 428   /* maximum number of gray cells in the buffer */
 429 #if FT_RENDER_POOL_SIZE > 2048
 430 #define FT_MAX_GRAY_POOL  ( FT_RENDER_POOL_SIZE / sizeof ( TCell ) )
 431 #else
 432 #define FT_MAX_GRAY_POOL  ( 2048 / sizeof ( TCell ) )
 433 #endif
 434 
 435   /* FT_Span buffer size for direct rendering only */
 436 #define FT_MAX_GRAY_SPANS  10
 437 
 438 
 439 #if defined( _MSC_VER )      /* Visual C++ (and Intel C++) */
 440   /* We disable the warning `structure was padded due to   */
 441   /* __declspec(align())' in order to compile cleanly with */
 442   /* the maximum level of warnings.                        */
 443 #pragma warning( push )
 444 #pragma warning( disable : 4324 )
 445 #endif /* _MSC_VER */
 446 
 447   typedef struct  gray_TWorker_
 448   {
 449     ft_jmp_buf  jump_buffer;
 450 
 451     TCoord  ex, ey;
 452     TCoord  min_ex, max_ex;
 453     TCoord  min_ey, max_ey;
 454 
 455     TArea   area;
 456     TCoord  cover;
 457     int     invalid;
 458 
 459     PCell*      ycells;
 460     PCell       cells;
 461     FT_PtrDist  max_cells;
 462     FT_PtrDist  num_cells;
 463 
 464     TPos    x,  y;
 465 
 466     FT_Outline  outline;
 467     TPixmap     target;
 468 
 469     FT_Raster_Span_Func  render_span;
 470     void*                render_span_data;
 471     FT_Span              spans[FT_MAX_GRAY_SPANS];
 472     int                  num_spans;
 473 
 474   } gray_TWorker, *gray_PWorker;
 475 
 476 #if defined( _MSC_VER )
 477 #pragma warning( pop )
 478 #endif
 479 
 480 
 481 #ifndef FT_STATIC_RASTER
 482 #define ras  (*worker)
 483 #else
 484   static gray_TWorker  ras;
 485 #endif
 486 
 487 
 488   typedef struct gray_TRaster_
 489   {
 490     void*         memory;
 491 
 492   } gray_TRaster, *gray_PRaster;


 504 
 505     for ( y = ras.min_ey; y < ras.max_ey; y++ )
 506     {
 507       PCell  cell = ras.ycells[y - ras.min_ey];
 508 
 509 
 510       printf( "%3d:", y );
 511 
 512       for ( ; cell != NULL; cell = cell->next )
 513         printf( " (%3d, c:%4d, a:%6d)",
 514                 cell->x, cell->cover, cell->area );
 515       printf( "\n" );
 516     }
 517   }
 518 
 519 #endif /* FT_DEBUG_LEVEL_TRACE */
 520 
 521 
 522   /**************************************************************************
 523    *
 524    * Record the current cell in the linked list.
 525    */
 526   static void
 527   gray_record_cell( RAS_ARG )
 528   {
 529     PCell  *pcell, cell;
 530     TCoord  x = ras.ex;
 531 
 532 
 533     pcell = &ras.ycells[ras.ey - ras.min_ey];
 534     while ( ( cell = *pcell ) )
 535     {
 536       if ( cell->x > x )

 537         break;
 538 
 539       if ( cell->x == x )
 540         goto Found;
 541 
 542       pcell = &cell->next;
 543     }
 544 
 545     if ( ras.num_cells >= ras.max_cells )
 546       ft_longjmp( ras.jump_buffer, 1 );
 547 
 548     /* insert new cell */
 549     cell        = ras.cells + ras.num_cells++;
 550     cell->x     = x;
 551     cell->area  = ras.area;
 552     cell->cover = ras.cover;
 553 
 554     cell->next  = *pcell;
 555     *pcell      = cell;
 556 


 564 
 565 
 566   /**************************************************************************
 567    *
 568    * Set the current cell to a new position.
 569    */
 570   static void
 571   gray_set_cell( RAS_ARG_ TCoord  ex,
 572                           TCoord  ey )
 573   {
 574     /* Move the cell pointer to a new position.  We set the `invalid'      */
 575     /* flag to indicate that the cell isn't part of those we're interested */
 576     /* in during the render phase.  This means that:                       */
 577     /*                                                                     */
 578     /* . the new vertical position must be within min_ey..max_ey-1.        */
 579     /* . the new horizontal position must be strictly less than max_ex     */
 580     /*                                                                     */
 581     /* Note that if a cell is to the left of the clipping region, it is    */
 582     /* actually set to the (min_ex-1) horizontal position.                 */
 583 



 584     /* record the current one if it is valid and substantial */
 585     if ( !ras.invalid && ( ras.area || ras.cover ) )
 586       gray_record_cell( RAS_VAR );
 587 
 588     ras.area  = 0;
 589     ras.cover = 0;
 590     ras.ex    = FT_MAX( ex, ras.min_ex - 1 );
 591     ras.ey    = ey;
 592 
 593     ras.invalid = ( ey >= ras.max_ey || ey < ras.min_ey ||
 594                     ex >= ras.max_ex );
 595   }
 596 
 597 
 598 #ifndef FT_LONG64
 599 
 600   /**************************************************************************
 601    *
 602    * Render a scanline as one or more cells.
 603    */
 604   static void
 605   gray_render_scanline( RAS_ARG_ TCoord  ey,
 606                                  TPos    x1,
 607                                  TCoord  y1,
 608                                  TPos    x2,
 609                                  TCoord  y2 )
 610   {
 611     TCoord  ex1, ex2, fx1, fx2, first, dy, delta, mod;
 612     TPos    p, dx;
 613     int     incr;
 614 
 615 
 616     ex1 = TRUNC( x1 );
 617     ex2 = TRUNC( x2 );
 618 
 619     /* trivial case.  Happens often */
 620     if ( y1 == y2 )
 621     {
 622       gray_set_cell( RAS_VAR_ ex2, ey );
 623       return;
 624     }
 625 
 626     fx1   = FRACT( x1 );
 627     fx2   = FRACT( x2 );
 628 
 629     /* everything is located in a single cell.  That is easy! */
 630     /*                                                        */
 631     if ( ex1 == ex2 )
 632       goto End;
 633 
 634     /* ok, we'll have to render a run of adjacent cells on the same */
 635     /* scanline...                                                  */
 636     /*                                                              */
 637     dx = x2 - x1;
 638     dy = y2 - y1;
 639 
 640     if ( dx > 0 )
 641     {
 642       p     = ( ONE_PIXEL - fx1 ) * dy;
 643       first = ONE_PIXEL;
 644       incr  = 1;
 645     }
 646     else
 647     {
 648       p     = fx1 * dy;
 649       first = 0;
 650       incr  = -1;
 651       dx    = -dx;
 652     }
 653 
 654     /* the fractional part of y-delta is mod/dx. It is essential to */
 655     /* keep track of its accumulation for accurate rendering.       */
 656     /* XXX: y-delta and x-delta below should be related.            */
 657     FT_DIV_MOD( TCoord, p, dx, delta, mod );
 658 
 659     ras.area  += (TArea)( ( fx1 + first ) * delta );
 660     ras.cover += delta;
 661     y1        += delta;
 662     ex1       += incr;
 663     gray_set_cell( RAS_VAR_ ex1, ey );
 664 
 665     if ( ex1 != ex2 )
 666     {
 667       TCoord  lift, rem;
 668 
 669 
 670       p = ONE_PIXEL * dy;
 671       FT_DIV_MOD( TCoord, p, dx, lift, rem );
 672 
 673       do
 674       {
 675         delta = lift;
 676         mod  += rem;


 702    *
 703    * Render a given line as a series of scanlines.
 704    */
 705   static void
 706   gray_render_line( RAS_ARG_ TPos  to_x,
 707                              TPos  to_y )
 708   {
 709     TCoord  ey1, ey2, fy1, fy2, first, delta, mod;
 710     TPos    p, dx, dy, x, x2;
 711     int     incr;
 712 
 713 
 714     ey1 = TRUNC( ras.y );
 715     ey2 = TRUNC( to_y );     /* if (ey2 >= ras.max_ey) ey2 = ras.max_ey-1; */
 716 
 717     /* perform vertical clipping */
 718     if ( ( ey1 >= ras.max_ey && ey2 >= ras.max_ey ) ||
 719          ( ey1 <  ras.min_ey && ey2 <  ras.min_ey ) )
 720       goto End;
 721 
 722     fy1 = FRACT( ras.y );
 723     fy2 = FRACT( to_y );
 724 
 725     /* everything is on a single scanline */
 726     if ( ey1 == ey2 )
 727     {
 728       gray_render_scanline( RAS_VAR_ ey1, ras.x, fy1, to_x, fy2 );
 729       goto End;
 730     }
 731 
 732     dx = to_x - ras.x;
 733     dy = to_y - ras.y;
 734 
 735     /* vertical line - avoid calling gray_render_scanline */
 736     if ( dx == 0 )
 737     {
 738       TCoord  ex     = TRUNC( ras.x );
 739       TCoord  two_fx = FRACT( ras.x ) << 1;
 740       TArea   area;
 741 
 742 
 743       if ( dy > 0)
 744       {
 745         first = ONE_PIXEL;
 746         incr  = 1;
 747       }
 748       else
 749       {
 750         first = 0;
 751         incr  = -1;
 752       }
 753 
 754       delta      = first - fy1;
 755       ras.area  += (TArea)two_fx * delta;
 756       ras.cover += delta;
 757       ey1       += incr;
 758 
 759       gray_set_cell( RAS_VAR_ ex, ey1 );


 774       ras.cover += delta;
 775 
 776       goto End;
 777     }
 778 
 779     /* ok, we have to render several scanlines */
 780     if ( dy > 0)
 781     {
 782       p     = ( ONE_PIXEL - fy1 ) * dx;
 783       first = ONE_PIXEL;
 784       incr  = 1;
 785     }
 786     else
 787     {
 788       p     = fy1 * dx;
 789       first = 0;
 790       incr  = -1;
 791       dy    = -dy;
 792     }
 793 
 794     /* the fractional part of x-delta is mod/dy. It is essential to */
 795     /* keep track of its accumulation for accurate rendering.       */
 796     FT_DIV_MOD( TCoord, p, dy, delta, mod );
 797 
 798     x = ras.x + delta;
 799     gray_render_scanline( RAS_VAR_ ey1, ras.x, fy1, x, first );
 800 
 801     ey1 += incr;
 802     gray_set_cell( RAS_VAR_ TRUNC( x ), ey1 );
 803 
 804     if ( ey1 != ey2 )
 805     {
 806       TCoord  lift, rem;
 807 
 808 
 809       p    = ONE_PIXEL * dx;
 810       FT_DIV_MOD( TCoord, p, dy, lift, rem );
 811 
 812       do
 813       {
 814         delta = lift;
 815         mod  += rem;


 832 
 833     gray_render_scanline( RAS_VAR_ ey1,
 834                                    x, ONE_PIXEL - first,
 835                                    to_x, fy2 );
 836 
 837   End:
 838     ras.x       = to_x;
 839     ras.y       = to_y;
 840   }
 841 
 842 #else
 843 
 844   /**************************************************************************
 845    *
 846    * Render a straight line across multiple cells in any direction.
 847    */
 848   static void
 849   gray_render_line( RAS_ARG_ TPos  to_x,
 850                              TPos  to_y )
 851   {
 852     TPos    dx, dy;
 853     TCoord  fx1, fy1, fx2, fy2;
 854     TCoord  ex1, ey1, ex2, ey2;
 855 
 856 
 857     ey1 = TRUNC( ras.y );
 858     ey2 = TRUNC( to_y );
 859 
 860     /* perform vertical clipping */
 861     if ( ( ey1 >= ras.max_ey && ey2 >= ras.max_ey ) ||
 862          ( ey1 <  ras.min_ey && ey2 <  ras.min_ey ) )
 863       goto End;
 864 
 865     ex1 = TRUNC( ras.x );
 866     ex2 = TRUNC( to_x );
 867 
 868     fx1 = FRACT( ras.x );
 869     fy1 = FRACT( ras.y );
 870 
 871     dx = to_x - ras.x;
 872     dy = to_y - ras.y;
 873 
 874     if ( ex1 == ex2 && ey1 == ey2 )       /* inside one cell */
 875       ;
 876     else if ( dy == 0 ) /* ex1 != ex2 */  /* any horizontal line */
 877     {
 878       gray_set_cell( RAS_VAR_ ex2, ey2 );
 879       goto End;
 880     }
 881     else if ( dx == 0 )
 882     {
 883       if ( dy > 0 )                       /* vertical line up */
 884         do
 885         {
 886           fy2 = ONE_PIXEL;
 887           ras.cover += ( fy2 - fy1 );
 888           ras.area  += ( fy2 - fy1 ) * fx1 * 2;
 889           fy1 = 0;
 890           ey1++;
 891           gray_set_cell( RAS_VAR_ ex1, ey1 );
 892         } while ( ey1 != ey2 );
 893       else                                /* vertical line down */
 894         do
 895         {
 896           fy2 = 0;
 897           ras.cover += ( fy2 - fy1 );
 898           ras.area  += ( fy2 - fy1 ) * fx1 * 2;
 899           fy1 = ONE_PIXEL;
 900           ey1--;
 901           gray_set_cell( RAS_VAR_ ex1, ey1 );
 902         } while ( ey1 != ey2 );
 903     }
 904     else                                  /* any other line */
 905     {
 906       TPos  prod = dx * (TPos)fy1 - dy * (TPos)fx1;
 907       FT_UDIVPREP( ex1 != ex2, dx );
 908       FT_UDIVPREP( ey1 != ey2, dy );
 909 
 910 
 911       /* The fundamental value `prod' determines which side and the  */
 912       /* exact coordinate where the line exits current cell.  It is  */
 913       /* also easily updated when moving from one cell to the next.  */
 914       do
 915       {
 916         if      ( prod                                   <= 0 &&
 917                   prod - dx * ONE_PIXEL                  >  0 ) /* left */
 918         {
 919           fx2 = 0;
 920           fy2 = FT_UDIV( -prod, -dx );
 921           prod -= dy * ONE_PIXEL;
 922           ras.cover += ( fy2 - fy1 );
 923           ras.area  += ( fy2 - fy1 ) * ( fx1 + fx2 );
 924           fx1 = ONE_PIXEL;
 925           fy1 = fy2;
 926           ex1--;
 927         }
 928         else if ( prod - dx * ONE_PIXEL                  <= 0 &&
 929                   prod - dx * ONE_PIXEL + dy * ONE_PIXEL >  0 ) /* up */
 930         {
 931           prod -= dx * ONE_PIXEL;
 932           fx2 = FT_UDIV( -prod, dy );
 933           fy2 = ONE_PIXEL;
 934           ras.cover += ( fy2 - fy1 );
 935           ras.area  += ( fy2 - fy1 ) * ( fx1 + fx2 );
 936           fx1 = fx2;
 937           fy1 = 0;
 938           ey1++;
 939         }
 940         else if ( prod - dx * ONE_PIXEL + dy * ONE_PIXEL <= 0 &&
 941                   prod                  + dy * ONE_PIXEL >= 0 ) /* right */
 942         {
 943           prod += dy * ONE_PIXEL;
 944           fx2 = ONE_PIXEL;
 945           fy2 = FT_UDIV( prod, dx );
 946           ras.cover += ( fy2 - fy1 );
 947           ras.area  += ( fy2 - fy1 ) * ( fx1 + fx2 );
 948           fx1 = 0;
 949           fy1 = fy2;
 950           ex1++;
 951         }
 952         else /* ( prod                  + dy * ONE_PIXEL <  0 &&
 953                   prod                                   >  0 )    down */
 954         {
 955           fx2 = FT_UDIV( prod, -dy );
 956           fy2 = 0;
 957           prod += dx * ONE_PIXEL;
 958           ras.cover += ( fy2 - fy1 );
 959           ras.area  += ( fy2 - fy1 ) * ( fx1 + fx2 );
 960           fx1 = fx2;
 961           fy1 = ONE_PIXEL;
 962           ey1--;
 963         }
 964 
 965         gray_set_cell( RAS_VAR_ ex1, ey1 );
 966       } while ( ex1 != ex2 || ey1 != ey2 );
 967     }
 968 
 969     fx2 = FRACT( to_x );
 970     fy2 = FRACT( to_y );
 971 
 972     ras.cover += ( fy2 - fy1 );
 973     ras.area  += ( fy2 - fy1 ) * ( fx1 + fx2 );
 974 
 975   End:
 976     ras.x       = to_x;
 977     ras.y       = to_y;
 978   }
 979 
 980 #endif
 981 
 982   static void
 983   gray_split_conic( FT_Vector*  base )
 984   {
 985     TPos  a, b;
 986 
 987 
 988     base[4].x = base[2].x;
 989     a = base[0].x + base[1].x;
 990     b = base[1].x + base[2].x;
 991     base[3].x = b >> 1;
 992     base[2].x = ( a + b ) >> 2;
 993     base[1].x = a >> 1;
 994 
 995     base[4].y = base[2].y;
 996     a = base[0].y + base[1].y;
 997     b = base[1].y + base[2].y;
 998     base[3].y = b >> 1;
 999     base[2].y = ( a + b ) >> 2;
1000     base[1].y = a >> 1;
1001   }
1002 
1003 
1004   static void
1005   gray_render_conic( RAS_ARG_ const FT_Vector*  control,
1006                               const FT_Vector*  to )
1007   {
1008     FT_Vector   bez_stack[16 * 2 + 1];  /* enough to accommodate bisections */
1009     FT_Vector*  arc = bez_stack;
1010     TPos        dx, dy;
1011     int         draw, split;
1012 
1013 
1014     arc[0].x = UPSCALE( to->x );
1015     arc[0].y = UPSCALE( to->y );
1016     arc[1].x = UPSCALE( control->x );
1017     arc[1].y = UPSCALE( control->y );
1018     arc[2].x = ras.x;
1019     arc[2].y = ras.y;
1020 


1034     dx = FT_ABS( arc[2].x + arc[0].x - 2 * arc[1].x );
1035     dy = FT_ABS( arc[2].y + arc[0].y - 2 * arc[1].y );
1036     if ( dx < dy )
1037       dx = dy;
1038 
1039     /* We can calculate the number of necessary bisections because  */
1040     /* each bisection predictably reduces deviation exactly 4-fold. */
1041     /* Even 32-bit deviation would vanish after 16 bisections.      */
1042     draw = 1;
1043     while ( dx > ONE_PIXEL / 4 )
1044     {
1045       dx   >>= 2;
1046       draw <<= 1;
1047     }
1048 
1049     /* We use decrement counter to count the total number of segments */
1050     /* to draw starting from 2^level. Before each draw we split as    */
1051     /* many times as there are trailing zeros in the counter.         */
1052     do
1053     {
1054       split = draw & ( -draw );  /* isolate the rightmost 1-bit */
1055       while ( ( split >>= 1 ) )
1056       {
1057         gray_split_conic( arc );
1058         arc += 2;

1059       }
1060 
1061       gray_render_line( RAS_VAR_ arc[0].x, arc[0].y );
1062       arc -= 2;
1063 
1064     } while ( --draw );
1065   }
1066 
1067 
1068   static void
1069   gray_split_cubic( FT_Vector*  base )
1070   {
1071     TPos  a, b, c;
1072 
1073 
1074     base[6].x = base[3].x;
1075     a = base[0].x + base[1].x;
1076     b = base[1].x + base[2].x;
1077     c = base[2].x + base[3].x;
1078     base[5].x = c >> 1;
1079     c += b;
1080     base[4].x = c >> 2;
1081     base[1].x = a >> 1;
1082     a += b;
1083     base[2].x = a >> 2;
1084     base[3].x = ( a + c ) >> 3;
1085 
1086     base[6].y = base[3].y;
1087     a = base[0].y + base[1].y;
1088     b = base[1].y + base[2].y;
1089     c = base[2].y + base[3].y;
1090     base[5].y = c >> 1;
1091     c += b;
1092     base[4].y = c >> 2;
1093     base[1].y = a >> 1;
1094     a += b;
1095     base[2].y = a >> 2;
1096     base[3].y = ( a + c ) >> 3;
1097   }
1098 
1099 
1100   static void
1101   gray_render_cubic( RAS_ARG_ const FT_Vector*  control1,
1102                               const FT_Vector*  control2,
1103                               const FT_Vector*  to )
1104   {
1105     FT_Vector   bez_stack[16 * 3 + 1];  /* enough to accommodate bisections */
1106     FT_Vector*  arc = bez_stack;



1107 
1108 
1109     arc[0].x = UPSCALE( to->x );
1110     arc[0].y = UPSCALE( to->y );
1111     arc[1].x = UPSCALE( control2->x );
1112     arc[1].y = UPSCALE( control2->y );
1113     arc[2].x = UPSCALE( control1->x );
1114     arc[2].y = UPSCALE( control1->y );
1115     arc[3].x = ras.x;
1116     arc[3].y = ras.y;
1117 
1118     /* short-cut the arc that crosses the current band */
1119     if ( ( TRUNC( arc[0].y ) >= ras.max_ey &&
1120            TRUNC( arc[1].y ) >= ras.max_ey &&
1121            TRUNC( arc[2].y ) >= ras.max_ey &&
1122            TRUNC( arc[3].y ) >= ras.max_ey ) ||
1123          ( TRUNC( arc[0].y ) <  ras.min_ey &&
1124            TRUNC( arc[1].y ) <  ras.min_ey &&
1125            TRUNC( arc[2].y ) <  ras.min_ey &&
1126            TRUNC( arc[3].y ) <  ras.min_ey ) )
1127     {
1128       ras.x = arc[0].x;
1129       ras.y = arc[0].y;
1130       return;
1131     }
1132 
1133     for (;;)
1134     {
1135       /* with each split, control points quickly converge towards  */
1136       /* chord trisection points and the vanishing distances below */
1137       /* indicate when the segment is flat enough to draw          */
1138       if ( FT_ABS( 2 * arc[0].x - 3 * arc[1].x + arc[3].x ) > ONE_PIXEL / 2 ||
1139            FT_ABS( 2 * arc[0].y - 3 * arc[1].y + arc[3].y ) > ONE_PIXEL / 2 ||
1140            FT_ABS( arc[0].x - 3 * arc[2].x + 2 * arc[3].x ) > ONE_PIXEL / 2 ||
1141            FT_ABS( arc[0].y - 3 * arc[2].y + 2 * arc[3].y ) > ONE_PIXEL / 2 )
































1142         goto Split;
1143 
1144       gray_render_line( RAS_VAR_ arc[0].x, arc[0].y );
1145 
1146       if ( arc == bez_stack )
1147         return;
1148 
1149       arc -= 3;
1150       continue;
1151 
1152     Split:
1153       gray_split_cubic( arc );
1154       arc += 3;
1155     }
1156   }
1157 
1158 
1159   static int
1160   gray_move_to( const FT_Vector*  to,
1161                 gray_PWorker      worker )


1196 
1197   static int
1198   gray_cubic_to( const FT_Vector*  control1,
1199                  const FT_Vector*  control2,
1200                  const FT_Vector*  to,
1201                  gray_PWorker      worker )
1202   {
1203     gray_render_cubic( RAS_VAR_ control1, control2, to );
1204     return 0;
1205   }
1206 
1207 
1208   static void
1209   gray_hline( RAS_ARG_ TCoord  x,
1210                        TCoord  y,
1211                        TArea   coverage,
1212                        TCoord  acount )
1213   {
1214     /* scale the coverage from 0..(ONE_PIXEL*ONE_PIXEL*2) to 0..256  */
1215     coverage >>= PIXEL_BITS * 2 + 1 - 8;


1216 
1217     /* compute the line's coverage depending on the outline fill rule */
1218     if ( ras.outline.flags & FT_OUTLINE_EVEN_ODD_FILL )
1219     {
1220       coverage &= 511;
1221 
1222       if ( coverage >= 256 )
1223         coverage = 511 - coverage;
1224     }
1225     else  /* default non-zero winding rule */
1226     {
1227       if ( coverage < 0 )
1228         coverage = ~coverage;  /* the same as -coverage - 1 */
1229 
1230       if ( coverage >= 256 )
1231         coverage = 255;
1232     }
1233 
1234     if ( ras.num_spans >= 0 )  /* for FT_RASTER_FLAG_DIRECT only */
1235     {
1236       FT_Span*  span = ras.spans + ras.num_spans++;
1237 
1238 
1239       span->x        = (short)x;
1240       span->len      = (unsigned short)acount;
1241       span->coverage = (unsigned char)coverage;
1242 
1243       if ( ras.num_spans == FT_MAX_GRAY_SPANS )
1244       {
1245         /* flush the span buffer and reset the count */
1246         ras.render_span( y, ras.num_spans, ras.spans, ras.render_span_data );
1247         ras.num_spans = 0;
1248       }
1249     }
1250     else
1251     {
1252       unsigned char*  q = ras.target.origin - ras.target.pitch * y + x;
1253       unsigned char   c = (unsigned char)coverage;
1254 
1255 
1256       /* For small-spans it is faster to do it by ourselves than
1257        * calling `memset'.  This is mainly due to the cost of the
1258        * function call.
1259        */
1260       switch ( acount )
1261       {
1262       case 7:
1263         *q++ = c;
1264         /* fall through */
1265       case 6:
1266         *q++ = c;
1267         /* fall through */
1268       case 5:
1269         *q++ = c;
1270         /* fall through */
1271       case 4:
1272         *q++ = c;
1273         /* fall through */
1274       case 3:
1275         *q++ = c;
1276         /* fall through */
1277       case 2:
1278         *q++ = c;
1279         /* fall through */
1280       case 1:
1281         *q = c;
1282         /* fall through */
1283       case 0:
1284         break;
1285       default:
1286         FT_MEM_SET( q, c, acount );
1287       }
1288     }
1289   }
1290 
1291 
1292   static void
1293   gray_sweep( RAS_ARG )
1294   {
1295     int  y;
1296 
1297 
1298     for ( y = ras.min_ey; y < ras.max_ey; y++ )
1299     {
1300       PCell   cell  = ras.ycells[y - ras.min_ey];
1301       TCoord  x     = ras.min_ex;
1302       TArea   cover = 0;
1303       TArea   area;
1304 
1305 
1306       for ( ; cell != NULL; cell = cell->next )
1307       {
1308         if ( cover != 0 && cell->x > x )
1309           gray_hline( RAS_VAR_ x, y, cover, cell->x - x );
1310 
1311         cover += (TArea)cell->cover * ( ONE_PIXEL * 2 );
1312         area   = cover - cell->area;
1313 
1314         if ( area != 0 && cell->x >= ras.min_ex )
1315           gray_hline( RAS_VAR_ cell->x, y, area, 1 );
1316 
1317         x = cell->x + 1;
1318       }
1319 
1320       if ( cover != 0 )
1321         gray_hline( RAS_VAR_ x, y, cover, ras.max_ex - x );
1322 
1323       if ( ras.num_spans > 0 )  /* for FT_RASTER_FLAG_DIRECT only */
1324       {
1325         /* flush the span buffer and reset the count */
1326         ras.render_span( y, ras.num_spans, ras.spans, ras.render_span_data );
1327         ras.num_spans = 0;
1328       }
1329     }
1330   }
1331 
1332 
1333 #ifdef STANDALONE_
1334 
1335   /**************************************************************************
1336    *
1337    * The following functions should only compile in stand-alone mode,
1338    * i.e., when building this component without the rest of FreeType.
1339    *
1340    */
1341 
1342   /**************************************************************************
1343    *
1344    * @Function:
1345    *   FT_Outline_Decompose
1346    *
1347    * @Description:
1348    *   Walk over an outline's structure to decompose it into individual


1358    *     A table of `emitters', i.e., function pointers
1359    *     called during decomposition to indicate path
1360    *     operations.
1361    *
1362    * @InOut:
1363    *   user ::
1364    *     A typeless pointer which is passed to each
1365    *     emitter during the decomposition.  It can be
1366    *     used to store the state during the
1367    *     decomposition.
1368    *
1369    * @Return:
1370    *   Error code.  0 means success.
1371    */
1372   static int
1373   FT_Outline_Decompose( const FT_Outline*        outline,
1374                         const FT_Outline_Funcs*  func_interface,
1375                         void*                    user )
1376   {
1377 #undef SCALED
1378 #define SCALED( x )  ( (x) * ( 1L << shift ) - delta )
1379 
1380     FT_Vector   v_last;
1381     FT_Vector   v_control;
1382     FT_Vector   v_start;
1383 
1384     FT_Vector*  point;
1385     FT_Vector*  limit;
1386     char*       tags;
1387 
1388     int         error;
1389 
1390     int   n;         /* index of contour in outline     */
1391     int   first;     /* index of first point in contour */
1392     char  tag;       /* current point's state           */
1393 
1394     int   shift;
1395     TPos  delta;
1396 
1397 
1398     if ( !outline )


1618 #endif /* STANDALONE_ */
1619 
1620 
1621   FT_DEFINE_OUTLINE_FUNCS(
1622     func_interface,
1623 
1624     (FT_Outline_MoveTo_Func) gray_move_to,   /* move_to  */
1625     (FT_Outline_LineTo_Func) gray_line_to,   /* line_to  */
1626     (FT_Outline_ConicTo_Func)gray_conic_to,  /* conic_to */
1627     (FT_Outline_CubicTo_Func)gray_cubic_to,  /* cubic_to */
1628 
1629     0,                                       /* shift    */
1630     0                                        /* delta    */
1631   )
1632 
1633 
1634   static int
1635   gray_convert_glyph_inner( RAS_ARG,
1636                             int  continued )
1637   {
1638     int  error;
1639 
1640 
1641     if ( ft_setjmp( ras.jump_buffer ) == 0 )
1642     {
1643       if ( continued )
1644         FT_Trace_Disable();
1645       error = FT_Outline_Decompose( &ras.outline, &func_interface, &ras );
1646       if ( continued )
1647         FT_Trace_Enable();
1648 
1649       if ( !ras.invalid )
1650         gray_record_cell( RAS_VAR );
1651 
1652       FT_TRACE7(( "band [%d..%d]: %d cell%s\n",
1653                   ras.min_ey,
1654                   ras.max_ey,
1655                   ras.num_cells,
1656                   ras.num_cells == 1 ? "" : "s" ));
1657     }
1658     else


1742           FT_TRACE7(( "gray_convert_glyph: rotten glyph\n" ));
1743           return 1;
1744         }
1745 
1746         band++;
1747         band[1]  = band[0];
1748         band[0] += width;
1749       } while ( band >= bands );
1750     }
1751 
1752     return 0;
1753   }
1754 
1755 
1756   static int
1757   gray_raster_render( FT_Raster                raster,
1758                       const FT_Raster_Params*  params )
1759   {
1760     const FT_Outline*  outline    = (const FT_Outline*)params->source;
1761     const FT_Bitmap*   target_map = params->target;

1762 
1763 #ifndef FT_STATIC_RASTER
1764     gray_TWorker  worker[1];
1765 #endif
1766 
1767 
1768     if ( !raster )
1769       return FT_THROW( Invalid_Argument );
1770 
1771     /* this version does not support monochrome rendering */
1772     if ( !( params->flags & FT_RASTER_FLAG_AA ) )
1773       return FT_THROW( Invalid_Mode );
1774 
1775     if ( !outline )
1776       return FT_THROW( Invalid_Outline );
1777 
1778     /* return immediately if the outline is empty */
1779     if ( outline->n_points == 0 || outline->n_contours <= 0 )
1780       return 0;
1781 
1782     if ( !outline->contours || !outline->points )
1783       return FT_THROW( Invalid_Outline );
1784 
1785     if ( outline->n_points !=
1786            outline->contours[outline->n_contours - 1] + 1 )
1787       return FT_THROW( Invalid_Outline );
1788 
1789     ras.outline = *outline;
1790 
1791     if ( params->flags & FT_RASTER_FLAG_DIRECT )
1792     {
1793       if ( !params->gray_spans )
1794         return 0;
1795 
1796       ras.render_span      = (FT_Raster_Span_Func)params->gray_spans;
1797       ras.render_span_data = params->user;
1798       ras.num_spans        = 0;
1799 
1800       ras.min_ex = params->clip_box.xMin;
1801       ras.min_ey = params->clip_box.yMin;
1802       ras.max_ex = params->clip_box.xMax;
1803       ras.max_ey = params->clip_box.yMax;
1804     }
1805     else
1806     {
1807       /* if direct mode is not set, we must have a target bitmap */
1808       if ( !target_map )
1809         return FT_THROW( Invalid_Argument );
1810 
1811       /* nothing to do */
1812       if ( !target_map->width || !target_map->rows )
1813         return 0;
1814 
1815       if ( !target_map->buffer )
1816         return FT_THROW( Invalid_Argument );
1817 
1818       if ( target_map->pitch < 0 )
1819         ras.target.origin = target_map->buffer;
1820       else
1821         ras.target.origin = target_map->buffer
1822               + ( target_map->rows - 1 ) * (unsigned int)target_map->pitch;
1823 
1824       ras.target.pitch = target_map->pitch;
1825 
1826       ras.render_span      = (FT_Raster_Span_Func)NULL;
1827       ras.render_span_data = NULL;
1828       ras.num_spans        = -1;  /* invalid */
1829 
1830       ras.min_ex = 0;
1831       ras.min_ey = 0;
1832       ras.max_ex = (FT_Pos)target_map->width;
1833       ras.max_ey = (FT_Pos)target_map->rows;







1834     }
1835 
1836     /* exit if nothing to do */





1837     if ( ras.max_ex <= ras.min_ex || ras.max_ey <= ras.min_ey )
1838       return 0;
1839 
1840     return gray_convert_glyph( RAS_VAR );
1841   }
1842 
1843 
1844   /**** RASTER OBJECT CREATION: In stand-alone mode, we simply use *****/
1845   /****                         a static object.                   *****/
1846 
1847 #ifdef STANDALONE_
1848 
1849   static int
1850   gray_raster_new( void*       memory,
1851                    FT_Raster*  araster )
1852   {
1853     static gray_TRaster  the_raster;
1854 
1855     FT_UNUSED( memory );
1856 


< prev index next >