From a0e71e557db65c8be875a8e82c634b684e6f4317 Mon Sep 17 00:00:00 2001 From: Simon Steiner Date: Wed, 13 Sep 2017 11:39:29 +0000 Subject: [PATCH] Cleanup offset code git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@1808231 13f79535-47bb-0310-9956-ffa450edef68 --- .../fop/fonts/truetype/OTFSubSetFile.java | 106 +++++++++--------- .../fonts/truetype/OTFSubSetFileTestCase.java | 23 ++-- 2 files changed, 61 insertions(+), 68 deletions(-) diff --git a/fop-core/src/main/java/org/apache/fop/fonts/truetype/OTFSubSetFile.java b/fop-core/src/main/java/org/apache/fop/fonts/truetype/OTFSubSetFile.java index ee534ba2e..be197c1ec 100644 --- a/fop-core/src/main/java/org/apache/fop/fonts/truetype/OTFSubSetFile.java +++ b/fop-core/src/main/java/org/apache/fop/fonts/truetype/OTFSubSetFile.java @@ -167,12 +167,12 @@ public class OTFSubSetFile extends OTFSubSetWriter { writeBytes(cffReader.getHeader()); //Name Index - writeIndex(Arrays.asList(embedFontName.getBytes())); + writeIndex(Arrays.asList(embedFontName.getBytes("UTF-8"))); + + Offsets offsets = new Offsets(); - //Keep offset of the topDICT so it can be updated once all data has been written - int topDictOffset = currentPos; //Top DICT Index and Data - int topDictDataOffset = topDictOffset + writeTopDICT(); + offsets.topDictData = currentPos + writeTopDICT(); boolean hasFDSelect = cffReader.getFDSelect() != null; @@ -198,47 +198,54 @@ public class OTFSubSetFile extends OTFSubSetWriter { writeIndex(subsetGlobalIndexSubr); //Encoding - int encodingOffset = currentPos; + offsets.encoding = currentPos; //Charset table - int charsetOffset = currentPos; + offsets.charset = currentPos; writeCharsetTable(hasFDSelect); //FDSelect table - int fdSelectOffset = currentPos; + offsets.fdSelect = currentPos; if (hasFDSelect) { writeFDSelect(); + if (!isCharStringBeforeFD()) { + offsets.fdArray = writeFDArray(subsetFDFonts, fontNameSIDs); + } } - int fdArrayOffset = -1; - if (hasFDSelect && !isCharStringBeforeFD()) { - fdArrayOffset = writeFDArray(subsetFDFonts, fontNameSIDs); - } //Char Strings Index - int charStringOffset = currentPos; + offsets.charString = currentPos; writeIndex(subsetCharStringsIndex); - if (hasFDSelect && isCharStringBeforeFD()) { - fdArrayOffset = writeFDArray(subsetFDFonts, fontNameSIDs); - } - if (hasFDSelect) { - updateCIDOffsets(topDictDataOffset, fdArrayOffset, fdSelectOffset, charsetOffset, - charStringOffset, encodingOffset); + if (isCharStringBeforeFD()) { + offsets.fdArray = writeFDArray(subsetFDFonts, fontNameSIDs); + } + updateCIDOffsets(offsets); } else { //Keep offset to modify later with the local subroutine index offset - int privateDictOffset = currentPos; + offsets.privateDict = currentPos; writePrivateDict(); //Local subroutine index - int localIndexOffset = currentPos; + offsets.localIndex = currentPos; writeIndex(subsetLocalIndexSubr); //Update the offsets - updateOffsets(topDictOffset, charsetOffset, charStringOffset, privateDictOffset, - localIndexOffset, encodingOffset); + updateOffsets(offsets); } } + static class Offsets { + Integer topDictData; + Integer encoding; + Integer charset; + Integer fdSelect; + Integer charString; + Integer fdArray; + Integer privateDict; + Integer localIndex; + } + private int writeFDArray(List subsetFDFonts, List fontNameSIDs) throws IOException { List privateDictOffsets = writeCIDDictsAndSubrs(subsetFDFonts); return writeFDArray(subsetFDFonts, privateDictOffsets, fontNameSIDs); @@ -308,7 +315,7 @@ public class OTFSubSetFile extends OTFSubSetWriter { int sidAStringIndex = stringIndexData.size() + 390; int sidB = dictEntry.getOperands().get(1).intValue(); if (sidB > 390) { - stringIndexData.add("Identity".getBytes()); + stringIndexData.add("Identity".getBytes("UTF-8")); } int sidBStringIndex = stringIndexData.size() + 390; byte[] cidEntryByteData = dictEntry.getByteData(); @@ -361,7 +368,7 @@ public class OTFSubSetFile extends OTFSubSetWriter { if (index < cffReader.getStringIndex().getNumObjects()) { if (mbFont != null) { mbFont.mapUsedGlyphName(subsetGlyph.getValue(), - new String(cffReader.getStringIndex().getValue(index))); + new String(cffReader.getStringIndex().getValue(index), "UTF-8")); } gidToSID.put(subsetGlyph.getValue(), stringIndexData.size() + 391); stringIndexData.add(cffReader.getStringIndex().getValue(index)); @@ -523,11 +530,10 @@ public class OTFSubSetFile extends OTFSubSetWriter { List fdFonts = cffReader.getFDFonts(); for (int i = 0; i < uniqueNewRefs.size(); i++) { FontDict curFDFont = fdFonts.get(uniqueNewRefs.get(i)); - Map fdPrivateDict = cffReader.parseDictData( - curFDFont.getPrivateDictData()); + byte[] fdPrivateDictByteData = curFDFont.getPrivateDictData(); + Map fdPrivateDict = cffReader.parseDictData(fdPrivateDictByteData); int privateDictOffset = currentPos; privateDictOffsets.add(privateDictOffset); - byte[] fdPrivateDictByteData = curFDFont.getPrivateDictData(); if (fdPrivateDict.get("Subrs") != null) { updateOffset(fdPrivateDictByteData, fdPrivateDict.get("Subrs").getOffset(), fdPrivateDict.get("Subrs").getOperandLength(), @@ -1077,9 +1083,7 @@ public class OTFSubSetFile extends OTFSubSetWriter { } } - protected void updateOffsets(int topDictOffset, int charsetOffset, int charStringOffset, - int privateDictOffset, int localIndexOffset, int encodingOffset) - throws IOException { + protected void updateOffsets(Offsets offsets) throws IOException { Map topDICT = cffReader.getTopDictEntries(); Map privateDICT = null; @@ -1088,65 +1092,59 @@ public class OTFSubSetFile extends OTFSubSetWriter { privateDICT = cffReader.getPrivateDict(privateEntry); } - int dataPos = 3 + (cffReader.getTopDictIndex().getOffSize() - * cffReader.getTopDictIndex().getOffsets().length); - int dataTopDictOffset = topDictOffset + dataPos; - - updateFixedOffsets(topDICT, dataTopDictOffset, charsetOffset, charStringOffset, encodingOffset); + updateFixedOffsets(topDICT, offsets); if (privateDICT != null) { //Private index offset in the top dict - int oldPrivateOffset = dataTopDictOffset + privateEntry.getOffset(); + int oldPrivateOffset = offsets.topDictData + privateEntry.getOffset(); updateOffset(output, oldPrivateOffset + privateEntry.getOperandLengths().get(0), - privateEntry.getOperandLengths().get(1), privateDictOffset); + privateEntry.getOperandLengths().get(1), offsets.privateDict); //Update the local subroutine index offset in the private dict DICTEntry subroutines = privateDICT.get("Subrs"); if (subroutines != null) { - int oldLocalSubrOffset = privateDictOffset + subroutines.getOffset(); + int oldLocalSubrOffset = offsets.privateDict + subroutines.getOffset(); updateOffset(output, oldLocalSubrOffset, subroutines.getOperandLength(), - (localIndexOffset - privateDictOffset)); + (offsets.localIndex - offsets.privateDict)); } } } - protected void updateFixedOffsets(Map topDICT, int dataTopDictOffset, - int charsetOffset, int charStringOffset, int encodingOffset) { + protected void updateFixedOffsets(Map topDICT, Offsets offsets) { //Charset offset in the top dict DICTEntry charset = topDICT.get("charset"); - int oldCharsetOffset = dataTopDictOffset + charset.getOffset(); - updateOffset(output, oldCharsetOffset, charset.getOperandLength(), charsetOffset); + int oldCharsetOffset = offsets.topDictData + charset.getOffset(); + updateOffset(output, oldCharsetOffset, charset.getOperandLength(), offsets.charset); //Char string index offset in the private dict DICTEntry charString = topDICT.get("CharStrings"); - int oldCharStringOffset = dataTopDictOffset + charString.getOffset(); - updateOffset(output, oldCharStringOffset, charString.getOperandLength(), charStringOffset); + int oldCharStringOffset = offsets.topDictData + charString.getOffset(); + updateOffset(output, oldCharStringOffset, charString.getOperandLength(), offsets.charString); DICTEntry encodingEntry = topDICT.get("Encoding"); if (encodingEntry != null && encodingEntry.getOperands().get(0).intValue() != 0 && encodingEntry.getOperands().get(0).intValue() != 1) { - int oldEncodingOffset = dataTopDictOffset + encodingEntry.getOffset(); - updateOffset(output, oldEncodingOffset, encodingEntry.getOperandLength(), encodingOffset); + int oldEncodingOffset = offsets.topDictData + encodingEntry.getOffset(); + updateOffset(output, oldEncodingOffset, encodingEntry.getOperandLength(), offsets.encoding); } } - protected void updateCIDOffsets(int topDictDataOffset, int fdArrayOffset, int fdSelectOffset, - int charsetOffset, int charStringOffset, int encodingOffset) { + protected void updateCIDOffsets(Offsets offsets) { Map topDict = cffReader.getTopDictEntries(); DICTEntry fdArrayEntry = topDict.get("FDArray"); if (fdArrayEntry != null) { - updateOffset(output, topDictDataOffset + fdArrayEntry.getOffset() - 1, - fdArrayEntry.getOperandLength(), fdArrayOffset); + updateOffset(output, offsets.topDictData + fdArrayEntry.getOffset() - 1, + fdArrayEntry.getOperandLength(), offsets.fdArray); } DICTEntry fdSelect = topDict.get("FDSelect"); if (fdSelect != null) { - updateOffset(output, topDictDataOffset + fdSelect.getOffset() - 1, - fdSelect.getOperandLength(), fdSelectOffset); + updateOffset(output, offsets.topDictData + fdSelect.getOffset() - 1, + fdSelect.getOperandLength(), offsets.fdSelect); } - updateFixedOffsets(topDict, topDictDataOffset, charsetOffset, charStringOffset, encodingOffset); + updateFixedOffsets(topDict, offsets); } protected void updateOffset(byte[] out, int position, int length, int replacement) { diff --git a/fop-core/src/test/java/org/apache/fop/fonts/truetype/OTFSubSetFileTestCase.java b/fop-core/src/test/java/org/apache/fop/fonts/truetype/OTFSubSetFileTestCase.java index b2d12ec90..f95f6ffd9 100644 --- a/fop-core/src/test/java/org/apache/fop/fonts/truetype/OTFSubSetFileTestCase.java +++ b/fop-core/src/test/java/org/apache/fop/fonts/truetype/OTFSubSetFileTestCase.java @@ -505,10 +505,9 @@ public class OTFSubSetFileTestCase extends OTFFileTestCase { super.createCFF(); } - protected void updateFixedOffsets(Map topDICT, int dataTopDictOffset, - int charsetOffset, int charStringOffset, int encodingOffset) { - this.charsetOffset = charsetOffset; - super.updateFixedOffsets(topDICT, dataTopDictOffset, charsetOffset, charStringOffset, encodingOffset); + protected void updateFixedOffsets(Map topDICT, Offsets offsets) { + this.charsetOffset = offsets.charset; + super.updateFixedOffsets(topDICT, offsets); } } @@ -587,10 +586,10 @@ public class OTFSubSetFileTestCase extends OTFFileTestCase { @Test public void testOrderOfEntries() throws IOException { OTFSubSetFileEntryOrder otfSubSetFile = getFont(3, 2); - assertTrue(otfSubSetFile.fdArrayOffset < otfSubSetFile.charStringOffset); + assertTrue(otfSubSetFile.offsets.fdArray < otfSubSetFile.offsets.charString); otfSubSetFile = getFont(2, 3); - assertTrue(otfSubSetFile.fdArrayOffset > otfSubSetFile.charStringOffset); + assertTrue(otfSubSetFile.offsets.fdArray > otfSubSetFile.offsets.charString); } private OTFSubSetFileEntryOrder getFont(int csLen, int fdLen) throws IOException { @@ -601,8 +600,7 @@ public class OTFSubSetFileTestCase extends OTFFileTestCase { } static class OTFSubSetFileEntryOrder extends OTFSubSetFile { - int fdArrayOffset; - int charStringOffset; + Offsets offsets; int csLen; int fdLen; @@ -635,12 +633,9 @@ public class OTFSubSetFileTestCase extends OTFFileTestCase { super.createCFF(); } - protected void updateCIDOffsets(int topDictDataOffset, int fdArrayOffset, int fdSelectOffset, - int charsetOffset, int charStringOffset, int encodingOffset) { - super.updateCIDOffsets( - topDictDataOffset, fdArrayOffset, fdSelectOffset, charsetOffset, charStringOffset, encodingOffset); - this.fdArrayOffset = fdArrayOffset; - this.charStringOffset = charStringOffset; + protected void updateCIDOffsets(Offsets offsets) { + super.updateCIDOffsets(offsets); + this.offsets = offsets; } } } -- 2.39.5