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
|
/* -*- mode: c++; c-basic-offset: 4 -*- */
#include "ft2font.h"
#include "mplutils.h"
#include <sstream>
#include "numpy/arrayobject.h"
/*
By definition, FT_FIXED as 2 16bit values stored in a single long.
We cast to long to ensure the correct Py::Int convertor is called
*/
#define FIXED_MAJOR(val) (long) ((val & 0xffff000) >> 16)
#define FIXED_MINOR(val) (long) (val & 0xffff)
/**
To improve the hinting of the fonts, this code uses a hack
presented here:
http://antigrain.com/research/font_rasterization/index.html
The idea is to limit the effect of hinting in the x-direction, while
preserving hinting in the y-direction. Since freetype does not
support this directly, the dpi in the x-direction is set higher than
in the y-direction, which affects the hinting grid. Then, a global
transform is placed on the font to shrink it back to the desired
size. While it is a bit surprising that the dpi setting affects
hinting, whereas the global transform does not, this is documented
behavior of freetype, and therefore hopefully unlikely to change.
The freetype 2 tutorial says:
NOTE: The transformation is applied to every glyph that is
loaded through FT_Load_Glyph and is completely independent of
any hinting process. This means that you won't get the same
results if you load a glyph at the size of 24 pixels, or a glyph
at the size at 12 pixels scaled by 2 through a transform,
because the hints will have been computed differently (except
you have disabled hints).
This hack is enabled only when VERTICAL_HINTING is defined, and will
only be effective when load_char and set_text are called with 'flags=
LOAD_DEFAULT', which is the default.
*/
#define VERTICAL_HINTING
#ifdef VERTICAL_HINTING
#define HORIZ_HINTING 8
#else
#define HORIZ_HINTING 1
#endif
FT_Library _ft2Library;
// FT2Image::FT2Image() :
// _isDirty(true),
// _buffer(NULL),
// _width(0), _height(0),
// _rgbCopy(NULL),
// _rgbaCopy(NULL) {
// _VERBOSE("FT2Image::FT2Image");
// }
FT2Image::FT2Image(unsigned long width, unsigned long height) :
_isDirty(true),
_buffer(NULL),
_width(0), _height(0),
_rgbCopy(NULL),
_rgbaCopy(NULL)
{
_VERBOSE("FT2Image::FT2Image");
resize(width, height);
}
FT2Image::~FT2Image()
{
_VERBOSE("FT2Image::~FT2Image");
delete [] _buffer;
_buffer = NULL;
delete _rgbCopy;
delete _rgbaCopy;
}
void
FT2Image::resize(long width, long height)
{
if (width < 0)
{
width = 1;
}
if (height < 0)
{
height = 1;
}
size_t numBytes = width * height;
if ((unsigned long)width != _width || (unsigned long)height != _height)
{
if (numBytes > _width*_height)
{
delete [] _buffer;
_buffer = NULL;
_buffer = new unsigned char [numBytes];
}
_width = (unsigned long)width;
_height = (unsigned long)height;
}
memset(_buffer, 0, numBytes);
_isDirty = true;
}
void
FT2Image::draw_bitmap(FT_Bitmap* bitmap,
FT_Int x,
FT_Int y)
{
_VERBOSE("FT2Image::draw_bitmap");
FT_Int image_width = (FT_Int)_width;
FT_Int image_height = (FT_Int)_height;
FT_Int char_width = bitmap->width;
FT_Int char_height = bitmap->rows;
FT_Int x1 = CLAMP(x, 0, image_width);
FT_Int y1 = CLAMP(y, 0, image_height);
FT_Int x2 = CLAMP(x + char_width, 0, image_width);
FT_Int y2 = CLAMP(y + char_height, 0, image_height);
FT_Int x_start = MAX(0, -x);
FT_Int y_offset = y1 - MAX(0, -y);
if (bitmap->pixel_mode == FT_PIXEL_MODE_GRAY) {
for (FT_Int i = y1; i < y2; ++i)
{
unsigned char* dst = _buffer + (i * image_width + x1);
unsigned char* src = bitmap->buffer + (((i - y_offset) * bitmap->pitch) + x_start);
for (FT_Int j = x1; j < x2; ++j, ++dst, ++src)
*dst |= *src;
}
} else if (bitmap->pixel_mode == FT_PIXEL_MODE_MONO) {
for (FT_Int i = y1; i < y2; ++i)
{
unsigned char* dst = _buffer + (i * image_width + x1);
unsigned char* src = bitmap->buffer + ((i - y_offset) * bitmap->pitch);
for (FT_Int j = x1; j < x2; ++j, ++dst) {
int x = (j - x1 + x_start);
int val = *(src + (x >> 3)) & (1 << (7 - (x & 0x7)));
*dst = val ? 255 : *dst;
}
}
} else {
throw Py::Exception("Unknown pixel mode");
}
_isDirty = true;
}
void
FT2Image::write_bitmap(const char* filename) const
{
FILE *fh = fopen(filename, "w");
for (size_t i = 0; i < _height; i++)
{
for (size_t j = 0; j < _width; ++j)
{
if (_buffer[j + i*_width])
{
fputc('#', fh);
}
else
{
fputc(' ', fh);
}
}
fputc('\n', fh);
}
fclose(fh);
}
char FT2Image::write_bitmap__doc__[] =
"write_bitmap(fname)\n"
"\n"
"Write the bitmap to file fname\n"
;
Py::Object
FT2Image::py_write_bitmap(const Py::Tuple & args)
{
_VERBOSE("FT2Image::write_bitmap");
args.verify_length(1);
std::string filename = Py::String(args[0]);
write_bitmap(filename.c_str());
return Py::Object();
}
void
FT2Image::draw_rect(unsigned long x0, unsigned long y0,
unsigned long x1, unsigned long y1)
{
if (x0 > _width || x1 > _width ||
y0 > _height || y1 > _height)
{
throw Py::ValueError("Rect coords outside image bounds");
}
size_t top = y0 * _width;
size_t bottom = y1 * _width;
for (size_t i = x0; i < x1 + 1; ++i)
{
_buffer[i + top] = 255;
_buffer[i + bottom] = 255;
}
for (size_t j = y0 + 1; j < y1; ++j)
{
_buffer[x0 + j*_width] = 255;
_buffer[x1 + j*_width] = 255;
}
_isDirty = true;
}
char FT2Image::draw_rect__doc__[] =
"draw_rect(x0, y0, x1, y1)\n"
"\n"
"Draw a rect to the image.\n"
"\n"
;
Py::Object
FT2Image::py_draw_rect(const Py::Tuple & args)
{
_VERBOSE("FT2Image::draw_rect");
args.verify_length(4);
long x0 = Py::Int(args[0]);
long y0 = Py::Int(args[1]);
long x1 = Py::Int(args[2]);
long y1 = Py::Int(args[3]);
draw_rect(x0, y0, x1, y1);
return Py::Object();
}
void
FT2Image::draw_rect_filled(unsigned long x0, unsigned long y0,
unsigned long x1, unsigned long y1)
{
x0 = std::min(x0, _width);
y0 = std::min(y0, _height);
x1 = std::min(x1, _width);
y1 = std::min(y1, _height);
for (size_t j = y0; j < y1 + 1; j++)
{
for (size_t i = x0; i < x1 + 1; i++)
{
_buffer[i + j*_width] = 255;
}
}
_isDirty = true;
}
char FT2Image::draw_rect_filled__doc__[] =
"draw_rect_filled(x0, y0, x1, y1)\n"
"\n"
"Draw a filled rect to the image.\n"
"\n"
;
Py::Object
FT2Image::py_draw_rect_filled(const Py::Tuple & args)
{
_VERBOSE("FT2Image::draw_rect_filled");
args.verify_length(4);
long x0 = Py::Int(args[0]);
long y0 = Py::Int(args[1]);
long x1 = Py::Int(args[2]);
long y1 = Py::Int(args[3]);
draw_rect_filled(x0, y0, x1, y1);
return Py::Object();
}
char FT2Image::as_str__doc__[] =
"width, height, s = image_as_str()\n"
"\n"
"Return the image buffer as a string\n"
"\n"
;
Py::Object
FT2Image::py_as_str(const Py::Tuple & args)
{
_VERBOSE("FT2Image::as_str");
args.verify_length(0);
return Py::asObject
(PyString_FromStringAndSize((const char *)_buffer,
_width*_height)
);
}
char FT2Image::as_array__doc__[] =
"x = image.as_array()\n"
"\n"
"Return the image buffer as a width x height numpy array of ubyte \n"
"\n"
;
Py::Object
FT2Image::py_as_array(const Py::Tuple & args)
{
_VERBOSE("FT2Image::as_array");
args.verify_length(0);
npy_intp dimensions[2];
dimensions[0] = get_height(); //numrows
dimensions[1] = get_width(); //numcols
PyArrayObject *A = (PyArrayObject *) PyArray_SimpleNewFromData(2, dimensions, PyArray_UBYTE, _buffer);
return Py::asObject((PyObject*)A);
}
void
FT2Image::makeRgbCopy()
{
if (!_isDirty)
{
return;
}
if (!_rgbCopy)
{
_rgbCopy = new FT2Image(_width * 3, _height);
}
else
{
_rgbCopy->resize(_width * 3, _height);
}
unsigned char *src = _buffer;
unsigned char *src_end = src + (_width * _height);
unsigned char *dst = _rgbCopy->_buffer;
unsigned char tmp;
while (src != src_end)
{
tmp = 255 - *src++;
*dst++ = tmp;
*dst++ = tmp;
*dst++ = tmp;
}
}
char FT2Image::as_rgb_str__doc__[] =
"width, height, s = image_as_rgb_str()\n"
"\n"
"Return the image buffer as a 24-bit RGB string.\n"
"\n"
;
Py::Object
FT2Image::py_as_rgb_str(const Py::Tuple & args)
{
_VERBOSE("FT2Image::as_str_rgb");
args.verify_length(0);
makeRgbCopy();
return _rgbCopy->py_as_str(args);
}
void FT2Image::makeRgbaCopy()
{
if (!_isDirty)
{
return;
}
if (!_rgbaCopy)
{
_rgbaCopy = new FT2Image(_width * 4, _height);
}
else
{
_rgbaCopy->resize(_width * 4, _height);
}
unsigned char *src = _buffer;
unsigned char *src_end = src + (_width * _height);
unsigned char *dst = _rgbaCopy->_buffer;
while (src != src_end)
{
// We know the array has already been zero'ed out in
// the resize method, so we just skip over the r, g and b.
dst += 3;
*dst++ = *src++;
}
}
char FT2Image::as_rgba_str__doc__[] =
"width, height, s = image_as_rgb_str()\n"
"\n"
"Return the image buffer as a 32-bit RGBA string.\n"
"\n"
;
Py::Object
FT2Image::py_as_rgba_str(const Py::Tuple & args)
{
_VERBOSE("FT2Image::as_str_rgba");
args.verify_length(0);
makeRgbaCopy();
return _rgbaCopy->py_as_str(args);
}
Py::Object
FT2Image::py_get_width(const Py::Tuple & args)
{
_VERBOSE("FT2Image::get_width");
args.verify_length(0);
return Py::Int((long)get_width());
}
Py::Object
FT2Image::py_get_height(const Py::Tuple & args)
{
_VERBOSE("FT2Image::get_height");
args.verify_length(0);
return Py::Int((long)get_height());
}
Glyph::Glyph(const FT_Face& face, const FT_Glyph& glyph, size_t ind) :
glyphInd(ind)
{
_VERBOSE("Glyph::Glyph");
FT_BBox bbox;
FT_Glyph_Get_CBox(glyph, ft_glyph_bbox_subpixels, &bbox);
setattr("width", Py::Int(face->glyph->metrics.width / HORIZ_HINTING));
setattr("height", Py::Int(face->glyph->metrics.height));
setattr("horiBearingX", Py::Int(face->glyph->metrics.horiBearingX / HORIZ_HINTING));
setattr("horiBearingY", Py::Int(face->glyph->metrics.horiBearingY));
setattr("horiAdvance", Py::Int(face->glyph->metrics.horiAdvance));
setattr("linearHoriAdvance", Py::Int(face->glyph->linearHoriAdvance / HORIZ_HINTING));
setattr("vertBearingX", Py::Int(face->glyph->metrics.vertBearingX));
setattr("vertBearingY", Py::Int(face->glyph->metrics.vertBearingY));
setattr("vertAdvance", Py::Int(face->glyph->metrics.vertAdvance));
//setattr("bitmap_left", Py::Int( face->glyph->bitmap_left) );
//setattr("bitmap_top", Py::Int( face->glyph->bitmap_top) );
Py::Tuple abbox(4);
abbox[0] = Py::Int(bbox.xMin);
abbox[1] = Py::Int(bbox.yMin);
abbox[2] = Py::Int(bbox.xMax);
abbox[3] = Py::Int(bbox.yMax);
setattr("bbox", abbox);
}
Glyph::~Glyph()
{
_VERBOSE("Glyph::~Glyph");
}
int
Glyph::setattr(const char *name, const Py::Object &value)
{
_VERBOSE("Glyph::setattr");
__dict__[name] = value;
return 0;
}
Py::Object
Glyph::getattr(const char *name)
{
_VERBOSE("Glyph::getattr");
if (__dict__.hasKey(name)) return __dict__[name];
else return getattr_default(name);
}
inline double conv(int v)
{
return double(v) / 64.0;
}
//see http://freetype.sourceforge.net/freetype2/docs/glyphs/glyphs-6.html
Py::Object
FT2Font::get_path()
{
//get the glyph as a path, a list of (COMMAND, *args) as desribed in matplotlib.path
// this code is from agg's decompose_ft_outline with minor modifications
if (!face->glyph) {
throw Py::ValueError("No glyph loaded");
}
enum {STOP = 0,
MOVETO = 1,
LINETO = 2,
CURVE3 = 3,
CURVE4 = 4,
ENDPOLY = 0x4f};
FT_Outline& outline = face->glyph->outline;
bool flip_y = false; //todo, pass me as kwarg
FT_Vector v_last;
FT_Vector v_control;
FT_Vector v_start;
FT_Vector* point;
FT_Vector* limit;
char* tags;
int n; // index of contour in outline
int first; // index of first point in contour
char tag; // current point's state
int count;
count = 0;
first = 0;
for (n = 0; n < outline.n_contours; n++)
{
int last; // index of last point in contour
bool starts_with_last;
last = outline.contours[n];
limit = outline.points + last;
v_start = outline.points[first];
v_last = outline.points[last];
v_control = v_start;
point = outline.points + first;
tags = outline.tags + first;
tag = FT_CURVE_TAG(tags[0]);
// A contour cannot start with a cubic control point!
if (tag == FT_CURVE_TAG_CUBIC)
{
throw Py::RuntimeError("A contour cannot start with a cubic control point");
}
else if (tag == FT_CURVE_TAG_CONIC)
{
starts_with_last = true;
} else {
starts_with_last = false;
}
count++;
while (point < limit)
{
if (!starts_with_last) {
point++;
tags++;
}
starts_with_last = false;
tag = FT_CURVE_TAG(tags[0]);
switch (tag)
{
case FT_CURVE_TAG_ON: // emit a single line_to
{
count++;
continue;
}
case FT_CURVE_TAG_CONIC: // consume conic arcs
{
Count_Do_Conic:
if (point < limit)
{
point++;
tags++;
tag = FT_CURVE_TAG(tags[0]);
if (tag == FT_CURVE_TAG_ON)
{
count += 2;
continue;
}
if (tag != FT_CURVE_TAG_CONIC)
{
throw Py::RuntimeError("Invalid font");
}
count += 2;
goto Count_Do_Conic;
}
count += 2;
goto Count_Close;
}
default: // FT_CURVE_TAG_CUBIC
{
if (point + 1 > limit || FT_CURVE_TAG(tags[1]) != FT_CURVE_TAG_CUBIC)
{
throw Py::RuntimeError("Invalid font");
}
point += 2;
tags += 2;
if (point <= limit)
{
count += 3;
continue;
}
count += 3;
goto Count_Close;
}
}
}
count++;
Count_Close:
first = last + 1;
}
PyArrayObject* vertices = NULL;
PyArrayObject* codes = NULL;
Py::Tuple result(2);
npy_intp vertices_dims[2] = {count, 2};
vertices = (PyArrayObject*)PyArray_SimpleNew(
2, vertices_dims, PyArray_DOUBLE);
if (vertices == NULL) {
throw;
}
npy_intp codes_dims[1] = {count};
codes = (PyArrayObject*)PyArray_SimpleNew(
1, codes_dims, PyArray_UINT8);
if (codes == NULL) {
throw;
}
result[0] = Py::Object((PyObject*)vertices, true);
result[1] = Py::Object((PyObject*)codes, true);
double* outpoints = (double *)PyArray_DATA(vertices);
unsigned char* outcodes = (unsigned char *)PyArray_DATA(codes);
first = 0;
for (n = 0; n < outline.n_contours; n++)
{
int last; // index of last point in contour
bool starts_with_last;
last = outline.contours[n];
limit = outline.points + last;
v_start = outline.points[first];
v_last = outline.points[last];
v_control = v_start;
point = outline.points + first;
tags = outline.tags + first;
tag = FT_CURVE_TAG(tags[0]);
double x, y;
if (tag != FT_CURVE_TAG_ON)
{
x = conv(v_last.x);
y = flip_y ? -conv(v_last.y) : conv(v_last.y);
starts_with_last = true;
} else {
x = conv(v_start.x);
y = flip_y ? -conv(v_start.y) : conv(v_start.y);
starts_with_last = false;
}
*(outpoints++) = x;
*(outpoints++) = y;
*(outcodes++) = MOVETO;
while (point < limit)
{
if (!starts_with_last) {
point++;
tags++;
}
starts_with_last = false;
tag = FT_CURVE_TAG(tags[0]);
switch (tag)
{
case FT_CURVE_TAG_ON: // emit a single line_to
{
double x = conv(point->x);
double y = flip_y ? -conv(point->y) : conv(point->y);
*(outpoints++) = x;
*(outpoints++) = y;
*(outcodes++) = LINETO;
continue;
}
case FT_CURVE_TAG_CONIC: // consume conic arcs
{
v_control.x = point->x;
v_control.y = point->y;
Do_Conic:
if (point < limit)
{
FT_Vector vec;
FT_Vector v_middle;
point++;
tags++;
tag = FT_CURVE_TAG(tags[0]);
vec.x = point->x;
vec.y = point->y;
if (tag == FT_CURVE_TAG_ON)
{
double xctl = conv(v_control.x);
double yctl = flip_y ? -conv(v_control.y) : conv(v_control.y);
double xto = conv(vec.x);
double yto = flip_y ? -conv(vec.y) : conv(vec.y);
*(outpoints++) = xctl;
*(outpoints++) = yctl;
*(outpoints++) = xto;
*(outpoints++) = yto;
*(outcodes++) = CURVE3;
*(outcodes++) = CURVE3;
continue;
}
v_middle.x = (v_control.x + vec.x) / 2;
v_middle.y = (v_control.y + vec.y) / 2;
double xctl = conv(v_control.x);
double yctl = flip_y ? -conv(v_control.y) : conv(v_control.y);
double xto = conv(v_middle.x);
double yto = flip_y ? -conv(v_middle.y) : conv(v_middle.y);
*(outpoints++) = xctl;
*(outpoints++) = yctl;
*(outpoints++) = xto;
*(outpoints++) = yto;
*(outcodes++) = CURVE3;
*(outcodes++) = CURVE3;
v_control = vec;
goto Do_Conic;
}
double xctl = conv(v_control.x);
double yctl = flip_y ? -conv(v_control.y) : conv(v_control.y);
double xto = conv(v_start.x);
double yto = flip_y ? -conv(v_start.y) : conv(v_start.y);
*(outpoints++) = xctl;
*(outpoints++) = yctl;
*(outpoints++) = xto;
*(outpoints++) = yto;
*(outcodes++) = CURVE3;
*(outcodes++) = CURVE3;
goto Close;
}
default: // FT_CURVE_TAG_CUBIC
{
FT_Vector vec1, vec2;
vec1.x = point[0].x;
vec1.y = point[0].y;
vec2.x = point[1].x;
vec2.y = point[1].y;
point += 2;
tags += 2;
if (point <= limit)
{
FT_Vector vec;
vec.x = point->x;
vec.y = point->y;
double xctl1 = conv(vec1.x);
double yctl1 = flip_y ? -conv(vec1.y) : conv(vec1.y);
double xctl2 = conv(vec2.x);
double yctl2 = flip_y ? -conv(vec2.y) : conv(vec2.y);
double xto = conv(vec.x);
double yto = flip_y ? -conv(vec.y) : conv(vec.y);
(*outpoints++) = xctl1;
(*outpoints++) = yctl1;
(*outpoints++) = xctl2;
(*outpoints++) = yctl2;
(*outpoints++) = xto;
(*outpoints++) = yto;
(*outcodes++) = CURVE4;
(*outcodes++) = CURVE4;
(*outcodes++) = CURVE4;
continue;
}
double xctl1 = conv(vec1.x);
double yctl1 = flip_y ? -conv(vec1.y) : conv(vec1.y);
double xctl2 = conv(vec2.x);
double yctl2 = flip_y ? -conv(vec2.y) : conv(vec2.y);
double xto = conv(v_start.x);
double yto = flip_y ? -conv(v_start.y) : conv(v_start.y);
(*outpoints++) = xctl1;
(*outpoints++) = yctl1;
(*outpoints++) = xctl2;
(*outpoints++) = yctl2;
(*outpoints++) = xto;
(*outpoints++) = yto;
(*outcodes++) = CURVE4;
(*outcodes++) = CURVE4;
(*outcodes++) = CURVE4;
goto Close;
}
}
}
(*outpoints++) = 0.0;
(*outpoints++) = 0.0;
(*outcodes++) = ENDPOLY;
Close:
first = last + 1;
}
if (outcodes - (unsigned char *)PyArray_DATA(codes) != count) {
throw Py::RuntimeError("Font path size doesn't match");
}
return result;
}
FT2Font::FT2Font(std::string facefile) :
image(NULL)
{
_VERBOSE(Printf("FT2Font::FT2Font %s", facefile.c_str()).str());
clear(Py::Tuple(0));
int error = FT_New_Face(_ft2Library, facefile.c_str(), 0, &face);
if (error == FT_Err_Unknown_File_Format)
{
std::ostringstream s;
s << "Could not load facefile " << facefile << "; Unknown_File_Format" << std::endl;
throw Py::RuntimeError(s.str());
}
else if (error == FT_Err_Cannot_Open_Resource)
{
std::ostringstream s;
s << "Could not open facefile " << facefile << "; Cannot_Open_Resource" << std::endl;
throw Py::RuntimeError(s.str());
}
else if (error == FT_Err_Invalid_File_Format)
{
std::ostringstream s;
s << "Could not open facefile " << facefile << "; Invalid_File_Format" << std::endl;
throw Py::RuntimeError(s.str());
}
else if (error)
{
std::ostringstream s;
s << "Could not open facefile " << facefile << "; freetype error code " << error << std::endl;
throw Py::RuntimeError(s.str());
}
// set a default fontsize 12 pt at 72dpi
#ifdef VERTICAL_HINTING
error = FT_Set_Char_Size(face, 12 * 64, 0, 72 * HORIZ_HINTING, 72);
static FT_Matrix transform = { 65536 / HORIZ_HINTING, 0, 0, 65536 };
FT_Set_Transform(face, &transform, 0);
#else
error = FT_Set_Char_Size(face, 12 * 64, 0, 72, 72);
#endif
//error = FT_Set_Char_Size( face, 20 * 64, 0, 80, 80 );
if (error)
{
std::ostringstream s;
s << "Could not set the fontsize for facefile " << facefile << std::endl;
throw Py::RuntimeError(s.str());
}
// set some face props as attributes
//small memory leak fixed after 2.1.8
//fields can be null so we have to check this first
const char* ps_name = FT_Get_Postscript_Name(face);
if (ps_name == NULL)
{
ps_name = "UNAVAILABLE";
}
const char* family_name = face->family_name;
if (family_name == NULL)
{
family_name = "UNAVAILABLE";
}
const char* style_name = face->style_name;
if (style_name == NULL)
{
style_name = "UNAVAILABLE";
}
setattr("postscript_name", Py::String(ps_name));
setattr("num_faces", Py::Int(face->num_faces));
setattr("family_name", Py::String(family_name));
setattr("style_name", Py::String(style_name));
setattr("face_flags", Py::Int(face->face_flags));
setattr("style_flags", Py::Int(face->style_flags));
setattr("num_glyphs", Py::Int(face->num_glyphs));
setattr("num_fixed_sizes", Py::Int(face->num_fixed_sizes));
setattr("num_charmaps", Py::Int(face->num_charmaps));
int scalable = FT_IS_SCALABLE(face);
setattr("scalable", Py::Int(scalable));
if (scalable)
{
setattr("units_per_EM", Py::Int(face->units_per_EM));
Py::Tuple bbox(4);
bbox[0] = Py::Int(face->bbox.xMin);
bbox[1] = Py::Int(face->bbox.yMin);
bbox[2] = Py::Int(face->bbox.xMax);
bbox[3] = Py::Int(face->bbox.yMax);
setattr("bbox", bbox);
setattr("ascender", Py::Int(face->ascender));
setattr("descender", Py::Int(face->descender));
setattr("height", Py::Int(face->height));
setattr("max_advance_width", Py::Int(face->max_advance_width));
setattr("max_advance_height", Py::Int(face->max_advance_height));
setattr("underline_position", Py::Int(face->underline_position));
setattr("underline_thickness", Py::Int(face->underline_thickness));
}
setattr("fname", Py::String(facefile));
_VERBOSE("FT2Font::FT2Font done");
}
FT2Font::~FT2Font()
{
_VERBOSE("FT2Font::~FT2Font");
Py_XDECREF(image);
FT_Done_Face(face);
for (size_t i = 0; i < glyphs.size(); i++)
{
FT_Done_Glyph(glyphs[i]);
}
}
int
FT2Font::setattr(const char *name, const Py::Object &value)
{
_VERBOSE("FT2Font::setattr");
__dict__[name] = value;
return 1;
}
Py::Object
FT2Font::getattr(const char *name)
{
_VERBOSE("FT2Font::getattr");
if (__dict__.hasKey(name)) return __dict__[name];
else return getattr_default(name);
}
char FT2Font::clear__doc__[] =
"clear()\n"
"\n"
"Clear all the glyphs, reset for a new set_text"
;
Py::Object
FT2Font::clear(const Py::Tuple & args)
{
_VERBOSE("FT2Font::clear");
args.verify_length(0);
Py_XDECREF(image);
image = NULL;
angle = 0.0;
pen.x = 0;
pen.y = 0;
for (size_t i = 0; i < glyphs.size(); i++)
{
FT_Done_Glyph(glyphs[i]);
}
glyphs.clear();
return Py::Object();
}
char FT2Font::set_size__doc__[] =
"set_size(ptsize, dpi)\n"
"\n"
"Set the point size and dpi of the text.\n"
;
Py::Object
FT2Font::set_size(const Py::Tuple & args)
{
_VERBOSE("FT2Font::set_size");
args.verify_length(2);
double ptsize = Py::Float(args[0]);
double dpi = Py::Float(args[1]);
#ifdef VERTICAL_HINTING
int error = FT_Set_Char_Size(face, (long)(ptsize * 64), 0,
(unsigned int)dpi * HORIZ_HINTING,
(unsigned int)dpi);
static FT_Matrix transform = { 65536 / HORIZ_HINTING, 0, 0, 65536 };
FT_Set_Transform(face, &transform, 0);
#else
int error = FT_Set_Char_Size(face, (long)(ptsize * 64), 0,
(unsigned int)dpi,
(unsigned int)dpi);
#endif
if (error)
{
throw Py::RuntimeError("Could not set the fontsize");
}
return Py::Object();
}
char FT2Font::set_charmap__doc__[] =
"set_charmap(i)\n"
"\n"
"Make the i-th charmap current\n"
;
Py::Object
FT2Font::set_charmap(const Py::Tuple & args)
{
_VERBOSE("FT2Font::set_charmap");
args.verify_length(1);
int i = Py::Int(args[0]);
if (i >= face->num_charmaps)
{
throw Py::ValueError("i exceeds the available number of char maps");
}
FT_CharMap charmap = face->charmaps[i];
if (FT_Set_Charmap(face, charmap))
{
throw Py::ValueError("Could not set the charmap");
}
return Py::Object();
}
char FT2Font::select_charmap__doc__[] =
"select_charmap(i)\n"
"\n"
"select charmap i where i is one of the FT_Encoding number\n"
;
Py::Object
FT2Font::select_charmap(const Py::Tuple & args)
{
_VERBOSE("FT2Font::set_charmap");
args.verify_length(1);
unsigned long i = Py::Long(args[0]);
//if (FT_Select_Charmap( face, FT_ENCODING_ADOBE_CUSTOM ))
if (FT_Select_Charmap(face, (FT_Encoding) i))
{
throw Py::ValueError("Could not set the charmap");
}
return Py::Object();
}
FT_BBox
FT2Font::compute_string_bbox()
{
_VERBOSE("FT2Font::compute_string_bbox");
FT_BBox bbox;
/* initialize string bbox to "empty" values */
bbox.xMin = bbox.yMin = 32000;
bbox.xMax = bbox.yMax = -32000;
int right_side = 0;
for (size_t n = 0; n < glyphs.size(); n++)
{
FT_BBox glyph_bbox;
FT_Glyph_Get_CBox(glyphs[n], ft_glyph_bbox_subpixels, &glyph_bbox);
if (glyph_bbox.xMin < bbox.xMin) bbox.xMin = glyph_bbox.xMin;
if (glyph_bbox.yMin < bbox.yMin) bbox.yMin = glyph_bbox.yMin;
if (glyph_bbox.xMin == glyph_bbox.xMax)
{
right_side += glyphs[n]->advance.x >> 10;
if (right_side > bbox.xMax) bbox.xMax = right_side;
}
else
{
if (glyph_bbox.xMax > bbox.xMax) bbox.xMax = glyph_bbox.xMax;
}
if (glyph_bbox.yMax > bbox.yMax) bbox.yMax = glyph_bbox.yMax;
}
/* check that we really grew the string bbox */
if (bbox.xMin > bbox.xMax)
{
bbox.xMin = 0;
bbox.yMin = 0;
bbox.xMax = 0;
bbox.yMax = 0;
}
return bbox;
}
char FT2Font::get_kerning__doc__[] =
"dx = get_kerning(left, right, mode)\n"
"\n"
"Get the kerning between left char and right glyph indices\n"
"mode is a kerning mode constant\n"
" KERNING_DEFAULT - Return scaled and grid-fitted kerning distances\n"
" KERNING_UNFITTED - Return scaled but un-grid-fitted kerning distances\n"
" KERNING_UNSCALED - Return the kerning vector in original font units\n"
;
Py::Object
FT2Font::get_kerning(const Py::Tuple & args)
{
_VERBOSE("FT2Font::get_kerning");
args.verify_length(3);
int left = Py::Int(args[0]);
int right = Py::Int(args[1]);
int mode = Py::Int(args[2]);
if (!FT_HAS_KERNING(face))
{
return Py::Int(0);
}
FT_Vector delta;
if (!FT_Get_Kerning(face, left, right, mode, &delta))
{
return Py::Int(delta.x / HORIZ_HINTING);
}
else
{
return Py::Int(0);
}
}
char FT2Font::set_text__doc__[] =
"set_text(s, angle)\n"
"\n"
"Set the text string and angle.\n"
"You must call this before draw_glyphs_to_bitmap\n"
"A sequence of x,y positions is returned";
Py::Object
FT2Font::set_text(const Py::Tuple & args, const Py::Dict & kwargs)
{
_VERBOSE("FT2Font::set_text");
args.verify_length(2);
Py::String text(args[0]);
std::string stdtext = "";
Py_UNICODE* pcode = NULL;
size_t N = 0;
if (PyUnicode_Check(text.ptr()))
{
pcode = PyUnicode_AsUnicode(text.ptr());
N = PyUnicode_GetSize(text.ptr());
}
else
{
stdtext = text.as_std_string();
N = stdtext.size();
}
angle = Py::Float(args[1]);
angle = angle / 360.0 * 2 * 3.14159;
long flags = FT_LOAD_FORCE_AUTOHINT;
if (kwargs.hasKey("flags"))
{
flags = Py::Long(kwargs["flags"]);
}
//this computes width and height in subpixels so we have to divide by 64
matrix.xx = (FT_Fixed)(cos(angle) * 0x10000L);
matrix.xy = (FT_Fixed)(-sin(angle) * 0x10000L);
matrix.yx = (FT_Fixed)(sin(angle) * 0x10000L);
matrix.yy = (FT_Fixed)(cos(angle) * 0x10000L);
FT_Bool use_kerning = FT_HAS_KERNING(face);
FT_UInt previous = 0;
glyphs.resize(0);
pen.x = 0;
pen.y = 0;
Py::Tuple xys(N);
for (unsigned int n = 0; n < N; n++)
{
std::string thischar("?");
FT_UInt glyph_index;
if (pcode == NULL)
{
// plain ol string
thischar = stdtext[n];
glyph_index = FT_Get_Char_Index(face, stdtext[n]);
}
else
{
//unicode
glyph_index = FT_Get_Char_Index(face, pcode[n]);
}
// retrieve kerning distance and move pen position
if (use_kerning && previous && glyph_index)
{
FT_Vector delta;
FT_Get_Kerning(face, previous, glyph_index,
FT_KERNING_DEFAULT, &delta);
pen.x += delta.x / HORIZ_HINTING;
}
error = FT_Load_Glyph(face, glyph_index, flags);
if (error)
{
std::cerr << "\tcould not load glyph for " << thischar << std::endl;
continue;
}
// ignore errors, jump to next glyph
// extract glyph image and store it in our table
FT_Glyph thisGlyph;
error = FT_Get_Glyph(face->glyph, &thisGlyph);
if (error)
{
std::cerr << "\tcould not get glyph for " << thischar << std::endl;
continue;
}
// ignore errors, jump to next glyph
FT_Glyph_Transform(thisGlyph, 0, &pen);
Py::Tuple xy(2);
xy[0] = Py::Float(pen.x);
xy[1] = Py::Float(pen.y);
xys[n] = xy;
pen.x += face->glyph->advance.x;
previous = glyph_index;
glyphs.push_back(thisGlyph);
}
// now apply the rotation
for (unsigned int n = 0; n < glyphs.size(); n++)
{
FT_Glyph_Transform(glyphs[n], &matrix, 0);
}
_VERBOSE("FT2Font::set_text done");
return xys;
}
char FT2Font::get_num_glyphs__doc__[] =
"get_num_glyphs()\n"
"\n"
"Return the number of loaded glyphs\n"
;
Py::Object
FT2Font::get_num_glyphs(const Py::Tuple & args)
{
_VERBOSE("FT2Font::get_num_glyphs");
args.verify_length(0);
return Py::Int((long)glyphs.size());
}
char FT2Font::load_char__doc__[] =
"load_char(charcode, flags=LOAD_FORCE_AUTOHINT)\n"
"\n"
"Load character with charcode in current fontfile and set glyph.\n"
"The flags argument can be a bitwise-or of the LOAD_XXX constants.\n"
"Return value is a Glyph object, with attributes\n"
" width # glyph width\n"
" height # glyph height\n"
" bbox # the glyph bbox (xmin, ymin, xmax, ymax)\n"
" horiBearingX # left side bearing in horizontal layouts\n"
" horiBearingY # top side bearing in horizontal layouts\n"
" horiAdvance # advance width for horizontal layout\n"
" vertBearingX # left side bearing in vertical layouts\n"
" vertBearingY # top side bearing in vertical layouts\n"
" vertAdvance # advance height for vertical layout\n"
;
Py::Object
FT2Font::load_char(const Py::Tuple & args, const Py::Dict & kwargs)
{
_VERBOSE("FT2Font::load_char");
//load a char using the unsigned long charcode
args.verify_length(1);
long charcode = Py::Long(args[0]), flags = Py::Long(FT_LOAD_FORCE_AUTOHINT);
if (kwargs.hasKey("flags"))
{
flags = Py::Long(kwargs["flags"]);
}
int error = FT_Load_Char(face, (unsigned long)charcode, flags);
if (error)
{
throw Py::RuntimeError(Printf("Could not load charcode %d", charcode).str());
}
FT_Glyph thisGlyph;
error = FT_Get_Glyph(face->glyph, &thisGlyph);
if (error)
{
throw Py::RuntimeError(Printf("Could not get glyph for char %d", charcode).str());
}
size_t num = glyphs.size(); //the index into the glyphs list
glyphs.push_back(thisGlyph);
Glyph* gm = new Glyph(face, thisGlyph, num);
return Py::asObject(gm);
}
char FT2Font::load_glyph__doc__[] =
"load_glyph(glyphindex, flags=LOAD_FORCE_AUTOHINT)\n"
"\n"
"Load character with glyphindex in current fontfile and set glyph.\n"
"The flags argument can be a bitwise-or of the LOAD_XXX constants.\n"
"Return value is a Glyph object, with attributes\n"
" width # glyph width\n"
" height # glyph height\n"
" bbox # the glyph bbox (xmin, ymin, xmax, ymax)\n"
" horiBearingX # left side bearing in horizontal layouts\n"
" horiBearingY # top side bearing in horizontal layouts\n"
" horiAdvance # advance width for horizontal layout\n"
" vertBearingX # left side bearing in vertical layouts\n"
" vertBearingY # top side bearing in vertical layouts\n"
" vertAdvance # advance height for vertical layout\n"
;
Py::Object
FT2Font::load_glyph(const Py::Tuple & args, const Py::Dict & kwargs)
{
_VERBOSE("FT2Font::load_glyph");
//load a char using the unsigned long charcode
args.verify_length(1);
long glyph_index = Py::Long(args[0]), flags = Py::Long(FT_LOAD_FORCE_AUTOHINT);
if (kwargs.hasKey("flags"))
{
flags = Py::Long(kwargs["flags"]);
}
int error = FT_Load_Glyph(face, glyph_index, flags);
if (error)
{
throw Py::RuntimeError(Printf("Could not load glyph index %d", glyph_index).str());
}
FT_Glyph thisGlyph;
error = FT_Get_Glyph(face->glyph, &thisGlyph);
if (error)
{
throw Py::RuntimeError(Printf("Could not get glyph for glyph index %d", glyph_index).str());
}
size_t num = glyphs.size(); //the index into the glyphs list
glyphs.push_back(thisGlyph);
Glyph* gm = new Glyph(face, thisGlyph, num);
return Py::asObject(gm);
}
char FT2Font::get_width_height__doc__[] =
"w, h = get_width_height()\n"
"\n"
"Get the width and height in 26.6 subpixels of the current string set by set_text\n"
"The rotation of the string is accounted for. To get width and height\n"
"in pixels, divide these values by 64\n"
;
Py::Object
FT2Font::get_width_height(const Py::Tuple & args)
{
_VERBOSE("FT2Font::get_width_height");
args.verify_length(0);
FT_BBox bbox = compute_string_bbox();
Py::Tuple ret(2);
ret[0] = Py::Int(bbox.xMax - bbox.xMin);
ret[1] = Py::Int(bbox.yMax - bbox.yMin);
return ret;
}
char FT2Font::get_descent__doc__[] =
"d = get_descent()\n"
"\n"
"Get the descent of the current string set by set_text in 26.6 subpixels.\n"
"The rotation of the string is accounted for. To get the descent\n"
"in pixels, divide this value by 64.\n"
;
Py::Object
FT2Font::get_descent(const Py::Tuple & args)
{
_VERBOSE("FT2Font::get_descent");
args.verify_length(0);
FT_BBox bbox = compute_string_bbox();
return Py::Int(- bbox.yMin);;
}
char FT2Font::draw_glyphs_to_bitmap__doc__[] =
"draw_glyphs_to_bitmap()\n"
"\n"
"Draw the glyphs that were loaded by set_text to the bitmap\n"
"The bitmap size will be automatically set to include the glyphs\n"
;
Py::Object
FT2Font::draw_glyphs_to_bitmap(const Py::Tuple &args, const Py::Dict &kwargs)
{
_VERBOSE("FT2Font::draw_glyphs_to_bitmap");
args.verify_length(0);
long antialiased = 1;
if (kwargs.hasKey("antialiased"))
{
antialiased = Py::Long(kwargs["antialiased"]);
}
FT_BBox string_bbox = compute_string_bbox();
size_t width = (string_bbox.xMax - string_bbox.xMin) / 64 + 2;
size_t height = (string_bbox.yMax - string_bbox.yMin) / 64 + 2;
Py_XDECREF(image);
image = NULL;
image = new FT2Image(width, height);
for (size_t n = 0; n < glyphs.size(); n++)
{
FT_BBox bbox;
FT_Glyph_Get_CBox(glyphs[n], ft_glyph_bbox_pixels, &bbox);
error = FT_Glyph_To_Bitmap(
&glyphs[n],
antialiased ? FT_RENDER_MODE_NORMAL : FT_RENDER_MODE_MONO,
0,
1);
if (error)
{
throw Py::RuntimeError("Could not convert glyph to bitmap");
}
FT_BitmapGlyph bitmap = (FT_BitmapGlyph)glyphs[n];
// now, draw to our target surface (convert position)
//bitmap left and top in pixel, string bbox in subpixel
FT_Int x = (FT_Int)(bitmap->left - (string_bbox.xMin / 64.));
FT_Int y = (FT_Int)((string_bbox.yMax / 64.) - bitmap->top + 1);
image->draw_bitmap(&bitmap->bitmap, x, y);
}
return Py::Object();
}
char FT2Font::get_xys__doc__[] =
"get_xys()\n"
"\n"
"Get the xy locations of the current glyphs\n"
;
Py::Object
FT2Font::get_xys(const Py::Tuple &args, const Py::Dict &kwargs)
{
_VERBOSE("FT2Font::get_xys");
args.verify_length(0);
long antialiased = 1;
if (kwargs.hasKey("antialiased"))
{
antialiased = Py::Long(kwargs["antialiased"]);
}
FT_BBox string_bbox = compute_string_bbox();
Py::Tuple xys(glyphs.size());
for (size_t n = 0; n < glyphs.size(); n++)
{
FT_BBox bbox;
FT_Glyph_Get_CBox(glyphs[n], ft_glyph_bbox_pixels, &bbox);
error = FT_Glyph_To_Bitmap(
&glyphs[n],
antialiased ? FT_RENDER_MODE_NORMAL : FT_RENDER_MODE_MONO,
0,
1);
if (error)
{
throw Py::RuntimeError("Could not convert glyph to bitmap");
}
FT_BitmapGlyph bitmap = (FT_BitmapGlyph)glyphs[n];
//bitmap left and top in pixel, string bbox in subpixel
FT_Int x = (FT_Int)(bitmap->left - string_bbox.xMin / 64.);
FT_Int y = (FT_Int)(string_bbox.yMax / 64. - bitmap->top + 1);
//make sure the index is non-neg
x = x < 0 ? 0 : x;
y = y < 0 ? 0 : y;
Py::Tuple xy(2);
xy[0] = Py::Float(x);
xy[1] = Py::Float(y);
xys[n] = xy;
}
return xys;
}
char FT2Font::draw_glyph_to_bitmap__doc__[] =
"draw_glyph_to_bitmap(bitmap, x, y, glyph)\n"
"\n"
"Draw a single glyph to the bitmap at pixel locations x,y\n"
"Note it is your responsibility to set up the bitmap manually\n"
"with set_bitmap_size(w,h) before this call is made.\n"
"\n"
"If you want automatic layout, use set_text in combinations with\n"
"draw_glyphs_to_bitmap. This function is intended for people who\n"
"want to render individual glyphs at precise locations, eg, a\n"
"a glyph returned by load_char\n";
Py::Object
FT2Font::draw_glyph_to_bitmap(const Py::Tuple &args, const Py::Dict &kwargs)
{
_VERBOSE("FT2Font::draw_glyph_to_bitmap");
args.verify_length(4);
if (!FT2Image::check(args[0].ptr()))
{
throw Py::TypeError("Usage: draw_glyph_to_bitmap(bitmap, x,y,glyph)");
}
FT2Image* im = static_cast<FT2Image*>(args[0].ptr());
double xd = Py::Float(args[1]);
double yd = Py::Float(args[2]);
long x = (long)xd;
long y = (long)yd;
FT_Vector sub_offset;
sub_offset.x = 0; // int((xd - (double)x) * 64.0);
sub_offset.y = 0; // int((yd - (double)y) * 64.0);
if (!Glyph::check(args[3].ptr()))
{
throw Py::TypeError("Usage: draw_glyph_to_bitmap(bitmap, x,y,glyph)");
}
Glyph* glyph = static_cast<Glyph*>(args[3].ptr());
long antialiased = 1;
if (kwargs.hasKey("antialiased"))
{
antialiased = Py::Long(kwargs["antialiased"]);
}
if ((size_t)glyph->glyphInd >= glyphs.size())
{
throw Py::ValueError("glyph num is out of range");
}
error = FT_Glyph_To_Bitmap(
&glyphs[glyph->glyphInd],
antialiased ? FT_RENDER_MODE_NORMAL : FT_RENDER_MODE_MONO,
&sub_offset, // additional translation
1 //destroy image
);
if (error)
{
throw Py::RuntimeError("Could not convert glyph to bitmap");
}
FT_BitmapGlyph bitmap = (FT_BitmapGlyph)glyphs[glyph->glyphInd];
im->draw_bitmap(&bitmap->bitmap, x + bitmap->left, y);
return Py::Object();
}
char FT2Font::get_glyph_name__doc__[] =
"get_glyph_name(index)\n"
"\n"
"Retrieves the ASCII name of a given glyph in a face.\n"
;
Py::Object
FT2Font::get_glyph_name(const Py::Tuple & args)
{
_VERBOSE("FT2Font::get_glyph_name");
args.verify_length(1);
if (!FT_HAS_GLYPH_NAMES(face))
{
throw Py::RuntimeError("Face has no glyph names");
}
char buffer[128];
if (FT_Get_Glyph_Name(face, (FT_UInt) Py::Int(args[0]), buffer, 128))
{
throw Py::RuntimeError("Could not get glyph names.");
}
return Py::String(buffer);
}
char FT2Font::get_charmap__doc__[] =
"get_charmap()\n"
"\n"
"Returns a dictionary that maps the character codes of the selected charmap\n"
"(Unicode by default) to their corresponding glyph indices.\n"
;
Py::Object
FT2Font::get_charmap(const Py::Tuple & args)
{
_VERBOSE("FT2Font::get_charmap");
args.verify_length(0);
FT_UInt index;
Py::Dict charmap;
//std::cout << "asd" << face->charmaps[1]->encoding << std::endl;
FT_ULong code = FT_Get_First_Char(face, &index);
while (index != 0)
{
charmap[Py::Long((long) code)] = Py::Int((int) index);
code = FT_Get_Next_Char(face, code, &index);
}
return charmap;
}
// ID Platform Encoding
// 0 Unicode Reserved (set to 0)
// 1 Macintoch The Script Manager code
// 2 ISO ISO encoding
// 3 Microsoft Microsoft encoding
// 240-255 User-defined Reserved for all nonregistered platforms
// Code ISO encoding scheme
// 0 7-bit ASCII
// 1 ISO 10646
// 2 ISO 8859-1
// Code Language Code Language Code
// 0 English 10 Hebrew 20 Urdu
// 1 French 11 Japanese 21 Hindi
// 2 German 12 Arabic 22 Thai
// 3 Italian 13 Finnish
// 4 Dutch 14 Greek
// 5 Swedish 15 Icelandic
// 6 Spanish 16 Maltese
// 7 Danish 17 Turkish
// 8 Portuguese 18 Yugoslavian
// 9 Norwegian 19 Chinese
// Code Meaning Description
// 0 Copyright notice e.g. "Copyright Apple Computer, Inc. 1992
// 1 Font family name e.g. "New York"
// 2 Font style e.g. "Bold"
// 3 Font identification e.g. "Apple Computer New York Bold Ver 1"
// 4 Full font name e.g. "New York Bold"
// 5 Version string e.g. "August 10, 1991, 1.08d21"
// 6 Postscript name e.g. "Times-Bold"
// 7 Trademark
// 8 Designer e.g. "Apple Computer"
char FT2Font::get_sfnt__doc__[] =
"get_sfnt(name)\n"
"\n"
"Get all values from the SFNT names table. Result is a dictionary whose"
"key is the platform-ID, ISO-encoding-scheme, language-code, and"
"description.\n"
/*
"The font name identifier codes are:\n"
"\n"
" 0 Copyright notice e.g. Copyright Apple Computer, Inc. 1992\n"
" 1 Font family name e.g. New York\n"
" 2 Font style e.g. Bold\n"
" 3 Font identification e.g. Apple Computer New York Bold Ver 1\n"
" 4 Full font name e.g. New York Bold\n"
" 5 Version string e.g. August 10, 1991, 1.08d21\n"
" 6 Postscript name e.g. Times-Bold\n"
" 7 Trademark \n"
" 8 Designer e.g. Apple Computer\n"
" 11 URL e.g. http://www.apple.com\n"
" 13 Copyright license \n"
*/
;
Py::Object
FT2Font::get_sfnt(const Py::Tuple & args)
{
_VERBOSE("FT2Font::get_sfnt");
args.verify_length(0);
if (!(face->face_flags & FT_FACE_FLAG_SFNT))
{
throw Py::RuntimeError("No SFNT name table");
}
size_t count = FT_Get_Sfnt_Name_Count(face);
Py::Dict names;
for (size_t j = 0; j < count; j++)
{
FT_SfntName sfnt;
FT_Error error = FT_Get_Sfnt_Name(face, j, &sfnt);
if (error)
{
throw Py::RuntimeError("Could not get SFNT name");
}
Py::Tuple key(4);
key[0] = Py::Int(sfnt.platform_id);
key[1] = Py::Int(sfnt.encoding_id);
key[2] = Py::Int(sfnt.language_id);
key[3] = Py::Int(sfnt.name_id);
names[key] = Py::String((char *) sfnt.string,
(int) sfnt.string_len);
}
return names;
}
char FT2Font::get_name_index__doc__[] =
"get_name_index(name)\n"
"\n"
"Returns the glyph index of a given glyph name.\n"
"The glyph index 0 means `undefined character code'.\n"
;
Py::Object
FT2Font::get_name_index(const Py::Tuple & args)
{
_VERBOSE("FT2Font::get_name_index");
args.verify_length(1);
std::string glyphname = Py::String(args[0]);
return Py::Long((long)
FT_Get_Name_Index(face, (FT_String *) glyphname.c_str()));
}
char FT2Font::get_ps_font_info__doc__[] =
"get_ps_font_info()\n"
"\n"
"Return the information in the PS Font Info structure.\n"
;
Py::Object
FT2Font::get_ps_font_info(const Py::Tuple & args)
{
_VERBOSE("FT2Font::get_ps_font_info");
args.verify_length(0);
PS_FontInfoRec fontinfo;
FT_Error error = FT_Get_PS_Font_Info(face, &fontinfo);
if (error)
{
Py::RuntimeError("Could not get PS font info");
return Py::Object();
}
Py::Tuple info(9);
info[0] = Py::String(fontinfo.version ? fontinfo.version : "");
info[1] = Py::String(fontinfo.notice ? fontinfo.notice : "");
info[2] = Py::String(fontinfo.full_name ? fontinfo.full_name : "");
info[3] = Py::String(fontinfo.family_name ? fontinfo.family_name : "");
info[4] = Py::String(fontinfo.weight ? fontinfo.weight : "");
info[5] = Py::Long(fontinfo.italic_angle);
info[6] = Py::Int(fontinfo.is_fixed_pitch);
info[7] = Py::Int(fontinfo.underline_position);
info[8] = Py::Int(fontinfo.underline_thickness);
return info;
}
char FT2Font::get_sfnt_table__doc__[] =
"get_sfnt_table(name)\n"
"\n"
"Return one of the following SFNT tables: head, maxp, OS/2, hhea, "
"vhea, post, or pclt.\n"
;
Py::Object
FT2Font::get_sfnt_table(const Py::Tuple & args)
{
_VERBOSE("FT2Font::get_sfnt_table");
args.verify_length(1);
std::string tagname = Py::String(args[0]);
int tag;
const char *tags[] = {"head", "maxp", "OS/2", "hhea",
"vhea", "post", "pclt", NULL
};
for (tag = 0; tags[tag] != NULL; tag++)
{
if (strcmp(tagname.c_str(), tags[tag]) == 0)
{
break;
}
}
void *table = FT_Get_Sfnt_Table(face, (FT_Sfnt_Tag) tag);
if (!table)
{
return Py::Object();
}
switch (tag)
{
case 0:
{
char head_dict[] = "{s:(h,h), s:(h,h), s:l, s:l, s:i, s:i,"
"s:(l,l), s:(l,l), s:h, s:h, s:h, s:h, s:i, s:i, s:h, s:h, s:h}";
TT_Header *t = (TT_Header *)table;
return Py::asObject(Py_BuildValue(head_dict,
"version",
FIXED_MAJOR(t->Table_Version),
FIXED_MINOR(t->Table_Version),
"fontRevision",
FIXED_MAJOR(t->Font_Revision),
FIXED_MINOR(t->Font_Revision),
"checkSumAdjustment", t->CheckSum_Adjust,
"magicNumber" , t->Magic_Number,
"flags", (unsigned)t->Flags,
"unitsPerEm", (unsigned)t->Units_Per_EM,
"created", t->Created[0], t->Created[1],
"modified", t->Modified[0], t->Modified[1],
"xMin", t->xMin,
"yMin", t->yMin,
"xMax", t->xMax,
"yMax", t->yMax,
"macStyle", (unsigned)t->Mac_Style,
"lowestRecPPEM", (unsigned)t->Lowest_Rec_PPEM,
"fontDirectionHint", t->Font_Direction,
"indexToLocFormat", t->Index_To_Loc_Format,
"glyphDataFormat", t->Glyph_Data_Format));
}
case 1:
{
char maxp_dict[] = "{s:(h,h), s:i, s:i, s:i, s:i, s:i, s:i,"
"s:i, s:i, s:i, s:i, s:i, s:i, s:i, s:i}";
TT_MaxProfile *t = (TT_MaxProfile *)table;
return Py::asObject(Py_BuildValue(maxp_dict,
"version",
FIXED_MAJOR(t->version),
FIXED_MINOR(t->version),
"numGlyphs", (unsigned)t->numGlyphs,
"maxPoints", (unsigned)t->maxPoints,
"maxContours", (unsigned)t->maxContours,
"maxComponentPoints",
(unsigned)t->maxCompositePoints,
"maxComponentContours",
(unsigned)t->maxCompositeContours,
"maxZones", (unsigned)t->maxZones,
"maxTwilightPoints", (unsigned)t->maxTwilightPoints,
"maxStorage", (unsigned)t->maxStorage,
"maxFunctionDefs", (unsigned)t->maxFunctionDefs,
"maxInstructionDefs",
(unsigned)t->maxInstructionDefs,
"maxStackElements", (unsigned)t->maxStackElements,
"maxSizeOfInstructions",
(unsigned)t->maxSizeOfInstructions,
"maxComponentElements",
(unsigned)t->maxComponentElements,
"maxComponentDepth",
(unsigned)t->maxComponentDepth));
}
case 2:
{
char os_2_dict[] = "{s:h, s:h, s:h, s:h, s:h, s:h, s:h, s:h,"
"s:h, s:h, s:h, s:h, s:h, s:h, s:h, s:h, s:s#, s:(llll),"
"s:s#, s:h, s:h, s:h}";
TT_OS2 *t = (TT_OS2 *)table;
return Py::asObject(Py_BuildValue(os_2_dict,
"version", (unsigned)t->version,
"xAvgCharWidth", t->xAvgCharWidth,
"usWeightClass", (unsigned)t->usWeightClass,
"usWidthClass", (unsigned)t->usWidthClass,
"fsType", t->fsType,
"ySubscriptXSize", t->ySubscriptXSize,
"ySubscriptYSize", t->ySubscriptYSize,
"ySubscriptXOffset", t->ySubscriptXOffset,
"ySubscriptYOffset", t->ySubscriptYOffset,
"ySuperscriptXSize", t->ySuperscriptXSize,
"ySuperscriptYSize", t->ySuperscriptYSize,
"ySuperscriptXOffset", t->ySuperscriptXOffset,
"ySuperscriptYOffset", t->ySuperscriptYOffset,
"yStrikeoutSize", t->yStrikeoutSize,
"yStrikeoutPosition", t->yStrikeoutPosition,
"sFamilyClass", t->sFamilyClass,
"panose", t->panose, 10,
"ulCharRange",
(unsigned long) t->ulUnicodeRange1,
(unsigned long) t->ulUnicodeRange2,
(unsigned long) t->ulUnicodeRange3,
(unsigned long) t->ulUnicodeRange4,
"achVendID", t->achVendID, 4,
"fsSelection", (unsigned)t->fsSelection,
"fsFirstCharIndex", (unsigned)t->usFirstCharIndex,
"fsLastCharIndex", (unsigned)t->usLastCharIndex));
}
case 3:
{
char hhea_dict[] = "{s:(h,h), s:h, s:h, s:h, s:i, s:h, s:h, s:h,"
"s:h, s:h, s:h, s:h, s:i}";
TT_HoriHeader *t = (TT_HoriHeader *)table;
return Py::asObject(Py_BuildValue(hhea_dict,
"version",
FIXED_MAJOR(t->Version),
FIXED_MINOR(t->Version),
"ascent", t->Ascender,
"descent", t->Descender,
"lineGap", t->Line_Gap,
"advanceWidthMax", (unsigned)t->advance_Width_Max,
"minLeftBearing", t->min_Left_Side_Bearing,
"minRightBearing", t->min_Right_Side_Bearing,
"xMaxExtent", t->xMax_Extent,
"caretSlopeRise", t->caret_Slope_Rise,
"caretSlopeRun", t->caret_Slope_Run,
"caretOffset", t->caret_Offset,
"metricDataFormat", t->metric_Data_Format,
"numOfLongHorMetrics",
(unsigned)t->number_Of_HMetrics));
}
case 4:
{
char vhea_dict[] = "{s:(h,h), s:h, s:h, s:h, s:i, s:h, s:h, s:h,"
"s:h, s:h, s:h, s:h, s:i}";
TT_VertHeader *t = (TT_VertHeader *)table;
return Py::asObject(Py_BuildValue(vhea_dict,
"version",
FIXED_MAJOR(t->Version),
FIXED_MINOR(t->Version),
"vertTypoAscender", t->Ascender,
"vertTypoDescender", t->Descender,
"vertTypoLineGap", t->Line_Gap,
"advanceHeightMax", (unsigned)t->advance_Height_Max,
"minTopSideBearing", t->min_Top_Side_Bearing,
"minBottomSizeBearing", t->min_Bottom_Side_Bearing,
"yMaxExtent", t->yMax_Extent,
"caretSlopeRise", t->caret_Slope_Rise,
"caretSlopeRun", t->caret_Slope_Run,
"caretOffset", t->caret_Offset,
"metricDataFormat", t->metric_Data_Format,
"numOfLongVerMetrics",
(unsigned)t->number_Of_VMetrics));
}
case 5:
{
TT_Postscript *t = (TT_Postscript *)table;
Py::Dict post;
Py::Tuple format(2), angle(2);
format[0] = Py::Int(FIXED_MAJOR(t->FormatType));
format[1] = Py::Int(FIXED_MINOR(t->FormatType));
post["format"] = format;
angle[0] = Py::Int(FIXED_MAJOR(t->italicAngle));
angle[1] = Py::Int(FIXED_MINOR(t->italicAngle));
post["italicAngle"] = angle;
post["underlinePosition"] = Py::Int(t->underlinePosition);
post["underlineThickness"] = Py::Int(t->underlineThickness);
post["isFixedPitch"] = Py::Long((long) t->isFixedPitch);
post["minMemType42"] = Py::Long((long) t->minMemType42);
post["maxMemType42"] = Py::Long((long) t->maxMemType42);
post["minMemType1"] = Py::Long((long) t->minMemType1);
post["maxMemType1"] = Py::Long((long) t->maxMemType1);
return post;
}
case 6:
{
TT_PCLT *t = (TT_PCLT *)table;
Py::Dict pclt;
Py::Tuple version(2);
version[0] = Py::Int(FIXED_MAJOR(t->Version));
version[1] = Py::Int(FIXED_MINOR(t->Version));
pclt["version"] = version;
pclt["fontNumber"] = Py::Long((long) t->FontNumber);
pclt["pitch"] = Py::Int((short) t->Pitch);
pclt["xHeight"] = Py::Int((short) t->xHeight);
pclt["style"] = Py::Int((short) t->Style);
pclt["typeFamily"] = Py::Int((short) t->TypeFamily);
pclt["capHeight"] = Py::Int((short) t->CapHeight);
pclt["symbolSet"] = Py::Int((short) t->SymbolSet);
pclt["typeFace"] = Py::String((char *) t->TypeFace, 16);
pclt["characterComplement"] = Py::String((char *)
t->CharacterComplement, 8);
pclt["filename"] = Py::String((char *) t->FileName, 6);
pclt["strokeWeight"] = Py::Int((int) t->StrokeWeight);
pclt["widthType"] = Py::Int((int) t->WidthType);
pclt["serifStyle"] = Py::Int((int) t->SerifStyle);
return pclt;
}
default:
return Py::Object();
}
}
char FT2Font::get_image__doc__ [] =
"get_image()\n"
"\n"
"Returns the underlying image buffer for this font object.\n";
Py::Object
FT2Font::get_image(const Py::Tuple &args)
{
args.verify_length(0);
if (image)
{
Py_XINCREF(image);
return Py::asObject(image);
}
throw Py::RuntimeError("You must call .set_text() before .get_image()");
}
char FT2Font::attach_file__doc__ [] =
"attach_file(filename)\n"
"\n"
"Attach a file with extra information on the font\n"
"(in practice, an AFM file with the metrics of a Type 1 font).\n"
"Throws an exception if unsuccessful.\n";
Py::Object
FT2Font::attach_file(const Py::Tuple &args)
{
args.verify_length(1);
std::string filename = Py::String(args[0]);
FT_Error error = FT_Attach_File(face, filename.c_str());
if (error)
{
std::ostringstream s;
s << "Could not attach file " << filename
<< " (freetype error code " << error << ")" << std::endl;
throw Py::RuntimeError(s.str());
}
return Py::Object();
}
Py::Object
ft2font_module::new_ft2image(const Py::Tuple &args)
{
args.verify_length(2);
int width = Py::Int(args[0]);
int height = Py::Int(args[1]);
return Py::asObject(new FT2Image(width, height));
}
Py::Object
ft2font_module::new_ft2font(const Py::Tuple &args)
{
_VERBOSE("ft2font_module::new_ft2font ");
args.verify_length(1);
std::string facefile = Py::String(args[0]);
return Py::asObject(new FT2Font(facefile));
}
void
FT2Image::init_type()
{
_VERBOSE("FT2Image::init_type");
behaviors().name("FT2Image");
behaviors().doc("FT2Image");
add_varargs_method("write_bitmap", &FT2Image::py_write_bitmap,
FT2Image::write_bitmap__doc__);
add_varargs_method("draw_rect", &FT2Image::py_draw_rect,
FT2Image::draw_rect__doc__);
add_varargs_method("draw_rect_filled", &FT2Image::py_draw_rect_filled,
FT2Image::draw_rect_filled__doc__);
add_varargs_method("as_array", &FT2Image::py_as_array,
FT2Image::as_array__doc__);
add_varargs_method("as_str", &FT2Image::py_as_str,
FT2Image::as_str__doc__);
add_varargs_method("as_rgb_str", &FT2Image::py_as_rgb_str,
FT2Image::as_rgb_str__doc__);
add_varargs_method("as_rgba_str", &FT2Image::py_as_rgba_str,
FT2Image::as_rgba_str__doc__);
add_varargs_method("get_width", &FT2Image::py_get_width,
"Returns the width of the image");
add_varargs_method("get_height", &FT2Image::py_get_height,
"Returns the height of the image");
}
void
Glyph::init_type()
{
_VERBOSE("Glyph::init_type");
behaviors().name("Glyph");
behaviors().doc("Glyph");
behaviors().supportGetattr();
behaviors().supportSetattr();
}
void
FT2Font::init_type()
{
_VERBOSE("FT2Font::init_type");
behaviors().name("FT2Font");
behaviors().doc("FT2Font");
add_varargs_method("clear", &FT2Font::clear,
FT2Font::clear__doc__);
add_keyword_method("draw_glyph_to_bitmap", &FT2Font::draw_glyph_to_bitmap,
FT2Font::draw_glyph_to_bitmap__doc__);
add_keyword_method("draw_glyphs_to_bitmap", &FT2Font::draw_glyphs_to_bitmap,
FT2Font::draw_glyphs_to_bitmap__doc__);
add_keyword_method("get_xys", &FT2Font::get_xys,
FT2Font::get_xys__doc__);
add_varargs_method("get_num_glyphs", &FT2Font::get_num_glyphs,
FT2Font::get_num_glyphs__doc__);
add_keyword_method("load_char", &FT2Font::load_char,
FT2Font::load_char__doc__);
add_keyword_method("load_glyph", &FT2Font::load_glyph,
FT2Font::load_glyph__doc__);
add_keyword_method("set_text", &FT2Font::set_text,
FT2Font::set_text__doc__);
add_varargs_method("set_size", &FT2Font::set_size,
FT2Font::set_size__doc__);
add_varargs_method("set_charmap", &FT2Font::set_charmap,
FT2Font::set_charmap__doc__);
add_varargs_method("select_charmap", &FT2Font::select_charmap,
FT2Font::select_charmap__doc__);
add_varargs_method("get_width_height", &FT2Font::get_width_height,
FT2Font::get_width_height__doc__);
add_varargs_method("get_descent", &FT2Font::get_descent,
FT2Font::get_descent__doc__);
add_varargs_method("get_glyph_name", &FT2Font::get_glyph_name,
FT2Font::get_glyph_name__doc__);
add_varargs_method("get_charmap", &FT2Font::get_charmap,
FT2Font::get_charmap__doc__);
add_varargs_method("get_kerning", &FT2Font::get_kerning,
FT2Font::get_kerning__doc__);
add_varargs_method("get_sfnt", &FT2Font::get_sfnt,
FT2Font::get_sfnt__doc__);
add_varargs_method("get_name_index", &FT2Font::get_name_index,
FT2Font::get_name_index__doc__);
add_varargs_method("get_ps_font_info", &FT2Font::get_ps_font_info,
FT2Font::get_ps_font_info__doc__);
add_varargs_method("get_sfnt_table", &FT2Font::get_sfnt_table,
FT2Font::get_sfnt_table__doc__);
add_varargs_method("get_image", &FT2Font::get_image,
FT2Font::get_image__doc__);
add_varargs_method("attach_file", &FT2Font::attach_file,
FT2Font::attach_file__doc__);
add_noargs_method("get_path", &FT2Font::get_path,
"");
behaviors().supportGetattr();
behaviors().supportSetattr();
}
//todo add module docs strings
char ft2font__doc__[] =
"ft2font\n"
"\n"
"Methods:\n"
" FT2Font(ttffile)\n"
"Face Constants\n"
" SCALABLE scalable\n"
" FIXED_SIZES \n"
" FIXED_WIDTH \n"
" SFNT \n"
" HORIZONTAL \n"
" VERTICAL \n"
" KERNING \n"
" FAST_GLYPHS \n"
" MULTIPLE_MASTERS \n"
" GLYPH_NAMES \n"
" EXTERNAL_STREAM \n"
"Style Constants\n"
" ITALIC \n"
" BOLD \n"
;
/* Function of no arguments returning new FT2Font object */
char ft2font_new__doc__[] =
"FT2Font(ttffile)\n"
"\n"
"Create a new FT2Font object\n"
"The following global font attributes are defined:\n"
" num_faces number of faces in file\n"
" face_flags face flags (int type); see the ft2font constants\n"
" style_flags style flags (int type); see the ft2font constants\n"
" num_glyphs number of glyphs in the face\n"
" family_name face family name\n"
" style_name face syle name\n"
" num_fixed_sizes number of bitmap in the face\n"
" scalable face is scalable\n"
"\n"
"The following are available, if scalable is true:\n"
" bbox face global bounding box (xmin, ymin, xmax, ymax)\n"
" units_per_EM number of font units covered by the EM\n"
" ascender ascender in 26.6 units\n"
" descender descender in 26.6 units\n"
" height height in 26.6 units; used to compute a default\n"
" line spacing (baseline-to-baseline distance)\n"
" max_advance_width maximum horizontal cursor advance for all glyphs\n"
" max_advance_height same for vertical layout\n"
" underline_position vertical position of the underline bar\n"
" underline_thickness vertical thickness of the underline\n"
" postscript_name PostScript name of the font\n"
;
#if defined(_MSC_VER)
DL_EXPORT(void)
#elif defined(__cplusplus)
extern "C" void
#else
void
#endif
initft2font(void)
{
static ft2font_module* ft2font = new ft2font_module;
import_array();
Py::Dict d = ft2font->moduleDictionary();
d["SCALABLE"] = Py::Int(FT_FACE_FLAG_SCALABLE);
d["FIXED_SIZES"] = Py::Int(FT_FACE_FLAG_FIXED_SIZES);
d["FIXED_WIDTH"] = Py::Int(FT_FACE_FLAG_FIXED_WIDTH);
d["SFNT"] = Py::Int(FT_FACE_FLAG_SFNT);
d["HORIZONTAL"] = Py::Int(FT_FACE_FLAG_HORIZONTAL);
d["VERTICAL"] = Py::Int(FT_FACE_FLAG_SCALABLE);
d["KERNING"] = Py::Int(FT_FACE_FLAG_KERNING);
d["FAST_GLYPHS"] = Py::Int(FT_FACE_FLAG_FAST_GLYPHS);
d["MULTIPLE_MASTERS"] = Py::Int(FT_FACE_FLAG_MULTIPLE_MASTERS);
d["GLYPH_NAMES"] = Py::Int(FT_FACE_FLAG_GLYPH_NAMES);
d["EXTERNAL_STREAM"] = Py::Int(FT_FACE_FLAG_EXTERNAL_STREAM);
d["ITALIC"] = Py::Int(FT_STYLE_FLAG_ITALIC);
d["BOLD"] = Py::Int(FT_STYLE_FLAG_BOLD);
d["KERNING_DEFAULT"] = Py::Int(FT_KERNING_DEFAULT);
d["KERNING_UNFITTED"] = Py::Int(FT_KERNING_UNFITTED);
d["KERNING_UNSCALED"] = Py::Int(FT_KERNING_UNSCALED);
d["LOAD_DEFAULT"] = Py::Long(FT_LOAD_DEFAULT);
d["LOAD_NO_SCALE"] = Py::Long(FT_LOAD_NO_SCALE);
d["LOAD_NO_HINTING"] = Py::Long(FT_LOAD_NO_HINTING);
d["LOAD_RENDER"] = Py::Long(FT_LOAD_RENDER);
d["LOAD_NO_BITMAP"] = Py::Long(FT_LOAD_NO_BITMAP);
d["LOAD_VERTICAL_LAYOUT"] = Py::Long(FT_LOAD_VERTICAL_LAYOUT);
d["LOAD_FORCE_AUTOHINT"] = Py::Long(FT_LOAD_FORCE_AUTOHINT);
d["LOAD_CROP_BITMAP"] = Py::Long(FT_LOAD_CROP_BITMAP);
d["LOAD_PEDANTIC"] = Py::Long(FT_LOAD_PEDANTIC);
d["LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH"] =
Py::Long(FT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH);
d["LOAD_NO_RECURSE"] = Py::Long(FT_LOAD_NO_RECURSE);
d["LOAD_IGNORE_TRANSFORM"] = Py::Long(FT_LOAD_IGNORE_TRANSFORM);
d["LOAD_MONOCHROME"] = Py::Long(FT_LOAD_MONOCHROME);
d["LOAD_LINEAR_DESIGN"] = Py::Long(FT_LOAD_LINEAR_DESIGN);
// These need casting because large-valued numeric literals could
// be either longs or unsigned longs:
d["LOAD_NO_AUTOHINT"] = Py::Long((unsigned long)FT_LOAD_NO_AUTOHINT);
d["LOAD_TARGET_NORMAL"] = Py::Long((unsigned long)FT_LOAD_TARGET_NORMAL);
d["LOAD_TARGET_LIGHT"] = Py::Long((unsigned long)FT_LOAD_TARGET_LIGHT);
d["LOAD_TARGET_MONO"] = Py::Long((unsigned long)FT_LOAD_TARGET_MONO);
d["LOAD_TARGET_LCD"] = Py::Long((unsigned long)FT_LOAD_TARGET_LCD);
d["LOAD_TARGET_LCD_V"] = Py::Long((unsigned long)FT_LOAD_TARGET_LCD_V);
//initialize library
int error = FT_Init_FreeType(&_ft2Library);
if (error)
{
throw Py::RuntimeError("Could not find initialize the freetype2 library");
}
{
FT_Int major, minor, patch;
char version_string[64];
FT_Library_Version(_ft2Library, &major, &minor, &patch);
sprintf(version_string, "%d.%d.%d", major, minor, patch);
d["__freetype_version__"] = Py::String(version_string);
}
}
ft2font_module::~ft2font_module()
{
FT_Done_FreeType(_ft2Library);
}
|