ttgload.c revision 6689a009ced7442c121df1224b3c529e81dc5017
1/***************************************************************************/
2/*                                                                         */
3/*  ttgload.c                                                              */
4/*                                                                         */
5/*    TrueType Glyph Loader (body).                                        */
6/*                                                                         */
7/*  Copyright 1996-2014                                                    */
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  = face->os2.sTypoAscender - face->os2.sTypoDescender;
104    }
105
106    else
107    {
108      *tsb = (FT_Short)( 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
742#ifdef TT_USE_BYTECODE_INTERPRETER
743    FT_UInt       n_ins;
744#else
745    FT_UNUSED( is_composite );
746#endif
747
748
749#ifdef TT_USE_BYTECODE_INTERPRETER
750    if ( loader->glyph->control_len > 0xFFFFL )
751    {
752      FT_TRACE1(( "TT_Hint_Glyph: too long instructions" ));
753      FT_TRACE1(( " (0x%lx byte) is truncated\n",
754                 loader->glyph->control_len ));
755    }
756    n_ins = (FT_UInt)( loader->glyph->control_len );
757
758    /* save original point position in org */
759    if ( n_ins > 0 )
760      FT_ARRAY_COPY( zone->org, zone->cur, zone->n_points );
761
762    /* Reset graphics state. */
763    loader->exec->GS = ((TT_Size)loader->size)->GS;
764
765    /* XXX: UNDOCUMENTED! Hinting instructions of a composite glyph */
766    /*      completely refer to the (already) hinted subglyphs.     */
767    if ( is_composite )
768    {
769      loader->exec->metrics.x_scale = 1 << 16;
770      loader->exec->metrics.y_scale = 1 << 16;
771
772      FT_ARRAY_COPY( zone->orus, zone->cur, zone->n_points );
773    }
774    else
775    {
776      loader->exec->metrics.x_scale =
777        ((TT_Size)loader->size)->metrics.x_scale;
778      loader->exec->metrics.y_scale =
779        ((TT_Size)loader->size)->metrics.y_scale;
780    }
781#endif
782
783    /* round phantom points */
784    zone->cur[zone->n_points - 4].x =
785      FT_PIX_ROUND( zone->cur[zone->n_points - 4].x );
786    zone->cur[zone->n_points - 3].x =
787      FT_PIX_ROUND( zone->cur[zone->n_points - 3].x );
788    zone->cur[zone->n_points - 2].y =
789      FT_PIX_ROUND( zone->cur[zone->n_points - 2].y );
790    zone->cur[zone->n_points - 1].y =
791      FT_PIX_ROUND( zone->cur[zone->n_points - 1].y );
792
793#ifdef TT_USE_BYTECODE_INTERPRETER
794
795    if ( n_ins > 0 )
796    {
797      FT_Bool   debug;
798      FT_Error  error;
799
800      FT_GlyphLoader  gloader         = loader->gloader;
801      FT_Outline      current_outline = gloader->current.outline;
802
803
804      TT_Set_CodeRange( loader->exec, tt_coderange_glyph,
805                        loader->exec->glyphIns, n_ins );
806
807      loader->exec->is_composite = is_composite;
808      loader->exec->pts          = *zone;
809
810      debug = FT_BOOL( !( loader->load_flags & FT_LOAD_NO_SCALE ) &&
811                       ((TT_Size)loader->size)->debug             );
812
813      error = TT_Run_Context( loader->exec, debug );
814      if ( error && loader->exec->pedantic_hinting )
815        return error;
816
817      /* store drop-out mode in bits 5-7; set bit 2 also as a marker */
818      current_outline.tags[0] |=
819        ( loader->exec->GS.scan_type << 5 ) | FT_CURVE_TAG_HAS_SCANMODE;
820    }
821
822#endif
823
824    /* save glyph phantom points */
825    loader->pp1 = zone->cur[zone->n_points - 4];
826    loader->pp2 = zone->cur[zone->n_points - 3];
827    loader->pp3 = zone->cur[zone->n_points - 2];
828    loader->pp4 = zone->cur[zone->n_points - 1];
829
830#ifdef TT_CONFIG_OPTION_SUBPIXEL_HINTING
831    if ( driver->interpreter_version == TT_INTERPRETER_VERSION_38 )
832    {
833      if ( loader->exec->sph_tweak_flags & SPH_TWEAK_DEEMBOLDEN )
834        FT_Outline_EmboldenXY( &loader->gloader->current.outline, -24, 0 );
835
836      else if ( loader->exec->sph_tweak_flags & SPH_TWEAK_EMBOLDEN )
837        FT_Outline_EmboldenXY( &loader->gloader->current.outline, 24, 0 );
838    }
839#endif /* TT_CONFIG_OPTION_SUBPIXEL_HINTING */
840
841    return FT_Err_Ok;
842  }
843
844
845  /*************************************************************************/
846  /*                                                                       */
847  /* <Function>                                                            */
848  /*    TT_Process_Simple_Glyph                                            */
849  /*                                                                       */
850  /* <Description>                                                         */
851  /*    Once a simple glyph has been loaded, it needs to be processed.     */
852  /*    Usually, this means scaling and hinting through bytecode           */
853  /*    interpretation.                                                    */
854  /*                                                                       */
855  static FT_Error
856  TT_Process_Simple_Glyph( TT_Loader  loader )
857  {
858    FT_GlyphLoader  gloader = loader->gloader;
859    FT_Error        error   = FT_Err_Ok;
860    FT_Outline*     outline;
861    FT_Int          n_points;
862
863
864    outline  = &gloader->current.outline;
865    n_points = outline->n_points;
866
867    /* set phantom points */
868
869    outline->points[n_points    ] = loader->pp1;
870    outline->points[n_points + 1] = loader->pp2;
871    outline->points[n_points + 2] = loader->pp3;
872    outline->points[n_points + 3] = loader->pp4;
873
874    outline->tags[n_points    ] = 0;
875    outline->tags[n_points + 1] = 0;
876    outline->tags[n_points + 2] = 0;
877    outline->tags[n_points + 3] = 0;
878
879    n_points += 4;
880
881#ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
882
883    if ( ((TT_Face)loader->face)->doblend )
884    {
885      /* Deltas apply to the unscaled data. */
886      FT_Vector*  deltas;
887      FT_Memory   memory = loader->face->memory;
888      FT_Int      i;
889
890
891      error = TT_Vary_Get_Glyph_Deltas( (TT_Face)(loader->face),
892                                        loader->glyph_index,
893                                        &deltas,
894                                        n_points );
895      if ( error )
896        return error;
897
898      for ( i = 0; i < n_points; ++i )
899      {
900        outline->points[i].x += deltas[i].x;
901        outline->points[i].y += deltas[i].y;
902      }
903
904      FT_FREE( deltas );
905    }
906
907#endif /* TT_CONFIG_OPTION_GX_VAR_SUPPORT */
908
909    if ( IS_HINTED( loader->load_flags ) )
910    {
911      tt_prepare_zone( &loader->zone, &gloader->current, 0, 0 );
912
913      FT_ARRAY_COPY( loader->zone.orus, loader->zone.cur,
914                     loader->zone.n_points + 4 );
915    }
916
917    {
918#ifdef TT_CONFIG_OPTION_SUBPIXEL_HINTING
919      TT_Face    face   = (TT_Face)loader->face;
920      TT_Driver  driver = (TT_Driver)FT_FACE_DRIVER( face );
921
922      FT_String*  family         = face->root.family_name;
923      FT_Int      ppem           = loader->size->metrics.x_ppem;
924      FT_String*  style          = face->root.style_name;
925      FT_Int      x_scale_factor = 1000;
926#endif
927
928      FT_Vector*  vec   = outline->points;
929      FT_Vector*  limit = outline->points + n_points;
930
931      FT_Fixed  x_scale = 0; /* pacify compiler */
932      FT_Fixed  y_scale = 0;
933
934      FT_Bool  do_scale = FALSE;
935
936
937#ifdef TT_CONFIG_OPTION_SUBPIXEL_HINTING
938
939      if ( driver->interpreter_version == TT_INTERPRETER_VERSION_38 )
940      {
941        /* scale, but only if enabled and only if TT hinting is being used */
942        if ( IS_HINTED( loader->load_flags ) )
943          x_scale_factor = sph_test_tweak_x_scaling( face,
944                                                     family,
945                                                     ppem,
946                                                     style,
947                                                     loader->glyph_index );
948        /* scale the glyph */
949        if ( ( loader->load_flags & FT_LOAD_NO_SCALE ) == 0 ||
950             x_scale_factor != 1000                         )
951        {
952          x_scale = FT_MulDiv( ((TT_Size)loader->size)->metrics.x_scale,
953                               x_scale_factor, 1000 );
954          y_scale = ((TT_Size)loader->size)->metrics.y_scale;
955
956          /* compensate for any scaling by de/emboldening; */
957          /* the amount was determined via experimentation */
958          if ( x_scale_factor != 1000 && ppem > 11 )
959            FT_Outline_EmboldenXY( outline,
960                                   FT_MulFix( 1280 * ppem,
961                                              1000 - x_scale_factor ),
962                                   0 );
963          do_scale = TRUE;
964        }
965      }
966      else
967
968#endif /* TT_CONFIG_OPTION_SUBPIXEL_HINTING */
969
970      {
971        /* scale the glyph */
972        if ( ( loader->load_flags & FT_LOAD_NO_SCALE ) == 0 )
973        {
974          x_scale = ((TT_Size)loader->size)->metrics.x_scale;
975          y_scale = ((TT_Size)loader->size)->metrics.y_scale;
976
977          do_scale = TRUE;
978        }
979      }
980
981      if ( do_scale )
982      {
983        for ( ; vec < limit; vec++ )
984        {
985          vec->x = FT_MulFix( vec->x, x_scale );
986          vec->y = FT_MulFix( vec->y, y_scale );
987        }
988
989        loader->pp1 = outline->points[n_points - 4];
990        loader->pp2 = outline->points[n_points - 3];
991        loader->pp3 = outline->points[n_points - 2];
992        loader->pp4 = outline->points[n_points - 1];
993      }
994    }
995
996    if ( IS_HINTED( loader->load_flags ) )
997    {
998      loader->zone.n_points += 4;
999
1000      error = TT_Hint_Glyph( loader, 0 );
1001    }
1002
1003    return error;
1004  }
1005
1006
1007  /*************************************************************************/
1008  /*                                                                       */
1009  /* <Function>                                                            */
1010  /*    TT_Process_Composite_Component                                     */
1011  /*                                                                       */
1012  /* <Description>                                                         */
1013  /*    Once a composite component has been loaded, it needs to be         */
1014  /*    processed.  Usually, this means transforming and translating.      */
1015  /*                                                                       */
1016  static FT_Error
1017  TT_Process_Composite_Component( TT_Loader    loader,
1018                                  FT_SubGlyph  subglyph,
1019                                  FT_UInt      start_point,
1020                                  FT_UInt      num_base_points )
1021  {
1022    FT_GlyphLoader  gloader    = loader->gloader;
1023    FT_Vector*      base_vec   = gloader->base.outline.points;
1024    FT_UInt         num_points = gloader->base.outline.n_points;
1025    FT_Bool         have_scale;
1026    FT_Pos          x, y;
1027
1028
1029    have_scale = FT_BOOL( subglyph->flags & ( WE_HAVE_A_SCALE     |
1030                                              WE_HAVE_AN_XY_SCALE |
1031                                              WE_HAVE_A_2X2       ) );
1032
1033    /* perform the transform required for this subglyph */
1034    if ( have_scale )
1035    {
1036      FT_UInt  i;
1037
1038
1039      for ( i = num_base_points; i < num_points; i++ )
1040        FT_Vector_Transform( base_vec + i, &subglyph->transform );
1041    }
1042
1043    /* get offset */
1044    if ( !( subglyph->flags & ARGS_ARE_XY_VALUES ) )
1045    {
1046      FT_UInt     k = subglyph->arg1;
1047      FT_UInt     l = subglyph->arg2;
1048      FT_Vector*  p1;
1049      FT_Vector*  p2;
1050
1051
1052      /* match l-th point of the newly loaded component to the k-th point */
1053      /* of the previously loaded components.                             */
1054
1055      /* change to the point numbers used by our outline */
1056      k += start_point;
1057      l += num_base_points;
1058      if ( k >= num_base_points ||
1059           l >= num_points      )
1060        return FT_THROW( Invalid_Composite );
1061
1062      p1 = gloader->base.outline.points + k;
1063      p2 = gloader->base.outline.points + l;
1064
1065      x = p1->x - p2->x;
1066      y = p1->y - p2->y;
1067    }
1068    else
1069    {
1070      x = subglyph->arg1;
1071      y = subglyph->arg2;
1072
1073      if ( !x && !y )
1074        return FT_Err_Ok;
1075
1076      /* Use a default value dependent on                                  */
1077      /* TT_CONFIG_OPTION_COMPONENT_OFFSET_SCALED.  This is useful for old */
1078      /* TT fonts which don't set the xxx_COMPONENT_OFFSET bit.            */
1079
1080      if ( have_scale &&
1081#ifdef TT_CONFIG_OPTION_COMPONENT_OFFSET_SCALED
1082           !( subglyph->flags & UNSCALED_COMPONENT_OFFSET ) )
1083#else
1084            ( subglyph->flags & SCALED_COMPONENT_OFFSET ) )
1085#endif
1086      {
1087
1088#if 0
1089
1090        /*******************************************************************/
1091        /*                                                                 */
1092        /* This algorithm is what Apple documents.  But it doesn't work.   */
1093        /*                                                                 */
1094        int  a = subglyph->transform.xx > 0 ?  subglyph->transform.xx
1095                                            : -subglyph->transform.xx;
1096        int  b = subglyph->transform.yx > 0 ?  subglyph->transform.yx
1097                                            : -subglyph->transform.yx;
1098        int  c = subglyph->transform.xy > 0 ?  subglyph->transform.xy
1099                                            : -subglyph->transform.xy;
1100        int  d = subglyph->transform.yy > 0 ? subglyph->transform.yy
1101                                            : -subglyph->transform.yy;
1102        int  m = a > b ? a : b;
1103        int  n = c > d ? c : d;
1104
1105
1106        if ( a - b <= 33 && a - b >= -33 )
1107          m *= 2;
1108        if ( c - d <= 33 && c - d >= -33 )
1109          n *= 2;
1110        x = FT_MulFix( x, m );
1111        y = FT_MulFix( y, n );
1112
1113#else /* 1 */
1114
1115        /*******************************************************************/
1116        /*                                                                 */
1117        /* This algorithm is a guess and works much better than the above. */
1118        /*                                                                 */
1119        FT_Fixed  mac_xscale = FT_Hypot( subglyph->transform.xx,
1120                                         subglyph->transform.xy );
1121        FT_Fixed  mac_yscale = FT_Hypot( subglyph->transform.yy,
1122                                         subglyph->transform.yx );
1123
1124
1125        x = FT_MulFix( x, mac_xscale );
1126        y = FT_MulFix( y, mac_yscale );
1127
1128#endif /* 1 */
1129
1130      }
1131
1132      if ( !( loader->load_flags & FT_LOAD_NO_SCALE ) )
1133      {
1134        FT_Fixed  x_scale = ((TT_Size)loader->size)->metrics.x_scale;
1135        FT_Fixed  y_scale = ((TT_Size)loader->size)->metrics.y_scale;
1136
1137
1138        x = FT_MulFix( x, x_scale );
1139        y = FT_MulFix( y, y_scale );
1140
1141        if ( subglyph->flags & ROUND_XY_TO_GRID )
1142        {
1143          x = FT_PIX_ROUND( x );
1144          y = FT_PIX_ROUND( y );
1145        }
1146      }
1147    }
1148
1149    if ( x || y )
1150      translate_array( num_points - num_base_points,
1151                       base_vec + num_base_points,
1152                       x, y );
1153
1154    return FT_Err_Ok;
1155  }
1156
1157
1158  /*************************************************************************/
1159  /*                                                                       */
1160  /* <Function>                                                            */
1161  /*    TT_Process_Composite_Glyph                                         */
1162  /*                                                                       */
1163  /* <Description>                                                         */
1164  /*    This is slightly different from TT_Process_Simple_Glyph, in that   */
1165  /*    its sole purpose is to hint the glyph.  Thus this function is      */
1166  /*    only available when bytecode interpreter is enabled.               */
1167  /*                                                                       */
1168  static FT_Error
1169  TT_Process_Composite_Glyph( TT_Loader  loader,
1170                              FT_UInt    start_point,
1171                              FT_UInt    start_contour )
1172  {
1173    FT_Error     error;
1174    FT_Outline*  outline;
1175    FT_UInt      i;
1176
1177
1178    outline = &loader->gloader->base.outline;
1179
1180    /* make room for phantom points */
1181    error = FT_GLYPHLOADER_CHECK_POINTS( loader->gloader,
1182                                         outline->n_points + 4,
1183                                         0 );
1184    if ( error )
1185      return error;
1186
1187    outline->points[outline->n_points    ] = loader->pp1;
1188    outline->points[outline->n_points + 1] = loader->pp2;
1189    outline->points[outline->n_points + 2] = loader->pp3;
1190    outline->points[outline->n_points + 3] = loader->pp4;
1191
1192    outline->tags[outline->n_points    ] = 0;
1193    outline->tags[outline->n_points + 1] = 0;
1194    outline->tags[outline->n_points + 2] = 0;
1195    outline->tags[outline->n_points + 3] = 0;
1196
1197#ifdef TT_USE_BYTECODE_INTERPRETER
1198
1199    {
1200      FT_Stream  stream = loader->stream;
1201      FT_UShort  n_ins, max_ins;
1202      FT_ULong   tmp;
1203
1204
1205      /* TT_Load_Composite_Glyph only gives us the offset of instructions */
1206      /* so we read them here                                             */
1207      if ( FT_STREAM_SEEK( loader->ins_pos ) ||
1208           FT_READ_USHORT( n_ins )           )
1209        return error;
1210
1211      FT_TRACE5(( "  Instructions size = %d\n", n_ins ));
1212
1213      /* check it */
1214      max_ins = ((TT_Face)loader->face)->max_profile.maxSizeOfInstructions;
1215      if ( n_ins > max_ins )
1216      {
1217        /* don't trust `maxSizeOfInstructions'; */
1218        /* only do a rough safety check         */
1219        if ( (FT_Int)n_ins > loader->byte_len )
1220        {
1221          FT_TRACE1(( "TT_Process_Composite_Glyph:"
1222                      " too many instructions (%d) for glyph with length %d\n",
1223                      n_ins, loader->byte_len ));
1224          return FT_THROW( Too_Many_Hints );
1225        }
1226
1227        tmp   = loader->exec->glyphSize;
1228        error = Update_Max( loader->exec->memory,
1229                            &tmp,
1230                            sizeof ( FT_Byte ),
1231                            (void*)&loader->exec->glyphIns,
1232                            n_ins );
1233
1234        loader->exec->glyphSize = (FT_UShort)tmp;
1235        if ( error )
1236          return error;
1237      }
1238      else if ( n_ins == 0 )
1239        return FT_Err_Ok;
1240
1241      if ( FT_STREAM_READ( loader->exec->glyphIns, n_ins ) )
1242        return error;
1243
1244      loader->glyph->control_data = loader->exec->glyphIns;
1245      loader->glyph->control_len  = n_ins;
1246    }
1247
1248#endif
1249
1250    tt_prepare_zone( &loader->zone, &loader->gloader->base,
1251                     start_point, start_contour );
1252
1253    /* Some points are likely touched during execution of  */
1254    /* instructions on components.  So let's untouch them. */
1255    for ( i = 0; i < loader->zone.n_points; i++ )
1256      loader->zone.tags[i] &= ~FT_CURVE_TAG_TOUCH_BOTH;
1257
1258    loader->zone.n_points += 4;
1259
1260    return TT_Hint_Glyph( loader, 1 );
1261  }
1262
1263
1264  /*
1265   * Calculate the phantom points
1266   *
1267   * Defining the right side bearing (rsb) as
1268   *
1269   *   rsb = aw - (lsb + xmax - xmin)
1270   *
1271   * (with `aw' the advance width, `lsb' the left side bearing, and `xmin'
1272   * and `xmax' the glyph's minimum and maximum x value), the OpenType
1273   * specification defines the initial position of horizontal phantom points
1274   * as
1275   *
1276   *   pp1 = (round(xmin - lsb), 0)      ,
1277   *   pp2 = (round(pp1 + aw), 0)        .
1278   *
1279   * Note that the rounding to the grid (in the device space) is not
1280   * documented currently in the specification.
1281   *
1282   * However, the specification lacks the precise definition of vertical
1283   * phantom points.  Greg Hitchcock provided the following explanation.
1284   *
1285   * - a `vmtx' table is present
1286   *
1287   *   For any glyph, the minimum and maximum y values (`ymin' and `ymax')
1288   *   are given in the `glyf' table, the top side bearing (tsb) and advance
1289   *   height (ah) are given in the `vmtx' table.  The bottom side bearing
1290   *   (bsb) is then calculated as
1291   *
1292   *     bsb = ah - (tsb + ymax - ymin)       ,
1293   *
1294   *   and the initial position of vertical phantom points is
1295   *
1296   *     pp3 = (x, round(ymax + tsb))       ,
1297   *     pp4 = (x, round(pp3 - ah))         .
1298   *
1299   *   See below for value `x'.
1300   *
1301   * - no `vmtx' table in the font
1302   *
1303   *   If there is an `OS/2' table, we set
1304   *
1305   *     DefaultAscender = sTypoAscender       ,
1306   *     DefaultDescender = sTypoDescender     ,
1307   *
1308   *   otherwise we use data from the `hhea' table:
1309   *
1310   *     DefaultAscender = Ascender         ,
1311   *     DefaultDescender = Descender       .
1312   *
1313   *   With these two variables we can now set
1314   *
1315   *     ah = DefaultAscender - sDefaultDescender    ,
1316   *     tsb = DefaultAscender - yMax                ,
1317   *
1318   *   and proceed as if a `vmtx' table was present.
1319   *
1320   * Usually we have
1321   *
1322   *   x = aw / 2      ,                                                (1)
1323   *
1324   * but there is one compatibility case where it can be set to
1325   *
1326   *   x = -DefaultDescender -
1327   *         ((DefaultAscender - DefaultDescender - aw) / 2)     .      (2)
1328   *
1329   * and another one with
1330   *
1331   *   x = 0     .                                                      (3)
1332   *
1333   * In Windows, the history of those values is quite complicated,
1334   * depending on the hinting engine (that is, the graphics framework).
1335   *
1336   *   framework        from                 to       formula
1337   *  ----------------------------------------------------------
1338   *    GDI       Windows 98               current      (1)
1339   *              (Windows 2000 for NT)
1340   *    GDI+      Windows XP               Windows 7    (2)
1341   *    GDI+      Windows 8                current      (3)
1342   *    DWrite    Windows 7                current      (3)
1343   *
1344   * For simplicity, FreeType uses (1) for grayscale subpixel hinting and
1345   * (3) for everything else.
1346   *
1347   */
1348#ifdef TT_CONFIG_OPTION_SUBPIXEL_HINTING
1349
1350#define TT_LOADER_SET_PP( loader )                                          \
1351          do                                                                \
1352          {                                                                 \
1353            FT_Bool  subpixel_  = loader->exec ? loader->exec->subpixel     \
1354                                               : 0;                         \
1355            FT_Bool  grayscale_ = loader->exec ? loader->exec->grayscale    \
1356                                               : 0;                         \
1357            FT_Bool  use_aw_2_  = (FT_Bool)( subpixel_ && grayscale_ );     \
1358                                                                            \
1359                                                                            \
1360            (loader)->pp1.x = (loader)->bbox.xMin - (loader)->left_bearing; \
1361            (loader)->pp1.y = 0;                                            \
1362            (loader)->pp2.x = (loader)->pp1.x + (loader)->advance;          \
1363            (loader)->pp2.y = 0;                                            \
1364                                                                            \
1365            (loader)->pp3.x = use_aw_2_ ? (loader)->advance / 2 : 0;        \
1366            (loader)->pp3.y = (loader)->bbox.yMax + (loader)->top_bearing;  \
1367            (loader)->pp4.x = use_aw_2_ ? (loader)->advance / 2 : 0;        \
1368            (loader)->pp4.y = (loader)->pp3.y - (loader)->vadvance;         \
1369          } while ( 0 )
1370
1371#else /* !TT_CONFIG_OPTION_SUBPIXEL_HINTING */
1372
1373#define TT_LOADER_SET_PP( loader )                                          \
1374          do                                                                \
1375          {                                                                 \
1376            (loader)->pp1.x = (loader)->bbox.xMin - (loader)->left_bearing; \
1377            (loader)->pp1.y = 0;                                            \
1378            (loader)->pp2.x = (loader)->pp1.x + (loader)->advance;          \
1379            (loader)->pp2.y = 0;                                            \
1380                                                                            \
1381            (loader)->pp3.x = 0;                                            \
1382            (loader)->pp3.y = (loader)->bbox.yMax + (loader)->top_bearing;  \
1383            (loader)->pp4.x = 0;                                            \
1384            (loader)->pp4.y = (loader)->pp3.y - (loader)->vadvance;         \
1385          } while ( 0 )
1386
1387#endif /* !TT_CONFIG_OPTION_SUBPIXEL_HINTING */
1388
1389
1390  /*************************************************************************/
1391  /*                                                                       */
1392  /* <Function>                                                            */
1393  /*    load_truetype_glyph                                                */
1394  /*                                                                       */
1395  /* <Description>                                                         */
1396  /*    Loads a given truetype glyph.  Handles composites and uses a       */
1397  /*    TT_Loader object.                                                  */
1398  /*                                                                       */
1399  static FT_Error
1400  load_truetype_glyph( TT_Loader  loader,
1401                       FT_UInt    glyph_index,
1402                       FT_UInt    recurse_count,
1403                       FT_Bool    header_only )
1404  {
1405    FT_Error        error        = FT_Err_Ok;
1406    FT_Fixed        x_scale, y_scale;
1407    FT_ULong        offset;
1408    TT_Face         face         = (TT_Face)loader->face;
1409    FT_GlyphLoader  gloader      = loader->gloader;
1410    FT_Bool         opened_frame = 0;
1411
1412#ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
1413    FT_Vector*      deltas       = NULL;
1414#endif
1415
1416#ifdef FT_CONFIG_OPTION_INCREMENTAL
1417    FT_StreamRec    inc_stream;
1418    FT_Data         glyph_data;
1419    FT_Bool         glyph_data_loaded = 0;
1420#endif
1421
1422
1423    /* some fonts have an incorrect value of `maxComponentDepth', */
1424    /* thus we allow depth 1 to catch the majority of them        */
1425    if ( recurse_count > 1                                   &&
1426         recurse_count > face->max_profile.maxComponentDepth )
1427    {
1428      error = FT_THROW( Invalid_Composite );
1429      goto Exit;
1430    }
1431
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
1439    loader->glyph_index = glyph_index;
1440
1441    if ( ( loader->load_flags & FT_LOAD_NO_SCALE ) == 0 )
1442    {
1443      x_scale = ((TT_Size)loader->size)->metrics.x_scale;
1444      y_scale = ((TT_Size)loader->size)->metrics.y_scale;
1445    }
1446    else
1447    {
1448      x_scale = 0x10000L;
1449      y_scale = 0x10000L;
1450    }
1451
1452    /* Set `offset' to the start of the glyph relative to the start of */
1453    /* the `glyf' table, and `byte_len' to the length of the glyph in  */
1454    /* bytes.                                                          */
1455
1456#ifdef FT_CONFIG_OPTION_INCREMENTAL
1457
1458    /* If we are loading glyph data via the incremental interface, set */
1459    /* the loader stream to a memory stream reading the data returned  */
1460    /* by the interface.                                               */
1461    if ( face->root.internal->incremental_interface )
1462    {
1463      error = face->root.internal->incremental_interface->funcs->get_glyph_data(
1464                face->root.internal->incremental_interface->object,
1465                glyph_index, &glyph_data );
1466      if ( error )
1467        goto Exit;
1468
1469      glyph_data_loaded = 1;
1470      offset            = 0;
1471      loader->byte_len  = glyph_data.length;
1472
1473      FT_MEM_ZERO( &inc_stream, sizeof ( inc_stream ) );
1474      FT_Stream_OpenMemory( &inc_stream,
1475                            glyph_data.pointer, glyph_data.length );
1476
1477      loader->stream = &inc_stream;
1478    }
1479    else
1480
1481#endif /* FT_CONFIG_OPTION_INCREMENTAL */
1482
1483      offset = tt_face_get_location( face, glyph_index,
1484                                     (FT_UInt*)&loader->byte_len );
1485
1486    if ( loader->byte_len > 0 )
1487    {
1488#ifdef FT_CONFIG_OPTION_INCREMENTAL
1489      /* for the incremental interface, `glyf_offset' is always zero */
1490      if ( !loader->glyf_offset                        &&
1491           !face->root.internal->incremental_interface )
1492#else
1493      if ( !loader->glyf_offset )
1494#endif /* FT_CONFIG_OPTION_INCREMENTAL */
1495      {
1496        FT_TRACE2(( "no `glyf' table but non-zero `loca' entry\n" ));
1497        error = FT_THROW( Invalid_Table );
1498        goto Exit;
1499      }
1500
1501      error = face->access_glyph_frame( loader, glyph_index,
1502                                        loader->glyf_offset + offset,
1503                                        loader->byte_len );
1504      if ( error )
1505        goto Exit;
1506
1507      opened_frame = 1;
1508
1509      /* read glyph header first */
1510      error = face->read_glyph_header( loader );
1511      if ( error )
1512        goto Exit;
1513
1514      /* the metrics must be computed after loading the glyph header */
1515      /* since we need the glyph's `yMax' value in case the vertical */
1516      /* metrics must be emulated                                    */
1517      error = tt_get_metrics( loader, glyph_index );
1518      if ( error )
1519        goto Exit;
1520
1521      if ( header_only )
1522        goto Exit;
1523    }
1524
1525    if ( loader->byte_len == 0 || loader->n_contours == 0 )
1526    {
1527      loader->bbox.xMin = 0;
1528      loader->bbox.xMax = 0;
1529      loader->bbox.yMin = 0;
1530      loader->bbox.yMax = 0;
1531
1532      error = tt_get_metrics( loader, glyph_index );
1533      if ( error )
1534        goto Exit;
1535
1536      if ( header_only )
1537        goto Exit;
1538
1539      /* must initialize points before (possibly) overriding */
1540      /* glyph metrics from the incremental interface        */
1541      TT_LOADER_SET_PP( loader );
1542
1543#ifdef FT_CONFIG_OPTION_INCREMENTAL
1544      tt_get_metrics_incr_overrides( loader, glyph_index );
1545#endif
1546
1547#ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
1548
1549      if ( ((TT_Face)(loader->face))->doblend )
1550      {
1551        /* this must be done before scaling */
1552        FT_Memory  memory = loader->face->memory;
1553
1554
1555        error = TT_Vary_Get_Glyph_Deltas( (TT_Face)(loader->face),
1556                                          glyph_index, &deltas, 4 );
1557        if ( error )
1558          goto Exit;
1559
1560        loader->pp1.x += deltas[0].x;
1561        loader->pp1.y += deltas[0].y;
1562        loader->pp2.x += deltas[1].x;
1563        loader->pp2.y += deltas[1].y;
1564
1565        loader->pp3.x += deltas[2].x;
1566        loader->pp3.y += deltas[2].y;
1567        loader->pp4.x += deltas[3].x;
1568        loader->pp4.y += deltas[3].y;
1569
1570        FT_FREE( deltas );
1571      }
1572
1573#endif /* TT_CONFIG_OPTION_GX_VAR_SUPPORT */
1574
1575      /* scale phantom points, if necessary; */
1576      /* they get rounded in `TT_Hint_Glyph' */
1577      if ( ( loader->load_flags & FT_LOAD_NO_SCALE ) == 0 )
1578      {
1579        loader->pp1.x = FT_MulFix( loader->pp1.x, x_scale );
1580        loader->pp2.x = FT_MulFix( loader->pp2.x, x_scale );
1581        /* pp1.y and pp2.y are always zero */
1582
1583        loader->pp3.x = FT_MulFix( loader->pp3.x, x_scale );
1584        loader->pp3.y = FT_MulFix( loader->pp3.y, y_scale );
1585        loader->pp4.x = FT_MulFix( loader->pp4.x, x_scale );
1586        loader->pp4.y = FT_MulFix( loader->pp4.y, y_scale );
1587      }
1588
1589      error = FT_Err_Ok;
1590      goto Exit;
1591    }
1592
1593    /* must initialize phantom points before (possibly) overriding */
1594    /* glyph metrics from the incremental interface                */
1595    TT_LOADER_SET_PP( loader );
1596
1597#ifdef FT_CONFIG_OPTION_INCREMENTAL
1598    tt_get_metrics_incr_overrides( loader, glyph_index );
1599#endif
1600
1601    /***********************************************************************/
1602    /***********************************************************************/
1603    /***********************************************************************/
1604
1605    /* if it is a simple glyph, load it */
1606
1607    if ( loader->n_contours > 0 )
1608    {
1609      error = face->read_simple_glyph( loader );
1610      if ( error )
1611        goto Exit;
1612
1613      /* all data have been read */
1614      face->forget_glyph_frame( loader );
1615      opened_frame = 0;
1616
1617      error = TT_Process_Simple_Glyph( loader );
1618      if ( error )
1619        goto Exit;
1620
1621      FT_GlyphLoader_Add( gloader );
1622    }
1623
1624    /***********************************************************************/
1625    /***********************************************************************/
1626    /***********************************************************************/
1627
1628    /* otherwise, load a composite! */
1629    else if ( loader->n_contours == -1 )
1630    {
1631      FT_UInt   start_point;
1632      FT_UInt   start_contour;
1633      FT_ULong  ins_pos;  /* position of composite instructions, if any */
1634
1635
1636      start_point   = gloader->base.outline.n_points;
1637      start_contour = gloader->base.outline.n_contours;
1638
1639      /* for each subglyph, read composite header */
1640      error = face->read_composite_glyph( loader );
1641      if ( error )
1642        goto Exit;
1643
1644      /* store the offset of instructions */
1645      ins_pos = loader->ins_pos;
1646
1647      /* all data we need are read */
1648      face->forget_glyph_frame( loader );
1649      opened_frame = 0;
1650
1651#ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
1652
1653      if ( face->doblend )
1654      {
1655        FT_Int       i, limit;
1656        FT_SubGlyph  subglyph;
1657        FT_Memory    memory = face->root.memory;
1658
1659
1660        /* this provides additional offsets */
1661        /* for each component's translation */
1662
1663        if ( ( error = TT_Vary_Get_Glyph_Deltas(
1664                         face,
1665                         glyph_index,
1666                         &deltas,
1667                         gloader->current.num_subglyphs + 4 ) ) != 0 )
1668          goto Exit;
1669
1670        subglyph = gloader->current.subglyphs + gloader->base.num_subglyphs;
1671        limit    = gloader->current.num_subglyphs;
1672
1673        for ( i = 0; i < limit; ++i, ++subglyph )
1674        {
1675          if ( subglyph->flags & ARGS_ARE_XY_VALUES )
1676          {
1677            /* XXX: overflow check for subglyph->{arg1,arg2}.   */
1678            /* deltas[i].{x,y} must be within signed 16-bit,    */
1679            /* but the restriction of summed delta is not clear */
1680            subglyph->arg1 += (FT_Int16)deltas[i].x;
1681            subglyph->arg2 += (FT_Int16)deltas[i].y;
1682          }
1683        }
1684
1685        loader->pp1.x += deltas[i + 0].x;
1686        loader->pp1.y += deltas[i + 0].y;
1687        loader->pp2.x += deltas[i + 1].x;
1688        loader->pp2.y += deltas[i + 1].y;
1689
1690        loader->pp3.x += deltas[i + 2].x;
1691        loader->pp3.y += deltas[i + 2].y;
1692        loader->pp4.x += deltas[i + 3].x;
1693        loader->pp4.y += deltas[i + 3].y;
1694
1695        FT_FREE( deltas );
1696      }
1697
1698#endif /* TT_CONFIG_OPTION_GX_VAR_SUPPORT */
1699
1700      /* scale phantom points, if necessary; */
1701      /* they get rounded in `TT_Hint_Glyph' */
1702      if ( ( loader->load_flags & FT_LOAD_NO_SCALE ) == 0 )
1703      {
1704        loader->pp1.x = FT_MulFix( loader->pp1.x, x_scale );
1705        loader->pp2.x = FT_MulFix( loader->pp2.x, x_scale );
1706        /* pp1.y and pp2.y are always zero */
1707
1708        loader->pp3.x = FT_MulFix( loader->pp3.x, x_scale );
1709        loader->pp3.y = FT_MulFix( loader->pp3.y, y_scale );
1710        loader->pp4.x = FT_MulFix( loader->pp4.x, x_scale );
1711        loader->pp4.y = FT_MulFix( loader->pp4.y, y_scale );
1712      }
1713
1714      /* if the flag FT_LOAD_NO_RECURSE is set, we return the subglyph */
1715      /* `as is' in the glyph slot (the client application will be     */
1716      /* responsible for interpreting these data)...                   */
1717      if ( loader->load_flags & FT_LOAD_NO_RECURSE )
1718      {
1719        FT_GlyphLoader_Add( gloader );
1720        loader->glyph->format = FT_GLYPH_FORMAT_COMPOSITE;
1721
1722        goto Exit;
1723      }
1724
1725      /*********************************************************************/
1726      /*********************************************************************/
1727      /*********************************************************************/
1728
1729      {
1730        FT_UInt      n, num_base_points;
1731        FT_SubGlyph  subglyph       = 0;
1732
1733        FT_UInt      num_points     = start_point;
1734        FT_UInt      num_subglyphs  = gloader->current.num_subglyphs;
1735        FT_UInt      num_base_subgs = gloader->base.num_subglyphs;
1736
1737        FT_Stream    old_stream     = loader->stream;
1738        FT_Int       old_byte_len   = loader->byte_len;
1739
1740
1741        FT_GlyphLoader_Add( gloader );
1742
1743        /* read each subglyph independently */
1744        for ( n = 0; n < num_subglyphs; n++ )
1745        {
1746          FT_Vector  pp[4];
1747
1748
1749          /* Each time we call load_truetype_glyph in this loop, the   */
1750          /* value of `gloader.base.subglyphs' can change due to table */
1751          /* reallocations.  We thus need to recompute the subglyph    */
1752          /* pointer on each iteration.                                */
1753          subglyph = gloader->base.subglyphs + num_base_subgs + n;
1754
1755          pp[0] = loader->pp1;
1756          pp[1] = loader->pp2;
1757          pp[2] = loader->pp3;
1758          pp[3] = loader->pp4;
1759
1760          num_base_points = gloader->base.outline.n_points;
1761
1762          error = load_truetype_glyph( loader, subglyph->index,
1763                                       recurse_count + 1, FALSE );
1764          if ( error )
1765            goto Exit;
1766
1767          /* restore subglyph pointer */
1768          subglyph = gloader->base.subglyphs + num_base_subgs + n;
1769
1770          /* restore phantom points if necessary */
1771          if ( !( subglyph->flags & USE_MY_METRICS ) )
1772          {
1773            loader->pp1 = pp[0];
1774            loader->pp2 = pp[1];
1775            loader->pp3 = pp[2];
1776            loader->pp4 = pp[3];
1777          }
1778
1779          num_points = gloader->base.outline.n_points;
1780
1781          if ( num_points == num_base_points )
1782            continue;
1783
1784          /* gloader->base.outline consists of three parts:               */
1785          /* 0 -(1)-> start_point -(2)-> num_base_points -(3)-> n_points. */
1786          /*                                                              */
1787          /* (1): exists from the beginning                               */
1788          /* (2): components that have been loaded so far                 */
1789          /* (3): the newly loaded component                              */
1790          error = TT_Process_Composite_Component( loader,
1791                                                  subglyph,
1792                                                  start_point,
1793                                                  num_base_points );
1794          if ( error )
1795            goto Exit;
1796        }
1797
1798        loader->stream   = old_stream;
1799        loader->byte_len = old_byte_len;
1800
1801        /* process the glyph */
1802        loader->ins_pos = ins_pos;
1803        if ( IS_HINTED( loader->load_flags ) &&
1804#ifdef TT_USE_BYTECODE_INTERPRETER
1805             subglyph->flags & WE_HAVE_INSTR &&
1806#endif
1807             num_points > start_point )
1808        {
1809          error = TT_Process_Composite_Glyph( loader,
1810                                              start_point,
1811                                              start_contour );
1812          if ( error )
1813            goto Exit;
1814        }
1815      }
1816    }
1817    else
1818    {
1819      /* invalid composite count (negative but not -1) */
1820      error = FT_THROW( Invalid_Outline );
1821      goto Exit;
1822    }
1823
1824    /***********************************************************************/
1825    /***********************************************************************/
1826    /***********************************************************************/
1827
1828  Exit:
1829
1830    if ( opened_frame )
1831      face->forget_glyph_frame( loader );
1832
1833#ifdef FT_CONFIG_OPTION_INCREMENTAL
1834
1835    if ( glyph_data_loaded )
1836      face->root.internal->incremental_interface->funcs->free_glyph_data(
1837        face->root.internal->incremental_interface->object,
1838        &glyph_data );
1839
1840#endif
1841
1842    return error;
1843  }
1844
1845
1846  static FT_Error
1847  compute_glyph_metrics( TT_Loader  loader,
1848                         FT_UInt    glyph_index )
1849  {
1850    TT_Face    face   = (TT_Face)loader->face;
1851#ifdef TT_CONFIG_OPTION_SUBPIXEL_HINTING
1852    TT_Driver  driver = (TT_Driver)FT_FACE_DRIVER( face );
1853#endif
1854
1855    FT_BBox       bbox;
1856    FT_Fixed      y_scale;
1857    TT_GlyphSlot  glyph = loader->glyph;
1858    TT_Size       size  = (TT_Size)loader->size;
1859
1860
1861    y_scale = 0x10000L;
1862    if ( ( loader->load_flags & FT_LOAD_NO_SCALE ) == 0 )
1863      y_scale = size->root.metrics.y_scale;
1864
1865    if ( glyph->format != FT_GLYPH_FORMAT_COMPOSITE )
1866      FT_Outline_Get_CBox( &glyph->outline, &bbox );
1867    else
1868      bbox = loader->bbox;
1869
1870    /* get the device-independent horizontal advance; it is scaled later */
1871    /* by the base layer.                                                */
1872    glyph->linearHoriAdvance = loader->linear;
1873
1874    glyph->metrics.horiBearingX = bbox.xMin;
1875    glyph->metrics.horiBearingY = bbox.yMax;
1876    glyph->metrics.horiAdvance  = loader->pp2.x - loader->pp1.x;
1877
1878    /* adjust advance width to the value contained in the hdmx table */
1879    if ( !face->postscript.isFixedPitch  &&
1880         IS_HINTED( loader->load_flags ) )
1881    {
1882      FT_Byte*  widthp;
1883
1884
1885      widthp = tt_face_get_device_metrics( face,
1886                                           size->root.metrics.x_ppem,
1887                                           glyph_index );
1888
1889#ifdef TT_CONFIG_OPTION_SUBPIXEL_HINTING
1890
1891      if ( driver->interpreter_version == TT_INTERPRETER_VERSION_38 )
1892      {
1893        FT_Bool  ignore_x_mode;
1894
1895
1896        ignore_x_mode = FT_BOOL( FT_LOAD_TARGET_MODE( loader->load_flags ) !=
1897                                 FT_RENDER_MODE_MONO );
1898
1899        if ( widthp                                                   &&
1900             ( ( ignore_x_mode && loader->exec->compatible_widths ) ||
1901                !ignore_x_mode                                      ||
1902                SPH_OPTION_BITMAP_WIDTHS                            ) )
1903          glyph->metrics.horiAdvance = *widthp << 6;
1904      }
1905      else
1906
1907#endif /* TT_CONFIG_OPTION_SUBPIXEL_HINTING */
1908
1909      {
1910        if ( widthp )
1911          glyph->metrics.horiAdvance = *widthp << 6;
1912      }
1913    }
1914
1915    /* set glyph dimensions */
1916    glyph->metrics.width  = bbox.xMax - bbox.xMin;
1917    glyph->metrics.height = bbox.yMax - bbox.yMin;
1918
1919    /* Now take care of vertical metrics.  In the case where there is */
1920    /* no vertical information within the font (relatively common),   */
1921    /* create some metrics manually                                   */
1922    {
1923      FT_Pos  top;      /* scaled vertical top side bearing  */
1924      FT_Pos  advance;  /* scaled vertical advance height    */
1925
1926
1927      /* Get the unscaled top bearing and advance height. */
1928      if ( face->vertical_info                   &&
1929           face->vertical.number_Of_VMetrics > 0 )
1930      {
1931        top = (FT_Short)FT_DivFix( loader->pp3.y - bbox.yMax,
1932                                   y_scale );
1933
1934        if ( loader->pp3.y <= loader->pp4.y )
1935          advance = 0;
1936        else
1937          advance = (FT_UShort)FT_DivFix( loader->pp3.y - loader->pp4.y,
1938                                          y_scale );
1939      }
1940      else
1941      {
1942        FT_Pos  height;
1943
1944
1945        /* XXX Compute top side bearing and advance height in  */
1946        /*     Get_VMetrics instead of here.                   */
1947
1948        /* NOTE: The OS/2 values are the only `portable' ones, */
1949        /*       which is why we use them, if there is an OS/2 */
1950        /*       table in the font.  Otherwise, we use the     */
1951        /*       values defined in the horizontal header.      */
1952
1953        height = (FT_Short)FT_DivFix( bbox.yMax - bbox.yMin,
1954                                      y_scale );
1955        if ( face->os2.version != 0xFFFFU )
1956          advance = (FT_Pos)( face->os2.sTypoAscender -
1957                              face->os2.sTypoDescender );
1958        else
1959          advance = (FT_Pos)( face->horizontal.Ascender -
1960                              face->horizontal.Descender );
1961
1962        top = ( advance - height ) / 2;
1963      }
1964
1965#ifdef FT_CONFIG_OPTION_INCREMENTAL
1966      {
1967        FT_Incremental_InterfaceRec*  incr;
1968        FT_Incremental_MetricsRec     metrics;
1969        FT_Error                      error;
1970
1971
1972        incr = face->root.internal->incremental_interface;
1973
1974        /* If this is an incrementally loaded font see if there are */
1975        /* overriding metrics for this glyph.                       */
1976        if ( incr && incr->funcs->get_glyph_metrics )
1977        {
1978          metrics.bearing_x = 0;
1979          metrics.bearing_y = top;
1980          metrics.advance   = advance;
1981
1982          error = incr->funcs->get_glyph_metrics( incr->object,
1983                                                  glyph_index,
1984                                                  TRUE,
1985                                                  &metrics );
1986          if ( error )
1987            return error;
1988
1989          top     = metrics.bearing_y;
1990          advance = metrics.advance;
1991        }
1992      }
1993
1994      /* GWW: Do vertical metrics get loaded incrementally too? */
1995
1996#endif /* FT_CONFIG_OPTION_INCREMENTAL */
1997
1998      glyph->linearVertAdvance = advance;
1999
2000      /* scale the metrics */
2001      if ( !( loader->load_flags & FT_LOAD_NO_SCALE ) )
2002      {
2003        top     = FT_MulFix( top,     y_scale );
2004        advance = FT_MulFix( advance, y_scale );
2005      }
2006
2007      /* XXX: for now, we have no better algorithm for the lsb, but it */
2008      /*      should work fine.                                        */
2009      /*                                                               */
2010      glyph->metrics.vertBearingX = glyph->metrics.horiBearingX -
2011                                      glyph->metrics.horiAdvance / 2;
2012      glyph->metrics.vertBearingY = top;
2013      glyph->metrics.vertAdvance  = advance;
2014    }
2015
2016    return 0;
2017  }
2018
2019
2020#ifdef TT_CONFIG_OPTION_EMBEDDED_BITMAPS
2021
2022  static FT_Error
2023  load_sbit_image( TT_Size       size,
2024                   TT_GlyphSlot  glyph,
2025                   FT_UInt       glyph_index,
2026                   FT_Int32      load_flags )
2027  {
2028    TT_Face             face;
2029    SFNT_Service        sfnt;
2030    FT_Stream           stream;
2031    FT_Error            error;
2032    TT_SBit_MetricsRec  metrics;
2033
2034
2035    face   = (TT_Face)glyph->face;
2036    sfnt   = (SFNT_Service)face->sfnt;
2037    stream = face->root.stream;
2038
2039    error = sfnt->load_sbit_image( face,
2040                                   size->strike_index,
2041                                   glyph_index,
2042                                   (FT_Int)load_flags,
2043                                   stream,
2044                                   &glyph->bitmap,
2045                                   &metrics );
2046    if ( !error )
2047    {
2048      glyph->outline.n_points   = 0;
2049      glyph->outline.n_contours = 0;
2050
2051      glyph->metrics.width  = (FT_Pos)metrics.width  << 6;
2052      glyph->metrics.height = (FT_Pos)metrics.height << 6;
2053
2054      glyph->metrics.horiBearingX = (FT_Pos)metrics.horiBearingX << 6;
2055      glyph->metrics.horiBearingY = (FT_Pos)metrics.horiBearingY << 6;
2056      glyph->metrics.horiAdvance  = (FT_Pos)metrics.horiAdvance  << 6;
2057
2058      glyph->metrics.vertBearingX = (FT_Pos)metrics.vertBearingX << 6;
2059      glyph->metrics.vertBearingY = (FT_Pos)metrics.vertBearingY << 6;
2060      glyph->metrics.vertAdvance  = (FT_Pos)metrics.vertAdvance  << 6;
2061
2062      glyph->format = FT_GLYPH_FORMAT_BITMAP;
2063
2064      if ( load_flags & FT_LOAD_VERTICAL_LAYOUT )
2065      {
2066        glyph->bitmap_left = metrics.vertBearingX;
2067        glyph->bitmap_top  = metrics.vertBearingY;
2068      }
2069      else
2070      {
2071        glyph->bitmap_left = metrics.horiBearingX;
2072        glyph->bitmap_top  = metrics.horiBearingY;
2073      }
2074    }
2075
2076    return error;
2077  }
2078
2079#endif /* TT_CONFIG_OPTION_EMBEDDED_BITMAPS */
2080
2081
2082  static FT_Error
2083  tt_loader_init( TT_Loader     loader,
2084                  TT_Size       size,
2085                  TT_GlyphSlot  glyph,
2086                  FT_Int32      load_flags,
2087                  FT_Bool       glyf_table_only )
2088  {
2089    FT_Error  error;
2090
2091    TT_Face    face;
2092    FT_Stream  stream;
2093#ifdef TT_USE_BYTECODE_INTERPRETER
2094    FT_Bool    pedantic = FT_BOOL( load_flags & FT_LOAD_PEDANTIC );
2095#endif
2096
2097
2098    face   = (TT_Face)glyph->face;
2099    stream = face->root.stream;
2100
2101    FT_MEM_ZERO( loader, sizeof ( TT_LoaderRec ) );
2102
2103#ifdef TT_USE_BYTECODE_INTERPRETER
2104
2105    /* load execution context */
2106    if ( IS_HINTED( load_flags ) && !glyf_table_only )
2107    {
2108      TT_ExecContext  exec;
2109      FT_Bool         grayscale;
2110
2111#ifdef TT_CONFIG_OPTION_SUBPIXEL_HINTING
2112      TT_Driver  driver = (TT_Driver)FT_FACE_DRIVER( face );
2113
2114      FT_Bool  subpixel = FALSE;
2115
2116#if 0
2117      /* not used yet */
2118      FT_Bool  compatible_widths;
2119      FT_Bool  symmetrical_smoothing;
2120      FT_Bool  bgr;
2121      FT_Bool  subpixel_positioned;
2122#endif
2123#endif /* TT_CONFIG_OPTION_SUBPIXEL_HINTING */
2124
2125      FT_Bool  reexecute = FALSE;
2126
2127
2128      if ( size->bytecode_ready < 0 || size->cvt_ready < 0 )
2129      {
2130        error = tt_size_ready_bytecode( size, pedantic );
2131        if ( error )
2132          return error;
2133      }
2134      else if ( size->bytecode_ready )
2135        return size->bytecode_ready;
2136      else if ( size->cvt_ready )
2137        return size->cvt_ready;
2138
2139      /* query new execution context */
2140      exec = size->debug ? size->context
2141                         : ( (TT_Driver)FT_FACE_DRIVER( face ) )->context;
2142      if ( !exec )
2143        return FT_THROW( Could_Not_Find_Context );
2144
2145#ifdef TT_CONFIG_OPTION_SUBPIXEL_HINTING
2146
2147      if ( driver->interpreter_version == TT_INTERPRETER_VERSION_38 )
2148      {
2149        subpixel = FT_BOOL( ( FT_LOAD_TARGET_MODE( load_flags ) !=
2150                              FT_RENDER_MODE_MONO               )  &&
2151                            SPH_OPTION_SET_SUBPIXEL                );
2152
2153        if ( subpixel )
2154          grayscale = FALSE;
2155        else if ( SPH_OPTION_SET_GRAYSCALE )
2156        {
2157          grayscale = TRUE;
2158          subpixel  = FALSE;
2159        }
2160        else
2161          grayscale = FALSE;
2162
2163        if ( FT_IS_TRICKY( glyph->face ) )
2164          subpixel = FALSE;
2165
2166        exec->ignore_x_mode      = subpixel || grayscale;
2167        exec->rasterizer_version = SPH_OPTION_SET_RASTERIZER_VERSION;
2168        if ( exec->sph_tweak_flags & SPH_TWEAK_RASTERIZER_35 )
2169          exec->rasterizer_version = TT_INTERPRETER_VERSION_35;
2170
2171#if 1
2172        exec->compatible_widths     = SPH_OPTION_SET_COMPATIBLE_WIDTHS;
2173        exec->symmetrical_smoothing = FALSE;
2174        exec->bgr                   = FALSE;
2175        exec->subpixel_positioned   = TRUE;
2176#else /* 0 */
2177        exec->compatible_widths =
2178          FT_BOOL( FT_LOAD_TARGET_MODE( load_flags ) !=
2179                   TT_LOAD_COMPATIBLE_WIDTHS );
2180        exec->symmetrical_smoothing =
2181          FT_BOOL( FT_LOAD_TARGET_MODE( load_flags ) !=
2182                   TT_LOAD_SYMMETRICAL_SMOOTHING );
2183        exec->bgr =
2184          FT_BOOL( FT_LOAD_TARGET_MODE( load_flags ) !=
2185                   TT_LOAD_BGR );
2186        exec->subpixel_positioned =
2187          FT_BOOL( FT_LOAD_TARGET_MODE( load_flags ) !=
2188                   TT_LOAD_SUBPIXEL_POSITIONED );
2189#endif /* 0 */
2190
2191      }
2192      else
2193
2194#endif /* TT_CONFIG_OPTION_SUBPIXEL_HINTING */
2195
2196      {
2197        grayscale = FT_BOOL( FT_LOAD_TARGET_MODE( load_flags ) !=
2198                             FT_RENDER_MODE_MONO );
2199      }
2200
2201      error = TT_Load_Context( exec, face, size );
2202      if ( error )
2203        return error;
2204
2205#ifdef TT_CONFIG_OPTION_SUBPIXEL_HINTING
2206
2207      if ( driver->interpreter_version == TT_INTERPRETER_VERSION_38 )
2208      {
2209        /* a change from mono to subpixel rendering (and vice versa) */
2210        /* requires a re-execution of the CVT program                */
2211        if ( subpixel != exec->subpixel )
2212        {
2213          FT_TRACE4(( "tt_loader_init: subpixel hinting change,"
2214                      " re-executing `prep' table\n" ));
2215
2216          exec->subpixel = subpixel;
2217          reexecute      = TRUE;
2218        }
2219
2220        /* a change from mono to grayscale rendering (and vice versa) */
2221        /* requires a re-execution of the CVT program                 */
2222        if ( grayscale != exec->grayscale )
2223        {
2224          FT_TRACE4(( "tt_loader_init: grayscale hinting change,"
2225                      " re-executing `prep' table\n" ));
2226
2227          exec->grayscale = grayscale;
2228          reexecute       = TRUE;
2229        }
2230      }
2231      else
2232
2233#endif /* TT_CONFIG_OPTION_SUBPIXEL_HINTING */
2234
2235      {
2236        /* a change from mono to grayscale rendering (and vice versa) */
2237        /* requires a re-execution of the CVT program                 */
2238        if ( grayscale != exec->grayscale )
2239        {
2240          FT_TRACE4(( "tt_loader_init: grayscale change,"
2241                      " re-executing `prep' table\n" ));
2242
2243          exec->grayscale = grayscale;
2244          reexecute       = TRUE;
2245        }
2246      }
2247
2248      if ( reexecute )
2249      {
2250        FT_UInt  i;
2251
2252
2253        for ( i = 0; i < size->cvt_size; i++ )
2254          size->cvt[i] = FT_MulFix( face->cvt[i], size->ttmetrics.scale );
2255        error = tt_size_run_prep( size, pedantic );
2256        if ( error )
2257          return error;
2258      }
2259
2260      /* see whether the cvt program has disabled hinting */
2261      if ( exec->GS.instruct_control & 1 )
2262        load_flags |= FT_LOAD_NO_HINTING;
2263
2264      /* load default graphics state -- if needed */
2265      if ( exec->GS.instruct_control & 2 )
2266        exec->GS = tt_default_graphics_state;
2267
2268      exec->pedantic_hinting = FT_BOOL( load_flags & FT_LOAD_PEDANTIC );
2269      loader->exec = exec;
2270      loader->instructions = exec->glyphIns;
2271    }
2272
2273#endif /* TT_USE_BYTECODE_INTERPRETER */
2274
2275    /* seek to the beginning of the glyph table -- for Type 42 fonts     */
2276    /* the table might be accessed from a Postscript stream or something */
2277    /* else...                                                           */
2278
2279#ifdef FT_CONFIG_OPTION_INCREMENTAL
2280
2281    if ( face->root.internal->incremental_interface )
2282      loader->glyf_offset = 0;
2283    else
2284
2285#endif
2286
2287    {
2288      error = face->goto_table( face, TTAG_glyf, stream, 0 );
2289
2290      if ( FT_ERR_EQ( error, Table_Missing ) )
2291        loader->glyf_offset = 0;
2292      else if ( error )
2293      {
2294        FT_ERROR(( "tt_loader_init: could not access glyph table\n" ));
2295        return error;
2296      }
2297      else
2298        loader->glyf_offset = FT_STREAM_POS();
2299    }
2300
2301    /* get face's glyph loader */
2302    if ( !glyf_table_only )
2303    {
2304      FT_GlyphLoader  gloader = glyph->internal->loader;
2305
2306
2307      FT_GlyphLoader_Rewind( gloader );
2308      loader->gloader = gloader;
2309    }
2310
2311    loader->load_flags = load_flags;
2312
2313    loader->face   = (FT_Face)face;
2314    loader->size   = (FT_Size)size;
2315    loader->glyph  = (FT_GlyphSlot)glyph;
2316    loader->stream = stream;
2317
2318    return FT_Err_Ok;
2319  }
2320
2321
2322  /*************************************************************************/
2323  /*                                                                       */
2324  /* <Function>                                                            */
2325  /*    TT_Load_Glyph                                                      */
2326  /*                                                                       */
2327  /* <Description>                                                         */
2328  /*    A function used to load a single glyph within a given glyph slot,  */
2329  /*    for a given size.                                                  */
2330  /*                                                                       */
2331  /* <Input>                                                               */
2332  /*    glyph       :: A handle to a target slot object where the glyph    */
2333  /*                   will be loaded.                                     */
2334  /*                                                                       */
2335  /*    size        :: A handle to the source face size at which the glyph */
2336  /*                   must be scaled/loaded.                              */
2337  /*                                                                       */
2338  /*    glyph_index :: The index of the glyph in the font file.            */
2339  /*                                                                       */
2340  /*    load_flags  :: A flag indicating what to load for this glyph.  The */
2341  /*                   FT_LOAD_XXX constants can be used to control the    */
2342  /*                   glyph loading process (e.g., whether the outline    */
2343  /*                   should be scaled, whether to load bitmaps or not,   */
2344  /*                   whether to hint the outline, etc).                  */
2345  /*                                                                       */
2346  /* <Return>                                                              */
2347  /*    FreeType error code.  0 means success.                             */
2348  /*                                                                       */
2349  FT_LOCAL_DEF( FT_Error )
2350  TT_Load_Glyph( TT_Size       size,
2351                 TT_GlyphSlot  glyph,
2352                 FT_UInt       glyph_index,
2353                 FT_Int32      load_flags )
2354  {
2355    FT_Error      error;
2356    TT_LoaderRec  loader;
2357
2358
2359    FT_TRACE1(( "TT_Load_Glyph: glyph index %d\n", glyph_index ));
2360
2361#ifdef TT_CONFIG_OPTION_EMBEDDED_BITMAPS
2362
2363    /* try to load embedded bitmap if any              */
2364    /*                                                 */
2365    /* XXX: The convention should be emphasized in     */
2366    /*      the documents because it can be confusing. */
2367    if ( size->strike_index != 0xFFFFFFFFUL      &&
2368         ( load_flags & FT_LOAD_NO_BITMAP ) == 0 )
2369    {
2370      error = load_sbit_image( size, glyph, glyph_index, load_flags );
2371      if ( !error )
2372      {
2373        if ( FT_IS_SCALABLE( glyph->face ) )
2374        {
2375          /* for the bbox we need the header only */
2376          (void)tt_loader_init( &loader, size, glyph, load_flags, TRUE );
2377          (void)load_truetype_glyph( &loader, glyph_index, 0, TRUE );
2378          glyph->linearHoriAdvance = loader.linear;
2379          glyph->linearVertAdvance = loader.vadvance;
2380
2381          /* sanity checks: if `xxxAdvance' in the sbit metric */
2382          /* structure isn't set, use `linearXXXAdvance'      */
2383          if ( !glyph->metrics.horiAdvance && glyph->linearHoriAdvance )
2384            glyph->metrics.horiAdvance =
2385              FT_MulFix( glyph->linearHoriAdvance,
2386                         size->root.metrics.x_scale );
2387          if ( !glyph->metrics.vertAdvance && glyph->linearVertAdvance )
2388            glyph->metrics.vertAdvance =
2389              FT_MulFix( glyph->linearVertAdvance,
2390                         size->root.metrics.y_scale );
2391        }
2392
2393        return FT_Err_Ok;
2394      }
2395    }
2396
2397#endif /* TT_CONFIG_OPTION_EMBEDDED_BITMAPS */
2398
2399    /* if FT_LOAD_NO_SCALE is not set, `ttmetrics' must be valid */
2400    if ( !( load_flags & FT_LOAD_NO_SCALE ) && !size->ttmetrics.valid )
2401      return FT_THROW( Invalid_Size_Handle );
2402
2403    if ( load_flags & FT_LOAD_SBITS_ONLY )
2404      return FT_THROW( Invalid_Argument );
2405
2406    error = tt_loader_init( &loader, size, glyph, load_flags, FALSE );
2407    if ( error )
2408      return error;
2409
2410    glyph->format        = FT_GLYPH_FORMAT_OUTLINE;
2411    glyph->num_subglyphs = 0;
2412    glyph->outline.flags = 0;
2413
2414    /* main loading loop */
2415    error = load_truetype_glyph( &loader, glyph_index, 0, FALSE );
2416    if ( !error )
2417    {
2418      if ( glyph->format == FT_GLYPH_FORMAT_COMPOSITE )
2419      {
2420        glyph->num_subglyphs = loader.gloader->base.num_subglyphs;
2421        glyph->subglyphs     = loader.gloader->base.subglyphs;
2422      }
2423      else
2424      {
2425        glyph->outline        = loader.gloader->base.outline;
2426        glyph->outline.flags &= ~FT_OUTLINE_SINGLE_PASS;
2427
2428        /* Translate array so that (0,0) is the glyph's origin.  Note  */
2429        /* that this behaviour is independent on the value of bit 1 of */
2430        /* the `flags' field in the `head' table -- at least major     */
2431        /* applications like Acroread indicate that.                   */
2432        if ( loader.pp1.x )
2433          FT_Outline_Translate( &glyph->outline, -loader.pp1.x, 0 );
2434      }
2435
2436#ifdef TT_USE_BYTECODE_INTERPRETER
2437
2438      if ( IS_HINTED( load_flags ) )
2439      {
2440        if ( loader.exec->GS.scan_control )
2441        {
2442          /* convert scan conversion mode to FT_OUTLINE_XXX flags */
2443          switch ( loader.exec->GS.scan_type )
2444          {
2445          case 0: /* simple drop-outs including stubs */
2446            glyph->outline.flags |= FT_OUTLINE_INCLUDE_STUBS;
2447            break;
2448          case 1: /* simple drop-outs excluding stubs */
2449            /* nothing; it's the default rendering mode */
2450            break;
2451          case 4: /* smart drop-outs including stubs */
2452            glyph->outline.flags |= FT_OUTLINE_SMART_DROPOUTS |
2453                                    FT_OUTLINE_INCLUDE_STUBS;
2454            break;
2455          case 5: /* smart drop-outs excluding stubs  */
2456            glyph->outline.flags |= FT_OUTLINE_SMART_DROPOUTS;
2457            break;
2458
2459          default: /* no drop-out control */
2460            glyph->outline.flags |= FT_OUTLINE_IGNORE_DROPOUTS;
2461            break;
2462          }
2463        }
2464        else
2465          glyph->outline.flags |= FT_OUTLINE_IGNORE_DROPOUTS;
2466      }
2467
2468#endif /* TT_USE_BYTECODE_INTERPRETER */
2469
2470      error = compute_glyph_metrics( &loader, glyph_index );
2471    }
2472
2473    /* Set the `high precision' bit flag.                           */
2474    /* This is _critical_ to get correct output for monochrome      */
2475    /* TrueType glyphs at all sizes using the bytecode interpreter. */
2476    /*                                                              */
2477    if ( !( load_flags & FT_LOAD_NO_SCALE ) &&
2478         size->root.metrics.y_ppem < 24     )
2479      glyph->outline.flags |= FT_OUTLINE_HIGH_PRECISION;
2480
2481    return error;
2482  }
2483
2484
2485/* END */
2486