Skip to content

Commit 99de1da

Browse files
committed
fix subset bug
1 parent 31d2178 commit 99de1da

3 files changed

Lines changed: 75 additions & 3 deletions

File tree

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2.7.0
1+
2.7.1

src/Subset.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,10 +243,16 @@ public function getSubsetFont(): string
243243
protected function getTableChecksum(string $table, int $length): int
244244
{
245245
$sum = 0;
246-
$tlen = ($length / 4);
246+
$tlen = (int) floor(($length + 3) / 4);
247247
$offset = 0;
248248
for ($idx = 0; $idx < $tlen; ++$idx) {
249-
$val = \unpack('Ni', \substr($table, $offset, 4));
249+
$chunk = \substr($table, $offset, 4);
250+
if (\strlen($chunk) < 4) {
251+
// OpenType checksums use zero-padding for trailing partial words.
252+
$chunk = \str_pad($chunk, 4, "\0", STR_PAD_RIGHT);
253+
}
254+
255+
$val = \unpack('Ni', $chunk);
250256
if ($val === false) {
251257
throw new FontException('Unable to unpack table data');
252258
}

test/SubsetTest.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
/**
4+
* SubsetTest.php
5+
*
6+
* @since 2026-05-01
7+
* @category Library
8+
* @package PdfFont
9+
* @author Nicola Asuni <info@tecnick.com>
10+
* @copyright 2011-2026 Nicola Asuni - Tecnick.com LTD
11+
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT)
12+
* @link https://github.com/tecnickcom/tc-lib-pdf-font
13+
*
14+
* This file is part of tc-lib-pdf-font software library.
15+
*/
16+
17+
namespace Test;
18+
19+
/**
20+
* Subset Test
21+
*
22+
* @since 2026-05-01
23+
* @category Library
24+
* @package PdfFont
25+
* @author Nicola Asuni <info@tecnick.com>
26+
* @copyright 2011-2026 Nicola Asuni - Tecnick.com LTD
27+
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT)
28+
* @link https://github.com/tecnickcom/tc-lib-pdf-font
29+
*/
30+
class SubsetTest extends TestUtil
31+
{
32+
public function testTableChecksumPadsTrailingBytes(): void
33+
{
34+
$subset = new class () extends \Com\Tecnick\Pdf\Font\Subset {
35+
public function __construct()
36+
{
37+
}
38+
39+
public function checksum(string $table, int $length): int
40+
{
41+
return $this->getTableChecksum($table, $length);
42+
}
43+
};
44+
45+
// 3-byte table must be zero-padded to a 4-byte word before summing.
46+
$this->assertSame(0x01020300, $subset->checksum("\x01\x02\x03", 3));
47+
}
48+
49+
public function testTableChecksumHandlesMixedFullAndPartialWords(): void
50+
{
51+
$subset = new class () extends \Com\Tecnick\Pdf\Font\Subset {
52+
public function __construct()
53+
{
54+
}
55+
56+
public function checksum(string $table, int $length): int
57+
{
58+
return $this->getTableChecksum($table, $length);
59+
}
60+
};
61+
62+
$table = "\x11\x22\x33\x44\x55\x66\x77";
63+
// 0x11223344 + 0x55667700, modulo 2^32
64+
$this->assertSame(0x6688AA44, $subset->checksum($table, 7));
65+
}
66+
}

0 commit comments

Comments
 (0)