Skip to content

Commit

Permalink
Update logging for TrueType fonts
Browse files Browse the repository at this point in the history
  • Loading branch information
MaximPlusov committed Oct 9, 2024
1 parent 678e621 commit 9244e1c
Show file tree
Hide file tree
Showing 10 changed files with 53 additions and 28 deletions.
4 changes: 2 additions & 2 deletions src/main/java/org/verapdf/pd/font/PDCIDFont.java
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ public FontProgram getFontProgram() {
this.fontProgram = StaticResources.getCachedFont(fontProgramID);
if (fontProgram == null) {
try (ASInputStream fontData = trueTypeFontFile.getData(COSStream.FilterFlags.DECODE)) {
this.fontProgram = new CIDFontType2Program(fontData, this.cMap, cidToGIDMap);
this.fontProgram = new CIDFontType2Program(fontData, this.cMap, cidToGIDMap, key);
StaticResources.cacheFontProgram(fontProgramID, this.fontProgram);
} catch (IOException e) {
LOGGER.log(Level.FINE, "Can't read TrueType font program.", e);
Expand Down Expand Up @@ -204,7 +204,7 @@ public FontProgram getFontProgram() {
try (ASInputStream fontData = fontFile.getData(COSStream.FilterFlags.DECODE)) {
this.fontProgram = new OpenTypeFontProgram(
fontData, isCFF, isCIDFontType2, isSymbolic, encoding,
this.cMap, isSubset, cidToGIDMap);
this.cMap, isSubset, cidToGIDMap, key);
StaticResources.cacheFontProgram(fontProgramID, this.fontProgram);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import org.verapdf.as.io.ASInputStream;
import org.verapdf.as.io.ASMemoryInStream;
import org.verapdf.cos.COSKey;
import org.verapdf.cos.COSObject;
import org.verapdf.io.SeekableInputStream;
import org.verapdf.pd.font.FontProgram;
Expand Down Expand Up @@ -57,6 +58,7 @@ public class OpenTypeFontProgram implements FontProgram {
private boolean successfullyParsed = false;
private final CMap externalCMap;
private final COSObject cidToGIDMap;
private final COSKey key;

/**
* Constructor from stream, containing font data, and encoding details.
Expand All @@ -66,7 +68,7 @@ public class OpenTypeFontProgram implements FontProgram {
* @param encoding is value of /Encoding in font dictionary.
*/
public OpenTypeFontProgram(ASInputStream source, boolean isCFF, boolean isCIDFontType2, boolean isSymbolic,
COSObject encoding, CMap externalCMap, boolean isSubset, COSObject cidToGIDMap) {
COSObject encoding, CMap externalCMap, boolean isSubset, COSObject cidToGIDMap, COSKey key) {
this.source = source;
this.isCFF = isCFF;
this.isCIDFontType2 = isCIDFontType2;
Expand All @@ -75,6 +77,7 @@ public OpenTypeFontProgram(ASInputStream source, boolean isCFF, boolean isCIDFon
this.externalCMap = externalCMap;
this.isSubset = isSubset;
this.cidToGIDMap = cidToGIDMap;
this.key = key;
}

/**
Expand Down Expand Up @@ -140,15 +143,15 @@ public void parseFont() throws IOException {
if (!attemptedParsing) {
attemptedParsing = true;
if (isCIDFontType2) {
this.font = new CIDFontType2Program(source, externalCMap, cidToGIDMap);
this.font = new CIDFontType2Program(source, externalCMap, cidToGIDMap, key);
this.font.parseFont();
} else if (isCFF) {
try (ASInputStream cffTable = getCFFTable()) {
this.font = new CFFFontProgram(cffTable, externalCMap, isSubset);
this.font.parseFont();
}
} else {
this.font = new TrueTypeFontProgram(source, isSymbolic, encoding);
this.font = new TrueTypeFontProgram(source, isSymbolic, encoding, key);
this.font.parseFont();
}
StaticResources.cacheFontProgram(null, this.font);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import org.verapdf.as.io.ASInputStream;
import org.verapdf.as.io.ASMemoryInStream;
import org.verapdf.cos.COSKey;
import org.verapdf.pd.font.FontProgram;
import org.verapdf.tools.resource.ASFileStreamCloser;

Expand All @@ -40,16 +41,18 @@ public abstract class BaseTrueTypeProgram implements FontProgram {
protected String[] encodingMappingArray;
private boolean attemptedParsing = false;
private boolean successfullyParsed = false;
private COSKey key;

/**
* Constructor from stream containing font data, and encoding details.
*
* @param stream is stream containing font data.
* @throws IOException if creation of @{link SeekableStream} fails.
*/
public BaseTrueTypeProgram(ASInputStream stream)
public BaseTrueTypeProgram(ASInputStream stream, COSKey key)
throws IOException {
this.parser = new TrueTypeFontParser(stream);
this.parser = new TrueTypeFontParser(stream, key);
this.key = key;
}

/**
Expand Down Expand Up @@ -161,4 +164,11 @@ public Double getDescent() {
}
return null;
}

protected String getErrorMessage(String message) {
if (key != null) {
return message + "(object key = " + key + ')';
}
return message;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
package org.verapdf.pd.font.truetype;

import org.verapdf.as.io.ASInputStream;
import org.verapdf.cos.COSKey;
import org.verapdf.cos.COSObject;
import org.verapdf.pd.font.CIDToGIDMapping;
import org.verapdf.pd.font.FontProgram;
Expand Down Expand Up @@ -48,8 +49,8 @@ public class CIDFontType2Program extends BaseTrueTypeProgram implements FontProg
* @param cidToGID
* @throws IOException
*/
public CIDFontType2Program(ASInputStream stream, CMap cMap, COSObject cidToGID) throws IOException {
super(stream);
public CIDFontType2Program(ASInputStream stream, CMap cMap, COSObject cidToGID, COSKey key) throws IOException {
super(stream, key);
this.cMap = cMap;
this.cidToGID = new CIDToGIDMapping(cidToGID);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ private void parseTrueTypeFontProgram(ASAtom fontFileType) {
if (fontProgram == null) {
try (ASInputStream fontData = trueTypeFontFile.getData(COSStream.FilterFlags.DECODE)) {
this.fontProgram = new TrueTypeFontProgram(fontData, isSymbolic,
encoding);
encoding, key);
StaticResources.cacheFontProgram(fontProgramID, this.fontProgram);
}
}
Expand All @@ -104,7 +104,7 @@ private void parseTrueTypeFontProgram(ASAtom fontFileType) {
if (fontProgram == null) {
try (ASInputStream fontData = trueTypeFontFile.getData(COSStream.FilterFlags.DECODE)) {
this.fontProgram = new OpenTypeFontProgram(fontData, false, false,
isSymbolic, encoding, null, isSubset, null);
isSymbolic, encoding, null, isSubset, null, key);
StaticResources.cacheFontProgram(fontProgramID, this.fontProgram);
}
}
Expand Down
24 changes: 17 additions & 7 deletions src/main/java/org/verapdf/pd/font/truetype/TrueTypeFontParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
package org.verapdf.pd.font.truetype;

import org.verapdf.as.io.ASInputStream;
import org.verapdf.cos.COSKey;

import java.io.IOException;
import java.util.logging.Level;
Expand Down Expand Up @@ -49,9 +50,11 @@ class TrueTypeFontParser extends TrueTypeBaseParser {
private TrueTypeCmapTable cmapParser;
private TrueTypePostTable postParser;
private TrueTypeMaxpTable maxpParser;
private COSKey key;

TrueTypeFontParser(ASInputStream source) throws IOException {
TrueTypeFontParser(ASInputStream source, COSKey key) throws IOException {
super(source);
this.key = key;
}

void readHeader() throws IOException {
Expand Down Expand Up @@ -86,42 +89,42 @@ void readTables() throws IOException {
if (headParser != null) {
this.headParser.readTable();
} else {
LOGGER.log(Level.FINE, "True type font doesn't contain head table. Default value for unitsPerEm used.");
LOGGER.log(Level.FINE, getErrorMessage("True type font doesn't contain head table. Default value for unitsPerEm used."));
this.headParser = new TrueTypeHeadTable();
}

if (hheaParser != null) {
this.hheaParser.readTable();
} else {
throw new IOException("True type font doesn't contain hhea table.");
throw new IOException(getErrorMessage("True type font doesn't contain hhea table."));
}

if (hmtxParser != null) {
this.hmtxParser.setNumberOfHMetrics(hheaParser.getNumberOfHMetrics());
this.hmtxParser.readTable();
} else {
throw new IOException("True type font doesn't contain hmtx table.");
throw new IOException(getErrorMessage("True type font doesn't contain hmtx table."));
}

if (cmapParser != null) {
this.cmapParser.readTable();
} else {
LOGGER.log(Level.FINE, "True type font doesn't contain cmap table.");
LOGGER.log(Level.FINE, getErrorMessage("True type font doesn't contain cmap table."));
}

if (this.maxpParser != null) {
this.maxpParser.readTable();
} else {
this.maxpParser = new TrueTypeMaxpTable(
this.hmtxParser.getLongHorMetrics().length);
LOGGER.log(Level.FINE, "True type font doesn't contain maxp table. Default value for numGlyphs used.");
LOGGER.log(Level.FINE, getErrorMessage("True type font doesn't contain maxp table. Default value for numGlyphs used."));
}

if (this.postParser != null) {
this.postParser.setNumGlyphs(maxpParser.getNumGlyphs());
this.postParser.readTable();
} else {
LOGGER.log(Level.FINE, "True type font doesn't contain post table.");
LOGGER.log(Level.FINE, getErrorMessage("True type font doesn't contain post table."));
}
}

Expand Down Expand Up @@ -160,4 +163,11 @@ TrueTypeCmapSubtable getCmapTable(int platformID, int encodingID) {
TrueTypeHheaTable getHheaParser() {
return hheaParser;
}

protected String getErrorMessage(String message) {
if (key != null) {
return message + "(object key = " + key + ')';
}
return message;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.verapdf.as.ASAtom;
import org.verapdf.as.io.ASInputStream;
import org.verapdf.cos.COSDictionary;
import org.verapdf.cos.COSKey;
import org.verapdf.cos.COSObjType;
import org.verapdf.cos.COSObject;
import org.verapdf.pd.font.FontProgram;
Expand Down Expand Up @@ -56,8 +57,8 @@ public class TrueTypeFontProgram extends BaseTrueTypeProgram implements FontProg
* @throws IOException if creation of @{link SeekableStream} fails.
*/
public TrueTypeFontProgram(ASInputStream stream, boolean isSymbolic,
COSObject encoding) throws IOException {
super(stream);
COSObject encoding, COSKey key) throws IOException {
super(stream, key);
this.isSymbolic = isSymbolic;
if (encoding != null) {
this.encoding = encoding;
Expand Down Expand Up @@ -259,12 +260,12 @@ private void createCIDToNameTable() throws IOException {
System.arraycopy(TrueTypePredefined.WIN_ANSI_ENCODING, 0,
encodingMappingArray, 0, 256);
} else {
throw new IOException("Error in reading /Encoding entry in font dictionary");
throw new IOException(getErrorMessage("Error in reading /Encoding entry in font dictionary"));
}
} else if (this.encoding.getType() == COSObjType.COS_DICT) {
createCIDToNameTableFromDict((COSDictionary) this.encoding.getDirectBase());
} else {
throw new IOException("Error in reading /Encoding entry in font dictionary");
throw new IOException(getErrorMessage("Error in reading /Encoding entry in font dictionary"));
}
}

Expand All @@ -282,7 +283,7 @@ private void createCIDToNameTableFromDict(COSDictionary encoding) throws IOExcep
System.arraycopy(TrueTypePredefined.MAC_EXPERT_ENCODING, 0,
encodingMappingArray, 0, 256);
} else {
LOGGER.log(Level.SEVERE, "Error in reading /Encoding entry in font dictionary");
LOGGER.log(Level.SEVERE, getErrorMessage("Error in reading /Encoding entry in font dictionary"));
System.arraycopy(TrueTypePredefined.STANDARD_ENCODING, 0,
encodingMappingArray, 0, 256);
}
Expand Down Expand Up @@ -310,7 +311,7 @@ private void applyDiffsToEncoding(COSDictionary encoding) throws IOException {
}
}
} else {
throw new IOException("Error in reading /Encoding entry in font dictionary");
throw new IOException(getErrorMessage("Error in reading /Encoding entry in font dictionary"));
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/verapdf/pd/font/type1/PDType1Font.java
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ private void parseType1FontProgram(ASAtom fontFileType) {
if (fontProgram == null) {
try (ASInputStream fontData = type1FontFile.getData(COSStream.FilterFlags.DECODE)) {
this.fontProgram = new OpenTypeFontProgram(fontData, true, false, isSymbolic,
encoding, null, isSubset, null);
encoding, null, isSubset, null, key);
StaticResources.cacheFontProgram(fontProgramID, this.fontProgram);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public void test() throws IOException {
COSObject encoding = COSName.construct(ASAtom.WIN_ANSI_ENCODING);
ASInputStream stream = new InternalInputStream(fontFilePath, 2);
OpenTypeFontProgram font = new OpenTypeFontProgram(stream, true, false, false,
encoding, null, true, null);
encoding, null, true, null, null);
font.parseFont();
assertTrue(font.getFont() instanceof CFFFontProgram);
assertFalse(((CFFFontProgram) font.getFont()).isCIDFont());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public class TrueTypeParserTest {
public void testMonospaced() throws IOException {

TrueTypeFontProgram font = new TrueTypeFontProgram(new InternalInputStream(MONO_FONT_PATH, 2),
IS_SYMBOLIC, ENCODING);
IS_SYMBOLIC, ENCODING, null);
font.parseFont();
assertEquals(600f, font.getWidth("z"), 0.0);
assertEquals(1000f, font.getWidth("yakute"), 0.0);
Expand All @@ -53,7 +53,7 @@ public void testMonospaced() throws IOException {
@Test
public void testRegular() throws IOException {
TrueTypeFontProgram font = new TrueTypeFontProgram(new InternalInputStream(REGULAR_FONT_PATH, 2),
IS_SYMBOLIC, ENCODING);
IS_SYMBOLIC, ENCODING, null);
font.parseFont();
assertEquals(500, (int) font.getWidth("z"));
assertEquals(556, (int) font.getWidth("zero"));
Expand Down

0 comments on commit 9244e1c

Please sign in to comment.