1 /***************************************************************************/
5 /* Routines used to compute global metrics automatically (body). */
7 /* Copyright 2000-2001 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] =
41 /* simple insertion sort */
43 sort_values( FT_Int count,
49 for ( i = 1; i < count; i++ )
51 for ( j = i; j > 0; j-- )
53 if ( table[j] > table[j - 1] )
57 table[j] = table[j - 1];
65 ah_hinter_compute_blues( AH_Hinter* hinter )
68 AH_Globals* globals = &hinter->globals->design;
69 FT_Pos flats [MAX_TEST_CHARACTERS];
70 FT_Pos rounds[MAX_TEST_CHARACTERS];
83 /* save current charmap */
84 charmap = face->charmap;
86 /* do we have a Unicode charmap in there? */
87 error = FT_Select_Charmap( face, ft_encoding_unicode );
91 /* we compute the blues simply by loading each character from the */
92 /* 'blue_chars[blues]' string, then compute its top-most and */
93 /* bottom-most points */
95 AH_LOG(( "blue zones computation\n" ));
96 AH_LOG(( "------------------------------------------------\n" ));
98 for ( blue = ah_blue_capital_top; blue < ah_blue_max; blue++ )
100 const char* p = blue_chars[blue];
101 const char* limit = p + MAX_TEST_CHARACTERS;
102 FT_Pos *blue_ref, *blue_shoot;
105 AH_LOG(( "blue %3d: ", blue ));
110 for ( ; p < limit; p++ )
115 FT_Vector* point_limit;
120 /* exit if we reach the end of the string */
124 AH_LOG(( "`%c'", *p ));
126 /* load the character in the face -- skip unknown or empty ones */
127 glyph_index = FT_Get_Char_Index( face, (FT_UInt)*p );
128 if ( glyph_index == 0 )
131 error = FT_Load_Glyph( face, glyph_index, FT_LOAD_NO_SCALE );
132 if ( error || glyph->outline.n_points <= 0 )
135 /* now compute min or max point indices and coordinates */
136 points = glyph->outline.points;
137 point_limit = points + glyph->outline.n_points;
142 if ( AH_IS_TOP_BLUE( blue ) )
144 for ( ; point < point_limit; point++ )
145 if ( point->y > extremum->y )
150 for ( ; point < point_limit; point++ )
151 if ( point->y < extremum->y )
155 AH_LOG(( "%5d", (int)extremum->y ));
157 /* now, check whether the point belongs to a straight or round */
158 /* segment; we first need to find in which contour the extremum */
159 /* lies, then see its previous and next points */
161 FT_Int index = (FT_Int)( extremum - points );
163 FT_Int first, last, prev, next, end;
170 for ( n = 0; n < glyph->outline.n_contours; n++ )
172 end = glyph->outline.contours[n];
181 /* XXX: should never happen! */
185 /* now look for the previous and next points that are not on the */
186 /* same Y coordinate. Threshold the `closeness'... */
198 dist = points[prev].y - extremum->y;
199 if ( dist < -5 || dist > 5 )
202 } while ( prev != index );
211 dist = points[next].y - extremum->y;
212 if ( dist < -5 || dist > 5 )
215 } while ( next != index );
217 /* now, set the `round' flag depending on the segment's kind */
219 FT_CURVE_TAG( glyph->outline.tags[prev] ) != FT_Curve_Tag_On ||
220 FT_CURVE_TAG( glyph->outline.tags[next] ) != FT_Curve_Tag_On );
222 AH_LOG(( "%c ", round ? 'r' : 'f' ));
226 rounds[num_rounds++] = extremum->y;
228 flats[num_flats++] = extremum->y;
233 /* we have computed the contents of the `rounds' and `flats' tables, */
234 /* now determine the reference and overshoot position of the blue; */
235 /* we simply take the median value after a simple short */
236 sort_values( num_rounds, rounds );
237 sort_values( num_flats, flats );
239 blue_ref = globals->blue_refs + blue;
240 blue_shoot = globals->blue_shoots + blue;
241 if ( num_flats == 0 && num_rounds == 0 )
244 *blue_shoot = -10000;
246 else if ( num_flats == 0 )
249 *blue_shoot = rounds[num_rounds / 2];
251 else if ( num_rounds == 0 )
254 *blue_shoot = flats[num_flats / 2];
258 *blue_ref = flats[num_flats / 2];
259 *blue_shoot = rounds[num_rounds / 2];
262 /* there are sometimes problems: if the overshoot position of top */
263 /* zones is under its reference position, or the opposite for bottom */
264 /* zones. We must thus check everything there and correct the errors */
265 if ( *blue_shoot != *blue_ref )
267 FT_Pos ref = *blue_ref;
268 FT_Pos shoot = *blue_shoot;
269 FT_Bool over_ref = FT_BOOL( shoot > ref );
272 if ( AH_IS_TOP_BLUE( blue ) ^ over_ref )
273 *blue_shoot = *blue_ref = ( shoot + ref ) / 2;
276 AH_LOG(( "-- ref = %ld, shoot = %ld\n", *blue_ref, *blue_shoot ));
279 /* reset original face charmap */
280 FT_Set_Charmap( face, charmap );
289 ah_hinter_compute_widths( AH_Hinter* hinter )
291 /* scan the array of segments in each direction */
292 AH_Outline* outline = hinter->glyph;
293 AH_Segment* segments;
295 AH_Globals* globals = &hinter->globals->design;
298 FT_Int* p_num_widths;
300 FT_Pos edge_distance_threshold = 32000;
303 globals->num_widths = 0;
304 globals->num_heights = 0;
306 /* For now, compute the standard width and height from the `o' */
307 /* character. I started computing the stem width of the `i' and the */
308 /* stem height of the "-", but it wasn't too good. Moreover, we now */
309 /* have a single character that gives us standard width and height. */
314 glyph_index = FT_Get_Char_Index( hinter->face, 'o' );
315 if ( glyph_index == 0 )
318 error = FT_Load_Glyph( hinter->face, glyph_index, FT_LOAD_NO_SCALE );
322 error = ah_outline_load( hinter->glyph, hinter->face );
326 ah_outline_compute_segments( hinter->glyph );
327 ah_outline_link_segments( hinter->glyph );
330 segments = outline->horz_segments;
331 limit = segments + outline->num_hsegments;
332 widths = globals->heights;
333 p_num_widths = &globals->num_heights;
335 for ( dimension = 1; dimension >= 0; dimension-- )
337 AH_Segment* seg = segments;
339 FT_Int num_widths = 0;
342 for ( ; seg < limit; seg++ )
345 /* we only consider stem segments there! */
346 if ( link && link->link == seg && link > seg )
351 dist = seg->pos - link->pos;
355 if ( num_widths < 12 )
356 widths[num_widths++] = dist;
360 sort_values( num_widths, widths );
361 *p_num_widths = num_widths;
363 /* we will now try to find the smallest width */
364 if ( num_widths > 0 && widths[0] < edge_distance_threshold )
365 edge_distance_threshold = widths[0];
367 segments = outline->vert_segments;
368 limit = segments + outline->num_vsegments;
369 widths = globals->widths;
370 p_num_widths = &globals->num_widths;
374 /* Now, compute the edge distance threshold as a fraction of the */
375 /* smallest width in the font. Set it in `hinter.glyph' too! */
376 if ( edge_distance_threshold == 32000 )
377 edge_distance_threshold = 50;
380 hinter->glyph->edge_distance_threshold = edge_distance_threshold / 5;
387 FT_LOCAL_DEF FT_Error
388 ah_hinter_compute_globals( AH_Hinter* hinter )
390 return ah_hinter_compute_widths( hinter ) ||
391 ah_hinter_compute_blues ( hinter );