Skip to content

Commit

Permalink
Fixes small data
Browse files Browse the repository at this point in the history
  • Loading branch information
catusf committed Nov 15, 2024
1 parent 21d0568 commit 6398a21
Showing 1 changed file with 27 additions and 9 deletions.
36 changes: 27 additions & 9 deletions run_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
from fontTools.ttLib import TTFont
from multiprocessing import Pool

OUTPUT_DIR = 'output'
OUTPUT_DIR = "output"


def get_name_encoding(name):
if name.platformID == 3:
Expand All @@ -30,6 +31,7 @@ def get_name_encoding(name):

return None


def get_decoded_name(name) -> str:
encoding = get_name_encoding(name)

Expand All @@ -40,12 +42,13 @@ def get_decoded_name(name) -> str:

return name_to_decode.decode(encoding)


def cleanup_font_name(input_file, output_file):
"""Remove the - character in font name"""
font = TTFont(input_file)
fontFamilyName = font['name'].getDebugName(1)
fontFamilyName = font["name"].getDebugName(1)
newfontFamilyName = fontFamilyName.replace("-", " ")
name_table = font['name']
name_table = font["name"]

for record in name_table.names:
encoding = get_name_encoding(record)
Expand All @@ -55,7 +58,10 @@ def cleanup_font_name(input_file, output_file):
record.string = newfontFamilyName.encode(encoding)

font.save(output_file)
print(f"Font has been updated and saved as '{newfontFamilyName}' in '{output_file}'.")
print(
f"Font has been updated and saved as '{newfontFamilyName}' in '{output_file}'."
)


def run_build_commands(data, config, save_config):
node_cmd = f"time node --max_old_space_size=8192 --optimize_for_size --stack_size=4096 --require babel-core/register ./index.js --config={config} --data={data}"
Expand All @@ -72,6 +78,7 @@ def run_build_commands(data, config, save_config):
print(f"Running: {command}")
subprocess.run(command, shell=True, check=True, executable="/bin/bash")


def process_combination(args):
data, config, save_config = args
print(f"Processing combination: data={data}, config={config}")
Expand All @@ -81,14 +88,15 @@ def process_combination(args):
with open(json_config) as f:
parameters = json.load(f)

fontname = parameters['fontName']
fontname = parameters["fontName"]
fontpath = os.path.join(OUTPUT_DIR, fontname + ".ttf")
new_fontpath = os.path.join(OUTPUT_DIR, fontname + "-new.ttf")
cleanup_font_name(fontpath, new_fontpath)


def main(save_config=False, small_data=False):
if small_data:
data_options = ["src/data-small-org.json"]
data_options = ["src/data-small.json"]
else:
data_options = ["src/data.json"]

Expand All @@ -112,14 +120,24 @@ def main(save_config=False, small_data=False):

# Create a pool of workers
with Pool() as pool:
pool.map(process_combination, [(data, config, save_config) for data, config in combinations])
pool.map(
process_combination,
[(data, config, save_config) for data, config in combinations],
)


if __name__ == "__main__":
import argparse

parser = argparse.ArgumentParser(description="Build fonts for all available config")
parser.add_argument('--save_config', action='store_true', help="Save the configuration then quit (optional)")
parser.add_argument('--small_data', action='store_true', help="Use small data (optional)")
parser.add_argument(
"--save_config",
action="store_true",
help="Save the configuration then quit (optional)",
)
parser.add_argument(
"--small_data", action="store_true", help="Use small data (optional)"
)

args = parser.parse_args()

Expand Down

0 comments on commit 6398a21

Please sign in to comment.