Skip to content

Commit

Permalink
PDFBOX-5469: use inner wrapper class to transfer values
Browse files Browse the repository at this point in the history
git-svn-id: https://svn.apache.org/repos/asf/pdfbox/trunk@1917613 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
lehmi committed May 9, 2024
1 parent 865d482 commit 6a509f6
Showing 1 changed file with 31 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,13 @@ public Type2CharStringParser(String fontName)
public List<Object> parse(byte[] bytes, byte[][] globalSubrIndex, byte[][] localSubrIndex)
throws IOException
{
return parseSequence(bytes, globalSubrIndex, localSubrIndex, new ArrayList<>(), 0, 0);
GlyphData glyphData = new GlyphData();
parseSequence(bytes, globalSubrIndex, localSubrIndex, glyphData);
return glyphData.sequence;
}

private List<Object> parseSequence(byte[] bytes, byte[][] globalSubrIndex,
byte[][] localSubrIndex, List<Object> sequence, int hstemCount, int vstemCount)
private void parseSequence(byte[] bytes, byte[][] globalSubrIndex,
byte[][] localSubrIndex, GlyphData glyphData)
throws IOException
{
DataInput input = new DataInputByteArray(bytes);
Expand All @@ -73,45 +75,44 @@ private List<Object> parseSequence(byte[] bytes, byte[][] globalSubrIndex,
int b0 = input.readUnsignedByte();
if (b0 == CALLSUBR && localSubroutineIndexProvided)
{
processCallSubr(globalSubrIndex, localSubrIndex, localSubrIndex, sequence);
processCallSubr(globalSubrIndex, localSubrIndex, localSubrIndex, glyphData);
}
else if (b0 == CALLGSUBR && globalSubroutineIndexProvided)
{
processCallSubr(globalSubrIndex, localSubrIndex, globalSubrIndex, sequence);
processCallSubr(globalSubrIndex, localSubrIndex, globalSubrIndex, glyphData);
}
else if ( (b0 >= 0 && b0 <= 27) || (b0 >= 29 && b0 <= 31))
{
sequence.add(
readCommand(b0, input, countNumbers(sequence), hstemCount, vstemCount));
glyphData.sequence.add(readCommand(b0, input, glyphData));
}
else if (b0 == 28 || (b0 >= 32 && b0 <= 255))
{
sequence.add(readNumber(b0, input));
glyphData.sequence.add(readNumber(b0, input));
}
else
{
throw new IllegalArgumentException();
}
}
return sequence;
}

private void processCallSubr(byte[][] globalSubrIndex, byte[][] localSubrIndex,
byte[][] subrIndex, List<Object> sequence)
byte[][] subrIndex, GlyphData glyphData)
throws IOException
{
int subrNumber = calculateSubrNumber((Integer) sequence.remove(sequence.size() - 1),
int subrNumber = calculateSubrNumber(
(Integer) glyphData.sequence.remove(glyphData.sequence.size() - 1),
subrIndex.length);
if (subrNumber < subrIndex.length)
{
byte[] subrBytes = subrIndex[subrNumber];
parseSequence(subrBytes, globalSubrIndex, localSubrIndex, sequence, 0, 0);
Object lastItem = sequence.get(sequence.size() - 1);
parseSequence(subrBytes, globalSubrIndex, localSubrIndex, glyphData);
Object lastItem = glyphData.sequence.get(glyphData.sequence.size() - 1);
if (lastItem instanceof CharStringCommand
&& Type2KeyWord.RET == ((CharStringCommand) lastItem).getType2KeyWord())
{
// remove "return" command
sequence.remove(sequence.size() - 1);
glyphData.sequence.remove(glyphData.sequence.size() - 1);
}
}
}
Expand All @@ -129,26 +130,25 @@ private int calculateSubrNumber(int operand, int subrIndexlength)
return 32768 + operand;
}

private CharStringCommand readCommand(int b0, DataInput input, int numberCount, int hstemCount,
int vstemCount)
private CharStringCommand readCommand(int b0, DataInput input, GlyphData glyphData)
throws IOException
{
switch (b0)
{
case 1:
case 18:
hstemCount += numberCount / 2;
glyphData.hstemCount += countNumbers(glyphData.sequence) / 2;
return CharStringCommand.getInstance(b0);
case 3:
case 23:
vstemCount += numberCount / 2;
glyphData.vstemCount += countNumbers(glyphData.sequence) / 2;
return CharStringCommand.getInstance(b0);
case 12:
return CharStringCommand.getInstance(b0, input.readUnsignedByte());
case 19:
case 20:
vstemCount += numberCount / 2;
int[] value = new int[1 + getMaskLength(hstemCount, vstemCount)];
glyphData.vstemCount += countNumbers(glyphData.sequence) / 2;
int[] value = new int[1 + getMaskLength(glyphData.hstemCount, glyphData.vstemCount)];
value[0] = b0;

for (int i = 1; i < value.length; i++)
Expand Down Expand Up @@ -227,4 +227,15 @@ public String toString()
{
return fontName;
}

private class GlyphData
{
final List<Object> sequence = new ArrayList<>();
int hstemCount = 0;
int vstemCount = 0;;

private GlyphData()
{
}
}
}

0 comments on commit 6a509f6

Please sign in to comment.