ttgload.c revision 64872a50165d842d72c520f5f7e19124dbf7822d
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 <ft2build.h>
20#include FT_INTERNAL_DEBUG_H
21#include FT_INTERNAL_CALC_H
22#include FT_INTERNAL_STREAM_H
23#include FT_INTERNAL_SFNT_H
24#include FT_TRUETYPE_TAGS_H
25#include FT_OUTLINE_H
26#include FT_TRUETYPE_DRIVER_H
27
28#include "ttgload.h"
29#include "ttpload.h"
30
31#ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
32#include "ttgxvar.h"
33#endif
34
35#include "tterrors.h"
36#include "ttsubpix.h"
37
38
39  /*************************************************************************/
40  /*                                                                       */
41  /* The macro FT_COMPONENT is used in trace mode.  It is an implicit      */
42  /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log  */
43  /* messages during execution.                                            */
44  /*                                                                       */
45#undef  FT_COMPONENT
46#define FT_COMPONENT  trace_ttgload
47
48
49  /*************************************************************************/
50  /*                                                                       */
51  /* Composite glyph flags.                                                */
52  /*                                                                       */
53#define ARGS_ARE_WORDS             0x0001
54#define ARGS_ARE_XY_VALUES         0x0002
55#define ROUND_XY_TO_GRID           0x0004
56#define WE_HAVE_A_SCALE            0x0008
57/* reserved                        0x0010 */
58#define MORE_COMPONENTS            0x0020
59#define WE_HAVE_AN_XY_SCALE        0x0040
60#define WE_HAVE_A_2X2              0x0080
61#define WE_HAVE_INSTR              0x0100
62#define USE_MY_METRICS             0x0200
63#define OVERLAP_COMPOUND           0x0400
64#define SCALED_COMPONENT_OFFSET    0x0800
65#define UNSCALED_COMPONENT_OFFSET  0x1000
66
67
68  /*************************************************************************/
69  /*                                                                       */
70  /* Return the horizontal metrics in font units for a given glyph.        */
71  /*                                                                       */
72  FT_LOCAL_DEF( void )
73  TT_Get_HMetrics( TT_Face     face,
74                   FT_UInt     idx,
75                   FT_Short*   lsb,
76                   FT_UShort*  aw )
77  {
78    ( (SFNT_Service)face->sfnt )->get_metrics( face, 0, idx, lsb, aw );
79
80    FT_TRACE5(( "  advance width (font units): %d\n", *aw ));
81    FT_TRACE5(( "  left side bearing (font units): %d\n", *lsb ));
82  }
83
84
85  /*************************************************************************/
86  /*                                                                       */
87  /* Return the vertical metrics in font units for a given glyph.          */
88  /* See macro `TT_LOADER_SET_PP' below for explanations.                  */
89  /*                                                                       */
90  FT_LOCAL_DEF( void )
91  TT_Get_VMetrics( TT_Face     face,
92                   FT_UInt     idx,
93                   FT_Pos      yMax,
94                   FT_Short*   tsb,
95                   FT_UShort*  ah )
96  {
97    if ( face->vertical_info )
98      ( (SFNT_Service)face->sfnt )->get_metrics( face, 1, idx, tsb, ah );
99
100    else if ( face->os2.version != 0xFFFFU )
101    {
102      *tsb = face->os2.sTypoAscender - yMax;
103      *ah  = face->os2.sTypoAscender - face->os2.sTypoDescender;
104    }
105
106    else
107    {
108      *tsb = face->horizontal.Ascender - yMax;
109      *ah  = face->horizontal.Ascender - face->horizontal.Descender;
110    }
111
112    FT_TRACE5(( "  advance height (font units): %d\n", *ah ));
113    FT_TRACE5(( "  top side bearing (font units): %d\n", *tsb ));
114  }
115
116
117  static FT_Error
118  tt_get_metrics( TT_Loader  loader,
119                  FT_UInt    glyph_index )
120  {
121    TT_Face    face   = (TT_Face)loader->face;
122#ifdef TT_CONFIG_OPTION_SUBPIXEL_HINTING
123    TT_Driver  driver = (TT_Driver)FT_FACE_DRIVER( face );
124#endif
125
126    FT_Error   error;
127    FT_Stream  stream = loader->stream;
128
129    FT_Short   left_bearing = 0, top_bearing = 0;
130    FT_UShort  advance_width = 0, advance_height = 0;
131
132    /* we must preserve the stream position          */
133    /* (which gets altered by the metrics functions) */
134    FT_ULong  pos = FT_STREAM_POS();
135
136
137    TT_Get_HMetrics( face, glyph_index,
138                     &left_bearing,
139                     &advance_width );
140    TT_Get_VMetrics( face, glyph_index,
141                     loader->bbox.yMax,
142                     &top_bearing,
143                     &advance_height );
144
145    if ( FT_STREAM_SEEK( pos ) )
146      return error;
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 ( driver->interpreter_version == TT_INTERPRETER_VERSION_38 )
155    {
156      if ( loader->exec )
157        loader->exec->sph_tweak_flags = 0;
158
159      /* this may not be the right place for this, but it works */
160      if ( loader->exec && loader->exec->ignore_x_mode )
161        sph_set_tweaks( loader, glyph_index );
162    }
163#endif /* TT_CONFIG_OPTION_SUBPIXEL_HINTING */
164
165    if ( !loader->linear_def )
166    {
167      loader->linear_def = 1;
168      loader->linear     = advance_width;
169    }
170
171    return FT_Err_Ok;
172  }
173
174
175#ifdef FT_CONFIG_OPTION_INCREMENTAL
176
177  static void
178  tt_get_metrics_incr_overrides( TT_Loader  loader,
179                                 FT_UInt    glyph_index )
180  {
181    TT_Face  face = (TT_Face)loader->face;
182
183    FT_Short   left_bearing = 0, top_bearing = 0;
184    FT_UShort  advance_width = 0, advance_height = 0;
185
186
187    /* If this is an incrementally loaded font check whether there are */
188    /* overriding metrics for this glyph.                              */
189    if ( face->root.internal->incremental_interface                           &&
190         face->root.internal->incremental_interface->funcs->get_glyph_metrics )
191    {
192      FT_Incremental_MetricsRec  metrics;
193      FT_Error                   error;
194
195
196      metrics.bearing_x = loader->left_bearing;
197      metrics.bearing_y = 0;
198      metrics.advance   = loader->advance;
199      metrics.advance_v = 0;
200
201      error = face->root.internal->incremental_interface->funcs->get_glyph_metrics(
202                face->root.internal->incremental_interface->object,
203                glyph_index, FALSE, &metrics );
204      if ( error )
205        goto Exit;
206
207      left_bearing  = (FT_Short)metrics.bearing_x;
208      advance_width = (FT_UShort)metrics.advance;
209
210#if 0
211
212      /* GWW: Do I do the same for vertical metrics? */
213      metrics.bearing_x = 0;
214      metrics.bearing_y = loader->top_bearing;
215      metrics.advance   = loader->vadvance;
216
217      error = face->root.internal->incremental_interface->funcs->get_glyph_metrics(
218                face->root.internal->incremental_interface->object,
219                glyph_index, TRUE, &metrics );
220      if ( error )
221        goto Exit;
222
223      top_bearing    = (FT_Short)metrics.bearing_y;
224      advance_height = (FT_UShort)metrics.advance;
225
226#endif /* 0 */
227
228      loader->left_bearing = left_bearing;
229      loader->advance      = advance_width;
230      loader->top_bearing  = top_bearing;
231      loader->vadvance     = advance_height;
232
233      if ( !loader->linear_def )
234      {
235        loader->linear_def = 1;
236        loader->linear     = advance_width;
237      }
238    }
239
240  Exit:
241    return;
242  }
243
244#endif /* FT_CONFIG_OPTION_INCREMENTAL */
245
246
247  /*************************************************************************/
248  /*                                                                       */
249  /* Translates an array of coordinates.                                   */
250  /*                                                                       */
251  static void
252  translate_array( FT_UInt     n,
253                   FT_Vector*  coords,
254                   FT_Pos      delta_x,
255                   FT_Pos      delta_y )
256  {
257    FT_UInt  k;
258
259
260    if ( delta_x )
261      for ( k = 0; k < n; k++ )
262        coords[k].x += delta_x;
263
264    if ( delta_y )
265      for ( k = 0; k < n; k++ )
266        coords[k].y += delta_y;
267  }
268
269
270  /*************************************************************************/
271  /*                                                                       */
272  /* The following functions are used by default with TrueType fonts.      */
273  /* However, they can be replaced by alternatives if we need to support   */
274  /* TrueType-compressed formats (like MicroType) in the future.           */
275  /*                                                                       */
276  /*************************************************************************/
277
278  FT_CALLBACK_DEF( FT_Error )
279  TT_Access_Glyph_Frame( TT_Loader  loader,
280                         FT_UInt    glyph_index,
281                         FT_ULong   offset,
282                         FT_UInt    byte_count )
283  {
284    FT_Error   error;
285    FT_Stream  stream = loader->stream;
286
287    /* for non-debug mode */
288    FT_UNUSED( glyph_index );
289
290
291    FT_TRACE4(( "Glyph %ld\n", glyph_index ));
292
293    /* the following line sets the `error' variable through macros! */
294    if ( FT_STREAM_SEEK( offset ) || FT_FRAME_ENTER( byte_count ) )
295      return error;
296
297    loader->cursor = stream->cursor;
298    loader->limit  = stream->limit;
299
300    return FT_Err_Ok;
301  }
302
303
304  FT_CALLBACK_DEF( void )
305  TT_Forget_Glyph_Frame( TT_Loader  loader )
306  {
307    FT_Stream  stream = loader->stream;
308
309
310    FT_FRAME_EXIT();
311  }
312
313
314  FT_CALLBACK_DEF( FT_Error )
315  TT_Load_Glyph_Header( TT_Loader  loader )
316  {
317    FT_Byte*  p     = loader->cursor;
318    FT_Byte*  limit = loader->limit;
319
320
321    if ( p + 10 > limit )
322      return FT_THROW( Invalid_Outline );
323
324    loader->n_contours = FT_NEXT_SHORT( p );
325
326    loader->bbox.xMin = FT_NEXT_SHORT( p );
327    loader->bbox.yMin = FT_NEXT_SHORT( p );
328    loader->bbox.xMax = FT_NEXT_SHORT( p );
329    loader->bbox.yMax = FT_NEXT_SHORT( p );
330
331    FT_TRACE5(( "  # of contours: %d\n", loader->n_contours ));
332    FT_TRACE5(( "  xMin: %4d  xMax: %4d\n", loader->bbox.xMin,
333                                            loader->bbox.xMax ));
334    FT_TRACE5(( "  yMin: %4d  yMax: %4d\n", loader->bbox.yMin,
335                                            loader->bbox.yMax ));
336    loader->cursor = p;
337
338    return FT_Err_Ok;
339  }
340
341
342  FT_CALLBACK_DEF( FT_Error )
343  TT_Load_Simple_Glyph( TT_Loader  load )
344  {
345    FT_Error        error;
346    FT_Byte*        p          = load->cursor;
347    FT_Byte*        limit      = load->limit;
348    FT_GlyphLoader  gloader    = load->gloader;
349    FT_Int          n_contours = load->n_contours;
350    FT_Outline*     outline;
351    FT_UShort       n_ins;
352    FT_Int          n_points;
353    FT_ULong        tmp;
354
355    FT_Byte         *flag, *flag_limit;
356    FT_Byte         c, count;
357    FT_Vector       *vec, *vec_limit;
358    FT_Pos          x;
359    FT_Short        *cont, *cont_limit, prev_cont;
360    FT_Int          xy_size = 0;
361
362
363    /* check that we can add the contours to the glyph */
364    error = FT_GLYPHLOADER_CHECK_POINTS( gloader, 0, n_contours );
365    if ( error )
366      goto Fail;
367
368    /* reading the contours' endpoints & number of points */
369    cont       = gloader->current.outline.contours;
370    cont_limit = cont + n_contours;
371
372    /* check space for contours array + instructions count */
373    if ( n_contours >= 0xFFF || p + ( n_contours + 1 ) * 2 > limit )
374      goto Invalid_Outline;
375
376    prev_cont = FT_NEXT_SHORT( p );
377
378    if ( n_contours > 0 )
379      cont[0] = prev_cont;
380
381    if ( prev_cont < 0 )
382      goto Invalid_Outline;
383
384    for ( cont++; cont < cont_limit; cont++ )
385    {
386      cont[0] = FT_NEXT_SHORT( p );
387      if ( cont[0] <= prev_cont )
388      {
389        /* unordered contours: this is invalid */
390        goto Invalid_Outline;
391      }
392      prev_cont = cont[0];
393    }
394
395    n_points = 0;
396    if ( n_contours > 0 )
397    {
398      n_points = cont[-1] + 1;
399      if ( n_points < 0 )
400        goto Invalid_Outline;
401    }
402
403    /* note that we will add four phantom points later */
404    error = FT_GLYPHLOADER_CHECK_POINTS( gloader, n_points + 4, 0 );
405    if ( error )
406      goto Fail;
407
408    /* reading the bytecode instructions */
409    load->glyph->control_len  = 0;
410    load->glyph->control_data = 0;
411
412    if ( p + 2 > limit )
413      goto Invalid_Outline;
414
415    n_ins = FT_NEXT_USHORT( p );
416
417    FT_TRACE5(( "  Instructions size: %u\n", n_ins ));
418
419    /* check it */
420    if ( ( limit - p ) < n_ins )
421    {
422      FT_TRACE0(( "TT_Load_Simple_Glyph: instruction count mismatch\n" ));
423      error = FT_THROW( Too_Many_Hints );
424      goto Fail;
425    }
426
427#ifdef TT_USE_BYTECODE_INTERPRETER
428
429    if ( IS_HINTED( load->load_flags ) )
430    {
431      /* we don't trust `maxSizeOfInstructions' in the `maxp' table */
432      /* and thus update the bytecode array size by ourselves       */
433
434      tmp   = load->exec->glyphSize;
435      error = Update_Max( load->exec->memory,
436                          &tmp,
437                          sizeof ( FT_Byte ),
438                          (void*)&load->exec->glyphIns,
439                          n_ins );
440
441      load->exec->glyphSize = (FT_UShort)tmp;
442      if ( error )
443        return error;
444
445      load->glyph->control_len  = n_ins;
446      load->glyph->control_data = load->exec->glyphIns;
447
448      FT_MEM_COPY( load->exec->glyphIns, p, (FT_Long)n_ins );
449    }
450
451#endif /* TT_USE_BYTECODE_INTERPRETER */
452
453    p += n_ins;
454
455    outline = &gloader->current.outline;
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    if ( IS_HINTED( loader->load_flags ) )
1006    {
1007      loader->zone.n_points += 4;
1008
1009      error = TT_Hint_Glyph( loader, 0 );
1010    }
1011
1012    return error;
1013  }
1014
1015
1016  /*************************************************************************/
1017  /*                                                                       */
1018  /* <Function>                                                            */
1019  /*    TT_Process_Composite_Component                                     */
1020  /*                                                                       */
1021  /* <Description>                                                         */
1022  /*    Once a composite component has been loaded, it needs to be         */
1023  /*    processed.  Usually, this means transforming and translating.      */
1024  /*                                                                       */
1025  static FT_Error
1026  TT_Process_Composite_Component( TT_Loader    loader,
1027                                  FT_SubGlyph  subglyph,
1028                                  FT_UInt      start_point,
1029                                  FT_UInt      num_base_points )
1030  {
1031    FT_GlyphLoader  gloader    = loader->gloader;
1032    FT_Vector*      base_vec   = gloader->base.outline.points;
1033    FT_UInt         num_points = gloader->base.outline.n_points;
1034    FT_Bool         have_scale;
1035    FT_Pos          x, y;
1036
1037
1038    have_scale = FT_BOOL( subglyph->flags & ( WE_HAVE_A_SCALE     |
1039                                              WE_HAVE_AN_XY_SCALE |
1040                                              WE_HAVE_A_2X2       ) );
1041
1042    /* perform the transform required for this subglyph */
1043    if ( have_scale )
1044    {
1045      FT_UInt  i;
1046
1047
1048      for ( i = num_base_points; i < num_points; i++ )
1049        FT_Vector_Transform( base_vec + i, &subglyph->transform );
1050    }
1051
1052    /* get offset */
1053    if ( !( subglyph->flags & ARGS_ARE_XY_VALUES ) )
1054    {
1055      FT_UInt     k = subglyph->arg1;
1056      FT_UInt     l = subglyph->arg2;
1057      FT_Vector*  p1;
1058      FT_Vector*  p2;
1059
1060
1061      /* match l-th point of the newly loaded component to the k-th point */
1062      /* of the previously loaded components.                             */
1063
1064      /* change to the point numbers used by our outline */
1065      k += start_point;
1066      l += num_base_points;
1067      if ( k >= num_base_points ||
1068           l >= num_points      )
1069        return FT_THROW( Invalid_Composite );
1070
1071      p1 = gloader->base.outline.points + k;
1072      p2 = gloader->base.outline.points + l;
1073
1074      x = p1->x - p2->x;
1075      y = p1->y - p2->y;
1076    }
1077    else
1078    {
1079      x = subglyph->arg1;
1080      y = subglyph->arg2;
1081
1082      if ( !x && !y )
1083        return FT_Err_Ok;
1084
1085  /* Use a default value dependent on                                     */
1086  /* TT_CONFIG_OPTION_COMPONENT_OFFSET_SCALED.  This is useful for old TT */
1087  /* fonts which don't set the xxx_COMPONENT_OFFSET bit.                  */
1088
1089      if ( have_scale &&
1090#ifdef TT_CONFIG_OPTION_COMPONENT_OFFSET_SCALED
1091           !( subglyph->flags & UNSCALED_COMPONENT_OFFSET ) )
1092#else
1093            ( subglyph->flags & SCALED_COMPONENT_OFFSET ) )
1094#endif
1095      {
1096
1097#if 0
1098
1099  /*************************************************************************/
1100  /*                                                                       */
1101  /* This algorithm is what Apple documents.  But it doesn't work.         */
1102  /*                                                                       */
1103        int  a = subglyph->transform.xx > 0 ?  subglyph->transform.xx
1104                                            : -subglyph->transform.xx;
1105        int  b = subglyph->transform.yx > 0 ?  subglyph->transform.yx
1106                                            : -subglyph->transform.yx;
1107        int  c = subglyph->transform.xy > 0 ?  subglyph->transform.xy
1108                                            : -subglyph->transform.xy;
1109        int  d = subglyph->transform.yy > 0 ? subglyph->transform.yy
1110                                            : -subglyph->transform.yy;
1111        int  m = a > b ? a : b;
1112        int  n = c > d ? c : d;
1113
1114
1115        if ( a - b <= 33 && a - b >= -33 )
1116          m *= 2;
1117        if ( c - d <= 33 && c - d >= -33 )
1118          n *= 2;
1119        x = FT_MulFix( x, m );
1120        y = FT_MulFix( y, n );
1121
1122#else /* 0 */
1123
1124  /*************************************************************************/
1125  /*                                                                       */
1126  /* This algorithm is a guess and works much better than the above.       */
1127  /*                                                                       */
1128        FT_Fixed  mac_xscale = FT_Hypot( subglyph->transform.xx,
1129                                         subglyph->transform.xy );
1130        FT_Fixed  mac_yscale = FT_Hypot( subglyph->transform.yy,
1131                                         subglyph->transform.yx );
1132
1133
1134        x = FT_MulFix( x, mac_xscale );
1135        y = FT_MulFix( y, mac_yscale );
1136
1137#endif /* 0 */
1138
1139      }
1140
1141      if ( !( loader->load_flags & FT_LOAD_NO_SCALE ) )
1142      {
1143        FT_Fixed  x_scale = ((TT_Size)loader->size)->metrics.x_scale;
1144        FT_Fixed  y_scale = ((TT_Size)loader->size)->metrics.y_scale;
1145
1146
1147        x = FT_MulFix( x, x_scale );
1148        y = FT_MulFix( y, y_scale );
1149
1150        if ( subglyph->flags & ROUND_XY_TO_GRID )
1151        {
1152          x = FT_PIX_ROUND( x );
1153          y = FT_PIX_ROUND( y );
1154        }
1155      }
1156    }
1157
1158    if ( x || y )
1159      translate_array( num_points - num_base_points,
1160                       base_vec + num_base_points,
1161                       x, y );
1162
1163    return FT_Err_Ok;
1164  }
1165
1166
1167  /*************************************************************************/
1168  /*                                                                       */
1169  /* <Function>                                                            */
1170  /*    TT_Process_Composite_Glyph                                         */
1171  /*                                                                       */
1172  /* <Description>                                                         */
1173  /*    This is slightly different from TT_Process_Simple_Glyph, in that   */
1174  /*    its sole purpose is to hint the glyph.  Thus this function is      */
1175  /*    only available when bytecode interpreter is enabled.               */
1176  /*                                                                       */
1177  static FT_Error
1178  TT_Process_Composite_Glyph( TT_Loader  loader,
1179                              FT_UInt    start_point,
1180                              FT_UInt    start_contour )
1181  {
1182    FT_Error     error;
1183    FT_Outline*  outline;
1184    FT_UInt      i;
1185
1186
1187    outline = &loader->gloader->base.outline;
1188
1189    /* make room for phantom points */
1190    error = FT_GLYPHLOADER_CHECK_POINTS( loader->gloader,
1191                                         outline->n_points + 4,
1192                                         0 );
1193    if ( error )
1194      return error;
1195
1196    outline->points[outline->n_points    ] = loader->pp1;
1197    outline->points[outline->n_points + 1] = loader->pp2;
1198    outline->points[outline->n_points + 2] = loader->pp3;
1199    outline->points[outline->n_points + 3] = loader->pp4;
1200
1201    outline->tags[outline->n_points    ] = 0;
1202    outline->tags[outline->n_points + 1] = 0;
1203    outline->tags[outline->n_points + 2] = 0;
1204    outline->tags[outline->n_points + 3] = 0;
1205
1206#ifdef TT_USE_BYTECODE_INTERPRETER
1207
1208    {
1209      FT_Stream  stream = loader->stream;
1210      FT_UShort  n_ins, max_ins;
1211      FT_ULong   tmp;
1212
1213
1214      /* TT_Load_Composite_Glyph only gives us the offset of instructions */
1215      /* so we read them here                                             */
1216      if ( FT_STREAM_SEEK( loader->ins_pos ) ||
1217           FT_READ_USHORT( n_ins )           )
1218        return error;
1219
1220      FT_TRACE5(( "  Instructions size = %d\n", n_ins ));
1221
1222      /* check it */
1223      max_ins = ((TT_Face)loader->face)->max_profile.maxSizeOfInstructions;
1224      if ( n_ins > max_ins )
1225      {
1226        /* don't trust `maxSizeOfInstructions'; */
1227        /* only do a rough safety check         */
1228        if ( (FT_Int)n_ins > loader->byte_len )
1229        {
1230          FT_TRACE1(( "TT_Process_Composite_Glyph:"
1231                      " too many instructions (%d) for glyph with length %d\n",
1232                      n_ins, loader->byte_len ));
1233          return FT_THROW( Too_Many_Hints );
1234        }
1235
1236        tmp   = loader->exec->glyphSize;
1237        error = Update_Max( loader->exec->memory,
1238                            &tmp,
1239                            sizeof ( FT_Byte ),
1240                            (void*)&loader->exec->glyphIns,
1241                            n_ins );
1242
1243        loader->exec->glyphSize = (FT_UShort)tmp;
1244        if ( error )
1245          return error;
1246      }
1247      else if ( n_ins == 0 )
1248        return FT_Err_Ok;
1249
1250      if ( FT_STREAM_READ( loader->exec->glyphIns, n_ins ) )
1251        return error;
1252
1253      loader->glyph->control_data = loader->exec->glyphIns;
1254      loader->glyph->control_len  = n_ins;
1255    }
1256
1257#endif
1258
1259    tt_prepare_zone( &loader->zone, &loader->gloader->base,
1260                     start_point, start_contour );
1261
1262    /* Some points are likely touched during execution of  */
1263    /* instructions on components.  So let's untouch them. */
1264    for ( i = 0; i < loader->zone.n_points; i++ )
1265      loader->zone.tags[i] &= ~FT_CURVE_TAG_TOUCH_BOTH;
1266
1267    loader->zone.n_points += 4;
1268
1269    return TT_Hint_Glyph( loader, 1 );
1270  }
1271
1272
1273  /*
1274   * Calculate the phantom points
1275   *
1276   * Defining the right side bearing (rsb) as
1277   *
1278   *   rsb = aw - (lsb + xmax - xmin)
1279   *
1280   * (with `aw' the advance width, `lsb' the left side bearing, and `xmin'
1281   * and `xmax' the glyph's minimum and maximum x value), the OpenType
1282   * specification defines the initial position of horizontal phantom points
1283   * as
1284   *
1285   *   pp1 = (xmin - lsb, 0)      ,
1286   *   pp2 = (pp1 + aw, 0)        .
1287   *
1288   * However, the specification lacks the precise definition of vertical
1289   * phantom points.  Greg Hitchcock provided the following explanation.
1290   *
1291   * - a `vmtx' table is present
1292   *
1293   *   For any glyph, the minimum and maximum y values (`ymin' and `ymax')
1294   *   are given in the `glyf' table, the top side bearing (tsb) and advance
1295   *   height (ah) are given in the `vmtx' table.  The bottom side bearing
1296   *   (bsb) is then calculated as
1297   *
1298   *     bsb = ah - (tsb + ymax - ymin)       ,
1299   *
1300   *   and the initial position of vertical phantom points is
1301   *
1302   *     pp3 = (x, ymax + tsb)       ,
1303   *     pp4 = (x, pp3 - ah)         .
1304   *
1305   *   See below for value `x'.
1306   *
1307   * - no `vmtx' table in the font
1308   *
1309   *   If there is an `OS/2' table, we set
1310   *
1311   *     DefaultAscender = sTypoAscender       ,
1312   *     DefaultDescender = sTypoDescender     ,
1313   *
1314   *   otherwise we use data from the `hhea' table:
1315   *
1316   *     DefaultAscender = Ascender         ,
1317   *     DefaultDescender = Descender       .
1318   *
1319   *   With these two variables we can now set
1320   *
1321   *     ah = DefaultAscender - sDefaultDescender    ,
1322   *     tsb = DefaultAscender - yMax                ,
1323   *
1324   *   and proceed as if a `vmtx' table was present.
1325   *
1326   * Usually we have
1327   *
1328   *   x = aw / 2      ,
1329   *
1330   * but there is a compatibility case where it can be set to
1331   *
1332   *   x = -DefaultDescender -
1333   *         ((DefaultAscender - DefaultDescender - aw) / 2)     .
1334   *
1335   */
1336#define TT_LOADER_SET_PP( loader )                                          \
1337          do {                                                              \
1338            (loader)->pp1.x = (loader)->bbox.xMin - (loader)->left_bearing; \
1339            (loader)->pp1.y = 0;                                            \
1340            (loader)->pp2.x = (loader)->pp1.x + (loader)->advance;          \
1341            (loader)->pp2.y = 0;                                            \
1342            (loader)->pp3.x = (loader)->advance / 2;                        \
1343            (loader)->pp3.y = (loader)->bbox.yMax + (loader)->top_bearing;  \
1344            (loader)->pp4.x = (loader)->advance / 2;                        \
1345            (loader)->pp4.y = (loader)->pp3.y - (loader)->vadvance;         \
1346          } while ( 0 )
1347
1348
1349  /*************************************************************************/
1350  /*                                                                       */
1351  /* <Function>                                                            */
1352  /*    load_truetype_glyph                                                */
1353  /*                                                                       */
1354  /* <Description>                                                         */
1355  /*    Loads a given truetype glyph.  Handles composites and uses a       */
1356  /*    TT_Loader object.                                                  */
1357  /*                                                                       */
1358  static FT_Error
1359  load_truetype_glyph( TT_Loader  loader,
1360                       FT_UInt    glyph_index,
1361                       FT_UInt    recurse_count,
1362                       FT_Bool    header_only )
1363  {
1364    FT_Error        error        = FT_Err_Ok;
1365    FT_Fixed        x_scale, y_scale;
1366    FT_ULong        offset;
1367    TT_Face         face         = (TT_Face)loader->face;
1368    FT_GlyphLoader  gloader      = loader->gloader;
1369    FT_Bool         opened_frame = 0;
1370
1371#ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
1372    FT_Vector*      deltas       = NULL;
1373#endif
1374
1375#ifdef FT_CONFIG_OPTION_INCREMENTAL
1376    FT_StreamRec    inc_stream;
1377    FT_Data         glyph_data;
1378    FT_Bool         glyph_data_loaded = 0;
1379#endif
1380
1381
1382    /* some fonts have an incorrect value of `maxComponentDepth', */
1383    /* thus we allow depth 1 to catch the majority of them        */
1384    if ( recurse_count > 1                                   &&
1385         recurse_count > face->max_profile.maxComponentDepth )
1386    {
1387      error = FT_THROW( Invalid_Composite );
1388      goto Exit;
1389    }
1390
1391    /* check glyph index */
1392    if ( glyph_index >= (FT_UInt)face->root.num_glyphs )
1393    {
1394      error = FT_THROW( Invalid_Glyph_Index );
1395      goto Exit;
1396    }
1397
1398    loader->glyph_index = glyph_index;
1399
1400    if ( ( loader->load_flags & FT_LOAD_NO_SCALE ) == 0 )
1401    {
1402      x_scale = ((TT_Size)loader->size)->metrics.x_scale;
1403      y_scale = ((TT_Size)loader->size)->metrics.y_scale;
1404    }
1405    else
1406    {
1407      x_scale = 0x10000L;
1408      y_scale = 0x10000L;
1409    }
1410
1411    /* Set `offset' to the start of the glyph relative to the start of */
1412    /* the `glyf' table, and `byte_len' to the length of the glyph in  */
1413    /* bytes.                                                          */
1414
1415#ifdef FT_CONFIG_OPTION_INCREMENTAL
1416
1417    /* If we are loading glyph data via the incremental interface, set */
1418    /* the loader stream to a memory stream reading the data returned  */
1419    /* by the interface.                                               */
1420    if ( face->root.internal->incremental_interface )
1421    {
1422      error = face->root.internal->incremental_interface->funcs->get_glyph_data(
1423                face->root.internal->incremental_interface->object,
1424                glyph_index, &glyph_data );
1425      if ( error )
1426        goto Exit;
1427
1428      glyph_data_loaded = 1;
1429      offset            = 0;
1430      loader->byte_len  = glyph_data.length;
1431
1432      FT_MEM_ZERO( &inc_stream, sizeof ( inc_stream ) );
1433      FT_Stream_OpenMemory( &inc_stream,
1434                            glyph_data.pointer, glyph_data.length );
1435
1436      loader->stream = &inc_stream;
1437    }
1438    else
1439
1440#endif /* FT_CONFIG_OPTION_INCREMENTAL */
1441
1442      offset = tt_face_get_location( face, glyph_index,
1443                                     (FT_UInt*)&loader->byte_len );
1444
1445    if ( loader->byte_len > 0 )
1446    {
1447#ifdef FT_CONFIG_OPTION_INCREMENTAL
1448      /* for the incremental interface, `glyf_offset' is always zero */
1449      if ( !loader->glyf_offset                        &&
1450           !face->root.internal->incremental_interface )
1451#else
1452      if ( !loader->glyf_offset )
1453#endif /* FT_CONFIG_OPTION_INCREMENTAL */
1454      {
1455        FT_TRACE2(( "no `glyf' table but non-zero `loca' entry\n" ));
1456        error = FT_THROW( Invalid_Table );
1457        goto Exit;
1458      }
1459
1460      error = face->access_glyph_frame( loader, glyph_index,
1461                                        loader->glyf_offset + offset,
1462                                        loader->byte_len );
1463      if ( error )
1464        goto Exit;
1465
1466      opened_frame = 1;
1467
1468      /* read glyph header first */
1469      error = face->read_glyph_header( loader );
1470      if ( error )
1471        goto Exit;
1472
1473      /* the metrics must be computed after loading the glyph header */
1474      /* since we need the glyph's `yMax' value in case the vertical */
1475      /* metrics must be emulated                                    */
1476      error = tt_get_metrics( loader, glyph_index );
1477      if ( error )
1478        goto Exit;
1479
1480      if ( header_only )
1481        goto Exit;
1482    }
1483
1484    if ( loader->byte_len == 0 || loader->n_contours == 0 )
1485    {
1486      loader->bbox.xMin = 0;
1487      loader->bbox.xMax = 0;
1488      loader->bbox.yMin = 0;
1489      loader->bbox.yMax = 0;
1490
1491      error = tt_get_metrics( loader, glyph_index );
1492      if ( error )
1493        goto Exit;
1494
1495      if ( header_only )
1496        goto Exit;
1497
1498      /* must initialize points before (possibly) overriding */
1499      /* glyph metrics from the incremental interface        */
1500      TT_LOADER_SET_PP( loader );
1501
1502#ifdef FT_CONFIG_OPTION_INCREMENTAL
1503      tt_get_metrics_incr_overrides( loader, glyph_index );
1504#endif
1505
1506#ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
1507
1508      if ( ((TT_Face)(loader->face))->doblend )
1509      {
1510        /* this must be done before scaling */
1511        FT_Memory  memory = loader->face->memory;
1512
1513
1514        error = TT_Vary_Get_Glyph_Deltas( (TT_Face)(loader->face),
1515                                          glyph_index, &deltas, 4 );
1516        if ( error )
1517          goto Exit;
1518
1519        loader->pp1.x += deltas[0].x; loader->pp1.y += deltas[0].y;
1520        loader->pp2.x += deltas[1].x; loader->pp2.y += deltas[1].y;
1521        loader->pp3.x += deltas[2].x; loader->pp3.y += deltas[2].y;
1522        loader->pp4.x += deltas[3].x; loader->pp4.y += deltas[3].y;
1523
1524        FT_FREE( deltas );
1525      }
1526
1527#endif
1528
1529      if ( ( loader->load_flags & FT_LOAD_NO_SCALE ) == 0 )
1530      {
1531        loader->pp1.x = FT_MulFix( loader->pp1.x, x_scale );
1532        loader->pp2.x = FT_MulFix( loader->pp2.x, x_scale );
1533        loader->pp3.y = FT_MulFix( loader->pp3.y, y_scale );
1534        loader->pp4.y = FT_MulFix( loader->pp4.y, y_scale );
1535      }
1536
1537      error = FT_Err_Ok;
1538      goto Exit;
1539    }
1540
1541    /* must initialize points before (possibly) overriding */
1542    /* glyph metrics from the incremental interface        */
1543    TT_LOADER_SET_PP( loader );
1544
1545#ifdef FT_CONFIG_OPTION_INCREMENTAL
1546    tt_get_metrics_incr_overrides( loader, glyph_index );
1547#endif
1548
1549    /***********************************************************************/
1550    /***********************************************************************/
1551    /***********************************************************************/
1552
1553    /* if it is a simple glyph, load it */
1554
1555    if ( loader->n_contours > 0 )
1556    {
1557      error = face->read_simple_glyph( loader );
1558      if ( error )
1559        goto Exit;
1560
1561      /* all data have been read */
1562      face->forget_glyph_frame( loader );
1563      opened_frame = 0;
1564
1565      error = TT_Process_Simple_Glyph( loader );
1566      if ( error )
1567        goto Exit;
1568
1569      FT_GlyphLoader_Add( gloader );
1570    }
1571
1572    /***********************************************************************/
1573    /***********************************************************************/
1574    /***********************************************************************/
1575
1576    /* otherwise, load a composite! */
1577    else if ( loader->n_contours == -1 )
1578    {
1579      FT_UInt   start_point;
1580      FT_UInt   start_contour;
1581      FT_ULong  ins_pos;  /* position of composite instructions, if any */
1582
1583
1584      start_point   = gloader->base.outline.n_points;
1585      start_contour = gloader->base.outline.n_contours;
1586
1587      /* for each subglyph, read composite header */
1588      error = face->read_composite_glyph( loader );
1589      if ( error )
1590        goto Exit;
1591
1592      /* store the offset of instructions */
1593      ins_pos = loader->ins_pos;
1594
1595      /* all data we need are read */
1596      face->forget_glyph_frame( loader );
1597      opened_frame = 0;
1598
1599#ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
1600
1601      if ( face->doblend )
1602      {
1603        FT_Int       i, limit;
1604        FT_SubGlyph  subglyph;
1605        FT_Memory    memory = face->root.memory;
1606
1607
1608        /* this provides additional offsets */
1609        /* for each component's translation */
1610
1611        if ( ( error = TT_Vary_Get_Glyph_Deltas(
1612                         face,
1613                         glyph_index,
1614                         &deltas,
1615                         gloader->current.num_subglyphs + 4 )) != 0 )
1616          goto Exit;
1617
1618        subglyph = gloader->current.subglyphs + gloader->base.num_subglyphs;
1619        limit    = gloader->current.num_subglyphs;
1620
1621        for ( i = 0; i < limit; ++i, ++subglyph )
1622        {
1623          if ( subglyph->flags & ARGS_ARE_XY_VALUES )
1624          {
1625            /* XXX: overflow check for subglyph->{arg1,arg2}.   */
1626            /* deltas[i].{x,y} must be within signed 16-bit,    */
1627            /* but the restriction of summed delta is not clear */
1628            subglyph->arg1 += (FT_Int16)deltas[i].x;
1629            subglyph->arg2 += (FT_Int16)deltas[i].y;
1630          }
1631        }
1632
1633        loader->pp1.x += deltas[i + 0].x; loader->pp1.y += deltas[i + 0].y;
1634        loader->pp2.x += deltas[i + 1].x; loader->pp2.y += deltas[i + 1].y;
1635        loader->pp3.x += deltas[i + 2].x; loader->pp3.y += deltas[i + 2].y;
1636        loader->pp4.x += deltas[i + 3].x; loader->pp4.y += deltas[i + 3].y;
1637
1638        FT_FREE( deltas );
1639      }
1640
1641#endif /* TT_CONFIG_OPTION_GX_VAR_SUPPORT */
1642
1643      if ( ( loader->load_flags & FT_LOAD_NO_SCALE ) == 0 )
1644      {
1645        loader->pp1.x = FT_MulFix( loader->pp1.x, x_scale );
1646        loader->pp2.x = FT_MulFix( loader->pp2.x, x_scale );
1647        loader->pp3.y = FT_MulFix( loader->pp3.y, y_scale );
1648        loader->pp4.y = FT_MulFix( loader->pp4.y, y_scale );
1649      }
1650
1651      /* if the flag FT_LOAD_NO_RECURSE is set, we return the subglyph */
1652      /* `as is' in the glyph slot (the client application will be     */
1653      /* responsible for interpreting these data)...                   */
1654      if ( loader->load_flags & FT_LOAD_NO_RECURSE )
1655      {
1656        FT_GlyphLoader_Add( gloader );
1657        loader->glyph->format = FT_GLYPH_FORMAT_COMPOSITE;
1658
1659        goto Exit;
1660      }
1661
1662      /*********************************************************************/
1663      /*********************************************************************/
1664      /*********************************************************************/
1665
1666      {
1667        FT_UInt      n, num_base_points;
1668        FT_SubGlyph  subglyph       = 0;
1669
1670        FT_UInt      num_points     = start_point;
1671        FT_UInt      num_subglyphs  = gloader->current.num_subglyphs;
1672        FT_UInt      num_base_subgs = gloader->base.num_subglyphs;
1673
1674        FT_Stream    old_stream     = loader->stream;
1675        FT_Int       old_byte_len   = loader->byte_len;
1676
1677
1678        FT_GlyphLoader_Add( gloader );
1679
1680        /* read each subglyph independently */
1681        for ( n = 0; n < num_subglyphs; n++ )
1682        {
1683          FT_Vector  pp[4];
1684
1685
1686          /* Each time we call load_truetype_glyph in this loop, the   */
1687          /* value of `gloader.base.subglyphs' can change due to table */
1688          /* reallocations.  We thus need to recompute the subglyph    */
1689          /* pointer on each iteration.                                */
1690          subglyph = gloader->base.subglyphs + num_base_subgs + n;
1691
1692          pp[0] = loader->pp1;
1693          pp[1] = loader->pp2;
1694          pp[2] = loader->pp3;
1695          pp[3] = loader->pp4;
1696
1697          num_base_points = gloader->base.outline.n_points;
1698
1699          error = load_truetype_glyph( loader, subglyph->index,
1700                                       recurse_count + 1, FALSE );
1701          if ( error )
1702            goto Exit;
1703
1704          /* restore subglyph pointer */
1705          subglyph = gloader->base.subglyphs + num_base_subgs + n;
1706
1707          if ( !( subglyph->flags & USE_MY_METRICS ) )
1708          {
1709            loader->pp1 = pp[0];
1710            loader->pp2 = pp[1];
1711            loader->pp3 = pp[2];
1712            loader->pp4 = pp[3];
1713          }
1714
1715          num_points = gloader->base.outline.n_points;
1716
1717          if ( num_points == num_base_points )
1718            continue;
1719
1720          /* gloader->base.outline consists of three parts:               */
1721          /* 0 -(1)-> start_point -(2)-> num_base_points -(3)-> n_points. */
1722          /*                                                              */
1723          /* (1): exists from the beginning                               */
1724          /* (2): components that have been loaded so far                 */
1725          /* (3): the newly loaded component                              */
1726          TT_Process_Composite_Component( loader, subglyph, start_point,
1727                                          num_base_points );
1728        }
1729
1730        loader->stream   = old_stream;
1731        loader->byte_len = old_byte_len;
1732
1733        /* process the glyph */
1734        loader->ins_pos = ins_pos;
1735        if ( IS_HINTED( loader->load_flags ) &&
1736
1737#ifdef TT_USE_BYTECODE_INTERPRETER
1738
1739             subglyph->flags & WE_HAVE_INSTR &&
1740
1741#endif
1742
1743             num_points > start_point )
1744          TT_Process_Composite_Glyph( loader, start_point, start_contour );
1745
1746      }
1747    }
1748    else
1749    {
1750      /* invalid composite count (negative but not -1) */
1751      error = FT_THROW( Invalid_Outline );
1752      goto Exit;
1753    }
1754
1755    /***********************************************************************/
1756    /***********************************************************************/
1757    /***********************************************************************/
1758
1759  Exit:
1760
1761    if ( opened_frame )
1762      face->forget_glyph_frame( loader );
1763
1764#ifdef FT_CONFIG_OPTION_INCREMENTAL
1765
1766    if ( glyph_data_loaded )
1767      face->root.internal->incremental_interface->funcs->free_glyph_data(
1768        face->root.internal->incremental_interface->object,
1769        &glyph_data );
1770
1771#endif
1772
1773    return error;
1774  }
1775
1776
1777  static FT_Error
1778  compute_glyph_metrics( TT_Loader  loader,
1779                         FT_UInt    glyph_index )
1780  {
1781    TT_Face    face   = (TT_Face)loader->face;
1782#ifdef TT_CONFIG_OPTION_SUBPIXEL_HINTING
1783    TT_Driver  driver = (TT_Driver)FT_FACE_DRIVER( face );
1784#endif
1785
1786    FT_BBox       bbox;
1787    FT_Fixed      y_scale;
1788    TT_GlyphSlot  glyph = loader->glyph;
1789    TT_Size       size  = (TT_Size)loader->size;
1790
1791
1792    y_scale = 0x10000L;
1793    if ( ( loader->load_flags & FT_LOAD_NO_SCALE ) == 0 )
1794      y_scale = size->root.metrics.y_scale;
1795
1796    if ( glyph->format != FT_GLYPH_FORMAT_COMPOSITE )
1797      FT_Outline_Get_CBox( &glyph->outline, &bbox );
1798    else
1799      bbox = loader->bbox;
1800
1801    /* get the device-independent horizontal advance; it is scaled later */
1802    /* by the base layer.                                                */
1803    glyph->linearHoriAdvance = loader->linear;
1804
1805    glyph->metrics.horiBearingX = bbox.xMin;
1806    glyph->metrics.horiBearingY = bbox.yMax;
1807    glyph->metrics.horiAdvance  = loader->pp2.x - loader->pp1.x;
1808
1809    /* adjust advance width to the value contained in the hdmx table */
1810    if ( !face->postscript.isFixedPitch  &&
1811         IS_HINTED( loader->load_flags ) )
1812    {
1813      FT_Byte*  widthp;
1814
1815
1816      widthp = tt_face_get_device_metrics( face,
1817                                           size->root.metrics.x_ppem,
1818                                           glyph_index );
1819
1820#ifdef TT_CONFIG_OPTION_SUBPIXEL_HINTING
1821
1822      if ( driver->interpreter_version == TT_INTERPRETER_VERSION_38 )
1823      {
1824        FT_Bool  ignore_x_mode;
1825
1826
1827        ignore_x_mode = FT_BOOL( FT_LOAD_TARGET_MODE( loader->load_flags ) !=
1828                                 FT_RENDER_MODE_MONO );
1829
1830        if ( widthp                                                   &&
1831             ( ( ignore_x_mode && loader->exec->compatible_widths ) ||
1832                !ignore_x_mode                                      ||
1833                SPH_OPTION_BITMAP_WIDTHS                            ) )
1834          glyph->metrics.horiAdvance = *widthp << 6;
1835      }
1836      else
1837
1838#endif /* TT_CONFIG_OPTION_SUBPIXEL_HINTING */
1839
1840      {
1841        if ( widthp )
1842          glyph->metrics.horiAdvance = *widthp << 6;
1843      }
1844    }
1845
1846    /* set glyph dimensions */
1847    glyph->metrics.width  = bbox.xMax - bbox.xMin;
1848    glyph->metrics.height = bbox.yMax - bbox.yMin;
1849
1850    /* Now take care of vertical metrics.  In the case where there is */
1851    /* no vertical information within the font (relatively common),   */
1852    /* create some metrics manually                                   */
1853    {
1854      FT_Pos  top;      /* scaled vertical top side bearing  */
1855      FT_Pos  advance;  /* scaled vertical advance height    */
1856
1857
1858      /* Get the unscaled top bearing and advance height. */
1859      if ( face->vertical_info                   &&
1860           face->vertical.number_Of_VMetrics > 0 )
1861      {
1862        top = (FT_Short)FT_DivFix( loader->pp3.y - bbox.yMax,
1863                                   y_scale );
1864
1865        if ( loader->pp3.y <= loader->pp4.y )
1866          advance = 0;
1867        else
1868          advance = (FT_UShort)FT_DivFix( loader->pp3.y - loader->pp4.y,
1869                                          y_scale );
1870      }
1871      else
1872      {
1873        FT_Pos  height;
1874
1875
1876        /* XXX Compute top side bearing and advance height in  */
1877        /*     Get_VMetrics instead of here.                   */
1878
1879        /* NOTE: The OS/2 values are the only `portable' ones, */
1880        /*       which is why we use them, if there is an OS/2 */
1881        /*       table in the font.  Otherwise, we use the     */
1882        /*       values defined in the horizontal header.      */
1883
1884        height = (FT_Short)FT_DivFix( bbox.yMax - bbox.yMin,
1885                                      y_scale );
1886        if ( face->os2.version != 0xFFFFU )
1887          advance = (FT_Pos)( face->os2.sTypoAscender -
1888                              face->os2.sTypoDescender );
1889        else
1890          advance = (FT_Pos)( face->horizontal.Ascender -
1891                              face->horizontal.Descender );
1892
1893        top = ( advance - height ) / 2;
1894      }
1895
1896#ifdef FT_CONFIG_OPTION_INCREMENTAL
1897      {
1898        FT_Incremental_InterfaceRec*  incr;
1899        FT_Incremental_MetricsRec     metrics;
1900        FT_Error                      error;
1901
1902
1903        incr = face->root.internal->incremental_interface;
1904
1905        /* If this is an incrementally loaded font see if there are */
1906        /* overriding metrics for this glyph.                       */
1907        if ( incr && incr->funcs->get_glyph_metrics )
1908        {
1909          metrics.bearing_x = 0;
1910          metrics.bearing_y = top;
1911          metrics.advance   = advance;
1912
1913          error = incr->funcs->get_glyph_metrics( incr->object,
1914                                                  glyph_index,
1915                                                  TRUE,
1916                                                  &metrics );
1917          if ( error )
1918            return error;
1919
1920          top     = metrics.bearing_y;
1921          advance = metrics.advance;
1922        }
1923      }
1924
1925      /* GWW: Do vertical metrics get loaded incrementally too? */
1926
1927#endif /* FT_CONFIG_OPTION_INCREMENTAL */
1928
1929      glyph->linearVertAdvance = advance;
1930
1931      /* scale the metrics */
1932      if ( !( loader->load_flags & FT_LOAD_NO_SCALE ) )
1933      {
1934        top     = FT_MulFix( top,     y_scale );
1935        advance = FT_MulFix( advance, y_scale );
1936      }
1937
1938      /* XXX: for now, we have no better algorithm for the lsb, but it */
1939      /*      should work fine.                                        */
1940      /*                                                               */
1941      glyph->metrics.vertBearingX = glyph->metrics.horiBearingX -
1942                                      glyph->metrics.horiAdvance / 2;
1943      glyph->metrics.vertBearingY = top;
1944      glyph->metrics.vertAdvance  = advance;
1945    }
1946
1947    return 0;
1948  }
1949
1950
1951#ifdef TT_CONFIG_OPTION_EMBEDDED_BITMAPS
1952
1953  static FT_Error
1954  load_sbit_image( TT_Size       size,
1955                   TT_GlyphSlot  glyph,
1956                   FT_UInt       glyph_index,
1957                   FT_Int32      load_flags )
1958  {
1959    TT_Face             face;
1960    SFNT_Service        sfnt;
1961    FT_Stream           stream;
1962    FT_Error            error;
1963    TT_SBit_MetricsRec  metrics;
1964
1965
1966    face   = (TT_Face)glyph->face;
1967    sfnt   = (SFNT_Service)face->sfnt;
1968    stream = face->root.stream;
1969
1970    error = sfnt->load_sbit_image( face,
1971                                   size->strike_index,
1972                                   glyph_index,
1973                                   (FT_Int)load_flags,
1974                                   stream,
1975                                   &glyph->bitmap,
1976                                   &metrics );
1977    if ( !error )
1978    {
1979      glyph->outline.n_points   = 0;
1980      glyph->outline.n_contours = 0;
1981
1982      glyph->metrics.width  = (FT_Pos)metrics.width  << 6;
1983      glyph->metrics.height = (FT_Pos)metrics.height << 6;
1984
1985      glyph->metrics.horiBearingX = (FT_Pos)metrics.horiBearingX << 6;
1986      glyph->metrics.horiBearingY = (FT_Pos)metrics.horiBearingY << 6;
1987      glyph->metrics.horiAdvance  = (FT_Pos)metrics.horiAdvance  << 6;
1988
1989      glyph->metrics.vertBearingX = (FT_Pos)metrics.vertBearingX << 6;
1990      glyph->metrics.vertBearingY = (FT_Pos)metrics.vertBearingY << 6;
1991      glyph->metrics.vertAdvance  = (FT_Pos)metrics.vertAdvance  << 6;
1992
1993      glyph->format = FT_GLYPH_FORMAT_BITMAP;
1994
1995      if ( load_flags & FT_LOAD_VERTICAL_LAYOUT )
1996      {
1997        glyph->bitmap_left = metrics.vertBearingX;
1998        glyph->bitmap_top  = metrics.vertBearingY;
1999      }
2000      else
2001      {
2002        glyph->bitmap_left = metrics.horiBearingX;
2003        glyph->bitmap_top  = metrics.horiBearingY;
2004      }
2005    }
2006
2007    return error;
2008  }
2009
2010#endif /* TT_CONFIG_OPTION_EMBEDDED_BITMAPS */
2011
2012
2013  static FT_Error
2014  tt_loader_init( TT_Loader     loader,
2015                  TT_Size       size,
2016                  TT_GlyphSlot  glyph,
2017                  FT_Int32      load_flags,
2018                  FT_Bool       glyf_table_only )
2019  {
2020    TT_Face    face;
2021    FT_Stream  stream;
2022#ifdef TT_USE_BYTECODE_INTERPRETER
2023    FT_Bool    pedantic = FT_BOOL( load_flags & FT_LOAD_PEDANTIC );
2024#endif
2025
2026
2027    face   = (TT_Face)glyph->face;
2028    stream = face->root.stream;
2029
2030    FT_MEM_ZERO( loader, sizeof ( TT_LoaderRec ) );
2031
2032#ifdef TT_USE_BYTECODE_INTERPRETER
2033
2034    /* load execution context */
2035    if ( IS_HINTED( load_flags ) && !glyf_table_only )
2036    {
2037      TT_ExecContext  exec;
2038      FT_Bool         grayscale;
2039
2040#ifdef TT_CONFIG_OPTION_SUBPIXEL_HINTING
2041      TT_Driver  driver = (TT_Driver)FT_FACE_DRIVER( face );
2042
2043      FT_Bool  subpixel_hinting  = FALSE;
2044      FT_Bool  grayscale_hinting = TRUE;
2045
2046#if 0
2047      /* not used yet */
2048      FT_Bool  compatible_widths;
2049      FT_Bool  symmetrical_smoothing;
2050      FT_Bool  bgr;
2051      FT_Bool  subpixel_positioned;
2052#endif
2053#endif /* TT_CONFIG_OPTION_SUBPIXEL_HINTING */
2054
2055      FT_Bool  reexecute = FALSE;
2056
2057
2058      if ( !size->cvt_ready )
2059      {
2060        FT_Error  error = tt_size_ready_bytecode( size, pedantic );
2061
2062
2063        if ( error )
2064          return error;
2065      }
2066
2067      /* query new execution context */
2068      exec = size->debug ? size->context
2069                         : ( (TT_Driver)FT_FACE_DRIVER( face ) )->context;
2070      if ( !exec )
2071        return FT_THROW( Could_Not_Find_Context );
2072
2073#ifdef TT_CONFIG_OPTION_SUBPIXEL_HINTING
2074
2075      if ( driver->interpreter_version == TT_INTERPRETER_VERSION_38 )
2076      {
2077        subpixel_hinting = FT_BOOL( ( FT_LOAD_TARGET_MODE( load_flags )
2078                                      != FT_RENDER_MODE_MONO )          &&
2079                                    SPH_OPTION_SET_SUBPIXEL             );
2080
2081        if ( subpixel_hinting )
2082          grayscale = grayscale_hinting = FALSE;
2083        else if ( SPH_OPTION_SET_GRAYSCALE )
2084        {
2085          grayscale = grayscale_hinting = TRUE;
2086          subpixel_hinting              = FALSE;
2087        }
2088        else
2089          grayscale = grayscale_hinting = FALSE;
2090
2091        if ( FT_IS_TRICKY( glyph->face ) )
2092          subpixel_hinting = grayscale_hinting = FALSE;
2093
2094        exec->ignore_x_mode      = subpixel_hinting || grayscale_hinting;
2095        exec->rasterizer_version = SPH_OPTION_SET_RASTERIZER_VERSION;
2096        if ( exec->sph_tweak_flags & SPH_TWEAK_RASTERIZER_35 )
2097          exec->rasterizer_version = TT_INTERPRETER_VERSION_35;
2098
2099#if 1
2100        exec->compatible_widths     = SPH_OPTION_SET_COMPATIBLE_WIDTHS;
2101        exec->symmetrical_smoothing = FALSE;
2102        exec->bgr                   = FALSE;
2103        exec->subpixel_positioned   = TRUE;
2104#else /* 0 */
2105        exec->compatible_widths =
2106          FT_BOOL( FT_LOAD_TARGET_MODE( load_flags ) !=
2107                   TT_LOAD_COMPATIBLE_WIDTHS );
2108        exec->symmetrical_smoothing =
2109          FT_BOOL( FT_LOAD_TARGET_MODE( load_flags ) !=
2110                   TT_LOAD_SYMMETRICAL_SMOOTHING );
2111        exec->bgr =
2112          FT_BOOL( FT_LOAD_TARGET_MODE( load_flags ) !=
2113                   TT_LOAD_BGR );
2114        exec->subpixel_positioned =
2115          FT_BOOL( FT_LOAD_TARGET_MODE( load_flags ) !=
2116                   TT_LOAD_SUBPIXEL_POSITIONED );
2117#endif /* 0 */
2118
2119      }
2120      else
2121
2122#endif /* TT_CONFIG_OPTION_SUBPIXEL_HINTING */
2123
2124      {
2125        grayscale = FT_BOOL( FT_LOAD_TARGET_MODE( load_flags ) !=
2126                             FT_RENDER_MODE_MONO );
2127      }
2128
2129      TT_Load_Context( exec, face, size );
2130
2131#ifdef TT_CONFIG_OPTION_SUBPIXEL_HINTING
2132
2133      if ( driver->interpreter_version == TT_INTERPRETER_VERSION_38 )
2134      {
2135        /* a change from mono to subpixel rendering (and vice versa) */
2136        /* requires a re-execution of the CVT program                */
2137        if ( subpixel_hinting != exec->subpixel_hinting )
2138        {
2139          FT_TRACE4(( "tt_loader_init: subpixel hinting change,"
2140                      " re-executing `prep' table\n" ));
2141
2142          exec->subpixel_hinting = subpixel_hinting;
2143          reexecute              = TRUE;
2144        }
2145
2146        /* a change from mono to grayscale rendering (and vice versa) */
2147        /* requires a re-execution of the CVT program                 */
2148        if ( grayscale != exec->grayscale_hinting )
2149        {
2150          FT_TRACE4(( "tt_loader_init: grayscale hinting change,"
2151                      " re-executing `prep' table\n" ));
2152
2153          exec->grayscale_hinting = grayscale_hinting;
2154          reexecute               = TRUE;
2155        }
2156      }
2157      else
2158
2159#endif /* TT_CONFIG_OPTION_SUBPIXEL_HINTING */
2160
2161      {
2162        /* a change from mono to grayscale rendering (and vice versa) */
2163        /* requires a re-execution of the CVT program                 */
2164        if ( grayscale != exec->grayscale )
2165        {
2166          FT_TRACE4(( "tt_loader_init: grayscale change,"
2167                      " re-executing `prep' table\n" ));
2168
2169          exec->grayscale = grayscale;
2170          reexecute       = TRUE;
2171        }
2172      }
2173
2174      if ( reexecute )
2175      {
2176        FT_UInt  i;
2177
2178
2179        for ( i = 0; i < size->cvt_size; i++ )
2180          size->cvt[i] = FT_MulFix( face->cvt[i], size->ttmetrics.scale );
2181        tt_size_run_prep( size, pedantic );
2182      }
2183
2184      /* see whether the cvt program has disabled hinting */
2185      if ( exec->GS.instruct_control & 1 )
2186        load_flags |= FT_LOAD_NO_HINTING;
2187
2188      /* load default graphics state -- if needed */
2189      if ( exec->GS.instruct_control & 2 )
2190        exec->GS = tt_default_graphics_state;
2191
2192      exec->pedantic_hinting = FT_BOOL( load_flags & FT_LOAD_PEDANTIC );
2193      loader->exec = exec;
2194      loader->instructions = exec->glyphIns;
2195    }
2196
2197#endif /* TT_USE_BYTECODE_INTERPRETER */
2198
2199    /* seek to the beginning of the glyph table -- for Type 42 fonts     */
2200    /* the table might be accessed from a Postscript stream or something */
2201    /* else...                                                           */
2202
2203#ifdef FT_CONFIG_OPTION_INCREMENTAL
2204
2205    if ( face->root.internal->incremental_interface )
2206      loader->glyf_offset = 0;
2207    else
2208
2209#endif
2210
2211    {
2212      FT_Error  error = face->goto_table( face, TTAG_glyf, stream, 0 );
2213
2214
2215      if ( FT_ERR_EQ( error, Table_Missing ) )
2216        loader->glyf_offset = 0;
2217      else if ( error )
2218      {
2219        FT_ERROR(( "tt_loader_init: could not access glyph table\n" ));
2220        return error;
2221      }
2222      else
2223        loader->glyf_offset = FT_STREAM_POS();
2224    }
2225
2226    /* get face's glyph loader */
2227    if ( !glyf_table_only )
2228    {
2229      FT_GlyphLoader  gloader = glyph->internal->loader;
2230
2231
2232      FT_GlyphLoader_Rewind( gloader );
2233      loader->gloader = gloader;
2234    }
2235
2236    loader->load_flags = load_flags;
2237
2238    loader->face   = (FT_Face)face;
2239    loader->size   = (FT_Size)size;
2240    loader->glyph  = (FT_GlyphSlot)glyph;
2241    loader->stream = stream;
2242
2243    return FT_Err_Ok;
2244  }
2245
2246
2247  /*************************************************************************/
2248  /*                                                                       */
2249  /* <Function>                                                            */
2250  /*    TT_Load_Glyph                                                      */
2251  /*                                                                       */
2252  /* <Description>                                                         */
2253  /*    A function used to load a single glyph within a given glyph slot,  */
2254  /*    for a given size.                                                  */
2255  /*                                                                       */
2256  /* <Input>                                                               */
2257  /*    glyph       :: A handle to a target slot object where the glyph    */
2258  /*                   will be loaded.                                     */
2259  /*                                                                       */
2260  /*    size        :: A handle to the source face size at which the glyph */
2261  /*                   must be scaled/loaded.                              */
2262  /*                                                                       */
2263  /*    glyph_index :: The index of the glyph in the font file.            */
2264  /*                                                                       */
2265  /*    load_flags  :: A flag indicating what to load for this glyph.  The */
2266  /*                   FT_LOAD_XXX constants can be used to control the    */
2267  /*                   glyph loading process (e.g., whether the outline    */
2268  /*                   should be scaled, whether to load bitmaps or not,   */
2269  /*                   whether to hint the outline, etc).                  */
2270  /*                                                                       */
2271  /* <Return>                                                              */
2272  /*    FreeType error code.  0 means success.                             */
2273  /*                                                                       */
2274  FT_LOCAL_DEF( FT_Error )
2275  TT_Load_Glyph( TT_Size       size,
2276                 TT_GlyphSlot  glyph,
2277                 FT_UInt       glyph_index,
2278                 FT_Int32      load_flags )
2279  {
2280    FT_Error      error;
2281    TT_LoaderRec  loader;
2282
2283
2284    error = FT_Err_Ok;
2285
2286    FT_TRACE1(( "TT_Load_Glyph: glyph index %d\n", glyph_index ));
2287
2288#ifdef TT_CONFIG_OPTION_EMBEDDED_BITMAPS
2289
2290    /* try to load embedded bitmap if any              */
2291    /*                                                 */
2292    /* XXX: The convention should be emphasized in     */
2293    /*      the documents because it can be confusing. */
2294    if ( size->strike_index != 0xFFFFFFFFUL      &&
2295         ( load_flags & FT_LOAD_NO_BITMAP ) == 0 )
2296    {
2297      error = load_sbit_image( size, glyph, glyph_index, load_flags );
2298      if ( !error )
2299      {
2300        if ( FT_IS_SCALABLE( glyph->face ) )
2301        {
2302          /* for the bbox we need the header only */
2303          (void)tt_loader_init( &loader, size, glyph, load_flags, TRUE );
2304          (void)load_truetype_glyph( &loader, glyph_index, 0, TRUE );
2305          glyph->linearHoriAdvance = loader.linear;
2306          glyph->linearVertAdvance = loader.top_bearing + loader.bbox.yMax -
2307                                       loader.vadvance;
2308
2309          /* sanity check: if `horiAdvance' in the sbit metric */
2310          /* structure isn't set, use `linearHoriAdvance'      */
2311          if ( !glyph->metrics.horiAdvance && glyph->linearHoriAdvance )
2312            glyph->metrics.horiAdvance =
2313              FT_MulFix( glyph->linearHoriAdvance,
2314                         size->root.metrics.x_scale );
2315        }
2316
2317        return FT_Err_Ok;
2318      }
2319    }
2320
2321#endif /* TT_CONFIG_OPTION_EMBEDDED_BITMAPS */
2322
2323    /* if FT_LOAD_NO_SCALE is not set, `ttmetrics' must be valid */
2324    if ( !( load_flags & FT_LOAD_NO_SCALE ) && !size->ttmetrics.valid )
2325      return FT_THROW( Invalid_Size_Handle );
2326
2327    if ( load_flags & FT_LOAD_SBITS_ONLY )
2328      return FT_THROW( Invalid_Argument );
2329
2330    error = tt_loader_init( &loader, size, glyph, load_flags, FALSE );
2331    if ( error )
2332      return error;
2333
2334    glyph->format        = FT_GLYPH_FORMAT_OUTLINE;
2335    glyph->num_subglyphs = 0;
2336    glyph->outline.flags = 0;
2337
2338    /* main loading loop */
2339    error = load_truetype_glyph( &loader, glyph_index, 0, FALSE );
2340    if ( !error )
2341    {
2342      if ( glyph->format == FT_GLYPH_FORMAT_COMPOSITE )
2343      {
2344        glyph->num_subglyphs = loader.gloader->base.num_subglyphs;
2345        glyph->subglyphs     = loader.gloader->base.subglyphs;
2346      }
2347      else
2348      {
2349        glyph->outline        = loader.gloader->base.outline;
2350        glyph->outline.flags &= ~FT_OUTLINE_SINGLE_PASS;
2351
2352        /* Translate array so that (0,0) is the glyph's origin.  Note  */
2353        /* that this behaviour is independent on the value of bit 1 of */
2354        /* the `flags' field in the `head' table -- at least major     */
2355        /* applications like Acroread indicate that.                   */
2356        if ( loader.pp1.x )
2357          FT_Outline_Translate( &glyph->outline, -loader.pp1.x, 0 );
2358      }
2359
2360#ifdef TT_USE_BYTECODE_INTERPRETER
2361
2362      if ( IS_HINTED( load_flags ) )
2363      {
2364        if ( loader.exec->GS.scan_control )
2365        {
2366          /* convert scan conversion mode to FT_OUTLINE_XXX flags */
2367          switch ( loader.exec->GS.scan_type )
2368          {
2369          case 0: /* simple drop-outs including stubs */
2370            glyph->outline.flags |= FT_OUTLINE_INCLUDE_STUBS;
2371            break;
2372          case 1: /* simple drop-outs excluding stubs */
2373            /* nothing; it's the default rendering mode */
2374            break;
2375          case 4: /* smart drop-outs including stubs */
2376            glyph->outline.flags |= FT_OUTLINE_SMART_DROPOUTS |
2377                                    FT_OUTLINE_INCLUDE_STUBS;
2378            break;
2379          case 5: /* smart drop-outs excluding stubs  */
2380            glyph->outline.flags |= FT_OUTLINE_SMART_DROPOUTS;
2381            break;
2382
2383          default: /* no drop-out control */
2384            glyph->outline.flags |= FT_OUTLINE_IGNORE_DROPOUTS;
2385            break;
2386          }
2387        }
2388        else
2389          glyph->outline.flags |= FT_OUTLINE_IGNORE_DROPOUTS;
2390      }
2391
2392#endif /* TT_USE_BYTECODE_INTERPRETER */
2393
2394      compute_glyph_metrics( &loader, glyph_index );
2395    }
2396
2397    /* Set the `high precision' bit flag.                           */
2398    /* This is _critical_ to get correct output for monochrome      */
2399    /* TrueType glyphs at all sizes using the bytecode interpreter. */
2400    /*                                                              */
2401    if ( !( load_flags & FT_LOAD_NO_SCALE ) &&
2402         size->root.metrics.y_ppem < 24     )
2403      glyph->outline.flags |= FT_OUTLINE_HIGH_PRECISION;
2404
2405    return error;
2406  }
2407
2408
2409/* END */
2410