Add FontConfig.Font.getPostScriptName API
Bug: 184003133 Bug: 185508395 Test: atest FontManagerTest#fontManager_PostScriptName Change-Id: Ie44e0d500c61b37035a0b39039891da13cd6a5ce
This commit is contained in:
@@ -14122,6 +14122,7 @@ package android.text {
|
||||
method @NonNull public java.io.File getFile();
|
||||
method @Nullable public String getFontFamilyName();
|
||||
method @NonNull public String getFontVariationSettings();
|
||||
method @NonNull public String getPostScriptName();
|
||||
method @NonNull public android.graphics.fonts.FontStyle getStyle();
|
||||
method public int getTtcIndex();
|
||||
method public void writeToParcel(@NonNull android.os.Parcel, int);
|
||||
|
||||
@@ -2489,6 +2489,7 @@ package android.text {
|
||||
method @NonNull public java.io.File getFile();
|
||||
method @Nullable public String getFontFamilyName();
|
||||
method @NonNull public String getFontVariationSettings();
|
||||
method @NonNull public String getPostScriptName();
|
||||
method @NonNull public android.graphics.fonts.FontStyle getStyle();
|
||||
method public int getTtcIndex();
|
||||
method public void writeToParcel(@NonNull android.os.Parcel, int);
|
||||
|
||||
@@ -194,6 +194,7 @@ public final class FontConfig implements Parcelable {
|
||||
public static final class Font implements Parcelable {
|
||||
private final @NonNull File mFile;
|
||||
private final @Nullable File mOriginalFile;
|
||||
private final @NonNull String mPostScriptName;
|
||||
private final @NonNull FontStyle mStyle;
|
||||
private final @IntRange(from = 0) int mIndex;
|
||||
private final @NonNull String mFontVariationSettings;
|
||||
@@ -204,11 +205,12 @@ public final class FontConfig implements Parcelable {
|
||||
*
|
||||
* @hide Only system server can create this instance and passed via IPC.
|
||||
*/
|
||||
public Font(@NonNull File file, @Nullable File originalFile, @NonNull FontStyle style,
|
||||
@IntRange(from = 0) int index, @NonNull String fontVariationSettings,
|
||||
@Nullable String fontFamilyName) {
|
||||
public Font(@NonNull File file, @Nullable File originalFile, @NonNull String postScriptName,
|
||||
@NonNull FontStyle style, @IntRange(from = 0) int index,
|
||||
@NonNull String fontVariationSettings, @Nullable String fontFamilyName) {
|
||||
mFile = file;
|
||||
mOriginalFile = originalFile;
|
||||
mPostScriptName = postScriptName;
|
||||
mStyle = style;
|
||||
mIndex = index;
|
||||
mFontVariationSettings = fontVariationSettings;
|
||||
@@ -224,6 +226,7 @@ public final class FontConfig implements Parcelable {
|
||||
public void writeToParcel(@NonNull Parcel dest, int flags) {
|
||||
dest.writeString8(mFile.getAbsolutePath());
|
||||
dest.writeString8(mOriginalFile == null ? null : mOriginalFile.getAbsolutePath());
|
||||
dest.writeString8(mPostScriptName);
|
||||
dest.writeInt(mStyle.getWeight());
|
||||
dest.writeInt(mStyle.getSlant());
|
||||
dest.writeInt(mIndex);
|
||||
@@ -238,14 +241,15 @@ public final class FontConfig implements Parcelable {
|
||||
File path = new File(source.readString8());
|
||||
String originalPathStr = source.readString8();
|
||||
File originalPath = originalPathStr == null ? null : new File(originalPathStr);
|
||||
String postScriptName = source.readString8();
|
||||
int weight = source.readInt();
|
||||
int slant = source.readInt();
|
||||
int index = source.readInt();
|
||||
String varSettings = source.readString8();
|
||||
String fallback = source.readString8();
|
||||
|
||||
return new Font(path, originalPath, new FontStyle(weight, slant), index,
|
||||
varSettings, fallback);
|
||||
return new Font(path, originalPath, postScriptName, new FontStyle(weight, slant),
|
||||
index, varSettings, fallback);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -313,6 +317,13 @@ public final class FontConfig implements Parcelable {
|
||||
return mIndex;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the PostScript name of this font.
|
||||
*/
|
||||
public @NonNull String getPostScriptName() {
|
||||
return mPostScriptName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the list of axes associated to this font.
|
||||
* @deprecated Use getFontVariationSettings
|
||||
|
||||
@@ -59,7 +59,7 @@ public final class FontListParserTest {
|
||||
+ "</family>";
|
||||
FontConfig.FontFamily expected = new FontConfig.FontFamily(
|
||||
Arrays.asList(
|
||||
new FontConfig.Font(new File("test.ttf"), null,
|
||||
new FontConfig.Font(new File("test.ttf"), null, "test",
|
||||
new FontStyle(FONT_WEIGHT_NORMAL, FONT_SLANT_UPRIGHT),
|
||||
0, "", null)),
|
||||
"sans-serif", LocaleList.getEmptyLocaleList(), VARIANT_DEFAULT);
|
||||
@@ -77,10 +77,10 @@ public final class FontListParserTest {
|
||||
+ "</family>";
|
||||
FontConfig.FontFamily expected = new FontConfig.FontFamily(
|
||||
Arrays.asList(
|
||||
new FontConfig.Font(new File("test.ttf"), null,
|
||||
new FontConfig.Font(new File("test.ttf"), null, "test",
|
||||
new FontStyle(FONT_WEIGHT_NORMAL, FONT_SLANT_UPRIGHT),
|
||||
0, "", null),
|
||||
new FontConfig.Font(new File("test.ttf"), null,
|
||||
new FontConfig.Font(new File("test.ttf"), null, "test",
|
||||
new FontStyle(FONT_WEIGHT_NORMAL, FONT_SLANT_UPRIGHT),
|
||||
0, "", "serif")),
|
||||
null, LocaleList.forLanguageTags("en"), VARIANT_DEFAULT);
|
||||
@@ -97,7 +97,7 @@ public final class FontListParserTest {
|
||||
+ "</family>";
|
||||
FontConfig.FontFamily expected = new FontConfig.FontFamily(
|
||||
Arrays.asList(
|
||||
new FontConfig.Font(new File("test.ttf"), null,
|
||||
new FontConfig.Font(new File("test.ttf"), null, "test",
|
||||
new FontStyle(FONT_WEIGHT_NORMAL, FONT_SLANT_UPRIGHT),
|
||||
0, "", null)),
|
||||
null, LocaleList.forLanguageTags("en"), VARIANT_COMPACT);
|
||||
@@ -114,7 +114,7 @@ public final class FontListParserTest {
|
||||
+ "</family>";
|
||||
FontConfig.FontFamily expected = new FontConfig.FontFamily(
|
||||
Arrays.asList(
|
||||
new FontConfig.Font(new File("test.ttf"), null,
|
||||
new FontConfig.Font(new File("test.ttf"), null, "test",
|
||||
new FontStyle(FONT_WEIGHT_NORMAL, FONT_SLANT_UPRIGHT),
|
||||
0, "", null)),
|
||||
null, LocaleList.forLanguageTags("en"), VARIANT_ELEGANT);
|
||||
@@ -133,13 +133,13 @@ public final class FontListParserTest {
|
||||
+ "</family>";
|
||||
FontConfig.FontFamily expected = new FontConfig.FontFamily(
|
||||
Arrays.asList(
|
||||
new FontConfig.Font(new File("normal.ttf"), null,
|
||||
new FontConfig.Font(new File("normal.ttf"), null, "test",
|
||||
new FontStyle(FONT_WEIGHT_NORMAL, FONT_SLANT_UPRIGHT),
|
||||
0, "", null),
|
||||
new FontConfig.Font(new File("weight.ttf"), null,
|
||||
new FontConfig.Font(new File("weight.ttf"), null, "test",
|
||||
new FontStyle(100, FONT_SLANT_UPRIGHT),
|
||||
0, "", null),
|
||||
new FontConfig.Font(new File("italic.ttf"), null,
|
||||
new FontConfig.Font(new File("italic.ttf"), null, "test",
|
||||
new FontStyle(FONT_WEIGHT_NORMAL, FONT_SLANT_ITALIC),
|
||||
0, "", null)),
|
||||
"sans-serif", LocaleList.getEmptyLocaleList(), VARIANT_DEFAULT);
|
||||
@@ -162,10 +162,10 @@ public final class FontListParserTest {
|
||||
+ "</family>";
|
||||
FontConfig.FontFamily expected = new FontConfig.FontFamily(
|
||||
Arrays.asList(
|
||||
new FontConfig.Font(new File("test-VF.ttf"), null,
|
||||
new FontConfig.Font(new File("test-VF.ttf"), null, "test",
|
||||
new FontStyle(FONT_WEIGHT_NORMAL, FONT_SLANT_UPRIGHT),
|
||||
0, "'wdth' 100.0,'wght' 200.0", null),
|
||||
new FontConfig.Font(new File("test-VF.ttf"), null,
|
||||
new FontConfig.Font(new File("test-VF.ttf"), null, "test",
|
||||
new FontStyle(FONT_WEIGHT_NORMAL, FONT_SLANT_UPRIGHT),
|
||||
0, "'wdth' 400.0,'wght' 700.0", null)),
|
||||
"sans-serif", LocaleList.getEmptyLocaleList(), VARIANT_DEFAULT);
|
||||
@@ -182,10 +182,30 @@ public final class FontListParserTest {
|
||||
+ "</family>";
|
||||
FontConfig.FontFamily expected = new FontConfig.FontFamily(
|
||||
Arrays.asList(
|
||||
new FontConfig.Font(new File("test.ttc"), null,
|
||||
new FontConfig.Font(new File("test.ttc"), null, "test",
|
||||
new FontStyle(FONT_WEIGHT_NORMAL, FONT_SLANT_UPRIGHT),
|
||||
0, "", null),
|
||||
new FontConfig.Font(new File("test.ttc"), null,
|
||||
new FontConfig.Font(new File("test.ttc"), null, "test",
|
||||
new FontStyle(FONT_WEIGHT_NORMAL, FONT_SLANT_UPRIGHT),
|
||||
1, "", null)),
|
||||
"sans-serif", LocaleList.getEmptyLocaleList(), VARIANT_DEFAULT);
|
||||
FontConfig.FontFamily family = readFamily(xml);
|
||||
assertThat(family).isEqualTo(expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void psName() throws Exception {
|
||||
String xml = "<?xml version='1.0' encoding='UTF-8'?>"
|
||||
+ "<family name='sans-serif'>"
|
||||
+ " <font index='0' postScriptName='foo'>test.ttc</font>"
|
||||
+ " <font index='1'>test.ttc</font>"
|
||||
+ "</family>";
|
||||
FontConfig.FontFamily expected = new FontConfig.FontFamily(
|
||||
Arrays.asList(
|
||||
new FontConfig.Font(new File("test.ttc"), null, "foo",
|
||||
new FontStyle(FONT_WEIGHT_NORMAL, FONT_SLANT_UPRIGHT),
|
||||
0, "", null),
|
||||
new FontConfig.Font(new File("test.ttc"), null, "test",
|
||||
new FontStyle(FONT_WEIGHT_NORMAL, FONT_SLANT_UPRIGHT),
|
||||
1, "", null)),
|
||||
"sans-serif", LocaleList.getEmptyLocaleList(), VARIANT_DEFAULT);
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -56,6 +56,7 @@ public class FontListParser {
|
||||
// XML constants for Font.
|
||||
public static final String ATTR_INDEX = "index";
|
||||
public static final String ATTR_WEIGHT = "weight";
|
||||
public static final String ATTR_POSTSCRIPT_NAME = "postScriptName";
|
||||
public static final String ATTR_STYLE = "style";
|
||||
public static final String ATTR_FALLBACK_FOR = "fallbackFor";
|
||||
public static final String STYLE_ITALIC = "italic";
|
||||
@@ -209,6 +210,7 @@ public class FontListParser {
|
||||
int weight = weightStr == null ? FontStyle.FONT_WEIGHT_NORMAL : Integer.parseInt(weightStr);
|
||||
boolean isItalic = STYLE_ITALIC.equals(parser.getAttributeValue(null, ATTR_STYLE));
|
||||
String fallbackFor = parser.getAttributeValue(null, ATTR_FALLBACK_FOR);
|
||||
String postScriptName = parser.getAttributeValue(null, ATTR_POSTSCRIPT_NAME);
|
||||
StringBuilder filename = new StringBuilder();
|
||||
while (keepReading(parser)) {
|
||||
if (parser.getEventType() == XmlPullParser.TEXT) {
|
||||
@@ -242,8 +244,18 @@ public class FontListParser {
|
||||
axes.toArray(new FontVariationAxis[0]));
|
||||
}
|
||||
|
||||
return new FontConfig.Font(new File(filePath),
|
||||
File file = new File(filePath);
|
||||
|
||||
if (postScriptName == null) {
|
||||
// If post script name was not provided, assume the file name is same to PostScript
|
||||
// name.
|
||||
String name = file.getName();
|
||||
postScriptName = name.substring(0, name.length() - 4);
|
||||
}
|
||||
|
||||
return new FontConfig.Font(file,
|
||||
originalPath == null ? null : new File(originalPath),
|
||||
postScriptName,
|
||||
new FontStyle(
|
||||
weight,
|
||||
isItalic ? FontStyle.FONT_SLANT_ITALIC : FontStyle.FONT_SLANT_UPRIGHT
|
||||
|
||||
@@ -502,8 +502,8 @@ final class UpdatableFontDir {
|
||||
Slog.e(TAG, "Failed to lookup font file that has " + font.getPostScriptName());
|
||||
return null;
|
||||
}
|
||||
resolvedFonts.add(new FontConfig.Font(info.mFile, null, font.getFontStyle(),
|
||||
font.getIndex(), font.getFontVariationSettings(), null));
|
||||
resolvedFonts.add(new FontConfig.Font(info.mFile, null, info.getPostScriptName(),
|
||||
font.getFontStyle(), font.getIndex(), font.getFontVariationSettings(), null));
|
||||
}
|
||||
return new FontConfig.FontFamily(resolvedFonts, fontFamily.getName(),
|
||||
null, FontConfig.FontFamily.VARIANT_DEFAULT);
|
||||
|
||||
@@ -4,6 +4,7 @@ import collections
|
||||
import copy
|
||||
import glob
|
||||
from os import path
|
||||
import re
|
||||
import sys
|
||||
from xml.etree import ElementTree
|
||||
|
||||
@@ -199,8 +200,9 @@ def check_hyphens(hyphens_dir):
|
||||
|
||||
|
||||
class FontRecord(object):
|
||||
def __init__(self, name, scripts, variant, weight, style, fallback_for, font):
|
||||
def __init__(self, name, psName, scripts, variant, weight, style, fallback_for, font):
|
||||
self.name = name
|
||||
self.psName = psName
|
||||
self.scripts = scripts
|
||||
self.variant = variant
|
||||
self.weight = weight
|
||||
@@ -236,6 +238,7 @@ def parse_fonts_xml(fonts_xml_path):
|
||||
assert variant in {None, 'elegant', 'compact'}, (
|
||||
'Unexpected value for variant: %s' % variant)
|
||||
|
||||
trim_re = re.compile(r"^[ \n\r\t]*(.+)[ \n\r\t]*$")
|
||||
for family in families:
|
||||
name = family.get('name')
|
||||
variant = family.get('variant')
|
||||
@@ -251,6 +254,10 @@ def parse_fonts_xml(fonts_xml_path):
|
||||
assert child.tag == 'font', (
|
||||
'Unknown tag <%s>' % child.tag)
|
||||
font_file = child.text.rstrip()
|
||||
|
||||
m = trim_re.match(font_file)
|
||||
font_file = m.group(1)
|
||||
|
||||
weight = int(child.get('weight'))
|
||||
assert weight % 100 == 0, (
|
||||
'Font weight "%d" is not a multiple of 100.' % weight)
|
||||
@@ -270,11 +277,12 @@ def parse_fonts_xml(fonts_xml_path):
|
||||
if index:
|
||||
index = int(index)
|
||||
|
||||
if not path.exists(path.join(_fonts_dir, font_file)):
|
||||
if not path.exists(path.join(_fonts_dir, m.group(1))):
|
||||
continue # Missing font is a valid case. Just ignore the missing font files.
|
||||
|
||||
record = FontRecord(
|
||||
name,
|
||||
child.get('postScriptName'),
|
||||
frozenset(scripts),
|
||||
variant,
|
||||
weight,
|
||||
@@ -664,6 +672,37 @@ def check_cjk_punctuation():
|
||||
break
|
||||
assert_font_supports_none_of_chars(record.font, cjk_punctuation, name)
|
||||
|
||||
def getPostScriptName(font):
|
||||
font_file, index = font
|
||||
font_path = path.join(_fonts_dir, font_file)
|
||||
if index is not None:
|
||||
# Use the first font file in the collection for resolving post script name.
|
||||
ttf = ttLib.TTFont(font_path, fontNumber=0)
|
||||
else:
|
||||
ttf = ttLib.TTFont(font_path)
|
||||
|
||||
nameTable = ttf['name']
|
||||
for name in nameTable.names:
|
||||
if (name.nameID == 6 and name.platformID == 3 and name.platEncID == 1
|
||||
and name.langID == 0x0409):
|
||||
return str(name)
|
||||
|
||||
def check_canonical_name():
|
||||
for record in _all_fonts:
|
||||
file_name, index = record.font
|
||||
|
||||
psName = getPostScriptName(record.font)
|
||||
if record.psName:
|
||||
# If fonts element has postScriptName attribute, it should match with the PostScript
|
||||
# name in the name table.
|
||||
assert psName == record.psName, ('postScriptName attribute %s should match with %s' % (
|
||||
record.psName, psName))
|
||||
else:
|
||||
# If fonts element doesn't have postScriptName attribute, the file name should match
|
||||
# with the PostScript name in the name table.
|
||||
assert psName == file_name[:-4], ('file name %s should match with %s' % (
|
||||
file_name, psName))
|
||||
|
||||
|
||||
def main():
|
||||
global _fonts_dir
|
||||
@@ -682,6 +721,8 @@ def main():
|
||||
|
||||
check_cjk_punctuation()
|
||||
|
||||
check_canonical_name()
|
||||
|
||||
check_emoji = sys.argv[2]
|
||||
if check_emoji == 'true':
|
||||
ucd_path = sys.argv[3]
|
||||
|
||||
Reference in New Issue
Block a user