ttgload.c revision 3033f4366b68024ca56f53a53e568e1805436446
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 haven't this field set correctly, */
1088    /* thus we add 1 to catch the majority of them  */
1089    if ( recurse_count > (FT_UInt)face->max_profile.maxComponentDepth + 1 )
1090    {
1091      error = TT_Err_Invalid_Composite;
1092      goto Exit;
1093    }
1094
1095    /* check glyph index */
1096    if ( glyph_index >= (FT_UInt)face->root.num_glyphs )
1097    {
1098      error = TT_Err_Invalid_Glyph_Index;
1099      goto Exit;
1100    }
1101
1102    loader->glyph_index = glyph_index;
1103
1104    if ( ( loader->load_flags & FT_LOAD_NO_SCALE ) == 0 )
1105    {
1106      x_scale = ((TT_Size)loader->size)->metrics.x_scale;
1107      y_scale = ((TT_Size)loader->size)->metrics.y_scale;
1108    }
1109    else
1110    {
1111      x_scale = 0x10000L;
1112      y_scale = 0x10000L;
1113    }
1114
1115    /* get metrics, horizontal and vertical */
1116    {
1117      FT_Short   left_bearing = 0, top_bearing = 0;
1118      FT_UShort  advance_width = 0, advance_height = 0;
1119
1120
1121      Get_HMetrics( face, glyph_index,
1122                    (FT_Bool)!( loader->load_flags &
1123                                FT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH ),
1124                    &left_bearing,
1125                    &advance_width );
1126      Get_VMetrics( face, glyph_index,
1127                    (FT_Bool)!( loader->load_flags &
1128                                FT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH ),
1129                    &top_bearing,
1130                    &advance_height );
1131
1132#ifdef FT_CONFIG_OPTION_INCREMENTAL
1133
1134      /* If this is an incrementally loaded font see if there are */
1135      /* overriding metrics for this glyph.                       */
1136      if ( face->root.internal->incremental_interface &&
1137           face->root.internal->incremental_interface->funcs->get_glyph_metrics )
1138      {
1139        FT_Incremental_MetricsRec  metrics;
1140
1141
1142        metrics.bearing_x = left_bearing;
1143        metrics.bearing_y = 0;
1144        metrics.advance = advance_width;
1145        error = face->root.internal->incremental_interface->funcs->get_glyph_metrics(
1146                  face->root.internal->incremental_interface->object,
1147                  glyph_index, FALSE, &metrics );
1148        if ( error )
1149          goto Exit;
1150        left_bearing  = (FT_Short)metrics.bearing_x;
1151        advance_width = (FT_UShort)metrics.advance;
1152
1153#if 0
1154
1155        /* GWW: Do I do the same for vertical metrics? */
1156        metrics.bearing_x = 0;
1157        metrics.bearing_y = top_bearing;
1158        metrics.advance = advance_height;
1159        error = face->root.internal->incremental_interface->funcs->get_glyph_metrics(
1160                  face->root.internal->incremental_interface->object,
1161                  glyph_index, TRUE, &metrics );
1162        if ( error )
1163          goto Exit;
1164        top_bearing  = (FT_Short)metrics.bearing_y;
1165        advance_height = (FT_UShort)metrics.advance;
1166
1167#endif /* 0 */
1168
1169      }
1170
1171#endif /* FT_CONFIG_OPTION_INCREMENTAL */
1172
1173      loader->left_bearing = left_bearing;
1174      loader->advance      = advance_width;
1175      loader->top_bearing  = top_bearing;
1176      loader->vadvance     = advance_height;
1177
1178      if ( !loader->linear_def )
1179      {
1180        loader->linear_def = 1;
1181        loader->linear     = advance_width;
1182      }
1183    }
1184
1185    /* Set `offset' to the start of the glyph relative to the start of */
1186    /* the `glyf' table, and `byte_len' to the length of the glyph in  */
1187    /* bytes.                                                          */
1188
1189#ifdef FT_CONFIG_OPTION_INCREMENTAL
1190
1191    /* If we are loading glyph data via the incremental interface, set */
1192    /* the loader stream to a memory stream reading the data returned  */
1193    /* by the interface.                                               */
1194    if ( face->root.internal->incremental_interface )
1195    {
1196      error = face->root.internal->incremental_interface->funcs->get_glyph_data(
1197                face->root.internal->incremental_interface->object,
1198                glyph_index, &glyph_data );
1199      if ( error )
1200        goto Exit;
1201
1202      glyph_data_loaded = 1;
1203      offset            = 0;
1204      loader->byte_len  = glyph_data.length;
1205
1206      FT_MEM_ZERO( &inc_stream, sizeof ( inc_stream ) );
1207      FT_Stream_OpenMemory( &inc_stream,
1208                            glyph_data.pointer, glyph_data.length );
1209
1210      loader->stream = &inc_stream;
1211    }
1212    else
1213
1214#endif /* FT_CONFIG_OPTION_INCREMENTAL */
1215
1216      offset = tt_face_get_location( face, glyph_index,
1217                                     (FT_UInt*)&loader->byte_len );
1218
1219    if ( loader->byte_len == 0 )
1220    {
1221      /* as described by Frederic Loyer, these are spaces or */
1222      /* the unknown glyph.                                  */
1223      loader->bbox.xMin = 0;
1224      loader->bbox.xMax = 0;
1225      loader->bbox.yMin = 0;
1226      loader->bbox.yMax = 0;
1227
1228      TT_LOADER_SET_PP( loader );
1229
1230#ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
1231
1232      if ( ((TT_Face)(loader->face))->doblend )
1233      {
1234        /* this must be done before scaling */
1235        FT_Memory  memory = loader->face->memory;
1236
1237
1238        error = TT_Vary_Get_Glyph_Deltas( (TT_Face)(loader->face),
1239                                          glyph_index, &deltas, 4 );
1240        if ( error )
1241          goto Exit;
1242
1243        loader->pp1.x += deltas[0].x; loader->pp1.y += deltas[0].y;
1244        loader->pp2.x += deltas[1].x; loader->pp2.y += deltas[1].y;
1245        loader->pp3.x += deltas[2].x; loader->pp3.y += deltas[2].y;
1246        loader->pp4.x += deltas[3].x; loader->pp4.y += deltas[3].y;
1247
1248        FT_FREE( deltas );
1249      }
1250
1251#endif
1252
1253      if ( ( loader->load_flags & FT_LOAD_NO_SCALE ) == 0 )
1254      {
1255        loader->pp1.x = FT_MulFix( loader->pp1.x, x_scale );
1256        loader->pp2.x = FT_MulFix( loader->pp2.x, x_scale );
1257        loader->pp3.y = FT_MulFix( loader->pp3.y, y_scale );
1258        loader->pp4.y = FT_MulFix( loader->pp4.y, y_scale );
1259      }
1260
1261      error = TT_Err_Ok;
1262      goto Exit;
1263    }
1264
1265    error = face->access_glyph_frame( loader, glyph_index,
1266                                      loader->glyf_offset + offset,
1267                                      loader->byte_len );
1268    if ( error )
1269      goto Exit;
1270
1271    opened_frame = 1;
1272
1273    /* read first glyph header */
1274    error = face->read_glyph_header( loader );
1275    if ( error )
1276      goto Exit;
1277
1278    TT_LOADER_SET_PP( loader );
1279
1280    /***********************************************************************/
1281    /***********************************************************************/
1282    /***********************************************************************/
1283
1284    /* if it is a simple glyph, load it */
1285
1286    if ( loader->n_contours >= 0 )
1287    {
1288      error = face->read_simple_glyph( loader );
1289      if ( error )
1290        goto Exit;
1291
1292      /* all data have been read */
1293      face->forget_glyph_frame( loader );
1294      opened_frame = 0;
1295
1296      error = TT_Process_Simple_Glyph( loader );
1297      if ( error )
1298        goto Exit;
1299
1300      FT_GlyphLoader_Add( gloader );
1301    }
1302
1303    /***********************************************************************/
1304    /***********************************************************************/
1305    /***********************************************************************/
1306
1307    /* otherwise, load a composite! */
1308    else if ( loader->n_contours == -1 )
1309    {
1310      FT_UInt   start_point;
1311      FT_UInt   start_contour;
1312      FT_ULong  ins_pos;  /* position of composite instructions, if any */
1313
1314
1315      start_point   = gloader->base.outline.n_points;
1316      start_contour = gloader->base.outline.n_contours;
1317
1318      /* for each subglyph, read composite header */
1319      error = face->read_composite_glyph( loader );
1320      if ( error )
1321        goto Exit;
1322
1323      /* store the offset of instructions */
1324      ins_pos = loader->ins_pos;
1325
1326      /* all data we need are read */
1327      face->forget_glyph_frame( loader );
1328      opened_frame = 0;
1329
1330#ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
1331
1332      if ( face->doblend )
1333      {
1334        FT_Int       i, limit;
1335        FT_SubGlyph  subglyph;
1336        FT_Memory    memory = face->root.memory;
1337
1338
1339        /* this provides additional offsets */
1340        /* for each component's translation */
1341
1342        if ( ( error = TT_Vary_Get_Glyph_Deltas(
1343                         face,
1344                         glyph_index,
1345                         &deltas,
1346                         gloader->current.num_subglyphs + 4 )) != 0 )
1347          goto Exit;
1348
1349        subglyph = gloader->current.subglyphs + gloader->base.num_subglyphs;
1350        limit    = gloader->current.num_subglyphs;
1351
1352        for ( i = 0; i < limit; ++i, ++subglyph )
1353        {
1354          if ( subglyph->flags & ARGS_ARE_XY_VALUES )
1355          {
1356            subglyph->arg1 += deltas[i].x;
1357            subglyph->arg2 += deltas[i].y;
1358          }
1359        }
1360
1361        loader->pp1.x += deltas[i + 0].x; loader->pp1.y += deltas[i + 0].y;
1362        loader->pp2.x += deltas[i + 1].x; loader->pp2.y += deltas[i + 1].y;
1363        loader->pp3.x += deltas[i + 2].x; loader->pp3.y += deltas[i + 2].y;
1364        loader->pp4.x += deltas[i + 3].x; loader->pp4.y += deltas[i + 3].y;
1365
1366        FT_FREE( deltas );
1367      }
1368
1369#endif /* TT_CONFIG_OPTION_GX_VAR_SUPPORT */
1370
1371      if ( ( loader->load_flags & FT_LOAD_NO_SCALE ) == 0 )
1372      {
1373        loader->pp1.x = FT_MulFix( loader->pp1.x, x_scale );
1374        loader->pp2.x = FT_MulFix( loader->pp2.x, x_scale );
1375        loader->pp3.y = FT_MulFix( loader->pp3.y, y_scale );
1376        loader->pp4.y = FT_MulFix( loader->pp4.y, y_scale );
1377      }
1378
1379      /* if the flag FT_LOAD_NO_RECURSE is set, we return the subglyph */
1380      /* `as is' in the glyph slot (the client application will be     */
1381      /* responsible for interpreting these data)...                   */
1382      if ( loader->load_flags & FT_LOAD_NO_RECURSE )
1383      {
1384        FT_GlyphLoader_Add( gloader );
1385        loader->glyph->format = FT_GLYPH_FORMAT_COMPOSITE;
1386
1387        goto Exit;
1388      }
1389
1390      /*********************************************************************/
1391      /*********************************************************************/
1392      /*********************************************************************/
1393
1394      {
1395        FT_UInt           n, num_base_points;
1396        FT_SubGlyph       subglyph       = 0;
1397
1398        FT_UInt           num_points     = start_point;
1399        FT_UInt           num_subglyphs  = gloader->current.num_subglyphs;
1400        FT_UInt           num_base_subgs = gloader->base.num_subglyphs;
1401
1402        FT_Stream         old_stream     = loader->stream;
1403
1404        TT_GraphicsState  saved_GS;
1405
1406
1407        if ( loader->exec )
1408          saved_GS = loader->exec->GS;
1409
1410        FT_GlyphLoader_Add( gloader );
1411
1412        /* read each subglyph independently */
1413        for ( n = 0; n < num_subglyphs; n++ )
1414        {
1415          FT_Vector  pp[4];
1416
1417
1418          /* reinitialize graphics state */
1419          if ( loader->exec )
1420            loader->exec->GS = saved_GS;
1421
1422          /* Each time we call load_truetype_glyph in this loop, the   */
1423          /* value of `gloader.base.subglyphs' can change due to table */
1424          /* reallocations.  We thus need to recompute the subglyph    */
1425          /* pointer on each iteration.                                */
1426          subglyph = gloader->base.subglyphs + num_base_subgs + n;
1427
1428          pp[0] = loader->pp1;
1429          pp[1] = loader->pp2;
1430          pp[2] = loader->pp3;
1431          pp[3] = loader->pp4;
1432
1433          num_base_points = gloader->base.outline.n_points;
1434
1435          error = load_truetype_glyph( loader, subglyph->index,
1436                                       recurse_count + 1 );
1437          if ( error )
1438            goto Exit;
1439
1440          /* restore subglyph pointer */
1441          subglyph = gloader->base.subglyphs + num_base_subgs + n;
1442
1443          if ( !( subglyph->flags & USE_MY_METRICS ) )
1444          {
1445            loader->pp1 = pp[0];
1446            loader->pp2 = pp[1];
1447            loader->pp3 = pp[2];
1448            loader->pp4 = pp[3];
1449          }
1450
1451          num_points = gloader->base.outline.n_points;
1452
1453          if ( num_points == num_base_points )
1454            continue;
1455
1456          /* gloader->base.outline consists of three parts:               */
1457          /* 0 -(1)-> start_point -(2)-> num_base_points -(3)-> n_points. */
1458          /*                                                              */
1459          /* (1): exists from the beginning                               */
1460          /* (2): components that have been loaded so far                 */
1461          /* (3): the newly loaded component                              */
1462          TT_Process_Composite_Component( loader, subglyph, start_point,
1463                                          num_base_points );
1464        }
1465
1466        loader->stream = old_stream;
1467
1468        /* process the glyph */
1469        loader->ins_pos = ins_pos;
1470        if ( IS_HINTED( loader->load_flags ) &&
1471
1472#ifdef TT_USE_BYTECODE_INTERPRETER
1473
1474             subglyph->flags & WE_HAVE_INSTR &&
1475
1476#endif
1477
1478             num_points > start_point )
1479          TT_Process_Composite_Glyph( loader, start_point, start_contour );
1480
1481      }
1482    }
1483    else
1484    {
1485      /* invalid composite count (negative but not -1) */
1486      error = TT_Err_Invalid_Outline;
1487      goto Exit;
1488    }
1489
1490    /***********************************************************************/
1491    /***********************************************************************/
1492    /***********************************************************************/
1493
1494  Exit:
1495
1496    if ( opened_frame )
1497      face->forget_glyph_frame( loader );
1498
1499#ifdef FT_CONFIG_OPTION_INCREMENTAL
1500
1501    if ( glyph_data_loaded )
1502      face->root.internal->incremental_interface->funcs->free_glyph_data(
1503        face->root.internal->incremental_interface->object,
1504        &glyph_data );
1505
1506#endif
1507
1508    return error;
1509  }
1510
1511
1512  static FT_Error
1513  compute_glyph_metrics( TT_Loader  loader,
1514                         FT_UInt    glyph_index )
1515  {
1516    FT_BBox       bbox;
1517    TT_Face       face = (TT_Face)loader->face;
1518    FT_Fixed      y_scale;
1519    TT_GlyphSlot  glyph = loader->glyph;
1520    TT_Size       size = (TT_Size)loader->size;
1521
1522
1523    y_scale = 0x10000L;
1524    if ( ( loader->load_flags & FT_LOAD_NO_SCALE ) == 0 )
1525      y_scale = size->root.metrics.y_scale;
1526
1527    if ( glyph->format != FT_GLYPH_FORMAT_COMPOSITE )
1528      FT_Outline_Get_CBox( &glyph->outline, &bbox );
1529    else
1530      bbox = loader->bbox;
1531
1532    /* get the device-independent horizontal advance; it is scaled later */
1533    /* by the base layer.                                                */
1534    {
1535      FT_Pos  advance = loader->linear;
1536
1537
1538      /* the flag FT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH was introduced to */
1539      /* correctly support DynaLab fonts, which have an incorrect       */
1540      /* `advance_Width_Max' field!  It is used, to my knowledge,       */
1541      /* exclusively in the X-TrueType font server.                     */
1542      /*                                                                */
1543      if ( face->postscript.isFixedPitch                                     &&
1544           ( loader->load_flags & FT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH ) == 0 )
1545        advance = face->horizontal.advance_Width_Max;
1546
1547      /* we need to return the advance in font units in linearHoriAdvance, */
1548      /* it will be scaled later by the base layer.                        */
1549      glyph->linearHoriAdvance = advance;
1550    }
1551
1552    glyph->metrics.horiBearingX = bbox.xMin;
1553    glyph->metrics.horiBearingY = bbox.yMax;
1554    glyph->metrics.horiAdvance  = loader->pp2.x - loader->pp1.x;
1555
1556    /* Now take care of vertical metrics.  In the case where there is    */
1557    /* no vertical information within the font (relatively common), make */
1558    /* up some metrics by `hand'...                                      */
1559
1560    {
1561      FT_Pos  top;      /* scaled vertical top side bearing  */
1562      FT_Pos  advance;  /* scaled vertical advance height    */
1563
1564
1565      /* Get the unscaled top bearing and advance height. */
1566      if ( face->vertical_info &&
1567           face->vertical.number_Of_VMetrics > 0 )
1568      {
1569        top = (FT_Short)FT_DivFix( loader->pp3.y - bbox.yMax,
1570                                   y_scale );
1571
1572        if ( loader->pp3.y <= loader->pp4.y )
1573          advance = 0;
1574        else
1575          advance = (FT_UShort)FT_DivFix( loader->pp3.y - loader->pp4.y,
1576                                          y_scale );
1577      }
1578      else
1579      {
1580        FT_Pos  height;
1581
1582
1583        /* XXX Compute top side bearing and advance height in  */
1584        /*     Get_VMetrics instead of here.                   */
1585
1586        /* NOTE: The OS/2 values are the only `portable' ones, */
1587        /*       which is why we use them, if there is an OS/2 */
1588        /*       table in the font.  Otherwise, we use the     */
1589        /*       values defined in the horizontal header.      */
1590
1591        height = (FT_Short)FT_DivFix( bbox.yMax - bbox.yMin,
1592                                      y_scale );
1593        if ( face->os2.version != 0xFFFFU )
1594          advance = (FT_Pos)( face->os2.sTypoAscender -
1595                              face->os2.sTypoDescender );
1596        else
1597          advance = (FT_Pos)( face->horizontal.Ascender -
1598                              face->horizontal.Descender );
1599
1600        top = ( advance - height ) / 2;
1601      }
1602
1603#ifdef FT_CONFIG_OPTION_INCREMENTAL
1604      {
1605        FT_Incremental_InterfaceRec*  incr;
1606        FT_Incremental_MetricsRec     metrics;
1607        FT_Error                      error;
1608
1609
1610        incr = face->root.internal->incremental_interface;
1611
1612        /* If this is an incrementally loaded font see if there are */
1613        /* overriding metrics for this glyph.                       */
1614        if ( incr && incr->funcs->get_glyph_metrics )
1615        {
1616          metrics.bearing_x = 0;
1617          metrics.bearing_y = top;
1618          metrics.advance   = advance;
1619
1620          error = incr->funcs->get_glyph_metrics( incr->object,
1621                                                  glyph_index,
1622                                                  TRUE,
1623                                                  &metrics );
1624          if ( error )
1625            return error;
1626
1627          top     = metrics.bearing_y;
1628          advance = metrics.advance;
1629        }
1630      }
1631
1632      /* GWW: Do vertical metrics get loaded incrementally too? */
1633
1634#endif /* FT_CONFIG_OPTION_INCREMENTAL */
1635
1636      glyph->linearVertAdvance = advance;
1637
1638      /* scale the metrics */
1639      if ( !( loader->load_flags & FT_LOAD_NO_SCALE ) )
1640      {
1641        top     = FT_MulFix( top, y_scale );
1642        advance = FT_MulFix( advance, y_scale );
1643      }
1644
1645      /* XXX: for now, we have no better algorithm for the lsb, but it */
1646      /*      should work fine.                                        */
1647      /*                                                               */
1648      glyph->metrics.vertBearingX = ( bbox.xMin - bbox.xMax ) / 2;
1649      glyph->metrics.vertBearingY = top;
1650      glyph->metrics.vertAdvance  = advance;
1651    }
1652
1653    /* adjust advance width to the value contained in the hdmx table */
1654    if ( !face->postscript.isFixedPitch &&
1655         IS_HINTED( loader->load_flags )        )
1656    {
1657      FT_Byte*  widthp;
1658
1659
1660      widthp = tt_face_get_device_metrics( face,
1661                                           size->root.metrics.x_ppem,
1662                                           glyph_index );
1663
1664      if ( widthp )
1665        glyph->metrics.horiAdvance = *widthp << 6;
1666    }
1667
1668    /* set glyph dimensions */
1669    glyph->metrics.width  = bbox.xMax - bbox.xMin;
1670    glyph->metrics.height = bbox.yMax - bbox.yMin;
1671
1672    return 0;
1673  }
1674
1675
1676#ifdef TT_CONFIG_OPTION_EMBEDDED_BITMAPS
1677
1678  static FT_Error
1679  load_sbit_image( TT_Size       size,
1680                   TT_GlyphSlot  glyph,
1681                   FT_UInt       glyph_index,
1682                   FT_Int32      load_flags )
1683  {
1684    TT_Face             face;
1685    SFNT_Service        sfnt;
1686    FT_Stream           stream;
1687    FT_Error            error;
1688    TT_SBit_MetricsRec  metrics;
1689
1690
1691    face   = (TT_Face)glyph->face;
1692    sfnt   = (SFNT_Service)face->sfnt;
1693    stream = face->root.stream;
1694
1695    error = sfnt->load_sbit_image( face,
1696                                   size->strike_index,
1697                                   glyph_index,
1698                                   (FT_Int)load_flags,
1699                                   stream,
1700                                   &glyph->bitmap,
1701                                   &metrics );
1702    if ( !error )
1703    {
1704      glyph->outline.n_points   = 0;
1705      glyph->outline.n_contours = 0;
1706
1707      glyph->metrics.width  = (FT_Pos)metrics.width  << 6;
1708      glyph->metrics.height = (FT_Pos)metrics.height << 6;
1709
1710      glyph->metrics.horiBearingX = (FT_Pos)metrics.horiBearingX << 6;
1711      glyph->metrics.horiBearingY = (FT_Pos)metrics.horiBearingY << 6;
1712      glyph->metrics.horiAdvance  = (FT_Pos)metrics.horiAdvance  << 6;
1713
1714      glyph->metrics.vertBearingX = (FT_Pos)metrics.vertBearingX << 6;
1715      glyph->metrics.vertBearingY = (FT_Pos)metrics.vertBearingY << 6;
1716      glyph->metrics.vertAdvance  = (FT_Pos)metrics.vertAdvance  << 6;
1717
1718      glyph->format = FT_GLYPH_FORMAT_BITMAP;
1719      if ( load_flags & FT_LOAD_VERTICAL_LAYOUT )
1720      {
1721        glyph->bitmap_left = metrics.vertBearingX;
1722        glyph->bitmap_top  = metrics.vertBearingY;
1723      }
1724      else
1725      {
1726        glyph->bitmap_left = metrics.horiBearingX;
1727        glyph->bitmap_top  = metrics.horiBearingY;
1728      }
1729    }
1730
1731    return error;
1732  }
1733
1734#endif /* TT_CONFIG_OPTION_EMBEDDED_BITMAPS */
1735
1736
1737  static FT_Error
1738  tt_loader_init( TT_Loader     loader,
1739                  TT_Size       size,
1740                  TT_GlyphSlot  glyph,
1741                  FT_Int32      load_flags )
1742  {
1743    TT_Face    face;
1744    FT_Stream  stream;
1745
1746
1747    face   = (TT_Face)glyph->face;
1748    stream = face->root.stream;
1749
1750    FT_MEM_ZERO( loader, sizeof ( TT_LoaderRec ) );
1751
1752#ifdef TT_USE_BYTECODE_INTERPRETER
1753
1754    /* load execution context */
1755    if ( IS_HINTED( load_flags ) )
1756    {
1757      TT_ExecContext  exec;
1758      FT_Bool         grayscale;
1759
1760
1761      if ( !size->cvt_ready )
1762      {
1763        FT_Error  error = tt_size_ready_bytecode( size );
1764        if ( error )
1765          return error;
1766      }
1767
1768      /* query new execution context */
1769      exec = size->debug ? size->context
1770                         : ( (TT_Driver)FT_FACE_DRIVER( face ) )->context;
1771      if ( !exec )
1772        return TT_Err_Could_Not_Find_Context;
1773
1774      grayscale =
1775        FT_BOOL( FT_LOAD_TARGET_MODE( load_flags ) != FT_RENDER_MODE_MONO );
1776
1777      TT_Load_Context( exec, face, size );
1778
1779      /* a change from mono to grayscale rendering (and vice versa) */
1780      /* requires a re-execution of the CVT program                 */
1781      if ( grayscale != exec->grayscale )
1782      {
1783        FT_UInt  i;
1784
1785
1786        exec->grayscale = grayscale;
1787
1788        for ( i = 0; i < size->cvt_size; i++ )
1789          size->cvt[i] = FT_MulFix( face->cvt[i], size->ttmetrics.scale );
1790        tt_size_run_prep( size );
1791      }
1792
1793      /* see whether the cvt program has disabled hinting */
1794      if ( exec->GS.instruct_control & 1 )
1795        load_flags |= FT_LOAD_NO_HINTING;
1796
1797      /* load default graphics state -- if needed */
1798      if ( exec->GS.instruct_control & 2 )
1799        exec->GS = tt_default_graphics_state;
1800
1801      exec->pedantic_hinting = FT_BOOL( load_flags & FT_LOAD_PEDANTIC );
1802      loader->exec = exec;
1803      loader->instructions = exec->glyphIns;
1804    }
1805
1806#endif /* TT_USE_BYTECODE_INTERPRETER */
1807
1808    /* seek to the beginning of the glyph table -- for Type 42 fonts     */
1809    /* the table might be accessed from a Postscript stream or something */
1810    /* else...                                                           */
1811
1812#ifdef FT_CONFIG_OPTION_INCREMENTAL
1813
1814    if ( face->root.internal->incremental_interface )
1815      loader->glyf_offset = 0;
1816    else
1817
1818#endif
1819
1820    {
1821      FT_Error  error = face->goto_table( face, TTAG_glyf, stream, 0 );
1822
1823
1824      if ( error )
1825      {
1826        FT_ERROR(( "TT_Load_Glyph: could not access glyph table\n" ));
1827        return error;
1828      }
1829      loader->glyf_offset = FT_STREAM_POS();
1830    }
1831
1832    /* get face's glyph loader */
1833    {
1834      FT_GlyphLoader  gloader = glyph->internal->loader;
1835
1836
1837      FT_GlyphLoader_Rewind( gloader );
1838      loader->gloader = gloader;
1839    }
1840
1841    loader->load_flags    = load_flags;
1842
1843    loader->face   = (FT_Face)face;
1844    loader->size   = (FT_Size)size;
1845    loader->glyph  = (FT_GlyphSlot)glyph;
1846    loader->stream = stream;
1847
1848    return TT_Err_Ok;
1849  }
1850
1851
1852  /*************************************************************************/
1853  /*                                                                       */
1854  /* <Function>                                                            */
1855  /*    TT_Load_Glyph                                                      */
1856  /*                                                                       */
1857  /* <Description>                                                         */
1858  /*    A function used to load a single glyph within a given glyph slot,  */
1859  /*    for a given size.                                                  */
1860  /*                                                                       */
1861  /* <Input>                                                               */
1862  /*    glyph       :: A handle to a target slot object where the glyph    */
1863  /*                   will be loaded.                                     */
1864  /*                                                                       */
1865  /*    size        :: A handle to the source face size at which the glyph */
1866  /*                   must be scaled/loaded.                              */
1867  /*                                                                       */
1868  /*    glyph_index :: The index of the glyph in the font file.            */
1869  /*                                                                       */
1870  /*    load_flags  :: A flag indicating what to load for this glyph.  The */
1871  /*                   FT_LOAD_XXX constants can be used to control the    */
1872  /*                   glyph loading process (e.g., whether the outline    */
1873  /*                   should be scaled, whether to load bitmaps or not,   */
1874  /*                   whether to hint the outline, etc).                  */
1875  /*                                                                       */
1876  /* <Return>                                                              */
1877  /*    FreeType error code.  0 means success.                             */
1878  /*                                                                       */
1879  FT_LOCAL_DEF( FT_Error )
1880  TT_Load_Glyph( TT_Size       size,
1881                 TT_GlyphSlot  glyph,
1882                 FT_UInt       glyph_index,
1883                 FT_Int32      load_flags )
1884  {
1885    TT_Face       face;
1886    FT_Error      error;
1887    TT_LoaderRec  loader;
1888
1889
1890    face   = (TT_Face)glyph->face;
1891    error  = TT_Err_Ok;
1892
1893#ifdef TT_CONFIG_OPTION_EMBEDDED_BITMAPS
1894
1895    /* try to load embedded bitmap if any              */
1896    /*                                                 */
1897    /* XXX: The convention should be emphasized in     */
1898    /*      the documents because it can be confusing. */
1899    if ( size->strike_index != 0xFFFFFFFFUL      &&
1900         ( load_flags & FT_LOAD_NO_BITMAP ) == 0 )
1901    {
1902      error = load_sbit_image( size, glyph, glyph_index, load_flags );
1903      if ( !error )
1904        return TT_Err_Ok;
1905    }
1906
1907#endif /* TT_CONFIG_OPTION_EMBEDDED_BITMAPS */
1908
1909    /* if FT_LOAD_NO_SCALE is not set, `ttmetrics' must be valid */
1910    if ( !( load_flags & FT_LOAD_NO_SCALE ) && !size->ttmetrics.valid )
1911      return TT_Err_Invalid_Size_Handle;
1912
1913    if ( load_flags & FT_LOAD_SBITS_ONLY )
1914      return TT_Err_Invalid_Argument;
1915
1916    error = tt_loader_init( &loader, size, glyph, load_flags );
1917    if ( error )
1918      return error;
1919
1920    glyph->format        = FT_GLYPH_FORMAT_OUTLINE;
1921    glyph->num_subglyphs = 0;
1922    glyph->outline.flags = 0;
1923
1924    /* main loading loop */
1925    error = load_truetype_glyph( &loader, glyph_index, 0 );
1926    if ( !error )
1927    {
1928      if ( glyph->format == FT_GLYPH_FORMAT_COMPOSITE )
1929      {
1930        glyph->num_subglyphs = loader.gloader->base.num_subglyphs;
1931        glyph->subglyphs     = loader.gloader->base.subglyphs;
1932      }
1933      else
1934      {
1935        glyph->outline        = loader.gloader->base.outline;
1936        glyph->outline.flags &= ~FT_OUTLINE_SINGLE_PASS;
1937
1938        /* In case bit 1 of the `flags' field in the `head' table isn't */
1939        /* set, translate array so that (0,0) is the glyph's origin.    */
1940        if ( ( face->header.Flags & 2 ) == 0 && loader.pp1.x )
1941          FT_Outline_Translate( &glyph->outline, -loader.pp1.x, 0 );
1942      }
1943
1944      compute_glyph_metrics( &loader, glyph_index );
1945    }
1946
1947    /* Set the `high precision' bit flag.                           */
1948    /* This is _critical_ to get correct output for monochrome      */
1949    /* TrueType glyphs at all sizes using the bytecode interpreter. */
1950    /*                                                              */
1951    if ( !( load_flags & FT_LOAD_NO_SCALE ) &&
1952         size->root.metrics.y_ppem < 24     )
1953      glyph->outline.flags |= FT_OUTLINE_HIGH_PRECISION;
1954
1955    return error;
1956  }
1957
1958
1959/* END */
1960