update for HEAD-2003050101
[reactos.git] / lib / freetype / src / autohint / ahglobal.c
1 /***************************************************************************/
2 /*                                                                         */
3 /*  ahglobal.c                                                             */
4 /*                                                                         */
5 /*    Routines used to compute global metrics automatically (body).        */
6 /*                                                                         */
7 /*  Copyright 2000-2001, 2002 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 #include <ft2build.h>
23 #include FT_INTERNAL_DEBUG_H
24 #include "ahglobal.h"
25 #include "ahglyph.h"
26
27
28 #define MAX_TEST_CHARACTERS  12
29
30   static
31   const char*  blue_chars[AH_BLUE_MAX] =
32   {
33     "THEZOCQS",
34     "HEZLOCUS",
35 #ifdef FT_CONFIG_CHESTER_SMALL_F
36     "fijkdbh",
37 #endif
38     "xzroesc",
39     "xzroesc",
40     "pqgjy"
41   };
42
43
44   /* simple insertion sort */
45   static void
46   sort_values( FT_Int   count,
47                FT_Pos*  table )
48   {
49     FT_Int  i, j;
50     FT_Pos  swap;
51
52
53     for ( i = 1; i < count; i++ )
54     {
55       for ( j = i; j > 0; j-- )
56       {
57         if ( table[j] > table[j - 1] )
58           break;
59
60         swap         = table[j];
61         table[j]     = table[j - 1];
62         table[j - 1] = swap;
63       }
64     }
65   }
66
67
68   static FT_Error
69   ah_hinter_compute_blues( AH_Hinter  hinter )
70   {
71     AH_Blue       blue;
72     AH_Globals    globals = &hinter->globals->design;
73     FT_Pos        flats [MAX_TEST_CHARACTERS];
74     FT_Pos        rounds[MAX_TEST_CHARACTERS];
75     FT_Int        num_flats;
76     FT_Int        num_rounds;
77
78     FT_Face       face;
79     FT_GlyphSlot  glyph;
80     FT_Error      error;
81     FT_CharMap    charmap;
82
83
84     face  = hinter->face;
85     glyph = face->glyph;
86
87     /* save current charmap */
88     charmap = face->charmap;
89
90     /* do we have a Unicode charmap in there? */
91     error = FT_Select_Charmap( face, FT_ENCODING_UNICODE );
92     if ( error )
93       goto Exit;
94
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                                             */
98
99     AH_LOG(( "blue zones computation\n" ));
100     AH_LOG(( "------------------------------------------------\n" ));
101
102     for ( blue = AH_BLUE_CAPITAL_TOP; blue < AH_BLUE_MAX; blue++ )
103     {
104       const char*  p     = blue_chars[blue];
105       const char*  limit = p + MAX_TEST_CHARACTERS;
106       FT_Pos       *blue_ref, *blue_shoot;
107
108
109       AH_LOG(( "blue %3d: ", blue ));
110
111       num_flats  = 0;
112       num_rounds = 0;
113
114       for ( ; p < limit; p++ )
115       {
116         FT_UInt     glyph_index;
117         FT_Vector*  extremum;
118         FT_Vector*  points;
119         FT_Vector*  point_limit;
120         FT_Vector*  point;
121         FT_Bool     round;
122
123
124         /* exit if we reach the end of the string */
125         if ( !*p )
126           break;
127
128         AH_LOG(( "`%c'", *p ));
129
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 )
133           continue;
134
135         error = FT_Load_Glyph( face, glyph_index, FT_LOAD_NO_SCALE );
136         if ( error || glyph->outline.n_points <= 0 )
137           continue;
138
139         /* now compute min or max point indices and coordinates */
140         points      = glyph->outline.points;
141         point_limit = points + glyph->outline.n_points;
142         point       = points;
143         extremum    = point;
144         point++;
145
146         if ( AH_IS_TOP_BLUE( blue ) )
147         {
148           for ( ; point < point_limit; point++ )
149             if ( point->y > extremum->y )
150               extremum = point;
151         }
152         else
153         {
154           for ( ; point < point_limit; point++ )
155             if ( point->y < extremum->y )
156               extremum = point;
157         }
158
159         AH_LOG(( "%5d", (int)extremum->y ));
160
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                  */
164         {
165           FT_Int  idx = (FT_Int)( extremum - points );
166           FT_Int  n;
167           FT_Int  first, last, prev, next, end;
168           FT_Pos  dist;
169
170
171           last  = -1;
172           first = 0;
173
174           for ( n = 0; n < glyph->outline.n_contours; n++ )
175           {
176             end = glyph->outline.contours[n];
177             if ( end >= idx )
178             {
179               last = end;
180               break;
181             }
182             first = end + 1;
183           }
184
185           /* XXX: should never happen! */
186           if ( last < 0 )
187             continue;
188
189           /* now look for the previous and next points that are not on the */
190           /* same Y coordinate.  Threshold the `closeness'...              */
191
192           prev = idx;
193           next = prev;
194
195           do
196           {
197             if ( prev > first )
198               prev--;
199             else
200               prev = last;
201
202             dist = points[prev].y - extremum->y;
203             if ( dist < -5 || dist > 5 )
204               break;
205
206           } while ( prev != idx );
207
208           do
209           {
210             if ( next < last )
211               next++;
212             else
213               next = first;
214
215             dist = points[next].y - extremum->y;
216             if ( dist < -5 || dist > 5 )
217               break;
218
219           } while ( next != idx );
220
221           /* now, set the `round' flag depending on the segment's kind */
222           round = FT_BOOL(
223             FT_CURVE_TAG( glyph->outline.tags[prev] ) != FT_CURVE_TAG_ON ||
224             FT_CURVE_TAG( glyph->outline.tags[next] ) != FT_CURVE_TAG_ON );
225
226           AH_LOG(( "%c ", round ? 'r' : 'f' ));
227         }
228
229         if ( round )
230           rounds[num_rounds++] = extremum->y;
231         else
232           flats[num_flats++] = extremum->y;
233       }
234
235       AH_LOG(( "\n" ));
236
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  );
242
243       blue_ref   = globals->blue_refs + blue;
244       blue_shoot = globals->blue_shoots + blue;
245       if ( num_flats == 0 && num_rounds == 0 )
246       {
247         *blue_ref   = -10000;
248         *blue_shoot = -10000;
249       }
250       else if ( num_flats == 0 )
251       {
252         *blue_ref   =
253         *blue_shoot = rounds[num_rounds / 2];
254       }
255       else if ( num_rounds == 0 )
256       {
257         *blue_ref   =
258         *blue_shoot = flats[num_flats / 2];
259       }
260       else
261       {
262         *blue_ref   = flats[num_flats / 2];
263         *blue_shoot = rounds[num_rounds / 2];
264       }
265
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 )
270       {
271         FT_Pos   ref      = *blue_ref;
272         FT_Pos   shoot    = *blue_shoot;
273         FT_Bool  over_ref = FT_BOOL( shoot > ref );
274
275
276         if ( AH_IS_TOP_BLUE( blue ) ^ over_ref )
277           *blue_shoot = *blue_ref = ( shoot + ref ) / 2;
278       }
279
280       AH_LOG(( "-- ref = %ld, shoot = %ld\n", *blue_ref, *blue_shoot ));
281     }
282
283     /* reset original face charmap */
284     FT_Set_Charmap( face, charmap );
285     error = 0;
286
287   Exit:
288     return error;
289   }
290
291
292   static FT_Error
293   ah_hinter_compute_widths( AH_Hinter  hinter )
294   {
295     /* scan the array of segments in each direction */
296     AH_Outline  outline = hinter->glyph;
297     AH_Segment  segments;
298     AH_Segment  limit;
299     AH_Globals  globals = &hinter->globals->design;
300     FT_Pos*     widths;
301     FT_Int      dimension;
302     FT_Int*     p_num_widths;
303     FT_Error    error = 0;
304     FT_Pos      edge_distance_threshold = 32000;
305
306
307     globals->num_widths  = 0;
308     globals->num_heights = 0;
309
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.  */
314     {
315       FT_UInt   glyph_index;
316
317
318       glyph_index = FT_Get_Char_Index( hinter->face, 'o' );
319       if ( glyph_index == 0 )
320         return 0;
321
322       error = FT_Load_Glyph( hinter->face, glyph_index, FT_LOAD_NO_SCALE );
323       if ( error )
324         goto Exit;
325
326       error = ah_outline_load( hinter->glyph, 0x10000L, 0x10000L, hinter->face );
327       if ( error )
328         goto Exit;
329
330       ah_outline_compute_segments( hinter->glyph );
331       ah_outline_link_segments( hinter->glyph );
332     }
333
334     segments     = outline->horz_segments;
335     limit        = segments + outline->num_hsegments;
336     widths       = globals->heights;
337     p_num_widths = &globals->num_heights;
338
339     for ( dimension = 1; dimension >= 0; dimension-- )
340     {
341       AH_Segment  seg = segments;
342       AH_Segment  link;
343       FT_Int      num_widths = 0;
344
345
346       for ( ; seg < limit; seg++ )
347       {
348         link = seg->link;
349         /* we only consider stem segments there! */
350         if ( link && link->link == seg && link > seg )
351         {
352           FT_Pos  dist;
353
354
355           dist = seg->pos - link->pos;
356           if ( dist < 0 )
357             dist = -dist;
358
359           if ( num_widths < AH_MAX_WIDTHS )
360             widths[num_widths++] = dist;
361         }
362       }
363
364       sort_values( num_widths, widths );
365       *p_num_widths = num_widths;
366
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];
370
371       segments     = outline->vert_segments;
372       limit        = segments + outline->num_vsegments;
373       widths       = globals->widths;
374       p_num_widths = &globals->num_widths;
375     }
376
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;
381
382     /* let's try 20% */
383     hinter->glyph->edge_distance_threshold = edge_distance_threshold / 5;
384
385   Exit:
386     return error;
387   }
388
389
390   FT_LOCAL_DEF( FT_Error )
391   ah_hinter_compute_globals( AH_Hinter  hinter )
392   {
393     return ah_hinter_compute_widths( hinter ) ||
394            ah_hinter_compute_blues ( hinter );
395   }
396
397
398 /* END */