Coverage Report

Created: 2021-08-22 09:07

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