Bug 470455 - test_database_sync_embed_visits.js leaks, r=sdwilsh
[wine-gecko.git] / modules / freetype2 / src / autofit / afloader.c
blob4e48c2fe97506e8b540cf5411e0bd652c1e5428a
1 /***************************************************************************/
2 /* */
3 /* afloader.c */
4 /* */
5 /* Auto-fitter glyph loading routines (body). */
6 /* */
7 /* Copyright 2003, 2004, 2005, 2006, 2007, 2008 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 /***************************************************************************/
19 #include "afloader.h"
20 #include "afhints.h"
21 #include "afglobal.h"
22 #include "aflatin.h"
23 #include "aferrors.h"
26 FT_LOCAL_DEF( FT_Error )
27 af_loader_init( AF_Loader loader,
28 FT_Memory memory )
30 FT_ZERO( loader );
32 af_glyph_hints_init( &loader->hints, memory );
33 #ifdef AF_DEBUG
34 _af_debug_hints = &loader->hints;
35 #endif
36 return FT_GlyphLoader_New( memory, &loader->gloader );
40 FT_LOCAL_DEF( FT_Error )
41 af_loader_reset( AF_Loader loader,
42 FT_Face face )
44 FT_Error error = AF_Err_Ok;
47 loader->face = face;
48 loader->globals = (AF_FaceGlobals)face->autohint.data;
50 FT_GlyphLoader_Rewind( loader->gloader );
52 if ( loader->globals == NULL )
54 error = af_face_globals_new( face, &loader->globals );
55 if ( !error )
57 face->autohint.data =
58 (FT_Pointer)loader->globals;
59 face->autohint.finalizer =
60 (FT_Generic_Finalizer)af_face_globals_free;
64 return error;
68 FT_LOCAL_DEF( void )
69 af_loader_done( AF_Loader loader )
71 af_glyph_hints_done( &loader->hints );
73 loader->face = NULL;
74 loader->globals = NULL;
76 #ifdef AF_DEBUG
77 _af_debug_hints = NULL;
78 #endif
79 FT_GlyphLoader_Done( loader->gloader );
80 loader->gloader = NULL;
84 static FT_Error
85 af_loader_load_g( AF_Loader loader,
86 AF_Scaler scaler,
87 FT_UInt glyph_index,
88 FT_Int32 load_flags,
89 FT_UInt depth )
91 FT_Error error;
92 FT_Face face = loader->face;
93 FT_GlyphLoader gloader = loader->gloader;
94 AF_ScriptMetrics metrics = loader->metrics;
95 AF_GlyphHints hints = &loader->hints;
96 FT_GlyphSlot slot = face->glyph;
97 FT_Slot_Internal internal = slot->internal;
100 error = FT_Load_Glyph( face, glyph_index, load_flags );
101 if ( error )
102 goto Exit;
104 loader->transformed = internal->glyph_transformed;
105 if ( loader->transformed )
107 FT_Matrix inverse;
110 loader->trans_matrix = internal->glyph_matrix;
111 loader->trans_delta = internal->glyph_delta;
113 inverse = loader->trans_matrix;
114 FT_Matrix_Invert( &inverse );
115 FT_Vector_Transform( &loader->trans_delta, &inverse );
118 /* set linear metrics */
119 slot->linearHoriAdvance = slot->metrics.horiAdvance;
120 slot->linearVertAdvance = slot->metrics.vertAdvance;
122 switch ( slot->format )
124 case FT_GLYPH_FORMAT_OUTLINE:
125 /* translate the loaded glyph when an internal transform is needed */
126 if ( loader->transformed )
127 FT_Outline_Translate( &slot->outline,
128 loader->trans_delta.x,
129 loader->trans_delta.y );
131 /* copy the outline points in the loader's current */
132 /* extra points which is used to keep original glyph coordinates */
133 error = FT_GLYPHLOADER_CHECK_POINTS( gloader,
134 slot->outline.n_points + 4,
135 slot->outline.n_contours );
136 if ( error )
137 goto Exit;
139 FT_ARRAY_COPY( gloader->current.outline.points,
140 slot->outline.points,
141 slot->outline.n_points );
143 FT_ARRAY_COPY( gloader->current.outline.contours,
144 slot->outline.contours,
145 slot->outline.n_contours );
147 FT_ARRAY_COPY( gloader->current.outline.tags,
148 slot->outline.tags,
149 slot->outline.n_points );
151 gloader->current.outline.n_points = slot->outline.n_points;
152 gloader->current.outline.n_contours = slot->outline.n_contours;
154 /* compute original horizontal phantom points (and ignore */
155 /* vertical ones) */
156 loader->pp1.x = hints->x_delta;
157 loader->pp1.y = hints->y_delta;
158 loader->pp2.x = FT_MulFix( slot->metrics.horiAdvance,
159 hints->x_scale ) + hints->x_delta;
160 loader->pp2.y = hints->y_delta;
162 /* be sure to check for spacing glyphs */
163 if ( slot->outline.n_points == 0 )
164 goto Hint_Metrics;
166 /* now load the slot image into the auto-outline and run the */
167 /* automatic hinting process */
168 if ( metrics->clazz->script_hints_apply )
169 metrics->clazz->script_hints_apply( hints,
170 &gloader->current.outline,
171 metrics );
173 /* we now need to hint the metrics according to the change in */
174 /* width/positioning that occurred during the hinting process */
175 if ( scaler->render_mode != FT_RENDER_MODE_LIGHT )
177 FT_Pos old_rsb, old_lsb, new_lsb;
178 FT_Pos pp1x_uh, pp2x_uh;
179 AF_AxisHints axis = &hints->axis[AF_DIMENSION_HORZ];
180 AF_Edge edge1 = axis->edges; /* leftmost edge */
181 AF_Edge edge2 = edge1 +
182 axis->num_edges - 1; /* rightmost edge */
185 if ( axis->num_edges > 1 && AF_HINTS_DO_ADVANCE( hints ) )
187 old_rsb = loader->pp2.x - edge2->opos;
188 old_lsb = edge1->opos;
189 new_lsb = edge1->pos;
191 /* remember unhinted values to later account */
192 /* for rounding errors */
194 pp1x_uh = new_lsb - old_lsb;
195 pp2x_uh = edge2->pos + old_rsb;
197 /* prefer too much space over too little space */
198 /* for very small sizes */
200 if ( old_lsb < 24 )
201 pp1x_uh -= 8;
203 if ( old_rsb < 24 )
204 pp2x_uh += 8;
206 loader->pp1.x = FT_PIX_ROUND( pp1x_uh );
207 loader->pp2.x = FT_PIX_ROUND( pp2x_uh );
209 if ( loader->pp1.x >= new_lsb && old_lsb > 0 )
210 loader->pp1.x -= 64;
212 if ( loader->pp2.x <= edge2->pos && old_rsb > 0 )
213 loader->pp2.x += 64;
215 slot->lsb_delta = loader->pp1.x - pp1x_uh;
216 slot->rsb_delta = loader->pp2.x - pp2x_uh;
218 else
220 FT_Pos pp1x = loader->pp1.x;
221 FT_Pos pp2x = loader->pp2.x;
223 loader->pp1.x = FT_PIX_ROUND( pp1x );
224 loader->pp2.x = FT_PIX_ROUND( pp2x );
226 slot->lsb_delta = loader->pp1.x - pp1x;
227 slot->rsb_delta = loader->pp2.x - pp2x;
230 else
232 FT_Pos pp1x = loader->pp1.x;
233 FT_Pos pp2x = loader->pp2.x;
235 loader->pp1.x = FT_PIX_ROUND( pp1x + hints->xmin_delta );
236 loader->pp2.x = FT_PIX_ROUND( pp2x + hints->xmax_delta );
238 slot->lsb_delta = loader->pp1.x - pp1x;
239 slot->rsb_delta = loader->pp2.x - pp2x;
242 /* good, we simply add the glyph to our loader's base */
243 FT_GlyphLoader_Add( gloader );
244 break;
246 case FT_GLYPH_FORMAT_COMPOSITE:
248 FT_UInt nn, num_subglyphs = slot->num_subglyphs;
249 FT_UInt num_base_subgs, start_point;
250 FT_SubGlyph subglyph;
253 start_point = gloader->base.outline.n_points;
255 /* first of all, copy the subglyph descriptors in the glyph loader */
256 error = FT_GlyphLoader_CheckSubGlyphs( gloader, num_subglyphs );
257 if ( error )
258 goto Exit;
260 FT_ARRAY_COPY( gloader->current.subglyphs,
261 slot->subglyphs,
262 num_subglyphs );
264 gloader->current.num_subglyphs = num_subglyphs;
265 num_base_subgs = gloader->base.num_subglyphs;
267 /* now, read each subglyph independently */
268 for ( nn = 0; nn < num_subglyphs; nn++ )
270 FT_Vector pp1, pp2;
271 FT_Pos x, y;
272 FT_UInt num_points, num_new_points, num_base_points;
275 /* gloader.current.subglyphs can change during glyph loading due */
276 /* to re-allocation -- we must recompute the current subglyph on */
277 /* each iteration */
278 subglyph = gloader->base.subglyphs + num_base_subgs + nn;
280 pp1 = loader->pp1;
281 pp2 = loader->pp2;
283 num_base_points = gloader->base.outline.n_points;
285 error = af_loader_load_g( loader, scaler, subglyph->index,
286 load_flags, depth + 1 );
287 if ( error )
288 goto Exit;
290 /* recompute subglyph pointer */
291 subglyph = gloader->base.subglyphs + num_base_subgs + nn;
293 if ( subglyph->flags & FT_SUBGLYPH_FLAG_USE_MY_METRICS )
295 pp1 = loader->pp1;
296 pp2 = loader->pp2;
298 else
300 loader->pp1 = pp1;
301 loader->pp2 = pp2;
304 num_points = gloader->base.outline.n_points;
305 num_new_points = num_points - num_base_points;
307 /* now perform the transform required for this subglyph */
309 if ( subglyph->flags & ( FT_SUBGLYPH_FLAG_SCALE |
310 FT_SUBGLYPH_FLAG_XY_SCALE |
311 FT_SUBGLYPH_FLAG_2X2 ) )
313 FT_Vector* cur = gloader->base.outline.points +
314 num_base_points;
315 FT_Vector* limit = cur + num_new_points;
318 for ( ; cur < limit; cur++ )
319 FT_Vector_Transform( cur, &subglyph->transform );
322 /* apply offset */
324 if ( !( subglyph->flags & FT_SUBGLYPH_FLAG_ARGS_ARE_XY_VALUES ) )
326 FT_Int k = subglyph->arg1;
327 FT_UInt l = subglyph->arg2;
328 FT_Vector* p1;
329 FT_Vector* p2;
332 if ( start_point + k >= num_base_points ||
333 l >= (FT_UInt)num_new_points )
335 error = AF_Err_Invalid_Composite;
336 goto Exit;
339 l += num_base_points;
341 /* for now, only use the current point coordinates; */
342 /* we may consider another approach in the near future */
343 p1 = gloader->base.outline.points + start_point + k;
344 p2 = gloader->base.outline.points + start_point + l;
346 x = p1->x - p2->x;
347 y = p1->y - p2->y;
349 else
351 x = FT_MulFix( subglyph->arg1, hints->x_scale ) + hints->x_delta;
352 y = FT_MulFix( subglyph->arg2, hints->y_scale ) + hints->y_delta;
354 x = FT_PIX_ROUND( x );
355 y = FT_PIX_ROUND( y );
359 FT_Outline dummy = gloader->base.outline;
362 dummy.points += num_base_points;
363 dummy.n_points = (short)num_new_points;
365 FT_Outline_Translate( &dummy, x, y );
369 break;
371 default:
372 /* we don't support other formats (yet?) */
373 error = AF_Err_Unimplemented_Feature;
376 Hint_Metrics:
377 if ( depth == 0 )
379 FT_BBox bbox;
380 FT_Vector vvector;
383 vvector.x = slot->metrics.vertBearingX - slot->metrics.horiBearingX;
384 vvector.y = slot->metrics.vertBearingY - slot->metrics.horiBearingY;
385 vvector.x = FT_MulFix( vvector.x, metrics->scaler.x_scale );
386 vvector.y = FT_MulFix( vvector.y, metrics->scaler.y_scale );
388 /* transform the hinted outline if needed */
389 if ( loader->transformed )
391 FT_Outline_Transform( &gloader->base.outline, &loader->trans_matrix );
392 FT_Vector_Transform( &vvector, &loader->trans_matrix );
394 #if 1
395 /* we must translate our final outline by -pp1.x and compute */
396 /* the new metrics */
397 if ( loader->pp1.x )
398 FT_Outline_Translate( &gloader->base.outline, -loader->pp1.x, 0 );
399 #endif
400 FT_Outline_Get_CBox( &gloader->base.outline, &bbox );
402 bbox.xMin = FT_PIX_FLOOR( bbox.xMin );
403 bbox.yMin = FT_PIX_FLOOR( bbox.yMin );
404 bbox.xMax = FT_PIX_CEIL( bbox.xMax );
405 bbox.yMax = FT_PIX_CEIL( bbox.yMax );
407 slot->metrics.width = bbox.xMax - bbox.xMin;
408 slot->metrics.height = bbox.yMax - bbox.yMin;
409 slot->metrics.horiBearingX = bbox.xMin;
410 slot->metrics.horiBearingY = bbox.yMax;
412 slot->metrics.vertBearingX = FT_PIX_FLOOR( bbox.xMin + vvector.x );
413 slot->metrics.vertBearingY = FT_PIX_FLOOR( bbox.yMax + vvector.y );
415 /* for mono-width fonts (like Andale, Courier, etc.) we need */
416 /* to keep the original rounded advance width */
417 #if 0
418 if ( !FT_IS_FIXED_WIDTH( slot->face ) )
419 slot->metrics.horiAdvance = loader->pp2.x - loader->pp1.x;
420 else
421 slot->metrics.horiAdvance = FT_MulFix( slot->metrics.horiAdvance,
422 x_scale );
423 #else
424 if ( !FT_IS_FIXED_WIDTH( slot->face ) )
426 /* non-spacing glyphs must stay as-is */
427 if ( slot->metrics.horiAdvance )
428 slot->metrics.horiAdvance = loader->pp2.x - loader->pp1.x;
430 else
432 slot->metrics.horiAdvance = FT_MulFix( slot->metrics.horiAdvance,
433 metrics->scaler.x_scale );
435 /* Set delta values to 0. Otherwise code that uses them is */
436 /* going to ruin the fixed advance width. */
437 slot->lsb_delta = 0;
438 slot->rsb_delta = 0;
440 #endif
442 slot->metrics.vertAdvance = FT_MulFix( slot->metrics.vertAdvance,
443 metrics->scaler.y_scale );
445 slot->metrics.horiAdvance = FT_PIX_ROUND( slot->metrics.horiAdvance );
446 slot->metrics.vertAdvance = FT_PIX_ROUND( slot->metrics.vertAdvance );
448 /* now copy outline into glyph slot */
449 FT_GlyphLoader_Rewind( internal->loader );
450 error = FT_GlyphLoader_CopyPoints( internal->loader, gloader );
451 if ( error )
452 goto Exit;
454 slot->outline = internal->loader->base.outline;
455 slot->format = FT_GLYPH_FORMAT_OUTLINE;
458 #ifdef DEBUG_HINTER
459 af_debug_hinter = hinter;
460 #endif
462 Exit:
463 return error;
467 FT_LOCAL_DEF( FT_Error )
468 af_loader_load_glyph( AF_Loader loader,
469 FT_Face face,
470 FT_UInt gindex,
471 FT_UInt32 load_flags )
473 FT_Error error;
474 FT_Size size = face->size;
475 AF_ScalerRec scaler;
478 if ( !size )
479 return AF_Err_Invalid_Argument;
481 FT_ZERO( &scaler );
483 scaler.face = face;
484 scaler.x_scale = size->metrics.x_scale;
485 scaler.x_delta = 0; /* XXX: TODO: add support for sub-pixel hinting */
486 scaler.y_scale = size->metrics.y_scale;
487 scaler.y_delta = 0; /* XXX: TODO: add support for sub-pixel hinting */
489 scaler.render_mode = FT_LOAD_TARGET_MODE( load_flags );
490 scaler.flags = 0; /* XXX: fix this */
492 error = af_loader_reset( loader, face );
493 if ( !error )
495 AF_ScriptMetrics metrics;
496 FT_UInt options = 0;
499 #ifdef FT_OPTION_AUTOFIT2
500 /* XXX: undocumented hook to activate the latin2 hinter */
501 if ( load_flags & ( 1UL << 20 ) )
502 options = 2;
503 #endif
505 error = af_face_globals_get_metrics( loader->globals, gindex,
506 options, &metrics );
507 if ( !error )
509 loader->metrics = metrics;
511 if ( metrics->clazz->script_metrics_scale )
512 metrics->clazz->script_metrics_scale( metrics, &scaler );
513 else
514 metrics->scaler = scaler;
516 load_flags |= FT_LOAD_NO_SCALE | FT_LOAD_IGNORE_TRANSFORM;
517 load_flags &= ~FT_LOAD_RENDER;
519 if ( metrics->clazz->script_hints_init )
521 error = metrics->clazz->script_hints_init( &loader->hints,
522 metrics );
523 if ( error )
524 goto Exit;
527 error = af_loader_load_g( loader, &scaler, gindex, load_flags, 0 );
530 Exit:
531 return error;
535 /* END */