Skip to content

Commit

Permalink
Uses multiprocessing to speed up
Browse files Browse the repository at this point in the history
  • Loading branch information
catusf committed Nov 15, 2024
1 parent 335f3ec commit 21d0568
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 69 deletions.
4 changes: 2 additions & 2 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
TODO List
- [ ] Move Pinyin first in the name
- [ ] Change the font names to be without '-'
- [X] Move Pinyin first in the name
- [X] Change the font names to be without '-'
- [ ] Make right option
- [ ] Add option for changing the unitsPerEm (canvas size) so we can copy glyphs from original fonts
- [x] Include the built fonts in 'input' folder
Expand Down
93 changes: 26 additions & 67 deletions run_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,11 @@
import json
import os
from fontTools.ttLib import TTFont
from multiprocessing import Pool

OUTPUT_DIR = "output"

OUTPUT_DIR = 'output'

def get_name_encoding(name):
"""
Parameters:
names (NameRecord): Name record from the naming record
Returns:
The cmap codepoint encoding.
If GDI does not support the name, return None.
"""
# From: https://github.com/MicrosoftDocs/typography-issues/issues/956#issuecomment-1205678068
if name.platformID == 3:
if name.platEncID == 3:
return "cp936"
Expand All @@ -38,78 +30,65 @@ def get_name_encoding(name):

return None


def get_decoded_name(name) -> str:
"""
Parameters:
names (NameRecord): Name record from the naming record
Returns:
The decoded name
"""

encoding = get_name_encoding(name)

if name.platformID == 3 and encoding != "utf_16_be":
# Compatibility for really old font
name_to_decode = name.string.replace(b"\x00", b"")
else:
name_to_decode = name.string

return name_to_decode.decode(encoding)


def cleanup_font_name(input_file, output_file):
"""Remove the - character in font name"""

# Load the font file
font = TTFont(input_file)

fontFamilyName = font["name"].getDebugName(1)
fontFamilyName = font['name'].getDebugName(1)
newfontFamilyName = fontFamilyName.replace("-", " ")
name_table = font['name']

# Access the 'name' table
name_table = font["name"]

# Iterate through the name table to find the font name entry (name_id = 4)
for record in name_table.names:
encoding = get_name_encoding(record)

decoded_name = record.string.decode(encoding)
# print(f"{decoded_name=}")

if fontFamilyName == decoded_name: # Font Name
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):
# Generate the font name

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}"

if save_config:
node_cmd += " --save_config"

# Define the commands
commands = [
"rm -f build/svg/*",
node_cmd,
]

# Run the commands
for command in commands:
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}")
run_build_commands(data, config, save_config)

def main(save_config=False, small_data=False):
json_config = config.replace(".js", ".json")
with open(json_config) as f:
parameters = json.load(f)

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.json"]
data_options = ["src/data-small-org.json"]
else:
data_options = ["src/data.json"]

Expand All @@ -129,38 +108,18 @@ def main(save_config=False, small_data=False):
"src/config/tigris.top.js",
]

# Generate all combinations of the parameters
combinations = itertools.product(data_options, config_options)

# Loop through each combination and run the commands
for data, config in combinations:
print(f"Processing combination: data={data}, config={config}")
run_build_commands(data, config, save_config)

json_config = config.replace(".js", ".json")

with open(json_config, "r", encoding="utf-8") as f:
parameters = json.load(f)

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, fontpath) # Overwrites


import argparse
# Create a pool of workers
with Pool() as pool:
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 21d0568

Please sign in to comment.