Home | History | Annotate | Download | only in base
      1 /***************************************************************************/
      2 /*                                                                         */
      3 /*  ftgloadr.c                                                             */
      4 /*                                                                         */
      5 /*    The FreeType glyph loader (body).                                    */
      6 /*                                                                         */
      7 /*  Copyright 2002-2006, 2010, 2013 by                                     */
      8 /*  David Turner, Robert Wilhelm, and Werner Lemberg                       */
      9 /*                                                                         */
     10 /*  This file is part of the FreeType project, and may only be used,       */
     11 /*  modified, and distributed under the terms of the FreeType project      */
     12 /*  license, LICENSE.TXT.  By continuing to use, modify, or distribute     */
     13 /*  this file you indicate that you have read the license and              */
     14 /*  understand and accept it fully.                                        */
     15 /*                                                                         */
     16 /***************************************************************************/
     17 
     18 
     19 #include <ft2build.h>
     20 #include FT_INTERNAL_DEBUG_H
     21 #include FT_INTERNAL_GLYPH_LOADER_H
     22 #include FT_INTERNAL_MEMORY_H
     23 #include FT_INTERNAL_OBJECTS_H
     24 
     25 #undef  FT_COMPONENT
     26 #define FT_COMPONENT  trace_gloader
     27 
     28 
     29   /*************************************************************************/
     30   /*************************************************************************/
     31   /*************************************************************************/
     32   /*****                                                               *****/
     33   /*****                                                               *****/
     34   /*****                    G L Y P H   L O A D E R                    *****/
     35   /*****                                                               *****/
     36   /*****                                                               *****/
     37   /*************************************************************************/
     38   /*************************************************************************/
     39   /*************************************************************************/
     40 
     41   /*************************************************************************/
     42   /*                                                                       */
     43   /* The glyph loader is a simple object which is used to load a set of    */
     44   /* glyphs easily.  It is critical for the correct loading of composites. */
     45   /*                                                                       */
     46   /* Ideally, one can see it as a stack of abstract `glyph' objects.       */
     47   /*                                                                       */
     48   /*   loader.base     Is really the bottom of the stack.  It describes a  */
     49   /*                   single glyph image made of the juxtaposition of     */
     50   /*                   several glyphs (those `in the stack').              */
     51   /*                                                                       */
     52   /*   loader.current  Describes the top of the stack, on which a new      */
     53   /*                   glyph can be loaded.                                */
     54   /*                                                                       */
     55   /*   Rewind          Clears the stack.                                   */
     56   /*   Prepare         Set up `loader.current' for addition of a new glyph */
     57   /*                   image.                                              */
     58   /*   Add             Add the `current' glyph image to the `base' one,    */
     59   /*                   and prepare for another one.                        */
     60   /*                                                                       */
     61   /* The glyph loader is now a base object.  Each driver used to           */
     62   /* re-implement it in one way or the other, which wasted code and        */
     63   /* energy.                                                               */
     64   /*                                                                       */
     65   /*************************************************************************/
     66 
     67 
     68   /* create a new glyph loader */
     69   FT_BASE_DEF( FT_Error )
     70   FT_GlyphLoader_New( FT_Memory        memory,
     71                       FT_GlyphLoader  *aloader )
     72   {
     73     FT_GlyphLoader  loader = NULL;
     74     FT_Error        error;
     75 
     76 
     77     if ( !FT_NEW( loader ) )
     78     {
     79       loader->memory = memory;
     80       *aloader       = loader;
     81     }
     82     return error;
     83   }
     84 
     85 
     86   /* rewind the glyph loader - reset counters to 0 */
     87   FT_BASE_DEF( void )
     88   FT_GlyphLoader_Rewind( FT_GlyphLoader  loader )
     89   {
     90     FT_GlyphLoad  base    = &loader->base;
     91     FT_GlyphLoad  current = &loader->current;
     92 
     93 
     94     base->outline.n_points   = 0;
     95     base->outline.n_contours = 0;
     96     base->num_subglyphs      = 0;
     97 
     98     *current = *base;
     99   }
    100 
    101 
    102   /* reset the glyph loader, frees all allocated tables */
    103   /* and starts from zero                               */
    104   FT_BASE_DEF( void )
    105   FT_GlyphLoader_Reset( FT_GlyphLoader  loader )
    106   {
    107     FT_Memory memory = loader->memory;
    108 
    109 
    110     FT_FREE( loader->base.outline.points );
    111     FT_FREE( loader->base.outline.tags );
    112     FT_FREE( loader->base.outline.contours );
    113     FT_FREE( loader->base.extra_points );
    114     FT_FREE( loader->base.subglyphs );
    115 
    116     loader->base.extra_points2 = NULL;
    117 
    118     loader->max_points    = 0;
    119     loader->max_contours  = 0;
    120     loader->max_subglyphs = 0;
    121 
    122     FT_GlyphLoader_Rewind( loader );
    123   }
    124 
    125 
    126   /* delete a glyph loader */
    127   FT_BASE_DEF( void )
    128   FT_GlyphLoader_Done( FT_GlyphLoader  loader )
    129   {
    130     if ( loader )
    131     {
    132       FT_Memory memory = loader->memory;
    133 
    134 
    135       FT_GlyphLoader_Reset( loader );
    136       FT_FREE( loader );
    137     }
    138   }
    139 
    140 
    141   /* re-adjust the `current' outline fields */
    142   static void
    143   FT_GlyphLoader_Adjust_Points( FT_GlyphLoader  loader )
    144   {
    145     FT_Outline*  base    = &loader->base.outline;
    146     FT_Outline*  current = &loader->current.outline;
    147 
    148 
    149     current->points   = base->points   + base->n_points;
    150     current->tags     = base->tags     + base->n_points;
    151     current->contours = base->contours + base->n_contours;
    152 
    153     /* handle extra points table - if any */
    154     if ( loader->use_extra )
    155     {
    156       loader->current.extra_points  = loader->base.extra_points +
    157                                       base->n_points;
    158 
    159       loader->current.extra_points2 = loader->base.extra_points2 +
    160                                       base->n_points;
    161     }
    162   }
    163 
    164 
    165   FT_BASE_DEF( FT_Error )
    166   FT_GlyphLoader_CreateExtra( FT_GlyphLoader  loader )
    167   {
    168     FT_Error   error;
    169     FT_Memory  memory = loader->memory;
    170 
    171 
    172     if ( !FT_NEW_ARRAY( loader->base.extra_points, 2 * loader->max_points ) )
    173     {
    174       loader->use_extra          = 1;
    175       loader->base.extra_points2 = loader->base.extra_points +
    176                                    loader->max_points;
    177 
    178       FT_GlyphLoader_Adjust_Points( loader );
    179     }
    180     return error;
    181   }
    182 
    183 
    184   /* re-adjust the `current' subglyphs field */
    185   static void
    186   FT_GlyphLoader_Adjust_Subglyphs( FT_GlyphLoader  loader )
    187   {
    188     FT_GlyphLoad  base    = &loader->base;
    189     FT_GlyphLoad  current = &loader->current;
    190 
    191 
    192     current->subglyphs = base->subglyphs + base->num_subglyphs;
    193   }
    194 
    195 
    196   /* Ensure that we can add `n_points' and `n_contours' to our glyph.      */
    197   /* This function reallocates its outline tables if necessary.  Note that */
    198   /* it DOESN'T change the number of points within the loader!             */
    199   /*                                                                       */
    200   FT_BASE_DEF( FT_Error )
    201   FT_GlyphLoader_CheckPoints( FT_GlyphLoader  loader,
    202                               FT_UInt         n_points,
    203                               FT_UInt         n_contours )
    204   {
    205     FT_Memory    memory  = loader->memory;
    206     FT_Error     error   = FT_Err_Ok;
    207     FT_Outline*  base    = &loader->base.outline;
    208     FT_Outline*  current = &loader->current.outline;
    209     FT_Bool      adjust  = 0;
    210 
    211     FT_UInt      new_max, old_max;
    212 
    213 
    214     /* check points & tags */
    215     new_max = base->n_points + current->n_points + n_points;
    216     old_max = loader->max_points;
    217 
    218     if ( new_max > old_max )
    219     {
    220       new_max = FT_PAD_CEIL( new_max, 8 );
    221 
    222       if ( new_max > FT_OUTLINE_POINTS_MAX )
    223         return FT_THROW( Array_Too_Large );
    224 
    225       if ( FT_RENEW_ARRAY( base->points, old_max, new_max ) ||
    226            FT_RENEW_ARRAY( base->tags,   old_max, new_max ) )
    227         goto Exit;
    228 
    229       if ( loader->use_extra )
    230       {
    231         if ( FT_RENEW_ARRAY( loader->base.extra_points,
    232                              old_max * 2, new_max * 2 ) )
    233           goto Exit;
    234 
    235         FT_ARRAY_MOVE( loader->base.extra_points + new_max,
    236                        loader->base.extra_points + old_max,
    237                        old_max );
    238 
    239         loader->base.extra_points2 = loader->base.extra_points + new_max;
    240       }
    241 
    242       adjust = 1;
    243       loader->max_points = new_max;
    244     }
    245 
    246     /* check contours */
    247     old_max = loader->max_contours;
    248     new_max = base->n_contours + current->n_contours +
    249               n_contours;
    250     if ( new_max > old_max )
    251     {
    252       new_max = FT_PAD_CEIL( new_max, 4 );
    253 
    254       if ( new_max > FT_OUTLINE_CONTOURS_MAX )
    255         return FT_THROW( Array_Too_Large );
    256 
    257       if ( FT_RENEW_ARRAY( base->contours, old_max, new_max ) )
    258         goto Exit;
    259 
    260       adjust = 1;
    261       loader->max_contours = new_max;
    262     }
    263 
    264     if ( adjust )
    265       FT_GlyphLoader_Adjust_Points( loader );
    266 
    267   Exit:
    268     return error;
    269   }
    270 
    271 
    272   /* Ensure that we can add `n_subglyphs' to our glyph. this function */
    273   /* reallocates its subglyphs table if necessary.  Note that it DOES */
    274   /* NOT change the number of subglyphs within the loader!            */
    275   /*                                                                  */
    276   FT_BASE_DEF( FT_Error )
    277   FT_GlyphLoader_CheckSubGlyphs( FT_GlyphLoader  loader,
    278                                  FT_UInt         n_subs )
    279   {
    280     FT_Memory     memory = loader->memory;
    281     FT_Error      error  = FT_Err_Ok;
    282     FT_UInt       new_max, old_max;
    283 
    284     FT_GlyphLoad  base    = &loader->base;
    285     FT_GlyphLoad  current = &loader->current;
    286 
    287 
    288     new_max = base->num_subglyphs + current->num_subglyphs + n_subs;
    289     old_max = loader->max_subglyphs;
    290     if ( new_max > old_max )
    291     {
    292       new_max = FT_PAD_CEIL( new_max, 2 );
    293       if ( FT_RENEW_ARRAY( base->subglyphs, old_max, new_max ) )
    294         goto Exit;
    295 
    296       loader->max_subglyphs = new_max;
    297 
    298       FT_GlyphLoader_Adjust_Subglyphs( loader );
    299     }
    300 
    301   Exit:
    302     return error;
    303   }
    304 
    305 
    306   /* prepare loader for the addition of a new glyph on top of the base one */
    307   FT_BASE_DEF( void )
    308   FT_GlyphLoader_Prepare( FT_GlyphLoader  loader )
    309   {
    310     FT_GlyphLoad  current = &loader->current;
    311 
    312 
    313     current->outline.n_points   = 0;
    314     current->outline.n_contours = 0;
    315     current->num_subglyphs      = 0;
    316 
    317     FT_GlyphLoader_Adjust_Points   ( loader );
    318     FT_GlyphLoader_Adjust_Subglyphs( loader );
    319   }
    320 
    321 
    322   /* add current glyph to the base image -- and prepare for another */
    323   FT_BASE_DEF( void )
    324   FT_GlyphLoader_Add( FT_GlyphLoader  loader )
    325   {
    326     FT_GlyphLoad  base;
    327     FT_GlyphLoad  current;
    328 
    329     FT_UInt       n_curr_contours;
    330     FT_UInt       n_base_points;
    331     FT_UInt       n;
    332 
    333 
    334     if ( !loader )
    335       return;
    336 
    337     base    = &loader->base;
    338     current = &loader->current;
    339 
    340     n_curr_contours = current->outline.n_contours;
    341     n_base_points   = base->outline.n_points;
    342 
    343     base->outline.n_points =
    344       (short)( base->outline.n_points + current->outline.n_points );
    345     base->outline.n_contours =
    346       (short)( base->outline.n_contours + current->outline.n_contours );
    347 
    348     base->num_subglyphs += current->num_subglyphs;
    349 
    350     /* adjust contours count in newest outline */
    351     for ( n = 0; n < n_curr_contours; n++ )
    352       current->outline.contours[n] =
    353         (short)( current->outline.contours[n] + n_base_points );
    354 
    355     /* prepare for another new glyph image */
    356     FT_GlyphLoader_Prepare( loader );
    357   }
    358 
    359 
    360   FT_BASE_DEF( FT_Error )
    361   FT_GlyphLoader_CopyPoints( FT_GlyphLoader  target,
    362                              FT_GlyphLoader  source )
    363   {
    364     FT_Error  error;
    365     FT_UInt   num_points   = source->base.outline.n_points;
    366     FT_UInt   num_contours = source->base.outline.n_contours;
    367 
    368 
    369     error = FT_GlyphLoader_CheckPoints( target, num_points, num_contours );
    370     if ( !error )
    371     {
    372       FT_Outline*  out = &target->base.outline;
    373       FT_Outline*  in  = &source->base.outline;
    374 
    375 
    376       FT_ARRAY_COPY( out->points, in->points,
    377                      num_points );
    378       FT_ARRAY_COPY( out->tags, in->tags,
    379                      num_points );
    380       FT_ARRAY_COPY( out->contours, in->contours,
    381                      num_contours );
    382 
    383       /* do we need to copy the extra points? */
    384       if ( target->use_extra && source->use_extra )
    385       {
    386         FT_ARRAY_COPY( target->base.extra_points, source->base.extra_points,
    387                        num_points );
    388         FT_ARRAY_COPY( target->base.extra_points2, source->base.extra_points2,
    389                        num_points );
    390       }
    391 
    392       out->n_points   = (short)num_points;
    393       out->n_contours = (short)num_contours;
    394 
    395       FT_GlyphLoader_Adjust_Points( target );
    396     }
    397 
    398     return error;
    399   }
    400 
    401 
    402 /* END */
    403