You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pdfbox.apache.org by le...@apache.org on 2016/10/06 15:28:06 UTC
svn commit: r1763609 - in /pdfbox/branches/2.0:
fontbox/src/main/java/org/apache/fontbox/ttf/CmapSubtable.java
pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDCIDFontType2Embedder.java
Author: lehmi
Date: Thu Oct 6 15:28:06 2016
New Revision: 1763609
URL: http://svn.apache.org/viewvc?rev=1763609&view=rev
Log:
PDFBOX-3300: create widths/unicode mapping when subsetting the font to avoid unnecessary huge mapping tables
Modified:
pdfbox/branches/2.0/fontbox/src/main/java/org/apache/fontbox/ttf/CmapSubtable.java
pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDCIDFontType2Embedder.java
Modified: pdfbox/branches/2.0/fontbox/src/main/java/org/apache/fontbox/ttf/CmapSubtable.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/2.0/fontbox/src/main/java/org/apache/fontbox/ttf/CmapSubtable.java?rev=1763609&r1=1763608&r2=1763609&view=diff
==============================================================================
--- pdfbox/branches/2.0/fontbox/src/main/java/org/apache/fontbox/ttf/CmapSubtable.java (original)
+++ pdfbox/branches/2.0/fontbox/src/main/java/org/apache/fontbox/ttf/CmapSubtable.java Thu Oct 6 15:28:06 2016
@@ -606,7 +606,8 @@ public class CmapSubtable
* @param gid glyph id
* @return character code
*
- * @deprecated the mapping may be ambiguous. The first mapped value is returned by default.
+ * @deprecated the mapping may be ambiguous, see {@link #getCharCodes(int)}. The first mapped value is returned by
+ * default.
*/
public Integer getCharacterCode(int gid)
{
@@ -615,12 +616,13 @@ public class CmapSubtable
{
return null;
}
- // ambiguous mapping, use the first mapping
+ // ambiguous mapping
if (code == Integer.MIN_VALUE)
{
List<Integer> mappedValues = glyphIdToCharacterCodeMultiple.get(gid);
if (mappedValues != null)
{
+ // use the first mapping
return mappedValues.get(0);
}
}
@@ -637,35 +639,30 @@ public class CmapSubtable
}
/**
- * Fills the given map with gid to unicode mappings.
- *
- * @param gidToUni the map to put the mappings into
- * @param maxGid the maximum gid value
+ * Returns all possible character codes for the given gid, or null if there is none.
+ *
+ * @param gid glyph id
+ * @return a list with all character codes the given gid maps to
*
*/
- public void createGID2UnicodeMapping(Map<Integer, Integer> gidToUni, int maxGid)
+ public List<Integer> getCharCodes(int gid)
{
- for (int gid = 1; gid <= maxGid; gid++)
+ int code = getCharCode(gid);
+ if (code == -1)
{
- int codePoint = getCharCode(gid);
- // skip composite glyph components that have no code point
- if (codePoint == -1)
- {
- continue;
- }
- if (codePoint == Integer.MIN_VALUE)
- {
- List<Integer> mappedValues = glyphIdToCharacterCodeMultiple.get(gid);
- for (Integer mappedValue : mappedValues)
- {
- gidToUni.put(gid, mappedValue); // CID = GID
- }
- }
- else
+ return null;
+ }
+ if (code == Integer.MIN_VALUE)
+ {
+ List<Integer> mappedValues = glyphIdToCharacterCodeMultiple.get(gid);
+ if (mappedValues != null)
{
- gidToUni.put(gid, codePoint); // CID = GID
+ return new ArrayList<Integer>(mappedValues);
}
}
+ List<Integer> codes = new ArrayList<Integer>(1);
+ codes.add(code);
+ return codes;
}
@Override
Modified: pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDCIDFontType2Embedder.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDCIDFontType2Embedder.java?rev=1763609&r1=1763608&r2=1763609&view=diff
==============================================================================
--- pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDCIDFontType2Embedder.java (original)
+++ pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDCIDFontType2Embedder.java Thu Oct 6 15:28:06 2016
@@ -23,6 +23,7 @@ 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;
@@ -48,7 +49,6 @@ final class PDCIDFontType2Embedder exten
private final PDType0Font parent;
private final COSDictionary dict;
private final COSDictionary cidFont;
- private final Map<Integer, Integer> gidToUni;
/**
* Creates a new TrueType font embedder for the given TTF as a PDCIDFontType2.
@@ -73,16 +73,16 @@ final class PDCIDFontType2Embedder exten
dict.setItem(COSName.ENCODING, COSName.IDENTITY_H); // CID = GID
// descendant CIDFont
- cidFont = createCIDFont();
+ cidFont = createCIDFont(embedSubset);
COSArray descendantFonts = new COSArray();
descendantFonts.add(cidFont);
dict.setItem(COSName.DESCENDANT_FONTS, descendantFonts);
- // build GID -> Unicode map
- gidToUni = new HashMap<Integer, Integer>(ttf.getMaximumProfile().getNumGlyphs());
- cmap.createGID2UnicodeMapping(gidToUni, ttf.getMaximumProfile().getNumGlyphs());
- // ToUnicode CMap
- buildToUnicodeCMap(null);
+ if (!embedSubset)
+ {
+ // build GID -> Unicode map
+ buildToUnicodeCMap(null);
+ }
}
/**
@@ -100,7 +100,8 @@ final class PDCIDFontType2Embedder exten
int oldGID = entry.getValue();
cidToGid.put(oldGID, newGID);
}
-
+ // build unicode mapping before subsetting as the subsetted font won't have a cmap
+ buildToUnicodeCMap(gidToCid);
// rebuild the relevant part of the font
buildFontFile2(ttfSubset);
addNameTag(tag);
@@ -135,9 +136,11 @@ final class PDCIDFontType2Embedder exten
}
// skip composite glyph components that have no code point
- Integer codePoint = gidToUni.get(cid); // old GID -> Unicode
- if (codePoint != null)
+ List<Integer> codes = cmap.getCharCodes(cid); // old GID -> Unicode
+ if (codes != null)
{
+ // use the first entry even for ambiguous mappings
+ int codePoint = codes.get(0);
if (codePoint > 0xFFFF)
{
hasSurrogates = true;
@@ -174,7 +177,7 @@ final class PDCIDFontType2Embedder exten
return info;
}
- private COSDictionary createCIDFont() throws IOException
+ private COSDictionary createCIDFont(boolean embedSubset) throws IOException
{
COSDictionary cidFont = new COSDictionary();
@@ -193,7 +196,12 @@ final class PDCIDFontType2Embedder exten
cidFont.setItem(COSName.FONT_DESC, fontDescriptor.getCOSObject());
// W - widths
- buildWidths(cidFont);
+ if (!embedSubset)
+ {
+ // subsetted fonts have a reduced amount of widths
+ // and will be created after subsetting
+ buildWidths(cidFont);
+ }
// CIDToGIDMap
cidFont.setItem(COSName.CID_TO_GID_MAP, COSName.IDENTITY);