Commit 14018b4af9c4274e0b89cd31e94ef78109f0d912

David Turner 2003-01-12T18:26:10

2003-01-11 David Chester <davidchester@qmx.net> * include/freetype/config/ftoption.h, src/autohint/ahglobal.h, src/autohint/ahglobal.c, src/autohint/ahglyph.c, src/autohint/ahtypes.h: included David Chester's patches to the auto-hinter in order to slightly improve the output. Note that everything is controlled through the new FT_CONFIG_OPTION_CHESTER_HINTS defined at the end of "ftoption.h", there are also individual FT_CONFIG_CHESTER_XXX macros to control individual "features". Note that all improvements are enabled by default, but can be tweaked for optimization and testing purpose. The configuration macros will most likely disappear in the short future. 2003-01-11 David Turner <david@freetype.org> * include/freetype/internal/fnttypes.h: fixed a structure field definition to avoid memory overwrites

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
diff --git a/ChangeLog b/ChangeLog
index 1ee3271..749395e 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,24 @@
+2003-01-11  David Chester  <davidchester@qmx.net>
+
+        * include/freetype/config/ftoption.h, src/autohint/ahglobal.h,
+        src/autohint/ahglobal.c, src/autohint/ahglyph.c,
+        src/autohint/ahtypes.h:
+
+          included David Chester's patches to the auto-hinter in order to
+          slightly improve the output. Note that everything is controlled
+          through the new FT_CONFIG_OPTION_CHESTER_HINTS defined at the
+          end of "ftoption.h", there are also individual FT_CONFIG_CHESTER_XXX
+          macros to control individual "features".
+
+          Note that all improvements are enabled by default, but can be
+          tweaked for optimization and testing purpose. The configuration
+          macros will most likely disappear in the short future.
+
+2003-01-11  David Turner  <david@freetype.org>
+
+        * include/freetype/internal/fnttypes.h: fixed a structure field
+        definition to avoid memory overwrites
+
 2003-01-08  Huw Dawies  <huw@codeweavers.com>
 
         * src/winfonts/winfnt.c: read 16 bytes into "reserved2", not "reserved"
diff --git a/include/freetype/config/ftoption.h b/include/freetype/config/ftoption.h
index 85c9e45..d5515c2 100644
--- a/include/freetype/config/ftoption.h
+++ b/include/freetype/config/ftoption.h
@@ -469,6 +469,17 @@ FT_BEGIN_HEADER
 
  /* */
 
+#undef   FT_CONFIG_OPTION_CHESTER_HINTS
+
+#ifdef   FT_CONFIG_OPTION_CHESTER_HINTS
+
+#  define  FT_CONFIG_CHESTER_SMALL_F
+#  define  FT_CONFIG_CHESTER_ASCENDER
+#  define  FT_CONFIG_CHESTER_SERIF
+#  define  FT_CONFIG_CHESTER_STEM
+
+#endif /* FT_CONFIG_OPTION_CHESTER_HINTS */
+
 FT_END_HEADER
 
 
diff --git a/include/freetype/internal/fnttypes.h b/include/freetype/internal/fnttypes.h
index 54e37ec..2edc567 100644
--- a/include/freetype/internal/fnttypes.h
+++ b/include/freetype/internal/fnttypes.h
@@ -108,7 +108,7 @@ FT_BEGIN_HEADER
     FT_UShort  B_space;
     FT_UShort  C_space;
     FT_UShort  color_table_offset;
-    FT_Byte    reserved2[4];
+    FT_Byte    reserved2[16];
 
   } WinFNT_HeaderRec, *WinFNT_Header;
 
diff --git a/src/autohint/ahglobal.c b/src/autohint/ahglobal.c
index 2ce8ac3..b8fb48a 100644
--- a/src/autohint/ahglobal.c
+++ b/src/autohint/ahglobal.c
@@ -32,6 +32,9 @@
   {
     "THEZOCQS",
     "HEZLOCUS",
+#ifdef FT_CONFIG_CHESTER_SMALL_F
+    "fijkdbh",
+#endif
     "xzroesc",
     "xzroesc",
     "pqgjy"
diff --git a/src/autohint/ahglobal.h b/src/autohint/ahglobal.h
index 9d557b9..ef99a01 100644
--- a/src/autohint/ahglobal.h
+++ b/src/autohint/ahglobal.h
@@ -32,8 +32,18 @@
 FT_BEGIN_HEADER
 
 
-#define AH_IS_TOP_BLUE( b )  ( (b) == AH_BLUE_CAPITAL_TOP || \
-                               (b) == AH_BLUE_SMALL_TOP   )
+#ifdef  FT_CONFIG_CHESTER_SMALL_F
+
+#  define AH_IS_TOP_BLUE( b )  ( (b) == AH_BLUE_CAPITAL_TOP || \
+                                 (b) == AH_BLUE_SMALL_F_TOP || \
+                                 (b) == AH_BLUE_SMALL_TOP   )
+
+#else /* !CHESTER_SMALL_F */
+
+#  define AH_IS_TOP_BLUE( b )  ( (b) == AH_BLUE_CAPITAL_TOP || \
+                                 (b) == AH_BLUE_SMALL_TOP   )
+
+#endif /* !CHESTER_SMALL_F */
 
 
   /* compute global metrics automatically */
diff --git a/src/autohint/ahglyph.c b/src/autohint/ahglyph.c
index ee628be..d4fcd4b 100644
--- a/src/autohint/ahglyph.c
+++ b/src/autohint/ahglyph.c
@@ -1350,10 +1350,20 @@
             else
               edge2 = seg2->edge;
 
+#ifdef FT_CONFIG_CHESTER_SERIF
+            if ( is_serif )
+            {
+              edge->serif   = edge2;
+              edge2->flags |= AH_EDGE_SERIF;
+            }
+            else
+              edge->link  = edge2;
+#else /* !CHESTER_SERIF */
             if ( is_serif )
               edge->serif = edge2;
             else
               edge->link  = edge2;
+#endif
           }
 
           seg = seg->edge_next;
@@ -1477,8 +1487,14 @@
 
       /* compute the initial threshold as a fraction of the EM size */
       best_dist = FT_MulFix( face_globals->face->units_per_EM / 40, y_scale );
+
+#ifdef FT_CONFIG_CHESTER_SMALL_F
+      if ( best_dist > 64 / 2 )
+        best_dist = 64 / 2;
+#else
       if ( best_dist > 64 / 4 )
-        best_dist = 64 / 4;
+         best_dist = 64 / 4;
+#endif
 
       for ( blue = AH_BLUE_CAPITAL_TOP; blue < AH_BLUE_MAX; blue++ )
       {
diff --git a/src/autohint/ahhint.c b/src/autohint/ahhint.c
index 1e2b7b3..e64ac7f 100644
--- a/src/autohint/ahhint.c
+++ b/src/autohint/ahhint.c
@@ -88,6 +88,137 @@
 
 
   /* compute the snapped width of a given stem */
+#ifdef FT_CONFIG_CHESTER_SERIF
+  static FT_Pos
+  ah_compute_stem_width( AH_Hinter      hinter,
+                         int            vertical,
+                         FT_Pos         width,
+                         AH_Edge_Flags  base_flags,
+                         AH_Edge_Flags  stem_flags )
+  {
+    AH_Globals  globals = &hinter->globals->scaled;
+    FT_Pos      dist    = width;
+    FT_Int      sign    = 0;
+
+
+    if ( dist < 0 )
+    {
+      dist = -width;
+      sign = 1;
+    }
+
+    if ( !hinter->do_stem_adjust )
+    {
+      /* leave stem widths unchanged */
+    }
+    else if ( (  vertical && !hinter->do_vert_snapping ) ||
+              ( !vertical && !hinter->do_horz_snapping ) )
+    {
+      /* smooth hinting process, very lightly quantize the stem width */
+      /*                                                              */
+
+      /* leave the widths of serifs alone */
+
+      if ( ( stem_flags & AH_EDGE_SERIF ) && vertical && ( dist < 3 * 64 ) )
+        goto Done_Width;
+
+      else if ( ( base_flags & AH_EDGE_ROUND ) )
+      {
+        if ( dist < 96 )
+          dist = 64;
+      }
+      else if ( dist < 56 )
+        dist = 56;
+
+      {
+        FT_Pos  delta = dist - globals->stds[vertical];
+
+
+        if ( delta < 0 )
+          delta = -delta;
+
+        if ( delta < 40 )
+        {
+          dist = globals->stds[vertical];
+          if ( dist < 48 )
+            dist = 48;
+
+          goto Done_Width;
+        }
+
+        if ( dist < 3 * 64 )
+        {
+          delta = ( dist & 63 );
+          dist &= -64;
+
+          if ( delta < 10 )
+            dist += delta;
+
+          else if ( delta < 32 )
+            dist += 10;
+
+          else if ( delta < 54 )
+            dist += 54;
+
+          else
+            dist += delta;
+        }
+        else
+          dist = ( dist + 32 ) & -64;
+      }
+    }
+    else
+    {
+      /* strong hinting process, snap the stem width to integer pixels */
+      /*                                                               */
+      if ( vertical )
+      {
+        dist = ah_snap_width( globals->heights, globals->num_heights, dist );
+
+        /* in the case of vertical hinting, always round */
+        /* the stem heights to integer pixels            */
+        if ( dist >= 64 )
+          dist = ( dist + 16 ) & -64;
+        else
+          dist = 64;
+      }
+      else
+      {
+        dist = ah_snap_width( globals->widths,  globals->num_widths, dist );
+
+        if ( hinter->flags & AH_HINTER_MONOCHROME )
+        {
+          /* monochrome horizontal hinting: snap widths to integer pixels */
+          /* with a different threshold                                   */
+          if ( dist < 64 )
+            dist = 64;
+          else
+            dist = ( dist + 32 ) & -64;
+        }
+        else
+        {
+          /* for horizontal anti-aliased hinting, we adopt a more subtle */
+          /* approach: we strengthen small stems, round stems whose size */
+          /* is between 1 and 2 pixels to an integer, otherwise nothing  */
+          if ( dist < 48 )
+            dist = ( dist + 64 ) >> 1;
+
+          else if ( dist < 128 )
+            dist = ( dist + 22 ) & -64;
+          else
+            /* XXX: round otherwise, prevent color fringes in LCD mode */
+            dist = ( dist + 32 ) & -64;
+        }
+      }
+    }
+
+  Done_Width:
+    if ( sign )
+      dist = -dist;
+
+    return dist;
+  }
+#else /* !CHESTER_SERIF */
   static FT_Pos
   ah_compute_stem_width( AH_Hinter  hinter,
                          int        vertical,
@@ -201,6 +332,7 @@
 
     return dist;
   }
+#endif /* !CHESTER_SERIF */
 
 
   /* align one stem edge relative to the previous stem edge */
@@ -212,9 +344,18 @@
   {
     FT_Pos  dist = stem_edge->opos - base_edge->opos;
 
+#ifdef FT_CONFIG_CHESTER_SERIF
+    FT_Pos  fitted_width = ah_compute_stem_width( hinter,
+                                                  vertical,
+                                                  dist,
+                                                  base_edge->flags,
+                                                  stem_edge->flags );
 
+    stem_edge->pos = base_edge->pos + fitted_width;
+#else
     stem_edge->pos = base_edge->pos +
                      ah_compute_stem_width( hinter, vertical, dist );
+#endif
   }
 
 
@@ -368,12 +509,61 @@
 
         if ( !anchor )
         {
+#ifdef FT_CONFIG_CHESTER_STEM
+          FT_Pos   org_len, org_center, cur_len;
+          FT_Pos   cur_pos1, error1, error2, u_off, d_off;
+
+          org_len    = edge2->opos - edge->opos;
+          cur_len    = ah_compute_stem_width( hinter, dimension, org_len,
+                                              edge->flags, edge2->flags   );
+
+          if (cur_len <= 64 )
+            u_off = d_off = 32;
+          else
+          {
+            u_off = 38;
+            d_off = 26;
+          }
+
+          if ( cur_len < 96 )
+          {
+            org_center = edge->opos + ( org_len >> 1 );
+
+            cur_pos1   = ( org_center + 32 ) & -64;
+
+            error1 = org_center - ( cur_pos1 - u_off );
+            if ( error1 < 0 )
+              error1 = -error1;
+
+            error2 = org_center - ( cur_pos1 + d_off );
+            if ( error2 < 0 )
+              error2 = -error2;
+
+            if ( error1 < error2 )
+              cur_pos1 -= u_off;
+            else
+              cur_pos1 += d_off;
+
+            edge->pos  = cur_pos1 - cur_len / 2;
+            edge2->pos = cur_pos1 + cur_len / 2;
+
+          }
+          else
+            edge->pos = ( edge->opos + 32 ) & -64;
+
+          anchor    = edge;
+
+          edge->flags |= AH_EDGE_DONE;
+
+          ah_align_linked_edge( hinter, edge, edge2, dimension );
+#else /* !CHESTER_STEM */
           edge->pos = ( edge->opos + 32 ) & -64;
           anchor    = edge;
 
           edge->flags |= AH_EDGE_DONE;
 
           ah_align_linked_edge( hinter, edge, edge2, dimension );
+#endif /* !CHESTER_STEM */
         }
         else
         {
@@ -385,7 +575,70 @@
           org_len    = edge2->opos - edge->opos;
           org_center = org_pos + ( org_len >> 1 );
 
+#ifdef FT_CONFIG_CHESTER_SERIF
+          cur_len    = ah_compute_stem_width( hinter, dimension, org_len,
+                                              edge->flags, edge2->flags  );
+#else  /* !CHESTER_SERIF */
           cur_len    = ah_compute_stem_width( hinter, dimension, org_len );
+#endif /* !CHESTER_SERIF */
+
+#ifdef FT_CONFIG_CHESTER_STEM
+          if ( cur_len < 96 )
+          {
+            FT_Pos  u_off, d_off;
+
+
+            cur_pos1   = ( org_center + 32 ) & -64;
+
+            if (cur_len <= 64 )
+              u_off = d_off = 32;
+            else
+            {
+              u_off = 38;
+              d_off = 26;
+            }
+
+            delta1 = org_center - (cur_pos1 - u_off);
+            if ( delta1 < 0 )
+              delta1 = -delta1;
+
+            delta2 = org_center - (cur_pos1 + d_off);
+            if ( delta2 < 0 )
+              delta2 = -delta2;
+
+            if ( delta1 < delta2 )
+              cur_pos1 -= u_off;
+            else
+              cur_pos1 += d_off;
+
+            edge->pos  = cur_pos1 - cur_len / 2;
+            edge2->pos = cur_pos1 + cur_len / 2;
+          }
+          else
+          {
+
+            org_pos    = anchor->pos + (edge->opos - anchor->opos);
+            org_len    = edge2->opos - edge->opos;
+            org_center = org_pos + ( org_len >> 1 );
+
+            cur_len    = ah_compute_stem_width( hinter, dimension, org_len,
+                                                edge->flags, edge2->flags );
+
+            cur_pos1   = ( org_pos + 32 ) & -64;
+            delta1     = ( cur_pos1 + ( cur_len >> 1 ) - org_center );
+            if ( delta1 < 0 )
+              delta1 = -delta1;
+
+            cur_pos2   = ( ( org_pos + org_len + 32 ) & -64 ) - cur_len;
+            delta2     = ( cur_pos2 + ( cur_len >> 1 ) - org_center );
+            if ( delta2 < 0 )
+              delta2 = -delta2;
+
+            edge->pos  = ( delta1 < delta2 ) ? cur_pos1 : cur_pos2;
+            edge2->pos = edge->pos + cur_len;
+          }
+
+#else /* !CHESTER_STEM */
 
           cur_pos1   = ( org_pos + 32 ) & -64;
           delta1     = ( cur_pos1 + ( cur_len >> 1 ) - org_center );
@@ -400,6 +653,8 @@
           edge->pos  = ( delta1 <= delta2 ) ? cur_pos1 : cur_pos2;
           edge2->pos = edge->pos + cur_len;
 
+#endif /* !CHESTER_STEM */
+
           edge->flags  |= AH_EDGE_DONE;
           edge2->flags |= AH_EDGE_DONE;
 
diff --git a/src/autohint/ahtypes.h b/src/autohint/ahtypes.h
index c9fbf27..33633b1 100644
--- a/src/autohint/ahtypes.h
+++ b/src/autohint/ahtypes.h
@@ -385,12 +385,26 @@ FT_BEGIN_HEADER
   } AH_OutlineRec, *AH_Outline;
 
 
-#define AH_BLUE_CAPITAL_TOP     0                              /* THEZOCQS */
-#define AH_BLUE_CAPITAL_BOTTOM  ( AH_BLUE_CAPITAL_TOP + 1 )    /* HEZLOCUS */
-#define AH_BLUE_SMALL_TOP       ( AH_BLUE_CAPITAL_BOTTOM + 1 ) /* xzroesc  */
-#define AH_BLUE_SMALL_BOTTOM    ( AH_BLUE_SMALL_TOP + 1 )      /* xzroesc  */
-#define AH_BLUE_SMALL_MINOR     ( AH_BLUE_SMALL_BOTTOM + 1 )   /* pqgjy    */
-#define AH_BLUE_MAX             ( AH_BLUE_SMALL_MINOR + 1 )
+#ifdef FT_CONFIG_CHESTER_SMALL_F
+
+#  define AH_BLUE_CAPITAL_TOP     0                              /* THEZOCQS */
+#  define AH_BLUE_CAPITAL_BOTTOM  ( AH_BLUE_CAPITAL_TOP + 1 )    /* HEZLOCUS */
+#  define AH_BLUE_SMALL_F_TOP     ( AH_BLUE_CAPITAL_BOTTOM + 1 ) /* fijkdbh  */
+#  define AH_BLUE_SMALL_TOP       ( AH_BLUE_SMALL_F_TOP + 1 )    /* xzroesc  */
+#  define AH_BLUE_SMALL_BOTTOM    ( AH_BLUE_SMALL_TOP + 1 )      /* xzroesc  */
+#  define AH_BLUE_SMALL_MINOR     ( AH_BLUE_SMALL_BOTTOM + 1 )   /* pqgjy    */
+#  define AH_BLUE_MAX             ( AH_BLUE_SMALL_MINOR + 1 )
+
+#else /* !CHESTER_SMALL_F */
+
+#  define AH_BLUE_CAPITAL_TOP     0                              /* THEZOCQS */
+#  define AH_BLUE_CAPITAL_BOTTOM  ( AH_BLUE_CAPITAL_TOP + 1 )    /* HEZLOCUS */
+#  define AH_BLUE_SMALL_TOP       ( AH_BLUE_CAPITAL_BOTTOM + 1)  /* xzroesc  */
+#  define AH_BLUE_SMALL_BOTTOM    ( AH_BLUE_SMALL_TOP + 1 )      /* xzroesc  */
+#  define AH_BLUE_SMALL_MINOR     ( AH_BLUE_SMALL_BOTTOM + 1 )   /* pqgjy    */
+#  define AH_BLUE_MAX             ( AH_BLUE_SMALL_MINOR + 1 )
+
+#endif /* !CHESTER_SMALL_F */
 
   typedef FT_Int  AH_Blue;
 
diff --git a/src/truetype/ttobjs.c b/src/truetype/ttobjs.c
index 636d32f..41afa35 100644
--- a/src/truetype/ttobjs.c
+++ b/src/truetype/ttobjs.c
@@ -566,16 +566,27 @@
       size->ttmetrics.y_ratio = 0x10000L;
     }
 
+#ifdef FT_CONFIG_CHESTER_ASCENDER
+
+    /* Compute root ascender, descender, test height, and max_advance */
+    metrics->ascender    = ( FT_MulFix( face->root.ascender,
+                                        metrics->y_scale ) + 63 ) & -64;
+    metrics->descender   = ( FT_MulFix( face->root.descender,
+                                        metrics->y_scale ) + 0  ) & -64;
+#else /* !CHESTER_ASCENDER */
     /* Compute root ascender, descender, test height, and max_advance */
     metrics->ascender    = ( FT_MulFix( face->root.ascender,
                                         metrics->y_scale ) + 32 ) & -64;
     metrics->descender   = ( FT_MulFix( face->root.descender,
                                         metrics->y_scale ) + 32 ) & -64;
+#endif /* !CHESTER_ASCENDER */
+
     metrics->height      = ( FT_MulFix( face->root.height,
                                         metrics->y_scale ) + 32 ) & -64;
     metrics->max_advance = ( FT_MulFix( face->root.max_advance_width,
                                         metrics->x_scale ) + 32 ) & -64;
 
+
 #ifdef TT_CONFIG_OPTION_EMBEDDED_BITMAPS
     /* set to `invalid' by default */
     size->strike_index = 0xFFFFU;