[FREETYPE]
[reactos.git] / reactos / lib / 3rdparty / freetype / src / autofit / afloader.c
1 /***************************************************************************/
2 /* */
3 /* afloader.c */
4 /* */
5 /* Auto-fitter glyph loading routines (body). */
6 /* */
7 /* Copyright 2003-2009, 2011-2013 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 /***************************************************************************/
17
18
19 #include "afglobal.h"
20 #include "afloader.h"
21 #include "afhints.h"
22 #include "aferrors.h"
23 #include "afmodule.h"
24 #include "afpic.h"
25
26
27 /* Initialize glyph loader. */
28
29 FT_LOCAL_DEF( FT_Error )
30 af_loader_init( AF_Module module )
31 {
32 AF_Loader loader = module->loader;
33 FT_Memory memory = module->root.library->memory;
34
35
36 FT_ZERO( loader );
37
38 af_glyph_hints_init( &loader->hints, memory );
39 #ifdef FT_DEBUG_AUTOFIT
40 _af_debug_hints = &loader->hints;
41 #endif
42 return FT_GlyphLoader_New( memory, &loader->gloader );
43 }
44
45
46 /* Reset glyph loader and compute globals if necessary. */
47
48 FT_LOCAL_DEF( FT_Error )
49 af_loader_reset( AF_Module module,
50 FT_Face face )
51 {
52 FT_Error error = FT_Err_Ok;
53 AF_Loader loader = module->loader;
54
55
56 loader->face = face;
57 loader->globals = (AF_FaceGlobals)face->autohint.data;
58
59 FT_GlyphLoader_Rewind( loader->gloader );
60
61 if ( loader->globals == NULL )
62 {
63 error = af_face_globals_new( face, &loader->globals, module );
64 if ( !error )
65 {
66 face->autohint.data =
67 (FT_Pointer)loader->globals;
68 face->autohint.finalizer =
69 (FT_Generic_Finalizer)af_face_globals_free;
70 }
71 }
72
73 return error;
74 }
75
76
77 /* Finalize glyph loader. */
78
79 FT_LOCAL_DEF( void )
80 af_loader_done( AF_Module module )
81 {
82 AF_Loader loader = module->loader;
83
84
85 af_glyph_hints_done( &loader->hints );
86
87 loader->face = NULL;
88 loader->globals = NULL;
89
90 #ifdef FT_DEBUG_AUTOFIT
91 _af_debug_hints = NULL;
92 #endif
93 FT_GlyphLoader_Done( loader->gloader );
94 loader->gloader = NULL;
95 }
96
97
98 /* Load a single glyph component. This routine calls itself */
99 /* recursively, if necessary, and does the main work of */
100 /* `af_loader_load_glyph.' */
101
102 static FT_Error
103 af_loader_load_g( AF_Loader loader,
104 AF_Scaler scaler,
105 FT_UInt glyph_index,
106 FT_Int32 load_flags,
107 FT_UInt depth )
108 {
109 FT_Error error;
110 FT_Face face = loader->face;
111 FT_GlyphLoader gloader = loader->gloader;
112 AF_ScriptMetrics metrics = loader->metrics;
113 AF_GlyphHints hints = &loader->hints;
114 FT_GlyphSlot slot = face->glyph;
115 FT_Slot_Internal internal = slot->internal;
116 FT_Int32 flags;
117
118
119 flags = load_flags | FT_LOAD_LINEAR_DESIGN;
120 error = FT_Load_Glyph( face, glyph_index, flags );
121 if ( error )
122 goto Exit;
123
124 loader->transformed = internal->glyph_transformed;
125 if ( loader->transformed )
126 {
127 FT_Matrix inverse;
128
129
130 loader->trans_matrix = internal->glyph_matrix;
131 loader->trans_delta = internal->glyph_delta;
132
133 inverse = loader->trans_matrix;
134 FT_Matrix_Invert( &inverse );
135 FT_Vector_Transform( &loader->trans_delta, &inverse );
136 }
137
138 switch ( slot->format )
139 {
140 case FT_GLYPH_FORMAT_OUTLINE:
141 /* translate the loaded glyph when an internal transform is needed */
142 if ( loader->transformed )
143 FT_Outline_Translate( &slot->outline,
144 loader->trans_delta.x,
145 loader->trans_delta.y );
146
147 /* copy the outline points in the loader's current */
148 /* extra points which are used to keep original glyph coordinates */
149 error = FT_GLYPHLOADER_CHECK_POINTS( gloader,
150 slot->outline.n_points + 4,
151 slot->outline.n_contours );
152 if ( error )
153 goto Exit;
154
155 FT_ARRAY_COPY( gloader->current.outline.points,
156 slot->outline.points,
157 slot->outline.n_points );
158
159 FT_ARRAY_COPY( gloader->current.outline.contours,
160 slot->outline.contours,
161 slot->outline.n_contours );
162
163 FT_ARRAY_COPY( gloader->current.outline.tags,
164 slot->outline.tags,
165 slot->outline.n_points );
166
167 gloader->current.outline.n_points = slot->outline.n_points;
168 gloader->current.outline.n_contours = slot->outline.n_contours;
169
170 /* compute original horizontal phantom points (and ignore */
171 /* vertical ones) */
172 loader->pp1.x = hints->x_delta;
173 loader->pp1.y = hints->y_delta;
174 loader->pp2.x = FT_MulFix( slot->metrics.horiAdvance,
175 hints->x_scale ) + hints->x_delta;
176 loader->pp2.y = hints->y_delta;
177
178 /* be sure to check for spacing glyphs */
179 if ( slot->outline.n_points == 0 )
180 goto Hint_Metrics;
181
182 /* now load the slot image into the auto-outline and run the */
183 /* automatic hinting process */
184 {
185 #ifdef FT_CONFIG_OPTION_PIC
186 AF_FaceGlobals globals = loader->globals;
187 #endif
188 AF_WritingSystemClass writing_system_class =
189 AF_WRITING_SYSTEM_CLASSES_GET
190 [metrics->script_class->writing_system];
191
192
193 if ( writing_system_class->script_hints_apply )
194 writing_system_class->script_hints_apply( hints,
195 &gloader->current.outline,
196 metrics );
197 }
198
199 /* we now need to adjust the metrics according to the change in */
200 /* width/positioning that occurred during the hinting process */
201 if ( scaler->render_mode != FT_RENDER_MODE_LIGHT )
202 {
203 FT_Pos old_rsb, old_lsb, new_lsb;
204 FT_Pos pp1x_uh, pp2x_uh;
205 AF_AxisHints axis = &hints->axis[AF_DIMENSION_HORZ];
206 AF_Edge edge1 = axis->edges; /* leftmost edge */
207 AF_Edge edge2 = edge1 +
208 axis->num_edges - 1; /* rightmost edge */
209
210
211 if ( axis->num_edges > 1 && AF_HINTS_DO_ADVANCE( hints ) )
212 {
213 old_rsb = loader->pp2.x - edge2->opos;
214 old_lsb = edge1->opos;
215 new_lsb = edge1->pos;
216
217 /* remember unhinted values to later account */
218 /* for rounding errors */
219
220 pp1x_uh = new_lsb - old_lsb;
221 pp2x_uh = edge2->pos + old_rsb;
222
223 /* prefer too much space over too little space */
224 /* for very small sizes */
225
226 if ( old_lsb < 24 )
227 pp1x_uh -= 8;
228
229 if ( old_rsb < 24 )
230 pp2x_uh += 8;
231
232 loader->pp1.x = FT_PIX_ROUND( pp1x_uh );
233 loader->pp2.x = FT_PIX_ROUND( pp2x_uh );
234
235 if ( loader->pp1.x >= new_lsb && old_lsb > 0 )
236 loader->pp1.x -= 64;
237
238 if ( loader->pp2.x <= edge2->pos && old_rsb > 0 )
239 loader->pp2.x += 64;
240
241 slot->lsb_delta = loader->pp1.x - pp1x_uh;
242 slot->rsb_delta = loader->pp2.x - pp2x_uh;
243 }
244 else
245 {
246 FT_Pos pp1x = loader->pp1.x;
247 FT_Pos pp2x = loader->pp2.x;
248
249
250 loader->pp1.x = FT_PIX_ROUND( pp1x );
251 loader->pp2.x = FT_PIX_ROUND( pp2x );
252
253 slot->lsb_delta = loader->pp1.x - pp1x;
254 slot->rsb_delta = loader->pp2.x - pp2x;
255 }
256 }
257 else
258 {
259 FT_Pos pp1x = loader->pp1.x;
260 FT_Pos pp2x = loader->pp2.x;
261
262
263 loader->pp1.x = FT_PIX_ROUND( pp1x + hints->xmin_delta );
264 loader->pp2.x = FT_PIX_ROUND( pp2x + hints->xmax_delta );
265
266 slot->lsb_delta = loader->pp1.x - pp1x;
267 slot->rsb_delta = loader->pp2.x - pp2x;
268 }
269
270 /* good, we simply add the glyph to our loader's base */
271 FT_GlyphLoader_Add( gloader );
272 break;
273
274 case FT_GLYPH_FORMAT_COMPOSITE:
275 {
276 FT_UInt nn, num_subglyphs = slot->num_subglyphs;
277 FT_UInt num_base_subgs, start_point;
278 FT_SubGlyph subglyph;
279
280
281 start_point = gloader->base.outline.n_points;
282
283 /* first of all, copy the subglyph descriptors in the glyph loader */
284 error = FT_GlyphLoader_CheckSubGlyphs( gloader, num_subglyphs );
285 if ( error )
286 goto Exit;
287
288 FT_ARRAY_COPY( gloader->current.subglyphs,
289 slot->subglyphs,
290 num_subglyphs );
291
292 gloader->current.num_subglyphs = num_subglyphs;
293 num_base_subgs = gloader->base.num_subglyphs;
294
295 /* now read each subglyph independently */
296 for ( nn = 0; nn < num_subglyphs; nn++ )
297 {
298 FT_Vector pp1, pp2;
299 FT_Pos x, y;
300 FT_UInt num_points, num_new_points, num_base_points;
301
302
303 /* gloader.current.subglyphs can change during glyph loading due */
304 /* to re-allocation -- we must recompute the current subglyph on */
305 /* each iteration */
306 subglyph = gloader->base.subglyphs + num_base_subgs + nn;
307
308 pp1 = loader->pp1;
309 pp2 = loader->pp2;
310
311 num_base_points = gloader->base.outline.n_points;
312
313 error = af_loader_load_g( loader, scaler, subglyph->index,
314 load_flags, depth + 1 );
315 if ( error )
316 goto Exit;
317
318 /* recompute subglyph pointer */
319 subglyph = gloader->base.subglyphs + num_base_subgs + nn;
320
321 if ( subglyph->flags & FT_SUBGLYPH_FLAG_USE_MY_METRICS )
322 {
323 pp1 = loader->pp1;
324 pp2 = loader->pp2;
325 }
326 else
327 {
328 loader->pp1 = pp1;
329 loader->pp2 = pp2;
330 }
331
332 num_points = gloader->base.outline.n_points;
333 num_new_points = num_points - num_base_points;
334
335 /* now perform the transformation required for this subglyph */
336
337 if ( subglyph->flags & ( FT_SUBGLYPH_FLAG_SCALE |
338 FT_SUBGLYPH_FLAG_XY_SCALE |
339 FT_SUBGLYPH_FLAG_2X2 ) )
340 {
341 FT_Vector* cur = gloader->base.outline.points +
342 num_base_points;
343 FT_Vector* limit = cur + num_new_points;
344
345
346 for ( ; cur < limit; cur++ )
347 FT_Vector_Transform( cur, &subglyph->transform );
348 }
349
350 /* apply offset */
351
352 if ( !( subglyph->flags & FT_SUBGLYPH_FLAG_ARGS_ARE_XY_VALUES ) )
353 {
354 FT_Int k = subglyph->arg1;
355 FT_UInt l = subglyph->arg2;
356 FT_Vector* p1;
357 FT_Vector* p2;
358
359
360 if ( start_point + k >= num_base_points ||
361 l >= (FT_UInt)num_new_points )
362 {
363 error = FT_THROW( Invalid_Composite );
364 goto Exit;
365 }
366
367 l += num_base_points;
368
369 /* for now, only use the current point coordinates; */
370 /* we eventually may consider another approach */
371 p1 = gloader->base.outline.points + start_point + k;
372 p2 = gloader->base.outline.points + start_point + l;
373
374 x = p1->x - p2->x;
375 y = p1->y - p2->y;
376 }
377 else
378 {
379 x = FT_MulFix( subglyph->arg1, hints->x_scale ) + hints->x_delta;
380 y = FT_MulFix( subglyph->arg2, hints->y_scale ) + hints->y_delta;
381
382 x = FT_PIX_ROUND( x );
383 y = FT_PIX_ROUND( y );
384 }
385
386 {
387 FT_Outline dummy = gloader->base.outline;
388
389
390 dummy.points += num_base_points;
391 dummy.n_points = (short)num_new_points;
392
393 FT_Outline_Translate( &dummy, x, y );
394 }
395 }
396 }
397 break;
398
399 default:
400 /* we don't support other formats (yet?) */
401 error = FT_THROW( Unimplemented_Feature );
402 }
403
404 Hint_Metrics:
405 if ( depth == 0 )
406 {
407 FT_BBox bbox;
408 FT_Vector vvector;
409
410
411 vvector.x = slot->metrics.vertBearingX - slot->metrics.horiBearingX;
412 vvector.y = slot->metrics.vertBearingY - slot->metrics.horiBearingY;
413 vvector.x = FT_MulFix( vvector.x, metrics->scaler.x_scale );
414 vvector.y = FT_MulFix( vvector.y, metrics->scaler.y_scale );
415
416 /* transform the hinted outline if needed */
417 if ( loader->transformed )
418 {
419 FT_Outline_Transform( &gloader->base.outline, &loader->trans_matrix );
420 FT_Vector_Transform( &vvector, &loader->trans_matrix );
421 }
422 #if 1
423 /* we must translate our final outline by -pp1.x and compute */
424 /* the new metrics */
425 if ( loader->pp1.x )
426 FT_Outline_Translate( &gloader->base.outline, -loader->pp1.x, 0 );
427 #endif
428 FT_Outline_Get_CBox( &gloader->base.outline, &bbox );
429
430 bbox.xMin = FT_PIX_FLOOR( bbox.xMin );
431 bbox.yMin = FT_PIX_FLOOR( bbox.yMin );
432 bbox.xMax = FT_PIX_CEIL( bbox.xMax );
433 bbox.yMax = FT_PIX_CEIL( bbox.yMax );
434
435 slot->metrics.width = bbox.xMax - bbox.xMin;
436 slot->metrics.height = bbox.yMax - bbox.yMin;
437 slot->metrics.horiBearingX = bbox.xMin;
438 slot->metrics.horiBearingY = bbox.yMax;
439
440 slot->metrics.vertBearingX = FT_PIX_FLOOR( bbox.xMin + vvector.x );
441 slot->metrics.vertBearingY = FT_PIX_FLOOR( bbox.yMax + vvector.y );
442
443 /* for mono-width fonts (like Andale, Courier, etc.) we need */
444 /* to keep the original rounded advance width; ditto for */
445 /* digits if all have the same advance width */
446 #if 0
447 if ( !FT_IS_FIXED_WIDTH( slot->face ) )
448 slot->metrics.horiAdvance = loader->pp2.x - loader->pp1.x;
449 else
450 slot->metrics.horiAdvance = FT_MulFix( slot->metrics.horiAdvance,
451 x_scale );
452 #else
453 if ( scaler->render_mode != FT_RENDER_MODE_LIGHT &&
454 ( FT_IS_FIXED_WIDTH( slot->face ) ||
455 ( af_face_globals_is_digit( loader->globals, glyph_index ) &&
456 metrics->digits_have_same_width ) ) )
457 {
458 slot->metrics.horiAdvance = FT_MulFix( slot->metrics.horiAdvance,
459 metrics->scaler.x_scale );
460
461 /* Set delta values to 0. Otherwise code that uses them is */
462 /* going to ruin the fixed advance width. */
463 slot->lsb_delta = 0;
464 slot->rsb_delta = 0;
465 }
466 else
467 {
468 /* non-spacing glyphs must stay as-is */
469 if ( slot->metrics.horiAdvance )
470 slot->metrics.horiAdvance = loader->pp2.x - loader->pp1.x;
471 }
472 #endif
473
474 slot->metrics.vertAdvance = FT_MulFix( slot->metrics.vertAdvance,
475 metrics->scaler.y_scale );
476
477 slot->metrics.horiAdvance = FT_PIX_ROUND( slot->metrics.horiAdvance );
478 slot->metrics.vertAdvance = FT_PIX_ROUND( slot->metrics.vertAdvance );
479
480 /* now copy outline into glyph slot */
481 FT_GlyphLoader_Rewind( internal->loader );
482 error = FT_GlyphLoader_CopyPoints( internal->loader, gloader );
483 if ( error )
484 goto Exit;
485
486 /* reassign all outline fields except flags to protect them */
487 slot->outline.n_contours = internal->loader->base.outline.n_contours;
488 slot->outline.n_points = internal->loader->base.outline.n_points;
489 slot->outline.points = internal->loader->base.outline.points;
490 slot->outline.tags = internal->loader->base.outline.tags;
491 slot->outline.contours = internal->loader->base.outline.contours;
492
493 slot->format = FT_GLYPH_FORMAT_OUTLINE;
494 }
495
496 Exit:
497 return error;
498 }
499
500
501 /* Load a glyph. */
502
503 FT_LOCAL_DEF( FT_Error )
504 af_loader_load_glyph( AF_Module module,
505 FT_Face face,
506 FT_UInt gindex,
507 FT_Int32 load_flags )
508 {
509 FT_Error error;
510 FT_Size size = face->size;
511 AF_Loader loader = module->loader;
512 AF_ScalerRec scaler;
513
514
515 if ( !size )
516 return FT_THROW( Invalid_Argument );
517
518 FT_ZERO( &scaler );
519
520 scaler.face = face;
521 scaler.x_scale = size->metrics.x_scale;
522 scaler.x_delta = 0; /* XXX: TODO: add support for sub-pixel hinting */
523 scaler.y_scale = size->metrics.y_scale;
524 scaler.y_delta = 0; /* XXX: TODO: add support for sub-pixel hinting */
525
526 scaler.render_mode = FT_LOAD_TARGET_MODE( load_flags );
527 scaler.flags = 0; /* XXX: fix this */
528
529 error = af_loader_reset( module, face );
530 if ( !error )
531 {
532 AF_ScriptMetrics metrics;
533 FT_UInt options = AF_SCRIPT_DFLT;
534
535
536 #ifdef FT_OPTION_AUTOFIT2
537 /* XXX: undocumented hook to activate the latin2 writing system */
538 if ( load_flags & ( 1UL << 20 ) )
539 options = AF_SCRIPT_LTN2;
540 #endif
541
542 error = af_face_globals_get_metrics( loader->globals, gindex,
543 options, &metrics );
544 if ( !error )
545 {
546 #ifdef FT_CONFIG_OPTION_PIC
547 AF_FaceGlobals globals = loader->globals;
548 #endif
549 AF_WritingSystemClass writing_system_class =
550 AF_WRITING_SYSTEM_CLASSES_GET
551 [metrics->script_class->writing_system];
552
553
554 loader->metrics = metrics;
555
556 if ( writing_system_class->script_metrics_scale )
557 writing_system_class->script_metrics_scale( metrics, &scaler );
558 else
559 metrics->scaler = scaler;
560
561 load_flags |= FT_LOAD_NO_SCALE | FT_LOAD_IGNORE_TRANSFORM;
562 load_flags &= ~FT_LOAD_RENDER;
563
564 if ( writing_system_class->script_hints_init )
565 {
566 error = writing_system_class->script_hints_init( &loader->hints,
567 metrics );
568 if ( error )
569 goto Exit;
570 }
571
572 error = af_loader_load_g( loader, &scaler, gindex, load_flags, 0 );
573 }
574 }
575 Exit:
576 return error;
577 }
578
579
580 /* END */