aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Schweinsberg <[email protected]>2015-12-30 15:50:23 -0800
committerDavid Schweinsberg <[email protected]>2015-12-30 15:50:23 -0800
commit52e8dce11e6456ff08f4c46e59ac619b67df603f (patch)
tree6ab75344b9c55229cef4dec2214e8e3232e10495
parent886b65573a163f32d663b8cc8c475eefe5f3a6df (diff)
Moved instruction pointer out of Charstring and into T2Interpreter
-rw-r--r--src/net/java/dev/typecast/app/editor/GlyphPanel.java4
-rw-r--r--src/net/java/dev/typecast/cff/CffFont.java14
-rw-r--r--src/net/java/dev/typecast/cff/CharstringType2.java86
-rw-r--r--src/net/java/dev/typecast/cff/T2Interpreter.java42
-rw-r--r--src/net/java/dev/typecast/ot/T2Glyph.java11
5 files changed, 86 insertions, 71 deletions
diff --git a/src/net/java/dev/typecast/app/editor/GlyphPanel.java b/src/net/java/dev/typecast/app/editor/GlyphPanel.java
index 64a7053..5aeb06e 100644
--- a/src/net/java/dev/typecast/app/editor/GlyphPanel.java
+++ b/src/net/java/dev/typecast/app/editor/GlyphPanel.java
@@ -87,9 +87,7 @@ public class GlyphPanel extends JPanel implements EditorView {
_glyphEdit.setGlyph(new T2Glyph(
cs,
font.getHmtxTable().getLeftSideBearing(cs.getIndex()),
- font.getHmtxTable().getAdvanceWidth(cs.getIndex()),
- font.getCffTable().getFont(cs.getIndex()).getLocalSubrsIndex(),
- font.getCffTable().getGlobalSubrIndex()));
+ font.getHmtxTable().getAdvanceWidth(cs.getIndex())));
}
}
diff --git a/src/net/java/dev/typecast/cff/CffFont.java b/src/net/java/dev/typecast/cff/CffFont.java
index 2c919bb..3599178 100644
--- a/src/net/java/dev/typecast/cff/CffFont.java
+++ b/src/net/java/dev/typecast/cff/CffFont.java
@@ -32,7 +32,7 @@ public class CffFont {
private final Dict _topDict;
private final Index _charStringsIndex;
private final Dict _privateDict;
- private final Index _localSubrsIndex;
+ private final Index _localSubrIndex;
private final Charset _charset;
private final Charstring[] _charstrings;
@@ -60,9 +60,9 @@ public class CffFont {
Integer localSubrsOffset = (Integer) _privateDict.getValue(19);
if (localSubrsOffset != null) {
di = table.getDataInputForOffset(privateSizeAndOffset.get(1) + localSubrsOffset);
- _localSubrsIndex = new Index(di);
+ _localSubrIndex = new Index(di);
} else {
- _localSubrsIndex = null;
+ _localSubrIndex = null;
//throw new Exception();
}
@@ -100,6 +100,10 @@ public class CffFont {
}
}
+ public CffTable getTable() {
+ return _table;
+ }
+
public Index getCharStringsIndex() {
return _charStringsIndex;
}
@@ -108,8 +112,8 @@ public class CffFont {
return _privateDict;
}
- public Index getLocalSubrsIndex() {
- return _localSubrsIndex;
+ public Index getLocalSubrIndex() {
+ return _localSubrIndex;
}
public Charset getCharset() {
diff --git a/src/net/java/dev/typecast/cff/CharstringType2.java b/src/net/java/dev/typecast/cff/CharstringType2.java
index 241044f..34ae551 100644
--- a/src/net/java/dev/typecast/cff/CharstringType2.java
+++ b/src/net/java/dev/typecast/cff/CharstringType2.java
@@ -107,7 +107,6 @@ public class CharstringType2 extends Charstring {
private final int[] _data;
private final int _offset;
private final int _length;
- private int _ip;
/** Creates a new instance of CharstringType2
* @param font
@@ -145,15 +144,16 @@ public class CharstringType2 extends Charstring {
return _name;
}
- private void disassemble(StringBuilder sb) {
- while (isOperandAtIndex()) {
- Number operand = nextOperand();
+ private int disassemble(int ip, StringBuilder sb) {
+ while (isOperandAtIndex(ip)) {
+ Number operand = operandAtIndex(ip);
sb.append(operand).append(" ");
+ ip = nextOperandIndex(ip);
}
- int operator = nextByte();
+ int operator = byteAtIndex(ip++);
String mnemonic;
if (operator == 12) {
- operator = nextByte();
+ operator = byteAtIndex(ip++);
// Check we're not exceeding the upper limit of our mnemonics
if (operator > 38) {
@@ -164,71 +164,93 @@ public class CharstringType2 extends Charstring {
mnemonic = _oneByteOperators[operator];
}
sb.append(mnemonic);
+ return ip;
}
- public void resetIP() {
- _ip = _offset;
+ public int getFirstIndex() {
+ return _offset;
}
- public boolean isOperandAtIndex() {
- int b0 = _data[_ip];
+ public boolean isOperandAtIndex(int ip) {
+ int b0 = _data[ip];
return (32 <= b0 && b0 <= 255) || b0 == 28;
}
- public Number nextOperand() {
- int b0 = _data[_ip];
+ public Number operandAtIndex(int ip) {
+ int b0 = _data[ip];
if (32 <= b0 && b0 <= 246) {
// 1 byte integer
- ++_ip;
return b0 - 139;
} else if (247 <= b0 && b0 <= 250) {
// 2 byte integer
- int b1 = _data[_ip + 1];
- _ip += 2;
+ int b1 = _data[ip + 1];
return (b0 - 247) * 256 + b1 + 108;
} else if (251 <= b0 && b0 <= 254) {
// 2 byte integer
- int b1 = _data[_ip + 1];
- _ip += 2;
+ int b1 = _data[ip + 1];
return -(b0 - 251) * 256 - b1 - 108;
} else if (b0 == 28) {
// 3 byte integer
- int b1 = (byte)_data[_ip + 1];
- int b2 = _data[_ip + 2];
- _ip += 3;
+ int b1 = (byte)_data[ip + 1];
+ int b2 = _data[ip + 2];
return b1 << 8 | b2;
} else if (b0 == 255) {
// 16-bit signed integer with 16 bits of fraction
- int b1 = (byte) _data[_ip + 1];
- int b2 = _data[_ip + 2];
- int b3 = _data[_ip + 3];
- int b4 = _data[_ip + 4];
- _ip += 5;
+ int b1 = (byte) _data[ip + 1];
+ int b2 = _data[ip + 2];
+ int b3 = _data[ip + 3];
+ int b4 = _data[ip + 4];
return new Float((b1 << 8 | b2) + ((b3 << 8 | b4) / 65536.0));
} else {
return null;
}
}
+
+ public int nextOperandIndex(int ip) {
+ int b0 = _data[ip];
+ if (32 <= b0 && b0 <= 246) {
+
+ // 1 byte integer
+ return ip + 1;
+ } else if (247 <= b0 && b0 <= 250) {
+
+ // 2 byte integer
+ return ip + 2;
+ } else if (251 <= b0 && b0 <= 254) {
+
+ // 2 byte integer
+ return ip + 2;
+ } else if (b0 == 28) {
+
+ // 3 byte integer
+ return ip + 3;
+ } else if (b0 == 255) {
+
+ return ip + 5;
+ } else {
+ return ip;
+ }
+ }
- public int nextByte() {
- return _data[_ip++];
+ public int byteAtIndex(int ip) {
+ return _data[ip];
}
- public boolean moreBytes() {
- return _ip < _offset + _length;
+ public boolean moreBytes(int ip) {
+ return ip < _offset + _length;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
- resetIP();
- while (moreBytes()) {
- disassemble(sb);
+ int ip = getFirstIndex();
+ while (moreBytes(ip)) {
+ ip = disassemble(ip, sb);
sb.append("\n");
}
return sb.toString();
diff --git a/src/net/java/dev/typecast/cff/T2Interpreter.java b/src/net/java/dev/typecast/cff/T2Interpreter.java
index b823454..ad23141 100644
--- a/src/net/java/dev/typecast/cff/T2Interpreter.java
+++ b/src/net/java/dev/typecast/cff/T2Interpreter.java
@@ -41,13 +41,12 @@ public class T2Interpreter {
private int _stemCount = 0;
private ArrayList<Point> _points;
- private final Index _localSubrIndex;
- private final Index _globalSubrIndex;
+ private Index _localSubrIndex;
+ private Index _globalSubrIndex;
+ private int _ip;
/** Creates a new instance of T2Interpreter */
- public T2Interpreter(Index localSubrIndex, Index globalSubrIndex) {
- _localSubrIndex = localSubrIndex;
- _globalSubrIndex = globalSubrIndex;
+ public T2Interpreter() {
}
/**
@@ -550,21 +549,15 @@ public class T2Interpreter {
clearArg();
}
- private void _hintmask(CharstringType2 cs) {
+ private void _hintmask() {
_stemCount += getArgCount() / 2;
- int maskLen = (_stemCount - 1) / 8 + 1;
- for (int i = 0; i < maskLen; ++i) {
- cs.nextByte();
- }
+ _ip += (_stemCount - 1) / 8 + 1;
clearArg();
}
- private void _cntrmask(CharstringType2 cs) {
+ private void _cntrmask() {
_stemCount += getArgCount() / 2;
- int maskLen = (_stemCount - 1) / 8 + 1;
- for (int i = 0; i < maskLen; ++i) {
- cs.nextByte();
- }
+ _ip += (_stemCount - 1) / 8 + 1;
clearArg();
}
@@ -826,15 +819,18 @@ public class T2Interpreter {
}
public Point[] execute(CharstringType2 cs) {
+ _localSubrIndex = cs.getFont().getLocalSubrIndex();
+ _globalSubrIndex = cs.getFont().getTable().getGlobalSubrIndex();
_points = new ArrayList<>();
- cs.resetIP();
- while (cs.moreBytes()) {
- while (cs.isOperandAtIndex()) {
- pushArg(cs.nextOperand());
+ _ip = cs.getFirstIndex();
+ while (cs.moreBytes(_ip)) {
+ while (cs.isOperandAtIndex(_ip)) {
+ pushArg(cs.operandAtIndex(_ip));
+ _ip = cs.nextOperandIndex(_ip);
}
- int operator = cs.nextByte();
+ int operator = cs.byteAtIndex(_ip++);
if (operator == 12) {
- operator = cs.nextByte();
+ operator = cs.byteAtIndex(_ip++);
// Two-byte operators
switch (operator) {
@@ -952,10 +948,10 @@ public class T2Interpreter {
_hstemhm();
break;
case T2Mnemonic.HINTMASK:
- _hintmask(cs);
+ _hintmask();
break;
case T2Mnemonic.CNTRMASK:
- _cntrmask(cs);
+ _cntrmask();
break;
case T2Mnemonic.RMOVETO:
_rmoveto();
diff --git a/src/net/java/dev/typecast/ot/T2Glyph.java b/src/net/java/dev/typecast/ot/T2Glyph.java
index 264d0a3..be1f573 100644
--- a/src/net/java/dev/typecast/ot/T2Glyph.java
+++ b/src/net/java/dev/typecast/ot/T2Glyph.java
@@ -18,7 +18,6 @@
package net.java.dev.typecast.ot;
import net.java.dev.typecast.cff.CharstringType2;
-import net.java.dev.typecast.cff.Index;
import net.java.dev.typecast.cff.T2Interpreter;
/**
@@ -35,19 +34,15 @@ public class T2Glyph extends Glyph {
* @param cs The CharstringType2 describing the glyph.
* @param lsb The Left Side Bearing.
* @param advance The advance width.
- * @param localSubrIndex
- * @param globalSubrIndex
*/
public T2Glyph(
CharstringType2 cs,
short lsb,
- int advance,
- Index localSubrIndex,
- Index globalSubrIndex) {
+ int advance) {
_leftSideBearing = lsb;
_advanceWidth = advance;
- T2Interpreter t2i = new T2Interpreter(localSubrIndex, globalSubrIndex);
- _points = t2i.execute((CharstringType2) cs);
+ T2Interpreter t2i = new T2Interpreter();
+ _points = t2i.execute(cs);
}
@Override