1
/***************************************************************************/
5
/* TrueType Glyph Loader (body). */
7
/* Copyright 1996-2013 */
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
26
#include FT_TRUETYPE_DRIVER_H
31
#ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
39
/*************************************************************************/
41
/* The macro FT_COMPONENT is used in trace mode. It is an implicit */
42
/* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */
43
/* messages during execution. */
46
#define FT_COMPONENT trace_ttgload
49
/*************************************************************************/
51
/* Composite glyph flags. */
53
#define ARGS_ARE_WORDS 0x0001
54
#define ARGS_ARE_XY_VALUES 0x0002
55
#define ROUND_XY_TO_GRID 0x0004
56
#define WE_HAVE_A_SCALE 0x0008
58
#define MORE_COMPONENTS 0x0020
59
#define WE_HAVE_AN_XY_SCALE 0x0040
60
#define WE_HAVE_A_2X2 0x0080
61
#define WE_HAVE_INSTR 0x0100
62
#define USE_MY_METRICS 0x0200
63
#define OVERLAP_COMPOUND 0x0400
64
#define SCALED_COMPONENT_OFFSET 0x0800
65
#define UNSCALED_COMPONENT_OFFSET 0x1000
68
/*************************************************************************/
70
/* Return the horizontal metrics in font units for a given glyph. */
73
TT_Get_HMetrics( TT_Face face,
78
( (SFNT_Service)face->sfnt )->get_metrics( face, 0, idx, lsb, aw );
80
FT_TRACE5(( " advance width (font units): %d\n", *aw ));
81
FT_TRACE5(( " left side bearing (font units): %d\n", *lsb ));
85
/*************************************************************************/
87
/* Return the vertical metrics in font units for a given glyph. */
88
/* See macro `TT_LOADER_SET_PP' below for explanations. */
91
TT_Get_VMetrics( TT_Face face,
97
if ( face->vertical_info )
98
( (SFNT_Service)face->sfnt )->get_metrics( face, 1, idx, tsb, ah );
100
else if ( face->os2.version != 0xFFFFU )
102
*tsb = face->os2.sTypoAscender - yMax;
103
*ah = face->os2.sTypoAscender - face->os2.sTypoDescender;
108
*tsb = face->horizontal.Ascender - yMax;
109
*ah = face->horizontal.Ascender - face->horizontal.Descender;
112
FT_TRACE5(( " advance height (font units): %d\n", *ah ));
113
FT_TRACE5(( " top side bearing (font units): %d\n", *tsb ));
118
tt_get_metrics( TT_Loader loader,
119
FT_UInt glyph_index )
121
TT_Face face = (TT_Face)loader->face;
122
#ifdef TT_CONFIG_OPTION_SUBPIXEL_HINTING
123
TT_Driver driver = (TT_Driver)FT_FACE_DRIVER( face );
127
FT_Stream stream = loader->stream;
129
FT_Short left_bearing = 0, top_bearing = 0;
130
FT_UShort advance_width = 0, advance_height = 0;
132
/* we must preserve the stream position */
133
/* (which gets altered by the metrics functions) */
134
FT_ULong pos = FT_STREAM_POS();
137
TT_Get_HMetrics( face, glyph_index,
140
TT_Get_VMetrics( face, glyph_index,
145
if ( FT_STREAM_SEEK( pos ) )
148
loader->left_bearing = left_bearing;
149
loader->advance = advance_width;
150
loader->top_bearing = top_bearing;
151
loader->vadvance = advance_height;
153
#ifdef TT_CONFIG_OPTION_SUBPIXEL_HINTING
154
if ( driver->interpreter_version == TT_INTERPRETER_VERSION_38 )
157
loader->exec->sph_tweak_flags = 0;
159
/* this may not be the right place for this, but it works */
160
if ( loader->exec && loader->exec->ignore_x_mode )
161
sph_set_tweaks( loader, glyph_index );
163
#endif /* TT_CONFIG_OPTION_SUBPIXEL_HINTING */
165
if ( !loader->linear_def )
167
loader->linear_def = 1;
168
loader->linear = advance_width;
175
#ifdef FT_CONFIG_OPTION_INCREMENTAL
178
tt_get_metrics_incr_overrides( TT_Loader loader,
179
FT_UInt glyph_index )
181
TT_Face face = (TT_Face)loader->face;
183
FT_Short left_bearing = 0, top_bearing = 0;
184
FT_UShort advance_width = 0, advance_height = 0;
187
/* If this is an incrementally loaded font check whether there are */
188
/* overriding metrics for this glyph. */
189
if ( face->root.internal->incremental_interface &&
190
face->root.internal->incremental_interface->funcs->get_glyph_metrics )
192
FT_Incremental_MetricsRec metrics;
196
metrics.bearing_x = loader->left_bearing;
197
metrics.bearing_y = 0;
198
metrics.advance = loader->advance;
199
metrics.advance_v = 0;
201
error = face->root.internal->incremental_interface->funcs->get_glyph_metrics(
202
face->root.internal->incremental_interface->object,
203
glyph_index, FALSE, &metrics );
207
left_bearing = (FT_Short)metrics.bearing_x;
208
advance_width = (FT_UShort)metrics.advance;
212
/* GWW: Do I do the same for vertical metrics? */
213
metrics.bearing_x = 0;
214
metrics.bearing_y = loader->top_bearing;
215
metrics.advance = loader->vadvance;
217
error = face->root.internal->incremental_interface->funcs->get_glyph_metrics(
218
face->root.internal->incremental_interface->object,
219
glyph_index, TRUE, &metrics );
223
top_bearing = (FT_Short)metrics.bearing_y;
224
advance_height = (FT_UShort)metrics.advance;
228
loader->left_bearing = left_bearing;
229
loader->advance = advance_width;
230
loader->top_bearing = top_bearing;
231
loader->vadvance = advance_height;
233
if ( !loader->linear_def )
235
loader->linear_def = 1;
236
loader->linear = advance_width;
244
#endif /* FT_CONFIG_OPTION_INCREMENTAL */
247
/*************************************************************************/
249
/* Translates an array of coordinates. */
252
translate_array( FT_UInt n,
261
for ( k = 0; k < n; k++ )
262
coords[k].x += delta_x;
265
for ( k = 0; k < n; k++ )
266
coords[k].y += delta_y;
270
/*************************************************************************/
272
/* The following functions are used by default with TrueType fonts. */
273
/* However, they can be replaced by alternatives if we need to support */
274
/* TrueType-compressed formats (like MicroType) in the future. */
276
/*************************************************************************/
278
FT_CALLBACK_DEF( FT_Error )
279
TT_Access_Glyph_Frame( TT_Loader loader,
285
FT_Stream stream = loader->stream;
287
/* for non-debug mode */
288
FT_UNUSED( glyph_index );
291
FT_TRACE4(( "Glyph %ld\n", glyph_index ));
293
/* the following line sets the `error' variable through macros! */
294
if ( FT_STREAM_SEEK( offset ) || FT_FRAME_ENTER( byte_count ) )
297
loader->cursor = stream->cursor;
298
loader->limit = stream->limit;
304
FT_CALLBACK_DEF( void )
305
TT_Forget_Glyph_Frame( TT_Loader loader )
307
FT_Stream stream = loader->stream;
314
FT_CALLBACK_DEF( FT_Error )
315
TT_Load_Glyph_Header( TT_Loader loader )
317
FT_Byte* p = loader->cursor;
318
FT_Byte* limit = loader->limit;
321
if ( p + 10 > limit )
322
return FT_THROW( Invalid_Outline );
324
loader->n_contours = FT_NEXT_SHORT( p );
326
loader->bbox.xMin = FT_NEXT_SHORT( p );
327
loader->bbox.yMin = FT_NEXT_SHORT( p );
328
loader->bbox.xMax = FT_NEXT_SHORT( p );
329
loader->bbox.yMax = FT_NEXT_SHORT( p );
331
FT_TRACE5(( " # of contours: %d\n", loader->n_contours ));
332
FT_TRACE5(( " xMin: %4d xMax: %4d\n", loader->bbox.xMin,
333
loader->bbox.xMax ));
334
FT_TRACE5(( " yMin: %4d yMax: %4d\n", loader->bbox.yMin,
335
loader->bbox.yMax ));
342
FT_CALLBACK_DEF( FT_Error )
343
TT_Load_Simple_Glyph( TT_Loader load )
346
FT_Byte* p = load->cursor;
347
FT_Byte* limit = load->limit;
348
FT_GlyphLoader gloader = load->gloader;
349
FT_Int n_contours = load->n_contours;
355
FT_Byte *flag, *flag_limit;
357
FT_Vector *vec, *vec_limit;
359
FT_Short *cont, *cont_limit, prev_cont;
363
/* check that we can add the contours to the glyph */
364
error = FT_GLYPHLOADER_CHECK_POINTS( gloader, 0, n_contours );
368
/* reading the contours' endpoints & number of points */
369
cont = gloader->current.outline.contours;
370
cont_limit = cont + n_contours;
372
/* check space for contours array + instructions count */
373
if ( n_contours >= 0xFFF || p + ( n_contours + 1 ) * 2 > limit )
374
goto Invalid_Outline;
376
prev_cont = FT_NEXT_SHORT( p );
378
if ( n_contours > 0 )
382
goto Invalid_Outline;
384
for ( cont++; cont < cont_limit; cont++ )
386
cont[0] = FT_NEXT_SHORT( p );
387
if ( cont[0] <= prev_cont )
389
/* unordered contours: this is invalid */
390
goto Invalid_Outline;
396
if ( n_contours > 0 )
398
n_points = cont[-1] + 1;
400
goto Invalid_Outline;
403
/* note that we will add four phantom points later */
404
error = FT_GLYPHLOADER_CHECK_POINTS( gloader, n_points + 4, 0 );
408
/* reading the bytecode instructions */
409
load->glyph->control_len = 0;
410
load->glyph->control_data = 0;
413
goto Invalid_Outline;
415
n_ins = FT_NEXT_USHORT( p );
417
FT_TRACE5(( " Instructions size: %u\n", n_ins ));
420
if ( ( limit - p ) < n_ins )
422
FT_TRACE0(( "TT_Load_Simple_Glyph: instruction count mismatch\n" ));
423
error = FT_THROW( Too_Many_Hints );
427
#ifdef TT_USE_BYTECODE_INTERPRETER
429
if ( IS_HINTED( load->load_flags ) )
431
/* we don't trust `maxSizeOfInstructions' in the `maxp' table */
432
/* and thus update the bytecode array size by ourselves */
434
tmp = load->exec->glyphSize;
435
error = Update_Max( load->exec->memory,
438
(void*)&load->exec->glyphIns,
441
load->exec->glyphSize = (FT_UShort)tmp;
445
load->glyph->control_len = n_ins;
446
load->glyph->control_data = load->exec->glyphIns;
448
FT_MEM_COPY( load->exec->glyphIns, p, (FT_Long)n_ins );
451
#endif /* TT_USE_BYTECODE_INTERPRETER */
455
outline = &gloader->current.outline;
457
/* reading the point tags */
458
flag = (FT_Byte*)outline->tags;
459
flag_limit = flag + n_points;
461
FT_ASSERT( flag != NULL );
463
while ( flag < flag_limit )
466
goto Invalid_Outline;
468
*flag++ = c = FT_NEXT_BYTE( p );
472
goto Invalid_Outline;
474
count = FT_NEXT_BYTE( p );
475
if ( flag + (FT_Int)count > flag_limit )
476
goto Invalid_Outline;
478
for ( ; count > 0; count-- )
483
/* reading the X coordinates */
485
vec = outline->points;
486
vec_limit = vec + n_points;
487
flag = (FT_Byte*)outline->tags;
490
if ( p + xy_size > limit )
491
goto Invalid_Outline;
493
for ( ; vec < vec_limit; vec++, flag++ )
502
goto Invalid_Outline;
504
y = (FT_Pos)FT_NEXT_BYTE( p );
505
if ( ( f & 16 ) == 0 )
508
else if ( ( f & 16 ) == 0 )
511
goto Invalid_Outline;
513
y = (FT_Pos)FT_NEXT_SHORT( p );
518
/* the cast is for stupid compilers */
519
*flag = (FT_Byte)( f & ~( 2 | 16 ) );
522
/* reading the Y coordinates */
524
vec = gloader->current.outline.points;
525
vec_limit = vec + n_points;
526
flag = (FT_Byte*)outline->tags;
529
for ( ; vec < vec_limit; vec++, flag++ )
538
goto Invalid_Outline;
540
y = (FT_Pos)FT_NEXT_BYTE( p );
541
if ( ( f & 32 ) == 0 )
544
else if ( ( f & 32 ) == 0 )
547
goto Invalid_Outline;
549
y = (FT_Pos)FT_NEXT_SHORT( p );
554
/* the cast is for stupid compilers */
555
*flag = (FT_Byte)( f & FT_CURVE_TAG_ON );
558
outline->n_points = (FT_UShort)n_points;
559
outline->n_contours = (FT_Short) n_contours;
567
error = FT_THROW( Invalid_Outline );
572
FT_CALLBACK_DEF( FT_Error )
573
TT_Load_Composite_Glyph( TT_Loader loader )
576
FT_Byte* p = loader->cursor;
577
FT_Byte* limit = loader->limit;
578
FT_GlyphLoader gloader = loader->gloader;
579
FT_SubGlyph subglyph;
580
FT_UInt num_subglyphs;
587
FT_Fixed xx, xy, yy, yx;
591
/* check that we can load a new subglyph */
592
error = FT_GlyphLoader_CheckSubGlyphs( gloader, num_subglyphs + 1 );
598
goto Invalid_Composite;
600
subglyph = gloader->current.subglyphs + num_subglyphs;
602
subglyph->arg1 = subglyph->arg2 = 0;
604
subglyph->flags = FT_NEXT_USHORT( p );
605
subglyph->index = FT_NEXT_USHORT( p );
609
if ( subglyph->flags & ARGS_ARE_WORDS )
611
if ( subglyph->flags & WE_HAVE_A_SCALE )
613
else if ( subglyph->flags & WE_HAVE_AN_XY_SCALE )
615
else if ( subglyph->flags & WE_HAVE_A_2X2 )
618
if ( p + count > limit )
619
goto Invalid_Composite;
622
if ( subglyph->flags & ARGS_ARE_WORDS )
624
subglyph->arg1 = FT_NEXT_SHORT( p );
625
subglyph->arg2 = FT_NEXT_SHORT( p );
629
subglyph->arg1 = FT_NEXT_CHAR( p );
630
subglyph->arg2 = FT_NEXT_CHAR( p );
637
if ( subglyph->flags & WE_HAVE_A_SCALE )
639
xx = (FT_Fixed)FT_NEXT_SHORT( p ) << 2;
642
else if ( subglyph->flags & WE_HAVE_AN_XY_SCALE )
644
xx = (FT_Fixed)FT_NEXT_SHORT( p ) << 2;
645
yy = (FT_Fixed)FT_NEXT_SHORT( p ) << 2;
647
else if ( subglyph->flags & WE_HAVE_A_2X2 )
649
xx = (FT_Fixed)FT_NEXT_SHORT( p ) << 2;
650
yx = (FT_Fixed)FT_NEXT_SHORT( p ) << 2;
651
xy = (FT_Fixed)FT_NEXT_SHORT( p ) << 2;
652
yy = (FT_Fixed)FT_NEXT_SHORT( p ) << 2;
655
subglyph->transform.xx = xx;
656
subglyph->transform.xy = xy;
657
subglyph->transform.yx = yx;
658
subglyph->transform.yy = yy;
662
} while ( subglyph->flags & MORE_COMPONENTS );
664
gloader->current.num_subglyphs = num_subglyphs;
666
#ifdef TT_USE_BYTECODE_INTERPRETER
669
FT_Stream stream = loader->stream;
672
/* we must undo the FT_FRAME_ENTER in order to point */
673
/* to the composite instructions, if we find some. */
674
/* We will process them later. */
676
loader->ins_pos = (FT_ULong)( FT_STREAM_POS() +
688
error = FT_THROW( Invalid_Composite );
694
TT_Init_Glyph_Loading( TT_Face face )
696
face->access_glyph_frame = TT_Access_Glyph_Frame;
697
face->read_glyph_header = TT_Load_Glyph_Header;
698
face->read_simple_glyph = TT_Load_Simple_Glyph;
699
face->read_composite_glyph = TT_Load_Composite_Glyph;
700
face->forget_glyph_frame = TT_Forget_Glyph_Frame;
705
tt_prepare_zone( TT_GlyphZone zone,
708
FT_UInt start_contour )
710
zone->n_points = (FT_UShort)( load->outline.n_points - start_point );
711
zone->n_contours = (FT_Short) ( load->outline.n_contours -
713
zone->org = load->extra_points + start_point;
714
zone->cur = load->outline.points + start_point;
715
zone->orus = load->extra_points2 + start_point;
716
zone->tags = (FT_Byte*)load->outline.tags + start_point;
717
zone->contours = (FT_UShort*)load->outline.contours + start_contour;
718
zone->first_point = (FT_UShort)start_point;
722
/*************************************************************************/
728
/* Hint the glyph using the zone prepared by the caller. Note that */
729
/* the zone is supposed to include four phantom points. */
732
TT_Hint_Glyph( TT_Loader loader,
733
FT_Bool is_composite )
735
#ifdef TT_CONFIG_OPTION_SUBPIXEL_HINTING
736
TT_Face face = (TT_Face)loader->face;
737
TT_Driver driver = (TT_Driver)FT_FACE_DRIVER( face );
740
TT_GlyphZone zone = &loader->zone;
742
#ifdef TT_USE_BYTECODE_INTERPRETER
745
FT_UNUSED( is_composite );
749
#ifdef TT_USE_BYTECODE_INTERPRETER
750
if ( loader->glyph->control_len > 0xFFFFL )
752
FT_TRACE1(( "TT_Hint_Glyph: too long instructions" ));
753
FT_TRACE1(( " (0x%lx byte) is truncated\n",
754
loader->glyph->control_len ));
756
n_ins = (FT_UInt)( loader->glyph->control_len );
758
/* save original point position in org */
760
FT_ARRAY_COPY( zone->org, zone->cur, zone->n_points );
762
/* Reset graphics state. */
763
loader->exec->GS = ((TT_Size)loader->size)->GS;
765
/* XXX: UNDOCUMENTED! Hinting instructions of a composite glyph */
766
/* completely refer to the (already) hinted subglyphs. */
769
loader->exec->metrics.x_scale = 1 << 16;
770
loader->exec->metrics.y_scale = 1 << 16;
772
FT_ARRAY_COPY( zone->orus, zone->cur, zone->n_points );
776
loader->exec->metrics.x_scale =
777
((TT_Size)loader->size)->metrics.x_scale;
778
loader->exec->metrics.y_scale =
779
((TT_Size)loader->size)->metrics.y_scale;
783
/* round phantom points */
784
zone->cur[zone->n_points - 4].x =
785
FT_PIX_ROUND( zone->cur[zone->n_points - 4].x );
786
zone->cur[zone->n_points - 3].x =
787
FT_PIX_ROUND( zone->cur[zone->n_points - 3].x );
788
zone->cur[zone->n_points - 2].y =
789
FT_PIX_ROUND( zone->cur[zone->n_points - 2].y );
790
zone->cur[zone->n_points - 1].y =
791
FT_PIX_ROUND( zone->cur[zone->n_points - 1].y );
793
#ifdef TT_USE_BYTECODE_INTERPRETER
800
FT_GlyphLoader gloader = loader->gloader;
801
FT_Outline current_outline = gloader->current.outline;
804
error = TT_Set_CodeRange( loader->exec, tt_coderange_glyph,
805
loader->exec->glyphIns, n_ins );
809
loader->exec->is_composite = is_composite;
810
loader->exec->pts = *zone;
812
debug = FT_BOOL( !( loader->load_flags & FT_LOAD_NO_SCALE ) &&
813
((TT_Size)loader->size)->debug );
815
error = TT_Run_Context( loader->exec, debug );
816
if ( error && loader->exec->pedantic_hinting )
819
/* store drop-out mode in bits 5-7; set bit 2 also as a marker */
820
current_outline.tags[0] |=
821
( loader->exec->GS.scan_type << 5 ) | FT_CURVE_TAG_HAS_SCANMODE;
826
/* save glyph phantom points */
827
loader->pp1 = zone->cur[zone->n_points - 4];
828
loader->pp2 = zone->cur[zone->n_points - 3];
829
loader->pp3 = zone->cur[zone->n_points - 2];
830
loader->pp4 = zone->cur[zone->n_points - 1];
832
#ifdef TT_CONFIG_OPTION_SUBPIXEL_HINTING
833
if ( driver->interpreter_version == TT_INTERPRETER_VERSION_38 )
835
if ( loader->exec->sph_tweak_flags & SPH_TWEAK_DEEMBOLDEN )
836
FT_Outline_EmboldenXY( &loader->gloader->current.outline, -24, 0 );
838
else if ( loader->exec->sph_tweak_flags & SPH_TWEAK_EMBOLDEN )
839
FT_Outline_EmboldenXY( &loader->gloader->current.outline, 24, 0 );
841
#endif /* TT_CONFIG_OPTION_SUBPIXEL_HINTING */
847
/*************************************************************************/
850
/* TT_Process_Simple_Glyph */
853
/* Once a simple glyph has been loaded, it needs to be processed. */
854
/* Usually, this means scaling and hinting through bytecode */
855
/* interpretation. */
858
TT_Process_Simple_Glyph( TT_Loader loader )
860
FT_GlyphLoader gloader = loader->gloader;
861
FT_Error error = FT_Err_Ok;
866
outline = &gloader->current.outline;
867
n_points = outline->n_points;
869
/* set phantom points */
871
outline->points[n_points ] = loader->pp1;
872
outline->points[n_points + 1] = loader->pp2;
873
outline->points[n_points + 2] = loader->pp3;
874
outline->points[n_points + 3] = loader->pp4;
876
outline->tags[n_points ] = 0;
877
outline->tags[n_points + 1] = 0;
878
outline->tags[n_points + 2] = 0;
879
outline->tags[n_points + 3] = 0;
883
#ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
885
if ( ((TT_Face)loader->face)->doblend )
887
/* Deltas apply to the unscaled data. */
889
FT_Memory memory = loader->face->memory;
893
error = TT_Vary_Get_Glyph_Deltas( (TT_Face)(loader->face),
900
for ( i = 0; i < n_points; ++i )
902
outline->points[i].x += deltas[i].x;
903
outline->points[i].y += deltas[i].y;
909
#endif /* TT_CONFIG_OPTION_GX_VAR_SUPPORT */
911
if ( IS_HINTED( loader->load_flags ) )
913
tt_prepare_zone( &loader->zone, &gloader->current, 0, 0 );
915
FT_ARRAY_COPY( loader->zone.orus, loader->zone.cur,
916
loader->zone.n_points + 4 );
920
#ifdef TT_CONFIG_OPTION_SUBPIXEL_HINTING
921
TT_Face face = (TT_Face)loader->face;
922
TT_Driver driver = (TT_Driver)FT_FACE_DRIVER( face );
924
FT_String* family = face->root.family_name;
925
FT_Int ppem = loader->size->metrics.x_ppem;
926
FT_String* style = face->root.style_name;
927
FT_Int x_scale_factor = 1000;
930
FT_Vector* vec = outline->points;
931
FT_Vector* limit = outline->points + n_points;
933
FT_Fixed x_scale = 0; /* pacify compiler */
934
FT_Fixed y_scale = 0;
936
FT_Bool do_scale = FALSE;
939
#ifdef TT_CONFIG_OPTION_SUBPIXEL_HINTING
941
if ( driver->interpreter_version == TT_INTERPRETER_VERSION_38 )
943
/* scale, but only if enabled and only if TT hinting is being used */
944
if ( IS_HINTED( loader->load_flags ) )
945
x_scale_factor = sph_test_tweak_x_scaling( face,
949
loader->glyph_index );
950
/* scale the glyph */
951
if ( ( loader->load_flags & FT_LOAD_NO_SCALE ) == 0 ||
952
x_scale_factor != 1000 )
954
x_scale = FT_MulDiv( ((TT_Size)loader->size)->metrics.x_scale,
955
x_scale_factor, 1000 );
956
y_scale = ((TT_Size)loader->size)->metrics.y_scale;
958
/* compensate for any scaling by de/emboldening; */
959
/* the amount was determined via experimentation */
960
if ( x_scale_factor != 1000 && ppem > 11 )
961
FT_Outline_EmboldenXY( outline,
962
FT_MulFix( 1280 * ppem,
963
1000 - x_scale_factor ),
970
#endif /* TT_CONFIG_OPTION_SUBPIXEL_HINTING */
973
/* scale the glyph */
974
if ( ( loader->load_flags & FT_LOAD_NO_SCALE ) == 0 )
976
x_scale = ((TT_Size)loader->size)->metrics.x_scale;
977
y_scale = ((TT_Size)loader->size)->metrics.y_scale;
985
for ( ; vec < limit; vec++ )
987
vec->x = FT_MulFix( vec->x, x_scale );
988
vec->y = FT_MulFix( vec->y, y_scale );
991
loader->pp1 = outline->points[n_points - 4];
992
loader->pp2 = outline->points[n_points - 3];
993
loader->pp3 = outline->points[n_points - 2];
994
loader->pp4 = outline->points[n_points - 1];
998
if ( IS_HINTED( loader->load_flags ) )
1000
loader->zone.n_points += 4;
1002
error = TT_Hint_Glyph( loader, 0 );
1009
/*************************************************************************/
1012
/* TT_Process_Composite_Component */
1015
/* Once a composite component has been loaded, it needs to be */
1016
/* processed. Usually, this means transforming and translating. */
1019
TT_Process_Composite_Component( TT_Loader loader,
1020
FT_SubGlyph subglyph,
1021
FT_UInt start_point,
1022
FT_UInt num_base_points )
1024
FT_GlyphLoader gloader = loader->gloader;
1025
FT_Vector* base_vec = gloader->base.outline.points;
1026
FT_UInt num_points = gloader->base.outline.n_points;
1031
have_scale = FT_BOOL( subglyph->flags & ( WE_HAVE_A_SCALE |
1032
WE_HAVE_AN_XY_SCALE |
1035
/* perform the transform required for this subglyph */
1041
for ( i = num_base_points; i < num_points; i++ )
1042
FT_Vector_Transform( base_vec + i, &subglyph->transform );
1046
if ( !( subglyph->flags & ARGS_ARE_XY_VALUES ) )
1048
FT_UInt k = subglyph->arg1;
1049
FT_UInt l = subglyph->arg2;
1054
/* match l-th point of the newly loaded component to the k-th point */
1055
/* of the previously loaded components. */
1057
/* change to the point numbers used by our outline */
1059
l += num_base_points;
1060
if ( k >= num_base_points ||
1062
return FT_THROW( Invalid_Composite );
1064
p1 = gloader->base.outline.points + k;
1065
p2 = gloader->base.outline.points + l;
1078
/* Use a default value dependent on */
1079
/* TT_CONFIG_OPTION_COMPONENT_OFFSET_SCALED. This is useful for old */
1080
/* TT fonts which don't set the xxx_COMPONENT_OFFSET bit. */
1083
#ifdef TT_CONFIG_OPTION_COMPONENT_OFFSET_SCALED
1084
!( subglyph->flags & UNSCALED_COMPONENT_OFFSET ) )
1086
( subglyph->flags & SCALED_COMPONENT_OFFSET ) )
1092
/*******************************************************************/
1094
/* This algorithm is what Apple documents. But it doesn't work. */
1096
int a = subglyph->transform.xx > 0 ? subglyph->transform.xx
1097
: -subglyph->transform.xx;
1098
int b = subglyph->transform.yx > 0 ? subglyph->transform.yx
1099
: -subglyph->transform.yx;
1100
int c = subglyph->transform.xy > 0 ? subglyph->transform.xy
1101
: -subglyph->transform.xy;
1102
int d = subglyph->transform.yy > 0 ? subglyph->transform.yy
1103
: -subglyph->transform.yy;
1104
int m = a > b ? a : b;
1105
int n = c > d ? c : d;
1108
if ( a - b <= 33 && a - b >= -33 )
1110
if ( c - d <= 33 && c - d >= -33 )
1112
x = FT_MulFix( x, m );
1113
y = FT_MulFix( y, n );
1117
/*******************************************************************/
1119
/* This algorithm is a guess and works much better than the above. */
1121
FT_Fixed mac_xscale = FT_Hypot( subglyph->transform.xx,
1122
subglyph->transform.xy );
1123
FT_Fixed mac_yscale = FT_Hypot( subglyph->transform.yy,
1124
subglyph->transform.yx );
1127
x = FT_MulFix( x, mac_xscale );
1128
y = FT_MulFix( y, mac_yscale );
1134
if ( !( loader->load_flags & FT_LOAD_NO_SCALE ) )
1136
FT_Fixed x_scale = ((TT_Size)loader->size)->metrics.x_scale;
1137
FT_Fixed y_scale = ((TT_Size)loader->size)->metrics.y_scale;
1140
x = FT_MulFix( x, x_scale );
1141
y = FT_MulFix( y, y_scale );
1143
if ( subglyph->flags & ROUND_XY_TO_GRID )
1145
x = FT_PIX_ROUND( x );
1146
y = FT_PIX_ROUND( y );
1152
translate_array( num_points - num_base_points,
1153
base_vec + num_base_points,
1160
/*************************************************************************/
1163
/* TT_Process_Composite_Glyph */
1166
/* This is slightly different from TT_Process_Simple_Glyph, in that */
1167
/* its sole purpose is to hint the glyph. Thus this function is */
1168
/* only available when bytecode interpreter is enabled. */
1171
TT_Process_Composite_Glyph( TT_Loader loader,
1172
FT_UInt start_point,
1173
FT_UInt start_contour )
1176
FT_Outline* outline;
1180
outline = &loader->gloader->base.outline;
1182
/* make room for phantom points */
1183
error = FT_GLYPHLOADER_CHECK_POINTS( loader->gloader,
1184
outline->n_points + 4,
1189
outline->points[outline->n_points ] = loader->pp1;
1190
outline->points[outline->n_points + 1] = loader->pp2;
1191
outline->points[outline->n_points + 2] = loader->pp3;
1192
outline->points[outline->n_points + 3] = loader->pp4;
1194
outline->tags[outline->n_points ] = 0;
1195
outline->tags[outline->n_points + 1] = 0;
1196
outline->tags[outline->n_points + 2] = 0;
1197
outline->tags[outline->n_points + 3] = 0;
1199
#ifdef TT_USE_BYTECODE_INTERPRETER
1202
FT_Stream stream = loader->stream;
1203
FT_UShort n_ins, max_ins;
1207
/* TT_Load_Composite_Glyph only gives us the offset of instructions */
1208
/* so we read them here */
1209
if ( FT_STREAM_SEEK( loader->ins_pos ) ||
1210
FT_READ_USHORT( n_ins ) )
1213
FT_TRACE5(( " Instructions size = %d\n", n_ins ));
1216
max_ins = ((TT_Face)loader->face)->max_profile.maxSizeOfInstructions;
1217
if ( n_ins > max_ins )
1219
/* don't trust `maxSizeOfInstructions'; */
1220
/* only do a rough safety check */
1221
if ( (FT_Int)n_ins > loader->byte_len )
1223
FT_TRACE1(( "TT_Process_Composite_Glyph:"
1224
" too many instructions (%d) for glyph with length %d\n",
1225
n_ins, loader->byte_len ));
1226
return FT_THROW( Too_Many_Hints );
1229
tmp = loader->exec->glyphSize;
1230
error = Update_Max( loader->exec->memory,
1233
(void*)&loader->exec->glyphIns,
1236
loader->exec->glyphSize = (FT_UShort)tmp;
1240
else if ( n_ins == 0 )
1243
if ( FT_STREAM_READ( loader->exec->glyphIns, n_ins ) )
1246
loader->glyph->control_data = loader->exec->glyphIns;
1247
loader->glyph->control_len = n_ins;
1252
tt_prepare_zone( &loader->zone, &loader->gloader->base,
1253
start_point, start_contour );
1255
/* Some points are likely touched during execution of */
1256
/* instructions on components. So let's untouch them. */
1257
for ( i = 0; i < loader->zone.n_points; i++ )
1258
loader->zone.tags[i] &= ~FT_CURVE_TAG_TOUCH_BOTH;
1260
loader->zone.n_points += 4;
1262
return TT_Hint_Glyph( loader, 1 );
1267
* Calculate the phantom points
1269
* Defining the right side bearing (rsb) as
1271
* rsb = aw - (lsb + xmax - xmin)
1273
* (with `aw' the advance width, `lsb' the left side bearing, and `xmin'
1274
* and `xmax' the glyph's minimum and maximum x value), the OpenType
1275
* specification defines the initial position of horizontal phantom points
1278
* pp1 = (round(xmin - lsb), 0) ,
1279
* pp2 = (round(pp1 + aw), 0) .
1281
* Note that the rounding to the grid (in the device space) is not
1282
* documented currently in the specification.
1284
* However, the specification lacks the precise definition of vertical
1285
* phantom points. Greg Hitchcock provided the following explanation.
1287
* - a `vmtx' table is present
1289
* For any glyph, the minimum and maximum y values (`ymin' and `ymax')
1290
* are given in the `glyf' table, the top side bearing (tsb) and advance
1291
* height (ah) are given in the `vmtx' table. The bottom side bearing
1292
* (bsb) is then calculated as
1294
* bsb = ah - (tsb + ymax - ymin) ,
1296
* and the initial position of vertical phantom points is
1298
* pp3 = (x, round(ymax + tsb)) ,
1299
* pp4 = (x, round(pp3 - ah)) .
1301
* See below for value `x'.
1303
* - no `vmtx' table in the font
1305
* If there is an `OS/2' table, we set
1307
* DefaultAscender = sTypoAscender ,
1308
* DefaultDescender = sTypoDescender ,
1310
* otherwise we use data from the `hhea' table:
1312
* DefaultAscender = Ascender ,
1313
* DefaultDescender = Descender .
1315
* With these two variables we can now set
1317
* ah = DefaultAscender - sDefaultDescender ,
1318
* tsb = DefaultAscender - yMax ,
1320
* and proceed as if a `vmtx' table was present.
1326
* but there is one compatibility case where it can be set to
1328
* x = -DefaultDescender -
1329
* ((DefaultAscender - DefaultDescender - aw) / 2) . (2)
1331
* and another one with
1335
* In Windows, the history of those values is quite complicated,
1336
* depending on the hinting engine (that is, the graphics framework).
1338
* framework from to formula
1339
* ----------------------------------------------------------
1340
* GDI Windows 98 current (1)
1341
* (Windows 2000 for NT)
1342
* GDI+ Windows XP Windows 7 (2)
1343
* GDI+ Windows 8 current (3)
1344
* DWrite Windows 7 current (3)
1346
* For simplicity, FreeType uses (1) for grayscale subpixel hinting and
1347
* (3) for everything else.
1350
#ifdef TT_CONFIG_OPTION_SUBPIXEL_HINTING
1352
#define TT_LOADER_SET_PP( loader ) \
1355
FT_Bool subpixel_ = loader->exec ? loader->exec->subpixel \
1357
FT_Bool grayscale_ = loader->exec ? loader->exec->grayscale \
1359
FT_Bool use_aw_2_ = (FT_Bool)( subpixel_ && grayscale_ ); \
1362
(loader)->pp1.x = (loader)->bbox.xMin - (loader)->left_bearing; \
1363
(loader)->pp1.y = 0; \
1364
(loader)->pp2.x = (loader)->pp1.x + (loader)->advance; \
1365
(loader)->pp2.y = 0; \
1367
(loader)->pp3.x = use_aw_2_ ? (loader)->advance / 2 : 0; \
1368
(loader)->pp3.y = (loader)->bbox.yMax + (loader)->top_bearing; \
1369
(loader)->pp4.x = use_aw_2_ ? (loader)->advance / 2 : 0; \
1370
(loader)->pp4.y = (loader)->pp3.y - (loader)->vadvance; \
1373
#else /* !TT_CONFIG_OPTION_SUBPIXEL_HINTING */
1375
#define TT_LOADER_SET_PP( loader ) \
1378
(loader)->pp1.x = (loader)->bbox.xMin - (loader)->left_bearing; \
1379
(loader)->pp1.y = 0; \
1380
(loader)->pp2.x = (loader)->pp1.x + (loader)->advance; \
1381
(loader)->pp2.y = 0; \
1383
(loader)->pp3.x = 0; \
1384
(loader)->pp3.y = (loader)->bbox.yMax + (loader)->top_bearing; \
1385
(loader)->pp4.x = 0; \
1386
(loader)->pp4.y = (loader)->pp3.y - (loader)->vadvance; \
1389
#endif /* !TT_CONFIG_OPTION_SUBPIXEL_HINTING */
1392
/*************************************************************************/
1395
/* load_truetype_glyph */
1398
/* Loads a given truetype glyph. Handles composites and uses a */
1399
/* TT_Loader object. */
1402
load_truetype_glyph( TT_Loader loader,
1403
FT_UInt glyph_index,
1404
FT_UInt recurse_count,
1405
FT_Bool header_only )
1407
FT_Error error = FT_Err_Ok;
1408
FT_Fixed x_scale, y_scale;
1410
TT_Face face = (TT_Face)loader->face;
1411
FT_GlyphLoader gloader = loader->gloader;
1412
FT_Bool opened_frame = 0;
1414
#ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
1415
FT_Vector* deltas = NULL;
1418
#ifdef FT_CONFIG_OPTION_INCREMENTAL
1419
FT_StreamRec inc_stream;
1421
FT_Bool glyph_data_loaded = 0;
1425
/* some fonts have an incorrect value of `maxComponentDepth', */
1426
/* thus we allow depth 1 to catch the majority of them */
1427
if ( recurse_count > 1 &&
1428
recurse_count > face->max_profile.maxComponentDepth )
1430
error = FT_THROW( Invalid_Composite );
1434
/* check glyph index */
1435
if ( glyph_index >= (FT_UInt)face->root.num_glyphs )
1437
error = FT_THROW( Invalid_Glyph_Index );
1441
loader->glyph_index = glyph_index;
1443
if ( ( loader->load_flags & FT_LOAD_NO_SCALE ) == 0 )
1445
x_scale = ((TT_Size)loader->size)->metrics.x_scale;
1446
y_scale = ((TT_Size)loader->size)->metrics.y_scale;
1454
/* Set `offset' to the start of the glyph relative to the start of */
1455
/* the `glyf' table, and `byte_len' to the length of the glyph in */
1458
#ifdef FT_CONFIG_OPTION_INCREMENTAL
1460
/* If we are loading glyph data via the incremental interface, set */
1461
/* the loader stream to a memory stream reading the data returned */
1462
/* by the interface. */
1463
if ( face->root.internal->incremental_interface )
1465
error = face->root.internal->incremental_interface->funcs->get_glyph_data(
1466
face->root.internal->incremental_interface->object,
1467
glyph_index, &glyph_data );
1471
glyph_data_loaded = 1;
1473
loader->byte_len = glyph_data.length;
1475
FT_MEM_ZERO( &inc_stream, sizeof ( inc_stream ) );
1476
FT_Stream_OpenMemory( &inc_stream,
1477
glyph_data.pointer, glyph_data.length );
1479
loader->stream = &inc_stream;
1483
#endif /* FT_CONFIG_OPTION_INCREMENTAL */
1485
offset = tt_face_get_location( face, glyph_index,
1486
(FT_UInt*)&loader->byte_len );
1488
if ( loader->byte_len > 0 )
1490
#ifdef FT_CONFIG_OPTION_INCREMENTAL
1491
/* for the incremental interface, `glyf_offset' is always zero */
1492
if ( !loader->glyf_offset &&
1493
!face->root.internal->incremental_interface )
1495
if ( !loader->glyf_offset )
1496
#endif /* FT_CONFIG_OPTION_INCREMENTAL */
1498
FT_TRACE2(( "no `glyf' table but non-zero `loca' entry\n" ));
1499
error = FT_THROW( Invalid_Table );
1503
error = face->access_glyph_frame( loader, glyph_index,
1504
loader->glyf_offset + offset,
1511
/* read glyph header first */
1512
error = face->read_glyph_header( loader );
1516
/* the metrics must be computed after loading the glyph header */
1517
/* since we need the glyph's `yMax' value in case the vertical */
1518
/* metrics must be emulated */
1519
error = tt_get_metrics( loader, glyph_index );
1527
if ( loader->byte_len == 0 || loader->n_contours == 0 )
1529
loader->bbox.xMin = 0;
1530
loader->bbox.xMax = 0;
1531
loader->bbox.yMin = 0;
1532
loader->bbox.yMax = 0;
1534
error = tt_get_metrics( loader, glyph_index );
1541
/* must initialize points before (possibly) overriding */
1542
/* glyph metrics from the incremental interface */
1543
TT_LOADER_SET_PP( loader );
1545
#ifdef FT_CONFIG_OPTION_INCREMENTAL
1546
tt_get_metrics_incr_overrides( loader, glyph_index );
1549
#ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
1551
if ( ((TT_Face)(loader->face))->doblend )
1553
/* this must be done before scaling */
1554
FT_Memory memory = loader->face->memory;
1557
error = TT_Vary_Get_Glyph_Deltas( (TT_Face)(loader->face),
1558
glyph_index, &deltas, 4 );
1562
loader->pp1.x += deltas[0].x;
1563
loader->pp1.y += deltas[0].y;
1564
loader->pp2.x += deltas[1].x;
1565
loader->pp2.y += deltas[1].y;
1567
loader->pp3.x += deltas[2].x;
1568
loader->pp3.y += deltas[2].y;
1569
loader->pp4.x += deltas[3].x;
1570
loader->pp4.y += deltas[3].y;
1575
#endif /* TT_CONFIG_OPTION_GX_VAR_SUPPORT */
1577
/* scale phantom points, if necessary; */
1578
/* they get rounded in `TT_Hint_Glyph' */
1579
if ( ( loader->load_flags & FT_LOAD_NO_SCALE ) == 0 )
1581
loader->pp1.x = FT_MulFix( loader->pp1.x, x_scale );
1582
loader->pp2.x = FT_MulFix( loader->pp2.x, x_scale );
1583
/* pp1.y and pp2.y are always zero */
1585
loader->pp3.x = FT_MulFix( loader->pp3.x, x_scale );
1586
loader->pp3.y = FT_MulFix( loader->pp3.y, y_scale );
1587
loader->pp4.x = FT_MulFix( loader->pp4.x, x_scale );
1588
loader->pp4.y = FT_MulFix( loader->pp4.y, y_scale );
1595
/* must initialize phantom points before (possibly) overriding */
1596
/* glyph metrics from the incremental interface */
1597
TT_LOADER_SET_PP( loader );
1599
#ifdef FT_CONFIG_OPTION_INCREMENTAL
1600
tt_get_metrics_incr_overrides( loader, glyph_index );
1603
/***********************************************************************/
1604
/***********************************************************************/
1605
/***********************************************************************/
1607
/* if it is a simple glyph, load it */
1609
if ( loader->n_contours > 0 )
1611
error = face->read_simple_glyph( loader );
1615
/* all data have been read */
1616
face->forget_glyph_frame( loader );
1619
error = TT_Process_Simple_Glyph( loader );
1623
FT_GlyphLoader_Add( gloader );
1626
/***********************************************************************/
1627
/***********************************************************************/
1628
/***********************************************************************/
1630
/* otherwise, load a composite! */
1631
else if ( loader->n_contours == -1 )
1633
FT_UInt start_point;
1634
FT_UInt start_contour;
1635
FT_ULong ins_pos; /* position of composite instructions, if any */
1638
start_point = gloader->base.outline.n_points;
1639
start_contour = gloader->base.outline.n_contours;
1641
/* for each subglyph, read composite header */
1642
error = face->read_composite_glyph( loader );
1646
/* store the offset of instructions */
1647
ins_pos = loader->ins_pos;
1649
/* all data we need are read */
1650
face->forget_glyph_frame( loader );
1653
#ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
1655
if ( face->doblend )
1658
FT_SubGlyph subglyph;
1659
FT_Memory memory = face->root.memory;
1662
/* this provides additional offsets */
1663
/* for each component's translation */
1665
if ( ( error = TT_Vary_Get_Glyph_Deltas(
1669
gloader->current.num_subglyphs + 4 ) ) != 0 )
1672
subglyph = gloader->current.subglyphs + gloader->base.num_subglyphs;
1673
limit = gloader->current.num_subglyphs;
1675
for ( i = 0; i < limit; ++i, ++subglyph )
1677
if ( subglyph->flags & ARGS_ARE_XY_VALUES )
1679
/* XXX: overflow check for subglyph->{arg1,arg2}. */
1680
/* deltas[i].{x,y} must be within signed 16-bit, */
1681
/* but the restriction of summed delta is not clear */
1682
subglyph->arg1 += (FT_Int16)deltas[i].x;
1683
subglyph->arg2 += (FT_Int16)deltas[i].y;
1687
loader->pp1.x += deltas[i + 0].x;
1688
loader->pp1.y += deltas[i + 0].y;
1689
loader->pp2.x += deltas[i + 1].x;
1690
loader->pp2.y += deltas[i + 1].y;
1692
loader->pp3.x += deltas[i + 2].x;
1693
loader->pp3.y += deltas[i + 2].y;
1694
loader->pp4.x += deltas[i + 3].x;
1695
loader->pp4.y += deltas[i + 3].y;
1700
#endif /* TT_CONFIG_OPTION_GX_VAR_SUPPORT */
1702
/* scale phantom points, if necessary; */
1703
/* they get rounded in `TT_Hint_Glyph' */
1704
if ( ( loader->load_flags & FT_LOAD_NO_SCALE ) == 0 )
1706
loader->pp1.x = FT_MulFix( loader->pp1.x, x_scale );
1707
loader->pp2.x = FT_MulFix( loader->pp2.x, x_scale );
1708
/* pp1.y and pp2.y are always zero */
1710
loader->pp3.x = FT_MulFix( loader->pp3.x, x_scale );
1711
loader->pp3.y = FT_MulFix( loader->pp3.y, y_scale );
1712
loader->pp4.x = FT_MulFix( loader->pp4.x, x_scale );
1713
loader->pp4.y = FT_MulFix( loader->pp4.y, y_scale );
1716
/* if the flag FT_LOAD_NO_RECURSE is set, we return the subglyph */
1717
/* `as is' in the glyph slot (the client application will be */
1718
/* responsible for interpreting these data)... */
1719
if ( loader->load_flags & FT_LOAD_NO_RECURSE )
1721
FT_GlyphLoader_Add( gloader );
1722
loader->glyph->format = FT_GLYPH_FORMAT_COMPOSITE;
1727
/*********************************************************************/
1728
/*********************************************************************/
1729
/*********************************************************************/
1732
FT_UInt n, num_base_points;
1733
FT_SubGlyph subglyph = 0;
1735
FT_UInt num_points = start_point;
1736
FT_UInt num_subglyphs = gloader->current.num_subglyphs;
1737
FT_UInt num_base_subgs = gloader->base.num_subglyphs;
1739
FT_Stream old_stream = loader->stream;
1740
FT_Int old_byte_len = loader->byte_len;
1743
FT_GlyphLoader_Add( gloader );
1745
/* read each subglyph independently */
1746
for ( n = 0; n < num_subglyphs; n++ )
1751
/* Each time we call load_truetype_glyph in this loop, the */
1752
/* value of `gloader.base.subglyphs' can change due to table */
1753
/* reallocations. We thus need to recompute the subglyph */
1754
/* pointer on each iteration. */
1755
subglyph = gloader->base.subglyphs + num_base_subgs + n;
1757
pp[0] = loader->pp1;
1758
pp[1] = loader->pp2;
1759
pp[2] = loader->pp3;
1760
pp[3] = loader->pp4;
1762
num_base_points = gloader->base.outline.n_points;
1764
error = load_truetype_glyph( loader, subglyph->index,
1765
recurse_count + 1, FALSE );
1769
/* restore subglyph pointer */
1770
subglyph = gloader->base.subglyphs + num_base_subgs + n;
1772
/* restore phantom points if necessary */
1773
if ( !( subglyph->flags & USE_MY_METRICS ) )
1775
loader->pp1 = pp[0];
1776
loader->pp2 = pp[1];
1777
loader->pp3 = pp[2];
1778
loader->pp4 = pp[3];
1781
num_points = gloader->base.outline.n_points;
1783
if ( num_points == num_base_points )
1786
/* gloader->base.outline consists of three parts: */
1787
/* 0 -(1)-> start_point -(2)-> num_base_points -(3)-> n_points. */
1789
/* (1): exists from the beginning */
1790
/* (2): components that have been loaded so far */
1791
/* (3): the newly loaded component */
1792
TT_Process_Composite_Component( loader, subglyph, start_point,
1796
loader->stream = old_stream;
1797
loader->byte_len = old_byte_len;
1799
/* process the glyph */
1800
loader->ins_pos = ins_pos;
1801
if ( IS_HINTED( loader->load_flags ) &&
1803
#ifdef TT_USE_BYTECODE_INTERPRETER
1805
subglyph->flags & WE_HAVE_INSTR &&
1809
num_points > start_point )
1810
TT_Process_Composite_Glyph( loader, start_point, start_contour );
1816
/* invalid composite count (negative but not -1) */
1817
error = FT_THROW( Invalid_Outline );
1821
/***********************************************************************/
1822
/***********************************************************************/
1823
/***********************************************************************/
1828
face->forget_glyph_frame( loader );
1830
#ifdef FT_CONFIG_OPTION_INCREMENTAL
1832
if ( glyph_data_loaded )
1833
face->root.internal->incremental_interface->funcs->free_glyph_data(
1834
face->root.internal->incremental_interface->object,
1844
compute_glyph_metrics( TT_Loader loader,
1845
FT_UInt glyph_index )
1847
TT_Face face = (TT_Face)loader->face;
1848
#ifdef TT_CONFIG_OPTION_SUBPIXEL_HINTING
1849
TT_Driver driver = (TT_Driver)FT_FACE_DRIVER( face );
1854
TT_GlyphSlot glyph = loader->glyph;
1855
TT_Size size = (TT_Size)loader->size;
1859
if ( ( loader->load_flags & FT_LOAD_NO_SCALE ) == 0 )
1860
y_scale = size->root.metrics.y_scale;
1862
if ( glyph->format != FT_GLYPH_FORMAT_COMPOSITE )
1863
FT_Outline_Get_CBox( &glyph->outline, &bbox );
1865
bbox = loader->bbox;
1867
/* get the device-independent horizontal advance; it is scaled later */
1868
/* by the base layer. */
1869
glyph->linearHoriAdvance = loader->linear;
1871
glyph->metrics.horiBearingX = bbox.xMin;
1872
glyph->metrics.horiBearingY = bbox.yMax;
1873
glyph->metrics.horiAdvance = loader->pp2.x - loader->pp1.x;
1875
/* adjust advance width to the value contained in the hdmx table */
1876
if ( !face->postscript.isFixedPitch &&
1877
IS_HINTED( loader->load_flags ) )
1882
widthp = tt_face_get_device_metrics( face,
1883
size->root.metrics.x_ppem,
1886
#ifdef TT_CONFIG_OPTION_SUBPIXEL_HINTING
1888
if ( driver->interpreter_version == TT_INTERPRETER_VERSION_38 )
1890
FT_Bool ignore_x_mode;
1893
ignore_x_mode = FT_BOOL( FT_LOAD_TARGET_MODE( loader->load_flags ) !=
1894
FT_RENDER_MODE_MONO );
1897
( ( ignore_x_mode && loader->exec->compatible_widths ) ||
1899
SPH_OPTION_BITMAP_WIDTHS ) )
1900
glyph->metrics.horiAdvance = *widthp << 6;
1904
#endif /* TT_CONFIG_OPTION_SUBPIXEL_HINTING */
1908
glyph->metrics.horiAdvance = *widthp << 6;
1912
/* set glyph dimensions */
1913
glyph->metrics.width = bbox.xMax - bbox.xMin;
1914
glyph->metrics.height = bbox.yMax - bbox.yMin;
1916
/* Now take care of vertical metrics. In the case where there is */
1917
/* no vertical information within the font (relatively common), */
1918
/* create some metrics manually */
1920
FT_Pos top; /* scaled vertical top side bearing */
1921
FT_Pos advance; /* scaled vertical advance height */
1924
/* Get the unscaled top bearing and advance height. */
1925
if ( face->vertical_info &&
1926
face->vertical.number_Of_VMetrics > 0 )
1928
top = (FT_Short)FT_DivFix( loader->pp3.y - bbox.yMax,
1931
if ( loader->pp3.y <= loader->pp4.y )
1934
advance = (FT_UShort)FT_DivFix( loader->pp3.y - loader->pp4.y,
1942
/* XXX Compute top side bearing and advance height in */
1943
/* Get_VMetrics instead of here. */
1945
/* NOTE: The OS/2 values are the only `portable' ones, */
1946
/* which is why we use them, if there is an OS/2 */
1947
/* table in the font. Otherwise, we use the */
1948
/* values defined in the horizontal header. */
1950
height = (FT_Short)FT_DivFix( bbox.yMax - bbox.yMin,
1952
if ( face->os2.version != 0xFFFFU )
1953
advance = (FT_Pos)( face->os2.sTypoAscender -
1954
face->os2.sTypoDescender );
1956
advance = (FT_Pos)( face->horizontal.Ascender -
1957
face->horizontal.Descender );
1959
top = ( advance - height ) / 2;
1962
#ifdef FT_CONFIG_OPTION_INCREMENTAL
1964
FT_Incremental_InterfaceRec* incr;
1965
FT_Incremental_MetricsRec metrics;
1969
incr = face->root.internal->incremental_interface;
1971
/* If this is an incrementally loaded font see if there are */
1972
/* overriding metrics for this glyph. */
1973
if ( incr && incr->funcs->get_glyph_metrics )
1975
metrics.bearing_x = 0;
1976
metrics.bearing_y = top;
1977
metrics.advance = advance;
1979
error = incr->funcs->get_glyph_metrics( incr->object,
1986
top = metrics.bearing_y;
1987
advance = metrics.advance;
1991
/* GWW: Do vertical metrics get loaded incrementally too? */
1993
#endif /* FT_CONFIG_OPTION_INCREMENTAL */
1995
glyph->linearVertAdvance = advance;
1997
/* scale the metrics */
1998
if ( !( loader->load_flags & FT_LOAD_NO_SCALE ) )
2000
top = FT_MulFix( top, y_scale );
2001
advance = FT_MulFix( advance, y_scale );
2004
/* XXX: for now, we have no better algorithm for the lsb, but it */
2005
/* should work fine. */
2007
glyph->metrics.vertBearingX = glyph->metrics.horiBearingX -
2008
glyph->metrics.horiAdvance / 2;
2009
glyph->metrics.vertBearingY = top;
2010
glyph->metrics.vertAdvance = advance;
2017
#ifdef TT_CONFIG_OPTION_EMBEDDED_BITMAPS
2020
load_sbit_image( TT_Size size,
2022
FT_UInt glyph_index,
2023
FT_Int32 load_flags )
2029
TT_SBit_MetricsRec metrics;
2032
face = (TT_Face)glyph->face;
2033
sfnt = (SFNT_Service)face->sfnt;
2034
stream = face->root.stream;
2036
error = sfnt->load_sbit_image( face,
2045
glyph->outline.n_points = 0;
2046
glyph->outline.n_contours = 0;
2048
glyph->metrics.width = (FT_Pos)metrics.width << 6;
2049
glyph->metrics.height = (FT_Pos)metrics.height << 6;
2051
glyph->metrics.horiBearingX = (FT_Pos)metrics.horiBearingX << 6;
2052
glyph->metrics.horiBearingY = (FT_Pos)metrics.horiBearingY << 6;
2053
glyph->metrics.horiAdvance = (FT_Pos)metrics.horiAdvance << 6;
2055
glyph->metrics.vertBearingX = (FT_Pos)metrics.vertBearingX << 6;
2056
glyph->metrics.vertBearingY = (FT_Pos)metrics.vertBearingY << 6;
2057
glyph->metrics.vertAdvance = (FT_Pos)metrics.vertAdvance << 6;
2059
glyph->format = FT_GLYPH_FORMAT_BITMAP;
2061
if ( load_flags & FT_LOAD_VERTICAL_LAYOUT )
2063
glyph->bitmap_left = metrics.vertBearingX;
2064
glyph->bitmap_top = metrics.vertBearingY;
2068
glyph->bitmap_left = metrics.horiBearingX;
2069
glyph->bitmap_top = metrics.horiBearingY;
2076
#endif /* TT_CONFIG_OPTION_EMBEDDED_BITMAPS */
2080
tt_loader_init( TT_Loader loader,
2083
FT_Int32 load_flags,
2084
FT_Bool glyf_table_only )
2088
#ifdef TT_USE_BYTECODE_INTERPRETER
2089
FT_Bool pedantic = FT_BOOL( load_flags & FT_LOAD_PEDANTIC );
2093
face = (TT_Face)glyph->face;
2094
stream = face->root.stream;
2096
FT_MEM_ZERO( loader, sizeof ( TT_LoaderRec ) );
2098
#ifdef TT_USE_BYTECODE_INTERPRETER
2100
/* load execution context */
2101
if ( IS_HINTED( load_flags ) && !glyf_table_only )
2103
TT_ExecContext exec;
2106
#ifdef TT_CONFIG_OPTION_SUBPIXEL_HINTING
2107
TT_Driver driver = (TT_Driver)FT_FACE_DRIVER( face );
2109
FT_Bool subpixel = FALSE;
2113
FT_Bool compatible_widths;
2114
FT_Bool symmetrical_smoothing;
2116
FT_Bool subpixel_positioned;
2118
#endif /* TT_CONFIG_OPTION_SUBPIXEL_HINTING */
2120
FT_Bool reexecute = FALSE;
2123
if ( !size->cvt_ready )
2125
FT_Error error = tt_size_ready_bytecode( size, pedantic );
2132
/* query new execution context */
2133
exec = size->debug ? size->context
2134
: ( (TT_Driver)FT_FACE_DRIVER( face ) )->context;
2136
return FT_THROW( Could_Not_Find_Context );
2138
#ifdef TT_CONFIG_OPTION_SUBPIXEL_HINTING
2140
if ( driver->interpreter_version == TT_INTERPRETER_VERSION_38 )
2142
subpixel = FT_BOOL( ( FT_LOAD_TARGET_MODE( load_flags ) !=
2143
FT_RENDER_MODE_MONO ) &&
2144
SPH_OPTION_SET_SUBPIXEL );
2148
else if ( SPH_OPTION_SET_GRAYSCALE )
2156
if ( FT_IS_TRICKY( glyph->face ) )
2159
exec->ignore_x_mode = subpixel || grayscale;
2160
exec->rasterizer_version = SPH_OPTION_SET_RASTERIZER_VERSION;
2161
if ( exec->sph_tweak_flags & SPH_TWEAK_RASTERIZER_35 )
2162
exec->rasterizer_version = TT_INTERPRETER_VERSION_35;
2165
exec->compatible_widths = SPH_OPTION_SET_COMPATIBLE_WIDTHS;
2166
exec->symmetrical_smoothing = FALSE;
2168
exec->subpixel_positioned = TRUE;
2170
exec->compatible_widths =
2171
FT_BOOL( FT_LOAD_TARGET_MODE( load_flags ) !=
2172
TT_LOAD_COMPATIBLE_WIDTHS );
2173
exec->symmetrical_smoothing =
2174
FT_BOOL( FT_LOAD_TARGET_MODE( load_flags ) !=
2175
TT_LOAD_SYMMETRICAL_SMOOTHING );
2177
FT_BOOL( FT_LOAD_TARGET_MODE( load_flags ) !=
2179
exec->subpixel_positioned =
2180
FT_BOOL( FT_LOAD_TARGET_MODE( load_flags ) !=
2181
TT_LOAD_SUBPIXEL_POSITIONED );
2187
#endif /* TT_CONFIG_OPTION_SUBPIXEL_HINTING */
2190
grayscale = FT_BOOL( FT_LOAD_TARGET_MODE( load_flags ) !=
2191
FT_RENDER_MODE_MONO );
2194
TT_Load_Context( exec, face, size );
2196
#ifdef TT_CONFIG_OPTION_SUBPIXEL_HINTING
2198
if ( driver->interpreter_version == TT_INTERPRETER_VERSION_38 )
2200
/* a change from mono to subpixel rendering (and vice versa) */
2201
/* requires a re-execution of the CVT program */
2202
if ( subpixel != exec->subpixel )
2204
FT_TRACE4(( "tt_loader_init: subpixel hinting change,"
2205
" re-executing `prep' table\n" ));
2207
exec->subpixel = subpixel;
2211
/* a change from mono to grayscale rendering (and vice versa) */
2212
/* requires a re-execution of the CVT program */
2213
if ( grayscale != exec->grayscale )
2215
FT_TRACE4(( "tt_loader_init: grayscale hinting change,"
2216
" re-executing `prep' table\n" ));
2218
exec->grayscale = grayscale;
2224
#endif /* TT_CONFIG_OPTION_SUBPIXEL_HINTING */
2227
/* a change from mono to grayscale rendering (and vice versa) */
2228
/* requires a re-execution of the CVT program */
2229
if ( grayscale != exec->grayscale )
2231
FT_TRACE4(( "tt_loader_init: grayscale change,"
2232
" re-executing `prep' table\n" ));
2234
exec->grayscale = grayscale;
2244
for ( i = 0; i < size->cvt_size; i++ )
2245
size->cvt[i] = FT_MulFix( face->cvt[i], size->ttmetrics.scale );
2246
tt_size_run_prep( size, pedantic );
2249
/* see whether the cvt program has disabled hinting */
2250
if ( exec->GS.instruct_control & 1 )
2251
load_flags |= FT_LOAD_NO_HINTING;
2253
/* load default graphics state -- if needed */
2254
if ( exec->GS.instruct_control & 2 )
2255
exec->GS = tt_default_graphics_state;
2257
exec->pedantic_hinting = FT_BOOL( load_flags & FT_LOAD_PEDANTIC );
2258
loader->exec = exec;
2259
loader->instructions = exec->glyphIns;
2262
#endif /* TT_USE_BYTECODE_INTERPRETER */
2264
/* seek to the beginning of the glyph table -- for Type 42 fonts */
2265
/* the table might be accessed from a Postscript stream or something */
2268
#ifdef FT_CONFIG_OPTION_INCREMENTAL
2270
if ( face->root.internal->incremental_interface )
2271
loader->glyf_offset = 0;
2277
FT_Error error = face->goto_table( face, TTAG_glyf, stream, 0 );
2280
if ( FT_ERR_EQ( error, Table_Missing ) )
2281
loader->glyf_offset = 0;
2284
FT_ERROR(( "tt_loader_init: could not access glyph table\n" ));
2288
loader->glyf_offset = FT_STREAM_POS();
2291
/* get face's glyph loader */
2292
if ( !glyf_table_only )
2294
FT_GlyphLoader gloader = glyph->internal->loader;
2297
FT_GlyphLoader_Rewind( gloader );
2298
loader->gloader = gloader;
2301
loader->load_flags = load_flags;
2303
loader->face = (FT_Face)face;
2304
loader->size = (FT_Size)size;
2305
loader->glyph = (FT_GlyphSlot)glyph;
2306
loader->stream = stream;
2312
/*************************************************************************/
2318
/* A function used to load a single glyph within a given glyph slot, */
2319
/* for a given size. */
2322
/* glyph :: A handle to a target slot object where the glyph */
2323
/* will be loaded. */
2325
/* size :: A handle to the source face size at which the glyph */
2326
/* must be scaled/loaded. */
2328
/* glyph_index :: The index of the glyph in the font file. */
2330
/* load_flags :: A flag indicating what to load for this glyph. The */
2331
/* FT_LOAD_XXX constants can be used to control the */
2332
/* glyph loading process (e.g., whether the outline */
2333
/* should be scaled, whether to load bitmaps or not, */
2334
/* whether to hint the outline, etc). */
2337
/* FreeType error code. 0 means success. */
2339
FT_LOCAL_DEF( FT_Error )
2340
TT_Load_Glyph( TT_Size size,
2342
FT_UInt glyph_index,
2343
FT_Int32 load_flags )
2346
TT_LoaderRec loader;
2349
FT_TRACE1(( "TT_Load_Glyph: glyph index %d\n", glyph_index ));
2351
#ifdef TT_CONFIG_OPTION_EMBEDDED_BITMAPS
2353
/* try to load embedded bitmap if any */
2355
/* XXX: The convention should be emphasized in */
2356
/* the documents because it can be confusing. */
2357
if ( size->strike_index != 0xFFFFFFFFUL &&
2358
( load_flags & FT_LOAD_NO_BITMAP ) == 0 )
2360
error = load_sbit_image( size, glyph, glyph_index, load_flags );
2363
if ( FT_IS_SCALABLE( glyph->face ) )
2365
/* for the bbox we need the header only */
2366
(void)tt_loader_init( &loader, size, glyph, load_flags, TRUE );
2367
(void)load_truetype_glyph( &loader, glyph_index, 0, TRUE );
2368
glyph->linearHoriAdvance = loader.linear;
2369
glyph->linearVertAdvance = loader.top_bearing + loader.bbox.yMax -
2372
/* sanity check: if `horiAdvance' in the sbit metric */
2373
/* structure isn't set, use `linearHoriAdvance' */
2374
if ( !glyph->metrics.horiAdvance && glyph->linearHoriAdvance )
2375
glyph->metrics.horiAdvance =
2376
FT_MulFix( glyph->linearHoriAdvance,
2377
size->root.metrics.x_scale );
2384
#endif /* TT_CONFIG_OPTION_EMBEDDED_BITMAPS */
2386
/* if FT_LOAD_NO_SCALE is not set, `ttmetrics' must be valid */
2387
if ( !( load_flags & FT_LOAD_NO_SCALE ) && !size->ttmetrics.valid )
2388
return FT_THROW( Invalid_Size_Handle );
2390
if ( load_flags & FT_LOAD_SBITS_ONLY )
2391
return FT_THROW( Invalid_Argument );
2393
error = tt_loader_init( &loader, size, glyph, load_flags, FALSE );
2397
glyph->format = FT_GLYPH_FORMAT_OUTLINE;
2398
glyph->num_subglyphs = 0;
2399
glyph->outline.flags = 0;
2401
/* main loading loop */
2402
error = load_truetype_glyph( &loader, glyph_index, 0, FALSE );
2405
if ( glyph->format == FT_GLYPH_FORMAT_COMPOSITE )
2407
glyph->num_subglyphs = loader.gloader->base.num_subglyphs;
2408
glyph->subglyphs = loader.gloader->base.subglyphs;
2412
glyph->outline = loader.gloader->base.outline;
2413
glyph->outline.flags &= ~FT_OUTLINE_SINGLE_PASS;
2415
/* Translate array so that (0,0) is the glyph's origin. Note */
2416
/* that this behaviour is independent on the value of bit 1 of */
2417
/* the `flags' field in the `head' table -- at least major */
2418
/* applications like Acroread indicate that. */
2420
FT_Outline_Translate( &glyph->outline, -loader.pp1.x, 0 );
2423
#ifdef TT_USE_BYTECODE_INTERPRETER
2425
if ( IS_HINTED( load_flags ) )
2427
if ( loader.exec->GS.scan_control )
2429
/* convert scan conversion mode to FT_OUTLINE_XXX flags */
2430
switch ( loader.exec->GS.scan_type )
2432
case 0: /* simple drop-outs including stubs */
2433
glyph->outline.flags |= FT_OUTLINE_INCLUDE_STUBS;
2435
case 1: /* simple drop-outs excluding stubs */
2436
/* nothing; it's the default rendering mode */
2438
case 4: /* smart drop-outs including stubs */
2439
glyph->outline.flags |= FT_OUTLINE_SMART_DROPOUTS |
2440
FT_OUTLINE_INCLUDE_STUBS;
2442
case 5: /* smart drop-outs excluding stubs */
2443
glyph->outline.flags |= FT_OUTLINE_SMART_DROPOUTS;
2446
default: /* no drop-out control */
2447
glyph->outline.flags |= FT_OUTLINE_IGNORE_DROPOUTS;
2452
glyph->outline.flags |= FT_OUTLINE_IGNORE_DROPOUTS;
2455
#endif /* TT_USE_BYTECODE_INTERPRETER */
2457
compute_glyph_metrics( &loader, glyph_index );
2460
/* Set the `high precision' bit flag. */
2461
/* This is _critical_ to get correct output for monochrome */
2462
/* TrueType glyphs at all sizes using the bytecode interpreter. */
2464
if ( !( load_flags & FT_LOAD_NO_SCALE ) &&
2465
size->root.metrics.y_ppem < 24 )
2466
glyph->outline.flags |= FT_OUTLINE_HIGH_PRECISION;