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