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