Skip to content

Commit

Permalink
woff2 arbitrary flag tag writing
Browse files Browse the repository at this point in the history
  • Loading branch information
m-abboud committed Feb 18, 2018
1 parent 23078a5 commit 325d9f2
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/main/java/org/mabb/fontverter/FontVerterUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public static int readLowerBits(int inByte, int nBits) throws IOException {
if (nBits > 8)
throw new IOException("Number of bits exceeds 8");

return inByte & (0x1F);
return inByte & (0x3F);
}

public static byte[] toPrimative(Byte[] objArray) {
Expand Down
27 changes: 17 additions & 10 deletions src/main/java/org/mabb/fontverter/woff/Woff2Font.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ public class Woff2Font extends WoffFont {
private byte[] cachedCompressedBlock;

public WoffTable createTable() {
return new Woff2Font.Woff2Table(new byte[0], arbitrary);
return new Woff2Font.Woff2Table(new byte[0], "arbitrary");
}

public void addFontTable(byte[] data, String tag, long checksum) {
WoffTable table = new Woff2Table(data, WoffConstants.TableFlagType.fromString(tag));
WoffTable table = new Woff2Table(data, tag);
tables.add(table);
}

Expand Down Expand Up @@ -103,11 +103,11 @@ public FontConverter createConverterForType(FontVerter.FontFormat fontFormat) th

public static class Woff2Table extends WoffTable {
private int transform = -1;
protected WoffConstants.TableFlagType flag;
protected String tag = "";

public Woff2Table(byte[] table, WoffConstants.TableFlagType flag) {
public Woff2Table(byte[] table, String tag) {
super(table);
this.flag = flag;
this.tag = tag;
}

protected byte[] compress(byte[] bytes) throws IOException {
Expand All @@ -119,8 +119,15 @@ protected byte[] compress(byte[] bytes) throws IOException {
public byte[] getDirectoryData() throws IOException {
WoffOutputStream writer = new WoffOutputStream();

WoffConstants.TableFlagType flag = WoffConstants.TableFlagType.fromString(tag);
writer.writeFlagByte(flag.getValue(), getTransform());
// todo tag here for arbitrary flag type

if (flag == arbitrary) {
// maybe should string pad < 4
assert tag.length() == 4;
writer.writeString(tag);
}

writer.writeUIntBase128(tableData.length);

if (isTableTransformed())
Expand All @@ -147,24 +154,24 @@ public int getTransform() {
}

private int initTransformValue() {
if (flag == glyf || flag == loca)
if (getFlag() == glyf || getFlag() == loca)
return 3;
return 0;
}

boolean isTableTransformed() {
if (flag == WoffConstants.TableFlagType.glyf || flag == loca)
if (getFlag() == WoffConstants.TableFlagType.glyf || getFlag() == loca)
return transform != 3;

return transform != 0;
}

public WoffConstants.TableFlagType getFlag() {
return flag;
return WoffConstants.TableFlagType.fromString(tag);
}

public String getTag() {
return flag.toString();
return getFlag().toString();
}
}
}
7 changes: 4 additions & 3 deletions src/main/java/org/mabb/fontverter/woff/Woff2Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,10 @@ private void parseDirectoryEntry() throws IOException {
int[] rawFlag = input.readSplitBits(2);

table.setTransform(rawFlag[0]);
table.flag = TableFlagType.fromInt(rawFlag[1]);
TableFlagType flag = TableFlagType.fromInt(rawFlag[1]);
table.tag = flag.toString();

if (table.flag.getValue() == 63) {
if (flag.getValue() == 63) {
String tagStr = new String(ByteBuffer.allocate(4).putInt(input.readInt()).array());
log.error("!! arbitrary flag type not tested" + tagStr);
}
Expand All @@ -66,7 +67,7 @@ private void parseDirectoryEntry() throws IOException {
table.transformLength = table.originalLength;

log.debug("Woff2 parse table dir read: {} {} o-len:" + table.originalLength + " t-len:" + table.transformLength,
table.flag, table.getTransform());
table.tag, table.getTransform());

font.getTables().add(table);
}
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/org/mabb/fontverter/woff/WoffConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ public static TableFlagType fromInt(int i) {
if (typeOn.getValue() == i) {
return typeOn;
}
return null;

return arbitrary;
}

public static TableFlagType fromString(String str) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ public void convertTtf_withUnsignedFlagByte() throws Exception {
Woff2Font woffFont = (Woff2Font) FontVerter.convertFont(TEST_PATH + "comic.ttf", FontFormat.WOFF2);
int otfLength = woffFont.getFonts().get(0).getData().length;

byte[] ttf = FontVerter.readFont(TEST_PATH + "comic.ttf").getData();
saveTempFile(ttf, "comic.ttf");

byte[] fontData = woffFont.getData();
saveTempFile(fontData, "comic.woff2");

Expand Down Expand Up @@ -98,7 +101,7 @@ public void convertCffToWoff2_thenAllOtfTableTypes_arePresentInWoff2Font() throw
for (TableFlagType flagOn : flags) {
boolean flagFound = false;
for (WoffTable table : parsedFont.getTables()) {
if (flagOn == ((Woff2Font.Woff2Table)table).flag)
if (flagOn == ((Woff2Font.Woff2Table)table).getFlag())
flagFound = true;
}

Expand Down

0 comments on commit 325d9f2

Please sign in to comment.