Skip to content

Commit

Permalink
Starting to want COLR VFs.
Browse files Browse the repository at this point in the history
  • Loading branch information
rsheeter committed Nov 24, 2020
1 parent 9061a59 commit 4a7c8a6
Show file tree
Hide file tree
Showing 12 changed files with 359 additions and 151 deletions.
4 changes: 2 additions & 2 deletions src/nanoemoji/color_glyph.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
"ignore_reuse_error",
True,
"Whether to fail or continue with a warning when picosvg cannot compute "
"affine between paths that normalize the same."
"affine between paths that normalize the same.",
)


Expand Down Expand Up @@ -233,7 +233,7 @@ def _common_gradient_parts(el, shape_opacity=1.0):
class PaintedLayer(NamedTuple):
paint: Paint
path: str # path.d
reuses: Tuple[Affine2D] = ()
reuses: Tuple[Affine2D, ...] = ()

def shape_cache_key(self):
# a hashable cache key ignoring paint
Expand Down
107 changes: 97 additions & 10 deletions src/nanoemoji/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,19 @@
FLAGS = flags.FLAGS


flags.DEFINE_string("config", None, "Config file")
flags.DEFINE_string(
"config", str(Path(__file__).parent / "default.toml"), "Config file"
)


_DEFAULT_FAMILY = "An Emoji Family"
_DEFAULT_REUSE_TOLERANCE = 0.1
_DEFAULT_COLOR_FORMAT = "glyf_colr_1"
_DEFAULT_UPEM = 1024
_DEFAULT_KEEP_GLYPH_NAMES = False
_DEFAULT_OUTPUT = "font"
_DEFAULT_FEA_FILE = "features.fea"
_DEFAULT_CODEPOINT_FILE = "codepointmap.csv"


class Axis(NamedTuple):
Expand All @@ -44,25 +56,76 @@ class MasterConfig(NamedTuple):


class FontConfig(NamedTuple):
family: str
output_file: str
color_format: str
upem: int
reuse_tolerance: float
keep_glyph_names: bool
output: str
fea_file: str
codepointmap_file: str
axes: Tuple[Axis]
masters: Tuple[MasterConfig]
source_names: Tuple[str]


def _consume(config, key):
return config.pop(key)


def load(config_file: Path = None) -> FontConfig:
@property
def output_format(self):
return Path(self.output_file).suffix


def _consume(config, key, default=None):
if default is None or key in config:
return config.pop(key)
return default


def write(dest: Path, config: FontConfig):
toml_cfg = {
"family": config.family,
"output_file": config.output_file,
"color_format": config.color_format,
"upem": config.upem,
"reuse_tolerance": config.reuse_tolerance,
"keep_glyph_names": config.keep_glyph_names,
"output": config.output,
"axis": {
a.axisTag: {
"name": a.name,
"default": a.default,
}
for a in config.axes
},
"master": {
m.name: {
"style_name": m.style_name,
"position": {p.axisTag: p.position for p in m.position},
"srcs": [str(p) for p in m.sources],
}
for m in config.masters
},
}
dest.write_text(toml.dumps(toml_cfg))


def load(config_file: Path = None, additional_srcs: Tuple[Path] = None) -> FontConfig:
if config_file is None:
config_file = Path(FLAGS.config).resolve()

config = toml.load(config_file)
config_dir = config_file.parent

family = _consume(config, "family", default=_DEFAULT_FAMILY)
output_file = _consume(config, "output_file")
color_format = _consume(config, "color_format")
color_format = _consume(config, "color_format", default=_DEFAULT_COLOR_FORMAT)
upem = int(_consume(config, "upem", default=_DEFAULT_UPEM))
reuse_tolerance = float(
_consume(config, "reuse_tolerance", default=_DEFAULT_REUSE_TOLERANCE)
)
keep_glyph_names = _consume(
config, "keep_glyph_names", default=_DEFAULT_KEEP_GLYPH_NAMES
)
output = _consume(config, "output", default=_DEFAULT_OUTPUT)

axes = []
for axis_tag, axis_config in _consume(config, "axis").items():
Expand All @@ -85,12 +148,29 @@ def load(config_file: Path = None) -> FontConfig:
for k, v in _consume(master_config, "position").items()
)
)
srcs = set()
if "srcs" in master_config:
for src in _consume(master_config, "srcs"):
if Path(src).is_file():
srcs.add(Path(src))
else:
srcs |= set(config_dir.glob(src))
if additional_srcs is not None:
srcs |= set(additional_srcs)
srcs = tuple(sorted(srcs))

master = MasterConfig(
master_name,
_consume(master_config, "style_name"),
".".join((Path(output_file).stem, master_name, "ufo",)),
".".join(
(
Path(output_file).stem,
master_name,
"ufo",
)
),
positions,
tuple(config_dir.glob(_consume(master_config, "srcs"))),
srcs,
)
if master_config:
raise ValueError(f"Unexpected '{master_name}' config: {master_config}")
Expand All @@ -111,8 +191,15 @@ def load(config_file: Path = None) -> FontConfig:
raise ValueError(f"Unexpected config: {config}")

return FontConfig(
family,
output_file,
color_format,
upem,
reuse_tolerance,
keep_glyph_names,
output,
_DEFAULT_FEA_FILE,
_DEFAULT_CODEPOINT_FILE,
tuple(axes),
tuple(masters),
tuple(sorted(source_names)),
Expand Down
12 changes: 12 additions & 0 deletions src/nanoemoji/default.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
output_file = "Font.ttf"
color_format = "glyf_colr_1"

[axis.wght]
name = "Weight"
default = 400

[master.regular]
style_name = "Regular"

[master.regular.position]
wght = 400
Loading

0 comments on commit 4a7c8a6

Please sign in to comment.