Skip to content

Commit

Permalink
8288067: Avoid redundant HashMap.containsKey call in Type1Font.expand…
Browse files Browse the repository at this point in the history
…Abbreviation

Reviewed-by: attila, prr
  • Loading branch information
Andrey Turbanov committed Jul 14, 2022
1 parent 5d588ed commit 02fae60
Showing 1 changed file with 30 additions and 33 deletions.
63 changes: 30 additions & 33 deletions src/java.desktop/share/classes/sun/font/Type1Font.java
Expand Up @@ -102,8 +102,8 @@ public Object run() {

private String psName = null;

private static HashMap<String, String> styleAbbreviationsMapping;
private static HashSet<String> styleNameTokes;
private static final HashMap<String, String> styleAbbreviationsMapping;
private static final HashSet<String> styleNameTokes;

static {
styleAbbreviationsMapping = new HashMap<>();
Expand Down Expand Up @@ -143,7 +143,7 @@ public Object run() {
for(int i=0; i<styleTokens.length; i++) {
styleNameTokes.add(styleTokens[i]);
}
}
}


/**
Expand Down Expand Up @@ -401,32 +401,32 @@ private void initNames(ByteBuffer bb) throws FontFormatException {
}
}
} catch (Exception e) {
throw new FontFormatException(e.toString());
throw new FontFormatException(e.toString());
}

/* Ignore all fonts besides Type1 (e.g. Type3 fonts) */
if (!"1".equals(fontType)) {
throw new FontFormatException("Unsupported font type");
}

if (psName == null) { //no explicit FontName
// Try to extract font name from the first text line.
// According to Type1 spec first line consist of
// "%!FontType1-SpecVersion: FontName FontVersion"
// or
// "%!PS-AdobeFont-1.0: FontName version"
bb.position(0);
if (bb.getShort() != 0x2521) { //if pfb (do not start with "%!")
//skip segment header and "%!"
bb.position(8);
//NB: assume that first segment is ASCII one
// (is it possible to have valid Type1 font with first binary segment?)
}
String formatType = getSimpleToken(bb);
if (!formatType.startsWith("FontType1-") && !formatType.startsWith("PS-AdobeFont-")) {
throw new FontFormatException("Unsupported font format [" + formatType + "]");
}
psName = getSimpleToken(bb);
if (psName == null) { //no explicit FontName
// Try to extract font name from the first text line.
// According to Type1 spec first line consist of
// "%!FontType1-SpecVersion: FontName FontVersion"
// or
// "%!PS-AdobeFont-1.0: FontName version"
bb.position(0);
if (bb.getShort() != 0x2521) { //if pfb (do not start with "%!")
//skip segment header and "%!"
bb.position(8);
//NB: assume that first segment is ASCII one
// (is it possible to have valid Type1 font with first binary segment?)
}
String formatType = getSimpleToken(bb);
if (!formatType.startsWith("FontType1-") && !formatType.startsWith("PS-AdobeFont-")) {
throw new FontFormatException("Unsupported font format [" + formatType + "]");
}
psName = getSimpleToken(bb);
}

//if we got to the end of file then we did not find at least one of FullName or FamilyName
Expand All @@ -446,8 +446,7 @@ private void initNames(ByteBuffer bb) throws FontFormatException {
}

private String fullName2FamilyName(String name) {
String res, token;
int len, start, end; //length of family name part
int start, end; //length of family name part

//FamilyName is truncated version of FullName
//Truncated tail must contain only style modifiers
Expand All @@ -460,19 +459,17 @@ private String fullName2FamilyName(String name) {
start--;
//as soon as we meet first non style token truncate
// current tail and return
if (!isStyleToken(name.substring(start+1, end))) {
return name.substring(0, end);
if (!isStyleToken(name.substring(start+1, end))) {
return name.substring(0, end);
}
end = start;
end = start;
}

return name; //should not happen
}
return name; //should not happen
}

private String expandAbbreviation(String abbr) {
if (styleAbbreviationsMapping.containsKey(abbr))
return styleAbbreviationsMapping.get(abbr);
return abbr;
return styleAbbreviationsMapping.getOrDefault(abbr, abbr);
}

private boolean isStyleToken(String token) {
Expand Down Expand Up @@ -545,7 +542,7 @@ private String expandName(String s, boolean tryExpandAbbreviations) {
res.append(s.substring(start, end));
}
start = end;
}
}

return res.toString();
}
Expand Down

1 comment on commit 02fae60

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.