You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pdfbox.apache.org by le...@apache.org on 2020/11/01 14:11:38 UTC
svn commit: r1883049 - in
/pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/cff:
CharStringCommand.java Type1CharString.java Type1CharStringParser.java
Type2CharString.java Type2CharStringParser.java
Author: lehmi
Date: Sun Nov 1 14:11:37 2020
New Revision: 1883049
URL: http://svn.apache.org/viewvc?rev=1883049&view=rev
Log:
PDFBOX-4987: refactor
Modified:
pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/cff/CharStringCommand.java
pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/cff/Type1CharString.java
pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/cff/Type1CharStringParser.java
pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/cff/Type2CharString.java
pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/cff/Type2CharStringParser.java
Modified: pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/cff/CharStringCommand.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/cff/CharStringCommand.java?rev=1883049&r1=1883048&r2=1883049&view=diff
==============================================================================
--- pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/cff/CharStringCommand.java (original)
+++ pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/cff/CharStringCommand.java Sun Nov 1 14:11:37 2020
@@ -28,7 +28,7 @@ import java.util.Map;
public class CharStringCommand
{
- private Key commandKey = null;
+ private final Key commandKey;
/**
* Constructor with one value.
@@ -37,7 +37,7 @@ public class CharStringCommand
*/
public CharStringCommand(int b0)
{
- setKey(new Key(b0));
+ commandKey = new Key(b0);
}
/**
@@ -48,7 +48,7 @@ public class CharStringCommand
*/
public CharStringCommand(int b0, int b1)
{
- setKey(new Key(b0, b1));
+ commandKey = new Key(b0, b1);
}
/**
@@ -58,7 +58,7 @@ public class CharStringCommand
*/
public CharStringCommand(int[] values)
{
- setKey(new Key(values));
+ commandKey = new Key(values);
}
/**
@@ -70,9 +70,14 @@ public class CharStringCommand
return commandKey;
}
- private void setKey(Key key)
+ public Type1KeyWord getType1KeyWord()
{
- commandKey = key;
+ return Type1KeyWord.valueOfKey(commandKey);
+ }
+
+ public Type2KeyWord getType2KeyWord()
+ {
+ return Type2KeyWord.valueOfKey(commandKey);
}
/**
@@ -82,14 +87,14 @@ public class CharStringCommand
public String toString()
{
String str = null;
- Type2KeyWord type2KeyWord = Type2KeyWord.valueOfKey(getKey());
+ Type2KeyWord type2KeyWord = getType2KeyWord();
if (type2KeyWord != null)
{
str = type2KeyWord.toString();
}
else
{
- Type1KeyWord type1KeyWord = Type1KeyWord.valueOfKey(getKey());
+ Type1KeyWord type1KeyWord = getType1KeyWord();
if (type1KeyWord != null)
{
str = type1KeyWord.toString();
@@ -131,7 +136,7 @@ public class CharStringCommand
public static class Key
{
- private int[] keyValues = null;
+ private final int[] keyValues;
/**
* Constructor with one value.
@@ -140,7 +145,7 @@ public class CharStringCommand
*/
public Key(int b0)
{
- setValue(new int[] { b0 });
+ keyValues = new int[] { b0 };
}
/**
@@ -151,7 +156,7 @@ public class CharStringCommand
*/
public Key(int b0, int b1)
{
- setValue(new int[] { b0, b1 });
+ keyValues = new int[] { b0, b1 };
}
/**
@@ -161,7 +166,7 @@ public class CharStringCommand
*/
public Key(int[] values)
{
- setValue(values);
+ keyValues = values;
}
/**
@@ -174,11 +179,6 @@ public class CharStringCommand
return keyValues;
}
- private void setValue(int[] value)
- {
- keyValues = value;
- }
-
/**
* {@inheritDoc}
*/
Modified: pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/cff/Type1CharString.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/cff/Type1CharString.java?rev=1883049&r1=1883048&r2=1883049&view=diff
==============================================================================
--- pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/cff/Type1CharString.java (original)
+++ pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/cff/Type1CharString.java Sun Nov 1 14:11:37 2020
@@ -161,7 +161,7 @@ public class Type1CharString
private List<Number> handleCommand(List<Number> numbers, CharStringCommand command)
{
commandCount++;
- Type1KeyWord type1KeyWord = CharStringCommand.Type1KeyWord.valueOfKey(command.getKey());
+ Type1KeyWord type1KeyWord = command.getType1KeyWord();
if (type1KeyWord == null)
{
// indicates an invalid charstring
Modified: pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/cff/Type1CharStringParser.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/cff/Type1CharStringParser.java?rev=1883049&r1=1883048&r2=1883049&view=diff
==============================================================================
--- pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/cff/Type1CharStringParser.java (original)
+++ pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/cff/Type1CharStringParser.java Sun Nov 1 14:11:37 2020
@@ -24,6 +24,7 @@ import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
+import org.apache.fontbox.cff.CharStringCommand.Type1KeyWord;
/**
* This class represents a converter for a mapping into a Type 1 sequence.
@@ -38,13 +39,12 @@ public class Type1CharStringParser
private static final Log LOG = LogFactory.getLog(Type1CharStringParser.class);
// 1-byte commands
- static final int RETURN = 11;
- static final int CALLSUBR = 10;
+ private static final int CALLSUBR = 10;
// 2-byte commands
- static final int TWO_BYTE = 12;
- static final int CALLOTHERSUBR = 16;
- static final int POP = 17;
+ private static final int TWO_BYTE = 12;
+ private static final int CALLOTHERSUBR = 16;
+ private static final int POP = 17;
private final String fontName;
private final String glyphName;
@@ -82,89 +82,11 @@ public class Type1CharStringParser
int b0 = input.readUnsignedByte();
if (b0 == CALLSUBR)
{
- // callsubr command
- Object obj = sequence.remove(sequence.size() - 1);
- if (!(obj instanceof Integer))
- {
- LOG.warn("Parameter " + obj + " for CALLSUBR is ignored, integer expected in glyph '"
- + glyphName + "' of font " + fontName);
- continue;
- }
- Integer operand = (Integer) obj;
-
- if (operand >= 0 && operand < subrs.size())
- {
- byte[] subrBytes = subrs.get(operand);
- parse(subrBytes, subrs, sequence);
- Object lastItem = sequence.get(sequence.size()-1);
- if (lastItem instanceof CharStringCommand &&
- ((CharStringCommand)lastItem).getKey().getValue()[0] == RETURN)
- {
- sequence.remove(sequence.size()-1); // remove "return" command
- }
- }
- else
- {
- LOG.warn("CALLSUBR is ignored, operand: " + operand
- + ", subrs.size(): " + subrs.size() + " in glyph '"
- + glyphName + "' of font " + fontName);
- // remove all parameters (there can be more than one)
- while (sequence.get(sequence.size() - 1) instanceof Integer)
- {
- sequence.remove(sequence.size() - 1);
- }
- }
+ processCallSubr(subrs, sequence);
}
else if (b0 == TWO_BYTE && input.peekUnsignedByte(0) == CALLOTHERSUBR)
{
- // callothersubr command (needed in order to expand Subrs)
- input.readByte();
-
- Integer othersubrNum = (Integer)sequence.remove(sequence.size()-1);
- Integer numArgs = (Integer)sequence.remove(sequence.size()-1);
-
- // othersubrs 0-3 have their own semantics
- Deque<Integer> results = new ArrayDeque<>();
- switch (othersubrNum)
- {
- case 0:
- results.push(removeInteger(sequence));
- results.push(removeInteger(sequence));
- sequence.remove(sequence.size() - 1);
- // end flex
- sequence.add(0);
- sequence.add(new CharStringCommand(TWO_BYTE, CALLOTHERSUBR));
- break;
- case 1:
- // begin flex
- sequence.add(1);
- sequence.add(new CharStringCommand(TWO_BYTE, CALLOTHERSUBR));
- break;
- case 3:
- // allows hint replacement
- results.push(removeInteger(sequence));
- break;
- default:
- // all remaining othersubrs use this fallback mechanism
- for (int i = 0; i < numArgs; i++)
- {
- results.push(removeInteger(sequence));
- }
- break;
- }
-
- // pop must follow immediately
- while (input.peekUnsignedByte(0) == TWO_BYTE && input.peekUnsignedByte(1) == POP)
- {
- input.readByte(); // B0_POP
- input.readByte(); // B1_POP
- sequence.add(results.pop());
- }
-
- if (results.size() > 0)
- {
- LOG.warn("Value left on the PostScript stack in glyph " + glyphName + " of font " + fontName);
- }
+ processCallOtherSubr(input, sequence);
}
else if (b0 >= 0 && b0 <= 31)
{
@@ -182,6 +104,94 @@ public class Type1CharStringParser
return sequence;
}
+ private void processCallSubr(List<byte[]> subrs, List<Object> sequence) throws IOException
+ {
+ // callsubr command
+ Object obj = sequence.remove(sequence.size() - 1);
+ if (!(obj instanceof Integer))
+ {
+ LOG.warn("Parameter " + obj + " for CALLSUBR is ignored, integer expected in glyph '"
+ + glyphName + "' of font " + fontName);
+ return;
+ }
+ Integer operand = (Integer) obj;
+
+ if (operand >= 0 && operand < subrs.size())
+ {
+ byte[] subrBytes = subrs.get(operand);
+ parse(subrBytes, subrs, sequence);
+ Object lastItem = sequence.get(sequence.size() - 1);
+ if (lastItem instanceof CharStringCommand
+ && Type1KeyWord.RET == ((CharStringCommand) lastItem).getType1KeyWord())
+ {
+ sequence.remove(sequence.size() - 1); // remove "return" command
+ }
+ }
+ else
+ {
+ LOG.warn("CALLSUBR is ignored, operand: " + operand + ", subrs.size(): " + subrs.size()
+ + " in glyph '" + glyphName + "' of font " + fontName);
+ // remove all parameters (there can be more than one)
+ while (sequence.get(sequence.size() - 1) instanceof Integer)
+ {
+ sequence.remove(sequence.size() - 1);
+ }
+ }
+ }
+
+ private void processCallOtherSubr(DataInput input, List<Object> sequence) throws IOException
+ {
+ // callothersubr command (needed in order to expand Subrs)
+ input.readByte();
+
+ Integer othersubrNum = (Integer) sequence.remove(sequence.size() - 1);
+ Integer numArgs = (Integer) sequence.remove(sequence.size() - 1);
+
+ // othersubrs 0-3 have their own semantics
+ Deque<Integer> results = new ArrayDeque<>();
+ switch (othersubrNum)
+ {
+ case 0:
+ results.push(removeInteger(sequence));
+ results.push(removeInteger(sequence));
+ sequence.remove(sequence.size() - 1);
+ // end flex
+ sequence.add(0);
+ sequence.add(new CharStringCommand(TWO_BYTE, CALLOTHERSUBR));
+ break;
+ case 1:
+ // begin flex
+ sequence.add(1);
+ sequence.add(new CharStringCommand(TWO_BYTE, CALLOTHERSUBR));
+ break;
+ case 3:
+ // allows hint replacement
+ results.push(removeInteger(sequence));
+ break;
+ default:
+ // all remaining othersubrs use this fallback mechanism
+ for (int i = 0; i < numArgs; i++)
+ {
+ results.push(removeInteger(sequence));
+ }
+ break;
+ }
+
+ // pop must follow immediately
+ while (input.peekUnsignedByte(0) == TWO_BYTE && input.peekUnsignedByte(1) == POP)
+ {
+ input.readByte(); // B0_POP
+ input.readByte(); // B1_POP
+ sequence.add(results.pop());
+ }
+
+ if (!results.isEmpty())
+ {
+ LOG.warn("Value left on the PostScript stack in glyph " + glyphName + " of font "
+ + fontName);
+ }
+ }
+
// this method is a workaround for the fact that Type1CharStringParser assumes that subrs and
// othersubrs can be unrolled without executing the 'div' operator, which isn't true
private static Integer removeInteger(List<Object> sequence) throws IOException
@@ -194,7 +204,7 @@ public class Type1CharStringParser
CharStringCommand command = (CharStringCommand) item;
// div
- if (command.getKey().getValue()[0] == 12 && command.getKey().getValue()[1] == 12)
+ if (Type1KeyWord.DIV == command.getType1KeyWord())
{
int a = (Integer) sequence.remove(sequence.size() - 1);
int b = (Integer) sequence.remove(sequence.size() - 1);
Modified: pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/cff/Type2CharString.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/cff/Type2CharString.java?rev=1883049&r1=1883048&r2=1883049&view=diff
==============================================================================
--- pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/cff/Type2CharString.java (original)
+++ pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/cff/Type2CharString.java Sun Nov 1 14:11:37 2020
@@ -90,7 +90,7 @@ public class Type2CharString extends Typ
private List<Number> handleCommand(List<Number> numbers, CharStringCommand command)
{
commandCount++;
- Type2KeyWord type2KeyWord = CharStringCommand.Type2KeyWord.valueOfKey(command.getKey());
+ Type2KeyWord type2KeyWord = command.getType2KeyWord();
if (type2KeyWord == null)
{
addCommand(numbers, command);
Modified: pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/cff/Type2CharStringParser.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/cff/Type2CharStringParser.java?rev=1883049&r1=1883048&r2=1883049&view=diff
==============================================================================
--- pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/cff/Type2CharStringParser.java (original)
+++ pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/cff/Type2CharStringParser.java Sun Nov 1 14:11:37 2020
@@ -21,12 +21,19 @@ import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
+import org.apache.fontbox.cff.CharStringCommand.Type2KeyWord;
+
/**
* This class represents a converter for a mapping into a Type2-sequence.
* @author Villu Ruusmann
*/
public class Type2CharStringParser
{
+
+ // 1-byte commands
+ private static final int CALLSUBR = 10;
+ private static final int CALLGSUBR = 29;
+
private int hstemCount = 0;
private int vstemCount = 0;
private List<Object> sequence = null;
@@ -88,87 +95,22 @@ public class Type2CharStringParser
while (input.hasRemaining())
{
int b0 = input.readUnsignedByte();
- if (b0 == 10 && localSubroutineIndexProvided)
- { // process subr command
- Integer operand=(Integer)sequence.remove(sequence.size()-1);
- //get subrbias
- int bias = 0;
- int nSubrs = localSubrIndex.length;
-
- if (nSubrs < 1240)
- {
- bias = 107;
- }
- else if (nSubrs < 33900)
- {
- bias = 1131;
- }
- else
- {
- bias = 32768;
- }
- int subrNumber = bias+operand;
- if (subrNumber < localSubrIndex.length)
- {
- byte[] subrBytes = localSubrIndex[subrNumber];
- parse(subrBytes, globalSubrIndex, localSubrIndex, false);
- Object lastItem=sequence.get(sequence.size()-1);
- if (lastItem instanceof CharStringCommand && ((CharStringCommand)lastItem).getKey().getValue()[0] == 11)
- {
- sequence.remove(sequence.size()-1); // remove "return" command
- }
- }
-
- }
- else if (b0 == 29 && globalSubroutineIndexProvided)
- { // process globalsubr command
- Integer operand=(Integer)sequence.remove(sequence.size()-1);
- //get subrbias
- int bias;
- int nSubrs = globalSubrIndex.length;
-
- if (nSubrs < 1240)
- {
- bias = 107;
- }
- else if (nSubrs < 33900)
- {
- bias = 1131;
- }
- else
- {
- bias = 32768;
- }
-
- int subrNumber = bias+operand;
- if (subrNumber < globalSubrIndex.length)
- {
- byte[] subrBytes = globalSubrIndex[subrNumber];
- parse(subrBytes, globalSubrIndex, localSubrIndex, false);
- Object lastItem=sequence.get(sequence.size()-1);
- if (lastItem instanceof CharStringCommand && ((CharStringCommand)lastItem).getKey().getValue()[0]==11)
- {
- sequence.remove(sequence.size()-1); // remove "return" command
- }
- }
-
- }
- else if (b0 >= 0 && b0 <= 27)
+ if (b0 == CALLSUBR && localSubroutineIndexProvided)
{
- sequence.add(readCommand(b0, input));
+ processCallSubr(globalSubrIndex, localSubrIndex);
}
- else if (b0 == 28)
+ else if (b0 == CALLGSUBR && globalSubroutineIndexProvided)
{
- sequence.add(readNumber(b0, input));
+ processCallGSubr(globalSubrIndex, localSubrIndex);
}
- else if (b0 >= 29 && b0 <= 31)
+ else if ( (b0 >= 0 && b0 <= 27) || (b0 >= 29 && b0 <= 31))
{
sequence.add(readCommand(b0, input));
}
- else if (b0 >= 32 && b0 <= 255)
+ else if (b0 == 28 || (b0 >= 32 && b0 <= 255))
{
sequence.add(readNumber(b0, input));
- }
+ }
else
{
throw new IllegalArgumentException();
@@ -177,6 +119,75 @@ public class Type2CharStringParser
return sequence;
}
+ private void processCallSubr(byte[][] globalSubrIndex, byte[][] localSubrIndex)
+ throws IOException
+ {
+ Integer operand = (Integer) sequence.remove(sequence.size() - 1);
+ // get subrbias
+ int bias = 0;
+ int nSubrs = localSubrIndex.length;
+
+ if (nSubrs < 1240)
+ {
+ bias = 107;
+ }
+ else if (nSubrs < 33900)
+ {
+ bias = 1131;
+ }
+ else
+ {
+ bias = 32768;
+ }
+ int subrNumber = bias + operand;
+ if (subrNumber < localSubrIndex.length)
+ {
+ byte[] subrBytes = localSubrIndex[subrNumber];
+ parse(subrBytes, globalSubrIndex, localSubrIndex, false);
+ Object lastItem = sequence.get(sequence.size() - 1);
+ if (lastItem instanceof CharStringCommand
+ && Type2KeyWord.RET == ((CharStringCommand) lastItem).getType2KeyWord())
+ {
+ sequence.remove(sequence.size() - 1); // remove "return" command
+ }
+ }
+ }
+
+ private void processCallGSubr(byte[][] globalSubrIndex, byte[][] localSubrIndex)
+ throws IOException
+ {
+ Integer operand = (Integer) sequence.remove(sequence.size() - 1);
+ // get subrbias
+ int bias;
+ int nSubrs = globalSubrIndex.length;
+
+ if (nSubrs < 1240)
+ {
+ bias = 107;
+ }
+ else if (nSubrs < 33900)
+ {
+ bias = 1131;
+ }
+ else
+ {
+ bias = 32768;
+ }
+
+ int subrNumber = bias + operand;
+ if (subrNumber < globalSubrIndex.length)
+ {
+ byte[] subrBytes = globalSubrIndex[subrNumber];
+ parse(subrBytes, globalSubrIndex, localSubrIndex, false);
+ Object lastItem = sequence.get(sequence.size() - 1);
+ if (lastItem instanceof CharStringCommand
+ && Type2KeyWord.RET == ((CharStringCommand) lastItem).getType2KeyWord())
+ {
+ sequence.remove(sequence.size() - 1); // remove "return" command
+ }
+ }
+ }
+
private CharStringCommand readCommand(int b0, DataInput input) throws IOException
{
@@ -213,7 +224,6 @@ public class Type2CharStringParser
private Number readNumber(int b0, DataInput input) throws IOException
{
-
if (b0 == 28)
{
return (int) input.readShort();