1
/***************************************************************************/
5
/* TrueType Glyph Loader (body). */
7
/* Copyright 1996-2011 */
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_USHORT( p );
367
if ( n_contours > 0 )
370
for ( cont++; cont < cont_limit; cont++ )
372
cont[0] = FT_NEXT_USHORT( p );
373
if ( cont[0] <= prev_cont )
375
/* unordered contours: this is invalid */
376
error = TT_Err_Invalid_Table;
383
if ( n_contours > 0 )
385
n_points = cont[-1] + 1;
387
goto Invalid_Outline;
390
/* note that we will add four phantom points later */
391
error = FT_GLYPHLOADER_CHECK_POINTS( gloader, n_points + 4, 0 );
395
/* we'd better check the contours table right now */
396
outline = &gloader->current.outline;
398
for ( cont = outline->contours + 1; cont < cont_limit; cont++ )
399
if ( cont[-1] >= cont[0] )
400
goto Invalid_Outline;
402
/* reading the bytecode instructions */
403
load->glyph->control_len = 0;
404
load->glyph->control_data = 0;
407
goto Invalid_Outline;
409
n_ins = FT_NEXT_USHORT( p );
411
FT_TRACE5(( " Instructions size: %u\n", n_ins ));
413
if ( n_ins > face->max_profile.maxSizeOfInstructions )
415
FT_TRACE0(( "TT_Load_Simple_Glyph: too many instructions (%d)\n",
417
error = TT_Err_Too_Many_Hints;
421
if ( ( limit - p ) < n_ins )
423
FT_TRACE0(( "TT_Load_Simple_Glyph: instruction count mismatch\n" ));
424
error = TT_Err_Too_Many_Hints;
428
#ifdef TT_USE_BYTECODE_INTERPRETER
430
if ( IS_HINTED( load->load_flags ) )
432
load->glyph->control_len = n_ins;
433
load->glyph->control_data = load->exec->glyphIns;
435
FT_MEM_COPY( load->exec->glyphIns, p, (FT_Long)n_ins );
438
#endif /* TT_USE_BYTECODE_INTERPRETER */
442
/* reading the point tags */
443
flag = (FT_Byte*)outline->tags;
444
flag_limit = flag + n_points;
446
FT_ASSERT( flag != NULL );
448
while ( flag < flag_limit )
451
goto Invalid_Outline;
453
*flag++ = c = FT_NEXT_BYTE( p );
457
goto Invalid_Outline;
459
count = FT_NEXT_BYTE( p );
460
if ( flag + (FT_Int)count > flag_limit )
461
goto Invalid_Outline;
463
for ( ; count > 0; count-- )
468
/* reading the X coordinates */
470
vec = outline->points;
471
vec_limit = vec + n_points;
472
flag = (FT_Byte*)outline->tags;
475
if ( p + xy_size > limit )
476
goto Invalid_Outline;
478
for ( ; vec < vec_limit; vec++, flag++ )
487
goto Invalid_Outline;
489
y = (FT_Pos)FT_NEXT_BYTE( p );
490
if ( ( f & 16 ) == 0 )
493
else if ( ( f & 16 ) == 0 )
496
goto Invalid_Outline;
498
y = (FT_Pos)FT_NEXT_SHORT( p );
503
/* the cast is for stupid compilers */
504
*flag = (FT_Byte)( f & ~( 2 | 16 ) );
507
/* reading the Y coordinates */
509
vec = gloader->current.outline.points;
510
vec_limit = vec + n_points;
511
flag = (FT_Byte*)outline->tags;
514
for ( ; vec < vec_limit; vec++, flag++ )
523
goto Invalid_Outline;
525
y = (FT_Pos)FT_NEXT_BYTE( p );
526
if ( ( f & 32 ) == 0 )
529
else if ( ( f & 32 ) == 0 )
532
goto Invalid_Outline;
534
y = (FT_Pos)FT_NEXT_SHORT( p );
539
/* the cast is for stupid compilers */
540
*flag = (FT_Byte)( f & FT_CURVE_TAG_ON );
543
outline->n_points = (FT_UShort)n_points;
544
outline->n_contours = (FT_Short) n_contours;
552
error = TT_Err_Invalid_Outline;
557
FT_CALLBACK_DEF( FT_Error )
558
TT_Load_Composite_Glyph( TT_Loader loader )
561
FT_Byte* p = loader->cursor;
562
FT_Byte* limit = loader->limit;
563
FT_GlyphLoader gloader = loader->gloader;
564
FT_SubGlyph subglyph;
565
FT_UInt num_subglyphs;
572
FT_Fixed xx, xy, yy, yx;
576
/* check that we can load a new subglyph */
577
error = FT_GlyphLoader_CheckSubGlyphs( gloader, num_subglyphs + 1 );
583
goto Invalid_Composite;
585
subglyph = gloader->current.subglyphs + num_subglyphs;
587
subglyph->arg1 = subglyph->arg2 = 0;
589
subglyph->flags = FT_NEXT_USHORT( p );
590
subglyph->index = FT_NEXT_USHORT( p );
594
if ( subglyph->flags & ARGS_ARE_WORDS )
596
if ( subglyph->flags & WE_HAVE_A_SCALE )
598
else if ( subglyph->flags & WE_HAVE_AN_XY_SCALE )
600
else if ( subglyph->flags & WE_HAVE_A_2X2 )
603
if ( p + count > limit )
604
goto Invalid_Composite;
607
if ( subglyph->flags & ARGS_ARE_WORDS )
609
subglyph->arg1 = FT_NEXT_SHORT( p );
610
subglyph->arg2 = FT_NEXT_SHORT( p );
614
subglyph->arg1 = FT_NEXT_CHAR( p );
615
subglyph->arg2 = FT_NEXT_CHAR( p );
622
if ( subglyph->flags & WE_HAVE_A_SCALE )
624
xx = (FT_Fixed)FT_NEXT_SHORT( p ) << 2;
627
else if ( subglyph->flags & WE_HAVE_AN_XY_SCALE )
629
xx = (FT_Fixed)FT_NEXT_SHORT( p ) << 2;
630
yy = (FT_Fixed)FT_NEXT_SHORT( p ) << 2;
632
else if ( subglyph->flags & WE_HAVE_A_2X2 )
634
xx = (FT_Fixed)FT_NEXT_SHORT( p ) << 2;
635
yx = (FT_Fixed)FT_NEXT_SHORT( p ) << 2;
636
xy = (FT_Fixed)FT_NEXT_SHORT( p ) << 2;
637
yy = (FT_Fixed)FT_NEXT_SHORT( p ) << 2;
640
subglyph->transform.xx = xx;
641
subglyph->transform.xy = xy;
642
subglyph->transform.yx = yx;
643
subglyph->transform.yy = yy;
647
} while ( subglyph->flags & MORE_COMPONENTS );
649
gloader->current.num_subglyphs = num_subglyphs;
651
#ifdef TT_USE_BYTECODE_INTERPRETER
654
FT_Stream stream = loader->stream;
657
/* we must undo the FT_FRAME_ENTER in order to point */
658
/* to the composite instructions, if we find some. */
659
/* We will process them later. */
661
loader->ins_pos = (FT_ULong)( FT_STREAM_POS() +
673
error = TT_Err_Invalid_Composite;
679
TT_Init_Glyph_Loading( TT_Face face )
681
face->access_glyph_frame = TT_Access_Glyph_Frame;
682
face->read_glyph_header = TT_Load_Glyph_Header;
683
face->read_simple_glyph = TT_Load_Simple_Glyph;
684
face->read_composite_glyph = TT_Load_Composite_Glyph;
685
face->forget_glyph_frame = TT_Forget_Glyph_Frame;
690
tt_prepare_zone( TT_GlyphZone zone,
693
FT_UInt start_contour )
695
zone->n_points = (FT_UShort)( load->outline.n_points - start_point );
696
zone->n_contours = (FT_Short) ( load->outline.n_contours -
698
zone->org = load->extra_points + start_point;
699
zone->cur = load->outline.points + start_point;
700
zone->orus = load->extra_points2 + start_point;
701
zone->tags = (FT_Byte*)load->outline.tags + start_point;
702
zone->contours = (FT_UShort*)load->outline.contours + start_contour;
703
zone->first_point = (FT_UShort)start_point;
707
/*************************************************************************/
713
/* Hint the glyph using the zone prepared by the caller. Note that */
714
/* the zone is supposed to include four phantom points. */
717
TT_Hint_Glyph( TT_Loader loader,
718
FT_Bool is_composite )
720
TT_GlyphZone zone = &loader->zone;
723
#ifdef TT_USE_BYTECODE_INTERPRETER
726
FT_UNUSED( is_composite );
730
#ifdef TT_USE_BYTECODE_INTERPRETER
731
if ( loader->glyph->control_len > 0xFFFFL )
733
FT_TRACE1(( "TT_Hint_Glyph: too long instructions " ));
734
FT_TRACE1(( "(0x%lx byte) is truncated\n",
735
loader->glyph->control_len ));
737
n_ins = (FT_UInt)( loader->glyph->control_len );
740
origin = zone->cur[zone->n_points - 4].x;
741
origin = FT_PIX_ROUND( origin ) - origin;
743
translate_array( zone->n_points, zone->cur, origin, 0 );
745
#ifdef TT_USE_BYTECODE_INTERPRETER
746
/* save original point position in org */
748
FT_ARRAY_COPY( zone->org, zone->cur, zone->n_points );
750
/* Reset graphics state. */
751
loader->exec->GS = ((TT_Size)loader->size)->GS;
753
/* XXX: UNDOCUMENTED! Hinting instructions of a composite glyph */
754
/* completely refer to the (already) hinted subglyphs. */
757
loader->exec->metrics.x_scale = 1 << 16;
758
loader->exec->metrics.y_scale = 1 << 16;
760
FT_ARRAY_COPY( zone->orus, zone->cur, zone->n_points );
764
loader->exec->metrics.x_scale =
765
((TT_Size)loader->size)->metrics.x_scale;
766
loader->exec->metrics.y_scale =
767
((TT_Size)loader->size)->metrics.y_scale;
771
/* round pp2 and pp4 */
772
zone->cur[zone->n_points - 3].x =
773
FT_PIX_ROUND( zone->cur[zone->n_points - 3].x );
774
zone->cur[zone->n_points - 1].y =
775
FT_PIX_ROUND( zone->cur[zone->n_points - 1].y );
777
#ifdef TT_USE_BYTECODE_INTERPRETER
784
FT_GlyphLoader gloader = loader->gloader;
785
FT_Outline current_outline = gloader->current.outline;
788
error = TT_Set_CodeRange( loader->exec, tt_coderange_glyph,
789
loader->exec->glyphIns, n_ins );
793
loader->exec->is_composite = is_composite;
794
loader->exec->pts = *zone;
796
debug = FT_BOOL( !( loader->load_flags & FT_LOAD_NO_SCALE ) &&
797
((TT_Size)loader->size)->debug );
799
error = TT_Run_Context( loader->exec, debug );
800
if ( error && loader->exec->pedantic_hinting )
803
/* store drop-out mode in bits 5-7; set bit 2 also as a marker */
804
current_outline.tags[0] |=
805
( loader->exec->GS.scan_type << 5 ) | FT_CURVE_TAG_HAS_SCANMODE;
810
/* save glyph phantom points */
811
if ( !loader->preserve_pps )
813
loader->pp1 = zone->cur[zone->n_points - 4];
814
loader->pp2 = zone->cur[zone->n_points - 3];
815
loader->pp3 = zone->cur[zone->n_points - 2];
816
loader->pp4 = zone->cur[zone->n_points - 1];
823
/*************************************************************************/
826
/* TT_Process_Simple_Glyph */
829
/* Once a simple glyph has been loaded, it needs to be processed. */
830
/* Usually, this means scaling and hinting through bytecode */
831
/* interpretation. */
834
TT_Process_Simple_Glyph( TT_Loader loader )
836
FT_GlyphLoader gloader = loader->gloader;
837
FT_Error error = TT_Err_Ok;
842
outline = &gloader->current.outline;
843
n_points = outline->n_points;
845
/* set phantom points */
847
outline->points[n_points ] = loader->pp1;
848
outline->points[n_points + 1] = loader->pp2;
849
outline->points[n_points + 2] = loader->pp3;
850
outline->points[n_points + 3] = loader->pp4;
852
outline->tags[n_points ] = 0;
853
outline->tags[n_points + 1] = 0;
854
outline->tags[n_points + 2] = 0;
855
outline->tags[n_points + 3] = 0;
859
#ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
861
if ( ((TT_Face)loader->face)->doblend )
863
/* Deltas apply to the unscaled data. */
865
FT_Memory memory = loader->face->memory;
869
error = TT_Vary_Get_Glyph_Deltas( (TT_Face)(loader->face),
876
for ( i = 0; i < n_points; ++i )
878
outline->points[i].x += deltas[i].x;
879
outline->points[i].y += deltas[i].y;
885
#endif /* TT_CONFIG_OPTION_GX_VAR_SUPPORT */
887
if ( IS_HINTED( loader->load_flags ) )
889
tt_prepare_zone( &loader->zone, &gloader->current, 0, 0 );
891
FT_ARRAY_COPY( loader->zone.orus, loader->zone.cur,
892
loader->zone.n_points + 4 );
895
/* scale the glyph */
896
if ( ( loader->load_flags & FT_LOAD_NO_SCALE ) == 0 )
898
FT_Vector* vec = outline->points;
899
FT_Vector* limit = outline->points + n_points;
900
FT_Fixed x_scale = ((TT_Size)loader->size)->metrics.x_scale;
901
FT_Fixed y_scale = ((TT_Size)loader->size)->metrics.y_scale;
904
for ( ; vec < limit; vec++ )
906
vec->x = FT_MulFix( vec->x, x_scale );
907
vec->y = FT_MulFix( vec->y, y_scale );
910
loader->pp1 = outline->points[n_points - 4];
911
loader->pp2 = outline->points[n_points - 3];
912
loader->pp3 = outline->points[n_points - 2];
913
loader->pp4 = outline->points[n_points - 1];
916
if ( IS_HINTED( loader->load_flags ) )
918
loader->zone.n_points += 4;
920
error = TT_Hint_Glyph( loader, 0 );
927
/*************************************************************************/
930
/* TT_Process_Composite_Component */
933
/* Once a composite component has been loaded, it needs to be */
934
/* processed. Usually, this means transforming and translating. */
937
TT_Process_Composite_Component( TT_Loader loader,
938
FT_SubGlyph subglyph,
940
FT_UInt num_base_points )
942
FT_GlyphLoader gloader = loader->gloader;
943
FT_Vector* base_vec = gloader->base.outline.points;
944
FT_UInt num_points = gloader->base.outline.n_points;
949
have_scale = FT_BOOL( subglyph->flags & ( WE_HAVE_A_SCALE |
950
WE_HAVE_AN_XY_SCALE |
953
/* perform the transform required for this subglyph */
959
for ( i = num_base_points; i < num_points; i++ )
960
FT_Vector_Transform( base_vec + i, &subglyph->transform );
964
if ( !( subglyph->flags & ARGS_ARE_XY_VALUES ) )
966
FT_UInt k = subglyph->arg1;
967
FT_UInt l = subglyph->arg2;
972
/* match l-th point of the newly loaded component to the k-th point */
973
/* of the previously loaded components. */
975
/* change to the point numbers used by our outline */
977
l += num_base_points;
978
if ( k >= num_base_points ||
980
return TT_Err_Invalid_Composite;
982
p1 = gloader->base.outline.points + k;
983
p2 = gloader->base.outline.points + l;
996
/* Use a default value dependent on */
997
/* TT_CONFIG_OPTION_COMPONENT_OFFSET_SCALED. This is useful for old TT */
998
/* fonts which don't set the xxx_COMPONENT_OFFSET bit. */
1001
#ifdef TT_CONFIG_OPTION_COMPONENT_OFFSET_SCALED
1002
!( subglyph->flags & UNSCALED_COMPONENT_OFFSET ) )
1004
( subglyph->flags & SCALED_COMPONENT_OFFSET ) )
1010
/*************************************************************************/
1012
/* This algorithm is what Apple documents. But it doesn't work. */
1014
int a = subglyph->transform.xx > 0 ? subglyph->transform.xx
1015
: -subglyph->transform.xx;
1016
int b = subglyph->transform.yx > 0 ? subglyph->transform.yx
1017
: -subglyph->transform.yx;
1018
int c = subglyph->transform.xy > 0 ? subglyph->transform.xy
1019
: -subglyph->transform.xy;
1020
int d = subglyph->transform.yy > 0 ? subglyph->transform.yy
1021
: -subglyph->transform.yy;
1022
int m = a > b ? a : b;
1023
int n = c > d ? c : d;
1026
if ( a - b <= 33 && a - b >= -33 )
1028
if ( c - d <= 33 && c - d >= -33 )
1030
x = FT_MulFix( x, m );
1031
y = FT_MulFix( y, n );
1035
/*************************************************************************/
1037
/* This algorithm is a guess and works much better than the above. */
1039
FT_Fixed mac_xscale = FT_SqrtFixed(
1040
(FT_Int32)FT_MulFix( subglyph->transform.xx,
1041
subglyph->transform.xx ) +
1042
(FT_Int32)FT_MulFix( subglyph->transform.xy,
1043
subglyph->transform.xy ) );
1044
FT_Fixed mac_yscale = FT_SqrtFixed(
1045
(FT_Int32)FT_MulFix( subglyph->transform.yy,
1046
subglyph->transform.yy ) +
1047
(FT_Int32)FT_MulFix( subglyph->transform.yx,
1048
subglyph->transform.yx ) );
1051
x = FT_MulFix( x, mac_xscale );
1052
y = FT_MulFix( y, mac_yscale );
1058
if ( !( loader->load_flags & FT_LOAD_NO_SCALE ) )
1060
FT_Fixed x_scale = ((TT_Size)loader->size)->metrics.x_scale;
1061
FT_Fixed y_scale = ((TT_Size)loader->size)->metrics.y_scale;
1064
x = FT_MulFix( x, x_scale );
1065
y = FT_MulFix( y, y_scale );
1067
if ( subglyph->flags & ROUND_XY_TO_GRID )
1069
x = FT_PIX_ROUND( x );
1070
y = FT_PIX_ROUND( y );
1076
translate_array( num_points - num_base_points,
1077
base_vec + num_base_points,
1084
/*************************************************************************/
1087
/* TT_Process_Composite_Glyph */
1090
/* This is slightly different from TT_Process_Simple_Glyph, in that */
1091
/* its sole purpose is to hint the glyph. Thus this function is */
1092
/* only available when bytecode interpreter is enabled. */
1095
TT_Process_Composite_Glyph( TT_Loader loader,
1096
FT_UInt start_point,
1097
FT_UInt start_contour )
1100
FT_Outline* outline;
1104
outline = &loader->gloader->base.outline;
1106
/* make room for phantom points */
1107
error = FT_GLYPHLOADER_CHECK_POINTS( loader->gloader,
1108
outline->n_points + 4,
1113
outline->points[outline->n_points ] = loader->pp1;
1114
outline->points[outline->n_points + 1] = loader->pp2;
1115
outline->points[outline->n_points + 2] = loader->pp3;
1116
outline->points[outline->n_points + 3] = loader->pp4;
1118
outline->tags[outline->n_points ] = 0;
1119
outline->tags[outline->n_points + 1] = 0;
1120
outline->tags[outline->n_points + 2] = 0;
1121
outline->tags[outline->n_points + 3] = 0;
1123
#ifdef TT_USE_BYTECODE_INTERPRETER
1126
FT_Stream stream = loader->stream;
1127
FT_UShort n_ins, max_ins;
1131
/* TT_Load_Composite_Glyph only gives us the offset of instructions */
1132
/* so we read them here */
1133
if ( FT_STREAM_SEEK( loader->ins_pos ) ||
1134
FT_READ_USHORT( n_ins ) )
1137
FT_TRACE5(( " Instructions size = %d\n", n_ins ));
1140
max_ins = ((TT_Face)loader->face)->max_profile.maxSizeOfInstructions;
1141
if ( n_ins > max_ins )
1143
/* acroread ignores this field, so we only do a rough safety check */
1144
if ( (FT_Int)n_ins > loader->byte_len )
1146
FT_TRACE1(( "TT_Process_Composite_Glyph: "
1147
"too many instructions (%d) for glyph with length %d\n",
1148
n_ins, loader->byte_len ));
1149
return TT_Err_Too_Many_Hints;
1152
tmp = loader->exec->glyphSize;
1153
error = Update_Max( loader->exec->memory,
1156
(void*)&loader->exec->glyphIns,
1158
loader->exec->glyphSize = (FT_UShort)tmp;
1162
else if ( n_ins == 0 )
1165
if ( FT_STREAM_READ( loader->exec->glyphIns, n_ins ) )
1168
loader->glyph->control_data = loader->exec->glyphIns;
1169
loader->glyph->control_len = n_ins;
1174
tt_prepare_zone( &loader->zone, &loader->gloader->base,
1175
start_point, start_contour );
1177
/* Some points are likely touched during execution of */
1178
/* instructions on components. So let's untouch them. */
1179
for ( i = start_point; i < loader->zone.n_points; i++ )
1180
loader->zone.tags[i] &= ~FT_CURVE_TAG_TOUCH_BOTH;
1182
loader->zone.n_points += 4;
1184
return TT_Hint_Glyph( loader, 1 );
1188
/* Calculate the four phantom points. */
1189
/* The first two stand for horizontal origin and advance. */
1190
/* The last two stand for vertical origin and advance. */
1191
#define TT_LOADER_SET_PP( loader ) \
1193
(loader)->pp1.x = (loader)->bbox.xMin - (loader)->left_bearing; \
1194
(loader)->pp1.y = 0; \
1195
(loader)->pp2.x = (loader)->pp1.x + (loader)->advance; \
1196
(loader)->pp2.y = 0; \
1197
(loader)->pp3.x = 0; \
1198
(loader)->pp3.y = (loader)->top_bearing + (loader)->bbox.yMax; \
1199
(loader)->pp4.x = 0; \
1200
(loader)->pp4.y = (loader)->pp3.y - (loader)->vadvance; \
1204
/*************************************************************************/
1207
/* load_truetype_glyph */
1210
/* Loads a given truetype glyph. Handles composites and uses a */
1211
/* TT_Loader object. */
1214
load_truetype_glyph( TT_Loader loader,
1215
FT_UInt glyph_index,
1216
FT_UInt recurse_count,
1217
FT_Bool header_only )
1219
FT_Error error = TT_Err_Ok;
1220
FT_Fixed x_scale, y_scale;
1222
TT_Face face = (TT_Face)loader->face;
1223
FT_GlyphLoader gloader = loader->gloader;
1224
FT_Bool opened_frame = 0;
1226
#ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
1227
FT_Vector* deltas = NULL;
1230
#ifdef FT_CONFIG_OPTION_INCREMENTAL
1231
FT_StreamRec inc_stream;
1233
FT_Bool glyph_data_loaded = 0;
1237
/* some fonts have an incorrect value of `maxComponentDepth', */
1238
/* thus we allow depth 1 to catch the majority of them */
1239
if ( recurse_count > 1 &&
1240
recurse_count > face->max_profile.maxComponentDepth )
1242
error = TT_Err_Invalid_Composite;
1246
/* check glyph index */
1247
if ( glyph_index >= (FT_UInt)face->root.num_glyphs )
1249
error = TT_Err_Invalid_Glyph_Index;
1253
loader->glyph_index = glyph_index;
1255
if ( ( loader->load_flags & FT_LOAD_NO_SCALE ) == 0 )
1257
x_scale = ((TT_Size)loader->size)->metrics.x_scale;
1258
y_scale = ((TT_Size)loader->size)->metrics.y_scale;
1266
tt_get_metrics( loader, glyph_index );
1268
/* Set `offset' to the start of the glyph relative to the start of */
1269
/* the `glyf' table, and `byte_len' to the length of the glyph in */
1272
#ifdef FT_CONFIG_OPTION_INCREMENTAL
1274
/* If we are loading glyph data via the incremental interface, set */
1275
/* the loader stream to a memory stream reading the data returned */
1276
/* by the interface. */
1277
if ( face->root.internal->incremental_interface )
1279
error = face->root.internal->incremental_interface->funcs->get_glyph_data(
1280
face->root.internal->incremental_interface->object,
1281
glyph_index, &glyph_data );
1285
glyph_data_loaded = 1;
1287
loader->byte_len = glyph_data.length;
1289
FT_MEM_ZERO( &inc_stream, sizeof ( inc_stream ) );
1290
FT_Stream_OpenMemory( &inc_stream,
1291
glyph_data.pointer, glyph_data.length );
1293
loader->stream = &inc_stream;
1297
#endif /* FT_CONFIG_OPTION_INCREMENTAL */
1299
offset = tt_face_get_location( face, glyph_index,
1300
(FT_UInt*)&loader->byte_len );
1302
if ( loader->byte_len > 0 )
1304
#ifdef FT_CONFIG_OPTION_INCREMENTAL
1305
/* for the incremental interface, `glyf_offset' is always zero */
1306
if ( !loader->glyf_offset &&
1307
!face->root.internal->incremental_interface )
1309
if ( !loader->glyf_offset )
1310
#endif /* FT_CONFIG_OPTION_INCREMENTAL */
1312
FT_TRACE2(( "no `glyf' table but non-zero `loca' entry\n" ));
1313
error = TT_Err_Invalid_Table;
1317
error = face->access_glyph_frame( loader, glyph_index,
1318
loader->glyf_offset + offset,
1325
/* read glyph header first */
1326
error = face->read_glyph_header( loader );
1327
if ( error || header_only )
1331
if ( loader->byte_len == 0 || loader->n_contours == 0 )
1333
loader->bbox.xMin = 0;
1334
loader->bbox.xMax = 0;
1335
loader->bbox.yMin = 0;
1336
loader->bbox.yMax = 0;
1341
/* must initialize points before (possibly) overriding */
1342
/* glyph metrics from the incremental interface */
1343
TT_LOADER_SET_PP( loader );
1345
#ifdef FT_CONFIG_OPTION_INCREMENTAL
1346
tt_get_metrics_incr_overrides( loader, glyph_index );
1349
#ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
1351
if ( ((TT_Face)(loader->face))->doblend )
1353
/* this must be done before scaling */
1354
FT_Memory memory = loader->face->memory;
1357
error = TT_Vary_Get_Glyph_Deltas( (TT_Face)(loader->face),
1358
glyph_index, &deltas, 4 );
1362
loader->pp1.x += deltas[0].x; loader->pp1.y += deltas[0].y;
1363
loader->pp2.x += deltas[1].x; loader->pp2.y += deltas[1].y;
1364
loader->pp3.x += deltas[2].x; loader->pp3.y += deltas[2].y;
1365
loader->pp4.x += deltas[3].x; loader->pp4.y += deltas[3].y;
1372
if ( ( loader->load_flags & FT_LOAD_NO_SCALE ) == 0 )
1374
loader->pp1.x = FT_MulFix( loader->pp1.x, x_scale );
1375
loader->pp2.x = FT_MulFix( loader->pp2.x, x_scale );
1376
loader->pp3.y = FT_MulFix( loader->pp3.y, y_scale );
1377
loader->pp4.y = FT_MulFix( loader->pp4.y, y_scale );
1384
/* must initialize points before (possibly) overriding */
1385
/* glyph metrics from the incremental interface */
1386
TT_LOADER_SET_PP( loader );
1388
#ifdef FT_CONFIG_OPTION_INCREMENTAL
1389
tt_get_metrics_incr_overrides( loader, glyph_index );
1392
/***********************************************************************/
1393
/***********************************************************************/
1394
/***********************************************************************/
1396
/* if it is a simple glyph, load it */
1398
if ( loader->n_contours > 0 )
1400
error = face->read_simple_glyph( loader );
1404
/* all data have been read */
1405
face->forget_glyph_frame( loader );
1408
error = TT_Process_Simple_Glyph( loader );
1412
FT_GlyphLoader_Add( gloader );
1415
/***********************************************************************/
1416
/***********************************************************************/
1417
/***********************************************************************/
1419
/* otherwise, load a composite! */
1420
else if ( loader->n_contours == -1 )
1422
FT_UInt start_point;
1423
FT_UInt start_contour;
1424
FT_ULong ins_pos; /* position of composite instructions, if any */
1427
start_point = gloader->base.outline.n_points;
1428
start_contour = gloader->base.outline.n_contours;
1430
/* for each subglyph, read composite header */
1431
error = face->read_composite_glyph( loader );
1435
/* store the offset of instructions */
1436
ins_pos = loader->ins_pos;
1438
/* all data we need are read */
1439
face->forget_glyph_frame( loader );
1442
#ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
1444
if ( face->doblend )
1447
FT_SubGlyph subglyph;
1448
FT_Memory memory = face->root.memory;
1451
/* this provides additional offsets */
1452
/* for each component's translation */
1454
if ( ( error = TT_Vary_Get_Glyph_Deltas(
1458
gloader->current.num_subglyphs + 4 )) != 0 )
1461
subglyph = gloader->current.subglyphs + gloader->base.num_subglyphs;
1462
limit = gloader->current.num_subglyphs;
1464
for ( i = 0; i < limit; ++i, ++subglyph )
1466
if ( subglyph->flags & ARGS_ARE_XY_VALUES )
1468
/* XXX: overflow check for subglyph->{arg1,arg2}. */
1469
/* deltas[i].{x,y} must be within signed 16-bit, */
1470
/* but the restriction of summed delta is not clear */
1471
subglyph->arg1 += (FT_Int16)deltas[i].x;
1472
subglyph->arg2 += (FT_Int16)deltas[i].y;
1476
loader->pp1.x += deltas[i + 0].x; loader->pp1.y += deltas[i + 0].y;
1477
loader->pp2.x += deltas[i + 1].x; loader->pp2.y += deltas[i + 1].y;
1478
loader->pp3.x += deltas[i + 2].x; loader->pp3.y += deltas[i + 2].y;
1479
loader->pp4.x += deltas[i + 3].x; loader->pp4.y += deltas[i + 3].y;
1484
#endif /* TT_CONFIG_OPTION_GX_VAR_SUPPORT */
1486
if ( ( loader->load_flags & FT_LOAD_NO_SCALE ) == 0 )
1488
loader->pp1.x = FT_MulFix( loader->pp1.x, x_scale );
1489
loader->pp2.x = FT_MulFix( loader->pp2.x, x_scale );
1490
loader->pp3.y = FT_MulFix( loader->pp3.y, y_scale );
1491
loader->pp4.y = FT_MulFix( loader->pp4.y, y_scale );
1494
/* if the flag FT_LOAD_NO_RECURSE is set, we return the subglyph */
1495
/* `as is' in the glyph slot (the client application will be */
1496
/* responsible for interpreting these data)... */
1497
if ( loader->load_flags & FT_LOAD_NO_RECURSE )
1499
FT_GlyphLoader_Add( gloader );
1500
loader->glyph->format = FT_GLYPH_FORMAT_COMPOSITE;
1505
/*********************************************************************/
1506
/*********************************************************************/
1507
/*********************************************************************/
1510
FT_UInt n, num_base_points;
1511
FT_SubGlyph subglyph = 0;
1513
FT_UInt num_points = start_point;
1514
FT_UInt num_subglyphs = gloader->current.num_subglyphs;
1515
FT_UInt num_base_subgs = gloader->base.num_subglyphs;
1517
FT_Stream old_stream = loader->stream;
1518
FT_Int old_byte_len = loader->byte_len;
1521
FT_GlyphLoader_Add( gloader );
1523
/* read each subglyph independently */
1524
for ( n = 0; n < num_subglyphs; n++ )
1529
/* Each time we call load_truetype_glyph in this loop, the */
1530
/* value of `gloader.base.subglyphs' can change due to table */
1531
/* reallocations. We thus need to recompute the subglyph */
1532
/* pointer on each iteration. */
1533
subglyph = gloader->base.subglyphs + num_base_subgs + n;
1535
pp[0] = loader->pp1;
1536
pp[1] = loader->pp2;
1537
pp[2] = loader->pp3;
1538
pp[3] = loader->pp4;
1540
num_base_points = gloader->base.outline.n_points;
1542
error = load_truetype_glyph( loader, subglyph->index,
1543
recurse_count + 1, FALSE );
1547
/* restore subglyph pointer */
1548
subglyph = gloader->base.subglyphs + num_base_subgs + n;
1550
if ( !( subglyph->flags & USE_MY_METRICS ) )
1552
loader->pp1 = pp[0];
1553
loader->pp2 = pp[1];
1554
loader->pp3 = pp[2];
1555
loader->pp4 = pp[3];
1558
num_points = gloader->base.outline.n_points;
1560
if ( num_points == num_base_points )
1563
/* gloader->base.outline consists of three parts: */
1564
/* 0 -(1)-> start_point -(2)-> num_base_points -(3)-> n_points. */
1566
/* (1): exists from the beginning */
1567
/* (2): components that have been loaded so far */
1568
/* (3): the newly loaded component */
1569
TT_Process_Composite_Component( loader, subglyph, start_point,
1573
loader->stream = old_stream;
1574
loader->byte_len = old_byte_len;
1576
/* process the glyph */
1577
loader->ins_pos = ins_pos;
1578
if ( IS_HINTED( loader->load_flags ) &&
1580
#ifdef TT_USE_BYTECODE_INTERPRETER
1582
subglyph->flags & WE_HAVE_INSTR &&
1586
num_points > start_point )
1587
TT_Process_Composite_Glyph( loader, start_point, start_contour );
1593
/* invalid composite count (negative but not -1) */
1594
error = TT_Err_Invalid_Outline;
1598
/***********************************************************************/
1599
/***********************************************************************/
1600
/***********************************************************************/
1605
face->forget_glyph_frame( loader );
1607
#ifdef FT_CONFIG_OPTION_INCREMENTAL
1609
if ( glyph_data_loaded )
1610
face->root.internal->incremental_interface->funcs->free_glyph_data(
1611
face->root.internal->incremental_interface->object,
1621
compute_glyph_metrics( TT_Loader loader,
1622
FT_UInt glyph_index )
1625
TT_Face face = (TT_Face)loader->face;
1627
TT_GlyphSlot glyph = loader->glyph;
1628
TT_Size size = (TT_Size)loader->size;
1632
if ( ( loader->load_flags & FT_LOAD_NO_SCALE ) == 0 )
1633
y_scale = size->root.metrics.y_scale;
1635
if ( glyph->format != FT_GLYPH_FORMAT_COMPOSITE )
1636
FT_Outline_Get_CBox( &glyph->outline, &bbox );
1638
bbox = loader->bbox;
1640
/* get the device-independent horizontal advance; it is scaled later */
1641
/* by the base layer. */
1642
glyph->linearHoriAdvance = loader->linear;
1644
glyph->metrics.horiBearingX = bbox.xMin;
1645
glyph->metrics.horiBearingY = bbox.yMax;
1646
glyph->metrics.horiAdvance = loader->pp2.x - loader->pp1.x;
1648
/* adjust advance width to the value contained in the hdmx table */
1649
if ( !face->postscript.isFixedPitch &&
1650
IS_HINTED( loader->load_flags ) )
1655
widthp = tt_face_get_device_metrics( face,
1656
size->root.metrics.x_ppem,
1660
glyph->metrics.horiAdvance = *widthp << 6;
1663
/* set glyph dimensions */
1664
glyph->metrics.width = bbox.xMax - bbox.xMin;
1665
glyph->metrics.height = bbox.yMax - bbox.yMin;
1667
/* Now take care of vertical metrics. In the case where there is */
1668
/* no vertical information within the font (relatively common), */
1669
/* create some metrics manually */
1671
FT_Pos top; /* scaled vertical top side bearing */
1672
FT_Pos advance; /* scaled vertical advance height */
1675
/* Get the unscaled top bearing and advance height. */
1676
if ( face->vertical_info &&
1677
face->vertical.number_Of_VMetrics > 0 )
1679
top = (FT_Short)FT_DivFix( loader->pp3.y - bbox.yMax,
1682
if ( loader->pp3.y <= loader->pp4.y )
1685
advance = (FT_UShort)FT_DivFix( loader->pp3.y - loader->pp4.y,
1693
/* XXX Compute top side bearing and advance height in */
1694
/* Get_VMetrics instead of here. */
1696
/* NOTE: The OS/2 values are the only `portable' ones, */
1697
/* which is why we use them, if there is an OS/2 */
1698
/* table in the font. Otherwise, we use the */
1699
/* values defined in the horizontal header. */
1701
height = (FT_Short)FT_DivFix( bbox.yMax - bbox.yMin,
1703
if ( face->os2.version != 0xFFFFU )
1704
advance = (FT_Pos)( face->os2.sTypoAscender -
1705
face->os2.sTypoDescender );
1707
advance = (FT_Pos)( face->horizontal.Ascender -
1708
face->horizontal.Descender );
1710
top = ( advance - height ) / 2;
1713
#ifdef FT_CONFIG_OPTION_INCREMENTAL
1715
FT_Incremental_InterfaceRec* incr;
1716
FT_Incremental_MetricsRec metrics;
1720
incr = face->root.internal->incremental_interface;
1722
/* If this is an incrementally loaded font see if there are */
1723
/* overriding metrics for this glyph. */
1724
if ( incr && incr->funcs->get_glyph_metrics )
1726
metrics.bearing_x = 0;
1727
metrics.bearing_y = top;
1728
metrics.advance = advance;
1730
error = incr->funcs->get_glyph_metrics( incr->object,
1737
top = metrics.bearing_y;
1738
advance = metrics.advance;
1742
/* GWW: Do vertical metrics get loaded incrementally too? */
1744
#endif /* FT_CONFIG_OPTION_INCREMENTAL */
1746
glyph->linearVertAdvance = advance;
1748
/* scale the metrics */
1749
if ( !( loader->load_flags & FT_LOAD_NO_SCALE ) )
1751
top = FT_MulFix( top, y_scale );
1752
advance = FT_MulFix( advance, y_scale );
1755
/* XXX: for now, we have no better algorithm for the lsb, but it */
1756
/* should work fine. */
1758
glyph->metrics.vertBearingX = glyph->metrics.horiBearingX -
1759
glyph->metrics.horiAdvance / 2;
1760
glyph->metrics.vertBearingY = top;
1761
glyph->metrics.vertAdvance = advance;
1768
#ifdef TT_CONFIG_OPTION_EMBEDDED_BITMAPS
1771
load_sbit_image( TT_Size size,
1773
FT_UInt glyph_index,
1774
FT_Int32 load_flags )
1780
TT_SBit_MetricsRec metrics;
1783
face = (TT_Face)glyph->face;
1784
sfnt = (SFNT_Service)face->sfnt;
1785
stream = face->root.stream;
1787
error = sfnt->load_sbit_image( face,
1796
glyph->outline.n_points = 0;
1797
glyph->outline.n_contours = 0;
1799
glyph->metrics.width = (FT_Pos)metrics.width << 6;
1800
glyph->metrics.height = (FT_Pos)metrics.height << 6;
1802
glyph->metrics.horiBearingX = (FT_Pos)metrics.horiBearingX << 6;
1803
glyph->metrics.horiBearingY = (FT_Pos)metrics.horiBearingY << 6;
1804
glyph->metrics.horiAdvance = (FT_Pos)metrics.horiAdvance << 6;
1806
glyph->metrics.vertBearingX = (FT_Pos)metrics.vertBearingX << 6;
1807
glyph->metrics.vertBearingY = (FT_Pos)metrics.vertBearingY << 6;
1808
glyph->metrics.vertAdvance = (FT_Pos)metrics.vertAdvance << 6;
1810
glyph->format = FT_GLYPH_FORMAT_BITMAP;
1812
if ( load_flags & FT_LOAD_VERTICAL_LAYOUT )
1814
glyph->bitmap_left = metrics.vertBearingX;
1815
glyph->bitmap_top = metrics.vertBearingY;
1819
glyph->bitmap_left = metrics.horiBearingX;
1820
glyph->bitmap_top = metrics.horiBearingY;
1827
#endif /* TT_CONFIG_OPTION_EMBEDDED_BITMAPS */
1831
tt_loader_init( TT_Loader loader,
1834
FT_Int32 load_flags,
1835
FT_Bool glyf_table_only )
1839
FT_Bool pedantic = FT_BOOL( load_flags & FT_LOAD_PEDANTIC );
1842
face = (TT_Face)glyph->face;
1843
stream = face->root.stream;
1845
FT_MEM_ZERO( loader, sizeof ( TT_LoaderRec ) );
1847
#ifdef TT_USE_BYTECODE_INTERPRETER
1849
/* load execution context */
1850
if ( IS_HINTED( load_flags ) && !glyf_table_only )
1852
TT_ExecContext exec;
1856
if ( !size->cvt_ready )
1858
FT_Error error = tt_size_ready_bytecode( size, pedantic );
1865
/* query new execution context */
1866
exec = size->debug ? size->context
1867
: ( (TT_Driver)FT_FACE_DRIVER( face ) )->context;
1869
return TT_Err_Could_Not_Find_Context;
1872
FT_BOOL( FT_LOAD_TARGET_MODE( load_flags ) != FT_RENDER_MODE_MONO );
1874
TT_Load_Context( exec, face, size );
1876
/* a change from mono to grayscale rendering (and vice versa) */
1877
/* requires a re-execution of the CVT program */
1878
if ( grayscale != exec->grayscale )
1883
FT_TRACE4(( "tt_loader_init: grayscale change,"
1884
" re-executing `prep' table\n" ));
1886
exec->grayscale = grayscale;
1888
for ( i = 0; i < size->cvt_size; i++ )
1889
size->cvt[i] = FT_MulFix( face->cvt[i], size->ttmetrics.scale );
1890
tt_size_run_prep( size, pedantic );
1893
/* see whether the cvt program has disabled hinting */
1894
if ( exec->GS.instruct_control & 1 )
1895
load_flags |= FT_LOAD_NO_HINTING;
1897
/* load default graphics state -- if needed */
1898
if ( exec->GS.instruct_control & 2 )
1899
exec->GS = tt_default_graphics_state;
1901
exec->pedantic_hinting = FT_BOOL( load_flags & FT_LOAD_PEDANTIC );
1902
loader->exec = exec;
1903
loader->instructions = exec->glyphIns;
1906
#endif /* TT_USE_BYTECODE_INTERPRETER */
1908
/* seek to the beginning of the glyph table -- for Type 42 fonts */
1909
/* the table might be accessed from a Postscript stream or something */
1912
#ifdef FT_CONFIG_OPTION_INCREMENTAL
1914
if ( face->root.internal->incremental_interface )
1915
loader->glyf_offset = 0;
1921
FT_Error error = face->goto_table( face, TTAG_glyf, stream, 0 );
1924
if ( error == TT_Err_Table_Missing )
1925
loader->glyf_offset = 0;
1928
FT_ERROR(( "tt_loader_init: could not access glyph table\n" ));
1932
loader->glyf_offset = FT_STREAM_POS();
1935
/* get face's glyph loader */
1936
if ( !glyf_table_only )
1938
FT_GlyphLoader gloader = glyph->internal->loader;
1941
FT_GlyphLoader_Rewind( gloader );
1942
loader->gloader = gloader;
1945
loader->load_flags = load_flags;
1947
loader->face = (FT_Face)face;
1948
loader->size = (FT_Size)size;
1949
loader->glyph = (FT_GlyphSlot)glyph;
1950
loader->stream = stream;
1956
/*************************************************************************/
1962
/* A function used to load a single glyph within a given glyph slot, */
1963
/* for a given size. */
1966
/* glyph :: A handle to a target slot object where the glyph */
1967
/* will be loaded. */
1969
/* size :: A handle to the source face size at which the glyph */
1970
/* must be scaled/loaded. */
1972
/* glyph_index :: The index of the glyph in the font file. */
1974
/* load_flags :: A flag indicating what to load for this glyph. The */
1975
/* FT_LOAD_XXX constants can be used to control the */
1976
/* glyph loading process (e.g., whether the outline */
1977
/* should be scaled, whether to load bitmaps or not, */
1978
/* whether to hint the outline, etc). */
1981
/* FreeType error code. 0 means success. */
1983
FT_LOCAL_DEF( FT_Error )
1984
TT_Load_Glyph( TT_Size size,
1986
FT_UInt glyph_index,
1987
FT_Int32 load_flags )
1991
TT_LoaderRec loader;
1994
face = (TT_Face)glyph->face;
1997
#ifdef TT_CONFIG_OPTION_EMBEDDED_BITMAPS
1999
/* try to load embedded bitmap if any */
2001
/* XXX: The convention should be emphasized in */
2002
/* the documents because it can be confusing. */
2003
if ( size->strike_index != 0xFFFFFFFFUL &&
2004
( load_flags & FT_LOAD_NO_BITMAP ) == 0 )
2006
error = load_sbit_image( size, glyph, glyph_index, load_flags );
2009
FT_Face root = &face->root;
2012
if ( FT_IS_SCALABLE( root ) )
2014
/* for the bbox we need the header only */
2015
(void)tt_loader_init( &loader, size, glyph, load_flags, TRUE );
2016
(void)load_truetype_glyph( &loader, glyph_index, 0, TRUE );
2017
glyph->linearHoriAdvance = loader.linear;
2018
glyph->linearVertAdvance = loader.top_bearing + loader.bbox.yMax -
2026
#endif /* TT_CONFIG_OPTION_EMBEDDED_BITMAPS */
2028
/* if FT_LOAD_NO_SCALE is not set, `ttmetrics' must be valid */
2029
if ( !( load_flags & FT_LOAD_NO_SCALE ) && !size->ttmetrics.valid )
2030
return TT_Err_Invalid_Size_Handle;
2032
if ( load_flags & FT_LOAD_SBITS_ONLY )
2033
return TT_Err_Invalid_Argument;
2035
error = tt_loader_init( &loader, size, glyph, load_flags, FALSE );
2039
glyph->format = FT_GLYPH_FORMAT_OUTLINE;
2040
glyph->num_subglyphs = 0;
2041
glyph->outline.flags = 0;
2043
/* main loading loop */
2044
error = load_truetype_glyph( &loader, glyph_index, 0, FALSE );
2047
if ( glyph->format == FT_GLYPH_FORMAT_COMPOSITE )
2049
glyph->num_subglyphs = loader.gloader->base.num_subglyphs;
2050
glyph->subglyphs = loader.gloader->base.subglyphs;
2054
glyph->outline = loader.gloader->base.outline;
2055
glyph->outline.flags &= ~FT_OUTLINE_SINGLE_PASS;
2057
/* Translate array so that (0,0) is the glyph's origin. Note */
2058
/* that this behaviour is independent on the value of bit 1 of */
2059
/* the `flags' field in the `head' table -- at least major */
2060
/* applications like Acroread indicate that. */
2062
FT_Outline_Translate( &glyph->outline, -loader.pp1.x, 0 );
2065
#ifdef TT_USE_BYTECODE_INTERPRETER
2067
if ( IS_HINTED( load_flags ) )
2069
if ( loader.exec->GS.scan_control )
2071
/* convert scan conversion mode to FT_OUTLINE_XXX flags */
2072
switch ( loader.exec->GS.scan_type )
2074
case 0: /* simple drop-outs including stubs */
2075
glyph->outline.flags |= FT_OUTLINE_INCLUDE_STUBS;
2077
case 1: /* simple drop-outs excluding stubs */
2078
/* nothing; it's the default rendering mode */
2080
case 4: /* smart drop-outs including stubs */
2081
glyph->outline.flags |= FT_OUTLINE_SMART_DROPOUTS |
2082
FT_OUTLINE_INCLUDE_STUBS;
2084
case 5: /* smart drop-outs excluding stubs */
2085
glyph->outline.flags |= FT_OUTLINE_SMART_DROPOUTS;
2088
default: /* no drop-out control */
2089
glyph->outline.flags |= FT_OUTLINE_IGNORE_DROPOUTS;
2094
glyph->outline.flags |= FT_OUTLINE_IGNORE_DROPOUTS;
2097
#endif /* TT_USE_BYTECODE_INTERPRETER */
2099
compute_glyph_metrics( &loader, glyph_index );
2102
/* Set the `high precision' bit flag. */
2103
/* This is _critical_ to get correct output for monochrome */
2104
/* TrueType glyphs at all sizes using the bytecode interpreter. */
2106
if ( !( load_flags & FT_LOAD_NO_SCALE ) &&
2107
size->root.metrics.y_ppem < 24 )
2108
glyph->outline.flags |= FT_OUTLINE_HIGH_PRECISION;