Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/Document/Dictionary/DictionaryValue/Array/ArrayValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ public static function fromValue(string $valueString): self|ReferenceValueArray|
return null;
}

$sanitizedValueString = substr($sanitizedValueString, 1, -1);
$sanitizedValueString = preg_replace('/(<[^>]*>)(?=<[^>]*>)/', '$1 $2', $sanitizedValueString)
?? throw new RuntimeException('An error occurred while sanitizing array value');
$sanitizedValueString = str_replace(['/', "\n"], [' /', ' '], rtrim(ltrim($sanitizedValueString, '[ '), ' ]'));
$sanitizedValueString = str_replace(['/', "\n"], [' /', ' '], $sanitizedValueString);
$sanitizedValueString = preg_replace('/\s+/', ' ', $sanitizedValueString)
?? throw new RuntimeException('An error occurred while removing duplicate spaces from array value');

Expand Down
37 changes: 16 additions & 21 deletions src/Document/Dictionary/DictionaryValue/Array/CIDFontWidths.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use PrinsFrank\PdfParser\Document\Dictionary\DictionaryValue\Array\Item\ConsecutiveCIDWidth;
use PrinsFrank\PdfParser\Document\Dictionary\DictionaryValue\Array\Item\RangeCIDWidth;
use PrinsFrank\PdfParser\Document\Dictionary\DictionaryValue\DictionaryValue;
use PrinsFrank\PdfParser\Exception\RuntimeException;

/** @see 9.7.4.3 Glyph metrics in CIDFonts */
class CIDFontWidths implements DictionaryValue {
Expand Down Expand Up @@ -37,37 +36,33 @@ public static function fromValue(string $valueString): ?self {
return new self();
}

if (preg_match_all('/(?<startingCID>[0-9]+)\s*(?<CIDS>[0-9]+\s*[0-9.]+|\[[0-9. ]+\])/', $valueString, $matches, PREG_SET_ORDER) <= 0) {
if (($arrayValue = ArrayValue::fromValue($valueString)) instanceof ArrayValue === false) {
return null;
}

$widths = [];
foreach ($matches as $match) {
if ((string) ($startingCID = (int) $match['startingCID']) !== $match['startingCID']) {
$nrOfTopLevelItems = count($arrayValue->value);
for ($i = 0; $i < $nrOfTopLevelItems; $i++) {
$item = $arrayValue->value[$i];
if (is_int($item) === false) {
return null;
}

if (str_starts_with($match['CIDS'], '[') && str_ends_with($match['CIDS'], ']')) {
$cidWidths = preg_split('/\s+/', trim(rtrim(ltrim($match['CIDS'], '['), ']')), -1, PREG_SPLIT_NO_EMPTY);
if ($cidWidths === false) {
throw new RuntimeException('Something went wrong while splitting');
if (($nextItem = $arrayValue->value[$i + 1] ?? null) instanceof ArrayValue) {
foreach ($nextItem->value as $itemValue) {
if (is_string($itemValue) === false && is_int($itemValue) === false) {
return null;
}
}

$widths[] = new ConsecutiveCIDWidth($startingCID, array_map('floatval', $cidWidths));

continue;
}

$arguments = explode(' ', $match['CIDS']);
if (count($arguments) !== 2) {
$widths[] = new ConsecutiveCIDWidth($item, array_map(fn(string|int $value) => is_string($value) ? (float) $value : $value, $nextItem->value));
$i++;
} elseif (is_int($nextItem) && (is_string($secondNextItem = $arrayValue->value[$i + 2] ?? null) || is_int($secondNextItem))) {
$widths[] = new RangeCIDWidth($item, $nextItem, (float) $secondNextItem);
$i += 2;
} else {
return null;
}

if ((string) ($endCID = (int) $arguments[0]) !== $arguments[0] || (string) ($width = (float) $arguments[1]) !== $arguments[1]) {
return null;
}

$widths[] = new RangeCIDWidth($startingCID, $endCID, $width);
}

return new self(... $widths);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ public function testFromValue(): void {
new ArrayValue([42, 43]),
ArrayValue::fromValue('[42 43]'),
);
static::assertEquals(
new ArrayValue([new ArrayValue([42, 43])]),
ArrayValue::fromValue('[[42 43]]'),
);
static::assertEquals(
new ArrayValue([42, 43]),
ArrayValue::fromValue(' [42 43] '),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,21 @@ public function testFromValueWithPaddedBrackets(): void {
);
}

public function testFromValueWithCompactBrackets(): void {
static::assertEquals(
new CIDFontWidths(
new ConsecutiveCIDWidth(3, [278]),
new ConsecutiveCIDWidth(18, [278]),
new ConsecutiveCIDWidth(46, [722]),
new ConsecutiveCIDWidth(72, [556]),
new ConsecutiveCIDWidth(80, [889]),
new RangeCIDWidth(81, 82, 611),
new ConsecutiveCIDWidth(88, [611]),
),
CIDFontWidths::fromValue('[3[278] 18[278] 46[722] 72[556] 80[889] 81 82 611 88[611]]'),
);
}

public function testFromValueAcceptsEmptyArray(): void {
static::assertEquals(
new CIDFontWidths(),
Expand Down
Loading