]> git.saurik.com Git - wxWidgets.git/blob - src/freetype/autohint/ahglobal.c
Removed 'interface' pragma for gcc 2.96
[wxWidgets.git] / src / freetype / autohint / ahglobal.c
1 /***************************************************************************/
2 /* */
3 /* ahglobal.c */
4 /* */
5 /* Routines used to compute global metrics automatically (body). */
6 /* */
7 /* Copyright 2000 Catharon Productions Inc. */
8 /* Author: David Turner */
9 /* */
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. */
16 /* */
17 /* Note that this license is compatible with the FreeType license. */
18 /* */
19 /***************************************************************************/
20
21
22 #ifdef FT_FLAT_COMPILE
23
24 #include "ahglobal.h"
25 #include "ahglyph.h"
26
27 #else
28
29 #include <autohint/ahglobal.h>
30 #include <autohint/ahglyph.h>
31
32 #endif
33
34
35 #define MAX_TEST_CHARACTERS 12
36
37 static
38 const char* blue_chars[ah_blue_max] =
39 {
40 "THEZOCQS",
41 "HEZLOCUS",
42 "xzroesc",
43 "xzroesc",
44 "pqgjy"
45 };
46
47
48 /* simple insertion sort */
49 static
50 void sort_values( FT_Int count,
51 FT_Pos* table )
52 {
53 FT_Int i, j, swap;
54
55
56 for ( i = 1; i < count; i++ )
57 {
58 for ( j = i; j > 1; j-- )
59 {
60 if ( table[j] > table[j - 1] )
61 break;
62
63 swap = table[j];
64 table[j] = table[j - 1];
65 table[j - 1] = swap;
66 }
67 }
68 }
69
70
71 static
72 FT_Error ah_hinter_compute_blues( AH_Hinter* hinter )
73 {
74 AH_Blue blue;
75 AH_Globals* globals = &hinter->globals->design;
76 FT_Pos flats [MAX_TEST_CHARACTERS];
77 FT_Pos rounds[MAX_TEST_CHARACTERS];
78 FT_Int num_flats;
79 FT_Int num_rounds;
80
81 FT_Face face;
82 FT_GlyphSlot glyph;
83 FT_Error error;
84 FT_CharMap charmap;
85
86
87 face = hinter->face;
88 glyph = face->glyph;
89
90 /* save current charmap */
91 charmap = face->charmap;
92
93 /* do we have a Unicode charmap in there? */
94 error = FT_Select_Charmap( face, ft_encoding_unicode );
95 if ( error )
96 goto Exit;
97
98 /* we compute the blues simply by loading each character from the */
99 /* 'blue_chars[blues]' string, then compute its top-most and */
100 /* bottom-most points */
101
102 AH_LOG(( "blue zones computation\n" ));
103 AH_LOG(( "------------------------------------------------\n" ));
104
105 for ( blue = ah_blue_capital_top; blue < ah_blue_max; blue++ )
106 {
107 const char* p = blue_chars[blue];
108 const char* limit = p + MAX_TEST_CHARACTERS;
109 FT_Pos *blue_ref, *blue_shoot;
110
111
112 AH_LOG(( "blue %3d: ", blue ));
113
114 num_flats = 0;
115 num_rounds = 0;
116
117 for ( ; p < limit; p++ )
118 {
119 FT_UInt glyph_index;
120 FT_Vector* extremum;
121 FT_Vector* points;
122 FT_Vector* point_limit;
123 FT_Vector* point;
124 FT_Bool round;
125
126
127 /* exit if we reach the end of the string */
128 if ( !*p )
129 break;
130
131 AH_LOG(( "`%c'", *p ));
132
133 /* load the character in the face -- skip unknown or empty ones */
134 glyph_index = FT_Get_Char_Index( face, (FT_UInt)*p );
135 if ( glyph_index == 0 )
136 continue;
137
138 error = FT_Load_Glyph( face, glyph_index, FT_LOAD_NO_SCALE );
139 if ( error || glyph->outline.n_points <= 0 )
140 continue;
141
142 /* now compute min or max point indices and coordinates */
143 points = glyph->outline.points;
144 point_limit = points + glyph->outline.n_points;
145 point = points;
146 extremum = point;
147 point++;
148
149 if ( AH_IS_TOP_BLUE( blue ) )
150 {
151 for ( ; point < point_limit; point++ )
152 if ( point->y > extremum->y )
153 extremum = point;
154 }
155 else
156 {
157 for ( ; point < point_limit; point++ )
158 if ( point->y < extremum->y )
159 extremum = point;
160 }
161
162 AH_LOG(( "%5d", (int)extremum->y ));
163
164 /* now, check whether the point belongs to a straight or round */
165 /* segment; we first need to find in which contour the extremum */
166 /* lies, then see its previous and next points */
167 {
168 FT_Int index = extremum - points;
169 FT_Int n;
170 FT_Int first, last, prev, next, end;
171 FT_Pos dist;
172
173
174 last = -1;
175 first = 0;
176
177 for ( n = 0; n < glyph->outline.n_contours; n++ )
178 {
179 end = glyph->outline.contours[n];
180 if ( end >= index )
181 {
182 last = end;
183 break;
184 }
185 first = end + 1;
186 }
187
188 /* XXX: should never happen! */
189 if ( last < 0 )
190 continue;
191
192 /* now look for the previous and next points that are not on the */
193 /* same Y coordinate. Threshold the `closeness'... */
194
195 prev = index;
196 next = prev;
197
198 do
199 {
200 if ( prev > first )
201 prev--;
202 else
203 prev = last;
204
205 dist = points[prev].y - extremum->y;
206 if ( dist < -5 || dist > 5 )
207 break;
208
209 } while ( prev != index );
210
211 do
212 {
213 if ( next < last )
214 next++;
215 else
216 next = first;
217
218 dist = points[next].y - extremum->y;
219 if ( dist < -5 || dist > 5 )
220 break;
221
222 } while ( next != index );
223
224 /* now, set the `round' flag depending on the segment's kind */
225 round =
226 FT_CURVE_TAG( glyph->outline.tags[prev] ) != FT_Curve_Tag_On ||
227 FT_CURVE_TAG( glyph->outline.tags[next] ) != FT_Curve_Tag_On ;
228
229 AH_LOG(( "%c ", round ? 'r' : 'f' ));
230 }
231
232 if ( round )
233 rounds[num_rounds++] = extremum->y;
234 else
235 flats[num_flats++] = extremum->y;
236 }
237
238 AH_LOG(( "\n" ));
239
240 /* we have computed the contents of the `rounds' and `flats' tables, */
241 /* now determine the reference and overshoot position of the blue; */
242 /* we simply take the median value after a simple short */
243 sort_values( num_rounds, rounds );
244 sort_values( num_flats, flats );
245
246 blue_ref = globals->blue_refs + blue;
247 blue_shoot = globals->blue_shoots + blue;
248 if ( num_flats == 0 && num_rounds == 0 )
249 {
250 *blue_ref = -10000;
251 *blue_shoot = -10000;
252 }
253 else if ( num_flats == 0 )
254 {
255 *blue_ref =
256 *blue_shoot = rounds[num_rounds / 2];
257 }
258 else if ( num_rounds == 0 )
259 {
260 *blue_ref =
261 *blue_shoot = flats[num_flats / 2];
262 }
263 else
264 {
265 *blue_ref = flats[num_flats / 2];
266 *blue_shoot = rounds[num_rounds / 2];
267 }
268
269 /* there are sometimes problems: if the overshoot position of top */
270 /* zones is under its reference position, or the opposite for bottom */
271 /* zones. We must thus check everything there and correct the errors */
272 if ( *blue_shoot != *blue_ref )
273 {
274 FT_Pos ref = *blue_ref;
275 FT_Pos shoot = *blue_shoot;
276 FT_Bool over_ref = ( shoot > ref );
277
278
279 if ( AH_IS_TOP_BLUE( blue ) ^ over_ref )
280 *blue_shoot = *blue_ref = ( shoot + ref ) / 2;
281 }
282
283 AH_LOG(( "-- ref = %ld, shoot = %ld\n", *blue_ref, *blue_shoot ));
284 }
285
286 /* reset original face charmap */
287 FT_Set_Charmap( face, charmap );
288 error = 0;
289
290 Exit:
291 return error;
292 }
293
294
295 static
296 FT_Error ah_hinter_compute_widths( AH_Hinter* hinter )
297 {
298 /* scan the array of segments in each direction */
299 AH_Outline* outline = hinter->glyph;
300 AH_Segment* segments;
301 AH_Segment* limit;
302 AH_Globals* globals = &hinter->globals->design;
303 FT_Pos* widths;
304 FT_Int dimension;
305 FT_Int* p_num_widths;
306 FT_Error error = 0;
307 FT_Pos edge_distance_threshold = 32000;
308
309
310 globals->num_widths = 0;
311 globals->num_heights = 0;
312
313 /* For now, compute the standard width and height from the `o' */
314 /* character. I started computing the stem width of the `i' and the */
315 /* stem height of the "-", but it wasn't too good. Moreover, we now */
316 /* have a single character that gives us standard width and height. */
317 {
318 FT_UInt glyph_index;
319
320
321 glyph_index = FT_Get_Char_Index( hinter->face, 'o' );
322 if ( glyph_index == 0 )
323 return 0;
324
325 error = FT_Load_Glyph( hinter->face, glyph_index, FT_LOAD_NO_SCALE );
326 if ( error )
327 goto Exit;
328
329 error = ah_outline_load( hinter->glyph, hinter->face );
330 if ( error )
331 goto Exit;
332
333 ah_outline_compute_segments( hinter->glyph );
334 ah_outline_link_segments( hinter->glyph );
335 }
336
337 segments = outline->horz_segments;
338 limit = segments + outline->num_hsegments;
339 widths = globals->heights;
340 p_num_widths = &globals->num_heights;
341
342 for ( dimension = 1; dimension >= 0; dimension-- )
343 {
344 AH_Segment* seg = segments;
345 AH_Segment* link;
346 FT_Int num_widths = 0;
347
348
349 for ( ; seg < limit; seg++ )
350 {
351 link = seg->link;
352 /* we only consider stem segments there! */
353 if ( link && link->link == seg && link > seg )
354 {
355 FT_Int dist;
356
357
358 dist = seg->pos - link->pos;
359 if ( dist < 0 )
360 dist = -dist;
361
362 if ( num_widths < 12 )
363 widths[num_widths++] = dist;
364 }
365 }
366
367 sort_values( num_widths, widths );
368 *p_num_widths = num_widths;
369
370 /* we will now try to find the smallest width */
371 if ( num_widths > 0 && widths[0] < edge_distance_threshold )
372 edge_distance_threshold = widths[0];
373
374 segments = outline->vert_segments;
375 limit = segments + outline->num_vsegments;
376 widths = globals->widths;
377 p_num_widths = &globals->num_widths;
378
379 }
380
381 /* Now, compute the edge distance threshold as a fraction of the */
382 /* smallest width in the font. Set it in `hinter.glyph' too! */
383 if ( edge_distance_threshold == 32000 )
384 edge_distance_threshold = 50;
385
386 /* let's try 20% */
387 hinter->glyph->edge_distance_threshold = edge_distance_threshold / 5;
388
389 Exit:
390 return error;
391 }
392
393
394 LOCAL_FUNC
395 FT_Error ah_hinter_compute_globals( AH_Hinter* hinter )
396 {
397 return ah_hinter_compute_widths( hinter ) ||
398 ah_hinter_compute_blues ( hinter );
399 }
400
401
402 /* END */