Skip to content

Commit

Permalink
Add Builder.build_and_save
Browse files Browse the repository at this point in the history
* Add a test for saving to the same file.
  • Loading branch information
kojiishi committed Aug 8, 2021
1 parent 7e41680 commit 62d8911
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 22 deletions.
30 changes: 17 additions & 13 deletions east_asian_spacing/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,26 +30,31 @@ def __init__(self, font, config=Config.default):
def has_spacings(self):
return len(self._spacings) > 0

async def build_and_save(self, output=None, **kwargs):
await self.build()
if not self.has_spacings:
return None
return self.save(output, **kwargs)

def save(self,
output_path=None,
output=None,
stem_suffix=None,
glyph_out=None,
print_path=False):
assert self.has_spacings
font = self.font
path_before_save = font.path
output_path = self.calc_output_path(path_before_save, output_path,
stem_suffix)
logger.info('Saving to "%s"', output_path)
font.save(output_path)
paths = [output_path, path_before_save]
output = self.calc_output_path(path_before_save, output, stem_suffix)
logger.info('Saving to "%s"', output)
font.save(output)
paths = [output, path_before_save]
if glyph_out:
glyphs_path = self.save_glyphs(glyph_out)
paths.append(glyphs_path)
if print_path:
print('\t'.join(str(path) for path in paths),
flush=True) # Flush, for better parallelism when piping.
return output_path
return output

@staticmethod
def calc_output_path(input_path, output_path, stem_suffix=None):
Expand Down Expand Up @@ -281,14 +286,13 @@ async def main():
config = config.with_fullwidth_advance(args.em)

builder = Builder(font, config)
await builder.build()
if not builder.has_spacings:
output = await builder.build_and_save(args.output,
stem_suffix=args.suffix,
glyph_out=args.glyph_out,
print_path=args.print_path)
if not output:
logger.info('Skipped saving due to no changes: "%s"', input)
continue
builder.save(args.output,
stem_suffix=args.suffix,
glyph_out=args.glyph_out,
print_path=args.print_path)
if args.test:
await builder.test(smoke=(args.test == 1))

Expand Down
16 changes: 9 additions & 7 deletions east_asian_spacing/font.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ def vertical_font(self):

def save(self, out_path=None):
if not out_path:
out_path = pathlib.Path("out" + self.path.suffix)
out_path = self.path
elif isinstance(out_path, str):
out_path = pathlib.Path(out_path)
logger.info("Saving to: \"%s\"", out_path)
Expand All @@ -154,15 +154,17 @@ def save(self, out_path=None):
self.ttfont.save(str(out_path))
self._set_path(out_path)

size_before = self.path.stat().st_size
size_after = out_path.stat().st_size
logger.info("File sizes: %d -> %d Delta: %d", size_before, size_after,
size_after - size_before)
if logger.isEnabledFor(logging.INFO):
size_before = self.path.stat().st_size
size_after = out_path.stat().st_size
logger.info("File sizes: %d -> %d Delta: %d", size_before,
size_after, size_after - size_before)

@staticmethod
def _before_save(ttfont):
# `TTFont.save()` compiles all loaded tables. Unload tables we know we did
# not modify, so that it copies instead of re-compile.
# `TTFont.save()` compiles all loaded tables. Unload tables we know we
# did not modify, so that it copies instead of re-compile.
# This speeds up saving significantly for large fonts.
loaded_keys = ttfont.tables.keys()
logger.debug("loaded_keys=%s", loaded_keys)
keys_to_save = set(('head', 'GPOS'))
Expand Down
19 changes: 19 additions & 0 deletions tests/builder_test.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import io
import os
import pathlib
import pytest
import shutil
import tempfile

from east_asian_spacing import Builder
from east_asian_spacing import EastAsianSpacing
from east_asian_spacing import Font


def test_calc_output_path(data_dir):
Expand Down Expand Up @@ -50,3 +54,18 @@ def call(items):
assert call(['-']) == ['line1', 'line2']
monkeypatch.setattr('sys.stdin', io.StringIO('line1\nline2\n'))
assert call(['a', '-', 'b']) == ['a', 'line1', 'line2', 'b']


@pytest.mark.asyncio
async def test_save_to_same_file(test_font_path, tmp_path):
tmp_font_path = tmp_path / test_font_path.name
shutil.copy(test_font_path, tmp_font_path)
font = Font.load(tmp_font_path)
assert not EastAsianSpacing.font_has_feature(font)

builder = Builder(font)
out_path = await builder.build_and_save()
assert out_path == tmp_font_path

font = Font.load(out_path)
assert EastAsianSpacing.font_has_feature(font)
4 changes: 2 additions & 2 deletions tests/full_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ async def test_build_and_diff(test_font_path, refs_dir, tmp_path):
and if there were any differences, update the reference files.
"""
builder = Builder(test_font_path)
await builder.build()
out_path = builder.save(tmp_path)
out_path = await builder.build_and_save(tmp_path)
assert out_path

await builder.test()

Expand Down

0 comments on commit 62d8911

Please sign in to comment.