ttgload.c revision 79e36baebba4532dff92ca1ca43c4dc2f779fd32
1/***************************************************************************/
2/*                                                                         */
3/*  ttgload.c                                                              */
4/*                                                                         */
5/*    TrueType Glyph Loader (body).                                        */
6/*                                                                         */
7/*  Copyright 1996-2012                                                    */
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#include "ttpload.h"
29
30#ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
31#include "ttgxvar.h"
32#endif
33
34#include "tterrors.h"
35#include "ttsubpix.h"
36
37
38  /*************************************************************************/
39  /*                                                                       */
40  /* The macro FT_COMPONENT is used in trace mode.  It is an implicit      */
41  /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log  */
42  /* messages during execution.                                            */
43  /*                                                                       */
44#undef  FT_COMPONENT
45#define FT_COMPONENT  trace_ttgload
46
47
48  /*************************************************************************/
49  /*                                                                       */
50  /* Composite glyph flags.                                                */
51  /*                                                                       */
52#define ARGS_ARE_WORDS             0x0001
53#define ARGS_ARE_XY_VALUES         0x0002
54#define ROUND_XY_TO_GRID           0x0004
55#define WE_HAVE_A_SCALE            0x0008
56/* reserved                        0x0010 */
57#define MORE_COMPONENTS            0x0020
58#define WE_HAVE_AN_XY_SCALE        0x0040
59#define WE_HAVE_A_2X2              0x0080
60#define WE_HAVE_INSTR              0x0100
61#define USE_MY_METRICS             0x0200
62#define OVERLAP_COMPOUND           0x0400
63#define SCALED_COMPONENT_OFFSET    0x0800
64#define UNSCALED_COMPONENT_OFFSET  0x1000
65
66
67  /*************************************************************************/
68  /*                                                                       */
69  /* Return the horizontal metrics in font units for a given glyph.        */
70  /*                                                                       */
71  FT_LOCAL_DEF( void )
72  TT_Get_HMetrics( TT_Face     face,
73                   FT_UInt     idx,
74                   FT_Short*   lsb,
75                   FT_UShort*  aw )
76  {
77    ( (SFNT_Service)face->sfnt )->get_metrics( face, 0, idx, lsb, aw );
78
79    FT_TRACE5(( "  advance width (font units): %d\n", *aw ));
80    FT_TRACE5(( "  left side bearing (font units): %d\n", *lsb ));
81  }
82
83
84  /*************************************************************************/
85  /*                                                                       */
86  /* Return the vertical metrics in font units for a given glyph.          */
87  /* Greg Hitchcock from Microsoft told us that if there were no `vmtx'    */
88  /* table, typoAscender/Descender from the `OS/2' table would be used     */
89  /* instead, and if there were no `OS/2' table, use ascender/descender    */
90  /* from the `hhea' table.  But that is not what Microsoft's rasterizer   */
91  /* apparently does: It uses the ppem value as the advance height, and    */
92  /* sets the top side bearing to be zero.                                 */
93  /*                                                                       */
94  FT_LOCAL_DEF( void )
95  TT_Get_VMetrics( TT_Face     face,
96                   FT_UInt     idx,
97                   FT_Short*   tsb,
98                   FT_UShort*  ah )
99  {
100    if ( face->vertical_info )
101      ( (SFNT_Service)face->sfnt )->get_metrics( face, 1, idx, tsb, ah );
102
103#if 1             /* Empirically determined, at variance with what MS said */
104
105    else
106    {
107      *tsb = 0;
108      *ah  = face->root.units_per_EM;
109    }
110
111#else      /* This is what MS said to do.  It isn't what they do, however. */
112
113    else if ( face->os2.version != 0xFFFFU )
114    {
115      *tsb = face->os2.sTypoAscender;
116      *ah  = face->os2.sTypoAscender - face->os2.sTypoDescender;
117    }
118    else
119    {
120      *tsb = face->horizontal.Ascender;
121      *ah  = face->horizontal.Ascender - face->horizontal.Descender;
122    }
123
124#endif
125
126    FT_TRACE5(( "  advance height (font units): %d\n", *ah ));
127    FT_TRACE5(( "  top side bearing (font units): %d\n", *tsb ));
128  }
129
130
131  static void
132  tt_get_metrics( TT_Loader  loader,
133                  FT_UInt    glyph_index )
134  {
135    TT_Face  face = (TT_Face)loader->face;
136
137    FT_Short   left_bearing = 0, top_bearing = 0;
138    FT_UShort  advance_width = 0, advance_height = 0;
139
140
141    TT_Get_HMetrics( face, glyph_index,
142                     &left_bearing,
143                     &advance_width );
144    TT_Get_VMetrics( face, glyph_index,
145                     &top_bearing,
146                     &advance_height );
147
148    loader->left_bearing = left_bearing;
149    loader->advance      = advance_width;
150    loader->top_bearing  = top_bearing;
151    loader->vadvance     = advance_height;
152
153#ifdef TT_CONFIG_OPTION_SUBPIXEL_HINTING
154    if ( loader->exec )
155      loader->exec->sph_tweak_flags = 0;
156
157    /* this may not be the right place for this, but it works */
158    if ( loader->exec && loader->exec->ignore_x_mode )
159      sph_set_tweaks( loader, glyph_index );
160#endif /* TT_CONFIG_OPTION_SUBPIXEL_HINTING */
161
162    if ( !loader->linear_def )
163    {
164      loader->linear_def = 1;
165      loader->linear     = advance_width;
166    }
167  }
168
169
170#ifdef FT_CONFIG_OPTION_INCREMENTAL
171
172  static void
173  tt_get_metrics_incr_overrides( TT_Loader  loader,
174                                 FT_UInt    glyph_index )
175  {
176    TT_Face  face = (TT_Face)loader->face;
177
178    FT_Short   left_bearing = 0, top_bearing = 0;
179    FT_UShort  advance_width = 0, advance_height = 0;
180
181
182    /* If this is an incrementally loaded font check whether there are */
183    /* overriding metrics for this glyph.                              */
184    if ( face->root.internal->incremental_interface                           &&
185         face->root.internal->incremental_interface->funcs->get_glyph_metrics )
186    {
187      FT_Incremental_MetricsRec  metrics;
188      FT_Error                   error;
189
190
191      metrics.bearing_x = loader->left_bearing;
192      metrics.bearing_y = 0;
193      metrics.advance   = loader->advance;
194      metrics.advance_v = 0;
195
196      error = face->root.internal->incremental_interface->funcs->get_glyph_metrics(
197                face->root.internal->incremental_interface->object,
198                glyph_index, FALSE, &metrics );
199      if ( error )
200        goto Exit;
201
202      left_bearing  = (FT_Short)metrics.bearing_x;
203      advance_width = (FT_UShort)metrics.advance;
204
205#if 0
206
207      /* GWW: Do I do the same for vertical metrics? */
208      metrics.bearing_x = 0;
209      metrics.bearing_y = loader->top_bearing;
210      metrics.advance   = loader->vadvance;
211
212      error = face->root.internal->incremental_interface->funcs->get_glyph_metrics(
213                face->root.internal->incremental_interface->object,
214                glyph_index, TRUE, &metrics );
215      if ( error )
216        goto Exit;
217
218      top_bearing    = (FT_Short)metrics.bearing_y;
219      advance_height = (FT_UShort)metrics.advance;
220
221#endif /* 0 */
222
223      loader->left_bearing = left_bearing;
224      loader->advance      = advance_width;
225      loader->top_bearing  = top_bearing;
226      loader->vadvance     = advance_height;
227
228      if ( !loader->linear_def )
229      {
230        loader->linear_def = 1;
231        loader->linear     = advance_width;
232      }
233    }
234
235  Exit:
236    return;
237  }
238
239#endif /* FT_CONFIG_OPTION_INCREMENTAL */
240
241
242  /*************************************************************************/
243  /*                                                                       */
244  /* Translates an array of coordinates.                                   */
245  /*                                                                       */
246  static void
247  translate_array( FT_UInt     n,
248                   FT_Vector*  coords,
249                   FT_Pos      delta_x,
250                   FT_Pos      delta_y )
251  {
252    FT_UInt  k;
253
254
255    if ( delta_x )
256      for ( k = 0; k < n; k++ )
257        coords[k].x += delta_x;
258
259    if ( delta_y )
260      for ( k = 0; k < n; k++ )
261        coords[k].y += delta_y;
262  }
263
264
265#undef  IS_HINTED
266#define IS_HINTED( flags )  ( ( flags & FT_LOAD_NO_HINTING ) == 0 )
267
268
269  /*************************************************************************/
270  /*                                                                       */
271  /* The following functions are used by default with TrueType fonts.      */
272  /* However, they can be replaced by alternatives if we need to support   */
273  /* TrueType-compressed formats (like MicroType) in the future.           */
274  /*                                                                       */
275  /*************************************************************************/
276
277  FT_CALLBACK_DEF( FT_Error )
278  TT_Access_Glyph_Frame( TT_Loader  loader,
279                         FT_UInt    glyph_index,
280                         FT_ULong   offset,
281                         FT_UInt    byte_count )
282  {
283    FT_Error   error;
284    FT_Stream  stream = loader->stream;
285
286    /* for non-debug mode */
287    FT_UNUSED( glyph_index );
288
289
290    FT_TRACE4(( "Glyph %ld\n", glyph_index ));
291
292    /* the following line sets the `error' variable through macros! */
293    if ( FT_STREAM_SEEK( offset ) || FT_FRAME_ENTER( byte_count ) )
294      return error;
295
296    loader->cursor = stream->cursor;
297    loader->limit  = stream->limit;
298
299    return TT_Err_Ok;
300  }
301
302
303  FT_CALLBACK_DEF( void )
304  TT_Forget_Glyph_Frame( TT_Loader  loader )
305  {
306    FT_Stream  stream = loader->stream;
307
308
309    FT_FRAME_EXIT();
310  }
311
312
313  FT_CALLBACK_DEF( FT_Error )
314  TT_Load_Glyph_Header( TT_Loader  loader )
315  {
316    FT_Byte*  p     = loader->cursor;
317    FT_Byte*  limit = loader->limit;
318
319
320    if ( p + 10 > limit )
321      return TT_Err_Invalid_Outline;
322
323    loader->n_contours = FT_NEXT_SHORT( p );
324
325    loader->bbox.xMin = FT_NEXT_SHORT( p );
326    loader->bbox.yMin = FT_NEXT_SHORT( p );
327    loader->bbox.xMax = FT_NEXT_SHORT( p );
328    loader->bbox.yMax = FT_NEXT_SHORT( p );
329
330    FT_TRACE5(( "  # of contours: %d\n", loader->n_contours ));
331    FT_TRACE5(( "  xMin: %4d  xMax: %4d\n", loader->bbox.xMin,
332                                            loader->bbox.xMax ));
333    FT_TRACE5(( "  yMin: %4d  yMax: %4d\n", loader->bbox.yMin,
334                                            loader->bbox.yMax ));
335    loader->cursor = p;
336
337    return TT_Err_Ok;
338  }
339
340
341  FT_CALLBACK_DEF( FT_Error )
342  TT_Load_Simple_Glyph( TT_Loader  load )
343  {
344    FT_Error        error;
345    FT_Byte*        p          = load->cursor;
346    FT_Byte*        limit      = load->limit;
347    FT_GlyphLoader  gloader    = load->gloader;
348    FT_Int          n_contours = load->n_contours;
349    FT_Outline*     outline;
350    TT_Face         face       = (TT_Face)load->face;
351    FT_UShort       n_ins;
352    FT_Int          n_points;
353
354    FT_Byte         *flag, *flag_limit;
355    FT_Byte         c, count;
356    FT_Vector       *vec, *vec_limit;
357    FT_Pos          x;
358    FT_Short        *cont, *cont_limit, prev_cont;
359    FT_Int          xy_size = 0;
360
361
362    /* check that we can add the contours to the glyph */
363    error = FT_GLYPHLOADER_CHECK_POINTS( gloader, 0, n_contours );
364    if ( error )
365      goto Fail;
366
367    /* reading the contours' endpoints & number of points */
368    cont       = gloader->current.outline.contours;
369    cont_limit = cont + n_contours;
370
371    /* check space for contours array + instructions count */
372    if ( n_contours >= 0xFFF || p + ( n_contours + 1 ) * 2 > limit )
373      goto Invalid_Outline;
374
375    prev_cont = FT_NEXT_SHORT( p );
376
377    if ( n_contours > 0 )
378      cont[0] = prev_cont;
379
380    if ( prev_cont < 0 )
381      goto Invalid_Outline;
382
383    for ( cont++; cont < cont_limit; cont++ )
384    {
385      cont[0] = FT_NEXT_SHORT( p );
386      if ( cont[0] <= prev_cont )
387      {
388        /* unordered contours: this is invalid */
389        goto Invalid_Outline;
390      }
391      prev_cont = cont[0];
392    }
393
394    n_points = 0;
395    if ( n_contours > 0 )
396    {
397      n_points = cont[-1] + 1;
398      if ( n_points < 0 )
399        goto Invalid_Outline;
400    }
401
402    /* note that we will add four phantom points later */
403    error = FT_GLYPHLOADER_CHECK_POINTS( gloader, n_points + 4, 0 );
404    if ( error )
405      goto Fail;
406
407    /* reading the bytecode instructions */
408    load->glyph->control_len  = 0;
409    load->glyph->control_data = 0;
410
411    if ( p + 2 > limit )
412      goto Invalid_Outline;
413
414    n_ins = FT_NEXT_USHORT( p );
415
416    FT_TRACE5(( "  Instructions size: %u\n", n_ins ));
417
418    if ( n_ins > face->max_profile.maxSizeOfInstructions )
419    {
420      FT_TRACE0(( "TT_Load_Simple_Glyph: too many instructions (%d)\n",
421                  n_ins ));
422      error = TT_Err_Too_Many_Hints;
423      goto Fail;
424    }
425
426    if ( ( limit - p ) < n_ins )
427    {
428      FT_TRACE0(( "TT_Load_Simple_Glyph: instruction count mismatch\n" ));
429      error = TT_Err_Too_Many_Hints;
430      goto Fail;
431    }
432
433#ifdef TT_USE_BYTECODE_INTERPRETER
434
435    if ( IS_HINTED( load->load_flags ) )
436    {
437      load->glyph->control_len  = n_ins;
438      load->glyph->control_data = load->exec->glyphIns;
439
440      FT_MEM_COPY( load->exec->glyphIns, p, (FT_Long)n_ins );
441    }
442
443#endif /* TT_USE_BYTECODE_INTERPRETER */
444
445    p += n_ins;
446
447    outline = &gloader->current.outline;
448
449    /* reading the point tags */
450    flag       = (FT_Byte*)outline->tags;
451    flag_limit = flag + n_points;
452
453    FT_ASSERT( flag != NULL );
454
455    while ( flag < flag_limit )
456    {
457      if ( p + 1 > limit )
458        goto Invalid_Outline;
459
460      *flag++ = c = FT_NEXT_BYTE( p );
461      if ( c & 8 )
462      {
463        if ( p + 1 > limit )
464          goto Invalid_Outline;
465
466        count = FT_NEXT_BYTE( p );
467        if ( flag + (FT_Int)count > flag_limit )
468          goto Invalid_Outline;
469
470        for ( ; count > 0; count-- )
471          *flag++ = c;
472      }
473    }
474
475    /* reading the X coordinates */
476
477    vec       = outline->points;
478    vec_limit = vec + n_points;
479    flag      = (FT_Byte*)outline->tags;
480    x         = 0;
481
482    if ( p + xy_size > limit )
483      goto Invalid_Outline;
484
485    for ( ; vec < vec_limit; vec++, flag++ )
486    {
487      FT_Pos   y = 0;
488      FT_Byte  f = *flag;
489
490
491      if ( f & 2 )
492      {
493        if ( p + 1 > limit )
494          goto Invalid_Outline;
495
496        y = (FT_Pos)FT_NEXT_BYTE( p );
497        if ( ( f & 16 ) == 0 )
498          y = -y;
499      }
500      else if ( ( f & 16 ) == 0 )
501      {
502        if ( p + 2 > limit )
503          goto Invalid_Outline;
504
505        y = (FT_Pos)FT_NEXT_SHORT( p );
506      }
507
508      x     += y;
509      vec->x = x;
510      /* the cast is for stupid compilers */
511      *flag  = (FT_Byte)( f & ~( 2 | 16 ) );
512    }
513
514    /* reading the Y coordinates */
515
516    vec       = gloader->current.outline.points;
517    vec_limit = vec + n_points;
518    flag      = (FT_Byte*)outline->tags;
519    x         = 0;
520
521    for ( ; vec < vec_limit; vec++, flag++ )
522    {
523      FT_Pos   y = 0;
524      FT_Byte  f = *flag;
525
526
527      if ( f & 4 )
528      {
529        if ( p + 1 > limit )
530          goto Invalid_Outline;
531
532        y = (FT_Pos)FT_NEXT_BYTE( p );
533        if ( ( f & 32 ) == 0 )
534          y = -y;
535      }
536      else if ( ( f & 32 ) == 0 )
537      {
538        if ( p + 2 > limit )
539          goto Invalid_Outline;
540
541        y = (FT_Pos)FT_NEXT_SHORT( p );
542      }
543
544      x     += y;
545      vec->y = x;
546      /* the cast is for stupid compilers */
547      *flag  = (FT_Byte)( f & FT_CURVE_TAG_ON );
548    }
549
550    outline->n_points   = (FT_UShort)n_points;
551    outline->n_contours = (FT_Short) n_contours;
552
553    load->cursor = p;
554
555  Fail:
556    return error;
557
558  Invalid_Outline:
559    error = TT_Err_Invalid_Outline;
560    goto Fail;
561  }
562
563
564  FT_CALLBACK_DEF( FT_Error )
565  TT_Load_Composite_Glyph( TT_Loader  loader )
566  {
567    FT_Error        error;
568    FT_Byte*        p       = loader->cursor;
569    FT_Byte*        limit   = loader->limit;
570    FT_GlyphLoader  gloader = loader->gloader;
571    FT_SubGlyph     subglyph;
572    FT_UInt         num_subglyphs;
573
574
575    num_subglyphs = 0;
576
577    do
578    {
579      FT_Fixed  xx, xy, yy, yx;
580      FT_UInt   count;
581
582
583      /* check that we can load a new subglyph */
584      error = FT_GlyphLoader_CheckSubGlyphs( gloader, num_subglyphs + 1 );
585      if ( error )
586        goto Fail;
587
588      /* check space */
589      if ( p + 4 > limit )
590        goto Invalid_Composite;
591
592      subglyph = gloader->current.subglyphs + num_subglyphs;
593
594      subglyph->arg1 = subglyph->arg2 = 0;
595
596      subglyph->flags = FT_NEXT_USHORT( p );
597      subglyph->index = FT_NEXT_USHORT( p );
598
599      /* check space */
600      count = 2;
601      if ( subglyph->flags & ARGS_ARE_WORDS )
602        count += 2;
603      if ( subglyph->flags & WE_HAVE_A_SCALE )
604        count += 2;
605      else if ( subglyph->flags & WE_HAVE_AN_XY_SCALE )
606        count += 4;
607      else if ( subglyph->flags & WE_HAVE_A_2X2 )
608        count += 8;
609
610      if ( p + count > limit )
611        goto Invalid_Composite;
612
613      /* read arguments */
614      if ( subglyph->flags & ARGS_ARE_WORDS )
615      {
616        subglyph->arg1 = FT_NEXT_SHORT( p );
617        subglyph->arg2 = FT_NEXT_SHORT( p );
618      }
619      else
620      {
621        subglyph->arg1 = FT_NEXT_CHAR( p );
622        subglyph->arg2 = FT_NEXT_CHAR( p );
623      }
624
625      /* read transform */
626      xx = yy = 0x10000L;
627      xy = yx = 0;
628
629      if ( subglyph->flags & WE_HAVE_A_SCALE )
630      {
631        xx = (FT_Fixed)FT_NEXT_SHORT( p ) << 2;
632        yy = xx;
633      }
634      else if ( subglyph->flags & WE_HAVE_AN_XY_SCALE )
635      {
636        xx = (FT_Fixed)FT_NEXT_SHORT( p ) << 2;
637        yy = (FT_Fixed)FT_NEXT_SHORT( p ) << 2;
638      }
639      else if ( subglyph->flags & WE_HAVE_A_2X2 )
640      {
641        xx = (FT_Fixed)FT_NEXT_SHORT( p ) << 2;
642        yx = (FT_Fixed)FT_NEXT_SHORT( p ) << 2;
643        xy = (FT_Fixed)FT_NEXT_SHORT( p ) << 2;
644        yy = (FT_Fixed)FT_NEXT_SHORT( p ) << 2;
645      }
646
647      subglyph->transform.xx = xx;
648      subglyph->transform.xy = xy;
649      subglyph->transform.yx = yx;
650      subglyph->transform.yy = yy;
651
652      num_subglyphs++;
653
654    } while ( subglyph->flags & MORE_COMPONENTS );
655
656    gloader->current.num_subglyphs = num_subglyphs;
657
658#ifdef TT_USE_BYTECODE_INTERPRETER
659
660    {
661      FT_Stream  stream = loader->stream;
662
663
664      /* we must undo the FT_FRAME_ENTER in order to point */
665      /* to the composite instructions, if we find some.   */
666      /* We will process them later.                       */
667      /*                                                   */
668      loader->ins_pos = (FT_ULong)( FT_STREAM_POS() +
669                                    p - limit );
670    }
671
672#endif
673
674    loader->cursor = p;
675
676  Fail:
677    return error;
678
679  Invalid_Composite:
680    error = TT_Err_Invalid_Composite;
681    goto Fail;
682  }
683
684
685  FT_LOCAL_DEF( void )
686  TT_Init_Glyph_Loading( TT_Face  face )
687  {
688    face->access_glyph_frame   = TT_Access_Glyph_Frame;
689    face->read_glyph_header    = TT_Load_Glyph_Header;
690    face->read_simple_glyph    = TT_Load_Simple_Glyph;
691    face->read_composite_glyph = TT_Load_Composite_Glyph;
692    face->forget_glyph_frame   = TT_Forget_Glyph_Frame;
693  }
694
695
696  static void
697  tt_prepare_zone( TT_GlyphZone  zone,
698                   FT_GlyphLoad  load,
699                   FT_UInt       start_point,
700                   FT_UInt       start_contour )
701  {
702    zone->n_points    = (FT_UShort)( load->outline.n_points - start_point );
703    zone->n_contours  = (FT_Short) ( load->outline.n_contours -
704                                       start_contour );
705    zone->org         = load->extra_points + start_point;
706    zone->cur         = load->outline.points + start_point;
707    zone->orus        = load->extra_points2 + start_point;
708    zone->tags        = (FT_Byte*)load->outline.tags + start_point;
709    zone->contours    = (FT_UShort*)load->outline.contours + start_contour;
710    zone->first_point = (FT_UShort)start_point;
711  }
712
713
714  /*************************************************************************/
715  /*                                                                       */
716  /* <Function>                                                            */
717  /*    TT_Hint_Glyph                                                      */
718  /*                                                                       */
719  /* <Description>                                                         */
720  /*    Hint the glyph using the zone prepared by the caller.  Note that   */
721  /*    the zone is supposed to include four phantom points.               */
722  /*                                                                       */
723  static FT_Error
724  TT_Hint_Glyph( TT_Loader  loader,
725                 FT_Bool    is_composite )
726  {
727    TT_GlyphZone  zone = &loader->zone;
728    FT_Pos        origin;
729
730#ifdef TT_USE_BYTECODE_INTERPRETER
731    FT_UInt       n_ins;
732#else
733    FT_UNUSED( is_composite );
734#endif
735
736
737#ifdef TT_USE_BYTECODE_INTERPRETER
738    if ( loader->glyph->control_len > 0xFFFFL )
739    {
740      FT_TRACE1(( "TT_Hint_Glyph: too long instructions " ));
741      FT_TRACE1(( "(0x%lx byte) is truncated\n",
742                 loader->glyph->control_len ));
743    }
744    n_ins = (FT_UInt)( loader->glyph->control_len );
745#endif
746
747    origin = zone->cur[zone->n_points - 4].x;
748    origin = FT_PIX_ROUND( origin ) - origin;
749    if ( origin )
750      translate_array( zone->n_points, zone->cur, origin, 0 );
751
752#ifdef TT_USE_BYTECODE_INTERPRETER
753    /* save original point position in org */
754    if ( n_ins > 0 )
755      FT_ARRAY_COPY( zone->org, zone->cur, zone->n_points );
756
757    /* Reset graphics state. */
758    loader->exec->GS = ((TT_Size)loader->size)->GS;
759
760    /* XXX: UNDOCUMENTED! Hinting instructions of a composite glyph */
761    /*      completely refer to the (already) hinted subglyphs.     */
762    if ( is_composite )
763    {
764      loader->exec->metrics.x_scale = 1 << 16;
765      loader->exec->metrics.y_scale = 1 << 16;
766
767      FT_ARRAY_COPY( zone->orus, zone->cur, zone->n_points );
768    }
769    else
770    {
771      loader->exec->metrics.x_scale =
772        ((TT_Size)loader->size)->metrics.x_scale;
773      loader->exec->metrics.y_scale =
774        ((TT_Size)loader->size)->metrics.y_scale;
775    }
776#endif
777
778    /* round pp2 and pp4 */
779    zone->cur[zone->n_points - 3].x =
780      FT_PIX_ROUND( zone->cur[zone->n_points - 3].x );
781    zone->cur[zone->n_points - 1].y =
782      FT_PIX_ROUND( zone->cur[zone->n_points - 1].y );
783
784#ifdef TT_USE_BYTECODE_INTERPRETER
785
786    if ( n_ins > 0 )
787    {
788      FT_Bool   debug;
789      FT_Error  error;
790
791      FT_GlyphLoader  gloader         = loader->gloader;
792      FT_Outline      current_outline = gloader->current.outline;
793
794
795      error = TT_Set_CodeRange( loader->exec, tt_coderange_glyph,
796                                loader->exec->glyphIns, n_ins );
797      if ( error )
798        return error;
799
800      loader->exec->is_composite = is_composite;
801      loader->exec->pts          = *zone;
802
803      debug = FT_BOOL( !( loader->load_flags & FT_LOAD_NO_SCALE ) &&
804                       ((TT_Size)loader->size)->debug             );
805
806      error = TT_Run_Context( loader->exec, debug );
807      if ( error && loader->exec->pedantic_hinting )
808        return error;
809
810      /* store drop-out mode in bits 5-7; set bit 2 also as a marker */
811      current_outline.tags[0] |=
812        ( loader->exec->GS.scan_type << 5 ) | FT_CURVE_TAG_HAS_SCANMODE;
813    }
814
815#endif
816
817    /* save glyph phantom points */
818    if ( !loader->preserve_pps )
819    {
820      loader->pp1 = zone->cur[zone->n_points - 4];
821      loader->pp2 = zone->cur[zone->n_points - 3];
822      loader->pp3 = zone->cur[zone->n_points - 2];
823      loader->pp4 = zone->cur[zone->n_points - 1];
824    }
825
826#ifdef TT_CONFIG_OPTION_SUBPIXEL_HINTING
827    if ( loader->exec->sph_tweak_flags & SPH_TWEAK_DEEMBOLDEN )
828      FT_Outline_EmboldenXY( &loader->gloader->current.outline, -24, 0 );
829
830    else if ( loader->exec->sph_tweak_flags & SPH_TWEAK_EMBOLDEN )
831      FT_Outline_EmboldenXY( &loader->gloader->current.outline, 24, 0 );
832#endif
833    return TT_Err_Ok;
834  }
835
836
837  /*************************************************************************/
838  /*                                                                       */
839  /* <Function>                                                            */
840  /*    TT_Process_Simple_Glyph                                            */
841  /*                                                                       */
842  /* <Description>                                                         */
843  /*    Once a simple glyph has been loaded, it needs to be processed.     */
844  /*    Usually, this means scaling and hinting through bytecode           */
845  /*    interpretation.                                                    */
846  /*                                                                       */
847  static FT_Error
848  TT_Process_Simple_Glyph( TT_Loader  loader )
849  {
850    FT_GlyphLoader  gloader = loader->gloader;
851    FT_Error        error   = TT_Err_Ok;
852    FT_Outline*     outline;
853    FT_Int          n_points;
854
855#ifdef TT_CONFIG_OPTION_SUBPIXEL_HINTING
856    TT_Face     face           = (TT_Face)loader->face;
857    FT_String*  family         = face->root.family_name;
858    FT_Int      ppem           = loader->size->metrics.x_ppem;
859    FT_String*  style          = face->root.style_name;
860    FT_Int      x_scale_factor = 1000;
861#endif
862
863
864    outline  = &gloader->current.outline;
865    n_points = outline->n_points;
866
867    /* set phantom points */
868
869    outline->points[n_points    ] = loader->pp1;
870    outline->points[n_points + 1] = loader->pp2;
871    outline->points[n_points + 2] = loader->pp3;
872    outline->points[n_points + 3] = loader->pp4;
873
874    outline->tags[n_points    ] = 0;
875    outline->tags[n_points + 1] = 0;
876    outline->tags[n_points + 2] = 0;
877    outline->tags[n_points + 3] = 0;
878
879    n_points += 4;
880
881#ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
882
883    if ( ((TT_Face)loader->face)->doblend )
884    {
885      /* Deltas apply to the unscaled data. */
886      FT_Vector*  deltas;
887      FT_Memory   memory = loader->face->memory;
888      FT_Int      i;
889
890
891      error = TT_Vary_Get_Glyph_Deltas( (TT_Face)(loader->face),
892                                        loader->glyph_index,
893                                        &deltas,
894                                        n_points );
895      if ( error )
896        return error;
897
898      for ( i = 0; i < n_points; ++i )
899      {
900        outline->points[i].x += deltas[i].x;
901        outline->points[i].y += deltas[i].y;
902      }
903
904      FT_FREE( deltas );
905    }
906
907#endif /* TT_CONFIG_OPTION_GX_VAR_SUPPORT */
908
909    if ( IS_HINTED( loader->load_flags ) )
910    {
911      tt_prepare_zone( &loader->zone, &gloader->current, 0, 0 );
912
913      FT_ARRAY_COPY( loader->zone.orus, loader->zone.cur,
914                     loader->zone.n_points + 4 );
915    }
916
917#ifdef TT_CONFIG_OPTION_SUBPIXEL_HINTING
918    /* scale, but only if enabled and only if TT hinting is being used */
919    if ( IS_HINTED( loader->load_flags ) )
920      x_scale_factor = scale_test_tweak( face, family, ppem, style,
921                                         loader->glyph_index, X_SCALING_Rules,
922                                         X_SCALING_RULES_SIZE );
923    /* scale the glyph */
924    if ( ( loader->load_flags & FT_LOAD_NO_SCALE ) == 0 ||
925         x_scale_factor != 1000                         )
926    {
927      FT_Vector*  vec     = outline->points;
928      FT_Vector*  limit   = outline->points + n_points;
929      FT_Fixed    x_scale = FT_MulDiv(
930                              ((TT_Size)loader->size)->metrics.x_scale,
931                              x_scale_factor, 1000 );
932      FT_Fixed    y_scale = ((TT_Size)loader->size)->metrics.y_scale;
933
934
935      /* compensate for any scaling by de/emboldening; */
936      /* the amount was determined via experimentation */
937      if ( x_scale_factor != 1000 && ppem > 11 )
938        FT_Outline_EmboldenXY( outline,
939                               FT_MulDiv( 16 * ppem,
940                                          1000 - x_scale_factor,
941                                          0x1000L ), 0 );
942#else
943    /* scale the glyph */
944    if ( ( loader->load_flags & FT_LOAD_NO_SCALE ) == 0 )
945    {
946      FT_Vector*  vec     = outline->points;
947      FT_Vector*  limit   = outline->points + n_points;
948      FT_Fixed    x_scale = ((TT_Size)loader->size)->metrics.x_scale;
949      FT_Fixed    y_scale = ((TT_Size)loader->size)->metrics.y_scale;
950#endif /* !TT_CONFIG_OPTION_SUBPIXEL_HINTING */
951
952
953      for ( ; vec < limit; vec++ )
954      {
955        vec->x = FT_MulFix( vec->x, x_scale );
956        vec->y = FT_MulFix( vec->y, y_scale );
957      }
958
959      loader->pp1 = outline->points[n_points - 4];
960      loader->pp2 = outline->points[n_points - 3];
961      loader->pp3 = outline->points[n_points - 2];
962      loader->pp4 = outline->points[n_points - 1];
963    }
964
965    if ( IS_HINTED( loader->load_flags ) )
966    {
967      loader->zone.n_points += 4;
968
969      error = TT_Hint_Glyph( loader, 0 );
970    }
971
972    return error;
973  }
974
975
976  /*************************************************************************/
977  /*                                                                       */
978  /* <Function>                                                            */
979  /*    TT_Process_Composite_Component                                     */
980  /*                                                                       */
981  /* <Description>                                                         */
982  /*    Once a composite component has been loaded, it needs to be         */
983  /*    processed.  Usually, this means transforming and translating.      */
984  /*                                                                       */
985  static FT_Error
986  TT_Process_Composite_Component( TT_Loader    loader,
987                                  FT_SubGlyph  subglyph,
988                                  FT_UInt      start_point,
989                                  FT_UInt      num_base_points )
990  {
991    FT_GlyphLoader  gloader    = loader->gloader;
992    FT_Vector*      base_vec   = gloader->base.outline.points;
993    FT_UInt         num_points = gloader->base.outline.n_points;
994    FT_Bool         have_scale;
995    FT_Pos          x, y;
996
997
998    have_scale = FT_BOOL( subglyph->flags & ( WE_HAVE_A_SCALE     |
999                                              WE_HAVE_AN_XY_SCALE |
1000                                              WE_HAVE_A_2X2       ) );
1001
1002    /* perform the transform required for this subglyph */
1003    if ( have_scale )
1004    {
1005      FT_UInt  i;
1006
1007
1008      for ( i = num_base_points; i < num_points; i++ )
1009        FT_Vector_Transform( base_vec + i, &subglyph->transform );
1010    }
1011
1012    /* get offset */
1013    if ( !( subglyph->flags & ARGS_ARE_XY_VALUES ) )
1014    {
1015      FT_UInt     k = subglyph->arg1;
1016      FT_UInt     l = subglyph->arg2;
1017      FT_Vector*  p1;
1018      FT_Vector*  p2;
1019
1020
1021      /* match l-th point of the newly loaded component to the k-th point */
1022      /* of the previously loaded components.                             */
1023
1024      /* change to the point numbers used by our outline */
1025      k += start_point;
1026      l += num_base_points;
1027      if ( k >= num_base_points ||
1028           l >= num_points      )
1029        return TT_Err_Invalid_Composite;
1030
1031      p1 = gloader->base.outline.points + k;
1032      p2 = gloader->base.outline.points + l;
1033
1034      x = p1->x - p2->x;
1035      y = p1->y - p2->y;
1036    }
1037    else
1038    {
1039      x = subglyph->arg1;
1040      y = subglyph->arg2;
1041
1042      if ( !x && !y )
1043        return TT_Err_Ok;
1044
1045  /* Use a default value dependent on                                     */
1046  /* TT_CONFIG_OPTION_COMPONENT_OFFSET_SCALED.  This is useful for old TT */
1047  /* fonts which don't set the xxx_COMPONENT_OFFSET bit.                  */
1048
1049      if ( have_scale &&
1050#ifdef TT_CONFIG_OPTION_COMPONENT_OFFSET_SCALED
1051           !( subglyph->flags & UNSCALED_COMPONENT_OFFSET ) )
1052#else
1053            ( subglyph->flags & SCALED_COMPONENT_OFFSET ) )
1054#endif
1055      {
1056
1057#if 0
1058
1059  /*************************************************************************/
1060  /*                                                                       */
1061  /* This algorithm is what Apple documents.  But it doesn't work.         */
1062  /*                                                                       */
1063        int  a = subglyph->transform.xx > 0 ?  subglyph->transform.xx
1064                                            : -subglyph->transform.xx;
1065        int  b = subglyph->transform.yx > 0 ?  subglyph->transform.yx
1066                                            : -subglyph->transform.yx;
1067        int  c = subglyph->transform.xy > 0 ?  subglyph->transform.xy
1068                                            : -subglyph->transform.xy;
1069        int  d = subglyph->transform.yy > 0 ? subglyph->transform.yy
1070                                            : -subglyph->transform.yy;
1071        int  m = a > b ? a : b;
1072        int  n = c > d ? c : d;
1073
1074
1075        if ( a - b <= 33 && a - b >= -33 )
1076          m *= 2;
1077        if ( c - d <= 33 && c - d >= -33 )
1078          n *= 2;
1079        x = FT_MulFix( x, m );
1080        y = FT_MulFix( y, n );
1081
1082#else /* 0 */
1083
1084  /*************************************************************************/
1085  /*                                                                       */
1086  /* This algorithm is a guess and works much better than the above.       */
1087  /*                                                                       */
1088        FT_Fixed  mac_xscale = FT_SqrtFixed(
1089                                 (FT_Int32)FT_MulFix( subglyph->transform.xx,
1090                                                      subglyph->transform.xx ) +
1091                                 (FT_Int32)FT_MulFix( subglyph->transform.xy,
1092                                                      subglyph->transform.xy ) );
1093        FT_Fixed  mac_yscale = FT_SqrtFixed(
1094                                 (FT_Int32)FT_MulFix( subglyph->transform.yy,
1095                                                      subglyph->transform.yy ) +
1096                                 (FT_Int32)FT_MulFix( subglyph->transform.yx,
1097                                                      subglyph->transform.yx ) );
1098
1099
1100        x = FT_MulFix( x, mac_xscale );
1101        y = FT_MulFix( y, mac_yscale );
1102
1103#endif /* 0 */
1104
1105      }
1106
1107      if ( !( loader->load_flags & FT_LOAD_NO_SCALE ) )
1108      {
1109        FT_Fixed  x_scale = ((TT_Size)loader->size)->metrics.x_scale;
1110        FT_Fixed  y_scale = ((TT_Size)loader->size)->metrics.y_scale;
1111
1112
1113        x = FT_MulFix( x, x_scale );
1114        y = FT_MulFix( y, y_scale );
1115
1116        if ( subglyph->flags & ROUND_XY_TO_GRID )
1117        {
1118          x = FT_PIX_ROUND( x );
1119          y = FT_PIX_ROUND( y );
1120        }
1121      }
1122    }
1123
1124    if ( x || y )
1125      translate_array( num_points - num_base_points,
1126                       base_vec + num_base_points,
1127                       x, y );
1128
1129    return TT_Err_Ok;
1130  }
1131
1132
1133  /*************************************************************************/
1134  /*                                                                       */
1135  /* <Function>                                                            */
1136  /*    TT_Process_Composite_Glyph                                         */
1137  /*                                                                       */
1138  /* <Description>                                                         */
1139  /*    This is slightly different from TT_Process_Simple_Glyph, in that   */
1140  /*    its sole purpose is to hint the glyph.  Thus this function is      */
1141  /*    only available when bytecode interpreter is enabled.               */
1142  /*                                                                       */
1143  static FT_Error
1144  TT_Process_Composite_Glyph( TT_Loader  loader,
1145                              FT_UInt    start_point,
1146                              FT_UInt    start_contour )
1147  {
1148    FT_Error     error;
1149    FT_Outline*  outline;
1150    FT_UInt      i;
1151
1152
1153    outline = &loader->gloader->base.outline;
1154
1155    /* make room for phantom points */
1156    error = FT_GLYPHLOADER_CHECK_POINTS( loader->gloader,
1157                                         outline->n_points + 4,
1158                                         0 );
1159    if ( error )
1160      return error;
1161
1162    outline->points[outline->n_points    ] = loader->pp1;
1163    outline->points[outline->n_points + 1] = loader->pp2;
1164    outline->points[outline->n_points + 2] = loader->pp3;
1165    outline->points[outline->n_points + 3] = loader->pp4;
1166
1167    outline->tags[outline->n_points    ] = 0;
1168    outline->tags[outline->n_points + 1] = 0;
1169    outline->tags[outline->n_points + 2] = 0;
1170    outline->tags[outline->n_points + 3] = 0;
1171
1172#ifdef TT_USE_BYTECODE_INTERPRETER
1173
1174    {
1175      FT_Stream  stream = loader->stream;
1176      FT_UShort  n_ins, max_ins;
1177      FT_ULong   tmp;
1178
1179
1180      /* TT_Load_Composite_Glyph only gives us the offset of instructions */
1181      /* so we read them here                                             */
1182      if ( FT_STREAM_SEEK( loader->ins_pos ) ||
1183           FT_READ_USHORT( n_ins )           )
1184        return error;
1185
1186      FT_TRACE5(( "  Instructions size = %d\n", n_ins ));
1187
1188      /* check it */
1189      max_ins = ((TT_Face)loader->face)->max_profile.maxSizeOfInstructions;
1190      if ( n_ins > max_ins )
1191      {
1192        /* acroread ignores this field, so we only do a rough safety check */
1193        if ( (FT_Int)n_ins > loader->byte_len )
1194        {
1195          FT_TRACE1(( "TT_Process_Composite_Glyph: "
1196                      "too many instructions (%d) for glyph with length %d\n",
1197                      n_ins, loader->byte_len ));
1198          return TT_Err_Too_Many_Hints;
1199        }
1200
1201        tmp = loader->exec->glyphSize;
1202        error = Update_Max( loader->exec->memory,
1203                            &tmp,
1204                            sizeof ( FT_Byte ),
1205                            (void*)&loader->exec->glyphIns,
1206                            n_ins );
1207        loader->exec->glyphSize = (FT_UShort)tmp;
1208        if ( error )
1209          return error;
1210      }
1211      else if ( n_ins == 0 )
1212        return TT_Err_Ok;
1213
1214      if ( FT_STREAM_READ( loader->exec->glyphIns, n_ins ) )
1215        return error;
1216
1217      loader->glyph->control_data = loader->exec->glyphIns;
1218      loader->glyph->control_len  = n_ins;
1219    }
1220
1221#endif
1222
1223    tt_prepare_zone( &loader->zone, &loader->gloader->base,
1224                     start_point, start_contour );
1225
1226    /* Some points are likely touched during execution of  */
1227    /* instructions on components.  So let's untouch them. */
1228    for ( i = start_point; i < loader->zone.n_points; i++ )
1229      loader->zone.tags[i] &= ~FT_CURVE_TAG_TOUCH_BOTH;
1230
1231    loader->zone.n_points += 4;
1232
1233    return TT_Hint_Glyph( loader, 1 );
1234  }
1235
1236
1237  /* Calculate the four phantom points.                     */
1238  /* The first two stand for horizontal origin and advance. */
1239  /* The last two stand for vertical origin and advance.    */
1240#define TT_LOADER_SET_PP( loader )                                          \
1241          do {                                                              \
1242            (loader)->pp1.x = (loader)->bbox.xMin - (loader)->left_bearing; \
1243            (loader)->pp1.y = 0;                                            \
1244            (loader)->pp2.x = (loader)->pp1.x + (loader)->advance;          \
1245            (loader)->pp2.y = 0;                                            \
1246            (loader)->pp3.x = 0;                                            \
1247            (loader)->pp3.y = (loader)->top_bearing + (loader)->bbox.yMax;  \
1248            (loader)->pp4.x = 0;                                            \
1249            (loader)->pp4.y = (loader)->pp3.y - (loader)->vadvance;         \
1250          } while ( 0 )
1251
1252
1253  /*************************************************************************/
1254  /*                                                                       */
1255  /* <Function>                                                            */
1256  /*    load_truetype_glyph                                                */
1257  /*                                                                       */
1258  /* <Description>                                                         */
1259  /*    Loads a given truetype glyph.  Handles composites and uses a       */
1260  /*    TT_Loader object.                                                  */
1261  /*                                                                       */
1262  static FT_Error
1263  load_truetype_glyph( TT_Loader  loader,
1264                       FT_UInt    glyph_index,
1265                       FT_UInt    recurse_count,
1266                       FT_Bool    header_only )
1267  {
1268    FT_Error        error        = TT_Err_Ok;
1269    FT_Fixed        x_scale, y_scale;
1270    FT_ULong        offset;
1271    TT_Face         face         = (TT_Face)loader->face;
1272    FT_GlyphLoader  gloader      = loader->gloader;
1273    FT_Bool         opened_frame = 0;
1274
1275#ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
1276    FT_Vector*      deltas       = NULL;
1277#endif
1278
1279#ifdef FT_CONFIG_OPTION_INCREMENTAL
1280    FT_StreamRec    inc_stream;
1281    FT_Data         glyph_data;
1282    FT_Bool         glyph_data_loaded = 0;
1283#endif
1284
1285
1286    /* some fonts have an incorrect value of `maxComponentDepth', */
1287    /* thus we allow depth 1 to catch the majority of them        */
1288    if ( recurse_count > 1                                   &&
1289         recurse_count > face->max_profile.maxComponentDepth )
1290    {
1291      error = TT_Err_Invalid_Composite;
1292      goto Exit;
1293    }
1294
1295    /* check glyph index */
1296    if ( glyph_index >= (FT_UInt)face->root.num_glyphs )
1297    {
1298      error = TT_Err_Invalid_Glyph_Index;
1299      goto Exit;
1300    }
1301
1302    loader->glyph_index = glyph_index;
1303
1304    if ( ( loader->load_flags & FT_LOAD_NO_SCALE ) == 0 )
1305    {
1306      x_scale = ((TT_Size)loader->size)->metrics.x_scale;
1307      y_scale = ((TT_Size)loader->size)->metrics.y_scale;
1308    }
1309    else
1310    {
1311      x_scale = 0x10000L;
1312      y_scale = 0x10000L;
1313    }
1314
1315    tt_get_metrics( loader, glyph_index );
1316
1317    /* Set `offset' to the start of the glyph relative to the start of */
1318    /* the `glyf' table, and `byte_len' to the length of the glyph in  */
1319    /* bytes.                                                          */
1320
1321#ifdef FT_CONFIG_OPTION_INCREMENTAL
1322
1323    /* If we are loading glyph data via the incremental interface, set */
1324    /* the loader stream to a memory stream reading the data returned  */
1325    /* by the interface.                                               */
1326    if ( face->root.internal->incremental_interface )
1327    {
1328      error = face->root.internal->incremental_interface->funcs->get_glyph_data(
1329                face->root.internal->incremental_interface->object,
1330                glyph_index, &glyph_data );
1331      if ( error )
1332        goto Exit;
1333
1334      glyph_data_loaded = 1;
1335      offset            = 0;
1336      loader->byte_len  = glyph_data.length;
1337
1338      FT_MEM_ZERO( &inc_stream, sizeof ( inc_stream ) );
1339      FT_Stream_OpenMemory( &inc_stream,
1340                            glyph_data.pointer, glyph_data.length );
1341
1342      loader->stream = &inc_stream;
1343    }
1344    else
1345
1346#endif /* FT_CONFIG_OPTION_INCREMENTAL */
1347
1348      offset = tt_face_get_location( face, glyph_index,
1349                                     (FT_UInt*)&loader->byte_len );
1350
1351    if ( loader->byte_len > 0 )
1352    {
1353#ifdef FT_CONFIG_OPTION_INCREMENTAL
1354      /* for the incremental interface, `glyf_offset' is always zero */
1355      if ( !loader->glyf_offset                        &&
1356           !face->root.internal->incremental_interface )
1357#else
1358      if ( !loader->glyf_offset )
1359#endif /* FT_CONFIG_OPTION_INCREMENTAL */
1360      {
1361        FT_TRACE2(( "no `glyf' table but non-zero `loca' entry\n" ));
1362        error = TT_Err_Invalid_Table;
1363        goto Exit;
1364      }
1365
1366      error = face->access_glyph_frame( loader, glyph_index,
1367                                        loader->glyf_offset + offset,
1368                                        loader->byte_len );
1369      if ( error )
1370        goto Exit;
1371
1372      opened_frame = 1;
1373
1374      /* read glyph header first */
1375      error = face->read_glyph_header( loader );
1376      if ( error || header_only )
1377        goto Exit;
1378    }
1379
1380    if ( loader->byte_len == 0 || loader->n_contours == 0 )
1381    {
1382      loader->bbox.xMin = 0;
1383      loader->bbox.xMax = 0;
1384      loader->bbox.yMin = 0;
1385      loader->bbox.yMax = 0;
1386
1387      if ( header_only )
1388        goto Exit;
1389
1390      /* must initialize points before (possibly) overriding */
1391      /* glyph metrics from the incremental interface        */
1392      TT_LOADER_SET_PP( loader );
1393
1394#ifdef FT_CONFIG_OPTION_INCREMENTAL
1395      tt_get_metrics_incr_overrides( loader, glyph_index );
1396#endif
1397
1398#ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
1399
1400      if ( ((TT_Face)(loader->face))->doblend )
1401      {
1402        /* this must be done before scaling */
1403        FT_Memory  memory = loader->face->memory;
1404
1405
1406        error = TT_Vary_Get_Glyph_Deltas( (TT_Face)(loader->face),
1407                                          glyph_index, &deltas, 4 );
1408        if ( error )
1409          goto Exit;
1410
1411        loader->pp1.x += deltas[0].x; loader->pp1.y += deltas[0].y;
1412        loader->pp2.x += deltas[1].x; loader->pp2.y += deltas[1].y;
1413        loader->pp3.x += deltas[2].x; loader->pp3.y += deltas[2].y;
1414        loader->pp4.x += deltas[3].x; loader->pp4.y += deltas[3].y;
1415
1416        FT_FREE( deltas );
1417      }
1418
1419#endif
1420
1421      if ( ( loader->load_flags & FT_LOAD_NO_SCALE ) == 0 )
1422      {
1423        loader->pp1.x = FT_MulFix( loader->pp1.x, x_scale );
1424        loader->pp2.x = FT_MulFix( loader->pp2.x, x_scale );
1425        loader->pp3.y = FT_MulFix( loader->pp3.y, y_scale );
1426        loader->pp4.y = FT_MulFix( loader->pp4.y, y_scale );
1427      }
1428
1429      error = TT_Err_Ok;
1430      goto Exit;
1431    }
1432
1433    /* must initialize points before (possibly) overriding */
1434    /* glyph metrics from the incremental interface        */
1435    TT_LOADER_SET_PP( loader );
1436
1437#ifdef FT_CONFIG_OPTION_INCREMENTAL
1438    tt_get_metrics_incr_overrides( loader, glyph_index );
1439#endif
1440
1441    /***********************************************************************/
1442    /***********************************************************************/
1443    /***********************************************************************/
1444
1445    /* if it is a simple glyph, load it */
1446
1447    if ( loader->n_contours > 0 )
1448    {
1449      error = face->read_simple_glyph( loader );
1450      if ( error )
1451        goto Exit;
1452
1453      /* all data have been read */
1454      face->forget_glyph_frame( loader );
1455      opened_frame = 0;
1456
1457      error = TT_Process_Simple_Glyph( loader );
1458      if ( error )
1459        goto Exit;
1460
1461      FT_GlyphLoader_Add( gloader );
1462    }
1463
1464    /***********************************************************************/
1465    /***********************************************************************/
1466    /***********************************************************************/
1467
1468    /* otherwise, load a composite! */
1469    else if ( loader->n_contours == -1 )
1470    {
1471      FT_UInt   start_point;
1472      FT_UInt   start_contour;
1473      FT_ULong  ins_pos;  /* position of composite instructions, if any */
1474
1475
1476      start_point   = gloader->base.outline.n_points;
1477      start_contour = gloader->base.outline.n_contours;
1478
1479      /* for each subglyph, read composite header */
1480      error = face->read_composite_glyph( loader );
1481      if ( error )
1482        goto Exit;
1483
1484      /* store the offset of instructions */
1485      ins_pos = loader->ins_pos;
1486
1487      /* all data we need are read */
1488      face->forget_glyph_frame( loader );
1489      opened_frame = 0;
1490
1491#ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
1492
1493      if ( face->doblend )
1494      {
1495        FT_Int       i, limit;
1496        FT_SubGlyph  subglyph;
1497        FT_Memory    memory = face->root.memory;
1498
1499
1500        /* this provides additional offsets */
1501        /* for each component's translation */
1502
1503        if ( ( error = TT_Vary_Get_Glyph_Deltas(
1504                         face,
1505                         glyph_index,
1506                         &deltas,
1507                         gloader->current.num_subglyphs + 4 )) != 0 )
1508          goto Exit;
1509
1510        subglyph = gloader->current.subglyphs + gloader->base.num_subglyphs;
1511        limit    = gloader->current.num_subglyphs;
1512
1513        for ( i = 0; i < limit; ++i, ++subglyph )
1514        {
1515          if ( subglyph->flags & ARGS_ARE_XY_VALUES )
1516          {
1517            /* XXX: overflow check for subglyph->{arg1,arg2}.   */
1518            /* deltas[i].{x,y} must be within signed 16-bit,    */
1519            /* but the restriction of summed delta is not clear */
1520            subglyph->arg1 += (FT_Int16)deltas[i].x;
1521            subglyph->arg2 += (FT_Int16)deltas[i].y;
1522          }
1523        }
1524
1525        loader->pp1.x += deltas[i + 0].x; loader->pp1.y += deltas[i + 0].y;
1526        loader->pp2.x += deltas[i + 1].x; loader->pp2.y += deltas[i + 1].y;
1527        loader->pp3.x += deltas[i + 2].x; loader->pp3.y += deltas[i + 2].y;
1528        loader->pp4.x += deltas[i + 3].x; loader->pp4.y += deltas[i + 3].y;
1529
1530        FT_FREE( deltas );
1531      }
1532
1533#endif /* TT_CONFIG_OPTION_GX_VAR_SUPPORT */
1534
1535      if ( ( loader->load_flags & FT_LOAD_NO_SCALE ) == 0 )
1536      {
1537        loader->pp1.x = FT_MulFix( loader->pp1.x, x_scale );
1538        loader->pp2.x = FT_MulFix( loader->pp2.x, x_scale );
1539        loader->pp3.y = FT_MulFix( loader->pp3.y, y_scale );
1540        loader->pp4.y = FT_MulFix( loader->pp4.y, y_scale );
1541      }
1542
1543      /* if the flag FT_LOAD_NO_RECURSE is set, we return the subglyph */
1544      /* `as is' in the glyph slot (the client application will be     */
1545      /* responsible for interpreting these data)...                   */
1546      if ( loader->load_flags & FT_LOAD_NO_RECURSE )
1547      {
1548        FT_GlyphLoader_Add( gloader );
1549        loader->glyph->format = FT_GLYPH_FORMAT_COMPOSITE;
1550
1551        goto Exit;
1552      }
1553
1554      /*********************************************************************/
1555      /*********************************************************************/
1556      /*********************************************************************/
1557
1558      {
1559        FT_UInt      n, num_base_points;
1560        FT_SubGlyph  subglyph       = 0;
1561
1562        FT_UInt      num_points     = start_point;
1563        FT_UInt      num_subglyphs  = gloader->current.num_subglyphs;
1564        FT_UInt      num_base_subgs = gloader->base.num_subglyphs;
1565
1566        FT_Stream    old_stream     = loader->stream;
1567        FT_Int       old_byte_len   = loader->byte_len;
1568
1569
1570        FT_GlyphLoader_Add( gloader );
1571
1572        /* read each subglyph independently */
1573        for ( n = 0; n < num_subglyphs; n++ )
1574        {
1575          FT_Vector  pp[4];
1576
1577
1578          /* Each time we call load_truetype_glyph in this loop, the   */
1579          /* value of `gloader.base.subglyphs' can change due to table */
1580          /* reallocations.  We thus need to recompute the subglyph    */
1581          /* pointer on each iteration.                                */
1582          subglyph = gloader->base.subglyphs + num_base_subgs + n;
1583
1584          pp[0] = loader->pp1;
1585          pp[1] = loader->pp2;
1586          pp[2] = loader->pp3;
1587          pp[3] = loader->pp4;
1588
1589          num_base_points = gloader->base.outline.n_points;
1590
1591          error = load_truetype_glyph( loader, subglyph->index,
1592                                       recurse_count + 1, FALSE );
1593          if ( error )
1594            goto Exit;
1595
1596          /* restore subglyph pointer */
1597          subglyph = gloader->base.subglyphs + num_base_subgs + n;
1598
1599          if ( !( subglyph->flags & USE_MY_METRICS ) )
1600          {
1601            loader->pp1 = pp[0];
1602            loader->pp2 = pp[1];
1603            loader->pp3 = pp[2];
1604            loader->pp4 = pp[3];
1605          }
1606
1607          num_points = gloader->base.outline.n_points;
1608
1609          if ( num_points == num_base_points )
1610            continue;
1611
1612          /* gloader->base.outline consists of three parts:               */
1613          /* 0 -(1)-> start_point -(2)-> num_base_points -(3)-> n_points. */
1614          /*                                                              */
1615          /* (1): exists from the beginning                               */
1616          /* (2): components that have been loaded so far                 */
1617          /* (3): the newly loaded component                              */
1618          TT_Process_Composite_Component( loader, subglyph, start_point,
1619                                          num_base_points );
1620        }
1621
1622        loader->stream   = old_stream;
1623        loader->byte_len = old_byte_len;
1624
1625        /* process the glyph */
1626        loader->ins_pos = ins_pos;
1627        if ( IS_HINTED( loader->load_flags ) &&
1628
1629#ifdef TT_USE_BYTECODE_INTERPRETER
1630
1631             subglyph->flags & WE_HAVE_INSTR &&
1632
1633#endif
1634
1635             num_points > start_point )
1636          TT_Process_Composite_Glyph( loader, start_point, start_contour );
1637
1638      }
1639    }
1640    else
1641    {
1642      /* invalid composite count (negative but not -1) */
1643      error = TT_Err_Invalid_Outline;
1644      goto Exit;
1645    }
1646
1647    /***********************************************************************/
1648    /***********************************************************************/
1649    /***********************************************************************/
1650
1651  Exit:
1652
1653    if ( opened_frame )
1654      face->forget_glyph_frame( loader );
1655
1656#ifdef FT_CONFIG_OPTION_INCREMENTAL
1657
1658    if ( glyph_data_loaded )
1659      face->root.internal->incremental_interface->funcs->free_glyph_data(
1660        face->root.internal->incremental_interface->object,
1661        &glyph_data );
1662
1663#endif
1664
1665    return error;
1666  }
1667
1668
1669  static FT_Error
1670  compute_glyph_metrics( TT_Loader  loader,
1671                         FT_UInt    glyph_index )
1672  {
1673    FT_BBox       bbox;
1674    TT_Face       face = (TT_Face)loader->face;
1675    FT_Fixed      y_scale;
1676    TT_GlyphSlot  glyph = loader->glyph;
1677    TT_Size       size = (TT_Size)loader->size;
1678
1679
1680    y_scale = 0x10000L;
1681    if ( ( loader->load_flags & FT_LOAD_NO_SCALE ) == 0 )
1682      y_scale = size->root.metrics.y_scale;
1683
1684    if ( glyph->format != FT_GLYPH_FORMAT_COMPOSITE )
1685      FT_Outline_Get_CBox( &glyph->outline, &bbox );
1686    else
1687      bbox = loader->bbox;
1688
1689    /* get the device-independent horizontal advance; it is scaled later */
1690    /* by the base layer.                                                */
1691    glyph->linearHoriAdvance = loader->linear;
1692
1693    glyph->metrics.horiBearingX = bbox.xMin;
1694    glyph->metrics.horiBearingY = bbox.yMax;
1695    glyph->metrics.horiAdvance  = loader->pp2.x - loader->pp1.x;
1696
1697    /* adjust advance width to the value contained in the hdmx table */
1698    if ( !face->postscript.isFixedPitch  &&
1699         IS_HINTED( loader->load_flags ) )
1700    {
1701      FT_Byte*  widthp;
1702
1703#ifdef TT_CONFIG_OPTION_SUBPIXEL_HINTING
1704      FT_Bool  ignore_x_mode;
1705
1706
1707      ignore_x_mode = FT_BOOL( FT_LOAD_TARGET_MODE( loader->load_flags ) !=
1708                               FT_RENDER_MODE_MONO );
1709#endif /* TT_CONFIG_OPTION_SUBPIXEL_HINTING */
1710
1711      widthp = tt_face_get_device_metrics( face,
1712                                           size->root.metrics.x_ppem,
1713                                           glyph_index );
1714
1715#ifdef TT_CONFIG_OPTION_SUBPIXEL_HINTING
1716      if ( widthp                                                   &&
1717           ( ( ignore_x_mode && loader->exec->compatible_widths ) ||
1718              !ignore_x_mode                                      ||
1719              SPH_OPTION_BITMAP_WIDTHS                            ) )
1720#else
1721      if ( widthp )
1722#endif /* TT_CONFIG_OPTION_SUBPIXEL_HINTING */
1723        glyph->metrics.horiAdvance = *widthp << 6;
1724    }
1725
1726    /* set glyph dimensions */
1727    glyph->metrics.width  = bbox.xMax - bbox.xMin;
1728    glyph->metrics.height = bbox.yMax - bbox.yMin;
1729
1730    /* Now take care of vertical metrics.  In the case where there is */
1731    /* no vertical information within the font (relatively common),   */
1732    /* create some metrics manually                                   */
1733    {
1734      FT_Pos  top;      /* scaled vertical top side bearing  */
1735      FT_Pos  advance;  /* scaled vertical advance height    */
1736
1737
1738      /* Get the unscaled top bearing and advance height. */
1739      if ( face->vertical_info                   &&
1740           face->vertical.number_Of_VMetrics > 0 )
1741      {
1742        top = (FT_Short)FT_DivFix( loader->pp3.y - bbox.yMax,
1743                                   y_scale );
1744
1745        if ( loader->pp3.y <= loader->pp4.y )
1746          advance = 0;
1747        else
1748          advance = (FT_UShort)FT_DivFix( loader->pp3.y - loader->pp4.y,
1749                                          y_scale );
1750      }
1751      else
1752      {
1753        FT_Pos  height;
1754
1755
1756        /* XXX Compute top side bearing and advance height in  */
1757        /*     Get_VMetrics instead of here.                   */
1758
1759        /* NOTE: The OS/2 values are the only `portable' ones, */
1760        /*       which is why we use them, if there is an OS/2 */
1761        /*       table in the font.  Otherwise, we use the     */
1762        /*       values defined in the horizontal header.      */
1763
1764        height = (FT_Short)FT_DivFix( bbox.yMax - bbox.yMin,
1765                                      y_scale );
1766        if ( face->os2.version != 0xFFFFU )
1767          advance = (FT_Pos)( face->os2.sTypoAscender -
1768                              face->os2.sTypoDescender );
1769        else
1770          advance = (FT_Pos)( face->horizontal.Ascender -
1771                              face->horizontal.Descender );
1772
1773        top = ( advance - height ) / 2;
1774      }
1775
1776#ifdef FT_CONFIG_OPTION_INCREMENTAL
1777      {
1778        FT_Incremental_InterfaceRec*  incr;
1779        FT_Incremental_MetricsRec     metrics;
1780        FT_Error                      error;
1781
1782
1783        incr = face->root.internal->incremental_interface;
1784
1785        /* If this is an incrementally loaded font see if there are */
1786        /* overriding metrics for this glyph.                       */
1787        if ( incr && incr->funcs->get_glyph_metrics )
1788        {
1789          metrics.bearing_x = 0;
1790          metrics.bearing_y = top;
1791          metrics.advance   = advance;
1792
1793          error = incr->funcs->get_glyph_metrics( incr->object,
1794                                                  glyph_index,
1795                                                  TRUE,
1796                                                  &metrics );
1797          if ( error )
1798            return error;
1799
1800          top     = metrics.bearing_y;
1801          advance = metrics.advance;
1802        }
1803      }
1804
1805      /* GWW: Do vertical metrics get loaded incrementally too? */
1806
1807#endif /* FT_CONFIG_OPTION_INCREMENTAL */
1808
1809      glyph->linearVertAdvance = advance;
1810
1811      /* scale the metrics */
1812      if ( !( loader->load_flags & FT_LOAD_NO_SCALE ) )
1813      {
1814        top     = FT_MulFix( top,     y_scale );
1815        advance = FT_MulFix( advance, y_scale );
1816      }
1817
1818      /* XXX: for now, we have no better algorithm for the lsb, but it */
1819      /*      should work fine.                                        */
1820      /*                                                               */
1821      glyph->metrics.vertBearingX = glyph->metrics.horiBearingX -
1822                                      glyph->metrics.horiAdvance / 2;
1823      glyph->metrics.vertBearingY = top;
1824      glyph->metrics.vertAdvance  = advance;
1825    }
1826
1827    return 0;
1828  }
1829
1830
1831#ifdef TT_CONFIG_OPTION_EMBEDDED_BITMAPS
1832
1833  static FT_Error
1834  load_sbit_image( TT_Size       size,
1835                   TT_GlyphSlot  glyph,
1836                   FT_UInt       glyph_index,
1837                   FT_Int32      load_flags )
1838  {
1839    TT_Face             face;
1840    SFNT_Service        sfnt;
1841    FT_Stream           stream;
1842    FT_Error            error;
1843    TT_SBit_MetricsRec  metrics;
1844
1845
1846    face   = (TT_Face)glyph->face;
1847    sfnt   = (SFNT_Service)face->sfnt;
1848    stream = face->root.stream;
1849
1850    error = sfnt->load_sbit_image( face,
1851                                   size->strike_index,
1852                                   glyph_index,
1853                                   (FT_Int)load_flags,
1854                                   stream,
1855                                   &glyph->bitmap,
1856                                   &metrics );
1857    if ( !error )
1858    {
1859      glyph->outline.n_points   = 0;
1860      glyph->outline.n_contours = 0;
1861
1862      glyph->metrics.width  = (FT_Pos)metrics.width  << 6;
1863      glyph->metrics.height = (FT_Pos)metrics.height << 6;
1864
1865      glyph->metrics.horiBearingX = (FT_Pos)metrics.horiBearingX << 6;
1866      glyph->metrics.horiBearingY = (FT_Pos)metrics.horiBearingY << 6;
1867      glyph->metrics.horiAdvance  = (FT_Pos)metrics.horiAdvance  << 6;
1868
1869      glyph->metrics.vertBearingX = (FT_Pos)metrics.vertBearingX << 6;
1870      glyph->metrics.vertBearingY = (FT_Pos)metrics.vertBearingY << 6;
1871      glyph->metrics.vertAdvance  = (FT_Pos)metrics.vertAdvance  << 6;
1872
1873      glyph->format = FT_GLYPH_FORMAT_BITMAP;
1874
1875      if ( load_flags & FT_LOAD_VERTICAL_LAYOUT )
1876      {
1877        glyph->bitmap_left = metrics.vertBearingX;
1878        glyph->bitmap_top  = metrics.vertBearingY;
1879      }
1880      else
1881      {
1882        glyph->bitmap_left = metrics.horiBearingX;
1883        glyph->bitmap_top  = metrics.horiBearingY;
1884      }
1885    }
1886
1887    return error;
1888  }
1889
1890#endif /* TT_CONFIG_OPTION_EMBEDDED_BITMAPS */
1891
1892
1893  static FT_Error
1894  tt_loader_init( TT_Loader     loader,
1895                  TT_Size       size,
1896                  TT_GlyphSlot  glyph,
1897                  FT_Int32      load_flags,
1898                  FT_Bool       glyf_table_only )
1899  {
1900    TT_Face    face;
1901    FT_Stream  stream;
1902    FT_Bool    pedantic = FT_BOOL( load_flags & FT_LOAD_PEDANTIC );
1903
1904
1905    face   = (TT_Face)glyph->face;
1906    stream = face->root.stream;
1907
1908    FT_MEM_ZERO( loader, sizeof ( TT_LoaderRec ) );
1909
1910#ifdef TT_USE_BYTECODE_INTERPRETER
1911
1912    /* load execution context */
1913    if ( IS_HINTED( load_flags ) && !glyf_table_only )
1914    {
1915      TT_ExecContext  exec;
1916      FT_Bool         grayscale;
1917#ifdef TT_CONFIG_OPTION_SUBPIXEL_HINTING
1918      FT_Bool         subpixel_hinting;
1919      FT_Bool         grayscale_hinting;
1920#if 0
1921      FT_Bool         compatible_widths;
1922      FT_Bool         symmetrical_smoothing;
1923      FT_Bool         bgr;
1924#endif
1925#endif /* TT_CONFIG_OPTION_SUBPIXEL_HINTING */
1926
1927
1928      if ( !size->cvt_ready )
1929      {
1930        FT_Error  error = tt_size_ready_bytecode( size, pedantic );
1931
1932
1933        if ( error )
1934          return error;
1935      }
1936
1937      /* query new execution context */
1938      exec = size->debug ? size->context
1939                         : ( (TT_Driver)FT_FACE_DRIVER( face ) )->context;
1940      if ( !exec )
1941        return TT_Err_Could_Not_Find_Context;
1942
1943#ifdef TT_CONFIG_OPTION_SUBPIXEL_HINTING
1944
1945      subpixel_hinting = FT_BOOL( ( FT_LOAD_TARGET_MODE( load_flags )
1946                                    != FT_RENDER_MODE_MONO )          &&
1947                                  SPH_OPTION_SET_SUBPIXEL             );
1948
1949      if ( subpixel_hinting )
1950        grayscale = grayscale_hinting = FALSE;
1951
1952      else if ( SPH_OPTION_SET_GRAYSCALE )
1953      {
1954        grayscale = grayscale_hinting = TRUE;
1955        subpixel_hinting = FALSE;
1956      }
1957
1958      if ( FT_IS_TRICKY( glyph->face ) )
1959        subpixel_hinting = grayscale_hinting = FALSE;
1960
1961      exec->ignore_x_mode      = subpixel_hinting || grayscale_hinting;
1962      exec->rasterizer_version = SPH_OPTION_SET_RASTERIZER_VERSION;
1963      if ( exec->sph_tweak_flags & SPH_TWEAK_RASTERIZER_35 )
1964        exec->rasterizer_version = 35;
1965
1966#if 1
1967      exec->compatible_widths     = SPH_OPTION_SET_COMPATIBLE_WIDTHS;
1968      exec->symmetrical_smoothing = FALSE;
1969      exec->bgr                   = FALSE;
1970#else /* 0 */
1971      exec->compatible_widths =
1972        FT_BOOL( FT_LOAD_TARGET_MODE( load_flags ) !=
1973                 TT_LOAD_COMPATIBLE_WIDTHS );
1974      exec->symmetrical_smoothing =
1975        FT_BOOL( FT_LOAD_TARGET_MODE( load_flags ) !=
1976                 TT_LOAD_SYMMETRICAL_SMOOTHING );
1977      exec->bgr =
1978        FT_BOOL( FT_LOAD_TARGET_MODE( load_flags ) !=
1979                 TT_LOAD_BGR );
1980#endif /* 0 */
1981
1982#else /* !TT_CONFIG_OPTION_SUBPIXEL_HINTING */
1983
1984      grayscale =
1985        FT_BOOL( FT_LOAD_TARGET_MODE( load_flags ) != FT_RENDER_MODE_MONO );
1986
1987#endif /* !TT_CONFIG_OPTION_SUBPIXEL_HINTING */
1988
1989      TT_Load_Context( exec, face, size );
1990
1991#ifdef TT_CONFIG_OPTION_SUBPIXEL_HINTING
1992
1993      /* a change from mono to subpixel rendering (and vice versa) */
1994      /* requires a re-execution of the CVT program                */
1995      if ( subpixel_hinting != exec->subpixel_hinting )
1996      {
1997        FT_UInt  i;
1998
1999
2000        exec->subpixel_hinting = subpixel_hinting;
2001
2002        for ( i = 0; i < size->cvt_size; i++ )
2003          size->cvt[i] = FT_MulFix( face->cvt[i], size->ttmetrics.scale );
2004        tt_size_run_prep( size, pedantic );
2005      }
2006
2007      /* a change from mono to grayscale rendering (and vice versa) */
2008      /* requires a re-execution of the CVT program                 */
2009      if ( grayscale != exec->grayscale_hinting )
2010      {
2011        FT_UInt  i;
2012
2013
2014        exec->grayscale_hinting = grayscale_hinting;
2015
2016        for ( i = 0; i < size->cvt_size; i++ )
2017          size->cvt[i] = FT_MulFix( face->cvt[i], size->ttmetrics.scale );
2018        tt_size_run_prep( size, pedantic );
2019      }
2020
2021#else /* !TT_CONFIG_OPTION_SUBPIXEL_HINTING */
2022
2023      /* a change from mono to grayscale rendering (and vice versa) */
2024      /* requires a re-execution of the CVT program                 */
2025      if ( grayscale != exec->grayscale )
2026      {
2027        FT_UInt  i;
2028
2029
2030        FT_TRACE4(( "tt_loader_init: grayscale change,"
2031                    " re-executing `prep' table\n" ));
2032
2033        exec->grayscale = grayscale;
2034
2035        for ( i = 0; i < size->cvt_size; i++ )
2036          size->cvt[i] = FT_MulFix( face->cvt[i], size->ttmetrics.scale );
2037        tt_size_run_prep( size, pedantic );
2038      }
2039
2040#endif /* !TT_CONFIG_OPTION_SUBPIXEL_HINTING */
2041
2042      /* see whether the cvt program has disabled hinting */
2043      if ( exec->GS.instruct_control & 1 )
2044        load_flags |= FT_LOAD_NO_HINTING;
2045
2046      /* load default graphics state -- if needed */
2047      if ( exec->GS.instruct_control & 2 )
2048        exec->GS = tt_default_graphics_state;
2049
2050      exec->pedantic_hinting = FT_BOOL( load_flags & FT_LOAD_PEDANTIC );
2051      loader->exec = exec;
2052      loader->instructions = exec->glyphIns;
2053    }
2054
2055#endif /* TT_USE_BYTECODE_INTERPRETER */
2056
2057    /* seek to the beginning of the glyph table -- for Type 42 fonts     */
2058    /* the table might be accessed from a Postscript stream or something */
2059    /* else...                                                           */
2060
2061#ifdef FT_CONFIG_OPTION_INCREMENTAL
2062
2063    if ( face->root.internal->incremental_interface )
2064      loader->glyf_offset = 0;
2065    else
2066
2067#endif
2068
2069    {
2070      FT_Error  error = face->goto_table( face, TTAG_glyf, stream, 0 );
2071
2072
2073      if ( error == TT_Err_Table_Missing )
2074        loader->glyf_offset = 0;
2075      else if ( error )
2076      {
2077        FT_ERROR(( "tt_loader_init: could not access glyph table\n" ));
2078        return error;
2079      }
2080      else
2081        loader->glyf_offset = FT_STREAM_POS();
2082    }
2083
2084    /* get face's glyph loader */
2085    if ( !glyf_table_only )
2086    {
2087      FT_GlyphLoader  gloader = glyph->internal->loader;
2088
2089
2090      FT_GlyphLoader_Rewind( gloader );
2091      loader->gloader = gloader;
2092    }
2093
2094    loader->load_flags = load_flags;
2095
2096    loader->face   = (FT_Face)face;
2097    loader->size   = (FT_Size)size;
2098    loader->glyph  = (FT_GlyphSlot)glyph;
2099    loader->stream = stream;
2100
2101    return TT_Err_Ok;
2102  }
2103
2104
2105  /*************************************************************************/
2106  /*                                                                       */
2107  /* <Function>                                                            */
2108  /*    TT_Load_Glyph                                                      */
2109  /*                                                                       */
2110  /* <Description>                                                         */
2111  /*    A function used to load a single glyph within a given glyph slot,  */
2112  /*    for a given size.                                                  */
2113  /*                                                                       */
2114  /* <Input>                                                               */
2115  /*    glyph       :: A handle to a target slot object where the glyph    */
2116  /*                   will be loaded.                                     */
2117  /*                                                                       */
2118  /*    size        :: A handle to the source face size at which the glyph */
2119  /*                   must be scaled/loaded.                              */
2120  /*                                                                       */
2121  /*    glyph_index :: The index of the glyph in the font file.            */
2122  /*                                                                       */
2123  /*    load_flags  :: A flag indicating what to load for this glyph.  The */
2124  /*                   FT_LOAD_XXX constants can be used to control the    */
2125  /*                   glyph loading process (e.g., whether the outline    */
2126  /*                   should be scaled, whether to load bitmaps or not,   */
2127  /*                   whether to hint the outline, etc).                  */
2128  /*                                                                       */
2129  /* <Return>                                                              */
2130  /*    FreeType error code.  0 means success.                             */
2131  /*                                                                       */
2132  FT_LOCAL_DEF( FT_Error )
2133  TT_Load_Glyph( TT_Size       size,
2134                 TT_GlyphSlot  glyph,
2135                 FT_UInt       glyph_index,
2136                 FT_Int32      load_flags )
2137  {
2138    FT_Error      error;
2139    TT_LoaderRec  loader;
2140
2141
2142    error = TT_Err_Ok;
2143
2144#ifdef TT_CONFIG_OPTION_EMBEDDED_BITMAPS
2145
2146    /* try to load embedded bitmap if any              */
2147    /*                                                 */
2148    /* XXX: The convention should be emphasized in     */
2149    /*      the documents because it can be confusing. */
2150    if ( size->strike_index != 0xFFFFFFFFUL      &&
2151         ( load_flags & FT_LOAD_NO_BITMAP ) == 0 )
2152    {
2153      error = load_sbit_image( size, glyph, glyph_index, load_flags );
2154      if ( !error )
2155      {
2156        if ( FT_IS_SCALABLE( glyph->face ) )
2157        {
2158          /* for the bbox we need the header only */
2159          (void)tt_loader_init( &loader, size, glyph, load_flags, TRUE );
2160          (void)load_truetype_glyph( &loader, glyph_index, 0, TRUE );
2161          glyph->linearHoriAdvance = loader.linear;
2162          glyph->linearVertAdvance = loader.top_bearing + loader.bbox.yMax -
2163                                       loader.vadvance;
2164        }
2165
2166        return TT_Err_Ok;
2167      }
2168    }
2169
2170#endif /* TT_CONFIG_OPTION_EMBEDDED_BITMAPS */
2171
2172    /* if FT_LOAD_NO_SCALE is not set, `ttmetrics' must be valid */
2173    if ( !( load_flags & FT_LOAD_NO_SCALE ) && !size->ttmetrics.valid )
2174      return TT_Err_Invalid_Size_Handle;
2175
2176    if ( load_flags & FT_LOAD_SBITS_ONLY )
2177      return TT_Err_Invalid_Argument;
2178
2179    error = tt_loader_init( &loader, size, glyph, load_flags, FALSE );
2180    if ( error )
2181      return error;
2182
2183    glyph->format        = FT_GLYPH_FORMAT_OUTLINE;
2184    glyph->num_subglyphs = 0;
2185    glyph->outline.flags = 0;
2186
2187    /* main loading loop */
2188    error = load_truetype_glyph( &loader, glyph_index, 0, FALSE );
2189    if ( !error )
2190    {
2191      if ( glyph->format == FT_GLYPH_FORMAT_COMPOSITE )
2192      {
2193        glyph->num_subglyphs = loader.gloader->base.num_subglyphs;
2194        glyph->subglyphs     = loader.gloader->base.subglyphs;
2195      }
2196      else
2197      {
2198        glyph->outline        = loader.gloader->base.outline;
2199        glyph->outline.flags &= ~FT_OUTLINE_SINGLE_PASS;
2200
2201        /* Translate array so that (0,0) is the glyph's origin.  Note  */
2202        /* that this behaviour is independent on the value of bit 1 of */
2203        /* the `flags' field in the `head' table -- at least major     */
2204        /* applications like Acroread indicate that.                   */
2205        if ( loader.pp1.x )
2206          FT_Outline_Translate( &glyph->outline, -loader.pp1.x, 0 );
2207      }
2208
2209#ifdef TT_USE_BYTECODE_INTERPRETER
2210
2211      if ( IS_HINTED( load_flags ) )
2212      {
2213        if ( loader.exec->GS.scan_control )
2214        {
2215          /* convert scan conversion mode to FT_OUTLINE_XXX flags */
2216          switch ( loader.exec->GS.scan_type )
2217          {
2218          case 0: /* simple drop-outs including stubs */
2219            glyph->outline.flags |= FT_OUTLINE_INCLUDE_STUBS;
2220            break;
2221          case 1: /* simple drop-outs excluding stubs */
2222            /* nothing; it's the default rendering mode */
2223            break;
2224          case 4: /* smart drop-outs including stubs */
2225            glyph->outline.flags |= FT_OUTLINE_SMART_DROPOUTS |
2226                                    FT_OUTLINE_INCLUDE_STUBS;
2227            break;
2228          case 5: /* smart drop-outs excluding stubs  */
2229            glyph->outline.flags |= FT_OUTLINE_SMART_DROPOUTS;
2230            break;
2231
2232          default: /* no drop-out control */
2233            glyph->outline.flags |= FT_OUTLINE_IGNORE_DROPOUTS;
2234            break;
2235          }
2236        }
2237        else
2238          glyph->outline.flags |= FT_OUTLINE_IGNORE_DROPOUTS;
2239      }
2240
2241#endif /* TT_USE_BYTECODE_INTERPRETER */
2242
2243      compute_glyph_metrics( &loader, glyph_index );
2244    }
2245
2246    /* Set the `high precision' bit flag.                           */
2247    /* This is _critical_ to get correct output for monochrome      */
2248    /* TrueType glyphs at all sizes using the bytecode interpreter. */
2249    /*                                                              */
2250    if ( !( load_flags & FT_LOAD_NO_SCALE ) &&
2251         size->root.metrics.y_ppem < 24     )
2252      glyph->outline.flags |= FT_OUTLINE_HIGH_PRECISION;
2253
2254    return error;
2255  }
2256
2257
2258/* END */
2259