Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update width calculation for CFFType1FontProgram #652

Open
wants to merge 1 commit into
base: integration
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 28 additions & 9 deletions src/main/java/org/verapdf/pd/font/cff/CFFType1FontProgram.java
Original file line number Diff line number Diff line change
Expand Up @@ -242,8 +242,11 @@ public String getGlyphName(int code) {
if (code < CFFPredefined.EXPERT_ENCODING.length) {
return CFFPredefined.STANDARD_STRINGS[CFFPredefined.EXPERT_ENCODING[code]];
}
} else if (code < encoding.length && encoding[code] + 1 < inverseCharSet.size()) {
return inverseCharSet.get(encoding[code] + 1);
} else {
Integer gid = getGIDFromCharCode(code);
if (gid != null && gid < inverseCharSet.size()) {
return inverseCharSet.get(gid);
}
}
return NOTDEF_STRING;
}
Expand All @@ -265,19 +268,31 @@ public Double getDescent() {
public float getWidth(int charCode) {
if (externalCMap != null) {
int gid = this.externalCMap.toCID(charCode);
float res = this.widths.getWidth(gid);
float res = getWidthFromGID(gid);
if (res != -1.) {
return res;
} else {
return this.widths.getWidth(0);
}
return getWidthFromGID(0);
}
try {
if (!isStandardEncoding && !isExpertEncoding) {
Integer gid = getGIDFromCharCode(charCode);
if (gid != null) {
return getWidthByGID(gid);
}
}
return this.getWidth(getGlyphName(charCode));
} catch (ArrayIndexOutOfBoundsException e) {
return -1;
}
}

private Integer getGIDFromCharCode(int charCode) {
if (charCode >= 0 && charCode < encoding.length) {
return encoding[charCode] + 1;
}
return null;
}

public float getWidthFromGID(int gid) {
return widths.getWidth(gid);
Expand All @@ -297,11 +312,15 @@ public boolean containsGlyph(String glyphName) {
*/
@Override
public float getWidth(String charName) {
Integer index = this.charSet.get(charName);
if (index == null || index >= this.widths.getWidthsAmount() || index < 0) {
return this.widths.getWidth(0);
Integer gid = this.charSet.get(charName);
return getWidthByGID(gid);
}

private float getWidthByGID(Integer gid) {
if (gid == null || gid < 0 || gid >= this.widths.getWidthsAmount()) {
return getWidthFromGID(0);
}
return this.widths.getWidth(index);
return getWidthFromGID(gid);
}

/**
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/org/verapdf/pd/font/cff/CharStringsWidths.java
Original file line number Diff line number Diff line change
Expand Up @@ -129,20 +129,20 @@ public CharStringsWidths(boolean isSubset, int charStringType, CFFCharStringsHan
public float getWidth(int gid) {
if (isSubset && gid >= 0 && gid < subsetFontWidths.length) {
return subsetFontWidths[gid];
} else if (!isSubset) {
}
if (!isSubset) {
Float res = generalFontWidths.get(gid);
if (res == null) {
CFFNumber width = getWidthFromCharstring(gid);
res = getActualWidth(width, gid);
this.generalFontWidths.put(gid, res);
}
return res;
} else {
LOGGER.log(Level.FINE, "Can't get width of charstring " + gid +
" in font subset, got only " + (subsetFontWidths.length - 1) +
" charstrings.");
return DEFAULT_WIDTH;
}
LOGGER.log(Level.FINE, "Can't get width of charstring " + gid +
" in font subset, got only " + (subsetFontWidths.length - 1) +
" charstrings.");
return DEFAULT_WIDTH;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/verapdf/pd/font/cmap/CMapParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public class CMapParser extends PSParser {
private final CMap cMap;

private static final String WMODE_STRING = "WMode";
private static final String REGISTRY_SRTRING = "Registry";
private static final String REGISTRY_STRING = "Registry";
private static final String ORDERING_STRING = "Ordering";
private static final String CMAP_NAME_STRING = "CMapName";
private static final String SUPPLEMENT_STRING = "Supplement";
Expand Down Expand Up @@ -339,7 +339,7 @@ private void checkTokenType(Token.Type type, String where) throws IOException {

private void setValuesFromUserDict(CMap cMap) {
cMap.setName(getStringFromUserDict(CMAP_NAME_STRING));
cMap.setRegistry(getStringFromUserDict(REGISTRY_SRTRING));
cMap.setRegistry(getStringFromUserDict(REGISTRY_STRING));
cMap.setOrdering(getStringFromUserDict(ORDERING_STRING));
cMap.setwMode((int) getLongFromUserDict(WMODE_STRING));
cMap.setSupplement((int) getLongFromUserDict(SUPPLEMENT_STRING));
Expand Down
Loading