• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /***************************************************************************/
2 /*                                                                         */
3 /*  cffobjs.c                                                              */
4 /*                                                                         */
5 /*    OpenType objects manager (body).                                     */
6 /*                                                                         */
7 /*  Copyright 1996-2017 by                                                 */
8 /*  David Turner, Robert Wilhelm, and Werner Lemberg.                      */
9 /*                                                                         */
10 /*  This file is part of the FreeType project, and may only be used,       */
11 /*  modified, and distributed under the terms of the FreeType project      */
12 /*  license, LICENSE.TXT.  By continuing to use, modify, or distribute     */
13 /*  this file you indicate that you have read the license and              */
14 /*  understand and accept it fully.                                        */
15 /*                                                                         */
16 /***************************************************************************/
17 
18 
19 #include <ft2build.h>
20 
21 #include FT_INTERNAL_DEBUG_H
22 #include FT_INTERNAL_CALC_H
23 #include FT_INTERNAL_STREAM_H
24 #include FT_ERRORS_H
25 #include FT_TRUETYPE_IDS_H
26 #include FT_TRUETYPE_TAGS_H
27 #include FT_INTERNAL_SFNT_H
28 #include FT_CFF_DRIVER_H
29 
30 #include "cffobjs.h"
31 #include "cffload.h"
32 #include "cffcmap.h"
33 #include "cffpic.h"
34 
35 #include "cfferrs.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_cffobjs
46 
47 
48   /*************************************************************************/
49   /*                                                                       */
50   /*                            SIZE FUNCTIONS                             */
51   /*                                                                       */
52   /*  Note that we store the global hints in the size's `internal' root    */
53   /*  field.                                                               */
54   /*                                                                       */
55   /*************************************************************************/
56 
57 
58   static PSH_Globals_Funcs
cff_size_get_globals_funcs(CFF_Size size)59   cff_size_get_globals_funcs( CFF_Size  size )
60   {
61     CFF_Face          face     = (CFF_Face)size->root.face;
62     CFF_Font          font     = (CFF_Font)face->extra.data;
63     PSHinter_Service  pshinter = font->pshinter;
64     FT_Module         module;
65 
66 
67     module = FT_Get_Module( size->root.face->driver->root.library,
68                             "pshinter" );
69     return ( module && pshinter && pshinter->get_globals_funcs )
70            ? pshinter->get_globals_funcs( module )
71            : 0;
72   }
73 
74 
75   FT_LOCAL_DEF( void )
cff_size_done(FT_Size cffsize)76   cff_size_done( FT_Size  cffsize )        /* CFF_Size */
77   {
78     CFF_Size      size     = (CFF_Size)cffsize;
79     CFF_Face      face     = (CFF_Face)size->root.face;
80     CFF_Font      font     = (CFF_Font)face->extra.data;
81     CFF_Internal  internal = (CFF_Internal)cffsize->internal;
82 
83 
84     if ( internal )
85     {
86       PSH_Globals_Funcs  funcs;
87 
88 
89       funcs = cff_size_get_globals_funcs( size );
90       if ( funcs )
91       {
92         FT_UInt  i;
93 
94 
95         funcs->destroy( internal->topfont );
96 
97         for ( i = font->num_subfonts; i > 0; i-- )
98           funcs->destroy( internal->subfonts[i - 1] );
99       }
100 
101       /* `internal' is freed by destroy_size (in ftobjs.c) */
102     }
103   }
104 
105 
106   /* CFF and Type 1 private dictionaries have slightly different      */
107   /* structures; we need to synthesize a Type 1 dictionary on the fly */
108 
109   static void
cff_make_private_dict(CFF_SubFont subfont,PS_Private priv)110   cff_make_private_dict( CFF_SubFont  subfont,
111                          PS_Private   priv )
112   {
113     CFF_Private  cpriv = &subfont->private_dict;
114     FT_UInt      n, count;
115 
116 
117     FT_ZERO( priv );
118 
119     count = priv->num_blue_values = cpriv->num_blue_values;
120     for ( n = 0; n < count; n++ )
121       priv->blue_values[n] = (FT_Short)cpriv->blue_values[n];
122 
123     count = priv->num_other_blues = cpriv->num_other_blues;
124     for ( n = 0; n < count; n++ )
125       priv->other_blues[n] = (FT_Short)cpriv->other_blues[n];
126 
127     count = priv->num_family_blues = cpriv->num_family_blues;
128     for ( n = 0; n < count; n++ )
129       priv->family_blues[n] = (FT_Short)cpriv->family_blues[n];
130 
131     count = priv->num_family_other_blues = cpriv->num_family_other_blues;
132     for ( n = 0; n < count; n++ )
133       priv->family_other_blues[n] = (FT_Short)cpriv->family_other_blues[n];
134 
135     priv->blue_scale = cpriv->blue_scale;
136     priv->blue_shift = (FT_Int)cpriv->blue_shift;
137     priv->blue_fuzz  = (FT_Int)cpriv->blue_fuzz;
138 
139     priv->standard_width[0]  = (FT_UShort)cpriv->standard_width;
140     priv->standard_height[0] = (FT_UShort)cpriv->standard_height;
141 
142     count = priv->num_snap_widths = cpriv->num_snap_widths;
143     for ( n = 0; n < count; n++ )
144       priv->snap_widths[n] = (FT_Short)cpriv->snap_widths[n];
145 
146     count = priv->num_snap_heights = cpriv->num_snap_heights;
147     for ( n = 0; n < count; n++ )
148       priv->snap_heights[n] = (FT_Short)cpriv->snap_heights[n];
149 
150     priv->force_bold     = cpriv->force_bold;
151     priv->language_group = cpriv->language_group;
152     priv->lenIV          = cpriv->lenIV;
153   }
154 
155 
156   FT_LOCAL_DEF( FT_Error )
cff_size_init(FT_Size cffsize)157   cff_size_init( FT_Size  cffsize )         /* CFF_Size */
158   {
159     CFF_Size           size  = (CFF_Size)cffsize;
160     FT_Error           error = FT_Err_Ok;
161     PSH_Globals_Funcs  funcs = cff_size_get_globals_funcs( size );
162 
163 
164     if ( funcs )
165     {
166       CFF_Face      face     = (CFF_Face)cffsize->face;
167       CFF_Font      font     = (CFF_Font)face->extra.data;
168       CFF_Internal  internal = NULL;
169 
170       PS_PrivateRec  priv;
171       FT_Memory      memory = cffsize->face->memory;
172 
173       FT_UInt  i;
174 
175 
176       if ( FT_NEW( internal ) )
177         goto Exit;
178 
179       cff_make_private_dict( &font->top_font, &priv );
180       error = funcs->create( cffsize->face->memory, &priv,
181                              &internal->topfont );
182       if ( error )
183         goto Exit;
184 
185       for ( i = font->num_subfonts; i > 0; i-- )
186       {
187         CFF_SubFont  sub = font->subfonts[i - 1];
188 
189 
190         cff_make_private_dict( sub, &priv );
191         error = funcs->create( cffsize->face->memory, &priv,
192                                &internal->subfonts[i - 1] );
193         if ( error )
194           goto Exit;
195       }
196 
197       cffsize->internal = (FT_Size_Internal)(void*)internal;
198     }
199 
200     size->strike_index = 0xFFFFFFFFUL;
201 
202   Exit:
203     return error;
204   }
205 
206 
207 #ifdef TT_CONFIG_OPTION_EMBEDDED_BITMAPS
208 
209   FT_LOCAL_DEF( FT_Error )
cff_size_select(FT_Size size,FT_ULong strike_index)210   cff_size_select( FT_Size   size,
211                    FT_ULong  strike_index )
212   {
213     CFF_Size           cffsize = (CFF_Size)size;
214     PSH_Globals_Funcs  funcs;
215 
216 
217     cffsize->strike_index = strike_index;
218 
219     FT_Select_Metrics( size->face, strike_index );
220 
221     funcs = cff_size_get_globals_funcs( cffsize );
222 
223     if ( funcs )
224     {
225       CFF_Face      face     = (CFF_Face)size->face;
226       CFF_Font      font     = (CFF_Font)face->extra.data;
227       CFF_Internal  internal = (CFF_Internal)size->internal;
228 
229       FT_Long  top_upm  = (FT_Long)font->top_font.font_dict.units_per_em;
230       FT_UInt  i;
231 
232 
233       funcs->set_scale( internal->topfont,
234                         size->metrics.x_scale, size->metrics.y_scale,
235                         0, 0 );
236 
237       for ( i = font->num_subfonts; i > 0; i-- )
238       {
239         CFF_SubFont  sub     = font->subfonts[i - 1];
240         FT_Long      sub_upm = (FT_Long)sub->font_dict.units_per_em;
241         FT_Pos       x_scale, y_scale;
242 
243 
244         if ( top_upm != sub_upm )
245         {
246           x_scale = FT_MulDiv( size->metrics.x_scale, top_upm, sub_upm );
247           y_scale = FT_MulDiv( size->metrics.y_scale, top_upm, sub_upm );
248         }
249         else
250         {
251           x_scale = size->metrics.x_scale;
252           y_scale = size->metrics.y_scale;
253         }
254 
255         funcs->set_scale( internal->subfonts[i - 1],
256                           x_scale, y_scale, 0, 0 );
257       }
258     }
259 
260     return FT_Err_Ok;
261   }
262 
263 #endif /* TT_CONFIG_OPTION_EMBEDDED_BITMAPS */
264 
265 
266   FT_LOCAL_DEF( FT_Error )
cff_size_request(FT_Size size,FT_Size_Request req)267   cff_size_request( FT_Size          size,
268                     FT_Size_Request  req )
269   {
270     CFF_Size           cffsize = (CFF_Size)size;
271     PSH_Globals_Funcs  funcs;
272 
273 
274 #ifdef TT_CONFIG_OPTION_EMBEDDED_BITMAPS
275 
276     if ( FT_HAS_FIXED_SIZES( size->face ) )
277     {
278       CFF_Face      cffface = (CFF_Face)size->face;
279       SFNT_Service  sfnt    = (SFNT_Service)cffface->sfnt;
280       FT_ULong      strike_index;
281 
282 
283       if ( sfnt->set_sbit_strike( cffface, req, &strike_index ) )
284         cffsize->strike_index = 0xFFFFFFFFUL;
285       else
286         return cff_size_select( size, strike_index );
287     }
288 
289 #endif /* TT_CONFIG_OPTION_EMBEDDED_BITMAPS */
290 
291     FT_Request_Metrics( size->face, req );
292 
293     funcs = cff_size_get_globals_funcs( cffsize );
294 
295     if ( funcs )
296     {
297       CFF_Face      cffface  = (CFF_Face)size->face;
298       CFF_Font      font     = (CFF_Font)cffface->extra.data;
299       CFF_Internal  internal = (CFF_Internal)size->internal;
300 
301       FT_Long  top_upm  = (FT_Long)font->top_font.font_dict.units_per_em;
302       FT_UInt  i;
303 
304 
305       funcs->set_scale( internal->topfont,
306                         size->metrics.x_scale, size->metrics.y_scale,
307                         0, 0 );
308 
309       for ( i = font->num_subfonts; i > 0; i-- )
310       {
311         CFF_SubFont  sub     = font->subfonts[i - 1];
312         FT_Long      sub_upm = (FT_Long)sub->font_dict.units_per_em;
313         FT_Pos       x_scale, y_scale;
314 
315 
316         if ( top_upm != sub_upm )
317         {
318           x_scale = FT_MulDiv( size->metrics.x_scale, top_upm, sub_upm );
319           y_scale = FT_MulDiv( size->metrics.y_scale, top_upm, sub_upm );
320         }
321         else
322         {
323           x_scale = size->metrics.x_scale;
324           y_scale = size->metrics.y_scale;
325         }
326 
327         funcs->set_scale( internal->subfonts[i - 1],
328                           x_scale, y_scale, 0, 0 );
329       }
330     }
331 
332     return FT_Err_Ok;
333   }
334 
335 
336   /*************************************************************************/
337   /*                                                                       */
338   /*                            SLOT  FUNCTIONS                            */
339   /*                                                                       */
340   /*************************************************************************/
341 
342   FT_LOCAL_DEF( void )
cff_slot_done(FT_GlyphSlot slot)343   cff_slot_done( FT_GlyphSlot  slot )
344   {
345     slot->internal->glyph_hints = NULL;
346   }
347 
348 
349   FT_LOCAL_DEF( FT_Error )
cff_slot_init(FT_GlyphSlot slot)350   cff_slot_init( FT_GlyphSlot  slot )
351   {
352     CFF_Face          face     = (CFF_Face)slot->face;
353     CFF_Font          font     = (CFF_Font)face->extra.data;
354     PSHinter_Service  pshinter = font->pshinter;
355 
356 
357     if ( pshinter )
358     {
359       FT_Module  module;
360 
361 
362       module = FT_Get_Module( slot->face->driver->root.library,
363                               "pshinter" );
364       if ( module )
365       {
366         T2_Hints_Funcs  funcs;
367 
368 
369         funcs = pshinter->get_t2_funcs( module );
370         slot->internal->glyph_hints = (void*)funcs;
371       }
372     }
373 
374     return FT_Err_Ok;
375   }
376 
377 
378   /*************************************************************************/
379   /*                                                                       */
380   /*                           FACE  FUNCTIONS                             */
381   /*                                                                       */
382   /*************************************************************************/
383 
384   static FT_String*
cff_strcpy(FT_Memory memory,const FT_String * source)385   cff_strcpy( FT_Memory         memory,
386               const FT_String*  source )
387   {
388     FT_Error    error;
389     FT_String*  result;
390 
391 
392     (void)FT_STRDUP( result, source );
393 
394     FT_UNUSED( error );
395 
396     return result;
397   }
398 
399 
400   /* Strip all subset prefixes of the form `ABCDEF+'.  Usually, there */
401   /* is only one, but font names like `APCOOG+JFABTD+FuturaBQ-Bold'   */
402   /* have been seen in the wild.                                      */
403 
404   static void
remove_subset_prefix(FT_String * name)405   remove_subset_prefix( FT_String*  name )
406   {
407     FT_Int32  idx             = 0;
408     FT_Int32  length          = (FT_Int32)strlen( name ) + 1;
409     FT_Bool   continue_search = 1;
410 
411 
412     while ( continue_search )
413     {
414       if ( length >= 7 && name[6] == '+' )
415       {
416         for ( idx = 0; idx < 6; idx++ )
417         {
418           /* ASCII uppercase letters */
419           if ( !( 'A' <= name[idx] && name[idx] <= 'Z' ) )
420             continue_search = 0;
421         }
422 
423         if ( continue_search )
424         {
425           for ( idx = 7; idx < length; idx++ )
426             name[idx - 7] = name[idx];
427           length -= 7;
428         }
429       }
430       else
431         continue_search = 0;
432     }
433   }
434 
435 
436   /* Remove the style part from the family name (if present). */
437 
438   static void
remove_style(FT_String * family_name,const FT_String * style_name)439   remove_style( FT_String*        family_name,
440                 const FT_String*  style_name )
441   {
442     FT_Int32  family_name_length, style_name_length;
443 
444 
445     family_name_length = (FT_Int32)strlen( family_name );
446     style_name_length  = (FT_Int32)strlen( style_name );
447 
448     if ( family_name_length > style_name_length )
449     {
450       FT_Int  idx;
451 
452 
453       for ( idx = 1; idx <= style_name_length; idx++ )
454       {
455         if ( family_name[family_name_length - idx] !=
456              style_name[style_name_length - idx] )
457           break;
458       }
459 
460       if ( idx > style_name_length )
461       {
462         /* family_name ends with style_name; remove it */
463         idx = family_name_length - style_name_length - 1;
464 
465         /* also remove special characters     */
466         /* between real family name and style */
467         while ( idx > 0                     &&
468                 ( family_name[idx] == '-' ||
469                   family_name[idx] == ' ' ||
470                   family_name[idx] == '_' ||
471                   family_name[idx] == '+' ) )
472           idx--;
473 
474         if ( idx > 0 )
475           family_name[idx + 1] = '\0';
476       }
477     }
478   }
479 
480 
481   FT_LOCAL_DEF( FT_Error )
cff_face_init(FT_Stream stream,FT_Face cffface,FT_Int face_index,FT_Int num_params,FT_Parameter * params)482   cff_face_init( FT_Stream      stream,
483                  FT_Face        cffface,        /* CFF_Face */
484                  FT_Int         face_index,
485                  FT_Int         num_params,
486                  FT_Parameter*  params )
487   {
488     CFF_Face            face        = (CFF_Face)cffface;
489     FT_Error            error;
490     SFNT_Service        sfnt;
491     FT_Service_PsCMaps  psnames;
492     PSHinter_Service    pshinter;
493     FT_Bool             pure_cff    = 1;
494     FT_Bool             cff2        = 0;
495     FT_Bool             sfnt_format = 0;
496     FT_Library          library     = cffface->driver->root.library;
497 
498 
499     sfnt = (SFNT_Service)FT_Get_Module_Interface(
500              library, "sfnt" );
501     if ( !sfnt )
502     {
503       FT_ERROR(( "cff_face_init: cannot access `sfnt' module\n" ));
504       error = FT_THROW( Missing_Module );
505       goto Exit;
506     }
507 
508     FT_FACE_FIND_GLOBAL_SERVICE( face, psnames, POSTSCRIPT_CMAPS );
509 
510     pshinter = (PSHinter_Service)FT_Get_Module_Interface(
511                  library, "pshinter" );
512 
513     FT_TRACE2(( "CFF driver\n" ));
514 
515     /* create input stream from resource */
516     if ( FT_STREAM_SEEK( 0 ) )
517       goto Exit;
518 
519     /* check whether we have a valid OpenType file */
520     FT_TRACE2(( "  " ));
521     error = sfnt->init_face( stream, face, face_index, num_params, params );
522     if ( !error )
523     {
524       if ( face->format_tag != TTAG_OTTO )  /* `OTTO'; OpenType/CFF font */
525       {
526         FT_TRACE2(( "  not an OpenType/CFF font\n" ));
527         error = FT_THROW( Unknown_File_Format );
528         goto Exit;
529       }
530 
531       /* if we are performing a simple font format check, exit immediately */
532       if ( face_index < 0 )
533         return FT_Err_Ok;
534 
535       sfnt_format = 1;
536 
537       /* now, the font can be either an OpenType/CFF font, or an SVG CEF */
538       /* font; in the latter case it doesn't have a `head' table         */
539       error = face->goto_table( face, TTAG_head, stream, 0 );
540       if ( !error )
541       {
542         pure_cff = 0;
543 
544         /* load font directory */
545         error = sfnt->load_face( stream, face, face_index,
546                                  num_params, params );
547         if ( error )
548           goto Exit;
549       }
550       else
551       {
552         /* load the `cmap' table explicitly */
553         error = sfnt->load_cmap( face, stream );
554         if ( error )
555           goto Exit;
556       }
557 
558       /* now load the CFF part of the file; */
559       /* give priority to CFF2              */
560       error = face->goto_table( face, TTAG_CFF2, stream, 0 );
561       if ( !error )
562       {
563         cff2         = 1;
564         face->isCFF2 = cff2;
565       }
566 
567       if ( FT_ERR_EQ( error, Table_Missing ) )
568         error = face->goto_table( face, TTAG_CFF, stream, 0 );
569 
570       if ( error )
571         goto Exit;
572     }
573     else
574     {
575       /* rewind to start of file; we are going to load a pure-CFF font */
576       if ( FT_STREAM_SEEK( 0 ) )
577         goto Exit;
578       error = FT_Err_Ok;
579     }
580 
581     /* now load and parse the CFF table in the file */
582     {
583       CFF_Font         cff = NULL;
584       CFF_FontRecDict  dict;
585       FT_Memory        memory = cffface->memory;
586       FT_Int32         flags;
587       FT_UInt          i;
588 
589 
590       if ( FT_NEW( cff ) )
591         goto Exit;
592 
593       face->extra.data = cff;
594       error = cff_font_load( library,
595                              stream,
596                              face_index,
597                              cff,
598                              pure_cff,
599                              cff2 );
600       if ( error )
601         goto Exit;
602 
603       /* if we are performing a simple font format check, exit immediately */
604       /* (this is here for pure CFF)                                       */
605       if ( face_index < 0 )
606       {
607         cffface->num_faces = (FT_Long)cff->num_faces;
608         return FT_Err_Ok;
609       }
610 
611       cff->pshinter = pshinter;
612       cff->psnames  = psnames;
613 
614       cffface->face_index = face_index & 0xFFFF;
615 
616       /* Complement the root flags with some interesting information. */
617       /* Note that this is only necessary for pure CFF and CEF fonts; */
618       /* SFNT based fonts use the `name' table instead.               */
619 
620       cffface->num_glyphs = (FT_Long)cff->num_glyphs;
621 
622       dict = &cff->top_font.font_dict;
623 
624       /* we need the `PSNames' module for CFF and CEF formats */
625       /* which aren't CID-keyed                               */
626       if ( dict->cid_registry == 0xFFFFU && !psnames )
627       {
628         FT_ERROR(( "cff_face_init:"
629                    " cannot open CFF & CEF fonts\n"
630                    "              "
631                    " without the `PSNames' module\n" ));
632         error = FT_THROW( Missing_Module );
633         goto Exit;
634       }
635 
636 #ifdef FT_DEBUG_LEVEL_TRACE
637       {
638         FT_UInt     idx;
639         FT_String*  s;
640 
641 
642         FT_TRACE4(( "SIDs\n" ));
643 
644         /* dump string index, including default strings for convenience */
645         for ( idx = 0; idx <= 390; idx++ )
646         {
647           s = cff_index_get_sid_string( cff, idx );
648           if ( s )
649             FT_TRACE4(( "  %5d %s\n", idx, s ));
650         }
651 
652         /* In Multiple Master CFFs, two SIDs hold the Normalize Design  */
653         /* Vector (NDV) and Convert Design Vector (CDV) charstrings,    */
654         /* which may contain NULL bytes in the middle of the data, too. */
655         /* We thus access `cff->strings' directly.                      */
656         for ( idx = 1; idx < cff->num_strings; idx++ )
657         {
658           FT_Byte*    s1    = cff->strings[idx - 1];
659           FT_Byte*    s2    = cff->strings[idx];
660           FT_PtrDist  s1len = s2 - s1 - 1; /* without the final NULL byte */
661           FT_PtrDist  l;
662 
663 
664           FT_TRACE4(( "  %5d ", idx + 390 ));
665           for ( l = 0; l < s1len; l++ )
666             FT_TRACE4(( "%c", s1[l] ));
667           FT_TRACE4(( "\n" ));
668         }
669 
670         /* print last element */
671         if ( cff->num_strings )
672         {
673           FT_Byte*    s1    = cff->strings[cff->num_strings - 1];
674           FT_Byte*    s2    = cff->string_pool + cff->string_pool_size;
675           FT_PtrDist  s1len = s2 - s1 - 1;
676           FT_PtrDist  l;
677 
678 
679           FT_TRACE4(( "  %5d ", cff->num_strings + 390 ));
680           for ( l = 0; l < s1len; l++ )
681             FT_TRACE4(( "%c", s1[l] ));
682           FT_TRACE4(( "\n" ));
683         }
684       }
685 #endif /* FT_DEBUG_LEVEL_TRACE */
686 
687 
688 
689 #ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
690 
691     {
692       FT_Service_MultiMasters  mm = (FT_Service_MultiMasters)face->mm;
693 
694       FT_Int  instance_index = face_index >> 16;
695 
696 
697       if ( FT_HAS_MULTIPLE_MASTERS( cffface ) &&
698            mm                                 &&
699            instance_index > 0                 )
700       {
701         FT_MM_Var*  mm_var;
702 
703 
704         error = mm->get_mm_var( cffface, NULL );
705         if ( error )
706           goto Exit;
707 
708         mm->get_var_blend( cffface, NULL, NULL, &mm_var );
709 
710         if ( mm_var->namedstyle )
711         {
712           FT_Var_Named_Style*  named_style;
713           FT_String*           style_name;
714 
715 
716           /* in `face_index', the instance index starts with value 1 */
717           named_style = mm_var->namedstyle + instance_index - 1;
718           error = sfnt->get_name( face,
719                                   (FT_UShort)named_style->strid,
720                                   &style_name );
721           if ( error )
722             goto Exit;
723 
724           /* set style name; if already set, replace it */
725           if ( face->root.style_name )
726             FT_FREE( face->root.style_name );
727           face->root.style_name = style_name;
728 
729           /* finally, select the named instance */
730           error = mm->set_var_design( cffface,
731                                       mm_var->num_axis,
732                                       named_style->coords );
733           if ( error )
734             goto Exit;
735         }
736       }
737     }
738 
739 #endif /* TT_CONFIG_OPTION_GX_VAR_SUPPORT */
740 
741 
742 
743       if ( !dict->has_font_matrix )
744         dict->units_per_em = pure_cff ? 1000 : face->root.units_per_EM;
745 
746       /* Normalize the font matrix so that `matrix->yy' is 1; if  */
747       /* it is zero, we use `matrix->yx' instead.  The scaling is */
748       /* done with `units_per_em' then (at this point, it already */
749       /* contains the scaling factor, but without normalization   */
750       /* of the matrix).                                          */
751       /*                                                          */
752       /* Note that the offsets must be expressed in integer font  */
753       /* units.                                                   */
754 
755       {
756         FT_Matrix*  matrix = &dict->font_matrix;
757         FT_Vector*  offset = &dict->font_offset;
758         FT_ULong*   upm    = &dict->units_per_em;
759         FT_Fixed    temp;
760 
761 
762         temp = matrix->yy ? FT_ABS( matrix->yy )
763                           : FT_ABS( matrix->yx );
764 
765         if ( temp != 0x10000L )
766         {
767           *upm = (FT_ULong)FT_DivFix( (FT_Long)*upm, temp );
768 
769           matrix->xx = FT_DivFix( matrix->xx, temp );
770           matrix->yx = FT_DivFix( matrix->yx, temp );
771           matrix->xy = FT_DivFix( matrix->xy, temp );
772           matrix->yy = FT_DivFix( matrix->yy, temp );
773           offset->x  = FT_DivFix( offset->x,  temp );
774           offset->y  = FT_DivFix( offset->y,  temp );
775         }
776 
777         offset->x >>= 16;
778         offset->y >>= 16;
779       }
780 
781       for ( i = cff->num_subfonts; i > 0; i-- )
782       {
783         CFF_FontRecDict  sub = &cff->subfonts[i - 1]->font_dict;
784         CFF_FontRecDict  top = &cff->top_font.font_dict;
785 
786         FT_Matrix*  matrix;
787         FT_Vector*  offset;
788         FT_ULong*   upm;
789         FT_Fixed    temp;
790 
791 
792         if ( sub->has_font_matrix )
793         {
794           FT_Long  scaling;
795 
796 
797           /* if we have a top-level matrix, */
798           /* concatenate the subfont matrix */
799 
800           if ( top->has_font_matrix )
801           {
802             if ( top->units_per_em > 1 && sub->units_per_em > 1 )
803               scaling = (FT_Long)FT_MIN( top->units_per_em,
804                                          sub->units_per_em );
805             else
806               scaling = 1;
807 
808             FT_Matrix_Multiply_Scaled( &top->font_matrix,
809                                        &sub->font_matrix,
810                                        scaling );
811             FT_Vector_Transform_Scaled( &sub->font_offset,
812                                         &top->font_matrix,
813                                         scaling );
814 
815             sub->units_per_em = (FT_ULong)
816                                   FT_MulDiv( (FT_Long)sub->units_per_em,
817                                              (FT_Long)top->units_per_em,
818                                              scaling );
819           }
820         }
821         else
822         {
823           sub->font_matrix = top->font_matrix;
824           sub->font_offset = top->font_offset;
825 
826           sub->units_per_em = top->units_per_em;
827         }
828 
829         matrix = &sub->font_matrix;
830         offset = &sub->font_offset;
831         upm    = &sub->units_per_em;
832 
833         temp = matrix->yy ? FT_ABS( matrix->yy )
834                           : FT_ABS( matrix->yx );
835 
836 
837         if ( temp != 0x10000L )
838         {
839           *upm = (FT_ULong)FT_DivFix( (FT_Long)*upm, temp );
840 
841           matrix->xx = FT_DivFix( matrix->xx, temp );
842           matrix->yx = FT_DivFix( matrix->yx, temp );
843           matrix->xy = FT_DivFix( matrix->xy, temp );
844           matrix->yy = FT_DivFix( matrix->yy, temp );
845           offset->x  = FT_DivFix( offset->x,  temp );
846           offset->y  = FT_DivFix( offset->y,  temp );
847         }
848 
849         offset->x >>= 16;
850         offset->y >>= 16;
851       }
852 
853       if ( pure_cff )
854       {
855         char*  style_name = NULL;
856 
857 
858         /* set up num_faces */
859         cffface->num_faces = (FT_Long)cff->num_faces;
860 
861         /* compute number of glyphs */
862         if ( dict->cid_registry != 0xFFFFU )
863           cffface->num_glyphs = (FT_Long)( cff->charset.max_cid + 1 );
864         else
865           cffface->num_glyphs = (FT_Long)cff->charstrings_index.count;
866 
867         /* set global bbox, as well as EM size */
868         cffface->bbox.xMin =   dict->font_bbox.xMin            >> 16;
869         cffface->bbox.yMin =   dict->font_bbox.yMin            >> 16;
870         /* no `U' suffix here to 0xFFFF! */
871         cffface->bbox.xMax = ( dict->font_bbox.xMax + 0xFFFF ) >> 16;
872         cffface->bbox.yMax = ( dict->font_bbox.yMax + 0xFFFF ) >> 16;
873 
874         cffface->units_per_EM = (FT_UShort)( dict->units_per_em );
875 
876         cffface->ascender  = (FT_Short)( cffface->bbox.yMax );
877         cffface->descender = (FT_Short)( cffface->bbox.yMin );
878 
879         cffface->height = (FT_Short)( ( cffface->units_per_EM * 12 ) / 10 );
880         if ( cffface->height < cffface->ascender - cffface->descender )
881           cffface->height = (FT_Short)( cffface->ascender - cffface->descender );
882 
883         cffface->underline_position  =
884           (FT_Short)( dict->underline_position >> 16 );
885         cffface->underline_thickness =
886           (FT_Short)( dict->underline_thickness >> 16 );
887 
888         /* retrieve font family & style name */
889         cffface->family_name = cff_index_get_name(
890                                  cff,
891                                  (FT_UInt)( face_index & 0xFFFF ) );
892         if ( cffface->family_name )
893         {
894           char*  full   = cff_index_get_sid_string( cff,
895                                                     dict->full_name );
896           char*  fullp  = full;
897           char*  family = cffface->family_name;
898           char*  family_name = NULL;
899 
900 
901           remove_subset_prefix( cffface->family_name );
902 
903           if ( dict->family_name )
904           {
905             family_name = cff_index_get_sid_string( cff,
906                                                     dict->family_name );
907             if ( family_name )
908               family = family_name;
909           }
910 
911           /* We try to extract the style name from the full name.   */
912           /* We need to ignore spaces and dashes during the search. */
913           if ( full && family )
914           {
915             while ( *fullp )
916             {
917               /* skip common characters at the start of both strings */
918               if ( *fullp == *family )
919               {
920                 family++;
921                 fullp++;
922                 continue;
923               }
924 
925               /* ignore spaces and dashes in full name during comparison */
926               if ( *fullp == ' ' || *fullp == '-' )
927               {
928                 fullp++;
929                 continue;
930               }
931 
932               /* ignore spaces and dashes in family name during comparison */
933               if ( *family == ' ' || *family == '-' )
934               {
935                 family++;
936                 continue;
937               }
938 
939               if ( !*family && *fullp )
940               {
941                 /* The full name begins with the same characters as the  */
942                 /* family name, with spaces and dashes removed.  In this */
943                 /* case, the remaining string in `fullp' will be used as */
944                 /* the style name.                                       */
945                 style_name = cff_strcpy( memory, fullp );
946 
947                 /* remove the style part from the family name (if present) */
948                 remove_style( cffface->family_name, style_name );
949               }
950               break;
951             }
952           }
953         }
954         else
955         {
956           char  *cid_font_name =
957                    cff_index_get_sid_string( cff,
958                                              dict->cid_font_name );
959 
960 
961           /* do we have a `/FontName' for a CID-keyed font? */
962           if ( cid_font_name )
963             cffface->family_name = cff_strcpy( memory, cid_font_name );
964         }
965 
966         if ( style_name )
967           cffface->style_name = style_name;
968         else
969           /* assume "Regular" style if we don't know better */
970           cffface->style_name = cff_strcpy( memory, (char *)"Regular" );
971 
972         /*******************************************************************/
973         /*                                                                 */
974         /* Compute face flags.                                             */
975         /*                                                                 */
976         flags = FT_FACE_FLAG_SCALABLE   | /* scalable outlines */
977                 FT_FACE_FLAG_HORIZONTAL | /* horizontal data   */
978                 FT_FACE_FLAG_HINTER;      /* has native hinter */
979 
980         if ( sfnt_format )
981           flags |= FT_FACE_FLAG_SFNT;
982 
983         /* fixed width font? */
984         if ( dict->is_fixed_pitch )
985           flags |= FT_FACE_FLAG_FIXED_WIDTH;
986 
987   /* XXX: WE DO NOT SUPPORT KERNING METRICS IN THE GPOS TABLE FOR NOW */
988 #if 0
989         /* kerning available? */
990         if ( face->kern_pairs )
991           flags |= FT_FACE_FLAG_KERNING;
992 #endif
993 
994         cffface->face_flags |= flags;
995 
996         /*******************************************************************/
997         /*                                                                 */
998         /* Compute style flags.                                            */
999         /*                                                                 */
1000         flags = 0;
1001 
1002         if ( dict->italic_angle )
1003           flags |= FT_STYLE_FLAG_ITALIC;
1004 
1005         {
1006           char  *weight = cff_index_get_sid_string( cff,
1007                                                     dict->weight );
1008 
1009 
1010           if ( weight )
1011             if ( !ft_strcmp( weight, "Bold"  ) ||
1012                  !ft_strcmp( weight, "Black" ) )
1013               flags |= FT_STYLE_FLAG_BOLD;
1014         }
1015 
1016         /* double check */
1017         if ( !(flags & FT_STYLE_FLAG_BOLD) && cffface->style_name )
1018           if ( !ft_strncmp( cffface->style_name, "Bold", 4 )  ||
1019                !ft_strncmp( cffface->style_name, "Black", 5 ) )
1020             flags |= FT_STYLE_FLAG_BOLD;
1021 
1022         cffface->style_flags = flags;
1023       }
1024 
1025 
1026 #ifndef FT_CONFIG_OPTION_NO_GLYPH_NAMES
1027       /* CID-keyed CFF fonts don't have glyph names -- the SFNT loader */
1028       /* has unset this flag because of the 3.0 `post' table.          */
1029       if ( dict->cid_registry == 0xFFFFU )
1030         cffface->face_flags |= FT_FACE_FLAG_GLYPH_NAMES;
1031 #endif
1032 
1033       if ( dict->cid_registry != 0xFFFFU && pure_cff )
1034         cffface->face_flags |= FT_FACE_FLAG_CID_KEYED;
1035 
1036 
1037       /*******************************************************************/
1038       /*                                                                 */
1039       /* Compute char maps.                                              */
1040       /*                                                                 */
1041 
1042       /* Try to synthesize a Unicode charmap if there is none available */
1043       /* already.  If an OpenType font contains a Unicode "cmap", we    */
1044       /* will use it, whatever be in the CFF part of the file.          */
1045       {
1046         FT_CharMapRec  cmaprec;
1047         FT_CharMap     cmap;
1048         FT_UInt        nn;
1049         CFF_Encoding   encoding = &cff->encoding;
1050 
1051 
1052         for ( nn = 0; nn < (FT_UInt)cffface->num_charmaps; nn++ )
1053         {
1054           cmap = cffface->charmaps[nn];
1055 
1056           /* Windows Unicode? */
1057           if ( cmap->platform_id == TT_PLATFORM_MICROSOFT &&
1058                cmap->encoding_id == TT_MS_ID_UNICODE_CS   )
1059             goto Skip_Unicode;
1060 
1061           /* Apple Unicode platform id? */
1062           if ( cmap->platform_id == TT_PLATFORM_APPLE_UNICODE )
1063             goto Skip_Unicode; /* Apple Unicode */
1064         }
1065 
1066         /* since CID-keyed fonts don't contain glyph names, we can't */
1067         /* construct a cmap                                          */
1068         if ( pure_cff && cff->top_font.font_dict.cid_registry != 0xFFFFU )
1069           goto Exit;
1070 
1071         /* we didn't find a Unicode charmap -- synthesize one */
1072         cmaprec.face        = cffface;
1073         cmaprec.platform_id = TT_PLATFORM_MICROSOFT;
1074         cmaprec.encoding_id = TT_MS_ID_UNICODE_CS;
1075         cmaprec.encoding    = FT_ENCODING_UNICODE;
1076 
1077         nn = (FT_UInt)cffface->num_charmaps;
1078 
1079         error = FT_CMap_New( &CFF_CMAP_UNICODE_CLASS_REC_GET, NULL,
1080                              &cmaprec, NULL );
1081         if ( error                                      &&
1082              FT_ERR_NEQ( error, No_Unicode_Glyph_Name ) )
1083           goto Exit;
1084         error = FT_Err_Ok;
1085 
1086         /* if no Unicode charmap was previously selected, select this one */
1087         if ( !cffface->charmap && nn != (FT_UInt)cffface->num_charmaps )
1088           cffface->charmap = cffface->charmaps[nn];
1089 
1090       Skip_Unicode:
1091         if ( encoding->count > 0 )
1092         {
1093           FT_CMap_Class  clazz;
1094 
1095 
1096           cmaprec.face        = cffface;
1097           cmaprec.platform_id = TT_PLATFORM_ADOBE;  /* Adobe platform id */
1098 
1099           if ( encoding->offset == 0 )
1100           {
1101             cmaprec.encoding_id = TT_ADOBE_ID_STANDARD;
1102             cmaprec.encoding    = FT_ENCODING_ADOBE_STANDARD;
1103             clazz               = &CFF_CMAP_ENCODING_CLASS_REC_GET;
1104           }
1105           else if ( encoding->offset == 1 )
1106           {
1107             cmaprec.encoding_id = TT_ADOBE_ID_EXPERT;
1108             cmaprec.encoding    = FT_ENCODING_ADOBE_EXPERT;
1109             clazz               = &CFF_CMAP_ENCODING_CLASS_REC_GET;
1110           }
1111           else
1112           {
1113             cmaprec.encoding_id = TT_ADOBE_ID_CUSTOM;
1114             cmaprec.encoding    = FT_ENCODING_ADOBE_CUSTOM;
1115             clazz               = &CFF_CMAP_ENCODING_CLASS_REC_GET;
1116           }
1117 
1118           error = FT_CMap_New( clazz, NULL, &cmaprec, NULL );
1119         }
1120       }
1121     }
1122 
1123   Exit:
1124     return error;
1125   }
1126 
1127 
1128   FT_LOCAL_DEF( void )
cff_face_done(FT_Face cffface)1129   cff_face_done( FT_Face  cffface )         /* CFF_Face */
1130   {
1131     CFF_Face      face = (CFF_Face)cffface;
1132     FT_Memory     memory;
1133     SFNT_Service  sfnt;
1134 
1135 
1136     if ( !face )
1137       return;
1138 
1139     memory = cffface->memory;
1140     sfnt   = (SFNT_Service)face->sfnt;
1141 
1142     if ( sfnt )
1143       sfnt->done_face( face );
1144 
1145     {
1146       CFF_Font  cff = (CFF_Font)face->extra.data;
1147 
1148 
1149       if ( cff )
1150       {
1151         cff_font_done( cff );
1152         FT_FREE( face->extra.data );
1153       }
1154     }
1155 
1156 #ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
1157     cff_done_blend( face );
1158     face->blend = NULL;
1159 #endif
1160   }
1161 
1162 
1163   FT_LOCAL_DEF( FT_Error )
cff_driver_init(FT_Module module)1164   cff_driver_init( FT_Module  module )        /* CFF_Driver */
1165   {
1166     CFF_Driver  driver = (CFF_Driver)module;
1167 
1168 
1169     /* set default property values, cf. `ftcffdrv.h' */
1170 #ifdef CFF_CONFIG_OPTION_OLD_ENGINE
1171     driver->hinting_engine = FT_CFF_HINTING_FREETYPE;
1172 #else
1173     driver->hinting_engine = FT_CFF_HINTING_ADOBE;
1174 #endif
1175 
1176     driver->no_stem_darkening = TRUE;
1177 
1178     driver->darken_params[0] = CFF_CONFIG_OPTION_DARKENING_PARAMETER_X1;
1179     driver->darken_params[1] = CFF_CONFIG_OPTION_DARKENING_PARAMETER_Y1;
1180     driver->darken_params[2] = CFF_CONFIG_OPTION_DARKENING_PARAMETER_X2;
1181     driver->darken_params[3] = CFF_CONFIG_OPTION_DARKENING_PARAMETER_Y2;
1182     driver->darken_params[4] = CFF_CONFIG_OPTION_DARKENING_PARAMETER_X3;
1183     driver->darken_params[5] = CFF_CONFIG_OPTION_DARKENING_PARAMETER_Y3;
1184     driver->darken_params[6] = CFF_CONFIG_OPTION_DARKENING_PARAMETER_X4;
1185     driver->darken_params[7] = CFF_CONFIG_OPTION_DARKENING_PARAMETER_Y4;
1186 
1187     return FT_Err_Ok;
1188   }
1189 
1190 
1191   FT_LOCAL_DEF( void )
cff_driver_done(FT_Module module)1192   cff_driver_done( FT_Module  module )        /* CFF_Driver */
1193   {
1194     FT_UNUSED( module );
1195   }
1196 
1197 
1198 /* END */
1199