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/08/17 22:02:20 UTC
svn commit: r1618516 [6/6] - in /pdfbox/no-awt:
examples/src/main/java/org/apache/pdfbox/examples/pdmodel/
fontbox/src/main/java/org/apache/fontbox/afm/
fontbox/src/main/java/org/apache/fontbox/cff/
fontbox/src/main/java/org/apache/fontbox/cff/charset/...
Modified: pdfbox/no-awt/pdfbox/src/main/java/org/apache/pdfbox/rendering/font/TTFGlyph2D.java
URL: http://svn.apache.org/viewvc/pdfbox/no-awt/pdfbox/src/main/java/org/apache/pdfbox/rendering/font/TTFGlyph2D.java?rev=1618516&r1=1618515&r2=1618516&view=diff
==============================================================================
--- pdfbox/no-awt/pdfbox/src/main/java/org/apache/pdfbox/rendering/font/TTFGlyph2D.java (original)
+++ pdfbox/no-awt/pdfbox/src/main/java/org/apache/pdfbox/rendering/font/TTFGlyph2D.java Sun Aug 17 20:02:18 2014
@@ -30,7 +30,7 @@ import org.apache.fontbox.cmap.CMap;
import org.apache.fontbox.ttf.GlyphData;
import org.apache.fontbox.ttf.HeaderTable;
import org.apache.fontbox.ttf.TrueTypeFont;
-import org.apache.pdfbox.pdmodel.font.PDCIDFontType2Font;
+import org.apache.pdfbox.pdmodel.font.PDCIDFontType2;
import org.apache.pdfbox.pdmodel.font.PDFont;
import org.apache.pdfbox.pdmodel.font.PDTrueTypeFont;
import org.apache.pdfbox.pdmodel.font.PDType0Font;
@@ -44,7 +44,7 @@ public class TTFGlyph2D implements Glyph
private PDFont pdFont;
private TrueTypeFont ttf;
- private PDCIDFontType2Font descendantFont;
+ private PDCIDFontType2 descendantFont;
private String name;
private float scale = 1.0f;
private boolean hasScaling = false;
@@ -62,7 +62,7 @@ public class TTFGlyph2D implements Glyph
*/
public TTFGlyph2D(PDTrueTypeFont ttfFont) throws IOException
{
- this(ttfFont.getTTFFont(), ttfFont, null);
+ this(ttfFont.getTrueTypeFont(), ttfFont, null);
}
/**
@@ -72,11 +72,11 @@ public class TTFGlyph2D implements Glyph
*/
public TTFGlyph2D(PDType0Font type0Font) throws IOException
{
- this(((PDCIDFontType2Font)type0Font.getDescendantFont()).getTTFFont(), type0Font,
- (PDCIDFontType2Font)type0Font.getDescendantFont());
+ this(((PDCIDFontType2)type0Font.getDescendantFont()).getTrueTypeFont(), type0Font,
+ (PDCIDFontType2)type0Font.getDescendantFont());
}
- public TTFGlyph2D(TrueTypeFont ttf, PDFont pdFont, PDCIDFontType2Font descFont)
+ public TTFGlyph2D(TrueTypeFont ttf, PDFont pdFont, PDCIDFontType2 descFont)
throws IOException
{
this.pdFont = pdFont;
@@ -98,7 +98,7 @@ public class TTFGlyph2D implements Glyph
*
* @param pdFont the given PDFont
*/
- private void extractFontSpecifics(PDFont pdFont, PDCIDFontType2Font descFont)
+ private void extractFontSpecifics(PDFont pdFont, PDCIDFontType2 descFont)
{
name = pdFont.getBaseFont();
if (descFont != null)
@@ -209,10 +209,7 @@ public class TTFGlyph2D implements Glyph
}
else
{
- if (LOG.isDebugEnabled())
- {
- LOG.debug(name + ": Glyph not found:" + glyphId);
- }
+ LOG.error(name + ": Glyph not found:" + glyphId);
}
}
return glyphPath != null ? (GeneralPath) glyphPath.clone() : null;
Modified: pdfbox/no-awt/pdfbox/src/main/java/org/apache/pdfbox/rendering/font/Type1Glyph2D.java
URL: http://svn.apache.org/viewvc/pdfbox/no-awt/pdfbox/src/main/java/org/apache/pdfbox/rendering/font/Type1Glyph2D.java?rev=1618516&r1=1618515&r2=1618516&view=diff
==============================================================================
--- pdfbox/no-awt/pdfbox/src/main/java/org/apache/pdfbox/rendering/font/Type1Glyph2D.java (original)
+++ pdfbox/no-awt/pdfbox/src/main/java/org/apache/pdfbox/rendering/font/Type1Glyph2D.java Sun Aug 17 20:02:18 2014
@@ -18,121 +18,127 @@ package org.apache.pdfbox.rendering.font
import java.awt.geom.GeneralPath;
import java.io.IOException;
-import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
-import org.apache.fontbox.cff.CFFFont;
-import org.apache.fontbox.type1.Type1Font;
-import org.apache.fontbox.type1.Type1Mapping;
import org.apache.pdfbox.encoding.Encoding;
+import org.apache.pdfbox.pdmodel.font.PDType1Equivalent;
/**
- * This class provides a glyph to GeneralPath conversion for Type 1 PFB and CFF fonts.
+ * Glyph to GeneralPath conversion for Type 1 PFB and CFF, and TrueType fonts with a 'post' table.
*/
public class Type1Glyph2D implements Glyph2D
{
private static final Log LOG = LogFactory.getLog(Type1Glyph2D.class);
- private HashMap<String, GeneralPath> glyphs = new HashMap<String, GeneralPath>();
- private Map<Integer, String> codeToName = new HashMap<Integer, String>();
- private String fontName = null;
-
- /**
- * Constructs a new Type1Glyph2D object for a CFF/Type2 font.
- *
- * @param font CFF/Type2 font
- * @param encoding PDF Encoding or null
- */
- public Type1Glyph2D(CFFFont font, Encoding encoding)
- {
- this(font.getName(), font.getType1Mappings(), encoding);
+ // alternative names for glyphs which are commonly encountered
+ private static final Map<String, String> ALT_NAMES = new HashMap<String, String>();
+ static
+ {
+ ALT_NAMES.put("ff", "f_f");
+ ALT_NAMES.put("ffi", "f_f_i");
+ ALT_NAMES.put("ffl", "f_f_l");
+ ALT_NAMES.put("fi", "f_i");
+ ALT_NAMES.put("fl", "f_l");
+ ALT_NAMES.put("st", "s_t");
+ ALT_NAMES.put("IJ", "I_J");
+ ALT_NAMES.put("ij", "i_j");
+ }
+
+ // unicode names for ligatures, needed to undo mapping in org.apache.pdfbox.Encoding
+ private static final Map<String, String> LIGATURE_UNI_NAMES = new HashMap<String, String>();
+ static
+ {
+ LIGATURE_UNI_NAMES.put("ff", "uniFB00");
+ LIGATURE_UNI_NAMES.put("fi", "uniFB01");
+ LIGATURE_UNI_NAMES.put("fl", "uniFB02");
+ LIGATURE_UNI_NAMES.put("ffi", "uniFB03");
+ LIGATURE_UNI_NAMES.put("ffl", "uniFB04");
+ LIGATURE_UNI_NAMES.put("pi", "uni03C0");
}
+ private final HashMap<Integer, GeneralPath> cache = new HashMap<Integer, GeneralPath>();
+ private final PDType1Equivalent font;
+
/**
- * Constructs a new Type1Glyph2D object for a Type 1 (PFB) font.
+ * Constructor.
*
- * @param font Type 1 (PFB) font
- * @param encoding PDF Encoding or null
+ * @param font PDF Type1 font.
*/
- public Type1Glyph2D(Type1Font font, Encoding encoding)
+ public Type1Glyph2D(PDType1Equivalent font)
{
- this(font.getFontName(), font.getType1Mappings(), encoding);
+ this.font = font;
}
- /**
- * Private constructor.
- */
- private Type1Glyph2D(String fontName, Collection<? extends Type1Mapping> mappings, Encoding encoding)
+ @Override
+ public GeneralPath getPathForCharacterCode(int code)
{
- this.fontName = fontName;
- // start with built-in encoding
- for (Type1Mapping mapping : mappings)
+ // cache
+ if (cache.containsKey(code))
{
- codeToName.put(mapping.getCode(), mapping.getName());
+ return cache.get(code);
}
- // override existing entries with an optional PDF Encoding
- if (encoding != null)
+
+ // fetch
+ try
{
- Map<Integer, String> encodingCodeToName = encoding.getCodeToNameMap();
- for (Integer key : encodingCodeToName.keySet())
+ String name = font.codeToName(code);
+ GeneralPath path = null;
+ if (font.hasGlyph(name))
{
- codeToName.put(key, encodingCodeToName.get(key));
+ path = font.getPath(name);
}
- }
- for (Type1Mapping mapping : mappings)
- {
- GeneralPath path;
- try
+ else
{
- path = mapping.getType1CharString().getPath();
- glyphs.put(mapping.getName(), path);
+ // try alternative name
+ String altName = ALT_NAMES.get(name);
+ if (altName != null && font.hasGlyph(altName))
+ {
+ path = font.getPath(altName);
+ }
+ else
+ {
+ // try unicode name
+ String unicodes = Encoding.getCharacterForName(name);
+ if (unicodes != null)
+ {
+ if (unicodes.length() == 1)
+ {
+ String uniName = String.format("uni%04X", unicodes.codePointAt(0));
+ path = font.getPath(uniName);
+ }
+ else if (unicodes.length() > 1)
+ {
+ if (LIGATURE_UNI_NAMES.containsKey(name))
+ {
+ path = font.getPath(LIGATURE_UNI_NAMES.get(name));
+ }
+ }
+ }
+ }
}
- catch (IOException exception)
+
+ if (path == null)
{
- LOG.error("Type 1 glyph rendering failed", exception);
+ LOG.warn("No glyph for " + code + " (" + name + ") in font " + font.getName());
+ path = font.getPath(".notdef");
}
- }
- }
-
- /**
- * Returns the path describing the glyph for the given name.
- *
- * @param name the name of the glyph
- * @return the GeneralPath for the given glyph
- */
- public GeneralPath getPathForGlyphName(String name)
- {
- return glyphs.get(name);
- }
- @Override
- public GeneralPath getPathForCharacterCode(int code)
- {
- if (codeToName.containsKey(code))
- {
- String name = codeToName.get(code);
- return glyphs.get(name);
+ cache.put(code, path);
+ return path;
}
- else
+ catch (IOException e)
{
- LOG.debug(fontName + ": glyph mapping for " + code + " not found");
+ LOG.error("Glyph rendering failed", e); // todo: escalate this error?
+ return new GeneralPath();
}
- return null;
}
@Override
public void dispose()
{
- if (glyphs != null)
- {
- glyphs.clear();
- }
- if (codeToName != null)
- {
- codeToName.clear();
- }
+ cache.clear();
}
}
Modified: pdfbox/no-awt/pdfbox/src/main/java/org/apache/pdfbox/util/PDFStreamEngine.java
URL: http://svn.apache.org/viewvc/pdfbox/no-awt/pdfbox/src/main/java/org/apache/pdfbox/util/PDFStreamEngine.java?rev=1618516&r1=1618515&r2=1618516&view=diff
==============================================================================
--- pdfbox/no-awt/pdfbox/src/main/java/org/apache/pdfbox/util/PDFStreamEngine.java (original)
+++ pdfbox/no-awt/pdfbox/src/main/java/org/apache/pdfbox/util/PDFStreamEngine.java Sun Aug 17 20:02:18 2014
@@ -42,6 +42,7 @@ import org.apache.pdfbox.pdmodel.common.
import org.apache.pdfbox.pdmodel.common.PDRectangle;
import org.apache.pdfbox.pdmodel.font.PDFont;
import org.apache.pdfbox.pdmodel.font.PDFontFactory;
+import org.apache.pdfbox.pdmodel.font.PDType0Font;
import org.apache.pdfbox.pdmodel.font.PDType3Font;
import org.apache.pdfbox.pdmodel.graphics.form.PDFormXObject;
import org.apache.pdfbox.pdmodel.graphics.state.PDExtendedGraphicsState;
@@ -393,8 +394,6 @@ public class PDFStreamEngine
fontMatrixYScaling = fontMatrix.getValue(1, 1);
}
- float maxVerticalDisplacementText = 0;
-
Matrix textStateParameters = new Matrix();
textStateParameters.setValue(0, 0, fontSizeText * horizontalScalingText);
textStateParameters.setValue(1, 1, fontSizeText);
@@ -406,19 +405,35 @@ public class PDFStreamEngine
Matrix td = new Matrix();
Matrix tempMatrix = new Matrix();
+ // todo: push this decision into the Font, i.e. hasTwoByteCharacterCodes()
+ // for now we use a workaround which accesses CMap fonts directly
int codeLength;
+ if (font instanceof PDType0Font)
+ {
+ // "When the current font is a Type 0 font whose Encoding entry is Identity-H or
+ // Identity-V, the string to be shown shall contain pairs of bytes representing CIDs,
+ // high-order byte first."
+
+ // "When the current font is a CIDFont, the string to be shown shall contain pairs of
+ // bytes representing CIDs, high-order byte first."
+
+ // todo: ((PDType0Font)font).getCMap().hasTwoByteMappings() ???
+ codeLength = 2; // todo: HACK, see "9.7.6.2 CMap Mapping" (also p275 for Identity-H or Identity-V,)
+ }
+ else
+ {
+ codeLength = 1;
+ }
+
for (int i = 0; i < string.length; i += codeLength)
{
// Decode the value to a Unicode character
- codeLength = 1;
String unicode = font.encode(string, i, codeLength);
int[] charCodes;
- if (unicode == null && i + 1 < string.length)
+ if (codeLength == 2)
{
- // maybe a multibyte encoding
- codeLength++;
- unicode = font.encode(string, i, codeLength);
charCodes = new int[] { font.getCodeFromArray(string, i, codeLength) };
+ // todo: shouldn't the above array have two codes?
}
else
{
@@ -434,9 +449,6 @@ public class PDFStreamEngine
charHorizontalDisplacementText = charHorizontalDisplacementText * fontMatrixXScaling;
charVerticalDisplacementText = charVerticalDisplacementText * fontMatrixYScaling;
- maxVerticalDisplacementText = Math.max(maxVerticalDisplacementText,
- charVerticalDisplacementText);
-
// PDF Spec - 5.5.2 Word Spacing
//
// Word spacing works the same was as character spacing, but applies
@@ -496,7 +508,7 @@ public class PDFStreamEngine
float startXPosition = textMatrixStart.getXPosition();
float widthText = endXPosition - startXPosition;
- float totalVerticalDisplacementDisp = maxVerticalDisplacementText * fontSizeText *
+ float totalVerticalDisplacementDisp = charVerticalDisplacementText * fontSizeText *
textXctm.getYScale();
// process the decoded glyph
Modified: pdfbox/no-awt/pdfbox/src/main/java/org/apache/pdfbox/util/PDFTextStreamEngine.java
URL: http://svn.apache.org/viewvc/pdfbox/no-awt/pdfbox/src/main/java/org/apache/pdfbox/util/PDFTextStreamEngine.java?rev=1618516&r1=1618515&r2=1618516&view=diff
==============================================================================
--- pdfbox/no-awt/pdfbox/src/main/java/org/apache/pdfbox/util/PDFTextStreamEngine.java (original)
+++ pdfbox/no-awt/pdfbox/src/main/java/org/apache/pdfbox/util/PDFTextStreamEngine.java Sun Aug 17 20:02:18 2014
@@ -132,7 +132,9 @@ public class PDFTextStreamEngine extends
// PDFBOX-373: Replace a null entry with "?" so it is not printed as "(null)"
if (unicode == null)
{
- unicode = "?";
+ //unicode = "?";
+ // fixme: new: don't process non-unicode Text, as it's not meaningful.
+ return;
}
processTextPosition(new TextPosition(pageRotation, pageSize.getWidth(),
Modified: pdfbox/no-awt/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/font/TestTTFParser.java
URL: http://svn.apache.org/viewvc/pdfbox/no-awt/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/font/TestTTFParser.java?rev=1618516&r1=1618515&r2=1618516&view=diff
==============================================================================
--- pdfbox/no-awt/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/font/TestTTFParser.java (original)
+++ pdfbox/no-awt/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/font/TestTTFParser.java Sun Aug 17 20:02:18 2014
@@ -62,7 +62,7 @@ public class TestTTFParser
for (CMAPEncodingEntry e : cmaps)
{
if (e.getPlatformId() == NameRecord.PLATFORM_WINDOWS
- && e.getPlatformEncodingId() == NameRecord.PLATFORM_ENCODING_WINDOWS_UNICODE)
+ && e.getPlatformEncodingId() == NameRecord.ENCODING_WINDOWS_UNICODE_BMP)
{
uc = e;
break;
Modified: pdfbox/no-awt/pdfbox/src/test/java/org/apache/pdfbox/util/TestTextStripper.java
URL: http://svn.apache.org/viewvc/pdfbox/no-awt/pdfbox/src/test/java/org/apache/pdfbox/util/TestTextStripper.java?rev=1618516&r1=1618515&r2=1618516&view=diff
==============================================================================
--- pdfbox/no-awt/pdfbox/src/test/java/org/apache/pdfbox/util/TestTextStripper.java (original)
+++ pdfbox/no-awt/pdfbox/src/test/java/org/apache/pdfbox/util/TestTextStripper.java Sun Aug 17 20:02:18 2014
@@ -228,6 +228,7 @@ public class TestTextStripper extends Te
}
}
+ //System.out.println(" " + inFile + (bSort ? " (sorted)" : ""));
PDDocument document = PDDocument.load(inFile);
try
{
@@ -299,7 +300,7 @@ public class TestTextStripper extends Te
{
this.bFail = true;
fail("FAILURE: Line mismatch for file " + inFile.getName() +
- " ( sort = "+bSort+")" +
+ " (sort = "+bSort+")" +
" at expected line: " + expectedReader.getLineNumber() +
" at actual line: " + actualReader.getLineNumber() +
"\nexpected line was: \"" + expectedLine + "\"" +
Modified: pdfbox/no-awt/preflight/src/main/java/org/apache/pdfbox/preflight/content/PreflightContentStream.java
URL: http://svn.apache.org/viewvc/pdfbox/no-awt/preflight/src/main/java/org/apache/pdfbox/preflight/content/PreflightContentStream.java?rev=1618516&r1=1618515&r2=1618516&view=diff
==============================================================================
--- pdfbox/no-awt/preflight/src/main/java/org/apache/pdfbox/preflight/content/PreflightContentStream.java (original)
+++ pdfbox/no-awt/preflight/src/main/java/org/apache/pdfbox/preflight/content/PreflightContentStream.java Sun Aug 17 20:02:18 2014
@@ -40,7 +40,6 @@ import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDResources;
import org.apache.pdfbox.pdmodel.common.PDStream;
import org.apache.pdfbox.pdmodel.font.PDFont;
-import org.apache.pdfbox.pdmodel.graphics.state.PDGraphicsState;
import org.apache.pdfbox.pdmodel.graphics.form.PDFormXObject;
import org.apache.pdfbox.pdmodel.graphics.state.PDTextState;
import org.apache.pdfbox.pdmodel.graphics.state.RenderingMode;
@@ -50,7 +49,6 @@ import org.apache.pdfbox.preflight.excep
import org.apache.pdfbox.preflight.font.container.FontContainer;
import org.apache.pdfbox.preflight.font.util.GlyphException;
import org.apache.pdfbox.util.operator.Operator;
-import org.apache.pdfbox.util.operator.Operator;
import org.apache.pdfbox.util.operator.OperatorProcessor;
public class PreflightContentStream extends PreflightStreamEngine
@@ -362,7 +360,7 @@ public class PreflightContentStream exte
codeLength++;
cid = font.encodeToCID(string, i, codeLength);
}
- fontContainer.checkGlyphWith(cid);
+ fontContainer.checkGlyphWidth(cid);
}
catch (IOException e)
{
Modified: pdfbox/no-awt/preflight/src/main/java/org/apache/pdfbox/preflight/font/Type0FontValidator.java
URL: http://svn.apache.org/viewvc/pdfbox/no-awt/preflight/src/main/java/org/apache/pdfbox/preflight/font/Type0FontValidator.java?rev=1618516&r1=1618515&r2=1618516&view=diff
==============================================================================
--- pdfbox/no-awt/preflight/src/main/java/org/apache/pdfbox/preflight/font/Type0FontValidator.java (original)
+++ pdfbox/no-awt/preflight/src/main/java/org/apache/pdfbox/preflight/font/Type0FontValidator.java Sun Aug 17 20:02:18 2014
@@ -24,6 +24,7 @@ package org.apache.pdfbox.preflight.font
import static org.apache.pdfbox.preflight.PreflightConstants.ERROR_FONTS_CIDKEYED_CMAP_INVALID_OR_MISSING;
import static org.apache.pdfbox.preflight.PreflightConstants.ERROR_FONTS_CIDKEYED_INVALID;
import static org.apache.pdfbox.preflight.PreflightConstants.ERROR_FONTS_CIDKEYED_SYSINFO;
+import static org.apache.pdfbox.preflight.PreflightConstants.ERROR_FONTS_CID_DAMAGED;
import static org.apache.pdfbox.preflight.PreflightConstants.ERROR_FONTS_CID_CMAP_DAMAGED;
import static org.apache.pdfbox.preflight.PreflightConstants.ERROR_FONTS_DICTIONARY_INVALID;
import static org.apache.pdfbox.preflight.PreflightConstants.FONT_DICTIONARY_DEFAULT_CMAP_WMODE;
@@ -46,8 +47,8 @@ import org.apache.pdfbox.cos.COSDictiona
import org.apache.pdfbox.cos.COSDocument;
import org.apache.pdfbox.cos.COSName;
import org.apache.pdfbox.cos.COSStream;
-import org.apache.pdfbox.pdmodel.font.PDCIDFontType0Font;
-import org.apache.pdfbox.pdmodel.font.PDCIDFontType2Font;
+import org.apache.pdfbox.pdmodel.font.PDCIDFontType0;
+import org.apache.pdfbox.pdmodel.font.PDCIDFontType2;
import org.apache.pdfbox.pdmodel.font.PDFont;
import org.apache.pdfbox.pdmodel.font.PDType0Font;
import org.apache.pdfbox.preflight.PreflightContext;
@@ -157,22 +158,34 @@ public class Type0FontValidator extends
/**
* Create the validation object for CIDType0 Font
- *
- * @return
*/
protected FontValidator<? extends FontContainer> createCIDType0FontValidator(COSDictionary fDict)
{
- return new CIDType0FontValidator(context, new PDCIDFontType0Font(fDict, (PDType0Font)font));
+ try
+ {
+ return new CIDType0FontValidator(context, new PDCIDFontType0(fDict, (PDType0Font)font));
+ }
+ catch (IOException e)
+ {
+ this.fontContainer.push(new ValidationError(ERROR_FONTS_CID_DAMAGED, "The CIDType0 font is damaged"));
+ return null;
+ }
}
/**
* Create the validation object for CIDType2 Font
- *
- * @return
*/
protected FontValidator<? extends FontContainer> createCIDType2FontValidator(COSDictionary fDict)
{
- return new CIDType2FontValidator(context, new PDCIDFontType2Font(fDict, (PDType0Font)font));
+ try
+ {
+ return new CIDType2FontValidator(context, new PDCIDFontType2(fDict, (PDType0Font)font));
+ }
+ catch (IOException e)
+ {
+ this.fontContainer.push(new ValidationError(ERROR_FONTS_CID_DAMAGED, "The CIDType2 font is damaged"));
+ return null;
+ }
}
/**
@@ -180,8 +193,6 @@ public class Type0FontValidator extends
*
* The CMap entry must be a dictionary in a PDF/A. This entry can be a String only if the String value is Identity-H
* or Identity-V
- *
- * @param encoding
*/
protected void checkEncoding()
{
Modified: pdfbox/no-awt/preflight/src/main/java/org/apache/pdfbox/preflight/font/Type3FontValidator.java
URL: http://svn.apache.org/viewvc/pdfbox/no-awt/preflight/src/main/java/org/apache/pdfbox/preflight/font/Type3FontValidator.java?rev=1618516&r1=1618515&r2=1618516&view=diff
==============================================================================
--- pdfbox/no-awt/preflight/src/main/java/org/apache/pdfbox/preflight/font/Type3FontValidator.java (original)
+++ pdfbox/no-awt/preflight/src/main/java/org/apache/pdfbox/preflight/font/Type3FontValidator.java Sun Aug 17 20:02:18 2014
@@ -385,15 +385,7 @@ public class Type3FontValidator extends
private String getCharNameFromEncoding(int cid) throws ValidationException
{
- try
- {
- return this.encoding.getName(cid);
- }
- catch (IOException e)
- {
- // shouldn't occur
- throw new ValidationException("Unable to check Widths consistency", e);
- }
+ return this.encoding.getName(cid);
}
/**
Modified: pdfbox/no-awt/preflight/src/main/java/org/apache/pdfbox/preflight/font/container/CIDType0Container.java
URL: http://svn.apache.org/viewvc/pdfbox/no-awt/preflight/src/main/java/org/apache/pdfbox/preflight/font/container/CIDType0Container.java?rev=1618516&r1=1618515&r2=1618516&view=diff
==============================================================================
--- pdfbox/no-awt/preflight/src/main/java/org/apache/pdfbox/preflight/font/container/CIDType0Container.java (original)
+++ pdfbox/no-awt/preflight/src/main/java/org/apache/pdfbox/preflight/font/container/CIDType0Container.java Sun Aug 17 20:02:18 2014
@@ -23,11 +23,10 @@ package org.apache.pdfbox.preflight.font
import java.io.IOException;
import java.util.ArrayList;
-import java.util.Collection;
import java.util.List;
+import org.apache.fontbox.cff.CFFCIDFont;
import org.apache.fontbox.cff.CFFFont;
-import org.apache.fontbox.cff.CFFFont.Mapping;
import org.apache.pdfbox.pdmodel.font.PDFont;
public class CIDType0Container extends FontContainer
@@ -42,65 +41,16 @@ public class CIDType0Container extends F
@Override
protected float getFontProgramWidth(int cid)
{
- // build the font container and keep it in the Handler.
- boolean cidFound = false;
- for (CFFFont font : this.lCFonts)
+ CFFCIDFont cffFont = (CFFCIDFont)lCFonts.get(0);
+ try
{
- Collection<Mapping> cMapping = font.getMappings();
- for (Mapping mapping : cMapping)
- {
- /*
- * REMARK : May be this code must be changed like the Type1FontContainer to Map the SID with the
- * character name? Not enough PDF with this kind of Font to test the current implementation
- */
- if (mapping.getSID() == cid)
- {
- cidFound = true;
- break;
- }
- }
- if (cidFound)
- {
- break;
- }
+ // fixme: this does not take into account the PDF's CMap or the FontDescriptor's default width
+ return cffFont.getType2CharString(cid).getWidth();
}
-
- float widthInFontProgram = 0;
- if (cidFound || cid == 0)
- {
-
- float defaultGlyphWidth = 0;
- if (this.font.getFontDescriptor() != null)
- {
- defaultGlyphWidth = this.font.getFontDescriptor().getMissingWidth();
- }
-
- try
- {
- // Search the CID in all CFFFont in the FontProgram
- for (CFFFont cff : this.lCFonts)
- {
- widthInFontProgram = cff.getWidth(cid);
- if (widthInFontProgram != defaultGlyphWidth)
- {
- break;
- }
- }
- }
- catch (IOException e)
- {
- widthInFontProgram = -1;
- }
- }
- else
+ catch (IOException e)
{
- /*
- * Cid 0 is commonly used as the NotDef Glyph. this glyph can be used as Space. IN PDF/A-1 the Notdef glyph
- * can be used as space. Not in PDF/A-2
- */
- widthInFontProgram = -1;
+ return -1;
}
- return widthInFontProgram;
}
public void setlCFonts(List<CFFFont> lCFonts)
Modified: pdfbox/no-awt/preflight/src/main/java/org/apache/pdfbox/preflight/font/container/FontContainer.java
URL: http://svn.apache.org/viewvc/pdfbox/no-awt/preflight/src/main/java/org/apache/pdfbox/preflight/font/container/FontContainer.java?rev=1618516&r1=1618515&r2=1618516&view=diff
==============================================================================
--- pdfbox/no-awt/preflight/src/main/java/org/apache/pdfbox/preflight/font/container/FontContainer.java (original)
+++ pdfbox/no-awt/preflight/src/main/java/org/apache/pdfbox/preflight/font/container/FontContainer.java Sun Aug 17 20:02:18 2014
@@ -104,7 +104,7 @@ public abstract class FontContainer
* @param cid
* @throws GlyphException
*/
- public void checkGlyphWith(int cid) throws GlyphException
+ public void checkGlyphWidth(int cid) throws GlyphException
{
if (isAlreadyComputedCid(cid))
{
Modified: pdfbox/no-awt/preflight/src/main/java/org/apache/pdfbox/preflight/font/container/Type1Container.java
URL: http://svn.apache.org/viewvc/pdfbox/no-awt/preflight/src/main/java/org/apache/pdfbox/preflight/font/container/Type1Container.java?rev=1618516&r1=1618515&r2=1618516&view=diff
==============================================================================
--- pdfbox/no-awt/preflight/src/main/java/org/apache/pdfbox/preflight/font/container/Type1Container.java (original)
+++ pdfbox/no-awt/preflight/src/main/java/org/apache/pdfbox/preflight/font/container/Type1Container.java Sun Aug 17 20:02:18 2014
@@ -25,7 +25,7 @@ import java.io.IOException;
import java.util.List;
import org.apache.fontbox.cff.CFFFont;
-import org.apache.fontbox.cff.CFFFont.Mapping;
+import org.apache.fontbox.cff.CFFType1Font;
import org.apache.pdfbox.pdmodel.font.PDFont;
import org.apache.pdfbox.preflight.PreflightConstants;
import org.apache.pdfbox.preflight.font.util.GlyphException;
@@ -55,6 +55,10 @@ public class Type1Container extends Font
@Override
protected float getFontProgramWidth(int cid) throws GlyphException
{
+ // fixme: Type 1 fonts don't use CIDs, so this method makes little sense,
+ // when making changes I've assumed that "cid" is actually the character code
+ // from the PDF content stream.
+
float widthResult = -1;
try
{
@@ -67,42 +71,15 @@ public class Type1Container extends Font
}
else
{
- /*
- * Retrieves the SID with the Character Name in the encoding map Need
- * more PDF with a Type1C subfont to valid this implementation
- */
- String name = null;
- if (this.font.getFontEncoding() != null) {
- name = this.font.getFontEncoding().getName(cid);
- }
-
- int SID = -1;
-
- /* For each CFF, try to found the SID that correspond to the CID.
- * Look up by name if the encoding entry is present in the PDFont object
- * otherwise use the internal encoding map of the font.
- */
- for (CFFFont cff : lCFonts)
- {
- if (name == null) {
- SID = cff.getEncoding().getSID(cid);
- } else {
- SID = getSIDByCharacterName(name, cff);
- }
-
- if (SID > 0) {
- widthResult = cff.getWidth(SID);
- if (widthResult != defaultGlyphWidth)
- {
- break;
- }
- }
- }
-
- if (SID < 0)
+ // assuming that "cid" is actually the character code
+ String name = this.font.getFontEncoding().getName(cid);
+ if (name.equals(".notdef"))
{
throw new GlyphException(PreflightConstants.ERROR_FONTS_GLYPH_MISSING, cid, "Unknown character CID(" + cid+")");
}
+
+ CFFType1Font cffFont = (CFFType1Font)lCFonts.get(0);
+ widthResult = cffFont.getType1CharString(name).getWidth();
}
}
catch (IOException e)
@@ -113,27 +90,6 @@ public class Type1Container extends Font
return widthResult;
}
- /**
- * Return the SID of the given character name.
- *
- * @param name the character name looked up
- * @param cff Compact Font Format that represents a sub set of the Type1C Font.
- * @return -1 if the name is missing from the Font encoding map, the SID of the character if it is present in the CFF.
- */
- private int getSIDByCharacterName(String name, CFFFont cff)
- {
- int SID = -1;
- for (Mapping m : cff.getMappings())
- {
- if (m.getName().equals(name))
- {
- SID = m.getSID();
- break;
- }
- }
- return SID;
- }
-
public void setType1Font(Type1 type1Font)
{
this.type1Font = type1Font;
Modified: pdfbox/no-awt/preflight/src/main/java/org/apache/pdfbox/preflight/font/util/Type1.java
URL: http://svn.apache.org/viewvc/pdfbox/no-awt/preflight/src/main/java/org/apache/pdfbox/preflight/font/util/Type1.java?rev=1618516&r1=1618515&r2=1618516&view=diff
==============================================================================
--- pdfbox/no-awt/preflight/src/main/java/org/apache/pdfbox/preflight/font/util/Type1.java (original)
+++ pdfbox/no-awt/preflight/src/main/java/org/apache/pdfbox/preflight/font/util/Type1.java Sun Aug 17 20:02:18 2014
@@ -450,21 +450,7 @@ public class Type1
private String getLabelAsName(int cid)
{
- String label = null;
-
- try
- {
- label = this.encoding.getName(cid);
- }
- catch (IOException e)
- {
- label = this.cidToLabel.get(cid);
- if (label == null)
- {
- label = Type1Parser.NOTDEF;
- }
- }
-
+ String label = this.encoding.getName(cid);
return label.charAt(0) == Type1Parser.NAME_START ? label : Type1Parser.NAME_START + label;
}
}
Modified: pdfbox/no-awt/tools/src/main/java/org/apache/pdfbox/tools/TextToPDF.java
URL: http://svn.apache.org/viewvc/pdfbox/no-awt/tools/src/main/java/org/apache/pdfbox/tools/TextToPDF.java?rev=1618516&r1=1618515&r2=1618516&view=diff
==============================================================================
--- pdfbox/no-awt/tools/src/main/java/org/apache/pdfbox/tools/TextToPDF.java (original)
+++ pdfbox/no-awt/tools/src/main/java/org/apache/pdfbox/tools/TextToPDF.java Sun Aug 17 20:02:18 2014
@@ -21,6 +21,8 @@ import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
+import java.util.HashMap;
+import java.util.Map;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
@@ -42,6 +44,25 @@ public class TextToPDF
private int fontSize = 10;
private PDFont font = PDType1Font.HELVETICA;
+ private static final Map<String, PDType1Font> STANDARD_14 = new HashMap<String, PDType1Font>();
+ static
+ {
+ STANDARD_14.put(PDType1Font.TIMES_ROMAN.getBaseFont(), PDType1Font.TIMES_ROMAN);
+ STANDARD_14.put(PDType1Font.TIMES_BOLD.getBaseFont(), PDType1Font.TIMES_BOLD);
+ STANDARD_14.put(PDType1Font.TIMES_ITALIC.getBaseFont(), PDType1Font.TIMES_ITALIC);
+ STANDARD_14.put(PDType1Font.TIMES_BOLD_ITALIC.getBaseFont(), PDType1Font.TIMES_BOLD_ITALIC);
+ STANDARD_14.put(PDType1Font.HELVETICA.getBaseFont(), PDType1Font.HELVETICA);
+ STANDARD_14.put(PDType1Font.HELVETICA_BOLD.getBaseFont(), PDType1Font.HELVETICA_BOLD);
+ STANDARD_14.put(PDType1Font.HELVETICA_OBLIQUE.getBaseFont(), PDType1Font.HELVETICA_OBLIQUE);
+ STANDARD_14.put(PDType1Font.HELVETICA_BOLD_OBLIQUE.getBaseFont(), PDType1Font.HELVETICA_BOLD_OBLIQUE);
+ STANDARD_14.put(PDType1Font.COURIER.getBaseFont(), PDType1Font.COURIER);
+ STANDARD_14.put(PDType1Font.COURIER_BOLD.getBaseFont(), PDType1Font.COURIER_BOLD);
+ STANDARD_14.put(PDType1Font.COURIER_OBLIQUE.getBaseFont(), PDType1Font.COURIER_OBLIQUE);
+ STANDARD_14.put(PDType1Font.COURIER_BOLD_OBLIQUE.getBaseFont(), PDType1Font.COURIER_BOLD_OBLIQUE);
+ STANDARD_14.put(PDType1Font.SYMBOL.getBaseFont(), PDType1Font.SYMBOL);
+ STANDARD_14.put(PDType1Font.ZAPF_DINGBATS.getBaseFont(), PDType1Font.ZAPF_DINGBATS);
+ }
+
/**
* Create a PDF document with some text.
*
@@ -188,7 +209,7 @@ public class TextToPDF
if( args[i].equals( "-standardFont" ))
{
i++;
- app.setFont( PDType1Font.getStandardFont( args[i] ));
+ app.setFont( getStandardFont( args[i] ));
}
else if( args[i].equals( "-ttf" ))
{
@@ -224,7 +245,7 @@ public class TextToPDF
*/
private void usage()
{
- String[] std14 = PDType1Font.getStandard14Names();
+ String[] std14 = getStandard14Names();
System.err.println( "usage: jar -jar pdfbox-app-x.y.z.jar TextToPDF [options] <output-file> <text-file>" );
System.err.println( " -standardFont <name> default:" + PDType1Font.HELVETICA.getBaseFont() );
for (String std14String : std14)
@@ -233,9 +254,32 @@ public class TextToPDF
}
System.err.println( " -ttf <ttf file> The TTF font to use.");
System.err.println( " -fontSize <fontSize> default:10" );
+ }
+ /**
+ * A convenience method to get one of the standard 14 font from name.
+ *
+ * @param name The name of the font to get.
+ *
+ * @return The font that matches the name or null if it does not exist.
+ */
+ private static PDType1Font getStandardFont(String name)
+ {
+ return STANDARD_14.get(name);
}
+
+ /**
+ * This will get the names of the standard 14 fonts.
+ *
+ * @return An array of the names of the standard 14 fonts.
+ */
+ private static String[] getStandard14Names()
+ {
+ return STANDARD_14.keySet().toArray(new String[14]);
+ }
+
+
/**
* @return Returns the font.
*/