2020-02-19 12:26:55 +01:00
|
|
|
#!/usr/bin/env python3
|
2018-01-26 22:57:48 +01:00
|
|
|
|
|
|
|
import os
|
|
|
|
|
|
|
|
# A single test in a subset test suite. Identifies a font
|
|
|
|
# a subsetting profile, and a subset to be cut.
|
|
|
|
class Test:
|
2023-02-22 21:23:34 +01:00
|
|
|
def __init__(self, font_path, profile_path, subset, instance, options):
|
2018-01-26 22:57:48 +01:00
|
|
|
self.font_path = font_path
|
|
|
|
self.profile_path = profile_path
|
|
|
|
self.subset = subset
|
2022-07-01 01:22:15 +02:00
|
|
|
self.instance = instance
|
2023-02-22 21:23:34 +01:00
|
|
|
self.options = options
|
2018-01-26 22:57:48 +01:00
|
|
|
|
|
|
|
def unicodes(self):
|
2021-05-11 20:44:32 +02:00
|
|
|
import re
|
2019-06-26 22:23:24 +02:00
|
|
|
if self.subset == '*':
|
|
|
|
return self.subset[0]
|
2023-04-14 22:44:15 +02:00
|
|
|
elif self.subset == "no-unicodes":
|
|
|
|
return ""
|
2021-05-11 20:44:32 +02:00
|
|
|
elif re.match("^U\+", self.subset):
|
|
|
|
s = re.sub (r"U\+", "", self.subset)
|
|
|
|
return s
|
2019-06-26 22:23:24 +02:00
|
|
|
else:
|
|
|
|
return ",".join("%X" % ord(c) for (i, c) in enumerate(self.subset))
|
2018-01-26 22:57:48 +01:00
|
|
|
|
2022-07-01 01:22:15 +02:00
|
|
|
def instance_name(self):
|
|
|
|
if not self.instance:
|
|
|
|
return self.instance
|
|
|
|
else:
|
|
|
|
s = "." + self.instance.replace(':', '-')
|
|
|
|
return s
|
|
|
|
|
2018-02-27 22:15:40 +01:00
|
|
|
def get_profile_flags(self):
|
2020-05-28 22:12:32 +02:00
|
|
|
with open (self.profile_path, mode="r", encoding="utf-8") as f:
|
|
|
|
return f.read().splitlines()
|
2018-02-27 22:15:40 +01:00
|
|
|
|
2022-07-01 01:22:15 +02:00
|
|
|
def get_instance_flags(self):
|
|
|
|
if not self.instance:
|
|
|
|
return []
|
|
|
|
else:
|
|
|
|
return self.instance.split(',')
|
|
|
|
|
2021-08-04 00:07:23 +02:00
|
|
|
def get_font_name(self):
|
2018-01-26 22:57:48 +01:00
|
|
|
font_base_name = os.path.basename(self.font_path)
|
|
|
|
font_base_name_parts = os.path.splitext(font_base_name)
|
|
|
|
profile_name = os.path.splitext(os.path.basename(self.profile_path))[0]
|
|
|
|
|
2019-06-26 22:23:24 +02:00
|
|
|
if self.unicodes() == "*":
|
2022-07-01 01:22:15 +02:00
|
|
|
return "%s.%s.retain-all-codepoint%s%s" % (font_base_name_parts[0],
|
2021-08-04 00:07:23 +02:00
|
|
|
profile_name,
|
2022-07-01 01:22:15 +02:00
|
|
|
self.instance_name(),
|
2021-08-04 00:07:23 +02:00
|
|
|
font_base_name_parts[1])
|
2023-04-14 22:44:15 +02:00
|
|
|
elif self.unicodes() == "":
|
|
|
|
return "%s.%s.no-unicodes%s%s" % (font_base_name_parts[0],
|
|
|
|
profile_name,
|
|
|
|
self.instance_name(),
|
|
|
|
font_base_name_parts[1])
|
2019-06-26 22:23:24 +02:00
|
|
|
else:
|
2022-07-01 01:22:15 +02:00
|
|
|
return "%s.%s.%s%s%s" % (font_base_name_parts[0],
|
2018-02-27 22:15:40 +01:00
|
|
|
profile_name,
|
2021-08-04 00:07:23 +02:00
|
|
|
self.unicodes(),
|
2022-07-01 01:22:15 +02:00
|
|
|
self.instance_name(),
|
2021-08-04 00:07:23 +02:00
|
|
|
font_base_name_parts[1])
|
2018-01-26 22:57:48 +01:00
|
|
|
|
2018-09-19 00:53:37 +02:00
|
|
|
def get_font_extension(self):
|
|
|
|
font_base_name = os.path.basename(self.font_path)
|
|
|
|
font_base_name_parts = os.path.splitext(font_base_name)
|
|
|
|
return font_base_name_parts[1]
|
|
|
|
|
2018-01-26 22:57:48 +01:00
|
|
|
# A group of tests to perform on the subsetter. Each test
|
|
|
|
# Identifies a font a subsetting profile, and a subset to be cut.
|
|
|
|
class SubsetTestSuite:
|
|
|
|
|
|
|
|
def __init__(self, test_path, definition):
|
|
|
|
self.test_path = test_path
|
2019-06-26 22:23:24 +02:00
|
|
|
self.fonts = []
|
|
|
|
self.profiles = []
|
|
|
|
self.subsets = []
|
2022-07-01 01:22:15 +02:00
|
|
|
self.instances = []
|
2023-02-22 21:23:34 +01:00
|
|
|
self.options = []
|
2018-01-26 22:57:48 +01:00
|
|
|
self._parse(definition)
|
|
|
|
|
|
|
|
def get_output_directory(self):
|
|
|
|
test_name = os.path.splitext(os.path.basename(self.test_path))[0]
|
|
|
|
data_dir = os.path.join(os.path.dirname(self.test_path), "..")
|
|
|
|
|
|
|
|
output_dir = os.path.normpath(os.path.join(data_dir, "expected", test_name))
|
|
|
|
if not os.path.exists(output_dir):
|
|
|
|
os.mkdir(output_dir)
|
|
|
|
if not os.path.isdir(output_dir):
|
2018-12-31 04:30:43 +01:00
|
|
|
raise Exception("%s is not a directory." % output_dir)
|
2018-01-26 22:57:48 +01:00
|
|
|
|
|
|
|
return output_dir
|
|
|
|
|
|
|
|
def tests(self):
|
|
|
|
for font in self.fonts:
|
|
|
|
font = os.path.join(self._base_path(), "fonts", font)
|
|
|
|
for profile in self.profiles:
|
|
|
|
profile = os.path.join(self._base_path(), "profiles", profile)
|
|
|
|
for subset in self.subsets:
|
2023-02-22 21:23:34 +01:00
|
|
|
if self.instances:
|
2022-07-01 01:22:15 +02:00
|
|
|
for instance in self.instances:
|
2023-02-22 21:23:34 +01:00
|
|
|
yield Test(font, profile, subset, instance, options=self.options)
|
2022-07-01 01:22:15 +02:00
|
|
|
else:
|
2023-02-22 21:23:34 +01:00
|
|
|
yield Test(font, profile, subset, "", options=self.options)
|
2018-01-26 22:57:48 +01:00
|
|
|
|
|
|
|
def _base_path(self):
|
|
|
|
return os.path.dirname(os.path.dirname(self.test_path))
|
|
|
|
|
|
|
|
def _parse(self, definition):
|
|
|
|
destinations = {
|
|
|
|
"FONTS:": self.fonts,
|
|
|
|
"PROFILES:": self.profiles,
|
2022-07-01 01:22:15 +02:00
|
|
|
"SUBSETS:": self.subsets,
|
2023-02-22 21:23:34 +01:00
|
|
|
"INSTANCES:": self.instances,
|
|
|
|
"OPTIONS:": self.options,
|
2018-01-26 22:57:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
current_destination = None
|
|
|
|
for line in definition.splitlines():
|
|
|
|
line = line.strip()
|
|
|
|
|
|
|
|
if line.startswith("#"):
|
|
|
|
continue
|
|
|
|
|
|
|
|
if not line:
|
|
|
|
continue
|
|
|
|
|
|
|
|
if line in destinations:
|
|
|
|
current_destination = destinations[line]
|
|
|
|
elif current_destination is not None:
|
2019-06-26 22:23:24 +02:00
|
|
|
current_destination.append(line)
|
2018-01-26 22:57:48 +01:00
|
|
|
else:
|
|
|
|
raise Exception("Failed to parse test suite file.")
|