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