Merge pull request #69 from stanim/master

add transparency support to pdf backend
This commit is contained in:
Stani 2015-07-14 12:57:39 +02:00
commit 27ca8d66a7
5 changed files with 35 additions and 20 deletions

View file

@ -190,7 +190,9 @@ func (gc *GraphicContext) StrokeStringAt(text string, x, y float64) (cursor floa
// Stroke strokes the paths with the color specified by SetStrokeColor // Stroke strokes the paths with the color specified by SetStrokeColor
func (gc *GraphicContext) Stroke(paths ...*draw2d.PathStorage) { func (gc *GraphicContext) Stroke(paths ...*draw2d.PathStorage) {
gc.draw("D", paths...) _, _, _, alphaS := gc.Current.StrokeColor.RGBA()
gc.draw("D", alphaS, paths...)
gc.Current.Path = draw2d.NewPathStorage()
} }
// Fill fills the paths with the color specified by SetFillColor // Fill fills the paths with the color specified by SetFillColor
@ -199,27 +201,43 @@ func (gc *GraphicContext) Fill(paths ...*draw2d.PathStorage) {
if !gc.Current.FillRule.UseNonZeroWinding() { if !gc.Current.FillRule.UseNonZeroWinding() {
style += "*" style += "*"
} }
gc.draw(style, paths...) _, _, _, alphaF := gc.Current.FillColor.RGBA()
gc.draw(style, alphaF, paths...)
gc.Current.Path = draw2d.NewPathStorage()
} }
// FillStroke first fills the paths and than strokes them // FillStroke first fills the paths and than strokes them
func (gc *GraphicContext) FillStroke(paths ...*draw2d.PathStorage) { func (gc *GraphicContext) FillStroke(paths ...*draw2d.PathStorage) {
style := "FD" var rule string
if !gc.Current.FillRule.UseNonZeroWinding() { if !gc.Current.FillRule.UseNonZeroWinding() {
style += "*" rule = "*"
} }
gc.draw(style, paths...) _, _, _, alphaS := gc.Current.StrokeColor.RGBA()
_, _, _, alphaF := gc.Current.FillColor.RGBA()
if alphaS == alphaF {
gc.draw("FD"+rule, alphaF, paths...)
} else {
gc.draw("F"+rule, alphaF, paths...)
gc.draw("S", alphaS, paths...)
}
gc.Current.Path = draw2d.NewPathStorage()
} }
var logger = log.New(os.Stdout, "", log.Lshortfile) var logger = log.New(os.Stdout, "", log.Lshortfile)
const alphaMax = float64(0xFFFF)
// draw fills and/or strokes paths // draw fills and/or strokes paths
func (gc *GraphicContext) draw(style string, paths ...*draw2d.PathStorage) { func (gc *GraphicContext) draw(style string, alpha uint32, paths ...*draw2d.PathStorage) {
paths = append(paths, gc.Current.Path) paths = append(paths, gc.Current.Path)
pathConverter := NewPathConverter(gc.pdf) pathConverter := NewPathConverter(gc.pdf)
pathConverter.Convert(paths...) pathConverter.Convert(paths...)
a := float64(alpha) / alphaMax
current, blendMode := gc.pdf.GetAlpha()
if a != current {
gc.pdf.SetAlpha(a, blendMode)
}
gc.pdf.DrawPath(style) gc.pdf.DrawPath(style)
gc.Current.Path = draw2d.NewPathStorage()
} }
// overwrite StackGraphicContext methods // overwrite StackGraphicContext methods

Binary file not shown.

Before

Width:  |  Height:  |  Size: 24 KiB

After

Width:  |  Height:  |  Size: 24 KiB

Binary file not shown.

View file

@ -80,7 +80,6 @@ func CurveRectangle(gc draw2d.GraphicContext, x0, y0,
} }
} }
gc.Close() gc.Close()
gc.SetStrokeColor(stroke) gc.SetStrokeColor(stroke)
gc.SetFillColor(fill) gc.SetFillColor(fill)
gc.SetLineWidth(10.0) gc.SetLineWidth(10.0)
@ -125,9 +124,8 @@ func Arc(gc draw2d.GraphicContext, xc, yc, width, height float64) {
gc.SetFillColor(color.NRGBA{255, 0x33, 0x33, 0x80}) gc.SetFillColor(color.NRGBA{255, 0x33, 0x33, 0x80})
gc.SetLineWidth(width / 20) gc.SetLineWidth(width / 20)
gc.MoveTo(xc, yc) gc.MoveTo(xc+math.Cos(startAngle)*radiusX, yc+math.Sin(startAngle)*radiusY)
gc.LineTo(xc+math.Cos(startAngle)*radiusX, yc+math.Sin(startAngle)*radiusY) gc.LineTo(xc, yc)
gc.MoveTo(xc, yc)
gc.LineTo(xc-radiusX, yc) gc.LineTo(xc-radiusX, yc)
gc.Stroke() gc.Stroke()
@ -154,9 +152,8 @@ func ArcNegative(gc draw2d.GraphicContext, xc, yc, width, height float64) {
gc.SetFillColor(color.NRGBA{255, 0x33, 0x33, 0x80}) gc.SetFillColor(color.NRGBA{255, 0x33, 0x33, 0x80})
gc.SetLineWidth(width / 20) gc.SetLineWidth(width / 20)
gc.MoveTo(xc, yc) gc.MoveTo(xc+math.Cos(startAngle)*radiusX, yc+math.Sin(startAngle)*radiusY)
gc.LineTo(xc+math.Cos(startAngle)*radiusX, yc+math.Sin(startAngle)*radiusY) gc.LineTo(xc, yc)
gc.MoveTo(xc, yc)
gc.LineTo(xc-radiusX, yc) gc.LineTo(xc-radiusX, yc)
gc.Stroke() gc.Stroke()
@ -205,7 +202,7 @@ func FillString(gc draw2d.GraphicContext, x, y, width, height float64) {
Name: "luxi", Name: "luxi",
Family: draw2d.FontFamilyMono, Family: draw2d.FontFamilyMono,
Style: draw2d.FontStyleBold | draw2d.FontStyleItalic}) Style: draw2d.FontStyleBold | draw2d.FontStyleItalic})
w := gc.FillString("cou") w := gc.FillString("Hug")
gc.Translate(w+sx, 0) gc.Translate(w+sx, 0)
left, top, right, bottom := gc.GetStringBounds("cou") left, top, right, bottom := gc.GetStringBounds("cou")
gc.SetStrokeColor(color.NRGBA{255, 0x33, 0x33, 0x80}) gc.SetStrokeColor(color.NRGBA{255, 0x33, 0x33, 0x80})
@ -215,7 +212,7 @@ func FillString(gc draw2d.GraphicContext, x, y, width, height float64) {
gc.SetFillColor(color.NRGBA{0x33, 0x33, 0xff, 0xff}) gc.SetFillColor(color.NRGBA{0x33, 0x33, 0xff, 0xff})
gc.SetStrokeColor(color.NRGBA{0x33, 0x33, 0xff, 0xff}) gc.SetStrokeColor(color.NRGBA{0x33, 0x33, 0xff, 0xff})
gc.SetLineWidth(height / 100) gc.SetLineWidth(height / 100)
gc.StrokeString("cou") gc.StrokeString("Hug")
gc.Restore() gc.Restore()
} }

View file

@ -21,7 +21,7 @@ func Main(gc draw2d.GraphicContext, ext string) (string, error) {
gc.SetFillColor(image.White) gc.SetFillColor(image.White)
gc.Save() gc.Save()
// Draw a (partial) gopher // Draw a (partial) gopher
gc.Translate(-65, 65) gc.Translate(-60, 65)
gc.Rotate(-30 * (math.Pi / 180.0)) gc.Rotate(-30 * (math.Pi / 180.0))
Draw(gc, 48, 48, 240, 72) Draw(gc, 48, 48, 240, 72)
gc.Restore() gc.Restore()
@ -41,13 +41,13 @@ func Draw(gc draw2d.GraphicContext, x, y, w, h float64) {
brb := color.RGBA{0x8B, 0x45, 0x13, 0xBB} // brown transparant brb := color.RGBA{0x8B, 0x45, 0x13, 0xBB} // brown transparant
// round head top // round head top
gc.MoveTo(x, y+h) gc.MoveTo(x, y+h*1.002)
gc.CubicCurveTo(x, y+h*1.05, x+w/2, y-h, x+w, y+h*1.05) gc.CubicCurveTo(x+w/4, y-h/3, x+3*w/4, y-h/3, x+w, y+h*1.002)
gc.Close() gc.Close()
gc.SetFillColor(brb) gc.SetFillColor(brb)
gc.Fill() gc.Fill()
// rectangle head bottom // rectangle head bottom
draw2d.RoundRect(gc, x, y+h, x+w, y+h+h, w/5, h/5) draw2d.RoundRect(gc, x, y+h, x+w, y+h+h, h/5, h/5)
gc.Fill() gc.Fill()
// left ear outside // left ear outside
draw2d.Circle(gc, x, y+h, w/12) draw2d.Circle(gc, x, y+h, w/12)