1 /***************************************************************************/
5 /* Routines used to compute global metrics automatically (body). */
7 /* Copyright 2000-2001, 2002 Catharon Productions Inc. */
8 /* Author: David Turner */
10 /* This file is part of the Catharon Typography Project and shall only */
11 /* be used, modified, and distributed under the terms of the Catharon */
12 /* Open Source License that should come with this file under the name */
13 /* `CatharonLicense.txt'. By continuing to use, modify, or distribute */
14 /* this file you indicate that you have read the license and */
15 /* understand and accept it fully. */
17 /* Note that this license is compatible with the FreeType license. */
19 /***************************************************************************/
23 #include FT_INTERNAL_DEBUG_H
28 #define MAX_TEST_CHARACTERS 12
31 const char* blue_chars[AH_BLUE_MAX] =
35 #ifdef FT_CONFIG_CHESTER_SMALL_F
44 /* simple insertion sort */
46 sort_values( FT_Int count,
53 for ( i = 1; i < count; i++ )
55 for ( j = i; j > 0; j-- )
57 if ( table[j] > table[j - 1] )
61 table[j] = table[j - 1];
69 ah_hinter_compute_blues( AH_Hinter hinter )
72 AH_Globals globals = &hinter->globals->design;
73 FT_Pos flats [MAX_TEST_CHARACTERS];
74 FT_Pos rounds[MAX_TEST_CHARACTERS];
87 /* save current charmap */
88 charmap = face->charmap;
90 /* do we have a Unicode charmap in there? */
91 error = FT_Select_Charmap( face, FT_ENCODING_UNICODE );
95 /* we compute the blues simply by loading each character from the */
96 /* 'blue_chars[blues]' string, then compute its top-most and */
97 /* bottom-most points */
99 AH_LOG(( "blue zones computation\n" ));
100 AH_LOG(( "------------------------------------------------\n" ));
102 for ( blue = AH_BLUE_CAPITAL_TOP; blue < AH_BLUE_MAX; blue++ )
104 const char* p = blue_chars[blue];
105 const char* limit = p + MAX_TEST_CHARACTERS;
106 FT_Pos *blue_ref, *blue_shoot;
109 AH_LOG(( "blue %3d: ", blue ));
114 for ( ; p < limit; p++ )
119 FT_Vector* point_limit;
124 /* exit if we reach the end of the string */
128 AH_LOG(( "`%c'", *p ));
130 /* load the character in the face -- skip unknown or empty ones */
131 glyph_index = FT_Get_Char_Index( face, (FT_UInt)*p );
132 if ( glyph_index == 0 )
135 error = FT_Load_Glyph( face, glyph_index, FT_LOAD_NO_SCALE );
136 if ( error || glyph->outline.n_points <= 0 )
139 /* now compute min or max point indices and coordinates */
140 points = glyph->outline.points;
141 point_limit = points + glyph->outline.n_points;
146 if ( AH_IS_TOP_BLUE( blue ) )
148 for ( ; point < point_limit; point++ )
149 if ( point->y > extremum->y )
154 for ( ; point < point_limit; point++ )
155 if ( point->y < extremum->y )
159 AH_LOG(( "%5d", (int)extremum->y ));
161 /* now, check whether the point belongs to a straight or round */
162 /* segment; we first need to find in which contour the extremum */
163 /* lies, then see its previous and next points */
165 FT_Int idx = (FT_Int)( extremum - points );
167 FT_Int first, last, prev, next, end;
174 for ( n = 0; n < glyph->outline.n_contours; n++ )
176 end = glyph->outline.contours[n];
185 /* XXX: should never happen! */
189 /* now look for the previous and next points that are not on the */
190 /* same Y coordinate. Threshold the `closeness'... */
202 dist = points[prev].y - extremum->y;
203 if ( dist < -5 || dist > 5 )
206 } while ( prev != idx );
215 dist = points[next].y - extremum->y;
216 if ( dist < -5 || dist > 5 )
219 } while ( next != idx );
221 /* now, set the `round' flag depending on the segment's kind */
223 FT_CURVE_TAG( glyph->outline.tags[prev] ) != FT_CURVE_TAG_ON ||
224 FT_CURVE_TAG( glyph->outline.tags[next] ) != FT_CURVE_TAG_ON );
226 AH_LOG(( "%c ", round ? 'r' : 'f' ));
230 rounds[num_rounds++] = extremum->y;
232 flats[num_flats++] = extremum->y;
237 /* we have computed the contents of the `rounds' and `flats' tables, */
238 /* now determine the reference and overshoot position of the blue; */
239 /* we simply take the median value after a simple short */
240 sort_values( num_rounds, rounds );
241 sort_values( num_flats, flats );
243 blue_ref = globals->blue_refs + blue;
244 blue_shoot = globals->blue_shoots + blue;
245 if ( num_flats == 0 && num_rounds == 0 )
248 *blue_shoot = -10000;
250 else if ( num_flats == 0 )
253 *blue_shoot = rounds[num_rounds / 2];
255 else if ( num_rounds == 0 )
258 *blue_shoot = flats[num_flats / 2];
262 *blue_ref = flats[num_flats / 2];
263 *blue_shoot = rounds[num_rounds / 2];
266 /* there are sometimes problems: if the overshoot position of top */
267 /* zones is under its reference position, or the opposite for bottom */
268 /* zones. We must thus check everything there and correct the errors */
269 if ( *blue_shoot != *blue_ref )
271 FT_Pos ref = *blue_ref;
272 FT_Pos shoot = *blue_shoot;
273 FT_Bool over_ref = FT_BOOL( shoot > ref );
276 if ( AH_IS_TOP_BLUE( blue ) ^ over_ref )
277 *blue_shoot = *blue_ref = ( shoot + ref ) / 2;
280 AH_LOG(( "-- ref = %ld, shoot = %ld\n", *blue_ref, *blue_shoot ));
283 /* reset original face charmap */
284 FT_Set_Charmap( face, charmap );
293 ah_hinter_compute_widths( AH_Hinter hinter )
295 /* scan the array of segments in each direction */
296 AH_Outline outline = hinter->glyph;
299 AH_Globals globals = &hinter->globals->design;
302 FT_Int* p_num_widths;
304 FT_Pos edge_distance_threshold = 32000;
307 globals->num_widths = 0;
308 globals->num_heights = 0;
310 /* For now, compute the standard width and height from the `o' */
311 /* character. I started computing the stem width of the `i' and the */
312 /* stem height of the "-", but it wasn't too good. Moreover, we now */
313 /* have a single character that gives us standard width and height. */
318 glyph_index = FT_Get_Char_Index( hinter->face, 'o' );
319 if ( glyph_index == 0 )
322 error = FT_Load_Glyph( hinter->face, glyph_index, FT_LOAD_NO_SCALE );
326 error = ah_outline_load( hinter->glyph, 0x10000L, 0x10000L, hinter->face );
330 ah_outline_compute_segments( hinter->glyph );
331 ah_outline_link_segments( hinter->glyph );
334 segments = outline->horz_segments;
335 limit = segments + outline->num_hsegments;
336 widths = globals->heights;
337 p_num_widths = &globals->num_heights;
339 for ( dimension = 1; dimension >= 0; dimension-- )
341 AH_Segment seg = segments;
343 FT_Int num_widths = 0;
346 for ( ; seg < limit; seg++ )
349 /* we only consider stem segments there! */
350 if ( link && link->link == seg && link > seg )
355 dist = seg->pos - link->pos;
359 if ( num_widths < AH_MAX_WIDTHS )
360 widths[num_widths++] = dist;
364 sort_values( num_widths, widths );
365 *p_num_widths = num_widths;
367 /* we will now try to find the smallest width */
368 if ( num_widths > 0 && widths[0] < edge_distance_threshold )
369 edge_distance_threshold = widths[0];
371 segments = outline->vert_segments;
372 limit = segments + outline->num_vsegments;
373 widths = globals->widths;
374 p_num_widths = &globals->num_widths;
377 /* Now, compute the edge distance threshold as a fraction of the */
378 /* smallest width in the font. Set it in `hinter.glyph' too! */
379 if ( edge_distance_threshold == 32000 )
380 edge_distance_threshold = 50;
383 hinter->glyph->edge_distance_threshold = edge_distance_threshold / 5;
390 FT_LOCAL_DEF( FT_Error )
391 ah_hinter_compute_globals( AH_Hinter hinter )
393 return ah_hinter_compute_widths( hinter ) ||
394 ah_hinter_compute_blues ( hinter );