feee38a4131b4ecd7df1b4b8ed3752568060f8bc
[platform/upstream/freetype2.git] / src / cff / cffgload.c
1 /****************************************************************************
2  *
3  * cffgload.c
4  *
5  *   OpenType Glyph Loader (body).
6  *
7  * Copyright (C) 1996-2020 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 #include <freetype/internal/sfnt.h>
22 #include <freetype/internal/ftcalc.h>
23 #include <freetype/internal/psaux.h>
24 #include <freetype/ftoutln.h>
25 #include <freetype/ftdriver.h>
26
27 #include "cffload.h"
28 #include "cffgload.h"
29
30 #include "cfferrs.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  cffgload
41
42
43   FT_LOCAL_DEF( FT_Error )
44   cff_get_glyph_data( TT_Face    face,
45                       FT_UInt    glyph_index,
46                       FT_Byte**  pointer,
47                       FT_ULong*  length )
48   {
49 #ifdef FT_CONFIG_OPTION_INCREMENTAL
50     /* For incremental fonts get the character data using the */
51     /* callback function.                                     */
52     if ( face->root.internal->incremental_interface )
53     {
54       FT_Data   data;
55       FT_Error  error =
56                   face->root.internal->incremental_interface->funcs->get_glyph_data(
57                     face->root.internal->incremental_interface->object,
58                     glyph_index, &data );
59
60
61       *pointer = (FT_Byte*)data.pointer;
62       *length  = (FT_ULong)data.length;
63
64       return error;
65     }
66     else
67 #endif /* FT_CONFIG_OPTION_INCREMENTAL */
68
69     {
70       CFF_Font  cff = (CFF_Font)(face->extra.data);
71
72
73       return cff_index_access_element( &cff->charstrings_index, glyph_index,
74                                        pointer, length );
75     }
76   }
77
78
79   FT_LOCAL_DEF( void )
80   cff_free_glyph_data( TT_Face    face,
81                        FT_Byte**  pointer,
82                        FT_ULong   length )
83   {
84 #ifndef FT_CONFIG_OPTION_INCREMENTAL
85     FT_UNUSED( length );
86 #endif
87
88 #ifdef FT_CONFIG_OPTION_INCREMENTAL
89     /* For incremental fonts get the character data using the */
90     /* callback function.                                     */
91     if ( face->root.internal->incremental_interface )
92     {
93       FT_Data  data;
94
95
96       data.pointer = *pointer;
97       data.length  = (FT_Int)length;
98
99       face->root.internal->incremental_interface->funcs->free_glyph_data(
100         face->root.internal->incremental_interface->object, &data );
101     }
102     else
103 #endif /* FT_CONFIG_OPTION_INCREMENTAL */
104
105     {
106       CFF_Font  cff = (CFF_Font)(face->extra.data);
107
108
109       cff_index_forget_element( &cff->charstrings_index, pointer );
110     }
111   }
112
113
114   /*************************************************************************/
115   /*************************************************************************/
116   /*************************************************************************/
117   /**********                                                      *********/
118   /**********                                                      *********/
119   /**********            COMPUTE THE MAXIMUM ADVANCE WIDTH         *********/
120   /**********                                                      *********/
121   /**********    The following code is in charge of computing      *********/
122   /**********    the maximum advance width of the font.  It        *********/
123   /**********    quickly processes each glyph charstring to        *********/
124   /**********    extract the value from either a `sbw' or `seac'   *********/
125   /**********    operator.                                         *********/
126   /**********                                                      *********/
127   /*************************************************************************/
128   /*************************************************************************/
129   /*************************************************************************/
130
131
132 #if 0 /* unused until we support pure CFF fonts */
133
134
135   FT_LOCAL_DEF( FT_Error )
136   cff_compute_max_advance( TT_Face  face,
137                            FT_Int*  max_advance )
138   {
139     FT_Error     error = FT_Err_Ok;
140     CFF_Decoder  decoder;
141     FT_Int       glyph_index;
142     CFF_Font     cff = (CFF_Font)face->other;
143
144     PSAux_Service            psaux         = (PSAux_Service)face->psaux;
145     const CFF_Decoder_Funcs  decoder_funcs = psaux->cff_decoder_funcs;
146
147
148     *max_advance = 0;
149
150     /* Initialize load decoder */
151     decoder_funcs->init( &decoder, face, 0, 0, 0, 0, 0, 0 );
152
153     decoder.builder.metrics_only = 1;
154     decoder.builder.load_points  = 0;
155
156     /* For each glyph, parse the glyph charstring and extract */
157     /* the advance width.                                     */
158     for ( glyph_index = 0; glyph_index < face->root.num_glyphs;
159           glyph_index++ )
160     {
161       FT_Byte*  charstring;
162       FT_ULong  charstring_len;
163
164
165       /* now get load the unscaled outline */
166       error = cff_get_glyph_data( face, glyph_index,
167                                   &charstring, &charstring_len );
168       if ( !error )
169       {
170         error = decoder_funcs->prepare( &decoder, size, glyph_index );
171         if ( !error )
172           error = decoder_funcs->parse_charstrings_old( &decoder,
173                                                         charstring,
174                                                         charstring_len,
175                                                         0 );
176
177         cff_free_glyph_data( face, &charstring, &charstring_len );
178       }
179
180       /* ignore the error if one has occurred -- skip to next glyph */
181       error = FT_Err_Ok;
182     }
183
184     *max_advance = decoder.builder.advance.x;
185
186     return FT_Err_Ok;
187   }
188
189
190 #endif /* 0 */
191
192
193   FT_LOCAL_DEF( FT_Error )
194   cff_slot_load( CFF_GlyphSlot  glyph,
195                  CFF_Size       size,
196                  FT_UInt        glyph_index,
197                  FT_Int32       load_flags )
198   {
199     FT_Error     error;
200     CFF_Decoder  decoder;
201     PS_Decoder   psdecoder;
202     TT_Face      face = (TT_Face)glyph->root.face;
203     FT_Bool      hinting, scaled, force_scaling;
204     CFF_Font     cff  = (CFF_Font)face->extra.data;
205
206     PSAux_Service            psaux         = (PSAux_Service)face->psaux;
207     const CFF_Decoder_Funcs  decoder_funcs = psaux->cff_decoder_funcs;
208
209     FT_Matrix  font_matrix;
210     FT_Vector  font_offset;
211
212
213     force_scaling = FALSE;
214
215     /* in a CID-keyed font, consider `glyph_index' as a CID and map */
216     /* it immediately to the real glyph_index -- if it isn't a      */
217     /* subsetted font, glyph_indices and CIDs are identical, though */
218     if ( cff->top_font.font_dict.cid_registry != 0xFFFFU &&
219          cff->charset.cids                               )
220     {
221       /* don't handle CID 0 (.notdef) which is directly mapped to GID 0 */
222       if ( glyph_index != 0 )
223       {
224         glyph_index = cff_charset_cid_to_gindex( &cff->charset,
225                                                  glyph_index );
226         if ( glyph_index == 0 )
227           return FT_THROW( Invalid_Argument );
228       }
229     }
230     else if ( glyph_index >= cff->num_glyphs )
231       return FT_THROW( Invalid_Argument );
232
233     if ( load_flags & FT_LOAD_NO_RECURSE )
234       load_flags |= FT_LOAD_NO_SCALE | FT_LOAD_NO_HINTING;
235
236     glyph->x_scale = 0x10000L;
237     glyph->y_scale = 0x10000L;
238     if ( size )
239     {
240       glyph->x_scale = size->root.metrics.x_scale;
241       glyph->y_scale = size->root.metrics.y_scale;
242     }
243
244 #ifdef TT_CONFIG_OPTION_EMBEDDED_BITMAPS
245
246     /* try to load embedded bitmap if any              */
247     /*                                                 */
248     /* XXX: The convention should be emphasized in     */
249     /*      the documents because it can be confusing. */
250     if ( size )
251     {
252       CFF_Face      cff_face = (CFF_Face)size->root.face;
253       SFNT_Service  sfnt     = (SFNT_Service)cff_face->sfnt;
254       FT_Stream     stream   = cff_face->root.stream;
255
256
257       if ( size->strike_index != 0xFFFFFFFFUL      &&
258            sfnt->load_eblc                         &&
259            ( load_flags & FT_LOAD_NO_BITMAP ) == 0 )
260       {
261         TT_SBit_MetricsRec  metrics;
262
263
264         error = sfnt->load_sbit_image( face,
265                                        size->strike_index,
266                                        glyph_index,
267                                        (FT_UInt)load_flags,
268                                        stream,
269                                        &glyph->root.bitmap,
270                                        &metrics );
271
272         if ( !error )
273         {
274           FT_Bool    has_vertical_info;
275           FT_UShort  advance;
276           FT_Short   dummy;
277
278
279           glyph->root.outline.n_points   = 0;
280           glyph->root.outline.n_contours = 0;
281
282           glyph->root.metrics.width  = (FT_Pos)metrics.width  * 64;
283           glyph->root.metrics.height = (FT_Pos)metrics.height * 64;
284
285           glyph->root.metrics.horiBearingX = (FT_Pos)metrics.horiBearingX * 64;
286           glyph->root.metrics.horiBearingY = (FT_Pos)metrics.horiBearingY * 64;
287           glyph->root.metrics.horiAdvance  = (FT_Pos)metrics.horiAdvance  * 64;
288
289           glyph->root.metrics.vertBearingX = (FT_Pos)metrics.vertBearingX * 64;
290           glyph->root.metrics.vertBearingY = (FT_Pos)metrics.vertBearingY * 64;
291           glyph->root.metrics.vertAdvance  = (FT_Pos)metrics.vertAdvance  * 64;
292
293           glyph->root.format = FT_GLYPH_FORMAT_BITMAP;
294
295           if ( load_flags & FT_LOAD_VERTICAL_LAYOUT )
296           {
297             glyph->root.bitmap_left = metrics.vertBearingX;
298             glyph->root.bitmap_top  = metrics.vertBearingY;
299           }
300           else
301           {
302             glyph->root.bitmap_left = metrics.horiBearingX;
303             glyph->root.bitmap_top  = metrics.horiBearingY;
304           }
305
306           /* compute linear advance widths */
307
308           (void)( (SFNT_Service)face->sfnt )->get_metrics( face, 0,
309                                                            glyph_index,
310                                                            &dummy,
311                                                            &advance );
312           glyph->root.linearHoriAdvance = advance;
313
314           has_vertical_info = FT_BOOL(
315                                 face->vertical_info                   &&
316                                 face->vertical.number_Of_VMetrics > 0 );
317
318           /* get the vertical metrics from the vmtx table if we have one */
319           if ( has_vertical_info )
320           {
321             (void)( (SFNT_Service)face->sfnt )->get_metrics( face, 1,
322                                                              glyph_index,
323                                                              &dummy,
324                                                              &advance );
325             glyph->root.linearVertAdvance = advance;
326           }
327           else
328           {
329             /* make up vertical ones */
330             if ( face->os2.version != 0xFFFFU )
331               glyph->root.linearVertAdvance = (FT_Pos)
332                 ( face->os2.sTypoAscender - face->os2.sTypoDescender );
333             else
334               glyph->root.linearVertAdvance = (FT_Pos)
335                 ( face->horizontal.Ascender - face->horizontal.Descender );
336           }
337
338           return error;
339         }
340       }
341     }
342
343 #endif /* TT_CONFIG_OPTION_EMBEDDED_BITMAPS */
344
345     /* return immediately if we only want the embedded bitmaps */
346     if ( load_flags & FT_LOAD_SBITS_ONLY )
347       return FT_THROW( Invalid_Argument );
348
349     /* if we have a CID subfont, use its matrix (which has already */
350     /* been multiplied with the root matrix)                       */
351
352     /* this scaling is only relevant if the PS hinter isn't active */
353     if ( cff->num_subfonts )
354     {
355       FT_Long  top_upm, sub_upm;
356       FT_Byte  fd_index = cff_fd_select_get( &cff->fd_select,
357                                              glyph_index );
358
359
360       if ( fd_index >= cff->num_subfonts )
361         fd_index = (FT_Byte)( cff->num_subfonts - 1 );
362
363       top_upm = (FT_Long)cff->top_font.font_dict.units_per_em;
364       sub_upm = (FT_Long)cff->subfonts[fd_index]->font_dict.units_per_em;
365
366       font_matrix = cff->subfonts[fd_index]->font_dict.font_matrix;
367       font_offset = cff->subfonts[fd_index]->font_dict.font_offset;
368
369       if ( top_upm != sub_upm )
370       {
371         glyph->x_scale = FT_MulDiv( glyph->x_scale, top_upm, sub_upm );
372         glyph->y_scale = FT_MulDiv( glyph->y_scale, top_upm, sub_upm );
373
374         force_scaling = TRUE;
375       }
376     }
377     else
378     {
379       font_matrix = cff->top_font.font_dict.font_matrix;
380       font_offset = cff->top_font.font_dict.font_offset;
381     }
382
383     glyph->root.outline.n_points   = 0;
384     glyph->root.outline.n_contours = 0;
385
386     /* top-level code ensures that FT_LOAD_NO_HINTING is set */
387     /* if FT_LOAD_NO_SCALE is active                         */
388     hinting = FT_BOOL( ( load_flags & FT_LOAD_NO_HINTING ) == 0 );
389     scaled  = FT_BOOL( ( load_flags & FT_LOAD_NO_SCALE   ) == 0 );
390
391     glyph->hint        = hinting;
392     glyph->scaled      = scaled;
393     glyph->root.format = FT_GLYPH_FORMAT_OUTLINE;  /* by default */
394
395     {
396 #ifdef CFF_CONFIG_OPTION_OLD_ENGINE
397       PS_Driver  driver = (PS_Driver)FT_FACE_DRIVER( face );
398 #endif
399
400       FT_Byte*  charstring;
401       FT_ULong  charstring_len;
402
403
404       decoder_funcs->init( &decoder, face, size, glyph, hinting,
405                            FT_LOAD_TARGET_MODE( load_flags ),
406                            cff_get_glyph_data,
407                            cff_free_glyph_data );
408
409       /* this is for pure CFFs */
410       if ( load_flags & FT_LOAD_ADVANCE_ONLY )
411         decoder.width_only = TRUE;
412
413       decoder.builder.no_recurse =
414         FT_BOOL( load_flags & FT_LOAD_NO_RECURSE );
415
416       /* now load the unscaled outline */
417       error = cff_get_glyph_data( face, glyph_index,
418                                   &charstring, &charstring_len );
419       if ( error )
420         goto Glyph_Build_Finished;
421
422       error = decoder_funcs->prepare( &decoder, size, glyph_index );
423       if ( error )
424         goto Glyph_Build_Finished;
425
426 #ifdef CFF_CONFIG_OPTION_OLD_ENGINE
427       /* choose which CFF renderer to use */
428       if ( driver->hinting_engine == FT_HINTING_FREETYPE )
429         error = decoder_funcs->parse_charstrings_old( &decoder,
430                                                       charstring,
431                                                       charstring_len,
432                                                       0 );
433       else
434 #endif
435       {
436         psaux->ps_decoder_init( &psdecoder, &decoder, FALSE );
437
438         error = decoder_funcs->parse_charstrings( &psdecoder,
439                                                   charstring,
440                                                   charstring_len );
441
442         /* Adobe's engine uses 16.16 numbers everywhere;              */
443         /* as a consequence, glyphs larger than 2000ppem get rejected */
444         if ( FT_ERR_EQ( error, Glyph_Too_Big ) )
445         {
446           /* this time, we retry unhinted and scale up the glyph later on */
447           /* (the engine uses and sets the hardcoded value 0x10000 / 64 = */
448           /* 0x400 for both `x_scale' and `y_scale' in this case)         */
449           hinting       = FALSE;
450           force_scaling = TRUE;
451           glyph->hint   = hinting;
452
453           error = decoder_funcs->parse_charstrings( &psdecoder,
454                                                     charstring,
455                                                     charstring_len );
456         }
457       }
458
459       cff_free_glyph_data( face, &charstring, charstring_len );
460
461       if ( error )
462         goto Glyph_Build_Finished;
463
464 #ifdef FT_CONFIG_OPTION_INCREMENTAL
465       /* Control data and length may not be available for incremental */
466       /* fonts.                                                       */
467       if ( face->root.internal->incremental_interface )
468       {
469         glyph->root.control_data = NULL;
470         glyph->root.control_len = 0;
471       }
472       else
473 #endif /* FT_CONFIG_OPTION_INCREMENTAL */
474
475       /* We set control_data and control_len if charstrings is loaded. */
476       /* See how charstring loads at cff_index_access_element() in     */
477       /* cffload.c.                                                    */
478       {
479         CFF_Index  csindex = &cff->charstrings_index;
480
481
482         if ( csindex->offsets )
483         {
484           glyph->root.control_data = csindex->bytes +
485                                      csindex->offsets[glyph_index] - 1;
486           glyph->root.control_len  = (FT_Long)charstring_len;
487         }
488       }
489
490   Glyph_Build_Finished:
491       /* save new glyph tables, if no error */
492       if ( !error )
493         decoder.builder.funcs.done( &decoder.builder );
494       /* XXX: anything to do for broken glyph entry? */
495     }
496
497 #ifdef FT_CONFIG_OPTION_INCREMENTAL
498
499     /* Incremental fonts can optionally override the metrics. */
500     if ( !error                                                               &&
501          face->root.internal->incremental_interface                           &&
502          face->root.internal->incremental_interface->funcs->get_glyph_metrics )
503     {
504       FT_Incremental_MetricsRec  metrics;
505
506
507       metrics.bearing_x = decoder.builder.left_bearing.x;
508       metrics.bearing_y = 0;
509       metrics.advance   = decoder.builder.advance.x;
510       metrics.advance_v = decoder.builder.advance.y;
511
512       error = face->root.internal->incremental_interface->funcs->get_glyph_metrics(
513                 face->root.internal->incremental_interface->object,
514                 glyph_index, FALSE, &metrics );
515
516       decoder.builder.left_bearing.x = metrics.bearing_x;
517       decoder.builder.advance.x      = metrics.advance;
518       decoder.builder.advance.y      = metrics.advance_v;
519     }
520
521 #endif /* FT_CONFIG_OPTION_INCREMENTAL */
522
523     if ( !error )
524     {
525       /* Now, set the metrics -- this is rather simple, as   */
526       /* the left side bearing is the xMin, and the top side */
527       /* bearing the yMax.                                   */
528
529       /* For composite glyphs, return only left side bearing and */
530       /* advance width.                                          */
531       if ( load_flags & FT_LOAD_NO_RECURSE )
532       {
533         FT_Slot_Internal  internal = glyph->root.internal;
534
535
536         glyph->root.metrics.horiBearingX = decoder.builder.left_bearing.x;
537         glyph->root.metrics.horiAdvance  = decoder.glyph_width;
538         internal->glyph_matrix           = font_matrix;
539         internal->glyph_delta            = font_offset;
540         internal->glyph_transformed      = 1;
541       }
542       else
543       {
544         FT_BBox            cbox;
545         FT_Glyph_Metrics*  metrics = &glyph->root.metrics;
546         FT_Bool            has_vertical_info;
547
548
549         if ( face->horizontal.number_Of_HMetrics )
550         {
551           FT_Short   horiBearingX = 0;
552           FT_UShort  horiAdvance  = 0;
553
554
555           ( (SFNT_Service)face->sfnt )->get_metrics( face, 0,
556                                                      glyph_index,
557                                                      &horiBearingX,
558                                                      &horiAdvance );
559           metrics->horiAdvance          = horiAdvance;
560           metrics->horiBearingX         = horiBearingX;
561           glyph->root.linearHoriAdvance = horiAdvance;
562         }
563         else
564         {
565           /* copy the _unscaled_ advance width */
566           metrics->horiAdvance          = decoder.glyph_width;
567           glyph->root.linearHoriAdvance = decoder.glyph_width;
568         }
569
570         glyph->root.internal->glyph_transformed = 0;
571
572         has_vertical_info = FT_BOOL( face->vertical_info                   &&
573                                      face->vertical.number_Of_VMetrics > 0 );
574
575         /* get the vertical metrics from the vmtx table if we have one */
576         if ( has_vertical_info )
577         {
578           FT_Short   vertBearingY = 0;
579           FT_UShort  vertAdvance  = 0;
580
581
582           ( (SFNT_Service)face->sfnt )->get_metrics( face, 1,
583                                                      glyph_index,
584                                                      &vertBearingY,
585                                                      &vertAdvance );
586           metrics->vertBearingY = vertBearingY;
587           metrics->vertAdvance  = vertAdvance;
588         }
589         else
590         {
591           /* make up vertical ones */
592           if ( face->os2.version != 0xFFFFU )
593             metrics->vertAdvance = (FT_Pos)( face->os2.sTypoAscender -
594                                              face->os2.sTypoDescender );
595           else
596             metrics->vertAdvance = (FT_Pos)( face->horizontal.Ascender -
597                                              face->horizontal.Descender );
598         }
599
600         glyph->root.linearVertAdvance = metrics->vertAdvance;
601
602         glyph->root.format = FT_GLYPH_FORMAT_OUTLINE;
603
604         glyph->root.outline.flags = 0;
605         if ( size && size->root.metrics.y_ppem < 24 )
606           glyph->root.outline.flags |= FT_OUTLINE_HIGH_PRECISION;
607
608         glyph->root.outline.flags |= FT_OUTLINE_REVERSE_FILL;
609
610         /* apply the font matrix, if any */
611         if ( font_matrix.xx != 0x10000L || font_matrix.yy != 0x10000L ||
612              font_matrix.xy != 0        || font_matrix.yx != 0        )
613         {
614           FT_Outline_Transform( &glyph->root.outline, &font_matrix );
615
616           metrics->horiAdvance = FT_MulFix( metrics->horiAdvance,
617                                             font_matrix.xx );
618           metrics->vertAdvance = FT_MulFix( metrics->vertAdvance,
619                                             font_matrix.yy );
620         }
621
622         if ( font_offset.x || font_offset.y )
623         {
624           FT_Outline_Translate( &glyph->root.outline,
625                                 font_offset.x,
626                                 font_offset.y );
627
628           metrics->horiAdvance += font_offset.x;
629           metrics->vertAdvance += font_offset.y;
630         }
631
632         if ( ( load_flags & FT_LOAD_NO_SCALE ) == 0 || force_scaling )
633         {
634           /* scale the outline and the metrics */
635           FT_Int       n;
636           FT_Outline*  cur     = &glyph->root.outline;
637           FT_Vector*   vec     = cur->points;
638           FT_Fixed     x_scale = glyph->x_scale;
639           FT_Fixed     y_scale = glyph->y_scale;
640
641
642           /* First of all, scale the points */
643           if ( !hinting || !decoder.builder.hints_funcs )
644             for ( n = cur->n_points; n > 0; n--, vec++ )
645             {
646               vec->x = FT_MulFix( vec->x, x_scale );
647               vec->y = FT_MulFix( vec->y, y_scale );
648             }
649
650           /* Then scale the metrics */
651           metrics->horiAdvance = FT_MulFix( metrics->horiAdvance, x_scale );
652           metrics->vertAdvance = FT_MulFix( metrics->vertAdvance, y_scale );
653         }
654
655         /* compute the other metrics */
656         FT_Outline_Get_CBox( &glyph->root.outline, &cbox );
657
658         metrics->width  = cbox.xMax - cbox.xMin;
659         metrics->height = cbox.yMax - cbox.yMin;
660
661         metrics->horiBearingX = cbox.xMin;
662         metrics->horiBearingY = cbox.yMax;
663
664         if ( has_vertical_info )
665         {
666           metrics->vertBearingX = metrics->horiBearingX -
667                                     metrics->horiAdvance / 2;
668           metrics->vertBearingY = FT_MulFix( metrics->vertBearingY,
669                                              glyph->y_scale );
670         }
671         else
672         {
673           if ( load_flags & FT_LOAD_VERTICAL_LAYOUT )
674             ft_synthesize_vertical_metrics( metrics,
675                                             metrics->vertAdvance );
676         }
677       }
678     }
679
680     return error;
681   }
682
683
684 /* END */