1 /****************************************************************************
2  *
3  * ftgloadr.c
4  *
5  *   The FreeType glyph loader (body).
6  *
7  * Copyright (C) 2002-2020 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 <freetype/internal/ftdebug.h>
20 #include <freetype/internal/ftgloadr.h>
21 #include <freetype/internal/ftmemory.h>
22 #include <freetype/internal/ftobjs.h>
23 
24 #undef  FT_COMPONENT
25 #define FT_COMPONENT  gloader
26 
27 
28   /*************************************************************************/
29   /*************************************************************************/
30   /*************************************************************************/
31   /*****                                                               *****/
32   /*****                                                               *****/
33   /*****                    G L Y P H   L O A D E R                    *****/
34   /*****                                                               *****/
35   /*****                                                               *****/
36   /*************************************************************************/
37   /*************************************************************************/
38   /*************************************************************************/
39 
40   /**************************************************************************
41    *
42    * The glyph loader is a simple object which is used to load a set of
43    * glyphs easily.  It is critical for the correct loading of composites.
44    *
45    * Ideally, one can see it as a stack of abstract `glyph' objects.
46    *
47    *   loader.base     Is really the bottom of the stack.  It describes a
48    *                   single glyph image made of the juxtaposition of
49    *                   several glyphs (those `in the stack').
50    *
51    *   loader.current  Describes the top of the stack, on which a new
52    *                   glyph can be loaded.
53    *
54    *   Rewind          Clears the stack.
55    *   Prepare         Set up `loader.current' for addition of a new glyph
56    *                   image.
57    *   Add             Add the `current' glyph image to the `base' one,
58    *                   and prepare for another one.
59    *
60    * The glyph loader is now a base object.  Each driver used to
61    * re-implement it in one way or the other, which wasted code and
62    * energy.
63    *
64    */
65 
66 
67   /* create a new glyph loader */
68   FT_BASE_DEF( FT_Error )
FT_GlyphLoader_New(FT_Memory memory,FT_GlyphLoader * aloader)69   FT_GlyphLoader_New( FT_Memory        memory,
70                       FT_GlyphLoader  *aloader )
71   {
72     FT_GlyphLoader  loader = NULL;
73     FT_Error        error;
74 
75 
76     if ( !FT_NEW( loader ) )
77     {
78       loader->memory = memory;
79       *aloader       = loader;
80     }
81     return error;
82   }
83 
84 
85   /* rewind the glyph loader - reset counters to 0 */
86   FT_BASE_DEF( void )
FT_GlyphLoader_Rewind(FT_GlyphLoader loader)87   FT_GlyphLoader_Rewind( FT_GlyphLoader  loader )
88   {
89     FT_GlyphLoad  base    = &loader->base;
90     FT_GlyphLoad  current = &loader->current;
91 
92 
93     base->outline.n_points   = 0;
94     base->outline.n_contours = 0;
95     base->outline.flags      = 0;
96     base->num_subglyphs      = 0;
97 
98     *current = *base;
99   }
100 
101 
102   /* reset glyph loader, free all allocated tables, */
103   /* and start from zero                            */
104   FT_BASE_DEF( void )
FT_GlyphLoader_Reset(FT_GlyphLoader loader)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 )
FT_GlyphLoader_Done(FT_GlyphLoader loader)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
FT_GlyphLoader_Adjust_Points(FT_GlyphLoader loader)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   = FT_OFFSET( base->points,   base->n_points );
150     current->tags     = FT_OFFSET( base->tags,     base->n_points );
151     current->contours = FT_OFFSET( 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 )
FT_GlyphLoader_CreateExtra(FT_GlyphLoader loader)166   FT_GlyphLoader_CreateExtra( FT_GlyphLoader  loader )
167   {
168     FT_Error   error;
169     FT_Memory  memory = loader->memory;
170 
171 
172     if ( loader->max_points == 0           ||
173          loader->base.extra_points != NULL )
174       return FT_Err_Ok;
175 
176     if ( !FT_NEW_ARRAY( loader->base.extra_points, 2 * loader->max_points ) )
177     {
178       loader->use_extra          = 1;
179       loader->base.extra_points2 = loader->base.extra_points +
180                                    loader->max_points;
181 
182       FT_GlyphLoader_Adjust_Points( loader );
183     }
184     return error;
185   }
186 
187 
188   /* re-adjust the `current' subglyphs field */
189   static void
FT_GlyphLoader_Adjust_Subglyphs(FT_GlyphLoader loader)190   FT_GlyphLoader_Adjust_Subglyphs( FT_GlyphLoader  loader )
191   {
192     FT_GlyphLoad  base    = &loader->base;
193     FT_GlyphLoad  current = &loader->current;
194 
195 
196     current->subglyphs = FT_OFFSET( base->subglyphs, base->num_subglyphs );
197   }
198 
199 
200   /* Ensure that we can add `n_points' and `n_contours' to our glyph.      */
201   /* This function reallocates its outline tables if necessary.  Note that */
202   /* it DOESN'T change the number of points within the loader!             */
203   /*                                                                       */
204   FT_BASE_DEF( FT_Error )
FT_GlyphLoader_CheckPoints(FT_GlyphLoader loader,FT_UInt n_points,FT_UInt n_contours)205   FT_GlyphLoader_CheckPoints( FT_GlyphLoader  loader,
206                               FT_UInt         n_points,
207                               FT_UInt         n_contours )
208   {
209     FT_Memory    memory  = loader->memory;
210     FT_Error     error   = FT_Err_Ok;
211     FT_Outline*  base    = &loader->base.outline;
212     FT_Outline*  current = &loader->current.outline;
213     FT_Bool      adjust  = 0;
214 
215     FT_UInt      new_max, old_max;
216 
217 
218     error = FT_GlyphLoader_CreateExtra( loader );
219     if ( error )
220       return error;
221 
222     /* check points & tags */
223     new_max = (FT_UInt)base->n_points + (FT_UInt)current->n_points +
224               n_points;
225     old_max = loader->max_points;
226 
227     if ( new_max > old_max )
228     {
229       new_max = FT_PAD_CEIL( new_max, 8 );
230 
231       if ( new_max > FT_OUTLINE_POINTS_MAX )
232         return FT_THROW( Array_Too_Large );
233 
234       if ( FT_RENEW_ARRAY( base->points, old_max, new_max ) ||
235            FT_RENEW_ARRAY( base->tags,   old_max, new_max ) )
236         goto Exit;
237 
238       if ( loader->use_extra )
239       {
240         if ( FT_RENEW_ARRAY( loader->base.extra_points,
241                              old_max * 2, new_max * 2 ) )
242           goto Exit;
243 
244         FT_ARRAY_MOVE( loader->base.extra_points + new_max,
245                        loader->base.extra_points + old_max,
246                        old_max );
247 
248         loader->base.extra_points2 = loader->base.extra_points + new_max;
249       }
250 
251       adjust = 1;
252       loader->max_points = new_max;
253     }
254 
255     error = FT_GlyphLoader_CreateExtra( loader );
256     if ( error )
257       return error;
258 
259     /* check contours */
260     old_max = loader->max_contours;
261     new_max = (FT_UInt)base->n_contours + (FT_UInt)current->n_contours +
262               n_contours;
263     if ( new_max > old_max )
264     {
265       new_max = FT_PAD_CEIL( new_max, 4 );
266 
267       if ( new_max > FT_OUTLINE_CONTOURS_MAX )
268         return FT_THROW( Array_Too_Large );
269 
270       if ( FT_RENEW_ARRAY( base->contours, old_max, new_max ) )
271         goto Exit;
272 
273       adjust = 1;
274       loader->max_contours = new_max;
275     }
276 
277     if ( adjust )
278       FT_GlyphLoader_Adjust_Points( loader );
279 
280   Exit:
281     if ( error )
282       FT_GlyphLoader_Reset( loader );
283 
284     return error;
285   }
286 
287 
288   /* Ensure that we can add `n_subglyphs' to our glyph. this function */
289   /* reallocates its subglyphs table if necessary.  Note that it DOES */
290   /* NOT change the number of subglyphs within the loader!            */
291   /*                                                                  */
292   FT_BASE_DEF( FT_Error )
FT_GlyphLoader_CheckSubGlyphs(FT_GlyphLoader loader,FT_UInt n_subs)293   FT_GlyphLoader_CheckSubGlyphs( FT_GlyphLoader  loader,
294                                  FT_UInt         n_subs )
295   {
296     FT_Memory     memory = loader->memory;
297     FT_Error      error  = FT_Err_Ok;
298     FT_UInt       new_max, old_max;
299 
300     FT_GlyphLoad  base    = &loader->base;
301     FT_GlyphLoad  current = &loader->current;
302 
303 
304     new_max = base->num_subglyphs + current->num_subglyphs + n_subs;
305     old_max = loader->max_subglyphs;
306     if ( new_max > old_max )
307     {
308       new_max = FT_PAD_CEIL( new_max, 2 );
309       if ( FT_RENEW_ARRAY( base->subglyphs, old_max, new_max ) )
310         goto Exit;
311 
312       loader->max_subglyphs = new_max;
313 
314       FT_GlyphLoader_Adjust_Subglyphs( loader );
315     }
316 
317   Exit:
318     return error;
319   }
320 
321 
322   /* prepare loader for the addition of a new glyph on top of the base one */
323   FT_BASE_DEF( void )
FT_GlyphLoader_Prepare(FT_GlyphLoader loader)324   FT_GlyphLoader_Prepare( FT_GlyphLoader  loader )
325   {
326     FT_GlyphLoad  current = &loader->current;
327 
328 
329     current->outline.n_points   = 0;
330     current->outline.n_contours = 0;
331     current->num_subglyphs      = 0;
332 
333     FT_GlyphLoader_Adjust_Points   ( loader );
334     FT_GlyphLoader_Adjust_Subglyphs( loader );
335   }
336 
337 
338   /* add current glyph to the base image -- and prepare for another */
339   FT_BASE_DEF( void )
FT_GlyphLoader_Add(FT_GlyphLoader loader)340   FT_GlyphLoader_Add( FT_GlyphLoader  loader )
341   {
342     FT_GlyphLoad  base;
343     FT_GlyphLoad  current;
344 
345     FT_Int        n_curr_contours;
346     FT_Int        n_base_points;
347     FT_Int        n;
348 
349 
350     if ( !loader )
351       return;
352 
353     base    = &loader->base;
354     current = &loader->current;
355 
356     n_curr_contours = current->outline.n_contours;
357     n_base_points   = base->outline.n_points;
358 
359     base->outline.n_points =
360       (short)( base->outline.n_points + current->outline.n_points );
361     base->outline.n_contours =
362       (short)( base->outline.n_contours + current->outline.n_contours );
363 
364     base->num_subglyphs += current->num_subglyphs;
365 
366     /* adjust contours count in newest outline */
367     for ( n = 0; n < n_curr_contours; n++ )
368       current->outline.contours[n] =
369         (short)( current->outline.contours[n] + n_base_points );
370 
371     /* prepare for another new glyph image */
372     FT_GlyphLoader_Prepare( loader );
373   }
374 
375 
376 /* END */
377