8288067: Avoid redundant HashMap.containsKey call in Type1Font.expandAbbreviation

Reviewed-by: attila, prr
This commit is contained in:
Andrey Turbanov 2022-07-14 06:10:28 +00:00
parent 5d588eda97
commit 02fae60740

@ -102,8 +102,8 @@ public class Type1Font extends FileFont {
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<>();
@ -143,7 +143,7 @@ public class Type1Font extends FileFont {
for(int i=0; i<styleTokens.length; i++) {
styleNameTokes.add(styleTokens[i]);
}
}
}
/**
@ -401,7 +401,7 @@ public class Type1Font extends FileFont {
}
}
} catch (Exception e) {
throw new FontFormatException(e.toString());
throw new FontFormatException(e.toString());
}
/* Ignore all fonts besides Type1 (e.g. Type3 fonts) */
@ -409,24 +409,24 @@ public class Type1Font extends FileFont {
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
@ -446,8 +446,7 @@ public class Type1Font extends FileFont {
}
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
@ -460,19 +459,17 @@ public class Type1Font extends FileFont {
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) {
@ -545,7 +542,7 @@ public class Type1Font extends FileFont {
res.append(s.substring(start, end));
}
start = end;
}
}
return res.toString();
}