ttgload.c revision ac812959bbe118d512d955d6d9b6a2f54c4b5522
1/***************************************************************************/
2/*                                                                         */
3/*  ttgload.c                                                              */
4/*                                                                         */
5/*    TrueType Glyph Loader (body).                                        */
6/*                                                                         */
7/*  Copyright 1996-2001 by                                                 */
8/*  David Turner, Robert Wilhelm, and Werner Lemberg.                      */
9/*                                                                         */
10/*  This file is part of the FreeType project, and may only be used,       */
11/*  modified, and distributed under the terms of the FreeType project      */
12/*  license, LICENSE.TXT.  By continuing to use, modify, or distribute     */
13/*  this file you indicate that you have read the license and              */
14/*  understand and accept it fully.                                        */
15/*                                                                         */
16/***************************************************************************/
17
18
19#include <ft2build.h>
20#include FT_INTERNAL_DEBUG_H
21#include FT_INTERNAL_CALC_H
22#include FT_INTERNAL_STREAM_H
23#include FT_INTERNAL_SFNT_H
24#include FT_TRUETYPE_TAGS_H
25#include FT_OUTLINE_H
26
27#include "ttgload.h"
28
29#include "tterrors.h"
30
31
32  /*************************************************************************/
33  /*                                                                       */
34  /* The macro FT_COMPONENT is used in trace mode.  It is an implicit      */
35  /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log  */
36  /* messages during execution.                                            */
37  /*                                                                       */
38#undef  FT_COMPONENT
39#define FT_COMPONENT  trace_ttgload
40
41
42  /*************************************************************************/
43  /*                                                                       */
44  /* Composite font flags.                                                 */
45  /*                                                                       */
46#define ARGS_ARE_WORDS       0x001
47#define ARGS_ARE_XY_VALUES   0x002
48#define ROUND_XY_TO_GRID     0x004
49#define WE_HAVE_A_SCALE      0x008
50/* reserved                  0x010 */
51#define MORE_COMPONENTS      0x020
52#define WE_HAVE_AN_XY_SCALE  0x040
53#define WE_HAVE_A_2X2        0x080
54#define WE_HAVE_INSTR        0x100
55#define USE_MY_METRICS       0x200
56
57
58
59  /*************************************************************************/
60  /*                                                                       */
61  /* <Function>                                                            */
62  /*    TT_Get_Metrics                                                     */
63  /*                                                                       */
64  /* <Description>                                                         */
65  /*    Returns the horizontal or vertical metrics in font units for a     */
66  /*    given glyph.  The metrics are the left side bearing (resp. top     */
67  /*    side bearing) and advance width (resp. advance height).            */
68  /*                                                                       */
69  /* <Input>                                                               */
70  /*    header  :: A pointer to either the horizontal or vertical metrics  */
71  /*               structure.                                              */
72  /*                                                                       */
73  /*    index   :: The glyph index.                                        */
74  /*                                                                       */
75  /* <Output>                                                              */
76  /*    bearing :: The bearing, either left side or top side.              */
77  /*                                                                       */
78  /*    advance :: The advance width resp. advance height.                 */
79  /*                                                                       */
80  /* <Note>                                                                */
81  /*    This function will much probably move to another component in the  */
82  /*    near future, but I haven't decided which yet.                      */
83  /*                                                                       */
84  FT_LOCAL_DEF void
85  TT_Get_Metrics( TT_HoriHeader*  header,
86                  FT_UInt         index,
87                  FT_Short*       bearing,
88                  FT_UShort*      advance )
89  {
90    TT_LongMetrics*  longs_m;
91    FT_UShort        k = header->number_Of_HMetrics;
92
93
94    if ( index < (FT_UInt)k )
95    {
96      longs_m  = (TT_LongMetrics*)header->long_metrics + index;
97      *bearing = longs_m->bearing;
98      *advance = longs_m->advance;
99    }
100    else
101    {
102      *bearing = ((TT_ShortMetrics*)header->short_metrics)[index - k];
103      *advance = ((TT_LongMetrics*)header->long_metrics)[k - 1].advance;
104    }
105  }
106
107
108  /*************************************************************************/
109  /*                                                                       */
110  /* Returns the horizontal metrics in font units for a given glyph.  If   */
111  /* `check' is true, take care of monospaced fonts by returning the       */
112  /* advance width maximum.                                                */
113  /*                                                                       */
114  static void
115  Get_HMetrics( TT_Face     face,
116                FT_UInt     index,
117                FT_Bool     check,
118                FT_Short*   lsb,
119                FT_UShort*  aw )
120  {
121    TT_Get_Metrics( &face->horizontal, index, lsb, aw );
122
123    if ( check && face->postscript.isFixedPitch )
124      *aw = face->horizontal.advance_Width_Max;
125  }
126
127
128  /*************************************************************************/
129  /*                                                                       */
130  /*    Returns the advance width table for a given pixel size if it is    */
131  /*    found in the font's `hdmx' table (if any).                         */
132  /*                                                                       */
133  static FT_Byte*
134  Get_Advance_Widths( TT_Face    face,
135                      FT_UShort  ppem )
136  {
137    FT_UShort  n;
138
139    for ( n = 0; n < face->hdmx.num_records; n++ )
140      if ( face->hdmx.records[n].ppem == ppem )
141        return face->hdmx.records[n].widths;
142
143    return NULL;
144  }
145
146
147#define cur_to_org( n, zone ) \
148          MEM_Copy( (zone)->org, (zone)->cur, (n) * sizeof ( FT_Vector ) )
149
150#define org_to_cur( n, zone ) \
151          MEM_Copy( (zone)->cur, (zone)->org, (n) * sizeof ( FT_Vector ) )
152
153
154  /*************************************************************************/
155  /*                                                                       */
156  /*    Translates an array of coordinates.                                */
157  /*                                                                       */
158  static void
159  translate_array( FT_UInt     n,
160                   FT_Vector*  coords,
161                   FT_Pos      delta_x,
162                   FT_Pos      delta_y )
163  {
164    FT_UInt  k;
165
166
167    if ( delta_x )
168      for ( k = 0; k < n; k++ )
169        coords[k].x += delta_x;
170
171    if ( delta_y )
172      for ( k = 0; k < n; k++ )
173        coords[k].y += delta_y;
174  }
175
176
177  static void
178  tt_prepare_zone( TT_GlyphZone*  zone,
179                   FT_GlyphLoad*  load,
180                   FT_UInt        start_point,
181                   FT_UInt        start_contour )
182  {
183    zone->n_points   = (FT_UShort)( load->outline.n_points - start_point );
184    zone->n_contours = (FT_Short) ( load->outline.n_contours - start_contour );
185    zone->org        = load->extra_points + start_point;
186    zone->cur        = load->outline.points + start_point;
187    zone->tags       = (FT_Byte*)load->outline.tags + start_point;
188    zone->contours   = (FT_UShort*)load->outline.contours + start_contour;
189  }
190
191
192#undef  IS_HINTED
193#define IS_HINTED( flags )  ( ( flags & FT_LOAD_NO_HINTING ) == 0 )
194
195
196  /*************************************************************************/
197  /*                                                                       */
198  /*  The following functions are used by default with TrueType fonts.     */
199  /*  However, they can be replaced by alternatives if we need to support  */
200  /*  TrueType-compressed formats (like MicroType) in the future.          */
201  /*                                                                       */
202  /*************************************************************************/
203
204  FT_CALLBACK_DEF( FT_Error )
205  TT_Access_Glyph_Frame( TT_Loader*  loader,
206                         FT_UInt     glyph_index,
207                         FT_ULong    offset,
208                         FT_UInt     byte_count )
209  {
210    FT_Error   error;
211    FT_Stream  stream = loader->stream;
212
213    /* for non-debug mode */
214    FT_UNUSED( glyph_index );
215
216
217    FT_TRACE5(( "Glyph %ld\n", glyph_index ));
218
219    /* the following line sets the `error' variable through macros! */
220    if ( FILE_Seek( offset ) || ACCESS_Frame( byte_count ) )
221      return error;
222
223    return TT_Err_Ok;
224  }
225
226
227  FT_CALLBACK_DEF( void )
228  TT_Forget_Glyph_Frame( TT_Loader*  loader )
229  {
230    FT_Stream  stream = loader->stream;
231
232
233    FORGET_Frame();
234  }
235
236
237  FT_CALLBACK_DEF( FT_Error )
238  TT_Load_Glyph_Header( TT_Loader*  loader )
239  {
240    FT_Stream   stream   = loader->stream;
241    FT_Int      byte_len = loader->byte_len - 10;
242
243
244    if ( byte_len < 0 )
245      return TT_Err_Invalid_Outline;
246
247    loader->n_contours = GET_Short();
248
249    loader->bbox.xMin = GET_Short();
250    loader->bbox.yMin = GET_Short();
251    loader->bbox.xMax = GET_Short();
252    loader->bbox.yMax = GET_Short();
253
254    FT_TRACE5(( "  # of contours: %d\n", loader->n_contours ));
255    FT_TRACE5(( "  xMin: %4d  xMax: %4d\n", loader->bbox.xMin,
256                                            loader->bbox.xMax ));
257    FT_TRACE5(( "  yMin: %4d  yMax: %4d\n", loader->bbox.yMin,
258                                            loader->bbox.yMax ));
259    loader->byte_len = byte_len;
260
261    return TT_Err_Ok;
262  }
263
264
265  FT_CALLBACK_DEF( FT_Error )
266  TT_Load_Simple_Glyph( TT_Loader*  load )
267  {
268    FT_Error         error;
269    FT_Stream        stream     = load->stream;
270    FT_GlyphLoader*  gloader    = load->gloader;
271    FT_Int           n_contours = load->n_contours;
272    FT_Outline*      outline;
273    TT_Face          face    = (TT_Face)load->face;
274    TT_GlyphSlot     slot    = (TT_GlyphSlot)load->glyph;
275    FT_UShort        n_ins;
276    FT_Int           n, n_points;
277    FT_Int           byte_len = load->byte_len;
278
279
280    /* reading the contours endpoints & number of points */
281    {
282      short*  cur   = gloader->current.outline.contours;
283      short*  limit = cur + n_contours;
284
285
286      /* check space for contours array + instructions count */
287      byte_len -= 2 * ( n_contours + 1 );
288      if ( byte_len < 0 )
289        goto Invalid_Outline;
290
291      for ( ; cur < limit; cur++ )
292        cur[0] = GET_UShort();
293
294      n_points = 0;
295      if ( n_contours > 0 )
296        n_points = cur[-1] + 1;
297
298      error = FT_GlyphLoader_Check_Points( gloader, n_points + 2, 0 );
299      if ( error )
300        goto Fail;
301
302      /* we'd better check the contours table right now */
303      outline = &gloader->current.outline;
304
305      for ( cur = outline->contours + 1; cur < limit; cur++ )
306        if ( cur[-1] >= cur[0] )
307          goto Invalid_Outline;
308    }
309
310    /* reading the bytecode instructions */
311    slot->control_len  = 0;
312    slot->control_data = 0;
313
314    n_ins = GET_UShort();
315
316    FT_TRACE5(( "  Instructions size: %d\n", n_ins ));
317
318    if ( n_ins > face->max_profile.maxSizeOfInstructions )
319    {
320      FT_TRACE0(( "ERROR: Too many instructions!\n" ));
321      error = TT_Err_Too_Many_Hints;
322      goto Fail;
323    }
324
325    byte_len -= n_ins;
326    if ( byte_len < 0 )
327    {
328      FT_TRACE0(( "ERROR: Instruction count mismatch!\n" ));
329      error = TT_Err_Too_Many_Hints;
330      goto Fail;
331    }
332
333#ifdef TT_CONFIG_OPTION_BYTECODE_INTERPRETER
334
335    if ( ( load->load_flags                        &
336         ( FT_LOAD_NO_SCALE | FT_LOAD_NO_HINTING ) ) == 0 &&
337           load->instructions )
338    {
339      slot->control_len  = n_ins;
340      slot->control_data = load->instructions;
341
342      MEM_Copy( load->instructions, stream->cursor, n_ins );
343    }
344
345#endif /* TT_CONFIG_OPTION_BYTECODE_INTERPRETER */
346
347    stream->cursor += n_ins;
348
349    /* reading the point tags */
350    {
351      FT_Byte*  flag  = (FT_Byte*)outline->tags;
352      FT_Byte*  limit = flag + n_points;
353      FT_Byte   c, count;
354
355
356      while ( flag < limit )
357      {
358        if ( --byte_len < 0 )
359          goto Invalid_Outline;
360
361        *flag++ = c = GET_Byte();
362        if ( c & 8 )
363        {
364          if ( --byte_len < 0 )
365            goto Invalid_Outline;
366
367          count = GET_Byte();
368          if ( flag + count > limit )
369            goto Invalid_Outline;
370
371          for ( ; count > 0; count-- )
372            *flag++ = c;
373        }
374      }
375
376      /* check that there is enough room to load the coordinates */
377      for ( flag = (FT_Byte*)outline->tags; flag < limit; flag++ )
378      {
379        if ( *flag & 2 )
380          byte_len -= 1;
381        else if ( ( *flag & 16 ) == 0 )
382          byte_len -= 2;
383
384        if ( *flag & 4 )
385          byte_len -= 1;
386        else if ( ( *flag & 32 ) == 0 )
387          byte_len -= 2;
388      }
389
390      if ( byte_len < 0 )
391        goto Invalid_Outline;
392    }
393
394    /* reading the X coordinates */
395
396    {
397      FT_Vector*  vec   = outline->points;
398      FT_Vector*  limit = vec + n_points;
399      FT_Byte*    flag  = (FT_Byte*)outline->tags;
400      FT_Pos      x     = 0;
401
402
403      for ( ; vec < limit; vec++, flag++ )
404      {
405        FT_Pos  y = 0;
406
407
408        if ( *flag & 2 )
409        {
410          y = GET_Byte();
411          if ( ( *flag & 16 ) == 0 )
412            y = -y;
413        }
414        else if ( ( *flag & 16 ) == 0 )
415          y = GET_Short();
416
417        x     += y;
418        vec->x = x;
419      }
420    }
421
422    /* reading the Y coordinates */
423
424    {
425      FT_Vector*  vec   = gloader->current.outline.points;
426      FT_Vector*  limit = vec + n_points;
427      FT_Byte*    flag  = (FT_Byte*)outline->tags;
428      FT_Pos      x     = 0;
429
430
431      for ( ; vec < limit; vec++, flag++ )
432      {
433        FT_Pos  y = 0;
434
435
436        if ( *flag & 4 )
437        {
438          y = GET_Byte();
439          if ( ( *flag & 32 ) == 0 )
440            y = -y;
441        }
442        else if ( ( *flag & 32 ) == 0 )
443          y = GET_Short();
444
445        x     += y;
446        vec->y = x;
447      }
448    }
449
450    /* clear the touch tags */
451    for ( n = 0; n < n_points; n++ )
452      outline->tags[n] &= FT_Curve_Tag_On;
453
454    outline->n_points   = (FT_UShort)n_points;
455    outline->n_contours = (FT_Short) n_contours;
456
457    load->byte_len = byte_len;
458
459  Fail:
460    return error;
461
462  Invalid_Outline:
463    error = TT_Err_Invalid_Outline;
464    goto Fail;
465  }
466
467
468  FT_CALLBACK_DEF( FT_Error )
469  TT_Load_Composite_Glyph( TT_Loader*  loader )
470  {
471    FT_Error         error;
472    FT_Stream        stream  = loader->stream;
473    FT_GlyphLoader*  gloader = loader->gloader;
474    FT_SubGlyph*     subglyph;
475    FT_UInt          num_subglyphs;
476    FT_Int           byte_len = loader->byte_len;
477
478
479    num_subglyphs = 0;
480
481    do
482    {
483      FT_Fixed  xx, xy, yy, yx;
484
485
486      /* check that we can load a new subglyph */
487      error = FT_GlyphLoader_Check_Subglyphs( gloader, num_subglyphs + 1 );
488      if ( error )
489        goto Fail;
490
491      /* check space */
492      byte_len -= 4;
493      if ( byte_len < 0 )
494        goto Invalid_Composite;
495
496      subglyph = gloader->current.subglyphs + num_subglyphs;
497
498      subglyph->arg1 = subglyph->arg2 = 0;
499
500      subglyph->flags = GET_UShort();
501      subglyph->index = GET_UShort();
502
503      /* check space */
504      byte_len -= 2;
505      if ( subglyph->flags & ARGS_ARE_WORDS )
506        byte_len -= 2;
507      if ( subglyph->flags & WE_HAVE_A_SCALE )
508        byte_len -= 2;
509      else if ( subglyph->flags & WE_HAVE_AN_XY_SCALE )
510        byte_len -= 4;
511      else if ( subglyph->flags & WE_HAVE_A_2X2 )
512        byte_len -= 8;
513
514      if ( byte_len < 0 )
515        goto Invalid_Composite;
516
517      /* read arguments */
518      if ( subglyph->flags & ARGS_ARE_WORDS )
519      {
520        subglyph->arg1 = GET_Short();
521        subglyph->arg2 = GET_Short();
522      }
523      else
524      {
525        subglyph->arg1 = GET_Char();
526        subglyph->arg2 = GET_Char();
527      }
528
529      /* read transform */
530      xx = yy = 0x10000L;
531      xy = yx = 0;
532
533      if ( subglyph->flags & WE_HAVE_A_SCALE )
534      {
535        xx = (FT_Fixed)GET_Short() << 2;
536        yy = xx;
537      }
538      else if ( subglyph->flags & WE_HAVE_AN_XY_SCALE )
539      {
540        xx = (FT_Fixed)GET_Short() << 2;
541        yy = (FT_Fixed)GET_Short() << 2;
542      }
543      else if ( subglyph->flags & WE_HAVE_A_2X2 )
544      {
545        xx = (FT_Fixed)GET_Short() << 2;
546        xy = (FT_Fixed)GET_Short() << 2;
547        yx = (FT_Fixed)GET_Short() << 2;
548        yy = (FT_Fixed)GET_Short() << 2;
549      }
550
551      subglyph->transform.xx = xx;
552      subglyph->transform.xy = xy;
553      subglyph->transform.yx = yx;
554      subglyph->transform.yy = yy;
555
556      num_subglyphs++;
557
558    } while ( subglyph->flags & MORE_COMPONENTS );
559
560    gloader->current.num_subglyphs = num_subglyphs;
561
562#ifdef TT_CONFIG_OPTION_BYTECODE_INTERPRETER
563    {
564      /* we must undo the ACCESS_Frame in order to point to the */
565      /* composite instructions, if we find some.               */
566      /* we will process them later...                          */
567      /*                                                        */
568      loader->ins_pos = (FT_ULong)( FILE_Pos() +
569                                    stream->cursor - stream->limit );
570    }
571#endif
572
573    loader->byte_len = byte_len;
574
575  Fail:
576    return error;
577
578  Invalid_Composite:
579    error = TT_Err_Invalid_Composite;
580    goto Fail;
581  }
582
583
584  FT_LOCAL_DEF void
585  TT_Init_Glyph_Loading( TT_Face  face )
586  {
587    face->access_glyph_frame   = TT_Access_Glyph_Frame;
588    face->read_glyph_header    = TT_Load_Glyph_Header;
589    face->read_simple_glyph    = TT_Load_Simple_Glyph;
590    face->read_composite_glyph = TT_Load_Composite_Glyph;
591    face->forget_glyph_frame   = TT_Forget_Glyph_Frame;
592  }
593
594
595  /*************************************************************************/
596  /*                                                                       */
597  /* <Function>                                                            */
598  /*    TT_Process_Simple_Glyph                                            */
599  /*                                                                       */
600  /* <Description>                                                         */
601  /*    Once a simple glyph has been loaded, it needs to be processed.     */
602  /*    Usually, this means scaling and hinting through bytecode           */
603  /*    interpretation.                                                    */
604  /*                                                                       */
605  static FT_Error
606  TT_Process_Simple_Glyph( TT_Loader*  load,
607                           FT_Bool     debug )
608  {
609    FT_GlyphLoader*  gloader  = load->gloader;
610    FT_Outline*      outline  = &gloader->current.outline;
611    FT_UInt          n_points = outline->n_points;
612    FT_UInt          n_ins;
613    TT_GlyphZone*    zone     = &load->zone;
614    FT_Error         error    = TT_Err_Ok;
615
616    FT_UNUSED( debug );  /* used by truetype interpreter only */
617
618
619    n_ins = load->glyph->control_len;
620
621    /* add shadow points */
622
623    /* Now add the two shadow points at n and n + 1.    */
624    /* We need the left side bearing and advance width. */
625
626    {
627      FT_Vector*  pp1;
628      FT_Vector*  pp2;
629
630
631      /* pp1 = xMin - lsb */
632      pp1    = outline->points + n_points;
633      pp1->x = load->bbox.xMin - load->left_bearing;
634      pp1->y = 0;
635
636      /* pp2 = pp1 + aw */
637      pp2    = pp1 + 1;
638      pp2->x = pp1->x + load->advance;
639      pp2->y = 0;
640
641      outline->tags[n_points    ] = 0;
642      outline->tags[n_points + 1] = 0;
643    }
644
645    /* Note that we return two more points that are not */
646    /* part of the glyph outline.                       */
647
648    n_points += 2;
649
650    /* set up zone for hinting */
651    tt_prepare_zone( zone, &gloader->current, 0, 0 );
652
653    /* eventually scale the glyph */
654    if ( !( load->load_flags & FT_LOAD_NO_SCALE ) )
655    {
656      FT_Vector*  vec     = zone->cur;
657      FT_Vector*  limit   = vec + n_points;
658      FT_Fixed    x_scale = load->size->metrics.x_scale;
659      FT_Fixed    y_scale = load->size->metrics.y_scale;
660
661
662      /* first scale the glyph points */
663      for ( ; vec < limit; vec++ )
664      {
665        vec->x = FT_MulFix( vec->x, x_scale );
666        vec->y = FT_MulFix( vec->y, y_scale );
667      }
668    }
669
670    cur_to_org( n_points, zone );
671
672    /* eventually hint the glyph */
673    if ( IS_HINTED( load->load_flags ) )
674    {
675      FT_Pos  x = zone->org[n_points-2].x;
676
677
678      x = ( ( x + 32 ) & -64 ) - x;
679      translate_array( n_points, zone->org, x, 0 );
680
681      org_to_cur( n_points, zone );
682
683      zone->cur[n_points - 1].x = ( zone->cur[n_points - 1].x + 32 ) & -64;
684
685#ifdef TT_CONFIG_OPTION_BYTECODE_INTERPRETER
686
687      /* now consider hinting */
688      if ( n_ins > 0 )
689      {
690        error = TT_Set_CodeRange( load->exec, tt_coderange_glyph,
691                                  load->exec->glyphIns, n_ins );
692        if ( error )
693          goto Exit;
694
695        load->exec->is_composite     = FALSE;
696        load->exec->pedantic_hinting = (FT_Bool)( load->load_flags &
697                                                  FT_LOAD_PEDANTIC );
698        load->exec->pts              = *zone;
699        load->exec->pts.n_points    += 2;
700
701        error = TT_Run_Context( load->exec, debug );
702        if ( error && load->exec->pedantic_hinting )
703          goto Exit;
704
705        error = TT_Err_Ok;  /* ignore bytecode errors in non-pedantic mode */
706      }
707
708#endif /* TT_CONFIG_OPTION_BYTECODE_INTERPRETER */
709
710    }
711
712    /* save glyph phantom points */
713    if ( !load->preserve_pps )
714    {
715      load->pp1 = zone->cur[n_points - 2];
716      load->pp2 = zone->cur[n_points - 1];
717    }
718
719#ifdef TT_CONFIG_OPTION_BYTECODE_INTERPRETER
720  Exit:
721#endif
722    return error;
723  }
724
725
726  /*************************************************************************/
727  /*                                                                       */
728  /* <Function>                                                            */
729  /*    load_truetype_glyph                                                */
730  /*                                                                       */
731  /* <Description>                                                         */
732  /*    Loads a given truetype glyph.  Handles composites and uses a       */
733  /*    TT_Loader object.                                                  */
734  /*                                                                       */
735  static FT_Error
736  load_truetype_glyph( TT_Loader*  loader,
737                       FT_UInt     glyph_index )
738  {
739
740#ifdef TT_CONFIG_OPTION_BYTECODE_INTERPRETER
741    FT_Stream        stream = loader->stream;
742#endif
743
744    FT_Error         error;
745    TT_Face          face   = (TT_Face)loader->face;
746    FT_ULong         offset;
747    FT_Int           contours_count;
748    FT_UInt          index, num_points, count;
749    FT_Fixed         x_scale, y_scale;
750    FT_GlyphLoader*  gloader = loader->gloader;
751    FT_Bool          opened_frame = 0;
752
753
754    /* check glyph index */
755    index = glyph_index;
756    if ( index >= (FT_UInt)face->root.num_glyphs )
757    {
758      error = TT_Err_Invalid_Glyph_Index;
759      goto Exit;
760    }
761
762    loader->glyph_index = glyph_index;
763    num_points   = 0;
764
765    x_scale = 0x10000L;
766    y_scale = 0x10000L;
767    if ( ( loader->load_flags & FT_LOAD_NO_SCALE ) == 0 )
768    {
769      x_scale = loader->size->metrics.x_scale;
770      y_scale = loader->size->metrics.y_scale;
771    }
772
773    /* get horizontal metrics */
774    {
775      FT_Short   left_bearing;
776      FT_UShort  advance_width;
777
778
779      Get_HMetrics( face, index,
780                    (FT_Bool)!(loader->load_flags &
781                                FT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH),
782                    &left_bearing,
783                    &advance_width );
784
785      loader->left_bearing = left_bearing;
786      loader->advance      = advance_width;
787
788      if ( !loader->linear_def )
789      {
790        loader->linear_def = 1;
791        loader->linear     = advance_width;
792      }
793    }
794
795    offset = face->glyph_locations[index];
796    count  = 0;
797
798    if ( index < (FT_UInt)face->num_locations - 1 )
799       count = face->glyph_locations[index + 1] - offset;
800
801    if ( count == 0 )
802    {
803      /* as described by Frederic Loyer, these are spaces, and */
804      /* not the unknown glyph.                                */
805      loader->bbox.xMin = 0;
806      loader->bbox.xMax = 0;
807      loader->bbox.yMin = 0;
808      loader->bbox.yMax = 0;
809
810      loader->pp1.x = 0;
811      loader->pp2.x = loader->advance;
812
813      if ( ( loader->load_flags & FT_LOAD_NO_SCALE ) == 0 )
814        loader->pp2.x = FT_MulFix( loader->pp2.x, x_scale );
815
816#ifdef TT_CONFIG_OPTION_BYTECODE_INTERPRETER
817
818      if ( loader->exec )
819        loader->exec->glyphSize = 0;
820
821#endif
822
823      error = TT_Err_Ok;
824      goto Exit;
825    }
826
827    loader->byte_len = (FT_Int)count;
828
829#if 0
830    /* temporary hack */
831    if ( count < 10 )
832    {
833      /* This glyph is corrupted -- it does not have a complete header */
834      error = TT_Err_Invalid_Outline;
835      goto Fail;
836    }
837#endif
838
839    offset = loader->glyf_offset + offset;
840
841    /* access glyph frame */
842    error = face->access_glyph_frame( loader, glyph_index, offset, count );
843    if ( error )
844      goto Exit;
845
846    opened_frame = 1;
847
848    /* read first glyph header */
849    error = face->read_glyph_header( loader );
850    if ( error )
851      goto Fail;
852
853    contours_count = loader->n_contours;
854
855    count -= 10;
856
857    loader->pp1.x = loader->bbox.xMin - loader->left_bearing;
858    loader->pp1.y = 0;
859    loader->pp2.x = loader->pp1.x + loader->advance;
860    loader->pp2.y = 0;
861
862    if ( ( loader->load_flags & FT_LOAD_NO_SCALE ) == 0 )
863    {
864      loader->pp1.x = FT_MulFix( loader->pp1.x, x_scale );
865      loader->pp2.x = FT_MulFix( loader->pp2.x, x_scale );
866    }
867
868    /***********************************************************************/
869    /***********************************************************************/
870    /***********************************************************************/
871
872    /* if it is a simple glyph, load it */
873
874    if ( contours_count >= 0 )
875    {
876      /* check that we can add the contours to the glyph */
877      error = FT_GlyphLoader_Check_Points( gloader, 0, contours_count );
878      if ( error )
879        goto Fail;
880
881      error = face->read_simple_glyph( loader );
882      if ( error )
883        goto Fail;
884
885#ifdef TT_CONFIG_OPTION_BYTECODE_INTERPRETER
886
887      {
888        TT_Size size = (TT_Size)loader->size;
889
890
891        error = TT_Process_Simple_Glyph( loader,
892                                         (FT_Bool)( size && size->debug ) );
893      }
894
895#else
896
897      error = TT_Process_Simple_Glyph( loader, 0 );
898
899#endif
900
901      if ( error )
902        goto Fail;
903
904      FT_GlyphLoader_Add( gloader );
905
906      /* Note: We could have put the simple loader source there */
907      /*       but the code is fat enough already :-)           */
908    }
909
910    /***********************************************************************/
911    /***********************************************************************/
912    /***********************************************************************/
913
914    /* otherwise, load a composite! */
915    else
916    {
917      TT_GlyphSlot  glyph = (TT_GlyphSlot)loader->glyph;
918      FT_UInt       start_point, start_contour;
919      FT_ULong      ins_pos;  /* position of composite instructions, if any */
920
921
922      /* for each subglyph, read composite header */
923      start_point   = gloader->base.outline.n_points;
924      start_contour = gloader->base.outline.n_contours;
925
926      error = face->read_composite_glyph( loader );
927      if ( error )
928        goto Fail;
929
930      ins_pos = loader->ins_pos;
931      face->forget_glyph_frame( loader );
932      opened_frame = 0;
933
934      /* if the flag FT_LOAD_NO_RECURSE is set, we return the subglyph */
935      /* `as is' in the glyph slot (the client application will be     */
936      /* responsible for interpreting this data)...                    */
937      /*                                                               */
938      if ( loader->load_flags & FT_LOAD_NO_RECURSE )
939      {
940        /* set up remaining glyph fields */
941        FT_GlyphLoader_Add( gloader );
942
943        glyph->num_subglyphs  = gloader->base.num_subglyphs;
944        glyph->format         = ft_glyph_format_composite;
945        glyph->subglyphs      = gloader->base.subglyphs;
946
947        goto Exit;
948      }
949
950      /*********************************************************************/
951      /*********************************************************************/
952      /*********************************************************************/
953
954      /* Now, read each subglyph independently. */
955      {
956        FT_Int        n, num_base_points, num_new_points;
957        FT_SubGlyph*  subglyph = 0;
958
959        FT_UInt num_subglyphs  = gloader->current.num_subglyphs;
960        FT_UInt num_base_subgs = gloader->base.num_subglyphs;
961
962
963        FT_GlyphLoader_Add( gloader );
964
965        for ( n = 0; n < (FT_Int)num_subglyphs; n++ )
966        {
967          FT_Vector  pp1, pp2;
968          FT_Pos     x, y;
969
970
971          /* Each time we call load_truetype_glyph in this loop, the   */
972          /* value of `gloader.base.subglyphs' can change due to table */
973          /* reallocations.  We thus need to recompute the subglyph    */
974          /* pointer on each iteration.                                */
975          subglyph = gloader->base.subglyphs + num_base_subgs + n;
976
977          pp1 = loader->pp1;
978          pp2 = loader->pp2;
979
980          num_base_points = gloader->base.outline.n_points;
981
982          error = load_truetype_glyph( loader, subglyph->index );
983          if ( error )
984            goto Fail;
985
986          /* restore subglyph pointer */
987          subglyph = gloader->base.subglyphs + num_base_subgs + n;
988
989          if ( subglyph->flags & USE_MY_METRICS )
990          {
991            pp1 = loader->pp1;
992            pp2 = loader->pp2;
993          }
994          else
995          {
996            loader->pp1 = pp1;
997            loader->pp2 = pp2;
998          }
999
1000          num_points = gloader->base.outline.n_points;
1001
1002          num_new_points = num_points - num_base_points;
1003
1004          /* now perform the transform required for this subglyph */
1005
1006          if ( subglyph->flags & ( WE_HAVE_A_SCALE     |
1007                                   WE_HAVE_AN_XY_SCALE |
1008                                   WE_HAVE_A_2X2       ) )
1009          {
1010            FT_Vector*  cur   = gloader->base.outline.points +
1011                                  num_base_points;
1012            FT_Vector*  org   = gloader->base.extra_points +
1013                                  num_base_points;
1014            FT_Vector*  limit = cur + num_new_points;
1015
1016
1017            for ( ; cur < limit; cur++, org++ )
1018            {
1019              FT_Vector_Transform( cur, &subglyph->transform );
1020              FT_Vector_Transform( org, &subglyph->transform );
1021            }
1022          }
1023
1024          /* apply offset */
1025
1026          if ( !( subglyph->flags & ARGS_ARE_XY_VALUES ) )
1027          {
1028            FT_UInt     k = subglyph->arg1;
1029            FT_UInt     l = subglyph->arg2;
1030            FT_Vector*  p1;
1031            FT_Vector*  p2;
1032
1033
1034            if ( start_point + k >= (FT_UInt)num_base_points ||
1035                               l >= (FT_UInt)num_new_points  )
1036            {
1037              error = TT_Err_Invalid_Composite;
1038              goto Fail;
1039            }
1040
1041            l += num_base_points;
1042
1043            p1 = gloader->base.outline.points + start_point + k;
1044            p2 = gloader->base.outline.points + start_point + l;
1045
1046            x = p1->x - p2->x;
1047            y = p1->y - p2->y;
1048          }
1049          else
1050          {
1051            x = subglyph->arg1;
1052            y = subglyph->arg2;
1053
1054            if ( !( loader->load_flags & FT_LOAD_NO_SCALE ) )
1055            {
1056              x = FT_MulFix( x, x_scale );
1057              y = FT_MulFix( y, y_scale );
1058
1059              if ( subglyph->flags & ROUND_XY_TO_GRID )
1060              {
1061                x = ( x + 32 ) & -64;
1062                y = ( y + 32 ) & -64;
1063              }
1064            }
1065          }
1066
1067          if ( x | y )
1068          {
1069            translate_array( num_new_points,
1070                             gloader->base.outline.points + num_base_points,
1071                             x, y );
1072
1073            translate_array( num_new_points,
1074                             gloader->base.extra_points + num_base_points,
1075                             x, y );
1076          }
1077        }
1078
1079        /*******************************************************************/
1080        /*******************************************************************/
1081        /*******************************************************************/
1082
1083        /* we have finished loading all sub-glyphs; now, look for */
1084        /* instructions for this composite!                       */
1085
1086#ifdef TT_CONFIG_OPTION_BYTECODE_INTERPRETER
1087
1088        if ( num_subglyphs > 0               &&
1089             loader->exec                    &&
1090             ins_pos         > 0             &&
1091             subglyph->flags & WE_HAVE_INSTR )
1092        {
1093          FT_UShort       n_ins;
1094          TT_ExecContext  exec = loader->exec;
1095          TT_GlyphZone*   pts;
1096          FT_Vector*      pp1;
1097
1098
1099          /* read size of instructions */
1100          if ( FILE_Seek( ins_pos ) ||
1101               READ_UShort( n_ins ) )
1102            goto Fail;
1103          FT_TRACE5(( "  Instructions size = %d\n", n_ins ));
1104
1105          /* in some fonts? */
1106          if ( n_ins == 0xFFFF )
1107            n_ins = 0;
1108
1109          /* check it */
1110          if ( n_ins > face->max_profile.maxSizeOfInstructions )
1111          {
1112            FT_TRACE0(( "Too many instructions (%d) in composite glyph %ld\n",
1113                        n_ins, subglyph->index ));
1114            error = TT_Err_Too_Many_Hints;
1115            goto Fail;
1116          }
1117
1118          /* read the instructions */
1119          if ( FILE_Read( exec->glyphIns, n_ins ) )
1120            goto Fail;
1121
1122          glyph->control_data = exec->glyphIns;
1123          glyph->control_len  = n_ins;
1124
1125          error = TT_Set_CodeRange( exec,
1126                                    tt_coderange_glyph,
1127                                    exec->glyphIns,
1128                                    n_ins );
1129          if ( error )
1130            goto Fail;
1131
1132          /* prepare the execution context */
1133          tt_prepare_zone( &exec->pts, &gloader->base,
1134                           start_point, start_contour );
1135          pts = &exec->pts;
1136
1137          pts->n_points   = (short)(num_points + 2);
1138          pts->n_contours = gloader->base.outline.n_contours;
1139
1140          /* add phantom points */
1141          pp1    = pts->cur + num_points;
1142          pp1[0] = loader->pp1;
1143          pp1[1] = loader->pp2;
1144
1145          pts->tags[num_points    ] = 0;
1146          pts->tags[num_points + 1] = 0;
1147
1148          /* if hinting, round the phantom points */
1149          if ( IS_HINTED( loader->load_flags ) )
1150          {
1151            pp1[0].x = ( ( loader->pp1.x + 32 ) & -64 );
1152            pp1[1].x = ( ( loader->pp2.x + 32 ) & -64 );
1153          }
1154
1155          {
1156            FT_UInt  k;
1157
1158
1159            for ( k = 0; k < num_points; k++ )
1160              pts->tags[k] &= FT_Curve_Tag_On;
1161          }
1162
1163          cur_to_org( num_points + 2, pts );
1164
1165          /* now consider hinting */
1166          if ( IS_HINTED( loader->load_flags ) && n_ins > 0 )
1167          {
1168            exec->is_composite     = TRUE;
1169            exec->pedantic_hinting =
1170              (FT_Bool)( loader->load_flags & FT_LOAD_PEDANTIC );
1171
1172            error = TT_Run_Context( exec, ((TT_Size)loader->size)->debug );
1173            if ( error && exec->pedantic_hinting )
1174              goto Fail;
1175          }
1176
1177          /* save glyph origin and advance points */
1178          loader->pp1 = pp1[0];
1179          loader->pp2 = pp1[1];
1180        }
1181
1182#endif /* TT_CONFIG_OPTION_BYTECODE_INTERPRETER */
1183
1184      }
1185      /* end of composite loading */
1186    }
1187
1188    /***********************************************************************/
1189    /***********************************************************************/
1190    /***********************************************************************/
1191
1192  Fail:
1193    if ( opened_frame )
1194      face->forget_glyph_frame( loader );
1195
1196  Exit:
1197    return error;
1198  }
1199
1200
1201  static void
1202  compute_glyph_metrics( TT_Loader*  loader,
1203                         FT_UInt     glyph_index )
1204  {
1205    FT_BBox       bbox;
1206    TT_Face       face = (TT_Face)loader->face;
1207    FT_Fixed      y_scale;
1208    TT_GlyphSlot  glyph = loader->glyph;
1209    TT_Size       size = (TT_Size)loader->size;
1210
1211
1212    y_scale = 0x10000L;
1213    if ( ( loader->load_flags & FT_LOAD_NO_SCALE ) == 0 )
1214      y_scale = size->root.metrics.y_scale;
1215
1216    if ( glyph->format != ft_glyph_format_composite )
1217    {
1218      glyph->outline.flags &= ~ft_outline_single_pass;
1219
1220      /* copy outline to our glyph slot */
1221      FT_GlyphLoader_Copy_Points( glyph->internal->loader, loader->gloader );
1222      glyph->outline = glyph->internal->loader->base.outline;
1223
1224      /* translate array so that (0,0) is the glyph's origin */
1225      FT_Outline_Translate( &glyph->outline, -loader->pp1.x, 0 );
1226
1227      FT_Outline_Get_CBox( &glyph->outline, &bbox );
1228
1229      if ( IS_HINTED( loader->load_flags ) )
1230      {
1231        /* grid-fit the bounding box */
1232        bbox.xMin &= -64;
1233        bbox.yMin &= -64;
1234        bbox.xMax  = ( bbox.xMax + 63 ) & -64;
1235        bbox.yMax  = ( bbox.yMax + 63 ) & -64;
1236      }
1237    }
1238    else
1239      bbox = loader->bbox;
1240
1241    /* get the device-independent horizontal advance.  It is scaled later */
1242    /* by the base layer.                                                 */
1243    {
1244      FT_Pos  advance = loader->linear;
1245
1246
1247      /* the flag FT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH was introduced to */
1248      /* correctly support DynaLab fonts, which have an incorrect       */
1249      /* `advance_Width_Max' field!  It is used, to my knowledge,       */
1250      /* exclusively in the X-TrueType font server.                     */
1251      /*                                                                */
1252      if ( face->postscript.isFixedPitch                                    &&
1253           ( loader->load_flags & FT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH ) == 0 )
1254        advance = face->horizontal.advance_Width_Max;
1255
1256      /* we need to return the advance in font units in linearHoriAdvance, */
1257      /* it will be scaled later by the base layer.                        */
1258      glyph->linearHoriAdvance = advance;
1259    }
1260
1261    glyph->metrics.horiBearingX = bbox.xMin;
1262    glyph->metrics.horiBearingY = bbox.yMax;
1263    glyph->metrics.horiAdvance  = loader->pp2.x - loader->pp1.x;
1264
1265    /* don't forget to hint the advance when we need to */
1266    if ( IS_HINTED( loader->load_flags ) )
1267      glyph->metrics.horiAdvance = ( glyph->metrics.horiAdvance + 32 ) & -64;
1268
1269    /* Now take care of vertical metrics.  In the case where there is    */
1270    /* no vertical information within the font (relatively common), make */
1271    /* up some metrics by `hand'...                                      */
1272
1273    {
1274      FT_Short   top_bearing;    /* vertical top side bearing (EM units) */
1275      FT_UShort  advance_height; /* vertical advance height   (EM units) */
1276
1277      FT_Pos  left;     /* scaled vertical left side bearing         */
1278      FT_Pos  top;      /* scaled vertical top side bearing          */
1279      FT_Pos  advance;  /* scaled vertical advance height            */
1280
1281
1282      /* Get the unscaled `tsb' and `ah' */
1283      if ( face->vertical_info                   &&
1284           face->vertical.number_Of_VMetrics > 0 )
1285      {
1286        /* Don't assume that both the vertical header and vertical */
1287        /* metrics are present in the same font :-)                */
1288
1289        TT_Get_Metrics( (TT_HoriHeader*)&face->vertical,
1290                        glyph_index,
1291                        &top_bearing,
1292                        &advance_height );
1293      }
1294      else
1295      {
1296        /* Make up the distances from the horizontal header.   */
1297
1298        /* NOTE: The OS/2 values are the only `portable' ones, */
1299        /*       which is why we use them, if there is an OS/2 */
1300        /*       table in the font.  Otherwise, we use the     */
1301        /*       values defined in the horizontal header.      */
1302        /*                                                     */
1303        /* NOTE2: The sTypoDescender is negative, which is why */
1304        /*        we compute the baseline-to-baseline distance */
1305        /*        here with:                                   */
1306        /*             ascender - descender + linegap          */
1307        /*                                                     */
1308        if ( face->os2.version != 0xFFFF )
1309        {
1310          top_bearing    = (FT_Short)( face->os2.sTypoLineGap / 2 );
1311          advance_height = (FT_UShort)( face->os2.sTypoAscender -
1312                                        face->os2.sTypoDescender +
1313                                        face->os2.sTypoLineGap );
1314        }
1315        else
1316        {
1317          top_bearing    = (FT_Short)( face->horizontal.Line_Gap / 2 );
1318          advance_height = (FT_UShort)( face->horizontal.Ascender  +
1319                                        face->horizontal.Descender +
1320                                        face->horizontal.Line_Gap );
1321        }
1322      }
1323
1324      /* We must adjust the top_bearing value from the bounding box given */
1325      /* in the glyph header to te bounding box calculated with           */
1326      /* FT_Get_Outline_CBox().                                           */
1327
1328      /* scale the metrics */
1329      if ( !( loader->load_flags & FT_LOAD_NO_SCALE ) )
1330      {
1331        top     = FT_MulFix( top_bearing + loader->bbox.yMax, y_scale )
1332                    - bbox.yMax;
1333        advance = FT_MulFix( advance_height, y_scale );
1334      }
1335      else
1336      {
1337        top     = top_bearing + loader->bbox.yMax - bbox.yMax;
1338        advance = advance_height;
1339      }
1340
1341      /* set the advance height in design units.  It is scaled later by */
1342      /* the base layer.                                                */
1343      glyph->linearVertAdvance = advance_height;
1344
1345      /* XXX: for now, we have no better algorithm for the lsb, but it */
1346      /*      should work fine.                                        */
1347      /*                                                               */
1348      left = ( bbox.xMin - bbox.xMax ) / 2;
1349
1350      /* grid-fit them if necessary */
1351      if ( IS_HINTED( loader->load_flags ) )
1352      {
1353        left   &= -64;
1354        top     = ( top + 63     ) & -64;
1355        advance = ( advance + 32 ) & -64;
1356      }
1357
1358      glyph->metrics.vertBearingX = left;
1359      glyph->metrics.vertBearingY = top;
1360      glyph->metrics.vertAdvance  = advance;
1361    }
1362
1363    /* adjust advance width to the value contained in the hdmx table */
1364    if ( !face->postscript.isFixedPitch && size &&
1365         IS_HINTED( loader->load_flags )        )
1366    {
1367      FT_Byte* widths = Get_Advance_Widths( face,
1368                                            size->root.metrics.x_ppem );
1369
1370
1371      if ( widths )
1372        glyph->metrics.horiAdvance = widths[glyph_index] << 6;
1373    }
1374
1375    /* set glyph dimensions */
1376    glyph->metrics.width  = bbox.xMax - bbox.xMin;
1377    glyph->metrics.height = bbox.yMax - bbox.yMin;
1378  }
1379
1380
1381  /*************************************************************************/
1382  /*                                                                       */
1383  /* <Function>                                                            */
1384  /*    TT_Load_Glyph                                                      */
1385  /*                                                                       */
1386  /* <Description>                                                         */
1387  /*    A function used to load a single glyph within a given glyph slot,  */
1388  /*    for a given size.                                                  */
1389  /*                                                                       */
1390  /* <Input>                                                               */
1391  /*    glyph       :: A handle to a target slot object where the glyph    */
1392  /*                   will be loaded.                                     */
1393  /*                                                                       */
1394  /*    size        :: A handle to the source face size at which the glyph */
1395  /*                   must be scaled/loaded.                              */
1396  /*                                                                       */
1397  /*    glyph_index :: The index of the glyph in the font file.            */
1398  /*                                                                       */
1399  /*    load_flags  :: A flag indicating what to load for this glyph.  The */
1400  /*                   FT_LOAD_XXX constants can be used to control the    */
1401  /*                   glyph loading process (e.g., whether the outline    */
1402  /*                   should be scaled, whether to load bitmaps or not,   */
1403  /*                   whether to hint the outline, etc).                  */
1404  /*                                                                       */
1405  /* <Return>                                                              */
1406  /*    FreeType error code.  0 means success.                             */
1407  /*                                                                       */
1408  FT_LOCAL_DEF FT_Error
1409  TT_Load_Glyph( TT_Size       size,
1410                 TT_GlyphSlot  glyph,
1411                 FT_UShort     glyph_index,
1412                 FT_UInt       load_flags )
1413  {
1414    SFNT_Interface*  sfnt;
1415    TT_Face          face;
1416    FT_Stream        stream;
1417    FT_Error         error;
1418    TT_Loader        loader;
1419
1420
1421    face   = (TT_Face)glyph->face;
1422    sfnt   = (SFNT_Interface*)face->sfnt;
1423    stream = face->root.stream;
1424    error  = 0;
1425
1426    if ( !size || ( load_flags & FT_LOAD_NO_SCALE )   ||
1427                  ( load_flags & FT_LOAD_NO_RECURSE ) )
1428    {
1429      size        = NULL;
1430      load_flags |= FT_LOAD_NO_SCALE   |
1431                    FT_LOAD_NO_HINTING |
1432                    FT_LOAD_NO_BITMAP;
1433    }
1434
1435    glyph->num_subglyphs = 0;
1436
1437#ifdef TT_CONFIG_OPTION_EMBEDDED_BITMAPS
1438
1439    /* try to load embedded bitmap if any              */
1440    /*                                                 */
1441    /* XXX: The convention should be emphasized in     */
1442    /*      the documents because it can be confusing. */
1443    if ( size                                    &&
1444         size->strike_index != 0xFFFF            &&
1445         sfnt->load_sbits                        &&
1446         ( load_flags & FT_LOAD_NO_BITMAP ) == 0 )
1447
1448    {
1449      TT_SBit_Metrics  metrics;
1450
1451
1452      error = sfnt->load_sbit_image( face,
1453                                     size->strike_index,
1454                                     glyph_index,
1455                                     load_flags,
1456                                     stream,
1457                                     &glyph->bitmap,
1458                                     &metrics );
1459      if ( !error )
1460      {
1461        glyph->outline.n_points   = 0;
1462        glyph->outline.n_contours = 0;
1463
1464        glyph->metrics.width  = (FT_Pos)metrics.width  << 6;
1465        glyph->metrics.height = (FT_Pos)metrics.height << 6;
1466
1467        glyph->metrics.horiBearingX = (FT_Pos)metrics.horiBearingX << 6;
1468        glyph->metrics.horiBearingY = (FT_Pos)metrics.horiBearingY << 6;
1469        glyph->metrics.horiAdvance  = (FT_Pos)metrics.horiAdvance  << 6;
1470
1471        glyph->metrics.vertBearingX = (FT_Pos)metrics.vertBearingX << 6;
1472        glyph->metrics.vertBearingY = (FT_Pos)metrics.vertBearingY << 6;
1473        glyph->metrics.vertAdvance  = (FT_Pos)metrics.vertAdvance  << 6;
1474
1475        glyph->format = ft_glyph_format_bitmap;
1476        if ( load_flags & FT_LOAD_VERTICAL_LAYOUT )
1477        {
1478          glyph->bitmap_left = metrics.vertBearingX;
1479          glyph->bitmap_top  = metrics.vertBearingY;
1480        }
1481        else
1482        {
1483          glyph->bitmap_left = metrics.horiBearingX;
1484          glyph->bitmap_top  = metrics.horiBearingY;
1485        }
1486        return error;
1487      }
1488    }
1489
1490#endif /* TT_CONFIG_OPTION_EMBEDDED_BITMAPS */
1491
1492    /* return immediately if we only want the embedded bitmaps */
1493    if ( load_flags & FT_LOAD_SBITS_ONLY )
1494      return FT_Err_Invalid_Argument;
1495
1496    /* seek to the beginning of the glyph table.  For Type 42 fonts      */
1497    /* the table might be accessed from a Postscript stream or something */
1498    /* else...                                                           */
1499
1500    error = face->goto_table( face, TTAG_glyf, stream, 0 );
1501    if ( error )
1502    {
1503      FT_ERROR(( "TT_Load_Glyph: could not access glyph table\n" ));
1504      goto Exit;
1505    }
1506
1507    MEM_Set( &loader, 0, sizeof ( loader ) );
1508
1509    /* update the glyph zone bounds */
1510    {
1511      FT_GlyphLoader*  gloader = FT_FACE_DRIVER(face)->glyph_loader;
1512
1513
1514      loader.gloader = gloader;
1515
1516      FT_GlyphLoader_Rewind( gloader );
1517
1518      tt_prepare_zone( &loader.zone, &gloader->base, 0, 0 );
1519      tt_prepare_zone( &loader.base, &gloader->base, 0, 0 );
1520    }
1521
1522#ifdef TT_CONFIG_OPTION_BYTECODE_INTERPRETER
1523
1524    if ( size )
1525    {
1526      /* query new execution context */
1527      loader.exec = size->debug ? size->context : TT_New_Context( face );
1528      if ( !loader.exec )
1529        return TT_Err_Could_Not_Find_Context;
1530
1531      TT_Load_Context( loader.exec, face, size );
1532      loader.instructions = loader.exec->glyphIns;
1533
1534      /* load default graphics state - if needed */
1535      if ( size->GS.instruct_control & 2 )
1536        loader.exec->GS = tt_default_graphics_state;
1537    }
1538
1539#endif /* TT_CONFIG_OPTION_BYTECODE_INTERPRETER */
1540
1541    /* clear all outline flags, except the `owner' one */
1542    glyph->outline.flags = 0;
1543
1544    if ( size && size->root.metrics.y_ppem < 24 )
1545      glyph->outline.flags |= ft_outline_high_precision;
1546
1547    /* let's initialize the rest of our loader now */
1548
1549    loader.load_flags    = load_flags;
1550
1551    loader.face   = (FT_Face)face;
1552    loader.size   = (FT_Size)size;
1553    loader.glyph  = (FT_GlyphSlot)glyph;
1554    loader.stream = stream;
1555
1556    loader.glyf_offset  = FILE_Pos();
1557
1558#ifdef TT_CONFIG_OPTION_BYTECODE_INTERPRETER
1559
1560    /* if the cvt program has disabled hinting, the argument */
1561    /* is ignored.                                           */
1562    if ( size && ( size->GS.instruct_control & 1 ) )
1563      loader.load_flags |= FT_LOAD_NO_HINTING;
1564
1565#endif /* TT_CONFIG_OPTION_BYTECODE_INTERPRETER */
1566
1567    /* Main loading loop */
1568    glyph->format        = ft_glyph_format_outline;
1569    glyph->num_subglyphs = 0;
1570    error = load_truetype_glyph( &loader, glyph_index );
1571    if ( !error )
1572      compute_glyph_metrics( &loader, glyph_index );
1573
1574#ifdef TT_CONFIG_OPTION_BYTECODE_INTERPRETER
1575
1576    if ( !size || !size->debug )
1577      TT_Done_Context( loader.exec );
1578
1579#endif /* TT_CONFIG_OPTION_BYTECODE_INTERPRETER */
1580
1581  Exit:
1582    return error;
1583  }
1584
1585
1586/* END */
1587