lib/prawn/svg/attributes/opacity.rb in prawn-svg-0.35.1 vs lib/prawn/svg/attributes/opacity.rb in prawn-svg-0.36.0

- old
+ new

@@ -1,15 +1,30 @@ module Prawn::SVG::Attributes::Opacity def parse_opacity_attributes_and_call - # We can't do nested opacities quite like the SVG requires, but this is close enough. - opacity = properties.opacity.to_f.clamp(0, 1) if properties.opacity - fill_opacity = properties.fill_opacity.to_f.clamp(0, 1) if properties.fill_opacity - stroke_opacity = properties.stroke_opacity.to_f.clamp(0, 1) if properties.stroke_opacity + # The opacity property is not inherited, but the opacity applies to all children underneath the element. + # + # Having an opacity property on a parent, and again on a child, multiplies the parent and child's opacities + # when drawing the children. + # + # The fill-opacity and stroke-opacity properties are inherited, but children which have a different value + # are displayed at that opacity rather than multiplying the parent's fill/stroke opacity with the child's. + # + # opacity and fill/stroke opacity can both be applied to the same element, and they multiply together. - if opacity || fill_opacity || stroke_opacity - state.fill_opacity *= [opacity || 1, fill_opacity || 1].min - state.stroke_opacity *= [opacity || 1, stroke_opacity || 1].min + opacity = computed_properties.opacity&.to_f&.clamp(0, 1) + fill_opacity = computed_properties.fill_opacity.to_f.clamp(0, 1) + stroke_opacity = computed_properties.stroke_opacity.to_f.clamp(0, 1) - add_call_and_enter 'transparent', state.fill_opacity, state.stroke_opacity + state.opacity *= opacity if opacity + + fill_opacity = (fill_opacity || 1) * state.opacity + stroke_opacity = (stroke_opacity || 1) * state.opacity + + fill_opacity = stroke_opacity = 0 if computed_properties.visibility != 'visible' + + if state.last_fill_opacity != fill_opacity || state.last_stroke_opacity != stroke_opacity + state.last_fill_opacity = fill_opacity + state.last_stroke_opacity = stroke_opacity + add_call_and_enter 'transparent', fill_opacity, stroke_opacity end end end