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