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