• 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
728           mm = (FT_Service_MultiMasters)face->mm;
729         FT_Service_MetricsVariations
730           var = (FT_Service_MetricsVariations)face->face_var;
731 
732         FT_UInt  instance_index = (FT_UInt)face_index >> 16;
733 
734 
735         if ( FT_HAS_MULTIPLE_MASTERS( cffface ) &&
736              mm                                 &&
737              instance_index > 0                 )
738         {
739           error = mm->set_instance( cffface, instance_index );
740           if ( error )
741             goto Exit;
742 
743           if ( var )
744             var->metrics_adjust( cffface );
745         }
746       }
747 #endif /* TT_CONFIG_OPTION_GX_VAR_SUPPORT */
748 
749       if ( !dict->has_font_matrix )
750         dict->units_per_em = pure_cff ? 1000 : face->root.units_per_EM;
751 
752       /* Normalize the font matrix so that `matrix->yy' is 1; if  */
753       /* it is zero, we use `matrix->yx' instead.  The scaling is */
754       /* done with `units_per_em' then (at this point, it already */
755       /* contains the scaling factor, but without normalization   */
756       /* of the matrix).                                          */
757       /*                                                          */
758       /* Note that the offsets must be expressed in integer font  */
759       /* units.                                                   */
760 
761       {
762         FT_Matrix*  matrix = &dict->font_matrix;
763         FT_Vector*  offset = &dict->font_offset;
764         FT_ULong*   upm    = &dict->units_per_em;
765         FT_Fixed    temp;
766 
767 
768         temp = matrix->yy ? FT_ABS( matrix->yy )
769                           : FT_ABS( matrix->yx );
770 
771         if ( temp != 0x10000L )
772         {
773           *upm = (FT_ULong)FT_DivFix( (FT_Long)*upm, temp );
774 
775           matrix->xx = FT_DivFix( matrix->xx, temp );
776           matrix->yx = FT_DivFix( matrix->yx, temp );
777           matrix->xy = FT_DivFix( matrix->xy, temp );
778           matrix->yy = FT_DivFix( matrix->yy, temp );
779           offset->x  = FT_DivFix( offset->x,  temp );
780           offset->y  = FT_DivFix( offset->y,  temp );
781         }
782 
783         offset->x >>= 16;
784         offset->y >>= 16;
785       }
786 
787       for ( i = cff->num_subfonts; i > 0; i-- )
788       {
789         CFF_FontRecDict  sub = &cff->subfonts[i - 1]->font_dict;
790         CFF_FontRecDict  top = &cff->top_font.font_dict;
791 
792         FT_Matrix*  matrix;
793         FT_Vector*  offset;
794         FT_ULong*   upm;
795         FT_Fixed    temp;
796 
797 
798         if ( sub->has_font_matrix )
799         {
800           FT_Long  scaling;
801 
802 
803           /* if we have a top-level matrix, */
804           /* concatenate the subfont matrix */
805 
806           if ( top->has_font_matrix )
807           {
808             if ( top->units_per_em > 1 && sub->units_per_em > 1 )
809               scaling = (FT_Long)FT_MIN( top->units_per_em,
810                                          sub->units_per_em );
811             else
812               scaling = 1;
813 
814             FT_Matrix_Multiply_Scaled( &top->font_matrix,
815                                        &sub->font_matrix,
816                                        scaling );
817             FT_Vector_Transform_Scaled( &sub->font_offset,
818                                         &top->font_matrix,
819                                         scaling );
820 
821             sub->units_per_em = (FT_ULong)
822                                   FT_MulDiv( (FT_Long)sub->units_per_em,
823                                              (FT_Long)top->units_per_em,
824                                              scaling );
825           }
826         }
827         else
828         {
829           sub->font_matrix = top->font_matrix;
830           sub->font_offset = top->font_offset;
831 
832           sub->units_per_em = top->units_per_em;
833         }
834 
835         matrix = &sub->font_matrix;
836         offset = &sub->font_offset;
837         upm    = &sub->units_per_em;
838 
839         temp = matrix->yy ? FT_ABS( matrix->yy )
840                           : FT_ABS( matrix->yx );
841 
842 
843         if ( temp != 0x10000L )
844         {
845           *upm = (FT_ULong)FT_DivFix( (FT_Long)*upm, temp );
846 
847           matrix->xx = FT_DivFix( matrix->xx, temp );
848           matrix->yx = FT_DivFix( matrix->yx, temp );
849           matrix->xy = FT_DivFix( matrix->xy, temp );
850           matrix->yy = FT_DivFix( matrix->yy, temp );
851           offset->x  = FT_DivFix( offset->x,  temp );
852           offset->y  = FT_DivFix( offset->y,  temp );
853         }
854 
855         offset->x >>= 16;
856         offset->y >>= 16;
857       }
858 
859       if ( pure_cff )
860       {
861         char*  style_name = NULL;
862 
863 
864         /* set up num_faces */
865         cffface->num_faces = (FT_Long)cff->num_faces;
866 
867         /* compute number of glyphs */
868         if ( dict->cid_registry != 0xFFFFU )
869           cffface->num_glyphs = (FT_Long)( cff->charset.max_cid + 1 );
870         else
871           cffface->num_glyphs = (FT_Long)cff->charstrings_index.count;
872 
873         /* set global bbox, as well as EM size */
874         cffface->bbox.xMin =   dict->font_bbox.xMin            >> 16;
875         cffface->bbox.yMin =   dict->font_bbox.yMin            >> 16;
876         /* no `U' suffix here to 0xFFFF! */
877         cffface->bbox.xMax = ( dict->font_bbox.xMax + 0xFFFF ) >> 16;
878         cffface->bbox.yMax = ( dict->font_bbox.yMax + 0xFFFF ) >> 16;
879 
880         cffface->units_per_EM = (FT_UShort)( dict->units_per_em );
881 
882         cffface->ascender  = (FT_Short)( cffface->bbox.yMax );
883         cffface->descender = (FT_Short)( cffface->bbox.yMin );
884 
885         cffface->height = (FT_Short)( ( cffface->units_per_EM * 12 ) / 10 );
886         if ( cffface->height < cffface->ascender - cffface->descender )
887           cffface->height = (FT_Short)( cffface->ascender -
888                                         cffface->descender );
889 
890         cffface->underline_position  =
891           (FT_Short)( dict->underline_position >> 16 );
892         cffface->underline_thickness =
893           (FT_Short)( dict->underline_thickness >> 16 );
894 
895         /* retrieve font family & style name */
896         if ( dict->family_name )
897         {
898           char*  family_name;
899 
900 
901           family_name = cff_index_get_sid_string( cff, dict->family_name );
902           if ( family_name )
903             cffface->family_name = cff_strcpy( memory, family_name );
904         }
905 
906         if ( !cffface->family_name )
907         {
908           cffface->family_name = cff_index_get_name(
909                                    cff,
910                                    (FT_UInt)( face_index & 0xFFFF ) );
911           if ( cffface->family_name )
912             remove_subset_prefix( cffface->family_name );
913         }
914 
915         if ( cffface->family_name )
916         {
917           char*  full   = cff_index_get_sid_string( cff,
918                                                     dict->full_name );
919           char*  fullp  = full;
920           char*  family = cffface->family_name;
921 
922 
923           /* We try to extract the style name from the full name.   */
924           /* We need to ignore spaces and dashes during the search. */
925           if ( full && family )
926           {
927             while ( *fullp )
928             {
929               /* skip common characters at the start of both strings */
930               if ( *fullp == *family )
931               {
932                 family++;
933                 fullp++;
934                 continue;
935               }
936 
937               /* ignore spaces and dashes in full name during comparison */
938               if ( *fullp == ' ' || *fullp == '-' )
939               {
940                 fullp++;
941                 continue;
942               }
943 
944               /* ignore spaces and dashes in family name during comparison */
945               if ( *family == ' ' || *family == '-' )
946               {
947                 family++;
948                 continue;
949               }
950 
951               if ( !*family && *fullp )
952               {
953                 /* The full name begins with the same characters as the  */
954                 /* family name, with spaces and dashes removed.  In this */
955                 /* case, the remaining string in `fullp' will be used as */
956                 /* the style name.                                       */
957                 style_name = cff_strcpy( memory, fullp );
958 
959                 /* remove the style part from the family name (if present) */
960                 if ( style_name )
961                   remove_style( cffface->family_name, style_name );
962               }
963               break;
964             }
965           }
966         }
967         else
968         {
969           char  *cid_font_name =
970                    cff_index_get_sid_string( cff,
971                                              dict->cid_font_name );
972 
973 
974           /* do we have a `/FontName' for a CID-keyed font? */
975           if ( cid_font_name )
976             cffface->family_name = cff_strcpy( memory, cid_font_name );
977         }
978 
979         if ( style_name )
980           cffface->style_name = style_name;
981         else
982           /* assume "Regular" style if we don't know better */
983           cffface->style_name = cff_strcpy( memory, "Regular" );
984 
985         /********************************************************************
986          *
987          * Compute face flags.
988          */
989         flags = FT_FACE_FLAG_SCALABLE   | /* scalable outlines */
990                 FT_FACE_FLAG_HORIZONTAL | /* horizontal data   */
991                 FT_FACE_FLAG_HINTER;      /* has native hinter */
992 
993         if ( sfnt_format )
994           flags |= FT_FACE_FLAG_SFNT;
995 
996         /* fixed width font? */
997         if ( dict->is_fixed_pitch )
998           flags |= FT_FACE_FLAG_FIXED_WIDTH;
999 
1000   /* XXX: WE DO NOT SUPPORT KERNING METRICS IN THE GPOS TABLE FOR NOW */
1001 #if 0
1002         /* kerning available? */
1003         if ( face->kern_pairs )
1004           flags |= FT_FACE_FLAG_KERNING;
1005 #endif
1006 
1007         cffface->face_flags |= flags;
1008 
1009         /********************************************************************
1010          *
1011          * Compute style flags.
1012          */
1013         flags = 0;
1014 
1015         if ( dict->italic_angle )
1016           flags |= FT_STYLE_FLAG_ITALIC;
1017 
1018         {
1019           char  *weight = cff_index_get_sid_string( cff,
1020                                                     dict->weight );
1021 
1022 
1023           if ( weight )
1024             if ( !ft_strcmp( weight, "Bold"  ) ||
1025                  !ft_strcmp( weight, "Black" ) )
1026               flags |= FT_STYLE_FLAG_BOLD;
1027         }
1028 
1029         /* double check */
1030         if ( !(flags & FT_STYLE_FLAG_BOLD) && cffface->style_name )
1031           if ( !ft_strncmp( cffface->style_name, "Bold", 4 )  ||
1032                !ft_strncmp( cffface->style_name, "Black", 5 ) )
1033             flags |= FT_STYLE_FLAG_BOLD;
1034 
1035         cffface->style_flags = flags;
1036       }
1037 
1038 #ifndef FT_CONFIG_OPTION_NO_GLYPH_NAMES
1039       /* CID-keyed CFF or CFF2 fonts don't have glyph names -- the SFNT */
1040       /* loader has unset this flag because of the 3.0 `post' table.    */
1041       if ( dict->cid_registry == 0xFFFFU && !cff2 )
1042         cffface->face_flags |= FT_FACE_FLAG_GLYPH_NAMES;
1043 #endif
1044 
1045       if ( dict->cid_registry != 0xFFFFU && pure_cff )
1046         cffface->face_flags |= FT_FACE_FLAG_CID_KEYED;
1047 
1048       /********************************************************************
1049        *
1050        * Compute char maps.
1051        */
1052 
1053       /* Try to synthesize a Unicode charmap if there is none available */
1054       /* already.  If an OpenType font contains a Unicode "cmap", we    */
1055       /* will use it, whatever be in the CFF part of the file.          */
1056       {
1057         FT_CharMapRec  cmaprec;
1058         FT_CharMap     cmap;
1059         FT_Int         nn;
1060         CFF_Encoding   encoding = &cff->encoding;
1061 
1062 
1063         for ( nn = 0; nn < cffface->num_charmaps; nn++ )
1064         {
1065           cmap = cffface->charmaps[nn];
1066 
1067           /* Windows Unicode? */
1068           if ( cmap->platform_id == TT_PLATFORM_MICROSOFT &&
1069                cmap->encoding_id == TT_MS_ID_UNICODE_CS   )
1070             goto Skip_Unicode;
1071 
1072           /* Apple Unicode platform id? */
1073           if ( cmap->platform_id == TT_PLATFORM_APPLE_UNICODE )
1074             goto Skip_Unicode; /* Apple Unicode */
1075         }
1076 
1077         /* since CID-keyed fonts don't contain glyph names, we can't */
1078         /* construct a cmap                                          */
1079         if ( pure_cff && cff->top_font.font_dict.cid_registry != 0xFFFFU )
1080           goto Exit;
1081 
1082         /* we didn't find a Unicode charmap -- synthesize one */
1083         cmaprec.face        = cffface;
1084         cmaprec.platform_id = TT_PLATFORM_MICROSOFT;
1085         cmaprec.encoding_id = TT_MS_ID_UNICODE_CS;
1086         cmaprec.encoding    = FT_ENCODING_UNICODE;
1087 
1088         nn = cffface->num_charmaps;
1089 
1090         error = FT_CMap_New( &cff_cmap_unicode_class_rec, NULL,
1091                              &cmaprec, NULL );
1092         if ( error                                      &&
1093              FT_ERR_NEQ( error, No_Unicode_Glyph_Name ) &&
1094              FT_ERR_NEQ( error, Unimplemented_Feature ) )
1095           goto Exit;
1096         error = FT_Err_Ok;
1097 
1098         /* if no Unicode charmap was previously selected, select this one */
1099         if ( !cffface->charmap && nn != cffface->num_charmaps )
1100           cffface->charmap = cffface->charmaps[nn];
1101 
1102       Skip_Unicode:
1103         if ( encoding->count > 0 )
1104         {
1105           FT_CMap_Class  clazz;
1106 
1107 
1108           cmaprec.face        = cffface;
1109           cmaprec.platform_id = TT_PLATFORM_ADOBE;  /* Adobe platform id */
1110 
1111           if ( encoding->offset == 0 )
1112           {
1113             cmaprec.encoding_id = TT_ADOBE_ID_STANDARD;
1114             cmaprec.encoding    = FT_ENCODING_ADOBE_STANDARD;
1115             clazz               = &cff_cmap_encoding_class_rec;
1116           }
1117           else if ( encoding->offset == 1 )
1118           {
1119             cmaprec.encoding_id = TT_ADOBE_ID_EXPERT;
1120             cmaprec.encoding    = FT_ENCODING_ADOBE_EXPERT;
1121             clazz               = &cff_cmap_encoding_class_rec;
1122           }
1123           else
1124           {
1125             cmaprec.encoding_id = TT_ADOBE_ID_CUSTOM;
1126             cmaprec.encoding    = FT_ENCODING_ADOBE_CUSTOM;
1127             clazz               = &cff_cmap_encoding_class_rec;
1128           }
1129 
1130           error = FT_CMap_New( clazz, NULL, &cmaprec, NULL );
1131         }
1132       }
1133     }
1134 
1135   Exit:
1136     return error;
1137   }
1138 
1139 
1140   FT_LOCAL_DEF( void )
cff_face_done(FT_Face cffface)1141   cff_face_done( FT_Face  cffface )         /* CFF_Face */
1142   {
1143     CFF_Face      face = (CFF_Face)cffface;
1144     FT_Memory     memory;
1145     SFNT_Service  sfnt;
1146 
1147 
1148     if ( !face )
1149       return;
1150 
1151     memory = cffface->memory;
1152     sfnt   = (SFNT_Service)face->sfnt;
1153 
1154     if ( sfnt )
1155       sfnt->done_face( face );
1156 
1157     {
1158       CFF_Font  cff = (CFF_Font)face->extra.data;
1159 
1160 
1161       if ( cff )
1162       {
1163         cff_font_done( cff );
1164         FT_FREE( face->extra.data );
1165       }
1166     }
1167 
1168 #ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
1169     cff_done_blend( face );
1170     face->blend = NULL;
1171 #endif
1172   }
1173 
1174 
1175   FT_LOCAL_DEF( FT_Error )
cff_driver_init(FT_Module module)1176   cff_driver_init( FT_Module  module )        /* CFF_Driver */
1177   {
1178     PS_Driver  driver = (PS_Driver)module;
1179 
1180     FT_UInt32  seed;
1181 
1182 
1183     /* set default property values, cf. `ftcffdrv.h' */
1184     driver->hinting_engine = FT_HINTING_ADOBE;
1185 
1186     driver->no_stem_darkening = TRUE;
1187 
1188     driver->darken_params[0] = CFF_CONFIG_OPTION_DARKENING_PARAMETER_X1;
1189     driver->darken_params[1] = CFF_CONFIG_OPTION_DARKENING_PARAMETER_Y1;
1190     driver->darken_params[2] = CFF_CONFIG_OPTION_DARKENING_PARAMETER_X2;
1191     driver->darken_params[3] = CFF_CONFIG_OPTION_DARKENING_PARAMETER_Y2;
1192     driver->darken_params[4] = CFF_CONFIG_OPTION_DARKENING_PARAMETER_X3;
1193     driver->darken_params[5] = CFF_CONFIG_OPTION_DARKENING_PARAMETER_Y3;
1194     driver->darken_params[6] = CFF_CONFIG_OPTION_DARKENING_PARAMETER_X4;
1195     driver->darken_params[7] = CFF_CONFIG_OPTION_DARKENING_PARAMETER_Y4;
1196 
1197     /* compute random seed from some memory addresses */
1198     seed = (FT_UInt32)( (FT_Offset)(char*)&seed          ^
1199                         (FT_Offset)(char*)&module        ^
1200                         (FT_Offset)(char*)module->memory );
1201     seed = seed ^ ( seed >> 10 ) ^ ( seed >> 20 );
1202 
1203     driver->random_seed = (FT_Int32)seed;
1204     if ( driver->random_seed < 0 )
1205       driver->random_seed = -driver->random_seed;
1206     else if ( driver->random_seed == 0 )
1207       driver->random_seed = 123456789;
1208 
1209     return FT_Err_Ok;
1210   }
1211 
1212 
1213   FT_LOCAL_DEF( void )
cff_driver_done(FT_Module module)1214   cff_driver_done( FT_Module  module )        /* CFF_Driver */
1215   {
1216     FT_UNUSED( module );
1217   }
1218 
1219 
1220 /* END */
1221