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

Added standard_viewbox_width property to config file to support fixed and zero/proportional width #460

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
5 changes: 4 additions & 1 deletion src/nanoemoji/color_glyph.py
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,10 @@ def _advance_width(view_box: Rect, config: FontConfig) -> int:
# Scale advance width proportionally to viewbox aspect ratio.
# Use the default advance width if it's larger than the proportional one.
font_height = config.ascender - config.descender # descender <= 0
return max(config.width, round(font_height * view_box.w / view_box.h))

# If the viewbox width is lower than standard viewbox width(32), then use the zero advance width to get proportional scaling.
width = 0 if view_box.w < config.standard_viewbox_width else config.width
return max(width, round(font_height * view_box.w / view_box.h))


def _mutating_traverse(paint, mutator):
Expand Down
6 changes: 6 additions & 0 deletions src/nanoemoji/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
# CLI flags override config file (which overrides default FontConfig).
flags.DEFINE_integer("upem", None, "Units per em.")
flags.DEFINE_integer("width", None, "Width.")
flags.DEFINE_integer("standard_viewbox_width", None, "viewBox width of the majority input SVGs.")
flags.DEFINE_integer("ascender", None, "Ascender")
flags.DEFINE_integer("descender", None, "Descender.")
flags.DEFINE_integer("linegap", None, "Line gap.")
Expand Down Expand Up @@ -152,6 +153,7 @@ class FontConfig(NamedTuple):
# metrics default based on Noto Emoji
upem: int = 1024
width: int = 1275
standard_viewbox_width: int = 32
ascender: int = 950
descender: int = -250
linegap: int = 0
Expand Down Expand Up @@ -214,6 +216,7 @@ def validate(self):
for attr_name in (
"upem",
"width",
"standard_viewbox_width",
"ascender",
"linegap",
"version_major",
Expand Down Expand Up @@ -268,6 +271,7 @@ def write(dest: Path, config: FontConfig):
"color_format": config.color_format,
"upem": config.upem,
"width": config.width,
"standard_viewbox_width": config.standard_viewbox_width,
"ascender": config.ascender,
"descender": config.descender,
"linegap": config.linegap,
Expand Down Expand Up @@ -356,6 +360,7 @@ def load(
color_format = _pop_flag(config, "color_format")
upem = int(_pop_flag(config, "upem"))
width = int(_pop_flag(config, "width"))
standard_viewbox_width = int(_pop_flag(config, "standard_viewbox_width"))
ascender = int(_pop_flag(config, "ascender"))
descender = int(_pop_flag(config, "descender"))
linegap = int(_pop_flag(config, "linegap"))
Expand Down Expand Up @@ -441,6 +446,7 @@ def load(
color_format=color_format,
upem=upem,
width=width,
standard_viewbox_width=standard_viewbox_width,
ascender=ascender,
descender=descender,
linegap=linegap,
Expand Down