1 /***************************************************************************/
5 /* The FreeType glyph loader (body). */
7 /* Copyright 2002-2006, 2010, 2013 by */
8 /* David Turner, Robert Wilhelm, and Werner Lemberg */
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. */
16 /***************************************************************************/
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
26 #define FT_COMPONENT trace_gloader
29 /*************************************************************************/
30 /*************************************************************************/
31 /*************************************************************************/
34 /***** G L Y P H L O A D E R *****/
37 /*************************************************************************/
38 /*************************************************************************/
39 /*************************************************************************/
41 /*************************************************************************/
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. */
46 /* Ideally, one can see it as a stack of abstract `glyph' objects. */
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'). */
52 /* loader.current Describes the top of the stack, on which a new */
53 /* glyph can be loaded. */
55 /* Rewind Clears the stack. */
56 /* Prepare Set up `loader.current' for addition of a new glyph */
58 /* Add Add the `current' glyph image to the `base' one, */
59 /* and prepare for another one. */
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 */
65 /*************************************************************************/
68 /* create a new glyph loader */
69 FT_BASE_DEF( FT_Error )
70 FT_GlyphLoader_New( FT_Memory memory,
71 FT_GlyphLoader *aloader )
73 FT_GlyphLoader loader = NULL;
77 if ( !FT_NEW( loader ) )
79 loader->memory = memory;
86 /* rewind the glyph loader - reset counters to 0 */
88 FT_GlyphLoader_Rewind( FT_GlyphLoader loader )
90 FT_GlyphLoad base = &loader->base;
91 FT_GlyphLoad current = &loader->current;
94 base->outline.n_points = 0;
95 base->outline.n_contours = 0;
96 base->num_subglyphs = 0;
102 /* reset the glyph loader, frees all allocated tables */
103 /* and starts from zero */
105 FT_GlyphLoader_Reset( FT_GlyphLoader loader )
107 FT_Memory memory = loader->memory;
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 );
116 loader->base.extra_points2 = NULL;
118 loader->max_points = 0;
119 loader->max_contours = 0;
120 loader->max_subglyphs = 0;
122 FT_GlyphLoader_Rewind( loader );
126 /* delete a glyph loader */
128 FT_GlyphLoader_Done( FT_GlyphLoader loader )
132 FT_Memory memory = loader->memory;
135 FT_GlyphLoader_Reset( loader );
141 /* re-adjust the `current' outline fields */
143 FT_GlyphLoader_Adjust_Points( FT_GlyphLoader loader )
145 FT_Outline* base = &loader->base.outline;
146 FT_Outline* current = &loader->current.outline;
149 current->points = base->points + base->n_points;
150 current->tags = base->tags + base->n_points;
151 current->contours = base->contours + base->n_contours;
153 /* handle extra points table - if any */
154 if ( loader->use_extra )
156 loader->current.extra_points = loader->base.extra_points +
159 loader->current.extra_points2 = loader->base.extra_points2 +
165 FT_BASE_DEF( FT_Error )
166 FT_GlyphLoader_CreateExtra( FT_GlyphLoader loader )
169 FT_Memory memory = loader->memory;
172 if ( !FT_NEW_ARRAY( loader->base.extra_points, 2 * loader->max_points ) )
174 loader->use_extra = 1;
175 loader->base.extra_points2 = loader->base.extra_points +
178 FT_GlyphLoader_Adjust_Points( loader );
184 /* re-adjust the `current' subglyphs field */
186 FT_GlyphLoader_Adjust_Subglyphs( FT_GlyphLoader loader )
188 FT_GlyphLoad base = &loader->base;
189 FT_GlyphLoad current = &loader->current;
192 current->subglyphs = base->subglyphs + base->num_subglyphs;
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! */
200 FT_BASE_DEF( FT_Error )
201 FT_GlyphLoader_CheckPoints( FT_GlyphLoader loader,
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;
211 FT_UInt new_max, old_max;
214 /* check points & tags */
215 new_max = base->n_points + current->n_points + n_points;
216 old_max = loader->max_points;
218 if ( new_max > old_max )
220 new_max = FT_PAD_CEIL( new_max, 8 );
222 if ( new_max > FT_OUTLINE_POINTS_MAX )
223 return FT_THROW( Array_Too_Large );
225 if ( FT_RENEW_ARRAY( base->points, old_max, new_max ) ||
226 FT_RENEW_ARRAY( base->tags, old_max, new_max ) )
229 if ( loader->use_extra )
231 if ( FT_RENEW_ARRAY( loader->base.extra_points,
232 old_max * 2, new_max * 2 ) )
235 FT_ARRAY_MOVE( loader->base.extra_points + new_max,
236 loader->base.extra_points + old_max,
239 loader->base.extra_points2 = loader->base.extra_points + new_max;
243 loader->max_points = new_max;
247 old_max = loader->max_contours;
248 new_max = base->n_contours + current->n_contours +
250 if ( new_max > old_max )
252 new_max = FT_PAD_CEIL( new_max, 4 );
254 if ( new_max > FT_OUTLINE_CONTOURS_MAX )
255 return FT_THROW( Array_Too_Large );
257 if ( FT_RENEW_ARRAY( base->contours, old_max, new_max ) )
261 loader->max_contours = new_max;
265 FT_GlyphLoader_Adjust_Points( loader );
269 FT_GlyphLoader_Reset( loader );
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! */
279 FT_BASE_DEF( FT_Error )
280 FT_GlyphLoader_CheckSubGlyphs( FT_GlyphLoader loader,
283 FT_Memory memory = loader->memory;
284 FT_Error error = FT_Err_Ok;
285 FT_UInt new_max, old_max;
287 FT_GlyphLoad base = &loader->base;
288 FT_GlyphLoad current = &loader->current;
291 new_max = base->num_subglyphs + current->num_subglyphs + n_subs;
292 old_max = loader->max_subglyphs;
293 if ( new_max > old_max )
295 new_max = FT_PAD_CEIL( new_max, 2 );
296 if ( FT_RENEW_ARRAY( base->subglyphs, old_max, new_max ) )
299 loader->max_subglyphs = new_max;
301 FT_GlyphLoader_Adjust_Subglyphs( loader );
309 /* prepare loader for the addition of a new glyph on top of the base one */
311 FT_GlyphLoader_Prepare( FT_GlyphLoader loader )
313 FT_GlyphLoad current = &loader->current;
316 current->outline.n_points = 0;
317 current->outline.n_contours = 0;
318 current->num_subglyphs = 0;
320 FT_GlyphLoader_Adjust_Points ( loader );
321 FT_GlyphLoader_Adjust_Subglyphs( loader );
325 /* add current glyph to the base image -- and prepare for another */
327 FT_GlyphLoader_Add( FT_GlyphLoader loader )
330 FT_GlyphLoad current;
332 FT_UInt n_curr_contours;
333 FT_UInt n_base_points;
340 base = &loader->base;
341 current = &loader->current;
343 n_curr_contours = current->outline.n_contours;
344 n_base_points = base->outline.n_points;
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 );
351 base->num_subglyphs += current->num_subglyphs;
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 );
358 /* prepare for another new glyph image */
359 FT_GlyphLoader_Prepare( loader );
363 FT_BASE_DEF( FT_Error )
364 FT_GlyphLoader_CopyPoints( FT_GlyphLoader target,
365 FT_GlyphLoader source )
368 FT_UInt num_points = source->base.outline.n_points;
369 FT_UInt num_contours = source->base.outline.n_contours;
372 error = FT_GlyphLoader_CheckPoints( target, num_points, num_contours );
375 FT_Outline* out = &target->base.outline;
376 FT_Outline* in = &source->base.outline;
379 FT_ARRAY_COPY( out->points, in->points,
381 FT_ARRAY_COPY( out->tags, in->tags,
383 FT_ARRAY_COPY( out->contours, in->contours,
386 /* do we need to copy the extra points? */
387 if ( target->use_extra && source->use_extra )
389 FT_ARRAY_COPY( target->base.extra_points, source->base.extra_points,
391 FT_ARRAY_COPY( target->base.extra_points2, source->base.extra_points2,
395 out->n_points = (short)num_points;
396 out->n_contours = (short)num_contours;
398 FT_GlyphLoader_Adjust_Points( target );