Skip to content

Commit

Permalink
Merge pull request #263 from hrko/master
Browse files Browse the repository at this point in the history
Use fill-opacity and stroke-opacity in the SVG renderer
  • Loading branch information
tdewolff committed Dec 18, 2023
2 parents 67b379b + eee17d4 commit 2ad5075
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion renderers/svg/svg.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,9 @@ func (r *SVG) RenderPath(path *canvas.Path, style canvas.Style, m canvas.Matrix)
if !style.Fill.IsColor() || style.Fill.Color != canvas.Black {
fmt.Fprintf(r.w, `" fill="`)
r.writePaint(r.w, style.Fill)
if style.Fill.IsColor() && style.Fill.Color.A != 255 {
fmt.Fprintf(r.w, `" fill-opacity="%v`, dec(float64(style.Fill.Color.A)/255.0))
}
}
if style.FillRule == canvas.EvenOdd {
fmt.Fprintf(r.w, `" fill-rule="evenodd`)
Expand All @@ -208,6 +211,9 @@ func (r *SVG) RenderPath(path *canvas.Path, style canvas.Style, m canvas.Matrix)
if !style.Fill.IsColor() || style.Fill.Color != canvas.Black {
fmt.Fprintf(b, ";fill:")
r.writePaint(b, style.Fill)
if style.Fill.IsColor() && style.Fill.Color.A != 255 {
fmt.Fprintf(b, ";fill-opacity:%v", dec(float64(style.Fill.Color.A)/255.0))
}
}
if style.FillRule == canvas.EvenOdd {
fmt.Fprintf(b, ";fill-rule:evenodd")
Expand All @@ -218,6 +224,9 @@ func (r *SVG) RenderPath(path *canvas.Path, style canvas.Style, m canvas.Matrix)
if style.HasStroke() && !strokeUnsupported {
fmt.Fprintf(b, `;stroke:`)
r.writePaint(b, style.Stroke)
if style.Stroke.IsColor() && style.Stroke.Color.A != 255 {
fmt.Fprintf(b, ";stroke-opacity:%v", dec(float64(style.Stroke.Color.A)/255.0))
}
if style.StrokeWidth != 1.0 {
fmt.Fprintf(b, ";stroke-width:%v", dec(style.StrokeWidth))
}
Expand Down Expand Up @@ -274,6 +283,9 @@ func (r *SVG) RenderPath(path *canvas.Path, style canvas.Style, m canvas.Matrix)
if !style.Stroke.IsColor() || style.Stroke.Color != canvas.Black {
fmt.Fprintf(r.w, `" fill="`)
r.writePaint(r.w, style.Stroke)
if style.Stroke.IsColor() && style.Stroke.Color.A != 255 {
fmt.Fprintf(r.w, `" fill-opacity="%v`, dec(float64(style.Stroke.Color.A)/255.0))
}
}
if style.FillRule == canvas.EvenOdd {
fmt.Fprintf(r.w, `" fill-rule="evenodd`)
Expand Down Expand Up @@ -326,13 +338,19 @@ func (r *SVG) writeFontStyle(face, faceMain *canvas.FontFace, rtl bool) {
if !face.Fill.Equal(faceMain.Fill) {
fmt.Fprintf(r.w, `;fill:`)
r.writePaint(r.w, face.Fill)
if face.Fill.IsColor() && face.Fill.Color.A != 255 {
fmt.Fprintf(r.w, `;fill-opacity:%v`, dec(float64(face.Fill.Color.A)/255.0))
}
}
if rtl {
fmt.Fprintf(r.w, `;direction:rtl`)
}
} else if differences == 1 && !face.Fill.Equal(faceMain.Fill) {
fmt.Fprintf(r.w, `" fill="`)
r.writePaint(r.w, face.Fill)
if face.Fill.IsColor() && face.Fill.Color.A != 255 {
fmt.Fprintf(r.w, `" fill-opacity="%v`, dec(float64(face.Fill.Color.A)/255.0))
}
} else if 0 < differences {
fmt.Fprintf(r.w, `" style="`)
buf := &bytes.Buffer{}
Expand All @@ -350,6 +368,9 @@ func (r *SVG) writeFontStyle(face, faceMain *canvas.FontFace, rtl bool) {
if !face.Fill.Equal(faceMain.Fill) {
fmt.Fprintf(buf, `;fill:`)
r.writePaint(r.w, face.Fill)
if face.Fill.IsColor() && face.Fill.Color.A != 255 {
fmt.Fprintf(buf, `;fill-opacity:%v`, dec(float64(face.Fill.Color.A)/255.0))
}
}
if rtl {
fmt.Fprintf(r.w, `;direction:rtl`)
Expand Down Expand Up @@ -402,6 +423,9 @@ func (r *SVG) RenderText(text *canvas.Text, m canvas.Matrix) {
if !faceMain.Fill.IsColor() || faceMain.Fill.Color != canvas.Black {
fmt.Fprintf(r.w, `;fill:`)
r.writePaint(r.w, faceMain.Fill)
if faceMain.Fill.IsColor() && faceMain.Fill.Color.A != 255 {
fmt.Fprintf(r.w, `;fill-opacity:%v`, dec(float64(faceMain.Fill.Color.A)/255.0))
}
}
if text.WritingMode != canvas.HorizontalTB {
if text.WritingMode == canvas.VerticalLR {
Expand Down Expand Up @@ -580,6 +604,8 @@ func (r *SVG) writePaint(w io.Writer, paint canvas.Paint) {
} else if paint.IsGradient() {
fmt.Fprintf(w, "url(#%v)", r.getPattern(paint.Gradient))
} else {
fmt.Fprintf(w, "%v", canvas.CSSColor(paint.Color))
c := paint.Color
c.A = 255
fmt.Fprintf(w, "%v", canvas.CSSColor(c))
}
}

0 comments on commit 2ad5075

Please sign in to comment.