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

WIP: allow nanoemoji to take --glyphmap_file #454

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
4 changes: 3 additions & 1 deletion src/nanoemoji/glyphmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,7 @@ def load_from(file) -> Tuple[GlyphMapping]:


def parse_csv(filename) -> Tuple[GlyphMapping]:
with open(filename) as f:
# 'utf-8-sig' is meant to skip MS Notepad's BOM which sometimes gets added
# https://docs.python.org/3/library/codecs.html#encodings-and-unicode
with open(filename, encoding="utf-8-sig") as f:
return load_from(f)
17 changes: 7 additions & 10 deletions src/nanoemoji/nanoemoji.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,18 +122,14 @@ def _glyphmap_file(font_config: FontConfig, master: MasterConfig) -> Path:
return _per_config_file(font_config, master_part + ".glyphmap")


def _glyphmap_file(font_config: FontConfig, master: MasterConfig) -> Path:
master_part = ""
if font_config.is_vf:
master_part = "." + master.output_ufo
return _per_config_file(font_config, master_part + ".glyphmap")


def write_glyphmap_rule(nw, glyphmap_generator):
def write_glyphmap_rule(nw, glyphmap_generator, glyphmap_file=None):
options = "--output_file $out @$out.rsp"
if glyphmap_file is not None:
options += f" --input_csv {rel_build(glyphmap_file)}"
module_rule(
nw,
glyphmap_generator,
f"--output_file $out @$out.rsp",
options,
rspfile="$out.rsp",
rspfile_content="$in",
allow_external=True,
Expand Down Expand Up @@ -642,14 +638,15 @@ def _run(argv):

if gen_ninja():
logging.info(f"Generating {build_file.relative_to(build_dir())}")
glyphmap_file = FLAGS.glyphmap_file
with open(build_file, "w") as f:
nw = NinjaWriter(f)
write_preamble(nw)

for glyphmap_generator in sorted(
{fc.glyphmap_generator for fc in font_configs}
):
write_glyphmap_rule(nw, glyphmap_generator)
write_glyphmap_rule(nw, glyphmap_generator, glyphmap_file)

# After rules, builds

Expand Down
41 changes: 35 additions & 6 deletions src/nanoemoji/write_glyphmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,15 @@
from absl import app
from absl import flags
from nanoemoji.glyph import glyph_name
from nanoemoji.glyphmap import GlyphMapping
from nanoemoji.glyphmap import GlyphMapping, parse_csv
from nanoemoji import codepoints
from nanoemoji import util
from pathlib import Path
from typing import Iterator, Sequence, Tuple
from typing import Iterator, Optional, Sequence, Tuple

FLAGS = flags.FLAGS

flags.DEFINE_string("input_csv", None, "Optional CSV file to read glyph mappings from")
flags.DEFINE_string("output_file", "-", "Output filename ('-' means stdout)")


Expand All @@ -46,23 +47,51 @@ class InputFileSuffix(enum.Enum):
PNG = ".png"


def _glyphmappings(input_files: Sequence[str]) -> Iterator[GlyphMapping]:
def _glyphmappings(
input_csv: Optional[Tuple[GlyphMapping]], input_files: Sequence[str]
) -> Iterator[GlyphMapping]:
# group .svg and/or .png files with the same filename stem
sources_by_stem = {}
suffix_index = {InputFileSuffix.SVG: 0, InputFileSuffix.PNG: 1}
for filename in input_files:
input_file = Path(filename)
i = suffix_index[InputFileSuffix(input_file.suffix)]
sources_by_stem.setdefault(input_file.stem, [None, None])[i] = input_file
input_mappings = {}
if input_csv:
for m in input_csv:
stem = None
if m.svg_file:
stem = m.svg_file.stem
elif m.bitmap_file:
stem = m.bitmap_file.stem
else:
raise ValueError(f"GlyphMapping {m} has no input files")
input_mappings[stem] = m
for source_stem, files in sources_by_stem.items():
cps = tuple(codepoints.from_filename(source_stem))
yield GlyphMapping(*files, cps, glyph_name(cps))
if source_stem in input_mappings:
m = input_mappings[source_stem]
cps = m.codepoints
name = m.glyph_name
if not m.glyph_name:
if len(cps) < 1:
raise ValueError(
f"GlyphMapping {m} has neither glyph name nor codepoints"
)
name = glyph_name(cps)
yield GlyphMapping(*files, cps, name)
else:
cps = tuple(codepoints.from_filename(source_stem))
yield GlyphMapping(*files, cps, glyph_name(cps))


def main(argv):
input_csv = None
if FLAGS.input_csv:
input_csv = parse_csv(Path(FLAGS.input_csv))
input_files = util.expand_ninja_response_files(argv[1:])
with util.file_printer(FLAGS.output_file) as print:
for gm in _glyphmappings(input_files):
for gm in _glyphmappings(input_csv, input_files):
# filename(s), glyph_name, codepoint(s)
print(gm.csv_line())

Expand Down