1 /***************************************************************************/
5 /* TrueType Glyph Loader (body). */
7 /* Copyright 1996-2001, 2002 by */
8 /* David Turner, Robert Wilhelm, and Werner Lemberg. */
10 /* This file is part of the FreeType project, and may only be used, */
11 /* modified, and distributed under the terms of the FreeType project */
12 /* license, LICENSE.TXT. By continuing to use, modify, or distribute */
13 /* this file you indicate that you have read the license and */
14 /* understand and accept it fully. */
16 /***************************************************************************/
20 #include FT_INTERNAL_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
32 /*************************************************************************/
34 /* The macro FT_COMPONENT is used in trace mode. It is an implicit */
35 /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */
36 /* messages during execution. */
39 #define FT_COMPONENT trace_ttgload
42 /*************************************************************************/
44 /* Composite font flags. */
46 #define ARGS_ARE_WORDS 0x0001
47 #define ARGS_ARE_XY_VALUES 0x0002
48 #define ROUND_XY_TO_GRID 0x0004
49 #define WE_HAVE_A_SCALE 0x0008
51 #define MORE_COMPONENTS 0x0020
52 #define WE_HAVE_AN_XY_SCALE 0x0040
53 #define WE_HAVE_A_2X2 0x0080
54 #define WE_HAVE_INSTR 0x0100
55 #define USE_MY_METRICS 0x0200
56 #define OVERLAP_COMPOUND 0x0400
57 #define SCALED_COMPONENT_OFFSET 0x0800
58 #define UNSCALED_COMPONENT_OFFSET 0x1000
61 /* Maximum recursion depth we allow for composite glyphs.
62 * The TrueType spec doesn't say anything about recursion,
63 * so it isn't clear that recursion is allowed at all. But
66 #define TT_MAX_COMPOSITE_RECURSE 5
70 /*************************************************************************/
76 /* Returns the horizontal or vertical metrics in font units for a */
77 /* given glyph. The metrics are the left side bearing (resp. top */
78 /* side bearing) and advance width (resp. advance height). */
81 /* header :: A pointer to either the horizontal or vertical metrics */
84 /* idx :: The glyph index. */
87 /* bearing :: The bearing, either left side or top side. */
89 /* advance :: The advance width resp. advance height. */
92 /* This function will much probably move to another component in the */
93 /* near future, but I haven't decided which yet. */
96 TT_Get_Metrics( TT_HoriHeader* header,
101 TT_LongMetrics longs_m;
102 FT_UShort k = header->number_Of_HMetrics;
107 *bearing = *advance = 0;
111 if ( idx < (FT_UInt)k )
113 longs_m = (TT_LongMetrics )header->long_metrics + idx;
114 *bearing = longs_m->bearing;
115 *advance = longs_m->advance;
119 *bearing = ((TT_ShortMetrics*)header->short_metrics)[idx - k];
120 *advance = ((TT_LongMetrics )header->long_metrics)[k - 1].advance;
125 /*************************************************************************/
127 /* Returns the horizontal metrics in font units for a given glyph. If */
128 /* `check' is true, take care of monospaced fonts by returning the */
129 /* advance width maximum. */
132 Get_HMetrics( TT_Face face,
138 TT_Get_Metrics( &face->horizontal, idx, lsb, aw );
140 if ( check && face->postscript.isFixedPitch )
141 *aw = face->horizontal.advance_Width_Max;
145 /*************************************************************************/
147 /* Returns the advance width table for a given pixel size if it is found */
148 /* in the font's `hdmx' table (if any). */
151 Get_Advance_Widths( TT_Face face,
157 for ( n = 0; n < face->hdmx.num_records; n++ )
158 if ( face->hdmx.records[n].ppem == ppem )
159 return face->hdmx.records[n].widths;
165 #define cur_to_org( n, zone ) \
166 FT_MEM_COPY( (zone)->org, (zone)->cur, (n) * sizeof ( FT_Vector ) )
168 #define org_to_cur( n, zone ) \
169 FT_MEM_COPY( (zone)->cur, (zone)->org, (n) * sizeof ( FT_Vector ) )
172 /*************************************************************************/
174 /* Translates an array of coordinates. */
177 translate_array( FT_UInt n,
186 for ( k = 0; k < n; k++ )
187 coords[k].x += delta_x;
190 for ( k = 0; k < n; k++ )
191 coords[k].y += delta_y;
196 tt_prepare_zone( TT_GlyphZone zone,
199 FT_UInt start_contour )
201 zone->n_points = (FT_UShort)( load->outline.n_points - start_point );
202 zone->n_contours = (FT_Short) ( load->outline.n_contours - start_contour );
203 zone->org = load->extra_points + start_point;
204 zone->cur = load->outline.points + start_point;
205 zone->tags = (FT_Byte*)load->outline.tags + start_point;
206 zone->contours = (FT_UShort*)load->outline.contours + start_contour;
211 #define IS_HINTED( flags ) ( ( flags & FT_LOAD_NO_HINTING ) == 0 )
214 /*************************************************************************/
216 /* The following functions are used by default with TrueType fonts. */
217 /* However, they can be replaced by alternatives if we need to support */
218 /* TrueType-compressed formats (like MicroType) in the future. */
220 /*************************************************************************/
222 FT_CALLBACK_DEF( FT_Error )
223 TT_Access_Glyph_Frame( TT_Loader loader,
229 FT_Stream stream = loader->stream;
231 /* for non-debug mode */
232 FT_UNUSED( glyph_index );
235 FT_TRACE5(( "Glyph %ld\n", glyph_index ));
237 /* the following line sets the `error' variable through macros! */
238 if ( FT_STREAM_SEEK( offset ) || FT_FRAME_ENTER( byte_count ) )
245 FT_CALLBACK_DEF( void )
246 TT_Forget_Glyph_Frame( TT_Loader loader )
248 FT_Stream stream = loader->stream;
255 FT_CALLBACK_DEF( FT_Error )
256 TT_Load_Glyph_Header( TT_Loader loader )
258 FT_Stream stream = loader->stream;
259 FT_Int byte_len = loader->byte_len - 10;
263 return TT_Err_Invalid_Outline;
265 loader->n_contours = FT_GET_SHORT();
267 loader->bbox.xMin = FT_GET_SHORT();
268 loader->bbox.yMin = FT_GET_SHORT();
269 loader->bbox.xMax = FT_GET_SHORT();
270 loader->bbox.yMax = FT_GET_SHORT();
272 FT_TRACE5(( " # of contours: %d\n", loader->n_contours ));
273 FT_TRACE5(( " xMin: %4d xMax: %4d\n", loader->bbox.xMin,
274 loader->bbox.xMax ));
275 FT_TRACE5(( " yMin: %4d yMax: %4d\n", loader->bbox.yMin,
276 loader->bbox.yMax ));
277 loader->byte_len = byte_len;
283 FT_CALLBACK_DEF( FT_Error )
284 TT_Load_Simple_Glyph( TT_Loader load )
287 FT_Stream stream = load->stream;
288 FT_GlyphLoader gloader = load->gloader;
289 FT_Int n_contours = load->n_contours;
291 TT_Face face = (TT_Face)load->face;
292 TT_GlyphSlot slot = (TT_GlyphSlot)load->glyph;
295 FT_Int byte_len = load->byte_len;
298 /* reading the contours endpoints & number of points */
300 short* cur = gloader->current.outline.contours;
301 short* limit = cur + n_contours;
304 /* check space for contours array + instructions count */
305 byte_len -= 2 * ( n_contours + 1 );
307 goto Invalid_Outline;
309 for ( ; cur < limit; cur++ )
310 cur[0] = FT_GET_USHORT();
313 if ( n_contours > 0 )
314 n_points = cur[-1] + 1;
316 error = FT_GlyphLoader_CheckPoints( gloader, n_points + 2, 0 );
320 /* we'd better check the contours table right now */
321 outline = &gloader->current.outline;
323 for ( cur = outline->contours + 1; cur < limit; cur++ )
324 if ( cur[-1] >= cur[0] )
325 goto Invalid_Outline;
328 /* reading the bytecode instructions */
329 slot->control_len = 0;
330 slot->control_data = 0;
332 n_ins = FT_GET_USHORT();
334 FT_TRACE5(( " Instructions size: %d\n", n_ins ));
336 if ( n_ins > face->max_profile.maxSizeOfInstructions )
338 FT_TRACE0(( "TT_Load_Simple_Glyph: Too many instructions!\n" ));
339 error = TT_Err_Too_Many_Hints;
346 FT_TRACE0(( "TT_Load_Simple_Glyph: Instruction count mismatch!\n" ));
347 error = TT_Err_Too_Many_Hints;
351 #ifdef TT_CONFIG_OPTION_BYTECODE_INTERPRETER
353 if ( ( load->load_flags &
354 ( FT_LOAD_NO_SCALE | FT_LOAD_NO_HINTING ) ) == 0 &&
357 slot->control_len = n_ins;
358 slot->control_data = load->instructions;
360 FT_MEM_COPY( load->instructions, stream->cursor, n_ins );
363 #endif /* TT_CONFIG_OPTION_BYTECODE_INTERPRETER */
365 stream->cursor += n_ins;
367 /* reading the point tags */
369 FT_Byte* flag = (FT_Byte*)outline->tags;
370 FT_Byte* limit = flag + n_points;
374 while ( flag < limit )
376 if ( --byte_len < 0 )
377 goto Invalid_Outline;
379 *flag++ = c = FT_GET_BYTE();
382 if ( --byte_len < 0 )
383 goto Invalid_Outline;
385 count = FT_GET_BYTE();
386 if ( flag + count > limit )
387 goto Invalid_Outline;
389 for ( ; count > 0; count-- )
394 /* check that there is enough room to load the coordinates */
395 for ( flag = (FT_Byte*)outline->tags; flag < limit; flag++ )
399 else if ( ( *flag & 16 ) == 0 )
404 else if ( ( *flag & 32 ) == 0 )
409 goto Invalid_Outline;
412 /* reading the X coordinates */
415 FT_Vector* vec = outline->points;
416 FT_Vector* limit = vec + n_points;
417 FT_Byte* flag = (FT_Byte*)outline->tags;
421 for ( ; vec < limit; vec++, flag++ )
429 if ( ( *flag & 16 ) == 0 )
432 else if ( ( *flag & 16 ) == 0 )
440 /* reading the Y coordinates */
443 FT_Vector* vec = gloader->current.outline.points;
444 FT_Vector* limit = vec + n_points;
445 FT_Byte* flag = (FT_Byte*)outline->tags;
449 for ( ; vec < limit; vec++, flag++ )
457 if ( ( *flag & 32 ) == 0 )
460 else if ( ( *flag & 32 ) == 0 )
468 /* clear the touch tags */
469 for ( n = 0; n < n_points; n++ )
470 outline->tags[n] &= FT_CURVE_TAG_ON;
472 outline->n_points = (FT_UShort)n_points;
473 outline->n_contours = (FT_Short) n_contours;
475 load->byte_len = byte_len;
481 error = TT_Err_Invalid_Outline;
486 FT_CALLBACK_DEF( FT_Error )
487 TT_Load_Composite_Glyph( TT_Loader loader )
490 FT_Stream stream = loader->stream;
491 FT_GlyphLoader gloader = loader->gloader;
492 FT_SubGlyph subglyph;
493 FT_UInt num_subglyphs;
494 FT_Int byte_len = loader->byte_len;
501 FT_Fixed xx, xy, yy, yx;
504 /* check that we can load a new subglyph */
505 error = FT_GlyphLoader_CheckSubGlyphs( gloader, num_subglyphs + 1 );
512 goto Invalid_Composite;
514 subglyph = gloader->current.subglyphs + num_subglyphs;
516 subglyph->arg1 = subglyph->arg2 = 0;
518 subglyph->flags = FT_GET_USHORT();
519 subglyph->index = FT_GET_USHORT();
523 if ( subglyph->flags & ARGS_ARE_WORDS )
525 if ( subglyph->flags & WE_HAVE_A_SCALE )
527 else if ( subglyph->flags & WE_HAVE_AN_XY_SCALE )
529 else if ( subglyph->flags & WE_HAVE_A_2X2 )
533 goto Invalid_Composite;
536 if ( subglyph->flags & ARGS_ARE_WORDS )
538 subglyph->arg1 = FT_GET_SHORT();
539 subglyph->arg2 = FT_GET_SHORT();
543 subglyph->arg1 = FT_GET_CHAR();
544 subglyph->arg2 = FT_GET_CHAR();
551 if ( subglyph->flags & WE_HAVE_A_SCALE )
553 xx = (FT_Fixed)FT_GET_SHORT() << 2;
556 else if ( subglyph->flags & WE_HAVE_AN_XY_SCALE )
558 xx = (FT_Fixed)FT_GET_SHORT() << 2;
559 yy = (FT_Fixed)FT_GET_SHORT() << 2;
561 else if ( subglyph->flags & WE_HAVE_A_2X2 )
563 xx = (FT_Fixed)FT_GET_SHORT() << 2;
564 yx = (FT_Fixed)FT_GET_SHORT() << 2;
565 xy = (FT_Fixed)FT_GET_SHORT() << 2;
566 yy = (FT_Fixed)FT_GET_SHORT() << 2;
569 subglyph->transform.xx = xx;
570 subglyph->transform.xy = xy;
571 subglyph->transform.yx = yx;
572 subglyph->transform.yy = yy;
576 } while ( subglyph->flags & MORE_COMPONENTS );
578 gloader->current.num_subglyphs = num_subglyphs;
580 #ifdef TT_CONFIG_OPTION_BYTECODE_INTERPRETER
582 /* we must undo the FT_FRAME_ENTER in order to point to the */
583 /* composite instructions, if we find some. */
584 /* we will process them later... */
586 loader->ins_pos = (FT_ULong)( FT_STREAM_POS() +
587 stream->cursor - stream->limit );
591 loader->byte_len = byte_len;
597 error = TT_Err_Invalid_Composite;
603 TT_Init_Glyph_Loading( TT_Face face )
605 face->access_glyph_frame = TT_Access_Glyph_Frame;
606 face->read_glyph_header = TT_Load_Glyph_Header;
607 face->read_simple_glyph = TT_Load_Simple_Glyph;
608 face->read_composite_glyph = TT_Load_Composite_Glyph;
609 face->forget_glyph_frame = TT_Forget_Glyph_Frame;
613 /*************************************************************************/
616 /* TT_Process_Simple_Glyph */
619 /* Once a simple glyph has been loaded, it needs to be processed. */
620 /* Usually, this means scaling and hinting through bytecode */
621 /* interpretation. */
624 TT_Process_Simple_Glyph( TT_Loader load,
627 FT_GlyphLoader gloader = load->gloader;
628 FT_Outline* outline = &gloader->current.outline;
629 FT_UInt n_points = outline->n_points;
630 #ifdef TT_CONFIG_OPTION_BYTECODE_INTERPRETER
633 TT_GlyphZone zone = &load->zone;
634 FT_Error error = TT_Err_Ok;
636 FT_UNUSED( debug ); /* used by truetype interpreter only */
639 #ifdef TT_CONFIG_OPTION_BYTECODE_INTERPRETER
640 n_ins = load->glyph->control_len;
643 /* add shadow points */
645 /* Now add the two shadow points at n and n + 1. */
646 /* We need the left side bearing and advance width. */
653 /* pp1 = xMin - lsb */
654 pp1 = outline->points + n_points;
655 pp1->x = load->bbox.xMin - load->left_bearing;
660 pp2->x = pp1->x + load->advance;
663 outline->tags[n_points ] = 0;
664 outline->tags[n_points + 1] = 0;
667 /* Note that we return two more points that are not */
668 /* part of the glyph outline. */
672 /* set up zone for hinting */
673 tt_prepare_zone( zone, &gloader->current, 0, 0 );
675 /* eventually scale the glyph */
676 if ( !( load->load_flags & FT_LOAD_NO_SCALE ) )
678 FT_Vector* vec = zone->cur;
679 FT_Vector* limit = vec + n_points;
680 FT_Fixed x_scale = load->size->metrics.x_scale;
681 FT_Fixed y_scale = load->size->metrics.y_scale;
684 /* first scale the glyph points */
685 for ( ; vec < limit; vec++ )
687 vec->x = FT_MulFix( vec->x, x_scale );
688 vec->y = FT_MulFix( vec->y, y_scale );
692 cur_to_org( n_points, zone );
694 /* eventually hint the glyph */
695 if ( IS_HINTED( load->load_flags ) )
697 FT_Pos x = zone->org[n_points-2].x;
700 x = ( ( x + 32 ) & -64 ) - x;
701 translate_array( n_points, zone->org, x, 0 );
703 org_to_cur( n_points, zone );
705 zone->cur[n_points - 1].x = ( zone->cur[n_points - 1].x + 32 ) & -64;
707 #ifdef TT_CONFIG_OPTION_BYTECODE_INTERPRETER
709 /* now consider hinting */
712 error = TT_Set_CodeRange( load->exec, tt_coderange_glyph,
713 load->exec->glyphIns, n_ins );
717 load->exec->is_composite = FALSE;
718 load->exec->pedantic_hinting = (FT_Bool)( load->load_flags &
720 load->exec->pts = *zone;
721 load->exec->pts.n_points += 2;
723 error = TT_Run_Context( load->exec, debug );
724 if ( error && load->exec->pedantic_hinting )
727 error = TT_Err_Ok; /* ignore bytecode errors in non-pedantic mode */
730 #endif /* TT_CONFIG_OPTION_BYTECODE_INTERPRETER */
734 /* save glyph phantom points */
735 if ( !load->preserve_pps )
737 load->pp1 = zone->cur[n_points - 2];
738 load->pp2 = zone->cur[n_points - 1];
741 #ifdef TT_CONFIG_OPTION_BYTECODE_INTERPRETER
748 /*************************************************************************/
751 /* load_truetype_glyph */
754 /* Loads a given truetype glyph. Handles composites and uses a */
755 /* TT_Loader object. */
758 load_truetype_glyph( TT_Loader loader,
760 FT_UInt recurse_count )
763 #ifdef TT_CONFIG_OPTION_BYTECODE_INTERPRETER
764 FT_Stream stream = loader->stream;
768 TT_Face face = (TT_Face)loader->face;
770 FT_Int contours_count;
771 FT_UInt num_points, count;
772 FT_Fixed x_scale, y_scale;
773 FT_GlyphLoader gloader = loader->gloader;
774 FT_Bool opened_frame = 0;
776 #ifdef FT_CONFIG_OPTION_INCREMENTAL
777 struct FT_StreamRec_ inc_stream;
779 FT_Bool glyph_data_loaded = 0;
782 if ( recurse_count >= TT_MAX_COMPOSITE_RECURSE )
784 error = TT_Err_Invalid_Composite;
788 /* check glyph index */
789 if ( glyph_index >= (FT_UInt)face->root.num_glyphs )
791 error = TT_Err_Invalid_Glyph_Index;
795 loader->glyph_index = glyph_index;
800 if ( ( loader->load_flags & FT_LOAD_NO_SCALE ) == 0 )
802 x_scale = loader->size->metrics.x_scale;
803 y_scale = loader->size->metrics.y_scale;
806 /* get horizontal metrics */
808 FT_Short left_bearing = 0;
809 FT_UShort advance_width = 0;
811 Get_HMetrics( face, glyph_index,
812 (FT_Bool)!( loader->load_flags &
813 FT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH ),
817 #ifdef FT_CONFIG_OPTION_INCREMENTAL
819 /* If this is an incrementally loaded font see if there are */
820 /* overriding metrics for this glyph. */
821 if ( face->root.internal->incremental_interface &&
822 face->root.internal->incremental_interface->funcs->get_glyph_metrics )
824 FT_Incremental_MetricsRec metrics;
826 metrics.bearing_x = left_bearing;
827 metrics.bearing_y = 0;
828 metrics.advance = advance_width;
829 error = face->root.internal->incremental_interface->funcs->get_glyph_metrics(
830 face->root.internal->incremental_interface->object,
831 glyph_index, FALSE, &metrics );
834 left_bearing = (FT_Short)metrics.bearing_x;
835 advance_width = (FT_UShort)metrics.advance;
838 #endif /* FT_CONFIG_OPTION_INCREMENTAL */
840 loader->left_bearing = left_bearing;
841 loader->advance = advance_width;
843 if ( !loader->linear_def )
845 loader->linear_def = 1;
846 loader->linear = advance_width;
850 #ifdef FT_CONFIG_OPTION_INCREMENTAL
852 /* Set `offset' to the start of the glyph program relative to the */
853 /* start of the 'glyf' table, and `count' to the length of the */
854 /* glyph program in bytes. */
856 /* If we are loading glyph data via the incremental interface, set */
857 /* the loader stream to a memory stream reading the data returned */
858 /* by the interface. */
860 if ( face->root.internal->incremental_interface )
862 error = face->root.internal->incremental_interface->funcs->get_glyph_data(
863 face->root.internal->incremental_interface->object,
864 glyph_index, &glyph_data );
868 glyph_data_loaded = 1;
870 count = glyph_data.length;
872 FT_MEM_ZERO( &inc_stream, sizeof ( inc_stream ) );
873 FT_Stream_OpenMemory( &inc_stream,
874 glyph_data.pointer, glyph_data.length );
876 loader->stream = &inc_stream;
880 #endif /* FT_CONFIG_OPTION_INCREMENTAL */
883 offset = face->glyph_locations[glyph_index];
886 if ( glyph_index < (FT_UInt)face->num_locations - 1 )
887 count = face->glyph_locations[glyph_index + 1] - offset;
892 /* as described by Frederic Loyer, these are spaces, and */
893 /* not the unknown glyph. */
894 loader->bbox.xMin = 0;
895 loader->bbox.xMax = 0;
896 loader->bbox.yMin = 0;
897 loader->bbox.yMax = 0;
900 loader->pp2.x = loader->advance;
902 if ( ( loader->load_flags & FT_LOAD_NO_SCALE ) == 0 )
903 loader->pp2.x = FT_MulFix( loader->pp2.x, x_scale );
905 #ifdef TT_CONFIG_OPTION_BYTECODE_INTERPRETER
908 loader->exec->glyphSize = 0;
916 loader->byte_len = (FT_Int)count;
918 offset = loader->glyf_offset + offset;
920 /* access glyph frame */
921 error = face->access_glyph_frame( loader, glyph_index, offset, count );
927 /* read first glyph header */
928 error = face->read_glyph_header( loader );
932 contours_count = loader->n_contours;
936 loader->pp1.x = loader->bbox.xMin - loader->left_bearing;
938 loader->pp2.x = loader->pp1.x + loader->advance;
941 if ( ( loader->load_flags & FT_LOAD_NO_SCALE ) == 0 )
943 loader->pp1.x = FT_MulFix( loader->pp1.x, x_scale );
944 loader->pp2.x = FT_MulFix( loader->pp2.x, x_scale );
947 /***********************************************************************/
948 /***********************************************************************/
949 /***********************************************************************/
951 /* if it is a simple glyph, load it */
953 if ( contours_count >= 0 )
955 /* check that we can add the contours to the glyph */
956 error = FT_GlyphLoader_CheckPoints( gloader, 0, contours_count );
960 error = face->read_simple_glyph( loader );
964 #ifdef TT_CONFIG_OPTION_BYTECODE_INTERPRETER
967 TT_Size size = (TT_Size)loader->size;
970 error = TT_Process_Simple_Glyph( loader,
971 (FT_Bool)( size && size->debug ) );
976 error = TT_Process_Simple_Glyph( loader, 0 );
983 FT_GlyphLoader_Add( gloader );
985 /* Note: We could have put the simple loader source there */
986 /* but the code is fat enough already :-) */
989 /***********************************************************************/
990 /***********************************************************************/
991 /***********************************************************************/
993 /* otherwise, load a composite! */
994 else if ( contours_count == -1 )
996 TT_GlyphSlot glyph = (TT_GlyphSlot)loader->glyph;
998 #ifdef TT_CONFIG_OPTION_BYTECODE_INTERPRETER
999 FT_UInt start_contour;
1000 FT_ULong ins_pos; /* position of composite instructions, if any */
1004 /* for each subglyph, read composite header */
1005 start_point = gloader->base.outline.n_points;
1006 #ifdef TT_CONFIG_OPTION_BYTECODE_INTERPRETER
1007 start_contour = gloader->base.outline.n_contours;
1010 error = face->read_composite_glyph( loader );
1014 #ifdef TT_CONFIG_OPTION_BYTECODE_INTERPRETER
1015 ins_pos = loader->ins_pos;
1017 face->forget_glyph_frame( loader );
1020 /* if the flag FT_LOAD_NO_RECURSE is set, we return the subglyph */
1021 /* `as is' in the glyph slot (the client application will be */
1022 /* responsible for interpreting these data)... */
1024 if ( loader->load_flags & FT_LOAD_NO_RECURSE )
1026 /* set up remaining glyph fields */
1027 FT_GlyphLoader_Add( gloader );
1029 glyph->num_subglyphs = gloader->base.num_subglyphs;
1030 glyph->format = FT_GLYPH_FORMAT_COMPOSITE;
1031 glyph->subglyphs = gloader->base.subglyphs;
1036 /*********************************************************************/
1037 /*********************************************************************/
1038 /*********************************************************************/
1040 /* Now, read each subglyph independently. */
1042 FT_Int n, num_base_points, num_new_points;
1043 FT_SubGlyph subglyph = 0;
1045 FT_UInt num_subglyphs = gloader->current.num_subglyphs;
1046 FT_UInt num_base_subgs = gloader->base.num_subglyphs;
1049 FT_GlyphLoader_Add( gloader );
1051 for ( n = 0; n < (FT_Int)num_subglyphs; n++ )
1057 /* Each time we call load_truetype_glyph in this loop, the */
1058 /* value of `gloader.base.subglyphs' can change due to table */
1059 /* reallocations. We thus need to recompute the subglyph */
1060 /* pointer on each iteration. */
1061 subglyph = gloader->base.subglyphs + num_base_subgs + n;
1066 num_base_points = gloader->base.outline.n_points;
1068 error = load_truetype_glyph( loader, subglyph->index,
1073 /* restore subglyph pointer */
1074 subglyph = gloader->base.subglyphs + num_base_subgs + n;
1076 if ( subglyph->flags & USE_MY_METRICS )
1087 num_points = gloader->base.outline.n_points;
1089 num_new_points = num_points - num_base_points;
1091 /* now perform the transform required for this subglyph */
1093 if ( subglyph->flags & ( WE_HAVE_A_SCALE |
1094 WE_HAVE_AN_XY_SCALE |
1097 FT_Vector* cur = gloader->base.outline.points +
1099 FT_Vector* org = gloader->base.extra_points +
1101 FT_Vector* limit = cur + num_new_points;
1104 for ( ; cur < limit; cur++, org++ )
1106 FT_Vector_Transform( cur, &subglyph->transform );
1107 FT_Vector_Transform( org, &subglyph->transform );
1113 if ( !( subglyph->flags & ARGS_ARE_XY_VALUES ) )
1115 FT_UInt k = subglyph->arg1;
1116 FT_UInt l = subglyph->arg2;
1121 if ( start_point + k >= (FT_UInt)num_base_points ||
1122 l >= (FT_UInt)num_new_points )
1124 error = TT_Err_Invalid_Composite;
1128 l += num_base_points;
1130 p1 = gloader->base.outline.points + start_point + k;
1131 p2 = gloader->base.outline.points + start_point + l;
1141 /* Use a default value dependent on */
1142 /* TT_CONFIG_OPTION_COMPONENT_OFFSET_SCALED. This is useful for old TT */
1143 /* fonts which don't set the xxx_COMPONENT_OFFSET bit. */
1145 #ifdef TT_CONFIG_OPTION_COMPONENT_OFFSET_SCALED
1146 if ( !( subglyph->flags & UNSCALED_COMPONENT_OFFSET ) &&
1148 if ( ( subglyph->flags & SCALED_COMPONENT_OFFSET ) &&
1150 ( subglyph->flags & ( WE_HAVE_A_SCALE |
1151 WE_HAVE_AN_XY_SCALE |
1156 /*************************************************************************/
1158 /* This algorithm is what Apple documents. But it doesn't work. */
1160 int a = subglyph->transform.xx > 0 ? subglyph->transform.xx
1161 : -subglyph->transform.xx;
1162 int b = subglyph->transform.yx > 0 ? subglyph->transform.yx
1163 : -subglyph->transform.yx;
1164 int c = subglyph->transform.xy > 0 ? subglyph->transform.xy
1165 : -subglyph->transform.xy;
1166 int d = subglyph->transform.yy > 0 ? subglyph->transform.yy
1167 : -subglyph->transform.yy;
1168 int m = a > b ? a : b;
1169 int n = c > d ? c : d;
1172 if ( a - b <= 33 && a - b >= -33 )
1174 if ( c - d <= 33 && c - d >= -33 )
1176 x = FT_MulFix( x, m );
1177 y = FT_MulFix( y, n );
1181 /*************************************************************************/
1183 /* This algorithm is a guess and works much better than the above. */
1185 int mac_xscale = FT_SqrtFixed(
1186 FT_MulFix( subglyph->transform.xx,
1187 subglyph->transform.xx ) +
1188 FT_MulFix( subglyph->transform.xy,
1189 subglyph->transform.xy) );
1190 int mac_yscale = FT_SqrtFixed(
1191 FT_MulFix( subglyph->transform.yy,
1192 subglyph->transform.yy ) +
1193 FT_MulFix( subglyph->transform.yx,
1194 subglyph->transform.yx ) );
1197 x = FT_MulFix( x, mac_xscale );
1198 y = FT_MulFix( y, mac_yscale );
1203 if ( !( loader->load_flags & FT_LOAD_NO_SCALE ) )
1205 x = FT_MulFix( x, x_scale );
1206 y = FT_MulFix( y, y_scale );
1208 if ( subglyph->flags & ROUND_XY_TO_GRID )
1210 x = ( x + 32 ) & -64;
1211 y = ( y + 32 ) & -64;
1218 translate_array( num_new_points,
1219 gloader->base.outline.points + num_base_points,
1222 translate_array( num_new_points,
1223 gloader->base.extra_points + num_base_points,
1228 /*******************************************************************/
1229 /*******************************************************************/
1230 /*******************************************************************/
1232 /* we have finished loading all sub-glyphs; now, look for */
1233 /* instructions for this composite! */
1235 #ifdef TT_CONFIG_OPTION_BYTECODE_INTERPRETER
1237 if ( num_subglyphs > 0 &&
1240 subglyph->flags & WE_HAVE_INSTR )
1243 TT_ExecContext exec = loader->exec;
1248 /* read size of instructions */
1249 if ( FT_STREAM_SEEK( ins_pos ) ||
1250 FT_READ_USHORT( n_ins ) )
1252 FT_TRACE5(( " Instructions size = %d\n", n_ins ));
1254 /* in some fonts? */
1255 if ( n_ins == 0xFFFFU )
1259 if ( n_ins > face->max_profile.maxSizeOfInstructions )
1261 FT_TRACE0(( "Too many instructions (%d) in composite glyph %ld\n",
1262 n_ins, subglyph->index ));
1263 error = TT_Err_Too_Many_Hints;
1267 /* read the instructions */
1268 if ( FT_STREAM_READ( exec->glyphIns, n_ins ) )
1271 glyph->control_data = exec->glyphIns;
1272 glyph->control_len = n_ins;
1274 error = TT_Set_CodeRange( exec,
1281 /* prepare the execution context */
1282 tt_prepare_zone( &exec->pts, &gloader->base,
1283 start_point, start_contour );
1286 pts->n_points = (short)(num_points + 2);
1287 pts->n_contours = gloader->base.outline.n_contours;
1289 /* add phantom points */
1290 pp1 = pts->cur + num_points;
1291 pp1[0] = loader->pp1;
1292 pp1[1] = loader->pp2;
1294 pts->tags[num_points ] = 0;
1295 pts->tags[num_points + 1] = 0;
1297 /* if hinting, round the phantom points */
1298 if ( IS_HINTED( loader->load_flags ) )
1300 pp1[0].x = ( ( loader->pp1.x + 32 ) & -64 );
1301 pp1[1].x = ( ( loader->pp2.x + 32 ) & -64 );
1308 for ( k = 0; k < num_points; k++ )
1309 pts->tags[k] &= FT_CURVE_TAG_ON;
1312 cur_to_org( num_points + 2, pts );
1314 /* now consider hinting */
1315 if ( IS_HINTED( loader->load_flags ) && n_ins > 0 )
1317 exec->is_composite = TRUE;
1318 exec->pedantic_hinting =
1319 (FT_Bool)( loader->load_flags & FT_LOAD_PEDANTIC );
1320 error = TT_Run_Context( exec, ((TT_Size)loader->size)->debug );
1321 if ( error && exec->pedantic_hinting )
1325 /* save glyph origin and advance points */
1326 loader->pp1 = pp1[0];
1327 loader->pp2 = pp1[1];
1330 #endif /* TT_CONFIG_OPTION_BYTECODE_INTERPRETER */
1333 /* end of composite loading */
1337 /* invalid composite count ( negative but not -1 ) */
1338 error = TT_Err_Invalid_Outline;
1342 /***********************************************************************/
1343 /***********************************************************************/
1344 /***********************************************************************/
1348 face->forget_glyph_frame( loader );
1352 #ifdef FT_CONFIG_OPTION_INCREMENTAL
1353 if ( glyph_data_loaded )
1354 face->root.internal->incremental_interface->funcs->free_glyph_data(
1355 face->root.internal->incremental_interface->object,
1364 compute_glyph_metrics( TT_Loader loader,
1365 FT_UInt glyph_index )
1368 TT_Face face = (TT_Face)loader->face;
1370 TT_GlyphSlot glyph = loader->glyph;
1371 TT_Size size = (TT_Size)loader->size;
1375 if ( ( loader->load_flags & FT_LOAD_NO_SCALE ) == 0 )
1376 y_scale = size->root.metrics.y_scale;
1378 if ( glyph->format != FT_GLYPH_FORMAT_COMPOSITE )
1380 glyph->outline.flags &= ~FT_OUTLINE_SINGLE_PASS;
1382 /* copy outline to our glyph slot */
1383 FT_GlyphLoader_CopyPoints( glyph->internal->loader, loader->gloader );
1384 glyph->outline = glyph->internal->loader->base.outline;
1386 /* translate array so that (0,0) is the glyph's origin */
1387 FT_Outline_Translate( &glyph->outline, -loader->pp1.x, 0 );
1389 FT_Outline_Get_CBox( &glyph->outline, &bbox );
1391 if ( IS_HINTED( loader->load_flags ) )
1393 /* grid-fit the bounding box */
1396 bbox.xMax = ( bbox.xMax + 63 ) & -64;
1397 bbox.yMax = ( bbox.yMax + 63 ) & -64;
1401 bbox = loader->bbox;
1403 /* get the device-independent horizontal advance. It is scaled later */
1404 /* by the base layer. */
1406 FT_Pos advance = loader->linear;
1409 /* the flag FT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH was introduced to */
1410 /* correctly support DynaLab fonts, which have an incorrect */
1411 /* `advance_Width_Max' field! It is used, to my knowledge, */
1412 /* exclusively in the X-TrueType font server. */
1414 if ( face->postscript.isFixedPitch &&
1415 ( loader->load_flags & FT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH ) == 0 )
1416 advance = face->horizontal.advance_Width_Max;
1418 /* we need to return the advance in font units in linearHoriAdvance, */
1419 /* it will be scaled later by the base layer. */
1420 glyph->linearHoriAdvance = advance;
1423 glyph->metrics.horiBearingX = bbox.xMin;
1424 glyph->metrics.horiBearingY = bbox.yMax;
1425 glyph->metrics.horiAdvance = loader->pp2.x - loader->pp1.x;
1427 /* don't forget to hint the advance when we need to */
1428 if ( IS_HINTED( loader->load_flags ) )
1429 glyph->metrics.horiAdvance = ( glyph->metrics.horiAdvance + 32 ) & -64;
1431 /* Now take care of vertical metrics. In the case where there is */
1432 /* no vertical information within the font (relatively common), make */
1433 /* up some metrics by `hand'... */
1436 FT_Short top_bearing; /* vertical top side bearing (EM units) */
1437 FT_UShort advance_height; /* vertical advance height (EM units) */
1439 FT_Pos left; /* scaled vertical left side bearing */
1440 FT_Pos top; /* scaled vertical top side bearing */
1441 FT_Pos advance; /* scaled vertical advance height */
1443 /* Get the unscaled top bearing and advance height. */
1444 if ( face->vertical_info &&
1445 face->vertical.number_Of_VMetrics > 0 )
1447 /* Don't assume that both the vertical header and vertical */
1448 /* metrics are present in the same font :-) */
1450 TT_Get_Metrics( (TT_HoriHeader*)&face->vertical,
1457 /* Make up the distances from the horizontal header. */
1459 /* NOTE: The OS/2 values are the only `portable' ones, */
1460 /* which is why we use them, if there is an OS/2 */
1461 /* table in the font. Otherwise, we use the */
1462 /* values defined in the horizontal header. */
1464 /* NOTE2: The sTypoDescender is negative, which is why */
1465 /* we compute the baseline-to-baseline distance */
1467 /* ascender - descender + linegap */
1469 if ( face->os2.version != 0xFFFFU )
1471 top_bearing = (FT_Short)( face->os2.sTypoLineGap / 2 );
1472 advance_height = (FT_UShort)( face->os2.sTypoAscender -
1473 face->os2.sTypoDescender +
1474 face->os2.sTypoLineGap );
1478 top_bearing = (FT_Short)( face->horizontal.Line_Gap / 2 );
1479 advance_height = (FT_UShort)( face->horizontal.Ascender +
1480 face->horizontal.Descender +
1481 face->horizontal.Line_Gap );
1485 #ifdef FT_CONFIG_OPTION_INCREMENTAL
1487 /* If this is an incrementally loaded font see if there are */
1488 /* overriding metrics for this glyph. */
1489 if ( face->root.internal->incremental_interface &&
1490 face->root.internal->incremental_interface->funcs->get_glyph_metrics )
1492 FT_Incremental_MetricsRec metrics;
1495 metrics.bearing_x = 0;
1496 metrics.bearing_y = top_bearing;
1497 metrics.advance = advance_height;
1499 face->root.internal->incremental_interface->funcs->get_glyph_metrics(
1500 face->root.internal->incremental_interface->object,
1501 glyph_index, TRUE, &metrics );
1506 top_bearing = (FT_Short)metrics.bearing_y;
1507 advance_height = (FT_UShort)metrics.advance;
1510 #endif /* FT_CONFIG_OPTION_INCREMENTAL */
1512 /* We must adjust the top_bearing value from the bounding box given */
1513 /* in the glyph header to the bounding box calculated with */
1514 /* FT_Get_Outline_CBox(). */
1516 /* scale the metrics */
1517 if ( !( loader->load_flags & FT_LOAD_NO_SCALE ) )
1519 top = FT_MulFix( top_bearing + loader->bbox.yMax, y_scale )
1521 advance = FT_MulFix( advance_height, y_scale );
1525 top = top_bearing + loader->bbox.yMax - bbox.yMax;
1526 advance = advance_height;
1529 /* set the advance height in design units. It is scaled later by */
1530 /* the base layer. */
1531 glyph->linearVertAdvance = advance_height;
1533 /* XXX: for now, we have no better algorithm for the lsb, but it */
1534 /* should work fine. */
1536 left = ( bbox.xMin - bbox.xMax ) / 2;
1538 /* grid-fit them if necessary */
1539 if ( IS_HINTED( loader->load_flags ) )
1542 top = ( top + 63 ) & -64;
1543 advance = ( advance + 32 ) & -64;
1546 glyph->metrics.vertBearingX = left;
1547 glyph->metrics.vertBearingY = top;
1548 glyph->metrics.vertAdvance = advance;
1551 /* adjust advance width to the value contained in the hdmx table */
1552 if ( !face->postscript.isFixedPitch && size &&
1553 IS_HINTED( loader->load_flags ) )
1555 FT_Byte* widths = Get_Advance_Widths( face,
1556 size->root.metrics.x_ppem );
1560 glyph->metrics.horiAdvance = widths[glyph_index] << 6;
1563 /* set glyph dimensions */
1564 glyph->metrics.width = bbox.xMax - bbox.xMin;
1565 glyph->metrics.height = bbox.yMax - bbox.yMin;
1571 /*************************************************************************/
1577 /* A function used to load a single glyph within a given glyph slot, */
1578 /* for a given size. */
1581 /* glyph :: A handle to a target slot object where the glyph */
1582 /* will be loaded. */
1584 /* size :: A handle to the source face size at which the glyph */
1585 /* must be scaled/loaded. */
1587 /* glyph_index :: The index of the glyph in the font file. */
1589 /* load_flags :: A flag indicating what to load for this glyph. The */
1590 /* FT_LOAD_XXX constants can be used to control the */
1591 /* glyph loading process (e.g., whether the outline */
1592 /* should be scaled, whether to load bitmaps or not, */
1593 /* whether to hint the outline, etc). */
1596 /* FreeType error code. 0 means success. */
1598 FT_LOCAL_DEF( FT_Error )
1599 TT_Load_Glyph( TT_Size size,
1601 FT_UShort glyph_index,
1602 FT_Int32 load_flags )
1608 TT_LoaderRec loader;
1611 face = (TT_Face)glyph->face;
1612 sfnt = (SFNT_Service)face->sfnt;
1613 stream = face->root.stream;
1616 if ( !size || ( load_flags & FT_LOAD_NO_SCALE ) ||
1617 ( load_flags & FT_LOAD_NO_RECURSE ) )
1620 load_flags |= FT_LOAD_NO_SCALE |
1621 FT_LOAD_NO_HINTING |
1625 glyph->num_subglyphs = 0;
1627 #ifdef TT_CONFIG_OPTION_EMBEDDED_BITMAPS
1629 /* try to load embedded bitmap if any */
1631 /* XXX: The convention should be emphasized in */
1632 /* the documents because it can be confusing. */
1634 size->strike_index != 0xFFFFU &&
1636 ( load_flags & FT_LOAD_NO_BITMAP ) == 0 )
1639 TT_SBit_MetricsRec metrics;
1642 error = sfnt->load_sbit_image( face,
1651 glyph->outline.n_points = 0;
1652 glyph->outline.n_contours = 0;
1654 glyph->metrics.width = (FT_Pos)metrics.width << 6;
1655 glyph->metrics.height = (FT_Pos)metrics.height << 6;
1657 glyph->metrics.horiBearingX = (FT_Pos)metrics.horiBearingX << 6;
1658 glyph->metrics.horiBearingY = (FT_Pos)metrics.horiBearingY << 6;
1659 glyph->metrics.horiAdvance = (FT_Pos)metrics.horiAdvance << 6;
1661 glyph->metrics.vertBearingX = (FT_Pos)metrics.vertBearingX << 6;
1662 glyph->metrics.vertBearingY = (FT_Pos)metrics.vertBearingY << 6;
1663 glyph->metrics.vertAdvance = (FT_Pos)metrics.vertAdvance << 6;
1665 glyph->format = FT_GLYPH_FORMAT_BITMAP;
1666 if ( load_flags & FT_LOAD_VERTICAL_LAYOUT )
1668 glyph->bitmap_left = metrics.vertBearingX;
1669 glyph->bitmap_top = metrics.vertBearingY;
1673 glyph->bitmap_left = metrics.horiBearingX;
1674 glyph->bitmap_top = metrics.horiBearingY;
1680 #endif /* TT_CONFIG_OPTION_EMBEDDED_BITMAPS */
1682 /* return immediately if we only want the embedded bitmaps */
1683 if ( load_flags & FT_LOAD_SBITS_ONLY )
1684 return FT_Err_Invalid_Argument;
1686 /* seek to the beginning of the glyph table. For Type 42 fonts */
1687 /* the table might be accessed from a Postscript stream or something */
1690 #ifdef FT_CONFIG_OPTION_INCREMENTAL
1692 /* Don't look for the glyph table if this is an incremental font. */
1693 if ( !face->root.internal->incremental_interface )
1698 error = face->goto_table( face, TTAG_glyf, stream, 0 );
1701 FT_ERROR(( "TT_Load_Glyph: could not access glyph table\n" ));
1706 FT_MEM_ZERO( &loader, sizeof ( loader ) );
1708 /* update the glyph zone bounds */
1710 FT_GlyphLoader gloader = FT_FACE_DRIVER(face)->glyph_loader;
1713 loader.gloader = gloader;
1715 FT_GlyphLoader_Rewind( gloader );
1717 tt_prepare_zone( &loader.zone, &gloader->base, 0, 0 );
1718 tt_prepare_zone( &loader.base, &gloader->base, 0, 0 );
1721 #ifdef TT_CONFIG_OPTION_BYTECODE_INTERPRETER
1725 /* query new execution context */
1726 loader.exec = size->debug ? size->context : TT_New_Context( face );
1728 return TT_Err_Could_Not_Find_Context;
1730 TT_Load_Context( loader.exec, face, size );
1731 loader.instructions = loader.exec->glyphIns;
1733 /* load default graphics state - if needed */
1734 if ( size->GS.instruct_control & 2 )
1735 loader.exec->GS = tt_default_graphics_state;
1738 #endif /* TT_CONFIG_OPTION_BYTECODE_INTERPRETER */
1740 /* clear all outline flags, except the `owner' one */
1741 glyph->outline.flags = 0;
1743 /* let's initialize the rest of our loader now */
1745 loader.load_flags = load_flags;
1747 loader.face = (FT_Face)face;
1748 loader.size = (FT_Size)size;
1749 loader.glyph = (FT_GlyphSlot)glyph;
1750 loader.stream = stream;
1752 #ifdef FT_CONFIG_OPTION_INCREMENTAL
1754 if ( face->root.internal->incremental_interface )
1755 loader.glyf_offset = 0;
1760 loader.glyf_offset = FT_STREAM_POS();
1762 #ifdef TT_CONFIG_OPTION_BYTECODE_INTERPRETER
1764 /* if the cvt program has disabled hinting, the argument */
1766 if ( size && ( size->GS.instruct_control & 1 ) )
1767 loader.load_flags |= FT_LOAD_NO_HINTING;
1769 #endif /* TT_CONFIG_OPTION_BYTECODE_INTERPRETER */
1771 /* Main loading loop */
1772 glyph->format = FT_GLYPH_FORMAT_OUTLINE;
1773 glyph->num_subglyphs = 0;
1775 error = load_truetype_glyph( &loader, glyph_index, 0 );
1777 compute_glyph_metrics( &loader, glyph_index );
1779 #ifdef TT_CONFIG_OPTION_BYTECODE_INTERPRETER
1781 if ( !size || !size->debug )
1782 TT_Done_Context( loader.exec );
1784 #endif /* TT_CONFIG_OPTION_BYTECODE_INTERPRETER */
1786 /* Set the `high precision' bit flag. */
1787 /* This is _critical_ to get correct output for monochrome */
1788 /* TrueType glyphs at all sizes using the bytecode interpreter. */
1790 if ( size && size->root.metrics.y_ppem < 24 )
1791 glyph->outline.flags |= FT_OUTLINE_HIGH_PRECISION;