From 9b2459fe813582b08c29fb8c044b0156d8a1e214 Mon Sep 17 00:00:00 2001 From: Maxim Date: Sat, 9 Nov 2024 22:41:44 +0300 Subject: [PATCH] Update width calculation for CFFType1FontProgram --- .../pd/font/cff/CFFType1FontProgram.java | 37 ++++++++++++++----- .../pd/font/cff/CharStringsWidths.java | 12 +++--- .../org/verapdf/pd/font/cmap/CMapParser.java | 4 +- 3 files changed, 36 insertions(+), 17 deletions(-) diff --git a/src/main/java/org/verapdf/pd/font/cff/CFFType1FontProgram.java b/src/main/java/org/verapdf/pd/font/cff/CFFType1FontProgram.java index e798fa47..55beec15 100644 --- a/src/main/java/org/verapdf/pd/font/cff/CFFType1FontProgram.java +++ b/src/main/java/org/verapdf/pd/font/cff/CFFType1FontProgram.java @@ -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; } @@ -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); @@ -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); } /** diff --git a/src/main/java/org/verapdf/pd/font/cff/CharStringsWidths.java b/src/main/java/org/verapdf/pd/font/cff/CharStringsWidths.java index 882d579f..42e2831c 100644 --- a/src/main/java/org/verapdf/pd/font/cff/CharStringsWidths.java +++ b/src/main/java/org/verapdf/pd/font/cff/CharStringsWidths.java @@ -129,7 +129,8 @@ 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); @@ -137,12 +138,11 @@ public float getWidth(int 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; } /** diff --git a/src/main/java/org/verapdf/pd/font/cmap/CMapParser.java b/src/main/java/org/verapdf/pd/font/cmap/CMapParser.java index c3b441b8..6f5adbd9 100644 --- a/src/main/java/org/verapdf/pd/font/cmap/CMapParser.java +++ b/src/main/java/org/verapdf/pd/font/cmap/CMapParser.java @@ -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"; @@ -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));