Skip to content
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
4 changes: 4 additions & 0 deletions src/main/java/org/verapdf/pd/font/PDFont.java
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,10 @@ public String toUnicode(int code) {
return cMapToUnicode(code);
}

public String toUnicode(int code, boolean isStrict) {
return toUnicode(code);
}

/**
* Gets toUnicode value just from toUnicode cMap.
*
Expand Down
27 changes: 20 additions & 7 deletions src/main/java/org/verapdf/pd/font/PDSimpleFont.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,11 @@ public PDSimpleFont(COSDictionary dictionary) {
*/
@Override
public String toUnicode(int code) {
return toUnicode(code, true);
}

@Override
public String toUnicode(int code, boolean isStrict) {
String unicodeString = super.toUnicode(code);
if (unicodeString != null) {
return unicodeString;
Expand All @@ -70,14 +74,23 @@ public String toUnicode(int code) {
glyphName = fontProgram.getGlyphName(code);
}
if (glyphName != null) {
AdobeGlyphList.AGLUnicode unicode = AdobeGlyphList.get(glyphName);
if (unicode != AdobeGlyphList.empty()) {
return unicode.getUnicodeString();
}
LOGGER.log(Level.FINE, "Cannot find glyph " + glyphName + " in Adobe Glyph List.");
if (ZapfDingbats.hasGlyphName(glyphName) || SymbolSet.hasGlyphName(glyphName)) {
return " "; // indicates that toUnicode should not be checked.
if (isStrict) {
AdobeGlyphList.AGLUnicode unicode = AdobeGlyphList.get(glyphName);
if (unicode != AdobeGlyphList.empty()) {
return unicode.getUnicodeString();
}
LOGGER.log(Level.FINE, "Cannot find glyph " + glyphName + " in Adobe Glyph List.");
if (ZapfDingbats.hasGlyphName(glyphName) || SymbolSet.hasGlyphName(glyphName)) {
return " "; // indicates that toUnicode should not be checked.
}
} else {
String mapped = AdobeGlyphList.mapGlyphNameToUnicode(glyphName);
if (!mapped.isEmpty()) {
return mapped;
}
LOGGER.log(Level.FINE, "Cannot find glyph " + glyphName + " in Adobe Glyph List, Zapf Dingbats, or as uni/u name.");
}

return null;
}
LOGGER.log(Level.FINE, "Cannot find encoding for glyph with code " + code + " in font " + this.getName());
Expand Down
91 changes: 91 additions & 0 deletions src/main/java/org/verapdf/pd/font/truetype/AdobeGlyphList.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@


import org.verapdf.io.SeekableInputStream;
import org.verapdf.pd.font.type1.ZapfDingbats;

import java.io.File;
import java.io.IOException;
Expand Down Expand Up @@ -161,4 +162,94 @@ public String getUnicodeString() {
return new String(res, 0, res.length);
}
}

public static String mapGlyphNameToUnicode(String glyphName) {
int dot = glyphName.indexOf('.');
if (dot >= 0) {
glyphName = glyphName.substring(0, dot);
}

String[] components = glyphName.split("_", -1);
StringBuilder result = new StringBuilder();
for (String comp : components) {
result.append(mapComponent(comp));
}
return result.toString();
}

private static String mapComponent(String component) {
if (ZapfDingbats.hasGlyphName(component)) {
return ZapfDingbats.toUnicode(component);
}

AdobeGlyphList.AGLUnicode unicode = AdobeGlyphList.get(component);
if (unicode != AdobeGlyphList.empty()) {
return unicode.getUnicodeString();
}

if (component.startsWith("uni") && component.length() > 3) {
String result = parseUniComponent(component);
if (result != null) {
return result;
}
}

if (component.startsWith("u") && component.length() > 1) {
String result = parseUComponent(component);
if (result != null) {
return result;
}
}

return "";
}

private static String parseUniComponent(String component) {
String hex = component.substring(3);
if (hex.isEmpty() || hex.length() % 4 != 0) {
return null;
}
StringBuilder sb = new StringBuilder(hex.length() / 4);
for (int i = 0; i < hex.length(); i += 4) {
String group = hex.substring(i, i + 4);
if (isNotValidHex(hex)) {
return null;
}
int cp = Integer.parseInt(group, 16);
if (!((cp >= 0x0000 && cp <= 0xD7FF) || (cp >= 0xE000 && cp <= 0xFFFF))) {
return null;
}
sb.append((char) cp);
}
return sb.toString();
}

private static String parseUComponent(String component) {
String hex = component.substring(1);
if (hex.length() < 4 || hex.length() > 6) {
return null;
}
if (isNotValidHex(hex)) {
return null;
}
int cp = Integer.parseInt(hex, 16);
if (!((cp >= 0x0000 && cp <= 0xD7FF) || (cp >= 0xE000 && cp <= 0x10FFFF))) {
return null;
}
if (cp <= 0xFFFF) {
return String.valueOf((char) cp);
} else {
return new String(Character.toChars(cp));
}
}

private static boolean isNotValidHex(String hex) {
for (int i = 0; i < hex.length(); i++) {
char c = hex.charAt(i);
if (!((c >= '0' && c <= '9') || (c >= 'A' && c <= 'F'))) {
return true;
}
}
return false;
}
}
Loading
Loading