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