Skip to content

Commit

Permalink
GH-80 some code clean up.
Browse files Browse the repository at this point in the history
  • Loading branch information
Patrick Corless committed Feb 5, 2021
1 parent 14baeec commit 59061bb
Show file tree
Hide file tree
Showing 12 changed files with 34 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ public class FontDescriptor extends Dictionary {
public static final Name FONT_FILE_3_CID_FONT_TYPE_0C = new Name("CIDFontType0C");
public static final Name FONT_FILE_3_OPEN_TYPE = new Name("OpenType");

private boolean embeddedFontDamaged;

/**
* Creates a new instance of a FontDescriptor.
*
Expand Down Expand Up @@ -215,6 +217,10 @@ public FontFile getEmbeddedFont() {
return font;
}

public boolean isEmbeddedFontDamaged() {
return embeddedFontDamaged;
}

/**
* Gets the fonts bounding box.
*
Expand Down Expand Up @@ -311,6 +317,7 @@ public synchronized void init(Name subtype) {
// occurs.
catch (Throwable e) {
logger.log(Level.FINE, "Error Reading Embedded Font ", e);
embeddedFontDamaged = true;
}

inited = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ enum ByteEncoding {

long LAYOUT_NONE = 0;

// todo do some refacotring of method name 'e' awlays seemed strange to me.

Point2D echarAdvance(char ech);

FontFile deriveFont(AffineTransform at);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ public void init() {
font = new ZFontType3(library, entries);
((ZFontType3) font).setParentResource(parentResource);
super.init();
inited = inited;
inited = true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ protected void parseCidToGidMap() {
Object gidMap = library.getObject(entries, CID_TO_GID_MAP_KEY);

// ordering != null && ordering.startsWith("Identity")) || ((gidMap != null || !isFontSubstitution)
if (true) {
if (!isFontSubstitution) {
// CMap subfontToUnicodeCMap = toUnicodeCMap != null ? toUnicodeCMap : CMap.IDENTITY;
if (gidMap == null) {
// throw new Exception("null CID_TO_GID_MAP_KEY " + gidMap);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,14 @@ public ZFontOpenType(byte[] fontBytes) throws Exception {
OpenTypeFont openTypeFont = otfParser.parse(new ByteArrayInputStream(fontBytes));
trueTypeFont = openTypeFont;
fontBoxFont = trueTypeFont;

if (openTypeFont.isPostScript()) {
// fontIsDamaged = true;
// logger.warning("Found CFF/OTF but expected embedded TTF font " + fd.getFontName());
isDamaged = true;
logger.warning("Found CFF/OTF but expected embedded TTF font " + trueTypeFont.getName());
}

extractCmapTable();
}
} catch (Throwable e) {
logger.log(Level.WARNING, "Error reading font file with ", e);
// fontIsDamaged = true;
throw new Exception(e);
}
}
Expand All @@ -57,11 +54,9 @@ public void drawEstring(Graphics2D g, String estr, float x, float y, long layout
if (glyphData == null) {
outline = new GeneralPath();
} else {
// must scaled by caller using FontMatrix
outline = glyphData.getPath();
}

// clean up, not very efficient
g.translate(x, y);
g.transform(this.fontTransform);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import java.util.logging.Level;
import java.util.logging.Logger;

public class ZFontTrueType extends ZSimpleFont {
public class ZFontTrueType extends ZSimpleFont implements Cloneable {

private static final Logger logger =
Logger.getLogger(ZFontTrueType.class.toString());
Expand Down Expand Up @@ -60,7 +60,6 @@ public ZFontTrueType(byte[] fontBytes) throws Exception {
}
} catch (Throwable e) {
logger.log(Level.WARNING, "Error reading font file with ", e);
// fontIsDamaged = true;
throw new Exception(e);
}
}
Expand All @@ -77,7 +76,6 @@ protected ZFontTrueType(ZFontTrueType font) {

@Override
public Point2D echarAdvance(char ech) {
// todo need to work with the gid.....
if (encoding != null) {
return super.echarAdvance(ech);
} else if (widths != null) {
Expand Down Expand Up @@ -111,9 +109,6 @@ public void drawEstring(Graphics2D g, String estr, float x, float y, long layout
outline = glyphData.getPath();
}
}


// clean up, not very efficient
g.translate(x, y);
g.transform(this.fontTransform);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,27 +22,22 @@ public class ZFontType0 extends ZSimpleFont {
private static final Logger logger =
Logger.getLogger(ZFontType0.class.toString());

// todo shout out to PDFbox.
private CFFCIDFont cidFont; // Top DICT that uses CIDFont operators
private FontBoxFont t1Font; // Top DICT that does not use CIDFont operators

// todo credit pdfbox for stream correction code
public ZFontType0(Stream fontStream) throws Exception {

byte[] fontBytes = fontStream.getDecodedStreamBytes();
CFFFont cffFont = null;
if (fontBytes != null && fontBytes.length > 0 && (fontBytes[0] & 0xff) == '%') {
// todo throw exception so substitution kicks in? still not too sure what to do here for fallback
logger.warning("Found PFB but expected embedded CFF font");
// fontIsDamaged = true;
isDamaged = true;
} else if (fontBytes != null) {
CFFParser cffParser = new CFFParser();
try {
cffFont = cffParser.parse(fontBytes, new FF3ByteSource(fontStream)).get(0);
} catch (IOException e) {
// todo throw exception so substitution kicks in.
logger.log(Level.WARNING, "Can't read the embedded CFF font ", e);
// fontIsDamaged = true;
throw new Exception(e);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ public ZFontType1(Stream fontStream) throws Exception {
public ZFontType1(URL url) throws IOException {
byte[] fontBytes = url.openStream().readAllBytes();
source = url;
// todo clean up error handling
type1Font = Type1Font.createWithPFB(fontBytes);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,15 @@ public class ZFontType1C extends ZSimpleFont {
private CFFType1Font cffType1Font;

public ZFontType1C(Stream fontStream) throws Exception {
byte[] fontBytes = fontStream.getDecodedStreamBytes();
try {
byte[] fontBytes = fontStream.getDecodedStreamBytes();
try {
if (fontBytes != null) {
// note: this could be an OpenType file, fortunately CFFParser can handle that
CFFParser cffParser = new CFFParser();
cffType1Font = (CFFType1Font) cffParser.parse(fontBytes, new FontFileByteSource(fontStream)).get(0);
}
fontBoxFont = cffType1Font;
} catch (IOException e) {
logger.log(Level.FINE, "Error reading font file with ", e);
// fontIsDamaged = true;
if (fontBytes != null) {
// note: this could be an OpenType file, fortunately CFFParser can handle that
CFFParser cffParser = new CFFParser();
cffType1Font = (CFFType1Font) cffParser.parse(fontBytes, new FontFileByteSource(fontStream)).get(0);
}

} catch (Throwable e) {
fontBoxFont = cffType1Font;
} catch (IOException e) {
logger.log(Level.FINE, "Error reading font file with ", e);
throw new Exception(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,26 +30,19 @@ public class ZFontType2 extends ZSimpleFont { //extends ZFontTrueType {

private CMap cid2gid;

// todo credit pdfbox
public ZFontType2(Stream fontStream) throws Exception {
try {
byte[] fontBytes = fontStream.getDecodedStreamBytes();
// embedded OTF or TTF
OTFParser otfParser = new OTFParser(true);
OpenTypeFont otf = otfParser.parse(new ByteArrayInputStream(fontBytes));
trueTypeFont = otf;

OpenTypeFont openTypeFont = otfParser.parse(new ByteArrayInputStream(fontBytes));
trueTypeFont = openTypeFont;
if (openTypeFont.isPostScript()) {
isDamaged = true;
logger.warning("Found CFF/OTF but expected embedded TTF font " + trueTypeFont.getName());
}
// extractCmapTable();

// if (otf.isPostScript())
// {
// // PDFBOX-3344 contains PostScript outlines instead of TrueType
// fontIsDamaged = true;
// LOG.warn("Found CFF/OTF but expected embedded TTF font " + fd.getFontName());
// }
} catch (Throwable e) {
// // NPE due to TTF parser being buggy
// fontIsDamaged = true;
logger.log(Level.SEVERE, "Could not initialize type2 font", e);
throw new Exception(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,7 @@ public ZFontType3(Library library, HashMap properties) {
fontMatrix = new AffineTransform(0.001f, 0.0f, 0.0f, 0.001f, 0.0f, 0.0f);
}

// todo fix up all simple font unicode assignments
toUnicode = null;//CMap.IDENTITY;
toUnicode = org.icepdf.core.pobjects.fonts.ofont.CMap.IDENTITY;

// CharProcs resources, contains glyph name/stream pairs.
o = library.getObject(properties, CHAR_PROCS_KEY);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public abstract class ZSimpleFont implements FontFile {
protected AffineTransform fontTransform = new AffineTransform();
protected AffineTransform gsTransform = new AffineTransform();

// todo fontDamaged flags
protected boolean isDamaged;

protected ZSimpleFont() {

Expand Down Expand Up @@ -249,6 +249,10 @@ public void setIsCid() {

}

public boolean isDamaged() {
return isDamaged;
}

protected void setFontTransform(AffineTransform at) {
gsTransform = new AffineTransform(at);
fontTransform = new AffineTransform(fontMatrix);
Expand Down

0 comments on commit 59061bb

Please sign in to comment.