Repository navigation
Refactor Type1 font parsing to use Type1Subroutines #737
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+198
−27
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
src/main/java/org/verapdf/pd/font/type1/Type1Subroutines.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| /* | ||
| * This file is part of veraPDF Parser, a module of the veraPDF project. | ||
| * Copyright (c) 2015-2026, veraPDF Consortium <info@verapdf.org> | ||
| * All rights reserved. | ||
| * | ||
| * veraPDF Parser is free software: you can redistribute it and/or modify | ||
| * it under the terms of either: | ||
| * | ||
| * The GNU General public license GPLv3+. | ||
| * You should have received a copy of the GNU General public license along | ||
| * with this program. If not, see https://www.gnu.org/licenses/gpl-3.0.en.html. | ||
| * | ||
| * The Mozilla Public License MPLv2+. | ||
| * You should have received a copy of the MPL license along with this program. | ||
| */ | ||
| package org.verapdf.pd.font.type1; | ||
|
|
||
| import org.verapdf.as.io.ASInputStream; | ||
| import org.verapdf.io.SeekableInputStream; | ||
| import org.verapdf.pd.font.CFFNumber; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.HashMap; | ||
| import java.util.HashSet; | ||
| import java.util.Map; | ||
| import java.util.Set; | ||
|
|
||
| final class Type1Subroutines { | ||
| private static final int MAX_SUBR_DEPTH = 100; | ||
|
|
||
| private final SeekableInputStream source; | ||
| private final Map<Integer, Subroutine> subroutines; | ||
| private final Map<Integer, CFFNumber> widths; | ||
| private final Set<Integer> subrsWithoutWidth; | ||
| private final Set<Integer> resolvingSubrs; | ||
| private int depth; | ||
|
|
||
| Type1Subroutines(SeekableInputStream source) { | ||
| this.source = source; | ||
| this.subroutines = new HashMap<>(); | ||
| this.widths = new HashMap<>(); | ||
| this.subrsWithoutWidth = new HashSet<>(); | ||
| this.resolvingSubrs = new HashSet<>(); | ||
| } | ||
|
|
||
| void addSubroutine(int number, long offset, long length, int lenIV) { | ||
| subroutines.put(number, new Subroutine(offset, length, lenIV)); | ||
| } | ||
|
|
||
| CFFNumber getWidth(int subrNumber) throws IOException { | ||
| CFFNumber width = widths.get(subrNumber); | ||
| if (width != null || subrsWithoutWidth.contains(subrNumber) || | ||
| resolvingSubrs.contains(subrNumber) || depth >= MAX_SUBR_DEPTH) { | ||
| return width; | ||
| } | ||
| Subroutine subroutine = subroutines.get(subrNumber); | ||
| if (subroutine == null) { | ||
| return null; | ||
| } | ||
| resolvingSubrs.add(subrNumber); | ||
| depth++; | ||
| try { | ||
| Type1CharStringParser parser = new Type1CharStringParser(subroutine.open(source), this); | ||
| width = parser.getWidth(); | ||
| if (width != null) { | ||
| widths.put(subrNumber, width); | ||
| } else { | ||
| subrsWithoutWidth.add(subrNumber); | ||
| } | ||
| return width; | ||
| } finally { | ||
| depth--; | ||
| resolvingSubrs.remove(subrNumber); | ||
| } | ||
| } | ||
|
|
||
| static final class Subroutine { | ||
| private final long offset; | ||
| private final long length; | ||
| private final int lenIV; | ||
|
|
||
| Subroutine(long offset, long length, int lenIV) { | ||
| this.offset = offset; | ||
| this.length = length; | ||
| this.lenIV = lenIV; | ||
| } | ||
|
|
||
| private ASInputStream open(SeekableInputStream source) throws IOException { | ||
| return new EexecFilterDecode(source.getStream(offset, length), true, lenIV); | ||
| } | ||
| } | ||
| } |
89 changes: 89 additions & 0 deletions
89
src/test/java/org/verapdf/pd/font/type1/Type1CharStringParserTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| /* | ||
| * This file is part of veraPDF Parser, a module of the veraPDF project. | ||
| * Copyright (c) 2015-2026, veraPDF Consortium <info@verapdf.org> | ||
| * All rights reserved. | ||
| * | ||
| * veraPDF Parser is free software: you can redistribute it and/or modify | ||
| * it under the terms of either: | ||
| * | ||
| * The GNU General public license GPLv3+. | ||
| * You should have received a copy of the GNU General public license along | ||
| * with this program. If not, see https://www.gnu.org/licenses/gpl-3.0.en.html. | ||
| * | ||
| * The Mozilla Public License MPLv2+. | ||
| * You should have received a copy of the MPL license along with this program. | ||
| */ | ||
| package org.verapdf.pd.font.type1; | ||
|
|
||
| import org.junit.jupiter.api.Assertions; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.verapdf.as.io.ASMemoryInStream; | ||
| import org.verapdf.io.SeekableInputStream; | ||
|
|
||
| import java.io.IOException; | ||
|
|
||
| class Type1CharStringParserTest { | ||
|
|
||
| @Test | ||
| void resolvesWidthThroughNestedSubrsRegardlessOfSubrOrder() throws IOException { | ||
| byte[] firstSubr = encrypt(charstring(number(2245), operator(10), operator(11))); | ||
| byte[] secondSubr = encrypt(charstring(number(0), number(239), operator(13), operator(11))); | ||
| byte[] sourceBytes = new byte[1 + firstSubr.length + secondSubr.length]; | ||
| System.arraycopy(firstSubr, 0, sourceBytes, 1, firstSubr.length); | ||
| System.arraycopy(secondSubr, 0, sourceBytes, 1 + firstSubr.length, secondSubr.length); | ||
| try (SeekableInputStream source = new ASMemoryInStream(sourceBytes)) { | ||
| Type1Subroutines subroutines = new Type1Subroutines(source); | ||
| subroutines.addSubroutine(2240, 1, firstSubr.length, 0); | ||
| subroutines.addSubroutine(2245, 1 + firstSubr.length, secondSubr.length, 0); | ||
|
|
||
| Type1CharStringParser parser = new Type1CharStringParser( | ||
| new ASMemoryInStream(charstring(number(2240), operator(10), operator(14))), | ||
| subroutines); | ||
|
|
||
| Assertions.assertEquals(239, parser.getWidth().getInteger()); | ||
| Type1CharStringParser cachedParser = new Type1CharStringParser( | ||
| new ASMemoryInStream(charstring(number(2240), operator(10), operator(14))), | ||
| subroutines); | ||
| Assertions.assertEquals(239, cachedParser.getWidth().getInteger()); | ||
| } | ||
| } | ||
|
|
||
| private static byte[] charstring(byte[]... parts) { | ||
| int length = 0; | ||
| for (byte[] part : parts) { | ||
| length += part.length; | ||
| } | ||
| byte[] result = new byte[length]; | ||
| int offset = 0; | ||
| for (byte[] part : parts) { | ||
| System.arraycopy(part, 0, result, offset, part.length); | ||
| offset += part.length; | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| private static byte[] number(int value) { | ||
| if (value >= -107 && value <= 107) { | ||
| return new byte[]{(byte) (value + 139)}; | ||
| } | ||
| return new byte[]{ | ||
| (byte) 255, (byte) (value >>> 24), (byte) (value >>> 16), | ||
| (byte) (value >>> 8), (byte) value | ||
| }; | ||
| } | ||
|
|
||
| private static byte[] operator(int value) { | ||
| return new byte[]{(byte) value}; | ||
| } | ||
|
|
||
| private static byte[] encrypt(byte[] cleartext) { | ||
| byte[] ciphertext = new byte[cleartext.length]; | ||
| int r = 4330; | ||
| for (int i = 0; i < cleartext.length; i++) { | ||
| int encoded = (cleartext[i] & 0xFF) ^ (r >> 8); | ||
| ciphertext[i] = (byte) encoded; | ||
| r = (encoded + r) * 52845 + 22719 & 0xFFFF; | ||
| } | ||
| return ciphertext; | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🗄️ Data Integrity & Integration | 🟠 Major | 🏗️ Heavy lift
🔎 Supported by static analysis
🏁 Script executed:
Repository: veraPDF/veraPDF-parser
Length of output: 12370
🏁 Script executed:
Repository: veraPDF/veraPDF-parser
Length of output: 41976
Preserve externally accessible constructor contracts.
If public and protected constructors are part of the library's supported API, retain the original overloads. The new
Type1Subroutinesconstructors do not preserve source compatibility for callers usingMap<Integer, CFFNumber>or subclasses using the six-argument constructor.Keep the new overloads for internal callers, and retain compatibility overloads with the original signatures and visibility.
📍 Affects 2 files
src/main/java/org/verapdf/pd/font/type1/Type1CharStringParser.java#L43-L45(this comment)src/main/java/org/verapdf/pd/font/type1/BaseCharStringParser.java#L79-L81🤖 Prompt for AI Agents