Skip to content
0.2.0-dev Download ZIP
2743 lines ·82.4 KB Raw
1/****************************************************************************
2 *
3 * ttgload.c
4 *
5 * TrueType Glyph Loader (body).
6 *
7 * Copyright (C) 1996-2026 by
8 * David Turner, Robert Wilhelm, and Werner Lemberg.
9 *
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.
15 *
16 */
17
18
19#include <ft2build.h>
20#include <freetype/internal/ftdebug.h>
21#include FT_CONFIG_CONFIG_H
22#include <freetype/internal/ftcalc.h>
23#include <freetype/internal/ftstream.h>
24#include <freetype/internal/sfnt.h>
25#include <freetype/tttags.h>
26#include <freetype/ftoutln.h>
27#include <freetype/ftdriver.h>
28#include <freetype/ftlist.h>
29
30#include "ttgload.h"
31#include "ttpload.h"
32
33#ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
34#include "ttgxvar.h"
35#endif
36
37#ifdef TT_CONFIG_OPTION_VARC
38#include "ttvarc.h"
39#endif
40
41#include "tterrors.h"
42
43
44 /**************************************************************************
45 *
46 * The macro FT_COMPONENT is used in trace mode. It is an implicit
47 * parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log
48 * messages during execution.
49 */
50#undef FT_COMPONENT
51#define FT_COMPONENT ttgload
52
53
54 /**************************************************************************
55 *
56 * Simple glyph flags.
57 */
58#define ON_CURVE_POINT 0x01
59#define X_SHORT_VECTOR 0x02
60#define Y_SHORT_VECTOR 0x04
61#define REPEAT_FLAG 0x08
62#define X_POSITIVE 0x10 /* two meanings depending on X_SHORT_VECTOR */
63#define SAME_X 0x10
64#define Y_POSITIVE 0x20 /* two meanings depending on Y_SHORT_VECTOR */
65#define SAME_Y 0x20
66#define OVERLAP_SIMPLE 0x40 /* retained as FT_OUTLINE_OVERLAP */
67#define CUBIC_FLAG 0x80
68
69
70 /**************************************************************************
71 *
72 * Composite glyph flags.
73 */
74#define ARGS_ARE_WORDS 0x0001
75#define ARGS_ARE_XY_VALUES 0x0002
76#define ROUND_XY_TO_GRID 0x0004
77#define WE_HAVE_A_SCALE 0x0008
78/* reserved 0x0010 */
79#define MORE_COMPONENTS 0x0020
80#define WE_HAVE_AN_XY_SCALE 0x0040
81#define WE_HAVE_A_2X2 0x0080
82#define WE_HAVE_INSTR 0x0100
83#define USE_MY_METRICS 0x0200
84#define OVERLAP_COMPOUND 0x0400 /* retained as FT_OUTLINE_OVERLAP */
85#define SCALED_COMPONENT_OFFSET 0x0800
86#define UNSCALED_COMPONENT_OFFSET 0x1000
87#define GID_IS_24_BIT 0x2000
88
89
90#ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
91#define IS_DEFAULT_INSTANCE( _face ) \
92 ( !( FT_IS_NAMED_INSTANCE( _face ) || \
93 FT_IS_VARIATION( _face ) ) )
94#else
95#define IS_DEFAULT_INSTANCE( _face ) 1
96#endif
97
98
99 /**************************************************************************
100 *
101 * Return the horizontal metrics in font units for a given glyph.
102 */
103 FT_LOCAL_DEF( void )
104 TT_Get_HMetrics( TT_Face face,
105 FT_UInt idx,
106 FT_Short* lsb,
107 FT_UShort* aw )
108 {
109 ( (SFNT_Service)face->sfnt )->get_metrics( face, 0, idx, lsb, aw );
110
111 FT_TRACE5(( " advance width (font units): %d\n", *aw ));
112 FT_TRACE5(( " left side bearing (font units): %d\n", *lsb ));
113 }
114
115
116 /**************************************************************************
117 *
118 * Return the vertical metrics in font units for a given glyph.
119 * See function `tt_loader_set_pp' below for explanations.
120 */
121 FT_LOCAL_DEF( void )
122 TT_Get_VMetrics( TT_Face face,
123 FT_UInt idx,
124 FT_Pos yMax,
125 FT_Short* tsb,
126 FT_UShort* ah )
127 {
128 if ( face->vertical_info )
129 ( (SFNT_Service)face->sfnt )->get_metrics( face, 1, idx, tsb, ah );
130
131 else if ( face->os2.version != 0xFFFFU )
132 {
133 *tsb = (FT_Short)( face->os2.sTypoAscender - yMax );
134 *ah = (FT_UShort)FT_ABS( face->os2.sTypoAscender -
135 face->os2.sTypoDescender );
136 }
137
138 else
139 {
140 *tsb = (FT_Short)( face->horizontal.Ascender - yMax );
141 *ah = (FT_UShort)FT_ABS( face->horizontal.Ascender -
142 face->horizontal.Descender );
143 }
144
145#ifdef FT_DEBUG_LEVEL_TRACE
146 if ( !face->vertical_info )
147 FT_TRACE5(( " [vertical metrics missing, computing values]\n" ));
148#endif
149
150 FT_TRACE5(( " advance height (font units): %d\n", *ah ));
151 FT_TRACE5(( " top side bearing (font units): %d\n", *tsb ));
152 }
153
154
155 static FT_Error
156 tt_get_metrics( TT_Loader loader,
157 FT_UInt glyph_index )
158 {
159 TT_Face face = loader->face;
160
161 FT_Error error;
162 FT_Stream stream = loader->stream;
163
164 FT_Short left_bearing = 0, top_bearing = 0;
165 FT_UShort advance_width = 0, advance_height = 0;
166
167 /* we must preserve the stream position */
168 /* (which gets altered by the metrics functions) */
169 FT_ULong pos = FT_STREAM_POS();
170
171
172 TT_Get_HMetrics( face, glyph_index,
173 &left_bearing,
174 &advance_width );
175 TT_Get_VMetrics( face, glyph_index,
176 loader->bbox.yMax,
177 &top_bearing,
178 &advance_height );
179
180 if ( FT_STREAM_SEEK( pos ) )
181 return error;
182
183 loader->left_bearing = left_bearing;
184 loader->advance = advance_width;
185 loader->top_bearing = top_bearing;
186 loader->vadvance = advance_height;
187
188#ifdef FT_CONFIG_OPTION_INCREMENTAL
189 /* With the incremental interface, these values are set by */
190 /* a call to `tt_get_metrics_incremental'. */
191 if ( face->root.internal->incremental_interface == NULL )
192#endif
193 {
194 if ( !loader->linear_def )
195 {
196 loader->linear_def = 1;
197 loader->linear = advance_width;
198 }
199 }
200
201 return FT_Err_Ok;
202 }
203
204
205#ifdef FT_CONFIG_OPTION_INCREMENTAL
206
207 static void
208 tt_get_metrics_incremental( TT_Loader loader,
209 FT_UInt glyph_index )
210 {
211 TT_Face face = loader->face;
212
213 FT_Short left_bearing = 0, top_bearing = 0;
214 FT_UShort advance_width = 0, advance_height = 0;
215
216
217 /* If this is an incrementally loaded font check whether there are */
218 /* overriding metrics for this glyph. */
219 if ( face->root.internal->incremental_interface &&
220 face->root.internal->incremental_interface->funcs->get_glyph_metrics )
221 {
222 FT_Incremental_MetricsRec incr_metrics;
223 FT_Error error;
224
225
226 incr_metrics.bearing_x = loader->left_bearing;
227 incr_metrics.bearing_y = 0;
228 incr_metrics.advance = loader->advance;
229 incr_metrics.advance_v = 0;
230
231 error = face->root.internal->incremental_interface->funcs->get_glyph_metrics(
232 face->root.internal->incremental_interface->object,
233 glyph_index, FALSE, &incr_metrics );
234 if ( error )
235 goto Exit;
236
237 left_bearing = (FT_Short)incr_metrics.bearing_x;
238 advance_width = (FT_UShort)incr_metrics.advance;
239
240#if 0
241
242 /* GWW: Do I do the same for vertical metrics? */
243 incr_metrics.bearing_x = 0;
244 incr_metrics.bearing_y = loader->top_bearing;
245 incr_metrics.advance = loader->vadvance;
246
247 error = face->root.internal->incremental_interface->funcs->get_glyph_metrics(
248 face->root.internal->incremental_interface->object,
249 glyph_index, TRUE, &incr_metrics );
250 if ( error )
251 goto Exit;
252
253 top_bearing = (FT_Short)incr_metrics.bearing_y;
254 advance_height = (FT_UShort)incr_metrics.advance;
255
256#endif /* 0 */
257
258 loader->left_bearing = left_bearing;
259 loader->advance = advance_width;
260 loader->top_bearing = top_bearing;
261 loader->vadvance = advance_height;
262
263 if ( !loader->linear_def )
264 {
265 loader->linear_def = 1;
266 loader->linear = advance_width;
267 }
268 }
269
270 Exit:
271 return;
272 }
273
274#endif /* FT_CONFIG_OPTION_INCREMENTAL */
275
276
277 /**************************************************************************
278 *
279 * The following functions are used by default with TrueType fonts.
280 * However, they can be replaced by alternatives if we need to support
281 * TrueType-compressed formats (like MicroType) in the future.
282 *
283 */
284
285 FT_CALLBACK_DEF( FT_Error )
286 TT_Access_Glyph_Frame( TT_Loader loader,
287 FT_UInt glyph_index,
288 FT_ULong offset,
289 FT_UInt byte_count )
290 {
291 FT_Error error;
292 FT_Stream stream = loader->stream;
293
294 FT_UNUSED( glyph_index );
295
296
297 /* the following line sets the `error' variable through macros! */
298 if ( FT_STREAM_SEEK( offset ) || FT_FRAME_ENTER( byte_count ) )
299 return error;
300
301 loader->cursor = stream->cursor;
302 loader->limit = stream->limit;
303
304 return FT_Err_Ok;
305 }
306
307
308 FT_CALLBACK_DEF( void )
309 TT_Forget_Glyph_Frame( TT_Loader loader )
310 {
311 FT_Stream stream = loader->stream;
312
313
314 FT_FRAME_EXIT();
315 }
316
317
318 FT_CALLBACK_DEF( FT_Error )
319 TT_Load_Glyph_Header( TT_Loader loader )
320 {
321 FT_Byte* p = loader->cursor;
322 FT_Byte* limit = loader->limit;
323
324
325 if ( p + 10 > limit )
326 return FT_THROW( Invalid_Outline );
327
328 loader->n_contours = FT_NEXT_SHORT( p );
329
330 loader->bbox.xMin = FT_NEXT_SHORT( p );
331 loader->bbox.yMin = FT_NEXT_SHORT( p );
332 loader->bbox.xMax = FT_NEXT_SHORT( p );
333 loader->bbox.yMax = FT_NEXT_SHORT( p );
334
335 FT_TRACE5(( " # of contours: %d\n", loader->n_contours ));
336 FT_TRACE5(( " xMin: %4ld xMax: %4ld\n", loader->bbox.xMin,
337 loader->bbox.xMax ));
338 FT_TRACE5(( " yMin: %4ld yMax: %4ld\n", loader->bbox.yMin,
339 loader->bbox.yMax ));
340 loader->cursor = p;
341
342 return FT_Err_Ok;
343 }
344
345
346 FT_CALLBACK_DEF( FT_Error )
347 TT_Load_Simple_Glyph( TT_Loader load )
348 {
349 FT_Error error;
350 FT_Byte* p = load->cursor;
351 FT_Byte* limit = load->limit;
352 FT_GlyphLoader gloader = load->gloader;
353 FT_Outline* outline = &gloader->current.outline;
354 FT_UInt n_contours = (FT_UInt)load->n_contours;
355 FT_UInt n_points;
356 FT_UShort n_ins;
357
358 FT_Byte *flag, *flag_limit;
359 FT_Byte c, count;
360 FT_Vector *vec, *vec_limit;
361 FT_Pos x, y;
362 FT_UShort *cont, *cont_limit;
363
364
365 /* check that we can add the contours to the glyph */
366 error = FT_GLYPHLOADER_CHECK_POINTS( gloader, 0, n_contours );
367 if ( error )
368 goto Fail;
369
370 /* check space for contours array + instructions count */
371 if ( n_contours >= 0xFFFU || p + 2 * n_contours + 2 > limit )
372 goto Invalid_Outline;
373
374 /* reading the contours' endpoints & number of points */
375 cont = outline->contours;
376 cont_limit = cont + n_contours;
377
378 n_points = 0;
379 for ( ; cont < cont_limit; cont++ )
380 {
381 *cont = FT_NEXT_USHORT( p );
382
383 if ( *cont < n_points )
384 goto Invalid_Outline;
385
386 n_points = *cont + 1U;
387 }
388
389 FT_TRACE5(( " # of points: %u\n", n_points ));
390
391 /* note that we will add four phantom points later */
392 error = FT_GLYPHLOADER_CHECK_POINTS( gloader, n_points + 4, 0 );
393 if ( error )
394 goto Fail;
395
396 /* space checked above */
397 n_ins = FT_NEXT_USHORT( p );
398
399 FT_TRACE5(( " Instructions size: %u\n", n_ins ));
400
401 /* check instructions size */
402 if ( p + n_ins > limit )
403 {
404 FT_TRACE1(( "TT_Load_Simple_Glyph: excessive instruction count\n" ));
405 error = FT_THROW( Too_Many_Hints );
406 goto Fail;
407 }
408
409#ifdef TT_USE_BYTECODE_INTERPRETER
410
411 if ( IS_HINTED( load->load_flags ) )
412 {
413 TT_ExecContext exec = load->exec;
414 FT_Memory memory = exec->memory;
415
416
417 if ( exec->glyphSize )
418 FT_FREE( exec->glyphIns );
419 exec->glyphSize = 0;
420
421 /* we don't trust `maxSizeOfInstructions' in the `maxp' table */
422 /* and thus allocate the bytecode array size by ourselves */
423 if ( n_ins )
424 {
425 if ( FT_DUP( exec->glyphIns, p, n_ins ) )
426 return error;
427
428 exec->glyphSize = n_ins;
429 }
430 }
431
432#endif /* TT_USE_BYTECODE_INTERPRETER */
433
434 p += n_ins;
435
436 /* reading the point tags */
437 flag = outline->tags;
438 flag_limit = flag + n_points;
439
440 FT_ASSERT( flag );
441
442 while ( flag < flag_limit )
443 {
444 if ( p + 1 > limit )
445 goto Invalid_Outline;
446
447 *flag++ = c = FT_NEXT_BYTE( p );
448 if ( c & REPEAT_FLAG )
449 {
450 if ( p + 1 > limit )
451 goto Invalid_Outline;
452
453 count = FT_NEXT_BYTE( p );
454 if ( flag + (FT_Int)count > flag_limit )
455 goto Invalid_Outline;
456
457 for ( ; count > 0; count-- )
458 *flag++ = c;
459 }
460 }
461
462 /* retain the overlap flag */
463 if ( n_points && outline->tags[0] & OVERLAP_SIMPLE )
464 gloader->base.outline.flags |= FT_OUTLINE_OVERLAP;
465
466 /* reading the X coordinates */
467
468 vec = outline->points;
469 vec_limit = vec + n_points;
470 flag = outline->tags;
471 x = 0;
472
473 for ( ; vec < vec_limit; vec++, flag++ )
474 {
475 FT_Pos delta = 0;
476 FT_Byte f = *flag;
477
478
479 if ( f & X_SHORT_VECTOR )
480 {
481 if ( p + 1 > limit )
482 goto Invalid_Outline;
483
484 delta = (FT_Pos)FT_NEXT_BYTE( p );
485 if ( !( f & X_POSITIVE ) )
486 delta = -delta;
487 }
488 else if ( !( f & SAME_X ) )
489 {
490 if ( p + 2 > limit )
491 goto Invalid_Outline;
492
493 delta = (FT_Pos)FT_NEXT_SHORT( p );
494 }
495
496 x += delta;
497 vec->x = x;
498 }
499
500 /* reading the Y coordinates */
501
502 vec = outline->points;
503 vec_limit = vec + n_points;
504 flag = outline->tags;
505 y = 0;
506
507 for ( ; vec < vec_limit; vec++, flag++ )
508 {
509 FT_Pos delta = 0;
510 FT_Byte f = *flag;
511
512
513 if ( f & Y_SHORT_VECTOR )
514 {
515 if ( p + 1 > limit )
516 goto Invalid_Outline;
517
518 delta = (FT_Pos)FT_NEXT_BYTE( p );
519 if ( !( f & Y_POSITIVE ) )
520 delta = -delta;
521 }
522 else if ( !( f & SAME_Y ) )
523 {
524 if ( p + 2 > limit )
525 goto Invalid_Outline;
526
527 delta = (FT_Pos)FT_NEXT_SHORT( p );
528 }
529
530 y += delta;
531 vec->y = y;
532
533 *flag = (FT_Byte)( f & ON_CURVE_POINT );
534 if ( load->face->is_extended_glyf && ( f & CUBIC_FLAG ) )
535 *flag |= FT_CURVE_TAG_CUBIC;
536 }
537
538 outline->n_points = (FT_UShort)n_points;
539 outline->n_contours = (FT_UShort)n_contours;
540
541 load->cursor = p;
542
543 Fail:
544 return error;
545
546 Invalid_Outline:
547 error = FT_THROW( Invalid_Outline );
548 goto Fail;
549 }
550
551
552 FT_CALLBACK_DEF( FT_Error )
553 TT_Load_Composite_Glyph( TT_Loader loader )
554 {
555 FT_Error error;
556 FT_Byte* p = loader->cursor;
557 FT_Byte* limit = loader->limit;
558 FT_GlyphLoader gloader = loader->gloader;
559 FT_Long num_glyphs = loader->face->root.num_glyphs;
560 FT_SubGlyph subglyph;
561 FT_UInt num_subglyphs;
562
563
564 num_subglyphs = 0;
565
566 do
567 {
568 FT_Fixed xx, xy, yy, yx;
569 FT_UInt count;
570
571
572 /* check that we can load a new subglyph */
573 error = FT_GlyphLoader_CheckSubGlyphs( gloader, num_subglyphs + 1 );
574 if ( error )
575 goto Fail;
576
577 /* check space for the flags and a 16-bit glyph ID */
578 if ( p + 4 > limit )
579 goto Invalid_Composite;
580
581 subglyph = gloader->current.subglyphs + num_subglyphs;
582
583 subglyph->arg1 = subglyph->arg2 = 0;
584
585 subglyph->flags = FT_NEXT_USHORT( p );
586 if ( loader->face->is_extended_glyf &&
587 ( subglyph->flags & GID_IS_24_BIT ) != 0 )
588 {
589 if ( p + 3 > limit )
590 goto Invalid_Composite;
591
592 subglyph->index = (FT_Int)FT_NEXT_UOFF3( p );
593 }
594 else
595 subglyph->index = FT_NEXT_USHORT( p );
596
597 /* we reject composites that have components */
598 /* with invalid glyph indices */
599 if ( subglyph->index >= num_glyphs )
600 goto Invalid_Composite;
601
602 /* check space */
603 count = 2;
604 if ( subglyph->flags & ARGS_ARE_WORDS )
605 count += 2;
606 if ( subglyph->flags & WE_HAVE_A_SCALE )
607 count += 2;
608 else if ( subglyph->flags & WE_HAVE_AN_XY_SCALE )
609 count += 4;
610 else if ( subglyph->flags & WE_HAVE_A_2X2 )
611 count += 8;
612
613 if ( p + count > limit )
614 goto Invalid_Composite;
615
616 /* read arguments */
617 if ( subglyph->flags & ARGS_ARE_XY_VALUES )
618 {
619 if ( subglyph->flags & ARGS_ARE_WORDS )
620 {
621 subglyph->arg1 = FT_NEXT_SHORT( p );
622 subglyph->arg2 = FT_NEXT_SHORT( p );
623 }
624 else
625 {
626 subglyph->arg1 = FT_NEXT_CHAR( p );
627 subglyph->arg2 = FT_NEXT_CHAR( p );
628 }
629 }
630 else
631 {
632 if ( subglyph->flags & ARGS_ARE_WORDS )
633 {
634 subglyph->arg1 = (FT_Int)FT_NEXT_USHORT( p );
635 subglyph->arg2 = (FT_Int)FT_NEXT_USHORT( p );
636 }
637 else
638 {
639 subglyph->arg1 = (FT_Int)FT_NEXT_BYTE( p );
640 subglyph->arg2 = (FT_Int)FT_NEXT_BYTE( p );
641 }
642 }
643
644 /* read transform */
645 xx = yy = 0x10000L;
646 xy = yx = 0;
647
648 if ( subglyph->flags & WE_HAVE_A_SCALE )
649 {
650 xx = (FT_Fixed)FT_NEXT_SHORT( p ) * 4;
651 yy = xx;
652 }
653 else if ( subglyph->flags & WE_HAVE_AN_XY_SCALE )
654 {
655 xx = (FT_Fixed)FT_NEXT_SHORT( p ) * 4;
656 yy = (FT_Fixed)FT_NEXT_SHORT( p ) * 4;
657 }
658 else if ( subglyph->flags & WE_HAVE_A_2X2 )
659 {
660 xx = (FT_Fixed)FT_NEXT_SHORT( p ) * 4;
661 yx = (FT_Fixed)FT_NEXT_SHORT( p ) * 4;
662 xy = (FT_Fixed)FT_NEXT_SHORT( p ) * 4;
663 yy = (FT_Fixed)FT_NEXT_SHORT( p ) * 4;
664 }
665
666 subglyph->transform.xx = xx;
667 subglyph->transform.xy = xy;
668 subglyph->transform.yx = yx;
669 subglyph->transform.yy = yy;
670
671 num_subglyphs++;
672
673 } while ( subglyph->flags & MORE_COMPONENTS );
674
675 gloader->current.num_subglyphs = num_subglyphs;
676 FT_TRACE5(( " %u component%s\n",
677 num_subglyphs,
678 num_subglyphs > 1 ? "s" : "" ));
679
680#ifdef FT_DEBUG_LEVEL_TRACE
681 {
682 FT_UInt i;
683
684
685 subglyph = gloader->current.subglyphs;
686
687 for ( i = 0; i < num_subglyphs; i++ )
688 {
689 if ( num_subglyphs > 1 )
690 FT_TRACE7(( " subglyph %u:\n", i ));
691
692 FT_TRACE7(( " glyph index: %d\n", subglyph->index ));
693
694 if ( subglyph->flags & ARGS_ARE_XY_VALUES )
695 FT_TRACE7(( " offset: x=%d, y=%d\n",
696 subglyph->arg1,
697 subglyph->arg2 ));
698 else
699 FT_TRACE7(( " matching points: base=%d, component=%d\n",
700 subglyph->arg1,
701 subglyph->arg2 ));
702
703 if ( subglyph->flags & WE_HAVE_A_SCALE )
704 FT_TRACE7(( " scaling: %f\n",
705 (double)subglyph->transform.xx / 65536 ));
706 else if ( subglyph->flags & WE_HAVE_AN_XY_SCALE )
707 FT_TRACE7(( " scaling: x=%f, y=%f\n",
708 (double)subglyph->transform.xx / 65536,
709 (double)subglyph->transform.yy / 65536 ));
710 else if ( subglyph->flags & WE_HAVE_A_2X2 )
711 {
712 FT_TRACE7(( " scaling: xx=%f, yx=%f\n",
713 (double)subglyph->transform.xx / 65536,
714 (double)subglyph->transform.yx / 65536 ));
715 FT_TRACE7(( " xy=%f, yy=%f\n",
716 (double)subglyph->transform.xy / 65536,
717 (double)subglyph->transform.yy / 65536 ));
718 }
719
720 subglyph++;
721 }
722 }
723#endif /* FT_DEBUG_LEVEL_TRACE */
724
725#ifdef TT_USE_BYTECODE_INTERPRETER
726
727 {
728 FT_Stream stream = loader->stream;
729
730
731 /* we must undo the FT_FRAME_ENTER in order to point */
732 /* to the composite instructions, if we find some. */
733 /* We will process them later. */
734 /* */
735 loader->ins_pos = (FT_ULong)( FT_STREAM_POS() +
736 p - limit );
737 }
738
739#endif
740
741 loader->cursor = p;
742
743 Fail:
744 return error;
745
746 Invalid_Composite:
747 error = FT_THROW( Invalid_Composite );
748 goto Fail;
749 }
750
751
752 FT_LOCAL_DEF( void )
753 TT_Init_Glyph_Loading( TT_Face face )
754 {
755 face->access_glyph_frame = TT_Access_Glyph_Frame;
756 face->read_glyph_header = TT_Load_Glyph_Header;
757 face->read_simple_glyph = TT_Load_Simple_Glyph;
758 face->read_composite_glyph = TT_Load_Composite_Glyph;
759 face->forget_glyph_frame = TT_Forget_Glyph_Frame;
760 }
761
762
763 static void
764 tt_prepare_zone( TT_GlyphZone zone,
765 FT_GlyphLoad load,
766 FT_UInt start_point,
767 FT_UInt start_contour )
768 {
769 zone->n_points = load->outline.n_points + 4 - (FT_UShort)start_point;
770 zone->n_contours = load->outline.n_contours - (FT_UShort)start_contour;
771 zone->org = load->extra_points + start_point;
772 zone->cur = load->outline.points + start_point;
773 zone->orus = load->extra_points2 + start_point;
774 zone->tags = load->outline.tags + start_point;
775 zone->contours = load->outline.contours + start_contour;
776 zone->first_point = (FT_UShort)start_point;
777 }
778
779
780 /**************************************************************************
781 *
782 * @Function:
783 * TT_Hint_Glyph
784 *
785 * @Description:
786 * Hint the glyph using the zone prepared by the caller. Note that
787 * the zone is supposed to include four phantom points.
788 */
789 static FT_Error
790 TT_Hint_Glyph( TT_Loader loader,
791 FT_Bool is_composite )
792 {
793 TT_GlyphZone zone = &loader->zone;
794
795#ifdef TT_USE_BYTECODE_INTERPRETER
796 TT_ExecContext exec = loader->exec;
797 TT_Size size = loader->size;
798 FT_Long n_ins = exec->glyphSize;
799#else
800 FT_UNUSED( is_composite );
801#endif
802
803
804#ifdef TT_USE_BYTECODE_INTERPRETER
805 /* save original point positions in `org' array */
806 if ( n_ins > 0 )
807 FT_ARRAY_COPY( zone->org, zone->cur, zone->n_points );
808
809 /* XXX: UNDOCUMENTED! Hinting instructions of a composite glyph */
810 /* completely refer to the (already) hinted subglyphs. */
811 if ( is_composite )
812 {
813 exec->metrics.x_scale = 1 << 16;
814 exec->metrics.y_scale = 1 << 16;
815
816 FT_ARRAY_COPY( zone->orus, zone->cur, zone->n_points );
817 }
818 else
819 {
820 exec->metrics.x_scale = size->metrics->x_scale;
821 exec->metrics.y_scale = size->metrics->y_scale;
822 }
823#endif
824
825 /* round phantom points */
826 zone->cur[zone->n_points - 4].x =
827 FT_PIX_ROUND( zone->cur[zone->n_points - 4].x );
828 zone->cur[zone->n_points - 3].x =
829 FT_PIX_ROUND( zone->cur[zone->n_points - 3].x );
830 zone->cur[zone->n_points - 2].y =
831 FT_PIX_ROUND( zone->cur[zone->n_points - 2].y );
832 zone->cur[zone->n_points - 1].y =
833 FT_PIX_ROUND( zone->cur[zone->n_points - 1].y );
834
835#ifdef TT_USE_BYTECODE_INTERPRETER
836
837 if ( n_ins > 0 )
838 {
839 FT_Error error;
840
841
842 TT_Set_CodeRange( exec, tt_coderange_glyph, exec->glyphIns, n_ins );
843
844 exec->is_composite = is_composite;
845 exec->pts = *zone;
846
847 error = TT_Run_Context( exec, size );
848 if ( error && exec->pedantic_hinting )
849 return error;
850
851 /* store drop-out mode in bits 5-7; set bit 2 also as a marker */
852 loader->gloader->current.outline.tags[0] |=
853 ( exec->GS.scan_type << 5 ) | FT_CURVE_TAG_HAS_SCANMODE;
854 }
855
856#endif
857
858 /* Save possibly modified glyph phantom points unless in v40 backward */
859 /* compatibility mode, where no movement on the x axis means no reason */
860 /* to change bearings or advance widths. */
861
862#ifdef TT_SUPPORT_SUBPIXEL_HINTING_MINIMAL
863 if ( exec->backward_compatibility )
864 return FT_Err_Ok;
865#endif
866
867 loader->pp1 = zone->cur[zone->n_points - 4];
868 loader->pp2 = zone->cur[zone->n_points - 3];
869 loader->pp3 = zone->cur[zone->n_points - 2];
870 loader->pp4 = zone->cur[zone->n_points - 1];
871
872 return FT_Err_Ok;
873 }
874
875
876 /**************************************************************************
877 *
878 * @Function:
879 * TT_Process_Simple_Glyph
880 *
881 * @Description:
882 * Once a simple glyph has been loaded, it needs to be processed.
883 * Usually, this means scaling and hinting through bytecode
884 * interpretation.
885 */
886 static FT_Error
887 TT_Process_Simple_Glyph( TT_Loader loader )
888 {
889 FT_Error error = FT_Err_Ok;
890 FT_GlyphLoader gloader = loader->gloader;
891 FT_Outline* outline = &gloader->current.outline;
892 FT_Int n_points = outline->n_points;
893
894#ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
895 FT_Memory memory = loader->face->root.memory;
896 FT_Vector* unrounded = NULL;
897#endif
898
899
900 /* set phantom points */
901 outline->points[n_points ] = loader->pp1;
902 outline->points[n_points + 1] = loader->pp2;
903 outline->points[n_points + 2] = loader->pp3;
904 outline->points[n_points + 3] = loader->pp4;
905
906 n_points += 4;
907
908#ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
909
910 if ( !IS_DEFAULT_INSTANCE( FT_FACE( loader->face ) ) )
911 {
912 if ( FT_QNEW_ARRAY( unrounded, n_points ) )
913 goto Exit;
914
915 /* Deltas apply to the unscaled data. */
916 error = TT_Vary_Apply_Glyph_Deltas( loader,
917 outline,
918 unrounded );
919 if ( error )
920 goto Exit;
921 }
922
923#endif /* TT_CONFIG_OPTION_GX_VAR_SUPPORT */
924
925 if ( IS_HINTED( loader->load_flags ) )
926 {
927 tt_prepare_zone( &loader->zone, &gloader->current, 0, 0 );
928
929 FT_ARRAY_COPY( loader->zone.orus, loader->zone.cur,
930 loader->zone.n_points );
931 }
932
933 {
934 FT_Vector* vec = outline->points;
935 FT_Vector* limit = outline->points + n_points;
936
937 FT_Fixed x_scale = 0; /* pacify compiler */
938 FT_Fixed y_scale = 0;
939
940 FT_Bool do_scale = FALSE;
941
942
943 {
944 /* scale the glyph */
945 if ( ( loader->load_flags & FT_LOAD_NO_SCALE ) == 0 )
946 {
947 x_scale = loader->size->metrics->x_scale;
948 y_scale = loader->size->metrics->y_scale;
949
950 do_scale = TRUE;
951 }
952 }
953
954 if ( do_scale )
955 {
956#ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
957 if ( !IS_DEFAULT_INSTANCE( FT_FACE( loader->face ) ) )
958 {
959 FT_Vector* u = unrounded;
960
961
962 for ( ; vec < limit; vec++, u++ )
963 {
964 vec->x = ADD_LONG( FT_MulFix( u->x, x_scale ), 32 ) >> 6;
965 vec->y = ADD_LONG( FT_MulFix( u->y, y_scale ), 32 ) >> 6;
966 }
967 }
968 else
969#endif /* TT_CONFIG_OPTION_GX_VAR_SUPPORT */
970 {
971 for ( ; vec < limit; vec++ )
972 {
973 vec->x = FT_MulFix( vec->x, x_scale );
974 vec->y = FT_MulFix( vec->y, y_scale );
975 }
976 }
977 }
978
979#ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
980 /* if we have a HVAR table, `pp1' and/or `pp2' */
981 /* are already adjusted but unscaled */
982 if ( ( loader->face->variation_support & TT_FACE_FLAG_VAR_HADVANCE ) &&
983 IS_HINTED( loader->load_flags ) )
984 {
985 loader->pp1.x = FT_MulFix( loader->pp1.x, x_scale );
986 loader->pp2.x = FT_MulFix( loader->pp2.x, x_scale );
987 /* pp1.y and pp2.y are always zero */
988 }
989 else
990#endif
991 {
992 loader->pp1 = outline->points[n_points - 4];
993 loader->pp2 = outline->points[n_points - 3];
994 }
995
996#ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
997 /* if we have a VVAR table, `pp3' and/or `pp4' */
998 /* are already adjusted but unscaled */
999 if ( ( loader->face->variation_support & TT_FACE_FLAG_VAR_VADVANCE ) &&
1000 IS_HINTED( loader->load_flags ) )
1001 {
1002 loader->pp3.x = FT_MulFix( loader->pp3.x, x_scale );
1003 loader->pp3.y = FT_MulFix( loader->pp3.y, y_scale );
1004 loader->pp4.x = FT_MulFix( loader->pp4.x, x_scale );
1005 loader->pp4.y = FT_MulFix( loader->pp4.y, y_scale );
1006 }
1007 else
1008#endif
1009 {
1010 loader->pp3 = outline->points[n_points - 2];
1011 loader->pp4 = outline->points[n_points - 1];
1012 }
1013 }
1014
1015 if ( IS_HINTED( loader->load_flags ) )
1016 error = TT_Hint_Glyph( loader, 0 );
1017
1018#ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
1019 Exit:
1020 FT_FREE( unrounded );
1021#endif
1022
1023 return error;
1024 }
1025
1026
1027 /**************************************************************************
1028 *
1029 * @Function:
1030 * TT_Process_Composite_Component
1031 *
1032 * @Description:
1033 * Once a composite component has been loaded, it needs to be
1034 * processed. Usually, this means transforming and translating.
1035 */
1036 static FT_Error
1037 TT_Process_Composite_Component( TT_Loader loader,
1038 FT_SubGlyph subglyph,
1039 FT_UInt start_point,
1040 FT_UInt num_base_points )
1041 {
1042 FT_GlyphLoader gloader = loader->gloader;
1043 FT_Outline current;
1044 FT_Bool have_scale;
1045 FT_Pos x, y;
1046
1047
1048 current.points = gloader->base.outline.points +
1049 num_base_points;
1050 current.n_points = gloader->base.outline.n_points -
1051 (FT_UShort)num_base_points;
1052
1053 have_scale = FT_BOOL( subglyph->flags & ( WE_HAVE_A_SCALE |
1054 WE_HAVE_AN_XY_SCALE |
1055 WE_HAVE_A_2X2 ) );
1056
1057 /* perform the transform required for this subglyph */
1058 if ( have_scale )
1059 FT_Outline_Transform( &current, &subglyph->transform );
1060
1061 /* get offset */
1062 if ( !( subglyph->flags & ARGS_ARE_XY_VALUES ) )
1063 {
1064 FT_UInt num_points = gloader->base.outline.n_points;
1065 FT_UInt k = (FT_UInt)subglyph->arg1;
1066 FT_UInt l = (FT_UInt)subglyph->arg2;
1067 FT_Vector* p1;
1068 FT_Vector* p2;
1069
1070
1071 /* match l-th point of the newly loaded component to the k-th point */
1072 /* of the previously loaded components. */
1073
1074 /* change to the point numbers used by our outline */
1075 k += start_point;
1076 l += num_base_points;
1077 if ( k >= num_base_points ||
1078 l >= num_points )
1079 return FT_THROW( Invalid_Composite );
1080
1081 p1 = gloader->base.outline.points + k;
1082 p2 = gloader->base.outline.points + l;
1083
1084 x = SUB_LONG( p1->x, p2->x );
1085 y = SUB_LONG( p1->y, p2->y );
1086 }
1087 else
1088 {
1089 x = subglyph->arg1;
1090 y = subglyph->arg2;
1091
1092 if ( !x && !y )
1093 return FT_Err_Ok;
1094
1095 /* Use a default value dependent on */
1096 /* TT_CONFIG_OPTION_COMPONENT_OFFSET_SCALED. This is useful for old */
1097 /* TT fonts which don't set the xxx_COMPONENT_OFFSET bit. */
1098
1099 if ( have_scale &&
1100#ifdef TT_CONFIG_OPTION_COMPONENT_OFFSET_SCALED
1101 !( subglyph->flags & UNSCALED_COMPONENT_OFFSET ) )
1102#else
1103 ( subglyph->flags & SCALED_COMPONENT_OFFSET ) )
1104#endif
1105 {
1106
1107#if 0
1108
1109 /********************************************************************
1110 *
1111 * This algorithm is what Apple documents. But it doesn't work.
1112 */
1113 int a = subglyph->transform.xx > 0 ? subglyph->transform.xx
1114 : -subglyph->transform.xx;
1115 int b = subglyph->transform.yx > 0 ? subglyph->transform.yx
1116 : -subglyph->transform.yx;
1117 int c = subglyph->transform.xy > 0 ? subglyph->transform.xy
1118 : -subglyph->transform.xy;
1119 int d = subglyph->transform.yy > 0 ? subglyph->transform.yy
1120 : -subglyph->transform.yy;
1121 int m = a > b ? a : b;
1122 int n = c > d ? c : d;
1123
1124
1125 if ( a - b <= 33 && a - b >= -33 )
1126 m *= 2;
1127 if ( c - d <= 33 && c - d >= -33 )
1128 n *= 2;
1129 x = FT_MulFix( x, m );
1130 y = FT_MulFix( y, n );
1131
1132#else /* 1 */
1133
1134 /********************************************************************
1135 *
1136 * This algorithm is a guess and works much better than the above.
1137 */
1138 FT_Fixed mac_xscale = FT_Hypot( subglyph->transform.xx,
1139 subglyph->transform.xy );
1140 FT_Fixed mac_yscale = FT_Hypot( subglyph->transform.yy,
1141 subglyph->transform.yx );
1142
1143
1144 x = FT_MulFix( x, mac_xscale );
1145 y = FT_MulFix( y, mac_yscale );
1146
1147#endif /* 1 */
1148
1149 }
1150
1151 if ( !( loader->load_flags & FT_LOAD_NO_SCALE ) )
1152 {
1153 FT_Fixed x_scale = loader->size->metrics->x_scale;
1154 FT_Fixed y_scale = loader->size->metrics->y_scale;
1155
1156
1157 x = FT_MulFix( x, x_scale );
1158 y = FT_MulFix( y, y_scale );
1159
1160 if ( subglyph->flags & ROUND_XY_TO_GRID &&
1161 IS_HINTED( loader->load_flags ) )
1162 {
1163#ifdef TT_SUPPORT_SUBPIXEL_HINTING_MINIMAL
1164 TT_Driver driver =
1165 (TT_Driver)FT_FACE_DRIVER( (FT_Face)loader->face );
1166
1167
1168 /* v40 approximates native ClearType's fine horizontal grid by */
1169 /* leaving X offsets of components unrounded. */
1170 if ( driver->interpreter_version != TT_INTERPRETER_VERSION_40 ||
1171 loader->exec->mode == FT_RENDER_MODE_MONO ||
1172 FT_IS_TRICKY( (FT_Face)loader->face ) )
1173#endif
1174 x = FT_PIX_ROUND( x );
1175
1176 y = FT_PIX_ROUND( y );
1177 }
1178 }
1179 }
1180
1181 if ( x || y )
1182 FT_Outline_Translate( &current, x, y );
1183
1184 return FT_Err_Ok;
1185 }
1186
1187
1188 /**************************************************************************
1189 *
1190 * @Function:
1191 * TT_Process_Composite_Glyph
1192 *
1193 * @Description:
1194 * This is slightly different from TT_Process_Simple_Glyph, in that
1195 * its sole purpose is to hint the glyph. Thus this function is
1196 * only available when bytecode interpreter is enabled.
1197 */
1198 static FT_Error
1199 TT_Process_Composite_Glyph( TT_Loader loader,
1200 FT_UInt start_point,
1201 FT_UInt start_contour )
1202 {
1203 FT_Error error;
1204 FT_Outline* outline = &loader->gloader->base.outline;
1205 FT_UInt i;
1206
1207
1208 /* make room for phantom points */
1209 error = FT_GLYPHLOADER_CHECK_POINTS( loader->gloader, 4, 0 );
1210 if ( error )
1211 return error;
1212
1213 outline->points[outline->n_points ] = loader->pp1;
1214 outline->points[outline->n_points + 1] = loader->pp2;
1215 outline->points[outline->n_points + 2] = loader->pp3;
1216 outline->points[outline->n_points + 3] = loader->pp4;
1217
1218#ifdef TT_USE_BYTECODE_INTERPRETER
1219
1220 {
1221 TT_ExecContext exec = loader->exec;
1222 FT_Memory memory = exec->memory;
1223 FT_Stream stream = loader->stream;
1224 FT_UShort n_ins;
1225
1226
1227 if ( exec->glyphSize )
1228 FT_FREE( exec->glyphIns );
1229 exec->glyphSize = 0;
1230
1231 /* TT_Load_Composite_Glyph only gives us the offset of instructions */
1232 /* so we read them here */
1233 if ( FT_STREAM_SEEK( loader->ins_pos ) ||
1234 FT_READ_USHORT( n_ins ) )
1235 return error;
1236
1237 FT_TRACE5(( " Instructions size = %hu\n", n_ins ));
1238
1239 if ( !n_ins )
1240 return FT_Err_Ok;
1241
1242 /* don't trust `maxSizeOfInstructions'; */
1243 /* only do a rough safety check */
1244 if ( n_ins > loader->byte_len )
1245 {
1246 FT_TRACE1(( "TT_Process_Composite_Glyph:"
1247 " too many instructions (%hu) for glyph with length %u\n",
1248 n_ins, loader->byte_len ));
1249 return FT_THROW( Too_Many_Hints );
1250 }
1251
1252 if ( FT_QNEW_ARRAY( exec->glyphIns, n_ins ) ||
1253 FT_STREAM_READ( exec->glyphIns, n_ins ) )
1254 return error;
1255
1256 exec->glyphSize = n_ins;
1257 }
1258
1259#endif
1260
1261 tt_prepare_zone( &loader->zone, &loader->gloader->base,
1262 start_point, start_contour );
1263
1264 /* Some points are likely touched during execution of */
1265 /* instructions on components. So let's untouch them. */
1266 for ( i = 0; i < loader->zone.n_points - 4U; i++ )
1267 loader->zone.tags[i] &= ~FT_CURVE_TAG_TOUCH_BOTH;
1268
1269 return TT_Hint_Glyph( loader, 1 );
1270 }
1271
1272
1273 /*
1274 * Calculate the phantom points
1275 *
1276 * Defining the right side bearing (rsb) as
1277 *
1278 * rsb = aw - (lsb + xmax - xmin)
1279 *
1280 * (with `aw' the advance width, `lsb' the left side bearing, and `xmin'
1281 * and `xmax' the glyph's minimum and maximum x value), the OpenType
1282 * specification defines the initial position of horizontal phantom points
1283 * as
1284 *
1285 * pp1 = (round(xmin - lsb), 0) ,
1286 * pp2 = (round(pp1 + aw), 0) .
1287 *
1288 * Note that the rounding to the grid (in the device space) is not
1289 * documented currently in the specification.
1290 *
1291 * However, the specification lacks the precise definition of vertical
1292 * phantom points. Greg Hitchcock provided the following explanation.
1293 *
1294 * - a `vmtx' table is present
1295 *
1296 * For any glyph, the minimum and maximum y values (`ymin' and `ymax')
1297 * are given in the `glyf' table, the top side bearing (tsb) and advance
1298 * height (ah) are given in the `vmtx' table. The bottom side bearing
1299 * (bsb) is then calculated as
1300 *
1301 * bsb = ah - (tsb + ymax - ymin) ,
1302 *
1303 * and the initial position of vertical phantom points is
1304 *
1305 * pp3 = (x, round(ymax + tsb)) ,
1306 * pp4 = (x, round(pp3 - ah)) .
1307 *
1308 * See below for value `x'.
1309 *
1310 * - no `vmtx' table in the font
1311 *
1312 * If there is an `OS/2' table, we set
1313 *
1314 * DefaultAscender = sTypoAscender ,
1315 * DefaultDescender = sTypoDescender ,
1316 *
1317 * otherwise we use data from the `hhea' table:
1318 *
1319 * DefaultAscender = Ascender ,
1320 * DefaultDescender = Descender .
1321 *
1322 * With these two variables we can now set
1323 *
1324 * ah = DefaultAscender - sDefaultDescender ,
1325 * tsb = DefaultAscender - yMax ,
1326 *
1327 * and proceed as if a `vmtx' table was present.
1328 *
1329 * Usually we have
1330 *
1331 * x = aw / 2 , (1)
1332 *
1333 * but there is one compatibility case where it can be set to
1334 *
1335 * x = -DefaultDescender -
1336 * ((DefaultAscender - DefaultDescender - aw) / 2) . (2)
1337 *
1338 * and another one with
1339 *
1340 * x = 0 . (3)
1341 *
1342 * In Windows, the history of those values is quite complicated,
1343 * depending on the hinting engine (that is, the graphics framework).
1344 *
1345 * framework from to formula
1346 * ----------------------------------------------------------
1347 * GDI Windows 98 current (1)
1348 * (Windows 2000 for NT)
1349 * GDI+ Windows XP Windows 7 (2)
1350 * GDI+ Windows 8 current (3)
1351 * DWrite Windows 7 current (3)
1352 *
1353 * For simplicity, FreeType uses (1) for grayscale subpixel hinting and
1354 * (3) for everything else.
1355 *
1356 */
1357 static void
1358 tt_loader_set_pp( TT_Loader loader )
1359 {
1360 loader->pp1.x = loader->bbox.xMin - loader->left_bearing;
1361 loader->pp1.y = 0;
1362 loader->pp2.x = loader->pp1.x + loader->advance;
1363 loader->pp2.y = 0;
1364
1365 loader->pp3.x = 0;
1366 loader->pp3.y = loader->bbox.yMax + loader->top_bearing;
1367 loader->pp4.x = 0;
1368 loader->pp4.y = loader->pp3.y - loader->vadvance;
1369
1370#ifdef TT_SUPPORT_SUBPIXEL_HINTING_MINIMAL
1371 {
1372 TT_Driver driver = (TT_Driver)FT_FACE_DRIVER( loader->face );
1373
1374
1375 if ( driver->interpreter_version == TT_INTERPRETER_VERSION_40 &&
1376 loader->exec &&
1377 loader->exec->mode != FT_RENDER_MODE_MONO &&
1378 loader->exec->mode != FT_RENDER_MODE_LCD &&
1379 loader->exec->mode != FT_RENDER_MODE_LCD_V )
1380 {
1381 loader->pp3.x = loader->advance / 2;
1382 loader->pp4.x = loader->advance / 2;
1383 }
1384 }
1385#endif
1386 }
1387
1388
1389 /* a utility function to retrieve i-th node from given FT_List */
1390 static FT_ListNode
1391 ft_list_get_node_at( FT_List list,
1392 FT_UInt idx )
1393 {
1394 FT_ListNode cur;
1395
1396
1397 if ( !list )
1398 return NULL;
1399
1400 for ( cur = list->head; cur; cur = cur->next )
1401 {
1402 if ( !idx )
1403 return cur;
1404
1405 idx--;
1406 }
1407
1408 return NULL;
1409 }
1410
1411
1412 /**************************************************************************
1413 *
1414 * @Function:
1415 * load_truetype_glyph
1416 *
1417 * @Description:
1418 * Loads a given truetype glyph. Handles composites and uses a
1419 * TT_Loader object.
1420 */
1421 static FT_Error
1422 load_truetype_glyph( TT_Loader loader,
1423 FT_UInt glyph_index,
1424 FT_UInt recurse_count,
1425 FT_Bool header_only )
1426 {
1427 FT_Error error = FT_Err_Ok;
1428 FT_Fixed x_scale, y_scale;
1429 FT_ULong offset;
1430 TT_Face face = loader->face;
1431 FT_GlyphLoader gloader = loader->gloader;
1432
1433 FT_Bool opened_frame = 0;
1434
1435#ifdef FT_CONFIG_OPTION_INCREMENTAL
1436 FT_StreamRec inc_stream;
1437 FT_Data glyph_data;
1438 FT_Bool glyph_data_loaded = 0;
1439#endif
1440
1441
1442#ifdef FT_DEBUG_LEVEL_TRACE
1443 if ( recurse_count )
1444 FT_TRACE5(( " nesting level: %u\n", recurse_count ));
1445#endif
1446
1447 /* arbitrary recursion limit */
1448 if ( recurse_count > 100 )
1449 {
1450 FT_TRACE4(( "load_truetype_glyph: recursion depth exceeded\n" ));
1451 error = FT_THROW( Invalid_Composite );
1452 goto Exit;
1453 }
1454
1455 /* some fonts have an incorrect value of `maxComponentDepth' */
1456 if ( recurse_count > face->max_profile.maxComponentDepth )
1457 {
1458 FT_TRACE1(( "load_truetype_glyph: maxComponentDepth set to %u\n",
1459 recurse_count ));
1460 face->max_profile.maxComponentDepth = (FT_UShort)recurse_count;
1461 }
1462
1463#ifndef FT_CONFIG_OPTION_INCREMENTAL
1464 /* check glyph index */
1465 if ( glyph_index >= (FT_UInt)face->root.num_glyphs )
1466 {
1467 error = FT_THROW( Invalid_Glyph_Index );
1468 goto Exit;
1469 }
1470#endif
1471
1472 loader->glyph_index = glyph_index;
1473
1474 if ( loader->load_flags & FT_LOAD_NO_SCALE )
1475 {
1476 x_scale = 0x10000L;
1477 y_scale = 0x10000L;
1478 }
1479 else
1480 {
1481 x_scale = loader->size->metrics->x_scale;
1482 y_scale = loader->size->metrics->y_scale;
1483 }
1484
1485 /* Set `offset' to the start of the glyph relative to the start of */
1486 /* the `glyf' table, and `byte_len' to the length of the glyph in */
1487 /* bytes. */
1488
1489#ifdef FT_CONFIG_OPTION_INCREMENTAL
1490
1491 /* If we are loading glyph data via the incremental interface, set */
1492 /* the loader stream to a memory stream reading the data returned */
1493 /* by the interface. */
1494 if ( face->root.internal->incremental_interface )
1495 {
1496 error = face->root.internal->incremental_interface->funcs->get_glyph_data(
1497 face->root.internal->incremental_interface->object,
1498 glyph_index, &glyph_data );
1499 if ( error )
1500 goto Exit;
1501
1502 glyph_data_loaded = 1;
1503 offset = 0;
1504 loader->byte_len = glyph_data.length;
1505
1506 FT_ZERO( &inc_stream );
1507 FT_Stream_OpenMemory( &inc_stream,
1508 glyph_data.pointer,
1509 glyph_data.length );
1510
1511 loader->stream = &inc_stream;
1512 }
1513 else
1514
1515#endif /* FT_CONFIG_OPTION_INCREMENTAL */
1516 {
1517 FT_ULong len;
1518
1519
1520 offset = tt_face_get_location( FT_FACE( face ), glyph_index, &len );
1521
1522 loader->byte_len = (FT_UInt)len;
1523 }
1524
1525 if ( loader->byte_len > 0 )
1526 {
1527#ifdef FT_CONFIG_OPTION_INCREMENTAL
1528 /* for the incremental interface, `glyf_offset' is always zero */
1529 if ( !face->glyf_offset &&
1530 !face->root.internal->incremental_interface )
1531#else
1532 if ( !face->glyf_offset )
1533#endif /* FT_CONFIG_OPTION_INCREMENTAL */
1534 {
1535 FT_TRACE2(( "no `glyf' table but non-zero `loca' entry\n" ));
1536 error = FT_THROW( Invalid_Table );
1537 goto Exit;
1538 }
1539
1540 error = face->access_glyph_frame( loader, glyph_index,
1541 face->glyf_offset + offset,
1542 loader->byte_len );
1543 if ( error )
1544 goto Exit;
1545
1546 /* read glyph header first */
1547 error = face->read_glyph_header( loader );
1548
1549 face->forget_glyph_frame( loader );
1550
1551 if ( error )
1552 goto Exit;
1553 }
1554
1555 /* a space glyph */
1556 if ( loader->byte_len == 0 || loader->n_contours == 0 )
1557 {
1558 loader->bbox.xMin = 0;
1559 loader->bbox.xMax = 0;
1560 loader->bbox.yMin = 0;
1561 loader->bbox.yMax = 0;
1562 }
1563
1564 /* the metrics must be computed after loading the glyph header */
1565 /* since we need the glyph's `yMax' value in case the vertical */
1566 /* metrics must be emulated */
1567 error = tt_get_metrics( loader, glyph_index );
1568 if ( error )
1569 goto Exit;
1570
1571 if ( header_only )
1572 goto Exit;
1573
1574#ifdef FT_CONFIG_OPTION_INCREMENTAL
1575 tt_get_metrics_incremental( loader, glyph_index );
1576#endif
1577 tt_loader_set_pp( loader );
1578
1579 /* shortcut for empty glyphs */
1580 if ( loader->byte_len == 0 || loader->n_contours == 0 )
1581 {
1582
1583#ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
1584
1585 if ( !IS_DEFAULT_INSTANCE( FT_FACE( face ) ) )
1586 {
1587 /* a small outline structure with four elements for */
1588 /* communication with `TT_Vary_Apply_Glyph_Deltas' */
1589 FT_Vector points[4];
1590 FT_Outline outline;
1591
1592 /* unrounded values */
1593 FT_Vector unrounded[4] = { {0, 0}, {0, 0}, {0, 0}, {0, 0} };
1594
1595
1596 points[0] = loader->pp1;
1597 points[1] = loader->pp2;
1598 points[2] = loader->pp3;
1599 points[3] = loader->pp4;
1600
1601 outline.n_points = 0;
1602 outline.n_contours = 0;
1603 outline.points = points;
1604 outline.tags = NULL;
1605 outline.contours = NULL;
1606
1607 /* this must be done before scaling */
1608 error = TT_Vary_Apply_Glyph_Deltas( loader,
1609 &outline,
1610 unrounded );
1611 if ( error )
1612 goto Exit;
1613 }
1614
1615#endif /* TT_CONFIG_OPTION_GX_VAR_SUPPORT */
1616
1617 /* scale phantom points, if necessary; */
1618 /* they get rounded in `TT_Hint_Glyph' */
1619 if ( ( loader->load_flags & FT_LOAD_NO_SCALE ) == 0 )
1620 {
1621 loader->pp1.x = FT_MulFix( loader->pp1.x, x_scale );
1622 loader->pp2.x = FT_MulFix( loader->pp2.x, x_scale );
1623 /* pp1.y and pp2.y are always zero */
1624
1625 loader->pp3.x = FT_MulFix( loader->pp3.x, x_scale );
1626 loader->pp3.y = FT_MulFix( loader->pp3.y, y_scale );
1627 loader->pp4.x = FT_MulFix( loader->pp4.x, x_scale );
1628 loader->pp4.y = FT_MulFix( loader->pp4.y, y_scale );
1629 }
1630
1631 error = FT_Err_Ok;
1632 goto Exit;
1633 }
1634
1635
1636 /***********************************************************************/
1637 /***********************************************************************/
1638 /***********************************************************************/
1639
1640 /* we now open a frame again, right after the glyph header */
1641 /* (which consists of 10 bytes) */
1642 error = face->access_glyph_frame( loader, glyph_index,
1643 face->glyf_offset + offset + 10,
1644 loader->byte_len - 10 );
1645 if ( error )
1646 goto Exit;
1647
1648 opened_frame = 1;
1649
1650 /* if it is a simple glyph, load it */
1651
1652 if ( loader->n_contours > 0 )
1653 {
1654 error = face->read_simple_glyph( loader );
1655 if ( error )
1656 goto Exit;
1657
1658 /* all data have been read */
1659 face->forget_glyph_frame( loader );
1660 opened_frame = 0;
1661
1662 error = TT_Process_Simple_Glyph( loader );
1663 if ( error )
1664 goto Exit;
1665
1666 FT_GlyphLoader_Add( gloader );
1667 }
1668
1669 /***********************************************************************/
1670 /***********************************************************************/
1671 /***********************************************************************/
1672
1673 /* otherwise, load a composite! */
1674 else if ( loader->n_contours < 0 )
1675 {
1676 FT_Memory memory = face->root.memory;
1677
1678 FT_UInt start_point;
1679 FT_UInt start_contour;
1680 FT_ULong ins_pos; /* position of composite instructions, if any */
1681
1682 FT_ListNode node, node2;
1683
1684
1685 /* normalize the `n_contours' value */
1686 loader->n_contours = -1;
1687
1688 /*
1689 * We store the glyph index directly in the `node->data' pointer,
1690 * following the glib solution (cf. macro `GUINT_TO_POINTER') with a
1691 * double cast to make this portable. Note, however, that this needs
1692 * pointers with a width of at least 32 bits.
1693 */
1694
1695 /* clear the nodes filled by sibling chains */
1696 node = ft_list_get_node_at( &loader->composites, recurse_count );
1697 for ( node2 = node; node2; node2 = node2->next )
1698 node2->data = (void*)-1;
1699
1700 /* check whether we already have a composite glyph with this index */
1701 if ( FT_List_Find( &loader->composites,
1702 FT_UINT_TO_POINTER( glyph_index ) ) )
1703 {
1704 FT_TRACE1(( "TT_Load_Composite_Glyph:"
1705 " infinite recursion detected\n" ));
1706 error = FT_THROW( Invalid_Composite );
1707 goto Exit;
1708 }
1709
1710 else if ( node )
1711 node->data = FT_UINT_TO_POINTER( glyph_index );
1712
1713 else
1714 {
1715 if ( FT_QNEW( node ) )
1716 goto Exit;
1717 node->data = FT_UINT_TO_POINTER( glyph_index );
1718 FT_List_Add( &loader->composites, node );
1719 }
1720
1721 start_point = gloader->base.outline.n_points;
1722 start_contour = gloader->base.outline.n_contours;
1723
1724 /* for each subglyph, read composite header */
1725 error = face->read_composite_glyph( loader );
1726 if ( error )
1727 goto Exit;
1728
1729 /* store the offset of instructions */
1730 ins_pos = loader->ins_pos;
1731
1732 /* all data we need are read */
1733 face->forget_glyph_frame( loader );
1734 opened_frame = 0;
1735
1736#ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
1737
1738 if ( !IS_DEFAULT_INSTANCE( FT_FACE( face ) ) )
1739 {
1740 FT_UShort i, limit;
1741 FT_SubGlyph subglyph;
1742
1743 FT_Outline outline = { 0, 0, NULL, NULL, NULL, 0 };
1744 FT_Vector* unrounded = NULL;
1745
1746
1747 limit = (FT_UShort)gloader->current.num_subglyphs;
1748
1749 /* construct an outline structure for */
1750 /* communication with `TT_Vary_Apply_Glyph_Deltas' */
1751 if ( FT_QNEW_ARRAY( outline.points, limit + 4 ) ||
1752 FT_QNEW_ARRAY( outline.tags, limit ) ||
1753 FT_QNEW_ARRAY( outline.contours, limit ) ||
1754 FT_QNEW_ARRAY( unrounded, limit + 4 ) )
1755 goto Exit1;
1756
1757 outline.n_contours = outline.n_points = limit;
1758
1759 subglyph = gloader->current.subglyphs;
1760
1761 for ( i = 0; i < limit; i++, subglyph++ )
1762 {
1763 /* applying deltas for anchor points doesn't make sense, */
1764 /* but we don't have to specially check this since */
1765 /* unused delta values are zero anyways */
1766 outline.points[i].x = subglyph->arg1;
1767 outline.points[i].y = subglyph->arg2;
1768 outline.tags[i] = ON_CURVE_POINT;
1769 outline.contours[i] = i;
1770 }
1771
1772 outline.points[i++] = loader->pp1;
1773 outline.points[i++] = loader->pp2;
1774 outline.points[i++] = loader->pp3;
1775 outline.points[i ] = loader->pp4;
1776
1777 /* this call provides additional offsets */
1778 /* for each component's translation */
1779 if ( FT_SET_ERROR( TT_Vary_Apply_Glyph_Deltas( loader,
1780 &outline,
1781 unrounded ) ) )
1782 goto Exit1;
1783
1784 subglyph = gloader->current.subglyphs;
1785
1786 for ( i = 0; i < limit; i++, subglyph++ )
1787 {
1788 if ( subglyph->flags & ARGS_ARE_XY_VALUES )
1789 {
1790 subglyph->arg1 = (FT_Int16)outline.points[i].x;
1791 subglyph->arg2 = (FT_Int16)outline.points[i].y;
1792 }
1793 }
1794
1795 Exit1:
1796 FT_FREE( outline.points );
1797 FT_FREE( outline.tags );
1798 FT_FREE( outline.contours );
1799 FT_FREE( unrounded );
1800
1801 if ( error )
1802 goto Exit;
1803 }
1804
1805#endif /* TT_CONFIG_OPTION_GX_VAR_SUPPORT */
1806
1807 /* scale phantom points, if necessary; */
1808 /* they get rounded in `TT_Hint_Glyph' */
1809 if ( ( loader->load_flags & FT_LOAD_NO_SCALE ) == 0 )
1810 {
1811 loader->pp1.x = FT_MulFix( loader->pp1.x, x_scale );
1812 loader->pp2.x = FT_MulFix( loader->pp2.x, x_scale );
1813 /* pp1.y and pp2.y are always zero */
1814
1815 loader->pp3.x = FT_MulFix( loader->pp3.x, x_scale );
1816 loader->pp3.y = FT_MulFix( loader->pp3.y, y_scale );
1817 loader->pp4.x = FT_MulFix( loader->pp4.x, x_scale );
1818 loader->pp4.y = FT_MulFix( loader->pp4.y, y_scale );
1819 }
1820
1821 /* if the flag FT_LOAD_NO_RECURSE is set, we return the subglyph */
1822 /* `as is' in the glyph slot (the client application will be */
1823 /* responsible for interpreting these data)... */
1824 if ( loader->load_flags & FT_LOAD_NO_RECURSE )
1825 {
1826 FT_GlyphLoader_Add( gloader );
1827 loader->glyph->format = FT_GLYPH_FORMAT_COMPOSITE;
1828
1829 goto Exit;
1830 }
1831
1832 /*********************************************************************/
1833 /*********************************************************************/
1834 /*********************************************************************/
1835
1836 {
1837 FT_UInt n, num_base_points;
1838 FT_SubGlyph subglyph = NULL;
1839
1840 FT_UInt num_points = start_point;
1841 FT_UInt num_subglyphs = gloader->current.num_subglyphs;
1842 FT_UInt num_base_subgs = gloader->base.num_subglyphs;
1843
1844 FT_Stream old_stream = loader->stream;
1845 FT_UInt old_byte_len = loader->byte_len;
1846
1847
1848 FT_GlyphLoader_Add( gloader );
1849
1850 /* read each subglyph independently */
1851 for ( n = 0; n < num_subglyphs; n++ )
1852 {
1853 FT_Vector pp[4];
1854
1855 FT_Int linear_hadvance;
1856 FT_Int linear_vadvance;
1857
1858
1859 /* Each time we call `load_truetype_glyph' in this loop, the */
1860 /* value of `gloader.base.subglyphs' can change due to table */
1861 /* reallocations. We thus need to recompute the subglyph */
1862 /* pointer on each iteration. */
1863 subglyph = gloader->base.subglyphs + num_base_subgs + n;
1864
1865 pp[0] = loader->pp1;
1866 pp[1] = loader->pp2;
1867 pp[2] = loader->pp3;
1868 pp[3] = loader->pp4;
1869
1870 linear_hadvance = loader->linear;
1871 linear_vadvance = loader->vadvance;
1872
1873 num_base_points = gloader->base.outline.n_points;
1874
1875 error = load_truetype_glyph( loader,
1876 (FT_UInt)subglyph->index,
1877 recurse_count + 1,
1878 FALSE );
1879 if ( error )
1880 goto Exit;
1881
1882 /* restore subglyph pointer */
1883 subglyph = gloader->base.subglyphs + num_base_subgs + n;
1884
1885 /* restore phantom points if necessary */
1886 if ( !( subglyph->flags & USE_MY_METRICS ) )
1887 {
1888 loader->pp1 = pp[0];
1889 loader->pp2 = pp[1];
1890 loader->pp3 = pp[2];
1891 loader->pp4 = pp[3];
1892
1893 loader->linear = linear_hadvance;
1894 loader->vadvance = linear_vadvance;
1895 }
1896
1897 num_points = gloader->base.outline.n_points;
1898
1899 if ( num_points == num_base_points )
1900 continue;
1901
1902 /* gloader->base.outline consists of three parts: */
1903 /* */
1904 /* 0 ----> start_point ----> num_base_points ----> n_points */
1905 /* (1) (2) (3) */
1906 /* */
1907 /* (1) points that exist from the beginning */
1908 /* (2) component points that have been loaded so far */
1909 /* (3) points of the newly loaded component */
1910 error = TT_Process_Composite_Component( loader,
1911 subglyph,
1912 start_point,
1913 num_base_points );
1914 if ( error )
1915 goto Exit;
1916 }
1917
1918 loader->stream = old_stream;
1919 loader->byte_len = old_byte_len;
1920
1921 /* process the glyph */
1922 loader->ins_pos = ins_pos;
1923 if ( IS_HINTED( loader->load_flags ) &&
1924#ifdef TT_USE_BYTECODE_INTERPRETER
1925 subglyph &&
1926 subglyph->flags & WE_HAVE_INSTR &&
1927#endif
1928 num_points > start_point )
1929 {
1930 error = TT_Process_Composite_Glyph( loader,
1931 start_point,
1932 start_contour );
1933 if ( error )
1934 goto Exit;
1935 }
1936 }
1937
1938 /* retain the overlap flag */
1939 if ( gloader->base.num_subglyphs &&
1940 gloader->base.subglyphs[0].flags & OVERLAP_COMPOUND )
1941 gloader->base.outline.flags |= FT_OUTLINE_OVERLAP;
1942 }
1943
1944 /***********************************************************************/
1945 /***********************************************************************/
1946 /***********************************************************************/
1947
1948 Exit:
1949
1950 if ( opened_frame )
1951 face->forget_glyph_frame( loader );
1952
1953#ifdef FT_CONFIG_OPTION_INCREMENTAL
1954
1955 /* restore the original stream */
1956 loader->stream = face->root.stream;
1957
1958 if ( glyph_data_loaded )
1959 face->root.internal->incremental_interface->funcs->free_glyph_data(
1960 face->root.internal->incremental_interface->object,
1961 &glyph_data );
1962
1963#endif
1964
1965 return error;
1966 }
1967
1968
1969 static FT_Error
1970 compute_glyph_metrics( TT_Loader loader,
1971 FT_UInt glyph_index )
1972 {
1973 TT_Face face = loader->face;
1974 TT_Size size = loader->size;
1975 TT_GlyphSlot glyph = loader->glyph;
1976 FT_BBox bbox;
1977 FT_Fixed y_scale;
1978
1979
1980 y_scale = 0x10000L;
1981 if ( ( loader->load_flags & FT_LOAD_NO_SCALE ) == 0 )
1982 y_scale = size->metrics->y_scale;
1983
1984 if ( glyph->format != FT_GLYPH_FORMAT_COMPOSITE )
1985 FT_Outline_Get_CBox( &glyph->outline, &bbox );
1986 else
1987 bbox = loader->bbox;
1988
1989 /* get the device-independent horizontal advance; it is scaled later */
1990 /* by the base layer. */
1991 glyph->linearHoriAdvance = loader->linear;
1992
1993 glyph->metrics.horiBearingX = bbox.xMin;
1994 glyph->metrics.horiBearingY = bbox.yMax;
1995 if ( loader->widthp )
1996 glyph->metrics.horiAdvance = loader->widthp[glyph_index] * 64;
1997 else
1998 glyph->metrics.horiAdvance = SUB_LONG( loader->pp2.x, loader->pp1.x );
1999
2000 /* set glyph dimensions */
2001 glyph->metrics.width = SUB_LONG( bbox.xMax, bbox.xMin );
2002 glyph->metrics.height = SUB_LONG( bbox.yMax, bbox.yMin );
2003
2004 /* Now take care of vertical metrics. In the case where there is */
2005 /* no vertical information within the font (relatively common), */
2006 /* create some metrics manually */
2007 {
2008 FT_Pos top; /* scaled vertical top side bearing */
2009 FT_Pos advance; /* scaled vertical advance height */
2010
2011
2012 /* Get the unscaled top bearing and advance height. */
2013 if ( face->vertical_info &&
2014 face->vertical.number_Of_VMetrics > 0 )
2015 {
2016 top = (FT_Short)FT_DivFix( SUB_LONG( loader->pp3.y, bbox.yMax ),
2017 y_scale );
2018
2019 if ( loader->pp3.y <= loader->pp4.y )
2020 advance = 0;
2021 else
2022 advance = (FT_UShort)FT_DivFix( SUB_LONG( loader->pp3.y,
2023 loader->pp4.y ),
2024 y_scale );
2025 }
2026 else
2027 {
2028 FT_Pos height;
2029
2030
2031 /* XXX Compute top side bearing and advance height in */
2032 /* Get_VMetrics instead of here. */
2033
2034 /* NOTE: The OS/2 values are the only `portable' ones, */
2035 /* which is why we use them, if there is an OS/2 */
2036 /* table in the font. Otherwise, we use the */
2037 /* values defined in the horizontal header. */
2038
2039 height = (FT_Short)FT_DivFix( SUB_LONG( bbox.yMax,
2040 bbox.yMin ),
2041 y_scale );
2042 if ( face->os2.version != 0xFFFFU )
2043 advance = (FT_Pos)( face->os2.sTypoAscender -
2044 face->os2.sTypoDescender );
2045 else
2046 advance = (FT_Pos)( face->horizontal.Ascender -
2047 face->horizontal.Descender );
2048
2049 top = ( advance - height ) / 2;
2050 }
2051
2052#ifdef FT_CONFIG_OPTION_INCREMENTAL
2053 {
2054 FT_Incremental_InterfaceRec* incr;
2055 FT_Incremental_MetricsRec incr_metrics;
2056 FT_Error error;
2057
2058
2059 incr = face->root.internal->incremental_interface;
2060
2061 /* If this is an incrementally loaded font see if there are */
2062 /* overriding metrics for this glyph. */
2063 if ( incr && incr->funcs->get_glyph_metrics )
2064 {
2065 incr_metrics.bearing_x = 0;
2066 incr_metrics.bearing_y = top;
2067 incr_metrics.advance = advance;
2068
2069 error = incr->funcs->get_glyph_metrics( incr->object,
2070 glyph_index,
2071 TRUE,
2072 &incr_metrics );
2073 if ( error )
2074 return error;
2075
2076 top = incr_metrics.bearing_y;
2077 advance = incr_metrics.advance;
2078 }
2079 }
2080
2081 /* GWW: Do vertical metrics get loaded incrementally too? */
2082
2083#endif /* FT_CONFIG_OPTION_INCREMENTAL */
2084
2085 glyph->linearVertAdvance = advance;
2086
2087 /* scale the metrics */
2088 if ( !( loader->load_flags & FT_LOAD_NO_SCALE ) )
2089 {
2090 top = FT_MulFix( top, y_scale );
2091 advance = FT_MulFix( advance, y_scale );
2092 }
2093
2094 /* XXX: for now, we have no better algorithm for the lsb, but it */
2095 /* should work fine. */
2096 /* */
2097 glyph->metrics.vertBearingX = SUB_LONG( glyph->metrics.horiBearingX,
2098 glyph->metrics.horiAdvance / 2 );
2099 glyph->metrics.vertBearingY = top;
2100 glyph->metrics.vertAdvance = advance;
2101 }
2102
2103 return FT_Err_Ok;
2104 }
2105
2106
2107#ifdef TT_CONFIG_OPTION_EMBEDDED_BITMAPS
2108
2109 static FT_Error
2110 load_sbit_image( TT_Size size,
2111 TT_GlyphSlot glyph,
2112 FT_UInt glyph_index,
2113 FT_Int32 load_flags )
2114 {
2115 TT_Face face = (TT_Face)glyph->face;
2116 SFNT_Service sfnt = (SFNT_Service)face->sfnt;
2117 FT_Error error;
2118 TT_SBit_MetricsRec sbit_metrics;
2119
2120
2121 error = sfnt->load_sbit_image( face,
2122 size->strike_index,
2123 glyph_index,
2124 (FT_UInt)load_flags,
2125 face->root.stream,
2126 &glyph->bitmap,
2127 &sbit_metrics );
2128 if ( !error )
2129 {
2130 glyph->metrics.width = (FT_Pos)sbit_metrics.width * 64;
2131 glyph->metrics.height = (FT_Pos)sbit_metrics.height * 64;
2132
2133 glyph->metrics.horiBearingX = (FT_Pos)sbit_metrics.horiBearingX * 64;
2134 glyph->metrics.horiBearingY = (FT_Pos)sbit_metrics.horiBearingY * 64;
2135 glyph->metrics.horiAdvance = (FT_Pos)sbit_metrics.horiAdvance * 64;
2136
2137 glyph->metrics.vertBearingX = (FT_Pos)sbit_metrics.vertBearingX * 64;
2138 glyph->metrics.vertBearingY = (FT_Pos)sbit_metrics.vertBearingY * 64;
2139 glyph->metrics.vertAdvance = (FT_Pos)sbit_metrics.vertAdvance * 64;
2140
2141 glyph->format = FT_GLYPH_FORMAT_BITMAP;
2142
2143 if ( load_flags & FT_LOAD_VERTICAL_LAYOUT )
2144 {
2145 glyph->bitmap_left = sbit_metrics.vertBearingX;
2146 glyph->bitmap_top = sbit_metrics.vertBearingY;
2147 }
2148 else
2149 {
2150 glyph->bitmap_left = sbit_metrics.horiBearingX;
2151 glyph->bitmap_top = sbit_metrics.horiBearingY;
2152 }
2153 }
2154 /* a missing glyph in a bitmap-only font is assumed whitespace */
2155 /* that needs to be constructed using metrics data from `hmtx' */
2156 /* and, optionally, `vmtx' tables */
2157 else if ( FT_ERR_EQ( error, Missing_Bitmap ) &&
2158 !FT_IS_SCALABLE( glyph->face ) &&
2159 face->horz_metrics_size )
2160 {
2161 FT_Fixed x_scale = size->root.metrics.x_scale;
2162 FT_Fixed y_scale = size->root.metrics.y_scale;
2163
2164 FT_Short left_bearing = 0;
2165 FT_Short top_bearing = 0;
2166
2167 FT_UShort advance_width = 0;
2168 FT_UShort advance_height = 0;
2169
2170
2171 TT_Get_HMetrics( face, glyph_index,
2172 &left_bearing,
2173 &advance_width );
2174 TT_Get_VMetrics( face, glyph_index,
2175 0,
2176 &top_bearing,
2177 &advance_height );
2178
2179 glyph->metrics.width = 0;
2180 glyph->metrics.height = 0;
2181
2182 glyph->metrics.horiBearingX = FT_MulFix( left_bearing, x_scale );
2183 glyph->metrics.horiBearingY = 0;
2184 glyph->metrics.horiAdvance = FT_MulFix( advance_width, x_scale );
2185
2186 glyph->metrics.vertBearingX = 0;
2187 glyph->metrics.vertBearingY = FT_MulFix( top_bearing, y_scale );
2188 glyph->metrics.vertAdvance = FT_MulFix( advance_height, y_scale );
2189
2190 glyph->format = FT_GLYPH_FORMAT_BITMAP;
2191 glyph->bitmap.pixel_mode = FT_PIXEL_MODE_MONO;
2192
2193 glyph->bitmap_left = 0;
2194 glyph->bitmap_top = 0;
2195
2196 error = FT_Err_Ok;
2197 }
2198
2199 return error;
2200 }
2201
2202#endif /* TT_CONFIG_OPTION_EMBEDDED_BITMAPS */
2203
2204
2205 static FT_Error
2206 tt_loader_init( TT_Loader loader,
2207 TT_Size size,
2208 TT_GlyphSlot glyph,
2209 FT_Int32 load_flags,
2210 FT_Bool glyf_table_only )
2211 {
2212 TT_Face face = (TT_Face)glyph->face;
2213
2214
2215 FT_ZERO( loader );
2216
2217#ifdef TT_USE_BYTECODE_INTERPRETER
2218
2219 /* load execution context */
2220 if ( IS_HINTED( load_flags ) && !glyf_table_only )
2221 {
2222 FT_Error error;
2223 TT_ExecContext exec;
2224 FT_Render_Mode mode = FT_LOAD_TARGET_MODE( load_flags );
2225 FT_Bool grayscale = FT_BOOL( mode != FT_RENDER_MODE_MONO );
2226 FT_Bool reexecute = FALSE;
2227#ifdef TT_SUPPORT_SUBPIXEL_HINTING_MINIMAL
2228 TT_Driver driver = (TT_Driver)FT_FACE_DRIVER( glyph->face );
2229#endif
2230
2231
2232 if ( size->bytecode_ready > 0 )
2233 return size->bytecode_ready;
2234 if ( size->bytecode_ready < 0 )
2235 {
2236 FT_Bool pedantic = FT_BOOL( load_flags & FT_LOAD_PEDANTIC );
2237
2238
2239 error = tt_size_init_bytecode( size, pedantic );
2240 if ( error )
2241 return error;
2242 }
2243
2244 exec = size->context;
2245
2246#ifdef TT_SUPPORT_SUBPIXEL_HINTING_MINIMAL
2247 /* reset backward compatibility; note that */
2248 /* the CVT program always runs without it */
2249 exec->backward_compatibility = 0;
2250
2251 if ( driver->interpreter_version == TT_INTERPRETER_VERSION_40 )
2252 {
2253 grayscale = FALSE;
2254
2255 /* any mode change requires a re-execution of the CVT program */
2256 if ( mode != exec->mode )
2257 {
2258 FT_TRACE4(( "tt_loader_init: render mode change,"
2259 " re-executing `prep' table\n" ));
2260
2261 exec->mode = mode;
2262 reexecute = TRUE;
2263 }
2264 }
2265#endif
2266
2267 /* a change from mono to grayscale rendering (and vice versa) */
2268 /* requires a re-execution of the CVT program */
2269 if ( grayscale != exec->grayscale )
2270 {
2271 FT_TRACE4(( "tt_loader_init: grayscale hinting change,"
2272 " re-executing `prep' table\n" ));
2273
2274 exec->grayscale = grayscale;
2275 reexecute = TRUE;
2276 }
2277
2278 if ( size->cvt_ready > 0 )
2279 return size->cvt_ready;
2280 if ( size->cvt_ready < 0 || reexecute )
2281 {
2282 error = tt_size_run_prep( size );
2283 if ( error )
2284 return error;
2285 }
2286
2287 TT_Load_Context( exec, face, size );
2288
2289 /* check whether the cvt program has disabled hinting */
2290 if ( size->GS.instruct_control & 1 )
2291 load_flags |= FT_LOAD_NO_HINTING;
2292
2293 /* check whether GS modifications should be reverted */
2294 if ( size->GS.instruct_control & 2 )
2295 size->GS = tt_default_graphics_state;
2296
2297#ifdef TT_SUPPORT_SUBPIXEL_HINTING_MINIMAL
2298 /*
2299 * Toggle backward compatibility according to what font wants, except
2300 * when
2301 *
2302 * 1) we have a `tricky' font that heavily relies on the interpreter to
2303 * render glyphs correctly, for example DFKai-SB, or
2304 * 2) FT_RENDER_MODE_MONO (i.e, monochrome rendering) is requested.
2305 *
2306 * In those cases, backward compatibility needs to be turned off to get
2307 * correct rendering. The rendering is then completely up to the
2308 * font's programming.
2309 *
2310 */
2311 if ( driver->interpreter_version == TT_INTERPRETER_VERSION_40 &&
2312 mode != FT_RENDER_MODE_MONO &&
2313 !FT_IS_TRICKY( glyph->face ) )
2314 exec->backward_compatibility = ( size->GS.instruct_control & 4 ) ^ 4;
2315
2316#endif /* TT_SUPPORT_SUBPIXEL_HINTING_MINIMAL */
2317
2318 loader->exec = exec;
2319
2320 /* Use the hdmx table if any unless FT_LOAD_COMPUTE_METRICS */
2321 /* is set or backward compatibility mode of the v38 or v40 */
2322 /* interpreters is active. See `ttinterp.h' for details on */
2323 /* backward compatibility mode. */
2324 if ( IS_HINTED( load_flags ) &&
2325 !( load_flags & FT_LOAD_COMPUTE_METRICS ) &&
2326#ifdef TT_SUPPORT_SUBPIXEL_HINTING_MINIMAL
2327 !exec->backward_compatibility &&
2328#endif
2329 !face->postscript.isFixedPitch )
2330 {
2331 loader->widthp = size->widthp;
2332 }
2333 else
2334 loader->widthp = NULL;
2335 }
2336
2337#endif /* TT_USE_BYTECODE_INTERPRETER */
2338
2339 /* get face's glyph loader */
2340 if ( !glyf_table_only )
2341 {
2342 FT_GlyphLoader gloader = glyph->internal->loader;
2343
2344
2345 FT_GlyphLoader_Rewind( gloader );
2346 loader->gloader = gloader;
2347 }
2348
2349 loader->load_flags = (FT_ULong)load_flags;
2350
2351 loader->face = face;
2352 loader->size = size;
2353 loader->glyph = (FT_GlyphSlot)glyph;
2354 loader->stream = face->root.stream;
2355
2356 loader->composites.head = NULL;
2357 loader->composites.tail = NULL;
2358
2359 return FT_Err_Ok;
2360 }
2361
2362
2363 static void
2364 tt_loader_done( TT_Loader loader )
2365 {
2366 FT_List_Finalize( &loader->composites,
2367 NULL,
2368 loader->face->root.memory,
2369 NULL );
2370 }
2371
2372
2373 /**************************************************************************
2374 *
2375 * @Function:
2376 * TT_Load_Glyph
2377 *
2378 * @Description:
2379 * A function used to load a single glyph within a given glyph slot,
2380 * for a given size.
2381 *
2382 * @InOut:
2383 * glyph ::
2384 * A handle to a target slot object where the glyph
2385 * will be loaded.
2386 *
2387 * @Input:
2388 * size ::
2389 * A handle to the source face size at which the glyph
2390 * must be scaled/loaded.
2391 *
2392 * glyph_index ::
2393 * The index of the glyph in the font file.
2394 *
2395 * load_flags ::
2396 * A flag indicating what to load for this glyph. The
2397 * FT_LOAD_XXX constants can be used to control the
2398 * glyph loading process (e.g., whether the outline
2399 * should be scaled, whether to load bitmaps or not,
2400 * whether to hint the outline, etc).
2401 *
2402 * @Return:
2403 * FreeType error code. 0 means success.
2404 */
2405 FT_LOCAL_DEF( FT_Error )
2406 TT_Load_Glyph( TT_Size size,
2407 TT_GlyphSlot glyph,
2408 FT_UInt glyph_index,
2409 FT_Int32 load_flags )
2410 {
2411 TT_Face face = (TT_Face)glyph->face;
2412 FT_Error error;
2413 TT_LoaderRec loader;
2414
2415
2416 FT_TRACE1(( "TT_Load_Glyph: glyph index %u\n", glyph_index ));
2417
2418#ifdef TT_CONFIG_OPTION_EMBEDDED_BITMAPS
2419
2420 /* try to load embedded bitmap (if any) */
2421 if ( size->strike_index != 0xFFFFFFFFUL &&
2422 !( load_flags & FT_LOAD_NO_BITMAP &&
2423 FT_IS_SCALABLE( glyph->face ) ) &&
2424 IS_DEFAULT_INSTANCE( glyph->face ) )
2425 {
2426 error = load_sbit_image( size, glyph, glyph_index, load_flags );
2427 if ( !error )
2428 {
2429 if ( FT_IS_SCALABLE( glyph->face ) ||
2430 FT_HAS_SBIX( glyph->face ) )
2431 {
2432 FT_Fixed x_scale = size->root.metrics.x_scale;
2433 FT_Fixed y_scale = size->root.metrics.y_scale;
2434
2435
2436 /* for the bbox we need the header only */
2437 (void)tt_loader_init( &loader, size, glyph, load_flags, TRUE );
2438 (void)load_truetype_glyph( &loader, glyph_index, 0, TRUE );
2439 tt_loader_done( &loader );
2440 glyph->linearHoriAdvance = loader.linear;
2441 glyph->linearVertAdvance = loader.vadvance;
2442
2443 /* Bitmaps from the 'sbix' table need special treatment: */
2444 /* if there is a glyph contour, the bitmap origin must be */
2445 /* shifted to be relative to the lower left corner of the */
2446 /* glyph bounding box, also taking the left-side bearing */
2447 /* (or top bearing) into account. */
2448 if ( face->sbit_table_type == TT_SBIT_TABLE_TYPE_SBIX &&
2449 loader.n_contours > 0 )
2450 {
2451 FT_Int bitmap_left;
2452 FT_Int bitmap_top;
2453
2454
2455 if ( load_flags & FT_LOAD_VERTICAL_LAYOUT )
2456 {
2457 /* This is a guess, since Apple's CoreText engine doesn't */
2458 /* really do vertical typesetting. */
2459 bitmap_left = loader.bbox.xMin;
2460 bitmap_top = loader.top_bearing;
2461 }
2462 else
2463 {
2464 bitmap_left = loader.left_bearing;
2465 bitmap_top = loader.bbox.yMin;
2466 }
2467
2468 glyph->bitmap_left += FT_MulFix( bitmap_left, x_scale ) >> 6;
2469 glyph->bitmap_top += FT_MulFix( bitmap_top, y_scale ) >> 6;
2470 }
2471
2472 /* sanity checks: if `xxxAdvance' in the sbit metric */
2473 /* structure isn't set, use `linearXXXAdvance' */
2474 if ( !glyph->metrics.horiAdvance && glyph->linearHoriAdvance )
2475 glyph->metrics.horiAdvance = FT_MulFix( glyph->linearHoriAdvance,
2476 x_scale );
2477 if ( !glyph->metrics.vertAdvance && glyph->linearVertAdvance )
2478 glyph->metrics.vertAdvance = FT_MulFix( glyph->linearVertAdvance,
2479 y_scale );
2480 }
2481
2482 goto Exit;
2483 }
2484 else if ( !FT_IS_SCALABLE( glyph->face ) )
2485 goto Exit;
2486 }
2487
2488 if ( load_flags & FT_LOAD_SBITS_ONLY )
2489 {
2490 error = FT_THROW( Invalid_Argument );
2491 goto Exit;
2492 }
2493
2494#endif /* TT_CONFIG_OPTION_EMBEDDED_BITMAPS */
2495
2496 /* if FT_LOAD_NO_SCALE is not set, `ttmetrics' must be valid */
2497 if ( !( load_flags & FT_LOAD_NO_SCALE ) && !size->ttmetrics.ppem )
2498 {
2499 error = FT_THROW( Invalid_Size_Handle );
2500 goto Exit;
2501 }
2502
2503#ifdef FT_CONFIG_OPTION_SVG
2504
2505 /* check for OT-SVG */
2506 if ( ( load_flags & FT_LOAD_NO_SVG ) == 0 &&
2507 ( load_flags & FT_LOAD_COLOR ) &&
2508 face->svg )
2509 {
2510 SFNT_Service sfnt = (SFNT_Service)face->sfnt;
2511
2512
2513 FT_TRACE3(( "Trying to load SVG glyph\n" ));
2514
2515 error = sfnt->load_svg_doc( glyph, glyph_index );
2516 if ( !error )
2517 {
2518 FT_Fixed x_scale = size->root.metrics.x_scale;
2519 FT_Fixed y_scale = size->root.metrics.y_scale;
2520
2521 FT_Short leftBearing;
2522 FT_Short topBearing;
2523 FT_UShort advanceX;
2524 FT_UShort advanceY;
2525
2526
2527 FT_TRACE3(( "Successfully loaded SVG glyph\n" ));
2528
2529 glyph->format = FT_GLYPH_FORMAT_SVG;
2530
2531 sfnt->get_metrics( face,
2532 FALSE,
2533 glyph_index,
2534 &leftBearing,
2535 &advanceX );
2536 sfnt->get_metrics( face,
2537 TRUE,
2538 glyph_index,
2539 &topBearing,
2540 &advanceY );
2541
2542 glyph->linearHoriAdvance = advanceX;
2543 glyph->linearVertAdvance = advanceY;
2544
2545 glyph->metrics.horiAdvance = FT_MulFix( advanceX, x_scale );
2546 glyph->metrics.vertAdvance = FT_MulFix( advanceY, y_scale );
2547
2548 goto Exit;
2549 }
2550
2551 FT_TRACE3(( "Failed to load SVG glyph\n" ));
2552 }
2553
2554 /* return immediately if we only want SVG glyphs */
2555 if ( load_flags & FT_LOAD_SVG_ONLY )
2556 {
2557 error = FT_THROW( Invalid_Argument );
2558 goto Exit;
2559 }
2560
2561#endif /* FT_CONFIG_OPTION_SVG */
2562
2563#ifdef TT_CONFIG_OPTION_VARC
2564
2565 /* check for VARC glyphs */
2566 if ( face->varc &&
2567 tt_face_has_varc_glyph( (FT_Face)face, glyph_index ) )
2568 {
2569 FT_TRACE3(( "Loading VARC glyph\n" ));
2570
2571 error = tt_face_load_varc_glyph( (FT_Face)face,
2572 (FT_GlyphSlot)glyph,
2573 glyph_index,
2574 load_flags );
2575 if ( !error )
2576 {
2577 FT_Short left_bearing = 0;
2578 FT_Short top_bearing = 0;
2579 FT_UShort advance_width = 0;
2580 FT_UShort advance_height = 0;
2581
2582
2583 /* Get advance width and height. */
2584 TT_Get_HMetrics( face, glyph_index,
2585 &left_bearing,
2586 &advance_width );
2587 TT_Get_VMetrics( face, glyph_index,
2588 0,
2589 &top_bearing,
2590 &advance_height );
2591
2592 glyph->linearHoriAdvance = advance_width;
2593 glyph->linearVertAdvance = advance_height;
2594
2595#ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
2596 {
2597 FT_Int advance = (FT_Int)advance_width;
2598
2599
2600 tt_hadvance_adjust( (FT_Face)face, glyph_index, &advance );
2601 advance_width = (FT_UShort)advance;
2602 }
2603#endif
2604
2605 {
2606 FT_BBox bbox;
2607
2608
2609 FT_Outline_Get_CBox( &glyph->outline, &bbox );
2610
2611 glyph->metrics.horiBearingX = bbox.xMin;
2612 glyph->metrics.horiBearingY = bbox.yMax;
2613 glyph->metrics.width = SUB_LONG( bbox.xMax, bbox.xMin );
2614 glyph->metrics.height = SUB_LONG( bbox.yMax, bbox.yMin );
2615 }
2616
2617 if ( load_flags & FT_LOAD_NO_SCALE )
2618 {
2619 /* Under NO_SCALE the advance is in font units (no 26.6 shift), */
2620 /* like the normal glyf path (pp2.x - pp1.x). */
2621 glyph->metrics.horiAdvance = advance_width;
2622 glyph->metrics.vertAdvance = advance_height;
2623 }
2624 else
2625 {
2626 glyph->metrics.horiAdvance = FT_MulFix( advance_width,
2627 size->metrics->x_scale );
2628 glyph->metrics.vertAdvance = FT_MulFix( advance_height,
2629 size->metrics->y_scale );
2630 }
2631
2632 FT_TRACE3(( "Successfully loaded VARC glyph\n" ));
2633 goto Exit;
2634 }
2635
2636 FT_TRACE3(( "Failed to load VARC glyph, falling back to glyf\n" ));
2637 }
2638
2639#endif /* TT_CONFIG_OPTION_VARC */
2640
2641 error = tt_loader_init( &loader, size, glyph, load_flags, FALSE );
2642 if ( error )
2643 goto Exit;
2644
2645 /* done if we are only interested in the `hdmx` advance */
2646 if ( load_flags & FT_LOAD_ADVANCE_ONLY &&
2647 !( load_flags & FT_LOAD_VERTICAL_LAYOUT ) &&
2648 loader.widthp )
2649 {
2650 glyph->metrics.horiAdvance = loader.widthp[glyph_index] * 64;
2651 goto Done;
2652 }
2653
2654 /* main loading loop */
2655 error = load_truetype_glyph( &loader, glyph_index, 0, FALSE );
2656 if ( !error )
2657 {
2658 if ( glyph->format == FT_GLYPH_FORMAT_COMPOSITE )
2659 {
2660 glyph->num_subglyphs = loader.gloader->base.num_subglyphs;
2661 glyph->subglyphs = loader.gloader->base.subglyphs;
2662 }
2663 else
2664 {
2665 glyph->format = FT_GLYPH_FORMAT_OUTLINE;
2666
2667 glyph->outline = loader.gloader->base.outline;
2668 glyph->outline.flags &= ~FT_OUTLINE_SINGLE_PASS;
2669
2670 /* Set the `high precision' bit flag. This is _critical_ to */
2671 /* get correct output for monochrome TrueType glyphs at all */
2672 /* sizes using the bytecode interpreter. */
2673 if ( !( load_flags & FT_LOAD_NO_SCALE ) &&
2674 size->metrics->y_ppem < 24 )
2675 glyph->outline.flags |= FT_OUTLINE_HIGH_PRECISION;
2676
2677 /* Translate array so that (0,0) is the glyph's origin. Note */
2678 /* that this behaviour is independent on the value of bit 1 of */
2679 /* the `flags' field in the `head' table -- at least major */
2680 /* applications like Acroread indicate that. */
2681 if ( loader.pp1.x )
2682 FT_Outline_Translate( &glyph->outline, -loader.pp1.x, 0 );
2683 }
2684
2685#ifdef TT_USE_BYTECODE_INTERPRETER
2686
2687 if ( IS_HINTED( load_flags ) )
2688 {
2689 glyph->control_data = loader.exec->glyphIns;
2690 glyph->control_len = loader.exec->glyphSize;
2691
2692 if ( loader.exec->GS.scan_control )
2693 {
2694 /* convert scan conversion mode to FT_OUTLINE_XXX flags */
2695 switch ( loader.exec->GS.scan_type )
2696 {
2697 case 0: /* simple drop-outs including stubs */
2698 glyph->outline.flags |= FT_OUTLINE_INCLUDE_STUBS;
2699 break;
2700 case 1: /* simple drop-outs excluding stubs */
2701 /* nothing; it's the default rendering mode */
2702 break;
2703 case 4: /* smart drop-outs including stubs */
2704 glyph->outline.flags |= FT_OUTLINE_SMART_DROPOUTS |
2705 FT_OUTLINE_INCLUDE_STUBS;
2706 break;
2707 case 5: /* smart drop-outs excluding stubs */
2708 glyph->outline.flags |= FT_OUTLINE_SMART_DROPOUTS;
2709 break;
2710
2711 default: /* no drop-out control */
2712 glyph->outline.flags |= FT_OUTLINE_IGNORE_DROPOUTS;
2713 break;
2714 }
2715 }
2716 else
2717 glyph->outline.flags |= FT_OUTLINE_IGNORE_DROPOUTS;
2718 }
2719
2720#endif /* TT_USE_BYTECODE_INTERPRETER */
2721
2722 error = compute_glyph_metrics( &loader, glyph_index );
2723 }
2724
2725 FT_TRACE1(( " subglyphs = %u, contours = %hu, points = %hu,"
2726 " flags = 0x%.3x\n",
2727 loader.gloader->base.num_subglyphs,
2728 glyph->outline.n_contours,
2729 glyph->outline.n_points,
2730 glyph->outline.flags ));
2731
2732 Done:
2733 tt_loader_done( &loader );
2734
2735 Exit:
2736 FT_TRACE1(( error ? " failed (error code 0x%x)\n" : "",
2737 error ));
2738
2739 return error;
2740 }
2741
2742
2743/* END */