|
6 | 6 | use PrinsFrank\PdfParser\Document\Dictionary\DictionaryValue\Array\Item\ConsecutiveCIDWidth; |
7 | 7 | use PrinsFrank\PdfParser\Document\Dictionary\DictionaryValue\Array\Item\RangeCIDWidth; |
8 | 8 | use PrinsFrank\PdfParser\Document\Dictionary\DictionaryValue\DictionaryValue; |
9 | | -use PrinsFrank\PdfParser\Exception\RuntimeException; |
10 | 9 |
|
11 | 10 | /** @see 9.7.4.3 Glyph metrics in CIDFonts */ |
12 | 11 | class CIDFontWidths implements DictionaryValue { |
@@ -37,37 +36,33 @@ public static function fromValue(string $valueString): ?self { |
37 | 36 | return new self(); |
38 | 37 | } |
39 | 38 |
|
40 | | - if (preg_match_all('/(?<startingCID>[0-9]+)\s*(?<CIDS>[0-9]+\s*[0-9.]+|\[[0-9. ]+\])/', $valueString, $matches, PREG_SET_ORDER) <= 0) { |
| 39 | + if (($arrayValue = ArrayValue::fromValue($valueString)) instanceof ArrayValue === false) { |
41 | 40 | return null; |
42 | 41 | } |
43 | 42 |
|
44 | 43 | $widths = []; |
45 | | - foreach ($matches as $match) { |
46 | | - if ((string) ($startingCID = (int) $match['startingCID']) !== $match['startingCID']) { |
| 44 | + $nrOfTopLevelItems = count($arrayValue->value); |
| 45 | + for ($i = 0; $i < $nrOfTopLevelItems; $i++) { |
| 46 | + $item = $arrayValue->value[$i]; |
| 47 | + if (is_int($item) === false) { |
47 | 48 | return null; |
48 | 49 | } |
49 | 50 |
|
50 | | - if (str_starts_with($match['CIDS'], '[') && str_ends_with($match['CIDS'], ']')) { |
51 | | - $cidWidths = preg_split('/\s+/', trim(rtrim(ltrim($match['CIDS'], '['), ']')), -1, PREG_SPLIT_NO_EMPTY); |
52 | | - if ($cidWidths === false) { |
53 | | - throw new RuntimeException('Something went wrong while splitting'); |
| 51 | + if (($nextItem = $arrayValue->value[$i + 1] ?? null) instanceof ArrayValue) { |
| 52 | + foreach ($nextItem->value as $itemValue) { |
| 53 | + if (is_string($itemValue) === false && is_int($itemValue) === false) { |
| 54 | + return null; |
| 55 | + } |
54 | 56 | } |
55 | 57 |
|
56 | | - $widths[] = new ConsecutiveCIDWidth($startingCID, array_map('floatval', $cidWidths)); |
57 | | - |
58 | | - continue; |
59 | | - } |
60 | | - |
61 | | - $arguments = explode(' ', $match['CIDS']); |
62 | | - if (count($arguments) !== 2) { |
| 58 | + $widths[] = new ConsecutiveCIDWidth($item, array_map(fn(string|int $value) => is_string($value) ? (float) $value : $value, $nextItem->value)); |
| 59 | + $i++; |
| 60 | + } elseif (is_int($nextItem) && (is_string($secondNextItem = $arrayValue->value[$i + 2] ?? null) || is_int($secondNextItem))) { |
| 61 | + $widths[] = new RangeCIDWidth($item, $nextItem, (float) $secondNextItem); |
| 62 | + $i += 2; |
| 63 | + } else { |
63 | 64 | return null; |
64 | 65 | } |
65 | | - |
66 | | - if ((string) ($endCID = (int) $arguments[0]) !== $arguments[0] || (string) ($width = (float) $arguments[1]) !== $arguments[1]) { |
67 | | - return null; |
68 | | - } |
69 | | - |
70 | | - $widths[] = new RangeCIDWidth($startingCID, $endCID, $width); |
71 | 66 | } |
72 | 67 |
|
73 | 68 | return new self(... $widths); |
|
0 commit comments