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

[colr_to_svg] drop unsupported PaintComposites with a warning #411

Merged
merged 1 commit into from
Apr 27, 2022
Merged
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
37 changes: 23 additions & 14 deletions src/nanoemoji/colr_to_svg.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from absl import logging
from nanoemoji import colors
from nanoemoji import color_glyph
from nanoemoji.glyph_reuse import GlyphReuseCache
Expand Down Expand Up @@ -265,21 +266,29 @@ def descend(parent: etree.Element, paint: otTables.Paint):
]:
descend(parent_el, child_paint)

elif ot_paint.Format == PaintComposite.format and (
ot_paint.CompositeMode == CompositeMode.SRC_IN
and ot_paint.BackdropPaint.Format == PaintSolid.format
):
# Only simple group opacity for now
color = _color(
ttfont,
ot_paint.BackdropPaint.PaletteIndex,
ot_paint.BackdropPaint.Alpha,
elif ot_paint.Format == PaintComposite.format:
if (
ot_paint.CompositeMode == CompositeMode.SRC_IN
and ot_paint.BackdropPaint.Format == PaintSolid.format
):
# Special-case simple PaintComposite for group opacity
color = _color(
ttfont,
ot_paint.BackdropPaint.PaletteIndex,
ot_paint.BackdropPaint.Alpha,
)
if color[:3] == (0, 0, 0):
g = etree.SubElement(parent_el, "g")
g.attrib["opacity"] = ntos(color.alpha)
descend(g, ot_paint.SourcePaint)
return

# https://github.com/googlefonts/nanoemoji/issues/409
logging.warning(
"PaintComposite => SVG not supported at the moment; "
"only BackdropPaint is kept."
)
if color[:3] != (0, 0, 0):
raise NotImplementedError(color)
g = etree.SubElement(parent_el, "g")
g.attrib["opacity"] = ntos(color.alpha)
descend(g, ot_paint.SourcePaint)
descend(parent_el, ot_paint.BackdropPaint)

elif ot_paint.Format == PaintColrGlyph.format:
el = parent_el
Expand Down
36 changes: 36 additions & 0 deletions tests/colr_to_svg_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,42 @@ def _buildPaint(source) -> ot.Paint:
</svg>
""",
),
# other kinds of PaintComposite are dropped for now, only BackdropPaint is kept
# https://github.com/googlefonts/nanoemoji/issues/409
(
"composite",
{
"composite": {
"Format": ot.PaintFormat.PaintComposite,
"CompositeMode": "soft_light",
"SourcePaint": {
"Format": ot.PaintFormat.PaintLinearGradient,
"ColorLine": {
"Extend": "pad",
"ColorStop": [(0.0, 1, 0.5), (0.5, 2, 0.5), (1.0, 3, 0.5)],
},
"x0": 47,
"y0": 790,
"x1": 890,
"y1": -342,
"x2": -1085,
"y2": -53,
},
"BackdropPaint": (
ot.PaintFormat.PaintGlyph,
(ot.PaintFormat.PaintSolid, 0),
"box",
),
},
},
{"box": _draw_box},
"""
<svg xmlns="http://www.w3.org/2000/svg">
<defs/>
<path d="M10,10 L90,10 L90,90 L10,90 Z"/>
</svg>
""",
),
],
)
def test_colr_v1_paint_to_svg(
Expand Down