This file is indexed.

/usr/lib/python3/dist-packages/psautohint/ufoFont.py is in python3-psautohint 1.1.0-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

   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
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
from __future__ import print_function, absolute_import

"""
ufoFont.py v1.30 May 2 2017

This module supports using the Adobe FDK tools which operate on 'bez'
files with UFO fonts. It provides low level utilities to manipulate UFO
data without fully parsing and instantiating UFO objects, and without
requiring that the AFDKO contain the robofab libraries.

Modified in Nov 2014, when AFDKO added the robofab libraries. It can now
be used with UFO fonts only to support the hash mechanism.

Developed in order to support checkOutlines and autohint, the code
supports two main functions:
- convert between UFO GLIF and bez formats
- keep a history of processing in a hash map, so that the (lengthy)
processing by autohint and checkOutlines can be avoided if the glyph has
already been processed, and the source data has not changed.

The basic model is:
 - read GLIF file
 - transform GLIF XML element to bez file
 - call FDK tool on bez file
 - transform new bez file to GLIF XML element with new data, and save in list

After all glyphs are done save all the new GLIF XML elements to GLIF
files, and update the hash map.

A complication in the Adobe UFO workflow comes from the fact we want to
make sure that checkOutlines and autohint have been run on each glyph in
a UFO font, when building an OTF font from the UFO font. We need to run
checkOutlines, because we no longer remove overlaps from source UFO font
data, because this can make revising a glyph much easier. We need to run
autohint, because the glyphs must be hinted after checkOutlines is run,
and in any case we want all glyphs to have been autohinted. The problem
with this is that it can take a minute or two to run autohint or
checkOutlines on a 2K-glyph font. The way we avoid this is to make and
keep a hash of the source glyph drawing operators for each tool. When
the tool is run, it calculates a hash of the source glyph, and compares
it to the saved hash. If these are the same, the tool can skip the
glyph. This saves a lot of time: if checkOutlines and autohint are run
on all glyphs in a font, then a second pass is under 2 seconds.

Another issue is that since we no longer remove overlaps from the source
glyph files, checkOutlines must write any edited glyph data to a
different layer in order to not destroy the source data. The ufoFont
defines an Adobe-specific glyph layer for processed glyphs, named
"glyphs.com.adobe.type.processedGlyphs".
checkOutlines writes new glyph files to the processed glyphs layer only
when it makes a change to the glyph data.

When the autohint program is run, the ufoFont must be able to tell
whether checkOutlines has been run and has altered a glyph: if so, the
input file needs to be from the processed glyph layer, else it needs to
be from the default glyph layer.

The way the hashmap works is that we keep an entry for every glyph that
has been processed, identified by a hash of its marking path data. Each
entry contains:
- a hash of the glyph point coordinates, from the default layer.
This is set after a program has been run.
- a history list: a list of the names of each program that has been run, in order.
- an editStatus flag.
Altered GLIF data is always written to the Adobe processed glyph layer. The
program may or may not have altered the outline data. For example, autohint adds
private hint data, and adds names to points, but does not change any paths.

If the stored hash for the glyph does not exist, the ufoFont lib will save the
new hash in the hash map entry and will set the history list to contain just the
current program. The program will read the glyph from the default layer.

If the stored hash matches the hash for the current glyph file in the default
layer, and the current program name is in the history list,then ufoFont
will return "skip=1", and the calling program may skip the glyph.

If the stored hash matches the hash for the current glyph file in the default
layer, and the current program name is not in the history list, then the
ufoFont will return "skip=0". If the font object field 'usedProcessedLayer' is
set True, the program will read the glyph from the from the Adobe processed
layer, if it exists, else it will always read from the default layer.

If the hash differs between the hash map entry and the current glyph in the
default layer, and usedProcessedLayer is False, then ufoFont will return
"skip=0". If usedProcessedLayer is True, then the program will consult the list
of required programs. If any of these are in the history list, then the program
will report an error and return skip =1, else it will return skip=1. The program
will then save the new hash in the hash map entry and reset the history list to
contain just the current program. If the old and new hash match, but the program
name is not in the history list, then the ufoFont will not skip the glyph, and
will add the program name to the history list.


The only tools using this are, at the moment, checkOutlines, checkOutlinesUFO
and autohint. checkOutlines and checkOutlinesUFO use the hash map to skip
processing only when being used to edit glyphs, not when reporting them.
checkOutlines necessarily flattens any components in the source glyph file to
actual outlines. autohint adds point names, and saves the hint data as a private
data in the new GLIF file.

autohint saves the hint data in the GLIF private data area, /lib/dict,
as a key and data pair. See below for the format.

autohint started with _hintFormat1_, a reasonably compact XML representation of
the data. In Sep 2105, autohhint switched to _hintFormat2_ in order to be plist
compatible. This was necessary in order to be compatible with the UFO spec, by
was driven more immediately by the fact the the UFO font file normalization
tools stripped out the _hintFormat1_ hint data as invalid elements.


"""

__copyright__ = """Copyright 2014 Adobe Systems Incorporated (http://www.adobe.com/). All Rights Reserved.
"""

_hintFormat1_ = """

Deprecated. See _hintFormat2_ below.

A <hintset> element identifies a specific point by its name, and
describes a new set of stem hints which should be applied before the
specific point.

A <flex> element identifies a specific point by its name. The point is
the first point of a curve. The presence of the <flex> element is a
processing suggestion, that the curve and its successor curve should
be converted to a flex operator.

One challenge in applying the hintset and flex elements is that in the
GLIF format, there is no explicit start and end operator: the first path
operator is both the end and the start of the path. I have chosen to
convert this to T1 by taking the first path operator, and making it a
move-to. I then also use it as the last path operator. An exception is a
line-to; in T1, this is omitted, as it is implied by the need to close
the path. Hence, if a hintset references the first operator, there is a
potential ambiguity: should it be applied before the T1 move-to, or
before the final T1 path operator? The logic here applies it before the
move-to only.

 <glyph>
...
	<lib>
		<dict>
			<key><com.adobe.type.autohint><key>
			<data>
				<hintSetList>
					<hintset pointTag="point name">
						(<hstem pos="<decimal value>" width="decimal value" />)*
						(<vstem pos="<decimal value>" width="decimal value" />)*
						<hstem3 stem3List="n0,n1,n2,n3,n4,n5" />* # where n1-5 are decimal values
						<vstem3 stem3List="n0,n1,n2,n3,n4,n5" />* # where n1-5 are decimal values
					</hintset>*
					(<hintSetList>*
						(<hintset pointIndex="positive integer">
							(<stemindex>positive integer</stemindex>)+
						</hintset>)+
					</hintSetList>)*
					<flexList>
						<flex pointTag="point Name" />
					</flexList>*
				</hintSetList>
			</data>
		</dict>
	</lib>
</glyph>

Example from "B" in SourceCodePro-Regular
<key><com.adobe.type.autohint><key>
<data>
<hintSetList id="64bf4987f05ced2a50195f971cd924984047eb1d79c8c43e6a0054f59cc85dea23a49deb20946a4ea84840534363f7a13cca31a81b1e7e33c832185173369086">
	<hintset pointTag="hintSet0000">
		<hstem pos="0" width="28" />
		<hstem pos="338" width="28" />
		<hstem pos="632" width="28" />
		<vstem pos="100" width="32" />
		<vstem pos="496" width="32" />
	</hintset>
	<hintset pointTag="hintSet0005">
		<hstem pos="0" width="28" />
		<hstem pos="338" width="28" />
		<hstem pos="632" width="28" />
		<vstem pos="100" width="32" />
		<vstem pos="454" width="32" />
		<vstem pos="496" width="32" />
	</hintset>
	<hintset pointTag="hintSet0016">
		<hstem pos="0" width="28" />
		<hstem pos="338" width="28" />
		<hstem pos="632" width="28" />
		<vstem pos="100" width="32" />
		<vstem pos="496" width="32" />
	</hintset>
</hintSetList>
</data>

"""

_hintFormat2_ = """

A <dict> element in the hintSetList array identifies a specific point by its
name, and describes a new set of stem hints which should be applied before the
specific point.

A <string> element in the flexList identifies a specific point by its name. The
point is the first point of a curve. The presence of the element is a processing
suggestion, that the curve and its successor curve should be converted to a flex
operator.

One challenge in applying the hintSetList and flexList elements is that in the
GLIF format, there is no explicit start and end operator: the first path
operator is both the end and the start of the path. I have chosen to convert
this to T1 by taking the first path operator, and making it a move-to. I then
also use it as the last path operator. An exception is a line-to; in T1, this is
omitted, as it is implied by the need to close the path. Hence, if a hintset
references the first operator, there is a potential ambiguity: should it be
applied before the T1 move-to, or before the final T1 path operator? The logic
here applies it before the move-to only.
 <glyph>
...
	<lib>
		<dict>
			<key><com.adobe.type.autohint></key>
			<dict>
				<key>id</key>
				<string> <fingerprint for glyph> </string>
				<key>hintSetList</key>
				<array>
					<dict>
						<key>pointTag</key>
						<string> <point name> </string>
						<key>stems</key>
						<array>
							<string>hstem <position value> <width value></string>*
							<string>vstem <position value> <width value></string>*
							<string>hstem3 <position value 0>...<position value 5></string>*
							<string>vstem3 <position value 0>...<position value 5></string>*
						</array>
					</dict>*
				</array>

				<key>flexList</key>*
				<array>
					<string><point name></string>+
				</array>
			</dict>
		</dict>
	</lib>
</glyph>

Example from "B" in SourceCodePro-Regular
<key><com.adobe.type.autohint><key>
<dict>
	<key>id</key>
	<string>64bf4987f05ced2a50195f971cd924984047eb1d79c8c43e6a0054f59cc85dea23a49deb20946a4ea84840534363f7a13cca31a81b1e7e33c832185173369086</string>
	<key>hintSetList</key>
	<array>
		<dict>
			<key>pointTag</key>
			<string>hintSet0000</string>
			<key>stems</key>
			<array>
				<string>hstem 338 28</string>
				<string>hstem 632 28</string>
				<string>hstem 100 32</string>
				<string>hstem 496 32</string>
			</array>
		</dict>
		<dict>
			<key>pointTag</key>
			<string>hintSet0005</string>
			<key>stems</key>
			<array>
				<string>hstem 0 28</string>
				<string>hstem 338 28</string>
				<string>hstem 632 28</string>
				<string>hstem 100 32</string>
				<string>hstem 454 32</string>
				<string>hstem 496 32</string>
			</array>
		</dict>
		<dict>
			<key>pointTag</key>
			<string>hintSet0016</string>
			<key>stems</key>
			<array>
				<string>hstem 0 28</string>
				<string>hstem 338 28</string>
				<string>hstem 632 28</string>
				<string>hstem 100 32</string>
				<string>hstem 496 32</string>
			</array>
		</dict>
	</array>
<dict>

"""

import re
import os
import plistlib
import hashlib

try:
	import xml.etree.cElementTree as ET
except ImportError:
	import xml.etree.ElementTree as ET

from psautohint import fdTools

XML = ET.XML
XMLElement = ET.Element
xmlToString = ET.tostring
debug = False

def debugMsg(*args):
	if debug:
		print(args)

#UFO names
kDefaultGlyphsLayerName = "public.default"
kDefaultGlyphsLayer = "glyphs"
kProcessedGlyphsLayerName = "AFDKO ProcessedGlyphs"
kProcessedGlyphsLayer = "glyphs.com.adobe.type.processedGlyphs"
kFontInfoName = "fontinfo.plist"
kContentsName = "contents.plist"
kLibName = "lib.plist"
kPublicGlyphOrderKey = "public.glyphOrder"

kAdobeDomainPrefix = "com.adobe.type"
kAdobHashMapName = "%s.processedHashMap" % (kAdobeDomainPrefix)
kAdobHashMapVersionName = "hashMapVersion"
kAdobHashMapVersion = (1,0) # If major version differs, do not use.
kAutohintName = "autohint"
kCheckOutlineName = "checkOutlines"
kCheckOutlineNameUFO = "checkOutlines"

kOutlinePattern = re.compile(r"<outline.+?outline>", re.DOTALL)

kStemHintsName = "stemhints"
kStemListName = "stemList"
kStemPosName = "pos"
kStemWidthName = "width"
kHStemName = "hstem"
kVStemName = "vstem"
kHStem3Name = "hstem3"
kVStem3Name = "vstem3"
kStem3Pos = "stem3List"
kHintSetListName = "hintSetList"
kFlexListName = "hintSetList"
kHintSetName = "hintset"
kBaseFlexName = "flexCurve"
kPointTag = "pointTag"
kStemIndexName = "stemindex"
kFlexIndexListName = "flexList"
kHintDomainName1 = "com.adobe.type.autohint"
kHintDomainName2 = "com.adobe.type.autohint.v2"
kPointName = "name"
# Hint stuff
kStackLimit = 46
kStemLimit = 96

kDefaultFMNDBPath = "FontMenuNameDB"
kDefaultGOADBPath = "GlyphOrderAndAliasDB"


class UFOParseError(ValueError):
	pass

class BezParseError(ValueError):
	pass

class UFOFontData:
	def __init__(self, parentPath, useHashMap, programName):
		self.parentPath = parentPath
		self.glyphMap = {}
		self.processedLayerGlyphMap = {}
		self.newGlyphMap = {}
		self.glyphList = []
		self.fontInfo = None
		self.useHashMap = useHashMap # if False, will skip reading has map and testing to see if glyph can be skipped.
		# should be used only when calling program is running in report mdoe only, and not changing any glyph data.
		self.hashMap = {} # Used to skip getting glyph data when glyph hash matches hash of current glyph data.
		self.fontDict = None
		self.programName = programName
		self.curSrcDir = None
		self.hashMapChanged = False
		self.glyphDefaultDir = os.path.join(parentPath, "glyphs")
		self.glyphLayerDir = os.path.join(parentPath, kProcessedGlyphsLayer)
		self.glyphWriteDir = self.glyphLayerDir
		self.historyList = []
		self.requiredHistory = [] # See documentation above.
		self.useProcessedLayer = False # If False, then read data only from the default layer; else read glyphs from processed layer, if it exists.
		self.writeToDefaultLayer = False # If True, then write data to the default layer
		self.doAll = False # if True, then do not skip any glyphs.
		self.deletedGlyph = False # track whether checkSkipGLyph has deleted a out of date glyph from the processed glyph layer
		self.allowDecimalCoords = False # if true, do NOT round x,y values when processing.

	def getUnitsPerEm(self):
		unitsPerEm = "1000"
		if self.fontInfo == None:
			self.loadFontInfo()
		if self.fontInfo:
			unitsPerEm = int(self.fontInfo["unitsPerEm"])
		unitsPerEm = int(unitsPerEm)
		return unitsPerEm

	def getPSName(self):
		psName = "PSName-Undefined"
		if self.fontInfo == None:
			self.loadFontInfo()
		if self.fontInfo:
			psName = self.fontInfo.get("postscriptFontName", psName)
		return psName

	def isCID(self):
		return 0

	def checkForHints(self, glyphName):
		hasHints = False
		glyphPath = self.getGlyphProcessedPath(glyphName)
		if glyphPath and os.path.exists(glyphPath):
			with open(glyphPath, "rt") as fp:
				data = fp.read()
			if "hintSetList" in data:
				hasHints = True
		return hasHints

	def convertToBez(self, glyphName, beVerbose, doAll=False):
		# convertGLIFToBez does not yet support hints - no need for removeHints arg.
		bezString, width = convertGLIFToBez(self, glyphName, beVerbose, doAll)
		hasHints = self.checkForHints(glyphName)
		return bezString, width, hasHints

	def updateFromBez(self, bezData, glyphName, width, beVerbose):
		# For UFO font, we don't use the width parameter: it is carried over from the input glif file.
		glifXML = convertBezToGLIF(self, glyphName, bezData )
		self.newGlyphMap[glyphName] = glifXML

	def saveChanges(self):
		if self.hashMapChanged:
			self.writeHashMap()
		self.hashMapChanged = False

		if not os.path.exists(self.glyphWriteDir):
			os.makedirs(self.glyphWriteDir)

		for glyphName, glifXML in self.newGlyphMap.items():
			glyphPath = self.getWriteGlyphPath(glyphName)
			#print("Saving file", glyphPath)
			et = ET.ElementTree(glifXML)
			with open(glyphPath, "wb") as fp:
				et.write(fp, encoding="UTF-8", xml_declaration=True)

		# Update the layer contents.plist file
		layerContentsFilePath = os.path.join(self.parentPath, "layercontents.plist")
		self.updateLayerContents(layerContentsFilePath)
		glyphContentsFilePath = os.path.join(self.glyphWriteDir, "contents.plist")
		self.updateLayerGlyphContents(glyphContentsFilePath, self.newGlyphMap)

	def getWriteGlyphPath(self, glyphName):
		if len(self.glyphMap) == 0:
			self.loadGlyphMap()

		glyphFileName = self.glyphMap[glyphName]
		if not self.writeToDefaultLayer:
			if self.processedLayerGlyphMap:
				try:
					glyphFileName = self.processedLayerGlyphMap[glyphName]
				except KeyError:
					pass

		glifFilePath = os.path.join(self.glyphWriteDir, glyphFileName)
		return glifFilePath

	def getGlyphMap(self):
		if len(self.glyphMap) == 0:
			self.loadGlyphMap()
		return self.glyphMap

	def readHashMap(self):
		hashPath = os.path.join(self.parentPath, "data", kAdobHashMapName)
		if os.path.exists(hashPath):
			with open(hashPath, "rt") as fp:
				data = fp.read()
			newMap = eval(data)
		else:
			newMap = {kAdobHashMapVersionName:kAdobHashMapVersion}

		try:
			version = newMap[kAdobHashMapVersionName]
			if version[0] > kAdobHashMapVersion[0]:
				raise UFOParseError("Hash map version is newer than program. Please update the FDK")
			elif version[0] < kAdobHashMapVersion[0]:
				print("Updating hash map: was older version")
				newMap = {kAdobHashMapVersionName:kAdobHashMapVersion}
		except KeyError:
			print("Updating hash map: was older version")
			newMap = {kAdobHashMapVersionName:kAdobHashMapVersion}
		self.hashMap = newMap
		return

	def writeHashMap(self):
		hashMap = self.hashMap
		if len(hashMap) == 0:
			return # no glyphs were processed.

		hashDir = os.path.join(self.parentPath, "data")
		if not os.path.exists(hashDir):
			os.makedirs(hashDir)
		hashPath = os.path.join(hashDir, kAdobHashMapName)

		hasMapKeys = hashMap.keys()
		hasMapKeys = sorted(hasMapKeys)
		data = ["{"]
		for gName in hasMapKeys:
			data.append("'%s': %s," % (gName, hashMap[gName]))
		data.append("}")
		data.append("")
		data = "\n".join(data)
		with open(hashPath, "wt") as fp:
			fp.write(data)
		return

	def getCurGlyphPath(self, glyphName):
		if self.curSrcDir == None:
			self.curSrcDir = self.glyphDefaultDir

		# Get the glyph file name.
		if len(self.glyphMap) == 0:
			self.loadGlyphMap()
		glyphFileName = self.glyphMap[glyphName]
		path = os.path.join(self.curSrcDir, glyphFileName)
		return path

	def getGlyphSrcPath(self, glyphName):
		if len(self.glyphMap) == 0:
			self.loadGlyphMap()
		glyphFileName = self.glyphMap[glyphName]

		if self.useProcessedLayer and self.processedLayerGlyphMap: # Try for processed layer first.
			try:
				glyphFileName = self.processedLayerGlyphMap[glyphName]
				self.curSrcDir = self.glyphLayerDir
				glyphPath = os.path.join(self.glyphLayerDir, glyphFileName)
				if os.path.exists(glyphPath):
					return glyphPath
			except KeyError:
				pass

		self.curSrcDir = self.glyphDefaultDir
		glyphPath = os.path.join(self.curSrcDir, glyphFileName)

		return glyphPath

	def getGlyphDefaultPath(self, glyphName):
		if len(self.glyphMap) == 0:
			self.loadGlyphMap()
		glyphFileName = self.glyphMap[glyphName]
		glyphPath = os.path.join(self.glyphDefaultDir, glyphFileName)
		return glyphPath

	def getGlyphProcessedPath(self, glyphName):
		if len(self.glyphMap) == 0:
			self.loadGlyphMap()
		if not self.processedLayerGlyphMap:
			return None

		try:
			glyphFileName = self.processedLayerGlyphMap[glyphName]
			glyphPath = os.path.join(self.glyphLayerDir, glyphFileName)
		except KeyError:
			glyphPath = None
		return glyphPath

	def updateHashEntry(self, glyphName, changed):
		# srcHarsh has already been set: we are fixing the history list.
		if not self.useHashMap:
			return
		# Get hash entry for glyph
		try:
			hashEntry = self.hashMap[glyphName]
			srcHash, historyList = hashEntry
		except KeyError:
			hashEntry = None

		self.hashMapChanged = True
		# If the program always reads data from the default layer, and we have just created a new glyph in the processed layer, then reset the history.
		if (not self.useProcessedLayer) and changed:
			self.hashMap[glyphName] = [srcHash, [self.programName] ]
			return
		else:
			try:
				programHistoryIndex = historyList.index(self.programName)
			except ValueError:
				#If the program is not in the history list, add it.
				historyList.append(self.programName)


	def checkSkipGlyph(self, glyphName, newSrcHash, doAll):
		skip = False
		if not self.useHashMap:
			return skip

		if len(self.hashMap) == 0:
			# Hash maps have not yet been read in. Get them.
			self.readHashMap()

		hashEntry = srcHash = None
		historyList = []
		programHistoryIndex = -1 # not found in historyList

		# Get hash entry for glyph
		try:
			hashEntry = self.hashMap[glyphName]
			srcHash, historyList = hashEntry
			try:
				programHistoryIndex = historyList.index(self.programName)
			except ValueError:
				pass
		except KeyError:
			# Glyph is as yet untouched by any program.
			pass

		if (srcHash == newSrcHash):
			if (programHistoryIndex >= 0):
				# The glyph has already been processed by this program, and there have been no changes since.
				skip = True and (not doAll)
			if not skip:
				if not self.useProcessedLayer: # case for Checkoutlines
					self.hashMapChanged = True
					self.hashMap[glyphName] = [newSrcHash, [self.programName] ]
					glyphPath = self.getGlyphProcessedPath(glyphName)
					if glyphPath and os.path.exists(glyphPath):
						os.remove(glyphPath)
				else:
					if (programHistoryIndex < 0):
						historyList.append(self.programName)
		else:
			if self.useProcessedLayer: # case for autohint
				# default layer glyph and stored glyph hash differ,and useProcessedLayer is True
				# If any of the programs in requiredHistory in are in the historyList, we need to complain and skip.
				foundMatch = False
				if len(historyList) > 0:
					for programName in self.requiredHistory:
						if programName in historyList:
							foundMatch = True
				if foundMatch:
					print("Error. Glyph '%s' has been edited. You must first run '%s' before running '%s'. Skipping." % (glyphName, self.requiredHistory, self.programName))
					skip = True

			# If the source hash has changed, we need to delete the processed layer glyph.
			self.hashMapChanged = True
			self.hashMap[glyphName] = [newSrcHash, [self.programName] ]
			glyphPath = self.getGlyphProcessedPath(glyphName)
			if glyphPath and os.path.exists(glyphPath):
				os.remove(glyphPath)
				self.deletedGlyph = True

		return skip

	def getGlyphXML(self, glyphDir, glyphFileName):
		glyphPath = os.path.join(glyphDir, glyphFileName) # default
		etRoot = ET.ElementTree()
		glifXML = etRoot.parse(glyphPath)
		outlineXML = glifXML.find("outline")
		try:
			widthXML = glifXML.find("advance")
			if widthXML != None:
				width = int(eval(widthXML.get("width")))
			else:
				width = 1000
		except UFOParseError as e:
			print("Error. skipping glyph '%s' because of parse error: %s" % (glyphName, e.message))
			return None, None, None
		return width, glifXML, outlineXML

	def getOrSkipGlyphXML(self, glyphName, doAll=False):
		# Get default glyph layer data, so we can check if the glyph has been edited since this program was last run.
		# If the program name is in the history list, and the srcHash matches the default glyph layer data, we can skip.
		if len(self.glyphMap) == 0:
			self.loadGlyphMap()
		glyphFileName = self.glyphMap[glyphName]
		width, glifXML, outlineXML = self.getGlyphXML(self.glyphDefaultDir, glyphFileName)
		if glifXML == None:
			skip = True
			return None, None, skip

		useDefaultGlyphDir = True # Hash is always from the default glyph layer.
		newHash, dataList = self.buildGlyphHashValue(width, outlineXML, glyphName, useDefaultGlyphDir)
		skip = self.checkSkipGlyph(glyphName, newHash, doAll)

		# If self.useProcessedLayer and there is a glyph in the processed layer, get the outline from that.
		if self.useProcessedLayer and self.processedLayerGlyphMap:
			try:
				glyphFileName = self.processedLayerGlyphMap[glyphName]
			except KeyError:
				pass
			glyphPath = os.path.join(self.glyphLayerDir, glyphFileName)
			if os.path.exists(glyphPath):
				width, glifXML, outlineXML = self.getGlyphXML(self.glyphLayerDir, glyphFileName)
				if glifXML == None:
					skip = True
					return None, None, skip

		return width, outlineXML, skip


	def getGlyphList(self):
		if len(self.glyphMap) == 0:
			self.loadGlyphMap()
		return self.glyphList

	def loadGlyphMap(self):
		# Need to both get the list of glyphs from contents.plist, and also the glyph order.
		# the latter is take from the public.glyphOrder key in lib.py, if it exists,
		# else it is taken from the contents.plist file. Any glyphs in contents.plist
		# which are not named in the public.glyphOrder are sorted after all glyphs which are named
		# in the public.glyphOrder,, in the order that they occured in contents.plist.
		contentsPath = os.path.join(self.parentPath, "glyphs", kContentsName)
		self.glyphMap, self.glyphList = parsePList(contentsPath)
		orderPath = os.path.join(self.parentPath, kLibName)
		self.orderMap = parseGlyphOrder(orderPath)
		if self.orderMap != None:
			orderIndex = len(self.orderMap)
			orderList = []

			# If there are glyphs in the font which are not named in the public.glyphOrder entry, add then in the order of the contents.plist file.
			for glyphName in self.glyphList:
				try:
					entry = [self.orderMap[glyphName], glyphName]
				except KeyError:
					entry = [orderIndex, glyphName]
					self.orderMap[glyphName] = orderIndex
					orderIndex += 1
				orderList.append(entry)
			orderList.sort()
			self.glyphList = []
			for entry in orderList:
				self.glyphList.append(entry[1])
		else:
			self.orderMap = {}
			numGlyphs = len(self.glyphList)
			for i in range(numGlyphs):
				self.orderMap[self.glyphList[i]] = i

		# I also need to get the glyph map for the processed layer,
		# and use this when the glyph is read from the processed layer.
		# Because checkOutliensUFO used the defcon library, it can write glyph file names
		# that differ from what is in the default glyph layer.
		contentsPath = os.path.join(self.glyphLayerDir, kContentsName)
		if os.path.exists(contentsPath):
			self.processedLayerGlyphMap, self.processedLayerGlyphList = parsePList(contentsPath)



	def loadFontInfo(self):
		fontInfoPath = os.path.join(self.parentPath, "fontinfo.plist")
		if not os.path.exists(fontInfoPath):
			return
		self.fontInfo, tempList = parsePList(fontInfoPath)

	def updateLayerContents(self, contentsFilePath):
		if os.path.exists(contentsFilePath):
			contentsList= plistlib.readPlist(contentsFilePath)
			# If the layer name already exists, don't add a new one, or change the path
			seenPublicDefault = False
			seenProcessedGlyph = False
			for layerName, layerPath in contentsList:
				if (layerPath == kProcessedGlyphsLayer):
					seenProcessedGlyph = True
				if (layerPath == kDefaultGlyphsLayer):
					seenPublicDefault = True
			update = False
			if not seenPublicDefault:
				update = True
				contentsList = [[kDefaultGlyphsLayerName, kDefaultGlyphsLayer]] + contentsList
			if not seenProcessedGlyph:
				update = True
				contentsList.append([kProcessedGlyphsLayerName, kProcessedGlyphsLayer])
			if update:
				plistlib.writePlist(contentsList, contentsFilePath)
		else:
			contentsList = [[kDefaultGlyphsLayerName, kDefaultGlyphsLayer]]
			contentsList.append([kProcessedGlyphsLayerName, kProcessedGlyphsLayer])
		plistlib.writePlist(contentsList, contentsFilePath)

	def updateLayerGlyphContents(self, contentsFilePath, newGlyphData):
		if os.path.exists(contentsFilePath):
			contentsDict = plistlib.readPlist(contentsFilePath)
		else:
			contentsDict = {}
		for glyphName in newGlyphData.keys():
			if self.useProcessedLayer and self.processedLayerGlyphMap: # Try for processed layer first.
				try:
					contentsDict[glyphName] = self.processedLayerGlyphMap[glyphName]
				except KeyError:
					contentsDict[glyphName] = self.glyphMap[glyphName]
			else:
				contentsDict[glyphName] = self.glyphMap[glyphName]
		plistlib.writePlist(contentsDict, contentsFilePath)

	def getFontInfo(self, fontPSName, inputPath, allow_no_blues, noFlex, vCounterGlyphs, hCounterGlyphs, fdIndex=0):
		if self.fontDict != None:
			return self.fontDict

		if self.fontInfo == None:
			self.loadFontInfo()

		fdDict = fdTools.FDDict()
		fdDict.LanguageGroup = self.fontInfo.get("languagegroup", "0") # should be 1 if the glyphs are ideographic, else 0.
		fdDict.OrigEmSqUnits = self.getUnitsPerEm()
		fdDict.FontName = self.getPSName()
		upm = self.getUnitsPerEm()
		low = min( -upm*0.25, float(self.fontInfo.get("openTypeOS2WinDescent", "0")) - 200)
		high = max ( upm*1.25, float(self.fontInfo.get("openTypeOS2WinAscent", "0")) + 200)
		# Make a set of inactive alignment zones: zones outside of the font bbox so as not to affect hinting.
		# Used when src font has no BlueValues or has invalid BlueValues. Some fonts have bad BBox values, so
		# I don't let this be smaller than -upm*0.25, upm*1.25.
		inactiveAlignmentValues = [low, low, high, high]
		blueValues = self.fontInfo.get("postscriptBlueValues", [])
		numBlueValues = len(blueValues)
		if numBlueValues < 4:
			if allow_no_blues:
				blueValues = inactiveAlignmentValues
				numBlueValues = len(blueValues)
			else:
				raise	UFOParseError("ERROR: Font must have at least four values in its BlueValues array for AC to work!")
		blueValues.sort()
		# The first pair only is a bottom zone, where the first value is the overshoot position;
		# the rest are top zones, and second value of the pair is the overshoot position.
		blueValues[0] = blueValues[0] - blueValues[1]
		for i in range(3, numBlueValues,2):
			blueValues[i] = blueValues[i] - blueValues[i-1]

		blueValues = [str(v) for v in blueValues]
		numBlueValues = min(numBlueValues, len(fdTools.kBlueValueKeys))
		for i in range(numBlueValues):
			key = fdTools.kBlueValueKeys[i]
			value = blueValues[i]
			exec("fdDict.%s = %s" % (key, value))

		otherBlues = self.fontInfo.get("postscriptOtherBlues", [])
		numBlueValues = len(otherBlues)

		if len(otherBlues) > 0:
			i = 0
			numBlueValues = len(otherBlues)
			otherBlues.sort()
			for i in range(0, numBlueValues,2):
				otherBlues[i] = otherBlues[i] - otherBlues[i+1]
			otherBlues = [str(v) for v in otherBlues]
			numBlueValues = min(numBlueValues, len(fdTools.kOtherBlueValueKeys))
			for i in range(numBlueValues):
				key = fdTools.kOtherBlueValueKeys[i]
				value = otherBlues[i]
				exec("fdDict.%s = %s" % (key, value))

		vstems = self.fontInfo.get("postscriptStemSnapV", [])
		if len(vstems) == 0:
			if allow_no_blues:
				vstems = [fdDict.OrigEmSqUnits] # dummy value. Needs to be larger than any hint will likely be,
				# as the autohint program strips out any hint wider than twice the largest global stem width.
			else:
				raise	UFOParseError("ERROR: Font does not have postscriptStemSnapV!")

		vstems.sort()
		if (len(vstems) == 0) or ((len(vstems) == 1) and (vstems[0] < 1) ):
			vstems = [fdDict.OrigEmSqUnits] # dummy value that will allow PyAC to run
			logMsg("WARNING: There is no value or 0 value for DominantV.")
		fdDict.DominantV = "[" + " ".join([str(v) for v in vstems]) + "]"

		hstems = self.fontInfo.get("postscriptStemSnapH", [])
		if len(hstems) == 0:
			if allow_no_blues:
				hstems = [fdDict.OrigEmSqUnits] # dummy value. Needs to be larger than any hint will likely be,
				# as the autohint program strips out any hint wider than twice the largest global stem width.
			else:
				raise	UFOParseError("ERROR: Font does not have postscriptStemSnapH!")

		hstems.sort()
		if (len(hstems) == 0) or ((len(hstems) == 1) and (hstems[0] < 1) ):
			hstems = [fdDict.OrigEmSqUnits] # dummy value that will allow PyAC to run
			logMsg("WARNING: There is no value or 0 value for DominantH.")
		fdDict.DominantH = "[" + " ".join([str(v) for v in hstems]) + "]"

		if noFlex:
			fdDict.FlexOK = "false"
		else:
			fdDict.FlexOK = "true"

		# Add candidate lists for counter hints, if any.
		if vCounterGlyphs:
			temp = " ".join(vCounterGlyphs)
			fdDict.VCounterChars = "( %s )" % (temp)
		if hCounterGlyphs:
			temp = " ".join(hCounterGlyphs)
			fdDict.HCounterChars = "( %s )" % (temp)

		fdDict.BlueFuzz = self.fontInfo.get("postscriptBlueFuzz", 1)
		# postscriptBlueShift
		# postscriptBlueScale
		self.fontDict = fdDict
		return fdDict

	def getfdIndex(self, gid):
		fdIndex = 0
		return fdIndex

	def getfdInfo(self, psName, inputPath, allow_no_blues, noFlex, vCounterGlyphs, hCounterGlyphs, glyphList, fdIndex=0):
		fontDictList = []
		fdGlyphDict = None
		fdDict = self.getFontInfo(psName, inputPath, allow_no_blues, noFlex, vCounterGlyphs, hCounterGlyphs, fdIndex)
		fontDictList.append(fdDict)

		# Check the fontinfo file, and add any other font dicts
		srcFontInfo = os.path.dirname(inputPath)
		srcFontInfo = os.path.join(srcFontInfo, "fontinfo")
		maxX = self.getUnitsPerEm()*2
		maxY = maxX
		minY = -self.getUnitsPerEm()
		if os.path.exists(srcFontInfo):
			with open(srcFontInfo, "rU") as fi:
				fontInfoData = fi.read()
			fontInfoData = re.sub(r"#[^\r\n]+", "", fontInfoData)

			if "FDDict" in fontInfoData:

				blueFuzz = fdDict.BlueFuzz
				fdGlyphDict, fontDictList, finalFDict = fdTools.parseFontInfoFile(fontDictList, fontInfoData, glyphList, maxY, minY, psName, blueFuzz)
				if finalFDict == None:
					# If a font dict was not explicitly specified for the output font, use the first user-specified font dict.
					fdTools.mergeFDDicts( fontDictList[1:], self.fontDict )
				else:
					fdTools.mergeFDDicts( [finalFDict], topDict )

		return fdGlyphDict, fontDictList

	def getGlyphID(self, glyphName):
		try:
			gid = self.orderMap[glyphName]
		except IndexError:
			raise UFOParseError("Could not find glyph name '%s' in UFO font contents plist. '%s'. " % (glyphName, self.parentPath))
		return gid

	def buildGlyphHashValue(self, width, outlineXML, glyphName, useDefaultGlyphDir, level = 0):
		"""
		glyphData must be the official <outline> XML from a GLIF.
		We skip contours with only one point.
		"""
		dataList = ["w%s" % (str(width))]
		if level > 10:
			raise UFOParseError("In parsing component, exceeded 10 levels of reference. '%s'. " % (glyphName))
		# <outline> tag is optional per spec., e.g. space glyph does not necessarily have it.
		if outlineXML:
			for childContour in outlineXML:
				if childContour.tag == "contour":
					if len(childContour) < 2:
						continue
					else:
						for child in childContour:
							if child.tag == "point":
								try:
									pointType = child.attrib["type"][0]
								except KeyError:
									pointType = ""
								dataList.append("%s%s%s" % (pointType, child.attrib["x"], child.attrib["y"]))
								#print(dataList[-3:])
				elif childContour.tag == "component":
					# append the component hash.
					try:
						compGlyphName = childContour.attrib["base"]
						dataList.append("%s%s" % ("base:", compGlyphName))
					except KeyError:
						raise UFOParseError("'%s' is missing the 'base' attribute in a component. glyph '%s'." % (glyphName))

					if useDefaultGlyphDir:
						try:
							componentPath = self.getGlyphDefaultPath(compGlyphName)
						except KeyError:
							raise UFOParseError("'%s' component glyph is missing from contents.plist." % (compGlyphName))
					else:
						# If we are not necessarily using the default layer for the main glyph, then a missing component
						# may not have been processed, and may just be in the default layer. We need to look for component
						# glyphs in the src list first, then in the defualt layer.
						try:
							componentPath = self.getGlyphSrcPath(compGlyphName)
							if not os.path.exists(componentPath):
								componentPath = self.getGlyphDefaultPath(compGlyphName)
						except KeyError:
							try:
								componentPath = self.getGlyphDefaultPath(compGlyphName)
							except KeyError:
								raise UFOParseError("'%s' component glyph is missing from contents.plist." % (compGlyphName))

					if not os.path.exists(componentPath):
						raise UFOParseError("'%s' component file is missing: '%s'." % (compGlyphName, componentPath))

					etRoot = ET.ElementTree()

					# Collect transformm fields, if any.
					for transformTag in ["xScale", "xyScale", "yxScale", "yScale", "xOffset", "yOffset"]:
						try:
							value = childContour.attrib[transformTag]
							rval = eval(value)
							if int(rval) == rval:
								value = str(int(rval))
							dataList.append(value)
						except KeyError:
							pass
					componentXML = etRoot.parse(componentPath)
					componentOutlineXML = componentXML.find("outline")
					componentHash, componentDataList = self.buildGlyphHashValue(width, componentOutlineXML, glyphName, useDefaultGlyphDir, level+1)
					dataList.extend(componentDataList)
		data = "".join(dataList)
		if len(data) < 128:
			hash = data
		else:
			hash = hashlib.sha512(data.encode("utf-8")).hexdigest()
		return hash, dataList

	def getComponentOutline(self, componentItem):
		try:
			compGlyphName = componentItem.attrib["base"]
		except KeyError:
			raise UFOParseError("'%s' attribute missing from component '%s'." % ("base", xmlToString(componentXML)))

		if not self.useProcessedLayer:
			try:
				compGlyphFilePath = self.getGlyphDefaultPath(compGlyphName)
			except KeyError:
				raise UFOParseError("'%s' component glyph is missing from contents.plist." % (compGlyphName))
		else:
			# If we are not necessarily using the default layer for the main glyph, then a missing component
			# may not have been processed, and may just be in the default layer. We need to look for component
			# glyphs in the src list first, then in the defualt layer.
			try:
				compGlyphFilePath = self.getGlyphSrcPath(compGlyphName)
				if not os.path.exists(compGlyphFilePath):
					compGlyphFilePath = self.getGlyphDefaultPath(compGlyphName)
			except KeyError:
				try:
					compGlyphFilePath = self.getGlyphDefaultPath(compGlyphName)
				except KeyError:
					raise UFOParseError("'%s' component glyph is missing from contents.plist." % (compGlyphName))

		if not os.path.exists(compGlyphFilePath):
			raise UFOParseError("'%s' component file is missing: '%s'." % (compGlyphName, compGlyphFilePath))

		etRoot = ET.ElementTree()
		glifXML = etRoot.parse(compGlyphFilePath)
		outlineXML = glifXML.find("outline")
		return outlineXML

	def copyTo(self, dstPath):
		""" Copy UFO font to target path"""
		return

	def close(self):
		if self.hashMapChanged:
			self.writeHashMap()
			self.hashMapChanged = False
		return

	def clearHashMap(self):
		self.hashMap = {kAdobHashMapVersionName:kAdobHashMapVersion}
		hashDir = os.path.join(self.parentPath, "data")
		if not os.path.exists(hashDir):
			return

		hashPath = os.path.join(hashDir, kAdobHashMapName)
		if os.path.exists(hashPath):
			os.remove(hashPath)

	def setWriteToDefault(self):
		self.useProcessedLayer = False
		self.writeToDefaultLayer = True
		self.glyphWriteDir = self.glyphDefaultDir

def parseGlyphOrder(filePath):
	orderMap = None
	if os.path.exists(filePath):
		publicOrderDict, temp = parsePList(filePath, kPublicGlyphOrderKey)
		if publicOrderDict != None:
			orderMap = {}
			glyphList = publicOrderDict[kPublicGlyphOrderKey]
			numGlyphs = len(glyphList)
			for i in range(numGlyphs):
				orderMap[glyphList[i]] = i
	return orderMap

def	parsePList(filePath, dictKey = None):
	# if dictKey is defined, parse and return only the data for that key.
	# This helps avoid needing to parse all plist types. I need to support only data for known keys amd lists.
	plistDict = {}
	plistKeys = []

	# I uses this rather than the plistlib in order to get a list that allows preserving key order.
	with open(filePath, "r") as fp:
		data = fp.read()
	contents = XML(data)
	dict = contents.find("dict")
	if dict == None:
		raise UFOParseError("In '%s', failed to find dict. '%s'." % (filePath))
	lastTag = "string"
	for child in dict:
		if child.tag == "key":
			if lastTag == "key":
				raise UFOParseError("In contents.plist, key name '%s' followed another key name '%s'." % (xmlToString(child), lastTag) )
			skipKeyData = False
			lastName = child.text
			lastTag = "key"
			if dictKey != None:
				if lastName != dictKey:
					skipKeyData = True
		elif child.tag != "key":
			if lastTag != "key":
				raise UFOParseError("In contents.plist, key value '%s' followed a non-key tag '%s'." % (xmlToString(child), lastTag) )
			lastTag = child.tag

			if skipKeyData:
				continue

			if lastName in plistDict:
				raise UFOParseError("Encountered duplicate key name '%s' in '%s'." % (lastName, filePath) )
			if child.tag == "array":
				list = []
				for listChild in child:
					val = listChild.text
					if listChild.tag == "integer":
						val= int(eval(val))
					elif listChild.tag == "real":
						val = float(eval(val))
					elif listChild.tag == "string":
						pass
					else:
						raise UFOParseError("In plist file, encountered unhandled key type '%s' in '%s' for parent key %s. %s." % (listChild.tag, child.tag, lastName, filePath))
					list.append(val)
				plistDict[lastName] = list
			elif child.tag == "integer":
				plistDict[lastName] = int(eval(child.text))
			elif child.tag == "real":
				plistDict[lastName] = float(eval(child.text))
			elif child.tag == "false":
				plistDict[lastName] = 0
			elif child.tag == "true":
				plistDict[lastName] = 1
			elif child.tag == "string":
				plistDict[lastName] = child.text
			else:
				raise UFOParseError("In plist file, encountered unhandled key type '%s' for key %s. %s." % (child.tag, lastName, filePath))
			plistKeys.append(lastName)
		else:
			raise UFOParseError("In plist file, encountered unexpected element '%s'. %s." % (xmlToString(child), filePath ))
	if len(plistDict) == 0:
		plistDict = None
	return plistDict, plistKeys




class UFOTransform:
	kTransformTagList = ["xScale", "xyScale", "yxScale", "yScale", "xOffset", "yOffset"]
	def __init__(self, componentXML):
		self.transformFactors = []
		self.isDefault = True
		self.isOffsetOnly = True
		hasScale = False
		hasOffset = False
		for tag in self.kTransformTagList:
			val = componentXML.attrib.get(tag, None)
			if tag in ["xScale", "yScale"]:
				if val == None:
					val = 1.0
				else:
					val = float(val)
					if val != 1.0:
						hasScale = True
			else:
				if val == None:
					val = 0
				else:
					val = float(val)
					if val != 0:
						self.isDefault = False
						if tag in ["xyScale", "yxScale"]:
							hasScale = True
						elif tag in ["xOffset", "yOffset"]:
							hasOffset = True
						else:
							raise UFOParseError("Unknown tag '%s' in component '%s'." % (tag, xmlToString(componentXML)))

			self.transformFactors.append(val)
		if hasScale or hasOffset:
			self.isDefault = False
		if hasScale:
			self.isOffsetOnly = False

	def concat(self, transform):
		if transform == None:
			return
		if transform.isDefault:
			return
		tfCur = self.transformFactors
		tfPrev = transform.transformFactors
		if transform.isOffsetOnly:
			tfCur[4] += tfPrev[4]
			tfCur[5] += tfPrev[5]
			self.isDefault = False
		else:
			tfNew = [0.0]*6
			tfNew[0] = tfCur[0]*tfPrev[0] + tfCur[1]*tfPrev[2];
			tfNew[1] = tfCur[0]*tfPrev[1] + tfCur[1]*tfPrev[3];
			tfNew[2] = tfCur[2]*tfPrev[0] + tfCur[3]*tfPrev[2];
			tfNew[3] = tfCur[2]*tfPrev[1] + tfCur[3]*tfPrev[3];
			tfNew[4] = tfCur[4]*tfPrev[0] + tfCur[5]*tfPrev[2] + tfPrev[4];
			tfNew[5] = tfCur[4]*tfPrev[1] + tfCur[5]*tfPrev[3] + tfPrev[5];
			self.transformFactors = tfNew
			self.isOffsetOnly = self.isOffsetOnly and transform.isOffsetOnly
			self.isDefault = False

	def apply(self, x, y):
		tfCur = self.transformFactors
		if self.isOffsetOnly:
			x += tfCur[4]
			y += tfCur[5]
		else:
			x, y = x*tfCur[0] + y*tfCur[2] + tfCur[4], \
				   x*tfCur[1] + y*tfCur[3] + tfCur[5]
		return x,y




def convertGlyphOutlineToBezString(outlineXML, ufoFontData, transform = None, level = 0):
	"""convert XML outline element containing contours and components to a bez string.
	Since xml.etree.CElementTree is compiled code, this
	will run faster than tokenizing and parsing in regular Python.

	glyphMap is a dict mapping glyph names to component file names.
	If it has a length of 1, it needs to be filled from the contents.plist file.
	It must have a key/value [UFO_FONTPATH] = path to parent UFO font.

	transformList is None, or a list of floats in the order:
	["xScale", "xyScale", "yxScale", "yScale", "xOffset", "yOffset"]

	Build a list of outlines and components.
	For each item:
		if is outline:
			get list of points
			for each point:
				if transform matrix:
					apply transform to coordinate
				if off line
					push coord on stack
				if online:
					add operator
			if is component:
				if any scale, skew, or offset factors:
					set transform
					call convertGlyphOutlineToBezString with transform
					add to bez string
	I don't bother adding in any hinting info, as this is used only for making
	temp bez files as input to checkOutlines or autohint, which invalidate hinting data
	by changing the outlines data, or at best ignore hinting data.

	bez ops output: ["rrmt", "dt", "rct", "cp" , "ed"]
	"""

	if level > 10:
		raise UFOParseError("In parsing component, exceeded 10 levels of reference. '%s'. " % (outlineXML))

	allowDecimals = ufoFontData.allowDecimalCoords

	bezStringList = []
	if outlineXML == None:
		bezstring = ""
	else:
		for outlineItem in outlineXML:

			if outlineItem.tag == "component":
				newTransform = UFOTransform(outlineItem)
				if newTransform.isDefault:
					if transform != None:
						newTransform.concat(transform)
					else:
						newTransform = None
				else:
					if transform != None:
						newTransform.concat(transform)
				componentOutline = ufoFontData.getComponentOutline(outlineItem)
				if componentOutline:
					outlineBezString = convertGlyphOutlineToBezString(componentOutline, ufoFontData, newTransform, level+1)
					bezStringList.append(outlineBezString)
				continue

			if outlineItem.tag != "contour":
				continue

			# May be an empty contour.
			if len(outlineItem) == 0:
				continue

			# We have a regular contour. Iterate over points.
			argStack = []
			# Deal with setting up move-to.
			lastItem = outlineItem[0]
			try:
				type = lastItem.attrib["type"]
			except KeyError:
				type = "offcurve"
			if type in ["curve","line","qccurve"]:
				outlineItem = outlineItem[1:]
				if type != "line":
					outlineItem.append(lastItem) # I don't do this for line, as AC behaves differently when a final line-to is explicit.
				x = float(lastItem.attrib["x"])
				y = float(lastItem.attrib["y"])
				if transform != None:
					x,y = transform.apply(x,y)

				if (not allowDecimals):
					x = int(round(x))
					y = int(round(y))

				if (allowDecimals):
					op = "%.3f %.3f mt" % (x , y)
				else:
					op = "%s %s mt" % (x, y)
				bezStringList.append(op)
			elif type == "move":
				# first op is a move-to.
				if len(outlineItem) == 1:
					# this is a move, and is the only op in this outline. Don't pass it through.
					# this is most likely left over from a GLIF format 1 anchor.
					argStack = []
					continue
			elif type == "offcurve":
				# We should only see an off curve point as the first point when the first op is a curve and the last op is a line.
				# In this case, insert a move-to to the line's coords, then omit the line.
				# Breaking news! In rare cases, a first off-curve point can occur when the first AND last op is a curve.
				curLastItem = outlineItem[-1]
				# In this case, insert a move-to to the last op's end pos.
				try:
					lastType = curLastItem.attrib["type"]
					x = float(curLastItem.attrib["x"])
					y = float(curLastItem.attrib["y"])
					if transform != None:
						x,y = transform.apply(x,y)

					if (not allowDecimals):
						x = int(round(x))
						y = int(round(y))

					if lastType == "line":

						if (allowDecimals):
							op = "%.3f %.3f mt" % (x , y)
						else:
							op = "%s %s mt" % (x, y)

						bezStringList.append(op)
						outlineItem = outlineItem[:-1]
					elif lastType == "curve":

						if (allowDecimals):
							op = "%.3f %.3f mt" % (x , y)
						else:
							op = "%s %s mt" % (x, y)
						bezStringList.append(op)

				except KeyError:
					raise UFOParseError("Unhandled case for first and last points in outline '%s'." % (xmlToString(outlineItem)))
			else:
				raise UFOParseError("Unhandled case for first point in outline '%s'." % (xmlToString(outlineItem)))

			for contourItem in outlineItem:
				if contourItem.tag != "point":
					continue
				x = float(contourItem.attrib["x"])
				y = float(contourItem.attrib["y"])
				if transform != None:
					x,y = transform.apply(x,y)

				if (not allowDecimals):
					x = int(round(x))
					y = int(round(y))

				try:
					type = contourItem.attrib["type"]
				except KeyError:
					type = "offcurve"

				if type == "offcurve":
					argStack.append(x)
					argStack.append(y)
				elif type == "move":
					if (allowDecimals):
						op = "%.3f %.3f mt" % (x , y)
					else:
						op = "%s %s mt" % (x, y)
					bezStringList.append(op)
					argStack = []
				elif type == "line":
					if (allowDecimals):
						op = "%.3f %.3f dt" % (x, y)
					else:
						op = "%s %s dt" % (x, y)
					bezStringList.append(op)
					argStack = []
				elif type == "curve":
					if len(argStack) != 4:
						raise UFOParseError("Argument stack error seen for curve point '%s'." % (xmlToString(contourItem)))
					if (allowDecimals):
						op = "%.3f %.3f %.3f %.3f %.3f %.3f ct" % ( argStack[0], argStack[1], argStack[2], argStack[3], x, y)
					else:
						op = "%s %s %s %s %s %s ct" % ( argStack[0], argStack[1], argStack[2], argStack[3], x, y)
					argStack = []
					bezStringList.append(op)
				elif type == "qccurve":
					raise UFOParseError("Point type not supported '%s'." % (xmlToString(contourItem)))
				else:
					raise UFOParseError("Unknown Point type not supported '%s'." % (xmlToString(contourItem)))

			# we get here only if there was at least a move.
			bezStringList.append("cp")
		bezstring = "\n".join(bezStringList)
	return bezstring


def convertGLIFToBez(ufoFontData, glyphName, beVerbose, doAll=False):
	width, outlineXML, skip = ufoFontData.getOrSkipGlyphXML(glyphName, doAll)
	if skip:
		return None, width

	if outlineXML == None:
		return None, width

	bezString = convertGlyphOutlineToBezString(outlineXML, ufoFontData)
	bezString = "\n".join(["% " + glyphName, "sc", bezString, "ed", ""])
	return bezString, width


class HintMask:
	# class used to collect hints for the current hint mask when converting bez to T2.
	def __init__(self, listPos):
		self.listPos = listPos # The index into the pointList is kept so we can quickly find them later.
		self.hList = [] # These contain the actual hint values.
		self.vList = []
		self.hstem3List = []
		self.vstem3List = []
		self.pointName = "hintSet" + str(listPos).zfill(4)	# The name attribute of the point which follows the new hint set.

	def addHintSet(self, hintSetList):
		# Add the hint set to hintSetList
		newHintSetDict = XMLElement("dict")
		hintSetList.append(newHintSetDict)

		newHintSetKey = XMLElement("key")
		newHintSetKey.text = kPointTag
		newHintSetDict.append(newHintSetKey)

		newHintSetValue = XMLElement("string")
		newHintSetValue.text = self.pointName
		newHintSetDict.append(newHintSetValue)

		stemKey = XMLElement("key")
		stemKey.text = "stems"
		newHintSetDict.append(stemKey)

		newHintSetArray = XMLElement("array")
		newHintSetDict.append(newHintSetArray)

		newHintSet = []
		if (len(self.hList) > 0) or (len(self.vstem3List)):
			isH = True
			addHintList(self.hList, self.hstem3List, newHintSetArray, isH)

		if (len(self.vList) > 0) or (len(self.vstem3List)):
			isH = False
			addHintList(self.vList, self.vstem3List, newHintSetArray, isH)


def makeStemHintList(hintsStem3, stemList, isH):
	stem3Args = []
	lastPos = 0
	# In bez terms, the first coordinate in each pair is absolute, second is relative, and hence is the width.
	if isH:
		op = kHStem3Name
	else:
		op = kVStem3Name
	newStem = XMLElement("string")
	posList = [op]
	for stem3 in hintsStem3:
		for pos, width in stem3:
			if (type(pos) == type(0.0)) and (int(pos) == pos):
				pos = int(pos)
			if (type(width) == type(0.0)) and (int(width) == width):
				width = int(width)
			posList.append("%s %s" % (pos, width))
	posString = " ".join(posList)
	newStem.text = posString
	stemList.append(newStem)

def makeHintList(hints, newHintSetArray, isH):
	# Add the list of hint operators
	hintList = []
	lastPos = 0
	# In bez terms, the first coordinate in each pair is absolute, second is relative, and hence is the width.
	for hint in hints:
		if not hint:
			continue
		pos = hint[0]
		if (type(pos) == type(0.0)) and (int(pos) == pos):
			pos = int(pos)
		width = hint[1]
		if (type(width) == type(0.0)) and (int(width) == width):
			width = int(width)
		if isH:
			op = kHStemName
		else:
			op = kVStemName
		newStem = XMLElement("string")
		newStem.text = "%s %s %s" % (op, pos, width)
		newHintSetArray.append(newStem)

def addFlexHint(flexList, flexArray):
	for pointTag in flexList:
		newFlexTag = XMLElement("string")
		newFlexTag.text = pointTag
		flexArray.append(newFlexTag)

def fixStartPoint(outlineItem, opList):
	# For the GLIF format, the idea of first/last point is funky, because the format avoids identifying a
	# a start point. This means there is no implied close-path line-to. If the last implied or explicit path-close
	# operator is a line-to, then replace the "mt" with linto, and remove the last explicit path-closing line-to, if any.
	# If the last op is a curve, then leave the first two point args on the stack at the end of the point list,
	# and move the last curveto to the first op, replacing the move-to.

	firstOp, firstX, firstY = opList[0]
	lastOp, lastX, lastY = opList[-1]
	firstPointElement = outlineItem[0]
	if (firstX == lastX) and (firstY == lastY):
		del outlineItem[-1]
		firstPointElement.set("type", lastOp)
	else:
		# we have an implied final line to. All we need to do is convert the inital moveto to a lineto.
		firstPointElement.set("type", "line")


bezToUFOPoint = {
		"mt" : 'move',
		"rmt" : 'move',
		 "hmt" : 'move',
		"vmt" : 'move',
		"rdt" : 'line',
		"dt" : 'line',
		"hdt" : "line",
		"vdt" : "line",
		"rct" : 'curve',
		"ct" : 'curve',
		"rcv" : 'curve', # Morisawa's alternate name for 'rct'.
		"vhct": 'curve',
		"hvct": 'curve',
		}

def convertCoords(curX, curY):
	showX = int(curX)
	if showX != curX:
		showX = curX
	showY = int(curY)
	if showY != curY:
		showY = curY
	return showX, showY

def convertBezToOutline(ufoFontData, glyphName, bezString):
	""" Since the UFO outline element as no attributes to preserve, I can
	just make a new one.
	"""
	# convert bez data to a UFO glif XML representation
	#
	# Convert all bez ops to simplest UFO equivalent
	# Add all hints to vertical and horizontal hint lists as encountered; insert a HintMask class whenever a
	# new set of hints is encountered
	# after all operators have been processed, convert HintMask items into hintmask ops and hintmask bytes
	# add all hints as prefix
	# review operator list to optimize T2 operators.
	# if useStem3 == 1, then any counter hints must be processed as stem3 hints, else the opposite.
	# counter hints are used only in LanguageGroup 1 glyphs, aka ideographs

	bezString = re.sub(r"%.+?\n", "", bezString) # supress comments
	bezList = re.findall(r"(\S+)", bezString)
	if not bezList:
		return "", None, None
	flexList = []
	hintMask = HintMask(0) # Create an initial hint mask. We use this if there is no explicit initial hint sub.
	hintMaskList = [hintMask]
	vStem3Args = []
	hStem3Args = []
	vStem3List = []
	hStem3List = []
	argList = []
	opList = []
	newHintMaskName = None
	inPreFlex = False
	hintInfoDict = None
	opIndex = 0
	lastPathOp = None
	curX = 0
	curY = 0
	newOutline = XMLElement("outline")
	outlineItem = None
	seenHints = False

	for token in bezList:
		try:
			val = float(token)
			argList.append(val)
			continue
		except ValueError:
			pass
		if token == "newcolors":
			lastPathOp = token
			pass
		elif token in ["beginsubr", "endsubr"]:
			lastPathOp = token
			pass
		elif token in ["snc"]:
			lastPathOp = token
			hintMask = HintMask(opIndex)
			if opIndex == 0: # If the new colors precedes any marking operator, then we want throw away the initial hint mask we made, and use the new one as the first hint mask.
				hintMaskList = [hintMask]
			else:
				hintMaskList.append(hintMask)
			newHintMaskName = hintMask.pointName
		elif token in ["enc"]:
			lastPathOp = token
			pass
		elif token == "div":
			# I specifically do NOT set lastPathOp for this.
			value = argList[-2]/float(argList[-1])
			argList[-2:] =[value]
		elif token == "rb":
			if newHintMaskName == None:
				newHintMaskName = hintMask.pointName
			lastPathOp = token
			hintMask.hList.append(argList)
			argList = []
			seenHints = True
		elif token == "ry":
			if newHintMaskName == None:
				newHintMaskName = hintMask.pointName
			lastPathOp = token
			hintMask.vList.append(argList)
			argList = []
			seenHints = True
		elif token == "rm": # vstem3's are vhints
			if newHintMaskName == None:
				newHintMaskName = hintMask.pointName
			seenHints = True
			vStem3Args.append(argList)
			argList = []
			lastPathOp = token
			if len(vStem3Args) == 3:
				hintMask.vstem3List.append(vStem3Args)
				vStem3Args = []

		elif token == "rv": # hstem3's are hhints
			seenHints = True
			hStem3Args.append(argList)
			argList = []
			lastPathOp = token
			if len(hStem3Args) == 3:
				hintMask.hstem3List.append(hStem3Args)
				hStem3Args = []

		elif token == "preflx1":
			# the preflx1/preflx2 sequence provides the same i as the flex sequence; the difference is that the
			# preflx1/preflx2 sequence provides the argument values needed for building a Type1 string
			# while the flex sequence is simply the 6 rcurveto points. Both sequences are always provided.
			lastPathOp = token
			argList = []
			inPreFlex = True # need to skip all move-tos until we see the "flex" operator.
		elif token == "preflx2a":
			lastPathOp = token
			argList = []
		elif token == "preflx2":
			lastPathOp = token
			argList = []
		elif token == "flxa": # flex with absolute coords.
			inPreFlex = False
			flexPointName = kBaseFlexName + str(opIndex).zfill(4)
			flexList.append(flexPointName)
			curveCnt = 2
			i = 0
			# The first 12 args are the 6 args for each of the two curves that make up the flex feature.
			while i < curveCnt:
				curX = argList[0]
				curY = argList[1]
				showX, showY = convertCoords(curX, curY)
				newPoint = XMLElement("point", {"x": "%s" % (showX), "y": "%s" % (showY) } )
				outlineItem.append(newPoint)
				curX = argList[2]
				curY = argList[3]
				showX, showY = convertCoords(curX, curY)
				newPoint = XMLElement("point", {"x": "%s" % (showX), "y": "%s" % (showY) } )
				outlineItem.append(newPoint)
				curX = argList[4]
				curY = argList[5]
				showX, showY = convertCoords(curX, curY)
				opName = 'curve'
				newPoint = XMLElement("point", {"x": "%s" % (showX), "y": "%s" % (showY), "type": opName } )
				outlineItem.append(newPoint)
				opList.append([opName,curX, curY])
				opIndex += 1
				if i == 0:
					argList = argList[6:12]
				i += 1
			outlineItem[-6].set(kPointName, flexPointName) # attach the point name to the first point of the first curve.
			if (newHintMaskName != None):
				# We have a hint mask that we want to attach to the first point of the flex op. However, there is already
				# a flex name in that attribute. What we do is set the flex point name into the hint mask.
				hintMask.pointName = flexPointName
				newHintMaskName = None
			lastPathOp = token
			argList = []
		elif token == "flx":
			inPreFlex = False
			flexPointName = kBaseFlexName + str(opIndex).zfill(4)
			flexList.append(flexPointName)
			curveCnt = 2
			i = 0
			# The first 12 args are the 6 args for each of the two curves that make up the flex feature.
			while i < curveCnt:
				curX += argList[0]
				curY += argList[1]
				showX, showY = convertCoords(curX, curY)
				newPoint = XMLElement("point", {"x": "%s" % (showX), "y": "%s" % (showY) } )
				outlineItem.append(newPoint)
				curX += argList[2]
				curY += argList[3]
				showX, showY = convertCoords(curX, curY)
				newPoint = XMLElement("point", {"x": "%s" % (showX), "y": "%s" % (showY) } )
				outlineItem.append(newPoint)
				curX += argList[4]
				curY += argList[5]
				showX, showY = convertCoords(curX, curY)
				opName = 'curve'
				newPoint = XMLElement("point", {"x": "%s" % (showX), "y": "%s" % (showY), "type": opName } )
				outlineItem.append(newPoint)
				opList.append([opName,curX, curY])
				opIndex += 1
				if i == 0:
					argList = argList[6:12]
				i += 1
			outlineItem[-6].set(kPointName, flexPointName) # attach the point name to the first point of the first curve.
			if (newHintMaskName != None):
				# We have a hint mask that we want to attach to the first point of the flex op. However, there is already
				# a flex name in that attribute. What we do is set the flex point name into the hint mask.
				hintMask.pointName = flexPointName
				newHintMaskName = None
			lastPathOp = token
			argList = []
		elif token == "sc":
			lastPathOp = token
			pass
		elif token == "cp":
			pass
		elif token == "ed":
		 	pass
		else:
			if inPreFlex and (token[-2:] == "mt"):
				continue

			if token[-2:] in ["mt", "dt", "ct", "cv"]:
				lastPathOp = token
				opIndex += 1
			else:
				print("Unhandled operation", argList, token)
				raise BezParseError("Unhandled operation: '%s' '%s'.", argList, token)
			dx = dy = 0
			opName = bezToUFOPoint[token]
			if token[-2:] in ["mt", "dt"]:
				if token in ["mt", "dt"]:
					curX = argList[0]
					curY = argList[1]
				else:
					if token in ["rmt", "rdt"]:
						dx = argList[0]
						dy = argList[1]
					elif token in ["hmt", "hdt"]:
						dx = argList[0]
					elif token in ["vmt", "vdt"]:
						dy = argList[0]
					curX += dx
					curY += dy
				showX, showY = convertCoords(curX, curY)
				newPoint = XMLElement("point", {"x": "%s" % (showX), "y": "%s" % (showY), "type": "%s" % (opName) } )

				if opName == "move":
					if outlineItem != None:
						if len(outlineItem) == 1:
							# Just in case we see two moves in a row, delete the previous outlineItem if it has only the move-to''
							print("Deleting moveto:", xmlToString(newOutline[-1]), "adding", xmlToString(outlineItem))
							del newOutline[-1]
						else:
							fixStartPoint(outlineItem, opList) # Fix the start/implied end path of the previous path.
					opList = []
					outlineItem = XMLElement('contour')
					newOutline.append(outlineItem)

				if (newHintMaskName != None):
					newPoint.set(kPointName, newHintMaskName)
					newHintMaskName = None
				outlineItem.append(newPoint)
				opList.append([opName,curX, curY])
			else:
				if token in ["ct", "cv"]:
					curX = argList[0]
					curY = argList[1]
					showX, showY = convertCoords(curX, curY)
					newPoint = XMLElement("point", {"x": "%s" % (showX), "y": "%s" % (showY) } )
					outlineItem.append(newPoint)
					curX = argList[2]
					curY = argList[3]
					showX, showY = convertCoords(curX, curY)
					newPoint = XMLElement("point", {"x": "%s" % (showX), "y": "%s" % (showY) } )
					outlineItem.append(newPoint)
					curX = argList[4]
					curY = argList[5]
					showX, showY = convertCoords(curX, curY)
					newPoint = XMLElement("point", {"x": "%s" % (showX), "y": "%s" % (showY), "type": "%s" % (opName) } )
					outlineItem.append(newPoint)
				else:
					if token in ["rct", "rcv"]:
						curX += argList[0]
						curY += argList[1]
						showX, showY = convertCoords(curX, curY)
						newPoint = XMLElement("point", {"x": "%s" % (showX), "y": "%s" % (showY) } )
						outlineItem.append(newPoint)
						curX += argList[2]
						curY += argList[3]
						showX, showY = convertCoords(curX, curY)
						newPoint = XMLElement("point", {"x": "%s" % (showX), "y": "%s" % (showY) } )
						outlineItem.append(newPoint)
						curX += argList[4]
						curY += argList[5]
						showX, showY = convertCoords(curX, curY)
						newPoint = XMLElement("point", {"x": "%s" % (showX), "y": "%s" % (showY), "type": "%s" % (opName) } )
						outlineItem.append(newPoint)
					elif token == "vhct":
						curY += argList[0]
						showX, showY = convertCoords(curX, curY)
						newPoint = XMLElement("point", {"x": "%s" % (showX), "y": "%s" % (showY) } )
						outlineItem.append(newPoint)
						curX += argList[1]
						curY += argList[2]
						showX, showY = convertCoords(curX, curY)
						newPoint = XMLElement("point", {"x": "%s" % (showX), "y": "%s" % (showY) } )
						outlineItem.append(newPoint)
						curX += argList[3]
						showX, showY = convertCoords(curX, curY)
						newPoint = XMLElement("point", {"x": "%s" % (showX), "y": "%s" % (showY), "type": "%s" % (opName) } )
						outlineItem.append(newPoint)
					elif token == "hvct":
						curX += argList[0]
						showX, showY = convertCoords(curX, curY)
						newPoint = XMLElement("point", {"x": "%s" % (showX), "y": "%s" % (showY) } )
						outlineItem.append(newPoint)
						curX += argList[1]
						curY += argList[2]
						showX, showY = convertCoords(curX, curY)
						newPoint = XMLElement("point", {"x": "%s" % (showX), "y": "%s" % (showY) } )
						outlineItem.append(newPoint)
						curY += argList[3]
						showX, showY = convertCoords(curX, curY)
						newPoint = XMLElement("point", {"x": "%s" % (showX), "y": "%s" % (showY), "type": "%s" % (opName) } )
						outlineItem.append(newPoint)
				if (newHintMaskName != None):
					outlineItem[-3].set(kPointName, newHintMaskName) # attach the pointName to the first point of the curve.
					newHintMaskName = None
				opList.append([opName,curX, curY])
			argList = []
	if outlineItem != None:
		if len(outlineItem) == 1:
			# Just in case we see two moves in a row, delete the previous outlineItem if it has zero length.
			del newOutline[-1]
		else:
			fixStartPoint(outlineItem, opList)

	# add hints, if any
	# Must be done at the end of op processing to make sure we have seen all the
	# hints in the bez string.
	# Note that the hintmasks are identified in the opList by the point name.
	# We will follow the T1 spec: a glyph may have stem3 counter hints or regular hints, but not both.

	hintSetList = None
	if (seenHints) or (len(flexList) > 0):
		hintInfoDict = XMLElement("dict")

		idItem = XMLElement("key")
		idItem.text = "id"
		hintInfoDict.append(idItem)

		idString = XMLElement("string")
		idString.text = "id"
		hintInfoDict.append(idString)

		hintSetListItem = XMLElement("key")
		hintSetListItem.text = kHintSetListName
		hintInfoDict.append(hintSetListItem)

		hintSetListArray = XMLElement("array")
		hintInfoDict.append(hintSetListArray)
		# Convert the rest of the hint masks to a hintmask op and hintmask bytes.
		for hintMask in hintMaskList:
			hintMask.addHintSet(hintSetListArray)

		if len(flexList) > 0:
			hintSetListItem = XMLElement("key")
			hintSetListItem.text = kFlexIndexListName
			hintInfoDict.append(hintSetListItem)

			flexArray = XMLElement("array")
			hintInfoDict.append(flexArray)
			addFlexHint(flexList, flexArray)
	return newOutline, hintInfoDict

def addHintList(hints, hintsStem3, newHintSetArray, isH):
	# A charstring may have regular v stem hints or vstem3 hints, but not both.
	# Same for h stem hints and hstem3 hints.
	if len(hintsStem3) > 0:
		hintsStem3.sort()
		numHints = len(hintsStem3)
		hintLimit = (kStackLimit-2)/2
		if numHints >=hintLimit:
			hintsStem3 = hintsStem3[:hintLimit]
			numHints = hintLimit
		makeStemHintList(hintsStem3, newHintSetArray, isH)

	else:
		hints.sort()
		numHints = len(hints)
		hintLimit = (kStackLimit-2)/2
		if numHints >=hintLimit:
			hints = hints[:hintLimit]
			numHints = hintLimit
		makeHintList(hints, newHintSetArray, isH)


def addWhiteSpace(parent, level):
	child = None
	childIndent = "\n" + ("  "*(level +1))
	prentIndent = "\n" + ("  "*(level))
	#print("parent Tag", parent.tag, repr(parent.text), repr(parent.tail))
	for child in parent:
		child.tail = childIndent
		addWhiteSpace(child, level +1)
	if child != None:
		if parent.text == None:
			parent.text = childIndent
		child.tail = prentIndent
		#print("lastChild Tag", child.tag, repr(child.text), repr(child.tail), "parent Tag", parent.tag)


def convertBezToGLIF(ufoFontData, glyphName, bezString, hintsOnly = False):
	# I need to replace the contours with data from the bez string.
	glyphPath = ufoFontData.getGlyphSrcPath(glyphName)

	with open(glyphPath, "r") as fp:
		data = fp.read()

	glifXML = XML(data)

	outlineItem = None
	libIndex = outlineIndex = -1
	outlineIndex = outlineIndex = -1
	childIndex = 0
	for childElement in glifXML:
		if childElement.tag == "outline":
			outlineItem = childElement
			outlineIndex = childIndex
		if childElement.tag == "lib":
			libIndex = childIndex
		childIndex += 1


	newOutlineElement, hintInfoDict = convertBezToOutline(ufoFontData, glyphName, bezString )
	#print(xmlToString(stemHints))

	if not hintsOnly:
		if outlineItem == None:
			# need to add it. Add it before the lib item, if any.
			if libIndex > 0:
				glifXML.insert(libIndex, newOutlineElement)
			else:
				glifXML.append(newOutlineElement)
		else:
			# remove the old one and add the new one.
			glifXML.remove(outlineItem)
			glifXML.insert(outlineIndex, newOutlineElement)

	# convertBezToGLIF is called only if the GLIF has been edited by a tool. We need to update the edit status
	# in the has map entry.
	# I assume that convertGLIFToBez has ben run before, which will add an entry for this glyph.
	ufoFontData.updateHashEntry(glyphName, changed=True)
	# Add the stem hints.
	if (hintInfoDict != None):
		widthXML = glifXML.find("advance")
		if widthXML != None:
			width = int(eval(widthXML.get("width")))
		else:
			width = 1000
		useDefaultGlyphDir = False
		newGlyphHash, dataList = ufoFontData.buildGlyphHashValue(width, newOutlineElement, glyphName, useDefaultGlyphDir)
		# We add this hash to the T1 data, as it is the hash which matches the output outline data.
		# This is not necessarily the same as the the hash of the source data - autohint can be used to change outlines.
		if libIndex > 0:
			libItem = glifXML[libIndex]
		else:
			libItem = XMLElement("lib")
			glifXML.append(libItem)

		dictItem = libItem.find("dict")
		if dictItem == None:
			dictItem = XMLElement("dict")
			libItem.append(dictItem)

		# Remove any existing hint data.
		i = 0
		childList = list(dictItem)
		for childItem in childList:
			i += 1
			if (childItem.tag == "key") and ((childItem.text == kHintDomainName1) or (childItem.text == kHintDomainName2)):
				dictItem.remove(childItem) # remove key
				dictItem.remove(childList[i]) # remove data item.

		glyphDictItem = dictItem
		key = XMLElement("key")
		key.text = kHintDomainName2
		glyphDictItem.append(key)

		glyphDictItem.append(hintInfoDict)

		childList = list(hintInfoDict)
		idValue = childList[1]
		idValue.text = newGlyphHash

	addWhiteSpace(glifXML, 0)
	return glifXML