• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /****************************************************************************
2  *
3  * cidobjs.c
4  *
5  *   CID 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 #include <freetype/internal/ftdebug.h>
20 #include <freetype/internal/ftstream.h>
21 
22 #include "cidgload.h"
23 #include "cidload.h"
24 
25 #include <freetype/internal/services/svpscmap.h>
26 #include <freetype/internal/psaux.h>
27 #include <freetype/internal/pshints.h>
28 #include <freetype/ftdriver.h>
29 
30 #include "ciderrs.h"
31 
32 
33   /**************************************************************************
34    *
35    * The macro FT_COMPONENT is used in trace mode.  It is an implicit
36    * parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log
37    * messages during execution.
38    */
39 #undef  FT_COMPONENT
40 #define FT_COMPONENT  cidobjs
41 
42 
43   /**************************************************************************
44    *
45    *                           SLOT  FUNCTIONS
46    *
47    */
48 
49   FT_LOCAL_DEF( void )
cid_slot_done(FT_GlyphSlot slot)50   cid_slot_done( FT_GlyphSlot  slot )
51   {
52     if ( slot->internal )
53       slot->internal->glyph_hints = NULL;
54   }
55 
56 
57   FT_LOCAL_DEF( FT_Error )
cid_slot_init(FT_GlyphSlot slot)58   cid_slot_init( FT_GlyphSlot  slot )
59   {
60     CID_Face          face;
61     PSHinter_Service  pshinter;
62 
63 
64     face     = (CID_Face)slot->face;
65     pshinter = (PSHinter_Service)face->pshinter;
66 
67     if ( pshinter )
68     {
69       FT_Module  module;
70 
71 
72       module = FT_Get_Module( slot->face->driver->root.library,
73                               "pshinter" );
74       if ( module )
75       {
76         T1_Hints_Funcs  funcs;
77 
78 
79         funcs = pshinter->get_t1_funcs( module );
80         slot->internal->glyph_hints = (void*)funcs;
81       }
82     }
83 
84     return 0;
85   }
86 
87 
88   /**************************************************************************
89    *
90    *                          SIZE  FUNCTIONS
91    *
92    */
93 
94 
95   static PSH_Globals_Funcs
cid_size_get_globals_funcs(CID_Size size)96   cid_size_get_globals_funcs( CID_Size  size )
97   {
98     CID_Face          face     = (CID_Face)size->root.face;
99     PSHinter_Service  pshinter = (PSHinter_Service)face->pshinter;
100     FT_Module         module;
101 
102 
103     module = FT_Get_Module( size->root.face->driver->root.library,
104                             "pshinter" );
105     return ( module && pshinter && pshinter->get_globals_funcs )
106            ? pshinter->get_globals_funcs( module )
107            : 0;
108   }
109 
110 
111   FT_LOCAL_DEF( void )
cid_size_done(FT_Size cidsize)112   cid_size_done( FT_Size  cidsize )         /* CID_Size */
113   {
114     CID_Size  size = (CID_Size)cidsize;
115 
116 
117     if ( cidsize->internal->module_data )
118     {
119       PSH_Globals_Funcs  funcs;
120 
121 
122       funcs = cid_size_get_globals_funcs( size );
123       if ( funcs )
124         funcs->destroy( (PSH_Globals)cidsize->internal->module_data );
125 
126       cidsize->internal->module_data = NULL;
127     }
128   }
129 
130 
131   FT_LOCAL_DEF( FT_Error )
cid_size_init(FT_Size cidsize)132   cid_size_init( FT_Size  cidsize )     /* CID_Size */
133   {
134     CID_Size           size  = (CID_Size)cidsize;
135     FT_Error           error = FT_Err_Ok;
136     PSH_Globals_Funcs  funcs = cid_size_get_globals_funcs( size );
137 
138 
139     if ( funcs )
140     {
141       PSH_Globals   globals;
142       CID_Face      face = (CID_Face)cidsize->face;
143       CID_FaceDict  dict = face->cid.font_dicts + face->root.face_index;
144       PS_Private    priv = &dict->private_dict;
145 
146 
147       error = funcs->create( cidsize->face->memory, priv, &globals );
148       if ( !error )
149         cidsize->internal->module_data = globals;
150     }
151 
152     return error;
153   }
154 
155 
156   FT_LOCAL( FT_Error )
cid_size_request(FT_Size size,FT_Size_Request req)157   cid_size_request( FT_Size          size,
158                     FT_Size_Request  req )
159   {
160     FT_Error  error;
161 
162     PSH_Globals_Funcs  funcs;
163 
164 
165     error = FT_Request_Metrics( size->face, req );
166     if ( error )
167       goto Exit;
168 
169     funcs = cid_size_get_globals_funcs( (CID_Size)size );
170 
171     if ( funcs )
172       funcs->set_scale( (PSH_Globals)size->internal->module_data,
173                         size->metrics.x_scale,
174                         size->metrics.y_scale,
175                         0, 0 );
176 
177   Exit:
178     return error;
179   }
180 
181 
182   /**************************************************************************
183    *
184    *                          FACE  FUNCTIONS
185    *
186    */
187 
188   /**************************************************************************
189    *
190    * @Function:
191    *   cid_face_done
192    *
193    * @Description:
194    *   Finalizes a given face object.
195    *
196    * @Input:
197    *   face ::
198    *     A pointer to the face object to destroy.
199    */
200   FT_LOCAL_DEF( void )
cid_face_done(FT_Face cidface)201   cid_face_done( FT_Face  cidface )         /* CID_Face */
202   {
203     CID_Face      face = (CID_Face)cidface;
204     FT_Memory     memory;
205     CID_FaceInfo  cid;
206     PS_FontInfo   info;
207 
208 
209     if ( !face )
210       return;
211 
212     cid    = &face->cid;
213     info   = &cid->font_info;
214     memory = cidface->memory;
215 
216     /* release subrs */
217     if ( face->subrs )
218     {
219       FT_UInt  n;
220 
221 
222       for ( n = 0; n < cid->num_dicts; n++ )
223       {
224         CID_Subrs  subr = face->subrs + n;
225 
226 
227         if ( subr->code )
228         {
229           FT_FREE( subr->code[0] );
230           FT_FREE( subr->code );
231         }
232       }
233 
234       FT_FREE( face->subrs );
235     }
236 
237     /* release FontInfo strings */
238     FT_FREE( info->version );
239     FT_FREE( info->notice );
240     FT_FREE( info->full_name );
241     FT_FREE( info->family_name );
242     FT_FREE( info->weight );
243 
244     /* release font dictionaries */
245     FT_FREE( cid->font_dicts );
246     cid->num_dicts = 0;
247 
248     /* release other strings */
249     FT_FREE( cid->cid_font_name );
250     FT_FREE( cid->registry );
251     FT_FREE( cid->ordering );
252 
253     cidface->family_name = NULL;
254     cidface->style_name  = NULL;
255 
256     FT_FREE( face->binary_data );
257     FT_FREE( face->cid_stream );
258   }
259 
260 
261   /**************************************************************************
262    *
263    * @Function:
264    *   cid_face_init
265    *
266    * @Description:
267    *   Initializes a given CID face object.
268    *
269    * @Input:
270    *   stream ::
271    *     The source font stream.
272    *
273    *   face_index ::
274    *     The index of the font face in the resource.
275    *
276    *   num_params ::
277    *     Number of additional generic parameters.  Ignored.
278    *
279    *   params ::
280    *     Additional generic parameters.  Ignored.
281    *
282    * @InOut:
283    *   face ::
284    *     The newly built face object.
285    *
286    * @Return:
287    *   FreeType error code.  0 means success.
288    */
289   FT_LOCAL_DEF( FT_Error )
cid_face_init(FT_Stream stream,FT_Face cidface,FT_Int face_index,FT_Int num_params,FT_Parameter * params)290   cid_face_init( FT_Stream      stream,
291                  FT_Face        cidface,        /* CID_Face */
292                  FT_Int         face_index,
293                  FT_Int         num_params,
294                  FT_Parameter*  params )
295   {
296     CID_Face          face = (CID_Face)cidface;
297     FT_Error          error;
298     PSAux_Service     psaux;
299     PSHinter_Service  pshinter;
300 
301     FT_UNUSED( num_params );
302     FT_UNUSED( params );
303     FT_UNUSED( stream );
304 
305 
306     cidface->num_faces = 1;
307 
308     psaux = (PSAux_Service)face->psaux;
309     if ( !psaux )
310     {
311       psaux = (PSAux_Service)FT_Get_Module_Interface(
312                 FT_FACE_LIBRARY( face ), "psaux" );
313 
314       if ( !psaux )
315       {
316         FT_ERROR(( "cid_face_init: cannot access `psaux' module\n" ));
317         error = FT_THROW( Missing_Module );
318         goto Exit;
319       }
320 
321       face->psaux = psaux;
322     }
323 
324     pshinter = (PSHinter_Service)face->pshinter;
325     if ( !pshinter )
326     {
327       pshinter = (PSHinter_Service)FT_Get_Module_Interface(
328                    FT_FACE_LIBRARY( face ), "pshinter" );
329 
330       face->pshinter = pshinter;
331     }
332 
333     FT_TRACE2(( "CID driver\n" ));
334 
335     /* open the tokenizer; this will also check the font format */
336     if ( FT_STREAM_SEEK( 0 ) )
337       goto Exit;
338 
339     error = cid_face_open( face, face_index );
340     if ( error )
341       goto Exit;
342 
343     /* if we just wanted to check the format, leave successfully now */
344     if ( face_index < 0 )
345       goto Exit;
346 
347     /* check the face index */
348     /* XXX: handle CID fonts with more than a single face */
349     if ( ( face_index & 0xFFFF ) != 0 )
350     {
351       FT_ERROR(( "cid_face_init: invalid face index\n" ));
352       error = FT_THROW( Invalid_Argument );
353       goto Exit;
354     }
355 
356     /* now load the font program into the face object */
357 
358     /* initialize the face object fields */
359 
360     /* set up root face fields */
361     {
362       CID_FaceInfo  cid  = &face->cid;
363       PS_FontInfo   info = &cid->font_info;
364 
365 
366       cidface->num_glyphs   = (FT_Long)cid->cid_count;
367       cidface->num_charmaps = 0;
368 
369       cidface->face_index = face_index & 0xFFFF;
370 
371       cidface->face_flags |= FT_FACE_FLAG_SCALABLE   | /* scalable outlines */
372                              FT_FACE_FLAG_HORIZONTAL | /* horizontal data   */
373                              FT_FACE_FLAG_HINTER;      /* has native hinter */
374 
375       if ( info->is_fixed_pitch )
376         cidface->face_flags |= FT_FACE_FLAG_FIXED_WIDTH;
377 
378       /* XXX: TODO: add kerning with .afm support */
379 
380       /* get style name -- be careful, some broken fonts only */
381       /* have a /FontName dictionary entry!                   */
382       cidface->family_name = info->family_name;
383       /* assume "Regular" style if we don't know better */
384       cidface->style_name = (char *)"Regular";
385       if ( cidface->family_name )
386       {
387         char*  full   = info->full_name;
388         char*  family = cidface->family_name;
389 
390 
391         if ( full )
392         {
393           while ( *full )
394           {
395             if ( *full == *family )
396             {
397               family++;
398               full++;
399             }
400             else
401             {
402               if ( *full == ' ' || *full == '-' )
403                 full++;
404               else if ( *family == ' ' || *family == '-' )
405                 family++;
406               else
407               {
408                 if ( !*family )
409                   cidface->style_name = full;
410                 break;
411               }
412             }
413           }
414         }
415       }
416       else
417       {
418         /* do we have a `/FontName'? */
419         if ( cid->cid_font_name )
420           cidface->family_name = cid->cid_font_name;
421       }
422 
423       /* compute style flags */
424       cidface->style_flags = 0;
425       if ( info->italic_angle )
426         cidface->style_flags |= FT_STYLE_FLAG_ITALIC;
427       if ( info->weight )
428       {
429         if ( !ft_strcmp( info->weight, "Bold"  ) ||
430              !ft_strcmp( info->weight, "Black" ) )
431           cidface->style_flags |= FT_STYLE_FLAG_BOLD;
432       }
433 
434       /* no embedded bitmap support */
435       cidface->num_fixed_sizes = 0;
436       cidface->available_sizes = NULL;
437 
438       cidface->bbox.xMin =   cid->font_bbox.xMin            >> 16;
439       cidface->bbox.yMin =   cid->font_bbox.yMin            >> 16;
440       /* no `U' suffix here to 0xFFFF! */
441       cidface->bbox.xMax = ( cid->font_bbox.xMax + 0xFFFF ) >> 16;
442       cidface->bbox.yMax = ( cid->font_bbox.yMax + 0xFFFF ) >> 16;
443 
444       if ( !cidface->units_per_EM )
445         cidface->units_per_EM = 1000;
446 
447       cidface->ascender  = (FT_Short)( cidface->bbox.yMax );
448       cidface->descender = (FT_Short)( cidface->bbox.yMin );
449 
450       cidface->height = (FT_Short)( ( cidface->units_per_EM * 12 ) / 10 );
451       if ( cidface->height < cidface->ascender - cidface->descender )
452         cidface->height = (FT_Short)( cidface->ascender - cidface->descender );
453 
454       cidface->underline_position  = (FT_Short)info->underline_position;
455       cidface->underline_thickness = (FT_Short)info->underline_thickness;
456     }
457 
458   Exit:
459     return error;
460   }
461 
462 
463   /**************************************************************************
464    *
465    * @Function:
466    *   cid_driver_init
467    *
468    * @Description:
469    *   Initializes a given CID driver object.
470    *
471    * @Input:
472    *   driver ::
473    *     A handle to the target driver object.
474    *
475    * @Return:
476    *   FreeType error code.  0 means success.
477    */
478   FT_LOCAL_DEF( FT_Error )
cid_driver_init(FT_Module module)479   cid_driver_init( FT_Module  module )
480   {
481     PS_Driver  driver = (PS_Driver)module;
482 
483     FT_UInt32  seed;
484 
485 
486     /* set default property values, cf. `ftt1drv.h' */
487     driver->hinting_engine = FT_HINTING_ADOBE;
488 
489     driver->no_stem_darkening = TRUE;
490 
491     driver->darken_params[0] = CFF_CONFIG_OPTION_DARKENING_PARAMETER_X1;
492     driver->darken_params[1] = CFF_CONFIG_OPTION_DARKENING_PARAMETER_Y1;
493     driver->darken_params[2] = CFF_CONFIG_OPTION_DARKENING_PARAMETER_X2;
494     driver->darken_params[3] = CFF_CONFIG_OPTION_DARKENING_PARAMETER_Y2;
495     driver->darken_params[4] = CFF_CONFIG_OPTION_DARKENING_PARAMETER_X3;
496     driver->darken_params[5] = CFF_CONFIG_OPTION_DARKENING_PARAMETER_Y3;
497     driver->darken_params[6] = CFF_CONFIG_OPTION_DARKENING_PARAMETER_X4;
498     driver->darken_params[7] = CFF_CONFIG_OPTION_DARKENING_PARAMETER_Y4;
499 
500     /* compute random seed from some memory addresses */
501     seed = (FT_UInt32)( (FT_Offset)(char*)&seed          ^
502                         (FT_Offset)(char*)&module        ^
503                         (FT_Offset)(char*)module->memory );
504     seed = seed ^ ( seed >> 10 ) ^ ( seed >> 20 );
505 
506     driver->random_seed = (FT_Int32)seed;
507     if ( driver->random_seed < 0 )
508       driver->random_seed = -driver->random_seed;
509     else if ( driver->random_seed == 0 )
510       driver->random_seed = 123456789;
511 
512     return FT_Err_Ok;
513   }
514 
515 
516   /**************************************************************************
517    *
518    * @Function:
519    *   cid_driver_done
520    *
521    * @Description:
522    *   Finalizes a given CID driver.
523    *
524    * @Input:
525    *   driver ::
526    *     A handle to the target CID driver.
527    */
528   FT_LOCAL_DEF( void )
cid_driver_done(FT_Module driver)529   cid_driver_done( FT_Module  driver )
530   {
531     FT_UNUSED( driver );
532   }
533 
534 
535 /* END */
536