1 /***************************************************************************/
5 /* The FreeType glyph loader (body). */
7 /* Copyright 2002, 2003, 2004, 2005, 2006 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
22 #include FT_INTERNAL_OBJECTS_H
25 #define FT_COMPONENT trace_gloader
28 /*************************************************************************/
29 /*************************************************************************/
30 /*************************************************************************/
33 /***** G L Y P H L O A D E R *****/
36 /*************************************************************************/
37 /*************************************************************************/
38 /*************************************************************************/
40 /*************************************************************************/
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. */
45 /* Ideally, one can see it as a stack of abstract `glyph' objects. */
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'). */
51 /* loader.current Describes the top of the stack, on which a new */
52 /* glyph can be loaded. */
54 /* Rewind Clears the stack. */
55 /* Prepare Set up `loader.current' for addition of a new glyph */
57 /* Add Add the `current' glyph image to the `base' one, */
58 /* and prepare for another one. */
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 */
64 /*************************************************************************/
67 /* create a new glyph loader */
68 FT_BASE_DEF( FT_Error
)
69 FT_GlyphLoader_New( FT_Memory memory
,
70 FT_GlyphLoader
*aloader
)
72 FT_GlyphLoader loader
;
76 if ( !FT_NEW( loader
) )
78 loader
->memory
= memory
;
85 /* rewind the glyph loader - reset counters to 0 */
87 FT_GlyphLoader_Rewind( FT_GlyphLoader loader
)
89 FT_GlyphLoad base
= &loader
->base
;
90 FT_GlyphLoad current
= &loader
->current
;
93 base
->outline
.n_points
= 0;
94 base
->outline
.n_contours
= 0;
95 base
->num_subglyphs
= 0;
101 /* reset the glyph loader, frees all allocated tables */
102 /* and starts from zero */
104 FT_GlyphLoader_Reset( FT_GlyphLoader loader
)
106 FT_Memory memory
= loader
->memory
;
109 FT_FREE( loader
->base
.outline
.points
);
110 FT_FREE( loader
->base
.outline
.tags
);
111 FT_FREE( loader
->base
.outline
.contours
);
112 FT_FREE( loader
->base
.extra_points
);
113 FT_FREE( loader
->base
.subglyphs
);
115 loader
->base
.extra_points2
= NULL
;
117 loader
->max_points
= 0;
118 loader
->max_contours
= 0;
119 loader
->max_subglyphs
= 0;
121 FT_GlyphLoader_Rewind( loader
);
125 /* delete a glyph loader */
127 FT_GlyphLoader_Done( FT_GlyphLoader loader
)
131 FT_Memory memory
= loader
->memory
;
134 FT_GlyphLoader_Reset( loader
);
140 /* re-adjust the `current' outline fields */
142 FT_GlyphLoader_Adjust_Points( FT_GlyphLoader loader
)
144 FT_Outline
* base
= &loader
->base
.outline
;
145 FT_Outline
* current
= &loader
->current
.outline
;
148 current
->points
= base
->points
+ base
->n_points
;
149 current
->tags
= base
->tags
+ base
->n_points
;
150 current
->contours
= base
->contours
+ base
->n_contours
;
152 /* handle extra points table - if any */
153 if ( loader
->use_extra
)
155 loader
->current
.extra_points
= loader
->base
.extra_points
+
158 loader
->current
.extra_points2
= loader
->base
.extra_points2
+
164 FT_BASE_DEF( FT_Error
)
165 FT_GlyphLoader_CreateExtra( FT_GlyphLoader loader
)
168 FT_Memory memory
= loader
->memory
;
171 if ( !FT_NEW_ARRAY( loader
->base
.extra_points
, 2 * loader
->max_points
) )
173 loader
->use_extra
= 1;
174 loader
->base
.extra_points2
= loader
->base
.extra_points
+
177 FT_GlyphLoader_Adjust_Points( loader
);
183 /* re-adjust the `current' subglyphs field */
185 FT_GlyphLoader_Adjust_Subglyphs( FT_GlyphLoader loader
)
187 FT_GlyphLoad base
= &loader
->base
;
188 FT_GlyphLoad current
= &loader
->current
;
191 current
->subglyphs
= base
->subglyphs
+ base
->num_subglyphs
;
195 /* Ensure that we can add `n_points' and `n_contours' to our glyph. */
196 /* This function reallocates its outline tables if necessary. Note that */
197 /* it DOESN'T change the number of points within the loader! */
199 FT_BASE_DEF( FT_Error
)
200 FT_GlyphLoader_CheckPoints( FT_GlyphLoader loader
,
204 FT_Memory memory
= loader
->memory
;
205 FT_Error error
= FT_Err_Ok
;
206 FT_Outline
* base
= &loader
->base
.outline
;
207 FT_Outline
* current
= &loader
->current
.outline
;
210 FT_UInt new_max
, old_max
;
213 /* check points & tags */
214 new_max
= base
->n_points
+ current
->n_points
+ n_points
;
215 old_max
= loader
->max_points
;
217 if ( new_max
> old_max
)
219 new_max
= FT_PAD_CEIL( new_max
, 8 );
221 if ( FT_RENEW_ARRAY( base
->points
, old_max
, new_max
) ||
222 FT_RENEW_ARRAY( base
->tags
, old_max
, new_max
) )
225 if ( loader
->use_extra
)
227 if ( FT_RENEW_ARRAY( loader
->base
.extra_points
,
228 old_max
* 2, new_max
* 2 ) )
231 FT_ARRAY_MOVE( loader
->base
.extra_points
+ new_max
,
232 loader
->base
.extra_points
+ old_max
,
235 loader
->base
.extra_points2
= loader
->base
.extra_points
+ new_max
;
239 loader
->max_points
= new_max
;
243 old_max
= loader
->max_contours
;
244 new_max
= base
->n_contours
+ current
->n_contours
+
246 if ( new_max
> old_max
)
248 new_max
= FT_PAD_CEIL( new_max
, 4 );
249 if ( FT_RENEW_ARRAY( base
->contours
, old_max
, new_max
) )
253 loader
->max_contours
= new_max
;
257 FT_GlyphLoader_Adjust_Points( loader
);
264 /* Ensure that we can add `n_subglyphs' to our glyph. this function */
265 /* reallocates its subglyphs table if necessary. Note that it DOES */
266 /* NOT change the number of subglyphs within the loader! */
268 FT_BASE_DEF( FT_Error
)
269 FT_GlyphLoader_CheckSubGlyphs( FT_GlyphLoader loader
,
272 FT_Memory memory
= loader
->memory
;
273 FT_Error error
= FT_Err_Ok
;
274 FT_UInt new_max
, old_max
;
276 FT_GlyphLoad base
= &loader
->base
;
277 FT_GlyphLoad current
= &loader
->current
;
280 new_max
= base
->num_subglyphs
+ current
->num_subglyphs
+ n_subs
;
281 old_max
= loader
->max_subglyphs
;
282 if ( new_max
> old_max
)
284 new_max
= FT_PAD_CEIL( new_max
, 2 );
285 if ( FT_RENEW_ARRAY( base
->subglyphs
, old_max
, new_max
) )
288 loader
->max_subglyphs
= new_max
;
290 FT_GlyphLoader_Adjust_Subglyphs( loader
);
298 /* prepare loader for the addition of a new glyph on top of the base one */
300 FT_GlyphLoader_Prepare( FT_GlyphLoader loader
)
302 FT_GlyphLoad current
= &loader
->current
;
305 current
->outline
.n_points
= 0;
306 current
->outline
.n_contours
= 0;
307 current
->num_subglyphs
= 0;
309 FT_GlyphLoader_Adjust_Points ( loader
);
310 FT_GlyphLoader_Adjust_Subglyphs( loader
);
314 /* add current glyph to the base image - and prepare for another */
316 FT_GlyphLoader_Add( FT_GlyphLoader loader
)
319 FT_GlyphLoad current
;
321 FT_UInt n_curr_contours
;
322 FT_UInt n_base_points
;
329 base
= &loader
->base
;
330 current
= &loader
->current
;
332 n_curr_contours
= current
->outline
.n_contours
;
333 n_base_points
= base
->outline
.n_points
;
335 base
->outline
.n_points
=
336 (short)( base
->outline
.n_points
+ current
->outline
.n_points
);
337 base
->outline
.n_contours
=
338 (short)( base
->outline
.n_contours
+ current
->outline
.n_contours
);
340 base
->num_subglyphs
+= current
->num_subglyphs
;
342 /* adjust contours count in newest outline */
343 for ( n
= 0; n
< n_curr_contours
; n
++ )
344 current
->outline
.contours
[n
] =
345 (short)( current
->outline
.contours
[n
] + n_base_points
);
347 /* prepare for another new glyph image */
348 FT_GlyphLoader_Prepare( loader
);
352 FT_BASE_DEF( FT_Error
)
353 FT_GlyphLoader_CopyPoints( FT_GlyphLoader target
,
354 FT_GlyphLoader source
)
357 FT_UInt num_points
= source
->base
.outline
.n_points
;
358 FT_UInt num_contours
= source
->base
.outline
.n_contours
;
361 error
= FT_GlyphLoader_CheckPoints( target
, num_points
, num_contours
);
364 FT_Outline
* out
= &target
->base
.outline
;
365 FT_Outline
* in
= &source
->base
.outline
;
368 FT_ARRAY_COPY( out
->points
, in
->points
,
370 FT_ARRAY_COPY( out
->tags
, in
->tags
,
372 FT_ARRAY_COPY( out
->contours
, in
->contours
,
375 /* do we need to copy the extra points? */
376 if ( target
->use_extra
&& source
->use_extra
)
378 FT_ARRAY_COPY( target
->base
.extra_points
, source
->base
.extra_points
,
380 FT_ARRAY_COPY( target
->base
.extra_points2
, source
->base
.extra_points2
,
384 out
->n_points
= (short)num_points
;
385 out
->n_contours
= (short)num_contours
;
387 FT_GlyphLoader_Adjust_Points( target
);