You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pdfbox.apache.org by ja...@apache.org on 2014/06/19 00:04:48 UTC
svn commit: r1603653 - in /pdfbox/trunk:
pdfbox/src/main/java/org/apache/pdfbox/encoding/
pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/
pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/
pdfbox/src/main/java/org/apache/pdfbox/rende...
Author: jahewson
Date: Wed Jun 18 22:04:47 2014
New Revision: 1603653
URL: http://svn.apache.org/r1603653
Log:
PDFBOX-2149: Font Refactoring, Phase 1: PDFont, Encoding, TTFGlyph2D
Added:
pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/font/PDFontTest.java
- copied, changed from r1603605, pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/font/PDSimpleFontTest.java
Removed:
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/encoding/EncodingManager.java
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDSimpleFont.java
pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/font/PDSimpleFontTest.java
Modified:
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/encoding/DictionaryEncoding.java
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/encoding/Encoding.java
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDCIDFont.java
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDCIDFontType0Font.java
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDCIDFontType2Font.java
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDFont.java
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDFontFactory.java
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDMMType1Font.java
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDTrueTypeFont.java
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDType0Font.java
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDType1AfmPfbFont.java
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDType1CFont.java
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDType1Font.java
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDType3Font.java
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDAppearanceString.java
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/rendering/PageDrawer.java
pdfbox/trunk/preflight/src/main/java/org/apache/pdfbox/preflight/font/Type3FontValidator.java
pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools/TextToPDF.java
Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/encoding/DictionaryEncoding.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/encoding/DictionaryEncoding.java?rev=1603653&r1=1603652&r2=1603653&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/encoding/DictionaryEncoding.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/encoding/DictionaryEncoding.java Wed Jun 18 22:04:47 2014
@@ -60,8 +60,7 @@ public class DictionaryEncoding extends
COSName baseEncodingName =
(COSName) encoding.getDictionaryObject(COSName.BASE_ENCODING);
if (baseEncodingName != null) {
- baseEncoding =
- EncodingManager.INSTANCE.getEncoding(baseEncodingName);
+ baseEncoding = Encoding.getInstance(baseEncodingName);
}
nameToCode.putAll( baseEncoding.nameToCode );
Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/encoding/Encoding.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/encoding/Encoding.java?rev=1603653&r1=1603652&r2=1603653&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/encoding/Encoding.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/encoding/Encoding.java Wed Jun 18 22:04:47 2014
@@ -28,22 +28,48 @@ import java.util.StringTokenizer;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
+import org.apache.pdfbox.cos.COSName;
import org.apache.pdfbox.pdmodel.common.COSObjectable;
import org.apache.pdfbox.util.ResourceLoader;
/**
* This is an interface to a text encoder.
*
- * @author <a href="mailto:ben@benlitchfield.com">Ben Litchfield</a>
- *
+ * @author Ben Litchfield
*/
public abstract class Encoding implements COSObjectable
{
+ private static final Log LOG = LogFactory.getLog(Encoding.class);
/**
- * Log instance.
+ * This will get an encoding by name.
+ *
+ * @param name The name of the encoding to get.
+ * @return The encoding that matches the name.
+ * @throws IOException if there is no encoding with that name.
*/
- private static final Log LOG = LogFactory.getLog(Encoding.class);
+ public static Encoding getInstance(COSName name) throws IOException {
+ if (COSName.STANDARD_ENCODING.equals(name))
+ {
+ return StandardEncoding.INSTANCE;
+ }
+ else if (COSName.WIN_ANSI_ENCODING.equals(name))
+ {
+ return WinAnsiEncoding.INSTANCE;
+ }
+ else if (COSName.MAC_ROMAN_ENCODING.equals(name))
+ {
+ return MacRomanEncoding.INSTANCE;
+ }
+ else if (COSName.PDF_DOC_ENCODING.equals(name))
+ {
+ return PdfDocEncoding.INSTANCE;
+ }
+ else
+ {
+ throw new IOException("Unknown encoding for '" + name.getName() + "'");
+ }
+ }
/** Identifies a non-mapped character. */
public static final String NOTDEF = ".notdef";
Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDCIDFont.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDCIDFont.java?rev=1603653&r1=1603652&r2=1603653&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDCIDFont.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDCIDFont.java Wed Jun 18 22:04:47 2014
@@ -37,7 +37,7 @@ import org.apache.pdfbox.util.ResourceLo
*
* @author Ben Litchfield
*/
-public abstract class PDCIDFont extends PDSimpleFont
+public abstract class PDCIDFont extends PDFont
{
private static final Log LOG = LogFactory.getLog(PDCIDFont.class);
@@ -46,18 +46,10 @@ public abstract class PDCIDFont extends
/**
* Constructor.
- */
- public PDCIDFont()
- {
- super();
- }
-
- /**
- * Constructor.
*
* @param fontDictionary The font dictionary according to the PDF specification.
*/
- public PDCIDFont(COSDictionary fontDictionary)
+ protected PDCIDFont(COSDictionary fontDictionary)
{
super(fontDictionary);
extractWidths();
@@ -84,7 +76,7 @@ public abstract class PDCIDFont extends
{
if (defaultWidth == 0)
{
- COSNumber number = (COSNumber) font.getDictionaryObject(COSName.DW);
+ COSNumber number = (COSNumber) dict.getDictionaryObject(COSName.DW);
if (number != null)
{
defaultWidth = number.intValue();
@@ -98,17 +90,6 @@ public abstract class PDCIDFont extends
}
/**
- * This will set the default width for the glyphs of this font.
- *
- * @param dw The default width.
- */
- public void setDefaultWidth(long dw)
- {
- defaultWidth = dw;
- font.setLong(COSName.DW, dw);
- }
-
- /**
* This will get the font width for a character.
*
* @param c The character code to get the width for.
@@ -136,7 +117,7 @@ public abstract class PDCIDFont extends
if (widthCache == null)
{
widthCache = new HashMap<Integer, Float>();
- COSArray widths = (COSArray) font.getDictionaryObject(COSName.W);
+ COSArray widths = (COSArray) dict.getDictionaryObject(COSName.W);
if (widths != null)
{
int size = widths.size();
@@ -227,7 +208,7 @@ public abstract class PDCIDFont extends
{
float totalWidths = 0.0f;
float characterCount = 0.0f;
- COSArray widths = (COSArray) font.getDictionaryObject(COSName.W);
+ COSArray widths = (COSArray) dict.getDictionaryObject(COSName.W);
if (widths != null)
{
@@ -283,7 +264,7 @@ public abstract class PDCIDFont extends
private String getCIDSystemInfo()
{
String cidSystemInfo = null;
- COSDictionary dict = (COSDictionary)font.getDictionaryObject(COSName.CIDSYSTEMINFO);
+ COSDictionary dict = (COSDictionary) this.dict.getDictionaryObject(COSName.CIDSYSTEMINFO);
if (dict != null)
{
String ordering = dict.getString(COSName.ORDERING);
@@ -294,62 +275,62 @@ public abstract class PDCIDFont extends
return cidSystemInfo;
}
+ // todo: do we want to do this at all? Isn't the parent Type0 font responsible for this?
@Override
protected void determineEncoding()
{
String cidSystemInfo = getCIDSystemInfo();
- if (cidSystemInfo != null)
+ if (cidSystemInfo == null)
{
- if (cidSystemInfo.contains("Identity"))
- {
- cidSystemInfo = "Identity-H";
- }
- else if (cidSystemInfo.startsWith("Adobe-UCS-"))
- {
- cidSystemInfo = "Adobe-Identity-UCS";
- }
- else
- {
- cidSystemInfo = cidSystemInfo.substring(0, cidSystemInfo.lastIndexOf('-')) +
- "-UCS2";
- }
- cmap = cmapObjects.get(cidSystemInfo);
- if (cmap == null)
+ // todo: CIDSystemInfo is required, so this is an error (perform recovery?)
+ LOG.error("Missing CIDSystemInfo in CIDFont dictionary");
+ return;
+ }
+
+ if (cidSystemInfo.contains("Identity"))
+ {
+ cidSystemInfo = "Identity-H";
+ }
+ else if (cidSystemInfo.startsWith("Adobe-UCS-"))
+ {
+ cidSystemInfo = "Adobe-Identity-UCS";
+ }
+ else
+ {
+ cidSystemInfo = cidSystemInfo.substring(0, cidSystemInfo.lastIndexOf("-")) + "-UCS2";
+ }
+
+ cmap = cmapObjects.get(cidSystemInfo);
+ if (cmap == null)
+ {
+ InputStream cmapStream = null;
+ try
{
- InputStream cmapStream = null;
- try
+ // look for a predefined CMap with the given name
+ cmapStream = ResourceLoader.loadResource(resourceRootCMAP + cidSystemInfo);
+ if (cmapStream != null)
{
- // look for a predefined CMap with the given name
- cmapStream = ResourceLoader.loadResource(resourceRootCMAP + cidSystemInfo);
- if (cmapStream != null)
- {
- cmap = parseCmap(resourceRootCMAP, cmapStream);
- if (cmap == null)
- {
- LOG.error("Error: Could not parse predefined CMAP file for '" +
- cidSystemInfo + "'");
- }
- }
- else
+ cmap = parseCmap(resourceRootCMAP, cmapStream);
+ if (cmap == null)
{
- LOG.debug("Debug: '" + cidSystemInfo + "' isn't a predefined CMap, most " +
- "likely it's embedded in the pdf itself.");
+ LOG.error("Could not parse predefined CMAP file for '" +
+ cidSystemInfo + "'");
}
}
- catch (IOException exception)
- {
- LOG.error("Error: Could not find predefined CMAP file for '" + cidSystemInfo +
- "'");
- }
- finally
+ else
{
- IOUtils.closeQuietly(cmapStream);
+ LOG.debug("'" + cidSystemInfo + "' isn't a predefined CMap, most " +
+ "likely it's embedded in the pdf itself.");
}
}
- }
- else
- {
- super.determineEncoding();
+ catch (IOException exception)
+ {
+ LOG.error("Could not find predefined CMAP file for '" + cidSystemInfo + "'");
+ }
+ finally
+ {
+ IOUtils.closeQuietly(cmapStream);
+ }
}
}
Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDCIDFontType0Font.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDCIDFontType0Font.java?rev=1603653&r1=1603652&r2=1603653&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDCIDFontType0Font.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDCIDFontType0Font.java Wed Jun 18 22:04:47 2014
@@ -21,7 +21,6 @@ import java.io.IOException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.pdfbox.cos.COSDictionary;
-import org.apache.pdfbox.cos.COSName;
/**
* A Type0 CIDFont (CFF).
@@ -30,24 +29,12 @@ import org.apache.pdfbox.cos.COSName;
*/
public class PDCIDFontType0Font extends PDCIDFont
{
- /**
- * Log instance.
- */
private static final Log LOG = LogFactory.getLog(PDCIDFontType0Font.class);
private PDType1CFont type1CFont = null;
/**
* Constructor.
- */
- public PDCIDFontType0Font()
- {
- super();
- font.setItem(COSName.SUBTYPE, COSName.CID_FONT_TYPE0);
- }
-
- /**
- * Constructor.
*
* @param fontDictionary The font dictionary according to the PDF specification.
*/
@@ -62,7 +49,7 @@ public class PDCIDFontType0Font extends
{
try
{
- type1CFont = new PDType1CFont(font);
+ type1CFont = new PDType1CFont(dict);
}
catch (IOException exception)
{
Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDCIDFontType2Font.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDCIDFontType2Font.java?rev=1603653&r1=1603652&r2=1603653&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDCIDFontType2Font.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDCIDFontType2Font.java Wed Jun 18 22:04:47 2014
@@ -45,15 +45,6 @@ public class PDCIDFontType2Font extends
/**
* Constructor.
- */
- public PDCIDFontType2Font()
- {
- super();
- font.setItem(COSName.SUBTYPE, COSName.CID_FONT_TYPE2);
- }
-
- /**
- * Constructor.
*
* @param fontDictionary The font dictionary according to the PDF specification.
*/
@@ -71,7 +62,7 @@ public class PDCIDFontType2Font extends
{
if (hasCIDToGIDMap == null)
{
- COSBase map = font.getDictionaryObject(COSName.CID_TO_GID_MAP);
+ COSBase map = dict.getDictionaryObject(COSName.CID_TO_GID_MAP);
if (map != null && map instanceof COSStream)
{
hasCIDToGIDMap = Boolean.TRUE;
@@ -93,7 +84,7 @@ public class PDCIDFontType2Font extends
{
if (hasIdentityCIDToGIDMap == null)
{
- COSBase map = font.getDictionaryObject(COSName.CID_TO_GID_MAP);
+ COSBase map = dict.getDictionaryObject(COSName.CID_TO_GID_MAP);
if (map != null && map instanceof COSName)
{
hasIdentityCIDToGIDMap = Boolean.TRUE;
@@ -152,7 +143,7 @@ public class PDCIDFontType2Font extends
private void readCIDToGIDMapping()
{
- COSBase map = font.getDictionaryObject(COSName.CID_TO_GID_MAP);
+ COSBase map = dict.getDictionaryObject(COSName.CID_TO_GID_MAP);
if (map instanceof COSStream)
{
COSStream stream = (COSStream) map;
@@ -179,7 +170,7 @@ public class PDCIDFontType2Font extends
/**
* Returns the embedded true type font.
- *
+ *
* @return the true type font
* @throws IOException exception if something went wrong
*/
@@ -195,4 +186,24 @@ public class PDCIDFontType2Font extends
}
return trueTypeFont;
}
+
+ @Override
+ public float getFontWidth(byte[] c, int offset, int length) throws IOException
+ {
+ // a suitable mapping is needed to address the correct width value
+ int code = getCodeFromArray(c, offset, length);
+ if (hasIdentityCIDToGIDMap() || hasCIDToGIDMap())
+ {
+ return getFontWidth(code);
+ }
+ else if (getCMap() != null)
+ {
+ String mappedString = getCMap().lookup(code, length);
+ if (mappedString != null)
+ {
+ return getFontWidth(mappedString.codePointAt(0));
+ }
+ }
+ return super.getFontWidth(c, offset, length);
+ }
}
Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDFont.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDFont.java?rev=1603653&r1=1603652&r2=1603653&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDFont.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDFont.java Wed Jun 18 22:04:47 2014
@@ -35,11 +35,16 @@ import org.apache.pdfbox.cos.COSDictiona
import org.apache.pdfbox.cos.COSFloat;
import org.apache.pdfbox.cos.COSInteger;
import org.apache.pdfbox.cos.COSName;
+import org.apache.pdfbox.cos.COSNumber;
+import org.apache.pdfbox.cos.COSStream;
+import org.apache.pdfbox.encoding.DictionaryEncoding;
import org.apache.pdfbox.encoding.Encoding;
+import org.apache.pdfbox.io.IOUtils;
import org.apache.pdfbox.pdmodel.common.COSArrayList;
import org.apache.pdfbox.pdmodel.common.COSObjectable;
import org.apache.pdfbox.pdmodel.common.PDMatrix;
import org.apache.pdfbox.pdmodel.common.PDRectangle;
+import org.apache.pdfbox.util.ResourceLoader;
/**
* This is the base class for all PDF fonts.
@@ -49,38 +54,90 @@ import org.apache.pdfbox.pdmodel.common.
public abstract class PDFont implements COSObjectable
{
private static final Log LOG = LogFactory.getLog(PDFont.class);
+ private static final byte[] SPACE_BYTES = { (byte) 32 }; // formerly in PDSimpleFont
protected static final String resourceRootCMAP = "org/apache/pdfbox/resources/cmap/";
-
protected static Map<String, CMap> cmapObjects =
Collections.synchronizedMap(new HashMap<String, CMap>()); // todo: why synchronized?
+ private static final String[] SINGLE_CHAR_STRING = new String[256];
+ private static final String[][] DOUBLE_CHAR_STRING = new String[256][256];
+ static
+ {
+ for (int i = 0; i < 256; i++)
+ {
+ try
+ {
+ SINGLE_CHAR_STRING[i] = new String(new byte[] { (byte) i }, "ISO-8859-1");
+ }
+ catch (UnsupportedEncodingException e)
+ {
+ // Nothing should happen here
+ LOG.error(e,e);
+ }
+ for (int j = 0; j < 256; j++)
+ {
+ try
+ {
+ DOUBLE_CHAR_STRING[i][j] = new String(new byte[] { (byte) i, (byte) j },
+ "UTF-16BE");
+ }
+ catch (UnsupportedEncodingException e)
+ {
+ // Nothing should happen here
+ LOG.error(e, e);
+ }
+ }
+ }
+ }
+
+ private static String getStringFromArray(byte[] c, int offset, int length) throws IOException
+ {
+ String retval;
+ if (length == 1)
+ {
+ retval = SINGLE_CHAR_STRING[(c[offset] + 256) % 256];
+ }
+ else if (length == 2)
+ {
+ retval = DOUBLE_CHAR_STRING[(c[offset] + 256) % 256][(c[offset + 1] + 256) % 256];
+ }
+ else
+ {
+ throw new IOException("Error:Unknown character length:" + length);
+ }
+ return retval;
+ }
+
/**
- * The cos dictionary for this font.
+ * The Font dictionary.
*/
- protected COSDictionary font;
+ protected COSDictionary dict;
/**
* The font matrix.
*/
protected PDMatrix fontMatrix = null;
- /**
- * Used only if this is a font object and it has an encoding and it is a Type0 font with a CMap.
- */
- protected CMap cmap = null;
+ // CMap / Encoding
+ protected CMap cmap = null; // only used when this is a Type0 font with a CMap
+ protected Encoding fontEncoding = null; // only used when this font has an encoding
- /**
- * The CMap holding the ToUnicode mapping.
- */
- protected CMap toUnicodeCmap = null;
+ // the CMap holding the ToUnicode mapping
+ private CMap toUnicodeCmap = null;
+ private boolean hasToUnicode = false;
private List<Integer> widths = null;
- private Encoding fontEncoding = null; // only used when this font has an encoding
+
private PDFontDescriptor fontDescriptor = null;
- private boolean hasToUnicode = false;
private boolean widthsAreMissing = false;
+ // formerly in PDSimpleFont
+ private final HashMap<Integer, Float> fontSizes = new HashMap<Integer, Float>(128);
+ private float avgFontWidth = 0.0f;
+ private float avgFontHeight = 0.0f;
+ private float fontWidthOfSpace = -1f;
+
/**
* This will clear AFM resources that are stored statically. This is usually not a problem
* unless you want to reclaim resources for a long running process.
@@ -89,7 +146,10 @@ public abstract class PDFont implements
* reside until PDFont is mature enough to take them over. PDFont is the appropriate place for
* them and not in COSObject but we need font calculations for text extraction. THIS METHOD WILL
* BE MOVED OR REMOVED TO ANOTHER LOCATION IN A FUTURE VERSION OF PDFBOX.
+ *
+ * @deprecated This method will be removed in a future version of PDFBox.
*/
+ @Deprecated
public static void clearResources()
{
cmapObjects.clear();
@@ -98,10 +158,10 @@ public abstract class PDFont implements
/**
* Constructor.
*/
- public PDFont()
+ protected PDFont()
{
- font = new COSDictionary();
- font.setItem(COSName.TYPE, COSName.FONT);
+ dict = new COSDictionary();
+ dict.setItem(COSName.TYPE, COSName.FONT);
}
/**
@@ -109,9 +169,9 @@ public abstract class PDFont implements
*
* @param fontDictionary The font dictionary according to the PDF specification.
*/
- public PDFont(COSDictionary fontDictionary)
+ protected PDFont(COSDictionary fontDictionary)
{
- font = fontDictionary;
+ dict = fontDictionary;
determineEncoding();
}
@@ -124,7 +184,7 @@ public abstract class PDFont implements
{
if (fontDescriptor == null)
{
- COSDictionary fd = (COSDictionary) font.getDictionaryObject(COSName.FONT_DESC);
+ COSDictionary fd = (COSDictionary) dict.getDictionaryObject(COSName.FONT_DESC);
if (fd != null)
{
fontDescriptor = new PDFontDescriptorDictionary(fd);
@@ -142,31 +202,96 @@ public abstract class PDFont implements
}
/**
- * This will set the font descriptor.
- *
- * @param fdDictionary The font descriptor.
+ * Determines the encoding for the font. This method as to be overwritten, as there are
+ * different possibilities to define a mapping.
*/
- public void setFontDescriptor(PDFontDescriptorDictionary fdDictionary)
+ protected void determineEncoding()
{
- COSDictionary dic = null;
- if (fdDictionary != null)
+ COSBase encoding = dict.getDictionaryObject(COSName.ENCODING);
+ Encoding fontEncoding = null;
+ if (encoding != null)
{
- dic = fdDictionary.getCOSDictionary();
+ if (encoding instanceof COSName)
+ {
+ COSName encodingName = (COSName)encoding;
+ try
+ {
+ fontEncoding = Encoding.getInstance(encodingName);
+ }
+ catch (IOException exception)
+ {
+ LOG.debug("Debug: Could not find encoding for " + encodingName);
+ }
+ }
+ else if (encoding instanceof COSDictionary)
+ {
+ try
+ {
+ fontEncoding = new DictionaryEncoding((COSDictionary) encoding);
+ }
+ catch (IOException exception)
+ {
+ LOG.error("Error: Could not create the DictionaryEncoding");
+ }
+ }
}
- font.setItem(COSName.FONT_DESC, dic);
- fontDescriptor = fdDictionary;
+ this.fontEncoding = fontEncoding;
+ extractToUnicodeEncoding();
}
- /**
- * Determines the encoding for the font. This method as to be overwritten, as there are
- * different possibilities to define a mapping.
- */
- protected abstract void determineEncoding();
+ protected final void extractToUnicodeEncoding()
+ {
+ COSName encodingName;
+ String cmapName;
+ COSBase toUnicode = dict.getDictionaryObject(COSName.TO_UNICODE);
+ if (toUnicode != null)
+ {
+ hasToUnicode = true;
+ if (toUnicode instanceof COSStream)
+ {
+ try
+ {
+ InputStream is = ((COSStream) toUnicode).getUnfilteredStream();
+ toUnicodeCmap = parseCmap(resourceRootCMAP, is);
+ IOUtils.closeQuietly(is);
+ }
+ catch (IOException exception)
+ {
+ LOG.error("Error: Could not load embedded ToUnicode CMap");
+ }
+ }
+ else if (toUnicode instanceof COSName)
+ {
+ encodingName = (COSName) toUnicode;
+ toUnicodeCmap = cmapObjects.get(encodingName.getName());
+ if (toUnicodeCmap == null)
+ {
+ cmapName = encodingName.getName();
+ String resourceName = resourceRootCMAP + cmapName;
+ try
+ {
+ toUnicodeCmap = parseCmap(resourceRootCMAP,
+ ResourceLoader.loadResource(resourceName));
+ }
+ catch (IOException exception)
+ {
+ LOG.error("Error: Could not find predefined ToUnicode CMap file for '" +
+ cmapName + "'");
+ }
+ if (toUnicodeCmap == null)
+ {
+ LOG.error("Error: Could not parse predefined ToUnicode CMap file for '" +
+ cmapName + "'");
+ }
+ }
+ }
+ }
+ }
@Override
public COSBase getCOSObject()
{
- return font;
+ return dict;
}
/**
@@ -178,7 +303,22 @@ public abstract class PDFont implements
* @return The width is in 1000 unit of text space, ie 333 or 777
* @throws IOException If an error occurs while parsing.
*/
- public abstract float getFontWidth(byte[] c, int offset, int length) throws IOException;
+ public float getFontWidth(byte[] c, int offset, int length) throws IOException
+ {
+ int code = getCodeFromArray(c, offset, length);
+ Float fontWidth = fontSizes.get(code);
+ if (fontWidth == null)
+ {
+ fontWidth = getFontWidth(code);
+ if (fontWidth <= 0)
+ {
+ // TODO should this be in PDType1Font??
+ fontWidth = getFontWidthFromAFMFile(code);
+ }
+ fontSizes.put(code, fontWidth);
+ }
+ return fontWidth;
+ }
/**
* This will get the font height for a character.
@@ -189,7 +329,56 @@ public abstract class PDFont implements
* @return The height is in 1000 unit of text space, ie 333 or 777
* @throws IOException If an error occurs while parsing.
*/
- public abstract float getFontHeight(byte[] c, int offset, int length) throws IOException;
+ public float getFontHeight(byte[] c, int offset, int length) throws IOException
+ {
+ // maybe there is already a precalculated value
+ if (avgFontHeight > 0)
+ {
+ return avgFontHeight;
+ }
+ float retval = 0;
+ FontMetric metric = getAFM();
+ if (metric != null)
+ {
+ int code = getCodeFromArray(c, offset, length);
+ Encoding encoding = getFontEncoding();
+ String characterName = encoding.getName(code);
+ retval = metric.getCharacterHeight(characterName);
+ }
+ else
+ {
+ PDFontDescriptor desc = getFontDescriptor();
+ if (desc != null)
+ {
+ // the following values are all more or less accurate at least all are average
+ // values. Maybe we'll find another way to get those value for every single glyph
+ // in the future if needed
+ PDRectangle fontBBox = desc.getFontBoundingBox();
+ if (fontBBox != null)
+ {
+ retval = fontBBox.getHeight() / 2;
+ }
+ if (retval == 0)
+ {
+ retval = desc.getCapHeight();
+ }
+ if (retval == 0)
+ {
+ retval = desc.getAscent();
+ }
+ if (retval == 0)
+ {
+ retval = desc.getXHeight();
+ if (retval > 0)
+ {
+ retval -= desc.getDescent();
+ }
+ }
+ avgFontHeight = retval;
+ }
+ }
+ return retval;
+ }
/**
* This will get the width of this string for this font.
@@ -215,7 +404,43 @@ public abstract class PDFont implements
* @return The width is in 1000 unit of text space, ie 333 or 777
* @throws IOException If an error occurs while parsing.
*/
- public abstract float getAverageFontWidth() throws IOException;
+ public float getAverageFontWidth() throws IOException
+ {
+ float average;
+ if (avgFontWidth != 0.0f)
+ {
+ average = avgFontWidth;
+ }
+ else
+ {
+ float totalWidth = 0.0f;
+ float characterCount = 0.0f;
+ COSArray widths = (COSArray) dict.getDictionaryObject(COSName.WIDTHS);
+ if (widths != null)
+ {
+ for (int i = 0; i < widths.size(); i++)
+ {
+ COSNumber fontWidth = (COSNumber) widths.getObject(i);
+ if (fontWidth.floatValue() > 0)
+ {
+ totalWidth += fontWidth.floatValue();
+ characterCount += 1;
+ }
+ }
+ }
+
+ if (totalWidth > 0)
+ {
+ average = totalWidth / characterCount;
+ }
+ else
+ {
+ average = getAverageFontWidthFromAFMFile();
+ }
+ avgFontWidth = average;
+ }
+ return average;
+ }
/**
* Used for multibyte encodings.
@@ -243,7 +468,7 @@ public abstract class PDFont implements
* @return The font width from the AFM file.
* @throws IOException if we cannot find the width.
*/
- protected float getFontWidthFromAFMFile(int code) throws IOException
+ private float getFontWidthFromAFMFile(int code) throws IOException
{
float retval = 0;
FontMetric metric = getAFM();
@@ -261,7 +486,7 @@ public abstract class PDFont implements
* @return The average font width from the AFM file.
* @throws IOException if we cannot find the width.
*/
- protected float getAverageFontWidthFromAFMFile() throws IOException
+ private float getAverageFontWidthFromAFMFile() throws IOException
{
float retval = 0;
FontMetric metric = getAFM();
@@ -282,39 +507,6 @@ public abstract class PDFont implements
return null;
}
- private COSBase encoding = null;
-
- /**
- * Cache the {@link COSName#ENCODING} object from the font's dictionary as it is called often.
- * <p>
- * Use this method instead of
- *
- * <pre>
- * font.getDictionaryObject(COSName.ENCODING);
- * </pre>
- *
- * @return the encoding
- */
- protected COSBase getEncoding()
- {
- if (encoding == null)
- {
- encoding = font.getDictionaryObject(COSName.ENCODING);
- }
- return encoding;
- }
-
- /**
- * Set the encoding object from the fonts dictionary.
- *
- * @param encodingValue the given encoding.
- */
- protected void setEncoding(COSBase encodingValue)
- {
- font.setItem(COSName.ENCODING, encodingValue);
- encoding = encodingValue;
- }
-
/**
* Encode the given value using the CMap of the font.
*
@@ -325,7 +517,7 @@ public abstract class PDFont implements
* @return The value of the encoded character.
* @throws IOException if something went wrong
*/
- protected String cmapEncoding(int code, int length, boolean isCIDFont, CMap sourceCmap)
+ protected final String cmapEncoding(int code, int length, boolean isCIDFont, CMap sourceCmap)
throws IOException
{
String retval = null;
@@ -392,55 +584,6 @@ public abstract class PDFont implements
return code;
}
- private static final String[] SINGLE_CHAR_STRING = new String[256];
- private static final String[][] DOUBLE_CHAR_STRING = new String[256][256];
- static
- {
- for (int i = 0; i < 256; i++)
- {
- try
- {
- SINGLE_CHAR_STRING[i] = new String(new byte[] { (byte) i }, "ISO-8859-1");
- }
- catch (UnsupportedEncodingException e)
- {
- // Nothing should happen here
- LOG.error(e,e);
- }
- for (int j = 0; j < 256; j++)
- {
- try
- {
- DOUBLE_CHAR_STRING[i][j] = new String(new byte[] { (byte) i, (byte) j },
- "UTF-16BE");
- }
- catch (UnsupportedEncodingException e)
- {
- // Nothing should happen here
- LOG.error(e, e);
- }
- }
- }
- }
-
- private static String getStringFromArray(byte[] c, int offset, int length) throws IOException
- {
- String retval;
- if (length == 1)
- {
- retval = SINGLE_CHAR_STRING[(c[offset] + 256) % 256];
- }
- else if (length == 2)
- {
- retval = DOUBLE_CHAR_STRING[(c[offset] + 256) % 256][(c[offset + 1] + 256) % 256];
- }
- else
- {
- throw new IOException("Error:Unknown character length:" + length);
- }
- return retval;
- }
-
/**
* Parse the given CMap.
*
@@ -448,7 +591,7 @@ public abstract class PDFont implements
* @param cmapStream the CMap to be read
* @return the parsed CMap
*/
- protected CMap parseCmap(String cmapRoot, InputStream cmapStream)
+ protected final CMap parseCmap(String cmapRoot, InputStream cmapStream)
{
CMap targetCmap = null;
if (cmapStream != null)
@@ -472,16 +615,6 @@ public abstract class PDFont implements
}
/**
- * The will set the encoding for this font.
- *
- * @param enc The font encoding.
- */
- public void setFontEncoding(Encoding enc)
- {
- fontEncoding = enc;
- }
-
- /**
* This will get or create the encoder.
*
* @return The encoding to use.
@@ -498,32 +631,17 @@ public abstract class PDFont implements
*/
public String getType()
{
- return font.getNameAsString(COSName.TYPE);
+ return dict.getNameAsString(COSName.TYPE);
}
- // Memorized values to avoid repeated dictionary lookups
- private String subtype = null;
- private boolean type1Font;
- private boolean type3Font;
- private boolean trueTypeFont;
- private boolean type0Font;
-
/**
- * This will get the subtype of font, Type1, Type3, ...
+ * This will get the subtype of font.
*
* @return The type of font that this is.
*/
public String getSubType()
{
- if (subtype == null)
- {
- subtype = font.getNameAsString(COSName.SUBTYPE);
- type1Font = "Type1".equals(subtype);
- trueTypeFont = "TrueType".equals(subtype);
- type0Font = "Type0".equals(subtype);
- type3Font = "Type3".equals(subtype);
- }
- return subtype;
+ return dict.getNameAsString(COSName.SUBTYPE);
}
/**
@@ -533,8 +651,7 @@ public abstract class PDFont implements
*/
public boolean isType1Font()
{
- getSubType();
- return type1Font;
+ return "Type1".equals(getSubType());
}
/**
@@ -544,8 +661,7 @@ public abstract class PDFont implements
*/
public boolean isType3Font()
{
- getSubType();
- return type3Font;
+ return "Type3".equals(getSubType());
}
/**
@@ -555,8 +671,7 @@ public abstract class PDFont implements
*/
public boolean isType0Font()
{
- getSubType();
- return type0Font;
+ return "Type0".equals(getSubType());
}
/**
@@ -566,8 +681,7 @@ public abstract class PDFont implements
*/
public boolean isTrueTypeFont()
{
- getSubType();
- return trueTypeFont;
+ return "TrueType".equals(getSubType());
}
/**
@@ -577,11 +691,7 @@ public abstract class PDFont implements
*/
public boolean isSymbolicFont()
{
- if (getFontDescriptor() != null)
- {
- return getFontDescriptor().isSymbolic();
- }
- return false;
+ return getFontDescriptor().isSymbolic();
}
/**
@@ -591,17 +701,7 @@ public abstract class PDFont implements
*/
public String getBaseFont()
{
- return font.getNameAsString(COSName.BASE_FONT);
- }
-
- /**
- * Set the PostScript name of the font.
- *
- * @param baseFont The postscript name for the font.
- */
- public void setBaseFont(String baseFont)
- {
- font.setName(COSName.BASE_FONT, baseFont);
+ return dict.getNameAsString(COSName.BASE_FONT);
}
/**
@@ -611,17 +711,7 @@ public abstract class PDFont implements
*/
public int getFirstChar()
{
- return font.getInt(COSName.FIRST_CHAR, -1);
- }
-
- /**
- * Set the first character this font supports.
- *
- * @param firstChar The first character.
- */
- public void setFirstChar(int firstChar)
- {
- font.setInt(COSName.FIRST_CHAR, firstChar);
+ return dict.getInt(COSName.FIRST_CHAR, -1);
}
/**
@@ -631,17 +721,7 @@ public abstract class PDFont implements
*/
public int getLastChar()
{
- return font.getInt(COSName.LAST_CHAR, -1);
- }
-
- /**
- * Set the last character this font supports.
- *
- * @param lastChar The last character.
- */
- public void setLastChar(int lastChar)
- {
- font.setInt(COSName.LAST_CHAR, lastChar);
+ return dict.getInt(COSName.LAST_CHAR, -1);
}
/**
@@ -653,7 +733,7 @@ public abstract class PDFont implements
{
if (widths == null && !widthsAreMissing)
{
- COSArray array = (COSArray) font.getDictionaryObject(COSName.WIDTHS);
+ COSArray array = (COSArray) dict.getDictionaryObject(COSName.WIDTHS);
if (array != null)
{
widths = COSArrayList.convertIntegerCOSArrayToList(array);
@@ -667,17 +747,6 @@ public abstract class PDFont implements
}
/**
- * Set the widths of the characters code.
- *
- * @param widthsList The widths of the character codes.
- */
- public void setWidths(List<Integer> widthsList)
- {
- widths = widthsList;
- font.setItem(COSName.WIDTHS, COSArrayList.converterToCOSArray(widths));
- }
-
- /**
* This will get the matrix that is used to transform glyph space to text space. By default
* there are 1000 glyph units to 1 text space unit, but type3 fonts can use any value.
*
@@ -690,7 +759,7 @@ public abstract class PDFont implements
{
if (fontMatrix == null)
{
- COSArray array = (COSArray) font.getDictionaryObject(COSName.FONT_MATRIX);
+ COSArray array = (COSArray) dict.getDictionaryObject(COSName.FONT_MATRIX);
if (array == null)
{
array = new COSArray();
@@ -712,7 +781,10 @@ public abstract class PDFont implements
* @return The fonts bounding box.
* @throws IOException If there is an error getting the bounding box.
*/
- public abstract PDRectangle getFontBoundingBox() throws IOException;
+ public PDRectangle getFontBoundingBox() throws IOException
+ {
+ return getFontDescriptor().getFontBoundingBox();
+ }
/**
* Determines the width of the given character.
@@ -759,21 +831,43 @@ public abstract class PDFont implements
}
/**
- * Sets hasToUnicode to the given value.
- *
- * @param hasToUnicodeValue the given value for hasToUnicode
- */
- protected void setHasToUnicode(boolean hasToUnicodeValue)
- {
- hasToUnicode = hasToUnicodeValue;
- }
-
- /**
* Determines the width of the space character.
*
* @return the width of the space character
*/
- public abstract float getSpaceWidth();
+ public float getSpaceWidth()
+ {
+ if (fontWidthOfSpace == -1f)
+ {
+ COSBase toUnicode = dict.getDictionaryObject(COSName.TO_UNICODE);
+ try
+ {
+ if (toUnicode != null)
+ {
+ int spaceMapping = toUnicodeCmap.getSpaceMapping();
+ if (spaceMapping > -1)
+ {
+ fontWidthOfSpace = getFontWidth(spaceMapping);
+ }
+ }
+ else
+ {
+ fontWidthOfSpace = getFontWidth(SPACE_BYTES, 0, 1);
+ }
+ // use the average font width as fall back
+ if (fontWidthOfSpace <= 0)
+ {
+ fontWidthOfSpace = getAverageFontWidth();
+ }
+ }
+ catch (Exception e)
+ {
+ LOG.error("Can't determine the width of the space character, assuming 250", e);
+ fontWidthOfSpace = 250f;
+ }
+ }
+ return fontWidthOfSpace;
+ }
/**
* Returns the toUnicode mapping if present.
Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDFontFactory.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDFontFactory.java?rev=1603653&r1=1603652&r2=1603653&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDFontFactory.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDFontFactory.java Wed Jun 18 22:04:47 2014
@@ -83,7 +83,7 @@ public class PDFontFactory
{
// assuming Type 1 font (see PDFBOX-1988) because it seems that Adobe Reader does this
// however, we may need more sophisticated logic perhaps looking at the FontFile
- LOG.warn("Invalid font subtype '" + subType.getName() + "'");
+ LOG.warn("Invalid font subtype '" + subType + "'");
return new PDType1Font(dictionary);
}
}
Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDMMType1Font.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDMMType1Font.java?rev=1603653&r1=1603652&r2=1603653&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDMMType1Font.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDMMType1Font.java Wed Jun 18 22:04:47 2014
@@ -31,8 +31,7 @@ public class PDMMType1Font extends PDTyp
*/
public PDMMType1Font()
{
- super();
- font.setItem(COSName.SUBTYPE, COSName.MM_TYPE1);
+ dict.setItem(COSName.SUBTYPE, COSName.MM_TYPE1);
}
/**
Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDTrueTypeFont.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDTrueTypeFont.java?rev=1603653&r1=1603652&r2=1603653&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDTrueTypeFont.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDTrueTypeFont.java Wed Jun 18 22:04:47 2014
@@ -50,6 +50,7 @@ import org.apache.pdfbox.encoding.MacOSR
import org.apache.pdfbox.encoding.WinAnsiEncoding;
import org.apache.pdfbox.io.IOUtils;
import org.apache.pdfbox.pdmodel.PDDocument;
+import org.apache.pdfbox.pdmodel.common.COSArrayList;
import org.apache.pdfbox.pdmodel.common.PDRectangle;
import org.apache.pdfbox.pdmodel.common.PDStream;
@@ -58,7 +59,7 @@ import org.apache.pdfbox.pdmodel.common.
*
* @author Ben Litchfield
*/
-public class PDTrueTypeFont extends PDSimpleFont
+public class PDTrueTypeFont extends PDFont
{
private static final Log LOG = LogFactory.getLog(PDTrueTypeFont.class);
@@ -102,7 +103,7 @@ public class PDTrueTypeFont extends PDSi
*/
private PDTrueTypeFont(PDDocument document, InputStream ttf) throws IOException
{
- font.setItem(COSName.SUBTYPE, COSName.TRUE_TYPE);
+ dict.setItem(COSName.SUBTYPE, COSName.TRUE_TYPE);
PDStream stream = new PDStream(document, ttf, false);
stream.getStream().setInt(COSName.LENGTH1, stream.getByteArray().length); // todo: wrong?
@@ -112,8 +113,8 @@ public class PDTrueTypeFont extends PDSi
// just use Identity-H with unicode mapping
Encoding encoding = new WinAnsiEncoding(); // fixme: read encoding from TTF
- setFontEncoding(encoding);
- setEncoding(encoding.getCOSObject());
+ this.fontEncoding = encoding;
+ dict.setItem(COSName.ENCODING, encoding.getCOSObject());
// as the stream was close within the PDStream constructor, we have to recreate it
InputStream stream2 = null;
@@ -128,9 +129,8 @@ public class PDTrueTypeFont extends PDSi
IOUtils.closeQuietly(stream2);
}
-
fd.setFontFile2(stream);
- setFontDescriptor(fd);
+ dict.setItem(COSName.FONT_DESC, fd);
}
// creates a new font descriptor dictionary for the given TTF
@@ -144,7 +144,7 @@ public class PDTrueTypeFont extends PDSi
{
if (nr.getNameId() == NameRecord.NAME_POSTSCRIPT_NAME)
{
- setBaseFont(nr.getString());
+ dict.setName(COSName.BASE_FONT, nr.getString());
fd.setFontName(nr.getString());
}
else if (nr.getNameId() == NameRecord.NAME_FONT_FAMILY_NAME)
@@ -318,9 +318,9 @@ public class PDTrueTypeFont extends PDSi
}
}
}
- setWidths(widths);
- font.setInt(COSName.FIRST_CHAR, firstChar);
- font.setInt(COSName.LAST_CHAR, lastChar);
+ dict.setItem(COSName.WIDTHS, COSArrayList.converterToCOSArray(widths));
+ dict.setInt(COSName.FIRST_CHAR, firstChar);
+ dict.setInt(COSName.LAST_CHAR, lastChar);
return fd;
}
Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDType0Font.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDType0Font.java?rev=1603653&r1=1603652&r2=1603653&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDType0Font.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDType0Font.java Wed Jun 18 22:04:47 2014
@@ -17,20 +17,27 @@
package org.apache.pdfbox.pdmodel.font;
import java.io.IOException;
+import java.io.InputStream;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.pdfbox.cos.COSArray;
+import org.apache.pdfbox.cos.COSBase;
import org.apache.pdfbox.cos.COSDictionary;
import org.apache.pdfbox.cos.COSName;
+import org.apache.pdfbox.cos.COSStream;
+import org.apache.pdfbox.encoding.DictionaryEncoding;
+import org.apache.pdfbox.encoding.Encoding;
+import org.apache.pdfbox.io.IOUtils;
import org.apache.pdfbox.pdmodel.common.PDRectangle;
+import org.apache.pdfbox.util.ResourceLoader;
/**
* Type 0 (composite) Font.
*
- * @author <Ben Litchfield
+ * @author Ben Litchfield
*/
-public class PDType0Font extends PDSimpleFont
+public class PDType0Font extends PDFont
{
private static final Log LOG = LogFactory.getLog(PDType0Font.class);
@@ -40,14 +47,6 @@ public class PDType0Font extends PDSimpl
/**
* Constructor.
- */
- public PDType0Font()
- {
- font.setItem(COSName.SUBTYPE, COSName.TYPE0);
- }
-
- /**
- * Constructor.
*
* @param fontDictionary The font dictionary according to the PDF specification.
*/
@@ -72,7 +71,7 @@ public class PDType0Font extends PDSimpl
{
if (descendantFontArray == null)
{
- descendantFontArray = (COSArray) font.getDictionaryObject(COSName.DESCENDANT_FONTS);
+ descendantFontArray = (COSArray) dict.getDictionaryObject(COSName.DESCENDANT_FONTS);
}
return descendantFontArray;
}
@@ -86,24 +85,6 @@ public class PDType0Font extends PDSimpl
@Override
public float getFontWidth(byte[] c, int offset, int length) throws IOException
{
- if (descendantFont instanceof PDCIDFontType2Font)
- {
- // a suitable mapping is needed to address the correct width value
- PDCIDFontType2Font cidType2Font = (PDCIDFontType2Font) descendantFont;
- int code = getCodeFromArray(c, offset, length);
- if (cidType2Font.hasIdentityCIDToGIDMap() || cidType2Font.hasCIDToGIDMap())
- {
- return cidType2Font.getFontWidth(code);
- }
- else if (getCMap() != null)
- {
- String mappedString = getCMap().lookup(code, length);
- if (mappedString != null)
- {
- return cidType2Font.getFontWidth(mappedString.codePointAt(0));
- }
- }
- }
return descendantFont.getFontWidth(c, offset, length);
}
@@ -125,6 +106,106 @@ public class PDType0Font extends PDSimpl
return descendantFont.getFontWidth(charCode);
}
+ // todo: copied from PDSimpleFont and modified
+ // todo: for a Type 0 font this can only be "The name of a predefined CMap, or a stream containing a
+ // CMap that maps character codes to font numbers and CIDs", so I should adjust this accordingly
+ @Override
+ protected void determineEncoding()
+ {
+ String cmapName = null;
+ COSName encodingName = null;
+ COSBase encoding = dict.getDictionaryObject(COSName.ENCODING);
+ Encoding fontEncoding = null;
+ if (encoding != null)
+ {
+ if (encoding instanceof COSName)
+ {
+ if (cmap == null)
+ {
+ encodingName = (COSName) encoding;
+ cmap = cmapObjects.get(encodingName.getName());
+ if (cmap == null)
+ {
+ cmapName = encodingName.getName();
+ }
+ }
+ if (cmap == null && cmapName != null)
+ {
+ try
+ {
+ fontEncoding = Encoding.getInstance(encodingName);
+ }
+ catch (IOException exception)
+ {
+ LOG.debug("Debug: Could not find encoding for " + encodingName);
+ }
+ }
+ }
+ else if (encoding instanceof COSStream)
+ {
+ if (cmap == null)
+ {
+ COSStream encodingStream = (COSStream) encoding;
+ try
+ {
+ InputStream is = encodingStream.getUnfilteredStream();
+ cmap = parseCmap(null, is);
+ IOUtils.closeQuietly(is);
+ }
+ catch (IOException exception)
+ {
+ LOG.error("Error: Could not parse the embedded CMAP");
+ }
+ }
+ }
+ else if (encoding instanceof COSDictionary)
+ {
+ try
+ {
+ fontEncoding = new DictionaryEncoding((COSDictionary) encoding);
+ }
+ catch (IOException exception)
+ {
+ LOG.error("Error: Could not create the DictionaryEncoding");
+ }
+ }
+ }
+ this.fontEncoding = fontEncoding;
+ extractToUnicodeEncoding(); // todo: IMPORTANT!
+
+ if (cmap == null && cmapName != null)
+ {
+ InputStream cmapStream = null;
+ try
+ {
+ // look for a predefined CMap with the given name
+ cmapStream = ResourceLoader.loadResource(resourceRootCMAP + cmapName);
+ if (cmapStream != null)
+ {
+ cmap = parseCmap(resourceRootCMAP, cmapStream);
+ if (cmap == null && encodingName == null)
+ {
+ LOG.error("Error: Could not parse predefined CMAP file for '" +
+ cmapName + "'");
+ }
+ }
+ else
+ {
+ LOG.debug("Debug: '" + cmapName + "' isn't a predefined map, most likely it's" +
+ "embedded in the pdf itself.");
+ }
+ }
+ catch (IOException exception)
+ {
+ LOG.error("Error: Could not find predefined CMAP file for '" + cmapName + "'");
+ }
+ finally
+ {
+ IOUtils.closeQuietly(cmapStream);
+ }
+ }
+ }
+
@Override
public String encode(byte[] c, int offset, int length) throws IOException
{
Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDType1AfmPfbFont.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDType1AfmPfbFont.java?rev=1603653&r1=1603652&r2=1603653&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDType1AfmPfbFont.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDType1AfmPfbFont.java Wed Jun 18 22:04:47 2014
@@ -36,6 +36,7 @@ import org.apache.pdfbox.encoding.AFMEnc
import org.apache.pdfbox.encoding.DictionaryEncoding;
import org.apache.pdfbox.encoding.Encoding;
import org.apache.pdfbox.pdmodel.PDDocument;
+import org.apache.pdfbox.pdmodel.common.COSArrayList;
import org.apache.pdfbox.pdmodel.common.PDRectangle;
import org.apache.pdfbox.pdmodel.common.PDStream;
@@ -54,13 +55,13 @@ public class PDType1AfmPfbFont extends P
* Create a new object.
*
* @param doc The PDF document that will hold the embedded font.
- * @param afmname The font filename.
+ * @param afmName The font filename.
* @throws IOException If there is an error loading the data.
*/
- public PDType1AfmPfbFont(final PDDocument doc, final String afmname) throws IOException
+ public PDType1AfmPfbFont(final PDDocument doc, final String afmName) throws IOException
{
- InputStream afmIn = new BufferedInputStream(new FileInputStream(afmname), BUFFER_SIZE);
- String pfbName = afmname.replaceAll(".AFM", "").replaceAll(".afm", "") + ".pfb";
+ InputStream afmIn = new BufferedInputStream(new FileInputStream(afmName), BUFFER_SIZE);
+ String pfbName = afmName.replaceAll(".AFM", "").replaceAll(".afm", "") + ".pfb";
InputStream pfbIn = new BufferedInputStream(new FileInputStream(pfbName), BUFFER_SIZE);
load(doc, afmIn, pfbIn);
}
@@ -91,7 +92,7 @@ public class PDType1AfmPfbFont extends P
throws IOException
{
PDFontDescriptorDictionary fd = new PDFontDescriptorDictionary();
- setFontDescriptor(fd);
+ dict.setItem(COSName.FONT_DESC, fd);
// read the pfb
PfbParser pfbparser = new PfbParser(pfb);
@@ -109,10 +110,10 @@ public class PDType1AfmPfbFont extends P
// read the afm
AFMParser parser = new AFMParser(afm);
metric = parser.parse();
- setFontEncoding(afmToDictionary(new AFMEncoding(metric)));
+ this.fontEncoding = afmToDictionary(new AFMEncoding(metric));
// set the values
- setBaseFont(metric.getFontName());
+ dict.setName(COSName.BASE_FONT, metric.getFontName());
fd.setFontName(metric.getFontName());
fd.setFontFamily(metric.getFamilyName());
fd.setNonSymbolic(true);
@@ -191,9 +192,9 @@ public class PDType1AfmPfbFont extends P
}
}
}
- setFirstChar(0);
- setLastChar(255);
- setWidths(widths);
+ dict.setInt(COSName.FIRST_CHAR, 0);
+ dict.setInt(COSName.LAST_CHAR, 255);
+ dict.setItem(COSName.WIDTHS, COSArrayList.converterToCOSArray(widths));
}
// This will generate a Encoding from the AFM-Encoding, because the AFM-Enconding isn't exported
Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDType1CFont.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDType1CFont.java?rev=1603653&r1=1603652&r2=1603653&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDType1CFont.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDType1CFont.java Wed Jun 18 22:04:47 2014
@@ -49,7 +49,7 @@ import org.apache.pdfbox.pdmodel.common.
*
* @author Villu Ruusmann
*/
-public class PDType1CFont extends PDSimpleFont
+public class PDType1CFont extends PDFont
{
private static final Log LOG = LogFactory.getLog(PDType1CFont.class);
private static final byte[] SPACE_BYTES = { (byte) 32 };
Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDType1Font.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDType1Font.java?rev=1603653&r1=1603652&r2=1603653&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDType1Font.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDType1Font.java Wed Jun 18 22:04:47 2014
@@ -19,7 +19,6 @@ package org.apache.pdfbox.pdmodel.font;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
-import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -50,7 +49,7 @@ import org.apache.pdfbox.util.ResourceLo
*
* @author Ben Litchfield
*/
-public class PDType1Font extends PDSimpleFont
+public class PDType1Font extends PDFont
{
private static final Log LOG = LogFactory.getLog(PDType1Font.class);
@@ -91,41 +90,39 @@ public class PDType1Font extends PDSimpl
/**
* The static map of the default Adobe font metrics.
*/
- private static final Map<String, FontMetric> AFM_MAP = getAdobeFontMetrics();
+ private static final Map<String, FontMetric> AFM_MAP;
- private static Map<String, FontMetric> getAdobeFontMetrics()
+ static
{
- Map<String, FontMetric> metrics = new HashMap<String, FontMetric>();
- addMetric(metrics, "Courier-Bold");
- addMetric(metrics, "Courier-BoldOblique");
- addMetric(metrics, "Courier");
- addMetric(metrics, "Courier-Oblique");
- addMetric(metrics, "Helvetica");
- addMetric(metrics, "Helvetica-Bold");
- addMetric(metrics, "Helvetica-BoldOblique");
- addMetric(metrics, "Helvetica-Oblique");
- addMetric(metrics, "Symbol");
- addMetric(metrics, "Times-Bold");
- addMetric(metrics, "Times-BoldItalic");
- addMetric(metrics, "Times-Italic");
- addMetric(metrics, "Times-Roman");
- addMetric(metrics, "ZapfDingbats");
-
- // PDFBOX-239
- addMetric(metrics, "Arial", "Helvetica");
- addMetric(metrics, "Arial,Bold", "Helvetica-Bold");
- addMetric(metrics, "Arial,Italic", "Helvetica-Oblique");
- addMetric(metrics, "Arial,BoldItalic", "Helvetica-BoldOblique");
+ AFM_MAP = new HashMap<String, FontMetric>();
+ addMetric("Courier-Bold");
+ addMetric("Courier-BoldOblique");
+ addMetric("Courier");
+ addMetric("Courier-Oblique");
+ addMetric("Helvetica");
+ addMetric("Helvetica-Bold");
+ addMetric("Helvetica-BoldOblique");
+ addMetric("Helvetica-Oblique");
+ addMetric("Symbol");
+ addMetric("Times-Bold");
+ addMetric("Times-BoldItalic");
+ addMetric("Times-Italic");
+ addMetric("Times-Roman");
+ addMetric("ZapfDingbats");
- return Collections.unmodifiableMap(metrics);
+ // PDFBOX-239
+ addMetric("Arial", "Helvetica");
+ addMetric("Arial,Bold", "Helvetica-Bold");
+ addMetric("Arial,Italic", "Helvetica-Oblique");
+ addMetric("Arial,BoldItalic", "Helvetica-BoldOblique");
}
- private static void addMetric(Map<String, FontMetric> metrics, String name)
+ private static void addMetric(String name)
{
- addMetric(metrics, name, name);
+ addMetric(name, name);
}
-
- private static void addMetric(Map<String, FontMetric> metrics, String name, String prefix)
+
+ private static void addMetric(String name, String prefix)
{
try
{
@@ -137,7 +134,7 @@ public class PDType1Font extends PDSimpl
{
AFMParser parser = new AFMParser(afmStream);
FontMetric metric = parser.parse();
- metrics.put(name, metric);
+ AFM_MAP.put(name, metric);
}
finally
{
@@ -158,9 +155,22 @@ public class PDType1Font extends PDSimpl
/**
* Constructor.
*/
- public PDType1Font()
+ protected PDType1Font()
+ {
+ dict.setItem(COSName.SUBTYPE, COSName.TYPE1);
+ }
+
+ /**
+ * Constructor.
+ *
+ * @param baseFont The base font for this font.
+ */
+ private PDType1Font(String baseFont)
{
- font.setItem(COSName.SUBTYPE, COSName.TYPE1);
+ dict.setItem(COSName.SUBTYPE, COSName.TYPE1);
+ dict.setName(COSName.BASE_FONT, baseFont);
+ this.fontEncoding = new WinAnsiEncoding();
+ dict.setItem(COSName.ENCODING, COSName.WIN_ANSI_ENCODING); // todo: really?
}
/**
@@ -180,7 +190,7 @@ public class PDType1Font extends PDSimpl
{
try
{
- type1CFont = new PDType1CFont(super.font);
+ type1CFont = new PDType1CFont(super.dict);
}
catch (IOException e)
{
@@ -214,24 +224,11 @@ public class PDType1Font extends PDSimpl
getEncodingFromFont(getFontEncoding() == null);
}
- /**
- * Constructor.
- *
- * @param baseFont The base font for this font.
- */
- public PDType1Font(String baseFont)
- {
- this();
- setBaseFont(baseFont);
- setFontEncoding(new WinAnsiEncoding());
- setEncoding(COSName.WIN_ANSI_ENCODING);
- }
-
protected FontMetric getAFM()
{
if (afm == null)
{
- COSBase baseFont = font.getDictionaryObject(COSName.BASE_FONT);
+ COSBase baseFont = dict.getDictionaryObject(COSName.BASE_FONT);
String name = null;
if (baseFont instanceof COSName)
{
@@ -289,7 +286,7 @@ public class PDType1Font extends PDSimpl
{
fontEncoding = new AFMEncoding(metric);
}
- setFontEncoding(fontEncoding);
+ this.fontEncoding = fontEncoding;
}
}
@@ -317,7 +314,7 @@ public class PDType1Font extends PDSimpl
org.apache.fontbox.encoding.Encoding encoding = type1font.getEncoding();
if (encoding instanceof org.apache.fontbox.encoding.StandardEncoding)
{
- setFontEncoding(StandardEncoding.INSTANCE);
+ this.fontEncoding = StandardEncoding.INSTANCE;
}
else if (encoding instanceof org.apache.fontbox.encoding.CustomEncoding)
{
@@ -327,7 +324,7 @@ public class PDType1Font extends PDSimpl
{
type1Encoding.addCharacterEncoding(code, codeToName.get(code));
}
- setFontEncoding(type1Encoding);
+ this.fontEncoding = type1Encoding;
}
}
}
Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDType3Font.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDType3Font.java?rev=1603653&r1=1603652&r2=1603653&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDType3Font.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDType3Font.java Wed Jun 18 22:04:47 2014
@@ -23,7 +23,6 @@ import org.apache.pdfbox.cos.COSDictiona
import org.apache.pdfbox.cos.COSName;
import org.apache.pdfbox.cos.COSStream;
import org.apache.pdfbox.pdmodel.PDResources;
-import org.apache.pdfbox.pdmodel.common.PDMatrix;
import org.apache.pdfbox.pdmodel.common.PDRectangle;
/**
@@ -31,19 +30,10 @@ import org.apache.pdfbox.pdmodel.common.
*
* @author Ben Litchfield
*/
-public class PDType3Font extends PDSimpleFont
+public class PDType3Font extends PDFont
{
private PDResources type3Resources = null;
private COSDictionary charProcs = null;
-
- /**
- * Constructor.
- */
- public PDType3Font()
- {
- super();
- font.setItem(COSName.SUBTYPE, COSName.TYPE3);
- }
/**
* Constructor.
@@ -56,16 +46,6 @@ public class PDType3Font extends PDSimpl
}
/**
- * Set the font matrix for this type3 font.
- *
- * @param matrix The font matrix for this type3 font.
- */
- public void setFontMatrix(PDMatrix matrix)
- {
- font.setItem(COSName.FONT_MATRIX, matrix);
- }
-
- /**
* Returns the optional resources of the type3 stream.
*
* @return the resources bound to be used when parsing the type3 stream
@@ -74,7 +54,7 @@ public class PDType3Font extends PDSimpl
{
if (type3Resources == null)
{
- COSDictionary resources = (COSDictionary)font.getDictionaryObject(COSName.RESOURCES);
+ COSDictionary resources = (COSDictionary) dict.getDictionaryObject(COSName.RESOURCES);
if (resources != null)
{
type3Resources = new PDResources(resources);
@@ -92,7 +72,7 @@ public class PDType3Font extends PDSimpl
@Override
public PDRectangle getFontBoundingBox() throws IOException
{
- COSArray rect = (COSArray)font.getDictionaryObject(COSName.FONT_BBOX);
+ COSArray rect = (COSArray) dict.getDictionaryObject(COSName.FONT_BBOX);
PDRectangle retval = null;
if(rect != null)
{
@@ -110,7 +90,7 @@ public class PDType3Font extends PDSimpl
{
if (charProcs == null)
{
- charProcs = (COSDictionary)font.getDictionaryObject(COSName.CHAR_PROCS);
+ charProcs = (COSDictionary) dict.getDictionaryObject(COSName.CHAR_PROCS);
}
return charProcs;
}
Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDAppearanceString.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDAppearanceString.java?rev=1603653&r1=1603652&r2=1603653&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDAppearanceString.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDAppearanceString.java Wed Jun 18 22:04:47 2014
@@ -45,7 +45,6 @@ import org.apache.pdfbox.pdmodel.common.
import org.apache.pdfbox.pdmodel.font.PDFont;
import org.apache.pdfbox.pdmodel.font.PDFontDescriptor;
-import org.apache.pdfbox.pdmodel.font.PDSimpleFont;
import org.apache.pdfbox.pdmodel.interactive.action.PDFormFieldAdditionalActions;
import org.apache.pdfbox.pdmodel.interactive.annotation.PDAppearanceDictionary;
@@ -553,9 +552,9 @@ public final class PDAppearanceString
float lineWidth = getLineWidth( tokens );
float stringWidth = pdFont.getStringWidth( value );
float height = 0;
- if( pdFont instanceof PDSimpleFont )
+ if( pdFont instanceof PDFont )
{
- height = ((PDSimpleFont)pdFont).getFontDescriptor().getFontBoundingBox().getHeight();
+ height = ((PDFont)pdFont).getFontDescriptor().getFontBoundingBox().getHeight();
}
else
{
@@ -592,7 +591,7 @@ public final class PDAppearanceString
}
else
{
- if( pdFont instanceof PDSimpleFont )
+ if( pdFont instanceof PDFont )
{
//BJL 9/25/2004
//This algorithm is a little bit of black magic. It does
@@ -601,7 +600,7 @@ public final class PDAppearanceString
//have determined that the below method of computing the position
//is correct for certain documents, but maybe not all. It does
//work f1040ez.pdf and Form_1.pdf
- PDFontDescriptor fd = ((PDSimpleFont)pdFont).getFontDescriptor();
+ PDFontDescriptor fd = ((PDFont)pdFont).getFontDescriptor();
float bBoxHeight = boundingBox.getHeight();
float fontHeight = fd.getFontBoundingBox().getHeight() + 2 * fd.getDescent();
fontHeight = (fontHeight/1000) * fontSize;
Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/rendering/PageDrawer.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/rendering/PageDrawer.java?rev=1603653&r1=1603652&r2=1603653&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/rendering/PageDrawer.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/rendering/PageDrawer.java Wed Jun 18 22:04:47 2014
@@ -64,7 +64,6 @@ import org.apache.pdfbox.pdmodel.font.PD
import org.apache.pdfbox.pdmodel.font.PDFont;
import org.apache.pdfbox.pdmodel.font.PDFontDescriptor;
import org.apache.pdfbox.pdmodel.font.PDFontDescriptorDictionary;
-import org.apache.pdfbox.pdmodel.font.PDSimpleFont;
import org.apache.pdfbox.pdmodel.font.PDTrueTypeFont;
import org.apache.pdfbox.pdmodel.font.PDType0Font;
import org.apache.pdfbox.pdmodel.font.PDType1CFont;
@@ -337,7 +336,7 @@ public class PageDrawer extends PDFStrea
{
// Use AWT to render the font (standard14 fonts, substituted embedded fonts)
// TODO to be removed in the long run
- drawString((PDSimpleFont) font, text.getCharacter(), at);
+ drawString(font, text.getCharacter(), at);
}
}
}
@@ -438,7 +437,7 @@ public class PageDrawer extends PDFStrea
*
* @throws IOException If there is an error drawing the specific string.
*/
- private void drawString(PDSimpleFont font, String string, AffineTransform at) throws IOException
+ private void drawString(PDFont font, String string, AffineTransform at) throws IOException
{
Font awtFont = createAWTFont(font);
FontRenderContext frc = new FontRenderContext(new AffineTransform(), true, true);
@@ -472,7 +471,7 @@ public class PageDrawer extends PDFStrea
* @return the corresponding AWT font
* @throws IOException if something went wrong
*/
- private Font createAWTFont(PDSimpleFont font) throws IOException
+ private Font createAWTFont(PDFont font) throws IOException
{
Font awtFont = null;
// Is there already a AWTFont for the given font?
@@ -496,11 +495,7 @@ public class PageDrawer extends PDFStrea
{
awtFont = FontManager.getAwtFont(fd.getFontName());
}
- if (awtFont != null)
- {
- type1Font.setIsFontSubstituted(true);
- }
- else
+ if (awtFont == null)
{
LOG.info("Can't find the specified font " + fd.getFontName());
}
@@ -526,7 +521,6 @@ public class PageDrawer extends PDFStrea
// Fallback: we can't find anything, so we have to use the standard font
awtFont = FontManager.getStandardFont();
LOG.info("Using font " + awtFont.getName() + " instead of " + font.getBaseFont());
- font.setIsFontSubstituted(true);
}
awtFonts.put(font, awtFont);
}
@@ -589,18 +583,18 @@ public class PageDrawer extends PDFStrea
PDType0Font type0Font = (PDType0Font) font;
if (type0Font.getDescendantFont() instanceof PDCIDFontType2Font)
{
- // a CIDFontType2Font contains TTF font
+ // a Type2 CIDFont contains a TTF font
PDCIDFontType2Font cidType2Font = (PDCIDFontType2Font) type0Font.getDescendantFont();
// get the true type font raw data
TrueTypeFont ttf = cidType2Font.getTTFFont();
if (ttf != null)
{
- glyph2D = new TTFGlyph2D(ttf, font, cidType2Font);
+ glyph2D = new TTFGlyph2D(type0Font);
}
}
else if (type0Font.getDescendantFont() instanceof PDCIDFontType0Font)
{
- // a CIDFontType2Font contains TTF font
+ // a Type0 CIDFont contains CFF font
PDCIDFontType0Font cidType2Font = (PDCIDFontType0Font) type0Font.getDescendantFont();
PDType1CFont type1CFont = cidType2Font.getType1CFont();
if (type1CFont != null)
Copied: pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/font/PDFontTest.java (from r1603605, pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/font/PDSimpleFontTest.java)
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/font/PDFontTest.java?p2=pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/font/PDFontTest.java&p1=pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/font/PDSimpleFontTest.java&r1=1603605&r2=1603653&rev=1603653&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/font/PDSimpleFontTest.java (original)
+++ pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/font/PDFontTest.java Wed Jun 18 22:04:47 2014
@@ -31,10 +31,10 @@ import org.junit.Test;
*
* @author adam
*/
-public class PDSimpleFontTest
+public class PDFontTest
{
- public PDSimpleFontTest()
+ public PDFontTest()
{
}
@@ -67,7 +67,7 @@ public class PDSimpleFontTest
PDDocument doc = null;
try
{
- doc = PDDocument.load(PDSimpleFontTest.class.getResourceAsStream("F001u_3_7j.pdf"));
+ doc = PDDocument.load(PDFontTest.class.getResourceAsStream("F001u_3_7j.pdf"));
PDFRenderer renderer = new PDFRenderer(doc);
renderer.renderImage(0);
// the allegation is that renderImage() will crash the JVM or hang
@@ -90,7 +90,7 @@ public class PDSimpleFontTest
PDDocument doc = null;
try
{
- doc = PDDocument.load(PDSimpleFontTest.class.getResourceAsStream("256.pdf"));
+ doc = PDDocument.load(PDFontTest.class.getResourceAsStream("256.pdf"));
PDFRenderer renderer = new PDFRenderer(doc);
renderer.renderImage(0);
// the allegation is that renderImage() will crash the JVM or hang
Modified: pdfbox/trunk/preflight/src/main/java/org/apache/pdfbox/preflight/font/Type3FontValidator.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/preflight/src/main/java/org/apache/pdfbox/preflight/font/Type3FontValidator.java?rev=1603653&r1=1603652&r2=1603653&view=diff
==============================================================================
--- pdfbox/trunk/preflight/src/main/java/org/apache/pdfbox/preflight/font/Type3FontValidator.java (original)
+++ pdfbox/trunk/preflight/src/main/java/org/apache/pdfbox/preflight/font/Type3FontValidator.java Wed Jun 18 22:04:47 2014
@@ -39,7 +39,6 @@ import org.apache.pdfbox.cos.COSName;
import org.apache.pdfbox.cos.COSStream;
import org.apache.pdfbox.encoding.DictionaryEncoding;
import org.apache.pdfbox.encoding.Encoding;
-import org.apache.pdfbox.encoding.EncodingManager;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDResources;
import org.apache.pdfbox.pdmodel.font.PDFont;
@@ -222,12 +221,11 @@ public class Type3FontValidator extends
*/
private void checkEncodingAsString(COSBase fontEncoding)
{
- EncodingManager emng = new EncodingManager();
// Encoding is a Name, check if it is an Existing Encoding
String enc = COSUtils.getAsString(fontEncoding, cosDocument);
try
{
- this.encoding = emng.getEncoding(COSName.getPDFName(enc));
+ this.encoding = Encoding.getInstance(COSName.getPDFName(enc));
}
catch (IOException e)
{
Modified: pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools/TextToPDF.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools/TextToPDF.java?rev=1603653&r1=1603652&r2=1603653&view=diff
==============================================================================
--- pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools/TextToPDF.java (original)
+++ pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools/TextToPDF.java Wed Jun 18 22:04:47 2014
@@ -26,7 +26,7 @@ import org.apache.pdfbox.pdmodel.PDDocum
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.edit.PDPageContentStream;
-import org.apache.pdfbox.pdmodel.font.PDSimpleFont;
+import org.apache.pdfbox.pdmodel.font.PDFont;
import org.apache.pdfbox.pdmodel.font.PDTrueTypeFont;
import org.apache.pdfbox.pdmodel.font.PDType1Font;
@@ -40,7 +40,7 @@ import org.apache.pdfbox.pdmodel.font.PD
public class TextToPDF
{
private int fontSize = 10;
- private PDSimpleFont font = PDType1Font.HELVETICA;
+ private PDFont font = PDType1Font.HELVETICA;
/**
* Create a PDF document with some text.
@@ -240,14 +240,14 @@ public class TextToPDF
/**
* @return Returns the font.
*/
- public PDSimpleFont getFont()
+ public PDFont getFont()
{
return font;
}
/**
* @param aFont The font to set.
*/
- public void setFont(PDSimpleFont aFont)
+ public void setFont(PDFont aFont)
{
this.font = aFont;
}