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