otl.php 237 KB
Newer Older
Nahla Shiri's avatar
Nahla Shiri committed
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 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489 5490 5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505 5506 5507 5508 5509 5510 5511 5512 5513 5514 5515 5516 5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537 5538 5539 5540 5541 5542 5543 5544 5545 5546 5547 5548 5549 5550 5551 5552 5553 5554 5555 5556 5557 5558 5559 5560 5561 5562 5563 5564 5565 5566 5567 5568 5569 5570 5571 5572 5573 5574 5575 5576 5577 5578 5579 5580 5581 5582 5583 5584 5585 5586 5587 5588 5589 5590 5591 5592 5593 5594 5595 5596 5597 5598 5599 5600 5601 5602 5603 5604 5605 5606 5607 5608 5609 5610 5611 5612 5613 5614 5615 5616 5617 5618 5619 5620 5621 5622 5623 5624 5625 5626 5627 5628 5629 5630 5631 5632 5633 5634 5635 5636 5637 5638 5639 5640 5641 5642 5643 5644 5645 5646 5647 5648 5649 5650 5651 5652 5653 5654 5655 5656 5657 5658 5659 5660 5661 5662 5663 5664 5665 5666 5667 5668 5669 5670 5671 5672 5673 5674 5675 5676 5677 5678 5679 5680 5681 5682 5683 5684 5685 5686 5687 5688 5689 5690 5691 5692 5693 5694 5695 5696 5697 5698 5699 5700 5701 5702 5703 5704 5705 5706 5707 5708 5709 5710 5711 5712 5713 5714 5715 5716 5717 5718 5719
<?php

define("_OTL_OLD_SPEC_COMPAT_1", true);

define("_DICT_NODE_TYPE_SPLIT", 0x01);
define("_DICT_NODE_TYPE_LINEAR", 0x02);
define("_DICT_INTERMEDIATE_MATCH", 0x03);
define("_DICT_FINAL_MATCH", 0x04);



class otl {

var $mpdf;
var $arabLeftJoining;
var $arabRightJoining;
var $arabTransparentJoin;
var $arabTransparent;
var $GSUBdata;
var $GPOSdata;
var $GSUBfont;
var $fontkey;
var $ttfOTLdata;
var $glyphIDtoUni;
var $_pos;
var $GSUB_offset;
var $GPOS_offset;
var $MarkAttachmentType;
var $MarkGlyphSets;
var $GlyphClassMarks; 
var $GlyphClassLigatures; 
var $GlyphClassBases; 
var $GlyphClassComponents; 
var $Ignores;
var $LuCoverage;
var $OTLdata;
var $assocLigs;
var $assocMarks;
var $shaper;
var $restrictToSyllable;
var $lbdicts;	// Line-breaking dictionaries
var $LuDataCache;

var $debugOTL = false;

function otl(&$mpdf) {
	$this->mpdf = $mpdf;

	$this->arabic_initialise();
	$this->current_fh = '';

	$this->lbdicts = array();
	$this->LuDataCache = array();
}

////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////
//////////       APPLY OTL          ////////////////////////////
////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////

function applyOTL($str, $useOTL) {
	$this->OTLdata = array();
	if (trim($str)=='') { return $str; }
	if (!$useOTL) { return $str; }

	// 1. Load GDEF data
	//==============================
	$this->fontkey = $this->mpdf->CurrentFont['fontkey'];
	$this->glyphIDtoUni = $this->mpdf->CurrentFont['glyphIDtoUni'];
	if (!isset($this->GDEFdata[$this->fontkey])) {
		include(_MPDF_TTFONTDATAPATH.$this->fontkey.'.GDEFdata.php'); 
		$this->GSUB_offset = $this->GDEFdata[$this->fontkey]['GSUB_offset'] = $GSUB_offset;
		$this->GPOS_offset = $this->GDEFdata[$this->fontkey]['GPOS_offset'] = $GPOS_offset;
		$this->GSUB_length = $this->GDEFdata[$this->fontkey]['GSUB_length'] = $GSUB_length;
		$this->MarkAttachmentType = $this->GDEFdata[$this->fontkey]['MarkAttachmentType'] = $MarkAttachmentType;
		$this->MarkGlyphSets = $this->GDEFdata[$this->fontkey]['MarkGlyphSets'] = $MarkGlyphSets;
		$this->GlyphClassMarks = $this->GDEFdata[$this->fontkey]['GlyphClassMarks'] = $GlyphClassMarks; 
		$this->GlyphClassLigatures = $this->GDEFdata[$this->fontkey]['GlyphClassLigatures'] = $GlyphClassLigatures; 
		$this->GlyphClassComponents = $this->GDEFdata[$this->fontkey]['GlyphClassComponents'] = $GlyphClassComponents; 
		$this->GlyphClassBases = $this->GDEFdata[$this->fontkey]['GlyphClassBases'] = $GlyphClassBases;
	}
	else {
		$this->GSUB_offset = $this->GDEFdata[$this->fontkey]['GSUB_offset'];
		$this->GPOS_offset = $this->GDEFdata[$this->fontkey]['GPOS_offset'];
		$this->GSUB_length = $this->GDEFdata[$this->fontkey]['GSUB_length'];
		$this->MarkAttachmentType = $this->GDEFdata[$this->fontkey]['MarkAttachmentType'];
		$this->MarkGlyphSets = $this->GDEFdata[$this->fontkey]['MarkGlyphSets'];
		$this->GlyphClassMarks = $this->GDEFdata[$this->fontkey]['GlyphClassMarks']; 
		$this->GlyphClassLigatures = $this->GDEFdata[$this->fontkey]['GlyphClassLigatures']; 
		$this->GlyphClassComponents = $this->GDEFdata[$this->fontkey]['GlyphClassComponents']; 
		$this->GlyphClassBases = $this->GDEFdata[$this->fontkey]['GlyphClassBases'];
	}

	// 2. Prepare string as HEX string and Analyse character properties
	//=================================================================
	$earr = $this->mpdf->UTF8StringToArray($str, false);

	$scriptblock = 0;
	$scriptblocks = array();
	$scriptblocks[0] = 0;
	$vstr = '';
	$OTLdata = array();
	$subchunk = 0;
	$charctr = 0;
	foreach($earr as $char) {
		$ucd_record = UCDN::get_ucd_record($char);
		$sbl = $ucd_record[6];

		// Special case - Arabic End of Ayah
		if ($char==1757) { $sbl = UCDN::SCRIPT_ARABIC; }

		if ($sbl && $sbl != 40 && $sbl != 102) {
			if ($scriptblock == 0) { $scriptblock = $sbl; $scriptblocks[$subchunk] = $scriptblock; }
			else if ($scriptblock > 0 && $scriptblock != $sbl) {
				// *************************************************
				// NEW (non-common) Script encountered in this chunk. Start a new subchunk
				$subchunk++;
				$scriptblock = $sbl;
				$charctr = 0;
				$scriptblocks[$subchunk] = $scriptblock;
			}
		}

		$OTLdata[$subchunk][$charctr]['general_category'] = $ucd_record[0];
		$OTLdata[$subchunk][$charctr]['bidi_type'] = $ucd_record[2];

		//$OTLdata[$subchunk][$charctr]['combining_class'] = $ucd_record[1];
		//$OTLdata[$subchunk][$charctr]['bidi_type'] = $ucd_record[2];
		//$OTLdata[$subchunk][$charctr]['mirrored'] = $ucd_record[3];
		//$OTLdata[$subchunk][$charctr]['east_asian_width'] = $ucd_record[4];
		//$OTLdata[$subchunk][$charctr]['normalization_check'] = $ucd_record[5];
		//$OTLdata[$subchunk][$charctr]['script'] = $ucd_record[6];

		$charasstr = $this->unicode_hex($char); 

		if (strpos($this->GlyphClassMarks, $charasstr)!==false) { $OTLdata[$subchunk][$charctr]['group'] =  'M'; }
		else if ($char == 32 || $char == 12288) { $OTLdata[$subchunk][$charctr]['group'] =  'S'; }	// 12288 = 0x3000 = CJK space
		else { $OTLdata[$subchunk][$charctr]['group'] =  'C'; }

		$OTLdata[$subchunk][$charctr]['uni'] =  $char;
		$OTLdata[$subchunk][$charctr]['hex'] =  $charasstr;
		$charctr++;
	}

	/* PROCESS EACH SUBCHUNK WITH DIFFERENT SCRIPTS */
for($sch=0;$sch<=$subchunk;$sch++) {
	$this->OTLdata = $OTLdata[$sch];
	$scriptblock = $scriptblocks[$sch];

	// 3. Get Appropriate Scripts, and Shaper engine from analysing text and list of available scripts/langsys in font
	//==============================
	// Based on actual script block of text, select shaper (and line-breaking dictionaries)
	if (UCDN::SCRIPT_DEVANAGARI <= $scriptblock && $scriptblock <= UCDN::SCRIPT_MALAYALAM) { $this->shaper = "I"; }	// INDIC shaper
	else if ($scriptblock == UCDN::SCRIPT_ARABIC || $scriptblock == UCDN::SCRIPT_SYRIAC) { $this->shaper = "A"; }	// ARABIC shaper
	else if ($scriptblock == UCDN::SCRIPT_NKO || $scriptblock == UCDN::SCRIPT_MANDAIC) { $this->shaper = "A"; }	// ARABIC shaper
	else if ($scriptblock == UCDN::SCRIPT_KHMER) { $this->shaper = "K"; }	// KHMER shaper
	else if ($scriptblock == UCDN::SCRIPT_THAI) { $this->shaper = "T"; }	// THAI shaper
	else if ($scriptblock == UCDN::SCRIPT_LAO) { $this->shaper = "L"; }	// LAO shaper
	else if ($scriptblock == UCDN::SCRIPT_SINHALA) { $this->shaper = "S"; }	// SINHALA shaper
	else if ($scriptblock == UCDN::SCRIPT_MYANMAR) { $this->shaper = "M"; }	// MYANMAR shaper
	else if ($scriptblock == UCDN::SCRIPT_NEW_TAI_LUE) { $this->shaper = "E"; }	// SEA South East Asian shaper
	else if ($scriptblock == UCDN::SCRIPT_CHAM) { $this->shaper = "E"; }		// SEA South East Asian shaper
	else if ($scriptblock == UCDN::SCRIPT_TAI_THAM) { $this->shaper = "E"; }	// SEA South East Asian shaper
	else $this->shaper = "";
	// Get scripttag based on actual text script
	$scripttag = UCDN::$uni_scriptblock[$scriptblock];

	$GSUBscriptTag = '';
	$GSUBlangsys = '';
	$GPOSscriptTag = '';
	$GPOSlangsys = '';
	$is_old_spec = false;

	$ScriptLang = $this->mpdf->CurrentFont['GSUBScriptLang'];
	if (count($ScriptLang)) { 
		list($GSUBscriptTag,$is_old_spec) = $this->_getOTLscriptTag($ScriptLang, $scripttag, $scriptblock, $this->shaper, $useOTL, 'GSUB');
		if ($this->mpdf->fontLanguageOverride && strpos($ScriptLang[$GSUBscriptTag], $this->mpdf->fontLanguageOverride)!==false) {
			$GSUBlangsys = str_pad($this->mpdf->fontLanguageOverride,4);
		}
		else if ($GSUBscriptTag && isset($ScriptLang[$GSUBscriptTag]) && $ScriptLang[$GSUBscriptTag]!='') { 
			$GSUBlangsys = $this->_getOTLLangTag($this->mpdf->currentLang, $ScriptLang[$GSUBscriptTag]);
		}
	}
	$ScriptLang = $this->mpdf->CurrentFont['GPOSScriptLang'];

	// NB If after GSUB, the same script/lang exist for GPOS, just use these...
	if ($GSUBscriptTag && $GSUBlangsys && isset($ScriptLang[$GSUBscriptTag]) && strpos($ScriptLang[$GSUBscriptTag], $GSUBlangsys)!==false) {
		$GPOSlangsys = $GSUBlangsys;
		$GPOSscriptTag = $GSUBscriptTag;
	}

	// else repeat for GPOS
	// [Font XBRiyaz has GSUB tables for latn, but not GPOS for latn]
	else if (count($ScriptLang)) {
		list($GPOSscriptTag,$dummy) = $this->_getOTLscriptTag($ScriptLang, $scripttag, $scriptblock, $this->shaper, $useOTL, 'GPOS');
		if ($GPOSscriptTag && $this->mpdf->fontLanguageOverride && strpos($ScriptLang[$GPOSscriptTag], $this->mpdf->fontLanguageOverride)!==false) {
			$GPOSlangsys = str_pad($this->mpdf->fontLanguageOverride,4);
		}
		else if ($GPOSscriptTag && isset($ScriptLang[$GPOSscriptTag]) && $ScriptLang[$GPOSscriptTag]!='') { 
			$GPOSlangsys = $this->_getOTLLangTag($this->mpdf->currentLang, $ScriptLang[$GPOSscriptTag]);
		}
	}

	////////////////////////////////////////////////////////////////
	// This is just for the font_dump_OTL utility to set script and langsys override
	if (isset($this->mpdf->overrideOTLsettings) && isset($this->mpdf->overrideOTLsettings[$this->fontkey])) {
		$GSUBscriptTag = $GPOSscriptTag = $this->mpdf->overrideOTLsettings[$this->fontkey]['script'];
		$GSUBlangsys = $GPOSlangsys = $this->mpdf->overrideOTLsettings[$this->fontkey]['lang'];
	}
	////////////////////////////////////////////////////////////////

	if (!$GSUBscriptTag && !$GSUBlangsys && !$GPOSscriptTag && !$GPOSlangsys) {
		// Remove ZWJ and ZWNJ
		for ($i=0;$i<count($this->OTLdata);$i++) {
			if ($this->OTLdata[$i]['uni']==8204 || $this->OTLdata[$i]['uni']==8205) {
				array_splice($this->OTLdata, $i, 1);
			}
		}
		$this->schOTLdata[$sch] = $this->OTLdata;
		$this->OTLdata = array();
		continue; 
	}

	// Don't use MYANMAR shaper unless using v2 scripttag
	if ($this->shaper == 'M' && $GSUBscriptTag != 'mym2') { $this->shaper = ''; }

	$GSUBFeatures = (isset($this->mpdf->CurrentFont['GSUBFeatures'][$GSUBscriptTag][$GSUBlangsys]) ? $this->mpdf->CurrentFont['GSUBFeatures'][$GSUBscriptTag][$GSUBlangsys] : false);
	$GPOSFeatures = (isset($this->mpdf->CurrentFont['GPOSFeatures'][$GPOSscriptTag][$GPOSlangsys]) ? $this->mpdf->CurrentFont['GPOSFeatures'][$GPOSscriptTag][$GPOSlangsys] : false);

	$this->assocLigs = array();	// Ligatures[$posarr lpos] => nc
	$this->assocMarks = array(); 	// assocMarks[$posarr mpos] => array(compID, ligPos)

	if (!isset($this->GDEFdata[$this->fontkey]['GSUBGPOStables'])) {
		$this->ttfOTLdata = $this->GDEFdata[$this->fontkey]['GSUBGPOStables'] = file_get_contents(_MPDF_TTFONTDATAPATH.$this->fontkey.'.GSUBGPOStables.dat','rb') or die('Can\'t open file ' . _MPDF_TTFONTDATAPATH.$this->fontkey.'.GSUBGPOStables.dat');
	}
	else {
		$this->ttfOTLdata = $this->GDEFdata[$this->fontkey]['GSUBGPOStables'];
	}


	if ($this->debugOTL) { $this->_dumpproc('BEGIN', '-', '-', '-', '-', -1, '-', 0); }


////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////
/////////  LINE BREAKING FOR KHMER, THAI + LAO /////////////////
////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////
	// Insert U+200B at word boundaries using dictionaries
	if ($this->mpdf->useDictionaryLBR && ($this->shaper == "K" || $this->shaper == "T" || $this->shaper == "L")) {
		// Sets $this->OTLdata[$i]['wordend']=true at possible end of word boundaries
		$this->SEAlineBreaking();
	}
	// Insert U+200B at word boundaries for Tibetan
	else if ($this->mpdf->useTibetanLBR && $scriptblock == UCDN::SCRIPT_TIBETAN ) {
		// Sets $this->OTLdata[$i]['wordend']=true at possible end of word boundaries
		$this->TibetanlineBreaking();
	}
////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////
//////////       GSUB          /////////////////////////////////
////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////
  if (($useOTL & 0xFF) && $GSUBscriptTag && $GSUBlangsys && $GSUBFeatures) {

	// 4. Load GSUB data, Coverage & Lookups
	//=================================================================

	$this->GSUBfont = $this->fontkey.'.GSUB.'.$GSUBscriptTag.'.'.$GSUBlangsys;

	if (!isset($this->GSUBdata[$this->GSUBfont])) {
		if (file_exists(_MPDF_TTFONTDATAPATH.$this->mpdf->CurrentFont['fontkey'].'.GSUB.'.$GSUBscriptTag.'.'.$GSUBlangsys.'.php')) {
			include_once(_MPDF_TTFONTDATAPATH.$this->mpdf->CurrentFont['fontkey'].'.GSUB.'.$GSUBscriptTag.'.'.$GSUBlangsys.'.php'); 
			$this->GSUBdata[$this->GSUBfont]['rtlSUB'] = $rtlSUB;
			$this->GSUBdata[$this->GSUBfont]['finals'] = $finals;
			if ($this->shaper=='I') {
				$this->GSUBdata[$this->GSUBfont]['rphf'] = $rphf;
				$this->GSUBdata[$this->GSUBfont]['half'] = $half;
				$this->GSUBdata[$this->GSUBfont]['pref'] = $pref;
				$this->GSUBdata[$this->GSUBfont]['blwf'] = $blwf;
				$this->GSUBdata[$this->GSUBfont]['pstf'] = $pstf;
			}
		}
		else { $this->GSUBdata[$this->GSUBfont] = array('rtlSUB'=>array(), 'rphf'=>array(), 'rphf'=>array(), 
			'pref'=>array(), 'blwf'=>array(), 'pstf'=>array(), 'finals'=>''
			);
		}
	}

	if (!isset($this->GSUBdata[$this->fontkey])) {
		include(_MPDF_TTFONTDATAPATH.$this->fontkey.'.GSUBdata.php'); 
		$this->GSLuCoverage = $this->GSUBdata[$this->fontkey]['GSLuCoverage'] = $GSLuCoverage;
	}
	else {
		$this->GSLuCoverage = $this->GSUBdata[$this->fontkey]['GSLuCoverage'];
	}

	$this->GSUBLookups = $this->mpdf->CurrentFont['GSUBLookups'];


	// 5(A). GSUB - Shaper - ARABIC
	//==============================
	if ($this->shaper == 'A') {
		//-----------------------------------------------------------------------------------
		// a. Apply initial GSUB Lookups (in order specified in lookup list but only selecting from certain tags)
		//-----------------------------------------------------------------------------------
		$tags = 'locl ccmp';
		$omittags = '';
		$usetags = $tags;
		if(!empty($this->mpdf->OTLtags)) { 
			$usetags = $this->_applyTagSettings($tags, $GSUBFeatures, $omittags, true) ;
		}
		$this->_applyGSUBrules($usetags, $GSUBscriptTag, $GSUBlangsys);

		//-----------------------------------------------------------------------------------
		// b. Apply context-specific forms GSUB Lookups (initial, isolated, medial, final)
		//-----------------------------------------------------------------------------------
		// Arab and Syriac are the only scripts requiring the special joining - which takes the place of
		// isol fina medi init rules in GSUB (+ fin2 fin3 med2 in Syriac syrc)
		$tags = 'isol fina fin2 fin3 medi med2 init';
		$omittags = '';
		$usetags = $tags;
		if(!empty($this->mpdf->OTLtags)) { 
			$usetags = $this->_applyTagSettings($tags, $GSUBFeatures, $omittags, true) ;
		}

		$this->arabGlyphs = $this->GSUBdata[$this->GSUBfont]['rtlSUB'];

		$gcms = explode("| ",$this->GlyphClassMarks);
		$gcm = array();
		foreach($gcms AS $g) { $gcm[hexdec($g)] = 1; }
		$this->arabTransparentJoin = $this->arabTransparent + $gcm;
		$this->arabic_shaper($usetags, $GSUBscriptTag);

		//-----------------------------------------------------------------------------------
		// c. Set Kashida points (after joining occurred - medi, fina, init) but before other substitutions
		//-----------------------------------------------------------------------------------
		//if ($scriptblock == UCDN::SCRIPT_ARABIC ) {
		for ($i=0;$i<count($this->OTLdata);$i++) {
			// Put the kashida marker on the character BEFORE which is inserted the kashida
			// Kashida marker is inverse of priority i.e. Priority 1 => 7, Priority 7 => 1.

			// Priority 1	User-inserted Kashida 0640 = Tatweel
			// The user entered a Kashida in a position
			// Position: Before the user-inserted kashida
			if ($this->OTLdata[$i]['uni']==0x0640) {
				$this->OTLdata[$i]['GPOSinfo']['kashida'] = 8; // Put before the next character
			}

			// Priority 2	Seen (0633)  FEB3, FEB4; Sad (0635)  FEBB, FEBC
			// Initial or medial form
			// Connecting to the next character
			// Position: After the character
			else if ($this->OTLdata[$i]['uni']==0xFEB3 || $this->OTLdata[$i]['uni']==0xFEB4 || $this->OTLdata[$i]['uni']==0xFEBB || $this->OTLdata[$i]['uni']==0xFEBC) {
				$checkpos = $i+1;
				while (isset($this->OTLdata[$checkpos]) && strpos($this->GlyphClassMarks, $this->OTLdata[$checkpos]['hex'])!==false) {
					$checkpos++; 
				}
				if (isset($this->OTLdata[$checkpos])) {
					$this->OTLdata[$checkpos]['GPOSinfo']['kashida'] = 7; // Put after marks on next character
				}
			}

			// Priority 3	Taa Marbutah (0629) FE94; Haa (062D) FEA2; Dal (062F) FEAA
			// Final form
			// Connecting to previous character
			// Position: Before the character
			else if ($this->OTLdata[$i]['uni']==0xFE94 || $this->OTLdata[$i]['uni']==0xFEA2 || $this->OTLdata[$i]['uni']==0xFEAA) {
				$this->OTLdata[$i]['GPOSinfo']['kashida'] = 6;
			}

			// Priority 4	Alef (0627) FE8E; Tah (0637) FEC2; Lam (0644) FEDE; Kaf (0643)  FEDA; Gaf (06AF) FB93
			// Final form
			// Connecting to previous character
			// Position: Before the character
			else if ($this->OTLdata[$i]['uni']==0xFE8E || $this->OTLdata[$i]['uni']==0xFEC2 || $this->OTLdata[$i]['uni']==0xFEDE || $this->OTLdata[$i]['uni']==0xFEDA || $this->OTLdata[$i]['uni']==0xFB93) {
				$this->OTLdata[$i]['GPOSinfo']['kashida'] = 5;
			}

			// Priority 5	RA (0631) FEAE; Ya (064A)  FEF2 FEF4; Alef Maqsurah (0649) FEF0 FBE9
			// Final or Medial form
			// Connected to preceding medial BAA (0628) = FE92
			// Position: Before preceding medial Baa
			// Although not mentioned in spec, added Farsi Yeh (06CC) FBFD FBFF; equivalent to 064A or 0649
			else if ($this->OTLdata[$i]['uni']==0xFEAE || $this->OTLdata[$i]['uni']==0xFEF2 || $this->OTLdata[$i]['uni']==0xFEF0
				|| $this->OTLdata[$i]['uni']==0xFEF4 || $this->OTLdata[$i]['uni']==0xFBE9
				|| $this->OTLdata[$i]['uni']==0xFBFD || $this->OTLdata[$i]['uni']==0xFBFF
				) {
				$checkpos = $i-1;
				while (isset($this->OTLdata[$checkpos]) && strpos($this->GlyphClassMarks, $this->OTLdata[$checkpos]['hex'])!==false) {
					$checkpos--; 
				}
				if (isset($this->OTLdata[$checkpos]) && $this->OTLdata[$checkpos]['uni']==0xFE92) {
					$this->OTLdata[$checkpos]['GPOSinfo']['kashida'] = 4;	// ******* Before preceding BAA
				}
			}

			// Priority 6	WAW (0648) FEEE; Ain (0639) FECA; Qaf (0642) FED6; Fa (0641) FED2
			// Final form
			// Connecting to previous character
			// Position: Before the character
			else if ($this->OTLdata[$i]['uni']==0xFEEE || $this->OTLdata[$i]['uni']==0xFECA || $this->OTLdata[$i]['uni']==0xFED6 || $this->OTLdata[$i]['uni']==0xFED2) {
				$this->OTLdata[$i]['GPOSinfo']['kashida'] = 3;
			}

			// Priority 7	Other connecting characters
			// Final form
			// Connecting to previous character
			// Position: Before the character
			/* This isn't in the spec, but using MS WORD as a basis, give a lower priority to the 3 characters already checked 
			   in (5) above. Test case: 
			   &#x62e;&#x652;&#x631;&#x64e;&#x649;&#x670;
			   &#x641;&#x64e;&#x62a;&#x64f;&#x630;&#x64e;&#x643;&#x651;&#x650;&#x631;
			*/

			if (!isset($this->OTLdata[$i]['GPOSinfo']['kashida'])) {
				if (strpos($this->GSUBdata[$this->GSUBfont]['finals'], $this->OTLdata[$i]['hex'])!==false) {	// ANY OTHER FINAL FORM
					$this->OTLdata[$i]['GPOSinfo']['kashida'] = 2;
				}
				else if (strpos('0FEAE 0FEF0 0FEF2',$this->OTLdata[$i]['hex'])!==false) {	// not already included in 5 above
					$this->OTLdata[$i]['GPOSinfo']['kashida'] = 1;
				}
			}
		}

		//-----------------------------------------------------------------------------------
		// d. Apply Presentation Forms GSUB Lookups (+ any discretionary) - Apply one at a time in Feature order
		//-----------------------------------------------------------------------------------
		$tags = 'rlig calt liga clig mset';

		$omittags = 'locl ccmp nukt akhn rphf rkrf pref blwf abvf half pstf cfar vatu cjct init medi fina isol med2 fin2 fin3 ljmo vjmo tjmo';
		$usetags = $tags;
		if(!empty($this->mpdf->OTLtags)) { 
			$usetags = $this->_applyTagSettings($tags, $GSUBFeatures, $omittags, false) ;
		}

		$ts = explode(' ',$usetags);
		foreach($ts AS $ut) {	//  - Apply one at a time in Feature order
			$this->_applyGSUBrules($ut, $GSUBscriptTag, $GSUBlangsys);
		}
		//-----------------------------------------------------------------------------------
		// e. NOT IN SPEC
		// If space precedes a mark -> substitute a &nbsp; before the Mark, to prevent line breaking Test: 
		//-----------------------------------------------------------------------------------
		for($ptr=1; $ptr<count($this->OTLdata); $ptr++) {
			if ($this->OTLdata[$ptr]['general_category'] == UCDN::UNICODE_GENERAL_CATEGORY_NON_SPACING_MARK && $this->OTLdata[$ptr-1]['uni'] == 32) {
				$this->OTLdata[$ptr-1]['uni'] =  0xa0;
				$this->OTLdata[$ptr-1]['hex'] =  '000A0';
			}
		}
	}

	// 5(I). GSUB - Shaper - INDIC and SINHALA and KHMER
	//===================================
	else if ($this->shaper == 'I' || $this->shaper == 'K' || $this->shaper == 'S') {
		$this->restrictToSyllable = true;
		//-----------------------------------------------------------------------------------
		// a. First decompose/compose split mattras
		// (normalize) ??????? Nukta/Halant order etc ??????????????????????????????????????????????????????????????????????????
		//-----------------------------------------------------------------------------------
		for($ptr=0; $ptr<count($this->OTLdata); $ptr++) {
			$char = $this->OTLdata[$ptr]['uni'];
			$sub = INDIC::decompose_indic($char);
			if ($sub) {
				$newinfo = array();
				for($i=0;$i<count($sub);$i++) {
					$newinfo[$i] = array();
					$ucd_record = UCDN::get_ucd_record($sub[$i]);
					$newinfo[$i]['general_category'] = $ucd_record[0];
					$newinfo[$i]['bidi_type'] = $ucd_record[2];
					$charasstr = $this->unicode_hex($sub[$i]); 
					if (strpos($this->GlyphClassMarks, $charasstr)!==false) { $newinfo[$i]['group'] =  'M'; }
					else { $newinfo[$i]['group'] =  'C'; }
					$newinfo[$i]['uni'] =  $sub[$i];
					$newinfo[$i]['hex'] =  $charasstr;
				}
				array_splice($this->OTLdata, $ptr, 1, $newinfo);
				$ptr += count($sub)-1;
			}
			/* Only Composition-exclusion exceptions that we want to recompose. */
			if ($this->shaper == 'I') {
			  if ($char == 0x09AF && isset($this->OTLdata[$ptr + 1]) && $this->OTLdata[$ptr + 1]['uni'] == 0x09BC) { 
				$sub = 0x09DF; 
				$newinfo = array();
				$newinfo[0] = array();
				$ucd_record = UCDN::get_ucd_record($sub);
				$newinfo[0]['general_category'] = $ucd_record[0];
				$newinfo[0]['bidi_type'] = $ucd_record[2];
				$newinfo[0]['group'] =  'C';
				$newinfo[0]['uni'] =  $sub;
				$newinfo[0]['hex'] =  $this->unicode_hex($sub);
				array_splice($this->OTLdata, $ptr, 2, $newinfo);
			  }
			}
		}
		//-----------------------------------------------------------------------------------
		// b. Analyse characters - group as syllables/clusters (Indic); invalid diacritics; add dotted circle
		//-----------------------------------------------------------------------------------
		$indic_category_string = '';
		foreach($this->OTLdata AS $eid=>$c) {
			INDIC::set_indic_properties($this->OTLdata[$eid], $scriptblock );	// sets ['indic_category'] and ['indic_position']
			//$c['general_category']
			//$c['combining_class']
			//$c['uni'] =  $char;

			$indic_category_string .= INDIC::$indic_category_char[$this->OTLdata[$eid]['indic_category']];
		}

		$broken_syllables = false;
		if ($this->shaper == 'I') {
			INDIC::set_syllables($this->OTLdata, $indic_category_string, $broken_syllables);
		}
		else if ($this->shaper == 'S') {
			INDIC::set_syllables_sinhala($this->OTLdata, $indic_category_string, $broken_syllables);
		}
		else if ($this->shaper == 'K') {
			INDIC::set_syllables_khmer($this->OTLdata, $indic_category_string, $broken_syllables);
		}
		$indic_category_string = '';

		//-----------------------------------------------------------------------------------
		// c. Initial Re-ordering (Indic / Khmer / Sinhala)
		//-----------------------------------------------------------------------------------
		// Find base consonant
		// Decompose/compose and reorder Matras
		// Reorder marks to canonical order

		$indic_config = INDIC::$indic_configs[$scriptblock];
		$dottedcircle = false; 
		if ($broken_syllables) { 
			if ($this->mpdf->_charDefined($this->mpdf->fonts[$this->fontkey]['cw'],0x25CC) ) { 
				$dottedcircle = array();
				$ucd_record = UCDN::get_ucd_record(0x25CC);
				$dottedcircle[0]['general_category'] = $ucd_record[0];
				$dottedcircle[0]['bidi_type'] = $ucd_record[2];
				$dottedcircle[0]['group'] =  'C';
				$dottedcircle[0]['uni'] =  0x25CC;
				$dottedcircle[0]['indic_category'] = INDIC::OT_DOTTEDCIRCLE;
				$dottedcircle[0]['indic_position'] = INDIC::POS_BASE_C;

				$dottedcircle[0]['hex'] =  '025CC';		// TEMPORARY *****
			}
		}
		INDIC::initial_reordering($this->OTLdata, $this->GSUBdata[$this->GSUBfont], $broken_syllables, $indic_config, $scriptblock, $is_old_spec, $dottedcircle);

		//-----------------------------------------------------------------------------------
		// d. Apply initial and basic shaping forms GSUB Lookups (one at a time)
		//-----------------------------------------------------------------------------------
		if ($this->shaper == 'I' || $this->shaper == 'S') {
			$tags = 'locl ccmp nukt akhn rphf rkrf pref blwf half pstf vatu cjct';
		}
		else if ($this->shaper == 'K') {
			$tags = 'locl ccmp pref blwf abvf pstf cfar';
		}
		$this->_applyGSUBrulesIndic($tags, $GSUBscriptTag, $GSUBlangsys, $is_old_spec);

		//-----------------------------------------------------------------------------------
		// e. Final Re-ordering (Indic / Khmer / Sinhala)
		//-----------------------------------------------------------------------------------
		// Reorder matras
		// Reorder reph
		// Reorder pre-base reordering consonants: 

		INDIC::final_reordering($this->OTLdata, $this->GSUBdata[$this->GSUBfont], $indic_config, $scriptblock, $is_old_spec);

		//-----------------------------------------------------------------------------------
		// f. Apply 'init' feature to first syllable in word (indicated by ['mask']) INDIC::FLAG(INDIC::INIT);
		//-----------------------------------------------------------------------------------
		if ($this->shaper == 'I' || $this->shaper == 'S') {
			$tags = 'init';
			$this->_applyGSUBrulesIndic($tags, $GSUBscriptTag, $GSUBlangsys, $is_old_spec);
		}

		//-----------------------------------------------------------------------------------
		// g. Apply Presentation Forms GSUB Lookups (+ any discretionary)
		//-----------------------------------------------------------------------------------
		$tags = 'pres abvs blws psts haln rlig calt liga clig mset';

		$omittags = 'locl ccmp nukt akhn rphf rkrf pref blwf abvf half pstf cfar vatu cjct init medi fina isol med2 fin2 fin3 ljmo vjmo tjmo';
		$usetags = $tags;
		if(!empty($this->mpdf->OTLtags)) { 
			$usetags = $this->_applyTagSettings($tags, $GSUBFeatures, $omittags, false) ;
		}
		if ($this->shaper == 'K') { 	// Features are applied one at a time, working through each codepoint
			$this->_applyGSUBrulesSingly($usetags, $GSUBscriptTag, $GSUBlangsys);
		}
		else {
			$this->_applyGSUBrules($usetags, $GSUBscriptTag, $GSUBlangsys);
		}
		$this->restrictToSyllable = false;
	}


	// 5(M). GSUB - Shaper - MYANMAR (ONLY mym2)
	//==============================
	// NB Old style 'mymr' is left to go through the default shaper
	else if ($this->shaper == 'M') {
		$this->restrictToSyllable = true;
		//-----------------------------------------------------------------------------------
		// a. Analyse characters - group as syllables/clusters (Myanmar); invalid diacritics; add dotted circle
		//-----------------------------------------------------------------------------------
		$myanmar_category_string = '';
		foreach($this->OTLdata AS $eid=>$c) {
			MYANMAR::set_myanmar_properties($this->OTLdata[$eid]);	// sets ['myanmar_category'] and ['myanmar_position']
			$myanmar_category_string .= MYANMAR::$myanmar_category_char[$this->OTLdata[$eid]['myanmar_category']];
		}
		$broken_syllables = false;
		MYANMAR::set_syllables($this->OTLdata, $myanmar_category_string, $broken_syllables);
		$myanmar_category_string = '';

		//-----------------------------------------------------------------------------------
		// b. Re-ordering (Myanmar mym2)
		//-----------------------------------------------------------------------------------
		$dottedcircle = false; 
		if ($broken_syllables) { 
			if ($this->mpdf->_charDefined($this->mpdf->fonts[$this->fontkey]['cw'],0x25CC) ) { 
				$dottedcircle = array();
				$ucd_record = UCDN::get_ucd_record(0x25CC);
				$dottedcircle[0]['general_category'] = $ucd_record[0];
				$dottedcircle[0]['bidi_type'] = $ucd_record[2];
				$dottedcircle[0]['group'] =  'C';
				$dottedcircle[0]['uni'] =  0x25CC;
				$dottedcircle[0]['myanmar_category'] = MYANMAR::OT_DOTTEDCIRCLE;
				$dottedcircle[0]['myanmar_position'] = MYANMAR::POS_BASE_C;
				$dottedcircle[0]['hex'] =  '025CC';
			}
		}
		MYANMAR::reordering($this->OTLdata, $this->GSUBdata[$this->GSUBfont], $broken_syllables, $dottedcircle);

		//-----------------------------------------------------------------------------------
		// c. Apply initial and basic shaping forms GSUB Lookups (one at a time)
		//-----------------------------------------------------------------------------------

		$tags = 'locl ccmp rphf pref blwf pstf';
		$this->_applyGSUBrulesMyanmar($tags, $GSUBscriptTag, $GSUBlangsys);

		//-----------------------------------------------------------------------------------
		// d. Apply Presentation Forms GSUB Lookups (+ any discretionary)
		//-----------------------------------------------------------------------------------
		$tags = 'pres abvs blws psts haln rlig calt liga clig mset';
		$omittags = 'locl ccmp nukt akhn rphf rkrf pref blwf abvf half pstf cfar vatu cjct init medi fina isol med2 fin2 fin3 ljmo vjmo tjmo';
		$usetags = $tags;
		if(!empty($this->mpdf->OTLtags)) { 
			$usetags = $this->_applyTagSettings($tags, $GSUBFeatures, $omittags, false) ;
		}
		$this->_applyGSUBrules($usetags, $GSUBscriptTag, $GSUBlangsys);
		$this->restrictToSyllable = false;
	}


	// 5(E). GSUB - Shaper - SEA South East Asian (New Tai Lue, Cham, Tai Tam)
	//==============================
	else if ($this->shaper == 'E') {
      /* HarfBuzz says: If the designer designed the font for the 'DFLT' script,
       * use the default shaper.  Otherwise, use the SEA shaper.
       * Note that for some simple scripts, there may not be *any*
       * GSUB/GPOS needed, so there may be no scripts found! */

		$this->restrictToSyllable = true;
		//-----------------------------------------------------------------------------------
		// a. Analyse characters - group as syllables/clusters (Indic); invalid diacritics; add dotted circle
		//-----------------------------------------------------------------------------------
		$sea_category_string = '';
		foreach($this->OTLdata AS $eid=>$c) {
			SEA::set_sea_properties($this->OTLdata[$eid], $scriptblock );	// sets ['sea_category'] and ['sea_position']
			//$c['general_category']
			//$c['combining_class']
			//$c['uni'] =  $char;

			$sea_category_string .= SEA::$sea_category_char[$this->OTLdata[$eid]['sea_category']];
		}

		$broken_syllables = false;
		SEA::set_syllables($this->OTLdata, $sea_category_string, $broken_syllables);
		$sea_category_string = '';

		//-----------------------------------------------------------------------------------
		// b. Apply locl and ccmp shaping forms - before initial re-ordering; GSUB Lookups (one at a time)
		//-----------------------------------------------------------------------------------
		$tags = 'locl ccmp';
		$this->_applyGSUBrulesSingly($tags, $GSUBscriptTag, $GSUBlangsys);

		//-----------------------------------------------------------------------------------
		// c. Initial Re-ordering
		//-----------------------------------------------------------------------------------
		// Find base consonant
		// Decompose/compose and reorder Matras
		// Reorder marks to canonical order

		$dottedcircle = false; 
		if ($broken_syllables) { 
			if ($this->mpdf->_charDefined($this->mpdf->fonts[$this->fontkey]['cw'],0x25CC) ) { 
				$dottedcircle = array();
				$ucd_record = UCDN::get_ucd_record(0x25CC);
				$dottedcircle[0]['general_category'] = $ucd_record[0];
				$dottedcircle[0]['bidi_type'] = $ucd_record[2];
				$dottedcircle[0]['group'] =  'C';
				$dottedcircle[0]['uni'] =  0x25CC;
				$dottedcircle[0]['sea_category'] = SEA::OT_GB;
				$dottedcircle[0]['sea_position'] = SEA::POS_BASE_C;

				$dottedcircle[0]['hex'] =  '025CC';		// TEMPORARY *****
			}
		}
		SEA::initial_reordering($this->OTLdata, $this->GSUBdata[$this->GSUBfont], $broken_syllables, $scriptblock, $dottedcircle);

		//-----------------------------------------------------------------------------------
		// d. Apply basic shaping forms GSUB Lookups (one at a time)
		//-----------------------------------------------------------------------------------
		$tags = 'pref abvf blwf pstf';
		$this->_applyGSUBrulesSingly($tags, $GSUBscriptTag, $GSUBlangsys);

		//-----------------------------------------------------------------------------------
		// e. Final Re-ordering
		//-----------------------------------------------------------------------------------

		SEA::final_reordering($this->OTLdata, $this->GSUBdata[$this->GSUBfont], $scriptblock);

		//-----------------------------------------------------------------------------------
		// f. Apply Presentation Forms GSUB Lookups (+ any discretionary)
		//-----------------------------------------------------------------------------------
		$tags = 'pres abvs blws psts';

		$omittags = 'locl ccmp nukt akhn rphf rkrf pref blwf abvf half pstf cfar vatu cjct init medi fina isol med2 fin2 fin3 ljmo vjmo tjmo';
		$usetags = $tags;
		if(!empty($this->mpdf->OTLtags)) { 
			$usetags = $this->_applyTagSettings($tags, $GSUBFeatures, $omittags, false) ;
		}
		$this->_applyGSUBrules($usetags, $GSUBscriptTag, $GSUBlangsys);
		$this->restrictToSyllable = false;
	}


	// 5(D). GSUB - Shaper - DEFAULT (including THAI and LAO and MYANMAR v1 [mymr] and TIBETAN)
	//==============================
	else {	// DEFAULT
		//-----------------------------------------------------------------------------------
		// a. First decompose/compose in Thai / Lao - Tibetan
		//-----------------------------------------------------------------------------------
		// Decomposition for THAI or LAO
		/* This function implements the shaping logic documented here:
		 *
		 *   http://linux.thai.net/~thep/th-otf/shaping.html
		 *
		 * The first shaping rule listed there is needed even if the font has Thai
		 * OpenType tables. 
		 *
		 *
		 * The following is NOT specified in the MS OT Thai spec, however, it seems
		 * to be what Uniscribe and other engines implement.  According to Eric Muller:
		 *
		 * When you have a SARA AM, decompose it in NIKHAHIT + SARA AA, *and* move the
		 * NIKHAHIT backwards over any tone mark (0E48-0E4B).
		 *
		 * <0E14, 0E4B, 0E33> -> <0E14, 0E4D, 0E4B, 0E32>
		 *
		 * This reordering is legit only when the NIKHAHIT comes from a SARA AM, not
		 * when it's there to start with. The string <0E14, 0E4B, 0E4D> is probably
		 * not what a user wanted, but the rendering is nevertheless nikhahit above
		 * chattawa.
		 *
		 * Same for Lao.
		 *
		 *			Thai		Lao
		 * SARA AM:		U+0E33	U+0EB3
		 * SARA AA:		U+0E32	U+0EB2
		 * Nikhahit:	U+0E4D	U+0ECD
		 *
		 * Testing shows that Uniscribe reorder the following marks:
		 * Thai:	<0E31,0E34..0E37,0E47..0E4E>
		 * Lao:	<0EB1,0EB4..0EB7,0EC7..0ECE>
		 *
		 * Lao versions are the same as Thai + 0x80.
		 */
		if ($this->shaper == 'T' || $this->shaper == 'L') {
			for($ptr=0; $ptr<count($this->OTLdata); $ptr++) {
				$char = $this->OTLdata[$ptr]['uni'];
    				if (($char & ~0x0080) == 0x0E33) {	// if SARA_AM (U+0E33 or U+0EB3)

					$NIKHAHIT = $char + 0x1A;
					$SARA_AA = $char - 1;
					$sub = array($SARA_AA, $NIKHAHIT);

					$newinfo = array();
					$ucd_record = UCDN::get_ucd_record($sub[0]);
					$newinfo[0]['general_category'] = $ucd_record[0];
					$newinfo[0]['bidi_type'] = $ucd_record[2];
					$charasstr = $this->unicode_hex($sub[0]); 
					if (strpos($this->GlyphClassMarks, $charasstr)!==false) { $newinfo[0]['group'] =  'M'; }
					else { $newinfo[0]['group'] =  'C'; }
					$newinfo[0]['uni'] =  $sub[0];
					$newinfo[0]['hex'] =  $charasstr;
					$this->OTLdata[$ptr] = $newinfo[0];	// Substitute SARA_AM => SARA_AA

					$ntones = 0;	// number of (preceding) tone marks
					// IS_TONE_MARK ((x) & ~0x0080, 0x0E34 - 0x0E37, 0x0E47 - 0x0E4E, 0x0E31)
					while (isset($this->OTLdata[$ptr - 1 - $ntones]) 
						&& (
							($this->OTLdata[$ptr - 1 - $ntones]['uni'] & ~0x0080) == 0x0E31 ||

							(($this->OTLdata[$ptr - 1 - $ntones]['uni'] & ~0x0080) >= 0x0E34 &&
							($this->OTLdata[$ptr - 1 - $ntones]['uni'] & ~0x0080) <= 0x0E37) ||

							(($this->OTLdata[$ptr - 1 - $ntones]['uni'] & ~0x0080) >= 0x0E47 &&
							($this->OTLdata[$ptr - 1 - $ntones]['uni'] & ~0x0080) <= 0x0E4E)
						)
					)  { $ntones++; }

					$newinfo = array();
					$ucd_record = UCDN::get_ucd_record($sub[1]);
					$newinfo[0]['general_category'] = $ucd_record[0];
					$newinfo[0]['bidi_type'] = $ucd_record[2];
					$charasstr = $this->unicode_hex($sub[1]); 
					if (strpos($this->GlyphClassMarks, $charasstr)!==false) { $newinfo[0]['group'] =  'M'; }
					else { $newinfo[0]['group'] =  'C'; }
					$newinfo[0]['uni'] =  $sub[1];
					$newinfo[0]['hex'] =  $charasstr;
					// Insert NIKAHIT
					array_splice($this->OTLdata, $ptr - $ntones, 0, $newinfo);

					$ptr++;
				}
			}
		}

		if ($scriptblock == UCDN::SCRIPT_TIBETAN) {
			// =========================
			// Reordering TIBETAN
			// =========================
			// Tibetan does not need to need a shaper generally, as long as characters are presented in the correct order
			// so we will do one minor change here:
           		// From ICU: If the present character is a number, and the next character is a pre-number combining mark
           		 // then the two characters are reordered
			// From MS OTL spec the following are Digit modifiers (Md): 0F180F19, 0F3E0F3F
			// Digits: 0F200F33
			// On testing only 0x0F3F (pre-based mark) seems to need re-ordering
			for($ptr=0; $ptr<count($this->OTLdata)-1; $ptr++) {
    				if (INDIC::in_range($this->OTLdata[$ptr]['uni'], 0x0F20, 0x0F33) && $this->OTLdata[$ptr+1]['uni'] == 0x0F3F ) {
					$tmp = $this->OTLdata[$ptr+1];
					$this->OTLdata[$ptr+1] = $this->OTLdata[$ptr];
					$this->OTLdata[$ptr] = $tmp;
				}
			}


			// =========================
			// Decomposition for TIBETAN
			// =========================
/* Recommended, but does not seem to change anything...
			for($ptr=0; $ptr<count($this->OTLdata); $ptr++) {
				$char = $this->OTLdata[$ptr]['uni'];
				$sub = INDIC::decompose_indic($char);
				if ($sub) {
					$newinfo = array();
					for($i=0;$i<count($sub);$i++) {
						$newinfo[$i] = array();
						$ucd_record = UCDN::get_ucd_record($sub[$i]);
						$newinfo[$i]['general_category'] = $ucd_record[0];
						$newinfo[$i]['bidi_type'] = $ucd_record[2];
						$charasstr = $this->unicode_hex($sub[$i]); 
						if (strpos($this->GlyphClassMarks, $charasstr)!==false) { $newinfo[$i]['group'] =  'M'; }
						else { $newinfo[$i]['group'] =  'C'; }
						$newinfo[$i]['uni'] =  $sub[$i];
						$newinfo[$i]['hex'] =  $charasstr;
					}
					array_splice($this->OTLdata, $ptr, 1, $newinfo);
					$ptr += count($sub)-1;
				}
			}
*/

		}


		//-----------------------------------------------------------------------------------
		// b. Apply all GSUB Lookups (in order specified in lookup list)
		//-----------------------------------------------------------------------------------
		$tags = 'locl ccmp pref blwf abvf pstf pres abvs blws psts haln rlig calt liga clig mset  RQD';
		// pref blwf abvf pstf required for Tibetan
		// " RQD" is a non-standard tag in Garuda font - presumably intended to be used by default ? "ReQuireD"
		// Being a 3 letter tag is non-standard, and does not allow it to be set by font-feature-settings


		/* ?Add these until shapers witten?
		Hangul: 	ljmo vjmo tjmo
		*/

		$omittags = '';
		$useGSUBtags = $tags;
		if(!empty($this->mpdf->OTLtags)) { 
			$useGSUBtags = $this->_applyTagSettings($tags, $GSUBFeatures, $omittags, false) ;
		}
		// APPLY GSUB rules (as long as not Latin + SmallCaps - but not OTL smcp)
		if (!(($this->mpdf->textvar & FC_SMALLCAPS) && $scriptblock == UCDN::SCRIPT_LATIN && strpos($useGSUBtags, 'smcp')===false)) {
			$this->_applyGSUBrules($useGSUBtags, $GSUBscriptTag, $GSUBlangsys);
		}
	}


  }

	// Shapers - KHMER & THAI & LAO - Replace Word boundary marker with U+200B
	// Also TIBETAN (no shaper)
	//=======================================================
	if (($this->shaper == "K" || $this->shaper == "T" || $this->shaper == "L") || $scriptblock == UCDN::SCRIPT_TIBETAN ) {
		// Set up properties to insert a U+200B character
		$newinfo = array();
		//$newinfo[0] = array('general_category' => 1, 'bidi_type' => 14, 'group' => 'S', 'uni' => 0x200B, 'hex' => '0200B');
		$newinfo[0] = array(
			'general_category' => UCDN::UNICODE_GENERAL_CATEGORY_FORMAT, 
			'bidi_type' => UCDN::BIDI_CLASS_BN, 
			'group' => 'S', 'uni' => 0x200B, 'hex' => '0200B');
		// Then insert U+200B at (after) all word end boundaries
		for ($i=count($this->OTLdata)-1;$i>0;$i--) {
			// Make sure after GSUB that wordend has not been moved - check next char is not in the same syllable
			if (isset($this->OTLdata[$i]['wordend']) && $this->OTLdata[$i]['wordend'] &&
					isset($this->OTLdata[$i+1]['uni']) && (!isset($this->OTLdata[$i+1]['syllable']) || !isset($this->OTLdata[$i+1]['syllable']) || $this->OTLdata[$i+1]['syllable']!=$this->OTLdata[$i]['syllable'])) { 
				array_splice($this->OTLdata, $i+1, 0, $newinfo);
				$this->_updateLigatureMarks($i, 1);
			}
			else if ($this->OTLdata[$i]['uni']==0x2e) {	// Word end if Full-stop.
				array_splice($this->OTLdata, $i+1, 0, $newinfo);
				$this->_updateLigatureMarks($i, 1);
			}
		}
	}


	// Shapers - INDIC & ARABIC & KHMER & SINHALA  & MYANMAR - Remove ZWJ and ZWNJ
	//=======================================================
	if ($this->shaper == 'I' || $this->shaper == 'S' || $this->shaper == 'A' || $this->shaper == 'K' || $this->shaper == 'M') {
		// Remove ZWJ and ZWNJ
		for ($i=0;$i<count($this->OTLdata);$i++) {
			if ($this->OTLdata[$i]['uni']==8204 || $this->OTLdata[$i]['uni']==8205) {
				array_splice($this->OTLdata, $i, 1);
				$this->_updateLigatureMarks($i, -1);
			}
		}
	}

//print_r($this->OTLdata); echo '<br />';
//print_r($this->assocMarks);  echo '<br />';
//print_r($this->assocLigs); exit;

////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////
//////////       GPOS          /////////////////////////////////
////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////

  if (($useOTL & 0xFF) && $GPOSscriptTag && $GPOSlangsys && $GPOSFeatures) {
	$this->Entry = array();
	$this->Exit = array();

	// 6. Load GPOS data, Coverage & Lookups
	//=================================================================
	if (!isset($this->GPOSdata[$this->fontkey])) {
		include(_MPDF_TTFONTDATAPATH.$this->mpdf->CurrentFont['fontkey'].'.GPOSdata.php'); 
		$this->LuCoverage = $this->GPOSdata[$this->fontkey]['LuCoverage'] = $LuCoverage;
	}
	else {
		$this->LuCoverage = $this->GPOSdata[$this->fontkey]['LuCoverage'];
	}

	$this->GPOSLookups = $this->mpdf->CurrentFont['GPOSLookups'];


	// 7. Select Feature tags to use (incl optional)
	//==============================
	$tags = 'abvm blwm mark mkmk curs cpsp dist requ';	// Default set
	/* 'requ' is not listed in the Microsoft registry of Feature tags
		Found in Arial Unicode MS, it repositions the baseline for punctuation in Kannada script */

	// ZZZ96
	// Set kern to be included by default in non-Latin script (? just when shapers used)
	// Kern is used in some fonts to reposition marks etc. and is essential for correct display
	//if ($this->shaper) {$tags .= ' kern'; }
	if ($scriptblock != UCDN::SCRIPT_LATIN) { $tags .= ' kern'; }

	$omittags = '';
	$usetags = $tags;
	if(!empty($this->mpdf->OTLtags)) { 
		$usetags = $this->_applyTagSettings($tags, $GPOSFeatures, $omittags, false) ;
	}



	// 8. Get GPOS LookupList from Feature tags
	//==============================
	$LookupList = array();
	foreach($GPOSFeatures AS $tag=>$arr) {
		if (strpos($usetags, $tag)!==false) {
			foreach($arr AS $lu) { $LookupList[$lu] = $tag; }
		}
	}
	ksort($LookupList);


	// 9. Apply GPOS Lookups (in order specified in lookup list but selecting from specified tags)
	//==============================

	// APPLY THE GPOS RULES (as long as not Latin + SmallCaps - but not OTL smcp)
	if (!(($this->mpdf->textvar & FC_SMALLCAPS) && $scriptblock == UCDN::SCRIPT_LATIN && strpos($useGSUBtags, 'smcp')===false)) {
		$this->_applyGPOSrules($LookupList, $is_old_spec);
		// (sets: $this->OTLdata[n]['GPOSinfo'] XPlacement YPlacement XAdvance Entry Exit )
	}

	// 10. Process cursive text
	//==============================
	if (count($this->Entry) || count($this->Exit)) {
		// RTL
		$incurs = false;
		for ($i=(count($this->OTLdata)-1);$i>=0;$i--) {
			if (isset($this->Entry[$i]) && isset($this->Entry[$i]['Y']) && $this->Entry[$i]['dir']=='RTL') {
				$nextbase = $i-1;	// Set as next base ignoring marks (next base reading RTL in logical oder
				while(isset($this->OTLdata[$nextbase]['hex']) && strpos($this->GlyphClassMarks, $this->OTLdata[$nextbase]['hex'])!==false) { $nextbase--; }
				if (isset($this->Exit[$nextbase]) && isset($this->Exit[$nextbase]['Y']) ) {
					$diff = $this->Entry[$i]['Y'] - $this->Exit[$nextbase]['Y'];
					if ($incurs===false) { $incurs = $diff; }
					else { $incurs += $diff; }
					for ($j=($i-1);$j>=$nextbase;$j--) {
						if (isset($this->OTLdata[$j]['GPOSinfo']['YPlacement'])) { $this->OTLdata[$j]['GPOSinfo']['YPlacement'] += $incurs; }
						else { $this->OTLdata[$j]['GPOSinfo']['YPlacement'] = $incurs; }
					}
					if (isset($this->Exit[$i]['X']) && isset($this->Entry[$nextbase]['X']) ) {
						$adj = -($this->Entry[$i]['X'] - $this->Exit[$nextbase]['X']);
						// If XAdvance is aplied - in order for PDF to position the Advance correctly need to place it on:
						// in RTL - the current glyph or the last of any associated marks
						if (isset($this->OTLdata[$nextbase+1]['GPOSinfo']['XAdvance'])) { $this->OTLdata[$nextbase+1]['GPOSinfo']['XAdvance'] += $adj; }
						else { $this->OTLdata[$nextbase+1]['GPOSinfo']['XAdvance'] = $adj; }
					}
				}
				else { $incurs = false; }
			}
			else if (strpos($this->GlyphClassMarks, $this->OTLdata[$i]['hex'])!==false) { continue; } // ignore Marks
			else { $incurs = false; }
		}
		// LTR
		$incurs = false;
		for ($i=0;$i<count($this->OTLdata);$i++) {
			if (isset($this->Exit[$i]) && isset($this->Exit[$i]['Y']) && $this->Exit[$i]['dir']=='LTR') {
				$nextbase = $i+1;	// Set as next base ignoring marks
				while(strpos($this->GlyphClassMarks, $this->OTLdata[$nextbase]['hex'])!==false) { $nextbase++; }
				if (isset($this->Entry[$nextbase]) && isset($this->Entry[$nextbase]['Y']) ) {

					$diff = $this->Exit[$i]['Y'] - $this->Entry[$nextbase]['Y'];
					if ($incurs===false) { $incurs = $diff; }
					else { $incurs += $diff; }
					for ($j=($i+1);$j<=$nextbase;$j++) {
						if (isset($this->OTLdata[$j]['GPOSinfo']['YPlacement'])) { $this->OTLdata[$j]['GPOSinfo']['YPlacement'] += $incurs; }
						else { $this->OTLdata[$j]['GPOSinfo']['YPlacement'] = $incurs; }
					}
					if (isset($this->Exit[$i]['X']) && isset($this->Entry[$nextbase]['X']) ) {
						$adj = -($this->Exit[$i]['X'] - $this->Entry[$nextbase]['X']);
						// If XAdvance is aplied - in order for PDF to position the Advance correctly need to place it on:
						// in LTR - the next glyph, ignoring marks
						if (isset($this->OTLdata[$nextbase]['GPOSinfo']['XAdvance'])) { $this->OTLdata[$nextbase]['GPOSinfo']['XAdvance'] += $adj; }
						else { $this->OTLdata[$nextbase]['GPOSinfo']['XAdvance'] = $adj; }
					}
				}
				else { $incurs = false; }
			}
			else if (strpos($this->GlyphClassMarks, $this->OTLdata[$i]['hex'])!==false) { continue; } // ignore Marks
			else { $incurs = false; }
		}
	}




  }	// end GPOS

	if ($this->debugOTL) { $this->_dumpproc('END', '-', '-', '-', '-', 0, '-', 0); exit; }

	$this->schOTLdata[$sch] = $this->OTLdata;
	$this->OTLdata = array();
}	// END foreach subchunk


	// 11. Re-assemble and return text string
	//==============================
	$newGPOSinfo = array();
	$newOTLdata = array();
	$newchar_data = array();
	$newgroup = '';
	$e = '';
	$ectr = 0;

	for($sch=0;$sch<=$subchunk;$sch++) {
		for ($i=0;$i<count($this->schOTLdata[$sch]);$i++) {
			if (isset($this->schOTLdata[$sch][$i]['GPOSinfo'])) {
				$newGPOSinfo[$ectr] = $this->schOTLdata[$sch][$i]['GPOSinfo'];
			}
			$newchar_data[$ectr] = array('bidi_class' => $this->schOTLdata[$sch][$i]['bidi_type'], 'uni' => $this->schOTLdata[$sch][$i]['uni']);
			$newgroup .= $this->schOTLdata[$sch][$i]['group'];
			$e.=code2utf($this->schOTLdata[$sch][$i]['uni']);
			if (isset($this->mpdf->CurrentFont['subset'])) {
				$this->mpdf->CurrentFont['subset'][$this->schOTLdata[$sch][$i]['uni']] = $this->schOTLdata[$sch][$i]['uni'];
			}
			$ectr++;
		}

	}
	$this->OTLdata['GPOSinfo'] = $newGPOSinfo;
	$this->OTLdata['char_data'] = $newchar_data ;
	$this->OTLdata['group'] = $newgroup ;


	// This leaves OTLdata::GPOSinfo, ::bidi_type, & ::group

	return $e;

}

function _applyTagSettings($tags, $Features, $omittags='', $onlytags=false) {
		if (empty($this->mpdf->OTLtags['Plus']) && empty($this->mpdf->OTLtags['Minus']) && empty($this->mpdf->OTLtags['FFPlus']) && empty($this->mpdf->OTLtags['FFMinus'])) { return $tags; }

		// Use $tags as starting point
		$usetags = $tags;

		// Only set / unset tags which are in the font
		// Ignore tags which are in $omittags
		// If $onlytags, then just unset tags which are already in the Tag list

		$fp = $fm = $ffp = $ffm = '';

		// Font features to enable - set by font-variant-xx
		if (isset($this->mpdf->OTLtags['Plus'])) $fp = $this->mpdf->OTLtags['Plus'];
		preg_match_all('/([a-zA-Z0-9]{4})/',$fp,$m);
		for($i=0;$i<count($m[0]);$i++) {
			$t = $m[1][$i];
			// Is it a valid tag?
			if(isset($Features[$t]) && strpos($omittags,$t)===false && (!$onlytags || strpos($tags,$t)!==false )) {
				$usetags .= ' '.$t;
			}
		}

		// Font features to disable - set by font-variant-xx
		if (isset($this->mpdf->OTLtags['Minus'])) $fm = $this->mpdf->OTLtags['Minus'];
		preg_match_all('/([a-zA-Z0-9]{4})/',$fm,$m);
		for($i=0;$i<count($m[0]);$i++) {
			$t = $m[1][$i];
			// Is it a valid tag?
			if(isset($Features[$t]) && strpos($omittags,$t)===false && (!$onlytags || strpos($tags,$t)!==false )) {
				$usetags = str_replace($t,'',$usetags);
			}
		}

		// Font features to enable - set by font-feature-settings
		if (isset($this->mpdf->OTLtags['FFPlus'])) $ffp = $this->mpdf->OTLtags['FFPlus'];	// Font Features - may include integer: salt4
		preg_match_all('/([a-zA-Z0-9]{4})([\d+]*)/',$ffp,$m);
		for($i=0;$i<count($m[0]);$i++) {
			$t = $m[1][$i];
			// Is it a valid tag?
			if(isset($Features[$t]) && strpos($omittags,$t)===false && (!$onlytags || strpos($tags,$t)!==false )) {
				$usetags .= ' '.$m[0][$i];		//  - may include integer: salt4
			}
		}

		// Font features to disable - set by font-feature-settings
		if (isset($this->mpdf->OTLtags['FFMinus'])) $ffm = $this->mpdf->OTLtags['FFMinus'];
		preg_match_all('/([a-zA-Z0-9]{4})/',$ffm,$m);
		for($i=0;$i<count($m[0]);$i++) {
			$t = $m[1][$i];
			// Is it a valid tag?
			if(isset($Features[$t]) && strpos($omittags,$t)===false && (!$onlytags || strpos($tags,$t)!==false )) {
				$usetags = str_replace($t,'',$usetags);
			}
		}
		return $usetags; 
}

function _applyGSUBrules($usetags, $scriptTag, $langsys) {
	// Features from all Tags are applied together, in Lookup List order.
	// For Indic - should be applied one syllable at a time	
	// - Implemented in functions checkContextMatch and checkContextMatchMultiple by failing to match if outside scope of current 'syllable'
	// if $this->restrictToSyllable is true

	$GSUBFeatures = $this->mpdf->CurrentFont['GSUBFeatures'][$scriptTag][$langsys];
	$LookupList = array();
	foreach($GSUBFeatures AS $tag=>$arr) {
		if (strpos($usetags, $tag)!==false) {
			foreach($arr AS $lu) { $LookupList[$lu] = $tag; }
		}
	}
	ksort($LookupList);

	foreach($LookupList AS $lu=>$tag) {
		$Type = $this->GSUBLookups[$lu]['Type'];
		$Flag = $this->GSUBLookups[$lu]['Flag'];
		$MarkFilteringSet = $this->GSUBLookups[$lu]['MarkFilteringSet'];
		$tagInt = 1;
		if (preg_match('/'.$tag.'([0-9]{1,2})/', $usetags, $m)) {
			$tagInt = $m[1];
		}
		$ptr = 0;
		// Test each glyph sequentially
		while($ptr < (count($this->OTLdata))) {	// whilst there is another glyph ..0064
			$currGlyph = $this->OTLdata[$ptr]['hex'];
			$currGID = $this->OTLdata[$ptr]['uni'];
			$shift = 1;
			foreach($this->GSUBLookups[$lu]['Subtables'] AS $c=>$subtable_offset) {
				// NB Coverage only looks at glyphs for position 1 (esp. 7.3 and 8.3)
				if (isset($this->GSLuCoverage[$lu][$c][$currGID])) {
					// Get rules from font GSUB subtable
					$shift = $this->_applyGSUBsubtable($lu, $c, $ptr, $currGlyph, $currGID, ($subtable_offset - $this->GSUB_offset), $Type, $Flag, $MarkFilteringSet, $this->GSLuCoverage[$lu][$c], 0, $tag, 0, $tagInt);

					if ($shift) { break; }
				}
			}
			if ($shift == 0) { $shift = 1; }
			$ptr += $shift;

		}
	}
}

function _applyGSUBrulesSingly($usetags, $scriptTag, $langsys) {
	// Features are applied one at a time, working through each codepoint

	$GSUBFeatures = $this->mpdf->CurrentFont['GSUBFeatures'][$scriptTag][$langsys];

	$tags = explode(' ',$usetags);
	foreach($tags AS $usetag) {
		$LookupList = array();
		foreach($GSUBFeatures AS $tag=>$arr) {
			if (strpos($usetags, $tag)!==false) {
				foreach($arr AS $lu) { $LookupList[$lu] = $tag; }
			}
		}
		ksort($LookupList);

		$ptr = 0;
		// Test each glyph sequentially
		while($ptr < (count($this->OTLdata))) {	// whilst there is another glyph ..0064
			$currGlyph = $this->OTLdata[$ptr]['hex'];
			$currGID = $this->OTLdata[$ptr]['uni'];
			$shift = 1;

			foreach($LookupList AS $lu=>$tag) {
				$Type = $this->GSUBLookups[$lu]['Type'];
				$Flag = $this->GSUBLookups[$lu]['Flag'];
				$MarkFilteringSet = $this->GSUBLookups[$lu]['MarkFilteringSet'];
				$tagInt = 1;
				if (preg_match('/'.$tag.'([0-9]{1,2})/', $usetags, $m)) {
					$tagInt = $m[1];
				}

				foreach($this->GSUBLookups[$lu]['Subtables'] AS $c=>$subtable_offset) {
					// NB Coverage only looks at glyphs for position 1 (esp. 7.3 and 8.3)
					if (isset($this->GSLuCoverage[$lu][$c][$currGID])) {
						// Get rules from font GSUB subtable
						$shift = $this->_applyGSUBsubtable($lu, $c, $ptr, $currGlyph, $currGID, ($subtable_offset - $this->GSUB_offset), $Type, $Flag, $MarkFilteringSet, $this->GSLuCoverage[$lu][$c], 0, $tag, 0, $tagInt);

						if ($shift) { break 2; }
					}
				}
			}
			if ($shift == 0) { $shift = 1; }
			$ptr += $shift;

		}
	}
}

function _applyGSUBrulesMyanmar($usetags, $scriptTag, $langsys) {
	// $usetags = locl ccmp rphf pref blwf pstf';
	// applied to all characters

	$GSUBFeatures = $this->mpdf->CurrentFont['GSUBFeatures'][$scriptTag][$langsys];

	// ALL should be applied one syllable at a time	
	// Implemented in functions checkContextMatch and checkContextMatchMultiple by failing to match if outside scope of current 'syllable'
	$tags = explode(' ',$usetags);
	foreach($tags AS $usetag) {

		$LookupList = array();
		foreach($GSUBFeatures AS $tag=>$arr) {
			if ($tag==$usetag) {
				foreach($arr AS $lu) { $LookupList[$lu] = $tag; }
			}
		}
		ksort($LookupList);

		foreach($LookupList AS $lu=>$tag) {

			$Type = $this->GSUBLookups[$lu]['Type'];
			$Flag = $this->GSUBLookups[$lu]['Flag'];
			$MarkFilteringSet = $this->GSUBLookups[$lu]['MarkFilteringSet'];
			$tagInt = 1;
			if (preg_match('/'.$tag.'([0-9]{1,2})/', $usetags, $m)) {
				$tagInt = $m[1];
			}

			$ptr = 0;
			// Test each glyph sequentially
			while($ptr < (count($this->OTLdata))) {	// whilst there is another glyph ..0064
				$currGlyph = $this->OTLdata[$ptr]['hex'];
				$currGID = $this->OTLdata[$ptr]['uni'];
				$shift = 1;
				foreach($this->GSUBLookups[$lu]['Subtables'] AS $c=>$subtable_offset) {
					// NB Coverage only looks at glyphs for position 1 (esp. 7.3 and 8.3)
					if (isset($this->GSLuCoverage[$lu][$c][$currGID])) {
						// Get rules from font GSUB subtable
						$shift = $this->_applyGSUBsubtable($lu, $c, $ptr, $currGlyph, $currGID, ($subtable_offset - $this->GSUB_offset), $Type, $Flag, $MarkFilteringSet, $this->GSLuCoverage[$lu][$c], 0, $usetag, 0, $tagInt);

						if ($shift) { break; }
					}
				}
				if ($shift == 0) { $shift = 1; }
				$ptr += $shift;

			}
		}
	}
}

function _applyGSUBrulesIndic($usetags, $scriptTag, $langsys, $is_old_spec) {
	// $usetags = 'locl ccmp nukt akhn rphf rkrf pref blwf half pstf vatu cjct'; then later - init
	// rphf, pref, blwf, half, abvf, pstf, and init are only applied where ['mask'] indicates:  INDIC::FLAG(INDIC::RPHF);
	// The rest are applied to all characters

	$GSUBFeatures = $this->mpdf->CurrentFont['GSUBFeatures'][$scriptTag][$langsys];

	// ALL should be applied one syllable at a time	
	// Implemented in functions checkContextMatch and checkContextMatchMultiple by failing to match if outside scope of current 'syllable'
	$tags = explode(' ',$usetags);
	foreach($tags AS $usetag) {

		$LookupList = array();
		foreach($GSUBFeatures AS $tag=>$arr) {
			if ($tag==$usetag) {
				foreach($arr AS $lu) { $LookupList[$lu] = $tag; }
			}
		}
		ksort($LookupList);

		foreach($LookupList AS $lu=>$tag) {

			$Type = $this->GSUBLookups[$lu]['Type'];
			$Flag = $this->GSUBLookups[$lu]['Flag'];
			$MarkFilteringSet = $this->GSUBLookups[$lu]['MarkFilteringSet'];
			$tagInt = 1;
			if (preg_match('/'.$tag.'([0-9]{1,2})/', $usetags, $m)) {
				$tagInt = $m[1];
			}

			$ptr = 0;
			// Test each glyph sequentially
			while($ptr < (count($this->OTLdata))) {	// whilst there is another glyph ..0064
				$currGlyph = $this->OTLdata[$ptr]['hex'];
				$currGID = $this->OTLdata[$ptr]['uni'];
				$shift = 1;
				foreach($this->GSUBLookups[$lu]['Subtables'] AS $c=>$subtable_offset) {
					// NB Coverage only looks at glyphs for position 1 (esp. 7.3 and 8.3)
					if (isset($this->GSLuCoverage[$lu][$c][$currGID])) {
						if (strpos('rphf pref blwf half pstf cfar init' , $usetag)!==false) { // only apply when mask indicates
							$mask = 0;
							switch ($usetag) {
								case 'rphf': $mask = (1<<(INDIC::RPHF)); break;
								case 'pref': $mask = (1<<(INDIC::PREF)); break;
								case 'blwf': $mask = (1<<(INDIC::BLWF)); break;
								case 'half': $mask = (1<<(INDIC::HALF)); break;
								case 'pstf': $mask = (1<<(INDIC::PSTF)); break;
								case 'cfar': $mask = (1<<(INDIC::CFAR)); break;
								case 'init': $mask = (1<<(INDIC::INIT)); break;
							}
							if (!($this->OTLdata[$ptr]['mask'] & $mask)) { continue; }
						}
						// Get rules from font GSUB subtable
						$shift = $this->_applyGSUBsubtable($lu, $c, $ptr, $currGlyph, $currGID, ($subtable_offset - $this->GSUB_offset), $Type, $Flag, $MarkFilteringSet, $this->GSLuCoverage[$lu][$c], 0, $usetag, $is_old_spec, $tagInt);

						if ($shift) { break; }
					}

					// Special case for Indic  ZZZ99S
					// Check to substitute Halant-Consonant in PREF, BLWF or PSTF
					// i.e. new spec but GSUB tables have Consonant-Halant in Lookups e.g. FreeSerif, which
					// incorrectly just moved old spec tables to new spec. Uniscribe seems to cope with this
					// See also ttffontsuni.php
					// First check if current glyph is a Halant/Virama
					else if (_OTL_OLD_SPEC_COMPAT_1 && $Type==4 && !$is_old_spec && strpos('0094D 009CD 00A4D 00ACD 00B4D 00BCD 00C4D 00CCD 00D4D',$currGlyph)!== false) {
						 // only apply when 'pref blwf pstf' tags, and when mask indicates
						if (strpos('pref blwf pstf' , $usetag)!==false) {
							$mask = 0;
							switch ($usetag) {
								case 'pref': $mask = (1<<(INDIC::PREF)); break;
								case 'blwf': $mask = (1<<(INDIC::BLWF)); break;
								case 'pstf': $mask = (1<<(INDIC::PSTF)); break;
							}
							if (!($this->OTLdata[$ptr]['mask'] & $mask)) { continue; }

							$nextGlyph = $this->OTLdata[$ptr+1]['hex'];
							$nextGID = $this->OTLdata[$ptr+1]['uni'];
							if (isset($this->GSLuCoverage[$lu][$c][$nextGID])) {

								// Get rules from font GSUB subtable
								$shift = $this->_applyGSUBsubtableSpecial($lu, $c, $ptr, $currGlyph, $currGID, $nextGlyph, $nextGID, ($subtable_offset - $this->GSUB_offset), $Type, $this->GSLuCoverage[$lu][$c]);

								if ($shift) { break; }
							}
						}
					}


				}
				if ($shift == 0) { $shift = 1; }
				$ptr += $shift;

			}
		}
	}
}


function _applyGSUBsubtableSpecial($lookupID, $subtable, $ptr, $currGlyph, $currGID, $nextGlyph, $nextGID, $subtable_offset, $Type, $LuCoverage) {

	// Special case for Indic
	// Check to substitute Halant-Consonant in PREF, BLWF or PSTF
	// i.e. new spec but GSUB tables have Consonant-Halant in Lookups e.g. FreeSerif, which
	// incorrectly just moved old spec tables to new spec. Uniscribe seems to cope with this
	// See also ttffontsuni.php

	$this->seek($subtable_offset);
	$SubstFormat= $this->read_ushort();

	// Subtable contains Consonant - Halant
	// Text string contains Halant ($CurrGlyph) - Consonant ($nextGlyph)
	// Halant has already been matched, and already checked that $nextGID is in Coverage table

	////////////////////////////////////////////////////////////////////////////////
	// Only does: LookupType 4: Ligature Substitution Subtable : n to 1
	////////////////////////////////////////////////////////////////////////////////
	$Coverage = $subtable_offset + $this->read_ushort();
	$NextGlyphPos = $LuCoverage[$nextGID];
	$LigSetCount = $this->read_short();

	$this->skip($NextGlyphPos * 2);
	$LigSet = $subtable_offset + $this->read_short();

	$this->seek($LigSet);
	$LigCount = $this->read_short();
	// LigatureSet i.e. all starting with the same Glyph $nextGlyph [Consonant]
	$LigatureOffset = array();
	for ($g=0;$g<$LigCount;$g++) { 
		$LigatureOffset[$g] = $LigSet + $this->read_ushort();
	}
	for ($g=0;$g<$LigCount;$g++) { 
		// Ligature tables
		$this->seek($LigatureOffset[$g]);
		$LigGlyph = $this->read_ushort();
		$substitute = $this->glyphToChar($LigGlyph);
		$CompCount = $this->read_ushort();

		if ($CompCount != 2) { return 0; }	// Only expecting to work with 2:1 (and no ignore characters in between)


		$gid = $this->read_ushort();
		$checkGlyph = $this->glyphToChar($gid); // Other component/input Glyphs starting at position 2 (arrayindex 1)

		if ($currGID == $checkGlyph) { $match = true; }
		else { $match = false; break; }

		$GlyphPos = array();
		$GlyphPos[] = $ptr;
		$GlyphPos[] = $ptr+1;


		if ($match) {
			$shift = $this->GSUBsubstitute($ptr, $substitute, 4, $GlyphPos );	// GlyphPos contains positions to set null
			if ($shift) return 1;
		}

	}
	return 0;
}

function _applyGSUBsubtable($lookupID, $subtable, $ptr, $currGlyph, $currGID, $subtable_offset, $Type, $Flag, $MarkFilteringSet, $LuCoverage, $level=0, $currentTag, $is_old_spec, $tagInt) {
	$ignore = $this->_getGCOMignoreString($Flag, $MarkFilteringSet);

	// Lets start
	$this->seek($subtable_offset);
	$SubstFormat= $this->read_ushort();

	////////////////////////////////////////////////////////////////////////////////
	// LookupType 1: Single Substitution Subtable : 1 to 1
	////////////////////////////////////////////////////////////////////////////////
	if ($Type == 1) {
		// Flag = Ignore
		if ($this->_checkGCOMignore($Flag, $currGlyph, $MarkFilteringSet)) { return 0; }
		$CoverageOffset = $subtable_offset + $this->read_ushort();
		$GlyphPos = $LuCoverage[$currGID];
		//===========
		// Format 1: 
		//===========
		if ($SubstFormat==1) {	// Calculated output glyph indices
			$DeltaGlyphID = $this->read_short();
			$this->seek($CoverageOffset);
			$glyphs = $this->_getCoverageGID();
			$GlyphID = $glyphs[$GlyphPos] + $DeltaGlyphID;
		}
		//===========
		// Format 2: 
		//===========
		else if ($SubstFormat==2) {	// Specified output glyph indices
			$GlyphCount = $this->read_ushort();
			$this->skip($GlyphPos * 2 );
			$GlyphID = $this->read_ushort();
		}

		$substitute = $this->glyphToChar($GlyphID);
		$shift = $this->GSUBsubstitute($ptr, $substitute, $Type );
		if ($this->debugOTL && $shift) { $this->_dumpproc('GSUB', $lookupID, $subtable, $Type, $SubstFormat, $ptr, $currGlyph, $level); }
		if ($shift) return 1;
		return 0;
	}

	////////////////////////////////////////////////////////////////////////////////
	// LookupType 2: Multiple Substitution Subtable : 1 to n
	////////////////////////////////////////////////////////////////////////////////
	else if ($Type == 2) {
		// Flag = Ignore
		if ($this->_checkGCOMignore($Flag, $currGlyph, $MarkFilteringSet)) { return 0; }
		$Coverage = $subtable_offset + $this->read_ushort();
		$GlyphPos = $LuCoverage[$currGID];
		$this->skip(2);
		$this->skip($GlyphPos * 2);
		$Sequences = $subtable_offset + $this->read_short();

		$this->seek($Sequences);
		$GlyphCount = $this->read_short();
		$SubstituteGlyphs = array();
		for ($g=0;$g<$GlyphCount;$g++) { 
			$sgid = $this->read_ushort();
			$SubstituteGlyphs[] = $this->glyphToChar($sgid);
		}

		$shift = $this->GSUBsubstitute($ptr, $SubstituteGlyphs, $Type  );
		if ($this->debugOTL && $shift) { $this->_dumpproc('GSUB', $lookupID, $subtable, $Type, $SubstFormat, $ptr, $currGlyph, $level); }
		if ($shift) return $shift;
		return 0;
	}
	////////////////////////////////////////////////////////////////////////////////
	// LookupType 3: Alternate Forms : 1 to 1(n)
	////////////////////////////////////////////////////////////////////////////////
	else if ($Type == 3) {
 		// Flag = Ignore
		if ($this->_checkGCOMignore($Flag, $currGlyph, $MarkFilteringSet)) { return 0; }
		$Coverage = $subtable_offset + $this->read_ushort();
		$AlternateSetCount = $this->read_short();
		///////////////////////////////////////////////////////////////////////////////!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
		// Need to set alternate IF set by CSS3 font-feature for a tag
		// i.e. if this is 'salt' alternate may be set to 2
		// default value will be $alt=1 ( === index of 0 in list of alternates)
		$alt = 1;	// $alt=1 points to Alternative[0]
		if ($tagInt>1) { $alt = $tagInt; }
		///////////////////////////////////////////////////////////////////////////////!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
		if ($alt == 0) { return 0; }	// If specified alternate not present, cancel [ or could default $alt = 1 ?]

		$GlyphPos = $LuCoverage[$currGID];
		$this->skip($GlyphPos * 2);

		$AlternateSets = $subtable_offset + $this->read_short();
		$this->seek($AlternateSets );

		$AlternateGlyphCount = $this->read_short();
		if ($alt > $AlternateGlyphCount) { return 0; }	// If specified alternate not present, cancel [ or could default $alt = 1 ?]

		$this->skip(($alt-1) * 2);
		$GlyphID = $this->read_ushort();

		$substitute = $this->glyphToChar($GlyphID);
		$shift = $this->GSUBsubstitute($ptr, $substitute, $Type );
		if ($this->debugOTL && $shift) { $this->_dumpproc('GSUB', $lookupID, $subtable, $Type, $SubstFormat, $ptr, $currGlyph, $level); }
		if ($shift) return 1;
		return 0;
	}
	////////////////////////////////////////////////////////////////////////////////
	// LookupType 4: Ligature Substitution Subtable : n to 1
	////////////////////////////////////////////////////////////////////////////////
	else if ($Type == 4) {
		// Flag = Ignore
		if ($this->_checkGCOMignore($Flag, $currGlyph, $MarkFilteringSet)) { return 0; }
		$Coverage = $subtable_offset + $this->read_ushort();
		$FirstGlyphPos = $LuCoverage[$currGID];

		$LigSetCount = $this->read_short();

		$this->skip($FirstGlyphPos * 2);
		$LigSet = $subtable_offset + $this->read_short();

		$this->seek($LigSet);
		$LigCount = $this->read_short();
		// LigatureSet i.e. all starting with the same first Glyph $currGlyph
		$LigatureOffset = array();
		for ($g=0;$g<$LigCount;$g++) { 
			$LigatureOffset[$g] = $LigSet + $this->read_ushort();
		}
		for ($g=0;$g<$LigCount;$g++) { 
			// Ligature tables
			$this->seek($LigatureOffset[$g]);
			$LigGlyph = $this->read_ushort();	// Output Ligature GlyphID
			$substitute = $this->glyphToChar($LigGlyph);
			$CompCount = $this->read_ushort();

			$spos = $ptr;
			$match = true; 
			$GlyphPos = array();
			$GlyphPos[] = $spos;
			for ($l=1;$l<$CompCount;$l++) { 
				$gid = $this->read_ushort();
				$checkGlyph = $this->glyphToChar($gid); // Other component/input Glyphs starting at position 2 (arrayindex 1)

				$spos++;
				//while $this->OTLdata[$spos]['uni'] is an "ignore" =>  spos++
				while (isset($this->OTLdata[$spos]) && strpos($ignore, $this->OTLdata[$spos]['hex'])!==false) { $spos++; }

				if (isset($this->OTLdata[$spos]) && $this->OTLdata[$spos]['uni'] == $checkGlyph) {
					$GlyphPos[] = $spos;
				}
				else { $match = false; break; }

			}


			if ($match) {
				$shift = $this->GSUBsubstitute($ptr, $substitute, $Type, $GlyphPos );	// GlyphPos contains positions to set null
				if ($this->debugOTL && $shift) { $this->_dumpproc('GSUB', $lookupID, $subtable, $Type, $SubstFormat, $ptr, $currGlyph, $level); }
				if ($shift) return ($spos-$ptr+1-($CompCount-1));
			}

		}
		return 0;
	}

	////////////////////////////////////////////////////////////////////////////////
	// LookupType 5: Contextual Substitution Subtable
	////////////////////////////////////////////////////////////////////////////////
	else if ($Type == 5) {
		//===========
		// Format 1: Simple Context Glyph Substitution
		//===========
		if ($SubstFormat==1) {
			$CoverageTableOffset = $subtable_offset + $this->read_ushort();
			$SubRuleSetCount = $this->read_ushort();
			$SubRuleSetOffset = array();
			for ($b=0;$b<$SubRuleSetCount;$b++) {
				$offset = $this->read_ushort();
				if ($offset==0x0000) {
					$SubRuleSetOffset[] = $offset;
				}
				else {
					$SubRuleSetOffset[] = $subtable_offset + $offset;
				}
			}

			// SubRuleSet tables: All contexts beginning with the same glyph
			// Select the SubRuleSet required using the position of the glyph in the coverage table
			$GlyphPos = $LuCoverage[$currGID];
			if ($SubRuleSetOffset[$GlyphPos]>0) {
					$this->seek($SubRuleSetOffset[$GlyphPos]);
					$SubRuleCnt = $this->read_ushort();
					$SubRule = array();
					for($b=0;$b<$SubRuleCnt;$b++) {
						$SubRule[$b] = $SubRuleSetOffset[$GlyphPos]+$this->read_ushort();
					}
					for($b=0;$b<$SubRuleCnt;$b++) {		// EACH RULE
						$this->seek($SubRule[$b]);
						$InputGlyphCount = $this->read_ushort();
						$SubstCount = $this->read_ushort();

						$Backtrack = array();
						$Lookahead = array();
						$Input = array();
						$Input[0] = $this->OTLdata[$ptr]['uni'];
						for ($r=1;$r<$InputGlyphCount;$r++) {
							$gid = $this->read_ushort();
							$Input[$r] = $this->glyphToChar($gid);
						}
						$matched = $this->checkContextMatch($Input, $Backtrack, $Lookahead, $ignore, $ptr);
						if ($matched) {
							if ($this->debugOTL) { $this->_dumpproc('GSUB', $lookupID, $subtable, $Type, $SubstFormat, $ptr, $currGlyph, $level); }
							for ($p=0;$p<$SubstCount;$p++) {	// EACH LOOKUP
								$SequenceIndex[$p] = $this->read_ushort();
								$LookupListIndex[$p] = $this->read_ushort();
							}

							for ($p=0;$p<$SubstCount;$p++) {
								// Apply  $LookupListIndex  at   $SequenceIndex
								if ($SequenceIndex[$p] >= $InputGlyphCount) { continue; }
								$lu = $LookupListIndex[$p];
								$luType = $this->GSUBLookups[$lu]['Type'];
								$luFlag = $this->GSUBLookups[$lu]['Flag'];
								$luMarkFilteringSet = $this->GSUBLookups[$lu]['MarkFilteringSet'];

								$luptr = $matched[$SequenceIndex[$p]];
								$lucurrGlyph = $this->OTLdata[$luptr]['hex'];
								$lucurrGID = $this->OTLdata[$luptr]['uni'];

								foreach($this->GSUBLookups[$lu]['Subtables'] AS $luc=>$lusubtable_offset) {
									$shift = $this->_applyGSUBsubtable($lu, $luc, $luptr, $lucurrGlyph, $lucurrGID, ($lusubtable_offset - $this->GSUB_offset) , $luType, $luFlag, $luMarkFilteringSet, $this->GSLuCoverage[$lu][$luc], 1, $currentTag, $is_old_spec, $tagInt);
									if ($shift) { break; }
								}
							}

							if (!defined("OMIT_OTL_FIX_3") || OMIT_OTL_FIX_3 != 1) { return $shift ; }	/* OTL_FIX_3 */
							else return $InputGlyphCount ;	// should be + matched ignores in Input Sequence

						}
					}

			}
			return 0;
		}

		//===========
		// Format 2: 
		//===========
		// Format 2: Class-based Context Glyph Substitution
		else if ($SubstFormat==2) {

			$CoverageTableOffset = $subtable_offset + $this->read_ushort();
			$InputClassDefOffset = $subtable_offset + $this->read_ushort();
			$SubClassSetCnt = $this->read_ushort();
			$SubClassSetOffset = array();
			for ($b=0;$b<$SubClassSetCnt;$b++) {
				$offset = $this->read_ushort();
				if ($offset==0x0000) {
					$SubClassSetOffset[] = $offset;
				}
				else {
					$SubClassSetOffset[] = $subtable_offset + $offset;
				}
			}

			$InputClasses = $this->_getClasses($InputClassDefOffset);

			for ($s=0;$s<$SubClassSetCnt;$s++) {	// $SubClassSet is ordered by input class-may be NULL
				// Select $SubClassSet if currGlyph is in First Input Class
				if ($SubClassSetOffset[$s]>0 && isset($InputClasses[$s][$currGID])) {
					$this->seek($SubClassSetOffset[$s]);
					$SubClassRuleCnt = $this->read_ushort();
					$SubClassRule = array();
					for($b=0;$b<$SubClassRuleCnt;$b++) {
						$SubClassRule[$b] = $SubClassSetOffset[$s]+$this->read_ushort();
					}

					for($b=0;$b<$SubClassRuleCnt;$b++) {		// EACH RULE
						$this->seek($SubClassRule[$b]);
						$InputGlyphCount = $this->read_ushort();
						$SubstCount = $this->read_ushort();
						$Input = array();
						for ($r=1;$r<$InputGlyphCount;$r++) {
							$Input[$r] = $this->read_ushort();
						}

						$inputClass = $s;	

						$inputGlyphs = array();
						$inputGlyphs[0] = $InputClasses[$inputClass];

						if ($InputGlyphCount>1) {
							//  NB starts at 1 
							for ($gcl=1;$gcl<$InputGlyphCount;$gcl++) {
								$classindex = $Input[$gcl];
								if (isset($InputClasses[$classindex])) { $inputGlyphs[$gcl] = $InputClasses[$classindex]; }
								else { $inputGlyphs[$gcl] = ''; }
							}
						}

						// Class 0 contains all the glyphs NOT in the other classes
						$class0excl = array();
						for ($gc=1;$gc<=count($InputClasses);$gc++) {
							if (is_array($InputClasses[$gc])) $class0excl = $class0excl + $InputClasses[$gc];
						}

						$backtrackGlyphs = array();
						$lookaheadGlyphs = array();

						$matched = $this->checkContextMatchMultipleUni($inputGlyphs, $backtrackGlyphs, $lookaheadGlyphs, $ignore, $ptr, $class0excl);
						if ($matched) {
							if ($this->debugOTL) { $this->_dumpproc('GSUB', $lookupID, $subtable, $Type, $SubstFormat, $ptr, $currGlyph, $level); }
							for ($p=0;$p<$SubstCount;$p++) {	// EACH LOOKUP
								$SequenceIndex[$p] = $this->read_ushort();
								$LookupListIndex[$p] = $this->read_ushort();
							}

							for ($p=0;$p<$SubstCount;$p++) {
								// Apply  $LookupListIndex  at   $SequenceIndex
								if ($SequenceIndex[$p] >= $InputGlyphCount) { continue; }
								$lu = $LookupListIndex[$p];
								$luType = $this->GSUBLookups[$lu]['Type'];
								$luFlag = $this->GSUBLookups[$lu]['Flag'];
								$luMarkFilteringSet = $this->GSUBLookups[$lu]['MarkFilteringSet'];

								$luptr = $matched[$SequenceIndex[$p]];
								$lucurrGlyph = $this->OTLdata[$luptr]['hex'];
								$lucurrGID = $this->OTLdata[$luptr]['uni'];

								foreach($this->GSUBLookups[$lu]['Subtables'] AS $luc=>$lusubtable_offset) {
									$shift = $this->_applyGSUBsubtable($lu, $luc, $luptr, $lucurrGlyph, $lucurrGID, ($lusubtable_offset - $this->GSUB_offset) , $luType, $luFlag, $luMarkFilteringSet, $this->GSLuCoverage[$lu][$luc], 1, $currentTag, $is_old_spec, $tagInt);
									if ($shift) { break; }
								}
							}

							if (!defined("OMIT_OTL_FIX_3") || OMIT_OTL_FIX_3 != 1) { return $shift ; }	/* OTL_FIX_3 */
							else return $InputGlyphCount ;	// should be + matched ignores in Input Sequence

						}

					}

				}
			}

			return 0;
		}

		//===========
		// Format 3: 
		//===========
		// Format 3: Coverage-based Context Glyph Substitution
		else if ($SubstFormat==3) {
			die("GSUB Lookup Type ".$Type." Format ".$SubstFormat." not TESTED YET."); 
			return 0;
		}

	}

	////////////////////////////////////////////////////////////////////////////////
	// LookupType 6: Chaining Contextual Substitution Subtable
	////////////////////////////////////////////////////////////////////////////////
	else if ($Type == 6) {

		//===========
		// Format 1: 
		//===========
		// Format 1: Simple Chaining Context Glyph Substitution
		if ($SubstFormat==1) {	
			$Coverage = $subtable_offset + $this->read_ushort();
			$GlyphPos = $LuCoverage[$currGID];
			$ChainSubRuleSetCount = $this->read_ushort();
			// All of the ChainSubRule tables defining contexts that begin with the same first glyph are grouped together and defined in a ChainSubRuleSet table
			$this->skip($GlyphPos * 2);
			$ChainSubRuleSet= $subtable_offset + $this->read_ushort();
			$this->seek($ChainSubRuleSet);
			$ChainSubRuleCount = $this->read_ushort();

			for($s=0;$s<$ChainSubRuleCount;$s++) {
				$ChainSubRule[$s] = $ChainSubRuleSet + $this->read_ushort();
			}

			for($s=0;$s<$ChainSubRuleCount;$s++) {
				$this->seek($ChainSubRule[$s]);

				$BacktrackGlyphCount = $this->read_ushort();
				$Backtrack = array();
				for ($b=0;$b<$BacktrackGlyphCount;$b++) {
					$gid = $this->read_ushort();
					$Backtrack[] = $this->glyphToChar($gid);
				}
				$Input = array();
				$Input[0] = $this->OTLdata[$ptr]['uni'];
				$InputGlyphCount = $this->read_ushort();
				for ($b=1;$b<$InputGlyphCount;$b++) {
					$gid = $this->read_ushort();
					$Input[$b] = $this->glyphToChar($gid);
				}
				$LookaheadGlyphCount = $this->read_ushort();
				$Lookahead = array();
				for ($b=0;$b<$LookaheadGlyphCount;$b++) {
					$gid = $this->read_ushort();
					$Lookahead[] = $this->glyphToChar($gid);
				}

				$matched = $this->checkContextMatch($Input, $Backtrack, $Lookahead, $ignore, $ptr);
				if ($matched) {
					if ($this->debugOTL) { $this->_dumpproc('GSUB', $lookupID, $subtable, $Type, $SubstFormat, $ptr, $currGlyph, $level); }
					$SubstCount = $this->read_ushort();
					for ($p=0;$p<$SubstCount;$p++) {
						// SubstLookupRecord
						$SubstLookupRecord[$p]['SequenceIndex'] = $this->read_ushort();
						$SubstLookupRecord[$p]['LookupListIndex'] = $this->read_ushort();
					}
					for ($p=0;$p<$SubstCount;$p++) {
						// Apply  $SubstLookupRecord[$p]['LookupListIndex']  at   $SubstLookupRecord[$p]['SequenceIndex']
						if ($SubstLookupRecord[$p]['SequenceIndex'] >= $InputGlyphCount) { continue; }
						$lu = $SubstLookupRecord[$p]['LookupListIndex'];
						$luType = $this->GSUBLookups[$lu]['Type'];
						$luFlag = $this->GSUBLookups[$lu]['Flag'];
						$luMarkFilteringSet = $this->GSUBLookups[$lu]['MarkFilteringSet'];

						$luptr = $matched[$SubstLookupRecord[$p]['SequenceIndex']];
						$lucurrGlyph = $this->OTLdata[$luptr]['hex'];
						$lucurrGID = $this->OTLdata[$luptr]['uni'];

						foreach($this->GSUBLookups[$lu]['Subtables'] AS $luc=>$lusubtable_offset) {
							$shift = $this->_applyGSUBsubtable($lu, $luc, $luptr, $lucurrGlyph, $lucurrGID, ($lusubtable_offset - $this->GSUB_offset), $luType, $luFlag, $luMarkFilteringSet, $this->GSLuCoverage[$lu][$luc], 1, $currentTag, $is_old_spec, $tagInt);
							if ($shift) { break; }
						}
					}
					if (!defined("OMIT_OTL_FIX_3") || OMIT_OTL_FIX_3 != 1) { return $shift ; }	/* OTL_FIX_3 */
					else return $InputGlyphCount ;	// should be + matched ignores in Input Sequence
				}




			}
			return 0;
		}

		//===========
		// Format 2: 
		//===========
		// Format 2: Class-based Chaining Context Glyph Substitution  p257
		else if ($SubstFormat==2) {	

			// NB Format 2 specifies fixed class assignments (identical for each position in the backtrack, input, or lookahead sequence) and exclusive classes (a glyph cannot be in more than one class at a time)

			$CoverageTableOffset = $subtable_offset + $this->read_ushort();
			$BacktrackClassDefOffset = $subtable_offset + $this->read_ushort();
			$InputClassDefOffset = $subtable_offset + $this->read_ushort();
			$LookaheadClassDefOffset = $subtable_offset + $this->read_ushort();
			$ChainSubClassSetCnt = $this->read_ushort();
			$ChainSubClassSetOffset = array();
			for ($b=0;$b<$ChainSubClassSetCnt;$b++) {
				$offset = $this->read_ushort();
				if ($offset==0x0000) {
					$ChainSubClassSetOffset[] = $offset;
				}
				else {
					$ChainSubClassSetOffset[] = $subtable_offset + $offset;
				}
			}

			$BacktrackClasses = $this->_getClasses($BacktrackClassDefOffset);
			$InputClasses = $this->_getClasses($InputClassDefOffset);
			$LookaheadClasses = $this->_getClasses($LookaheadClassDefOffset);

			for ($s=0;$s<$ChainSubClassSetCnt;$s++) {	// $ChainSubClassSet is ordered by input class-may be NULL
				// Select $ChainSubClassSet if currGlyph is in First Input Class
				if ($ChainSubClassSetOffset[$s]>0 && isset($InputClasses[$s][$currGID])) {
					$this->seek($ChainSubClassSetOffset[$s]);
					$ChainSubClassRuleCnt = $this->read_ushort();
					$ChainSubClassRule = array();
					for($b=0;$b<$ChainSubClassRuleCnt;$b++) {
						$ChainSubClassRule[$b] = $ChainSubClassSetOffset[$s]+$this->read_ushort();
					}

					for($b=0;$b<$ChainSubClassRuleCnt;$b++) {		// EACH RULE
						$this->seek($ChainSubClassRule[$b]);
						$BacktrackGlyphCount = $this->read_ushort();
						for ($r=0;$r<$BacktrackGlyphCount;$r++) {
							$Backtrack[$r] = $this->read_ushort();
						}
						$InputGlyphCount = $this->read_ushort();
						for ($r=1;$r<$InputGlyphCount;$r++) {
							$Input[$r] = $this->read_ushort();
						}
						$LookaheadGlyphCount = $this->read_ushort();
						for ($r=0;$r<$LookaheadGlyphCount;$r++) {
							$Lookahead[$r] = $this->read_ushort();
						}


						// These contain classes of glyphs as arrays
						// $InputClasses[(class)] e.g. 0x02E6,0x02E7,0x02E8
						// $LookaheadClasses[(class)]
						// $BacktrackClasses[(class)]

						// These contain arrays of classIndexes
						// [Backtrack] [Lookahead] and [Input] (Input is from the second position only)


						$inputClass = $s;	//???

						$inputGlyphs = array();
						$inputGlyphs[0] = $InputClasses[$inputClass];

						if ($InputGlyphCount>1) {
							//  NB starts at 1 
							for ($gcl=1;$gcl<$InputGlyphCount;$gcl++) {
								$classindex = $Input[$gcl];
								if (isset($InputClasses[$classindex])) { $inputGlyphs[$gcl] = $InputClasses[$classindex]; }
								else { $inputGlyphs[$gcl] = ''; }
							}
						}

						// Class 0 contains all the glyphs NOT in the other classes
						$class0excl = array();
						for ($gc=1;$gc<=count($InputClasses);$gc++) {
							if (isset($InputClasses[$gc])) $class0excl = $class0excl + $InputClasses[$gc];
						}

						if ($BacktrackGlyphCount) {
							for ($gcl=0;$gcl<$BacktrackGlyphCount;$gcl++) {
								$classindex = $Backtrack[$gcl];
								if (isset($BacktrackClasses[$classindex])) { $backtrackGlyphs[$gcl] = $BacktrackClasses[$classindex]; }
								else { $backtrackGlyphs[$gcl] = ''; }
							}
						}
						else { $backtrackGlyphs = array(); }

						// Class 0 contains all the glyphs NOT in the other classes
						$bclass0excl = array();
						for ($gc=1;$gc<=count($BacktrackClasses);$gc++) {
							if (isset($BacktrackClasses[$gc])) $bclass0excl = $bclass0excl + $BacktrackClasses[$gc];
						}


						if ($LookaheadGlyphCount) {
							for ($gcl=0;$gcl<$LookaheadGlyphCount;$gcl++) {
								$classindex = $Lookahead[$gcl];
								if (isset($LookaheadClasses[$classindex])) { $lookaheadGlyphs[$gcl] = $LookaheadClasses[$classindex]; }
								else { $lookaheadGlyphs[$gcl] = ''; }
							}
						}
						else { $lookaheadGlyphs = array(); }

 						// Class 0 contains all the glyphs NOT in the other classes
						$lclass0excl = array();
						for ($gc=1;$gc<=count($LookaheadClasses);$gc++) {
							if (isset($LookaheadClasses[$gc])) $lclass0excl = $lclass0excl + $LookaheadClasses[$gc];
						}


						$matched = $this->checkContextMatchMultipleUni($inputGlyphs, $backtrackGlyphs, $lookaheadGlyphs, $ignore, $ptr, $class0excl, $bclass0excl, $lclass0excl );
						if ($matched) {
							if ($this->debugOTL) { $this->_dumpproc('GSUB', $lookupID, $subtable, $Type, $SubstFormat, $ptr, $currGlyph, $level); }
							$SubstCount = $this->read_ushort();
							for ($p=0;$p<$SubstCount;$p++) {	// EACH LOOKUP
								$SequenceIndex[$p] = $this->read_ushort();
								$LookupListIndex[$p] = $this->read_ushort();
							}

							for ($p=0;$p<$SubstCount;$p++) {
								// Apply  $LookupListIndex  at   $SequenceIndex
								if ($SequenceIndex[$p] >= $InputGlyphCount) { continue; }
								$lu = $LookupListIndex[$p];
								$luType = $this->GSUBLookups[$lu]['Type'];
								$luFlag = $this->GSUBLookups[$lu]['Flag'];
								$luMarkFilteringSet = $this->GSUBLookups[$lu]['MarkFilteringSet'];

								$luptr = $matched[$SequenceIndex[$p]];
								$lucurrGlyph = $this->OTLdata[$luptr]['hex'];
								$lucurrGID = $this->OTLdata[$luptr]['uni'];

								foreach($this->GSUBLookups[$lu]['Subtables'] AS $luc=>$lusubtable_offset) {
									$shift = $this->_applyGSUBsubtable($lu, $luc, $luptr, $lucurrGlyph, $lucurrGID, ($lusubtable_offset - $this->GSUB_offset) , $luType, $luFlag, $luMarkFilteringSet, $this->GSLuCoverage[$lu][$luc], 1, $currentTag, $is_old_spec, $tagInt);
									if ($shift) { break; }
								}
							}

							if (!defined("OMIT_OTL_FIX_3") || OMIT_OTL_FIX_3 != 1) { return $shift ; }	/* OTL_FIX_3 */
							else return $InputGlyphCount ;	// should be + matched ignores in Input Sequence

						}

					}

				}
			}

			return 0;
		}
 
		//===========
		// Format 3: 
		//===========
		// Format 3: Coverage-based Chaining Context Glyph Substitution  p259
		else if ($SubstFormat==3) {

			$BacktrackGlyphCount = $this->read_ushort();
			for ($b=0;$b<$BacktrackGlyphCount;$b++) {
				$CoverageBacktrackOffset[] = $subtable_offset + $this->read_ushort();	// in glyph sequence order
			}
			$InputGlyphCount = $this->read_ushort();
			for ($b=0;$b<$InputGlyphCount;$b++) {
				$CoverageInputOffset[] = $subtable_offset + $this->read_ushort();	// in glyph sequence order
			}
			$LookaheadGlyphCount = $this->read_ushort();
			for ($b=0;$b<$LookaheadGlyphCount;$b++) {
				$CoverageLookaheadOffset[] = $subtable_offset + $this->read_ushort();	// in glyph sequence order
			}
			$SubstCount = $this->read_ushort();
			$save_pos = $this->_pos;	// Save the point just after PosCount

			$CoverageBacktrackGlyphs = array();
			for ($b=0;$b<$BacktrackGlyphCount;$b++) {
				$this->seek($CoverageBacktrackOffset[$b]);
				$glyphs = $this->_getCoverage();
				$CoverageBacktrackGlyphs[$b] = implode("|",$glyphs);
			}
			$CoverageInputGlyphs = array();
			for ($b=0;$b<$InputGlyphCount;$b++) {
				$this->seek($CoverageInputOffset[$b]);
				$glyphs = $this->_getCoverage();
				$CoverageInputGlyphs[$b] = implode("|",$glyphs);
			}
			$CoverageLookaheadGlyphs = array();
			for ($b=0;$b<$LookaheadGlyphCount;$b++) {
				$this->seek($CoverageLookaheadOffset[$b]);
				$glyphs = $this->_getCoverage();
				$CoverageLookaheadGlyphs[$b] = implode("|",$glyphs);
			}

			$matched = $this->checkContextMatchMultiple($CoverageInputGlyphs, $CoverageBacktrackGlyphs, $CoverageLookaheadGlyphs , $ignore, $ptr);
			if ($matched) {
				if ($this->debugOTL) { $this->_dumpproc('GSUB', $lookupID, $subtable, $Type, $SubstFormat, $ptr, $currGlyph, $level); }

				$this->seek($save_pos);	// Return to just after PosCount
				for ($p=0;$p<$SubstCount;$p++) {
					// SubstLookupRecord
					$SubstLookupRecord[$p]['SequenceIndex'] = $this->read_ushort();
					$SubstLookupRecord[$p]['LookupListIndex'] = $this->read_ushort();
				}
				for ($p=0;$p<$SubstCount;$p++) {
					// Apply  $SubstLookupRecord[$p]['LookupListIndex']  at   $SubstLookupRecord[$p]['SequenceIndex']
					if ($SubstLookupRecord[$p]['SequenceIndex'] >= $InputGlyphCount) { continue; }
					$lu = $SubstLookupRecord[$p]['LookupListIndex'];
					$luType = $this->GSUBLookups[$lu]['Type'];
					$luFlag = $this->GSUBLookups[$lu]['Flag'];
					$luMarkFilteringSet = $this->GSUBLookups[$lu]['MarkFilteringSet'];

					$luptr = $matched[$SubstLookupRecord[$p]['SequenceIndex']];
					$lucurrGlyph = $this->OTLdata[$luptr]['hex'];
					$lucurrGID = $this->OTLdata[$luptr]['uni'];

					foreach($this->GSUBLookups[$lu]['Subtables'] AS $luc=>$lusubtable_offset) {
						$shift = $this->_applyGSUBsubtable($lu, $luc, $luptr, $lucurrGlyph, $lucurrGID, ($lusubtable_offset - $this->GSUB_offset), $luType, $luFlag, $luMarkFilteringSet, $this->GSLuCoverage[$lu][$luc], 1, $currentTag, $is_old_spec, $tagInt);
						if ($shift) { break; }
					}
				}
				if (!defined("OMIT_OTL_FIX_3") || OMIT_OTL_FIX_3 != 1) { return (isset($shift) ? $shift : 0) ; }	/* OTL_FIX_3 */
				else return $InputGlyphCount ;	// should be + matched ignores in Input Sequence
			}

			return 0;

		}
	}

	else { die("GSUB Lookup Type ".$Type." not supported."); }

}

function _updateLigatureMarks($pos, $n) {
	if ($n > 0) {
		// Update position of Ligatures and associated Marks
		// Foreach lig/assocMarks
		// Any position lpos or mpos > $pos + count($substitute)
		//	$this->assocMarks = array(); 	// assocMarks[$pos mpos] => array(compID, ligPos)
		//	$this->assocLigs = array();	// Ligatures[$pos lpos] => nc
		for ($p=count($this->OTLdata)-1;$p>=($pos+$n);$p--) {
			if (isset($this->assocLigs[$p])) {
				$tmp = $this->assocLigs[$p];
				unset($this->assocLigs[$p]); 
				$this->assocLigs[($p + $n)] = $tmp;
			}
		}
		for ($p=count($this->OTLdata)-1;$p>=0;$p--) {
			if (isset($this->assocMarks[$p])) {
				if ($this->assocMarks[$p]['ligPos'] >=($pos+$n)) { $this->assocMarks[$p]['ligPos'] += $n; }
				if ($p>=($pos+$n)) {
					$tmp = $this->assocMarks[$p];
					unset($this->assocMarks[$p]); 
					$this->assocMarks[($p + $n)] = $tmp;
				}
			}
		}
	}

	else if ($n<1) { // glyphs removed
		$nrem = -$n;
		// Update position of pre-existing Ligatures and associated Marks
		for ($p=($pos+1);$p<count($this->OTLdata);$p++) {
			if (isset($this->assocLigs[$p])) {
				$tmp = $this->assocLigs[$p];
				unset($this->assocLigs[$p]); 
				$this->assocLigs[($p - $nrem)] = $tmp;
			}
		}
		for ($p=0;$p<count($this->OTLdata);$p++) {
			if (isset($this->assocMarks[$p])) {
				if ($this->assocMarks[$p]['ligPos'] >=($pos)) { $this->assocMarks[$p]['ligPos'] -= $nrem; }
				if ($p>$pos) {
					$tmp = $this->assocMarks[$p];
					unset($this->assocMarks[$p]); 
					$this->assocMarks[($p - $nrem)] = $tmp;
				}
			}
		}
	}
}

function GSUBsubstitute($pos, $substitute, $Type, $GlyphPos=NULL ) {

	// LookupType 1: Simple Substitution Subtable : 1 to 1
	// LookupType 3: Alternate Forms : 1 to 1(n)
	if ($Type == 1 || $Type == 3) {
		$this->OTLdata[$pos]['uni'] = $substitute;
		$this->OTLdata[$pos]['hex'] = $this->unicode_hex($substitute);
		return 1;
	}
	// LookupType 2: Multiple Substitution Subtable : 1 to n
	else if ($Type == 2) {
		for($i=0;$i<count($substitute);$i++) {
			$uni = $substitute[$i];
			$newOTLdata[$i] = array();
			$newOTLdata[$i]['uni'] = $uni;
			$newOTLdata[$i]['hex'] = $this->unicode_hex($uni);


			// Get types of new inserted chars - or replicate type of char being replaced
		//	$bt = UCDN::get_bidi_class($uni);
		//	if (!$bt) { 
				$bt = $this->OTLdata[$pos]['bidi_type']; 
		//	}

			if (strpos($this->GlyphClassMarks, $newOTLdata[$i]['hex'] )!==false) { $gp = 'M'; }
			else if ($uni == 32) { $gp = 'S'; }
			else { $gp = 'C'; }

			// Need to update matra_type ??? of new glyphs inserted ???????????????????????????????????????

			$newOTLdata[$i]['bidi_type'] = $bt;
			$newOTLdata[$i]['group'] = $gp;

			// Need to update details of new glyphs inserted 
			$newOTLdata[$i]['general_category'] = $this->OTLdata[$pos]['general_category'];

			if ($this->shaper=='I' || $this->shaper=='K' || $this->shaper=='S') {
				$newOTLdata[$i]['indic_category'] = $this->OTLdata[$pos]['indic_category'];
				$newOTLdata[$i]['indic_position'] = $this->OTLdata[$pos]['indic_position'];
			}
			else if ($this->shaper=='M') {
				$newOTLdata[$i]['myanmar_category'] = $this->OTLdata[$pos]['myanmar_category'];
				$newOTLdata[$i]['myanmar_position'] = $this->OTLdata[$pos]['myanmar_position'];
			}
			if (isset($this->OTLdata[$pos]['mask'])) { $newOTLdata[$i]['mask'] = $this->OTLdata[$pos]['mask']; }
			if (isset($this->OTLdata[$pos]['syllable'])) { $newOTLdata[$i]['syllable'] = $this->OTLdata[$pos]['syllable']; }

		}
		if ($this->shaper=='K' || $this->shaper=='T' || $this->shaper=='L') {
			if ($this->OTLdata[$pos]['wordend']) { $newOTLdata[count($substitute)-1]['wordend'] = true; }
		}

		array_splice($this->OTLdata, $pos, 1, $newOTLdata);	// Replace 1 with n
		// Update position of Ligatures and associated Marks
		// count($substitute)-1  is the number of glyphs added
		$nadd = count($substitute)-1;
		$this->_updateLigatureMarks($pos, $nadd);
		return count($substitute);
	}
	// LookupType 4: Ligature Substitution Subtable : n to 1
	else if ($Type == 4) {
		// Create Ligatures and associated Marks
		$firstGlyph = $this->OTLdata[$pos]['hex'];

		// If all components of the ligature are marks (and in the same syllable), we call this a mark ligature.
		$contains_marks = false;
		$contains_nonmarks = false;
		if (isset($this->OTLdata[$pos]['syllable'])) { $current_syllable = $this->OTLdata[$pos]['syllable']; }
		else { $current_syllable = 0; }
		for($i=0;$i<count($GlyphPos);$i++) {
			// If subsequent components are not Marks as well - don't ligate
			$unistr = $this->OTLdata[$GlyphPos[$i]]['hex'];
			if ($this->restrictToSyllable && isset($this->OTLdata[$GlyphPos[$i]]['syllable']) && $this->OTLdata[$GlyphPos[$i]]['syllable'] != $current_syllable) {
				return 0;
			}
			if (strpos($this->GlyphClassMarks, $unistr )!==false) { $contains_marks = true; }
			else  { $contains_nonmarks = true; }
		}
		if ($contains_marks && !$contains_nonmarks) {
			// Mark Ligature (all components are Marks)
			$firstMarkAssoc = '';
			if (isset($this->assocMarks[$pos])) { 
				$firstMarkAssoc = $this->assocMarks[$pos]; 
			}
			// If all components of the ligature are marks, we call this a mark ligature.
			for($i=1;$i<count($GlyphPos);$i++) {

				// If subsequent components are not Marks as well - don't ligate
		//		$unistr = $this->OTLdata[$GlyphPos[$i]]['hex'];
		//		if (strpos($this->GlyphClassMarks, $unistr )===false) { return; }

				$nextMarkAssoc = '';
				if (isset($this->assocMarks[$GlyphPos[$i]])) {
					$nextMarkAssoc = $this->assocMarks[$GlyphPos[$i]];
				}
				// If first component was attached to a previous ligature component,
				// all subsequent components should be attached to the same ligature
				// component, otherwise we shouldn't ligate them.
				// If first component was NOT attached to a previous ligature component,
				// all subsequent components should also NOT be attached to any ligature component,
				if ($firstMarkAssoc != $nextMarkAssoc ) { 
					// unless they are attached to the first component itself!
		//			if (!is_array($nextMarkAssoc) || $nextMarkAssoc['ligPos']!= $pos) { return; }

					// Update/Edit - In test with myanmartext font 
					// &#x1004;&#x103a;&#x1039;&#x1000;&#x1039;&#x1000;&#x103b;&#x103c;&#x103d;&#x1031;&#x102d;
					// => Lookup 17  E003 E066B E05A 102D
					// E003 and 102D should form a mark ligature, but 102D is already associated with (non-mark) ligature E05A
					// So instead of disallowing the mark ligature to form, just dissociate...
					if (!is_array($nextMarkAssoc) || $nextMarkAssoc['ligPos']!= $pos) { unset($this->assocMarks[$GlyphPos[$i]]); }
				}
			}

  /*
   * - If it *is* a mark ligature, we don't allocate a new ligature id, and leave
   *   the ligature to keep its old ligature id.  This will allow it to attach to
   *   a base ligature in GPOS.  Eg. if the sequence is: LAM,LAM,SHADDA,FATHA,HEH,
   *   and LAM,LAM,HEH form a ligature, they will leave SHADDA and FATHA wit a
   *   ligature id and component value of 2.  Then if SHADDA,FATHA form a ligature
   *   later, we don't want them to lose their ligature id/component, otherwise
   *   GPOS will fail to correctly position the mark ligature on top of the
   *   LAM,LAM,HEH ligature. 
   */
			// So if is_array($firstMarkAssoc) - the new (Mark) ligature should keep this association

			$lastPos = $GlyphPos[(count($GlyphPos)-1)];
		}
		else {
/*
   * - Ligatures cannot be formed across glyphs attached to different components
   *   of previous ligatures.  Eg. the sequence is LAM,SHADDA,LAM,FATHA,HEH, and
   *   LAM,LAM,HEH form a ligature, leaving SHADDA,FATHA next to eachother.
   *   However, it would be wrong to ligate that SHADDA,FATHA sequence.
   *   There is an exception to this: If a ligature tries ligating with marks that
   *   belong to it itself, go ahead, assuming that the font designer knows what
   *   they are doing (otherwise it can break Indic stuff when a matra wants to
   *   ligate with a conjunct...)
   */

  /*
   * - If a ligature is formed of components that some of which are also ligatures
   *   themselves, and those ligature components had marks attached to *their*
   *   components, we have to attach the marks to the new ligature component
   *   positions!  Now *that*'s tricky!  And these marks may be following the
   *   last component of the whole sequence, so we should loop forward looking
   *   for them and update them.
   *
   *   Eg. the sequence is LAM,LAM,SHADDA,FATHA,HEH, and the font first forms a
   *   'calt' ligature of LAM,HEH, leaving the SHADDA and FATHA with a ligature
   *   id and component == 1.  Now, during 'liga', the LAM and the LAM-HEH ligature
   *   form a LAM-LAM-HEH ligature.  We need to reassign the SHADDA and FATHA to
   *   the new ligature with a component value of 2.
   *
   *   This in fact happened to a font...  See:
   *   https://bugzilla.gnome.org/show_bug.cgi?id=437633
   */

			$currComp = 0;
			for($i=0;$i<count($GlyphPos);$i++) {
				if ($i>0 && isset($this->assocLigs[$GlyphPos[$i]])) {	// One of the other components is already a ligature
					$nc = $this->assocLigs[$GlyphPos[$i]];
				}
				else { $nc = 1; }
			 	// While next char to right is a mark (but not the next matched glyph)
				// ?? + also include a Mark Ligature here
				$ic = 1;
				while((($i==count($GlyphPos)-1) || (isset($GlyphPos[$i+1]) && ($GlyphPos[$i]+$ic) < $GlyphPos[$i+1])) && isset($this->OTLdata[($GlyphPos[$i]+$ic)]) && strpos($this->GlyphClassMarks, $this->OTLdata[($GlyphPos[$i]+$ic)]['hex'])!== false) {
					$newComp = $currComp;
					if (isset($this->assocMarks[$GlyphPos[$i]+$ic])) {	// One of the inbetween Marks is already associated with a Lig
						// OK as long as it is associated with the current Lig
				//		if ($this->assocMarks[($GlyphPos[$i]+$ic)]['ligPos'] != ($GlyphPos[$i]+$ic)) { die("Problem #1"); }
						$newComp += $this->assocMarks[($GlyphPos[$i]+$ic)]['compID']; 
					}
					$this->assocMarks[($GlyphPos[$i]+$ic)] = array('compID'=>$newComp, 'ligPos'=>$pos); 
					$ic++;
				}
				$currComp += $nc;
			}
			$lastPos = $GlyphPos[(count($GlyphPos)-1)]+$ic-1;
			$this->assocLigs[$pos] = $currComp ;	// Number of components in new Ligature
		}

		// Now remove the unwanted glyphs and associated metadata
		$newOTLdata[0] = array();

		// Get types of new inserted chars - or replicate type of char being replaced
	//	$bt = UCDN::get_bidi_class($substitute);
	//	if (!$bt) { 
			$bt = $this->OTLdata[$pos]['bidi_type']; 
	//	}

		if (strpos($this->GlyphClassMarks, $this->unicode_hex($substitute))!==false) { $gp = 'M'; }
		else if ($substitute == 32) { $gp = 'S'; }
		else { $gp = 'C'; }

		// Need to update details of new glyphs inserted 
		$newOTLdata[0]['general_category'] = $this->OTLdata[$pos]['general_category'];

		$newOTLdata[0]['bidi_type'] = $bt;
		$newOTLdata[0]['group'] = $gp;

		// KASHIDA: If forming a ligature when the last component was identified as a kashida point (final form)
		// If previous/first component of ligature is a medial form, then keep this as a kashida point
		// TEST (Arabic Typesetting) &#x64a;&#x64e;&#x646;&#x62a;&#x64f;&#x645;
		$ka = 0;
		if (isset($this->OTLdata[$GlyphPos[(count($GlyphPos)-1)]]['GPOSinfo']['kashida'])) {
			$ka = $this->OTLdata[$GlyphPos[(count($GlyphPos)-1)]]['GPOSinfo']['kashida']; 
		}
		if ($ka==1 && isset($this->OTLdata[$pos]['form']) && $this->OTLdata[$pos]['form']==3) { $newOTLdata[0]['GPOSinfo']['kashida'] = $ka; }

		$newOTLdata[0]['uni'] = $substitute;
		$newOTLdata[0]['hex'] = $this->unicode_hex($substitute);

		if ($this->shaper=='I' || $this->shaper=='K' || $this->shaper=='S') {
			$newOTLdata[0]['indic_category'] = $this->OTLdata[$pos]['indic_category'];
			$newOTLdata[0]['indic_position'] = $this->OTLdata[$pos]['indic_position'];
		}
		else if ($this->shaper=='M') {
			$newOTLdata[0]['myanmar_category'] = $this->OTLdata[$pos]['myanmar_category'];
			$newOTLdata[0]['myanmar_position'] = $this->OTLdata[$pos]['myanmar_position'];
		}
		if (isset($this->OTLdata[$pos]['mask'])) {$newOTLdata[0]['mask'] = $this->OTLdata[$pos]['mask']; }
		if (isset($this->OTLdata[$pos]['syllable'])) {$newOTLdata[0]['syllable'] = $this->OTLdata[$pos]['syllable']; }

		$newOTLdata[0]['is_ligature'] = true;


		array_splice($this->OTLdata, $pos, 1, $newOTLdata);

		// GlyphPos contains array of arr_pos to set null - not necessarily contiguous

		// +- Remove any assocMarks or assocLigs from the main components (the ones that are deleted)
		for($i=count($GlyphPos)-1;$i>0;$i--) {
			$gpos = $GlyphPos[$i];
			array_splice($this->OTLdata, $gpos, 1);
			unset($this->assocLigs[$gpos]);
			unset($this->assocMarks[$gpos]);
		}
		//	$this->assocLigs = array();	// Ligatures[$posarr lpos] => nc
		//	$this->assocMarks = array(); 	// assocMarks[$posarr mpos] => array(compID, ligPos)

		// Update position of pre-existing Ligatures and associated Marks
		// Start after first GlyphPos
		// count($GlyphPos)-1  is the number of glyphs removed from string
		for($p=($GlyphPos[0]+1);$p<(count($this->OTLdata)+count($GlyphPos)-1) ;$p++ ) {
			$nrem = 0;	// Number of Glyphs removed at this point in the string
			for($i=0;$i<count($GlyphPos);$i++) {
				if ($i>0 && $p > $GlyphPos[$i]) { $nrem++; }
			}
			if (isset($this->assocLigs[$p])) {
				$tmp = $this->assocLigs[$p];
				unset($this->assocLigs[$p]); 
				$this->assocLigs[($p - $nrem)] = $tmp;
			}
			if (isset($this->assocMarks[$p])) {
				$tmp = $this->assocMarks[$p];
				unset($this->assocMarks[$p]); 
				if ($tmp['ligPos'] > $GlyphPos[0]) { $tmp['ligPos'] -= $nrem; }
				$this->assocMarks[($p - $nrem)] = $tmp;
			}
		}
		return 1;
	}
	else { return 0; }
}



////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////
//////////       ARABIC        /////////////////////////////////
////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////

function arabic_initialise() {
		// cf. http://unicode.org/Public/UNIDATA/ArabicShaping.txt
		// http://unicode.org/Public/UNIDATA/extracted/DerivedJoiningType.txt
		// JOIN TO FOLLOWING LETTER IN LOGICAL ORDER (i.e. AS INITIAL/MEDIAL FORM) = Unicode Left-Joining (+ Dual-Joining + Join_Causing 00640)
		$this->arabLeftJoining = array(
		0x0620=>1, 0x0626=>1, 0x0628=>1, 0x062A=>1, 0x062B=>1, 0x062C=>1, 0x062D=>1, 0x062E=>1, 
		0x0633=>1, 0x0634=>1, 0x0635=>1, 0x0636=>1, 0x0637=>1, 0x0638=>1, 0x0639=>1, 0x063A=>1, 
		0x063B=>1, 0x063C=>1, 0x063D=>1, 0x063E=>1, 0x063F=>1, 0x0640=>1, 0x0641=>1, 0x0642=>1, 
		0x0643=>1, 0x0644=>1, 0x0645=>1, 0x0646=>1, 0x0647=>1, 0x0649=>1, 0x064A=>1, 0x066E=>1, 
		0x066F=>1, 0x0678=>1, 0x0679=>1, 0x067A=>1, 0x067B=>1, 0x067C=>1, 0x067D=>1, 0x067E=>1, 
		0x067F=>1, 0x0680=>1, 0x0681=>1, 0x0682=>1, 0x0683=>1, 0x0684=>1, 0x0685=>1, 0x0686=>1, 
		0x0687=>1, 0x069A=>1, 0x069B=>1, 0x069C=>1, 0x069D=>1, 0x069E=>1, 0x069F=>1, 0x06A0=>1, 
		0x06A1=>1, 0x06A2=>1, 0x06A3=>1, 0x06A4=>1, 0x06A5=>1, 0x06A6=>1, 0x06A7=>1, 0x06A8=>1, 
		0x06A9=>1, 0x06AA=>1, 0x06AB=>1, 0x06AC=>1, 0x06AD=>1, 0x06AE=>1, 0x06AF=>1, 0x06B0=>1, 
		0x06B1=>1, 0x06B2=>1, 0x06B3=>1, 0x06B4=>1, 0x06B5=>1, 0x06B6=>1, 0x06B7=>1, 0x06B8=>1, 
		0x06B9=>1, 0x06BA=>1, 0x06BB=>1, 0x06BC=>1, 0x06BD=>1, 0x06BE=>1, 0x06BF=>1, 0x06C1=>1, 
		0x06C2=>1, 0x06CC=>1, 0x06CE=>1, 0x06D0=>1, 0x06D1=>1, 0x06FA=>1, 0x06FB=>1, 0x06FC=>1, 
		0x06FF=>1,
		/* Arabic Supplement */
		0x0750=>1, 0x0751=>1, 0x0752=>1, 0x0753=>1, 0x0754=>1, 0x0755=>1, 0x0756=>1, 0x0757=>1, 
		0x0758=>1, 0x075C=>1, 0x075D=>1, 0x075E=>1, 0x075F=>1, 0x0760=>1, 0x0761=>1, 0x0762=>1, 
		0x0763=>1, 0x0764=>1, 0x0765=>1, 0x0766=>1, 0x0767=>1, 0x0768=>1, 0x0769=>1, 0x076A=>1, 
		0x076D=>1, 0x076E=>1, 0x076F=>1, 0x0770=>1, 0x0772=>1, 0x0775=>1, 0x0776=>1, 0x0777=>1, 
		0x077A=>1, 0x077B=>1, 0x077C=>1, 0x077D=>1, 0x077E=>1, 0x077F=>1,
		/* Extended Arabic */
		0x08A0=>1, 0x08A2=>1, 0x08A3=>1, 0x08A4=>1, 0x08A5=>1, 0x08A6=>1, 0x08A7=>1, 0x08A8=>1, 
		0x08A9=>1,
		/* 'syrc' Syriac */
		0x0712=>1, 0x0713=>1, 0x0714=>1, 0x071A=>1, 0x071B=>1, 0x071C=>1, 0x071D=>1, 0x071F=>1, 
		0x0720=>1, 0x0721=>1, 0x0722=>1, 0x0723=>1, 0x0724=>1, 0x0725=>1, 0x0726=>1, 0x0727=>1, 
		0x0729=>1, 0x072B=>1, 0x072D=>1, 0x072E=>1, 0x074E=>1, 0x074F=>1,
		/* N'Ko */
		0x07CA=>1, 0x07CB=>1, 0x07CC=>1, 0x07CD=>1, 0x07CE=>1, 0x07CF=>1, 0x07D0=>1, 0x07D1=>1, 
		0x07D2=>1, 0x07D3=>1, 0x07D4=>1, 0x07D5=>1, 0x07D6=>1, 0x07D7=>1, 0x07D8=>1, 0x07D9=>1, 
		0x07DA=>1, 0x07DB=>1, 0x07DC=>1, 0x07DD=>1, 0x07DE=>1, 0x07DF=>1, 0x07E0=>1, 0x07E1=>1, 
		0x07E2=>1, 0x07E3=>1, 0x07E4=>1, 0x07E5=>1, 0x07E6=>1, 0x07E7=>1, 0x07E8=>1, 0x07E9=>1, 
		0x07EA=>1, 0x07FA=>1,
		/* Mandaic */
		0x0841=>1, 0x0842=>1, 0x0843=>1, 0x0844=>1, 0x0845=>1, 0x0847=>1, 0x0848=>1, 0x084A=>1, 
		0x084B=>1, 0x084C=>1, 0x084D=>1, 0x084E=>1, 0x0850=>1, 0x0851=>1, 0x0852=>1, 0x0853=>1, 
		0x0855=>1, 
		/* ZWJ U+200D */
		0x0200D=>1);

		/* JOIN TO PREVIOUS LETTER IN LOGICAL ORDER (i.e. AS FINAL/MEDIAL FORM) = Unicode Right-Joining (+ Dual-Joining + Join_Causing) */
		$this->arabRightJoining = array(
		0x0620=>1, 0x0622=>1, 0x0623=>1, 0x0624=>1, 0x0625=>1, 0x0626=>1, 0x0627=>1, 0x0628=>1, 
		0x0629=>1, 0x062A=>1, 0x062B=>1, 0x062C=>1, 0x062D=>1, 0x062E=>1, 0x062F=>1, 0x0630=>1, 
		0x0631=>1, 0x0632=>1, 0x0633=>1, 0x0634=>1, 0x0635=>1, 0x0636=>1, 0x0637=>1, 0x0638=>1, 
		0x0639=>1, 0x063A=>1, 0x063B=>1, 0x063C=>1, 0x063D=>1, 0x063E=>1, 0x063F=>1, 0x0640=>1, 
		0x0641=>1, 0x0642=>1, 0x0643=>1, 0x0644=>1, 0x0645=>1, 0x0646=>1, 0x0647=>1, 0x0648=>1, 
		0x0649=>1, 0x064A=>1, 0x066E=>1, 0x066F=>1, 0x0671=>1, 0x0672=>1, 0x0673=>1, 0x0675=>1, 
		0x0676=>1, 0x0677=>1, 0x0678=>1, 0x0679=>1, 0x067A=>1, 0x067B=>1, 0x067C=>1, 0x067D=>1, 
		0x067E=>1, 0x067F=>1, 0x0680=>1, 0x0681=>1, 0x0682=>1, 0x0683=>1, 0x0684=>1, 0x0685=>1, 
		0x0686=>1, 0x0687=>1, 0x0688=>1, 0x0689=>1, 0x068A=>1, 0x068B=>1, 0x068C=>1, 0x068D=>1, 
		0x068E=>1, 0x068F=>1, 0x0690=>1, 0x0691=>1, 0x0692=>1, 0x0693=>1, 0x0694=>1, 0x0695=>1, 
		0x0696=>1, 0x0697=>1, 0x0698=>1, 0x0699=>1, 0x069A=>1, 0x069B=>1, 0x069C=>1, 0x069D=>1, 
		0x069E=>1, 0x069F=>1, 0x06A0=>1, 0x06A1=>1, 0x06A2=>1, 0x06A3=>1, 0x06A4=>1, 0x06A5=>1, 
		0x06A6=>1, 0x06A7=>1, 0x06A8=>1, 0x06A9=>1, 0x06AA=>1, 0x06AB=>1, 0x06AC=>1, 0x06AD=>1, 
		0x06AE=>1, 0x06AF=>1, 0x06B0=>1, 0x06B1=>1, 0x06B2=>1, 0x06B3=>1, 0x06B4=>1, 0x06B5=>1, 
		0x06B6=>1, 0x06B7=>1, 0x06B8=>1, 0x06B9=>1, 0x06BA=>1, 0x06BB=>1, 0x06BC=>1, 0x06BD=>1, 
		0x06BE=>1, 0x06BF=>1, 0x06C0=>1, 0x06C1=>1, 0x06C2=>1, 0x06C3=>1, 0x06C4=>1, 0x06C5=>1, 
		0x06C6=>1, 0x06C7=>1, 0x06C8=>1, 0x06C9=>1, 0x06CA=>1, 0x06CB=>1, 0x06CC=>1, 0x06CD=>1, 
		0x06CE=>1, 0x06CF=>1, 0x06D0=>1, 0x06D1=>1, 0x06D2=>1, 0x06D3=>1, 0x06D5=>1, 0x06EE=>1, 
		0x06EF=>1, 0x06FA=>1, 0x06FB=>1, 0x06FC=>1, 0x06FF=>1,
		/* Arabic Supplement */
		0x0750=>1, 0x0751=>1, 0x0752=>1, 0x0753=>1, 0x0754=>1, 0x0755=>1, 0x0756=>1, 0x0757=>1, 
		0x0758=>1, 0x0759=>1, 0x075A=>1, 0x075B=>1, 0x075C=>1, 0x075D=>1, 0x075E=>1, 0x075F=>1, 
		0x0760=>1, 0x0761=>1, 0x0762=>1, 0x0763=>1, 0x0764=>1, 0x0765=>1, 0x0766=>1, 0x0767=>1, 
		0x0768=>1, 0x0769=>1, 0x076A=>1, 0x076B=>1, 0x076C=>1, 0x076D=>1, 0x076E=>1, 0x076F=>1, 
		0x0770=>1, 0x0771=>1, 0x0772=>1, 0x0773=>1, 0x0774=>1, 0x0775=>1, 0x0776=>1, 0x0777=>1, 
		0x0778=>1, 0x0779=>1, 0x077A=>1, 0x077B=>1, 0x077C=>1, 0x077D=>1, 0x077E=>1, 0x077F=>1,
		/* Extended Arabic */
		0x08A0=>1, 0x08A2=>1, 0x08A3=>1, 0x08A4=>1, 0x08A5=>1, 0x08A6=>1, 0x08A7=>1, 0x08A8=>1, 
		0x08A9=>1, 0x08AA=>1, 0x08AB=>1, 0x08AC=>1,
		/* 'syrc' Syriac */
		0x0710=>1, 0x0712=>1, 0x0713=>1, 0x0714=>1, 0x0715=>1, 0x0716=>1, 0x0717=>1, 0x0718=>1, 
		0x0719=>1, 0x071A=>1, 0x071B=>1, 0x071C=>1, 0x071D=>1, 0x071E=>1, 0x071F=>1, 0x0720=>1, 
		0x0721=>1, 0x0722=>1, 0x0723=>1, 0x0724=>1, 0x0725=>1, 0x0726=>1, 0x0727=>1, 0x0728=>1, 
		0x0729=>1, 0x072A=>1, 0x072B=>1, 0x072C=>1, 0x072D=>1, 0x072E=>1, 0x072F=>1, 0x074D=>1, 
		0x074E=>1, 0x074F,
		/* N'Ko */
		0x07CA=>1, 0x07CB=>1, 0x07CC=>1, 0x07CD=>1, 0x07CE=>1, 0x07CF=>1, 0x07D0=>1, 0x07D1=>1, 
		0x07D2=>1, 0x07D3=>1, 0x07D4=>1, 0x07D5=>1, 0x07D6=>1, 0x07D7=>1, 0x07D8=>1, 0x07D9=>1, 
		0x07DA=>1, 0x07DB=>1, 0x07DC=>1, 0x07DD=>1, 0x07DE=>1, 0x07DF=>1, 0x07E0=>1, 0x07E1=>1, 
		0x07E2=>1, 0x07E3=>1, 0x07E4=>1, 0x07E5=>1, 0x07E6=>1, 0x07E7=>1, 0x07E8=>1, 0x07E9=>1, 
		0x07EA=>1, 0x07FA=>1,
		/* Mandaic */
		0x0841=>1, 0x0842=>1, 0x0843=>1, 0x0844=>1, 0x0845=>1, 0x0847=>1, 0x0848=>1, 0x084A=>1, 
		0x084B=>1, 0x084C=>1, 0x084D=>1, 0x084E=>1, 0x0850=>1, 0x0851=>1, 0x0852=>1, 0x0853=>1, 
		0x0855=>1, 
		0x0840=>1, 0x0846=>1, 0x0849=>1, 0x084F=>1, 0x0854=>1, /* Right joining */
		/* ZWJ U+200D */
		0x0200D=>1);


		/* VOWELS = TRANSPARENT-JOINING = Unicode Transparent-Joining type (not just vowels) */
		$this->arabTransparent = array(
		0x0610=>1, 0x0611=>1, 0x0612=>1, 0x0613=>1, 0x0614=>1, 0x0615=>1, 0x0616=>1, 0x0617=>1, 
		0x0618=>1, 0x0619=>1, 0x061A=>1, 0x064B=>1, 0x064C=>1, 0x064D=>1, 0x064E=>1, 0x064F=>1, 
		0x0650=>1, 0x0651=>1, 0x0652=>1, 0x0653=>1, 0x0654=>1, 0x0655=>1, 0x0656=>1, 0x0657=>1, 
		0x0658=>1, 0x0659=>1, 0x065A=>1, 0x065B=>1, 0x065C=>1, 0x065D=>1, 0x065E=>1, 0x065F=>1, 
		0x0670=>1, 0x06D6=>1, 0x06D7=>1, 0x06D8=>1, 0x06D9=>1, 0x06DA=>1, 0x06DB=>1, 0x06DC=>1, 
		0x06DF=>1, 0x06E0=>1, 0x06E1=>1, 0x06E2=>1, 0x06E3=>1, 0x06E4=>1, 0x06E7=>1, 0x06E8=>1, 
		0x06EA=>1, 0x06EB=>1, 0x06EC=>1, 0x06ED=>1,
		/* Extended Arabic */
		0x08E4=>1, 0x08E5=>1, 0x08E6=>1, 0x08E7=>1, 0x08E8=>1, 0x08E9=>1, 0x08EA=>1, 0x08EB=>1, 
		0x08EC=>1, 0x08ED=>1, 0x08EE=>1, 0x08EF=>1, 0x08F0=>1, 0x08F1=>1, 0x08F2=>1, 0x08F3=>1, 
		0x08F4=>1, 0x08F5=>1, 0x08F6=>1, 0x08F7=>1, 0x08F8=>1, 0x08F9=>1, 0x08FA=>1, 0x08FB=>1, 
		0x08FC=>1, 0x08FD=>1, 0x08FE=>1,
		/* Arabic ligatures in presentation form (converted in 'ccmp' in e.g. Arial and Times ? need to add others in this range) */
		0xFC5E=>1, 0xFC5F=>1, 0xFC60=>1, 0xFC61=>1, 0xFC62=>1,
		/*  'syrc' Syriac */
		0x070F=>1, 0x0711=>1, 0x0730=>1, 0x0731=>1, 0x0732=>1, 0x0733=>1, 0x0734=>1, 0x0735=>1, 
		0x0736=>1, 0x0737=>1, 0x0738=>1, 0x0739=>1, 0x073A=>1, 0x073B=>1, 0x073C=>1, 0x073D=>1, 
		0x073E=>1, 0x073F=>1, 0x0740=>1, 0x0741=>1, 0x0742=>1, 0x0743=>1, 0x0744=>1, 0x0745=>1, 
		0x0746=>1, 0x0747=>1, 0x0748=>1, 0x0749=>1, 0x074A=>1,
		/* N'Ko */
		0x07EB=>1, 0x07EC=>1, 0x07ED=>1, 0x07EE=>1, 0x07EF=>1, 0x07F0=>1, 0x07F1=>1, 0x07F2=>1, 
		0x07F3=>1,
		/* Mandaic */
		0x0859=>1, 0x085A=>1, 0x085B=>1,
		);

}


function arabic_shaper($usetags, $scriptTag) {
		$chars = array();
		for($i=0;$i<count($this->OTLdata);$i++) {
			$chars[] = $this->OTLdata[$i]['hex'];
		}
		$crntChar = null;
		$prevChar = null;
		$nextChar = null;
		$output = array();
		$max = count($chars);
		for ($i = $max - 1; $i >= 0; $i--) {
			$crntChar = $chars[$i];
			if ($i > 0){ $prevChar = hexdec($chars[$i - 1]); }
			else{ $prevChar = NULL; }
			if ($prevChar && isset($this->arabTransparentJoin[$prevChar]) && isset($chars[$i - 2]) ) {
				$prevChar = hexdec($chars[$i - 2]);
				if ($prevChar && isset($this->arabTransparentJoin[$prevChar]) && isset($chars[$i - 3])) {
					$prevChar = hexdec($chars[$i - 3]);
					if ($prevChar && isset($this->arabTransparentJoin[$prevChar]) && isset($chars[$i - 4])) {
						$prevChar = hexdec($chars[$i - 4]);
					}
				}
			}
			if ($crntChar && isset($this->arabTransparentJoin[ hexdec($crntChar)]) ) {
				// If next_char = RightJoining && prev_char = LeftJoining:
				if (isset($chars[$i + 1]) && $chars[$i + 1] && isset($this->arabRightJoining[ hexdec($chars[$i + 1]) ])  && $prevChar && isset($this->arabLeftJoining[$prevChar])) {
					$output[] = $this->get_arab_glyphs($crntChar, 1, $chars, $i, $scriptTag, $usetags);	// <final> form
				} 
				else {
					$output[] = $this->get_arab_glyphs($crntChar, 0, $chars, $i, $scriptTag, $usetags);  // <isolated> form
				}
				continue;
			}
			if (hexdec($crntChar) < 128) {
				$output[] = array($crntChar,0);
				$nextChar = $crntChar;
				continue;
			}
			// 0=ISOLATED FORM :: 1=FINAL :: 2=INITIAL :: 3=MEDIAL
			$form = 0;
			if ($prevChar && isset($this->arabLeftJoining[$prevChar])) {
				$form++;
			}
			if ($nextChar && isset($this->arabRightJoining[ hexdec($nextChar) ])) {
				$form += 2;
			}
			$output[] = $this->get_arab_glyphs($crntChar, $form, $chars, $i, $scriptTag, $usetags) ;
			$nextChar = $crntChar;
		}
		$ra = array_reverse($output);
		for($i=0;$i<count($this->OTLdata);$i++) {
			$this->OTLdata[$i]['uni'] = hexdec($ra[$i][0]);
			$this->OTLdata[$i]['hex'] = $ra[$i][0];
			$this->OTLdata[$i]['form'] = $ra[$i][1];	// Actaul form substituted 0=ISOLATED FORM :: 1=FINAL :: 2=INITIAL :: 3=MEDIAL
		}
}


function get_arab_glyphs($char, $type, &$chars, $i, $scriptTag, $usetags) {

	// Optional Feature settings	// doesn't control Syriac at present
	if (($type===0 && strpos($usetags, 'isol')===false) || ($type===1 && strpos($usetags, 'fina')===false) || ($type===2 && strpos($usetags, 'init')===false) || ($type===3 && strpos($usetags, 'medi')===false)) {
		return array($char,0);
	}

	// 0=ISOLATED FORM :: 1=FINAL :: 2=INITIAL :: 3=MEDIAL (:: 4=MED2 :: 5=FIN2 :: 6=FIN3)
	$retk = -1;
	// Alaph 00710 in Syriac
	if ($scriptTag=='syrc' && $char=='00710') {
		// if there is a preceding (base?) character *** should search back to previous base - ignoring vowels and change $n
		// set $n as the position of the last base; for now we'll just do this:
		$n = $i-1;
		// if the preceding (base) character cannot be joined to
		// not in $this->arabLeftJoining i.e. not a char which can join to the next one
		if (isset($chars[$n]) && isset($this->arabLeftJoining[hexdec($chars[$n])])) {
			// if in the middle of Syriac words
			if (isset($chars[$i+1]) && preg_match('/[\x{0700}-\x{0745}]/u',code2utf(hexdec($chars[$n]))) && preg_match('/[\x{0700}-\x{0745}]/u',code2utf(hexdec($chars[$i+1]))) && isset($this->arabGlyphs[$char][4])) { $retk = 4; }
			// if at the end of Syriac words
			else if(!isset($chars[$i+1]) || !preg_match('/[\x{0700}-\x{0745}]/u',code2utf(hexdec($chars[$i+1])))) {
				// if preceding base character IS (00715|00716|0072A)
				if (strpos('0715|0716|072A',$chars[$n])!==false && isset($this->arabGlyphs[$char][6])) { $retk = 6; }

				// else if preceding base character is NOT (00715|00716|0072A) 
				else if (isset($this->arabGlyphs[$char][5])) { $retk = 5; }
			}
		}
		if ($retk != -1) { 
			return array($this->arabGlyphs[$char][$retk],$retk);
		}
		else { return array($char,0); }
	}

	if (($type>0 || $type===0) && isset($this->arabGlyphs[$char][$type])) {
		$retk = $type; 
	}
	else if ($type==3 && isset($this->arabGlyphs[$char][1])) {	// if <medial> not defined, but <final>, return <final>
		$retk = 1;
	}
	else if ($type==2 && isset($this->arabGlyphs[$char][0])) {	// if <initial> not defined, but <isolated>, return <isolated>
		$retk = 0;
	}
	if ($retk != -1) {
		$match = true;
		// If GSUB includes a Backtrack or Lookahead condition (e.g. font ArabicTypesetting)
		if (isset($this->arabGlyphs[$char]['prel'][$retk]) && $this->arabGlyphs[$char]['prel'][$retk]) {
			$ig = 1;
			foreach($this->arabGlyphs[$char]['prel'][$retk] AS $k=>$v) {	// $k starts 0, 1...
				if (!isset($chars[$i-$ig-$k])) { $match = false; }
				else if (strpos($v,$chars[$i-$ig-$k])===false) { 
					while (strpos($this->arabGlyphs[$char]['ignore'][$retk],$chars[$i-$ig-$k])!==false) { 	// ignore
						$ig++;
					}
					if (!isset($chars[$i-$ig-$k])) { $match = false; }
					else if (strpos($v,$chars[$i-$ig-$k])===false) { $match = false; }
				}
			}
		}
		if (isset($this->arabGlyphs[$char]['postl'][$retk]) && $this->arabGlyphs[$char]['postl'][$retk]) {
			$ig = 1;
			foreach($this->arabGlyphs[$char]['postl'][$retk] AS $k=>$v) {	// $k starts 0, 1...
				if (!isset($chars[$i+$ig+$k])) { $match = false; }
				else if (strpos($v,$chars[$i+$ig+$k])===false) { 
					while (strpos($this->arabGlyphs[$char]['ignore'][$retk],$chars[$i+$ig+$k])!==false) { 	// ignore
						$ig++;
					}
					if (!isset($chars[$i+$ig+$k])) { $match = false; }
					else if (strpos($v,$chars[$i+$ig+$k])===false) { $match = false; }
				}
			}
		}
		if ($match) {
			return array($this->arabGlyphs[$char][$retk],$retk);
		}
		else { return array($char,0); }
	}
	else { return array($char,0); }
}


////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////
/////////////////       LINE BREAKING    ///////////////////////
////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////
/////////////       TIBETAN LINE BREAKING    ///////////////////
////////////////////////////////////////////////////////////////
// Sets $this->OTLdata[$i]['wordend']=true at possible end of word boundaries
function TibetanlineBreaking() {
	for ($ptr=0; $ptr<count($this->OTLdata);$ptr++) {
		// Break opportunities at U+0F0B Tsheg or U=0F0D
		if (isset($this->OTLdata[$ptr]['uni']) && ($this->OTLdata[$ptr]['uni']==0x0F0B || $this->OTLdata[$ptr]['uni']==0x0F0D)) {
			if (isset($this->OTLdata[$ptr+1]['uni']) && ($this->OTLdata[$ptr+1]['uni']==0x0F0D || $this->OTLdata[$ptr+1]['uni']==0xF0E)) { continue; }
			// Set end of word marker in OTLdata at matchpos
			$this->OTLdata[$ptr]['wordend'] = true;
		}
	}

}

////////////////////////////////////////////////////////////////
//////////       SOUTH EAST ASIAN LINE BREAKING    /////////////
////////////////////////////////////////////////////////////////
// South East Asian Linebreaking (Thai, Khmer and Lao) using dictionary of words
// Sets $this->OTLdata[$i]['wordend']=true at possible end of word boundaries
function SEAlineBreaking() {
	// Load Line-breaking dictionary
	if (!isset($this->lbdicts[$this->shaper]) && file_exists(_MPDF_PATH.'includes/linebrdict'.$this->shaper.'.dat')) { $this->lbdicts[$this->shaper] = file_get_contents(_MPDF_PATH.'includes/linebrdict'.$this->shaper.'.dat'); }

	$dict =&$this->lbdicts[$this->shaper];

	// Find all word boundaries and mark end of word $this->OTLdata[$i]['wordend']=true on last character
	// If Thai, allow for possible suffixes (not in Lao or Khmer)

	// repeater/ellision characters
	// (0x0E2F);		// Ellision character THAI_PAIYANNOI 0x0E2F  UTF-8 0xE0 0xB8 0xAF 
	// (0x0E46);		// Repeat character THAI_MAIYAMOK 0x0E46   UTF-8 0xE0 0xB9 0x86
	// (0x0EC6);		// Repeat character LAO   UTF-8 0xE0 0xBB 0x86

	$rollover = array();
	$ptr = 0;
	while ($ptr<count($this->OTLdata)-3) {
		if (count($rollover)) {
			$matches = $rollover;
			$rollover = array();
		}
		else {
			$matches = $this->checkwordmatch($dict, $ptr);
		}
		if (count($matches)==1) {
			$matchpos = $matches[0];
			// Check for repeaters - if so $matchpos++
			if (isset($this->OTLdata[$matchpos+1]['uni']) && ($this->OTLdata[$matchpos+1]['uni']==0x0E2F || $this->OTLdata[$matchpos+1]['uni']==0x0E46 || $this->OTLdata[$matchpos+1]['uni']==0x0EC6)) { $matchpos++; }
			// Set end of word marker in OTLdata at matchpos
			$this->OTLdata[$matchpos]['wordend'] = true;
			$ptr = $matchpos + 1;
		}
		else if (empty($matches)) {
			$ptr++;
			// Move past any ASCII characters 
			while (isset($this->OTLdata[$ptr]['uni']) && ($this->OTLdata[$ptr]['uni'] >> 8) == 0) { $ptr++; }
		}
		else {	// Multiple matches
			$secondmatch = false;
			for ($m=count($matches)-1;$m>=0;$m--) {
			//for ($m=0;$m<count($matches);$m++) {
				$firstmatch = $matches[$m];
				$matches2 = $this->checkwordmatch($dict, $firstmatch+1);
				if (count($matches2)) {
					// Set end of word marker in OTLdata at matchpos
					$this->OTLdata[$firstmatch]['wordend'] = true;
					$ptr = $firstmatch + 1;
					$rollover = $matches2;
					$secondmatch = true;
					break;
				}
			}
			if (!$secondmatch) {
				// Set end of word marker in OTLdata at end of longest first match
				$this->OTLdata[$matches[count($matches)-1]]['wordend'] = true;
				$ptr = $matches[count($matches)-1] + 1;
				// Move past any ASCII characters 
				while (isset($this->OTLdata[$ptr]['uni']) && ($this->OTLdata[$ptr]['uni'] >> 8) == 0) { $ptr++; }
			}
		}

	}

}

function checkwordmatch(&$dict, $ptr) {
/*
define("_DICT_NODE_TYPE_SPLIT", 0x01);
define("_DICT_NODE_TYPE_LINEAR", 0x02);
define("_DICT_INTERMEDIATE_MATCH", 0x03);
define("_DICT_FINAL_MATCH", 0x04);

Node type: Split.
Divide at < 98 >= 98
Offset for >= 98 == 79	(long 4-byte unsigned)

Node type: Linear match. 
Char = 97

Intermediate match

Final match
*/

	$dictptr = 0;
	$ok = true;
	$matches = array();
	while ($ok) {
		$x = ord($dict{$dictptr});
		$c = $this->OTLdata[$ptr]['uni'] & 0xFF;
		if ($x==_DICT_INTERMEDIATE_MATCH) {
//echo "DICT_INTERMEDIATE_MATCH: ".dechex($c).'<br />';
			// Do not match if next character in text is a Mark
			if (isset($this->OTLdata[$ptr]['uni']) && strpos($this->GlyphClassMarks, $this->OTLdata[$ptr]['hex'])===false) { 
				$matches[] = $ptr - 1;
			}
			$dictptr++;
		}
		else if ($x==_DICT_FINAL_MATCH) {
//echo "DICT_FINAL_MATCH: ".dechex($c).'<br />';
			// Do not match if next character in text is a Mark
			if (isset($this->OTLdata[$ptr]['uni']) && strpos($this->GlyphClassMarks, $this->OTLdata[$ptr]['hex'])===false) { 
				$matches[] = $ptr - 1;
			}
			return $matches;
		}
		else if ($x==_DICT_NODE_TYPE_LINEAR) {
//echo "DICT_NODE_TYPE_LINEAR: ".dechex($c).'<br />';
			$dictptr++;
			$m = ord($dict{$dictptr});
			if ($c == $m) {
				$ptr++;
				if ($ptr > count($this->OTLdata)-1) {
					$next = ord($dict{$dictptr+1});
					if ($next==_DICT_INTERMEDIATE_MATCH || $next==_DICT_FINAL_MATCH) {
						// Do not match if next character in text is a Mark
						if (isset($this->OTLdata[$ptr]['uni']) && strpos($this->GlyphClassMarks, $this->OTLdata[$ptr]['hex'])===false) { 
							$matches[] = $ptr - 1;
						}
					}
					return $matches;
				}
				$dictptr++;
				continue;
			}
			else {
//echo "DICT_NODE_TYPE_LINEAR NOT: ".dechex($c).'<br />';
				return $matches;
			}
		}
		else if ($x==_DICT_NODE_TYPE_SPLIT) {
//echo "DICT_NODE_TYPE_SPLIT ON ".dechex($d).": ".dechex($c).'<br />';
			$dictptr++;
			$d = ord($dict{$dictptr});
			if ($c < $d) {
				$dictptr += 5;
			}
			else {
				$dictptr++;
				// Unsigned long 32-bit offset
				$offset = (ord($dict{$dictptr})*16777216) + (ord($dict{$dictptr+1})<<16) + (ord($dict{$dictptr+2})<<8) + ord($dict{$dictptr+3});
				$dictptr = $offset;
			}
		}
		else {
//echo "PROBLEM: ".($x).'<br />';
			$ok = false;	// Something has gone wrong
		}
	}

	return $matches;
}



////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////
//////////       GPOS    ///////////////////////////////////////
////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////

function _applyGPOSrules($LookupList, $is_old_spec=false) {
	foreach($LookupList AS $lu=>$tag) {
		$Type = $this->GPOSLookups[$lu]['Type'];
		$Flag = $this->GPOSLookups[$lu]['Flag'];
		$MarkFilteringSet = '';
		if (isset($this->GPOSLookups[$lu]['MarkFilteringSet']))
			$MarkFilteringSet = $this->GPOSLookups[$lu]['MarkFilteringSet'];
		$ptr = 0;
		// Test each glyph sequentially
		while($ptr < (count($this->OTLdata))) {	// whilst there is another glyph ..0064
			$currGlyph = $this->OTLdata[$ptr]['hex'];
			$currGID = $this->OTLdata[$ptr]['uni'];
			$shift = 1;
			foreach($this->GPOSLookups[$lu]['Subtables'] AS $c=>$subtable_offset) {
				// NB Coverage only looks at glyphs for position 1 (esp. 7.3 and 8.3)
				if (isset($this->LuCoverage[$lu][$c][$currGID])) {
					// Get rules from font GPOS subtable
					if (isset($this->OTLdata[$ptr]['bidi_type'])) { 	// No need to check bidi_type - just a check that it exists
						$shift = $this->_applyGPOSsubtable($lu, $c, $ptr, $currGlyph, $currGID, ($subtable_offset - $this->GPOS_offset + $this->GSUB_length), $Type, $Flag, $MarkFilteringSet, $this->LuCoverage[$lu][$c], $tag, 0, $is_old_spec);
						if ($shift) { break; }
					}
				}
			}
			if ($shift == 0) { $shift = 1; }
			$ptr += $shift;

		}
	}
}

	//////////////////////////////////////////////////////////////////////////////////
		// GPOS Types
			// Lookup Type 1: Single Adjustment Positioning Subtable		Adjust position of a single glyph
			// Lookup Type 2: Pair Adjustment Positioning Subtable		Adjust position of a pair of glyphs
			// Lookup Type 3: Cursive Attachment Positioning Subtable		Attach cursive glyphs
			// Lookup Type 4: MarkToBase Attachment Positioning Subtable	Attach a combining mark to a base glyph
			// Lookup Type 5: MarkToLigature Attachment Positioning Subtable	Attach a combining mark to a ligature
			// Lookup Type 6: MarkToMark Attachment Positioning Subtable	Attach a combining mark to another mark
			// Lookup Type 7: Contextual Positioning Subtables			Position one or more glyphs in context
			// Lookup Type 8: Chaining Contextual Positioning Subtable		Position one or more glyphs in chained context
			// Lookup Type 9: Extension positioning
	//////////////////////////////////////////////////////////////////////////////////
function _applyGPOSvaluerecord($basepos,$Value) {

	// If current glyph is a mark with a defined width, any XAdvance is considered to REPLACE the character Advance Width
	// Test case <div style="font-family:myanmartext">&#x1004;&#x103a;&#x1039;&#x1000;&#x1039;&#x1000;&#x103b;&#x103c;&#x103d;&#x1031;&#x102d;</div>
	if (strpos($this->GlyphClassMarks, $this->OTLdata[$basepos]['hex'])!==false) { 
		$cw = round($this->mpdf->_getCharWidth($this->mpdf->CurrentFont['cw'], $this->OTLdata[$basepos]['uni']) * $this->mpdf->CurrentFont['unitsPerEm'] / 1000);	// convert back to font design units
	}
	else { 
		$cw = 0; 
	}

	$apos = $this->_getXAdvancePos($basepos);

	if (isset($Value['XAdvance']) && ($Value['XAdvance']-$cw) != 0) {
		// However DON'T REPLACE the character Advance Width if Advance Width is negative
		// Test case <div style="font-family: dejavusansmono">&#x440;&#x443;&#x301;&#x441;&#x441;&#x43a;&#x438;&#x439;</div>
		if ($Value['XAdvance'] < 0) { $cw = 0; }

		// For LTR apply XAdvanceL to the last mark following the base = at $apos
		// For RTL apply XAdvanceR to base = at $basepos
		if (isset($this->OTLdata[$apos]['GPOSinfo']['XAdvanceL'])) { $this->OTLdata[$apos]['GPOSinfo']['XAdvanceL'] += $Value['XAdvance']-$cw; }
		else { $this->OTLdata[$apos]['GPOSinfo']['XAdvanceL'] = $Value['XAdvance']-$cw; }
		if (isset($this->OTLdata[$basepos]['GPOSinfo']['XAdvanceR'])) { $this->OTLdata[$basepos]['GPOSinfo']['XAdvanceR'] += $Value['XAdvance']-$cw; }
		else { $this->OTLdata[$basepos]['GPOSinfo']['XAdvanceR'] = $Value['XAdvance']-$cw; }
	}

	// Any XPlacement (? and Y Placement) apply to base and marks (from basepos to apos)
	for ($a=$basepos;$a<=$apos;$a++) {
		if (isset($Value['XPlacement'])) { 
			if (isset($this->OTLdata[$a]['GPOSinfo']['XPlacement'])) { $this->OTLdata[$a]['GPOSinfo']['XPlacement'] += $Value['XPlacement']; }
			else { $this->OTLdata[$a]['GPOSinfo']['XPlacement'] = $Value['XPlacement']; }
		}
		if (isset($Value['YPlacement'])) { 
			if (isset($this->OTLdata[$a]['GPOSinfo']['YPlacement'])) { $this->OTLdata[$a]['GPOSinfo']['YPlacement'] += $Value['YPlacement']; }
			else { $this->OTLdata[$a]['GPOSinfo']['YPlacement'] = $Value['YPlacement']; }
		}
	}
}



// If XAdvance is aplied to $ptr - in order for PDF to position the Advance correctly need to place it on
// the last of any Marks which immediately follow the current glyph
function _getXAdvancePos($pos) {
	// NB Not all fonts have all marks specified in GlyphClassMarks

	// If the current glyph is not a base (but a mark) then ignore this, and apply to the current position
	if (strpos($this->GlyphClassMarks, $this->OTLdata[$pos]['hex'])!==false) { return $pos; }

	while(isset($this->OTLdata[$pos+1]['hex']) && strpos($this->GlyphClassMarks, $this->OTLdata[$pos+1]['hex'])!==false) { $pos++; }
	return $pos ;
}




function _applyGPOSsubtable($lookupID, $subtable, $ptr, $currGlyph, $currGID, $subtable_offset, $Type, $Flag, $MarkFilteringSet, $LuCoverage, $tag, $level=0, $is_old_spec) {
	if (($Flag  & 0x0001) == 1) { $dir = 'RTL'; }	// only used for Type 3
	else { $dir = 'LTR'; }
	$ignore = $this->_getGCOMignoreString($Flag, $MarkFilteringSet);

	// Lets start
	$this->seek($subtable_offset);
	$PosFormat = $this->read_ushort();

	////////////////////////////////////////////////////////////////////////////////
	// LookupType 1: Single adjustment 	Adjust position of a single glyph (e.g. SmallCaps/Sups/Subs)
	////////////////////////////////////////////////////////////////////////////////
	if ($Type == 1) {
		//===========
		// Format 1: 
		//===========
		if ($PosFormat==1) {
			$Coverage = $subtable_offset + $this->read_ushort();
			$ValueFormat = $this->read_ushort();
			$Value = $this->_getValueRecord($ValueFormat);
		}
		//===========
		// Format 2: 
		//===========
		else if ($PosFormat==2) {	
			$Coverage = $subtable_offset + $this->read_ushort();
			$ValueFormat = $this->read_ushort();
			$ValueCount = $this->read_ushort();
			$GlyphPos = $LuCoverage[$currGID];
			$this->skip($GlyphPos * 2 * $this->count_bits($ValueFormat) );
			$Value = $this->_getValueRecord($ValueFormat);
		}
		$this->_applyGPOSvaluerecord($ptr,$Value);
		if ($this->debugOTL) { $this->_dumpproc('GPOS', $lookupID, $subtable, $Type, $PosFormat, $ptr, $currGlyph, $level); }
		return 1; 
	}

	////////////////////////////////////////////////////////////////////////////////
	// LookupType 2: Pair adjustment 	Adjust position of a pair of glyphs (Kerning)
	////////////////////////////////////////////////////////////////////////////////
	else if ($Type == 2) {
		$Coverage = $subtable_offset + $this->read_ushort();
		$ValueFormat1 = $this->read_ushort();
		$ValueFormat2 = $this->read_ushort();
		$sizeOfPair = ( 2*$this->count_bits($ValueFormat1) ) + ( 2*$this->count_bits($ValueFormat2) );
		//===========
		// Format 1: 
		//===========
		if ($PosFormat==1) {	
			$PairSetCount = $this->read_ushort();
			$PairSetOffset = array();
			for($p=0;$p<$PairSetCount;$p++) {
				$PairSetOffset[] = $subtable_offset + $this->read_ushort();
			}
			for($p=0;$p<$PairSetCount;$p++) {
				if (isset($LuCoverage[$currGID]) && $LuCoverage[$currGID]==$p) {
					$this->seek($PairSetOffset[$p]);
					//PairSet table
					$PairValueCount = $this->read_ushort();
					for($pv=0;$pv<$PairValueCount;$pv++) {
						//PairValueRecord
						$gid = $this->read_ushort();
						$SecondGlyph = $this->glyphToChar($gid); 
						$FirstGlyph = $this->OTLdata[$ptr]['uni']; 

						$checkpos = $ptr;
						$checkpos++;
						while (isset($this->OTLdata[$checkpos]) && strpos($ignore, $this->OTLdata[$checkpos]['hex'])!==false) {
							 $checkpos++; 
						}
						if (isset($this->OTLdata[$checkpos]) && $this->OTLdata[$checkpos]['uni']==$SecondGlyph) {
								$matchedpos = $checkpos;
						}
						else { $matchedpos = false; }

						if ($matchedpos !== false) {
							$Value1 = $this->_getValueRecord($ValueFormat1);
							$Value2 = $this->_getValueRecord($ValueFormat2);
							if($ValueFormat1) {
								$this->_applyGPOSvaluerecord($ptr,$Value1);
							}
							if($ValueFormat2) { 
								$this->_applyGPOSvaluerecord($matchedpos,$Value2);
								if ($this->debugOTL) { $this->_dumpproc('GPOS', $lookupID, $subtable, $Type, $PosFormat, $ptr, $currGlyph, $level); }
								return $matchedpos - $ptr +1; 
							}
							if ($this->debugOTL) { $this->_dumpproc('GPOS', $lookupID, $subtable, $Type, $PosFormat, $ptr, $currGlyph, $level); }
							return $matchedpos - $ptr;
						}
						else {
							$this->skip($sizeOfPair);
						}
					}
				}
			}
			return 0;
		}
		//===========
		// Format 2: 
		//===========
		else if ($PosFormat==2) {
			$ClassDef1 = $subtable_offset + $this->read_ushort();
			$ClassDef2 = $subtable_offset + $this->read_ushort();
			$Class1Count = $this->read_ushort();
			$Class2Count = $this->read_ushort();

			$sizeOfValueRecords = $Class1Count * $Class2Count * $sizeOfPair;

			//$this->skip($sizeOfValueRecords );  ???? NOT NEEDED

			// NB Class1Count includes Class 0 even though it is not defined by $ClassDef1
			// i.e. Class1Count = 5; Class1 will contain array(indices 1-4);
			$Class1 = $this->_getClassDefinitionTable($ClassDef1);
			$Class2 = $this->_getClassDefinitionTable($ClassDef2);
			$FirstGlyph = $this->OTLdata[$ptr]['uni'];
			$checkpos = $ptr;
			$checkpos++;
			while (isset($this->OTLdata[$checkpos]) && strpos($ignore, $this->OTLdata[$checkpos]['hex'])!==false) {
				$checkpos++; 
			}
			if (isset($this->OTLdata[$checkpos])) { $matchedpos = $checkpos; }
			else { return 0; }

			$SecondGlyph = $this->OTLdata[$matchedpos]['uni'];
			for($i=0;$i<$Class1Count;$i++) {
				if (isset($Class1[$i]) && count($Class1[$i])) {
					$FirstClassPos = array_search($FirstGlyph, $Class1[$i]);
					if ($FirstClassPos === false) { continue; }
					else {
						for($j=0;$j<$Class2Count;$j++) {
							if (isset($Class2[$j]) && count($Class2[$j])) {

									$SecondClassPos = array_search($SecondGlyph, $Class2[$j]);
									if ($SecondClassPos === false) { continue; }

									// Get ValueRecord[$i][$j]
									$offs = ($i*$Class2Count*$sizeOfPair) + ($j*$sizeOfPair);
									$this->seek($subtable_offset + 16 + $offs);

									$Value1 = $this->_getValueRecord($ValueFormat1);
									$Value2 = $this->_getValueRecord($ValueFormat2);
									if($ValueFormat1) {
										$this->_applyGPOSvaluerecord($ptr,$Value1);
									}
									if($ValueFormat2) { 
										$this->_applyGPOSvaluerecord($matchedpos,$Value2);
										if ($this->debugOTL) { $this->_dumpproc('GPOS', $lookupID, $subtable, $Type, $PosFormat, $ptr, $currGlyph, $level); }
										return $matchedpos - $ptr +1; 
									}
									if ($this->debugOTL) { $this->_dumpproc('GPOS', $lookupID, $subtable, $Type, $PosFormat, $ptr, $currGlyph, $level); }
									return $matchedpos - $ptr;
							}
						}
					}

				}
			}
			return 0;
		}
	}

	////////////////////////////////////////////////////////////////////////////////
	// LookupType 3: Cursive attachment 	Attach cursive glyphs
	////////////////////////////////////////////////////////////////////////////////
	else if ($Type == 3) {
		$this->skip(4);
		// Need default XAdvance for glyph
		$pdfWidth = $this->mpdf->_getCharWidth($this->mpdf->CurrentFont['cw'], hexdec($currGlyph));	// DON'T convert back to design units

		$CPos = $LuCoverage[$currGID];
		$this->skip($CPos * 4);
		$EntryAnchor = $this->read_ushort();
		$ExitAnchor = $this->read_ushort();
		if ($EntryAnchor != 0) {
			$EntryAnchor += $subtable_offset;
			list($x,$y) = $this->_getAnchorTable($EntryAnchor);
			if ($dir == 'RTL') {
				if (round($pdfWidth) == round($x * 1000/ $this->mpdf->CurrentFont['unitsPerEm']) ) {
					$x = 0;
				}
				else { $x = $x - ($pdfWidth * $this->mpdf->CurrentFont['unitsPerEm']/1000); }
			}

			$this->Entry[$ptr] = array('X'=>$x, 'Y'=>$y, 'dir'=>$dir);
		}
		if ($ExitAnchor != 0) {
			$ExitAnchor += $subtable_offset;
			list($x,$y) = $this->_getAnchorTable($ExitAnchor);
			if ($dir == 'LTR') {
				if (round($pdfWidth) == round($x * 1000/ $this->mpdf->CurrentFont['unitsPerEm']) ) {
					$x = 0;
				}
				else { $x = $x - ($pdfWidth * $this->mpdf->CurrentFont['unitsPerEm']/1000); }
			}
			$this->Exit[$ptr] = array('X'=>$x, 'Y'=>$y, 'dir'=>$dir);
		}
		if ($this->debugOTL) { $this->_dumpproc('GPOS', $lookupID, $subtable, $Type, $PosFormat, $ptr, $currGlyph, $level); }
		return 1;
	}

	////////////////////////////////////////////////////////////////////////////////
	// LookupType 4: MarkToBase attachment 	Attach a combining mark to a base glyph
	////////////////////////////////////////////////////////////////////////////////
	else if ($Type == 4) {
		$MarkCoverage = $subtable_offset + $this->read_ushort();
		//$MarkCoverage is already set in $LuCoverage 00065|00073 etc
		$BaseCoverage = $subtable_offset + $this->read_ushort();
		$ClassCount = $this->read_ushort(); // Number of classes defined for marks = Number of mark glyphs in the MarkCoverage table
		$MarkArray = $subtable_offset + $this->read_ushort();	// Offset to MarkArray table
		$BaseArray = $subtable_offset + $this->read_ushort(); // Offset to BaseArray table

		$this->seek($BaseCoverage);
		$BaseGlyphs = implode('|',$this->_getCoverage());

		$checkpos = $ptr;
		$checkpos--;

		// ZZZ93
		// In Lohit-Kannada font (old-spec), rules specify a Type 4 GPOS to attach below-forms to base glyph
		// the repositioning does not happen in MS Word, and shouldn't happen comparing with other fonts
		// ?Why not
		// This Fix blocks the GPOS rule if the "mark" is not actually classified as a mark in the GlyphClasses of GDEF
		// but only in Indic old-spec.
		// Test cases: &#xca8;&#xccd;&#xca8;&#xcc1; and &#xc95;&#xccd;&#xcb0;&#xccc;
		if ($this->shaper=='I' && $is_old_spec && strpos($this->GlyphClassMarks, $this->OTLdata[$ptr]['hex'])===false) { return; }


		// "To identify the base glyph that combines with a mark, the text-processing client must look backward in the glyph string from the mark to the preceding base glyph."
		while (isset($this->OTLdata[$checkpos]) && strpos($this->GlyphClassMarks, $this->OTLdata[$checkpos]['hex'])!==false) {
			$checkpos--; 
		}

		if (isset($this->OTLdata[$checkpos]) && strpos($BaseGlyphs, $this->OTLdata[$checkpos]['hex'])!==false) {
			$matchedpos = $checkpos;
		}
		else { $matchedpos = false; }

		if ($matchedpos !== false) {

			// Get the relevant MarkRecord
			$MarkPos = $LuCoverage[$currGID];
			$MarkRecord = $this->_getMarkRecord($MarkArray, $MarkPos);	// e.g. Array ( [Class] => 0 [AnchorX] => -549 [AnchorY] => 1548 ) 
			//Mark Class is = $MarkRecord['Class']

			// Get the relevant BaseRecord
			$this->seek($BaseArray);
			$BaseCount = $this->read_ushort();
			$BasePos = strpos($BaseGlyphs, $this->OTLdata[$matchedpos]['hex'])/6;

			// Move to the BaseRecord we want
			$nSkip = (2 * $BasePos * $ClassCount );
			$this->skip($nSkip);

			// Read BaseRecord we want for appropriate Class
			$nSkip = 2*$MarkRecord['Class'];
			$this->skip($nSkip);
			$BaseRecordOffset = $BaseArray + $this->read_ushort();
			list($x,$y) = $this->_getAnchorTable($BaseRecordOffset);
			$BaseRecord = array('AnchorX'=>$x, 'AnchorY'=>$y);	// e.g. Array ( [AnchorX] => 660 [AnchorY] => 1556 ) 

			// Need default XAdvance for Base glyph
			$BaseWidth = $this->mpdf->_getCharWidth($this->mpdf->CurrentFont['cw'], $this->OTLdata[$matchedpos]['uni']) * $this->mpdf->CurrentFont['unitsPerEm'] / 1000;	// convert back to font design units
			$this->OTLdata[$ptr]['GPOSinfo']['BaseWidth'] = $BaseWidth;
			// And any intervening (ignored) characters
			if (($ptr - $matchedpos) > 1) {
				for ($i=$matchedpos+1; $i<$ptr; $i++) {
					$BaseWidthExtra = $this->mpdf->_getCharWidth($this->mpdf->CurrentFont['cw'], $this->OTLdata[$i]['uni']) * $this->mpdf->CurrentFont['unitsPerEm'] / 1000;	// convert back to font design units
					$this->OTLdata[$ptr]['GPOSinfo']['BaseWidth'] += $BaseWidthExtra;

				}
			}

			// Align to previous Glyph by attachment - so need to add to previous placement values
			$prevXPlacement = (isset($this->OTLdata[$matchedpos]['GPOSinfo']['XPlacement']) ? $this->OTLdata[$matchedpos]['GPOSinfo']['XPlacement'] : 0);
			$prevYPlacement = (isset($this->OTLdata[$matchedpos]['GPOSinfo']['YPlacement']) ? $this->OTLdata[$matchedpos]['GPOSinfo']['YPlacement'] : 0);

			$this->OTLdata[$ptr]['GPOSinfo']['XPlacement'] = $prevXPlacement + $BaseRecord['AnchorX'] - $MarkRecord['AnchorX'];
			$this->OTLdata[$ptr]['GPOSinfo']['YPlacement'] = $prevYPlacement + $BaseRecord['AnchorY'] - $MarkRecord['AnchorY'];
			if ($this->debugOTL) { $this->_dumpproc('GPOS', $lookupID, $subtable, $Type, $PosFormat, $ptr, $currGlyph, $level); }
			return 1;

		}
		return 0;
	}

	////////////////////////////////////////////////////////////////////////////////
	// LookupType 5: MarkToLigature attachment 	Attach a combining mark to a ligature
	////////////////////////////////////////////////////////////////////////////////
	else if ($Type == 5) {
		$MarkCoverage = $subtable_offset + $this->read_ushort();
		//$MarkCoverage is already set in $LuCoverage 00065|00073 etc
		$LigatureCoverage = $subtable_offset + $this->read_ushort();
		$ClassCount = $this->read_ushort(); // Number of classes defined for marks = Number of mark glyphs in the MarkCoverage table
		$MarkArray = $subtable_offset + $this->read_ushort();	// Offset to MarkArray table
		$LigatureArray = $subtable_offset + $this->read_ushort(); // Offset to LigatureArray table

		$this->seek($LigatureCoverage);
		$LigatureGlyphs = implode('|',$this->_getCoverage());


		$checkpos = $ptr;
		$checkpos--;

		// "To position a combining mark using a MarkToLigature attachment subtable, the text-processing client must work backward from the mark to the preceding ligature glyph."
		while (isset($this->OTLdata[$checkpos]) && strpos($this->GlyphClassMarks, $this->OTLdata[$checkpos]['hex'])!==false) {
			$checkpos--; 
		}

		if (isset($this->OTLdata[$checkpos]) && strpos($LigatureGlyphs, $this->OTLdata[$checkpos]['hex'])!==false) {
			$matchedpos = $checkpos;
		}
		else { $matchedpos = false; }

		if ($matchedpos !== false) {

			// Get the relevant MarkRecord
			$MarkPos = $LuCoverage[$currGID];
			$MarkRecord = $this->_getMarkRecord($MarkArray, $MarkPos);	// e.g. Array ( [Class] => 0 [AnchorX] => -549 [AnchorY] => 1548 ) 
			//Mark Class is = $MarkRecord['Class']


			// Get the relevant LigatureRecord
			$this->seek($LigatureArray);
			$LigatureCount = $this->read_ushort();
			$LigaturePos = strpos($LigatureGlyphs, $this->OTLdata[$matchedpos]['hex'])/6;

			// Move to the LigatureAttach table Record we want
			$nSkip = (2 * $LigaturePos);
			$this->skip($nSkip);
			$LigatureAttachOffset = $LigatureArray + $this->read_ushort();
			$this->seek($LigatureAttachOffset);
			$ComponentCount = $this->read_ushort();
			$offsets = array();
			for ($comp=0;$comp<$ComponentCount;$comp++) {
				// ComponentRecords
				for ($class=0;$class<$ClassCount;$class++) {
					$offsets[$comp][$class] = $this->read_ushort();
				}
			}

			// Get the specific component for this mark attachment
			if (isset($this->assocLigs[$matchedpos]) && isset($this->assocMarks[$ptr]['ligPos']) && $this->assocMarks[$ptr]['ligPos']==$matchedpos) {
				$component = $this->assocMarks[$ptr]['compID'] ;
			}
			else { $component = $ComponentCount-1; }

			$offset = $offsets[$component][$MarkRecord['Class']];
			if ($offset!=0) { 
				$LigatureRecordOffset = $offset + $LigatureAttachOffset; 
					list($x,$y) = $this->_getAnchorTable($LigatureRecordOffset);
					$LigatureRecord = array('AnchorX'=>$x, 'AnchorY'=>$y);

					// Need default XAdvance for Ligature glyph
					$LigatureWidth = $this->mpdf->_getCharWidth($this->mpdf->CurrentFont['cw'], $this->OTLdata[$matchedpos]['uni']) * $this->mpdf->CurrentFont['unitsPerEm'] / 1000;	// convert back to font design units
					$this->OTLdata[$ptr]['GPOSinfo']['BaseWidth'] = $LigatureWidth;
					// And any intervening (ignored)characters
					if (($ptr - $matchedpos) > 1) {
						for ($i=$matchedpos+1; $i<$ptr; $i++) {
							$LigatureWidthExtra = $this->mpdf->_getCharWidth($this->mpdf->CurrentFont['cw'], $this->OTLdata[$i]['uni']) * $this->mpdf->CurrentFont['unitsPerEm'] / 1000;	// convert back to font design units
							$this->OTLdata[$ptr]['GPOSinfo']['BaseWidth'] += $LigatureWidthExtra;

						}
					}

					// Align to previous Ligature by attachment - so need to add to previous placement values
					if (isset($this->OTLdata[$matchedpos]['GPOSinfo']['XPlacement'])) $prevXPlacement = $this->OTLdata[$matchedpos]['GPOSinfo']['XPlacement'];
					else { $prevXPlacement = 0; }
					if (isset($this->OTLdata[$matchedpos]['GPOSinfo']['YPlacement'])) { $prevYPlacement = $this->OTLdata[$matchedpos]['GPOSinfo']['YPlacement']; }
					else { $prevYPlacement = 0; }

					$this->OTLdata[$ptr]['GPOSinfo']['XPlacement'] = $prevXPlacement + $LigatureRecord['AnchorX'] - $MarkRecord['AnchorX'];
					$this->OTLdata[$ptr]['GPOSinfo']['YPlacement'] = $prevYPlacement + $LigatureRecord['AnchorY'] - $MarkRecord['AnchorY'];
					if ($this->debugOTL) { $this->_dumpproc('GPOS', $lookupID, $subtable, $Type, $PosFormat, $ptr, $currGlyph, $level); }
					return 1;

			}
		}
		return 0;
	}

	////////////////////////////////////////////////////////////////////////////////
	// LookupType 6: MarkToMark attachment 	Attach a combining mark to another mark
	////////////////////////////////////////////////////////////////////////////////
	else if ($Type == 6) {
		$Mark1Coverage = $subtable_offset + $this->read_ushort();	// Combining Mark
		//$Mark1Coverage is already set in $LuCoverage 0065|0073 etc
		$Mark2Coverage = $subtable_offset + $this->read_ushort();	// Base Mark
		$ClassCount = $this->read_ushort(); // Number of classes defined for marks = No. of Combining mark1 glyphs in the MarkCoverage table
		$Mark1Array = $subtable_offset + $this->read_ushort();	// Offset to MarkArray table
		$Mark2Array = $subtable_offset + $this->read_ushort(); // Offset to Mark2Array table
		$this->seek($Mark2Coverage);
		$Mark2Glyphs = implode('|',$this->_getCoverage());
		$checkpos = $ptr;
		$checkpos--;
		while (isset($this->OTLdata[$checkpos]) && strpos($ignore, $this->OTLdata[$checkpos]['hex'])!==false) {
			$checkpos--; 
		}
		if (isset($this->OTLdata[$checkpos]) && strpos($Mark2Glyphs, $this->OTLdata[$checkpos]['hex'])!==false) {
			$matchedpos = $checkpos;
		}
		else { $matchedpos = false; }

		if ($matchedpos !== false) {

			// Get the relevant MarkRecord
			$Mark1Pos = $LuCoverage[$currGID];
			$Mark1Record = $this->_getMarkRecord($Mark1Array, $Mark1Pos); // e.g. Array ( [Class] => 0 [AnchorX] => -549 [AnchorY] => 1548 ) 
			//Mark Class is = $Mark1Record['Class']

			// Get the relevant Mark2Record
			$this->seek($Mark2Array);
			$Mark2Count = $this->read_ushort();
			$Mark2Pos = strpos($Mark2Glyphs, $this->OTLdata[$matchedpos]['hex'])/6;

			// Move to the Mark2Record we want
			$nSkip = (2 * $Mark2Pos * $ClassCount );
			$this->skip($nSkip);

			// Read Mark2Record we want for appropriate Class
			$nSkip = 2*$Mark1Record['Class'];
			$this->skip($nSkip);
			$Mark2RecordOffset = $Mark2Array + $this->read_ushort();
			list($x,$y) = $this->_getAnchorTable($Mark2RecordOffset);
			$Mark2Record = array('AnchorX'=>$x, 'AnchorY'=>$y);	// e.g. Array ( [AnchorX] => 660 [AnchorY] => 1556 ) 

			// Need default XAdvance for Mark2 glyph
			$Mark2Width = $this->mpdf->_getCharWidth($this->mpdf->CurrentFont['cw'], $this->OTLdata[$matchedpos]['uni']) * $this->mpdf->CurrentFont['unitsPerEm'] / 1000;	// convert back to font design units


			// IF combining marks are set on different components of a ligature glyph, do not apply this rule
			// Test: arabictypesetting: &#x625;&#x650;&#x644;&#x64e;&#x649;&#x670;&#x653;
			// Test: arabictypesetting: &#x628;&#x651;&#x64e;&#x64a;&#x652;&#x646;&#x64e;&#x643;&#x64f;&#x645;&#x652;
			$prevLig = -1;
			$thisLig = -1;
			$prevComp = -1;
			$thisComp = -1;
			if (isset($this->assocMarks[$matchedpos])) { 
				$prevLig = $this->assocMarks[$matchedpos]['ligPos']; 
				$prevComp = $this->assocMarks[$matchedpos]['compID']; 
			}
			if (isset($this->assocMarks[$ptr])) {
				$thisLig = $this->assocMarks[$ptr]['ligPos']; 
				$thisComp = $this->assocMarks[$ptr]['compID']; 
			}

			// However IF Mark2 (first in logical order, i.e. being attached to) is not associated with a base, carry on
			// This happens in Indic when the Mark being attached to e.g. [Halant Ma lig] -> MatraU,  [U+0B4D + U+B2E as E0F5]-> U+0B41 become E135
			if (!defined("OMIT_OTL_FIX_1") || OMIT_OTL_FIX_1 != 1) { 
			/* OTL_FIX_1 */
				if (isset($this->assocMarks[$matchedpos]) && ($prevLig != $thisLig || $prevComp != $thisComp ))  { return 0; }
			}
			else {
			/* Original code */
				if ($prevLig != $thisLig || $prevComp != $thisComp ) { return 0; }
			}


			if (!defined("OMIT_OTL_FIX_2") || OMIT_OTL_FIX_2 != 1) {
			/* OTL_FIX_2 */ 
				if (!isset($this->OTLdata[$matchedpos]['GPOSinfo']['BaseWidth']) || !$this->OTLdata[$matchedpos]['GPOSinfo']['BaseWidth']) { $this->OTLdata[$ptr]['GPOSinfo']['BaseWidth'] = $Mark2Width; }
			}

			// ZZZ99Q - Test Case font-family: garuda &#xe19;&#xe49;&#xe33;
			if (isset($this->OTLdata[$matchedpos]['GPOSinfo']['BaseWidth']) && $this->OTLdata[$matchedpos]['GPOSinfo']['BaseWidth']) { $this->OTLdata[$ptr]['GPOSinfo']['BaseWidth'] = $this->OTLdata[$matchedpos]['GPOSinfo']['BaseWidth']; }

			// Align to previous Mark by attachment - so need to add the previous placement values
			$prevXPlacement = (isset($this->OTLdata[$matchedpos]['GPOSinfo']['XPlacement']) ? $this->OTLdata[$matchedpos]['GPOSinfo']['XPlacement'] : 0);
			$prevYPlacement = (isset($this->OTLdata[$matchedpos]['GPOSinfo']['YPlacement']) ? $this->OTLdata[$matchedpos]['GPOSinfo']['YPlacement'] : 0);
			$this->OTLdata[$ptr]['GPOSinfo']['XPlacement'] = $prevXPlacement + $Mark2Record['AnchorX'] - $Mark1Record['AnchorX'];
			$this->OTLdata[$ptr]['GPOSinfo']['YPlacement'] = $prevYPlacement + $Mark2Record['AnchorY'] - $Mark1Record['AnchorY'];
			if ($this->debugOTL) { $this->_dumpproc('GPOS', $lookupID, $subtable, $Type, $PosFormat, $ptr, $currGlyph, $level); }
			return 1;

		}
		return 0;
	}

	////////////////////////////////////////////////////////////////////////////////
	// LookupType 7: Context positioning 	Position one or more glyphs in context
	////////////////////////////////////////////////////////////////////////////////
	else if ($Type == 7) {
		//===========
		// Format 1: 
		//===========
		if ($PosFormat==1) {	
			die("GPOS Lookup Type ".$Type." Format ".$PosFormat." not TESTED YET."); 
			return 0;
		}
		//===========
		// Format 2: 
		//===========
		else if ($PosFormat==2) {	
			$CoverageTableOffset = $subtable_offset + $this->read_ushort();
			$InputClassDefOffset = $subtable_offset + $this->read_ushort();
			$PosClassSetCnt = $this->read_ushort();
			$PosClassSetOffset = array();
			for ($b=0;$b<$PosClassSetCnt;$b++) {
				$offset = $this->read_ushort();
				if ($offset==0x0000) {
					$PosClassSetOffset[] = $offset;
				}
				else {
					$PosClassSetOffset[] = $subtable_offset + $offset;
				}
			}

			$InputClasses = $this->_getClasses($InputClassDefOffset);

			for ($s=0;$s<$PosClassSetCnt;$s++) {	// $ChainPosClassSet is ordered by input class-may be NULL
				// Select $PosClassSet if currGlyph is in First Input Class
				if ($PosClassSetOffset[$s]>0 && isset($InputClasses[$s][$currGID])) {
					$this->seek($PosClassSetOffset[$s]);
					$PosClassRuleCnt = $this->read_ushort();
					$PosClassRule = array();
					for($b=0;$b<$PosClassRuleCnt;$b++) {
						$PosClassRule[$b] = $PosClassSetOffset[$s]+$this->read_ushort();
					}

					for($b=0;$b<$PosClassRuleCnt;$b++) {		// EACH RULE
						$this->seek($PosClassRule[$b]);
						$InputGlyphCount = $this->read_ushort();
						$PosCount = $this->read_ushort();

						$Input = array();
						for ($r=1;$r<$InputGlyphCount;$r++) {
							$Input[$r] = $this->read_ushort();
						}
						$inputClass = $s;

						$inputGlyphs = array();
						$inputGlyphs[0] = $InputClasses[$inputClass];

						if ($InputGlyphCount>1) {
							//  NB starts at 1 
							for ($gcl=1;$gcl<$InputGlyphCount;$gcl++) {
								$classindex = $Input[$gcl];
								if (isset($InputClasses[$classindex])) { $inputGlyphs[$gcl] = $InputClasses[$classindex]; }
								else { $inputGlyphs[$gcl] = ''; }
							}
						}

						// Class 0 contains all the glyphs NOT in the other classes
						$class0excl = array();
						for ($gc=1;$gc<=count($InputClasses);$gc++) {
							if (is_array($InputClasses[$gc])) $class0excl = $class0excl + $InputClasses[$gc];
						}

						$backtrackGlyphs = array();
						$lookaheadGlyphs = array();

						$matched = $this->checkContextMatchMultipleUni($inputGlyphs, $backtrackGlyphs, $lookaheadGlyphs, $ignore, $ptr, $class0excl );
						if ($matched) {
							for ($p=0;$p<$PosCount;$p++) {	// EACH LOOKUP
								$SequenceIndex[$p] = $this->read_ushort();
								$LookupListIndex[$p] = $this->read_ushort();
							}

							for ($p=0;$p<$PosCount;$p++) {
								// Apply  $LookupListIndex  at   $SequenceIndex
								if ($SequenceIndex[$p] >= $InputGlyphCount) { continue; }
								$lu = $LookupListIndex[$p];
								$luType = $this->GPOSLookups[$lu]['Type'];
								$luFlag = $this->GPOSLookups[$lu]['Flag'];
								$luMarkFilteringSet = $this->GPOSLookups[$lu]['MarkFilteringSet'];

								$luptr = $matched[$SequenceIndex[$p]];
								$lucurrGlyph = $this->OTLdata[$luptr]['hex'];
								$lucurrGID = $this->OTLdata[$luptr]['uni'];

								foreach($this->GPOSLookups[$lu]['Subtables'] AS $luc=>$lusubtable_offset) {
									$shift = $this->_applyGPOSsubtable($lu, $luc, $luptr, $lucurrGlyph, $lucurrGID, ($lusubtable_offset - $this->GPOS_offset + $this->GSUB_length), $luType, $luFlag, $luMarkFilteringSet, $this->LuCoverage[$lu][$luc], $tag, 1, $is_old_spec);
									if ($this->debugOTL && $shift) { $this->_dumpproc('GPOS', $lookupID, $subtable, $Type, $PosFormat, $ptr, $currGlyph, $level); }
									if ($shift) { break; }
								}
							}

							if (!defined("OMIT_OTL_FIX_3") || OMIT_OTL_FIX_3 != 1) { return $shift ; }	/* OTL_FIX_3 */
							else return $InputGlyphCount ;	// should be + matched ignores in Input Sequence

						}
					}

				}
			}

			return 0;
		}
		//===========
		// Format 3: 
		//===========
		else if ($PosFormat==3) {	
			die("GPOS Lookup Type ".$Type." Format ".$PosFormat." not TESTED YET."); 
			return 0;
		}
		else { die("GPOS Lookup Type ".$Type.", Format ".$PosFormat." not supported."); }
	}

	////////////////////////////////////////////////////////////////////////////////
	// LookupType 8: Chained Context positioning 	Position one or more glyphs in chained context
	////////////////////////////////////////////////////////////////////////////////
	else if ($Type == 8) {
		//===========
		// Format 1: 
		//===========
		if ($PosFormat==1) {
			die("GPOS Lookup Type ".$Type." Format ".$PosFormat." not TESTED YET."); 
			return 0;
		}
		//===========
		// Format 2: 
		//===========
		else if ($PosFormat==2) {

			$CoverageTableOffset = $subtable_offset + $this->read_ushort();
			$BacktrackClassDefOffset = $subtable_offset + $this->read_ushort();
			$InputClassDefOffset = $subtable_offset + $this->read_ushort();
			$LookaheadClassDefOffset = $subtable_offset + $this->read_ushort();
			$ChainPosClassSetCnt = $this->read_ushort();
			$ChainPosClassSetOffset = array();
			for ($b=0;$b<$ChainPosClassSetCnt;$b++) {
				$offset = $this->read_ushort();
				if ($offset==0x0000) {
					$ChainPosClassSetOffset[] = $offset;
				}
				else {
					$ChainPosClassSetOffset[] = $subtable_offset + $offset;
				}
			}

			$BacktrackClasses = $this->_getClasses($BacktrackClassDefOffset);
			$InputClasses = $this->_getClasses($InputClassDefOffset);
			$LookaheadClasses = $this->_getClasses($LookaheadClassDefOffset);

			for ($s=0;$s<$ChainPosClassSetCnt;$s++) {	// $ChainPosClassSet is ordered by input class-may be NULL
				// Select $ChainPosClassSet if currGlyph is in First Input Class
				if ($ChainPosClassSetOffset[$s]>0 && isset($InputClasses[$s][$currGID])) {
					$this->seek($ChainPosClassSetOffset[$s]);
					$ChainPosClassRuleCnt = $this->read_ushort();
					$ChainPosClassRule = array();
					for($b=0;$b<$ChainPosClassRuleCnt;$b++) {
						$ChainPosClassRule[$b] = $ChainPosClassSetOffset[$s]+$this->read_ushort();
					}

					for($b=0;$b<$ChainPosClassRuleCnt;$b++) {		// EACH RULE
						$this->seek($ChainPosClassRule[$b]);
						$BacktrackGlyphCount = $this->read_ushort();
						$Backtrack = array();
						for ($r=0;$r<$BacktrackGlyphCount;$r++) {
							$Backtrack[$r] = $this->read_ushort();
						}
						$InputGlyphCount = $this->read_ushort();
						$Input = array();
						for ($r=1;$r<$InputGlyphCount;$r++) {
							$Input[$r] = $this->read_ushort();
						}
						$LookaheadGlyphCount = $this->read_ushort();
						$Lookahead = array();
						for ($r=0;$r<$LookaheadGlyphCount;$r++) {
							$Lookahead[$r] = $this->read_ushort();
						}

						$inputClass = $s;	//???

						$inputGlyphs = array();
						$inputGlyphs[0] = $InputClasses[$inputClass];

						if ($InputGlyphCount>1) {
							//  NB starts at 1 
							for ($gcl=1;$gcl<$InputGlyphCount;$gcl++) {
								$classindex = $Input[$gcl];
								if (isset($InputClasses[$classindex])) { $inputGlyphs[$gcl] = $InputClasses[$classindex]; }
								else { $inputGlyphs[$gcl] = ''; }
							}
						}

						// Class 0 contains all the glyphs NOT in the other classes
						$class0excl = array();
						for ($gc=1;$gc<=count($InputClasses);$gc++) {
							if (isset($InputClasses[$gc]) && is_array($InputClasses[$gc])) $class0excl = $class0excl + $InputClasses[$gc];
						}

						if ($BacktrackGlyphCount) {
							$backtrackGlyphs = array();
							for ($gcl=0;$gcl<$BacktrackGlyphCount;$gcl++) {
								$classindex = $Backtrack[$gcl];
								if (isset($BacktrackClasses[$classindex])) { $backtrackGlyphs[$gcl] = $BacktrackClasses[$classindex]; }
								else { $backtrackGlyphs[$gcl] = ''; }
							}
						}
						else { $backtrackGlyphs = array(); }

						// Class 0 contains all the glyphs NOT in the other classes
						$bclass0excl = array();
						for ($gc=1;$gc<=count($BacktrackClasses);$gc++) {
							if (isset($BacktrackClasses[$gc]) && is_array($BacktrackClasses[$gc])) $bclass0excl = $bclass0excl + $BacktrackClasses[$gc];
						}

						if ($LookaheadGlyphCount) {
							$lookaheadGlyphs = array();
							for ($gcl=0;$gcl<$LookaheadGlyphCount;$gcl++) {
								$classindex = $Lookahead[$gcl];
								if (isset($LookaheadClasses[$classindex])) { $lookaheadGlyphs[$gcl] = $LookaheadClasses[$classindex]; }
								else { $lookaheadGlyphs[$gcl] = ''; }
							}
						}
						else { $lookaheadGlyphs = array(); }

						// Class 0 contains all the glyphs NOT in the other classes
						$lclass0excl = array();
						for ($gc=1;$gc<=count($LookaheadClasses);$gc++) {
							if (isset($LookaheadClasses[$gc]) && is_array($LookaheadClasses[$gc])) $lclass0excl = $lclass0excl + $LookaheadClasses[$gc];
						}

						$matched = $this->checkContextMatchMultipleUni($inputGlyphs, $backtrackGlyphs, $lookaheadGlyphs, $ignore, $ptr, $class0excl, $bclass0excl, $lclass0excl );
						if ($matched) {
							$PosCount = $this->read_ushort();
							$SequenceIndex = array();
							$LookupListIndex = array();
							for ($p=0;$p<$PosCount;$p++) {	// EACH LOOKUP
								$SequenceIndex[$p] = $this->read_ushort();
								$LookupListIndex[$p] = $this->read_ushort();
							}

							for ($p=0;$p<$PosCount;$p++) {
								// Apply  $LookupListIndex  at   $SequenceIndex
								if ($SequenceIndex[$p] >= $InputGlyphCount) { continue; }
								$lu = $LookupListIndex[$p];
								$luType = $this->GPOSLookups[$lu]['Type'];
								$luFlag = $this->GPOSLookups[$lu]['Flag'];
								$luMarkFilteringSet = $this->GPOSLookups[$lu]['MarkFilteringSet'];

								$luptr = $matched[$SequenceIndex[$p]];
								$lucurrGlyph = $this->OTLdata[$luptr]['hex'];
								$lucurrGID = $this->OTLdata[$luptr]['uni'];

								foreach($this->GPOSLookups[$lu]['Subtables'] AS $luc=>$lusubtable_offset) {
									$shift = $this->_applyGPOSsubtable($lu, $luc, $luptr, $lucurrGlyph, $lucurrGID, ($lusubtable_offset - $this->GPOS_offset + $this->GSUB_length), $luType, $luFlag, $luMarkFilteringSet, $this->LuCoverage[$lu][$luc], $tag, 1, $is_old_spec);
									if ($this->debugOTL && $shift) { $this->_dumpproc('GPOS', $lookupID, $subtable, $Type, $PosFormat, $ptr, $currGlyph, $level); }
									if ($shift) { break; }
								}
							}

							if (!defined("OMIT_OTL_FIX_3") || OMIT_OTL_FIX_3 != 1) { return $shift ; }	/* OTL_FIX_3 */
							else return $InputGlyphCount ;	// should be + matched ignores in Input Sequence

						}
					}

				}
			}

			return 0;
		}
		//===========
		// Format 3: 
		//===========
		else if ($PosFormat==3) {
			$BacktrackGlyphCount = $this->read_ushort();
			for ($b=0;$b<$BacktrackGlyphCount;$b++) {
				$CoverageBacktrackOffset[] = $subtable_offset + $this->read_ushort();	// in glyph sequence order
			}
			$InputGlyphCount = $this->read_ushort();
			for ($b=0;$b<$InputGlyphCount;$b++) {
				$CoverageInputOffset[] = $subtable_offset + $this->read_ushort();	// in glyph sequence order
			}
			$LookaheadGlyphCount = $this->read_ushort();
			for ($b=0;$b<$LookaheadGlyphCount;$b++) {
				$CoverageLookaheadOffset[] = $subtable_offset + $this->read_ushort();	// in glyph sequence order
			}
			$PosCount = $this->read_ushort();
			$save_pos = $this->_pos;	// Save the point just after PosCount

			$CoverageBacktrackGlyphs = array();
			for ($b=0;$b<$BacktrackGlyphCount;$b++) {
				$this->seek($CoverageBacktrackOffset[$b]);
				$glyphs = $this->_getCoverage();
				$CoverageBacktrackGlyphs[$b] = implode("|",$glyphs);
			}
			$CoverageInputGlyphs = array();
			for ($b=0;$b<$InputGlyphCount;$b++) {
				$this->seek($CoverageInputOffset[$b]);
				$glyphs = $this->_getCoverage();
				$CoverageInputGlyphs[$b] = implode("|",$glyphs);
			}
			$CoverageLookaheadGlyphs = array();
			for ($b=0;$b<$LookaheadGlyphCount;$b++) {
				$this->seek($CoverageLookaheadOffset[$b]);
				$glyphs = $this->_getCoverage();
				$CoverageLookaheadGlyphs[$b] = implode("|",$glyphs);
			}
			$matched = $this->checkContextMatchMultiple($CoverageInputGlyphs, $CoverageBacktrackGlyphs, $CoverageLookaheadGlyphs , $ignore, $ptr);
			if ($matched) {

				$this->seek($save_pos);	// Return to just after PosCount
				for ($p=0;$p<$PosCount;$p++) {
					// PosLookupRecord
					$PosLookupRecord[$p]['SequenceIndex'] = $this->read_ushort();
					$PosLookupRecord[$p]['LookupListIndex'] = $this->read_ushort();
				}
				for ($p=0;$p<$PosCount;$p++) {
					// Apply  $PosLookupRecord[$p]['LookupListIndex']  at   $PosLookupRecord[$p]['SequenceIndex']
					if ($PosLookupRecord[$p]['SequenceIndex'] >= $InputGlyphCount) { continue; }
					$lu = $PosLookupRecord[$p]['LookupListIndex'];
					$luType = $this->GPOSLookups[$lu]['Type'];
					$luFlag = $this->GPOSLookups[$lu]['Flag'];
					if (isset($this->GPOSLookups[$lu]['MarkFilteringSet'])) { $luMarkFilteringSet = $this->GPOSLookups[$lu]['MarkFilteringSet']; }
					else { $luMarkFilteringSet = ''; }

					$luptr = $matched[$PosLookupRecord[$p]['SequenceIndex']];
					$lucurrGlyph = $this->OTLdata[$luptr]['hex'];
					$lucurrGID = $this->OTLdata[$luptr]['uni'];

					foreach($this->GPOSLookups[$lu]['Subtables'] AS $luc=>$lusubtable_offset) {
						$shift = $this->_applyGPOSsubtable($lu, $luc, $luptr, $lucurrGlyph, $lucurrGID, ($lusubtable_offset - $this->GPOS_offset + $this->GSUB_length), $luType, $luFlag, $luMarkFilteringSet, $this->LuCoverage[$lu][$luc], $tag, 1, $is_old_spec);
						if ($this->debugOTL && $shift) { $this->_dumpproc('GPOS', $lookupID, $subtable, $Type, $PosFormat, $ptr, $currGlyph, $level); }
						if ($shift) { break; }
					}
				}
			}


		}
		else { die("GPOS Lookup Type ".$Type.", Format ".$PosFormat." not supported."); }
	}

	else { die("GPOS Lookup Type ".$Type." not supported."); }
}

	//////////////////////////////////////////////////////////////////////////////////
	//////////////////////////////////////////////////////////////////////////////////
	// GPOS / GSUB / GCOM (common) functions
	//////////////////////////////////////////////////////////////////////////////////
	//////////////////////////////////////////////////////////////////////////////////

	function checkContextMatch($Input, $Backtrack, $Lookahead, $ignore, $ptr) {
		// Input etc are single numbers - GSUB Format 6.1
		// Input starts with (1=>xxx)
		// return false if no match, else an array of ptr for matches (0=>0, 1=>3,...)

		$current_syllable = (isset($this->OTLdata[$ptr]['syllable']) ? $this->OTLdata[$ptr]['syllable'] : 0);

		// BACKTRACK
		$checkpos = $ptr;
		for ($i=0;$i<count($Backtrack);$i++) {
			$checkpos--;
			while (isset($this->OTLdata[$checkpos]) && strpos($ignore, $this->OTLdata[$checkpos]['hex'])!==false) { $checkpos--; }
			// If outside scope of current syllable - return no match
			if ($this->restrictToSyllable && isset($this->OTLdata[$checkpos]['syllable']) && $this->OTLdata[$checkpos]['syllable'] != $current_syllable) {
				return false;
			}
			else if (!isset($this->OTLdata[$checkpos]) || $this->OTLdata[$checkpos]['uni'] != $Backtrack[$i]) {
				return false;
			}
		}

		// INPUT
		$matched = array(0=>$ptr);
		$checkpos = $ptr;
		for ($i=1;$i<count($Input);$i++) {
			$checkpos++;
			while (isset($this->OTLdata[$checkpos]) && strpos($ignore, $this->OTLdata[$checkpos]['hex'])!==false) { $checkpos++; }
			// If outside scope of current syllable - return no match
			if ($this->restrictToSyllable && isset($this->OTLdata[$checkpos]['syllable']) && $this->OTLdata[$checkpos]['syllable'] != $current_syllable) {
				return false;
			}
			else if (isset($this->OTLdata[$checkpos]) && $this->OTLdata[$checkpos]['uni'] == $Input[$i]) {
					$matched[] = $checkpos;
			}
			else { return false; }
		}
		
		// LOOKAHEAD
		for ($i=0;$i<count($Lookahead);$i++) {
			$checkpos++;
			while (isset($this->OTLdata[$checkpos]) && strpos($ignore, $this->OTLdata[$checkpos]['hex'])!==false) { $checkpos++; }
			// If outside scope of current syllable - return no match
			if ($this->restrictToSyllable && isset($this->OTLdata[$checkpos]['syllable']) && $this->OTLdata[$checkpos]['syllable'] != $current_syllable) {
				return false;
			}
			else if (!isset($this->OTLdata[$checkpos]) || $this->OTLdata[$checkpos]['uni'] != $Lookahead[$i]) {
				return false;
			}
		}

		return $matched;
	}


	function checkContextMatchMultiple($Input, $Backtrack, $Lookahead, $ignore, $ptr, $class0excl='', $bclass0excl='', $lclass0excl='') {
		// Input etc are string/array of glyph strings  - GSUB Format 5.2, 5.3, 6.2, 6.3, GPOS Format 7.2, 7.3, 8.2, 8.3
		// Input starts with (1=>xxx)
		// return false if no match, else an array of ptr for matches (0=>0, 1=>3,...)
		// $class0excl is the string of glyphs in all classes except Class 0 (GSUB 5.2, 6.2, GPOS 7.2, 8.2)
		// $bclass0excl & $lclass0excl are the same for lookahead and backtrack (GSUB 6.2, GPOS 8.2)

		$current_syllable = (isset($this->OTLdata[$ptr]['syllable']) ? $this->OTLdata[$ptr]['syllable'] : 0);

		// BACKTRACK
		$checkpos = $ptr;
		for ($i=0;$i<count($Backtrack);$i++) {
			$checkpos--;
			while (isset($this->OTLdata[$checkpos]) && strpos($ignore, $this->OTLdata[$checkpos]['hex'])!==false) { $checkpos--; }
			// If outside scope of current syllable - return no match
			if ($this->restrictToSyllable && isset($this->OTLdata[$checkpos]['syllable']) && $this->OTLdata[$checkpos]['syllable'] != $current_syllable) {
				return false;
			}
			// If Class 0 specified, matches anything NOT in $bclass0excl 
			else if (!$Backtrack[$i] && isset($this->OTLdata[$checkpos]) && strpos($bclass0excl,$this->OTLdata[$checkpos]['hex'])!==false) {
				return false;
			}
			else if (!isset($this->OTLdata[$checkpos]) || strpos($Backtrack[$i], $this->OTLdata[$checkpos]['hex'])===false) {
				return false;
			}
		}

		// INPUT
		$matched = array(0=>$ptr);
		$checkpos = $ptr;
		for ($i=1;$i<count($Input);$i++) {	// Start at 1 - already matched the first InputGlyph
			$checkpos++;
			while (isset($this->OTLdata[$checkpos]) && strpos($ignore, $this->OTLdata[$checkpos]['hex'])!==false) { $checkpos++; }
			// If outside scope of current syllable - return no match
			if ($this->restrictToSyllable && isset($this->OTLdata[$checkpos]['syllable']) && $this->OTLdata[$checkpos]['syllable'] != $current_syllable) {
				return false;
			}
			// If Input Class 0 specified, matches anything NOT in $class0excl 
			else if (!$Input[$i] && isset($this->OTLdata[$checkpos]) && strpos($class0excl,$this->OTLdata[$checkpos]['hex'])===false) {
					$matched[] = $checkpos;
			}
			else if (isset($this->OTLdata[$checkpos]) && strpos($Input[$i],$this->OTLdata[$checkpos]['hex'])!==false) {
					$matched[] = $checkpos;
			}
			else { return false; }
		}
		
		// LOOKAHEAD
		for ($i=0;$i<count($Lookahead);$i++) {
			$checkpos++;
			while (isset($this->OTLdata[$checkpos]) && strpos($ignore, $this->OTLdata[$checkpos]['hex'])!==false) { $checkpos++; }
			// If outside scope of current syllable - return no match
			if ($this->restrictToSyllable && isset($this->OTLdata[$checkpos]['syllable']) && $this->OTLdata[$checkpos]['syllable'] != $current_syllable) {
				return false;
			}
			// If Class 0 specified, matches anything NOT in $lclass0excl 
			else if (!$Lookahead[$i] && isset($this->OTLdata[$checkpos]) && strpos($lclass0excl,$this->OTLdata[$checkpos]['hex'])!==false) {
				return false;
			}
			else if (!isset($this->OTLdata[$checkpos]) || strpos($Lookahead[$i],$this->OTLdata[$checkpos]['hex'])===false) {
				return false;
			}
		}
		return $matched;
	}

	function checkContextMatchMultipleUni($Input, $Backtrack, $Lookahead, $ignore, $ptr, $class0excl=array(), $bclass0excl=array(), $lclass0excl=array()) {
		// Input etc are array of glyphs - GSUB Format 5.2, 5.3, 6.2, 6.3, GPOS Format 7.2, 7.3, 8.2, 8.3
		// Input starts with (1=>xxx)
		// return false if no match, else an array of ptr for matches (0=>0, 1=>3,...)
		// $class0excl is array of glyphs in all classes except Class 0 (GSUB 5.2, 6.2, GPOS 7.2, 8.2)
		// $bclass0excl & $lclass0excl are the same for lookahead and backtrack (GSUB 6.2, GPOS 8.2)

		$current_syllable = (isset($this->OTLdata[$ptr]['syllable']) ? $this->OTLdata[$ptr]['syllable'] : 0);

		// BACKTRACK
		$checkpos = $ptr;
		for ($i=0;$i<count($Backtrack);$i++) {
			$checkpos--;
			while (isset($this->OTLdata[$checkpos]) && strpos($ignore, $this->OTLdata[$checkpos]['hex'])!==false) { $checkpos--; }
			// If outside scope of current syllable - return no match
			if ($this->restrictToSyllable && isset($this->OTLdata[$checkpos]['syllable']) && $this->OTLdata[$checkpos]['syllable'] != $current_syllable) {
				return false;
			}
			// If Class 0 specified, matches anything NOT in $bclass0excl 
			else if (!$Backtrack[$i] && isset($this->OTLdata[$checkpos]) && isset($bclass0excl[$this->OTLdata[$checkpos]['uni']]) ) {
				return false;
			}
			else if (!isset($this->OTLdata[$checkpos]) || !isset($Backtrack[$i][$this->OTLdata[$checkpos]['uni']])) {
				return false;
			}
		}

		// INPUT
		$matched = array(0=>$ptr);
		$checkpos = $ptr;
		for ($i=1;$i<count($Input);$i++) {	// Start at 1 - already matched the first InputGlyph
			$checkpos++;
			while (isset($this->OTLdata[$checkpos]) && strpos($ignore, $this->OTLdata[$checkpos]['hex'])!==false) { $checkpos++; }
			// If outside scope of current syllable - return no match
			if ($this->restrictToSyllable && isset($this->OTLdata[$checkpos]['syllable']) && $this->OTLdata[$checkpos]['syllable'] != $current_syllable) {
				return false;
			}
			// If Input Class 0 specified, matches anything NOT in $class0excl 
			else if (!$Input[$i] && isset($this->OTLdata[$checkpos]) && !isset($class0excl[$this->OTLdata[$checkpos]['uni']]) ) {
					$matched[] = $checkpos;
			}
			else if (isset($this->OTLdata[$checkpos]) && isset($Input[$i][$this->OTLdata[$checkpos]['uni']])) {
					$matched[] = $checkpos;
			}
			else { return false; }
		}
		
		// LOOKAHEAD
		for ($i=0;$i<count($Lookahead);$i++) {
			$checkpos++;
			while (isset($this->OTLdata[$checkpos]) && strpos($ignore, $this->OTLdata[$checkpos]['hex'])!==false) { $checkpos++; }
			// If outside scope of current syllable - return no match
			if ($this->restrictToSyllable && isset($this->OTLdata[$checkpos]['syllable']) && $this->OTLdata[$checkpos]['syllable'] != $current_syllable) {
				return false;
			}
			// If Class 0 specified, matches anything NOT in $lclass0excl 
			else if (!$Lookahead[$i] && isset($this->OTLdata[$checkpos]) && isset($lclass0excl[$this->OTLdata[$checkpos]['uni']]) ) {
				return false;
			}
			else if (!isset($this->OTLdata[$checkpos]) || !isset($Lookahead[$i][$this->OTLdata[$checkpos]['uni']])) {
				return false;
			}
		}
		return $matched;
	}





	function _getClassDefinitionTable($offset) {
		if (isset($this->LuDataCache[$this->fontkey][$offset])) {
			$GlyphByClass = $this->LuDataCache[$this->fontkey][$offset];
		}
		else {
			$this->seek($offset);
			$ClassFormat = $this->read_ushort();
			$GlyphClass = array();
	//		$GlyphByClass = array(0=>array());	// NB This forces an index[0]
			if ($ClassFormat == 1) {
				$StartGlyph = $this->read_ushort();
				$GlyphCount = $this->read_ushort();
				for ($i=0;$i<$GlyphCount;$i++) {
					$GlyphClass[$i]['startGlyphID'] = $StartGlyph + $i;
					$GlyphClass[$i]['endGlyphID'] = $StartGlyph + $i;
					$GlyphClass[$i]['class'] = $this->read_ushort();
					for($g=$GlyphClass[$i]['startGlyphID'];$g<=$GlyphClass[$i]['endGlyphID'];$g++) {
						$GlyphByClass[$GlyphClass[$i]['class']][] = $this->glyphToChar($g);
					}
				}
			}
			else if ($ClassFormat == 2) {
				$tableCount = $this->read_ushort();
				for ($i=0;$i<$tableCount;$i++) {
					$GlyphClass[$i]['startGlyphID'] = $this->read_ushort();
					$GlyphClass[$i]['endGlyphID'] = $this->read_ushort();
					$GlyphClass[$i]['class'] = $this->read_ushort();
					for($g=$GlyphClass[$i]['startGlyphID'];$g<=$GlyphClass[$i]['endGlyphID'];$g++) {
						$GlyphByClass[$GlyphClass[$i]['class']][] = $this->glyphToChar($g);
					}
				}
			}
			ksort($GlyphByClass);
			$this->LuDataCache[$this->fontkey][$offset] = $GlyphByClass;
		}
		return $GlyphByClass;
	}

	function count_bits($n) {     
		for ($c=0; $n; $c++) {
			$n &= $n - 1; // clear the least significant bit set
		}
		return $c;
	}

	function _getValueRecord($ValueFormat) {	// Common ValueRecord for GPOS
		// Only returns 3 possible: $vra['XPlacement'] $vra['YPlacement'] $vra['XAdvance'] 
		$vra = array();
		// Horizontal adjustment for placement - in design units
		if (($ValueFormat & 0x0001) == 0x0001) { $vra['XPlacement'] = $this->read_short(); }
		// Vertical adjustment for placement - in design units
		if (($ValueFormat & 0x0002) == 0x0002) { $vra['YPlacement'] = $this->read_short(); }
		// Horizontal adjustment for advance - in design units (only used for horizontal writing)
		if (($ValueFormat & 0x0004) == 0x0004) { $vra['XAdvance'] = $this->read_short(); }
		// Vertical adjustment for advance - in design units (only used for vertical writing)
		if (($ValueFormat & 0x0008) == 0x0008) { $this->read_short(); }
		// Offset to Device table for horizontal placement-measured from beginning of PosTable (may be NULL)
		if (($ValueFormat & 0x0010) == 0x0010) { $this->read_ushort(); }
		// Offset to Device table for vertical placement-measured from beginning of PosTable (may be NULL)
		if (($ValueFormat & 0x0020) == 0x0020) { $this->read_ushort(); }
		// Offset to Device table for horizontal advance-measured from beginning of PosTable (may be NULL)
		if (($ValueFormat & 0x0040) == 0x0040) { $this->read_ushort(); }
		// Offset to Device table for vertical advance-measured from beginning of PosTable (may be NULL)
		if (($ValueFormat & 0x0080) == 0x0080) { $this->read_ushort(); }
		return $vra;
	}

	function _getAnchorTable($offset=0) {
		if ($offset) { $this->seek($offset); }
		$AnchorFormat = $this->read_ushort();
		$XCoordinate = $this->read_short(); 
		$YCoordinate = $this->read_short(); 
		// Format 2 specifies additional link to contour point; Format 3 additional Device table
		return array($XCoordinate, $YCoordinate);
	}

	function _getMarkRecord($offset, $MarkPos) {
		$this->seek($offset);
		$MarkCount = $this->read_ushort();
		$this->skip($MarkPos*4);
		$Class = $this->read_ushort(); 
		$MarkAnchor = $offset + $this->read_ushort(); 	// = Offset to anchor table
		list($x,$y) = $this->_getAnchorTable($MarkAnchor );
		$MarkRecord = array('Class'=>$Class, 'AnchorX'=>$x, 'AnchorY'=>$y);
		return $MarkRecord;
	}

	function _getGCOMignoreString($flag, $MarkFilteringSet) {
		// If ignoreFlag set, combine all ignore glyphs into -> "(?:( 0FBA1| 0FBA2| 0FBA3)*)"
		// else "()"
		// for Input - set on secondary Lookup table if in Context, and set Backtrack and Lookahead on Context Lookup
		$str = "";
		$ignoreflag = 0;

		// Flag & 0xFF?? = MarkAttachmentType
		if ($flag & 0xFF00) {
			// "a lookup must ignore any mark glyphs that are not in the specified mark attachment class"
			// $this->MarkAttachmentType is already adjusted for this i.e. contains all Marks except those in the MarkAttachmentClassDef table
			$MarkAttachmentType = $flag >> 8;
			$ignoreflag = $flag; 
			$str = $this->MarkAttachmentType[$MarkAttachmentType]; 
		}

		// Flag & 0x0010 = UseMarkFilteringSet
		if ($flag & 0x0010) {
			die("This font [".$this->fontkey."] contains MarkGlyphSets - Not tested yet"); 
			// Change also in ttfontsuni.php
			if ($MarkFilteringSet=='') die("This font [".$this->fontkey."] contains MarkGlyphSets - but MarkFilteringSet not set"); 
			$str = $this->MarkGlyphSets[$MarkFilteringSet]; 
		}

		// If Ignore Marks set, supercedes any above
		// Flag & 0x0008 = Ignore Marks - (unless already done with MarkAttachmentType)
		if (($flag & 0x0008) == 0x0008 && ($flag & 0xFF00) == 0) { 
			$ignoreflag = 8; 
			$str = $this->GlyphClassMarks; 
		}

		// Flag & 0x0004 = Ignore Ligatures  
		if (($flag & 0x0004) == 0x0004) {
			$ignoreflag += 4; 
			if ($str) { $str .= "|"; }
			$str .= $this->GlyphClassLigatures; 
		}
		// Flag & 0x0002 = Ignore BaseGlyphs  
		if (($flag & 0x0002) == 0x0002) {
			$ignoreflag += 2; 
			if ($str) { $str .= "|"; }
			$str .= $this->GlyphClassBases; 
		}
		if ($str) { return "((?:(?:" . $str . "))*)"; }
		else return "()";
	}

	function _checkGCOMignore($flag, $glyph, $MarkFilteringSet) {
		$ignore = false;
		// Flag & 0x0008 = Ignore Marks - (unless already done with MarkAttachmentType)
		if (($flag & 0x0008 && ($flag & 0xFF00) == 0) && strpos($this->GlyphClassMarks,$glyph)) { $ignore = true; }
		if (($flag & 0x0004) && strpos($this->GlyphClassLigatures,$glyph)) { $ignore = true; }
		if (($flag & 0x0002) && strpos($this->GlyphClassBases,$glyph)) { $ignore = true; }
		// Flag & 0xFF?? = MarkAttachmentType
		if ($flag & 0xFF00) {
			// "a lookup must ignore any mark glyphs that are not in the specified mark attachment class"
			// $this->MarkAttachmentType is already adjusted for this i.e. contains all Marks except those in the MarkAttachmentClassDef table
			if (strpos($this->MarkAttachmentType[($flag >> 8)],$glyph)) { $ignore = true; }
		}
		// Flag & 0x0010 = UseMarkFilteringSet
		if (($flag & 0x0010) && strpos($this->MarkGlyphSets[$MarkFilteringSet],$glyph)) { $ignore = true; }
		return $ignore;
	}

////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////
//////////       BIDI ALGORITHM         ////////////////////////
////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////
// These functions are called from mpdf after GSUB/GPOS has taken place
// At this stage the bidi-type is in string form
////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////
/*
Bidirectional Character Types
=============================
Type 	Description 	General Scope
Strong 	
L 	Left-to-Right 		LRM, most alphabetic, syllabic, Han ideographs, non-European or non-Arabic digits, ...
LRE 	Left-to-Right Embedding LRE
LRO 	Left-to-Right Override 	LRO
R 	Right-to-Left 		RLM, Hebrew alphabet, and related punctuation
AL 	Right-to-Left Arabic 	Arabic, Thaana, and Syriac alphabets, most punctuation specific to those scripts, ...
RLE 	Right-to-Left Embedding RLE
RLO 	Right-to-Left Override 	RLO
Weak 	
PDF 	Pop Directional Format 		PDF
EN 	European Number 			European digits, Eastern Arabic-Indic digits, ...
ES 	European Number Separator 	Plus sign, minus sign
ET 	European Number Terminator 	Degree sign, currency symbols, ...
AN 	Arabic Number 			Arabic-Indic digits, Arabic decimal and thousands separators, ...
CS 	Common Number Separator 	Colon, comma, full stop (period), No-break space, ...
NSM 	Nonspacing Mark 			Characters marked Mn (Nonspacing_Mark) and Me (Enclosing_Mark) in the Unicode Character Database
BN 	Boundary Neutral 			Default ignorables, non-characters, and control characters, other than those explicitly given other types.
Neutral 	
B 	Paragraph Separator 	Paragraph separator, appropriate Newline Functions, higher-level protocol paragraph determination
S 	Segment Separator 	Tab
WS 	Whitespace 			Space, figure space, line separator, form feed, General Punctuation spaces, ...
ON 	Other Neutrals 		All other characters, including OBJECT REPLACEMENT CHARACTER
*/

function _bidiSort($ta, $str='', $dir, &$chunkOTLdata, $useGPOS) {

	$pel = 0;	// paragraph embedding level
	$maxlevel = 0;
	$numchars = count($chunkOTLdata['char_data']);

	// Set the initial paragraph embedding level
	if ($dir == 'rtl') { $pel = 1; } 
	else { $pel = 0; }


	// X1. Begin by setting the current embedding level to the paragraph embedding level. Set the directional override status to neutral. 
	// Current Embedding Level
	$cel = $pel;
	// directional override status (-1 is Neutral)
	$dos = -1;
	$remember = array();

	// Array of characters data
	$chardata = Array();

	// Process each character iteratively, applying rules X2 through X9. Only embedding levels from 0 to 61 are valid in this phase.
	// In the resolution of levels in rules I1 and I2, the maximum embedding level of 62 can be reached.
	for ($i=0; $i < $numchars; ++$i) {
		if ($chunkOTLdata['char_data'][$i]['uni'] == 8235) {	// RLE
			// X2. With each RLE, compute the least greater odd embedding level.
			//	a. If this new level would be valid, then this embedding code is valid. Remember (push) the current embedding level and override status. Reset the current level to this new level, and reset the override status to neutral.
			//	b. If the new level would not be valid, then this code is invalid. Do not change the current level or override status.
			$next_level = $cel + ($cel % 2) + 1;
			if ($next_level < 62) {
				$remember[] = array('num' => 8235, 'cel' => $cel, 'dos' => $dos);
				$cel = $next_level;
				$dos = -1;
			}
		}
		else if ($chunkOTLdata['char_data'][$i]['uni'] == 8234) {	// LRE
			// X3. With each LRE, compute the least greater even embedding level.
			//	a. If this new level would be valid, then this embedding code is valid. Remember (push) the current embedding level and override status. Reset the current level to this new level, and reset the override status to neutral.
			//	b. If the new level would not be valid, then this code is invalid. Do not change the current level or override status.
			$next_level = $cel + 2 - ($cel % 2);
			if ( $next_level < 62 ) {
				$remember[] = array('num' => 8234, 'cel' => $cel, 'dos' => $dos);
				$cel = $next_level;
				$dos = -1;
			}
		}
		else if ($chunkOTLdata['char_data'][$i]['uni'] == 8238) { // RLO
			// X4. With each RLO, compute the least greater odd embedding level.
			//	a. If this new level would be valid, then this embedding code is valid. Remember (push) the current embedding level and override status. Reset the current level to this new level, and reset the override status to right-to-left.
			//	b. If the new level would not be valid, then this code is invalid. Do not change the current level or override status.
			$next_level = $cel + ($cel % 2) + 1;
			if ($next_level < 62) {
				$remember[] = array('num' => 8238, 'cel' => $cel, 'dos' => $dos);
				$cel = $next_level;
				$dos = UCDN::BIDI_CLASS_R;
			}
		}
		else if ($chunkOTLdata['char_data'][$i]['uni'] == 8237) {	// LRO
			// X5. With each LRO, compute the least greater even embedding level.
			//	a. If this new level would be valid, then this embedding code is valid. Remember (push) the current embedding level and override status. Reset the current level to this new level, and reset the override status to left-to-right.
			//	b. If the new level would not be valid, then this code is invalid. Do not change the current level or override status.
			$next_level = $cel + 2 - ($cel % 2);
			if ( $next_level < 62 ) {
				$remember[] = array('num' => 8237, 'cel' => $cel, 'dos' => $dos);
				$cel = $next_level;
				$dos = UCDN::BIDI_CLASS_L;
			}
		}
		else if ($chunkOTLdata['char_data'][$i]['uni'] == 8236) {	// PDF
			// X7. With each PDF, determine the matching embedding or override code. If there was a valid matching code, restore (pop) the last remembered (pushed) embedding level and directional override.
			if (count($remember)) {
				$last = count($remember ) - 1;
				if (($remember[$last]['num'] == 8235) || ($remember[$last]['num'] == 8234) || ($remember[$last]['num'] == 8238) || 
					($remember[$last]['num'] == 8237)) {
					$match = array_pop($remember);
					$cel = $match['cel'];
					$dos = $match['dos'];
				}
			}
		}
		else if ($chunkOTLdata['char_data'][$i]['uni'] == 10) {	// NEW LINE
			// Reset to start values
			$cel = $pel;
			$dos = -1;
			$remember = array();
		}
		else {
			// X6. For all types besides RLE, LRE, RLO, LRO, and PDF:
			//	a. Set the level of the current character to the current embedding level.
			//	b. When the directional override status is not neutral, reset the current character type to directional override status.
			if ($dos != -1) { $chardir = $dos; } 
			else {
				$chardir = $chunkOTLdata['char_data'][$i]['bidi_class'];
			}
			// stores string characters and other information
			if (isset($chunkOTLdata['GPOSinfo'][$i])) { $gpos = $chunkOTLdata['GPOSinfo'][$i]; }
			else $gpos = '';
			$chardata[] = array('char' => $chunkOTLdata['char_data'][$i]['uni'], 'level' => $cel, 'type' => $chardir, 'group' => $chunkOTLdata['group']{$i}, 'GPOSinfo' => $gpos);
		}
	}

	$numchars = count($chardata);

	// X8. All explicit directional embeddings and overrides are completely terminated at the end of each paragraph.
	// Paragraph separators are not included in the embedding.
	// X9. Remove all RLE, LRE, RLO, LRO, and PDF codes.
	// This is effectively done by only saving other codes to chardata

	// X10. Determine the start-of-sequence (sor) and end-of-sequence (eor) types, either L or R, for each isolating run sequence. These depend on the higher of the two levels on either side of the sequence boundary:
	// For sor, compare the level of the first character in the sequence with the level of the character preceding it in the paragraph or if there is none, with the paragraph embedding level.
	// For eor, compare the level of the last character in the sequence with the level of the character following it in the paragraph or if there is none, with the paragraph embedding level.
	// If the higher level is odd, the sor or eor is R; otherwise, it is L.

	$prelevel = $pel;
	$postlevel = $pel;
	$cel = $prelevel;	// current embedding level
	for ($i=0; $i < $numchars; ++$i) {
		$level = $chardata[$i]['level'];
		if ($i==0) { $left = $prelevel; }
		else { $left = $chardata[$i-1]['level']; }
		if ($i==($numchars-1)) { $right = $postlevel; }
		else { $right = $chardata[$i+1]['level']; }
		$chardata[$i]['sor'] = max($left, $level) % 2 ? UCDN::BIDI_CLASS_R : UCDN::BIDI_CLASS_L;
		$chardata[$i]['eor'] = max($right, $level) % 2 ? UCDN::BIDI_CLASS_R : UCDN::BIDI_CLASS_L;
	}



	// 3.3.3 Resolving Weak Types
	// Weak types are now resolved one level run at a time. At level run boundaries where the type of the character on the other side of the boundary is required, the type assigned to sor or eor is used.
	// Nonspacing marks are now resolved based on the previous characters.

	// W1. Examine each nonspacing mark (NSM) in the level run, and change the type of the NSM to the type of the previous character. If the NSM is at the start of the level run, it will get the type of sor.
	for ($i=0; $i < $numchars; ++$i) {
		if ($chardata[$i]['type'] == UCDN::BIDI_CLASS_NSM) {
			if ($i==0 || $chardata[$i]['level']!=$chardata[$i-1]['level']) {
				$chardata[$i]['type'] = $chardata[$i]['sor'];
			}
			else {
				$chardata[$i]['type'] = $chardata[($i-1)]['type'];
			}
		}
	}

	// W2. Search backward from each instance of a European number until the first strong type (R, L, AL, or sor) is found. If an AL is found, change the type of the European number to Arabic number.
	$prevlevel = -1;
	$levcount = 0;
	for ($i=0; $i < $numchars; ++$i) {
		if ($chardata[$i]['type'] == UCDN::BIDI_CLASS_EN) {
			$found = false;
			for ($j=$levcount; $j >= 0; $j--) {
				if ($chardata[$j]['type'] == UCDN::BIDI_CLASS_AL) { $chardata[$i]['type'] = UCDN::BIDI_CLASS_AN; $found = true; break; }
				else if (($chardata[$j]['type'] == UCDN::BIDI_CLASS_L) || ($chardata[$j]['type'] == UCDN::BIDI_CLASS_R)) { $found = true; break; }
			}
		}
		if ($chardata[$i]['level'] != $prevlevel) { $levcount = 0; } 
		else { ++$levcount; }
		$prevlevel = $chardata[$i]['level'];
	}

	// W3. Change all ALs to R.
	for ($i=0; $i < $numchars; ++$i) {
 		if ($chardata[$i]['type'] == UCDN::BIDI_CLASS_AL) { $chardata[$i]['type'] = UCDN::BIDI_CLASS_R; }
	}

	// W4. A single European separator between two European numbers changes to a European number. A single common separator between two numbers of the same type changes to that type.
	for ($i=1; $i < $numchars; ++$i) {
		if ( ($i+1) < $numchars && $chardata[($i)]['level'] == $chardata[($i+1)]['level'] && $chardata[($i)]['level'] == $chardata[($i-1)]['level']) {
			if ($chardata[$i]['type'] == UCDN::BIDI_CLASS_ES && $chardata[($i-1)]['type'] == UCDN::BIDI_CLASS_EN && $chardata[($i+1)]['type'] == UCDN::BIDI_CLASS_EN) {
				$chardata[$i]['type'] = UCDN::BIDI_CLASS_EN;
			}
			else if ($chardata[$i]['type'] == UCDN::BIDI_CLASS_CS && $chardata[($i-1)]['type'] == UCDN::BIDI_CLASS_EN && $chardata[($i+1)]['type'] == UCDN::BIDI_CLASS_EN) {
				$chardata[$i]['type'] = UCDN::BIDI_CLASS_EN;
			}
			else if ($chardata[$i]['type'] == UCDN::BIDI_CLASS_CS && $chardata[($i-1)]['type'] == UCDN::BIDI_CLASS_AN && $chardata[($i+1)]['type'] == UCDN::BIDI_CLASS_AN) {
				$chardata[$i]['type'] = UCDN::BIDI_CLASS_AN;
			}
		}
	}

	// W5. A sequence of European terminators adjacent to European numbers changes to all European numbers.
	for ($i=0; $i < $numchars; ++$i) {
		if ($chardata[$i]['type'] == UCDN::BIDI_CLASS_ET) {
			if ($i > 0 && $chardata[($i-1)]['type'] == UCDN::BIDI_CLASS_EN && $chardata[($i)]['level'] == $chardata[($i-1)]['level']) {
				$chardata[$i]['type'] = UCDN::BIDI_CLASS_EN;
			}
			else {
				$j = $i+1;
				while ($j < $numchars && $chardata[$j]['level'] == $chardata[$i]['level'] ) {
					if ($chardata[$j]['type'] == UCDN::BIDI_CLASS_EN) {
						$chardata[$i]['type'] = UCDN::BIDI_CLASS_EN;
						break;
					}
					else if ($chardata[$j]['type'] != UCDN::BIDI_CLASS_ET) { break; }
					++$j;
				}
			}
		}
	}

	// W6. Otherwise, separators and terminators change to Other Neutral.
	for ($i=0; $i < $numchars; ++$i) {
		if (($chardata[$i]['type'] == UCDN::BIDI_CLASS_ET) || ($chardata[$i]['type'] == UCDN::BIDI_CLASS_ES) || ($chardata[$i]['type'] == UCDN::BIDI_CLASS_CS)) {
			$chardata[$i]['type'] = UCDN::BIDI_CLASS_ON;
		}
	}

	//W7. Search backward from each instance of a European number until the first strong type (R, L, or sor) is found. If an L is found, then change the type of the European number to L.
	for ($i=0; $i < $numchars; ++$i) {
		if ($chardata[$i]['type'] == UCDN::BIDI_CLASS_EN) {
			if ($i==0) {	// Start of Level run
				if ($chardata[$i]['sor']==UCDN::BIDI_CLASS_L) $chardata[$i]['type'] = $chardata[$i]['sor'];
			}
			else {
				for ($j=$i-1; $j >= 0; $j--) {
					if ($chardata[$j]['level'] != $chardata[$i]['level']) {	// Level run boundary 
						if ($chardata[$j+1]['sor']==UCDN::BIDI_CLASS_L) $chardata[$i]['type'] = $chardata[$j+1]['sor'];
						break;
					}
					else if ($chardata[$j]['type'] == UCDN::BIDI_CLASS_L) {
						$chardata[$i]['type'] = UCDN::BIDI_CLASS_L;
						break;
					}
					else if ($chardata[$j]['type'] == UCDN::BIDI_CLASS_R) {
						break;
					}
				}
			}
		}
	}

	// N1. A sequence of neutrals takes the direction of the surrounding strong text if the text on both sides has the same direction. European and Arabic numbers act as if they were R in terms of their influence on neutrals. Start-of-level-run (sor) and end-of-level-run (eor) are used at level run boundaries.
	for ($i=0; $i < $numchars; ++$i) {
		if ($chardata[$i]['type'] == UCDN::BIDI_CLASS_ON || $chardata[$i]['type'] == UCDN::BIDI_CLASS_WS) {
			$left = -1;
			// LEFT
			if ($i==0) { 	// first char 
				$left = $chardata[($i)]['sor'];
			}
			else if ($chardata[($i-1)]['level'] != $chardata[($i)]['level']) { 	// run boundary
				$left = $chardata[($i)]['sor'];
			}
			else if ($chardata[($i-1)]['type'] == UCDN::BIDI_CLASS_L) {
				$left = UCDN::BIDI_CLASS_L;
			}
			else if ($chardata[($i-1)]['type'] == UCDN::BIDI_CLASS_R || $chardata[($i-1)]['type'] == UCDN::BIDI_CLASS_EN || $chardata[($i-1)]['type'] == UCDN::BIDI_CLASS_AN) {
				$left = UCDN::BIDI_CLASS_R;
			}
			// RIGHT
			$right = -1;
			$j=$i;
			// move to the right of any following neutrals OR hit a run boundary
			while(($chardata[$j]['type'] == UCDN::BIDI_CLASS_ON || $chardata[$j]['type'] == UCDN::BIDI_CLASS_WS) && $j<=($numchars-1)) { 
				if ($j==($numchars-1)) { 	// last char
					$right = $chardata[($j)]['eor'];
					break;
				}
				else if ($chardata[($j+1)]['level'] != $chardata[($j)]['level']) { 	// run boundary
					$right = $chardata[($j)]['eor'];
					break;
				}
				else if ($chardata[($j+1)]['type'] == UCDN::BIDI_CLASS_L) {
					$right = UCDN::BIDI_CLASS_L;
					break;
				}
				else if ($chardata[($j+1)]['type'] == UCDN::BIDI_CLASS_R || $chardata[($j+1)]['type'] == UCDN::BIDI_CLASS_EN || $chardata[($j+1)]['type'] == UCDN::BIDI_CLASS_AN) {
					$right = UCDN::BIDI_CLASS_R;
					break;
				}
				$j++; 
			}
			if ($left > -1 && $left==$right) {
				$chardata[$i]['orig_type'] = $chardata[$i]['type'];	// Need to store the original 'WS' for reference in L1 below
				$chardata[$i]['type'] = $left;
			}
		}
	}

	// N2. Any remaining neutrals take the embedding direction
	for ($i=0; $i < $numchars; ++$i) {
		if ($chardata[$i]['type'] == UCDN::BIDI_CLASS_ON || $chardata[$i]['type'] == UCDN::BIDI_CLASS_WS) {
			$chardata[$i]['type'] = ($chardata[$i]['level'] % 2) ?  UCDN::BIDI_CLASS_R : UCDN::BIDI_CLASS_L;
			$chardata[$i]['orig_type'] = $chardata[$i]['type'];	// Need to store the original 'WS' for reference in L1 below
		}
	}

	// I1. For all characters with an even (left-to-right) embedding direction, those of type R go up one level and those of type AN or EN go up two levels.
	// I2. For all characters with an odd (right-to-left) embedding direction, those of type L, EN or AN go up one level.
	for ($i=0; $i < $numchars; ++$i) {
		$odd = $chardata[$i]['level'] % 2;
		if ($odd) {
			if (($chardata[$i]['type'] == UCDN::BIDI_CLASS_L) || ($chardata[$i]['type'] == UCDN::BIDI_CLASS_AN) || ($chardata[$i]['type'] == UCDN::BIDI_CLASS_EN)) {
				$chardata[$i]['level'] += 1;
			}
		}
		else {
			if ($chardata[$i]['type'] == UCDN::BIDI_CLASS_R) { $chardata[$i]['level'] += 1; }
			else if (($chardata[$i]['type'] == UCDN::BIDI_CLASS_AN) || ($chardata[$i]['type'] == UCDN::BIDI_CLASS_EN)) { $chardata[$i]['level'] += 2; }
		}
		$maxlevel = max($chardata[$i]['level'],$maxlevel);
	}

// NB
//	Separate into lines at this point************
//

	// L1. On each line, reset the embedding level of the following characters to the paragraph embedding level:
	//	1. Segment separators (Tab) 'S',
	//	2. Paragraph separators 'B',
	//	3. Any sequence of whitespace characters 'WS' preceding a segment separator or paragraph separator, and
	//	4. Any sequence of whitespace characters 'WS' at the end of the line.
	//	The types of characters used here are the original types, not those modified by the previous phase cf N1 and N2*******
	//	Because a Paragraph Separator breaks lines, there will be at most one per line, at the end of that line.

	for ($i=($numchars-1); $i>0; $i--) {
		if ($chardata[$i]['type'] == UCDN::BIDI_CLASS_WS || (isset($chardata[$i]['orig_type']) && $chardata[$i]['orig_type'] == UCDN::BIDI_CLASS_WS)) {
				$chardata[$i]['level'] = $pel;
		}
		else { break; }
	}


	// L2. From the highest level found in the text to the lowest odd level on each line, including intermediate levels not actually present in the text, reverse any contiguous sequence of characters that are at that level or higher.
	for ($j=$maxlevel; $j > 0; $j--) {
		$ordarray = array();
		$revarr = array();
		$onlevel = false;
		for ($i=0; $i < $numchars; ++$i) {
			if ($chardata[$i]['level'] >= $j) {
				$onlevel = true;

				// L4. A character is depicted by a mirrored glyph if and only if (a) the resolved directionality of that character is R, and (b) the Bidi_Mirrored property value of that character is true. 
				if (isset(UCDN::$mirror_pairs[$chardata[$i]['char']]) && $chardata[$i]['type']==UCDN::BIDI_CLASS_R) {
					$chardata[$i]['char'] = UCDN::$mirror_pairs[$chardata[$i]['char']];
				}

				$revarr[] = $chardata[$i];
			}
			else {
				if ($onlevel) {
					$revarr = array_reverse($revarr);
					$ordarray = array_merge($ordarray, $revarr);
					$revarr = Array();
					$onlevel = false;
				}
				$ordarray[] = $chardata[$i];
			}
		}
		if ($onlevel) {
			$revarr = array_reverse($revarr);
			$ordarray = array_merge($ordarray, $revarr);
		}
		$chardata = $ordarray;
 	}

	$group = '';
	$e = '';
	$GPOS = array();
	$cctr = 0;
	$rtl_content = 0x0;
	foreach ($chardata as $cd) {
		$e.=code2utf($cd['char']);
		$group .= $cd['group'];
		if ($useGPOS && is_array($cd['GPOSinfo'])) {
			$GPOS[$cctr] = $cd['GPOSinfo'];
			$GPOS[$cctr]['wDir'] = ($cd['level'] % 2) ? 'RTL' : 'LTR';
		}
		if($cd['type']==UCDN::BIDI_CLASS_L) { $rtl_content |= 1; }
		else if($cd['type']==UCDN::BIDI_CLASS_R) { $rtl_content |= 2; }
		$cctr++;
	}


	$chunkOTLdata['group'] = $group ;
	if ($useGPOS) {
		$chunkOTLdata['GPOSinfo'] = $GPOS;
	}

	return array($e,$rtl_content);
}

// **********************************************************************************************
// The following versions for BidiSort work on amalgamated chunks to process the whole paragraph
// Firstly set the level in the OTLdata - called from fn printbuffer() [_bidiPrepare]
// Secondly re-order - called from fn writeFlowingBlock and FinishFlowingBlock, when already divided into lines. [_bidiReorder]
// **********************************************************************************************

function _bidiPrepare(&$para, $dir) {

	// Set the initial paragraph embedding level
	$pel = 0;	// paragraph embedding level
	if ($dir == 'rtl') { $pel = 1; } 

	// X1. Begin by setting the current embedding level to the paragraph embedding level. Set the directional override status to neutral. 
	// Current Embedding Level
	$cel = $pel;
	// directional override status (-1 is Neutral)
	$dos = -1;
	$remember = array();
	$controlchars = false;
	$strongrtl = false;
	$diid = 0;	// direction isolate ID
	$dictr = 0;	// direction isolate counter

	// Process each character iteratively, applying rules X2 through X9. Only embedding levels from 0 to 61 are valid in this phase.
	// In the resolution of levels in rules I1 and I2, the maximum embedding level of 62 can be reached.
	$numchunks = count($para);
	for ($nc=0;$nc<$numchunks;$nc++) {
		$chunkOTLdata =& $para[$nc][18];

		$numchars = count($chunkOTLdata['char_data']);
		for ($i=0; $i < $numchars; ++$i) {
		  if ($chunkOTLdata['char_data'][$i]['uni'] == 8235) {	// RLE
			// X2. With each RLE, compute the least greater odd embedding level.
			//	a. If this new level would be valid, then this embedding code is valid. Remember (push) the current embedding level and override status. Reset the current level to this new level, and reset the override status to neutral.
			//	b. If the new level would not be valid, then this code is invalid. Do not change the current level or override status.
			$next_level = $cel + ($cel % 2) + 1;
			if ($next_level < 62) {
				$remember[] = array('num' => 8235, 'cel' => $cel, 'dos' => $dos);
				$cel = $next_level;
				$dos = -1;
				$controlchars = true;
			}
		  }
		  else if ($chunkOTLdata['char_data'][$i]['uni'] == 8234) {	// LRE
			// X3. With each LRE, compute the least greater even embedding level.
			//	a. If this new level would be valid, then this embedding code is valid. Remember (push) the current embedding level and override status. Reset the current level to this new level, and reset the override status to neutral.
			//	b. If the new level would not be valid, then this code is invalid. Do not change the current level or override status.
			$next_level = $cel + 2 - ($cel % 2);
			if ( $next_level < 62 ) {
				$remember[] = array('num' => 8234, 'cel' => $cel, 'dos' => $dos);
				$cel = $next_level;
				$dos = -1;
				$controlchars = true;
			}
		  }
		  else if ($chunkOTLdata['char_data'][$i]['uni'] == 8238) { // RLO
			// X4. With each RLO, compute the least greater odd embedding level.
			//	a. If this new level would be valid, then this embedding code is valid. Remember (push) the current embedding level and override status. Reset the current level to this new level, and reset the override status to right-to-left.
			//	b. If the new level would not be valid, then this code is invalid. Do not change the current level or override status.
			$next_level = $cel + ($cel % 2) + 1;
			if ($next_level < 62) {
				$remember[] = array('num' => 8238, 'cel' => $cel, 'dos' => $dos);
				$cel = $next_level;
				$dos = UCDN::BIDI_CLASS_R;
				$controlchars = true;
			}
		  }
		  else if ($chunkOTLdata['char_data'][$i]['uni'] == 8237) {	// LRO
			// X5. With each LRO, compute the least greater even embedding level.
			//	a. If this new level would be valid, then this embedding code is valid. Remember (push) the current embedding level and override status. Reset the current level to this new level, and reset the override status to left-to-right.
			//	b. If the new level would not be valid, then this code is invalid. Do not change the current level or override status.
			$next_level = $cel + 2 - ($cel % 2);
			if ( $next_level < 62 ) {
				$remember[] = array('num' => 8237, 'cel' => $cel, 'dos' => $dos);
				$cel = $next_level;
				$dos = UCDN::BIDI_CLASS_L;
				$controlchars = true;
			}
		  }
		  else if ($chunkOTLdata['char_data'][$i]['uni'] == 8236) {	// PDF
			// X7. With each PDF, determine the matching embedding or override code. If there was a valid matching code, restore (pop) the last remembered (pushed) embedding level and directional override.
			if (count($remember)) {
				$last = count($remember ) - 1;
				if (($remember[$last]['num'] == 8235) || ($remember[$last]['num'] == 8234) || ($remember[$last]['num'] == 8238) || 
					($remember[$last]['num'] == 8237)) {
					$match = array_pop($remember);
					$cel = $match['cel'];
					$dos = $match['dos'];
				}
			}
		  }
		  else if ($chunkOTLdata['char_data'][$i]['uni'] == 8294 || $chunkOTLdata['char_data'][$i]['uni'] == 8295 ||
			$chunkOTLdata['char_data'][$i]['uni'] == 8296) {	// LRI // RLI // FSI
			// X5a. With each RLI:
			// X5b. With each LRI:
			// X5c. With each FSI, apply rules P2 and P3 for First Strong character
			//	Set the RLI/LRI/FSI embedding level to the embedding level of the last entry on the directional status stack.
			if ($dos != -1) { $chardir = $dos; } 
			else { $chardir = $chunkOTLdata['char_data'][$i]['bidi_class']; }
			$chunkOTLdata['char_data'][$i]['level'] = $cel;
			$chunkOTLdata['char_data'][$i]['type'] = $chardir;
			$chunkOTLdata['char_data'][$i]['diid'] = $diid;

			$fsi = '';
			// X5c. With each FSI, apply rules P2 and P3 within the isolate run for First Strong character
			if ($chunkOTLdata['char_data'][$i]['uni'] == 8296) {	// FSI
				$lvl = 0;
				$nc2 = $nc;
				$i2 = $i;
				while (!($nc2==($numchunks-1) && $i2==((count($para[$nc2][18]['char_data']))-1))) { 	// while not at end of last chunk
					$i2++;
					if ($i2 >= count($para[$nc2][18]['char_data'])) { 
						$nc2++;
						$i2 = 0;
					}
					if ($lvl > 0) { continue; }
					if ($para[$nc2][18]['char_data'][$i2]['uni'] == 8294 || $para[$nc2][18]['char_data'][$i2]['uni'] == 8295 || $para[$nc2][18]['char_data'][$i2]['uni'] == 8296) {
						$lvl++;
						continue;
					}
					if ($para[$nc2][18]['char_data'][$i2]['uni'] == 8297) {
						$lvl--;
						if ($lvl < 0) { break; }
					}
					if ($para[$nc2][18]['char_data'][$i2]['bidi_class'] === UCDN::BIDI_CLASS_L || $para[$nc2][18]['char_data'][$i2]['bidi_class'] == UCDN::BIDI_CLASS_AL || $para[$nc2][18]['char_data'][$i2]['bidi_class'] === UCDN::BIDI_CLASS_R) {
						$fsi = $para[$nc2][18]['char_data'][$i2]['bidi_class'];
						break;
					}
				}
				// if fsi not found, fsi is same as paragraph embedding level
				if (!$fsi && $fsi!==0) {
					if ($pel==1) { $fsi = UCDN::BIDI_CLASS_R ; }
					else { $fsi = UCDN::BIDI_CLASS_L ; }
				}
			}

			if ($chunkOTLdata['char_data'][$i]['uni'] == 8294 || $fsi === UCDN::BIDI_CLASS_L ) {	// LRI or FSI-L
			//	Compute the least even embedding level greater than the embedding level of the last entry on the directional status stack.
				$next_level = $cel + 2 - ($cel % 2);
			}
			else if ($chunkOTLdata['char_data'][$i]['uni'] == 8295 || $fsi == UCDN::BIDI_CLASS_R || $fsi == UCDN::BIDI_CLASS_AL ) {	// RLI or FSI-R
			//	Compute the least odd embedding level greater than the embedding level of the last entry on the directional status stack.
				$next_level = $cel + ($cel % 2) + 1;
			}


			//	Increment the isolate count by one, and push an entry consisting of the new embedding level,
			//	neutral directional override status, and true directional isolate status onto the directional status stack.
			$remember[] = array('num' => $chunkOTLdata['char_data'][$i]['uni'], 'cel' => $cel, 'dos' => $dos, 'diid' => $diid);
			$cel = $next_level;
			$dos = -1;
			$diid = ++$dictr;	// Set new direction isolate ID after incrementing direction isolate counter

			$controlchars = true;
		  }
		  else if ($chunkOTLdata['char_data'][$i]['uni'] == 8297) {	// PDI
			// X6a. With each PDI, perform the following steps:
			//	Pop the last entry from the directional status stack and decrement the isolate count by one.
			while (count($remember)) {
				$last = count($remember ) - 1;
				if (($remember[$last]['num'] == 8294) || ($remember[$last]['num'] == 8295) || ($remember[$last]['num'] == 8296)) {
					$match = array_pop($remember);
					$cel = $match['cel'];
					$dos = $match['dos'];
					$diid = $match['diid'];
					break;
				}
				// End/close any open embedding states not explicitly closed during the isolate
				else if (($remember[$last]['num'] == 8235) || ($remember[$last]['num'] == 8234) || ($remember[$last]['num'] == 8238) ||
					($remember[$last]['num'] == 8237)) {
					$match = array_pop($remember);
				}
			}
			//	In all cases, set the PDIs level to the embedding level of the last entry on the directional status stack left after the steps above.
			//	NB The level assigned to an isolate initiator is always the same as that assigned to the matching PDI.
			if ($dos != -1) { $chardir = $dos; } 
			else { $chardir = $chunkOTLdata['char_data'][$i]['bidi_class']; }
			$chunkOTLdata['char_data'][$i]['level'] = $cel;
			$chunkOTLdata['char_data'][$i]['type'] = $chardir;
			$chunkOTLdata['char_data'][$i]['diid'] = $diid;
			$controlchars = true;
		  }
		  else if ($chunkOTLdata['char_data'][$i]['uni'] == 10) {	// NEW LINE
			// Reset to start values
			$cel = $pel;
			$dos = -1;
			$remember = array();
		  }
		  else {
			// X6. For all types besides RLE, LRE, RLO, LRO, and PDF:
			//	a. Set the level of the current character to the current embedding level.
			//	b. When the directional override status is not neutral, reset the current character type to directional override status.
			if ($dos != -1) { $chardir = $dos; } 
			else {
				$chardir = $chunkOTLdata['char_data'][$i]['bidi_class'];
				if ($chardir == UCDN::BIDI_CLASS_R || $chardir == UCDN::BIDI_CLASS_AL) { $strongrtl = true; }
			}
			$chunkOTLdata['char_data'][$i]['level'] = $cel;
			$chunkOTLdata['char_data'][$i]['type'] = $chardir;
			$chunkOTLdata['char_data'][$i]['diid'] = $diid;
		  }
		}
		// X8. All explicit directional embeddings and overrides are completely terminated at the end of each paragraph.
		// Paragraph separators are not included in the embedding.
		// X9. Remove all RLE, LRE, RLO, LRO, and PDF codes.
		if ($controlchars) {
			$this->removeChar($para[$nc][0], $para[$nc][18], "\xe2\x80\xaa");
			$this->removeChar($para[$nc][0], $para[$nc][18], "\xe2\x80\xab");
			$this->removeChar($para[$nc][0], $para[$nc][18], "\xe2\x80\xac");
			$this->removeChar($para[$nc][0], $para[$nc][18], "\xe2\x80\xad");
			$this->removeChar($para[$nc][0], $para[$nc][18], "\xe2\x80\xae");
			preg_replace("/\x{202a}-\x{202e}/u", '', $para[$nc][0]);
		}
	}

	// Remove any blank chunks made by removing directional codes
	$numchunks = count($para);
	for ($nc=($numchunks-1);$nc>=0;$nc--) {
		if (count($para[$nc][18]['char_data'])==0) { array_splice($para, $nc, 1); }
	}
	if ($dir != 'rtl' && !$strongrtl && !$controlchars) { return; }

	$numchunks = count($para);

	// X10. Determine the start-of-sequence (sor) and end-of-sequence (eor) types, either L or R, for each isolating run sequence. These depend on the higher of the two levels on either side of the sequence boundary:
	// For sor, compare the level of the first character in the sequence with the level of the character preceding it in the paragraph or if there is none, with the paragraph embedding level.
	// For eor, compare the level of the last character in the sequence with the level of the character following it in the paragraph or if there is none, with the paragraph embedding level.
	// If the higher level is odd, the sor or eor is R; otherwise, it is L.

	for ($ir=0; $ir<=$dictr;$ir++) {
		$prelevel = $pel;	
		$postlevel = $pel;
		$firstchar = true;
		for ($nc=0;$nc<$numchunks;$nc++) {
			$chardata =& $para[$nc][18]['char_data'];
			$numchars = count($chardata);
			for ($i=0; $i < $numchars; ++$i) {
				if (!isset($chardata[$i]['diid']) || $chardata[$i]['diid']!=$ir) { continue; }	// Ignore characters in a different isolate run
				$right = $postlevel;
				$nc2 = $nc;
				$i2 = $i;
				while (!($nc2==($numchunks-1) && $i2==((count($para[$nc2][18]['char_data']))-1))) { 	// while not at end of last chunk
					$i2++;
					if ($i2 >= count($para[$nc2][18]['char_data'])) { 
						$nc2++;
						$i2 = 0;
					}

					if (isset($para[$nc2][18]['char_data'][$i2]['diid']) && $para[$nc2][18]['char_data'][$i2]['diid']==$ir) { $right = $para[$nc2][18]['char_data'][$i2]['level']; break; }
				}

				$level = $chardata[$i]['level'];
				if ($firstchar || $level!=$prelevel) {
					$chardata[$i]['sor'] = max($prelevel, $level) % 2 ? UCDN::BIDI_CLASS_R : UCDN::BIDI_CLASS_L;
				}
				if (($nc==($numchunks-1) && $i==($numchars-1)) || $level != $right) {
					$chardata[$i]['eor'] = max($right, $level) % 2 ? UCDN::BIDI_CLASS_R : UCDN::BIDI_CLASS_L;
				}
				$prelevel = $level;
				$firstchar = false;
			}
		}
	}


	// 3.3.3 Resolving Weak Types
	// Weak types are now resolved one level run at a time. At level run boundaries where the type of the character on the other side of the boundary is required, the type assigned to sor or eor is used.
	// Nonspacing marks are now resolved based on the previous characters.

	// W1. Examine each nonspacing mark (NSM) in the level run, and change the type of the NSM to the type of the previous character. If the NSM is at the start of the level run, it will get the type of sor.
	for ($ir=0; $ir<=$dictr;$ir++) {
		$prevtype = 0;
		for ($nc=0;$nc<$numchunks;$nc++) {
			$chardata =& $para[$nc][18]['char_data'];
			$numchars = count($chardata);
			for ($i=0; $i < $numchars; ++$i) {
				if (!isset($chardata[$i]['diid']) || $chardata[$i]['diid']!=$ir) { continue; }	// Ignore characters in a different isolate run
				if ($chardata[$i]['type'] == UCDN::BIDI_CLASS_NSM) {
					if (isset($chardata[$i]['sor'])) {
						$chardata[$i]['type'] = $chardata[$i]['sor'];
					}
					else {
						$chardata[$i]['type'] = $prevtype;
					}
				}
				$prevtype = $chardata[$i]['type'];
			}
		}
	}

	// W2. Search backward from each instance of a European number until the first strong type (R, L, AL or sor) is found. If an AL is found, change the type of the European number to Arabic number.
	for ($ir=0; $ir<=$dictr;$ir++) {
		$laststrongtype = -1;
		for ($nc=0;$nc<$numchunks;$nc++) {
			$chardata =& $para[$nc][18]['char_data'];
			$numchars = count($chardata);
			for ($i=0; $i < $numchars; ++$i) {
				if (!isset($chardata[$i]['diid']) || $chardata[$i]['diid']!=$ir) { continue; }	// Ignore characters in a different isolate run
				if (isset($chardata[$i]['sor'])) { $laststrongtype = $chardata[$i]['sor']; }
				if ($chardata[$i]['type'] == UCDN::BIDI_CLASS_EN && $laststrongtype == UCDN::BIDI_CLASS_AL ) { 
					$chardata[$i]['type'] = UCDN::BIDI_CLASS_AN; 
				}
				if ($chardata[$i]['type'] == UCDN::BIDI_CLASS_L || $chardata[$i]['type'] == UCDN::BIDI_CLASS_R || $chardata[$i]['type'] == UCDN::BIDI_CLASS_AL) { 
					$laststrongtype =  $chardata[$i]['type']; 
				}
			}
		}
	}


	// W3. Change all ALs to R.
	for ($nc=0;$nc<$numchunks;$nc++) {
		$chardata =& $para[$nc][18]['char_data'];
		$numchars = count($chardata);
		for ($i=0; $i < $numchars; ++$i) {
 			if (isset($chardata[$i]['type']) && $chardata[$i]['type'] == UCDN::BIDI_CLASS_AL) { $chardata[$i]['type'] = UCDN::BIDI_CLASS_R; }
		}
	}


	// W4. A single European separator between two European numbers changes to a European number. A single common separator between two numbers of the same type changes to that type.
	for ($ir=0; $ir<=$dictr;$ir++) {
		$prevtype = -1;
		$nexttype = -1;
		for ($nc=0;$nc<$numchunks;$nc++) {
			$chardata =& $para[$nc][18]['char_data'];
			$numchars = count($chardata);
			for ($i=0; $i < $numchars; ++$i) {
				if (!isset($chardata[$i]['diid']) || $chardata[$i]['diid']!=$ir) { continue; }	// Ignore characters in a different isolate run

				// Get next type
				$nexttype = -1; 
				$nc2 = $nc;
				$i2 = $i;
				while (!($nc2==($numchunks-1) && $i2==((count($para[$nc2][18]['char_data']))-1))) { 	// while not at end of last chunk
					$i2++;
					if ($i2 >= count($para[$nc2][18]['char_data'])) { 
						$nc2++;
						$i2 = 0;
					}

					if (isset($para[$nc2][18]['char_data'][$i2]['diid']) && $para[$nc2][18]['char_data'][$i2]['diid']==$ir) { $nexttype = $para[$nc2][18]['char_data'][$i2]['type']; break; }
				}

				if (!isset($chardata[$i]['sor']) && !isset($chardata[$i]['eor'])) {
					if ($chardata[$i]['type'] == UCDN::BIDI_CLASS_ES && $prevtype == UCDN::BIDI_CLASS_EN && $nexttype == UCDN::BIDI_CLASS_EN) {
						$chardata[$i]['type'] = UCDN::BIDI_CLASS_EN;
					}
					else if ($chardata[$i]['type'] == UCDN::BIDI_CLASS_CS && $prevtype == UCDN::BIDI_CLASS_EN && $nexttype == UCDN::BIDI_CLASS_EN) {
						$chardata[$i]['type'] = UCDN::BIDI_CLASS_EN;
					}
					else if ($chardata[$i]['type'] == UCDN::BIDI_CLASS_CS && $prevtype == UCDN::BIDI_CLASS_AN && $nexttype == UCDN::BIDI_CLASS_AN) {
						$chardata[$i]['type'] = UCDN::BIDI_CLASS_AN;
					}
				}
				$prevtype = $chardata[$i]['type'];
			}
		}
	}

	// W5. A sequence of European terminators adjacent to European numbers changes to all European numbers.
	for ($ir=0; $ir<=$dictr;$ir++) {
		$prevtype = -1;
		$nexttype = -1;
		for ($nc=0;$nc<$numchunks;$nc++) {
			$chardata =& $para[$nc][18]['char_data'];
			$numchars = count($chardata);
			for ($i=0; $i < $numchars; ++$i) {
				if (!isset($chardata[$i]['diid']) || $chardata[$i]['diid']!=$ir) { continue; }	// Ignore characters in a different isolate run
				if (isset($chardata[$i]['sor'])) { $prevtype = $chardata[$i]['sor']; }

				if ($chardata[$i]['type'] == UCDN::BIDI_CLASS_ET) {
					if ($prevtype == UCDN::BIDI_CLASS_EN) {
						$chardata[$i]['type'] = UCDN::BIDI_CLASS_EN;
					}
					else if (!isset($chardata[$i]['eor'])) {
						$nexttype = -1;
						$nc2 = $nc;
						$i2 = $i;
						while (!($nc2==($numchunks-1) && $i2==((count($para[$nc2][18]['char_data']))-1))) { // while not at end of last chunk
							$i2++;
							if ($i2 >= count($para[$nc2][18]['char_data'])) { 
								$nc2++;
								$i2 = 0;
							}
							if ($para[$nc2][18]['char_data'][$i2]['diid']!=$ir) { continue; }
							$nexttype = $para[$nc2][18]['char_data'][$i2]['type']; 
							if (isset($para[$nc2][18]['char_data'][$i2]['sor'])) { break; }
							if ($nexttype == UCDN::BIDI_CLASS_EN) {
								$chardata[$i]['type'] = UCDN::BIDI_CLASS_EN;
								break;
							}
							else if ($nexttype != UCDN::BIDI_CLASS_ET) { break; }
						}
					}
				}
				$prevtype = $chardata[$i]['type'];
			}
		}
	}

	// W6. Otherwise, separators and terminators change to Other Neutral.
	for ($nc=0;$nc<$numchunks;$nc++) {
		$chardata =& $para[$nc][18]['char_data'];
		$numchars = count($chardata);
		for ($i=0; $i < $numchars; ++$i) {
			if (isset($chardata[$i]['type']) && (($chardata[$i]['type'] == UCDN::BIDI_CLASS_ET) || ($chardata[$i]['type'] == UCDN::BIDI_CLASS_ES) || ($chardata[$i]['type'] == UCDN::BIDI_CLASS_CS))) {
				$chardata[$i]['type'] = UCDN::BIDI_CLASS_ON;
			}
		}
	}

	//W7. Search backward from each instance of a European number until the first strong type (R, L, or sor) is found. If an L is found, then change the type of the European number to L.
	for ($ir=0; $ir<=$dictr;$ir++) {
		$laststrongtype = -1;
		for ($nc=0;$nc<$numchunks;$nc++) {
			$chardata =& $para[$nc][18]['char_data'];
			$numchars = count($chardata);
			for ($i=0; $i < $numchars; ++$i) {
				if (!isset($chardata[$i]['diid']) || $chardata[$i]['diid']!=$ir) { continue; }	// Ignore characters in a different isolate run
				if (isset($chardata[$i]['sor'])) { $laststrongtype = $chardata[$i]['sor']; }
				if (isset($chardata[$i]['type']) && $chardata[$i]['type'] == UCDN::BIDI_CLASS_EN && $laststrongtype == UCDN::BIDI_CLASS_L ) { 
					$chardata[$i]['type'] = UCDN::BIDI_CLASS_L; 
				}
				if (isset($chardata[$i]['type']) && ($chardata[$i]['type'] == UCDN::BIDI_CLASS_L || $chardata[$i]['type'] == UCDN::BIDI_CLASS_R || $chardata[$i]['type'] == UCDN::BIDI_CLASS_AL)) { 
					$laststrongtype =  $chardata[$i]['type']; 
				}
			}
		}
	}

	// N1. A sequence of neutrals takes the direction of the surrounding strong text if the text on both sides has the same direction. European and Arabic numbers act as if they were R in terms of their influence on neutrals. Start-of-level-run (sor) and end-of-level-run (eor) are used at level run boundaries.
	for ($ir=0; $ir<=$dictr;$ir++) {
		$laststrongtype = -1;
		for ($nc=0;$nc<$numchunks;$nc++) {
			$chardata =& $para[$nc][18]['char_data'];
			$numchars = count($chardata);
			for ($i=0; $i < $numchars; ++$i) {
				if (!isset($chardata[$i]['diid']) || $chardata[$i]['diid']!=$ir) { continue; }	// Ignore characters in a different isolate run
				if (isset($chardata[$i]['sor'])) { $laststrongtype = $chardata[$i]['sor']; }
				if ($chardata[$i]['type'] == UCDN::BIDI_CLASS_ON || $chardata[$i]['type'] == UCDN::BIDI_CLASS_WS) {
					$left = -1;
					// LEFT
					if ($laststrongtype == UCDN::BIDI_CLASS_R || $laststrongtype == UCDN::BIDI_CLASS_EN || $laststrongtype == UCDN::BIDI_CLASS_AN) { 
						$left = UCDN::BIDI_CLASS_R; 
					}
					else if ($laststrongtype == UCDN::BIDI_CLASS_L) { 
						$left = UCDN::BIDI_CLASS_L; 
					}
					// RIGHT
					$right = -1;
					// move to the right of any following neutrals OR hit a run boundary

					if (isset($chardata[$i]['eor'])) { 
						$right = $chardata[$i]['eor']; 
					}
					else {
						$nexttype = -1;
						$nc2 = $nc;
						$i2 = $i;
						while (!($nc2==($numchunks-1) && $i2==((count($para[$nc2][18]['char_data']))-1))) { // while not at end of last chunk
							$i2++;
							if ($i2 >= count($para[$nc2][18]['char_data'])) { 
								$nc2++;
								$i2 = 0;
							}
							if (!isset($para[$nc2][18]['char_data'][$i2]['diid']) || $para[$nc2][18]['char_data'][$i2]['diid']!=$ir) { continue; }
							$nexttype = $para[$nc2][18]['char_data'][$i2]['type']; 
							if ($nexttype == UCDN::BIDI_CLASS_R || $nexttype == UCDN::BIDI_CLASS_EN || $nexttype == UCDN::BIDI_CLASS_AN) { 
								$right =  UCDN::BIDI_CLASS_R; 
								break;
							}
							else if ($nexttype == UCDN::BIDI_CLASS_L) { 
								$right =  UCDN::BIDI_CLASS_L; 
								break;
							}
							else if (isset($para[$nc2][18]['char_data'][$i2]['eor'])) { 
								$right = $para[$nc2][18]['char_data'][$i2]['eor']; 
								break;
							}
						}
					}

					if ($left > -1 && $left==$right) {
						$chardata[$i]['orig_type'] = $chardata[$i]['type'];	// Need to store the original 'WS' for reference in L1 below
						$chardata[$i]['type'] = $left;
					}
				}
				else if ($chardata[$i]['type'] == UCDN::BIDI_CLASS_L || $chardata[$i]['type'] == UCDN::BIDI_CLASS_R || $chardata[$i]['type'] == UCDN::BIDI_CLASS_EN || $chardata[$i]['type'] == UCDN::BIDI_CLASS_AN) { 
					$laststrongtype =  $chardata[$i]['type']; 
				}
			}
		}
	}

	// N2. Any remaining neutrals take the embedding direction
	for ($nc=0;$nc<$numchunks;$nc++) {
		$chardata =& $para[$nc][18]['char_data'];
		$numchars = count($chardata);
		for ($i=0; $i < $numchars; ++$i) {
			if (isset($chardata[$i]['type']) && ($chardata[$i]['type'] == UCDN::BIDI_CLASS_ON || $chardata[$i]['type'] == UCDN::BIDI_CLASS_WS)) {
				$chardata[$i]['orig_type'] = $chardata[$i]['type'];	// Need to store the original 'WS' for reference in L1 below
				$chardata[$i]['type'] = ($chardata[$i]['level'] % 2) ?  UCDN::BIDI_CLASS_R : UCDN::BIDI_CLASS_L;
			}
		}
	}

	// I1. For all characters with an even (left-to-right) embedding direction, those of type R go up one level and those of type AN or EN go up two levels.
	// I2. For all characters with an odd (right-to-left) embedding direction, those of type L, EN or AN go up one level.
	for ($nc=0;$nc<$numchunks;$nc++) {
		$chardata =& $para[$nc][18]['char_data'];
		$numchars = count($chardata);
		for ($i=0; $i < $numchars; ++$i) {
			if (isset($chardata[$i]['level'])) { 
				$odd = $chardata[$i]['level'] % 2;
				if ($odd) {
					if (($chardata[$i]['type'] == UCDN::BIDI_CLASS_L) || ($chardata[$i]['type'] == UCDN::BIDI_CLASS_AN) || ($chardata[$i]['type'] == UCDN::BIDI_CLASS_EN)) {
						$chardata[$i]['level'] += 1;
					}
				}
				else {
					if ($chardata[$i]['type'] == UCDN::BIDI_CLASS_R) { $chardata[$i]['level'] += 1; }
					else if (($chardata[$i]['type'] == UCDN::BIDI_CLASS_AN) || ($chardata[$i]['type'] == UCDN::BIDI_CLASS_EN)) { $chardata[$i]['level'] += 2; }
				}
			}
		}
	}

	// Remove Isolate formatters
	$numchunks = count($para);
	if ($controlchars) {
		for ($nc=0;$nc<$numchunks;$nc++) {
			$this->removeChar($para[$nc][0], $para[$nc][18], "\xe2\x81\xa6");
			$this->removeChar($para[$nc][0], $para[$nc][18], "\xe2\x81\xa7");
			$this->removeChar($para[$nc][0], $para[$nc][18], "\xe2\x81\xa8");
			$this->removeChar($para[$nc][0], $para[$nc][18], "\xe2\x81\xa9");
			preg_replace("/\x{2066}-\x{2069}/u", '', $para[$nc][0]);
		}
		// Remove any blank chunks made by removing directional codes
		for ($nc=($numchunks-1);$nc>=0;$nc--) {
			if (count($para[$nc][18]['char_data'])==0) { array_splice($para, $nc, 1); }
		}
	}

}



// Reorder, once divided into lines

function _bidiReorder(&$chunkorder, &$content, &$cOTLdata, $blockdir) {

	$bidiData = array();

	// First combine into one array (and get the highest level in use)
	$numchunks = count($content);
	$maxlevel = 0;
	for ($nc=0;$nc<$numchunks;$nc++) {
		$numchars = count($cOTLdata[$nc]['char_data']);
		for ($i=0; $i < $numchars; ++$i) {

			$carac = array();
			if (isset($cOTLdata[$nc]['GPOSinfo'][$i])) {$carac['GPOSinfo'] = $cOTLdata[$nc]['GPOSinfo'][$i]; }
			$carac['uni'] = $cOTLdata[$nc]['char_data'][$i]['uni'];
			if (isset($cOTLdata[$nc]['char_data'][$i]['type'])) $carac['type'] = $cOTLdata[$nc]['char_data'][$i]['type'];
			if (isset($cOTLdata[$nc]['char_data'][$i]['level'])) $carac['level'] = $cOTLdata[$nc]['char_data'][$i]['level'];
			if (isset($cOTLdata[$nc]['char_data'][$i]['orig_type'])) { $carac['orig_type'] = $cOTLdata[$nc]['char_data'][$i]['orig_type']; }
			$carac['group'] = $cOTLdata[$nc]['group']{$i};
			$carac['chunkid'] = $chunkorder[$nc];	// gives font id and/or object ID

			$maxlevel = max((isset($carac['level']) ? $carac['level'] : 0),$maxlevel);
			$bidiData[] = $carac;
		}
	}
	if ($maxlevel==0) { return; }

	$numchars = count($bidiData);

	// L1. On each line, reset the embedding level of the following characters to the paragraph embedding level:
	//	1. Segment separators (Tab) 'S',
	//	2. Paragraph separators 'B',
	//	3. Any sequence of whitespace characters 'WS' preceding a segment separator or paragraph separator, and
	//	4. Any sequence of whitespace characters 'WS' at the end of the line.
	//	The types of characters used here are the original types, not those modified by the previous phase cf N1 and N2*******
	//	Because a Paragraph Separator breaks lines, there will be at most one per line, at the end of that line.

	// Set the initial paragraph embedding level
	if ($blockdir == 'rtl') { $pel = 1; } 
	else { $pel = 0; }

	for ($i=($numchars-1); $i>0; $i--) {
		if ($bidiData[$i]['type'] == UCDN::BIDI_CLASS_WS || (isset($bidiData[$i]['orig_type']) && $bidiData[$i]['orig_type'] == UCDN::BIDI_CLASS_WS)) {
				$bidiData[$i]['level'] = $pel;
		}
		else { break; }
	}


	// L2. From the highest level found in the text to the lowest odd level on each line, including intermediate levels not actually present in the text, reverse any contiguous sequence of characters that are at that level or higher.
	for ($j=$maxlevel; $j > 0; $j--) {
		$ordarray = array();
		$revarr = array();
		$onlevel = false;
		for ($i=0; $i < $numchars; ++$i) {
			if ($bidiData[$i]['level'] >= $j) {
				$onlevel = true;
				// L4. A character is depicted by a mirrored glyph if and only if (a) the resolved directionality of that character is R, and (b) the Bidi_Mirrored property value of that character is true. 
				if (isset(UCDN::$mirror_pairs[$bidiData[$i]['uni']]) && $bidiData[$i]['type']==UCDN::BIDI_CLASS_R) {
					$bidiData[$i]['uni'] = UCDN::$mirror_pairs[$bidiData[$i]['uni']];
				}

				$revarr[] = $bidiData[$i];
			}
			else {
				if ($onlevel) {
					$revarr = array_reverse($revarr);
					$ordarray = array_merge($ordarray, $revarr);
					$revarr = Array();
					$onlevel = false;
				}
				$ordarray[] = $bidiData[$i];
			}
		}
		if ($onlevel) {
			$revarr = array_reverse($revarr);
			$ordarray = array_merge($ordarray, $revarr);
		}
		$bidiData = $ordarray;
 	}

	$content = array();
	$cOTLdata = array();
	$chunkorder = array();



	$nc = -1;	// New chunk order ID
	$chunkid = -1;

	foreach ($bidiData as $carac) {
		if ($carac['chunkid'] != $chunkid) {
			$nc++;
			$chunkorder[$nc] = $carac['chunkid'];
			$cctr = 0;
			$content[$nc] = '';
			$cOTLdata[$nc]['group'] = '';
		}
		if ($carac['uni'] != 0xFFFC) {  	// Object replacement character (65532)
			$content[$nc] .= code2utf($carac['uni']);
			$cOTLdata[$nc]['group'] .= $carac['group'];
			if (!empty($carac['GPOSinfo'])) {
				if (isset($carac['GPOSinfo'])) { $cOTLdata[$nc]['GPOSinfo'][$cctr] = $carac['GPOSinfo']; }
				$cOTLdata[$nc]['GPOSinfo'][$cctr]['wDir'] = ($carac['level'] % 2) ? 'RTL' : 'LTR';
			}
		}
		$chunkid = $carac['chunkid'];
		$cctr++;
	}

}






////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////
// These functions are called from mpdf after GSUB/GPOS has taken place
// At this stage the bidi-type is in string form
////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////
function splitOTLdata(&$cOTLdata, $OTLcutoffpos, $OTLrestartpos='') {
	if (!$OTLrestartpos) { $OTLrestartpos = $OTLcutoffpos; }
	$newOTLdata = array('GPOSinfo' => array(), 'char_data' => array());
	$newOTLdata['group'] = substr($cOTLdata['group'],$OTLrestartpos);
	$cOTLdata['group'] = substr($cOTLdata['group'],0,$OTLcutoffpos);

	if (isset($cOTLdata['GPOSinfo']) && $cOTLdata['GPOSinfo']) {
		foreach($cOTLdata['GPOSinfo'] AS $k => $val) {
			if ($k >= $OTLrestartpos) {
				$newOTLdata['GPOSinfo'][($k - $OTLrestartpos)] = $val;
			}
			if ($k >= $OTLcutoffpos) {
				unset($cOTLdata['GPOSinfo'][$k]);
				//$cOTLdata['GPOSinfo'][$k] = array();
			}
		}
	}
	if (isset($cOTLdata['char_data'])) {
		$newOTLdata['char_data'] = array_slice($cOTLdata['char_data'], $OTLrestartpos);
		array_splice($cOTLdata['char_data'], $OTLcutoffpos);
	}

	// Not necessary - easier to debug
	if (isset($cOTLdata['GPOSinfo'])) ksort($cOTLdata['GPOSinfo']);
	if (isset($newOTLdata['GPOSinfo'])) ksort($newOTLdata['GPOSinfo']);

	return $newOTLdata;
}

function sliceOTLdata($OTLdata, $pos, $len) {
	$newOTLdata = array('GPOSinfo' => array(), 'char_data' => array());
	$newOTLdata['group'] = substr($OTLdata['group'],$pos,$len);

	if ($OTLdata['GPOSinfo']) {
		foreach($OTLdata['GPOSinfo'] AS $k => $val) {
			if ($k >= $pos && $k <($pos+$len)) {
				$newOTLdata['GPOSinfo'][($k - $pos)] = $val;
			}
		}
	}

	if (isset($OTLdata['char_data'])) { $newOTLdata['char_data'] = array_slice($OTLdata['char_data'], $pos, $len); }

	// Not necessary - easier to debug
	if ($newOTLdata['GPOSinfo']) ksort($newOTLdata['GPOSinfo']);

	return $newOTLdata;
}

// Remove one or more occurrences of $char (single character) from $txt and adjust OTLdata
function removeChar(&$txt, &$cOTLdata, $char) {
	while(mb_strpos($txt, $char, 0, $this->mpdf->mb_enc )!== false) {
		$pos = mb_strpos($txt, $char, 0, $this->mpdf->mb_enc );
		$newGPOSinfo = array();
		$cOTLdata['group'] = substr_replace($cOTLdata['group'], '', $pos, 1);
		if ($cOTLdata['GPOSinfo']) {
			foreach($cOTLdata['GPOSinfo'] AS $k => $val) {
				if ($k > $pos) {
					$newGPOSinfo[($k - 1)] = $val;
				}
				else if ($k!=$pos) {
					$newGPOSinfo[$k] = $val;
				}
			}
			$cOTLdata['GPOSinfo'] = $newGPOSinfo;
		}
		if (isset($cOTLdata['char_data'])) { array_splice($cOTLdata['char_data'], $pos, 1); }

		$txt = preg_replace("/".$char."/",'',$txt, 1);
	}
}

// Remove one or more occurrences of $char (single character) from $txt and adjust OTLdata
function replaceSpace(&$txt, &$cOTLdata) {
	$char = chr(194).chr(160);	// NBSP
	while(mb_strpos($txt, $char, 0, $this->mpdf->mb_enc )!== false) {
		$pos = mb_strpos($txt, $char, 0, $this->mpdf->mb_enc );
		if ($cOTLdata['char_data'][$pos]['uni'] == 160) {
			$cOTLdata['char_data'][$pos]['uni'] = 32;
		}
		$txt = preg_replace("/".$char."/",' ',$txt, 1);
	}
}

function trimOTLdata(&$cOTLdata, $Left=true, $Right=true) {

	$len = count($cOTLdata['char_data']);
	$nLeft = 0;
	$nRight = 0;
	for($i=0;$i<$len;$i++) {
		if($cOTLdata['char_data'][$i]['uni']==32 || $cOTLdata['char_data'][$i]['uni']==12288) { $nLeft++; }	// 12288 = 0x3000 = CJK space
		else { break; }
	}
	for($i=($len-1);$i>=0;$i--) {
		if($cOTLdata['char_data'][$i]['uni']==32 || $cOTLdata['char_data'][$i]['uni']==12288) { $nRight++; }	// 12288 = 0x3000 = CJK space
		else { break; }
	}

	// Trim Right
	if ($Right && $nRight) {
		$cOTLdata['group'] = substr($cOTLdata['group'],0,strlen($cOTLdata['group'])-$nRight);
		if ($cOTLdata['GPOSinfo']) {
			foreach($cOTLdata['GPOSinfo'] AS $k => $val) {
				if ($k >= $len-$nRight) {
					unset($cOTLdata['GPOSinfo'][$k]);
				}
			}
		}
		if (isset($cOTLdata['char_data'])) {
			for($i=0;$i<$nRight;$i++) {
				array_pop($cOTLdata['char_data']);
			}
		}
	}
	// Trim Left
	if ($Left && $nLeft) {
		$cOTLdata['group'] = substr($cOTLdata['group'],$nLeft);
		if ($cOTLdata['GPOSinfo']) {
			$newPOSinfo = array();
			foreach($cOTLdata['GPOSinfo'] AS $k => $val) {
				if ($k >= $nLeft) {
					$newPOSinfo[$k-$nLeft] = $cOTLdata['GPOSinfo'][$k];
				}
			}
			$cOTLdata['GPOSinfo'] = $newPOSinfo;
		}
		if (isset($cOTLdata['char_data'])) {
			for($i=0;$i<$nLeft;$i++) {
				array_shift($cOTLdata['char_data']);
			}
		}
	}
}


////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////
//////////         GENERAL OTL FUNCTIONS       /////////////////
////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////


	function glyphToChar($gid) {
		return (ord($this->glyphIDtoUni[$gid*3]) << 16) + (ord($this->glyphIDtoUni[$gid*3+1]) << 8) + ord($this->glyphIDtoUni[$gid*3+2]);
	}

	function unicode_hex($unicode_dec) {
		return (str_pad(strtoupper(dechex($unicode_dec)),5,'0',STR_PAD_LEFT));
	}

	function seek($pos) {
		$this->_pos = $pos;
	}

	function skip($delta) {
		$this->_pos += $delta;
	}
	function read_short() {
		$a = (ord($this->ttfOTLdata[$this->_pos])<<8) + ord($this->ttfOTLdata[$this->_pos+1]);
		if ($a & (1 << 15) ) { 
			$a = ($a - (1 << 16)); 
		}
		$this->_pos += 2;
		return $a;
	}

	function read_ushort() {
		$a = (ord($this->ttfOTLdata[$this->_pos])<<8) + ord($this->ttfOTLdata[$this->_pos+1]);
		$this->_pos += 2;
		return $a;
	}


	function _getCoverageGID() {
	// Called from Lookup Type 1, Format 1 - returns glyphIDs rather than hexstrings
	// Need to do this separately to cache separately
	// Otherwise the same as fn below _getCoverage
		$offset = $this->_pos;
		if (isset($this->LuDataCache[$this->fontkey]['GID'][$offset])) {
			$g = $this->LuDataCache[$this->fontkey]['GID'][$offset];
		}
		else {
			$g = array();
			$CoverageFormat= $this->read_ushort();
			if ($CoverageFormat == 1) {
				$CoverageGlyphCount= $this->read_ushort();
				for ($gid=0;$gid<$CoverageGlyphCount;$gid++) {
					$glyphID = $this->read_ushort();
					$g[] = $glyphID;
				}
			}						
			if ($CoverageFormat == 2) {
				$RangeCount= $this->read_ushort();
				for ($r=0;$r<$RangeCount;$r++) {
					$start = $this->read_ushort();
					$end = $this->read_ushort();
					$StartCoverageIndex = $this->read_ushort(); // n/a
					for ($glyphID=$start;$glyphID<=$end;$glyphID++) {
						$g[] = $glyphID;
					}
				}
			}
			$this->LuDataCache[$this->fontkey]['GID'][$offset] = $g;
		}
		return $g;						
	}


	function _getCoverage() {
		$offset = $this->_pos;
		if (isset($this->LuDataCache[$this->fontkey][$offset])) {
			$g = $this->LuDataCache[$this->fontkey][$offset];
		}
		else {
			$g = array();
			$CoverageFormat= $this->read_ushort();
			if ($CoverageFormat == 1) {
				$CoverageGlyphCount= $this->read_ushort();
				for ($gid=0;$gid<$CoverageGlyphCount;$gid++) {
					$glyphID = $this->read_ushort();
					$g[] = $this->unicode_hex($this->glyphToChar($glyphID)); 
				}
			}						
			if ($CoverageFormat == 2) {
				$RangeCount= $this->read_ushort();
				for ($r=0;$r<$RangeCount;$r++) {
					$start = $this->read_ushort();
					$end = $this->read_ushort();
					$StartCoverageIndex = $this->read_ushort(); // n/a
					for ($glyphID=$start;$glyphID<=$end;$glyphID++) {
						$g[] = $this->unicode_hex($this->glyphToChar($glyphID));
					}
				}
			}
			$this->LuDataCache[$this->fontkey][$offset] = $g;
		}
		return $g;						
	}

	function _getClasses($offset) {
		if (isset($this->LuDataCache[$this->fontkey][$offset])) {
			$GlyphByClass = $this->LuDataCache[$this->fontkey][$offset];
		}
		else {
			$this->seek($offset);
			$ClassFormat = $this->read_ushort();
			$GlyphByClass = array();
			if ($ClassFormat == 1) {
				$StartGlyph = $this->read_ushort();
				$GlyphCount = $this->read_ushort();
				for ($i=0;$i<$GlyphCount;$i++) {
					$startGlyphID = $StartGlyph + $i;
					$endGlyphID = $StartGlyph + $i;
					$class = $this->read_ushort();
					// Note: Font FreeSerif , tag "blws"
					// $BacktrackClasses[0] is defined ? a mistake in the font ???
					// Let's ignore for now
					if ($class > 0) {
						for($g=$startGlyphID;$g<=$endGlyphID;$g++) {
							if ($this->glyphToChar($g)) {
								$GlyphByClass[$class][$this->glyphToChar($g)] = 1;
							}
						}
					}
				}
			}
			else if ($ClassFormat == 2) {
				$tableCount = $this->read_ushort();
				for ($i=0;$i<$tableCount;$i++) {
					$startGlyphID = $this->read_ushort();
					$endGlyphID = $this->read_ushort();
					$class = $this->read_ushort();
					// Note: Font FreeSerif , tag "blws"
					// $BacktrackClasses[0] is defined ? a mistake in the font ???
					// Let's ignore for now
					if ($class > 0) {
						for($g=$startGlyphID;$g<=$endGlyphID;$g++) {
							if ($this->glyphToChar($g)) {
								$GlyphByClass[$class][$this->glyphToChar($g)] = 1;
							}
						}
					}
				}
			}
			$this->LuDataCache[$this->fontkey][$offset] = $GlyphByClass;
		}
		return $GlyphByClass;
	}


function _getOTLscriptTag($ScriptLang, $scripttag, $scriptblock, $shaper, $useOTL, $mode) {
	// ScriptLang is the array of available script/lang tags supported by the font
	// $scriptblock is the (number/code) for the script of the actual text string based on Unicode properties (UCDN::$uni_scriptblock)
	// $scripttag is the default tag derived from $scriptblock
/*
	http://www.microsoft.com/typography/otspec/ttoreg.htm
	http://www.microsoft.com/typography/otspec/scripttags.htm

Values for useOTL

Bit	dn	hn	Value
1	1	0x0001	GSUB/GPOS - Latin scripts
2	2	0x0002	GSUB/GPOS - Cyrillic scripts
3	4	0x0004	GSUB/GPOS - Greek scripts
4	8	0x0008	GSUB/GPOS - CJK scripts (excluding Hangul-Jamo)
5	16	0x0010	(Reserved)
6	32	0x0020	(Reserved)
7	64	0x0040	(Reserved)
8	128	0x0080	GSUB/GPOS - All other scripts (including all RTL scripts, complex scripts with shapers etc)

NB If change for RTL - cf. function magic_reverse_dir in mpdf.php to update

*/


	if ($scriptblock == UCDN::SCRIPT_LATIN) {
		if (!($useOTL & 0x01)) { return array('',false); }
	}
	else if ($scriptblock == UCDN::SCRIPT_CYRILLIC) {
		if (!($useOTL & 0x02)) { return array('',false); }
	}
	else if ($scriptblock == UCDN::SCRIPT_GREEK) {
		if (!($useOTL & 0x04)) { return array('',false); }
	}
	else if ($scriptblock >= UCDN::SCRIPT_HIRAGANA && $scriptblock <= UCDN::SCRIPT_YI ) { 
		if (!($useOTL & 0x08)) { return array('',false); }
	}
	else {
		if (!($useOTL & 0x80)) { return array('',false); }
	}

	//	If availabletags includes scripttag - choose
	if (isset($ScriptLang[$scripttag])) { return array($scripttag, false); }

	// 	If INDIC (or Myanmar) and available tag not includes new version, check if includes old version & choose old version
	if ($shaper) {
		switch($scripttag) {
			CASE 'bng2': if (isset($ScriptLang['beng'])) return array('beng',true);
			CASE 'dev2': if (isset($ScriptLang['deva'])) return array('deva',true);
			CASE 'gjr2': if (isset($ScriptLang['gujr'])) return array('gujr',true);
			CASE 'gur2': if (isset($ScriptLang['guru'])) return array('guru',true);
			CASE 'knd2': if (isset($ScriptLang['knda'])) return array('knda',true);
			CASE 'mlm2': if (isset($ScriptLang['mlym'])) return array('mlym',true);
			CASE 'ory2': if (isset($ScriptLang['orya'])) return array('orya',true);
			CASE 'tml2': if (isset($ScriptLang['taml'])) return array('taml',true);
			CASE 'tel2': if (isset($ScriptLang['telu'])) return array('telu',true);
			CASE 'mym2': if (isset($ScriptLang['mymr'])) return array('mymr',true);
		}
	}

	// 	choose DFLT if present 
	if (isset($ScriptLang['DFLT'])) { return array('DFLT', false); } 
	// 	else choose dflt if present
	if (isset($ScriptLang['dflt'])) { return array('dflt', false); } 
	// 	else return no scriptTag
	if (isset($ScriptLang['latn'])) { return array('latn', false); } 
	// 	else return no scriptTag
	return array('',false);

}



// LangSys tags
function _getOTLLangTag($ietf, $available) {
	// http://en.wikipedia.org/wiki/List_of_ISO_639-1_codes
	// http://www.microsoft.com/typography/otspec/languagetags.htm
	// IETF tag = e.g. en-US, und-Arab, sr-Cyrl cf. config_lang2fonts.php
	if ($available=='') { return ''; }
	$tags = preg_split('/-/',$ietf);
	$lang = '';
	$country = '';
	$script = '';
	$lang = strtolower($tags[0]);
	if (isset($tags[1]) && $tags[1]) { 
		if (strlen($tags[1]) == 2) { $country = strtolower($tags[1]); }
	}
	if (isset($tags[2]) && $tags[2]) { $country = strtolower($tags[2]); }

	if ($lang!='' && isset(UCDN::$ot_languages[$lang])) { $langsys = UCDN::$ot_languages[$lang]; }
	else if ($lang!='' && $country !='' && isset(UCDN::$ot_languages[$lang.''.$country])) { 
		$langsys = UCDN::$ot_languages[$lang.''.$country]; 
	}
	else { $langsys = "DFLT"; }
	if (strpos($available, $langsys)===false) { 
		if (strpos($available, "DFLT")!==false) { return "DFLT"; }
		else return ''; 
	}
	return $langsys;
}

function _dumpproc($GPOSSUB, $lookupID, $subtable, $Type, $Format, $ptr, $currGlyph, $level) {
	echo '<div style="padding-left: '.($level*2).'em;">';
	echo $GPOSSUB .' LookupID #'.$lookupID.' Subtable#'.$subtable .' Type: '.$Type.' Format: '.$Format.'<br />';
	echo '<div style="font-family:monospace">';
	echo 'Glyph position: '.$ptr.' Current Glyph: '.$currGlyph.'<br />';

	for ($i=0;$i<count($this->OTLdata);$i++) {
		if ($i==$ptr) { echo '<b>'; }
		echo $this->OTLdata[$i]['hex'] . ' ';
		if ($i==$ptr) { echo '</b>'; }
	}
	echo '<br />';

	for ($i=0;$i<count($this->OTLdata);$i++) {
		if ($i==$ptr) { echo '<b>'; }
		echo str_pad($this->OTLdata[$i]['uni'],5) . ' ';
		if ($i==$ptr) { echo '</b>'; }
	}
	echo '<br />';

	if ($GPOSSUB == 'GPOS') {
		for ($i=0;$i<count($this->OTLdata);$i++) {
			if (!empty($this->OTLdata[$i]['GPOSinfo'])) {
				echo $this->OTLdata[$i]['hex'] . ' &#x'.$this->OTLdata[$i]['hex'].'; ';
				print_r($this->OTLdata[$i]['GPOSinfo']);
				echo ' '; 
			}
		}
	}

	echo '</div>';
	echo '</div>';
}


}

?>