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