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