Skip to content

Commit

Permalink
Merge pull request #2 from llgcode/Draw2d-Clean-Up
Browse files Browse the repository at this point in the history
Draw2d Clean Up
  • Loading branch information
llgcode committed Aug 14, 2015
2 parents bc650a8 + a682f7d commit 51e4389
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 27 deletions.
58 changes: 33 additions & 25 deletions operators_graphics.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ func moveto(interpreter *Interpreter) {
func rmoveto(interpreter *Interpreter) {
y := interpreter.PopFloat()
x := interpreter.PopFloat()
interpreter.GetGraphicContext().RMoveTo(x, y)
sx, sy := interpreter.GetGraphicContext().LastPoint()
interpreter.GetGraphicContext().MoveTo(sx+x, sy+y)
}

func lineto(interpreter *Interpreter) {
Expand All @@ -48,7 +49,8 @@ func lineto(interpreter *Interpreter) {
func rlineto(interpreter *Interpreter) {
y := interpreter.PopFloat()
x := interpreter.PopFloat()
interpreter.GetGraphicContext().RLineTo(x, y)
sx, sy := interpreter.GetGraphicContext().LastPoint()
interpreter.GetGraphicContext().LineTo(sx+x, sy+y)
}

func curveto(interpreter *Interpreter) {
Expand All @@ -68,7 +70,8 @@ func rcurveto(interpreter *Interpreter) {
cx2 := interpreter.PopFloat()
cy1 := interpreter.PopFloat()
cx1 := interpreter.PopFloat()
interpreter.GetGraphicContext().RCubicCurveTo(cx1, cy1, cx2, cy2, cx3, cy3)
sx, sy := interpreter.GetGraphicContext().LastPoint()
interpreter.GetGraphicContext().CubicCurveTo(sx+cx1, sy+cy1, sx+cx2, sy+cy2, sx+cx3, sy+cy3)
}

func arc(interpreter *Interpreter) {
Expand Down Expand Up @@ -294,47 +297,48 @@ func initmatrix(interpreter *Interpreter) {
}

func identmatrix(interpreter *Interpreter) {
tr := interpreter.Pop().(draw2d.MatrixTransform)
tr := interpreter.Pop().(draw2d.Matrix)
ident := draw2d.NewIdentityMatrix()
copy(tr[:], ident[:])
interpreter.Push(tr)
}

func defaultmatrix(interpreter *Interpreter) {
tr := interpreter.Pop().(draw2d.MatrixTransform)
tr := interpreter.Pop().(draw2d.Matrix)
ident := draw2d.NewIdentityMatrix()
copy(tr[:], ident[:])
interpreter.Push(tr)
}

func currentmatrix(interpreter *Interpreter) {
tr := interpreter.Pop().(draw2d.MatrixTransform)
tr := interpreter.Pop().(draw2d.Matrix)
ctm := interpreter.GetGraphicContext().GetMatrixTransform()
copy(tr[:], ctm[:])
interpreter.Push(tr)
}

func setmatrix(interpreter *Interpreter) {
tr := interpreter.Pop().(draw2d.MatrixTransform)
tr := interpreter.Pop().(draw2d.Matrix)
interpreter.GetGraphicContext().SetMatrixTransform(tr)
}

func concat(interpreter *Interpreter) {
tr := interpreter.Pop().(draw2d.MatrixTransform)
tr := interpreter.Pop().(draw2d.Matrix)
interpreter.GetGraphicContext().ComposeMatrixTransform(tr)
}
func concatmatrix(interpreter *Interpreter) {
tr3 := interpreter.Pop().(draw2d.MatrixTransform)
tr2 := interpreter.Pop().(draw2d.MatrixTransform)
tr1 := interpreter.Pop().(draw2d.MatrixTransform)
result := tr1.Multiply(tr2)
tr3 := interpreter.Pop().(draw2d.Matrix)
tr2 := interpreter.Pop().(draw2d.Matrix)
tr1 := interpreter.Pop().(draw2d.Matrix)
result := tr2.Copy()
result.Compose(tr1)
copy(tr3[:], result[:])
interpreter.Push(tr3)
}

func transform(interpreter *Interpreter) {
value := interpreter.Pop()
matrix, ok := value.(draw2d.MatrixTransform)
matrix, ok := value.(draw2d.Matrix)
var y float64
if !ok {
matrix = interpreter.GetGraphicContext().GetMatrixTransform()
Expand All @@ -343,14 +347,15 @@ func transform(interpreter *Interpreter) {
y = interpreter.PopFloat()
}
x := interpreter.PopFloat()
matrix.Transform(&x, &y)

x, y = matrix.TransformPoint(x, y)
interpreter.Push(x)
interpreter.Push(y)
}

func itransform(interpreter *Interpreter) {
value := interpreter.Pop()
matrix, ok := value.(draw2d.MatrixTransform)
matrix, ok := value.(draw2d.Matrix)
var y float64
if !ok {
matrix = interpreter.GetGraphicContext().GetMatrixTransform()
Expand All @@ -359,14 +364,14 @@ func itransform(interpreter *Interpreter) {
y = interpreter.PopFloat()
}
x := interpreter.PopFloat()
matrix.InverseTransform(&x, &y)
x, y = matrix.InverseTransformPoint(x, y)
interpreter.Push(x)
interpreter.Push(y)
}

func translate(interpreter *Interpreter) {
value := interpreter.Pop()
matrix, ok := value.(draw2d.MatrixTransform)
matrix, ok := value.(draw2d.Matrix)
var y float64
if !ok {
matrix = interpreter.GetGraphicContext().GetMatrixTransform()
Expand All @@ -378,14 +383,15 @@ func translate(interpreter *Interpreter) {
if !ok {
interpreter.GetGraphicContext().Translate(x, y)
} else {
matrix = draw2d.NewTranslationMatrix(x, y).Multiply(matrix)
interpreter.Push(matrix)
result := matrix.Copy()
result.Translate(x, y)
interpreter.Push(result)
}
}

func rotate(interpreter *Interpreter) {
value := interpreter.Pop()
matrix, ok := value.(draw2d.MatrixTransform)
matrix, ok := value.(draw2d.Matrix)
var angle float64
if !ok {
matrix = interpreter.GetGraphicContext().GetMatrixTransform()
Expand All @@ -396,14 +402,15 @@ func rotate(interpreter *Interpreter) {
if !ok {
interpreter.GetGraphicContext().Rotate(angle)
} else {
matrix = draw2d.NewRotationMatrix(angle).Multiply(matrix)
interpreter.Push(matrix)
result := matrix.Copy()
result.Rotate(angle)
interpreter.Push(result)
}
}

func scale(interpreter *Interpreter) {
value := interpreter.Pop()
matrix, ok := value.(draw2d.MatrixTransform)
matrix, ok := value.(draw2d.Matrix)
var y float64
if !ok {
matrix = interpreter.GetGraphicContext().GetMatrixTransform()
Expand All @@ -415,8 +422,9 @@ func scale(interpreter *Interpreter) {
if !ok {
interpreter.GetGraphicContext().Scale(x, y)
} else {
matrix = draw2d.NewScaleMatrix(x, y).Multiply(matrix)
interpreter.Push(matrix)
result := matrix.Copy()
result.Scale(x, y)
interpreter.Push(result)
}
}

Expand Down
Binary file modified result/TestPostscript.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions samples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"strings"
"testing"

"github.com/llgcode/draw2d"
"github.com/llgcode/draw2d/draw2dimg"
)

func saveToPngFile(filePath string, m image.Image) {
Expand Down Expand Up @@ -41,7 +41,7 @@ func init() {

func TestTiger(t *testing.T) {
i := image.NewRGBA(image.Rect(0, 0, 600, 800))
gc := draw2d.NewGraphicContext(i)
gc := draw2dimg.NewGraphicContext(i)
gc.Translate(0, 380)
gc.Scale(1, -1)
gc.Translate(0, -380)
Expand Down

0 comments on commit 51e4389

Please sign in to comment.