• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /****************************************************************************
2  *
3  * t1gload.c
4  *
5  *   Type 1 Glyph Loader (body).
6  *
7  * Copyright 1996-2018 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 <ft2build.h>
20 #include "t1gload.h"
21 #include FT_INTERNAL_CALC_H
22 #include FT_INTERNAL_DEBUG_H
23 #include FT_INTERNAL_STREAM_H
24 #include FT_OUTLINE_H
25 #include FT_INTERNAL_POSTSCRIPT_AUX_H
26 #include FT_INTERNAL_CFF_TYPES_H
27 #include FT_DRIVER_H
28 
29 #include "t1errors.h"
30 
31 
32   /**************************************************************************
33    *
34    * The macro FT_COMPONENT is used in trace mode.  It is an implicit
35    * parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log
36    * messages during execution.
37    */
38 #undef  FT_COMPONENT
39 #define FT_COMPONENT  trace_t1gload
40 
41 
42   static FT_Error
T1_Parse_Glyph_And_Get_Char_String(T1_Decoder decoder,FT_UInt glyph_index,FT_Data * char_string,FT_Bool * force_scaling)43   T1_Parse_Glyph_And_Get_Char_String( T1_Decoder  decoder,
44                                       FT_UInt     glyph_index,
45                                       FT_Data*    char_string,
46                                       FT_Bool*    force_scaling )
47   {
48     T1_Face   face  = (T1_Face)decoder->builder.face;
49     T1_Font   type1 = &face->type1;
50     FT_Error  error = FT_Err_Ok;
51 
52     PSAux_Service           psaux         = (PSAux_Service)face->psaux;
53     const T1_Decoder_Funcs  decoder_funcs = psaux->t1_decoder_funcs;
54     PS_Decoder              psdecoder;
55 
56 #ifdef FT_CONFIG_OPTION_INCREMENTAL
57     FT_Incremental_InterfaceRec *inc =
58                       face->root.internal->incremental_interface;
59 #endif
60 
61 #ifdef T1_CONFIG_OPTION_OLD_ENGINE
62     PS_Driver  driver = (PS_Driver)FT_FACE_DRIVER( face );
63 #endif
64 
65 
66     decoder->font_matrix = type1->font_matrix;
67     decoder->font_offset = type1->font_offset;
68 
69 #ifdef FT_CONFIG_OPTION_INCREMENTAL
70 
71     /* For incremental fonts get the character data using the */
72     /* callback function.                                     */
73     if ( inc )
74       error = inc->funcs->get_glyph_data( inc->object,
75                                           glyph_index, char_string );
76     else
77 
78 #endif /* FT_CONFIG_OPTION_INCREMENTAL */
79 
80     /* For ordinary fonts get the character data stored in the face record. */
81     {
82       char_string->pointer = type1->charstrings[glyph_index];
83       char_string->length  = (FT_Int)type1->charstrings_len[glyph_index];
84     }
85 
86     if ( !error )
87     {
88       /* choose which renderer to use */
89 #ifdef T1_CONFIG_OPTION_OLD_ENGINE
90       if ( driver->hinting_engine == FT_HINTING_FREETYPE ||
91            decoder->builder.metrics_only                 )
92         error = decoder_funcs->parse_charstrings_old(
93                   decoder,
94                   (FT_Byte*)char_string->pointer,
95                   (FT_UInt)char_string->length );
96 #else
97       if ( decoder->builder.metrics_only )
98         error = decoder_funcs->parse_metrics(
99                   decoder,
100                   (FT_Byte*)char_string->pointer,
101                   (FT_UInt)char_string->length );
102 #endif
103       else
104       {
105         CFF_SubFontRec  subfont;
106 
107 
108         psaux->ps_decoder_init( &psdecoder, decoder, TRUE );
109 
110         psaux->t1_make_subfont( FT_FACE( face ),
111                                 &face->type1.private_dict, &subfont );
112         psdecoder.current_subfont = &subfont;
113 
114         error = decoder_funcs->parse_charstrings(
115                   &psdecoder,
116                   (FT_Byte*)char_string->pointer,
117                   (FT_ULong)char_string->length );
118 
119         /* Adobe's engine uses 16.16 numbers everywhere;              */
120         /* as a consequence, glyphs larger than 2000ppem get rejected */
121         if ( FT_ERR_EQ( error, Glyph_Too_Big ) )
122         {
123           /* this time, we retry unhinted and scale up the glyph later on */
124           /* (the engine uses and sets the hardcoded value 0x10000 / 64 = */
125           /* 0x400 for both `x_scale' and `y_scale' in this case)         */
126           ((T1_GlyphSlot)decoder->builder.glyph)->hint = FALSE;
127 
128           *force_scaling = TRUE;
129 
130           error = decoder_funcs->parse_charstrings(
131                     &psdecoder,
132                     (FT_Byte*)char_string->pointer,
133                     (FT_ULong)char_string->length );
134         }
135       }
136     }
137 
138 #ifdef FT_CONFIG_OPTION_INCREMENTAL
139 
140     /* Incremental fonts can optionally override the metrics. */
141     if ( !error && inc && inc->funcs->get_glyph_metrics )
142     {
143       FT_Incremental_MetricsRec  metrics;
144 
145 
146       metrics.bearing_x = FIXED_TO_INT( decoder->builder.left_bearing.x );
147       metrics.bearing_y = 0;
148       metrics.advance   = FIXED_TO_INT( decoder->builder.advance.x );
149       metrics.advance_v = FIXED_TO_INT( decoder->builder.advance.y );
150 
151       error = inc->funcs->get_glyph_metrics( inc->object,
152                                              glyph_index, FALSE, &metrics );
153 
154       decoder->builder.left_bearing.x = INT_TO_FIXED( metrics.bearing_x );
155       decoder->builder.advance.x      = INT_TO_FIXED( metrics.advance );
156       decoder->builder.advance.y      = INT_TO_FIXED( metrics.advance_v );
157     }
158 
159 #endif /* FT_CONFIG_OPTION_INCREMENTAL */
160 
161     return error;
162   }
163 
164 
165   FT_CALLBACK_DEF( FT_Error )
T1_Parse_Glyph(T1_Decoder decoder,FT_UInt glyph_index)166   T1_Parse_Glyph( T1_Decoder  decoder,
167                   FT_UInt     glyph_index )
168   {
169     FT_Data   glyph_data;
170     FT_Bool   force_scaling = FALSE;
171     FT_Error  error         = T1_Parse_Glyph_And_Get_Char_String(
172                                 decoder, glyph_index, &glyph_data,
173                                 &force_scaling );
174 
175 
176 #ifdef FT_CONFIG_OPTION_INCREMENTAL
177 
178     if ( !error )
179     {
180       T1_Face  face = (T1_Face)decoder->builder.face;
181 
182 
183       if ( face->root.internal->incremental_interface )
184         face->root.internal->incremental_interface->funcs->free_glyph_data(
185           face->root.internal->incremental_interface->object,
186           &glyph_data );
187     }
188 
189 #endif /* FT_CONFIG_OPTION_INCREMENTAL */
190 
191     return error;
192   }
193 
194 
195   /*************************************************************************/
196   /*************************************************************************/
197   /*************************************************************************/
198   /**********                                                      *********/
199   /**********            COMPUTE THE MAXIMUM ADVANCE WIDTH         *********/
200   /**********                                                      *********/
201   /**********    The following code is in charge of computing      *********/
202   /**********    the maximum advance width of the font.  It        *********/
203   /**********    quickly processes each glyph charstring to        *********/
204   /**********    extract the value from either a `sbw' or `seac'   *********/
205   /**********    operator.                                         *********/
206   /**********                                                      *********/
207   /*************************************************************************/
208   /*************************************************************************/
209   /*************************************************************************/
210 
211 
212   FT_LOCAL_DEF( FT_Error )
T1_Compute_Max_Advance(T1_Face face,FT_Pos * max_advance)213   T1_Compute_Max_Advance( T1_Face  face,
214                           FT_Pos*  max_advance )
215   {
216     FT_Error       error;
217     T1_DecoderRec  decoder;
218     FT_Int         glyph_index;
219     T1_Font        type1 = &face->type1;
220     PSAux_Service  psaux = (PSAux_Service)face->psaux;
221 
222 
223     FT_ASSERT( ( face->len_buildchar == 0 ) == ( face->buildchar == NULL ) );
224 
225     *max_advance = 0;
226 
227     /* initialize load decoder */
228     error = psaux->t1_decoder_funcs->init( &decoder,
229                                            (FT_Face)face,
230                                            0, /* size       */
231                                            0, /* glyph slot */
232                                            (FT_Byte**)type1->glyph_names,
233                                            face->blend,
234                                            0,
235                                            FT_RENDER_MODE_NORMAL,
236                                            T1_Parse_Glyph );
237     if ( error )
238       return error;
239 
240     decoder.builder.metrics_only = 1;
241     decoder.builder.load_points  = 0;
242 
243     decoder.num_subrs     = type1->num_subrs;
244     decoder.subrs         = type1->subrs;
245     decoder.subrs_len     = type1->subrs_len;
246     decoder.subrs_hash    = type1->subrs_hash;
247 
248     decoder.buildchar     = face->buildchar;
249     decoder.len_buildchar = face->len_buildchar;
250 
251     *max_advance = 0;
252 
253     FT_TRACE6(( "T1_Compute_Max_Advance:\n" ));
254 
255     /* for each glyph, parse the glyph charstring and extract */
256     /* the advance width                                      */
257     for ( glyph_index = 0; glyph_index < type1->num_glyphs; glyph_index++ )
258     {
259       /* now get load the unscaled outline */
260       (void)T1_Parse_Glyph( &decoder, (FT_UInt)glyph_index );
261       if ( glyph_index == 0 || decoder.builder.advance.x > *max_advance )
262         *max_advance = decoder.builder.advance.x;
263 
264       /* ignore the error if one occurred - skip to next glyph */
265     }
266 
267     FT_TRACE6(( "T1_Compute_Max_Advance: max advance: %f\n",
268                 *max_advance / 65536.0 ));
269 
270     psaux->t1_decoder_funcs->done( &decoder );
271 
272     return FT_Err_Ok;
273   }
274 
275 
276   FT_LOCAL_DEF( FT_Error )
T1_Get_Advances(FT_Face t1face,FT_UInt first,FT_UInt count,FT_Int32 load_flags,FT_Fixed * advances)277   T1_Get_Advances( FT_Face    t1face,        /* T1_Face */
278                    FT_UInt    first,
279                    FT_UInt    count,
280                    FT_Int32   load_flags,
281                    FT_Fixed*  advances )
282   {
283     T1_Face        face  = (T1_Face)t1face;
284     T1_DecoderRec  decoder;
285     T1_Font        type1 = &face->type1;
286     PSAux_Service  psaux = (PSAux_Service)face->psaux;
287     FT_UInt        nn;
288     FT_Error       error;
289 
290 
291     FT_TRACE5(( "T1_Get_Advances:\n" ));
292 
293     if ( load_flags & FT_LOAD_VERTICAL_LAYOUT )
294     {
295       for ( nn = 0; nn < count; nn++ )
296       {
297         advances[nn] = 0;
298 
299         FT_TRACE5(( "  idx %d: advance height 0 font units\n",
300                     first + nn ));
301       }
302 
303       return FT_Err_Ok;
304     }
305 
306     error = psaux->t1_decoder_funcs->init( &decoder,
307                                            (FT_Face)face,
308                                            0, /* size       */
309                                            0, /* glyph slot */
310                                            (FT_Byte**)type1->glyph_names,
311                                            face->blend,
312                                            0,
313                                            FT_RENDER_MODE_NORMAL,
314                                            T1_Parse_Glyph );
315     if ( error )
316       return error;
317 
318     decoder.builder.metrics_only = 1;
319     decoder.builder.load_points  = 0;
320 
321     decoder.num_subrs  = type1->num_subrs;
322     decoder.subrs      = type1->subrs;
323     decoder.subrs_len  = type1->subrs_len;
324     decoder.subrs_hash = type1->subrs_hash;
325 
326     decoder.buildchar     = face->buildchar;
327     decoder.len_buildchar = face->len_buildchar;
328 
329     for ( nn = 0; nn < count; nn++ )
330     {
331       error = T1_Parse_Glyph( &decoder, first + nn );
332       if ( !error )
333         advances[nn] = FIXED_TO_INT( decoder.builder.advance.x );
334       else
335         advances[nn] = 0;
336 
337       FT_TRACE5(( "  idx %d: advance width %d font unit%s\n",
338                   first + nn,
339                   advances[nn],
340                   advances[nn] == 1 ? "" : "s" ));
341     }
342 
343     return FT_Err_Ok;
344   }
345 
346 
347   FT_LOCAL_DEF( FT_Error )
T1_Load_Glyph(FT_GlyphSlot t1glyph,FT_Size t1size,FT_UInt glyph_index,FT_Int32 load_flags)348   T1_Load_Glyph( FT_GlyphSlot  t1glyph,          /* T1_GlyphSlot */
349                  FT_Size       t1size,           /* T1_Size      */
350                  FT_UInt       glyph_index,
351                  FT_Int32      load_flags )
352   {
353     T1_GlyphSlot            glyph = (T1_GlyphSlot)t1glyph;
354     FT_Error                error;
355     T1_DecoderRec           decoder;
356     T1_Face                 face = (T1_Face)t1glyph->face;
357     FT_Bool                 hinting;
358     FT_Bool                 scaled;
359     FT_Bool                 force_scaling = FALSE;
360     T1_Font                 type1         = &face->type1;
361     PSAux_Service           psaux         = (PSAux_Service)face->psaux;
362     const T1_Decoder_Funcs  decoder_funcs = psaux->t1_decoder_funcs;
363 
364     FT_Matrix               font_matrix;
365     FT_Vector               font_offset;
366     FT_Data                 glyph_data;
367     FT_Bool                 must_finish_decoder = FALSE;
368 #ifdef FT_CONFIG_OPTION_INCREMENTAL
369     FT_Bool                 glyph_data_loaded = 0;
370 #endif
371 
372 
373 #ifdef FT_CONFIG_OPTION_INCREMENTAL
374     if ( glyph_index >= (FT_UInt)face->root.num_glyphs &&
375          !face->root.internal->incremental_interface   )
376 #else
377     if ( glyph_index >= (FT_UInt)face->root.num_glyphs )
378 #endif /* FT_CONFIG_OPTION_INCREMENTAL */
379     {
380       error = FT_THROW( Invalid_Argument );
381       goto Exit;
382     }
383 
384     FT_TRACE1(( "T1_Load_Glyph: glyph index %d\n", glyph_index ));
385 
386     FT_ASSERT( ( face->len_buildchar == 0 ) == ( face->buildchar == NULL ) );
387 
388     if ( load_flags & FT_LOAD_NO_RECURSE )
389       load_flags |= FT_LOAD_NO_SCALE | FT_LOAD_NO_HINTING;
390 
391     if ( t1size )
392     {
393       glyph->x_scale = t1size->metrics.x_scale;
394       glyph->y_scale = t1size->metrics.y_scale;
395     }
396     else
397     {
398       glyph->x_scale = 0x10000L;
399       glyph->y_scale = 0x10000L;
400     }
401 
402     t1glyph->outline.n_points   = 0;
403     t1glyph->outline.n_contours = 0;
404 
405     hinting = FT_BOOL( ( load_flags & FT_LOAD_NO_SCALE   ) == 0 &&
406                        ( load_flags & FT_LOAD_NO_HINTING ) == 0 );
407     scaled  = FT_BOOL( ( load_flags & FT_LOAD_NO_SCALE   ) == 0 );
408 
409     glyph->hint     = hinting;
410     glyph->scaled   = scaled;
411     t1glyph->format = FT_GLYPH_FORMAT_OUTLINE;
412 
413     error = decoder_funcs->init( &decoder,
414                                  t1glyph->face,
415                                  t1size,
416                                  t1glyph,
417                                  (FT_Byte**)type1->glyph_names,
418                                  face->blend,
419                                  FT_BOOL( hinting ),
420                                  FT_LOAD_TARGET_MODE( load_flags ),
421                                  T1_Parse_Glyph );
422     if ( error )
423       goto Exit;
424 
425     must_finish_decoder = TRUE;
426 
427     decoder.builder.no_recurse = FT_BOOL(
428                                    ( load_flags & FT_LOAD_NO_RECURSE ) != 0 );
429 
430     decoder.num_subrs     = type1->num_subrs;
431     decoder.subrs         = type1->subrs;
432     decoder.subrs_len     = type1->subrs_len;
433     decoder.subrs_hash    = type1->subrs_hash;
434 
435     decoder.buildchar     = face->buildchar;
436     decoder.len_buildchar = face->len_buildchar;
437 
438     /* now load the unscaled outline */
439     error = T1_Parse_Glyph_And_Get_Char_String( &decoder, glyph_index,
440                                                 &glyph_data,
441                                                 &force_scaling );
442     if ( error )
443       goto Exit;
444 #ifdef FT_CONFIG_OPTION_INCREMENTAL
445     glyph_data_loaded = 1;
446 #endif
447 
448     hinting     = glyph->hint;
449     font_matrix = decoder.font_matrix;
450     font_offset = decoder.font_offset;
451 
452     /* save new glyph tables */
453     decoder_funcs->done( &decoder );
454 
455     must_finish_decoder = FALSE;
456 
457     /* now, set the metrics -- this is rather simple, as   */
458     /* the left side bearing is the xMin, and the top side */
459     /* bearing the yMax                                    */
460     if ( !error )
461     {
462       t1glyph->outline.flags &= FT_OUTLINE_OWNER;
463       t1glyph->outline.flags |= FT_OUTLINE_REVERSE_FILL;
464 
465       /* for composite glyphs, return only left side bearing and */
466       /* advance width                                           */
467       if ( load_flags & FT_LOAD_NO_RECURSE )
468       {
469         FT_Slot_Internal  internal = t1glyph->internal;
470 
471 
472         t1glyph->metrics.horiBearingX =
473           FIXED_TO_INT( decoder.builder.left_bearing.x );
474         t1glyph->metrics.horiAdvance  =
475           FIXED_TO_INT( decoder.builder.advance.x );
476 
477         internal->glyph_matrix      = font_matrix;
478         internal->glyph_delta       = font_offset;
479         internal->glyph_transformed = 1;
480       }
481       else
482       {
483         FT_BBox            cbox;
484         FT_Glyph_Metrics*  metrics = &t1glyph->metrics;
485 
486 
487         /* copy the _unscaled_ advance width */
488         metrics->horiAdvance =
489           FIXED_TO_INT( decoder.builder.advance.x );
490         t1glyph->linearHoriAdvance =
491           FIXED_TO_INT( decoder.builder.advance.x );
492         t1glyph->internal->glyph_transformed = 0;
493 
494         if ( load_flags & FT_LOAD_VERTICAL_LAYOUT )
495         {
496           /* make up vertical ones */
497           metrics->vertAdvance = ( face->type1.font_bbox.yMax -
498                                    face->type1.font_bbox.yMin ) >> 16;
499           t1glyph->linearVertAdvance = metrics->vertAdvance;
500         }
501         else
502         {
503           metrics->vertAdvance =
504             FIXED_TO_INT( decoder.builder.advance.y );
505           t1glyph->linearVertAdvance =
506             FIXED_TO_INT( decoder.builder.advance.y );
507         }
508 
509         t1glyph->format = FT_GLYPH_FORMAT_OUTLINE;
510 
511         if ( t1size && t1size->metrics.y_ppem < 24 )
512           t1glyph->outline.flags |= FT_OUTLINE_HIGH_PRECISION;
513 
514 #if 1
515         /* apply the font matrix, if any */
516         if ( font_matrix.xx != 0x10000L || font_matrix.yy != 0x10000L ||
517              font_matrix.xy != 0        || font_matrix.yx != 0        )
518         {
519           FT_Outline_Transform( &t1glyph->outline, &font_matrix );
520 
521           metrics->horiAdvance = FT_MulFix( metrics->horiAdvance,
522                                             font_matrix.xx );
523           metrics->vertAdvance = FT_MulFix( metrics->vertAdvance,
524                                             font_matrix.yy );
525         }
526 
527         if ( font_offset.x || font_offset.y )
528         {
529           FT_Outline_Translate( &t1glyph->outline,
530                                 font_offset.x,
531                                 font_offset.y );
532 
533           metrics->horiAdvance += font_offset.x;
534           metrics->vertAdvance += font_offset.y;
535         }
536 #endif
537 
538         if ( ( load_flags & FT_LOAD_NO_SCALE ) == 0 || force_scaling )
539         {
540           /* scale the outline and the metrics */
541           FT_Int       n;
542           FT_Outline*  cur = decoder.builder.base;
543           FT_Vector*   vec = cur->points;
544           FT_Fixed     x_scale = glyph->x_scale;
545           FT_Fixed     y_scale = glyph->y_scale;
546 
547 
548           /* First of all, scale the points, if we are not hinting */
549           if ( !hinting || ! decoder.builder.hints_funcs )
550             for ( n = cur->n_points; n > 0; n--, vec++ )
551             {
552               vec->x = FT_MulFix( vec->x, x_scale );
553               vec->y = FT_MulFix( vec->y, y_scale );
554             }
555 
556           /* Then scale the metrics */
557           metrics->horiAdvance = FT_MulFix( metrics->horiAdvance, x_scale );
558           metrics->vertAdvance = FT_MulFix( metrics->vertAdvance, y_scale );
559         }
560 
561         /* compute the other metrics */
562         FT_Outline_Get_CBox( &t1glyph->outline, &cbox );
563 
564         metrics->width  = cbox.xMax - cbox.xMin;
565         metrics->height = cbox.yMax - cbox.yMin;
566 
567         metrics->horiBearingX = cbox.xMin;
568         metrics->horiBearingY = cbox.yMax;
569 
570         if ( load_flags & FT_LOAD_VERTICAL_LAYOUT )
571         {
572           /* make up vertical ones */
573           ft_synthesize_vertical_metrics( metrics,
574                                           metrics->vertAdvance );
575         }
576       }
577 
578       /* Set control data to the glyph charstrings.  Note that this is */
579       /* _not_ zero-terminated.                                        */
580       t1glyph->control_data = (FT_Byte*)glyph_data.pointer;
581       t1glyph->control_len  = glyph_data.length;
582     }
583 
584 
585   Exit:
586 
587 #ifdef FT_CONFIG_OPTION_INCREMENTAL
588     if ( glyph_data_loaded && face->root.internal->incremental_interface )
589     {
590       face->root.internal->incremental_interface->funcs->free_glyph_data(
591         face->root.internal->incremental_interface->object,
592         &glyph_data );
593 
594       /* Set the control data to null - it is no longer available if   */
595       /* loaded incrementally.                                         */
596       t1glyph->control_data = NULL;
597       t1glyph->control_len  = 0;
598     }
599 #endif
600 
601     if ( must_finish_decoder )
602       decoder_funcs->done( &decoder );
603 
604     return error;
605   }
606 
607 
608 /* END */
609