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