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