You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pdfbox.apache.org by ti...@apache.org on 2022/08/28 07:58:40 UTC
svn commit: r1903718 - /pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDCIDFontType2Embedder.java
Author: tilman
Date: Sun Aug 28 07:58:39 2022
New Revision: 1903718
URL: http://svn.apache.org/viewvc?rev=1903718&view=rev
Log:
PDFBOX-4892: optimize with TreeMap
Modified:
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDCIDFontType2Embedder.java
Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDCIDFontType2Embedder.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDCIDFontType2Embedder.java?rev=1903718&r1=1903717&r2=1903718&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDCIDFontType2Embedder.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDCIDFontType2Embedder.java Sun Aug 28 07:58:39 2022
@@ -21,12 +21,10 @@ import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
-import java.util.Collections;
-import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
-import java.util.TreeSet;
+import java.util.TreeMap;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@@ -105,7 +103,7 @@ final class PDCIDFontType2Embedder exten
throws IOException
{
// build CID2GIDMap, because the content stream has been written with the old GIDs
- Map<Integer, Integer> cidToGid = new HashMap<>(gidToCid.size());
+ TreeMap<Integer, Integer> cidToGid = new TreeMap<>();
gidToCid.forEach((newGID, oldGID) -> cidToGid.put(oldGID, newGID));
// build unicode mapping before subsetting as the subsetted font won't have a cmap
@@ -232,9 +230,9 @@ final class PDCIDFontType2Embedder exten
cidFont.setName(COSName.BASE_FONT, newName);
}
- private void buildCIDToGIDMap(Map<Integer, Integer> cidToGid) throws IOException
+ private void buildCIDToGIDMap(TreeMap<Integer, Integer> cidToGid) throws IOException
{
- int cidMax = Collections.max(cidToGid.keySet());
+ int cidMax = cidToGid.lastKey();
byte buffer[] = new byte[cidMax * 2 + 2];
int bi = 0;
for (int i = 0; i <= cidMax; i++)
@@ -259,9 +257,9 @@ final class PDCIDFontType2Embedder exten
* Builds the CIDSet entry, required by PDF/A. This lists all CIDs in the font, including those
* that don't have a GID.
*/
- private void buildCIDSet(Map<Integer, Integer> cidToGid) throws IOException
+ private void buildCIDSet(TreeMap<Integer, Integer> cidToGid) throws IOException
{
- int cidMax = Collections.max(cidToGid.keySet());
+ int cidMax = cidToGid.lastKey();
byte[] bytes = new byte[cidMax / 8 + 1];
for (int cid = 0; cid <= cidMax; cid++)
{
@@ -278,7 +276,7 @@ final class PDCIDFontType2Embedder exten
/**
* Builds widths with a custom CIDToGIDMap (for embedding font subset).
*/
- private void buildWidths(Map<Integer, Integer> cidToGid) throws IOException
+ private void buildWidths(TreeMap<Integer, Integer> cidToGid) throws IOException
{
float scaling = 1000f / ttf.getHeader().getUnitsPerEm();
@@ -286,7 +284,7 @@ final class PDCIDFontType2Embedder exten
COSArray ws = new COSArray();
int prev = Integer.MIN_VALUE;
// Use a sorted list to get an optimal width array
- Set<Integer> keys = new TreeSet<>(cidToGid.keySet());
+ Set<Integer> keys = cidToGid.keySet();
HorizontalMetricsTable horizontalMetricsTable = ttf.getHorizontalMetrics();
for (int cid : keys)
{
@@ -336,7 +334,7 @@ final class PDCIDFontType2Embedder exten
/**
* Builds vertical metrics with a custom CIDToGIDMap (for embedding font subset).
*/
- private void buildVerticalMetrics(Map<Integer, Integer> cidToGid) throws IOException
+ private void buildVerticalMetrics(TreeMap<Integer, Integer> cidToGid) throws IOException
{
// The "vhea" and "vmtx" tables that specify vertical metrics shall never be used by a conforming
// reader. The only way to specify vertical metrics in PDF shall be by means of the DW2 and W2
@@ -361,7 +359,7 @@ final class PDCIDFontType2Embedder exten
COSArray w2 = new COSArray();
int prev = Integer.MIN_VALUE;
// Use a sorted list to get an optimal width array
- Set<Integer> keys = new TreeSet<>(cidToGid.keySet());
+ Set<Integer> keys = cidToGid.keySet();
for (int cid : keys)
{
// Unlike buildWidths, we look up with cid (not gid) here because this is