Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[makeinstancesufo.py] Adding multiprocessing to run #1293

Merged
merged 2 commits into from
Feb 3, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 37 additions & 16 deletions python/afdko/makeinstancesufo.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import os
import shutil
import sys
import multiprocessing
from itertools import repeat

from fontTools.designspaceLib import (
DesignSpaceDocument,
Expand Down Expand Up @@ -281,6 +283,15 @@ def collect_features_content(instances, inst_idx_lst):
return fea_dict


def starmap_kwargs(pool, fun, args, kwargs):
starmap_args = zip(repeat(fun), args, kwargs)
return pool.starmap(fun_args_and_kwargs, starmap_args)


def fun_args_and_kwargs(fun, args, kwargs):
return fun(*args, **kwargs)


def run(options):

ds_doc = DesignSpaceDocument.fromfile(options.dsPath)
Expand Down Expand Up @@ -327,35 +338,45 @@ def run(options):

logger.info("Built %s instances." % newInstancesCount)
# Remove glyph.lib and font.lib (except for "public.glyphOrder")
for instancePath in newInstancesList:
postProcessInstance(instancePath, options)
pool = multiprocessing.Pool(os.cpu_count() - 1)
pool.starmap(postProcessInstance, [(instancePath, options)
for instancePath in newInstancesList])

if options.doNormalize:
logger.info("Applying UFO normalization...")
for instancePath in newInstancesList:
normalizeUFO(instancePath, outputPath=None, onlyModified=True,
writeModTimes=False)
args = zip(newInstancesList)
kwargs = repeat(
{'outputPath': None,
'onlyModified': True,
'writeModTimes': False},
newInstancesCount)
starmap_kwargs(pool, normalizeUFO, args, kwargs)

if options.doAutoHint or options.doOverlapRemoval:
logger.info("Applying post-processing...")
# Apply autohint and checkoutlines, if requested.
for instancePath in newInstancesList:
# make new instance font.
updateInstance(instancePath, options)
# make new instance font.
pool.starmap(updateInstance, [(instancePath, options)
for instancePath in newInstancesList])

# checkoutlinesufo does ufotools.validateLayers()
if not options.doOverlapRemoval:
for instancePath in newInstancesList:
# make sure that there are no old glyphs left in the
# processed glyphs folder
validateLayers(instancePath)
# make sure that there are no old glyphs left in the
# processed glyphs folder
pool.map(validateLayers, newInstancesList)

# The defcon library renames glyphs. Need to fix them again
if options.doOverlapRemoval or options.doAutoHint:
for instancePath in newInstancesList:
if options.doNormalize:
normalizeUFO(instancePath, outputPath=None, onlyModified=False,
writeModTimes=False)
if options.doNormalize:
args = zip(newInstancesList)
kwargs = repeat(
{'outputPath': None,
'onlyModified': False,
'writeModTimes': False},
newInstancesCount)
starmap_kwargs(pool, normalizeUFO, args, kwargs)
pool.close()
pool.join()

# Restore the contents of the instances' 'features.fea' files
for fea_pth, fea_cntnts in features_store.items():
Expand Down