1 /***************************************************************************/
5 /* Type 1 Glyph Loader (body). */
7 /* Copyright 1996-2001, 2002 by */
8 /* David Turner, Robert Wilhelm, and Werner Lemberg. */
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. */
16 /***************************************************************************/
21 #include FT_INTERNAL_DEBUG_H
22 #include FT_INTERNAL_STREAM_H
24 #include FT_INTERNAL_POSTSCRIPT_AUX_H
29 /*************************************************************************/
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. */
36 #define FT_COMPONENT trace_t1gload
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 /*************************************************************************/
56 FT_LOCAL_DEF( FT_Error )
57 T1_Parse_Glyph_And_Get_Char_String( T1_Decoder decoder,
59 FT_Data* char_string )
61 T1_Face face = (T1_Face)decoder->builder.face;
62 T1_Font type1 = &face->type1;
66 decoder->font_matrix = type1->font_matrix;
67 decoder->font_offset = type1->font_offset;
69 #ifdef FT_CONFIG_OPTION_INCREMENTAL
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 );
79 #endif /* FT_CONFIG_OPTION_INCREMENTAL */
81 /* For ordinary fonts get the character data stored in the face record. */
83 char_string->pointer = type1->charstrings[glyph_index];
84 char_string->length = type1->charstrings_len[glyph_index];
88 error = decoder->funcs.parse_charstrings(
89 decoder, (FT_Byte*)char_string->pointer,
90 char_string->length );
92 #ifdef FT_CONFIG_OPTION_INCREMENTAL
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 )
98 FT_Incremental_MetricsRec metrics;
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;
112 #endif /* FT_CONFIG_OPTION_INCREMENTAL */
118 FT_CALLBACK_DEF( FT_Error )
119 T1_Parse_Glyph( T1_Decoder decoder,
120 FT_UInt glyph_index )
123 FT_Error error = T1_Parse_Glyph_And_Get_Char_String(
124 decoder, glyph_index, &glyph_data );
127 #ifdef FT_CONFIG_OPTION_INCREMENTAL
130 T1_Face face = (T1_Face)decoder->builder.face;
133 if ( face->root.internal->incremental_interface )
134 face->root.internal->incremental_interface->funcs->free_glyph_data(
135 face->root.internal->incremental_interface->object,
138 #endif /* FT_CONFIG_OPTION_INCREMENTAL */
144 FT_LOCAL_DEF( FT_Error )
145 T1_Compute_Max_Advance( T1_Face face,
146 FT_Int* max_advance )
149 T1_DecoderRec decoder;
151 T1_Font type1 = &face->type1;
152 PSAux_Service psaux = (PSAux_Service)face->psaux;
157 /* initialize load decoder */
158 error = psaux->t1_decoder_funcs->init( &decoder,
162 (FT_Byte**)type1->glyph_names,
165 FT_RENDER_MODE_NORMAL,
170 decoder.builder.metrics_only = 1;
171 decoder.builder.load_points = 0;
173 decoder.num_subrs = type1->num_subrs;
174 decoder.subrs = type1->subrs;
175 decoder.subrs_len = type1->subrs_len;
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++ )
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;
188 /* ignore the error if one occured - skip to next glyph */
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 /*************************************************************************/
212 FT_LOCAL_DEF( FT_Error )
213 T1_Load_Glyph( T1_GlyphSlot glyph,
216 FT_Int32 load_flags )
219 T1_DecoderRec decoder;
220 T1_Face face = (T1_Face)glyph->root.face;
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;
226 FT_Matrix font_matrix;
227 FT_Vector font_offset;
229 FT_Bool glyph_data_loaded = 0;
232 if ( load_flags & FT_LOAD_NO_RECURSE )
233 load_flags |= FT_LOAD_NO_SCALE | FT_LOAD_NO_HINTING;
235 glyph->x_scale = size->root.metrics.x_scale;
236 glyph->y_scale = size->root.metrics.y_scale;
238 glyph->root.outline.n_points = 0;
239 glyph->root.outline.n_contours = 0;
241 hinting = FT_BOOL( ( load_flags & FT_LOAD_NO_SCALE ) == 0 &&
242 ( load_flags & FT_LOAD_NO_HINTING ) == 0 );
244 glyph->root.format = FT_GLYPH_FORMAT_OUTLINE;
246 error = decoder_funcs->init( &decoder,
250 (FT_Byte**)type1->glyph_names,
253 FT_LOAD_TARGET_MODE(load_flags),
258 decoder.builder.no_recurse = FT_BOOL(
259 ( load_flags & FT_LOAD_NO_RECURSE ) != 0 );
261 decoder.num_subrs = type1->num_subrs;
262 decoder.subrs = type1->subrs;
263 decoder.subrs_len = type1->subrs_len;
265 /* now load the unscaled outline */
266 error = T1_Parse_Glyph_And_Get_Char_String( &decoder, glyph_index,
270 glyph_data_loaded = 1;
272 font_matrix = decoder.font_matrix;
273 font_offset = decoder.font_offset;
275 /* save new glyph tables */
276 decoder_funcs->done( &decoder );
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 */
283 glyph->root.outline.flags &= FT_OUTLINE_OWNER;
284 glyph->root.outline.flags |= FT_OUTLINE_REVERSE_FILL;
286 /* for composite glyphs, return only left side bearing and */
288 if ( load_flags & FT_LOAD_NO_RECURSE )
290 FT_Slot_Internal internal = glyph->root.internal;
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;
302 FT_Glyph_Metrics* metrics = &glyph->root.metrics;
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;
310 /* make up vertical metrics */
311 metrics->vertBearingX = 0;
312 metrics->vertBearingY = 0;
313 metrics->vertAdvance = 0;
315 glyph->root.linearVertAdvance = 0;
317 glyph->root.format = FT_GLYPH_FORMAT_OUTLINE;
319 if ( size && size->root.metrics.y_ppem < 24 )
320 glyph->root.outline.flags |= FT_OUTLINE_HIGH_PRECISION;
323 /* apply the font matrix, if any */
324 FT_Outline_Transform( &glyph->root.outline, &font_matrix );
326 FT_Outline_Translate( &glyph->root.outline,
331 if ( ( load_flags & FT_LOAD_NO_SCALE ) == 0 )
333 /* scale the outline and the metrics */
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;
341 /* First of all, scale the points, if we are not hinting */
343 for ( n = cur->n_points; n > 0; n--, vec++ )
345 vec->x = FT_MulFix( vec->x, x_scale );
346 vec->y = FT_MulFix( vec->y, y_scale );
349 FT_Outline_Get_CBox( &glyph->root.outline, &cbox );
351 /* Then scale the metrics */
352 metrics->horiAdvance = FT_MulFix( metrics->horiAdvance, x_scale );
353 metrics->vertAdvance = FT_MulFix( metrics->vertAdvance, y_scale );
355 metrics->vertBearingX = FT_MulFix( metrics->vertBearingX, x_scale );
356 metrics->vertBearingY = FT_MulFix( metrics->vertBearingY, y_scale );
360 metrics->horiAdvance = ( metrics->horiAdvance + 32 ) & -64;
361 metrics->vertAdvance = ( metrics->vertAdvance + 32 ) & -64;
363 metrics->vertBearingX = ( metrics->vertBearingX + 32 ) & -64;
364 metrics->vertBearingY = ( metrics->vertBearingY + 32 ) & -64;
368 /* compute the other metrics */
369 FT_Outline_Get_CBox( &glyph->root.outline, &cbox );
371 /* grid fit the bounding box if necessary */
376 cbox.xMax = ( cbox.xMax+63 ) & -64;
377 cbox.yMax = ( cbox.yMax+63 ) & -64;
380 metrics->width = cbox.xMax - cbox.xMin;
381 metrics->height = cbox.yMax - cbox.yMin;
383 metrics->horiBearingX = cbox.xMin;
384 metrics->horiBearingY = cbox.yMax;
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;
396 #ifdef FT_CONFIG_OPTION_INCREMENTAL
397 if ( glyph_data_loaded && face->root.internal->incremental_interface )
399 face->root.internal->incremental_interface->funcs->free_glyph_data(
400 face->root.internal->incremental_interface->object,
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;