ftgloadr.c revision ee451cb395940862dad63c85adfe8f2fd55e864c
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 "../../include/ft2build.h"
20#include "../../include/freetype/internal/ftdebug.h"
21#include "../../include/freetype/internal/ftgloadr.h"
22#include "../../include/freetype/internal/ftmemory.h"
23#include "../../include/freetype/internal/ftobjs.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    if ( error )
269      FT_GlyphLoader_Reset( loader );
270
271    return error;
272  }
273
274
275  /* Ensure that we can add `n_subglyphs' to our glyph. this function */
276  /* reallocates its subglyphs table if necessary.  Note that it DOES */
277  /* NOT change the number of subglyphs within the loader!            */
278  /*                                                                  */
279  FT_BASE_DEF( FT_Error )
280  FT_GlyphLoader_CheckSubGlyphs( FT_GlyphLoader  loader,
281                                 FT_UInt         n_subs )
282  {
283    FT_Memory     memory = loader->memory;
284    FT_Error      error  = FT_Err_Ok;
285    FT_UInt       new_max, old_max;
286
287    FT_GlyphLoad  base    = &loader->base;
288    FT_GlyphLoad  current = &loader->current;
289
290
291    new_max = base->num_subglyphs + current->num_subglyphs + n_subs;
292    old_max = loader->max_subglyphs;
293    if ( new_max > old_max )
294    {
295      new_max = FT_PAD_CEIL( new_max, 2 );
296      if ( FT_RENEW_ARRAY( base->subglyphs, old_max, new_max ) )
297        goto Exit;
298
299      loader->max_subglyphs = new_max;
300
301      FT_GlyphLoader_Adjust_Subglyphs( loader );
302    }
303
304  Exit:
305    return error;
306  }
307
308
309  /* prepare loader for the addition of a new glyph on top of the base one */
310  FT_BASE_DEF( void )
311  FT_GlyphLoader_Prepare( FT_GlyphLoader  loader )
312  {
313    FT_GlyphLoad  current = &loader->current;
314
315
316    current->outline.n_points   = 0;
317    current->outline.n_contours = 0;
318    current->num_subglyphs      = 0;
319
320    FT_GlyphLoader_Adjust_Points   ( loader );
321    FT_GlyphLoader_Adjust_Subglyphs( loader );
322  }
323
324
325  /* add current glyph to the base image -- and prepare for another */
326  FT_BASE_DEF( void )
327  FT_GlyphLoader_Add( FT_GlyphLoader  loader )
328  {
329    FT_GlyphLoad  base;
330    FT_GlyphLoad  current;
331
332    FT_UInt       n_curr_contours;
333    FT_UInt       n_base_points;
334    FT_UInt       n;
335
336
337    if ( !loader )
338      return;
339
340    base    = &loader->base;
341    current = &loader->current;
342
343    n_curr_contours = current->outline.n_contours;
344    n_base_points   = base->outline.n_points;
345
346    base->outline.n_points =
347      (short)( base->outline.n_points + current->outline.n_points );
348    base->outline.n_contours =
349      (short)( base->outline.n_contours + current->outline.n_contours );
350
351    base->num_subglyphs += current->num_subglyphs;
352
353    /* adjust contours count in newest outline */
354    for ( n = 0; n < n_curr_contours; n++ )
355      current->outline.contours[n] =
356        (short)( current->outline.contours[n] + n_base_points );
357
358    /* prepare for another new glyph image */
359    FT_GlyphLoader_Prepare( loader );
360  }
361
362
363  FT_BASE_DEF( FT_Error )
364  FT_GlyphLoader_CopyPoints( FT_GlyphLoader  target,
365                             FT_GlyphLoader  source )
366  {
367    FT_Error  error;
368    FT_UInt   num_points   = source->base.outline.n_points;
369    FT_UInt   num_contours = source->base.outline.n_contours;
370
371
372    error = FT_GlyphLoader_CheckPoints( target, num_points, num_contours );
373    if ( !error )
374    {
375      FT_Outline*  out = &target->base.outline;
376      FT_Outline*  in  = &source->base.outline;
377
378
379      FT_ARRAY_COPY( out->points, in->points,
380                     num_points );
381      FT_ARRAY_COPY( out->tags, in->tags,
382                     num_points );
383      FT_ARRAY_COPY( out->contours, in->contours,
384                     num_contours );
385
386      /* do we need to copy the extra points? */
387      if ( target->use_extra && source->use_extra )
388      {
389        FT_ARRAY_COPY( target->base.extra_points, source->base.extra_points,
390                       num_points );
391        FT_ARRAY_COPY( target->base.extra_points2, source->base.extra_points2,
392                       num_points );
393      }
394
395      out->n_points   = (short)num_points;
396      out->n_contours = (short)num_contours;
397
398      FT_GlyphLoader_Adjust_Points( target );
399    }
400
401    return error;
402  }
403
404
405/* END */
406