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