~ubuntu-branches/ubuntu/vivid/emscripten/vivid

« back to all changes in this revision

Viewing changes to tests/freetype/src/truetype/ttgload.c

  • Committer: Package Import Robot
  • Author(s): Sylvestre Ledru
  • Date: 2013-05-02 13:11:51 UTC
  • Revision ID: package-import@ubuntu.com-20130502131151-q8dvteqr1ef2x7xz
Tags: upstream-1.4.1~20130504~adb56cb
ImportĀ upstreamĀ versionĀ 1.4.1~20130504~adb56cb

Show diffs side-by-side

added added

removed removed

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