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/09/19 23:05:56 UTC
svn commit: r1626333 - in
/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox: pdmodel/font/
rendering/
Author: jahewson
Date: Fri Sep 19 21:05:56 2014
New Revision: 1626333
URL: http://svn.apache.org/r1626333
Log:
PDFBOX-2360: Added PDFontLike interface for common PDFont and PDCIDFont aspects
Added:
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDFontLike.java
Modified:
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDCIDFont.java
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDCIDFontType0.java
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDCIDFontType2.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/PDSimpleFont.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/PDType1CFont.java
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDType1Font.java
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/rendering/PageDrawer.java
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=1626333&r1=1626332&r2=1626333&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 Fri Sep 19 21:05:56 2014
@@ -34,9 +34,11 @@ import org.apache.pdfbox.util.Vector;
* A CIDFont. A CIDFont is a PDF object that contains information about a CIDFont program. Although
* its Type value is Font, a CIDFont is not actually a font.
*
+ * <p>It is not usually necessary to use this class directly, prefer
+ *
* @author Ben Litchfield
*/
-public abstract class PDCIDFont implements COSObjectable
+public abstract class PDCIDFont implements COSObjectable, PDFontLike
{
protected final PDType0Font parent;
@@ -172,11 +174,13 @@ public abstract class PDCIDFont implemen
return dict.getNameAsString(COSName.BASE_FONT);
}
- /**
- * This will get the font descriptor for this font. A font descriptor is required for a CIDFont.
- *
- * @return The font descriptor for this font.
- */
+ @Override
+ public String getName()
+ {
+ return getBaseFont();
+ }
+
+ @Override
public PDFontDescriptor getFontDescriptor()
{
if (fontDescriptor == null)
@@ -190,9 +194,7 @@ public abstract class PDCIDFont implemen
return fontDescriptor;
}
- /**
- * Returns the font matrix, which represents the transformation from glyph space to text space.
- */
+ @Override
public abstract Matrix getFontMatrix();
/**
@@ -205,9 +207,7 @@ public abstract class PDCIDFont implemen
return parent;
}
- /**
- * Returns the font's bounding box.
- */
+ @Override
public abstract BoundingBox getBoundingBox() throws IOException;
/**
@@ -260,12 +260,7 @@ public abstract class PDCIDFont implemen
return new Vector(w0 / 2, dw2[0]);
}
- /**
- * Returns the position vector (v) in 1/1000 text space, for the given character code.
- *
- * @param code character code
- * @return position vector (v)
- */
+ @Override
public Vector getPositionVector(int code)
{
int cid = codeToCID(code);
@@ -300,19 +295,10 @@ public abstract class PDCIDFont implemen
}
}
- /**
- * This will get the font height for a character.
- *
- * @param code character code
- * @return The height is in 1000 unit of text space, ie 333 or 777
- */
+ @Override
public abstract float getHeight(int code) throws IOException;
- /**
- * Returns the width of the given character.
- *
- * @param code character code
- */
+ @Override
public float getWidth(int code) throws IOException
{
// these widths are supposed to be consistent with the actual widths given in the CIDFont
@@ -338,25 +324,14 @@ public abstract class PDCIDFont implemen
}
}
- /**
- * Returns the width of a glyph in the embedded font file.
- *
- * @param code character code
- * @return width in glyph space
- * @throws IOException if the font could not be read
- */
- protected abstract float getWidthFromFont(int code) throws IOException;
+ @Override
+ public abstract float getWidthFromFont(int code) throws IOException;
- /**
- * Returns true if the font file is embedded in the PDF.
- */
+ @Override
public abstract boolean isEmbedded();
- /**
- * This will get the average font width for all characters.
- *
- * @return The width is in 1000 unit of text space, ie 333 or 777
- */
+ @Override
+ // todo: this method is highly suspicious, the average glyph width is not usually a good metric
public float getAverageFontWidth()
{
float totalWidths = 0.0f;
@@ -414,13 +389,4 @@ public abstract class PDCIDFont implemen
* @return GID
*/
public abstract int codeToGID(int code) throws IOException;
-
- public void clear()
- {
- if (widths != null)
- {
- widths.clear();
- widths = null;
- }
- }
}
Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDCIDFontType0.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDCIDFontType0.java?rev=1626333&r1=1626332&r2=1626333&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDCIDFontType0.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDCIDFontType0.java Fri Sep 19 21:05:56 2014
@@ -244,7 +244,7 @@ public class PDCIDFontType0 extends PDCI
}
@Override
- protected float getWidthFromFont(int code) throws IOException
+ public float getWidthFromFont(int code) throws IOException
{
int cid = codeToCID(code);
int width = getType2CharString(cid).getWidth();
Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDCIDFontType2.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDCIDFontType2.java?rev=1626333&r1=1626332&r2=1626333&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDCIDFontType2.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDCIDFontType2.java Fri Sep 19 21:05:56 2014
@@ -349,7 +349,7 @@ public class PDCIDFontType2 extends PDCI
}
@Override
- protected float getWidthFromFont(int code) throws IOException
+ public float getWidthFromFont(int code) throws IOException
{
int gid = codeToGID(code);
int width = ttf.getAdvanceWidth(gid);
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=1626333&r1=1626332&r2=1626333&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 Fri Sep 19 21:05:56 2014
@@ -42,7 +42,7 @@ import org.apache.pdfbox.util.Vector;
*
* @author Ben Litchfield
*/
-public abstract class PDFont implements COSObjectable
+public abstract class PDFont implements COSObjectable, PDFontLike
{
private static final Log LOG = LogFactory.getLog(PDFont.class);
private static final Matrix DEFAULT_FONT_MATRIX = new Matrix(0.001f, 0, 0, 0.001f, 0, 0);
@@ -102,16 +102,14 @@ public abstract class PDFont implements
}
}
- /**
- * Returns the font descriptor, may be null.
- */
+ @Override
public PDFontDescriptor getFontDescriptor()
{
return fontDescriptor;
}
/**
- * Sets the font descriptor.
+ * Sets the font descriptor. For internal PDFBox use only.
*/
void setFontDescriptor(PDFontDescriptor fontDescriptor)
{
@@ -157,14 +155,7 @@ public abstract class PDFont implements
return dict;
}
- /**
- * Returns the position vector (v), in text space, for the given character.
- * This represents the position of vertical origin relative to horizontal origin, for
- * horizontal writing it will always be (0, 0). For vertical writing both x and y are set.
- *
- * @param code character code
- * @return position vector
- */
+ @Override
public Vector getPositionVector(int code)
{
throw new UnsupportedOperationException("Horizontal fonts have no position vector");
@@ -182,11 +173,7 @@ public abstract class PDFont implements
return new Vector(getWidth(code) / 1000, 0);
}
- /**
- * Returns the advance width of the given character, in glyph space.
- *
- * @param code character code
- */
+ @Override
public float getWidth(int code) throws IOException
{
// Acrobat overrides the widths in the font program on the conforming reader's system with
@@ -218,31 +205,18 @@ public abstract class PDFont implements
}
}
- /**
- * Returns the width of a glyph in the embedded font file.
- *
- * @param code character code
- * @return width in glyph space
- * @throws IOException if the font could not be read
- */
+ @Override
public abstract float getWidthFromFont(int code) throws IOException;
- /**
- * Returns true if the font file is embedded in the PDF.
- */
+ @Override
public abstract boolean isEmbedded();
- /**
- * Returns the height of the given character, in glyph space. This can be expensive to
- * calculate. Results are only approximate.
- *
- * @param code character code
- */
+ @Override
public abstract float getHeight(int code) throws IOException;
/**
* Returns the width of the given Unicode string.
- *
+ *
* @param string The string to get the width of.
* @return The width of the string in 1000 units of text space, ie 333 567...
* @throws IOException If there is an error getting the width information.
@@ -260,7 +234,7 @@ public abstract class PDFont implements
/**
* This will get the average font width for all characters.
- *
+ *
* @return The width is in 1000 unit of text space, ie 333 or 777
*/
// todo: this method is highly suspicious, the average glyph width is not usually a good metric
@@ -404,25 +378,10 @@ public abstract class PDFont implements
return null;
}
- /**
- * Returns the PostScript name of the font.
- */
- public String getBaseFont()
- {
- return dict.getNameAsString(COSName.BASE_FONT);
- }
-
- /**
- * Returns the name of this font, either the PostScript "BaseName" or the Type 3 "Name".
- */
- public String getName()
- {
- return getBaseFont();
- }
+ @Override
+ public abstract String getName();
- /**
- * Returns the font's bounding box.
- */
+ @Override
public abstract BoundingBox getBoundingBox() throws IOException;
/**
@@ -447,9 +406,7 @@ public abstract class PDFont implements
return widths;
}
- /**
- * Returns the font matrix, which represents the transformation from glyph space to text space.
- */
+ @Override
public Matrix getFontMatrix()
{
return DEFAULT_FONT_MATRIX;
Added: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDFontLike.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDFontLike.java?rev=1626333&view=auto
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDFontLike.java (added)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDFontLike.java Fri Sep 19 21:05:56 2014
@@ -0,0 +1,98 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.pdfbox.pdmodel.font;
+
+import java.io.IOException;
+import org.apache.fontbox.util.BoundingBox;
+import org.apache.pdfbox.util.Matrix;
+import org.apache.pdfbox.util.Vector;
+
+/**
+ * A font-like object.
+ *
+ * @author John Hewson
+ */
+public interface PDFontLike
+{
+ /**
+ * Returns the name of this font, either the PostScript "BaseName" or the Type 3 "Name".
+ */
+ public String getName();
+
+ /**
+ * Returns the font descriptor, may be null.
+ */
+ public PDFontDescriptor getFontDescriptor();
+
+ /**
+ * Returns the font matrix, which represents the transformation from glyph space to text space.
+ */
+ public Matrix getFontMatrix();
+
+ /**
+ * Returns the font's bounding box.
+ */
+ public abstract BoundingBox getBoundingBox() throws IOException;
+
+ /**
+ * Returns the position vector (v), in text space, for the given character.
+ * This represents the position of vertical origin relative to horizontal origin, for
+ * horizontal writing it will always be (0, 0). For vertical writing both x and y are set.
+ *
+ * @param code character code
+ * @return position vector
+ */
+ public Vector getPositionVector(int code);
+
+ /**
+ * Returns the height of the given character, in glyph space. This can be expensive to
+ * calculate. Results are only approximate.
+ *
+ * @param code character code
+ */
+ public abstract float getHeight(int code) throws IOException;
+
+ /**
+ * Returns the advance width of the given character, in glyph space.
+ *
+ * @param code character code
+ */
+ public float getWidth(int code) throws IOException;
+
+ /**
+ * Returns the width of a glyph in the embedded font file.
+ *
+ * @param code character code
+ * @return width in glyph space
+ * @throws IOException if the font could not be read
+ */
+ public abstract float getWidthFromFont(int code) throws IOException;
+
+ /**
+ * Returns true if the font file is embedded in the PDF.
+ */
+ public abstract boolean isEmbedded();
+
+ /**
+ * This will get the average font width for all characters.
+ *
+ * @return The width is in 1000 unit of text space, ie 333 or 777
+ */
+ // todo: this method is highly suspicious, the average glyph width is not usually a good metric
+ public float getAverageFontWidth();
+}
Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDSimpleFont.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDSimpleFont.java?rev=1626333&r1=1626332&r2=1626333&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDSimpleFont.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDSimpleFont.java Fri Sep 19 21:05:56 2014
@@ -139,7 +139,7 @@ public abstract class PDSimpleFont exten
}
// assign the glyph list based on the font
- if ("ZapfDingbats".equals(getBaseFont()))
+ if ("ZapfDingbats".equals(getName()))
{
glyphList = GlyphList.ZAPF_DINGBATS;
}
@@ -182,7 +182,7 @@ public abstract class PDSimpleFont exten
}
else if (isStandard14())
{
- return getBaseFont().equals("Symbol") || getBaseFont().equals("ZapfDingbats");
+ return getName().equals("Symbol") || getName().equals("ZapfDingbats");
}
else
{
@@ -288,6 +288,6 @@ public abstract class PDSimpleFont exten
*/
public boolean isStandard14()
{
- return !isEmbedded() && STANDARD_14.contains(getBaseFont());
+ return !isEmbedded() && STANDARD_14.contains(getName());
}
}
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=1626333&r1=1626332&r2=1626333&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 Fri Sep 19 21:05:56 2014
@@ -32,6 +32,7 @@ import org.apache.fontbox.ttf.TTFParser;
import org.apache.fontbox.ttf.TrueTypeFont;
import org.apache.fontbox.util.BoundingBox;
import org.apache.pdfbox.cos.COSDictionary;
+import org.apache.pdfbox.cos.COSName;
import org.apache.pdfbox.encoding.Encoding;
import org.apache.pdfbox.encoding.GlyphList;
import org.apache.pdfbox.encoding.MacOSRomanEncoding;
@@ -148,6 +149,14 @@ public class PDTrueTypeFont extends PDSi
readEncoding();
}
+ /**
+ * Returns the PostScript name of the font.
+ */
+ public String getBaseFont()
+ {
+ return dict.getNameAsString(COSName.BASE_FONT);
+ }
+
@Override
protected Encoding readEncodingFromFont() throws IOException
{
@@ -174,6 +183,12 @@ public class PDTrueTypeFont extends PDSi
}
@Override
+ public String getName()
+ {
+ return getBaseFont();
+ }
+
+ @Override
public BoundingBox getBoundingBox() throws IOException
{
return ttf.getFontBBox();
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=1626333&r1=1626332&r2=1626333&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 Fri Sep 19 21:05:56 2014
@@ -153,6 +153,14 @@ public class PDType0Font extends PDFont
}
/**
+ * Returns the PostScript name of the font.
+ */
+ public String getBaseFont()
+ {
+ return dict.getNameAsString(COSName.BASE_FONT);
+ }
+
+ /**
* Returns the descendant font.
*/
public PDCIDFont getDescendantFont()
@@ -272,6 +280,12 @@ public class PDType0Font extends PDFont
}
@Override
+ public String getName()
+ {
+ return getBaseFont();
+ }
+
+ @Override
public BoundingBox getBoundingBox() throws IOException
{
return descendantFont.getBoundingBox();
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=1626333&r1=1626332&r2=1626333&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 Fri Sep 19 21:05:56 2014
@@ -33,8 +33,8 @@ import org.apache.fontbox.cff.CFFType1Fo
import org.apache.fontbox.ttf.Type1Equivalent;
import org.apache.fontbox.util.BoundingBox;
import org.apache.pdfbox.cos.COSDictionary;
+import org.apache.pdfbox.cos.COSName;
import org.apache.pdfbox.encoding.Encoding;
-import org.apache.pdfbox.encoding.GlyphList;
import org.apache.pdfbox.encoding.Type1Encoding;
import org.apache.pdfbox.io.IOUtils;
import org.apache.pdfbox.pdmodel.common.PDRectangle;
@@ -120,6 +120,14 @@ public class PDType1CFont extends PDSimp
return type1Equivalent;
}
+ /**
+ * Returns the PostScript name of the font.
+ */
+ public String getBaseFont()
+ {
+ return dict.getNameAsString(COSName.BASE_FONT);
+ }
+
@Override
public GeneralPath getPath(String name) throws IOException
{
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=1626333&r1=1626332&r2=1626333&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 Fri Sep 19 21:05:56 2014
@@ -273,6 +273,14 @@ public class PDType1Font extends PDSimpl
return null;
}
+ /**
+ * Returns the PostScript name of the font.
+ */
+ public String getBaseFont()
+ {
+ return dict.getNameAsString(COSName.BASE_FONT);
+ }
+
@Override
public PDFontDescriptor getFontDescriptor()
{
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=1626333&r1=1626332&r2=1626333&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 Fri Sep 19 21:05:56 2014
@@ -528,7 +528,7 @@ public class PageDrawer extends PDFGraph
if (glyph2D == null)
{
// todo: make sure this never happens
- throw new UnsupportedOperationException("No font for " + font.getBaseFont());
+ throw new UnsupportedOperationException("No font for " + font.getName());
}
return glyph2D;