cf2ft.c revision ec0bab5697bb31ba980810145f62e3799946ec60
1/***************************************************************************/
2/*                                                                         */
3/*  cf2ft.c                                                                */
4/*                                                                         */
5/*    FreeType Glue Component to Adobe's Interpreter (body).               */
6/*                                                                         */
7/*  Copyright 2013 Adobe Systems Incorporated.                             */
8/*                                                                         */
9/*  This software, and all works of authorship, whether in source or       */
10/*  object code form as indicated by the copyright notice(s) included      */
11/*  herein (collectively, the "Work") is made available, and may only be   */
12/*  used, modified, and distributed under the FreeType Project License,    */
13/*  LICENSE.TXT.  Additionally, subject to the terms and conditions of the */
14/*  FreeType Project License, each contributor to the Work hereby grants   */
15/*  to any individual or legal entity exercising permissions granted by    */
16/*  the FreeType Project License and this section (hereafter, "You" or     */
17/*  "Your") a perpetual, worldwide, non-exclusive, no-charge,              */
18/*  royalty-free, irrevocable (except as stated in this section) patent    */
19/*  license to make, have made, use, offer to sell, sell, import, and      */
20/*  otherwise transfer the Work, where such license applies only to those  */
21/*  patent claims licensable by such contributor that are necessarily      */
22/*  infringed by their contribution(s) alone or by combination of their    */
23/*  contribution(s) with the Work to which such contribution(s) was        */
24/*  submitted.  If You institute patent litigation against any entity      */
25/*  (including a cross-claim or counterclaim in a lawsuit) alleging that   */
26/*  the Work or a contribution incorporated within the Work constitutes    */
27/*  direct or contributory patent infringement, then any patent licenses   */
28/*  granted to You under this License for that Work shall terminate as of  */
29/*  the date such litigation is filed.                                     */
30/*                                                                         */
31/*  By using, modifying, or distributing the Work you indicate that you    */
32/*  have read and understood the terms and conditions of the               */
33/*  FreeType Project License as well as those provided in this section,    */
34/*  and you accept them fully.                                             */
35/*                                                                         */
36/***************************************************************************/
37
38
39#include "cf2ft.h"
40#include FT_INTERNAL_DEBUG_H
41
42#include "cf2font.h"
43#include "cf2error.h"
44
45
46#define CF2_MAX_SIZE  cf2_intToFixed( 2000 )    /* max ppem */
47
48
49  /*
50   * This check should avoid most internal overflow cases.  Clients should
51   * generally respond to `Glyph_Too_Big' by getting a glyph outline
52   * at EM size, scaling it and filling it as a graphics operation.
53   *
54   */
55  static FT_Error
56  cf2_checkTransform( const CF2_Matrix*  transform,
57                      CF2_Int            unitsPerEm )
58  {
59    CF2_Fixed  maxScale;
60
61
62    FT_ASSERT( unitsPerEm > 0 );
63
64    FT_ASSERT( transform->a > 0 && transform->d > 0 );
65    FT_ASSERT( transform->b == 0 && transform->c == 0 );
66    FT_ASSERT( transform->tx == 0 && transform->ty == 0 );
67
68    if ( unitsPerEm > 0x7FFF )
69      return FT_THROW( Glyph_Too_Big );
70
71    maxScale = FT_DivFix( CF2_MAX_SIZE, cf2_intToFixed( unitsPerEm ) );
72
73    if ( transform->a > maxScale || transform->d > maxScale )
74      return FT_THROW( Glyph_Too_Big );
75
76    return FT_Err_Ok;
77  }
78
79
80  static void
81  cf2_setGlyphWidth( CF2_Outline  outline,
82                     CF2_Fixed    width )
83  {
84    CFF_Decoder*  decoder = outline->decoder;
85
86
87    FT_ASSERT( decoder );
88
89    decoder->glyph_width = cf2_fixedToInt( width );
90  }
91
92
93  /* Clean up font instance. */
94  static void
95  cf2_free_instance( void*  ptr )
96  {
97    CF2_Font  font = (CF2_Font)ptr;
98
99
100    if ( font )
101    {
102      FT_Memory  memory = font->memory;
103
104
105      (void)memory;
106    }
107  }
108
109
110  /********************************************/
111  /*                                          */
112  /* functions for handling client outline;   */
113  /* FreeType uses coordinates in 26.6 format */
114  /*                                          */
115  /********************************************/
116
117  static void
118  cf2_builder_moveTo( CF2_OutlineCallbacks      callbacks,
119                      const CF2_CallbackParams  params )
120  {
121    /* downcast the object pointer */
122    CF2_Outline   outline = (CF2_Outline)callbacks;
123    CFF_Builder*  builder;
124
125    (void)params;        /* only used in debug mode */
126
127
128    FT_ASSERT( outline && outline->decoder );
129    FT_ASSERT( params->op == CF2_PathOpMoveTo );
130
131    builder = &outline->decoder->builder;
132
133    /* note: two successive moves simply close the contour twice */
134    cff_builder_close_contour( builder );
135    builder->path_begun = 0;
136  }
137
138
139  static void
140  cf2_builder_lineTo( CF2_OutlineCallbacks      callbacks,
141                      const CF2_CallbackParams  params )
142  {
143    /* downcast the object pointer */
144    CF2_Outline   outline = (CF2_Outline)callbacks;
145    CFF_Builder*  builder;
146
147
148    FT_ASSERT( outline && outline->decoder );
149    FT_ASSERT( params->op == CF2_PathOpLineTo );
150
151    builder = &outline->decoder->builder;
152
153    if ( !builder->path_begun )
154    {
155      /* record the move before the line; also check points and set */
156      /* `path_begun'                                               */
157      cff_builder_start_point( builder,
158                               params->pt0.x,
159                               params->pt0.y );
160    }
161
162    /* `cff_builder_add_point1' includes a check_points call for one point */
163    cff_builder_add_point1( builder,
164                            params->pt1.x,
165                            params->pt1.y );
166  }
167
168
169  static void
170  cf2_builder_cubeTo( CF2_OutlineCallbacks      callbacks,
171                      const CF2_CallbackParams  params )
172  {
173    /* downcast the object pointer */
174    CF2_Outline   outline = (CF2_Outline)callbacks;
175    CFF_Builder*  builder;
176
177
178    FT_ASSERT( outline && outline->decoder );
179    FT_ASSERT( params->op == CF2_PathOpCubeTo );
180
181    builder = &outline->decoder->builder;
182
183    if ( !builder->path_begun )
184    {
185      /* record the move before the line; also check points and set */
186      /* `path_begun'                                               */
187      cff_builder_start_point( builder,
188                               params->pt0.x,
189                               params->pt0.y );
190    }
191
192    /* prepare room for 3 points: 2 off-curve, 1 on-curve */
193    cff_check_points( builder, 3 );
194
195    cff_builder_add_point( builder,
196                           params->pt1.x,
197                           params->pt1.y, 0 );
198    cff_builder_add_point( builder,
199                           params->pt2.x,
200                           params->pt2.y, 0 );
201    cff_builder_add_point( builder,
202                           params->pt3.x,
203                           params->pt3.y, 1 );
204  }
205
206
207  static void
208  cf2_outline_init( CF2_Outline  outline,
209                    FT_Memory    memory,
210                    FT_Error*    error )
211  {
212    FT_MEM_ZERO( outline, sizeof ( CF2_OutlineRec ) );
213
214    outline->root.memory = memory;
215    outline->root.error  = error;
216
217    outline->root.moveTo = cf2_builder_moveTo;
218    outline->root.lineTo = cf2_builder_lineTo;
219    outline->root.cubeTo = cf2_builder_cubeTo;
220  }
221
222
223  /* get scaling and hint flag from GlyphSlot */
224  static void
225  cf2_getScaleAndHintFlag( CFF_Decoder*  decoder,
226                           CF2_Fixed*    x_scale,
227                           CF2_Fixed*    y_scale,
228                           FT_Bool*      hinted,
229                           FT_Bool*      scaled )
230  {
231    FT_ASSERT( decoder && decoder->builder.glyph );
232
233    /* note: FreeType scale includes a factor of 64 */
234    *hinted = decoder->builder.glyph->hint;
235    *scaled = decoder->builder.glyph->scaled;
236
237    if ( *hinted )
238    {
239      *x_scale = FT_DivFix( decoder->builder.glyph->x_scale,
240                            cf2_intToFixed( 64 ) );
241      *y_scale = FT_DivFix( decoder->builder.glyph->y_scale,
242                            cf2_intToFixed( 64 ) );
243    }
244    else
245    {
246      /* for unhinted outlines, `cff_slot_load' does the scaling, */
247      /* thus render at `unity' scale                             */
248
249      *x_scale = 0x0400;   /* 1/64 as 16.16 */
250      *y_scale = 0x0400;
251    }
252  }
253
254
255  /* get units per em from `FT_Face' */
256  /* TODO: should handle font matrix concatenation? */
257  static FT_UShort
258  cf2_getUnitsPerEm( CFF_Decoder*  decoder )
259  {
260    FT_ASSERT( decoder && decoder->builder.face );
261    FT_ASSERT( decoder->builder.face->root.units_per_EM );
262
263    return decoder->builder.face->root.units_per_EM;
264  }
265
266
267  /* Main entry point: Render one glyph. */
268  FT_LOCAL_DEF( FT_Error )
269  cf2_decoder_parse_charstrings( CFF_Decoder*  decoder,
270                                 FT_Byte*      charstring_base,
271                                 FT_ULong      charstring_len )
272  {
273    FT_Memory  memory;
274    FT_Error   error = FT_Err_Ok;
275    CF2_Font   font;
276
277
278    FT_ASSERT( decoder && decoder->cff );
279
280    memory = decoder->builder.memory;
281
282    /* CF2 data is saved here across glyphs */
283    font = (CF2_Font)decoder->cff->cf2_instance.data;
284
285    /* on first glyph, allocate instance structure */
286    if ( decoder->cff->cf2_instance.data == NULL )
287    {
288      decoder->cff->cf2_instance.finalizer =
289        (FT_Generic_Finalizer)cf2_free_instance;
290
291      if ( FT_ALLOC( decoder->cff->cf2_instance.data,
292                     sizeof ( CF2_FontRec ) ) )
293        return FT_THROW( Out_Of_Memory );
294
295      font = (CF2_Font)decoder->cff->cf2_instance.data;
296
297      font->memory = memory;
298
299      /* initialize a client outline, to be shared by each glyph rendered */
300      cf2_outline_init( &font->outline, font->memory, &font->error );
301    }
302
303    /* save decoder; it is a stack variable and will be different on each */
304    /* call                                                               */
305    font->decoder         = decoder;
306    font->outline.decoder = decoder;
307
308    {
309      /* build parameters for Adobe engine */
310
311      CFF_Builder*  builder = &decoder->builder;
312      CFF_Driver    driver  = (CFF_Driver)FT_FACE_DRIVER( builder->face );
313
314      /* local error */
315      FT_Error       error2 = FT_Err_Ok;
316      CF2_BufferRec  buf;
317      CF2_Matrix     transform;
318      CF2_F16Dot16   glyphWidth;
319
320      FT_Bool  hinted;
321      FT_Bool  scaled;
322
323
324      /* FreeType has already looked up the GID; convert to         */
325      /* `RegionBuffer', assuming that the input has been validated */
326      FT_ASSERT( charstring_base + charstring_len >= charstring_base );
327
328      FT_ZERO( &buf );
329      buf.start =
330      buf.ptr   = charstring_base;
331      buf.end   = charstring_base + charstring_len;
332
333      FT_ZERO( &transform );
334
335      cf2_getScaleAndHintFlag( decoder,
336                               &transform.a,
337                               &transform.d,
338                               &hinted,
339                               &scaled );
340
341      font->renderingFlags = 0;
342      if ( hinted )
343        font->renderingFlags |= CF2_FlagsHinted;
344      if ( scaled && !driver->no_stem_darkening )
345        font->renderingFlags |= CF2_FlagsDarkened;
346
347      font->darkenParams[0] = driver->darken_params[0];
348      font->darkenParams[1] = driver->darken_params[1];
349      font->darkenParams[2] = driver->darken_params[2];
350      font->darkenParams[3] = driver->darken_params[3];
351      font->darkenParams[4] = driver->darken_params[4];
352      font->darkenParams[5] = driver->darken_params[5];
353      font->darkenParams[6] = driver->darken_params[6];
354      font->darkenParams[7] = driver->darken_params[7];
355
356      /* now get an outline for this glyph;      */
357      /* also get units per em to validate scale */
358      font->unitsPerEm = (CF2_Int)cf2_getUnitsPerEm( decoder );
359
360      error2 = cf2_checkTransform( &transform, font->unitsPerEm );
361      if ( error2 )
362        return error2;
363
364      error2 = cf2_getGlyphOutline( font, &buf, &transform, &glyphWidth );
365      if ( error2 )
366        return FT_ERR( Invalid_File_Format );
367
368      cf2_setGlyphWidth( &font->outline, glyphWidth );
369
370      return FT_Err_Ok;
371    }
372  }
373
374
375  /* get pointer to current FreeType subfont (based on current glyphID) */
376  FT_LOCAL_DEF( CFF_SubFont )
377  cf2_getSubfont( CFF_Decoder*  decoder )
378  {
379    FT_ASSERT( decoder && decoder->current_subfont );
380
381    return decoder->current_subfont;
382  }
383
384
385  /* get `y_ppem' from `CFF_Size' */
386  FT_LOCAL_DEF( CF2_Fixed )
387  cf2_getPpemY( CFF_Decoder*  decoder )
388  {
389    FT_ASSERT( decoder                          &&
390               decoder->builder.face            &&
391               decoder->builder.face->root.size );
392    FT_ASSERT( decoder->builder.face->root.size->metrics.y_ppem );
393
394    return cf2_intToFixed(
395             decoder->builder.face->root.size->metrics.y_ppem );
396  }
397
398
399  /* get standard stem widths for the current subfont; */
400  /* FreeType stores these as integer font units       */
401  /* (note: variable names seem swapped)               */
402  FT_LOCAL_DEF( CF2_Fixed )
403  cf2_getStdVW( CFF_Decoder*  decoder )
404  {
405    FT_ASSERT( decoder && decoder->current_subfont );
406
407    return cf2_intToFixed(
408             decoder->current_subfont->private_dict.standard_height );
409  }
410
411
412  FT_LOCAL_DEF( CF2_Fixed )
413  cf2_getStdHW( CFF_Decoder*  decoder )
414  {
415    FT_ASSERT( decoder && decoder->current_subfont );
416
417    return cf2_intToFixed(
418             decoder->current_subfont->private_dict.standard_width );
419  }
420
421
422  /* note: FreeType stores 1000 times the actual value for `BlueScale' */
423  FT_LOCAL_DEF( void )
424  cf2_getBlueMetrics( CFF_Decoder*  decoder,
425                      CF2_Fixed*    blueScale,
426                      CF2_Fixed*    blueShift,
427                      CF2_Fixed*    blueFuzz )
428  {
429    FT_ASSERT( decoder && decoder->current_subfont );
430
431    *blueScale = FT_DivFix(
432                   decoder->current_subfont->private_dict.blue_scale,
433                   cf2_intToFixed( 1000 ) );
434    *blueShift = cf2_intToFixed(
435                   decoder->current_subfont->private_dict.blue_shift );
436    *blueFuzz  = cf2_intToFixed(
437                   decoder->current_subfont->private_dict.blue_fuzz );
438  }
439
440
441  /* get blue values counts and arrays; the FreeType parser has validated */
442  /* the counts and verified that each is an even number                  */
443  FT_LOCAL_DEF( void )
444  cf2_getBlueValues( CFF_Decoder*  decoder,
445                     size_t*       count,
446                     FT_Pos*      *data )
447  {
448    FT_ASSERT( decoder && decoder->current_subfont );
449
450    *count = decoder->current_subfont->private_dict.num_blue_values;
451    *data  = (FT_Pos*)
452               &decoder->current_subfont->private_dict.blue_values;
453  }
454
455
456  FT_LOCAL_DEF( void )
457  cf2_getOtherBlues( CFF_Decoder*  decoder,
458                     size_t*       count,
459                     FT_Pos*      *data )
460  {
461    FT_ASSERT( decoder && decoder->current_subfont );
462
463    *count = decoder->current_subfont->private_dict.num_other_blues;
464    *data  = (FT_Pos*)
465               &decoder->current_subfont->private_dict.other_blues;
466  }
467
468
469  FT_LOCAL_DEF( void )
470  cf2_getFamilyBlues( CFF_Decoder*  decoder,
471                      size_t*       count,
472                      FT_Pos*      *data )
473  {
474    FT_ASSERT( decoder && decoder->current_subfont );
475
476    *count = decoder->current_subfont->private_dict.num_family_blues;
477    *data  = (FT_Pos*)
478               &decoder->current_subfont->private_dict.family_blues;
479  }
480
481
482  FT_LOCAL_DEF( void )
483  cf2_getFamilyOtherBlues( CFF_Decoder*  decoder,
484                           size_t*       count,
485                           FT_Pos*      *data )
486  {
487    FT_ASSERT( decoder && decoder->current_subfont );
488
489    *count = decoder->current_subfont->private_dict.num_family_other_blues;
490    *data  = (FT_Pos*)
491               &decoder->current_subfont->private_dict.family_other_blues;
492  }
493
494
495  FT_LOCAL_DEF( CF2_Int )
496  cf2_getLanguageGroup( CFF_Decoder*  decoder )
497  {
498    FT_ASSERT( decoder && decoder->current_subfont );
499
500    return decoder->current_subfont->private_dict.language_group;
501  }
502
503
504  /* convert unbiased subroutine index to `CF2_Buffer' and */
505  /* return 0 on success                                   */
506  FT_LOCAL_DEF( CF2_Int )
507  cf2_initGlobalRegionBuffer( CFF_Decoder*  decoder,
508                              CF2_UInt      idx,
509                              CF2_Buffer    buf )
510  {
511    FT_ASSERT( decoder && decoder->globals );
512
513    FT_ZERO( buf );
514
515    idx += decoder->globals_bias;
516    if ( idx >= decoder->num_globals )
517      return TRUE;     /* error */
518
519    buf->start =
520    buf->ptr   = decoder->globals[idx];
521    buf->end   = decoder->globals[idx + 1];
522
523    return FALSE;      /* success */
524  }
525
526
527  /* convert AdobeStandardEncoding code to CF2_Buffer; */
528  /* used for seac component                           */
529  FT_LOCAL_DEF( FT_Error )
530  cf2_getSeacComponent( CFF_Decoder*  decoder,
531                        CF2_UInt      code,
532                        CF2_Buffer    buf )
533  {
534    CF2_Int   gid;
535    FT_Byte*  charstring;
536    FT_ULong  len;
537    FT_Error  error;
538
539
540    FT_ASSERT( decoder );
541
542    FT_ZERO( buf );
543
544    gid = cff_lookup_glyph_by_stdcharcode( decoder->cff, code );
545    if ( gid < 0 )
546      return FT_THROW( Invalid_Glyph_Format );
547
548    error = cff_get_glyph_data( decoder->builder.face,
549                                gid,
550                                &charstring,
551                                &len );
552    /* TODO: for now, just pass the FreeType error through */
553    if ( error )
554      return error;
555
556    /* assume input has been validated */
557    FT_ASSERT( charstring + len >= charstring );
558
559    buf->start = charstring;
560    buf->end   = charstring + len;
561    buf->ptr   = buf->start;
562
563    return FT_Err_Ok;
564  }
565
566
567  FT_LOCAL_DEF( void )
568  cf2_freeSeacComponent( CFF_Decoder*  decoder,
569                         CF2_Buffer    buf )
570  {
571    FT_ASSERT( decoder );
572
573    cff_free_glyph_data( decoder->builder.face,
574                         (FT_Byte**)&buf->start,
575                         (FT_ULong)( buf->end - buf->start ) );
576  }
577
578
579  FT_LOCAL_DEF( CF2_Int )
580  cf2_initLocalRegionBuffer( CFF_Decoder*  decoder,
581                             CF2_UInt      idx,
582                             CF2_Buffer    buf )
583  {
584    FT_ASSERT( decoder && decoder->locals );
585
586    FT_ZERO( buf );
587
588    idx += decoder->locals_bias;
589    if ( idx >= decoder->num_locals )
590      return TRUE;     /* error */
591
592    buf->start =
593    buf->ptr   = decoder->locals[idx];
594    buf->end   = decoder->locals[idx + 1];
595
596    return FALSE;      /* success */
597  }
598
599
600  FT_LOCAL_DEF( CF2_Fixed )
601  cf2_getDefaultWidthX( CFF_Decoder*  decoder )
602  {
603    FT_ASSERT( decoder && decoder->current_subfont );
604
605    return cf2_intToFixed(
606             decoder->current_subfont->private_dict.default_width );
607  }
608
609
610  FT_LOCAL_DEF( CF2_Fixed )
611  cf2_getNominalWidthX( CFF_Decoder*  decoder )
612  {
613    FT_ASSERT( decoder && decoder->current_subfont );
614
615    return cf2_intToFixed(
616             decoder->current_subfont->private_dict.nominal_width );
617  }
618
619
620  FT_LOCAL_DEF( void )
621  cf2_outline_reset( CF2_Outline  outline )
622  {
623    CFF_Decoder*  decoder = outline->decoder;
624
625
626    FT_ASSERT( decoder );
627
628    outline->root.windingMomentum = 0;
629
630    FT_GlyphLoader_Rewind( decoder->builder.loader );
631  }
632
633
634  FT_LOCAL_DEF( void )
635  cf2_outline_close( CF2_Outline  outline )
636  {
637    CFF_Decoder*  decoder = outline->decoder;
638
639
640    FT_ASSERT( decoder );
641
642    cff_builder_close_contour( &decoder->builder );
643
644    FT_GlyphLoader_Add( decoder->builder.loader );
645  }
646
647
648/* END */
649