From 04f38fcea127bccb71770c86984c3ed2ac8d9686 Mon Sep 17 00:00:00 2001 From: Vladimir Osipchuk Date: Tue, 15 Aug 2017 16:02:24 +0300 Subject: [PATCH] Port cb0b508: Copy all global subroutines to GSubr INDEX. DEV-1885 --- .../com/itextpdf/text/pdf/CFFFontSubset.java | 50 ++++++++++++++++++- 1 file changed, 49 insertions(+), 1 deletion(-) diff --git a/itext/src/main/java/com/itextpdf/text/pdf/CFFFontSubset.java b/itext/src/main/java/com/itextpdf/text/pdf/CFFFontSubset.java index b7dde24a96..605216efe1 100644 --- a/itext/src/main/java/com/itextpdf/text/pdf/CFFFontSubset.java +++ b/itext/src/main/java/com/itextpdf/text/pdf/CFFFontSubset.java @@ -484,7 +484,7 @@ else if (fonts[Font].privateSubrs>=0) // Builds the New Local Subrs index NewSubrsIndexNonCID = BuildNewIndex(fonts[Font].SubrsOffsets,hSubrsUsedNonCID,RETURN_OP); //Builds the New Global Subrs index - NewGSubrsIndex = BuildNewIndex(gsubrOffsets,hGSubrsUsed,RETURN_OP); + NewGSubrsIndex = BuildNewIndexAndCopyAllGSubrs(gsubrOffsets, RETURN_OP); } /** @@ -980,6 +980,54 @@ protected byte[] BuildNewIndex(int[] Offsets,HashMap Used,byte O return AssembleIndex(NewOffsets,NewObjects); } + /** + * Function builds the new offset array, object array and assembles the index. + * used for creating the glyph and subrs subsetted index + * + * @param Offsets the offset array of the original index + * @param OperatorForUnusedEntries the operator inserted into the data stream for unused entries + * @return the new index subset version + * @throws java.io.IOException + */ + protected byte[] BuildNewIndexAndCopyAllGSubrs(int[] Offsets, byte OperatorForUnusedEntries) throws java.io.IOException { + int unusedCount = 0; + int Offset = 0; + int[] NewOffsets = new int[Offsets.length]; + // Build the Offsets Array for the Subset + for (int i = 0; i < Offsets.length - 1; ++i) { + NewOffsets[i] = Offset; + Offset += Offsets[i + 1] - Offsets[i]; + } + // Else the same offset is kept in i+1. + NewOffsets[Offsets.length - 1] = Offset; + unusedCount++; + + // Offset var determines the size of the object array + byte[] NewObjects = new byte[Offset + unusedCount]; + // Build the new Object array + int unusedOffset = 0; + for (int i = 0; i < Offsets.length - 1; ++i) { + int start = NewOffsets[i]; + int end = NewOffsets[i + 1]; + NewOffsets[i] = start + unusedOffset; + // If start != End then the Object is used + // So, we will copy the object data from the font file + if (start != end) { + // All offsets are Global Offsets relative to the beginning of the font file. + // Jump the file pointer to the start address to read from. + buf.seek(Offsets[i]); + // Read from the buffer and write into the array at start. + buf.readFully(NewObjects, start + unusedOffset, end - start); + } else { + NewObjects[start + unusedOffset] = OperatorForUnusedEntries; + unusedOffset++; + } + } + NewOffsets[Offsets.length - 1] += unusedOffset; + // Use AssembleIndex to build the index from the offset & object arrays + return AssembleIndex(NewOffsets, NewObjects); + } + /** * Function creates the new index, inserting the count,offsetsize,offset array * and object array.