1 /***************************************************************************/
5 /* TrueType Glyph Loader (body). */
7 /* Copyright 1996-2012 */
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_DEBUG_H
21 #include FT_INTERNAL_CALC_H
22 #include FT_INTERNAL_STREAM_H
23 #include FT_INTERNAL_SFNT_H
24 #include FT_TRUETYPE_TAGS_H
30 #ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
37 /*************************************************************************/
39 /* The macro FT_COMPONENT is used in trace mode. It is an implicit */
40 /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */
41 /* messages during execution. */
44 #define FT_COMPONENT trace_ttgload
47 /*************************************************************************/
49 /* Composite glyph flags. */
51 #define ARGS_ARE_WORDS 0x0001
52 #define ARGS_ARE_XY_VALUES 0x0002
53 #define ROUND_XY_TO_GRID 0x0004
54 #define WE_HAVE_A_SCALE 0x0008
56 #define MORE_COMPONENTS 0x0020
57 #define WE_HAVE_AN_XY_SCALE 0x0040
58 #define WE_HAVE_A_2X2 0x0080
59 #define WE_HAVE_INSTR 0x0100
60 #define USE_MY_METRICS 0x0200
61 #define OVERLAP_COMPOUND 0x0400
62 #define SCALED_COMPONENT_OFFSET 0x0800
63 #define UNSCALED_COMPONENT_OFFSET 0x1000
66 /*************************************************************************/
68 /* Return the horizontal metrics in font units for a given glyph. */
71 TT_Get_HMetrics( TT_Face face,
76 ( (SFNT_Service)face->sfnt )->get_metrics( face, 0, idx, lsb, aw );
78 FT_TRACE5(( " advance width (font units): %d\n", *aw ));
79 FT_TRACE5(( " left side bearing (font units): %d\n", *lsb ));
83 /*************************************************************************/
85 /* Return the vertical metrics in font units for a given glyph. */
86 /* Greg Hitchcock from Microsoft told us that if there were no `vmtx' */
87 /* table, typoAscender/Descender from the `OS/2' table would be used */
88 /* instead, and if there were no `OS/2' table, use ascender/descender */
89 /* from the `hhea' table. But that is not what Microsoft's rasterizer */
90 /* apparently does: It uses the ppem value as the advance height, and */
91 /* sets the top side bearing to be zero. */
94 TT_Get_VMetrics( TT_Face face,
99 if ( face->vertical_info )
100 ( (SFNT_Service)face->sfnt )->get_metrics( face, 1, idx, tsb, ah );
102 #if 1 /* Empirically determined, at variance with what MS said */
107 *ah = face->root.units_per_EM;
110 #else /* This is what MS said to do. It isn't what they do, however. */
112 else if ( face->os2.version != 0xFFFFU )
114 *tsb = face->os2.sTypoAscender;
115 *ah = face->os2.sTypoAscender - face->os2.sTypoDescender;
119 *tsb = face->horizontal.Ascender;
120 *ah = face->horizontal.Ascender - face->horizontal.Descender;
125 FT_TRACE5(( " advance height (font units): %d\n", *ah ));
126 FT_TRACE5(( " top side bearing (font units): %d\n", *tsb ));
131 tt_get_metrics( TT_Loader loader,
132 FT_UInt glyph_index )
134 TT_Face face = (TT_Face)loader->face;
136 FT_Short left_bearing = 0, top_bearing = 0;
137 FT_UShort advance_width = 0, advance_height = 0;
140 TT_Get_HMetrics( face, glyph_index,
143 TT_Get_VMetrics( face, glyph_index,
147 loader->left_bearing = left_bearing;
148 loader->advance = advance_width;
149 loader->top_bearing = top_bearing;
150 loader->vadvance = advance_height;
152 if ( !loader->linear_def )
154 loader->linear_def = 1;
155 loader->linear = advance_width;
160 #ifdef FT_CONFIG_OPTION_INCREMENTAL
163 tt_get_metrics_incr_overrides( TT_Loader loader,
164 FT_UInt glyph_index )
166 TT_Face face = (TT_Face)loader->face;
168 FT_Short left_bearing = 0, top_bearing = 0;
169 FT_UShort advance_width = 0, advance_height = 0;
172 /* If this is an incrementally loaded font check whether there are */
173 /* overriding metrics for this glyph. */
174 if ( face->root.internal->incremental_interface &&
175 face->root.internal->incremental_interface->funcs->get_glyph_metrics )
177 FT_Incremental_MetricsRec metrics;
181 metrics.bearing_x = loader->left_bearing;
182 metrics.bearing_y = 0;
183 metrics.advance = loader->advance;
184 metrics.advance_v = 0;
186 error = face->root.internal->incremental_interface->funcs->get_glyph_metrics(
187 face->root.internal->incremental_interface->object,
188 glyph_index, FALSE, &metrics );
192 left_bearing = (FT_Short)metrics.bearing_x;
193 advance_width = (FT_UShort)metrics.advance;
197 /* GWW: Do I do the same for vertical metrics? */
198 metrics.bearing_x = 0;
199 metrics.bearing_y = loader->top_bearing;
200 metrics.advance = loader->vadvance;
202 error = face->root.internal->incremental_interface->funcs->get_glyph_metrics(
203 face->root.internal->incremental_interface->object,
204 glyph_index, TRUE, &metrics );
208 top_bearing = (FT_Short)metrics.bearing_y;
209 advance_height = (FT_UShort)metrics.advance;
213 loader->left_bearing = left_bearing;
214 loader->advance = advance_width;
215 loader->top_bearing = top_bearing;
216 loader->vadvance = advance_height;
218 if ( !loader->linear_def )
220 loader->linear_def = 1;
221 loader->linear = advance_width;
229 #endif /* FT_CONFIG_OPTION_INCREMENTAL */
232 /*************************************************************************/
234 /* Translates an array of coordinates. */
237 translate_array( FT_UInt n,
246 for ( k = 0; k < n; k++ )
247 coords[k].x += delta_x;
250 for ( k = 0; k < n; k++ )
251 coords[k].y += delta_y;
256 #define IS_HINTED( flags ) ( ( flags & FT_LOAD_NO_HINTING ) == 0 )
259 /*************************************************************************/
261 /* The following functions are used by default with TrueType fonts. */
262 /* However, they can be replaced by alternatives if we need to support */
263 /* TrueType-compressed formats (like MicroType) in the future. */
265 /*************************************************************************/
267 FT_CALLBACK_DEF( FT_Error )
268 TT_Access_Glyph_Frame( TT_Loader loader,
274 FT_Stream stream = loader->stream;
276 /* for non-debug mode */
277 FT_UNUSED( glyph_index );
280 FT_TRACE4(( "Glyph %ld\n", glyph_index ));
282 /* the following line sets the `error' variable through macros! */
283 if ( FT_STREAM_SEEK( offset ) || FT_FRAME_ENTER( byte_count ) )
286 loader->cursor = stream->cursor;
287 loader->limit = stream->limit;
293 FT_CALLBACK_DEF( void )
294 TT_Forget_Glyph_Frame( TT_Loader loader )
296 FT_Stream stream = loader->stream;
303 FT_CALLBACK_DEF( FT_Error )
304 TT_Load_Glyph_Header( TT_Loader loader )
306 FT_Byte* p = loader->cursor;
307 FT_Byte* limit = loader->limit;
310 if ( p + 10 > limit )
311 return TT_Err_Invalid_Outline;
313 loader->n_contours = FT_NEXT_SHORT( p );
315 loader->bbox.xMin = FT_NEXT_SHORT( p );
316 loader->bbox.yMin = FT_NEXT_SHORT( p );
317 loader->bbox.xMax = FT_NEXT_SHORT( p );
318 loader->bbox.yMax = FT_NEXT_SHORT( p );
320 FT_TRACE5(( " # of contours: %d\n", loader->n_contours ));
321 FT_TRACE5(( " xMin: %4d xMax: %4d\n", loader->bbox.xMin,
322 loader->bbox.xMax ));
323 FT_TRACE5(( " yMin: %4d yMax: %4d\n", loader->bbox.yMin,
324 loader->bbox.yMax ));
331 FT_CALLBACK_DEF( FT_Error )
332 TT_Load_Simple_Glyph( TT_Loader load )
335 FT_Byte* p = load->cursor;
336 FT_Byte* limit = load->limit;
337 FT_GlyphLoader gloader = load->gloader;
338 FT_Int n_contours = load->n_contours;
340 TT_Face face = (TT_Face)load->face;
344 FT_Byte *flag, *flag_limit;
346 FT_Vector *vec, *vec_limit;
348 FT_Short *cont, *cont_limit, prev_cont;
352 /* check that we can add the contours to the glyph */
353 error = FT_GLYPHLOADER_CHECK_POINTS( gloader, 0, n_contours );
357 /* reading the contours' endpoints & number of points */
358 cont = gloader->current.outline.contours;
359 cont_limit = cont + n_contours;
361 /* check space for contours array + instructions count */
362 if ( n_contours >= 0xFFF || p + ( n_contours + 1 ) * 2 > limit )
363 goto Invalid_Outline;
365 prev_cont = FT_NEXT_SHORT( p );
367 if ( n_contours > 0 )
371 goto Invalid_Outline;
373 for ( cont++; cont < cont_limit; cont++ )
375 cont[0] = FT_NEXT_SHORT( p );
376 if ( cont[0] <= prev_cont )
378 /* unordered contours: this is invalid */
379 goto Invalid_Outline;
385 if ( n_contours > 0 )
387 n_points = cont[-1] + 1;
389 goto Invalid_Outline;
392 /* note that we will add four phantom points later */
393 error = FT_GLYPHLOADER_CHECK_POINTS( gloader, n_points + 4, 0 );
397 /* reading the bytecode instructions */
398 load->glyph->control_len = 0;
399 load->glyph->control_data = 0;
402 goto Invalid_Outline;
404 n_ins = FT_NEXT_USHORT( p );
406 FT_TRACE5(( " Instructions size: %u\n", n_ins ));
408 if ( n_ins > face->max_profile.maxSizeOfInstructions )
410 FT_TRACE0(( "TT_Load_Simple_Glyph: too many instructions (%d)\n",
412 error = TT_Err_Too_Many_Hints;
416 if ( ( limit - p ) < n_ins )
418 FT_TRACE0(( "TT_Load_Simple_Glyph: instruction count mismatch\n" ));
419 error = TT_Err_Too_Many_Hints;
423 #ifdef TT_USE_BYTECODE_INTERPRETER
425 if ( IS_HINTED( load->load_flags ) )
427 load->glyph->control_len = n_ins;
428 load->glyph->control_data = load->exec->glyphIns;
430 FT_MEM_COPY( load->exec->glyphIns, p, (FT_Long)n_ins );
433 #endif /* TT_USE_BYTECODE_INTERPRETER */
437 outline = &gloader->current.outline;
439 /* reading the point tags */
440 flag = (FT_Byte*)outline->tags;
441 flag_limit = flag + n_points;
443 FT_ASSERT( flag != NULL );
445 while ( flag < flag_limit )
448 goto Invalid_Outline;
450 *flag++ = c = FT_NEXT_BYTE( p );
454 goto Invalid_Outline;
456 count = FT_NEXT_BYTE( p );
457 if ( flag + (FT_Int)count > flag_limit )
458 goto Invalid_Outline;
460 for ( ; count > 0; count-- )
465 /* reading the X coordinates */
467 vec = outline->points;
468 vec_limit = vec + n_points;
469 flag = (FT_Byte*)outline->tags;
472 if ( p + xy_size > limit )
473 goto Invalid_Outline;
475 for ( ; vec < vec_limit; vec++, flag++ )
484 goto Invalid_Outline;
486 y = (FT_Pos)FT_NEXT_BYTE( p );
487 if ( ( f & 16 ) == 0 )
490 else if ( ( f & 16 ) == 0 )
493 goto Invalid_Outline;
495 y = (FT_Pos)FT_NEXT_SHORT( p );
500 /* the cast is for stupid compilers */
501 *flag = (FT_Byte)( f & ~( 2 | 16 ) );
504 /* reading the Y coordinates */
506 vec = gloader->current.outline.points;
507 vec_limit = vec + n_points;
508 flag = (FT_Byte*)outline->tags;
511 for ( ; vec < vec_limit; vec++, flag++ )
520 goto Invalid_Outline;
522 y = (FT_Pos)FT_NEXT_BYTE( p );
523 if ( ( f & 32 ) == 0 )
526 else if ( ( f & 32 ) == 0 )
529 goto Invalid_Outline;
531 y = (FT_Pos)FT_NEXT_SHORT( p );
536 /* the cast is for stupid compilers */
537 *flag = (FT_Byte)( f & FT_CURVE_TAG_ON );
540 outline->n_points = (FT_UShort)n_points;
541 outline->n_contours = (FT_Short) n_contours;
549 error = TT_Err_Invalid_Outline;
554 FT_CALLBACK_DEF( FT_Error )
555 TT_Load_Composite_Glyph( TT_Loader loader )
558 FT_Byte* p = loader->cursor;
559 FT_Byte* limit = loader->limit;
560 FT_GlyphLoader gloader = loader->gloader;
561 FT_SubGlyph subglyph;
562 FT_UInt num_subglyphs;
569 FT_Fixed xx, xy, yy, yx;
573 /* check that we can load a new subglyph */
574 error = FT_GlyphLoader_CheckSubGlyphs( gloader, num_subglyphs + 1 );
580 goto Invalid_Composite;
582 subglyph = gloader->current.subglyphs + num_subglyphs;
584 subglyph->arg1 = subglyph->arg2 = 0;
586 subglyph->flags = FT_NEXT_USHORT( p );
587 subglyph->index = FT_NEXT_USHORT( p );
591 if ( subglyph->flags & ARGS_ARE_WORDS )
593 if ( subglyph->flags & WE_HAVE_A_SCALE )
595 else if ( subglyph->flags & WE_HAVE_AN_XY_SCALE )
597 else if ( subglyph->flags & WE_HAVE_A_2X2 )
600 if ( p + count > limit )
601 goto Invalid_Composite;
604 if ( subglyph->flags & ARGS_ARE_WORDS )
606 subglyph->arg1 = FT_NEXT_SHORT( p );
607 subglyph->arg2 = FT_NEXT_SHORT( p );
611 subglyph->arg1 = FT_NEXT_CHAR( p );
612 subglyph->arg2 = FT_NEXT_CHAR( p );
619 if ( subglyph->flags & WE_HAVE_A_SCALE )
621 xx = (FT_Fixed)FT_NEXT_SHORT( p ) << 2;
624 else if ( subglyph->flags & WE_HAVE_AN_XY_SCALE )
626 xx = (FT_Fixed)FT_NEXT_SHORT( p ) << 2;
627 yy = (FT_Fixed)FT_NEXT_SHORT( p ) << 2;
629 else if ( subglyph->flags & WE_HAVE_A_2X2 )
631 xx = (FT_Fixed)FT_NEXT_SHORT( p ) << 2;
632 yx = (FT_Fixed)FT_NEXT_SHORT( p ) << 2;
633 xy = (FT_Fixed)FT_NEXT_SHORT( p ) << 2;
634 yy = (FT_Fixed)FT_NEXT_SHORT( p ) << 2;
637 subglyph->transform.xx = xx;
638 subglyph->transform.xy = xy;
639 subglyph->transform.yx = yx;
640 subglyph->transform.yy = yy;
644 } while ( subglyph->flags & MORE_COMPONENTS );
646 gloader->current.num_subglyphs = num_subglyphs;
648 #ifdef TT_USE_BYTECODE_INTERPRETER
651 FT_Stream stream = loader->stream;
654 /* we must undo the FT_FRAME_ENTER in order to point */
655 /* to the composite instructions, if we find some. */
656 /* We will process them later. */
658 loader->ins_pos = (FT_ULong)( FT_STREAM_POS() +
670 error = TT_Err_Invalid_Composite;
676 TT_Init_Glyph_Loading( TT_Face face )
678 face->access_glyph_frame = TT_Access_Glyph_Frame;
679 face->read_glyph_header = TT_Load_Glyph_Header;
680 face->read_simple_glyph = TT_Load_Simple_Glyph;
681 face->read_composite_glyph = TT_Load_Composite_Glyph;
682 face->forget_glyph_frame = TT_Forget_Glyph_Frame;
687 tt_prepare_zone( TT_GlyphZone zone,
690 FT_UInt start_contour )
692 zone->n_points = (FT_UShort)( load->outline.n_points - start_point );
693 zone->n_contours = (FT_Short) ( load->outline.n_contours -
695 zone->org = load->extra_points + start_point;
696 zone->cur = load->outline.points + start_point;
697 zone->orus = load->extra_points2 + start_point;
698 zone->tags = (FT_Byte*)load->outline.tags + start_point;
699 zone->contours = (FT_UShort*)load->outline.contours + start_contour;
700 zone->first_point = (FT_UShort)start_point;
704 /*************************************************************************/
710 /* Hint the glyph using the zone prepared by the caller. Note that */
711 /* the zone is supposed to include four phantom points. */
714 TT_Hint_Glyph( TT_Loader loader,
715 FT_Bool is_composite )
717 TT_GlyphZone zone = &loader->zone;
720 #ifdef TT_USE_BYTECODE_INTERPRETER
723 FT_UNUSED( is_composite );
727 #ifdef TT_USE_BYTECODE_INTERPRETER
728 if ( loader->glyph->control_len > 0xFFFFL )
730 FT_TRACE1(( "TT_Hint_Glyph: too long instructions " ));
731 FT_TRACE1(( "(0x%lx byte) is truncated\n",
732 loader->glyph->control_len ));
734 n_ins = (FT_UInt)( loader->glyph->control_len );
737 origin = zone->cur[zone->n_points - 4].x;
738 origin = FT_PIX_ROUND( origin ) - origin;
740 translate_array( zone->n_points, zone->cur, origin, 0 );
742 #ifdef TT_USE_BYTECODE_INTERPRETER
743 /* save original point position in org */
745 FT_ARRAY_COPY( zone->org, zone->cur, zone->n_points );
747 /* Reset graphics state. */
748 loader->exec->GS = ((TT_Size)loader->size)->GS;
750 /* XXX: UNDOCUMENTED! Hinting instructions of a composite glyph */
751 /* completely refer to the (already) hinted subglyphs. */
754 loader->exec->metrics.x_scale = 1 << 16;
755 loader->exec->metrics.y_scale = 1 << 16;
757 FT_ARRAY_COPY( zone->orus, zone->cur, zone->n_points );
761 loader->exec->metrics.x_scale =
762 ((TT_Size)loader->size)->metrics.x_scale;
763 loader->exec->metrics.y_scale =
764 ((TT_Size)loader->size)->metrics.y_scale;
768 /* round pp2 and pp4 */
769 zone->cur[zone->n_points - 3].x =
770 FT_PIX_ROUND( zone->cur[zone->n_points - 3].x );
771 zone->cur[zone->n_points - 1].y =
772 FT_PIX_ROUND( zone->cur[zone->n_points - 1].y );
774 #ifdef TT_USE_BYTECODE_INTERPRETER
781 FT_GlyphLoader gloader = loader->gloader;
782 FT_Outline current_outline = gloader->current.outline;
785 error = TT_Set_CodeRange( loader->exec, tt_coderange_glyph,
786 loader->exec->glyphIns, n_ins );
790 loader->exec->is_composite = is_composite;
791 loader->exec->pts = *zone;
793 debug = FT_BOOL( !( loader->load_flags & FT_LOAD_NO_SCALE ) &&
794 ((TT_Size)loader->size)->debug );
796 error = TT_Run_Context( loader->exec, debug );
797 if ( error && loader->exec->pedantic_hinting )
800 /* store drop-out mode in bits 5-7; set bit 2 also as a marker */
801 current_outline.tags[0] |=
802 ( loader->exec->GS.scan_type << 5 ) | FT_CURVE_TAG_HAS_SCANMODE;
807 /* save glyph phantom points */
808 if ( !loader->preserve_pps )
810 loader->pp1 = zone->cur[zone->n_points - 4];
811 loader->pp2 = zone->cur[zone->n_points - 3];
812 loader->pp3 = zone->cur[zone->n_points - 2];
813 loader->pp4 = zone->cur[zone->n_points - 1];
820 /*************************************************************************/
823 /* TT_Process_Simple_Glyph */
826 /* Once a simple glyph has been loaded, it needs to be processed. */
827 /* Usually, this means scaling and hinting through bytecode */
828 /* interpretation. */
831 TT_Process_Simple_Glyph( TT_Loader loader )
833 FT_GlyphLoader gloader = loader->gloader;
834 FT_Error error = TT_Err_Ok;
839 outline = &gloader->current.outline;
840 n_points = outline->n_points;
842 /* set phantom points */
844 outline->points[n_points ] = loader->pp1;
845 outline->points[n_points + 1] = loader->pp2;
846 outline->points[n_points + 2] = loader->pp3;
847 outline->points[n_points + 3] = loader->pp4;
849 outline->tags[n_points ] = 0;
850 outline->tags[n_points + 1] = 0;
851 outline->tags[n_points + 2] = 0;
852 outline->tags[n_points + 3] = 0;
856 #ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
858 if ( ((TT_Face)loader->face)->doblend )
860 /* Deltas apply to the unscaled data. */
862 FT_Memory memory = loader->face->memory;
866 error = TT_Vary_Get_Glyph_Deltas( (TT_Face)(loader->face),
873 for ( i = 0; i < n_points; ++i )
875 outline->points[i].x += deltas[i].x;
876 outline->points[i].y += deltas[i].y;
882 #endif /* TT_CONFIG_OPTION_GX_VAR_SUPPORT */
884 if ( IS_HINTED( loader->load_flags ) )
886 tt_prepare_zone( &loader->zone, &gloader->current, 0, 0 );
888 FT_ARRAY_COPY( loader->zone.orus, loader->zone.cur,
889 loader->zone.n_points + 4 );
892 /* scale the glyph */
893 if ( ( loader->load_flags & FT_LOAD_NO_SCALE ) == 0 )
895 FT_Vector* vec = outline->points;
896 FT_Vector* limit = outline->points + n_points;
897 FT_Fixed x_scale = ((TT_Size)loader->size)->metrics.x_scale;
898 FT_Fixed y_scale = ((TT_Size)loader->size)->metrics.y_scale;
901 for ( ; vec < limit; vec++ )
903 vec->x = FT_MulFix( vec->x, x_scale );
904 vec->y = FT_MulFix( vec->y, y_scale );
907 loader->pp1 = outline->points[n_points - 4];
908 loader->pp2 = outline->points[n_points - 3];
909 loader->pp3 = outline->points[n_points - 2];
910 loader->pp4 = outline->points[n_points - 1];
913 if ( IS_HINTED( loader->load_flags ) )
915 loader->zone.n_points += 4;
917 error = TT_Hint_Glyph( loader, 0 );
924 /*************************************************************************/
927 /* TT_Process_Composite_Component */
930 /* Once a composite component has been loaded, it needs to be */
931 /* processed. Usually, this means transforming and translating. */
934 TT_Process_Composite_Component( TT_Loader loader,
935 FT_SubGlyph subglyph,
937 FT_UInt num_base_points )
939 FT_GlyphLoader gloader = loader->gloader;
940 FT_Vector* base_vec = gloader->base.outline.points;
941 FT_UInt num_points = gloader->base.outline.n_points;
946 have_scale = FT_BOOL( subglyph->flags & ( WE_HAVE_A_SCALE |
947 WE_HAVE_AN_XY_SCALE |
950 /* perform the transform required for this subglyph */
956 for ( i = num_base_points; i < num_points; i++ )
957 FT_Vector_Transform( base_vec + i, &subglyph->transform );
961 if ( !( subglyph->flags & ARGS_ARE_XY_VALUES ) )
963 FT_UInt k = subglyph->arg1;
964 FT_UInt l = subglyph->arg2;
969 /* match l-th point of the newly loaded component to the k-th point */
970 /* of the previously loaded components. */
972 /* change to the point numbers used by our outline */
974 l += num_base_points;
975 if ( k >= num_base_points ||
977 return TT_Err_Invalid_Composite;
979 p1 = gloader->base.outline.points + k;
980 p2 = gloader->base.outline.points + l;
993 /* Use a default value dependent on */
994 /* TT_CONFIG_OPTION_COMPONENT_OFFSET_SCALED. This is useful for old TT */
995 /* fonts which don't set the xxx_COMPONENT_OFFSET bit. */
998 #ifdef TT_CONFIG_OPTION_COMPONENT_OFFSET_SCALED
999 !( subglyph->flags & UNSCALED_COMPONENT_OFFSET ) )
1001 ( subglyph->flags & SCALED_COMPONENT_OFFSET ) )
1007 /*************************************************************************/
1009 /* This algorithm is what Apple documents. But it doesn't work. */
1011 int a = subglyph->transform.xx > 0 ? subglyph->transform.xx
1012 : -subglyph->transform.xx;
1013 int b = subglyph->transform.yx > 0 ? subglyph->transform.yx
1014 : -subglyph->transform.yx;
1015 int c = subglyph->transform.xy > 0 ? subglyph->transform.xy
1016 : -subglyph->transform.xy;
1017 int d = subglyph->transform.yy > 0 ? subglyph->transform.yy
1018 : -subglyph->transform.yy;
1019 int m = a > b ? a : b;
1020 int n = c > d ? c : d;
1023 if ( a - b <= 33 && a - b >= -33 )
1025 if ( c - d <= 33 && c - d >= -33 )
1027 x = FT_MulFix( x, m );
1028 y = FT_MulFix( y, n );
1032 /*************************************************************************/
1034 /* This algorithm is a guess and works much better than the above. */
1036 FT_Fixed mac_xscale = FT_SqrtFixed(
1037 (FT_Int32)FT_MulFix( subglyph->transform.xx,
1038 subglyph->transform.xx ) +
1039 (FT_Int32)FT_MulFix( subglyph->transform.xy,
1040 subglyph->transform.xy ) );
1041 FT_Fixed mac_yscale = FT_SqrtFixed(
1042 (FT_Int32)FT_MulFix( subglyph->transform.yy,
1043 subglyph->transform.yy ) +
1044 (FT_Int32)FT_MulFix( subglyph->transform.yx,
1045 subglyph->transform.yx ) );
1048 x = FT_MulFix( x, mac_xscale );
1049 y = FT_MulFix( y, mac_yscale );
1055 if ( !( loader->load_flags & FT_LOAD_NO_SCALE ) )
1057 FT_Fixed x_scale = ((TT_Size)loader->size)->metrics.x_scale;
1058 FT_Fixed y_scale = ((TT_Size)loader->size)->metrics.y_scale;
1061 x = FT_MulFix( x, x_scale );
1062 y = FT_MulFix( y, y_scale );
1064 if ( subglyph->flags & ROUND_XY_TO_GRID )
1066 x = FT_PIX_ROUND( x );
1067 y = FT_PIX_ROUND( y );
1073 translate_array( num_points - num_base_points,
1074 base_vec + num_base_points,
1081 /*************************************************************************/
1084 /* TT_Process_Composite_Glyph */
1087 /* This is slightly different from TT_Process_Simple_Glyph, in that */
1088 /* its sole purpose is to hint the glyph. Thus this function is */
1089 /* only available when bytecode interpreter is enabled. */
1092 TT_Process_Composite_Glyph( TT_Loader loader,
1093 FT_UInt start_point,
1094 FT_UInt start_contour )
1097 FT_Outline* outline;
1101 outline = &loader->gloader->base.outline;
1103 /* make room for phantom points */
1104 error = FT_GLYPHLOADER_CHECK_POINTS( loader->gloader,
1105 outline->n_points + 4,
1110 outline->points[outline->n_points ] = loader->pp1;
1111 outline->points[outline->n_points + 1] = loader->pp2;
1112 outline->points[outline->n_points + 2] = loader->pp3;
1113 outline->points[outline->n_points + 3] = loader->pp4;
1115 outline->tags[outline->n_points ] = 0;
1116 outline->tags[outline->n_points + 1] = 0;
1117 outline->tags[outline->n_points + 2] = 0;
1118 outline->tags[outline->n_points + 3] = 0;
1120 #ifdef TT_USE_BYTECODE_INTERPRETER
1123 FT_Stream stream = loader->stream;
1124 FT_UShort n_ins, max_ins;
1128 /* TT_Load_Composite_Glyph only gives us the offset of instructions */
1129 /* so we read them here */
1130 if ( FT_STREAM_SEEK( loader->ins_pos ) ||
1131 FT_READ_USHORT( n_ins ) )
1134 FT_TRACE5(( " Instructions size = %d\n", n_ins ));
1137 max_ins = ((TT_Face)loader->face)->max_profile.maxSizeOfInstructions;
1138 if ( n_ins > max_ins )
1140 /* acroread ignores this field, so we only do a rough safety check */
1141 if ( (FT_Int)n_ins > loader->byte_len )
1143 FT_TRACE1(( "TT_Process_Composite_Glyph: "
1144 "too many instructions (%d) for glyph with length %d\n",
1145 n_ins, loader->byte_len ));
1146 return TT_Err_Too_Many_Hints;
1149 tmp = loader->exec->glyphSize;
1150 error = Update_Max( loader->exec->memory,
1153 (void*)&loader->exec->glyphIns,
1155 loader->exec->glyphSize = (FT_UShort)tmp;
1159 else if ( n_ins == 0 )
1162 if ( FT_STREAM_READ( loader->exec->glyphIns, n_ins ) )
1165 loader->glyph->control_data = loader->exec->glyphIns;
1166 loader->glyph->control_len = n_ins;
1171 tt_prepare_zone( &loader->zone, &loader->gloader->base,
1172 start_point, start_contour );
1174 /* Some points are likely touched during execution of */
1175 /* instructions on components. So let's untouch them. */
1176 for ( i = start_point; i < loader->zone.n_points; i++ )
1177 loader->zone.tags[i] &= ~FT_CURVE_TAG_TOUCH_BOTH;
1179 loader->zone.n_points += 4;
1181 return TT_Hint_Glyph( loader, 1 );
1185 /* Calculate the four phantom points. */
1186 /* The first two stand for horizontal origin and advance. */
1187 /* The last two stand for vertical origin and advance. */
1188 #define TT_LOADER_SET_PP( loader ) \
1190 (loader)->pp1.x = (loader)->bbox.xMin - (loader)->left_bearing; \
1191 (loader)->pp1.y = 0; \
1192 (loader)->pp2.x = (loader)->pp1.x + (loader)->advance; \
1193 (loader)->pp2.y = 0; \
1194 (loader)->pp3.x = 0; \
1195 (loader)->pp3.y = (loader)->top_bearing + (loader)->bbox.yMax; \
1196 (loader)->pp4.x = 0; \
1197 (loader)->pp4.y = (loader)->pp3.y - (loader)->vadvance; \
1201 /*************************************************************************/
1204 /* load_truetype_glyph */
1207 /* Loads a given truetype glyph. Handles composites and uses a */
1208 /* TT_Loader object. */
1211 load_truetype_glyph( TT_Loader loader,
1212 FT_UInt glyph_index,
1213 FT_UInt recurse_count,
1214 FT_Bool header_only )
1216 FT_Error error = TT_Err_Ok;
1217 FT_Fixed x_scale, y_scale;
1219 TT_Face face = (TT_Face)loader->face;
1220 FT_GlyphLoader gloader = loader->gloader;
1221 FT_Bool opened_frame = 0;
1223 #ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
1224 FT_Vector* deltas = NULL;
1227 #ifdef FT_CONFIG_OPTION_INCREMENTAL
1228 FT_StreamRec inc_stream;
1230 FT_Bool glyph_data_loaded = 0;
1234 /* some fonts have an incorrect value of `maxComponentDepth', */
1235 /* thus we allow depth 1 to catch the majority of them */
1236 if ( recurse_count > 1 &&
1237 recurse_count > face->max_profile.maxComponentDepth )
1239 error = TT_Err_Invalid_Composite;
1243 /* check glyph index */
1244 if ( glyph_index >= (FT_UInt)face->root.num_glyphs )
1246 error = TT_Err_Invalid_Glyph_Index;
1250 loader->glyph_index = glyph_index;
1252 if ( ( loader->load_flags & FT_LOAD_NO_SCALE ) == 0 )
1254 x_scale = ((TT_Size)loader->size)->metrics.x_scale;
1255 y_scale = ((TT_Size)loader->size)->metrics.y_scale;
1263 tt_get_metrics( loader, glyph_index );
1265 /* Set `offset' to the start of the glyph relative to the start of */
1266 /* the `glyf' table, and `byte_len' to the length of the glyph in */
1269 #ifdef FT_CONFIG_OPTION_INCREMENTAL
1271 /* If we are loading glyph data via the incremental interface, set */
1272 /* the loader stream to a memory stream reading the data returned */
1273 /* by the interface. */
1274 if ( face->root.internal->incremental_interface )
1276 error = face->root.internal->incremental_interface->funcs->get_glyph_data(
1277 face->root.internal->incremental_interface->object,
1278 glyph_index, &glyph_data );
1282 glyph_data_loaded = 1;
1284 loader->byte_len = glyph_data.length;
1286 FT_MEM_ZERO( &inc_stream, sizeof ( inc_stream ) );
1287 FT_Stream_OpenMemory( &inc_stream,
1288 glyph_data.pointer, glyph_data.length );
1290 loader->stream = &inc_stream;
1294 #endif /* FT_CONFIG_OPTION_INCREMENTAL */
1296 offset = tt_face_get_location( face, glyph_index,
1297 (FT_UInt*)&loader->byte_len );
1299 if ( loader->byte_len > 0 )
1301 #ifdef FT_CONFIG_OPTION_INCREMENTAL
1302 /* for the incremental interface, `glyf_offset' is always zero */
1303 if ( !loader->glyf_offset &&
1304 !face->root.internal->incremental_interface )
1306 if ( !loader->glyf_offset )
1307 #endif /* FT_CONFIG_OPTION_INCREMENTAL */
1309 FT_TRACE2(( "no `glyf' table but non-zero `loca' entry\n" ));
1310 error = TT_Err_Invalid_Table;
1314 error = face->access_glyph_frame( loader, glyph_index,
1315 loader->glyf_offset + offset,
1322 /* read glyph header first */
1323 error = face->read_glyph_header( loader );
1324 if ( error || header_only )
1328 if ( loader->byte_len == 0 || loader->n_contours == 0 )
1330 loader->bbox.xMin = 0;
1331 loader->bbox.xMax = 0;
1332 loader->bbox.yMin = 0;
1333 loader->bbox.yMax = 0;
1338 /* must initialize points before (possibly) overriding */
1339 /* glyph metrics from the incremental interface */
1340 TT_LOADER_SET_PP( loader );
1342 #ifdef FT_CONFIG_OPTION_INCREMENTAL
1343 tt_get_metrics_incr_overrides( loader, glyph_index );
1346 #ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
1348 if ( ((TT_Face)(loader->face))->doblend )
1350 /* this must be done before scaling */
1351 FT_Memory memory = loader->face->memory;
1354 error = TT_Vary_Get_Glyph_Deltas( (TT_Face)(loader->face),
1355 glyph_index, &deltas, 4 );
1359 loader->pp1.x += deltas[0].x; loader->pp1.y += deltas[0].y;
1360 loader->pp2.x += deltas[1].x; loader->pp2.y += deltas[1].y;
1361 loader->pp3.x += deltas[2].x; loader->pp3.y += deltas[2].y;
1362 loader->pp4.x += deltas[3].x; loader->pp4.y += deltas[3].y;
1369 if ( ( loader->load_flags & FT_LOAD_NO_SCALE ) == 0 )
1371 loader->pp1.x = FT_MulFix( loader->pp1.x, x_scale );
1372 loader->pp2.x = FT_MulFix( loader->pp2.x, x_scale );
1373 loader->pp3.y = FT_MulFix( loader->pp3.y, y_scale );
1374 loader->pp4.y = FT_MulFix( loader->pp4.y, y_scale );
1381 /* must initialize points before (possibly) overriding */
1382 /* glyph metrics from the incremental interface */
1383 TT_LOADER_SET_PP( loader );
1385 #ifdef FT_CONFIG_OPTION_INCREMENTAL
1386 tt_get_metrics_incr_overrides( loader, glyph_index );
1389 /***********************************************************************/
1390 /***********************************************************************/
1391 /***********************************************************************/
1393 /* if it is a simple glyph, load it */
1395 if ( loader->n_contours > 0 )
1397 error = face->read_simple_glyph( loader );
1401 /* all data have been read */
1402 face->forget_glyph_frame( loader );
1405 error = TT_Process_Simple_Glyph( loader );
1409 FT_GlyphLoader_Add( gloader );
1412 /***********************************************************************/
1413 /***********************************************************************/
1414 /***********************************************************************/
1416 /* otherwise, load a composite! */
1417 else if ( loader->n_contours == -1 )
1419 FT_UInt start_point;
1420 FT_UInt start_contour;
1421 FT_ULong ins_pos; /* position of composite instructions, if any */
1424 start_point = gloader->base.outline.n_points;
1425 start_contour = gloader->base.outline.n_contours;
1427 /* for each subglyph, read composite header */
1428 error = face->read_composite_glyph( loader );
1432 /* store the offset of instructions */
1433 ins_pos = loader->ins_pos;
1435 /* all data we need are read */
1436 face->forget_glyph_frame( loader );
1439 #ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
1441 if ( face->doblend )
1444 FT_SubGlyph subglyph;
1445 FT_Memory memory = face->root.memory;
1448 /* this provides additional offsets */
1449 /* for each component's translation */
1451 if ( ( error = TT_Vary_Get_Glyph_Deltas(
1455 gloader->current.num_subglyphs + 4 )) != 0 )
1458 subglyph = gloader->current.subglyphs + gloader->base.num_subglyphs;
1459 limit = gloader->current.num_subglyphs;
1461 for ( i = 0; i < limit; ++i, ++subglyph )
1463 if ( subglyph->flags & ARGS_ARE_XY_VALUES )
1465 /* XXX: overflow check for subglyph->{arg1,arg2}. */
1466 /* deltas[i].{x,y} must be within signed 16-bit, */
1467 /* but the restriction of summed delta is not clear */
1468 subglyph->arg1 += (FT_Int16)deltas[i].x;
1469 subglyph->arg2 += (FT_Int16)deltas[i].y;
1473 loader->pp1.x += deltas[i + 0].x; loader->pp1.y += deltas[i + 0].y;
1474 loader->pp2.x += deltas[i + 1].x; loader->pp2.y += deltas[i + 1].y;
1475 loader->pp3.x += deltas[i + 2].x; loader->pp3.y += deltas[i + 2].y;
1476 loader->pp4.x += deltas[i + 3].x; loader->pp4.y += deltas[i + 3].y;
1481 #endif /* TT_CONFIG_OPTION_GX_VAR_SUPPORT */
1483 if ( ( loader->load_flags & FT_LOAD_NO_SCALE ) == 0 )
1485 loader->pp1.x = FT_MulFix( loader->pp1.x, x_scale );
1486 loader->pp2.x = FT_MulFix( loader->pp2.x, x_scale );
1487 loader->pp3.y = FT_MulFix( loader->pp3.y, y_scale );
1488 loader->pp4.y = FT_MulFix( loader->pp4.y, y_scale );
1491 /* if the flag FT_LOAD_NO_RECURSE is set, we return the subglyph */
1492 /* `as is' in the glyph slot (the client application will be */
1493 /* responsible for interpreting these data)... */
1494 if ( loader->load_flags & FT_LOAD_NO_RECURSE )
1496 FT_GlyphLoader_Add( gloader );
1497 loader->glyph->format = FT_GLYPH_FORMAT_COMPOSITE;
1502 /*********************************************************************/
1503 /*********************************************************************/
1504 /*********************************************************************/
1507 FT_UInt n, num_base_points;
1508 FT_SubGlyph subglyph = 0;
1510 FT_UInt num_points = start_point;
1511 FT_UInt num_subglyphs = gloader->current.num_subglyphs;
1512 FT_UInt num_base_subgs = gloader->base.num_subglyphs;
1514 FT_Stream old_stream = loader->stream;
1515 FT_Int old_byte_len = loader->byte_len;
1518 FT_GlyphLoader_Add( gloader );
1520 /* read each subglyph independently */
1521 for ( n = 0; n < num_subglyphs; n++ )
1526 /* Each time we call load_truetype_glyph in this loop, the */
1527 /* value of `gloader.base.subglyphs' can change due to table */
1528 /* reallocations. We thus need to recompute the subglyph */
1529 /* pointer on each iteration. */
1530 subglyph = gloader->base.subglyphs + num_base_subgs + n;
1532 pp[0] = loader->pp1;
1533 pp[1] = loader->pp2;
1534 pp[2] = loader->pp3;
1535 pp[3] = loader->pp4;
1537 num_base_points = gloader->base.outline.n_points;
1539 error = load_truetype_glyph( loader, subglyph->index,
1540 recurse_count + 1, FALSE );
1544 /* restore subglyph pointer */
1545 subglyph = gloader->base.subglyphs + num_base_subgs + n;
1547 if ( !( subglyph->flags & USE_MY_METRICS ) )
1549 loader->pp1 = pp[0];
1550 loader->pp2 = pp[1];
1551 loader->pp3 = pp[2];
1552 loader->pp4 = pp[3];
1555 num_points = gloader->base.outline.n_points;
1557 if ( num_points == num_base_points )
1560 /* gloader->base.outline consists of three parts: */
1561 /* 0 -(1)-> start_point -(2)-> num_base_points -(3)-> n_points. */
1563 /* (1): exists from the beginning */
1564 /* (2): components that have been loaded so far */
1565 /* (3): the newly loaded component */
1566 TT_Process_Composite_Component( loader, subglyph, start_point,
1570 loader->stream = old_stream;
1571 loader->byte_len = old_byte_len;
1573 /* process the glyph */
1574 loader->ins_pos = ins_pos;
1575 if ( IS_HINTED( loader->load_flags ) &&
1577 #ifdef TT_USE_BYTECODE_INTERPRETER
1579 subglyph->flags & WE_HAVE_INSTR &&
1583 num_points > start_point )
1584 TT_Process_Composite_Glyph( loader, start_point, start_contour );
1590 /* invalid composite count (negative but not -1) */
1591 error = TT_Err_Invalid_Outline;
1595 /***********************************************************************/
1596 /***********************************************************************/
1597 /***********************************************************************/
1602 face->forget_glyph_frame( loader );
1604 #ifdef FT_CONFIG_OPTION_INCREMENTAL
1606 if ( glyph_data_loaded )
1607 face->root.internal->incremental_interface->funcs->free_glyph_data(
1608 face->root.internal->incremental_interface->object,
1618 compute_glyph_metrics( TT_Loader loader,
1619 FT_UInt glyph_index )
1622 TT_Face face = (TT_Face)loader->face;
1624 TT_GlyphSlot glyph = loader->glyph;
1625 TT_Size size = (TT_Size)loader->size;
1629 if ( ( loader->load_flags & FT_LOAD_NO_SCALE ) == 0 )
1630 y_scale = size->root.metrics.y_scale;
1632 if ( glyph->format != FT_GLYPH_FORMAT_COMPOSITE )
1633 FT_Outline_Get_CBox( &glyph->outline, &bbox );
1635 bbox = loader->bbox;
1637 /* get the device-independent horizontal advance; it is scaled later */
1638 /* by the base layer. */
1639 glyph->linearHoriAdvance = loader->linear;
1641 glyph->metrics.horiBearingX = bbox.xMin;
1642 glyph->metrics.horiBearingY = bbox.yMax;
1643 glyph->metrics.horiAdvance = loader->pp2.x - loader->pp1.x;
1645 /* adjust advance width to the value contained in the hdmx table */
1646 if ( !face->postscript.isFixedPitch &&
1647 IS_HINTED( loader->load_flags ) )
1652 widthp = tt_face_get_device_metrics( face,
1653 size->root.metrics.x_ppem,
1657 glyph->metrics.horiAdvance = *widthp << 6;
1660 /* set glyph dimensions */
1661 glyph->metrics.width = bbox.xMax - bbox.xMin;
1662 glyph->metrics.height = bbox.yMax - bbox.yMin;
1664 /* Now take care of vertical metrics. In the case where there is */
1665 /* no vertical information within the font (relatively common), */
1666 /* create some metrics manually */
1668 FT_Pos top; /* scaled vertical top side bearing */
1669 FT_Pos advance; /* scaled vertical advance height */
1672 /* Get the unscaled top bearing and advance height. */
1673 if ( face->vertical_info &&
1674 face->vertical.number_Of_VMetrics > 0 )
1676 top = (FT_Short)FT_DivFix( loader->pp3.y - bbox.yMax,
1679 if ( loader->pp3.y <= loader->pp4.y )
1682 advance = (FT_UShort)FT_DivFix( loader->pp3.y - loader->pp4.y,
1690 /* XXX Compute top side bearing and advance height in */
1691 /* Get_VMetrics instead of here. */
1693 /* NOTE: The OS/2 values are the only `portable' ones, */
1694 /* which is why we use them, if there is an OS/2 */
1695 /* table in the font. Otherwise, we use the */
1696 /* values defined in the horizontal header. */
1698 height = (FT_Short)FT_DivFix( bbox.yMax - bbox.yMin,
1700 if ( face->os2.version != 0xFFFFU )
1701 advance = (FT_Pos)( face->os2.sTypoAscender -
1702 face->os2.sTypoDescender );
1704 advance = (FT_Pos)( face->horizontal.Ascender -
1705 face->horizontal.Descender );
1707 top = ( advance - height ) / 2;
1710 #ifdef FT_CONFIG_OPTION_INCREMENTAL
1712 FT_Incremental_InterfaceRec* incr;
1713 FT_Incremental_MetricsRec metrics;
1717 incr = face->root.internal->incremental_interface;
1719 /* If this is an incrementally loaded font see if there are */
1720 /* overriding metrics for this glyph. */
1721 if ( incr && incr->funcs->get_glyph_metrics )
1723 metrics.bearing_x = 0;
1724 metrics.bearing_y = top;
1725 metrics.advance = advance;
1727 error = incr->funcs->get_glyph_metrics( incr->object,
1734 top = metrics.bearing_y;
1735 advance = metrics.advance;
1739 /* GWW: Do vertical metrics get loaded incrementally too? */
1741 #endif /* FT_CONFIG_OPTION_INCREMENTAL */
1743 glyph->linearVertAdvance = advance;
1745 /* scale the metrics */
1746 if ( !( loader->load_flags & FT_LOAD_NO_SCALE ) )
1748 top = FT_MulFix( top, y_scale );
1749 advance = FT_MulFix( advance, y_scale );
1752 /* XXX: for now, we have no better algorithm for the lsb, but it */
1753 /* should work fine. */
1755 glyph->metrics.vertBearingX = glyph->metrics.horiBearingX -
1756 glyph->metrics.horiAdvance / 2;
1757 glyph->metrics.vertBearingY = top;
1758 glyph->metrics.vertAdvance = advance;
1765 #ifdef TT_CONFIG_OPTION_EMBEDDED_BITMAPS
1768 load_sbit_image( TT_Size size,
1770 FT_UInt glyph_index,
1771 FT_Int32 load_flags )
1777 TT_SBit_MetricsRec metrics;
1780 face = (TT_Face)glyph->face;
1781 sfnt = (SFNT_Service)face->sfnt;
1782 stream = face->root.stream;
1784 error = sfnt->load_sbit_image( face,
1793 glyph->outline.n_points = 0;
1794 glyph->outline.n_contours = 0;
1796 glyph->metrics.width = (FT_Pos)metrics.width << 6;
1797 glyph->metrics.height = (FT_Pos)metrics.height << 6;
1799 glyph->metrics.horiBearingX = (FT_Pos)metrics.horiBearingX << 6;
1800 glyph->metrics.horiBearingY = (FT_Pos)metrics.horiBearingY << 6;
1801 glyph->metrics.horiAdvance = (FT_Pos)metrics.horiAdvance << 6;
1803 glyph->metrics.vertBearingX = (FT_Pos)metrics.vertBearingX << 6;
1804 glyph->metrics.vertBearingY = (FT_Pos)metrics.vertBearingY << 6;
1805 glyph->metrics.vertAdvance = (FT_Pos)metrics.vertAdvance << 6;
1807 glyph->format = FT_GLYPH_FORMAT_BITMAP;
1809 if ( load_flags & FT_LOAD_VERTICAL_LAYOUT )
1811 glyph->bitmap_left = metrics.vertBearingX;
1812 glyph->bitmap_top = metrics.vertBearingY;
1816 glyph->bitmap_left = metrics.horiBearingX;
1817 glyph->bitmap_top = metrics.horiBearingY;
1824 #endif /* TT_CONFIG_OPTION_EMBEDDED_BITMAPS */
1828 tt_loader_init( TT_Loader loader,
1831 FT_Int32 load_flags,
1832 FT_Bool glyf_table_only )
1836 FT_Bool pedantic = FT_BOOL( load_flags & FT_LOAD_PEDANTIC );
1839 face = (TT_Face)glyph->face;
1840 stream = face->root.stream;
1842 FT_MEM_ZERO( loader, sizeof ( TT_LoaderRec ) );
1844 #ifdef TT_USE_BYTECODE_INTERPRETER
1846 /* load execution context */
1847 if ( IS_HINTED( load_flags ) && !glyf_table_only )
1849 TT_ExecContext exec;
1853 if ( !size->cvt_ready )
1855 FT_Error error = tt_size_ready_bytecode( size, pedantic );
1862 /* query new execution context */
1863 exec = size->debug ? size->context
1864 : ( (TT_Driver)FT_FACE_DRIVER( face ) )->context;
1866 return TT_Err_Could_Not_Find_Context;
1869 FT_BOOL( FT_LOAD_TARGET_MODE( load_flags ) != FT_RENDER_MODE_MONO );
1871 TT_Load_Context( exec, face, size );
1873 /* a change from mono to grayscale rendering (and vice versa) */
1874 /* requires a re-execution of the CVT program */
1875 if ( grayscale != exec->grayscale )
1880 FT_TRACE4(( "tt_loader_init: grayscale change,"
1881 " re-executing `prep' table\n" ));
1883 exec->grayscale = grayscale;
1885 for ( i = 0; i < size->cvt_size; i++ )
1886 size->cvt[i] = FT_MulFix( face->cvt[i], size->ttmetrics.scale );
1887 tt_size_run_prep( size, pedantic );
1890 /* see whether the cvt program has disabled hinting */
1891 if ( exec->GS.instruct_control & 1 )
1892 load_flags |= FT_LOAD_NO_HINTING;
1894 /* load default graphics state -- if needed */
1895 if ( exec->GS.instruct_control & 2 )
1896 exec->GS = tt_default_graphics_state;
1898 exec->pedantic_hinting = FT_BOOL( load_flags & FT_LOAD_PEDANTIC );
1899 loader->exec = exec;
1900 loader->instructions = exec->glyphIns;
1903 #endif /* TT_USE_BYTECODE_INTERPRETER */
1905 /* seek to the beginning of the glyph table -- for Type 42 fonts */
1906 /* the table might be accessed from a Postscript stream or something */
1909 #ifdef FT_CONFIG_OPTION_INCREMENTAL
1911 if ( face->root.internal->incremental_interface )
1912 loader->glyf_offset = 0;
1918 FT_Error error = face->goto_table( face, TTAG_glyf, stream, 0 );
1921 if ( error == TT_Err_Table_Missing )
1922 loader->glyf_offset = 0;
1925 FT_ERROR(( "tt_loader_init: could not access glyph table\n" ));
1929 loader->glyf_offset = FT_STREAM_POS();
1932 /* get face's glyph loader */
1933 if ( !glyf_table_only )
1935 FT_GlyphLoader gloader = glyph->internal->loader;
1938 FT_GlyphLoader_Rewind( gloader );
1939 loader->gloader = gloader;
1942 loader->load_flags = load_flags;
1944 loader->face = (FT_Face)face;
1945 loader->size = (FT_Size)size;
1946 loader->glyph = (FT_GlyphSlot)glyph;
1947 loader->stream = stream;
1953 /*************************************************************************/
1959 /* A function used to load a single glyph within a given glyph slot, */
1960 /* for a given size. */
1963 /* glyph :: A handle to a target slot object where the glyph */
1964 /* will be loaded. */
1966 /* size :: A handle to the source face size at which the glyph */
1967 /* must be scaled/loaded. */
1969 /* glyph_index :: The index of the glyph in the font file. */
1971 /* load_flags :: A flag indicating what to load for this glyph. The */
1972 /* FT_LOAD_XXX constants can be used to control the */
1973 /* glyph loading process (e.g., whether the outline */
1974 /* should be scaled, whether to load bitmaps or not, */
1975 /* whether to hint the outline, etc). */
1978 /* FreeType error code. 0 means success. */
1980 FT_LOCAL_DEF( FT_Error )
1981 TT_Load_Glyph( TT_Size size,
1983 FT_UInt glyph_index,
1984 FT_Int32 load_flags )
1988 TT_LoaderRec loader;
1991 face = (TT_Face)glyph->face;
1994 #ifdef TT_CONFIG_OPTION_EMBEDDED_BITMAPS
1996 /* try to load embedded bitmap if any */
1998 /* XXX: The convention should be emphasized in */
1999 /* the documents because it can be confusing. */
2000 if ( size->strike_index != 0xFFFFFFFFUL &&
2001 ( load_flags & FT_LOAD_NO_BITMAP ) == 0 )
2003 error = load_sbit_image( size, glyph, glyph_index, load_flags );
2006 FT_Face root = &face->root;
2009 if ( FT_IS_SCALABLE( root ) )
2011 /* for the bbox we need the header only */
2012 (void)tt_loader_init( &loader, size, glyph, load_flags, TRUE );
2013 (void)load_truetype_glyph( &loader, glyph_index, 0, TRUE );
2014 glyph->linearHoriAdvance = loader.linear;
2015 glyph->linearVertAdvance = loader.top_bearing + loader.bbox.yMax -
2023 #endif /* TT_CONFIG_OPTION_EMBEDDED_BITMAPS */
2025 /* if FT_LOAD_NO_SCALE is not set, `ttmetrics' must be valid */
2026 if ( !( load_flags & FT_LOAD_NO_SCALE ) && !size->ttmetrics.valid )
2027 return TT_Err_Invalid_Size_Handle;
2029 if ( load_flags & FT_LOAD_SBITS_ONLY )
2030 return TT_Err_Invalid_Argument;
2032 error = tt_loader_init( &loader, size, glyph, load_flags, FALSE );
2036 glyph->format = FT_GLYPH_FORMAT_OUTLINE;
2037 glyph->num_subglyphs = 0;
2038 glyph->outline.flags = 0;
2040 /* main loading loop */
2041 error = load_truetype_glyph( &loader, glyph_index, 0, FALSE );
2044 if ( glyph->format == FT_GLYPH_FORMAT_COMPOSITE )
2046 glyph->num_subglyphs = loader.gloader->base.num_subglyphs;
2047 glyph->subglyphs = loader.gloader->base.subglyphs;
2051 glyph->outline = loader.gloader->base.outline;
2052 glyph->outline.flags &= ~FT_OUTLINE_SINGLE_PASS;
2054 /* Translate array so that (0,0) is the glyph's origin. Note */
2055 /* that this behaviour is independent on the value of bit 1 of */
2056 /* the `flags' field in the `head' table -- at least major */
2057 /* applications like Acroread indicate that. */
2059 FT_Outline_Translate( &glyph->outline, -loader.pp1.x, 0 );
2062 #ifdef TT_USE_BYTECODE_INTERPRETER
2064 if ( IS_HINTED( load_flags ) )
2066 if ( loader.exec->GS.scan_control )
2068 /* convert scan conversion mode to FT_OUTLINE_XXX flags */
2069 switch ( loader.exec->GS.scan_type )
2071 case 0: /* simple drop-outs including stubs */
2072 glyph->outline.flags |= FT_OUTLINE_INCLUDE_STUBS;
2074 case 1: /* simple drop-outs excluding stubs */
2075 /* nothing; it's the default rendering mode */
2077 case 4: /* smart drop-outs including stubs */
2078 glyph->outline.flags |= FT_OUTLINE_SMART_DROPOUTS |
2079 FT_OUTLINE_INCLUDE_STUBS;
2081 case 5: /* smart drop-outs excluding stubs */
2082 glyph->outline.flags |= FT_OUTLINE_SMART_DROPOUTS;
2085 default: /* no drop-out control */
2086 glyph->outline.flags |= FT_OUTLINE_IGNORE_DROPOUTS;
2091 glyph->outline.flags |= FT_OUTLINE_IGNORE_DROPOUTS;
2094 #endif /* TT_USE_BYTECODE_INTERPRETER */
2096 compute_glyph_metrics( &loader, glyph_index );
2099 /* Set the `high precision' bit flag. */
2100 /* This is _critical_ to get correct output for monochrome */
2101 /* TrueType glyphs at all sizes using the bytecode interpreter. */
2103 if ( !( load_flags & FT_LOAD_NO_SCALE ) &&
2104 size->root.metrics.y_ppem < 24 )
2105 glyph->outline.flags |= FT_OUTLINE_HIGH_PRECISION;