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 ( new_max
> FT_OUTLINE_POINTS_MAX
)
222 return FT_Err_Array_Too_Large
;
224 if ( FT_RENEW_ARRAY( base
->points
, old_max
, new_max
) ||
225 FT_RENEW_ARRAY( base
->tags
, old_max
, new_max
) )
228 if ( loader
->use_extra
)
230 if ( FT_RENEW_ARRAY( loader
->base
.extra_points
,
231 old_max
* 2, new_max
* 2 ) )
234 FT_ARRAY_MOVE( loader
->base
.extra_points
+ new_max
,
235 loader
->base
.extra_points
+ old_max
,
238 loader
->base
.extra_points2
= loader
->base
.extra_points
+ new_max
;
242 loader
->max_points
= new_max
;
246 old_max
= loader
->max_contours
;
247 new_max
= base
->n_contours
+ current
->n_contours
+
249 if ( new_max
> old_max
)
251 new_max
= FT_PAD_CEIL( new_max
, 4 );
253 if ( new_max
> FT_OUTLINE_CONTOURS_MAX
)
254 return FT_Err_Array_Too_Large
;
256 if ( FT_RENEW_ARRAY( base
->contours
, old_max
, new_max
) )
260 loader
->max_contours
= new_max
;
264 FT_GlyphLoader_Adjust_Points( loader
);
271 /* Ensure that we can add `n_subglyphs' to our glyph. this function */
272 /* reallocates its subglyphs table if necessary. Note that it DOES */
273 /* NOT change the number of subglyphs within the loader! */
275 FT_BASE_DEF( FT_Error
)
276 FT_GlyphLoader_CheckSubGlyphs( FT_GlyphLoader loader
,
279 FT_Memory memory
= loader
->memory
;
280 FT_Error error
= FT_Err_Ok
;
281 FT_UInt new_max
, old_max
;
283 FT_GlyphLoad base
= &loader
->base
;
284 FT_GlyphLoad current
= &loader
->current
;
287 new_max
= base
->num_subglyphs
+ current
->num_subglyphs
+ n_subs
;
288 old_max
= loader
->max_subglyphs
;
289 if ( new_max
> old_max
)
291 new_max
= FT_PAD_CEIL( new_max
, 2 );
292 if ( FT_RENEW_ARRAY( base
->subglyphs
, old_max
, new_max
) )
295 loader
->max_subglyphs
= new_max
;
297 FT_GlyphLoader_Adjust_Subglyphs( loader
);
305 /* prepare loader for the addition of a new glyph on top of the base one */
307 FT_GlyphLoader_Prepare( FT_GlyphLoader loader
)
309 FT_GlyphLoad current
= &loader
->current
;
312 current
->outline
.n_points
= 0;
313 current
->outline
.n_contours
= 0;
314 current
->num_subglyphs
= 0;
316 FT_GlyphLoader_Adjust_Points ( loader
);
317 FT_GlyphLoader_Adjust_Subglyphs( loader
);
321 /* add current glyph to the base image - and prepare for another */
323 FT_GlyphLoader_Add( FT_GlyphLoader loader
)
326 FT_GlyphLoad current
;
328 FT_UInt n_curr_contours
;
329 FT_UInt n_base_points
;
336 base
= &loader
->base
;
337 current
= &loader
->current
;
339 n_curr_contours
= current
->outline
.n_contours
;
340 n_base_points
= base
->outline
.n_points
;
342 base
->outline
.n_points
=
343 (short)( base
->outline
.n_points
+ current
->outline
.n_points
);
344 base
->outline
.n_contours
=
345 (short)( base
->outline
.n_contours
+ current
->outline
.n_contours
);
347 base
->num_subglyphs
+= current
->num_subglyphs
;
349 /* adjust contours count in newest outline */
350 for ( n
= 0; n
< n_curr_contours
; n
++ )
351 current
->outline
.contours
[n
] =
352 (short)( current
->outline
.contours
[n
] + n_base_points
);
354 /* prepare for another new glyph image */
355 FT_GlyphLoader_Prepare( loader
);
359 FT_BASE_DEF( FT_Error
)
360 FT_GlyphLoader_CopyPoints( FT_GlyphLoader target
,
361 FT_GlyphLoader source
)
364 FT_UInt num_points
= source
->base
.outline
.n_points
;
365 FT_UInt num_contours
= source
->base
.outline
.n_contours
;
368 error
= FT_GlyphLoader_CheckPoints( target
, num_points
, num_contours
);
371 FT_Outline
* out
= &target
->base
.outline
;
372 FT_Outline
* in
= &source
->base
.outline
;
375 FT_ARRAY_COPY( out
->points
, in
->points
,
377 FT_ARRAY_COPY( out
->tags
, in
->tags
,
379 FT_ARRAY_COPY( out
->contours
, in
->contours
,
382 /* do we need to copy the extra points? */
383 if ( target
->use_extra
&& source
->use_extra
)
385 FT_ARRAY_COPY( target
->base
.extra_points
, source
->base
.extra_points
,
387 FT_ARRAY_COPY( target
->base
.extra_points2
, source
->base
.extra_points2
,
391 out
->n_points
= (short)num_points
;
392 out
->n_contours
= (short)num_contours
;
394 FT_GlyphLoader_Adjust_Points( target
);