Commit 9f7d079c896fe4915caf6b43ee6091691d7c4b5e

David Turner 2002-07-03T21:00:26

adding Owen Taylor's patches to the Postscript hinter for testing

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
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
diff --git a/src/pshinter/Jamfile b/src/pshinter/Jamfile
index 6eea3be..9e40038 100644
--- a/src/pshinter/Jamfile
+++ b/src/pshinter/Jamfile
@@ -10,7 +10,7 @@ SubDirHdrs  [ FT2_SubDir  src pshinter ] ;
 
   if $(FT2_MULTI)
   {
-    _sources = pshrec pshglob pshalgo1 pshalgo2 pshmod ;
+    _sources = pshrec pshglob pshalgo1 pshalgo2 pshalgo3 pshmod ;
   }
   else
   {
diff --git a/src/pshinter/pshalgo.h b/src/pshinter/pshalgo.h
index 6097671..e79ad02 100644
--- a/src/pshinter/pshalgo.h
+++ b/src/pshinter/pshalgo.h
@@ -22,17 +22,26 @@
 FT_BEGIN_HEADER
 
 /* define to choose hinting algorithm */
-#define PSH_ALGORITHM_2
+#define PSH_ALGORITHM_3
 
-#ifdef PSH_ALGORITHM_1
+#if defined(PSH_ALGORITHM_1)
 
-#include "pshalgo1.h"
-#define PS_HINTS_APPLY_FUNC  ps1_hints_apply
+#  include "pshalgo1.h"
+#  define PS_HINTS_APPLY_FUNC  ps1_hints_apply
+
+#elif defined(PSH_ALGORITHM_2)
+
+#  include "pshalgo2.h"
+#  define PS_HINTS_APPLY_FUNC  ps2_hints_apply
+
+#elif defined(PSH_ALGORITHM_3)
+
+#  include "pshalgo3.h"
+#  define PS_HINTS_APPLY_FUNC  ps3_hints_apply
 
 #else
 
-#include "pshalgo2.h"
-#define PS_HINTS_APPLY_FUNC  ps2_hints_apply
+#  error "invalid Postscript Hinter algorithm selection"
 
 #endif
 
diff --git a/src/pshinter/pshalgo3.c b/src/pshinter/pshalgo3.c
new file mode 100644
index 0000000..afc357e
--- /dev/null
+++ b/src/pshinter/pshalgo3.c
@@ -0,0 +1,1722 @@
+/***************************************************************************/
+/*                                                                         */
+/*  pshalgo3.c                                                             */
+/*                                                                         */
+/*    PostScript hinting algorithm 3 (body).                               */
+/*                                                                         */
+/*  Copyright 2001, 2002 by                                                */
+/*  David Turner, Robert Wilhelm, and Werner Lemberg.                      */
+/*                                                                         */
+/*  This file is part of the FreeType project, and may only be used        */
+/*  modified and distributed under the terms of the FreeType project       */
+/*  license, LICENSE.TXT.  By continuing to use, modify, or distribute     */
+/*  this file you indicate that you have read the license and              */
+/*  understand and accept it fully.                                        */
+/*                                                                         */
+/***************************************************************************/
+
+
+#include <ft2build.h>
+#include FT_INTERNAL_OBJECTS_H
+#include FT_INTERNAL_DEBUG_H
+#include "pshalgo3.h"
+
+#undef  FT_COMPONENT
+#define FT_COMPONENT  trace_pshalgo2
+
+#ifdef DEBUG_HINTER
+  extern PSH3_Hint_Table  ps3_debug_hint_table = 0;
+  extern PSH3_HintFunc    ps3_debug_hint_func  = 0;
+  extern PSH3_Glyph       ps3_debug_glyph      = 0;
+#endif
+
+
+
+#undef  SNAP_STEMS
+#undef  ONLY_ALIGN_Y
+      
+
+
+  /*************************************************************************/
+  /*************************************************************************/
+  /*****                                                               *****/
+  /*****                  BASIC HINTS RECORDINGS                       *****/
+  /*****                                                               *****/
+  /*************************************************************************/
+  /*************************************************************************/
+
+  /* return true iff two stem hints overlap */
+  static FT_Int
+  psh3_hint_overlap( PSH3_Hint  hint1,
+                     PSH3_Hint  hint2 )
+  {
+    return ( hint1->org_pos + hint1->org_len >= hint2->org_pos &&
+             hint2->org_pos + hint2->org_len >= hint1->org_pos );
+  }
+
+
+  /* destroy hints table */
+  static void
+  psh3_hint_table_done( PSH3_Hint_Table  table,
+                        FT_Memory        memory )
+  {
+    FT_FREE( table->zones );
+    table->num_zones = 0;
+    table->zone      = 0;
+
+    FT_FREE( table->sort );
+    FT_FREE( table->hints );
+    table->num_hints   = 0;
+    table->max_hints   = 0;
+    table->sort_global = 0;
+  }
+
+
+  /* deactivate all hints in a table */
+  static void
+  psh3_hint_table_deactivate( PSH3_Hint_Table  table )
+  {
+    FT_UInt   count = table->max_hints;
+    PSH3_Hint  hint  = table->hints;
+
+
+    for ( ; count > 0; count--, hint++ )
+    {
+      psh3_hint_deactivate( hint );
+      hint->order = -1;
+    }
+  }
+
+
+  /* internal function used to record a new hint */
+  static void
+  psh3_hint_table_record( PSH3_Hint_Table  table,
+                          FT_UInt          idx )
+  {
+    PSH3_Hint  hint = table->hints + idx;
+
+
+    if ( idx >= table->max_hints )
+    {
+      FT_ERROR(( "%s.activate: invalid hint index %d\n", idx ));
+      return;
+    }
+
+    /* ignore active hints */
+    if ( psh3_hint_is_active( hint ) )
+      return;
+
+    psh3_hint_activate( hint );
+
+    /* now scan the current active hint set in order to determine */
+    /* if we are overlapping with another segment                 */
+    {
+      PSH3_Hint*  sorted = table->sort_global;
+      FT_UInt     count  = table->num_hints;
+      PSH3_Hint   hint2;
+
+
+      hint->parent = 0;
+      for ( ; count > 0; count--, sorted++ )
+      {
+        hint2 = sorted[0];
+
+        if ( psh3_hint_overlap( hint, hint2 ) )
+        {
+          hint->parent = hint2;
+          break;
+        }
+      }
+    }
+
+    if ( table->num_hints < table->max_hints )
+      table->sort_global[table->num_hints++] = hint;
+    else
+      FT_ERROR(( "%s.activate: too many sorted hints!  BUG!\n",
+                 "ps.fitter" ));
+  }
+
+
+  static void
+  psh3_hint_table_record_mask( PSH3_Hint_Table  table,
+                               PS_Mask          hint_mask )
+  {
+    FT_Int    mask = 0, val = 0;
+    FT_Byte*  cursor = hint_mask->bytes;
+    FT_UInt   idx, limit;
+
+
+    limit = hint_mask->num_bits;
+
+    for ( idx = 0; idx < limit; idx++ )
+    {
+      if ( mask == 0 )
+      {
+        val  = *cursor++;
+        mask = 0x80;
+      }
+
+      if ( val & mask )
+        psh3_hint_table_record( table, idx );
+
+      mask >>= 1;
+    }
+  }
+
+
+  /* create hints table */
+  static FT_Error
+  psh3_hint_table_init( PSH3_Hint_Table  table,
+                        PS_Hint_Table    hints,
+                        PS_Mask_Table    hint_masks,
+                        PS_Mask_Table    counter_masks,
+                        FT_Memory        memory )
+  {
+    FT_UInt   count = hints->num_hints;
+    FT_Error  error;
+
+    FT_UNUSED( counter_masks );
+
+
+    /* allocate our tables */
+    if ( FT_NEW_ARRAY( table->sort,  2 * count     ) ||
+         FT_NEW_ARRAY( table->hints,     count     ) ||
+         FT_NEW_ARRAY( table->zones, 2 * count + 1 ) )
+      goto Exit;
+
+    table->max_hints   = count;
+    table->sort_global = table->sort + count;
+    table->num_hints   = 0;
+    table->num_zones   = 0;
+    table->zone        = 0;
+
+    /* now, initialize the "hints" array */
+    {
+      PSH3_Hint  write = table->hints;
+      PS_Hint    read  = hints->hints;
+
+
+      for ( ; count > 0; count--, write++, read++ )
+      {
+        write->org_pos = read->pos;
+        write->org_len = read->len;
+        write->flags   = read->flags;
+      }
+    }
+
+    /* we now need to determine the initial "parent" stems; first  */
+    /* activate the hints that are given by the initial hint masks */
+    if ( hint_masks )
+    {
+      FT_UInt  Count = hint_masks->num_masks;
+      PS_Mask  Mask  = hint_masks->masks;
+
+
+      table->hint_masks = hint_masks;
+
+      for ( ; Count > 0; Count--, Mask++ )
+        psh3_hint_table_record_mask( table, Mask );
+    }
+
+    /* now, do a linear parse in case some hints were left alone */
+    if ( table->num_hints != table->max_hints )
+    {
+      FT_UInt   Index, Count;
+
+
+      FT_ERROR(( "%s.init: missing/incorrect hint masks!\n" ));
+      Count = table->max_hints;
+      for ( Index = 0; Index < Count; Index++ )
+        psh3_hint_table_record( table, Index );
+    }
+
+  Exit:
+    return error;
+  }
+
+
+  static void
+  psh3_hint_table_activate_mask( PSH3_Hint_Table  table,
+                                 PS_Mask          hint_mask )
+  {
+    FT_Int    mask = 0, val = 0;
+    FT_Byte*  cursor = hint_mask->bytes;
+    FT_UInt   idx, limit, count;
+
+
+    limit = hint_mask->num_bits;
+    count = 0;
+
+    psh3_hint_table_deactivate( table );
+
+    for ( idx = 0; idx < limit; idx++ )
+    {
+      if ( mask == 0 )
+      {
+        val  = *cursor++;
+        mask = 0x80;
+      }
+
+      if ( val & mask )
+      {
+        PSH3_Hint  hint = &table->hints[idx];
+
+
+        if ( !psh3_hint_is_active( hint ) )
+        {
+          FT_UInt     count2;
+
+#if 0
+          PSH3_Hint*  sort = table->sort;
+          PSH3_Hint   hint2;
+
+          for ( count2 = count; count2 > 0; count2--, sort++ )
+          {
+            hint2 = sort[0];
+            if ( psh3_hint_overlap( hint, hint2 ) )
+              FT_ERROR(( "%s.activate_mask: found overlapping hints\n",
+                         "psf.hint" ));
+          }
+#else
+          count2 = 0;
+#endif
+
+          if ( count2 == 0 )
+          {
+            psh3_hint_activate( hint );
+            if ( count < table->max_hints )
+              table->sort[count++] = hint;
+            else
+              FT_ERROR(( "%s.activate_mask: too many active hints\n",
+                         "psf.hint" ));
+          }
+        }
+      }
+
+      mask >>= 1;
+    }
+    table->num_hints = count;
+
+    /* now, sort the hints; they are guaranteed to not overlap */
+    /* so we can compare their "org_pos" field directly        */
+    {
+      FT_Int      i1, i2;
+      PSH3_Hint   hint1, hint2;
+      PSH3_Hint*  sort = table->sort;
+
+
+      /* a simple bubble sort will do, since in 99% of cases, the hints */
+      /* will be already sorted -- and the sort will be linear          */
+      for ( i1 = 1; i1 < (FT_Int)count; i1++ )
+      {
+        hint1 = sort[i1];
+        for ( i2 = i1 - 1; i2 >= 0; i2-- )
+        {
+          hint2 = sort[i2];
+
+          if ( hint2->org_pos < hint1->org_pos )
+            break;
+
+          sort[i2 + 1] = hint2;
+          sort[i2]     = hint1;
+        }
+      }
+    }
+  }
+
+
+  /*************************************************************************/
+  /*************************************************************************/
+  /*****                                                               *****/
+  /*****               HINTS GRID-FITTING AND OPTIMIZATION             *****/
+  /*****                                                               *****/
+  /*************************************************************************/
+  /*************************************************************************/
+
+#ifdef DEBUG_HINTER
+  static void
+  ps3_simple_scale( PSH3_Hint_Table  table,
+                    FT_Fixed         scale,
+                    FT_Fixed         delta,
+                    FT_Int           dimension )
+  {
+    PSH3_Hint  hint;
+    FT_UInt    count;
+
+
+    for ( count = 0; count < table->max_hints; count++ )
+    {
+      hint = table->hints + count;
+
+      hint->cur_pos = FT_MulFix( hint->org_pos, scale ) + delta;
+      hint->cur_len = FT_MulFix( hint->org_len, scale );
+
+      if ( ps3_debug_hint_func )
+        ps3_debug_hint_func( hint, dimension );
+    }
+  }
+#endif
+
+  static FT_Fixed
+  psh3_hint_snap_stem_side_delta ( FT_Fixed pos,
+				   FT_Fixed len )
+  {
+    FT_Fixed delta1 = ( ( pos + 32 ) & -64 ) - pos;
+    FT_Fixed delta2 = ( ( pos + len + 32 ) & -64  ) - pos - len;
+	      
+    if ( ABS( delta1 ) <= ABS( delta2 ) )
+    {
+      return delta1;
+    }
+    else
+    {
+      return delta2;
+    }
+  }
+
+
+  static void
+  psh3_hint_align( PSH3_Hint    hint,
+                   PSH_Globals  globals,
+                   FT_Int       dimension )
+  {
+    PSH_Dimension  dim   = &globals->dimension[dimension];
+    FT_Fixed       scale = dim->scale_mult;
+    FT_Fixed       delta = dim->scale_delta;
+
+
+    if ( !psh3_hint_is_fitted(hint) )
+    {
+      FT_Pos  pos = FT_MulFix( hint->org_pos, scale ) + delta;
+      FT_Pos  len = FT_MulFix( hint->org_len, scale );
+
+#ifdef SNAP_STEMS
+      FT_Pos  fit_center;
+      FT_Pos  fit_len;
+
+      PSH_AlignmentRec  align;
+
+      /* compute fitted width/height */
+      fit_len = 0;
+      if ( hint->org_len )
+      {
+        fit_len = psh_dimension_snap_width( dim, hint->org_len );
+        if ( fit_len < 64 )
+          fit_len = 64;
+        else
+          fit_len = ( fit_len + 32 ) & -64;
+      }
+
+      hint->cur_len = fit_len;
+
+      /* check blue zones for horizontal stems */
+      align.align = PSH_BLUE_ALIGN_NONE;
+      align.align_bot = align.align_top = 0;
+
+      if ( dimension == 1 )
+        psh_blues_snap_stem( &globals->blues,
+                             hint->org_pos + hint->org_len,
+                             hint->org_pos,
+                             &align );
+
+      switch ( align.align )
+      {
+      case PSH_BLUE_ALIGN_TOP:
+        /* the top of the stem is aligned against a blue zone */
+        hint->cur_pos = align.align_top - fit_len;
+        break;
+
+      case PSH_BLUE_ALIGN_BOT:
+        /* the bottom of the stem is aligned against a blue zone */
+        hint->cur_pos = align.align_bot;
+        break;
+
+      case PSH_BLUE_ALIGN_TOP | PSH_BLUE_ALIGN_BOT:
+        /* both edges of the stem are aligned against blue zones */
+        hint->cur_pos = align.align_bot;
+        hint->cur_len = align.align_top - align.align_bot;
+        break;
+
+      default:
+        {
+          PSH3_Hint  parent = hint->parent;
+
+
+          if ( parent )
+          {
+            FT_Pos  par_org_center, par_cur_center;
+            FT_Pos  cur_org_center, cur_delta;
+
+
+            /* ensure that parent is already fitted */
+            if ( !psh3_hint_is_fitted( parent ) )
+              psh3_hint_align( parent, globals, dimension );
+
+            par_org_center = parent->org_pos + ( parent->org_len / 2);
+            par_cur_center = parent->cur_pos + ( parent->cur_len / 2);
+            cur_org_center = hint->org_pos   + ( hint->org_len   / 2);
+
+            cur_delta = FT_MulFix( cur_org_center - par_org_center, scale );
+#if 0
+            if ( cur_delta >= 0 )
+              cur_delta = ( cur_delta + 16 ) & -64;
+            else
+              cur_delta = -( (-cur_delta + 16 ) & -64 );
+#endif
+            pos = par_cur_center + cur_delta - ( len >> 1 );
+          }
+
+          /* normal processing */
+          if ( ( fit_len / 64 ) & 1 )
+          {
+            /* odd number of pixels */
+            fit_center = ( ( pos + ( len >> 1 ) ) & -64 ) + 32;
+          }
+          else
+          {
+            /* even number of pixels */
+            fit_center = ( pos + ( len >> 1 ) + 32 ) & -64;
+          }
+
+          hint->cur_pos = fit_center - ( fit_len >> 1 );
+        }
+      }
+#else
+      PSH_AlignmentRec  align;
+
+      hint->cur_len = len;
+
+      /* check blue zones for horizontal stems */
+      align.align = PSH_BLUE_ALIGN_NONE;
+      align.align_bot = align.align_top = 0;
+
+      if ( dimension == 1 )
+        psh_blues_snap_stem( &globals->blues,
+                             hint->org_pos + hint->org_len,
+                             hint->org_pos,
+                             &align );
+#ifdef ONLY_ALIGN_Y
+      else
+      {
+	hint->cur_pos = pos;
+	return;
+      }
+#endif
+      
+      switch ( align.align )
+      {
+      case PSH_BLUE_ALIGN_TOP:
+        /* the top of the stem is aligned against a blue zone */
+        hint->cur_pos = align.align_top - len;
+        break;
+
+      case PSH_BLUE_ALIGN_BOT:
+        /* the bottom of the stem is aligned against a blue zone */
+        hint->cur_pos = align.align_bot;
+        break;
+
+      case PSH_BLUE_ALIGN_TOP | PSH_BLUE_ALIGN_BOT:
+        /* both edges of the stem are aligned against blue zones */
+        hint->cur_pos = align.align_bot;
+        hint->cur_len = align.align_top - align.align_bot;
+        break;
+
+      default:
+        {
+          PSH3_Hint  parent = hint->parent;
+
+          if ( parent )
+          {
+            FT_Pos  par_org_center, par_cur_center;
+            FT_Pos  cur_org_center, cur_delta;
+
+
+            /* ensure that parent is already fitted */
+            if ( !psh3_hint_is_fitted( parent ) )
+              psh3_hint_align( parent, globals, dimension );
+
+            par_org_center = parent->org_pos + ( parent->org_len / 2);
+            par_cur_center = parent->cur_pos + ( parent->cur_len / 2);
+            cur_org_center = hint->org_pos   + ( hint->org_len   / 2);
+
+            cur_delta = FT_MulFix( cur_org_center - par_org_center, scale );
+            pos = par_cur_center + cur_delta - ( len >> 1 );
+          }
+
+          {
+	    /* Stems less than one pixel wide are easy - we want to
+	     * make them as dark as possible, so they must fall within
+	     * one pixel. If the stem is split between two pixels
+	     * then snap the edge that is nearer to the pixel boundary
+	     * to the pixel boundary
+	     */
+	    if (len <= 64)
+	    {
+	      if ( ( pos + len + 63 ) / 64  != pos / 64 + 1 )
+		pos += psh3_hint_snap_stem_side_delta ( pos, len );
+	    }
+	    /* Position stems other to minimize the amount of mid-grays.
+	     * There are, in general, two positions that do this,
+	     * illustrated as A) and B) below.
+	     *
+	     *   +                   +                   +                   +
+	     *
+	     * A)             |--------------------------------|
+	     * B)   |--------------------------------|
+	     * C)       |--------------------------------|
+	     *
+	     * Position A) (split the excess stem equally) should be better
+	     * for stems of width N + f where f < 0.5
+	     *
+	     * Position B) (split the deficiency equally) should be better
+	     * for stems of width N + f where f > 0.5
+	     *
+	     * It turns out though that minimizing the total number of lit
+	     * pixels is also important, so position C), with one edge
+	     * aligned with a pixel boundary is actually preferable
+	     * to A). There are also more possibile positions for C) than
+	     * for A) or B), so it involves less distortion of the overall
+	     * character shape.
+	     */
+	    else
+	    {
+	      FT_Fixed frac_len = len & 63;
+	      FT_Fixed center = pos + ( len >> 1 );
+
+	      FT_Fixed delta_a, delta_b;
+
+	      if ( ( len / 64 ) & 1 )
+	      {
+	        delta_a = ( center & -64 ) + 32 - center;
+	        delta_b = ( ( center + 32 ) & - 64 ) - center;
+	      }
+	      else
+	      {
+	        delta_a = ( ( center + 32 ) & - 64 ) - center;
+	        delta_b = ( center & -64 ) + 32 - center;
+	      }
+
+	      /* We choose between B) and C) above based on the amount
+	       * of fractinal stem width; for small amounts, choose
+	       * C) always, for large amounts, B) always, and inbetween,
+	       * pick whichever one involves less stem movement.
+	       */
+	      if (frac_len < 32)
+	      {
+		pos += psh3_hint_snap_stem_side_delta ( pos, len );
+	      }
+	      else if (frac_len < 48)
+	      {
+		FT_Fixed side_delta = psh3_hint_snap_stem_side_delta ( pos, len );
+		  
+		if ( ABS( side_delta ) < ABS( delta_b ) )
+		  pos += side_delta;
+		else
+		  pos += delta_b;
+	      }
+	      else
+	      {
+		pos += delta_b;
+	      }
+	    }
+	  }
+	 
+	  hint->cur_pos = pos;
+	}
+      }
+#endif
+      
+      psh3_hint_set_fitted( hint );
+
+#ifdef DEBUG_HINTER
+      if ( ps3_debug_hint_func )
+        ps3_debug_hint_func( hint, dimension );
+#endif
+    }
+  }
+
+
+  static void
+  psh3_hint_table_align_hints( PSH3_Hint_Table  table,
+                               PSH_Globals      globals,
+                               FT_Int           dimension )
+  {
+    PSH3_Hint      hint;
+    FT_UInt        count;
+
+#ifdef DEBUG_HINTER
+    PSH_Dimension  dim   = &globals->dimension[dimension];
+    FT_Fixed       scale = dim->scale_mult;
+    FT_Fixed       delta = dim->scale_delta;
+
+
+    if ( ps_debug_no_vert_hints && dimension == 0 )
+    {
+      ps3_simple_scale( table, scale, delta, dimension );
+      return;
+    }
+
+    if ( ps_debug_no_horz_hints && dimension == 1 )
+    {
+      ps3_simple_scale( table, scale, delta, dimension );
+      return;
+    }
+#endif
+
+    hint  = table->hints;
+    count = table->max_hints;
+
+    for ( ; count > 0; count--, hint++ )
+      psh3_hint_align( hint, globals, dimension );
+  }
+
+
+  /*************************************************************************/
+  /*************************************************************************/
+  /*****                                                               *****/
+  /*****                POINTS INTERPOLATION ROUTINES                  *****/
+  /*****                                                               *****/
+  /*************************************************************************/
+  /*************************************************************************/
+
+#define PSH3_ZONE_MIN  -3200000
+#define PSH3_ZONE_MAX  +3200000
+
+#define xxDEBUG_ZONES
+
+
+#ifdef DEBUG_ZONES
+
+#include <stdio.h>
+
+  static void
+  psh3_print_zone( PSH3_Zone  zone )
+  {
+    printf( "zone [scale,delta,min,max] = [%.3f,%.3f,%d,%d]\n",
+             zone->scale/65536.0,
+             zone->delta/64.0,
+             zone->min,
+             zone->max );
+  }
+
+#else
+
+#define psh3_print_zone( x )   do { } while ( 0 )
+
+#endif
+
+#if 0
+  /* setup interpolation zones once the hints have been grid-fitted */
+  /* by the optimizer                                               */
+  static void
+  psh3_hint_table_setup_zones( PSH3_Hint_Table  table,
+                               FT_Fixed         scale,
+                               FT_Fixed         delta )
+  {
+    FT_UInt     count;
+    PSH3_Zone   zone;
+    PSH3_Hint  *sort, hint, hint2;
+
+
+    zone = table->zones;
+
+    /* special case, no hints defined */
+    if ( table->num_hints == 0 )
+    {
+      zone->scale = scale;
+      zone->delta = delta;
+      zone->min   = PSH3_ZONE_MIN;
+      zone->max   = PSH3_ZONE_MAX;
+
+      table->num_zones = 1;
+      table->zone      = zone;
+      return;
+    }
+
+    /* the first zone is before the first hint */
+    /* x' = (x-x0)*s + x0' = x*s + ( x0' - x0*s ) */
+    sort = table->sort;
+    hint = sort[0];
+
+    zone->scale = scale;
+    zone->delta = hint->cur_pos - FT_MulFix( hint->org_pos, scale );
+    zone->min   = PSH3_ZONE_MIN;
+    zone->max   = hint->org_pos;
+
+    psh3_print_zone( zone );
+
+    zone++;
+
+    for ( count = table->num_hints; count > 0; count-- )
+    {
+      FT_Fixed  scale2;
+
+
+      if ( hint->org_len > 0 )
+      {
+        /* setup a zone for inner-stem interpolation */
+        /* (x' - x0') = (x - x0)*(x1'-x0')/(x1-x0)   */
+        /* x' = x*s2 + x0' - x0*s2                   */
+
+        scale2      = FT_DivFix( hint->cur_len, hint->org_len );
+        zone->scale = scale2;
+        zone->min   = hint->org_pos;
+        zone->max   = hint->org_pos + hint->org_len;
+        zone->delta = hint->cur_pos - FT_MulFix( zone->min, scale2 );
+
+        psh3_print_zone( zone );
+
+        zone++;
+      }
+
+      if ( count == 1 )
+        break;
+
+      sort++;
+      hint2 = sort[0];
+
+      /* setup zone for inter-stem interpolation */
+      /* (x'-x1') = (x-x1)*(x2'-x1')/(x2-x1)     */
+      /* x' = x*s3 + x1' - x1*s3                 */
+
+      scale2 = FT_DivFix( hint2->cur_pos - (hint->cur_pos + hint->cur_len),
+                          hint2->org_pos - (hint->org_pos + hint->org_len) );
+      zone->scale = scale2;
+      zone->min   = hint->org_pos + hint->org_len;
+      zone->max   = hint2->org_pos;
+      zone->delta = hint->cur_pos + hint->cur_len -
+                    FT_MulFix( zone->min, scale2 );
+
+      psh3_print_zone( zone );
+
+      zone++;
+
+      hint = hint2;
+    }
+
+    /* the last zone */
+    zone->scale = scale;
+    zone->min   = hint->org_pos + hint->org_len;
+    zone->max   = PSH3_ZONE_MAX;
+    zone->delta = hint->cur_pos + hint->cur_len -
+                  FT_MulFix( zone->min, scale );
+
+    psh3_print_zone( zone );
+
+    zone++;
+
+    table->num_zones = zone - table->zones;
+    table->zone      = table->zones;
+  }
+#endif
+
+#if 0
+  /* tune a single coordinate with the current interpolation zones */
+  static FT_Pos
+  psh3_hint_table_tune_coord( PSH3_Hint_Table  table,
+                              FT_Int           coord )
+  {
+    PSH3_Zone   zone;
+
+
+    zone = table->zone;
+
+    if ( coord < zone->min )
+    {
+      do
+      {
+        if ( zone == table->zones )
+          break;
+
+        zone--;
+
+      } while ( coord < zone->min );
+      table->zone = zone;
+    }
+    else if ( coord > zone->max )
+    {
+      do
+      {
+        if ( zone == table->zones + table->num_zones - 1 )
+          break;
+
+        zone++;
+
+      } while ( coord > zone->max );
+      table->zone = zone;
+    }
+
+    return FT_MulFix( coord, zone->scale ) + zone->delta;
+  }
+#endif
+
+#if 0
+ /* tune a given outline with current interpolation zones */
+ /* the function only works in a single dimension..       */
+  static void
+  psh3_hint_table_tune_outline( PSH3_Hint_Table  table,
+                                FT_Outline*      outline,
+                                PSH_Globals      globals,
+                                FT_Int           dimension )
+
+  {
+    FT_UInt        count, first, last;
+    PS_Mask_Table  hint_masks = table->hint_masks;
+    PS_Mask        mask;
+    PSH_Dimension  dim        = &globals->dimension[dimension];
+    FT_Fixed       scale      = dim->scale_mult;
+    FT_Fixed       delta      = dim->scale_delta;
+
+
+    if ( hint_masks && hint_masks->num_masks > 0 )
+    {
+      first = 0;
+      mask  = hint_masks->masks;
+      count = hint_masks->num_masks;
+
+      for ( ; count > 0; count--, mask++ )
+      {
+        last = mask->end_point;
+
+        if ( last > first )
+        {
+          FT_Vector*   vec;
+          FT_Int       count2;
+
+
+          psh3_hint_table_activate_mask( table, mask );
+          psh3_hint_table_optimize( table, globals, outline, dimension );
+          psh3_hint_table_setup_zones( table, scale, delta );
+          last = mask->end_point;
+
+          vec    = outline->points + first;
+          count2 = last - first;
+
+          for ( ; count2 > 0; count2--, vec++ )
+          {
+            FT_Pos  x, *px;
+
+
+            px  = dimension ? &vec->y : &vec->x;
+            x   = *px;
+
+            *px = psh3_hint_table_tune_coord( table, (FT_Int)x );
+          }
+        }
+
+        first = last;
+      }
+    }
+    else    /* no hints in this glyph, simply scale the outline */
+    {
+      FT_Vector*  vec;
+
+
+      vec   = outline->points;
+      count = outline->n_points;
+
+      if ( dimension == 0 )
+      {
+        for ( ; count > 0; count--, vec++ )
+          vec->x = FT_MulFix( vec->x, scale ) + delta;
+      }
+      else
+      {
+        for ( ; count > 0; count--, vec++ )
+          vec->y = FT_MulFix( vec->y, scale ) + delta;
+      }
+    }
+  }
+#endif
+
+
+  /*************************************************************************/
+  /*************************************************************************/
+  /*****                                                               *****/
+  /*****                    HINTER GLYPH MANAGEMENT                    *****/
+  /*****                                                               *****/
+  /*************************************************************************/
+  /*************************************************************************/
+
+  static int
+  psh3_point_is_extremum( PSH3_Point  point )
+  {
+    PSH3_Point  before = point;
+    PSH3_Point  after  = point;
+    FT_Pos      d_before;
+    FT_Pos      d_after;
+
+
+    do
+    {
+      before = before->prev;
+      if ( before == point )
+        return 0;
+
+      d_before = before->org_u - point->org_u;
+
+    } while ( d_before == 0 );
+
+    do
+    {
+      after = after->next;
+      if ( after == point )
+        return 0;
+
+      d_after = after->org_u - point->org_u;
+
+    } while ( d_after == 0 );
+
+    return ( ( d_before > 0 && d_after > 0 ) ||
+             ( d_before < 0 && d_after < 0 ) );
+  }
+
+
+  static void
+  psh3_glyph_done( PSH3_Glyph  glyph )
+  {
+    FT_Memory  memory = glyph->memory;
+
+
+    psh3_hint_table_done( &glyph->hint_tables[1], memory );
+    psh3_hint_table_done( &glyph->hint_tables[0], memory );
+
+    FT_FREE( glyph->points );
+    FT_FREE( glyph->contours );
+
+    glyph->num_points   = 0;
+    glyph->num_contours = 0;
+
+    glyph->memory = 0;
+  }
+
+
+  static int
+  psh3_compute_dir( FT_Pos  dx,
+                    FT_Pos  dy )
+  {
+    FT_Pos  ax, ay;
+    int     result = PSH3_DIR_NONE;
+
+
+    ax = ( dx >= 0 ) ? dx : -dx;
+    ay = ( dy >= 0 ) ? dy : -dy;
+
+    if ( ay * 12 < ax )
+    {
+      /* |dy| <<< |dx|  means a near-horizontal segment */
+      result = ( dx >= 0 ) ? PSH3_DIR_RIGHT : PSH3_DIR_LEFT;
+    }
+    else if ( ax * 12 < ay )
+    {
+      /* |dx| <<< |dy|  means a near-vertical segment */
+      result = ( dy >= 0 ) ? PSH3_DIR_UP : PSH3_DIR_DOWN;
+    }
+
+    return result;
+  }
+
+
+  static FT_Error
+  psh3_glyph_init( PSH3_Glyph   glyph,
+                   FT_Outline*  outline,
+                   PS_Hints     ps_hints,
+                   PSH_Globals  globals )
+  {
+    FT_Error   error;
+    FT_Memory  memory;
+
+
+    /* clear all fields */
+    ft_memset( glyph, 0, sizeof ( *glyph ) );
+
+    memory = globals->memory;
+
+    /* allocate and setup points + contours arrays */
+    if ( FT_NEW_ARRAY( glyph->points,   outline->n_points   ) ||
+         FT_NEW_ARRAY( glyph->contours, outline->n_contours ) )
+      goto Exit;
+
+    glyph->num_points   = outline->n_points;
+    glyph->num_contours = outline->n_contours;
+
+    {
+      FT_UInt       first = 0, next, n;
+      PSH3_Point    points  = glyph->points;
+      PSH3_Contour  contour = glyph->contours;
+
+
+      for ( n = 0; n < glyph->num_contours; n++ )
+      {
+        FT_Int      count;
+        PSH3_Point  point;
+
+
+        next  = outline->contours[n] + 1;
+        count = next - first;
+
+        contour->start = points + first;
+        contour->count = (FT_UInt)count;
+
+        if ( count > 0 )
+        {
+          point = points + first;
+
+          point->prev    = points + next - 1;
+          point->contour = contour;
+
+          for ( ; count > 1; count-- )
+          {
+            point[0].next = point + 1;
+            point[1].prev = point;
+            point++;
+            point->contour = contour;
+          }
+          point->next = points + first;
+        }
+
+        contour++;
+        first = next;
+      }
+    }
+
+    {
+      PSH3_Point  points = glyph->points;
+      PSH3_Point  point  = points;
+      FT_Vector*  vec    = outline->points;
+      FT_UInt     n;
+
+
+      for ( n = 0; n < glyph->num_points; n++, point++ )
+      {
+        FT_Int  n_prev = point->prev - points;
+        FT_Int  n_next = point->next - points;
+        FT_Pos  dxi, dyi, dxo, dyo;
+
+
+        if ( !( outline->tags[n] & FT_Curve_Tag_On ) )
+          point->flags = PSH3_POINT_OFF;
+
+        dxi = vec[n].x - vec[n_prev].x;
+        dyi = vec[n].y - vec[n_prev].y;
+
+        point->dir_in = (FT_Char)psh3_compute_dir( dxi, dyi );
+
+        dxo = vec[n_next].x - vec[n].x;
+        dyo = vec[n_next].y - vec[n].y;
+
+        point->dir_out = (FT_Char)psh3_compute_dir( dxo, dyo );
+
+        /* detect smooth points */
+        if ( point->flags & PSH3_POINT_OFF )
+          point->flags |= PSH3_POINT_SMOOTH;
+        else if ( point->dir_in  != PSH3_DIR_NONE ||
+                  point->dir_out != PSH3_DIR_NONE )
+        {
+          if ( point->dir_in == point->dir_out )
+            point->flags |= PSH3_POINT_SMOOTH;
+        }
+        else
+        {
+          FT_Angle  angle_in, angle_out, diff;
+
+
+          angle_in  = FT_Atan2( dxi, dyi );
+          angle_out = FT_Atan2( dxo, dyo );
+
+          diff = angle_in - angle_out;
+          if ( diff < 0 )
+            diff = -diff;
+
+          if ( diff > FT_ANGLE_PI )
+            diff = FT_ANGLE_2PI - diff;
+
+          if ( diff < FT_ANGLE_PI / 16 )
+            point->flags |= PSH3_POINT_SMOOTH;
+        }
+      }
+    }
+
+    glyph->memory  = memory;
+    glyph->outline = outline;
+    glyph->globals = globals;
+
+    /* now deal with hints tables */
+    error = psh3_hint_table_init( &glyph->hint_tables [0],
+                                  &ps_hints->dimension[0].hints,
+                                  &ps_hints->dimension[0].masks,
+                                  &ps_hints->dimension[0].counters,
+                                  memory );
+    if ( error )
+      goto Exit;
+
+    error = psh3_hint_table_init( &glyph->hint_tables [1],
+                                  &ps_hints->dimension[1].hints,
+                                  &ps_hints->dimension[1].masks,
+                                  &ps_hints->dimension[1].counters,
+                                  memory );
+    if ( error )
+      goto Exit;
+
+  Exit:
+    return error;
+  }
+
+
+  /* load outline point coordinates into hinter glyph */
+  static void
+  psh3_glyph_load_points( PSH3_Glyph  glyph,
+                          FT_Int      dimension )
+  {
+    FT_Vector*  vec   = glyph->outline->points;
+    PSH3_Point  point = glyph->points;
+    FT_UInt     count = glyph->num_points;
+
+
+    for ( ; count > 0; count--, point++, vec++ )
+    {
+      point->flags &= PSH3_POINT_OFF | PSH3_POINT_SMOOTH;
+      point->hint   = 0;
+      if ( dimension == 0 )
+        point->org_u = vec->x;
+      else
+        point->org_u = vec->y;
+
+#ifdef DEBUG_HINTER
+      point->org_x = vec->x;
+      point->org_y = vec->y;
+#endif
+    }
+  }
+
+
+  /* save hinted point coordinates back to outline */
+  static void
+  psh3_glyph_save_points( PSH3_Glyph  glyph,
+                          FT_Int      dimension )
+  {
+    FT_UInt     n;
+    PSH3_Point  point = glyph->points;
+    FT_Vector*  vec   = glyph->outline->points;
+    char*       tags  = glyph->outline->tags;
+
+
+    for ( n = 0; n < glyph->num_points; n++ )
+    {
+      if ( dimension == 0 )
+        vec[n].x = point->cur_u;
+      else
+        vec[n].y = point->cur_u;
+
+      if ( psh3_point_is_strong( point ) )
+        tags[n] |= (char)( ( dimension == 0 ) ? 32 : 64 );
+
+#ifdef DEBUG_HINTER
+      if ( dimension == 0 )
+      {
+        point->cur_x   = point->cur_u;
+        point->flags_x = point->flags;
+      }
+      else
+      {
+        point->cur_y   = point->cur_u;
+        point->flags_y = point->flags;
+      }
+#endif
+      point++;
+    }
+  }
+
+
+#define PSH3_STRONG_THRESHOLD  10
+
+
+  static void
+  psh3_hint_table_find_strong_point( PSH3_Hint_Table  table,
+                                     PSH3_Point       point,
+                                     FT_Int           major_dir )
+  {
+    PSH3_Hint*   sort      = table->sort;
+    FT_UInt      num_hints = table->num_hints;
+
+
+    for ( ; num_hints > 0; num_hints--, sort++ )
+    {
+      PSH3_Hint  hint = sort[0];
+
+
+      if ( ABS( point->dir_in )  == major_dir ||
+           ABS( point->dir_out ) == major_dir )
+      {
+        FT_Pos  d;
+
+
+        d = point->org_u - hint->org_pos;
+        if ( ABS( d ) < PSH3_STRONG_THRESHOLD )
+        {
+        Is_Strong:
+          psh3_point_set_strong( point );
+          point->hint = hint;
+          break;
+        }
+
+        d -= hint->org_len;
+        if ( ABS( d ) < PSH3_STRONG_THRESHOLD )
+          goto Is_Strong;
+      }
+
+#if 1
+      if ( point->org_u >= hint->org_pos &&
+           point->org_u <= hint->org_pos + hint->org_len &&
+           psh3_point_is_extremum( point ) )
+      {
+        /* attach to hint, but don't mark as strong */
+        point->hint = hint;
+        break;
+      }
+#endif
+    }
+  }
+
+
+  /* find strong points in a glyph */
+  static void
+  psh3_glyph_find_strong_points( PSH3_Glyph  glyph,
+                                 FT_Int      dimension )
+  {
+    /* a point is strong if it is located on a stem                   */
+    /* edge and has an "in" or "out" tangent to the hint's direction  */
+    {
+      PSH3_Hint_Table  table     = &glyph->hint_tables[dimension];
+      PS_Mask          mask      = table->hint_masks->masks;
+      FT_UInt          num_masks = table->hint_masks->num_masks;
+      FT_UInt          first     = 0;
+      FT_Int           major_dir = dimension == 0 ? PSH3_DIR_UP : PSH3_DIR_RIGHT;
+
+
+      /* process secondary hints to "selected" points */
+      if ( num_masks > 1 )
+      {
+        first = mask->end_point;
+        mask++;
+        for ( ; num_masks > 1; num_masks--, mask++ )
+        {
+          FT_UInt  next;
+          FT_Int   count;
+
+
+          next  = mask->end_point;
+          count = next - first;
+          if ( count > 0 )
+          {
+            PSH3_Point  point = glyph->points + first;
+
+
+            psh3_hint_table_activate_mask( table, mask );
+
+            for ( ; count > 0; count--, point++ )
+              psh3_hint_table_find_strong_point( table, point, major_dir );
+          }
+          first = next;
+        }
+      }
+
+      /* process primary hints for all points */
+      if ( num_masks == 1 )
+      {
+        FT_UInt     count = glyph->num_points;
+        PSH3_Point  point = glyph->points;
+
+
+        psh3_hint_table_activate_mask( table, table->hint_masks->masks );
+        for ( ; count > 0; count--, point++ )
+        {
+          if ( !psh3_point_is_strong( point ) )
+            psh3_hint_table_find_strong_point( table, point, major_dir );
+        }
+      }
+
+      /* now, certain points may have been attached to hint and */
+      /* not marked as strong; update their flags then          */
+      {
+        FT_UInt     count = glyph->num_points;
+        PSH3_Point  point = glyph->points;
+
+
+        for ( ; count > 0; count--, point++ )
+          if ( point->hint && !psh3_point_is_strong( point ) )
+            psh3_point_set_strong( point );
+      }
+    }
+  }
+
+
+  /* interpolate strong points with the help of hinted coordinates */
+  static void
+  psh3_glyph_interpolate_strong_points( PSH3_Glyph  glyph,
+                                        FT_Int      dimension )
+  {
+    PSH_Dimension    dim   = &glyph->globals->dimension[dimension];
+    FT_Fixed         scale = dim->scale_mult;
+
+
+    {
+      FT_UInt     count = glyph->num_points;
+      PSH3_Point  point = glyph->points;
+
+
+      for ( ; count > 0; count--, point++ )
+      {
+        PSH3_Hint  hint = point->hint;
+
+
+        if ( hint )
+        {
+          FT_Pos  delta;
+
+
+          delta = point->org_u - hint->org_pos;
+
+          if ( delta <= 0 )
+            point->cur_u = hint->cur_pos + FT_MulFix( delta, scale );
+
+          else if ( delta >= hint->org_len )
+            point->cur_u = hint->cur_pos + hint->cur_len +
+                           FT_MulFix( delta - hint->org_len, scale );
+
+          else if ( hint->org_len > 0 )
+            point->cur_u = hint->cur_pos +
+                           FT_MulDiv( delta, hint->cur_len, hint->org_len );
+          else
+            point->cur_u = hint->cur_pos;
+
+          psh3_point_set_fitted( point );
+        }
+      }
+    }
+  }
+
+
+  static void
+  psh3_glyph_interpolate_normal_points( PSH3_Glyph  glyph,
+                                        FT_Int      dimension )
+  {
+#if 1
+    PSH_Dimension    dim   = &glyph->globals->dimension[dimension];
+    FT_Fixed         scale = dim->scale_mult;
+
+
+    /* first technique: a point is strong if it is a local extrema */
+    {
+      FT_UInt     count = glyph->num_points;
+      PSH3_Point  point = glyph->points;
+
+
+      for ( ; count > 0; count--, point++ )
+      {
+        if ( psh3_point_is_strong( point ) )
+          continue;
+
+        /* sometimes, some local extremas are smooth points */
+        if ( psh3_point_is_smooth( point ) )
+        {
+          if ( point->dir_in == PSH3_DIR_NONE  ||
+               point->dir_in != point->dir_out )
+            continue;
+
+          if ( !psh3_point_is_extremum( point ) )
+            continue;
+
+          point->flags &= ~PSH3_POINT_SMOOTH;
+        }
+
+        /* find best enclosing point coordinates */
+        {
+          PSH3_Point  before = 0;
+          PSH3_Point  after  = 0;
+
+          FT_Pos      diff_before = -32000;
+          FT_Pos      diff_after  =  32000;
+          FT_Pos      u = point->org_u;
+
+          FT_Int      count2 = glyph->num_points;
+          PSH3_Point  cur    = glyph->points;
+
+
+          for ( ; count2 > 0; count2--, cur++ )
+          {
+            if ( psh3_point_is_strong( cur ) )
+            {
+              FT_Pos   diff = cur->org_u - u;;
+
+
+              if ( diff <= 0 )
+              {
+                if ( diff > diff_before )
+                {
+                  diff_before = diff;
+                  before      = cur;
+                }
+              }
+              else if ( diff >= 0 )
+              {
+                if ( diff < diff_after )
+                {
+                  diff_after = diff;
+                  after      = cur;
+                }
+              }
+            }
+          }
+
+          if ( !before )
+          {
+            if ( !after )
+              continue;
+
+            /* we are before the first strong point coordinate; */
+            /* simply translate the point                       */
+            point->cur_u = after->cur_u +
+                           FT_MulFix( point->org_u - after->org_u, scale );
+          }
+          else if ( !after )
+          {
+            /* we are after the last strong point coordinate; */
+            /* simply translate the point                     */
+            point->cur_u = before->cur_u +
+                           FT_MulFix( point->org_u - before->org_u, scale );
+          }
+          else
+          {
+            if ( diff_before == 0 )
+              point->cur_u = before->cur_u;
+
+            else if ( diff_after == 0 )
+              point->cur_u = after->cur_u;
+
+            else
+              point->cur_u = before->cur_u +
+                             FT_MulDiv( u - before->org_u,
+                                        after->cur_u - before->cur_u,
+                                        after->org_u - before->org_u );
+          }
+
+          psh3_point_set_fitted( point );
+        }
+      }
+    }
+#endif
+  }
+
+
+  /* interpolate other points */
+  static void
+  psh3_glyph_interpolate_other_points( PSH3_Glyph  glyph,
+                                       FT_Int      dimension )
+  {
+    PSH_Dimension dim          = &glyph->globals->dimension[dimension];
+    FT_Fixed      scale        = dim->scale_mult;
+    FT_Fixed      delta        = dim->scale_delta;
+    PSH3_Contour  contour      = glyph->contours;
+    FT_UInt       num_contours = glyph->num_contours;
+
+
+    for ( ; num_contours > 0; num_contours--, contour++ )
+    {
+      PSH3_Point   start = contour->start;
+      PSH3_Point   first, next, point;
+      FT_UInt      fit_count;
+
+
+      /* count the number of strong points in this contour */
+      next      = start + contour->count;
+      fit_count = 0;
+      first     = 0;
+
+      for ( point = start; point < next; point++ )
+        if ( psh3_point_is_fitted( point ) )
+        {
+          if ( !first )
+            first = point;
+
+          fit_count++;
+        }
+
+      /* if there are less than 2 fitted points in the contour, we */
+      /* simply scale and eventually translate the contour points  */
+      if ( fit_count < 2 )
+      {
+        if ( fit_count == 1 )
+          delta = first->cur_u - FT_MulFix( first->org_u, scale );
+
+        for ( point = start; point < next; point++ )
+          if ( point != first )
+            point->cur_u = FT_MulFix( point->org_u, scale ) + delta;
+
+        goto Next_Contour;
+      }
+
+      /* there are more than 2 strong points in this contour; we */
+      /* need to interpolate weak points between them            */
+      start = first;
+      do
+      {
+        point = first;
+
+        /* skip consecutive fitted points */
+        for (;;)
+        {
+          next = first->next;
+          if ( next == start )
+            goto Next_Contour;
+
+          if ( !psh3_point_is_fitted( next ) )
+            break;
+
+          first = next;
+        }
+
+        /* find next fitted point after unfitted one */
+        for (;;)
+        {
+          next = next->next;
+          if ( psh3_point_is_fitted( next ) )
+            break;
+        }
+
+        /* now interpolate between them */
+        {
+          FT_Pos    org_a, org_ab, cur_a, cur_ab;
+          FT_Pos    org_c, org_ac, cur_c;
+          FT_Fixed  scale_ab;
+
+
+          if ( first->org_u <= next->org_u )
+          {
+            org_a  = first->org_u;
+            cur_a  = first->cur_u;
+            org_ab = next->org_u - org_a;
+            cur_ab = next->cur_u - cur_a;
+          }
+          else
+          {
+            org_a  = next->org_u;
+            cur_a  = next->cur_u;
+            org_ab = first->org_u - org_a;
+            cur_ab = first->cur_u - cur_a;
+          }
+
+          scale_ab = 0x10000L;
+          if ( org_ab > 0 )
+            scale_ab = FT_DivFix( cur_ab, org_ab );
+
+          point = first->next;
+          do
+          {
+            org_c  = point->org_u;
+            org_ac = org_c - org_a;
+
+            if ( org_ac <= 0 )
+            {
+              /* on the left of the interpolation zone */
+              cur_c = cur_a + FT_MulFix( org_ac, scale );
+            }
+            else if ( org_ac >= org_ab )
+            {
+              /* on the right on the interpolation zone */
+              cur_c = cur_a + cur_ab + FT_MulFix( org_ac - org_ab, scale );
+            }
+            else
+            {
+              /* within the interpolation zone */
+              cur_c = cur_a + FT_MulFix( org_ac, scale_ab );
+            }
+
+            point->cur_u = cur_c;
+
+            point = point->next;
+
+          } while ( point != next );
+        }
+
+        /* keep going until all points in the contours have been processed */
+        first = next;
+
+      } while ( first != start );
+
+    Next_Contour:
+      ;
+    }
+  }
+
+
+  /*************************************************************************/
+  /*************************************************************************/
+  /*****                                                               *****/
+  /*****                     HIGH-LEVEL INTERFACE                      *****/
+  /*****                                                               *****/
+  /*************************************************************************/
+  /*************************************************************************/
+
+  FT_Error
+  ps3_hints_apply( PS_Hints     ps_hints,
+                   FT_Outline*  outline,
+                   PSH_Globals  globals )
+  {
+    PSH3_GlyphRec  glyphrec;
+    PSH3_Glyph     glyph = &glyphrec;
+    FT_Error       error;
+#ifdef DEBUG_HINTER
+    FT_Memory      memory;
+#endif
+    FT_Int         dimension;
+
+
+#ifdef DEBUG_HINTER
+    memory = globals->memory;
+
+    if ( ps3_debug_glyph )
+    {
+      psh3_glyph_done( ps3_debug_glyph );
+      FT_FREE( ps3_debug_glyph );
+    }
+
+    if ( FT_NEW( glyph ) )
+      return error;
+
+    ps3_debug_glyph = glyph;
+#endif
+
+    error = psh3_glyph_init( glyph, outline, ps_hints, globals );
+    if ( error )
+      goto Exit;
+
+    for ( dimension = 0; dimension < 2; dimension++ )
+    {
+      /* load outline coordinates into glyph */
+      psh3_glyph_load_points( glyph, dimension );
+
+      /* compute aligned stem/hints positions */
+      psh3_hint_table_align_hints( &glyph->hint_tables[dimension],
+                                   glyph->globals,
+                                   dimension );
+
+      /* find strong points, align them, then interpolate others */
+      psh3_glyph_find_strong_points( glyph, dimension );
+      psh3_glyph_interpolate_strong_points( glyph, dimension );
+      psh3_glyph_interpolate_normal_points( glyph, dimension );
+      psh3_glyph_interpolate_other_points( glyph, dimension );
+
+      /* save hinted coordinates back to outline */
+      psh3_glyph_save_points( glyph, dimension );
+    }
+
+  Exit:
+#ifndef DEBUG_HINTER
+    psh3_glyph_done( glyph );
+#endif
+    return error;
+  }
+
+
+/* END */
diff --git a/src/pshinter/pshalgo3.h b/src/pshinter/pshalgo3.h
new file mode 100644
index 0000000..4d4abb7
--- /dev/null
+++ b/src/pshinter/pshalgo3.h
@@ -0,0 +1,202 @@
+/***************************************************************************/
+/*                                                                         */
+/*  pshalgo3.h                                                             */
+/*                                                                         */
+/*    PostScript hinting algorithm 3 (specification).                      */
+/*                                                                         */
+/*  Copyright 2001 by                                                      */
+/*  David Turner, Robert Wilhelm, and Werner Lemberg.                      */
+/*                                                                         */
+/*  This file is part of the FreeType project, and may only be used,       */
+/*  modified, and distributed under the terms of the FreeType project      */
+/*  license, LICENSE.TXT.  By continuing to use, modify, or distribute     */
+/*  this file you indicate that you have read the license and              */
+/*  understand and accept it fully.                                        */
+/*                                                                         */
+/***************************************************************************/
+
+
+#ifndef __PSHALGO3_H__
+#define __PSHALGO3_H__
+
+
+#include "pshrec.h"
+#include "pshglob.h"
+#include FT_TRIGONOMETRY_H
+
+
+FT_BEGIN_HEADER
+
+
+  typedef struct PSH3_HintRec_*  PSH3_Hint;
+
+  typedef enum
+  {
+    PSH3_HINT_GHOST  = PS_HINT_FLAG_GHOST,
+    PSH3_HINT_BOTTOM = PS_HINT_FLAG_BOTTOM,
+    PSH3_HINT_ACTIVE = 4,
+    PSH3_HINT_FITTED = 8
+
+  } PSH3_Hint_Flags;
+
+
+#define psh3_hint_is_active( x )  ( ( (x)->flags & PSH3_HINT_ACTIVE ) != 0 )
+#define psh3_hint_is_ghost( x )   ( ( (x)->flags & PSH3_HINT_GHOST  ) != 0 )
+#define psh3_hint_is_fitted( x )  ( ( (x)->flags & PSH3_HINT_FITTED ) != 0 )
+
+#define psh3_hint_activate( x )    (x)->flags |=  PSH3_HINT_ACTIVE
+#define psh3_hint_deactivate( x )  (x)->flags &= ~PSH3_HINT_ACTIVE
+#define psh3_hint_set_fitted( x )  (x)->flags |=  PSH3_HINT_FITTED
+
+
+  typedef struct  PSH3_HintRec_
+  {
+    FT_Int     org_pos;
+    FT_Int     org_len;
+    FT_Pos     cur_pos;
+    FT_Pos     cur_len;
+    FT_UInt    flags;
+    PSH3_Hint  parent;
+    FT_Int     order;
+
+  } PSH3_HintRec;
+
+
+  /* this is an interpolation zone used for strong points;  */
+  /* weak points are interpolated according to their strong */
+  /* neighbours                                             */
+  typedef struct  PSH3_ZoneRec_
+  {
+    FT_Fixed  scale;
+    FT_Fixed  delta;
+    FT_Pos    min;
+    FT_Pos    max;
+
+  } PSH3_ZoneRec, *PSH3_Zone;
+
+
+  typedef struct  PSH3_Hint_TableRec_
+  {
+    FT_UInt        max_hints;
+    FT_UInt        num_hints;
+    PSH3_Hint      hints;
+    PSH3_Hint*     sort;
+    PSH3_Hint*     sort_global;
+    FT_UInt        num_zones;
+    PSH3_Zone      zones;
+    PSH3_Zone      zone;
+    PS_Mask_Table  hint_masks;
+    PS_Mask_Table  counter_masks;
+
+  } PSH3_Hint_TableRec, *PSH3_Hint_Table;
+
+
+  typedef struct PSH3_PointRec_*    PSH3_Point;
+  typedef struct PSH3_ContourRec_*  PSH3_Contour;
+
+  enum
+  {
+    PSH3_DIR_NONE  =  4,
+    PSH3_DIR_UP    =  1,
+    PSH3_DIR_DOWN  = -1,
+    PSH3_DIR_LEFT  = -2,
+    PSH3_DIR_RIGHT =  2
+  };
+
+  enum
+  {
+    PSH3_POINT_OFF    = 1,   /* point is off the curve  */
+    PSH3_POINT_STRONG = 2,   /* point is strong         */
+    PSH3_POINT_SMOOTH = 4,   /* point is smooth         */
+    PSH3_POINT_FITTED = 8    /* point is already fitted */
+  };
+
+
+  typedef struct  PSH3_PointRec_
+  {
+    PSH3_Point    prev;
+    PSH3_Point    next;
+    PSH3_Contour  contour;
+    FT_UInt       flags;
+    FT_Char       dir_in;
+    FT_Char       dir_out;
+    FT_Angle      angle_in;
+    FT_Angle      angle_out;
+    PSH3_Hint     hint;
+    FT_Pos        org_u;
+    FT_Pos        cur_u;
+#ifdef DEBUG_HINTER
+    FT_Pos        org_x;
+    FT_Pos        cur_x;
+    FT_Pos        org_y;
+    FT_Pos        cur_y;
+    FT_UInt       flags_x;
+    FT_UInt       flags_y;
+#endif
+
+  } PSH3_PointRec;
+
+
+#define psh3_point_is_strong( p )   ( (p)->flags & PSH3_POINT_STRONG )
+#define psh3_point_is_fitted( p )   ( (p)->flags & PSH3_POINT_FITTED )
+#define psh3_point_is_smooth( p )   ( (p)->flags & PSH3_POINT_SMOOTH )
+
+#define psh3_point_set_strong( p )  (p)->flags |= PSH3_POINT_STRONG
+#define psh3_point_set_fitted( p )  (p)->flags |= PSH3_POINT_FITTED
+#define psh3_point_set_smooth( p )  (p)->flags |= PSH3_POINT_SMOOTH
+
+
+  typedef struct  PSH3_ContourRec_
+  {
+    PSH3_Point  start;
+    FT_UInt     count;
+
+  } PSH3_ContourRec;
+
+
+  typedef struct  PSH3_GlyphRec_
+  {
+    FT_UInt             num_points;
+    FT_UInt             num_contours;
+
+    PSH3_Point          points;
+    PSH3_Contour        contours;
+
+    FT_Memory           memory;
+    FT_Outline*         outline;
+    PSH_Globals         globals;
+    PSH3_Hint_TableRec  hint_tables[2];
+
+    FT_Bool             vertical;
+    FT_Int              major_dir;
+    FT_Int              minor_dir;
+
+  } PSH3_GlyphRec, *PSH3_Glyph;
+
+
+#ifdef DEBUG_HINTER
+  extern PSH3_Hint_Table  ps3_debug_hint_table;
+
+  typedef void
+  (*PSH3_HintFunc)( PSH3_Hint  hint,
+                    FT_Bool    vertical );
+
+  extern PSH3_HintFunc    ps3_debug_hint_func;
+
+  extern PSH3_Glyph       ps3_debug_glyph;
+#endif
+
+
+  extern FT_Error
+  ps3_hints_apply( PS_Hints     ps_hints,
+                   FT_Outline*  outline,
+                   PSH_Globals  globals );
+
+
+FT_END_HEADER
+
+
+#endif /* __PSHALGO3_H__ */
+
+
+/* END */
diff --git a/src/pshinter/pshinter.c b/src/pshinter/pshinter.c
index 6b99182..180f6c8 100644
--- a/src/pshinter/pshinter.c
+++ b/src/pshinter/pshinter.c
@@ -23,6 +23,7 @@
 #include "pshglob.c"
 #include "pshalgo1.c"
 #include "pshalgo2.c"
+#include "pshalgo3.c"
 #include "pshmod.c"
 
 
diff --git a/src/pshinter/rules.mk b/src/pshinter/rules.mk
index 409a790..207d349 100644
--- a/src/pshinter/rules.mk
+++ b/src/pshinter/rules.mk
@@ -30,7 +30,8 @@ PSHINTER_DRV_SRC := $(PSHINTER_DIR_)pshrec.c   \
                     $(PSHINTER_DIR_)pshglob.c  \
                     $(PSHINTER_DIR_)pshmod.c   \
                     $(PSHINTER_DIR_)pshalgo1.c \
-                    $(PSHINTER_DIR_)pshalgo2.c
+                    $(PSHINTER_DIR_)pshalgo2.c \
+                    $(PSHINTER_DIR_)pshalgo3.c
 
 
 # PSHINTER driver headers
diff --git a/tests/gview.c b/tests/gview.c
index 8e1b242..dcdf506 100644
--- a/tests/gview.c
+++ b/tests/gview.c
@@ -130,15 +130,17 @@ init_symbols( void )
   nv_path_new_rectangle( renderer, -1, -1, 3, 3, 0, 0, &symbol_square );
   nv_path_new_rectangle( renderer, -1, -6, 2, 12, 0, 0, &symbol_rect_v );
   nv_path_new_rectangle( renderer, -6, -1, 12, 2, 0, 0, &symbol_rect_h );
+
   nv_path_new_circle( renderer, 0, 0, 3., &symbol_dot );
+
   nv_path_stroke( symbol_dot, 0.6,
                   nv_path_linecap_butt,
                   nv_path_linejoin_miter, 1.,
                   &symbol_circle );
 
   nv_path_destroy( symbol_dot );
-  nv_path_new_circle( renderer, 0, 0, 2., &symbol_dot );
 
+  nv_path_new_circle( renderer, 0, 0, 2., &symbol_dot );
  }
 
 static void