1 /***************************************************************************/
5 /* TrueType Glyph Loader (body). */
7 /* Copyright 1996-2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, */
9 /* David Turner, Robert Wilhelm, and Werner Lemberg. */
11 /* This file is part of the FreeType project, and may only be used, */
12 /* modified, and distributed under the terms of the FreeType project */
13 /* license, LICENSE.TXT. By continuing to use, modify, or distribute */
14 /* this file you indicate that you have read the license and */
15 /* understand and accept it fully. */
17 /***************************************************************************/
21 #include FT_INTERNAL_DEBUG_H
22 #include FT_INTERNAL_CALC_H
23 #include FT_INTERNAL_STREAM_H
24 #include FT_INTERNAL_SFNT_H
25 #include FT_TRUETYPE_TAGS_H
31 #ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
38 /*************************************************************************/
40 /* The macro FT_COMPONENT is used in trace mode. It is an implicit */
41 /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */
42 /* messages during execution. */
45 #define FT_COMPONENT trace_ttgload
48 /*************************************************************************/
50 /* Composite font flags. */
52 #define ARGS_ARE_WORDS 0x0001
53 #define ARGS_ARE_XY_VALUES 0x0002
54 #define ROUND_XY_TO_GRID 0x0004
55 #define WE_HAVE_A_SCALE 0x0008
57 #define MORE_COMPONENTS 0x0020
58 #define WE_HAVE_AN_XY_SCALE 0x0040
59 #define WE_HAVE_A_2X2 0x0080
60 #define WE_HAVE_INSTR 0x0100
61 #define USE_MY_METRICS 0x0200
62 #define OVERLAP_COMPOUND 0x0400
63 #define SCALED_COMPONENT_OFFSET 0x0800
64 #define UNSCALED_COMPONENT_OFFSET 0x1000
67 /*************************************************************************/
69 /* Returns the horizontal metrics in font units for a given glyph. If */
70 /* `check' is true, take care of monospaced fonts by returning the */
71 /* advance width maximum. */
74 TT_Get_HMetrics( TT_Face face
,
80 ( (SFNT_Service
)face
->sfnt
)->get_metrics( face
, 0, idx
, lsb
, aw
);
82 if ( check
&& face
->postscript
.isFixedPitch
)
83 *aw
= face
->horizontal
.advance_Width_Max
;
85 FT_TRACE5(( " advance width (font units): %d\n", *aw
));
86 FT_TRACE5(( " left side bearing (font units): %d\n", *lsb
));
90 /*************************************************************************/
92 /* Returns the vertical metrics in font units for a given glyph. */
93 /* Greg Hitchcock from Microsoft told us that if there were no `vmtx' */
94 /* table, typoAscender/Descender from the `OS/2' table would be used */
95 /* instead, and if there were no `OS/2' table, use ascender/descender */
96 /* from the `hhea' table. But that is not what Microsoft's rasterizer */
97 /* apparently does: It uses the ppem value as the advance height, and */
98 /* sets the top side bearing to be zero. */
100 /* The monospace `check' is probably not meaningful here, but we leave */
101 /* it in for a consistent interface. */
104 TT_Get_VMetrics( TT_Face face
,
112 if ( face
->vertical_info
)
113 ( (SFNT_Service
)face
->sfnt
)->get_metrics( face
, 1, idx
, tsb
, ah
);
115 #if 1 /* Empirically determined, at variance with what MS said */
120 *ah
= face
->root
.units_per_EM
;
123 #else /* This is what MS said to do. It isn't what they do, however. */
125 else if ( face
->os2
.version
!= 0xFFFFU
)
127 *tsb
= face
->os2
.sTypoAscender
;
128 *ah
= face
->os2
.sTypoAscender
- face
->os2
.sTypoDescender
;
132 *tsb
= face
->horizontal
.Ascender
;
133 *ah
= face
->horizontal
.Ascender
- face
->horizontal
.Descender
;
138 FT_TRACE5(( " advance height (font units): %d\n", *ah
));
139 FT_TRACE5(( " top side bearing (font units): %d\n", *tsb
));
144 tt_get_metrics( TT_Loader loader
,
145 FT_UInt glyph_index
)
147 TT_Face face
= (TT_Face
)loader
->face
;
149 FT_Short left_bearing
= 0, top_bearing
= 0;
150 FT_UShort advance_width
= 0, advance_height
= 0;
153 TT_Get_HMetrics( face
, glyph_index
,
154 (FT_Bool
)!( loader
->load_flags
&
155 FT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH
),
158 TT_Get_VMetrics( face
, glyph_index
,
159 (FT_Bool
)!( loader
->load_flags
&
160 FT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH
),
164 loader
->left_bearing
= left_bearing
;
165 loader
->advance
= advance_width
;
166 loader
->top_bearing
= top_bearing
;
167 loader
->vadvance
= advance_height
;
169 if ( !loader
->linear_def
)
171 loader
->linear_def
= 1;
172 loader
->linear
= advance_width
;
177 #ifdef FT_CONFIG_OPTION_INCREMENTAL
180 tt_get_metrics_incr_overrides( TT_Loader loader
,
181 FT_UInt glyph_index
)
183 TT_Face face
= (TT_Face
)loader
->face
;
185 FT_Short left_bearing
= 0, top_bearing
= 0;
186 FT_UShort advance_width
= 0, advance_height
= 0;
189 /* If this is an incrementally loaded font check whether there are */
190 /* overriding metrics for this glyph. */
191 if ( face
->root
.internal
->incremental_interface
&&
192 face
->root
.internal
->incremental_interface
->funcs
->get_glyph_metrics
)
194 FT_Incremental_MetricsRec metrics
;
198 metrics
.bearing_x
= loader
->left_bearing
;
199 metrics
.bearing_y
= 0;
200 metrics
.advance
= loader
->advance
;
201 metrics
.advance_v
= 0;
203 error
= face
->root
.internal
->incremental_interface
->funcs
->get_glyph_metrics(
204 face
->root
.internal
->incremental_interface
->object
,
205 glyph_index
, FALSE
, &metrics
);
209 left_bearing
= (FT_Short
)metrics
.bearing_x
;
210 advance_width
= (FT_UShort
)metrics
.advance
;
214 /* GWW: Do I do the same for vertical metrics? */
215 metrics
.bearing_x
= 0;
216 metrics
.bearing_y
= loader
->top_bearing
;
217 metrics
.advance
= loader
->vadvance
;
219 error
= face
->root
.internal
->incremental_interface
->funcs
->get_glyph_metrics(
220 face
->root
.internal
->incremental_interface
->object
,
221 glyph_index
, TRUE
, &metrics
);
225 top_bearing
= (FT_Short
)metrics
.bearing_y
;
226 advance_height
= (FT_UShort
)metrics
.advance
;
230 loader
->left_bearing
= left_bearing
;
231 loader
->advance
= advance_width
;
232 loader
->top_bearing
= top_bearing
;
233 loader
->vadvance
= advance_height
;
235 if ( !loader
->linear_def
)
237 loader
->linear_def
= 1;
238 loader
->linear
= advance_width
;
246 #endif /* FT_CONFIG_OPTION_INCREMENTAL */
249 /*************************************************************************/
251 /* Translates an array of coordinates. */
254 translate_array( FT_UInt n
,
263 for ( k
= 0; k
< n
; k
++ )
264 coords
[k
].x
+= delta_x
;
267 for ( k
= 0; k
< n
; k
++ )
268 coords
[k
].y
+= delta_y
;
273 #define IS_HINTED( flags ) ( ( flags & FT_LOAD_NO_HINTING ) == 0 )
276 /*************************************************************************/
278 /* The following functions are used by default with TrueType fonts. */
279 /* However, they can be replaced by alternatives if we need to support */
280 /* TrueType-compressed formats (like MicroType) in the future. */
282 /*************************************************************************/
284 FT_CALLBACK_DEF( FT_Error
)
285 TT_Access_Glyph_Frame( TT_Loader loader
,
291 FT_Stream stream
= loader
->stream
;
293 /* for non-debug mode */
294 FT_UNUSED( glyph_index
);
297 FT_TRACE4(( "Glyph %ld\n", glyph_index
));
299 /* the following line sets the `error' variable through macros! */
300 if ( FT_STREAM_SEEK( offset
) || FT_FRAME_ENTER( byte_count
) )
303 loader
->cursor
= stream
->cursor
;
304 loader
->limit
= stream
->limit
;
310 FT_CALLBACK_DEF( void )
311 TT_Forget_Glyph_Frame( TT_Loader loader
)
313 FT_Stream stream
= loader
->stream
;
320 FT_CALLBACK_DEF( FT_Error
)
321 TT_Load_Glyph_Header( TT_Loader loader
)
323 FT_Byte
* p
= loader
->cursor
;
324 FT_Byte
* limit
= loader
->limit
;
327 if ( p
+ 10 > limit
)
328 return TT_Err_Invalid_Outline
;
330 loader
->n_contours
= FT_NEXT_SHORT( p
);
332 loader
->bbox
.xMin
= FT_NEXT_SHORT( p
);
333 loader
->bbox
.yMin
= FT_NEXT_SHORT( p
);
334 loader
->bbox
.xMax
= FT_NEXT_SHORT( p
);
335 loader
->bbox
.yMax
= FT_NEXT_SHORT( p
);
337 FT_TRACE5(( " # of contours: %d\n", loader
->n_contours
));
338 FT_TRACE5(( " xMin: %4d xMax: %4d\n", loader
->bbox
.xMin
,
339 loader
->bbox
.xMax
));
340 FT_TRACE5(( " yMin: %4d yMax: %4d\n", loader
->bbox
.yMin
,
341 loader
->bbox
.yMax
));
348 FT_CALLBACK_DEF( FT_Error
)
349 TT_Load_Simple_Glyph( TT_Loader load
)
352 FT_Byte
* p
= load
->cursor
;
353 FT_Byte
* limit
= load
->limit
;
354 FT_GlyphLoader gloader
= load
->gloader
;
355 FT_Int n_contours
= load
->n_contours
;
357 TT_Face face
= (TT_Face
)load
->face
;
361 FT_Byte
*flag
, *flag_limit
;
363 FT_Vector
*vec
, *vec_limit
;
365 FT_Short
*cont
, *cont_limit
, prev_cont
;
369 /* check that we can add the contours to the glyph */
370 error
= FT_GLYPHLOADER_CHECK_POINTS( gloader
, 0, n_contours
);
374 /* reading the contours' endpoints & number of points */
375 cont
= gloader
->current
.outline
.contours
;
376 cont_limit
= cont
+ n_contours
;
378 /* check space for contours array + instructions count */
379 if ( n_contours
>= 0xFFF || p
+ ( n_contours
+ 1 ) * 2 > limit
)
380 goto Invalid_Outline
;
382 prev_cont
= FT_NEXT_USHORT( p
);
384 if ( n_contours
> 0 )
387 for ( cont
++; cont
< cont_limit
; cont
++ )
389 cont
[0] = FT_NEXT_USHORT( p
);
390 if ( cont
[0] <= prev_cont
)
392 /* unordered contours: this is invalid */
393 error
= TT_Err_Invalid_Table
;
400 if ( n_contours
> 0 )
402 n_points
= cont
[-1] + 1;
404 goto Invalid_Outline
;
407 /* note that we will add four phantom points later */
408 error
= FT_GLYPHLOADER_CHECK_POINTS( gloader
, n_points
+ 4, 0 );
412 /* we'd better check the contours table right now */
413 outline
= &gloader
->current
.outline
;
415 for ( cont
= outline
->contours
+ 1; cont
< cont_limit
; cont
++ )
416 if ( cont
[-1] >= cont
[0] )
417 goto Invalid_Outline
;
419 /* reading the bytecode instructions */
420 load
->glyph
->control_len
= 0;
421 load
->glyph
->control_data
= 0;
424 goto Invalid_Outline
;
426 n_ins
= FT_NEXT_USHORT( p
);
428 FT_TRACE5(( " Instructions size: %u\n", n_ins
));
430 if ( n_ins
> face
->max_profile
.maxSizeOfInstructions
)
432 FT_TRACE0(( "TT_Load_Simple_Glyph: too many instructions (%d)\n",
434 error
= TT_Err_Too_Many_Hints
;
438 if ( ( limit
- p
) < n_ins
)
440 FT_TRACE0(( "TT_Load_Simple_Glyph: instruction count mismatch\n" ));
441 error
= TT_Err_Too_Many_Hints
;
445 #ifdef TT_USE_BYTECODE_INTERPRETER
447 if ( IS_HINTED( load
->load_flags
) )
449 load
->glyph
->control_len
= n_ins
;
450 load
->glyph
->control_data
= load
->exec
->glyphIns
;
452 FT_MEM_COPY( load
->exec
->glyphIns
, p
, (FT_Long
)n_ins
);
455 #endif /* TT_USE_BYTECODE_INTERPRETER */
459 /* reading the point tags */
460 flag
= (FT_Byte
*)outline
->tags
;
461 flag_limit
= flag
+ n_points
;
463 FT_ASSERT( flag
!= NULL
);
465 while ( flag
< flag_limit
)
468 goto Invalid_Outline
;
470 *flag
++ = c
= FT_NEXT_BYTE( p
);
474 goto Invalid_Outline
;
476 count
= FT_NEXT_BYTE( p
);
477 if ( flag
+ (FT_Int
)count
> flag_limit
)
478 goto Invalid_Outline
;
480 for ( ; count
> 0; count
-- )
485 /* reading the X coordinates */
487 vec
= outline
->points
;
488 vec_limit
= vec
+ n_points
;
489 flag
= (FT_Byte
*)outline
->tags
;
492 if ( p
+ xy_size
> limit
)
493 goto Invalid_Outline
;
495 for ( ; vec
< vec_limit
; vec
++, flag
++ )
504 goto Invalid_Outline
;
506 y
= (FT_Pos
)FT_NEXT_BYTE( p
);
507 if ( ( f
& 16 ) == 0 )
510 else if ( ( f
& 16 ) == 0 )
513 goto Invalid_Outline
;
515 y
= (FT_Pos
)FT_NEXT_SHORT( p
);
520 /* the cast is for stupid compilers */
521 *flag
= (FT_Byte
)( f
& ~( 2 | 16 ) );
524 /* reading the Y coordinates */
526 vec
= gloader
->current
.outline
.points
;
527 vec_limit
= vec
+ n_points
;
528 flag
= (FT_Byte
*)outline
->tags
;
531 for ( ; vec
< vec_limit
; vec
++, flag
++ )
540 goto Invalid_Outline
;
542 y
= (FT_Pos
)FT_NEXT_BYTE( p
);
543 if ( ( f
& 32 ) == 0 )
546 else if ( ( f
& 32 ) == 0 )
549 goto Invalid_Outline
;
551 y
= (FT_Pos
)FT_NEXT_SHORT( p
);
556 /* the cast is for stupid compilers */
557 *flag
= (FT_Byte
)( f
& FT_CURVE_TAG_ON
);
560 outline
->n_points
= (FT_UShort
)n_points
;
561 outline
->n_contours
= (FT_Short
) n_contours
;
569 error
= TT_Err_Invalid_Outline
;
574 FT_CALLBACK_DEF( FT_Error
)
575 TT_Load_Composite_Glyph( TT_Loader loader
)
578 FT_Byte
* p
= loader
->cursor
;
579 FT_Byte
* limit
= loader
->limit
;
580 FT_GlyphLoader gloader
= loader
->gloader
;
581 FT_SubGlyph subglyph
;
582 FT_UInt num_subglyphs
;
589 FT_Fixed xx
, xy
, yy
, yx
;
593 /* check that we can load a new subglyph */
594 error
= FT_GlyphLoader_CheckSubGlyphs( gloader
, num_subglyphs
+ 1 );
600 goto Invalid_Composite
;
602 subglyph
= gloader
->current
.subglyphs
+ num_subglyphs
;
604 subglyph
->arg1
= subglyph
->arg2
= 0;
606 subglyph
->flags
= FT_NEXT_USHORT( p
);
607 subglyph
->index
= FT_NEXT_USHORT( p
);
611 if ( subglyph
->flags
& ARGS_ARE_WORDS
)
613 if ( subglyph
->flags
& WE_HAVE_A_SCALE
)
615 else if ( subglyph
->flags
& WE_HAVE_AN_XY_SCALE
)
617 else if ( subglyph
->flags
& WE_HAVE_A_2X2
)
620 if ( p
+ count
> limit
)
621 goto Invalid_Composite
;
624 if ( subglyph
->flags
& ARGS_ARE_WORDS
)
626 subglyph
->arg1
= FT_NEXT_SHORT( p
);
627 subglyph
->arg2
= FT_NEXT_SHORT( p
);
631 subglyph
->arg1
= FT_NEXT_CHAR( p
);
632 subglyph
->arg2
= FT_NEXT_CHAR( p
);
639 if ( subglyph
->flags
& WE_HAVE_A_SCALE
)
641 xx
= (FT_Fixed
)FT_NEXT_SHORT( p
) << 2;
644 else if ( subglyph
->flags
& WE_HAVE_AN_XY_SCALE
)
646 xx
= (FT_Fixed
)FT_NEXT_SHORT( p
) << 2;
647 yy
= (FT_Fixed
)FT_NEXT_SHORT( p
) << 2;
649 else if ( subglyph
->flags
& WE_HAVE_A_2X2
)
651 xx
= (FT_Fixed
)FT_NEXT_SHORT( p
) << 2;
652 yx
= (FT_Fixed
)FT_NEXT_SHORT( p
) << 2;
653 xy
= (FT_Fixed
)FT_NEXT_SHORT( p
) << 2;
654 yy
= (FT_Fixed
)FT_NEXT_SHORT( p
) << 2;
657 subglyph
->transform
.xx
= xx
;
658 subglyph
->transform
.xy
= xy
;
659 subglyph
->transform
.yx
= yx
;
660 subglyph
->transform
.yy
= yy
;
664 } while ( subglyph
->flags
& MORE_COMPONENTS
);
666 gloader
->current
.num_subglyphs
= num_subglyphs
;
668 #ifdef TT_USE_BYTECODE_INTERPRETER
671 FT_Stream stream
= loader
->stream
;
674 /* we must undo the FT_FRAME_ENTER in order to point */
675 /* to the composite instructions, if we find some. */
676 /* We will process them later. */
678 loader
->ins_pos
= (FT_ULong
)( FT_STREAM_POS() +
690 error
= TT_Err_Invalid_Composite
;
696 TT_Init_Glyph_Loading( TT_Face face
)
698 face
->access_glyph_frame
= TT_Access_Glyph_Frame
;
699 face
->read_glyph_header
= TT_Load_Glyph_Header
;
700 face
->read_simple_glyph
= TT_Load_Simple_Glyph
;
701 face
->read_composite_glyph
= TT_Load_Composite_Glyph
;
702 face
->forget_glyph_frame
= TT_Forget_Glyph_Frame
;
707 tt_prepare_zone( TT_GlyphZone zone
,
710 FT_UInt start_contour
)
712 zone
->n_points
= (FT_UShort
)( load
->outline
.n_points
- start_point
);
713 zone
->n_contours
= (FT_Short
) ( load
->outline
.n_contours
-
715 zone
->org
= load
->extra_points
+ start_point
;
716 zone
->cur
= load
->outline
.points
+ start_point
;
717 zone
->orus
= load
->extra_points2
+ start_point
;
718 zone
->tags
= (FT_Byte
*)load
->outline
.tags
+ start_point
;
719 zone
->contours
= (FT_UShort
*)load
->outline
.contours
+ start_contour
;
720 zone
->first_point
= (FT_UShort
)start_point
;
724 /*************************************************************************/
730 /* Hint the glyph using the zone prepared by the caller. Note that */
731 /* the zone is supposed to include four phantom points. */
734 TT_Hint_Glyph( TT_Loader loader
,
735 FT_Bool is_composite
)
737 TT_GlyphZone zone
= &loader
->zone
;
740 #ifdef TT_USE_BYTECODE_INTERPRETER
743 FT_UNUSED( is_composite
);
747 #ifdef TT_USE_BYTECODE_INTERPRETER
748 if ( loader
->glyph
->control_len
> 0xFFFFL
)
750 FT_TRACE1(( "TT_Hint_Glyph: too long instructions " ));
751 FT_TRACE1(( "(0x%lx byte) is truncated\n",
752 loader
->glyph
->control_len
));
754 n_ins
= (FT_UInt
)( loader
->glyph
->control_len
);
757 origin
= zone
->cur
[zone
->n_points
- 4].x
;
758 origin
= FT_PIX_ROUND( origin
) - origin
;
760 translate_array( zone
->n_points
, zone
->cur
, origin
, 0 );
762 #ifdef TT_USE_BYTECODE_INTERPRETER
763 /* save original point position in org */
765 FT_ARRAY_COPY( zone
->org
, zone
->cur
, zone
->n_points
);
767 /* Reset graphics state. */
768 loader
->exec
->GS
= ((TT_Size
)loader
->size
)->GS
;
770 /* XXX: UNDOCUMENTED! Hinting instructions of a composite glyph */
771 /* completely refer to the (already) hinted subglyphs. */
774 loader
->exec
->metrics
.x_scale
= 1 << 16;
775 loader
->exec
->metrics
.y_scale
= 1 << 16;
777 FT_ARRAY_COPY( zone
->orus
, zone
->cur
, zone
->n_points
);
781 loader
->exec
->metrics
.x_scale
=
782 ((TT_Size
)loader
->size
)->metrics
.x_scale
;
783 loader
->exec
->metrics
.y_scale
=
784 ((TT_Size
)loader
->size
)->metrics
.y_scale
;
788 /* round pp2 and pp4 */
789 zone
->cur
[zone
->n_points
- 3].x
=
790 FT_PIX_ROUND( zone
->cur
[zone
->n_points
- 3].x
);
791 zone
->cur
[zone
->n_points
- 1].y
=
792 FT_PIX_ROUND( zone
->cur
[zone
->n_points
- 1].y
);
794 #ifdef TT_USE_BYTECODE_INTERPRETER
801 FT_GlyphLoader gloader
= loader
->gloader
;
802 FT_Outline current_outline
= gloader
->current
.outline
;
805 error
= TT_Set_CodeRange( loader
->exec
, tt_coderange_glyph
,
806 loader
->exec
->glyphIns
, n_ins
);
810 loader
->exec
->is_composite
= is_composite
;
811 loader
->exec
->pts
= *zone
;
813 debug
= FT_BOOL( !( loader
->load_flags
& FT_LOAD_NO_SCALE
) &&
814 ((TT_Size
)loader
->size
)->debug
);
816 error
= TT_Run_Context( loader
->exec
, debug
);
817 if ( error
&& loader
->exec
->pedantic_hinting
)
820 /* store drop-out mode in bits 5-7; set bit 2 also as a marker */
821 current_outline
.tags
[0] |=
822 ( loader
->exec
->GS
.scan_type
<< 5 ) | FT_CURVE_TAG_HAS_SCANMODE
;
827 /* save glyph phantom points */
828 if ( !loader
->preserve_pps
)
830 loader
->pp1
= zone
->cur
[zone
->n_points
- 4];
831 loader
->pp2
= zone
->cur
[zone
->n_points
- 3];
832 loader
->pp3
= zone
->cur
[zone
->n_points
- 2];
833 loader
->pp4
= zone
->cur
[zone
->n_points
- 1];
840 /*************************************************************************/
843 /* TT_Process_Simple_Glyph */
846 /* Once a simple glyph has been loaded, it needs to be processed. */
847 /* Usually, this means scaling and hinting through bytecode */
848 /* interpretation. */
851 TT_Process_Simple_Glyph( TT_Loader loader
)
853 FT_GlyphLoader gloader
= loader
->gloader
;
854 FT_Error error
= TT_Err_Ok
;
859 outline
= &gloader
->current
.outline
;
860 n_points
= outline
->n_points
;
862 /* set phantom points */
864 outline
->points
[n_points
] = loader
->pp1
;
865 outline
->points
[n_points
+ 1] = loader
->pp2
;
866 outline
->points
[n_points
+ 2] = loader
->pp3
;
867 outline
->points
[n_points
+ 3] = loader
->pp4
;
869 outline
->tags
[n_points
] = 0;
870 outline
->tags
[n_points
+ 1] = 0;
871 outline
->tags
[n_points
+ 2] = 0;
872 outline
->tags
[n_points
+ 3] = 0;
876 #ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
878 if ( ((TT_Face
)loader
->face
)->doblend
)
880 /* Deltas apply to the unscaled data. */
882 FT_Memory memory
= loader
->face
->memory
;
886 error
= TT_Vary_Get_Glyph_Deltas( (TT_Face
)(loader
->face
),
893 for ( i
= 0; i
< n_points
; ++i
)
895 outline
->points
[i
].x
+= deltas
[i
].x
;
896 outline
->points
[i
].y
+= deltas
[i
].y
;
902 #endif /* TT_CONFIG_OPTION_GX_VAR_SUPPORT */
904 if ( IS_HINTED( loader
->load_flags
) )
906 tt_prepare_zone( &loader
->zone
, &gloader
->current
, 0, 0 );
908 FT_ARRAY_COPY( loader
->zone
.orus
, loader
->zone
.cur
,
909 loader
->zone
.n_points
+ 4 );
912 /* scale the glyph */
913 if ( ( loader
->load_flags
& FT_LOAD_NO_SCALE
) == 0 )
915 FT_Vector
* vec
= outline
->points
;
916 FT_Vector
* limit
= outline
->points
+ n_points
;
917 FT_Fixed x_scale
= ((TT_Size
)loader
->size
)->metrics
.x_scale
;
918 FT_Fixed y_scale
= ((TT_Size
)loader
->size
)->metrics
.y_scale
;
921 for ( ; vec
< limit
; vec
++ )
923 vec
->x
= FT_MulFix( vec
->x
, x_scale
);
924 vec
->y
= FT_MulFix( vec
->y
, y_scale
);
927 loader
->pp1
= outline
->points
[n_points
- 4];
928 loader
->pp2
= outline
->points
[n_points
- 3];
929 loader
->pp3
= outline
->points
[n_points
- 2];
930 loader
->pp4
= outline
->points
[n_points
- 1];
933 if ( IS_HINTED( loader
->load_flags
) )
935 loader
->zone
.n_points
+= 4;
937 error
= TT_Hint_Glyph( loader
, 0 );
944 /*************************************************************************/
947 /* TT_Process_Composite_Component */
950 /* Once a composite component has been loaded, it needs to be */
951 /* processed. Usually, this means transforming and translating. */
954 TT_Process_Composite_Component( TT_Loader loader
,
955 FT_SubGlyph subglyph
,
957 FT_UInt num_base_points
)
959 FT_GlyphLoader gloader
= loader
->gloader
;
960 FT_Vector
* base_vec
= gloader
->base
.outline
.points
;
961 FT_UInt num_points
= gloader
->base
.outline
.n_points
;
966 have_scale
= FT_BOOL( subglyph
->flags
& ( WE_HAVE_A_SCALE
|
967 WE_HAVE_AN_XY_SCALE
|
970 /* perform the transform required for this subglyph */
976 for ( i
= num_base_points
; i
< num_points
; i
++ )
977 FT_Vector_Transform( base_vec
+ i
, &subglyph
->transform
);
981 if ( !( subglyph
->flags
& ARGS_ARE_XY_VALUES
) )
983 FT_UInt k
= subglyph
->arg1
;
984 FT_UInt l
= subglyph
->arg2
;
989 /* match l-th point of the newly loaded component to the k-th point */
990 /* of the previously loaded components. */
992 /* change to the point numbers used by our outline */
994 l
+= num_base_points
;
995 if ( k
>= num_base_points
||
997 return TT_Err_Invalid_Composite
;
999 p1
= gloader
->base
.outline
.points
+ k
;
1000 p2
= gloader
->base
.outline
.points
+ l
;
1013 /* Use a default value dependent on */
1014 /* TT_CONFIG_OPTION_COMPONENT_OFFSET_SCALED. This is useful for old TT */
1015 /* fonts which don't set the xxx_COMPONENT_OFFSET bit. */
1018 #ifdef TT_CONFIG_OPTION_COMPONENT_OFFSET_SCALED
1019 !( subglyph
->flags
& UNSCALED_COMPONENT_OFFSET
) )
1021 ( subglyph
->flags
& SCALED_COMPONENT_OFFSET
) )
1027 /*************************************************************************/
1029 /* This algorithm is what Apple documents. But it doesn't work. */
1031 int a
= subglyph
->transform
.xx
> 0 ? subglyph
->transform
.xx
1032 : -subglyph
->transform
.xx
;
1033 int b
= subglyph
->transform
.yx
> 0 ? subglyph
->transform
.yx
1034 : -subglyph
->transform
.yx
;
1035 int c
= subglyph
->transform
.xy
> 0 ? subglyph
->transform
.xy
1036 : -subglyph
->transform
.xy
;
1037 int d
= subglyph
->transform
.yy
> 0 ? subglyph
->transform
.yy
1038 : -subglyph
->transform
.yy
;
1039 int m
= a
> b
? a
: b
;
1040 int n
= c
> d
? c
: d
;
1043 if ( a
- b
<= 33 && a
- b
>= -33 )
1045 if ( c
- d
<= 33 && c
- d
>= -33 )
1047 x
= FT_MulFix( x
, m
);
1048 y
= FT_MulFix( y
, n
);
1052 /*************************************************************************/
1054 /* This algorithm is a guess and works much better than the above. */
1056 FT_Fixed mac_xscale
= FT_SqrtFixed(
1057 (FT_Int32
)FT_MulFix( subglyph
->transform
.xx
,
1058 subglyph
->transform
.xx
) +
1059 (FT_Int32
)FT_MulFix( subglyph
->transform
.xy
,
1060 subglyph
->transform
.xy
) );
1061 FT_Fixed mac_yscale
= FT_SqrtFixed(
1062 (FT_Int32
)FT_MulFix( subglyph
->transform
.yy
,
1063 subglyph
->transform
.yy
) +
1064 (FT_Int32
)FT_MulFix( subglyph
->transform
.yx
,
1065 subglyph
->transform
.yx
) );
1068 x
= FT_MulFix( x
, mac_xscale
);
1069 y
= FT_MulFix( y
, mac_yscale
);
1075 if ( !( loader
->load_flags
& FT_LOAD_NO_SCALE
) )
1077 FT_Fixed x_scale
= ((TT_Size
)loader
->size
)->metrics
.x_scale
;
1078 FT_Fixed y_scale
= ((TT_Size
)loader
->size
)->metrics
.y_scale
;
1081 x
= FT_MulFix( x
, x_scale
);
1082 y
= FT_MulFix( y
, y_scale
);
1084 if ( subglyph
->flags
& ROUND_XY_TO_GRID
)
1086 x
= FT_PIX_ROUND( x
);
1087 y
= FT_PIX_ROUND( y
);
1093 translate_array( num_points
- num_base_points
,
1094 base_vec
+ num_base_points
,
1101 /*************************************************************************/
1104 /* TT_Process_Composite_Glyph */
1107 /* This is slightly different from TT_Process_Simple_Glyph, in that */
1108 /* its sole purpose is to hint the glyph. Thus this function is */
1109 /* only available when bytecode interpreter is enabled. */
1112 TT_Process_Composite_Glyph( TT_Loader loader
,
1113 FT_UInt start_point
,
1114 FT_UInt start_contour
)
1117 FT_Outline
* outline
;
1121 outline
= &loader
->gloader
->base
.outline
;
1123 /* make room for phantom points */
1124 error
= FT_GLYPHLOADER_CHECK_POINTS( loader
->gloader
,
1125 outline
->n_points
+ 4,
1130 outline
->points
[outline
->n_points
] = loader
->pp1
;
1131 outline
->points
[outline
->n_points
+ 1] = loader
->pp2
;
1132 outline
->points
[outline
->n_points
+ 2] = loader
->pp3
;
1133 outline
->points
[outline
->n_points
+ 3] = loader
->pp4
;
1135 outline
->tags
[outline
->n_points
] = 0;
1136 outline
->tags
[outline
->n_points
+ 1] = 0;
1137 outline
->tags
[outline
->n_points
+ 2] = 0;
1138 outline
->tags
[outline
->n_points
+ 3] = 0;
1140 #ifdef TT_USE_BYTECODE_INTERPRETER
1143 FT_Stream stream
= loader
->stream
;
1144 FT_UShort n_ins
, max_ins
;
1148 /* TT_Load_Composite_Glyph only gives us the offset of instructions */
1149 /* so we read them here */
1150 if ( FT_STREAM_SEEK( loader
->ins_pos
) ||
1151 FT_READ_USHORT( n_ins
) )
1154 FT_TRACE5(( " Instructions size = %d\n", n_ins
));
1157 max_ins
= ((TT_Face
)loader
->face
)->max_profile
.maxSizeOfInstructions
;
1158 if ( n_ins
> max_ins
)
1160 /* acroread ignores this field, so we only do a rough safety check */
1161 if ( (FT_Int
)n_ins
> loader
->byte_len
)
1163 FT_TRACE1(( "TT_Process_Composite_Glyph: "
1164 "too many instructions (%d) for glyph with length %d\n",
1165 n_ins
, loader
->byte_len
));
1166 return TT_Err_Too_Many_Hints
;
1169 tmp
= loader
->exec
->glyphSize
;
1170 error
= Update_Max( loader
->exec
->memory
,
1173 (void*)&loader
->exec
->glyphIns
,
1175 loader
->exec
->glyphSize
= (FT_UShort
)tmp
;
1179 else if ( n_ins
== 0 )
1182 if ( FT_STREAM_READ( loader
->exec
->glyphIns
, n_ins
) )
1185 loader
->glyph
->control_data
= loader
->exec
->glyphIns
;
1186 loader
->glyph
->control_len
= n_ins
;
1191 tt_prepare_zone( &loader
->zone
, &loader
->gloader
->base
,
1192 start_point
, start_contour
);
1194 /* Some points are likely touched during execution of */
1195 /* instructions on components. So let's untouch them. */
1196 for ( i
= start_point
; i
< loader
->zone
.n_points
; i
++ )
1197 loader
->zone
.tags
[i
] &= ~FT_CURVE_TAG_TOUCH_BOTH
;
1199 loader
->zone
.n_points
+= 4;
1201 return TT_Hint_Glyph( loader
, 1 );
1205 /* Calculate the four phantom points. */
1206 /* The first two stand for horizontal origin and advance. */
1207 /* The last two stand for vertical origin and advance. */
1208 #define TT_LOADER_SET_PP( loader ) \
1210 (loader)->pp1.x = (loader)->bbox.xMin - (loader)->left_bearing; \
1211 (loader)->pp1.y = 0; \
1212 (loader)->pp2.x = (loader)->pp1.x + (loader)->advance; \
1213 (loader)->pp2.y = 0; \
1214 (loader)->pp3.x = 0; \
1215 (loader)->pp3.y = (loader)->top_bearing + (loader)->bbox.yMax; \
1216 (loader)->pp4.x = 0; \
1217 (loader)->pp4.y = (loader)->pp3.y - (loader)->vadvance; \
1221 /*************************************************************************/
1224 /* load_truetype_glyph */
1227 /* Loads a given truetype glyph. Handles composites and uses a */
1228 /* TT_Loader object. */
1231 load_truetype_glyph( TT_Loader loader
,
1232 FT_UInt glyph_index
,
1233 FT_UInt recurse_count
,
1234 FT_Bool header_only
)
1236 FT_Error error
= TT_Err_Ok
;
1237 FT_Fixed x_scale
, y_scale
;
1239 TT_Face face
= (TT_Face
)loader
->face
;
1240 FT_GlyphLoader gloader
= loader
->gloader
;
1241 FT_Bool opened_frame
= 0;
1243 #ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
1244 FT_Vector
* deltas
= NULL
;
1247 #ifdef FT_CONFIG_OPTION_INCREMENTAL
1248 FT_StreamRec inc_stream
;
1250 FT_Bool glyph_data_loaded
= 0;
1254 /* some fonts have an incorrect value of `maxComponentDepth', */
1255 /* thus we allow depth 1 to catch the majority of them */
1256 if ( recurse_count
> 1 &&
1257 recurse_count
> face
->max_profile
.maxComponentDepth
)
1259 error
= TT_Err_Invalid_Composite
;
1263 /* check glyph index */
1264 if ( glyph_index
>= (FT_UInt
)face
->root
.num_glyphs
)
1266 error
= TT_Err_Invalid_Glyph_Index
;
1270 loader
->glyph_index
= glyph_index
;
1272 if ( ( loader
->load_flags
& FT_LOAD_NO_SCALE
) == 0 )
1274 x_scale
= ((TT_Size
)loader
->size
)->metrics
.x_scale
;
1275 y_scale
= ((TT_Size
)loader
->size
)->metrics
.y_scale
;
1283 tt_get_metrics( loader
, glyph_index
);
1285 /* Set `offset' to the start of the glyph relative to the start of */
1286 /* the `glyf' table, and `byte_len' to the length of the glyph in */
1289 #ifdef FT_CONFIG_OPTION_INCREMENTAL
1291 /* If we are loading glyph data via the incremental interface, set */
1292 /* the loader stream to a memory stream reading the data returned */
1293 /* by the interface. */
1294 if ( face
->root
.internal
->incremental_interface
)
1296 error
= face
->root
.internal
->incremental_interface
->funcs
->get_glyph_data(
1297 face
->root
.internal
->incremental_interface
->object
,
1298 glyph_index
, &glyph_data
);
1302 glyph_data_loaded
= 1;
1304 loader
->byte_len
= glyph_data
.length
;
1306 FT_MEM_ZERO( &inc_stream
, sizeof ( inc_stream
) );
1307 FT_Stream_OpenMemory( &inc_stream
,
1308 glyph_data
.pointer
, glyph_data
.length
);
1310 loader
->stream
= &inc_stream
;
1314 #endif /* FT_CONFIG_OPTION_INCREMENTAL */
1316 offset
= tt_face_get_location( face
, glyph_index
,
1317 (FT_UInt
*)&loader
->byte_len
);
1319 if ( loader
->byte_len
> 0 )
1321 #ifdef FT_CONFIG_OPTION_INCREMENTAL
1322 /* for the incremental interface, `glyf_offset' is always zero */
1323 if ( !loader
->glyf_offset
&&
1324 !face
->root
.internal
->incremental_interface
)
1326 if ( !loader
->glyf_offset
)
1327 #endif /* FT_CONFIG_OPTION_INCREMENTAL */
1329 FT_TRACE2(( "no `glyf' table but non-zero `loca' entry\n" ));
1330 error
= TT_Err_Invalid_Table
;
1334 error
= face
->access_glyph_frame( loader
, glyph_index
,
1335 loader
->glyf_offset
+ offset
,
1342 /* read glyph header first */
1343 error
= face
->read_glyph_header( loader
);
1344 if ( error
|| header_only
)
1348 if ( loader
->byte_len
== 0 || loader
->n_contours
== 0 )
1350 loader
->bbox
.xMin
= 0;
1351 loader
->bbox
.xMax
= 0;
1352 loader
->bbox
.yMin
= 0;
1353 loader
->bbox
.yMax
= 0;
1358 /* must initialize points before (possibly) overriding */
1359 /* glyph metrics from the incremental interface */
1360 TT_LOADER_SET_PP( loader
);
1362 #ifdef FT_CONFIG_OPTION_INCREMENTAL
1363 tt_get_metrics_incr_overrides( loader
, glyph_index
);
1366 #ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
1368 if ( ((TT_Face
)(loader
->face
))->doblend
)
1370 /* this must be done before scaling */
1371 FT_Memory memory
= loader
->face
->memory
;
1374 error
= TT_Vary_Get_Glyph_Deltas( (TT_Face
)(loader
->face
),
1375 glyph_index
, &deltas
, 4 );
1379 loader
->pp1
.x
+= deltas
[0].x
; loader
->pp1
.y
+= deltas
[0].y
;
1380 loader
->pp2
.x
+= deltas
[1].x
; loader
->pp2
.y
+= deltas
[1].y
;
1381 loader
->pp3
.x
+= deltas
[2].x
; loader
->pp3
.y
+= deltas
[2].y
;
1382 loader
->pp4
.x
+= deltas
[3].x
; loader
->pp4
.y
+= deltas
[3].y
;
1389 if ( ( loader
->load_flags
& FT_LOAD_NO_SCALE
) == 0 )
1391 loader
->pp1
.x
= FT_MulFix( loader
->pp1
.x
, x_scale
);
1392 loader
->pp2
.x
= FT_MulFix( loader
->pp2
.x
, x_scale
);
1393 loader
->pp3
.y
= FT_MulFix( loader
->pp3
.y
, y_scale
);
1394 loader
->pp4
.y
= FT_MulFix( loader
->pp4
.y
, y_scale
);
1401 /* must initialize points before (possibly) overriding */
1402 /* glyph metrics from the incremental interface */
1403 TT_LOADER_SET_PP( loader
);
1405 #ifdef FT_CONFIG_OPTION_INCREMENTAL
1406 tt_get_metrics_incr_overrides( loader
, glyph_index
);
1409 /***********************************************************************/
1410 /***********************************************************************/
1411 /***********************************************************************/
1413 /* if it is a simple glyph, load it */
1415 if ( loader
->n_contours
> 0 )
1417 error
= face
->read_simple_glyph( loader
);
1421 /* all data have been read */
1422 face
->forget_glyph_frame( loader
);
1425 error
= TT_Process_Simple_Glyph( loader
);
1429 FT_GlyphLoader_Add( gloader
);
1432 /***********************************************************************/
1433 /***********************************************************************/
1434 /***********************************************************************/
1436 /* otherwise, load a composite! */
1437 else if ( loader
->n_contours
== -1 )
1439 FT_UInt start_point
;
1440 FT_UInt start_contour
;
1441 FT_ULong ins_pos
; /* position of composite instructions, if any */
1444 start_point
= gloader
->base
.outline
.n_points
;
1445 start_contour
= gloader
->base
.outline
.n_contours
;
1447 /* for each subglyph, read composite header */
1448 error
= face
->read_composite_glyph( loader
);
1452 /* store the offset of instructions */
1453 ins_pos
= loader
->ins_pos
;
1455 /* all data we need are read */
1456 face
->forget_glyph_frame( loader
);
1459 #ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
1461 if ( face
->doblend
)
1464 FT_SubGlyph subglyph
;
1465 FT_Memory memory
= face
->root
.memory
;
1468 /* this provides additional offsets */
1469 /* for each component's translation */
1471 if ( ( error
= TT_Vary_Get_Glyph_Deltas(
1475 gloader
->current
.num_subglyphs
+ 4 )) != 0 )
1478 subglyph
= gloader
->current
.subglyphs
+ gloader
->base
.num_subglyphs
;
1479 limit
= gloader
->current
.num_subglyphs
;
1481 for ( i
= 0; i
< limit
; ++i
, ++subglyph
)
1483 if ( subglyph
->flags
& ARGS_ARE_XY_VALUES
)
1485 /* XXX: overflow check for subglyph->{arg1,arg2}. */
1486 /* deltas[i].{x,y} must be within signed 16-bit, */
1487 /* but the restriction of summed delta is not clear */
1488 subglyph
->arg1
+= (FT_Int16
)deltas
[i
].x
;
1489 subglyph
->arg2
+= (FT_Int16
)deltas
[i
].y
;
1493 loader
->pp1
.x
+= deltas
[i
+ 0].x
; loader
->pp1
.y
+= deltas
[i
+ 0].y
;
1494 loader
->pp2
.x
+= deltas
[i
+ 1].x
; loader
->pp2
.y
+= deltas
[i
+ 1].y
;
1495 loader
->pp3
.x
+= deltas
[i
+ 2].x
; loader
->pp3
.y
+= deltas
[i
+ 2].y
;
1496 loader
->pp4
.x
+= deltas
[i
+ 3].x
; loader
->pp4
.y
+= deltas
[i
+ 3].y
;
1501 #endif /* TT_CONFIG_OPTION_GX_VAR_SUPPORT */
1503 if ( ( loader
->load_flags
& FT_LOAD_NO_SCALE
) == 0 )
1505 loader
->pp1
.x
= FT_MulFix( loader
->pp1
.x
, x_scale
);
1506 loader
->pp2
.x
= FT_MulFix( loader
->pp2
.x
, x_scale
);
1507 loader
->pp3
.y
= FT_MulFix( loader
->pp3
.y
, y_scale
);
1508 loader
->pp4
.y
= FT_MulFix( loader
->pp4
.y
, y_scale
);
1511 /* if the flag FT_LOAD_NO_RECURSE is set, we return the subglyph */
1512 /* `as is' in the glyph slot (the client application will be */
1513 /* responsible for interpreting these data)... */
1514 if ( loader
->load_flags
& FT_LOAD_NO_RECURSE
)
1516 FT_GlyphLoader_Add( gloader
);
1517 loader
->glyph
->format
= FT_GLYPH_FORMAT_COMPOSITE
;
1522 /*********************************************************************/
1523 /*********************************************************************/
1524 /*********************************************************************/
1527 FT_UInt n
, num_base_points
;
1528 FT_SubGlyph subglyph
= 0;
1530 FT_UInt num_points
= start_point
;
1531 FT_UInt num_subglyphs
= gloader
->current
.num_subglyphs
;
1532 FT_UInt num_base_subgs
= gloader
->base
.num_subglyphs
;
1534 FT_Stream old_stream
= loader
->stream
;
1535 FT_Int old_byte_len
= loader
->byte_len
;
1538 FT_GlyphLoader_Add( gloader
);
1540 /* read each subglyph independently */
1541 for ( n
= 0; n
< num_subglyphs
; n
++ )
1546 /* Each time we call load_truetype_glyph in this loop, the */
1547 /* value of `gloader.base.subglyphs' can change due to table */
1548 /* reallocations. We thus need to recompute the subglyph */
1549 /* pointer on each iteration. */
1550 subglyph
= gloader
->base
.subglyphs
+ num_base_subgs
+ n
;
1552 pp
[0] = loader
->pp1
;
1553 pp
[1] = loader
->pp2
;
1554 pp
[2] = loader
->pp3
;
1555 pp
[3] = loader
->pp4
;
1557 num_base_points
= gloader
->base
.outline
.n_points
;
1559 error
= load_truetype_glyph( loader
, subglyph
->index
,
1560 recurse_count
+ 1, FALSE
);
1564 /* restore subglyph pointer */
1565 subglyph
= gloader
->base
.subglyphs
+ num_base_subgs
+ n
;
1567 if ( !( subglyph
->flags
& USE_MY_METRICS
) )
1569 loader
->pp1
= pp
[0];
1570 loader
->pp2
= pp
[1];
1571 loader
->pp3
= pp
[2];
1572 loader
->pp4
= pp
[3];
1575 num_points
= gloader
->base
.outline
.n_points
;
1577 if ( num_points
== num_base_points
)
1580 /* gloader->base.outline consists of three parts: */
1581 /* 0 -(1)-> start_point -(2)-> num_base_points -(3)-> n_points. */
1583 /* (1): exists from the beginning */
1584 /* (2): components that have been loaded so far */
1585 /* (3): the newly loaded component */
1586 TT_Process_Composite_Component( loader
, subglyph
, start_point
,
1590 loader
->stream
= old_stream
;
1591 loader
->byte_len
= old_byte_len
;
1593 /* process the glyph */
1594 loader
->ins_pos
= ins_pos
;
1595 if ( IS_HINTED( loader
->load_flags
) &&
1597 #ifdef TT_USE_BYTECODE_INTERPRETER
1599 subglyph
->flags
& WE_HAVE_INSTR
&&
1603 num_points
> start_point
)
1604 TT_Process_Composite_Glyph( loader
, start_point
, start_contour
);
1610 /* invalid composite count (negative but not -1) */
1611 error
= TT_Err_Invalid_Outline
;
1615 /***********************************************************************/
1616 /***********************************************************************/
1617 /***********************************************************************/
1622 face
->forget_glyph_frame( loader
);
1624 #ifdef FT_CONFIG_OPTION_INCREMENTAL
1626 if ( glyph_data_loaded
)
1627 face
->root
.internal
->incremental_interface
->funcs
->free_glyph_data(
1628 face
->root
.internal
->incremental_interface
->object
,
1638 compute_glyph_metrics( TT_Loader loader
,
1639 FT_UInt glyph_index
)
1642 TT_Face face
= (TT_Face
)loader
->face
;
1644 TT_GlyphSlot glyph
= loader
->glyph
;
1645 TT_Size size
= (TT_Size
)loader
->size
;
1649 if ( ( loader
->load_flags
& FT_LOAD_NO_SCALE
) == 0 )
1650 y_scale
= size
->root
.metrics
.y_scale
;
1652 if ( glyph
->format
!= FT_GLYPH_FORMAT_COMPOSITE
)
1653 FT_Outline_Get_CBox( &glyph
->outline
, &bbox
);
1655 bbox
= loader
->bbox
;
1657 /* get the device-independent horizontal advance; it is scaled later */
1658 /* by the base layer. */
1660 FT_Pos advance
= loader
->linear
;
1663 /* the flag FT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH was introduced to */
1664 /* correctly support DynaLab fonts, which have an incorrect */
1665 /* `advance_Width_Max' field! It is used, to my knowledge, */
1666 /* exclusively in the X-TrueType font server. */
1668 if ( face
->postscript
.isFixedPitch
&&
1669 ( loader
->load_flags
& FT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH
) == 0 )
1670 advance
= face
->horizontal
.advance_Width_Max
;
1672 /* we need to return the advance in font units in linearHoriAdvance, */
1673 /* it will be scaled later by the base layer. */
1674 glyph
->linearHoriAdvance
= advance
;
1677 glyph
->metrics
.horiBearingX
= bbox
.xMin
;
1678 glyph
->metrics
.horiBearingY
= bbox
.yMax
;
1679 glyph
->metrics
.horiAdvance
= loader
->pp2
.x
- loader
->pp1
.x
;
1681 /* adjust advance width to the value contained in the hdmx table */
1682 if ( !face
->postscript
.isFixedPitch
&&
1683 IS_HINTED( loader
->load_flags
) )
1688 widthp
= tt_face_get_device_metrics( face
,
1689 size
->root
.metrics
.x_ppem
,
1693 glyph
->metrics
.horiAdvance
= *widthp
<< 6;
1696 /* set glyph dimensions */
1697 glyph
->metrics
.width
= bbox
.xMax
- bbox
.xMin
;
1698 glyph
->metrics
.height
= bbox
.yMax
- bbox
.yMin
;
1700 /* Now take care of vertical metrics. In the case where there is */
1701 /* no vertical information within the font (relatively common), */
1702 /* create some metrics manually */
1704 FT_Pos top
; /* scaled vertical top side bearing */
1705 FT_Pos advance
; /* scaled vertical advance height */
1708 /* Get the unscaled top bearing and advance height. */
1709 if ( face
->vertical_info
&&
1710 face
->vertical
.number_Of_VMetrics
> 0 )
1712 top
= (FT_Short
)FT_DivFix( loader
->pp3
.y
- bbox
.yMax
,
1715 if ( loader
->pp3
.y
<= loader
->pp4
.y
)
1718 advance
= (FT_UShort
)FT_DivFix( loader
->pp3
.y
- loader
->pp4
.y
,
1726 /* XXX Compute top side bearing and advance height in */
1727 /* Get_VMetrics instead of here. */
1729 /* NOTE: The OS/2 values are the only `portable' ones, */
1730 /* which is why we use them, if there is an OS/2 */
1731 /* table in the font. Otherwise, we use the */
1732 /* values defined in the horizontal header. */
1734 height
= (FT_Short
)FT_DivFix( bbox
.yMax
- bbox
.yMin
,
1736 if ( face
->os2
.version
!= 0xFFFFU
)
1737 advance
= (FT_Pos
)( face
->os2
.sTypoAscender
-
1738 face
->os2
.sTypoDescender
);
1740 advance
= (FT_Pos
)( face
->horizontal
.Ascender
-
1741 face
->horizontal
.Descender
);
1743 top
= ( advance
- height
) / 2;
1746 #ifdef FT_CONFIG_OPTION_INCREMENTAL
1748 FT_Incremental_InterfaceRec
* incr
;
1749 FT_Incremental_MetricsRec metrics
;
1753 incr
= face
->root
.internal
->incremental_interface
;
1755 /* If this is an incrementally loaded font see if there are */
1756 /* overriding metrics for this glyph. */
1757 if ( incr
&& incr
->funcs
->get_glyph_metrics
)
1759 metrics
.bearing_x
= 0;
1760 metrics
.bearing_y
= top
;
1761 metrics
.advance
= advance
;
1763 error
= incr
->funcs
->get_glyph_metrics( incr
->object
,
1770 top
= metrics
.bearing_y
;
1771 advance
= metrics
.advance
;
1775 /* GWW: Do vertical metrics get loaded incrementally too? */
1777 #endif /* FT_CONFIG_OPTION_INCREMENTAL */
1779 glyph
->linearVertAdvance
= advance
;
1781 /* scale the metrics */
1782 if ( !( loader
->load_flags
& FT_LOAD_NO_SCALE
) )
1784 top
= FT_MulFix( top
, y_scale
);
1785 advance
= FT_MulFix( advance
, y_scale
);
1788 /* XXX: for now, we have no better algorithm for the lsb, but it */
1789 /* should work fine. */
1791 glyph
->metrics
.vertBearingX
= glyph
->metrics
.horiBearingX
-
1792 glyph
->metrics
.horiAdvance
/ 2;
1793 glyph
->metrics
.vertBearingY
= top
;
1794 glyph
->metrics
.vertAdvance
= advance
;
1801 #ifdef TT_CONFIG_OPTION_EMBEDDED_BITMAPS
1804 load_sbit_image( TT_Size size
,
1806 FT_UInt glyph_index
,
1807 FT_Int32 load_flags
)
1813 TT_SBit_MetricsRec metrics
;
1816 face
= (TT_Face
)glyph
->face
;
1817 sfnt
= (SFNT_Service
)face
->sfnt
;
1818 stream
= face
->root
.stream
;
1820 error
= sfnt
->load_sbit_image( face
,
1829 glyph
->outline
.n_points
= 0;
1830 glyph
->outline
.n_contours
= 0;
1832 glyph
->metrics
.width
= (FT_Pos
)metrics
.width
<< 6;
1833 glyph
->metrics
.height
= (FT_Pos
)metrics
.height
<< 6;
1835 glyph
->metrics
.horiBearingX
= (FT_Pos
)metrics
.horiBearingX
<< 6;
1836 glyph
->metrics
.horiBearingY
= (FT_Pos
)metrics
.horiBearingY
<< 6;
1837 glyph
->metrics
.horiAdvance
= (FT_Pos
)metrics
.horiAdvance
<< 6;
1839 glyph
->metrics
.vertBearingX
= (FT_Pos
)metrics
.vertBearingX
<< 6;
1840 glyph
->metrics
.vertBearingY
= (FT_Pos
)metrics
.vertBearingY
<< 6;
1841 glyph
->metrics
.vertAdvance
= (FT_Pos
)metrics
.vertAdvance
<< 6;
1843 glyph
->format
= FT_GLYPH_FORMAT_BITMAP
;
1845 if ( load_flags
& FT_LOAD_VERTICAL_LAYOUT
)
1847 glyph
->bitmap_left
= metrics
.vertBearingX
;
1848 glyph
->bitmap_top
= metrics
.vertBearingY
;
1852 glyph
->bitmap_left
= metrics
.horiBearingX
;
1853 glyph
->bitmap_top
= metrics
.horiBearingY
;
1860 #endif /* TT_CONFIG_OPTION_EMBEDDED_BITMAPS */
1864 tt_loader_init( TT_Loader loader
,
1867 FT_Int32 load_flags
,
1868 FT_Bool glyf_table_only
)
1874 face
= (TT_Face
)glyph
->face
;
1875 stream
= face
->root
.stream
;
1877 FT_MEM_ZERO( loader
, sizeof ( TT_LoaderRec
) );
1879 #ifdef TT_USE_BYTECODE_INTERPRETER
1881 /* load execution context */
1882 if ( IS_HINTED( load_flags
) && !glyf_table_only
)
1884 TT_ExecContext exec
;
1888 if ( !size
->cvt_ready
)
1890 FT_Error error
= tt_size_ready_bytecode( size
);
1895 /* query new execution context */
1896 exec
= size
->debug
? size
->context
1897 : ( (TT_Driver
)FT_FACE_DRIVER( face
) )->context
;
1899 return TT_Err_Could_Not_Find_Context
;
1902 FT_BOOL( FT_LOAD_TARGET_MODE( load_flags
) != FT_RENDER_MODE_MONO
);
1904 TT_Load_Context( exec
, face
, size
);
1906 /* a change from mono to grayscale rendering (and vice versa) */
1907 /* requires a re-execution of the CVT program */
1908 if ( grayscale
!= exec
->grayscale
)
1913 FT_TRACE4(( "tt_loader_init: grayscale change,"
1914 " re-executing `prep' table\n" ));
1916 exec
->grayscale
= grayscale
;
1918 for ( i
= 0; i
< size
->cvt_size
; i
++ )
1919 size
->cvt
[i
] = FT_MulFix( face
->cvt
[i
], size
->ttmetrics
.scale
);
1920 tt_size_run_prep( size
);
1923 /* see whether the cvt program has disabled hinting */
1924 if ( exec
->GS
.instruct_control
& 1 )
1925 load_flags
|= FT_LOAD_NO_HINTING
;
1927 /* load default graphics state -- if needed */
1928 if ( exec
->GS
.instruct_control
& 2 )
1929 exec
->GS
= tt_default_graphics_state
;
1931 exec
->pedantic_hinting
= FT_BOOL( load_flags
& FT_LOAD_PEDANTIC
);
1932 loader
->exec
= exec
;
1933 loader
->instructions
= exec
->glyphIns
;
1936 #endif /* TT_USE_BYTECODE_INTERPRETER */
1938 /* seek to the beginning of the glyph table -- for Type 42 fonts */
1939 /* the table might be accessed from a Postscript stream or something */
1942 #ifdef FT_CONFIG_OPTION_INCREMENTAL
1944 if ( face
->root
.internal
->incremental_interface
)
1945 loader
->glyf_offset
= 0;
1951 FT_Error error
= face
->goto_table( face
, TTAG_glyf
, stream
, 0 );
1954 if ( error
== TT_Err_Table_Missing
)
1955 loader
->glyf_offset
= 0;
1958 FT_ERROR(( "tt_loader_init: could not access glyph table\n" ));
1962 loader
->glyf_offset
= FT_STREAM_POS();
1965 /* get face's glyph loader */
1966 if ( !glyf_table_only
)
1968 FT_GlyphLoader gloader
= glyph
->internal
->loader
;
1971 FT_GlyphLoader_Rewind( gloader
);
1972 loader
->gloader
= gloader
;
1975 loader
->load_flags
= load_flags
;
1977 loader
->face
= (FT_Face
)face
;
1978 loader
->size
= (FT_Size
)size
;
1979 loader
->glyph
= (FT_GlyphSlot
)glyph
;
1980 loader
->stream
= stream
;
1986 /*************************************************************************/
1992 /* A function used to load a single glyph within a given glyph slot, */
1993 /* for a given size. */
1996 /* glyph :: A handle to a target slot object where the glyph */
1997 /* will be loaded. */
1999 /* size :: A handle to the source face size at which the glyph */
2000 /* must be scaled/loaded. */
2002 /* glyph_index :: The index of the glyph in the font file. */
2004 /* load_flags :: A flag indicating what to load for this glyph. The */
2005 /* FT_LOAD_XXX constants can be used to control the */
2006 /* glyph loading process (e.g., whether the outline */
2007 /* should be scaled, whether to load bitmaps or not, */
2008 /* whether to hint the outline, etc). */
2011 /* FreeType error code. 0 means success. */
2013 FT_LOCAL_DEF( FT_Error
)
2014 TT_Load_Glyph( TT_Size size
,
2016 FT_UInt glyph_index
,
2017 FT_Int32 load_flags
)
2021 TT_LoaderRec loader
;
2024 face
= (TT_Face
)glyph
->face
;
2027 #ifdef TT_CONFIG_OPTION_EMBEDDED_BITMAPS
2029 /* try to load embedded bitmap if any */
2031 /* XXX: The convention should be emphasized in */
2032 /* the documents because it can be confusing. */
2033 if ( size
->strike_index
!= 0xFFFFFFFFUL
&&
2034 ( load_flags
& FT_LOAD_NO_BITMAP
) == 0 )
2036 error
= load_sbit_image( size
, glyph
, glyph_index
, load_flags
);
2039 FT_Face root
= &face
->root
;
2042 if ( FT_IS_SCALABLE( root
) )
2044 /* for the bbox we need the header only */
2045 (void)tt_loader_init( &loader
, size
, glyph
, load_flags
, TRUE
);
2046 (void)load_truetype_glyph( &loader
, glyph_index
, 0, TRUE
);
2047 glyph
->linearHoriAdvance
= loader
.linear
;
2048 glyph
->linearVertAdvance
= loader
.top_bearing
+ loader
.bbox
.yMax
-
2050 if ( face
->postscript
.isFixedPitch
&&
2051 ( load_flags
& FT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH
) == 0 )
2052 glyph
->linearHoriAdvance
= face
->horizontal
.advance_Width_Max
;
2059 #endif /* TT_CONFIG_OPTION_EMBEDDED_BITMAPS */
2061 /* if FT_LOAD_NO_SCALE is not set, `ttmetrics' must be valid */
2062 if ( !( load_flags
& FT_LOAD_NO_SCALE
) && !size
->ttmetrics
.valid
)
2063 return TT_Err_Invalid_Size_Handle
;
2065 if ( load_flags
& FT_LOAD_SBITS_ONLY
)
2066 return TT_Err_Invalid_Argument
;
2068 error
= tt_loader_init( &loader
, size
, glyph
, load_flags
, FALSE
);
2072 glyph
->format
= FT_GLYPH_FORMAT_OUTLINE
;
2073 glyph
->num_subglyphs
= 0;
2074 glyph
->outline
.flags
= 0;
2076 /* main loading loop */
2077 error
= load_truetype_glyph( &loader
, glyph_index
, 0, FALSE
);
2080 if ( glyph
->format
== FT_GLYPH_FORMAT_COMPOSITE
)
2082 glyph
->num_subglyphs
= loader
.gloader
->base
.num_subglyphs
;
2083 glyph
->subglyphs
= loader
.gloader
->base
.subglyphs
;
2087 glyph
->outline
= loader
.gloader
->base
.outline
;
2088 glyph
->outline
.flags
&= ~FT_OUTLINE_SINGLE_PASS
;
2090 /* Translate array so that (0,0) is the glyph's origin. Note */
2091 /* that this behaviour is independent on the value of bit 1 of */
2092 /* the `flags' field in the `head' table -- at least major */
2093 /* applications like Acroread indicate that. */
2095 FT_Outline_Translate( &glyph
->outline
, -loader
.pp1
.x
, 0 );
2098 #ifdef TT_USE_BYTECODE_INTERPRETER
2100 if ( IS_HINTED( load_flags
) )
2102 if ( loader
.exec
->GS
.scan_control
)
2104 /* convert scan conversion mode to FT_OUTLINE_XXX flags */
2105 switch ( loader
.exec
->GS
.scan_type
)
2107 case 0: /* simple drop-outs including stubs */
2108 glyph
->outline
.flags
|= FT_OUTLINE_INCLUDE_STUBS
;
2110 case 1: /* simple drop-outs excluding stubs */
2111 /* nothing; it's the default rendering mode */
2113 case 4: /* smart drop-outs including stubs */
2114 glyph
->outline
.flags
|= FT_OUTLINE_SMART_DROPOUTS
|
2115 FT_OUTLINE_INCLUDE_STUBS
;
2117 case 5: /* smart drop-outs excluding stubs */
2118 glyph
->outline
.flags
|= FT_OUTLINE_SMART_DROPOUTS
;
2121 default: /* no drop-out control */
2122 glyph
->outline
.flags
|= FT_OUTLINE_IGNORE_DROPOUTS
;
2127 glyph
->outline
.flags
|= FT_OUTLINE_IGNORE_DROPOUTS
;
2130 #endif /* TT_USE_BYTECODE_INTERPRETER */
2132 compute_glyph_metrics( &loader
, glyph_index
);
2135 /* Set the `high precision' bit flag. */
2136 /* This is _critical_ to get correct output for monochrome */
2137 /* TrueType glyphs at all sizes using the bytecode interpreter. */
2139 if ( !( load_flags
& FT_LOAD_NO_SCALE
) &&
2140 size
->root
.metrics
.y_ppem
< 24 )
2141 glyph
->outline
.flags
|= FT_OUTLINE_HIGH_PRECISION
;