summaryrefslogtreecommitdiff
path: root/tools
diff options
context:
space:
mode:
authorTreeHugger Robot <treehugger-gerrit@google.com>2021-04-17 22:34:51 +0000
committerAndroid (Google) Code Review <android-gerrit@google.com>2021-04-17 22:34:51 +0000
commit0566ee964d904996d43a16a278cf0614a67b63f7 (patch)
tree8128ccf4b23bc16e47c1f03790d5dfc8c283e509 /tools
parent6c98925999a89d98066120a29483111a97c27e95 (diff)
parent5681124abbf3d41a360df0057e58125bf9a754ea (diff)
downloadbase-0566ee964d904996d43a16a278cf0614a67b63f7.tar.gz
Merge "Add FontConfig.Font.getPostScriptName API" into sc-dev
Diffstat (limited to 'tools')
-rwxr-xr-xtools/fonts/fontchain_linter.py45
1 files changed, 43 insertions, 2 deletions
diff --git a/tools/fonts/fontchain_linter.py b/tools/fonts/fontchain_linter.py
index f0b759547a93..1d308df00ef7 100755
--- a/tools/fonts/fontchain_linter.py
+++ b/tools/fonts/fontchain_linter.py
@@ -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]