Skip to content

Commit

Permalink
Update width calculation got CFFType1FontProgram
Browse files Browse the repository at this point in the history
  • Loading branch information
MaximPlusov committed Nov 9, 2024
1 parent ee4eb2e commit d8da1c6
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 17 deletions.
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

0 comments on commit d8da1c6

Please sign in to comment.