Skip to content

Commit

Permalink
Fix from picosvg #76
Browse files Browse the repository at this point in the history
  • Loading branch information
rsheeter committed Aug 10, 2020
1 parent 3d17abe commit 46618c1
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/nanoemoji/color_glyph.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,22 +198,22 @@ def _parse_radial_gradient(grad_el, shape_bbox, view_box, upem):
}


def _color_stop(stop_el) -> ColorStop:
def _color_stop(stop_el, shape_opacity=1.0) -> ColorStop:
offset = _number_or_percentage(stop_el.attrib.get("offset", "0"))
color = Color.fromstring(stop_el.attrib.get("stop-color", "black"))
opacity = _number_or_percentage(stop_el.attrib.get("stop-opacity", "1"))
color = color._replace(alpha=color.alpha * opacity)
color = color._replace(alpha=color.alpha * opacity * shape_opacity)
return ColorStop(stopOffset=offset, color=color)


def _common_gradient_parts(el):
def _common_gradient_parts(el, shape_opacity=1.0):
spread_method = el.attrib.get("spreadMethod", "pad").upper()
if spread_method not in Extend.__members__:
raise ValueError(f"Unknown spreadMethod {spread_method}")

return {
"extend": Extend.__members__[spread_method],
"stops": tuple(_color_stop(stop) for stop in el),
"stops": tuple(_color_stop(stop, shape_opacity) for stop in el),
}


Expand All @@ -237,7 +237,7 @@ def _paint(self, shape):
el = self.picosvg.resolve_url(shape.fill, "*")

grad_type, grad_type_parser = _GRADIENT_INFO[etree.QName(el).localname]
grad_args = _common_gradient_parts(el)
grad_args = _common_gradient_parts(el, shape.opacity)
try:
grad_args.update(
grad_type_parser(
Expand Down
26 changes: 26 additions & 0 deletions tests/color_glyph_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,32 @@ def _round_gradient_coordinates(paint, prec=6):
)
},
),
# Shape with opacity, should apply to gradient colors
# See https://github.com/googlefonts/picosvg/issues/76
(
"gradient_opacity.svg",
{
PaintLinearGradient(
stops=(
ColorStop(stopOffset=0.1, color=Color.fromstring("blue", alpha=0.4)),
ColorStop(stopOffset=0.9, color=Color.fromstring("cyan", alpha=0.4 * 0.8)),
),
p0=Point(200.0, 800.0),
p1=Point(800.0, 800.0),
),
PaintRadialGradient(
stops=(
ColorStop(stopOffset=0.1, color=Color.fromstring("red", alpha=0.5),),
ColorStop(stopOffset=0.9, color=Color.fromstring("yellow", alpha=0.5 * 0.8),),
),
c0=Point(x=500.0, y=400.0),
c1=Point(x=500.0, y=400.0),
r0=0.0,
r1=300.0,
affine2x2=(1.0, 0.0, 0.0, -0.333333),
)
},
),
],
)
def test_paint_from_shape(svg_in, expected_paints):
Expand Down
17 changes: 17 additions & 0 deletions tests/gradient_opacity.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 46618c1

Please sign in to comment.