1 /***************************************************************************/
5 /* The FreeType glyph loader (body). */
7 /* Copyright 2002 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_GLYPH_LOADER_H
21 #include FT_INTERNAL_MEMORY_H
24 #define FT_COMPONENT trace_gloader
27 /*************************************************************************/
28 /*************************************************************************/
29 /*************************************************************************/
32 /***** G L Y P H L O A D E R *****/
35 /*************************************************************************/
36 /*************************************************************************/
37 /*************************************************************************/
39 /*************************************************************************/
41 /* The glyph loader is a simple object which is used to load a set of */
42 /* glyphs easily. It is critical for the correct loading of composites. */
44 /* Ideally, one can see it as a stack of abstract `glyph' objects. */
46 /* loader.base Is really the bottom of the stack. It describes a */
47 /* single glyph image made of the juxtaposition of */
48 /* several glyphs (those `in the stack'). */
50 /* loader.current Describes the top of the stack, on which a new */
51 /* glyph can be loaded. */
53 /* Rewind Clears the stack. */
54 /* Prepare Set up `loader.current' for addition of a new glyph */
56 /* Add Add the `current' glyph image to the `base' one, */
57 /* and prepare for another one. */
59 /* The glyph loader is now a base object. Each driver used to */
60 /* re-implement it in one way or the other, which wasted code and */
63 /*************************************************************************/
66 /* create a new glyph loader */
67 FT_BASE_DEF( FT_Error )
68 FT_GlyphLoader_New( FT_Memory memory,
69 FT_GlyphLoader *aloader )
71 FT_GlyphLoader loader;
75 if ( !FT_NEW( loader ) )
77 loader->memory = memory;
84 /* rewind the glyph loader - reset counters to 0 */
86 FT_GlyphLoader_Rewind( FT_GlyphLoader loader )
88 FT_GlyphLoad base = &loader->base;
89 FT_GlyphLoad current = &loader->current;
92 base->outline.n_points = 0;
93 base->outline.n_contours = 0;
94 base->num_subglyphs = 0;
100 /* reset the glyph loader, frees all allocated tables */
101 /* and starts from zero */
103 FT_GlyphLoader_Reset( FT_GlyphLoader loader )
105 FT_Memory memory = loader->memory;
108 FT_FREE( loader->base.outline.points );
109 FT_FREE( loader->base.outline.tags );
110 FT_FREE( loader->base.outline.contours );
111 FT_FREE( loader->base.extra_points );
112 FT_FREE( loader->base.subglyphs );
114 loader->max_points = 0;
115 loader->max_contours = 0;
116 loader->max_subglyphs = 0;
118 FT_GlyphLoader_Rewind( loader );
122 /* delete a glyph loader */
124 FT_GlyphLoader_Done( FT_GlyphLoader loader )
128 FT_Memory memory = loader->memory;
131 FT_GlyphLoader_Reset( loader );
137 /* re-adjust the `current' outline fields */
139 FT_GlyphLoader_Adjust_Points( FT_GlyphLoader loader )
141 FT_Outline* base = &loader->base.outline;
142 FT_Outline* current = &loader->current.outline;
145 current->points = base->points + base->n_points;
146 current->tags = base->tags + base->n_points;
147 current->contours = base->contours + base->n_contours;
149 /* handle extra points table - if any */
150 if ( loader->use_extra )
151 loader->current.extra_points =
152 loader->base.extra_points + base->n_points;
156 FT_BASE_DEF( FT_Error )
157 FT_GlyphLoader_CreateExtra( FT_GlyphLoader loader )
160 FT_Memory memory = loader->memory;
163 if ( !FT_NEW_ARRAY( loader->base.extra_points, loader->max_points ) )
165 loader->use_extra = 1;
166 FT_GlyphLoader_Adjust_Points( loader );
172 /* re-adjust the `current' subglyphs field */
174 FT_GlyphLoader_Adjust_Subglyphs( FT_GlyphLoader loader )
176 FT_GlyphLoad base = &loader->base;
177 FT_GlyphLoad current = &loader->current;
180 current->subglyphs = base->subglyphs + base->num_subglyphs;
184 /* Ensure that we can add `n_points' and `n_contours' to our glyph. this */
185 /* function reallocates its outline tables if necessary. Note that it */
186 /* DOESN'T change the number of points within the loader! */
188 FT_BASE_DEF( FT_Error )
189 FT_GlyphLoader_CheckPoints( FT_GlyphLoader loader,
193 FT_Memory memory = loader->memory;
194 FT_Error error = FT_Err_Ok;
195 FT_Outline* base = &loader->base.outline;
196 FT_Outline* current = &loader->current.outline;
199 FT_UInt new_max, old_max;
202 /* check points & tags */
203 new_max = base->n_points + current->n_points + n_points;
204 old_max = loader->max_points;
206 if ( new_max > old_max )
208 new_max = ( new_max + 7 ) & -8;
210 if ( FT_RENEW_ARRAY( base->points, old_max, new_max ) ||
211 FT_RENEW_ARRAY( base->tags, old_max, new_max ) )
214 if ( loader->use_extra &&
215 FT_RENEW_ARRAY( loader->base.extra_points, old_max, new_max ) )
219 loader->max_points = new_max;
223 old_max = loader->max_contours;
224 new_max = base->n_contours + current->n_contours +
226 if ( new_max > old_max )
228 new_max = ( new_max + 3 ) & -4;
229 if ( FT_RENEW_ARRAY( base->contours, old_max, new_max ) )
233 loader->max_contours = new_max;
237 FT_GlyphLoader_Adjust_Points( loader );
244 /* Ensure that we can add `n_subglyphs' to our glyph. this function */
245 /* reallocates its subglyphs table if necessary. Note that it DOES */
246 /* NOT change the number of subglyphs within the loader! */
248 FT_BASE_DEF( FT_Error )
249 FT_GlyphLoader_CheckSubGlyphs( FT_GlyphLoader loader,
252 FT_Memory memory = loader->memory;
253 FT_Error error = FT_Err_Ok;
254 FT_UInt new_max, old_max;
256 FT_GlyphLoad base = &loader->base;
257 FT_GlyphLoad current = &loader->current;
260 new_max = base->num_subglyphs + current->num_subglyphs + n_subs;
261 old_max = loader->max_subglyphs;
262 if ( new_max > old_max )
264 new_max = ( new_max + 1 ) & -2;
265 if ( FT_RENEW_ARRAY( base->subglyphs, old_max, new_max ) )
268 loader->max_subglyphs = new_max;
270 FT_GlyphLoader_Adjust_Subglyphs( loader );
278 /* prepare loader for the addition of a new glyph on top of the base one */
280 FT_GlyphLoader_Prepare( FT_GlyphLoader loader )
282 FT_GlyphLoad current = &loader->current;
285 current->outline.n_points = 0;
286 current->outline.n_contours = 0;
287 current->num_subglyphs = 0;
289 FT_GlyphLoader_Adjust_Points ( loader );
290 FT_GlyphLoader_Adjust_Subglyphs( loader );
294 /* add current glyph to the base image - and prepare for another */
296 FT_GlyphLoader_Add( FT_GlyphLoader loader )
298 FT_GlyphLoad base = &loader->base;
299 FT_GlyphLoad current = &loader->current;
301 FT_UInt n_curr_contours = current->outline.n_contours;
302 FT_UInt n_base_points = base->outline.n_points;
306 base->outline.n_points =
307 (short)( base->outline.n_points + current->outline.n_points );
308 base->outline.n_contours =
309 (short)( base->outline.n_contours + current->outline.n_contours );
311 base->num_subglyphs += current->num_subglyphs;
313 /* adjust contours count in newest outline */
314 for ( n = 0; n < n_curr_contours; n++ )
315 current->outline.contours[n] =
316 (short)( current->outline.contours[n] + n_base_points );
318 /* prepare for another new glyph image */
319 FT_GlyphLoader_Prepare( loader );
323 FT_BASE_DEF( FT_Error )
324 FT_GlyphLoader_CopyPoints( FT_GlyphLoader target,
325 FT_GlyphLoader source )
328 FT_UInt num_points = source->base.outline.n_points;
329 FT_UInt num_contours = source->base.outline.n_contours;
332 error = FT_GlyphLoader_CheckPoints( target, num_points, num_contours );
335 FT_Outline* out = &target->base.outline;
336 FT_Outline* in = &source->base.outline;
339 FT_MEM_COPY( out->points, in->points,
340 num_points * sizeof ( FT_Vector ) );
341 FT_MEM_COPY( out->tags, in->tags,
342 num_points * sizeof ( char ) );
343 FT_MEM_COPY( out->contours, in->contours,
344 num_contours * sizeof ( short ) );
346 /* do we need to copy the extra points? */
347 if ( target->use_extra && source->use_extra )
348 FT_MEM_COPY( target->base.extra_points, source->base.extra_points,
349 num_points * sizeof ( FT_Vector ) );
351 out->n_points = (short)num_points;
352 out->n_contours = (short)num_contours;
354 FT_GlyphLoader_Adjust_Points( target );