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