update for HEAD-2003050101
[reactos.git] / lib / freetype / src / type1 / t1gload.c
1 /***************************************************************************/
2 /*                                                                         */
3 /*  t1gload.c                                                              */
4 /*                                                                         */
5 /*    Type 1 Glyph Loader (body).                                          */
6 /*                                                                         */
7 /*  Copyright 1996-2001, 2002 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_DEBUG_H
22 #include FT_INTERNAL_STREAM_H
23 #include FT_OUTLINE_H
24 #include FT_INTERNAL_POSTSCRIPT_AUX_H
25
26 #include "t1errors.h"
27
28
29   /*************************************************************************/
30   /*                                                                       */
31   /* The macro FT_COMPONENT is used in trace mode.  It is an implicit      */
32   /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log  */
33   /* messages during execution.                                            */
34   /*                                                                       */
35 #undef  FT_COMPONENT
36 #define FT_COMPONENT  trace_t1gload
37
38
39   /*************************************************************************/
40   /*************************************************************************/
41   /*************************************************************************/
42   /**********                                                      *********/
43   /**********            COMPUTE THE MAXIMUM ADVANCE WIDTH         *********/
44   /**********                                                      *********/
45   /**********    The following code is in charge of computing      *********/
46   /**********    the maximum advance width of the font.  It        *********/
47   /**********    quickly processes each glyph charstring to        *********/
48   /**********    extract the value from either a `sbw' or `seac'   *********/
49   /**********    operator.                                         *********/
50   /**********                                                      *********/
51   /*************************************************************************/
52   /*************************************************************************/
53   /*************************************************************************/
54
55
56   FT_LOCAL_DEF( FT_Error )
57   T1_Parse_Glyph_And_Get_Char_String( T1_Decoder  decoder,
58                                       FT_UInt     glyph_index,
59                                       FT_Data*    char_string )
60   {
61     T1_Face   face  = (T1_Face)decoder->builder.face;
62     T1_Font   type1 = &face->type1;
63     FT_Error  error = 0;
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 ( face->root.internal->incremental_interface )
74       error = face->root.internal->incremental_interface->funcs->get_glyph_data(
75                 face->root.internal->incremental_interface->object,
76                 glyph_index, char_string );
77     else
78
79 #endif /* FT_CONFIG_OPTION_INCREMENTAL */
80
81     /* For ordinary fonts get the character data stored in the face record. */
82     {
83       char_string->pointer = type1->charstrings[glyph_index];
84       char_string->length  = type1->charstrings_len[glyph_index];
85     }
86
87     if ( !error )
88       error = decoder->funcs.parse_charstrings(
89                 decoder, (FT_Byte*)char_string->pointer,
90                 char_string->length );
91
92 #ifdef FT_CONFIG_OPTION_INCREMENTAL
93
94     /* Incremental fonts can optionally override the metrics. */
95     if ( !error && face->root.internal->incremental_interface                 &&
96          face->root.internal->incremental_interface->funcs->get_glyph_metrics )
97     {
98       FT_Incremental_MetricsRec  metrics;
99
100       metrics.bearing_x = decoder->builder.left_bearing.x;
101           metrics.bearing_y = decoder->builder.left_bearing.y;
102           metrics.advance   = decoder->builder.advance.x;
103       error = face->root.internal->incremental_interface->funcs->get_glyph_metrics(
104                 face->root.internal->incremental_interface->object,
105                 glyph_index, FALSE, &metrics );
106       decoder->builder.left_bearing.x = metrics.bearing_x;
107       decoder->builder.left_bearing.y = metrics.bearing_y;
108       decoder->builder.advance.x      = metrics.advance;
109       decoder->builder.advance.y      = 0;
110     }
111
112 #endif /* FT_CONFIG_OPTION_INCREMENTAL */
113
114   return error;
115   }
116
117
118   FT_CALLBACK_DEF( FT_Error )
119   T1_Parse_Glyph( T1_Decoder  decoder,
120                   FT_UInt     glyph_index )
121   {
122     FT_Data   glyph_data;
123     FT_Error  error = T1_Parse_Glyph_And_Get_Char_String( 
124                         decoder, glyph_index, &glyph_data );
125
126
127 #ifdef FT_CONFIG_OPTION_INCREMENTAL
128     if ( !error )
129     {
130       T1_Face  face = (T1_Face)decoder->builder.face;
131
132
133           if ( face->root.internal->incremental_interface )
134         face->root.internal->incremental_interface->funcs->free_glyph_data(
135           face->root.internal->incremental_interface->object,
136           &glyph_data );
137     }
138 #endif /* FT_CONFIG_OPTION_INCREMENTAL */
139
140     return error;
141   }
142
143
144   FT_LOCAL_DEF( FT_Error )
145   T1_Compute_Max_Advance( T1_Face  face,
146                           FT_Int*  max_advance )
147   {
148     FT_Error       error;
149     T1_DecoderRec  decoder;
150     FT_Int         glyph_index;
151     T1_Font        type1 = &face->type1;
152     PSAux_Service  psaux = (PSAux_Service)face->psaux;
153
154
155     *max_advance = 0;
156
157     /* initialize load decoder */
158     error = psaux->t1_decoder_funcs->init( &decoder,
159                                            (FT_Face)face,
160                                            0, /* size       */
161                                            0, /* glyph slot */
162                                            (FT_Byte**)type1->glyph_names,
163                                            face->blend,
164                                            0,
165                                            FT_RENDER_MODE_NORMAL,
166                                            T1_Parse_Glyph );
167     if ( error )
168       return error;
169
170     decoder.builder.metrics_only = 1;
171     decoder.builder.load_points  = 0;
172
173     decoder.num_subrs = type1->num_subrs;
174     decoder.subrs     = type1->subrs;
175     decoder.subrs_len = type1->subrs_len;
176
177     *max_advance = 0;
178
179     /* for each glyph, parse the glyph charstring and extract */
180     /* the advance width                                      */
181     for ( glyph_index = 0; glyph_index < type1->num_glyphs; glyph_index++ )
182     {
183       /* now get load the unscaled outline */
184       error = T1_Parse_Glyph( &decoder, glyph_index );
185       if ( glyph_index == 0 || decoder.builder.advance.x > *max_advance )
186         *max_advance = decoder.builder.advance.x;
187         
188       /* ignore the error if one occured - skip to next glyph */
189     }
190
191     return T1_Err_Ok;
192   }
193
194
195   /*************************************************************************/
196   /*************************************************************************/
197   /*************************************************************************/
198   /**********                                                      *********/
199   /**********               UNHINTED GLYPH LOADER                  *********/
200   /**********                                                      *********/
201   /**********    The following code is in charge of loading a      *********/
202   /**********    single outline.  It completely ignores hinting    *********/
203   /**********    and is used when FT_LOAD_NO_HINTING is set.       *********/
204   /**********                                                      *********/
205   /**********      The Type 1 hinter is located in `t1hint.c'      *********/
206   /**********                                                      *********/
207   /*************************************************************************/
208   /*************************************************************************/
209   /*************************************************************************/
210
211
212   FT_LOCAL_DEF( FT_Error )
213   T1_Load_Glyph( T1_GlyphSlot  glyph,
214                  T1_Size       size,
215                  FT_UInt       glyph_index,
216                  FT_Int32      load_flags )
217   {
218     FT_Error                error;
219     T1_DecoderRec           decoder;
220     T1_Face                 face = (T1_Face)glyph->root.face;
221     FT_Bool                 hinting;
222     T1_Font                 type1         = &face->type1;
223     PSAux_Service           psaux         = (PSAux_Service)face->psaux;
224     const T1_Decoder_Funcs  decoder_funcs = psaux->t1_decoder_funcs;
225
226     FT_Matrix               font_matrix;
227     FT_Vector               font_offset;
228     FT_Data                 glyph_data;
229     FT_Bool                 glyph_data_loaded = 0;
230
231
232     if ( load_flags & FT_LOAD_NO_RECURSE )
233       load_flags |= FT_LOAD_NO_SCALE | FT_LOAD_NO_HINTING;
234
235     glyph->x_scale = size->root.metrics.x_scale;
236     glyph->y_scale = size->root.metrics.y_scale;
237
238     glyph->root.outline.n_points   = 0;
239     glyph->root.outline.n_contours = 0;
240
241     hinting = FT_BOOL( ( load_flags & FT_LOAD_NO_SCALE   ) == 0 &&
242                        ( load_flags & FT_LOAD_NO_HINTING ) == 0 );
243
244     glyph->root.format = FT_GLYPH_FORMAT_OUTLINE;
245
246     error = decoder_funcs->init( &decoder,
247                                  (FT_Face)face,
248                                  (FT_Size)size,
249                                  (FT_GlyphSlot)glyph,
250                                  (FT_Byte**)type1->glyph_names,
251                                  face->blend,
252                                  FT_BOOL( hinting ),
253                                  FT_LOAD_TARGET_MODE(load_flags),
254                                  T1_Parse_Glyph );
255     if ( error )
256       goto Exit;
257
258     decoder.builder.no_recurse = FT_BOOL(
259                                    ( load_flags & FT_LOAD_NO_RECURSE ) != 0 );
260
261     decoder.num_subrs = type1->num_subrs;
262     decoder.subrs     = type1->subrs;
263     decoder.subrs_len = type1->subrs_len;
264
265     /* now load the unscaled outline */
266     error = T1_Parse_Glyph_And_Get_Char_String( &decoder, glyph_index,
267                                                 &glyph_data );
268     if ( error )
269       goto Exit;
270     glyph_data_loaded = 1;
271
272     font_matrix = decoder.font_matrix;
273     font_offset = decoder.font_offset;
274
275     /* save new glyph tables */
276     decoder_funcs->done( &decoder );
277
278     /* now, set the metrics -- this is rather simple, as   */
279     /* the left side bearing is the xMin, and the top side */
280     /* bearing the yMax                                    */
281     if ( !error )
282     {
283       glyph->root.outline.flags &= FT_OUTLINE_OWNER;
284       glyph->root.outline.flags |= FT_OUTLINE_REVERSE_FILL;
285
286       /* for composite glyphs, return only left side bearing and */
287       /* advance width                                           */
288       if ( load_flags & FT_LOAD_NO_RECURSE )
289       {
290         FT_Slot_Internal  internal = glyph->root.internal;
291
292
293         glyph->root.metrics.horiBearingX = decoder.builder.left_bearing.x;
294         glyph->root.metrics.horiAdvance  = decoder.builder.advance.x;
295         internal->glyph_matrix           = font_matrix;
296         internal->glyph_delta            = font_offset;
297         internal->glyph_transformed      = 1;
298       }
299       else
300       {
301         FT_BBox            cbox;
302         FT_Glyph_Metrics*  metrics = &glyph->root.metrics;
303
304
305         /* copy the _unscaled_ advance width */
306         metrics->horiAdvance                    = decoder.builder.advance.x;
307         glyph->root.linearHoriAdvance           = decoder.builder.advance.x;
308         glyph->root.internal->glyph_transformed = 0;
309
310         /* make up vertical metrics */
311         metrics->vertBearingX = 0;
312         metrics->vertBearingY = 0;
313         metrics->vertAdvance  = 0;
314
315         glyph->root.linearVertAdvance = 0;
316
317         glyph->root.format = FT_GLYPH_FORMAT_OUTLINE;
318
319         if ( size && size->root.metrics.y_ppem < 24 )
320           glyph->root.outline.flags |= FT_OUTLINE_HIGH_PRECISION;
321
322 #if 1
323         /* apply the font matrix, if any */
324         FT_Outline_Transform( &glyph->root.outline, &font_matrix );
325
326         FT_Outline_Translate( &glyph->root.outline,
327                               font_offset.x,
328                               font_offset.y );
329 #endif
330
331         if ( ( load_flags & FT_LOAD_NO_SCALE ) == 0 )
332         {
333           /* scale the outline and the metrics */
334           FT_Int       n;
335           FT_Outline*  cur = decoder.builder.base;
336           FT_Vector*   vec = cur->points;
337           FT_Fixed     x_scale = glyph->x_scale;
338           FT_Fixed     y_scale = glyph->y_scale;
339
340
341           /* First of all, scale the points, if we are not hinting */
342           if ( !hinting )
343             for ( n = cur->n_points; n > 0; n--, vec++ )
344             {
345               vec->x = FT_MulFix( vec->x, x_scale );
346               vec->y = FT_MulFix( vec->y, y_scale );
347             }
348
349           FT_Outline_Get_CBox( &glyph->root.outline, &cbox );
350
351           /* Then scale the metrics */
352           metrics->horiAdvance  = FT_MulFix( metrics->horiAdvance,  x_scale );
353           metrics->vertAdvance  = FT_MulFix( metrics->vertAdvance,  y_scale );
354
355           metrics->vertBearingX = FT_MulFix( metrics->vertBearingX, x_scale );
356           metrics->vertBearingY = FT_MulFix( metrics->vertBearingY, y_scale );
357
358           if ( hinting )
359           {
360             metrics->horiAdvance = ( metrics->horiAdvance + 32 ) & -64;
361             metrics->vertAdvance = ( metrics->vertAdvance + 32 ) & -64;
362
363             metrics->vertBearingX = ( metrics->vertBearingX + 32 ) & -64;
364             metrics->vertBearingY = ( metrics->vertBearingY + 32 ) & -64;
365           }
366         }
367
368         /* compute the other metrics */
369         FT_Outline_Get_CBox( &glyph->root.outline, &cbox );
370
371         /* grid fit the bounding box if necessary */
372         if ( hinting )
373         {
374           cbox.xMin &= -64;
375           cbox.yMin &= -64;
376           cbox.xMax  = ( cbox.xMax+63 ) & -64;
377           cbox.yMax  = ( cbox.yMax+63 ) & -64;
378         }
379
380         metrics->width  = cbox.xMax - cbox.xMin;
381         metrics->height = cbox.yMax - cbox.yMin;
382
383         metrics->horiBearingX = cbox.xMin;
384         metrics->horiBearingY = cbox.yMax;
385       }
386
387       /* Set control data to the glyph charstrings.  Note that this is */
388       /* _not_ zero-terminated.                                        */
389       glyph->root.control_data = (FT_Byte*)glyph_data.pointer;
390       glyph->root.control_len  = glyph_data.length;
391     }
392
393
394   Exit:
395
396 #ifdef FT_CONFIG_OPTION_INCREMENTAL
397     if ( glyph_data_loaded && face->root.internal->incremental_interface )
398     {
399       face->root.internal->incremental_interface->funcs->free_glyph_data(
400         face->root.internal->incremental_interface->object,
401         &glyph_data );
402
403       /* Set the control data to null - it is no longer available if   */
404       /* loaded incrementally.                                         */
405       glyph->root.control_data = 0;
406       glyph->root.control_len  = 0;
407     }
408 #endif
409
410     return error;
411   }
412
413
414 /* END */