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
|
/* Copyright (C) 2001-2022 Artifex Software, Inc.
All Rights Reserved.
This software is provided AS-IS with no warranty, either express or
implied.
This software is distributed under license and may not be copied,
modified or distributed except as expressly authorized under the terms
of the license contained in the file LICENSE in this distribution.
Refer to licensing information at http://www.artifex.com or contact
Artifex Software, Inc., 1305 Grant Avenue - Suite 200, Novato,
CA 94945, U.S.A., +1(415)492-9861, for further information.
*/
/* plchar.c */
/* PCL font handling library -- operations on individual characters */
#include "math_.h"
#include "memory_.h"
#include "stdio_.h" /* for gdebug.h */
#include "gdebug.h"
#include "gserrors.h"
#include "gstypes.h"
#include "gsmemory.h"
#include "gsstruct.h"
#include "gsmatrix.h"
#include "gsstate.h"
#include "gschar.h"
#include "gsimage.h"
#include "gspaint.h"
#include "gspath.h"
#include "gsbittab.h"
#include "gxarith.h" /* for igcd */
#include "gxfont.h"
#include "gxfont42.h"
#include "plfont.h"
#include "plvalue.h"
#include "gscoord.h"
#include "gsstate.h"
#include "gxdevice.h"
#include "gxdevmem.h"
#include "gxpath.h"
/* We really shouldn't need the following, but currently they are needed */
/* for pgs->path and penum->log2_current_scale in pl_tt_build_char. */
#include "gxfixed.h"
#include "gxchar.h"
#include "gxfcache.h"
#include "gxttf.h"
#include "gzstate.h"
#include "plchar.h"
/* include the prototype for pixmap_high_level_pattern */
#include "gsptype1.h"
#include "gsgstate.h"
#include "gxgstate.h"
/* Define whether to cache TrueType characters. */
/* This would only be disabled for debugging. */
#define CACHE_TRUETYPE_CHARS
/* Structure descriptors */
gs_private_st_ptrs1(st_pl_font_glyph, pl_font_glyph_t, "pl_font_glyph_t",
pl_font_glyph_enum_ptrs, pl_font_glyph_reloc_ptrs, data);
gs_private_st_element(st_pl_font_glyph_element, pl_font_glyph_t,
"pl_font_glyph_t[]",
pl_font_glyph_elt_enum_ptrs,
pl_font_glyph_elt_reloc_ptrs, st_pl_font_glyph);
int
pl_prepend_xl_dummy_header(gs_memory_t * mem, byte ** ppheader)
{
return 0;
}
int
pl_swap_header(byte * header, uint gifct)
{
return 0;
}
/* ---------------- Utilities ---------------- */
/* Look up a glyph in a font. Return a pointer to the glyph's slot */
/* (data != 0) or where it should be added (data == 0). */
pl_font_glyph_t *
pl_font_lookup_glyph(const pl_font_t * plfont, gs_glyph glyph)
{
uint size = plfont->glyphs.size;
uint skip = plfont->glyphs.skip;
uint index = glyph % size;
pl_font_glyph_t *pfg;
pl_font_glyph_t *result = 0;
while ((pfg = plfont->glyphs.table + index)->data ?
pfg->glyph != glyph : pfg->glyph != 0) {
if (!pfg->data)
result = pfg;
index = (index >= skip ? index : index + size) - skip;
}
return (!pfg->data && result) ? result : pfg;
}
/* ---------------- Bitmap font support ---------------- */
/* Encode a character for a bitmap font. This is simple, because */
/* bitmap fonts are always bound. */
static gs_glyph
pl_bitmap_encode_char(gs_font * pfont, gs_char chr, gs_glyph_space_t not_used)
{
return (gs_glyph) chr;
}
/* Get character existence and escapement for a bitmap font. */
/* This is simple for the same reason. */
static int
pl_bitmap_char_width(const pl_font_t * plfont, const void *pgs,
gs_char char_code, gs_point * pwidth)
{
const byte *cdata = pl_font_lookup_glyph(plfont, char_code)->data;
pwidth->x = pwidth->y = 0;
if (cdata == 0) {
return 1;
}
if (cdata[0] == 0) { /* PCL XL characters don't have an escapement. */
pwidth->x = pwidth->y = 0;
return 0;
}
{
const byte *params = cdata + 6;
pwidth->x = (plfont->header[13] ? /* variable pitch */
pl_get_int16(params + 8) * 0.25 :
pl_get_int16(params) /*lsb */ +pl_get_int16(params +
4) /*width */
);
}
return 0;
}
static int
pl_bitmap_char_metrics(const pl_font_t * plfont, const void *pgs,
gs_char char_code, float metrics[4])
{
gs_point width;
const byte *cdata = pl_font_lookup_glyph(plfont, char_code)->data;
/* never a vertical substitute */
metrics[0] = metrics[1] = metrics[2] = metrics[3] = 0;
/* no data - character not found */
if (cdata == 0)
return 1;
/* We are not concerned about PCL XL characters */
if (cdata[0] == 0)
return 0;
metrics[0] = (float)pl_get_int16(cdata + 6);
pl_bitmap_char_width(plfont, pgs, char_code, &width);
metrics[2] = width.x;
return 0;
}
/*
* For pseudo-bolding, we have to "smear" a bitmap horizontally and
* vertically by ORing together a rectangle of bits below and to the left of
* each output bit. We do this separately for horizontal and vertical
* smearing. Eventually, we will replace the current procedure, which takes
* time proportional to W * H * (N + log2(N)), with one that is only
* proportional to N (but takes W * N additional buffer space).
*/
/* Allocate the line buffer for bolding. We need 2 + bold scan lines. */
static byte *
alloc_bold_lines(gs_memory_t * mem, uint width, int bold, client_name_t cname)
{
return gs_alloc_byte_array(mem, 2 + bold, bitmap_raster(width + bold),
cname);
}
/* Merge one (aligned) scan line into another, for vertical smearing. */
static void
bits_merge(byte * dest, const byte * src, uint nbytes)
{
long *dp = (long *)dest;
const long *sp = (const long *)src;
uint n = (nbytes + sizeof(long) - 1) >> ARCH_LOG2_SIZEOF_LONG;
for (; n >= 4; sp += 4, dp += 4, n -= 4)
dp[0] |= sp[0], dp[1] |= sp[1], dp[2] |= sp[2], dp[3] |= sp[3];
for (; n; ++sp, ++dp, --n)
*dp |= *sp;
}
/* Smear a scan line horizontally. Note that the output is wider than */
/* the input by the amount of bolding (smear_width). */
static void
bits_smear_horizontally(byte * dest, const byte * src, uint width,
uint smear_width)
{
uint bits_on = 0;
const byte *sp = src;
uint sbyte = *sp;
byte *dp = dest;
uint dbyte = sbyte;
uint sdmask = 0x80;
const byte *zp = src;
uint zmask = 0x80;
uint i = 0;
/* Process the first smear_width bits. */
{
uint stop = min(smear_width, width);
for (; i < stop; ++i) {
if (sbyte & sdmask)
bits_on++;
else if (bits_on)
dbyte |= sdmask;
if ((sdmask >>= 1) == 0)
sdmask = 0x80, *dp++ = dbyte, dbyte = sbyte = *++sp;
}
}
/* Process all but the last smear_width bits. */
{
for (; i < width; ++i) {
if (sbyte & sdmask)
bits_on++;
else if (bits_on)
dbyte |= sdmask;
if (*zp & zmask)
--bits_on;
if ((sdmask >>= 1) == 0) {
sdmask = 0x80;
*dp++ = dbyte;
on:switch ((dbyte = sbyte = *++sp)) {
case 0xff:
if (width - i <= 8)
break;
*dp++ = 0xff;
bits_on += 8 -
byte_count_bits[(*zp & (zmask - 1)) +
(zp[1] & -(int)zmask)];
++zp;
i += 8;
goto on;
case 0:
if (bits_on || width - i <= 8)
break;
*dp++ = 0;
/* We know there can't be any bits to be zeroed, */
/* because bits_on can't go negative. */
++zp;
i += 8;
goto on;
default:
;
}
}
if ((zmask >>= 1) == 0)
zmask = 0x80, ++zp;
}
}
/* Process the last smear_width bits. */
/****** WRONG IF width < smear_width ******/
{
uint stop = width + smear_width;
for (; i < stop; ++i) {
if (bits_on)
dbyte |= sdmask;
if ((sdmask >>= 1) == 0)
sdmask = 0x80, *dp++ = dbyte, dbyte = 0;
if (*zp & zmask)
--bits_on;
if ((zmask >>= 1) == 0)
zmask = 0x80, ++zp;
}
}
if (sdmask != 0x80)
*dp = dbyte;
}
/* Image a bitmap character, with or without bolding. */
int
pl_image_bitmap_char(gs_image_enum * ienum, const gs_image_t * pim,
const byte * bitmap_data, uint sraster, int bold,
byte * bold_lines, gs_gstate * pgs)
{
uint dest_bytes = (pim->Width + 7) >> 3;
int code;
gs_image_enum *penum;
uint used;
code = gx_set_dev_color(pgs);
if (code == gs_error_Remap_Color)
code = pixmap_high_level_pattern(pgs);
if (code != 0)
return code;
penum = gs_image_enum_alloc(gs_gstate_memory(pgs), "pl_image_bitmap_char");
if (penum == NULL)
return_error(gs_error_VMerror);
code = gs_image_init(penum, pim, pim->ImageMask | pim->CombineWithColor, true, pgs);
if (code < 0)
goto free_penum_and_exit;
if (bold) { /* Pass individual smeared lines. */
uint src_width = pim->Width - bold;
uint src_height = pim->Height - bold;
uint dest_raster = bitmap_raster(pim->Width);
int n1 = bold + 1;
#define merged_line(i) (bold_lines + ((i) % n1 + 1) * dest_raster)
int y;
for (y = 0; y < pim->Height; ++y) {
int y0 = (y < bold ? 0 : y - bold);
int y1 = min(y + 1, src_height);
if (y < src_height) {
bits_smear_horizontally(merged_line(y),
bitmap_data + y * sraster,
src_width, bold);
{ /* Now re-establish the invariant -- see below. */
int kmask = 1;
for (; (y & kmask) == kmask && y - kmask >= y0;
kmask = (kmask << 1) + 1)
bits_merge(merged_line(y - kmask),
merged_line(y - (kmask >> 1)), dest_bytes);
}
}
/*
* As of this point in the loop, we maintain the following
* invariant to cache partial merges of groups of lines: for
* each Y, y0 <= Y < y1, let K be the maximum k such that Y
* mod 2^k = 0 and Y + 2^k < y1; then merged_line(Y) holds
* the union of horizontally smeared source lines Y through
* Y + 2^k - 1. The idea behind this is similar to the idea
* of quicksort.
*/
{ /* Now construct the output line. */
bool first = true;
int iy;
for (iy = y1 - 1; iy >= y0; --iy) {
int kmask = 1;
while ((iy & kmask) == kmask && iy - kmask >= y0)
iy -= kmask, kmask <<= 1;
if (first) {
memcpy(bold_lines, merged_line(iy), dest_bytes);
first = false;
} else
bits_merge(bold_lines, merged_line(iy), dest_bytes);
}
}
code = gs_image_next(penum, bold_lines, dest_bytes, &used);
if (code != 0)
break;
}
#undef merged_line
} else { /* Pass the entire image at once. */
int i;
for (i = 0; i < pim->Height; i++) {
code = gs_image_next(penum, bitmap_data, dest_bytes, &used);
if (code < 0)
break;
bitmap_data += sraster;
}
}
free_penum_and_exit:
{
int code2 = gs_image_cleanup_and_free_enum(penum, pgs);
if (code == 0)
code = code2;
}
return code;
}
/* Render a character for a bitmap font. */
/* This handles both format 0 (PCL XL) and format 4 (PCL5 bitmap). */
static int
pl_bitmap_build_char(gs_show_enum * penum, gs_gstate * pgs, gs_font * pfont,
gs_char chr, gs_glyph glyph)
{
pl_font_t *plfont = (pl_font_t *) pfont->client_data;
const byte *cdata = pl_font_lookup_glyph(plfont, glyph)->data;
bool orient = plfont->orient;
if (cdata == 0) {
return gs_setcharwidth(penum, pgs, 0, 0);
} else {
const byte *params;
const byte *bitmap_data;
int lsb, ascent;
float delta_x;
gs_image_t image;
gs_image_enum *ienum;
int code;
uint bold;
byte *bold_lines = 0;
if (cdata[0] == 0) { /* PCL XL format */
params = cdata + 2;
bitmap_data = cdata + round_up(10, ARCH_ALIGN_PTR_MOD);
delta_x = 0; /* irrelevant */
lsb = pl_get_int16(params);
ascent = pl_get_int16(params + 2);
} else { /* PCL5 format */
params = cdata + 6;
bitmap_data = cdata + 16;
delta_x = (plfont->header[13] ? /* variable pitch */
pl_get_int16(params + 8) * 0.25 :
(short)(pl_get_int16(params) /*lsb */
+pl_get_int16(params + 4)) /*width */ );
lsb = pl_get_int16(params);
ascent = pl_get_int16(params + 2);
}
ienum = gs_image_enum_alloc(pgs->memory, "pl_bitmap_build_char");
if (ienum == 0)
return_error(gs_error_VMerror);
gs_image_t_init_mask(&image, true);
image.Width = pl_get_uint16(params + 4);
image.Height = pl_get_uint16(params + 6);
/* Determine the amount of pseudo-bolding. */
if (plfont->bold_fraction != 0) {
bold = (uint) (2 * image.Height * plfont->bold_fraction + 0.5);
bold_lines = alloc_bold_lines(pgs->memory, image.Width, bold,
"pl_bitmap_build_char(bold_line)");
if (bold_lines == 0) {
code = gs_note_error(gs_error_VMerror);
goto out;
}
image.Width += bold;
image.Height += bold;
ascent += bold;
} else
bold = 0;
gs_make_identity(&image.ImageMatrix);
gs_matrix_rotate(&image.ImageMatrix, orient * -90,
&image.ImageMatrix);
image.ImageMatrix.tx -= lsb;
image.ImageMatrix.ty += ascent;
image.adjust = true;
if (bold || orient != 0)
code = gs_setcharwidth(penum, pgs, delta_x, 0);
else {
/* we use cache device for portrait bitmap fonts to
avoid image setup overhead. */
float m[6];
m[0] = delta_x;
m[1] = 0;
m[2] = (float)lsb;
m[3] = (float)(image.Height - ascent);
m[4] = image.Width + m[2];
m[5] = (float)(-ascent);
code = gs_setcachedevice(penum, pgs, m);
}
if (code < 0)
goto out;
#ifdef DEBUG
if (gs_debug_c('B')) {
int i;
int pixels = round_up(image.Width, 8) * image.Height;
dmprintf7(pgs->memory,
"bitmap font data chr=%ld, width=%d, height=%d, lsb=%d, ascent=%d, top offset=%d left offset=%d\n",
chr, image.Width, image.Height, lsb, ascent,
pl_get_int16(params + 2), pl_get_int16(params));
for (i = 0; i < pixels; i++) {
if (i % round_up(image.Width, 8) == 0)
dmprintf(pgs->memory, "\n");
dmprintf1(pgs->memory, "%d",
bitmap_data[i >> 3] & (128 >> (i & 7)) ? 1 : 0);
}
dmprintf(pgs->memory, "\n");
}
#endif
code = pl_image_bitmap_char(ienum, &image, bitmap_data,
(image.Width - bold + 7) >> 3, bold,
bold_lines, pgs);
out:gs_free_object(pgs->memory, bold_lines,
"pl_bitmap_build_char(bold_lines)");
gs_free_object(pgs->memory, ienum, "pl_bitmap_build_char");
return (code < 0 ? code : 0);
}
}
/* ---------------- TrueType font support ---------------- */
/* Look up a character in the TrueType character-to-TT-glyph map. */
/* Return a pointer to the glyph's slot (chr != gs_no_char) or where */
/* it should be added (chr == gs_no_char). */
pl_tt_char_glyph_t *
pl_tt_lookup_char(const pl_font_t * plfont, gs_glyph glyph)
{
uint size = plfont->char_glyphs.size;
uint skip = plfont->char_glyphs.skip;
uint index = glyph % size;
pl_tt_char_glyph_t *ptcg;
pl_tt_char_glyph_t *result = 0;
while ((ptcg = plfont->char_glyphs.table + index)->chr != gs_no_char ?
ptcg->chr != glyph : ptcg->glyph) {
if (ptcg->chr == gs_no_char)
result = ptcg;
index = (index >= skip ? index : index + size) - skip;
}
return (result ? result : ptcg);
}
/* Get a string from a TrueType font. */
static int
pl_tt_string_proc(gs_font_type42 * pfont, ulong offset, uint length,
const byte ** pdata)
{
pl_font_t *plfont = pfont->client_data;
*pdata = plfont->header + plfont->offsets.GT +
(plfont->large_sizes ? 6 : 4) + offset;
if (*pdata > plfont->header + plfont->header_size)
return_error(gs_error_invalidfont);
return 0;
}
/* Return the vertical substitute for a glyph, if it has one; */
/* otherwise return GS_NO_GLYPH. */
gs_glyph
pl_font_vertical_glyph(gs_glyph glyph, const pl_font_t * plfont)
{
long VT = plfont->offsets.VT;
const byte *vtseg;
uint i, len;
if (VT < 0)
return GS_NO_GLYPH;
vtseg = plfont->header + VT;
if (plfont->large_sizes)
len = pl_get_uint32(vtseg + 2), i = 6;
else
len = pl_get_uint16(vtseg + 2), i = 4;
len += i;
for (; i < len; i += 4)
if (glyph == pl_get_uint16(vtseg + i))
return pl_get_uint16(vtseg + i + 2);
return GS_NO_GLYPH;
}
/* retrieve lsb and width metrics for Format 1 Class 2 glyphs */
int
pl_tt_f1c2_get_metrics(gs_font_type42 * pfont, uint glyph_index,
int wmode, float *sbw)
{
int code = gs_error_undefined;
pl_font_t *plfont = pfont->client_data;
const pl_font_glyph_t *pfg = 0;
const byte *cdata = 0;
if (plfont->glyphs.table != 0) {
/* at least one caller calls before the glyph.table is valid, no chars yet
* test routes caller to gs_type42_default_get_metrics
*/
pfg = pl_font_lookup_glyph(plfont, glyph_index);
cdata = pfg->data;
if (cdata && (cdata[1] == 1 || cdata[1] == 2)) {
double factor = 1.0 / pfont->data.unitsPerEm;
uint width;
int lsb;
lsb = pl_get_int16(cdata + 4);
width = pl_get_int16(cdata + 6);
/* foo NB what about the top side bearing in class 2 ? */
if (wmode) {
/* NB BUG all other fonts work without this sign
change it should already be accounted for in the
character ctm */
factor = -factor; /* lsb and width go down the page */
sbw[0] = 0, sbw[1] = lsb * factor;
sbw[2] = 0, sbw[3] = width * factor;
} else {
sbw[0] = lsb * factor, sbw[1] = 0;
sbw[2] = width * factor, sbw[3] = 0;
}
code = 0; /* tt class 1,2 */
}
}
return code;
}
/* get metrics with support for XL tt class 1 and 2
* pl overrides gstype42_default_get_metrics
*/
static int
pl_tt_get_metrics(gs_font_type42 * pfont, uint glyph_index,
gs_type42_metrics_options_t options, float *sbw)
{
int wmode = gs_type42_metrics_options_wmode(options);
int code;
if ((code = pl_tt_f1c2_get_metrics (pfont, glyph_index, wmode, sbw)) != gs_error_undefined) {
return code;
}
/* else call default implementation for tt class 0, incomplete font */
/* first check for a vertical substitute if writing mode is
vertical. We unpleasantly replace the glyph_index parameter
passed to procedure to be consist with the pl_tt_build_char()
debacle */
{
pl_font_t *plfont = pfont->client_data;
if (plfont->allow_vertical_substitutes) {
gs_glyph vertical = pl_font_vertical_glyph(glyph_index, plfont);
if (vertical != GS_NO_GLYPH)
glyph_index = vertical;
}
}
/* the graphics library does not do this correctly. If there
aren't two sets of metrics WMode should be ignored. We work
around that here. */
if (wmode == 1) {
const gs_type42_mtx_t *pmtx = &pfont->data.metrics[wmode];
if (pmtx->length == 0) {
wmode = 0;
} else {
if (gs_debug_c('=')) {
dmprintf(pfont->memory, "Found vertical metrics\n");
}
}
}
return gs_type42_default_get_metrics(pfont, glyph_index, wmode, sbw);
}
/* Get the outline data for a glyph in a downloaded TrueType font. */
int
pl_tt_get_outline(gs_font_type42 * pfont, uint index, gs_glyph_data_t * pdata)
{
pl_font_t *plfont = pfont->client_data;
const pl_font_glyph_t *pfg = pl_font_lookup_glyph(plfont, index);
const byte *cdata = pfg->data;
if (cdata == 0) {
/* undefined glyph */
gs_glyph_data_from_null(pdata);
} else {
uint desc_size =
(*cdata == 15 ? cdata[2] /* PCL5 */ : 0 /* PCL XL */ );
uint data_size = pl_get_uint16(cdata + 2 + desc_size);
if (data_size > pfg->data_len)
data_size = pfg->data_len;
if (data_size <= 4) {
/* empty outline */
gs_glyph_data_from_null(pdata);
} else if (cdata[1] == 0) {
gs_glyph_data_from_bytes(pdata,
cdata,
6 + desc_size, data_size - 4, NULL);
} else if (cdata[1] == 1) {
gs_glyph_data_from_bytes(pdata, cdata, 10, data_size - 8, NULL);
} else if (cdata[1] == 2) {
gs_glyph_data_from_bytes(pdata, cdata, 12, data_size - 10, NULL);
}
}
return 0;
}
#define access(base, length, vptr)\
(*pfont->data.string_proc)(pfont, (ulong)(base), length, &vptr)
/* Find a table in a TrueType font. */
/* Return the data offset of the table; store the length in *plen. */
/* If the table is missing, return 0. */
ulong
tt_find_table(gs_font_type42 * pfont, const char *tname, uint * plen)
{
const byte *OffsetTable;
uint numTables;
const byte *TableDirectory;
uint i;
ulong table_dir_offset = 0;
int code;
if (((code = access(0, 12, OffsetTable)) < 0) ||
((code = access(table_dir_offset, 12, OffsetTable)) < 0))
return 0;
numTables = pl_get_uint16(OffsetTable + 4);
if (access(table_dir_offset + 12, numTables * 16, TableDirectory) < 0)
return 0;
for (i = 0; i < numTables; ++i) {
const byte *tab = TableDirectory + i * 16;
if (!memcmp(tab, tname, 4)) {
if (plen)
*plen = pl_get_uint32(tab + 12);
return pl_get_uint32(tab + 8);
}
}
return 0;
}
/*
* Map a key through a cmap sub-table. We export this so we can use
* it someday for mapping between glyph vocabularies. If the key is
* not mappable, return gs_error_undefined; if the sub-table type is
* unknown, return gs_error_invalidfont.
*/
static int
pl_cmap_lookup(const uint key, const byte * table, uint * pvalue)
{ /* Dispatch according to the table type. */
switch (pl_get_uint16(table)) {
case 0:
{ /* Apple standard 1-to-1 mapping. */
*pvalue = table[key + 6];
if_debug2('J', "[J]%u => %u\n", key, *pvalue);
break;
}
case 4:
{ /* Microsoft/Adobe segmented mapping. What a mess! */
uint segCount2 = pl_get_uint16(table + 6);
const byte *endCount = table + 14;
const byte *startCount = endCount + segCount2 + 2;
const byte *idDelta = startCount + segCount2;
const byte *idRangeOffset = idDelta + segCount2;
/*const byte *glyphIdArray = idRangeOffset + segCount2; */
uint i2;
for (i2 = 0; i2 < segCount2 - 3; i2 += 2) {
int delta, roff;
uint start = pl_get_uint16(startCount + i2);
uint glyph;
if_debug4('J', "[J]start=%u end=%u delta=%d roff=%d\n",
start, pl_get_uint16(endCount + i2),
pl_get_int16(idDelta + i2),
pl_get_int16(idRangeOffset + i2));
if (key < start) {
if_debug1('J', "[J]%u out of range\n", key);
return_error(gs_error_undefined);
}
if (key > pl_get_uint16(endCount + i2))
continue;
delta = pl_get_int16(idDelta + i2);
roff = pl_get_int16(idRangeOffset + i2);
if (roff == 0) {
*pvalue = (key + delta) & 0xffff; /* mod 65536 */
if_debug2('J', "[J]%u => %u\n", key, *pvalue);
return 0;
}
glyph = pl_get_uint16(idRangeOffset + i2 + roff +
((key - start) << 1));
*pvalue = (glyph == 0 ? 0 : glyph + delta);
if_debug2('J', "[J]%u => %u\n", key, *pvalue);
return 0;
}
/*
* The TrueType documentation says that the last range is
* always supposed to end with 0xffff, so this shouldn't
* happen; however, in some real fonts, it does.
*/
if_debug1('J', "[J]%u out of range\n", key);
return_error(gs_error_undefined);
}
case 6:
{ /* Single interval lookup. */
uint firstCode = pl_get_uint16(table + 6);
uint entryCount = pl_get_uint16(table + 8);
if (key < firstCode || key >= firstCode + entryCount) {
if_debug1('J', "[J]%u out of range\n", key);
return_error(gs_error_undefined);
}
*pvalue =
pl_get_uint16(table + 10 + ((key - firstCode) << 1));
if_debug2('J', "[J]%u => %u\n", key, *pvalue);
break;
}
default:
return_error(gs_error_invalidfont);
}
return 0;
}
/* Encode a character using the TrueType cmap tables. */
/* (We think this is never used for downloaded fonts.) */
static gs_glyph
pl_tt_cmap_encode_char(gs_font_type42 * pfont, ulong cmap_offset,
uint cmap_len, gs_char chr)
{
const byte *cmap;
const byte *cmap_sub;
const byte *table;
uint value;
int code;
access(cmap_offset, cmap_len, cmap);
/* Since the Apple cmap format is of no help in determining */
/* the encoding, look for a Microsoft table; but if we can't */
/* find one, take the first one. */
cmap_sub = cmap + 4;
{
uint i;
for (i = 0; i < pl_get_uint16(cmap + 2); ++i) {
if_debug3m('j', pfont->memory,
"[j]cmap %d: platform %u encoding %u\n", i,
pl_get_uint16(cmap_sub + i * 8),
pl_get_uint16(cmap_sub + i * 8 + 2));
if (pl_get_uint16(cmap_sub + i * 8) == 3) {
cmap_sub += i * 8;
break;
}
}
}
{
uint offset = cmap_offset + pl_get_uint32(cmap_sub + 4);
access(offset, cmap_offset + cmap_len - offset, table);
}
code = pl_cmap_lookup((uint) chr, table, &value);
return (code < 0 ? GS_NO_GLYPH : value);
}
/* Encode a character using the map built for downloaded TrueType fonts. */
static gs_glyph
pl_tt_dynamic_encode_char(const gs_font_type42 * pfont, gs_char chr)
{
pl_font_t *plfont = pfont->client_data;
const pl_tt_char_glyph_t *ptcg = pl_tt_lookup_char(plfont, chr);
return (ptcg->chr == gs_no_char ? GS_NO_GLYPH : ptcg->glyph);
}
/* Return the galley character for a character code, if any; */
/* otherwise return gs_no_char. */
/* Note that we return 0xffff for a character that is explicitly */
/* designated as undefined. */
static gs_char
pl_font_galley_character(gs_char chr, const pl_font_t * plfont)
{
long GC = plfont->offsets.GC;
const byte *gcseg;
uint b0, b1;
uint i, len;
uint default_char;
if (GC < 0)
return gs_no_char;
gcseg = plfont->header + GC;
if (plfont->large_sizes)
len = pl_get_uint32(gcseg + 2), i = 12;
else
len = pl_get_uint16(gcseg + 2), i = 10;
if (len != pl_get_uint16(gcseg + i - 2) * 6 + 6) /* bad data */
return gs_no_char;
default_char = pl_get_uint16(gcseg + i - 4); /* default character */
len += i - 6;
b0 = chr >> 8;
b1 = chr & 0xff;
for (; i < len; i += 6)
if (b0 >= gcseg[i] && b0 <= gcseg[i + 1] &&
b1 >= gcseg[i + 2] && b1 <= gcseg[i + 3]
)
return pl_get_uint16(gcseg + i + 4);
return default_char;
}
/* Encode a character for a TrueType font. */
/* What we actually return is the TT glyph index. Note that */
/* we may return either GS_NO_GLYPH or 0 for an undefined character. */
gs_glyph
pl_tt_encode_char(gs_font * pfont_generic, gs_char chr,
gs_glyph_space_t not_used)
{
gs_font_type42 *pfont = (gs_font_type42 *) pfont_generic;
uint cmap_len;
ulong cmap_offset = tt_find_table(pfont, "cmap", &cmap_len);
gs_glyph glyph = (cmap_offset == 0 ?
/* This is a downloaded font with no cmap. */
pl_tt_dynamic_encode_char(pfont, chr) :
pl_tt_cmap_encode_char(pfont, cmap_offset, cmap_len,
chr));
pl_font_t *plfont = pfont->client_data;
pl_font_glyph_t *pfg;
if (plfont->offsets.GC < 0)
return glyph; /* no substitute */
pfg = pl_font_lookup_glyph(plfont, glyph);
/* If the character is missing, use the galley character instead. */
if (!pfg->data) {
gs_char galley_char = pl_font_galley_character(chr, plfont);
if (galley_char != gs_no_char) {
return
(galley_char == 0xffff ? 0 :
cmap_offset == 0 ?
pl_tt_dynamic_encode_char(pfont, galley_char) :
pl_tt_cmap_encode_char(pfont, cmap_offset, cmap_len,
galley_char));
}
}
return glyph; /* no substitute */
}
/* Get metrics */
static int
pl_tt_char_metrics(const pl_font_t * plfont, const void *pgs,
gs_char char_code, float metrics[4])
{
gs_glyph unused_glyph = GS_NO_GLYPH;
gs_glyph glyph =
pl_tt_encode_char(plfont->pfont, char_code, unused_glyph);
if (glyph == GS_NO_GLYPH) {
return 1;
}
return gs_type42_get_metrics((gs_font_type42 *) plfont->pfont,
glyph, metrics);
}
/* Get character existence and escapement for a TrueType font. */
static int
pl_tt_char_width(const pl_font_t * plfont, const void *pgs, gs_char char_code,
gs_point * pwidth)
{
gs_font *pfont = plfont->pfont;
gs_char chr = char_code;
gs_glyph unused_glyph = GS_NO_GLYPH;
gs_glyph glyph = pl_tt_encode_char(pfont, chr, unused_glyph);
int code;
float sbw[4];
pwidth->x = pwidth->y = 0;
/* Check for a vertical substitute. */
if (pfont->WMode & 1) {
gs_glyph vertical = pl_font_vertical_glyph(glyph, plfont);
if (vertical != GS_NO_GLYPH)
glyph = vertical;
}
/* undefined character */
if (glyph == 0xffff || glyph == GS_NO_GLYPH)
return 1;
code = gs_type42_get_metrics((gs_font_type42 *) pfont, glyph, sbw);
if (code < 0)
return code;
/* character exists */
pwidth->x = sbw[2];
return 0;
}
/* Render a TrueType character. */
static int
pl_tt_build_char(gs_show_enum * penum, gs_gstate * pgs, gs_font * pfont,
gs_char chr, gs_glyph orig_glyph)
{
gs_glyph glyph = orig_glyph;
#define pbfont ((gs_font_base *)pfont)
#define pfont42 ((gs_font_type42 *)pfont)
int code;
pl_font_t *plfont = (pl_font_t *) pfont->client_data;
float bold_fraction =
gs_show_in_charpath(penum) != cpm_show ? 0.0 : plfont->bold_fraction;
uint bold_added;
double scale = 1.0;
float sbw[4], w2[6];
int ipx = 0;
int ipy = 0;
int iqx, iqy;
gx_device_memory *pmdev;
bool ctm_modified = false;
bool bold_device_created = false;
gs_matrix save_ctm;
#define isDownloaded(p42) ((p42)->data.proc_data == 0)
#ifdef CACHE_TRUETYPE_CHARS
# define tt_set_cache(penum, pgs, w2)\
gs_setcachedevice(penum, pgs, w2)
#else
# define tt_set_cache(penum, pgs, w2)\
gs_setcharwidth(penum, pgs, w2[0], w2[1]);
#endif
/* undefined */
if (glyph == GS_NO_GLYPH)
return 0;
/* Get the metrics and set the cache device. */
code = gs_type42_get_metrics(pfont42, glyph, sbw);
if (code < 0)
return code;
w2[0] = sbw[2], w2[1] = sbw[3];
/* Adjust the bounding box for stroking if needed. */
{
const gs_rect *pbbox = &pbfont->FontBBox;
w2[2] = pbbox->p.x, w2[3] = pbbox->p.y;
w2[4] = pbbox->q.x, w2[5] = pbbox->q.y;
if (pfont->PaintType) {
double expand = max(1.415, gs_currentmiterlimit(pgs)) *
gs_currentlinewidth(pgs) / 2;
w2[2] -= expand, w2[3] -= expand;
w2[4] += expand, w2[5] += expand;
}
}
/* Establish a current point. */
if ((code = gs_moveto(pgs, 0.0, 0.0)) < 0)
return code;
{
/* Check for a vertical substitute. */
if (plfont->allow_vertical_substitutes) {
pl_font_t *plfont = pfont->client_data;
gs_glyph vertical = pl_font_vertical_glyph(glyph, plfont);
if (vertical != GS_NO_GLYPH) {
glyph = vertical;
}
}
/* now check for rotation. This is the ringer, fonts with
escapement 1 em get rotated. If you hold an HP
engineer's head close to your ear you can hear the
ocean. */
if ((pfont->WMode & 1) && sbw[2] == 1.0) {
/* save the ctm */
gs_currentmatrix(pgs, &save_ctm);
ctm_modified = true;
/* magic numbers - we don't completely understand
the translation magic used by HP. This provides a
good approximation */
gs_translate(pgs, 1.0 / 1.15, -(1.0 - 1.0 / 1.15));
gs_rotate(pgs, 90);
}
}
/*
* If we want pseudo-bold, render untransformed to an intermediate
* bitmap, smear it, and then transform it to produce the output.
* This is really messy.
*/
if (bold_fraction == 0) {
code = tt_set_cache(penum, pgs, w2);
if (code < 0)
return code;
bold_added = 0;
} else {
gs_matrix mat, smat;
gs_rect sbox;
code = gs_gsave(pgs);
if (code < 0)
return code;
gs_currentmatrix(pgs, &mat);
/* Determine an appropriate scale for the bitmap. */
scale = max(fabs(mat.xx) + fabs(mat.yx), fabs(mat.xy) + fabs(mat.yy));
gs_make_scaling(scale, scale, &smat);
sbox.p.x = w2[2], sbox.p.y = w2[3];
sbox.q.x = w2[4], sbox.q.y = w2[5];
code = gs_bbox_transform(&sbox, &smat, &sbox);
if (code < 0)
return code;
ipx = (int)sbox.p.x, ipy = (int)sbox.p.y;
iqx = (int)ceil(sbox.q.x), iqy = (int)ceil(sbox.q.y);
/* Set up the memory device for the bitmap. NB should check code. */
code = gs_make_mem_mono_device_with_copydevice(&pmdev, pgs->memory, pgs->device);
if (code < 0)
return code;
bold_device_created = true;
/* due to rounding, bold added (integer) can be zero while
bold fraction (float) is non zero in which case we add
1 scan line. We do not "0" bold simply because it is
inconvenient to back out at this point. We don't have
any HP tests which would show measurable difference
either way (0 or 1). */
bold_added = max((int)(scale * bold_fraction * 2 + 0.5), 1);
pmdev->width = iqx - ipx + bold_added;
pmdev->height = iqy - ipy;
pmdev->bitmap_memory = pgs->memory;
code = (*dev_proc(pmdev, open_device)) ((gx_device *) pmdev);
if (code < 0) {
gs_grestore(pgs);
return code;
}
/* NB we have no idea why opening the device doesn't set
the is_open flag, but this meme is repeated in various
parts of the code, if it isn't done closing the memory
device will not have no effect (release bitmap and
mask). */
pmdev->is_open = true;
/* Don't allow gs_setdevice to reset things. */
gx_set_device_only(pgs, (gx_device *) pmdev);
{
gs_fixed_rect cbox;
cbox.p.x = cbox.p.y = fixed_0;
cbox.q.x = int2fixed(pmdev->width);
cbox.q.y = int2fixed(pmdev->height);
code = gx_clip_to_rectangle(pgs, &cbox);
if (code < 0)
return code;
}
/* Make sure we clear the entire bitmap. */
memset(pmdev->base, 0, (size_t)bitmap_raster(pmdev->width) * pmdev->height);
code = gx_set_device_color_1(pgs); /* write 1's */
if (code < 0)
return code;
smat.tx = (float)(-ipx);
smat.ty = (float)(-ipy);
gs_setmatrix(pgs, &smat);
}
code = gs_type42_append(glyph, pgs, pgs->path,
(gs_text_enum_t *) penum, pfont,
gs_show_in_charpath(penum) != cpm_show);
if (code >= 0) {
/* Save the current value of fill adjust and use the
special value of -1 to indicate dropout prevention
should be enabled, later restore the old fill adjust
value. Similar code for the PDF and PS interpreter is
in zchar42.c */
gs_fixed_point fa = pgs->fill_adjust;
pgs->fill_adjust.x = pgs->fill_adjust.y = -1;
code = (pfont->PaintType ? gs_stroke(pgs) : gs_fill(pgs));
pgs->fill_adjust = fa;
}
if (ctm_modified)
gs_setmatrix(pgs, &save_ctm);
if (bold_added && (code >= 0))
code = gs_grestore(pgs);
if (code < 0 || !bold_added)
return (code < 0 ? code : 0);
/* Now smear the bitmap and copy it to the destination. */
{
gs_image_t image;
gs_image_enum *ienum =
gs_image_enum_alloc(pgs->memory, "pl_tt_build_char");
byte *bold_lines =
alloc_bold_lines(pgs->memory, pmdev->width - bold_added,
bold_added,
"pl_tt_build_char(bold_lines)");
if (ienum == 0 || bold_lines == 0) {
code = gs_note_error(gs_error_VMerror);
goto out;
}
gs_image_t_init_mask(&image, true);
image.Width = pmdev->width;
image.Height = pmdev->height + bold_added;
gs_make_scaling(scale, scale, &image.ImageMatrix);
image.ImageMatrix.tx = (float)(-ipx);
image.ImageMatrix.ty = (float)(-ipy);
image.adjust = true;
code = gs_setcharwidth(penum, pgs, w2[0], w2[1]);
if (code < 0)
goto out;
code = pl_image_bitmap_char(ienum, &image, pmdev->base,
bitmap_raster(pmdev->width), bold_added,
bold_lines, pgs);
out:if (bold_device_created)
gx_device_retain((gx_device *) pmdev, false);
gs_free_object(pgs->memory, bold_lines,
"pl_tt_build_char(bold_lines)");
gs_free_object(pgs->memory, ienum, "pl_tt_build_char(image enum)");
}
return (code < 0 ? code : 0);
#undef pfont42
#undef pbfont
}
/* We don't have to do any character encoding, since Intellifonts are */
/* indexed by character code (if bound) or MSL code (if unbound). */
static gs_glyph
pl_intelli_encode_char(gs_font * pfont, gs_char pchr,
gs_glyph_space_t not_used)
{
return (gs_glyph) pchr;
}
/* Define the structure of the Intellifont metrics. */
typedef struct intelli_metrics_s
{
byte charSymbolBox[4][2];
byte charEscapementBox[4][2];
byte halfLine[2];
byte centerline[2];
} intelli_metrics_t;
/* Merge the bounding box of a character into the composite box, */
/* and set the escapement. Return true if the character is defined. */
static bool
pl_intelli_merge_box(float wbox[6], const pl_font_t * plfont, gs_glyph glyph)
{
const byte *cdata = pl_font_lookup_glyph(plfont, glyph)->data;
if (cdata == 0)
return false;
wbox[1] = 0;
if (cdata[3] == 4) { /* Compound character. Merge the component boxes; */
/* use the compound character's escapement. */
bool found = false;
uint i;
for (i = 0; i < cdata[6]; ++i)
found |= pl_intelli_merge_box(wbox, plfont,
pl_get_uint16(cdata + 8 + i * 6));
wbox[0] = (float)pl_get_int16(cdata + 4);
return found;
}
/* Non-compound character. */
cdata += 4; /* skip PCL character header */
{
const intelli_metrics_t *metrics =
(const intelli_metrics_t *)(cdata + pl_get_uint16(cdata + 2));
int llx = pl_get_int16(metrics->charSymbolBox[0]);
int lly = pl_get_int16(metrics->charSymbolBox[1]);
int urx = pl_get_int16(metrics->charSymbolBox[2]);
int ury = pl_get_int16(metrics->charSymbolBox[3]);
wbox[0] = (float)(pl_get_int16(metrics->charEscapementBox[2]) -
pl_get_int16(metrics->charEscapementBox[0]));
wbox[2] = min(wbox[2], llx);
wbox[3] = min(wbox[3], lly);
wbox[4] = max(wbox[4], urx);
wbox[5] = max(wbox[5], ury);
}
return true;
}
/* Do the work for rendering an Intellifont character. */
/* The caller has done the setcachedevice. */
static int
pl_intelli_show_char(gs_gstate * pgs, const pl_font_t * plfont, gs_glyph glyph)
{
int code;
const byte *cdata, *cdata_end;
pl_font_glyph_t *font_glyph;
const intelli_metrics_t *metrics;
int *xBuffer = NULL, *yBuffer = NULL;
client_name_t cname = (client_name_t) "pl_intelli_show_char";
font_glyph = pl_font_lookup_glyph(plfont, glyph);
cdata = font_glyph->data;
cdata_end = cdata + font_glyph->data_len;
if (cdata == 0) {
if_debug1m('1', pgs->memory, "[1] no character data for glyph %ld\n",
glyph);
return 0;
}
if (cdata[3] == 4) { /* Compound character */
gs_matrix save_ctm;
int i;
gs_currentmatrix(pgs, &save_ctm);
for (i = 0; i < cdata[6]; ++i) {
const byte *edata = cdata + 8 + i * 6;
double x_offset = pl_get_int16(edata + 2);
double y_offset = pl_get_int16(edata + 4);
gs_translate(pgs, x_offset, y_offset);
code = pl_intelli_show_char(pgs, plfont, pl_get_uint16(edata));
gs_setmatrix(pgs, &save_ctm);
if (code < 0)
return code;
}
return 0;
}
/* compound character */
/* not compound character */
{
const byte *outlines;
uint num_loops;
uint i;
cdata += 4; /* skip PCL character header */
outlines = cdata + pl_get_uint16(cdata + 6);
if (outlines >= cdata_end)
return 0;
num_loops = pl_get_uint16(outlines);
if_debug2m('1', pgs->memory, "[1]ifont glyph %lu: loops=%u\n",
(ulong) glyph, num_loops);
if (num_loops == 0)
return -1;
for (i = 0; i < num_loops; ++i) {
const byte *xyc = cdata + pl_get_uint16(outlines + 4 + i * 8);
uint num_points;
uint num_aux_points;
const byte *x_coords, *y_coords, *x_coords_last;
const byte *x_aux_coords, *y_aux_coords, *x_aux_coords_last;
int llx, lly, urx, ury; /* character bounding box */
int x, y;
int xAux, yAux;
int *xScan, *yScan, *xLast;
int pointBufferSize;
uint sz;
if ((outlines + 4 + i * 8) >= cdata_end) {
code = gs_note_error(gs_error_invalidfont);
break;
}
num_points = pl_get_uint16(xyc);
num_aux_points = pl_get_uint16(xyc + 2);
x_coords = xyc + 4;
y_coords = x_coords + num_points * 2;
x_coords_last = y_coords;
if (x_coords_last >= cdata_end
|| (y_coords + num_points * 2) >= cdata_end) {
code = 0;
break;
}
metrics =
(const intelli_metrics_t *)(cdata + pl_get_uint16(cdata + 2));
llx = pl_get_int16(metrics->charSymbolBox[0]);
lly = pl_get_int16(metrics->charSymbolBox[1]);
urx = pl_get_int16(metrics->charSymbolBox[2]);
ury = pl_get_int16(metrics->charSymbolBox[3]);
pointBufferSize = num_points; /* allocate enough to hold all points */
if (num_aux_points != 0xffff) {
pointBufferSize += num_aux_points;
x_aux_coords = y_coords + num_points * 2;
y_aux_coords = x_aux_coords + num_aux_points;
x_aux_coords_last = y_coords;
if ((y_aux_coords + num_aux_points * 2) >= cdata_end) {
if (x_aux_coords_last >= cdata_end) {
pointBufferSize -= num_aux_points;
num_aux_points = 0xffff;
x_aux_coords = NULL;
y_aux_coords = NULL;
x_aux_coords_last = NULL;
}
else {
/* if we don't have enough data for all the declared points
* use as much as we have.
*/
pointBufferSize -= num_aux_points;
num_aux_points = (cdata_end - y_aux_coords) / 2;
pointBufferSize += num_aux_points;
x_aux_coords_last = x_aux_coords + num_aux_points;
}
}
} else {
x_aux_coords = NULL;
y_aux_coords = NULL;
x_aux_coords_last = NULL;
}
sz = pointBufferSize * sizeof(int);
if (i == 0) {
xBuffer = (int *)gs_alloc_bytes(pgs->memory, sz, cname);
yBuffer = (int *)gs_alloc_bytes(pgs->memory, sz, cname);
} else {
/* NB we don't have a font that tests this yet */
xBuffer =
(int *)gs_resize_object(pgs->memory, xBuffer, sz, cname);
yBuffer =
(int *)gs_resize_object(pgs->memory, yBuffer, sz, cname);
}
if (xBuffer == NULL || yBuffer == NULL) {
if (xBuffer != NULL)
gs_free_object(pgs->memory, xBuffer, "x point buffer");
if (yBuffer != NULL)
gs_free_object(pgs->memory, yBuffer, "y point buffer");
return_error(gs_error_VMerror);
}
xLast = NULL;
if_debug2m('1', pgs->memory,
"[1]num_points=%u num_aux_points=%u\n", num_points,
num_aux_points);
/* collect the points in the buffers, since we need to clean them up later */
/* only points inside the bounding box are allowed */
/* aux points are points inserted between two points, making the outline smoother */
/* the aux points could be used for curve fitting, but we add line segments */
for (xScan = xBuffer, yScan = yBuffer; x_coords < x_coords_last;
x_coords += 2, y_coords += 2) {
x = pl_get_uint16(x_coords) & 0x3fff;
y = pl_get_uint16(y_coords) & 0x3fff;
if_debug4m('1', pgs->memory, "[1]%s (%d,%d) %s\n",
(*x_coords & 0x80 ? " line" : "curve"), x, y,
(*y_coords & 0x80 ? " line" : "curve"));
if (xScan > xBuffer) { /* not first point, therefore aux is possible */
if (x_aux_coords < x_aux_coords_last && !(*x_coords & 0x80)) { /* use an aux point */
/* The auxiliary dx and dy values are signed. */
int dx = (*x_aux_coords++ ^ 0x80) - 0x80;
int dy = (*y_aux_coords++ ^ 0x80) - 0x80;
if_debug2m('1', pgs->memory, "[1]... aux (%d,%d)\n",
dx, dy);
xAux = (x + *(xScan - 1)) / 2 + dx;
yAux = (y + *(yScan - 1)) / 2 + dy;
if ((xAux >= llx && xAux <= urx) && (yAux >= lly && yAux <= ury)) { /* aux point is inside bounding box */
*xScan++ = xAux;
*yScan++ = yAux;
}
/* end point inside bounding box */
/* what do points outside the bounding box mean? */
} /* use an aux point */
}
/* not first point */
if ((x >= llx && x <= urx) && (y >= lly && y <= ury)) { /* point inside bounding box */
*xScan++ = x;
*yScan++ = y;
} /* point inside bounding box */
} /* for num_points - first time through */
if (num_aux_points != 0xffff)
xLast = xScan;
else
xLast = xScan - 1; /* discard the last point */
xScan = xBuffer;
yScan = yBuffer;
if (xLast > xBuffer) {
code = gs_moveto(pgs, (double) * xScan++, (double) * yScan++);
if (code < 0)
goto cleanup;
}
for (; xScan < xLast;) {
code = gs_lineto(pgs, (double) * xScan++, (double) * yScan++);
if (code < 0)
goto cleanup;
}
/* close the path of this loop */
code = gs_closepath(pgs);
if (code < 0)
break;
} /* for num_loops */
cleanup:
gs_free_object(pgs->memory, xBuffer, "x point buffer");
gs_free_object(pgs->memory, yBuffer, "y point buffer");
} /* end not compound */
return code;
}
/* Get character existence and escapement for an Intellifont. */
static int
pl_intelli_char_width(const pl_font_t * plfont, const void *pgs,
gs_char char_code, gs_point * pwidth)
{
const byte *cdata = pl_font_lookup_glyph(plfont, char_code)->data;
int wx;
if (!pwidth)
return (cdata == 0 ? 1 : 0);
if (cdata == 0) {
pwidth->x = pwidth->y = 0;
return 1;
}
switch (cdata[3]) {
case 3: /* non-compound character */
cdata += 4; /* skip PCL character header */
{
const intelli_metrics_t *metrics =
(const intelli_metrics_t *)(cdata +
pl_get_uint16(cdata + 2));
wx = pl_get_int16(metrics->charEscapementBox[2]) -
pl_get_int16(metrics->charEscapementBox[0]);
}
break;
case 4: /* compound character */
wx = pl_get_int16(cdata + 4);
break;
default: /* shouldn't happen */
pwidth->x = pwidth->y = 0;
return 0;
}
pwidth->x = (double) wx / 8782.0;
return 0;
}
static int
pl_intelli_char_metrics(const pl_font_t * plfont, const void *pgs,
gs_char char_code, float metrics[4])
{
gs_point width;
const byte *cdata = pl_font_lookup_glyph(plfont, char_code)->data;
metrics[0] = metrics[1] = metrics[2] = metrics[3] = 0;
if (cdata == 0) {
return 1;
}
/* compound */
if (cdata[3] == 4) {
dmprintf(plfont->pfont->memory,
"warning compound intellifont metrics not supported");
return 0;
}
cdata += 4;
{
const intelli_metrics_t *intelli_metrics =
(const intelli_metrics_t *)(cdata + pl_get_uint16(cdata + 2));
/* NB probably not right */
/* never a vertical substitute, doesn't yet handle compound characters */
metrics[0] = (float)pl_get_int16(intelli_metrics->charSymbolBox[0]);
metrics[0] /= 8782.0;
pl_intelli_char_width(plfont, pgs, char_code, &width);
metrics[2] = width.x;
return 0;
}
}
/* Render a character for an Intellifont. */
static int
pl_intelli_build_char(gs_show_enum * penum, gs_gstate * pgs, gs_font * pfont,
gs_char chr, gs_glyph glyph)
{
const pl_font_t *plfont = (const pl_font_t *)pfont->client_data;
float wbox[6];
int code;
wbox[0] = wbox[1] = 0;
wbox[2] = wbox[3] = 65536.0;
wbox[4] = wbox[5] = -65536.0;
if (!pl_intelli_merge_box(wbox, plfont, glyph)) {
wbox[2] = wbox[3] = wbox[4] = wbox[5] = 0;
code = gs_setcachedevice(penum, pgs, wbox);
return (code < 0 ? code : 0);
}
code = gs_setcachedevice(penum, pgs, wbox);
if (code < 0)
return code;
code = pl_intelli_show_char(pgs, plfont, glyph);
if (code < 0)
return code;
/* Since we don't take into account which side of the loops is */
/* outside, we take the easy way out.... */
code = gs_eofill(pgs);
return (code < 0 ? code : 0);
}
/* ---------------- Internal initialization ---------------- */
/* Initialize the procedures for a bitmap font. */
void
pl_bitmap_init_procs(gs_font_base * pfont)
{
pfont->procs.encode_char = pl_bitmap_encode_char;
pfont->procs.build_char = pl_bitmap_build_char;
#define plfont ((pl_font_t *)pfont->client_data)
plfont->char_width = pl_bitmap_char_width;
plfont->char_metrics = pl_bitmap_char_metrics;
#undef plfont
}
/* Initialize the procedures for a TrueType font. */
void
pl_tt_init_procs(gs_font_type42 * pfont)
{
pfont->procs.encode_char = pl_tt_encode_char;
pfont->procs.build_char = pl_tt_build_char;
pfont->data.string_proc = pl_tt_string_proc;
#define plfont ((pl_font_t *)pfont->client_data)
plfont->char_width = pl_tt_char_width;
plfont->char_metrics = pl_tt_char_metrics;
#undef plfont
}
static uint
pl_tt_get_glyph_index(gs_font_type42 * pfont42, gs_glyph glyph)
{
/* identity */
return glyph;
}
/* Finish initializing a TrueType font. */
void
pl_tt_finish_init(gs_font_type42 * pfont, bool downloaded)
{
float upem = (float)pfont->data.unitsPerEm;
ulong head = tt_find_table(pfont, "head", NULL);
const byte *hdata;
pfont->data.get_glyph_index = pl_tt_get_glyph_index;
if (downloaded)
pfont->data.get_outline = pl_tt_get_outline;
/* Set the FontBBox. */
access(head, 44, hdata);
pfont->FontBBox.p.x = pl_get_int16(hdata + 36) / upem;
pfont->FontBBox.p.y = pl_get_int16(hdata + 38) / upem;
pfont->FontBBox.q.x = pl_get_int16(hdata + 40) / upem;
pfont->FontBBox.q.y = pl_get_int16(hdata + 42) / upem;
#ifdef DEBUG
if (gs_debug_c('m')) {
const byte *OffsetTable;
uint numTables;
const byte *TableDirectory;
uint i;
access(0, 12, OffsetTable);
numTables = pl_get_uint16(OffsetTable + 4);
access(12, numTables * 16, TableDirectory);
for (i = 0; i < numTables; ++i) {
const byte *tab = TableDirectory + i * 16;
dmprintf6(pfont->memory,
"%c%c%c%c offset = %lu length = %lu\n",
tab[0], tab[1], tab[2], tab[3],
(ulong) pl_get_uint32(tab + 8),
(ulong) pl_get_uint32(tab + 12));
}
}
#endif
/* override default get metrics */
pfont->data.get_metrics = pl_tt_get_metrics;
}
void
pl_intelli_init_procs(gs_font_base * pfont)
{
pfont->procs.encode_char = pl_intelli_encode_char;
pfont->procs.build_char = pl_intelli_build_char;
#define plfont ((pl_font_t *)pfont->client_data)
plfont->char_width = pl_intelli_char_width;
plfont->char_metrics = pl_intelli_char_metrics;
#undef plfont
}
/* ---------------- Public procedures ---------------- */
/* Allocate the glyph table. */
int
pl_font_alloc_glyph_table(pl_font_t * plfont, uint num_glyphs,
gs_memory_t * mem, client_name_t cname)
{
uint size = num_glyphs + (num_glyphs >> 2) + 5;
pl_font_glyph_t *glyphs =
gs_alloc_struct_array(mem, size, pl_font_glyph_t,
&st_pl_font_glyph_element, cname);
if (glyphs == 0)
return_error(gs_error_VMerror);
{
uint i;
for (i = 0; i < size; ++i)
glyphs[i].glyph = 0, glyphs[i].data = 0;
}
plfont->glyphs.table = glyphs;
plfont->glyphs.used = 0;
plfont->glyphs.limit = num_glyphs;
plfont->glyphs.size = size;
plfont->glyphs.skip = size * 2 / 3;
while (igcd(plfont->glyphs.skip, size) > 1)
plfont->glyphs.skip++;
return 0;
}
/* Expand the glyph table. */
static int
expand_glyph_table(pl_font_t * plfont, gs_memory_t * mem)
{
pl_glyph_table_t old_table;
int code;
uint i;
old_table = plfont->glyphs;
code = pl_font_alloc_glyph_table(plfont, old_table.size, mem,
"expand_glyph_table(new table)");
if (code < 0)
return code;
for (i = 0; i < old_table.size; ++i)
if (old_table.table[i].data)
*pl_font_lookup_glyph(plfont, old_table.table[i].glyph) =
old_table.table[i];
gs_free_object(mem, old_table.table, "expand_glyph_table(old table)");
plfont->glyphs.used = old_table.used;
return 0;
}
/* Allocate the TrueType character to glyph index map. */
int
pl_tt_alloc_char_glyphs(pl_font_t * plfont, uint num_chars, gs_memory_t * mem,
client_name_t cname)
{
uint size = num_chars + (num_chars >> 2) + 5;
pl_tt_char_glyph_t *char_glyphs = (pl_tt_char_glyph_t *)
gs_alloc_byte_array(mem, size, sizeof(pl_tt_char_glyph_t), cname);
if (char_glyphs == 0)
return_error(gs_error_VMerror);
{
uint i;
for (i = 0; i < size; ++i)
char_glyphs[i].chr = gs_no_char, char_glyphs[i].glyph = 0;
}
plfont->char_glyphs.table = char_glyphs;
plfont->char_glyphs.used = 0;
plfont->char_glyphs.limit = num_chars;
plfont->char_glyphs.size = size;
plfont->char_glyphs.skip = size * 2 / 3;
while (igcd(plfont->char_glyphs.skip, size) > 1)
plfont->char_glyphs.skip++;
return 0;
}
/* Expand the character to glyph index map. */
static int
expand_char_glyph_table(pl_font_t * plfont, gs_memory_t * mem)
{
pl_tt_char_glyph_table_t old_table;
int code;
uint i;
old_table = plfont->char_glyphs;
code = pl_tt_alloc_char_glyphs(plfont, old_table.size, mem,
"expand_char_glyphs(new table)");
if (code < 0)
return code;
for (i = 0; i < old_table.size; ++i)
if (old_table.table[i].chr != gs_no_char)
*pl_tt_lookup_char(plfont, old_table.table[i].chr) =
old_table.table[i];
gs_free_object(mem, old_table.table, "expand_char_glyphs(old table)");
plfont->char_glyphs.used = old_table.used;
return 0;
}
/* Add a glyph to a font. Return -1 if the table is full. */
typedef struct font_glyph_s
{
gs_font *font;
gs_glyph glyph;
} font_glyph_t;
static bool
match_font_glyph(const gs_memory_t * mem, cached_char * cc, void *vpfg)
{
const font_glyph_t *pfg = vpfg;
return (cc->pair->font == pfg->font && cc->code == pfg->glyph);
}
int
pl_font_add_glyph(pl_font_t * plfont, gs_glyph glyph, const byte * cdata, const int cdata_len)
{
gs_font *pfont = plfont->pfont;
gs_glyph key = glyph;
pl_tt_char_glyph_t *ptcg = 0;
pl_font_glyph_t *pfg;
/*
* If this is a downloaded TrueType font, the "glyph" is actually
* a character code, and the actual TrueType glyph index is in the
* character header. In this case, the character data must be either
* a PCL5 format 15 or a PCL XL format 1 downloaded character.
*/
tcg:if (plfont->char_glyphs.table) {
ptcg = pl_tt_lookup_char(plfont, key);
if (ptcg->chr == gs_no_char && plfont->char_glyphs.used >= plfont->char_glyphs.limit) { /* Table is full, try to expand it. */
int code = expand_char_glyph_table(plfont, pfont->memory);
if (code < 0)
return code;
goto tcg;
}
/* get glyph id from character download */
if (cdata[0] == 1)
/* pxl truetype format 1,
* class 0 at offset 4, class 1 at offset 8 or class 2 at 10 */
key =
pl_get_uint16(cdata +
((cdata[1] ==
0) ? 4 : ((cdata[1] == 1) ? 8 : 10)));
else
/* pcl truetype format 15 */
key = pl_get_uint16(cdata + cdata[2] + 4);
}
fg:pfg = pl_font_lookup_glyph(plfont, key);
if (pfg->data != 0) { /* Remove the glyph and a possible cached representation. */
font_glyph_t match_fg;
match_fg.font = pfont;
match_fg.glyph = key;
gx_purge_selected_cached_chars(pfont->dir, match_font_glyph,
&match_fg);
/* replacing a read only glyph nothing we can do, so return. */
if (plfont->data_are_permanent)
return 0;
gs_free_object(pfont->memory, (void *)pfg->data,
"pl_font_add_glyph(old data)");
} else {
if (plfont->glyphs.used >= plfont->glyphs.limit) { /* Table is full, try to expand it. */
int code = expand_glyph_table(plfont, pfont->memory);
if (code < 0)
return code;
goto fg;
}
plfont->glyphs.used++;
}
if (ptcg) {
if (ptcg->chr == gs_no_char)
plfont->char_glyphs.used++;
ptcg->chr = glyph;
ptcg->glyph = key;
}
pfg->glyph = key;
pfg->data = cdata;
pfg->data_len = cdata_len;
return 0;
}
static bool
is_composite(const byte * pgdata)
{
return (pl_get_int16(pgdata) == -1);
}
int
pl_font_disable_composite_metrics(pl_font_t * plfont, gs_glyph glyph)
{
gs_glyph key = glyph;
gs_font_type42 *pfont = (gs_font_type42 *) plfont->pfont;
gs_glyph_data_t glyph_data;
int code;
/* This is the unusual way of looking up a glyph thanks to the pcl
font wrapper format. It is documented in several other places
in this file. If a char_glyphs table is not available it is
not a downloadedd TT font wrapper so we do nothing. */
if (plfont->char_glyphs.table) {
pl_tt_char_glyph_t *ptcg = pl_tt_lookup_char(plfont, key);
if (ptcg->chr == gs_no_char)
return 0;
key = ptcg->glyph;
} else {
return -1;
}
/* should never fail as this procedure is called only after a
glyph has been successfully added. */
code = pfont->data.get_outline(pfont, key, &glyph_data);
if (code < 0)
return code;
/* null glyph */
if (glyph_data.bits.data == 0)
return 0;
/* the glyph is guaranteed by the langauges to be have a reasonable
header (enough to test for a composite glyph and do the tests
below for components) so a UMR or overflow is not possible but
it would be better to add checks. The component parser below */
if (!is_composite(glyph_data.bits.data))
return 0;
/* enumerate the components and rewrite the flags field to not use
metrics from the component. Similar to the enumeration code in
gstype42.c */
{
uint flags;
byte *next_component = (byte *) glyph_data.bits.data + 10;
do {
gs_matrix_fixed mat;
byte *last_component = next_component;
memset(&mat, 0, sizeof(mat)); /* arbitrary */
gs_type42_parse_component((const byte **)&next_component, &flags,
&mat, NULL,
(const gs_font_type42 *)plfont->pfont,
&mat);
if (flags & TT_CG_USE_MY_METRICS)
/* bit 9 of the flags is the "use my metrics" flag
which is bit 1 of byte 0 big endian wise */
last_component[0] &= ~(1 << 1);
} while (flags & TT_CG_MORE_COMPONENTS);
}
return 0;
}
/* Remove a glyph from a font. Return 1 if the glyph was present. */
int
pl_font_remove_glyph(pl_font_t * plfont, gs_glyph glyph)
{
gs_font *pfont = plfont->pfont;
gs_glyph key = glyph;
pl_font_glyph_t *pfg;
/* See above regarding downloaded TrueType fonts. */
if (plfont->char_glyphs.table) {
pl_tt_char_glyph_t *ptcg = pl_tt_lookup_char(plfont, key);
if (ptcg->chr == gs_no_char)
return 0;
key = ptcg->glyph;
ptcg->chr = gs_no_char;
ptcg->glyph = 1; /* mark as deleted */
plfont->char_glyphs.used--;
/* we have to clear out the widths cache now */
pl_font_glyph_width_cache_remove_nodes(plfont);
}
/* may not have a glyph table in case of cloned resident */
if (plfont->glyphs.table == 0)
return 0;
pfg = pl_font_lookup_glyph(plfont, key);
if (pfg->data == 0)
return 0; /* character not defined */
{ /* Remove the glyph from the character cache. */
font_glyph_t match_fg;
match_fg.font = pfont;
match_fg.glyph = key;
gx_purge_selected_cached_chars(pfont->dir, match_font_glyph,
&match_fg);
gs_free_object(pfont->memory, (void *)pfg->data,
"pl_font_remove_glyph(data)");
}
pfg->data = 0;
pfg->glyph = 1; /* mark as deleted */
plfont->glyphs.used--;
/* we have to clear out the widths cache now */
pl_font_glyph_width_cache_remove_nodes(plfont);
return 1;
}
|