draw2d/path_adder.go

94 lines
2.5 KiB
Go
Raw Permalink Normal View History

2011-04-27 08:06:14 +00:00
// Copyright 2010 The draw2d Authors. All rights reserved.
// created: 13/12/2010 by Laurent Le Goff
2012-04-17 09:03:56 +00:00
2011-04-27 08:06:14 +00:00
package draw2d
import (
2012-01-13 09:14:12 +00:00
"code.google.com/p/freetype-go/freetype/raster"
2011-04-27 08:06:14 +00:00
)
type VertexAdder struct {
2011-05-18 21:23:31 +00:00
command VertexCommand
adder raster.Adder
2011-04-27 08:06:14 +00:00
}
func NewVertexAdder(adder raster.Adder) *VertexAdder {
return &VertexAdder{VertexNoCommand, adder}
}
func (vertexAdder *VertexAdder) NextCommand(cmd VertexCommand) {
vertexAdder.command = cmd
}
func (vertexAdder *VertexAdder) Vertex(x, y float64) {
switch vertexAdder.command {
case VertexStartCommand:
2015-07-10 13:24:23 +00:00
vertexAdder.adder.Start(raster.Point{
X: raster.Fix32(x * 256),
Y: raster.Fix32(y * 256)})
2011-04-27 08:06:14 +00:00
default:
2015-07-10 13:24:23 +00:00
vertexAdder.adder.Add1(raster.Point{
X: raster.Fix32(x * 256),
Y: raster.Fix32(y * 256)})
2011-04-27 08:06:14 +00:00
}
vertexAdder.command = VertexNoCommand
}
type PathAdder struct {
adder raster.Adder
2011-05-18 21:23:31 +00:00
firstPoint raster.Point
2011-04-27 08:06:14 +00:00
ApproximationScale float64
}
func NewPathAdder(adder raster.Adder) *PathAdder {
2015-07-10 13:24:23 +00:00
return &PathAdder{adder, raster.Point{X: 0, Y: 0}, 1}
2011-04-27 08:06:14 +00:00
}
func (pathAdder *PathAdder) Convert(paths ...*PathStorage) {
for _, path := range paths {
j := 0
for _, cmd := range path.Commands {
2011-04-27 08:06:14 +00:00
switch cmd {
case MoveTo:
2015-07-10 13:24:23 +00:00
pathAdder.firstPoint = raster.Point{
X: raster.Fix32(path.Vertices[j] * 256),
Y: raster.Fix32(path.Vertices[j+1] * 256)}
2011-04-27 08:06:14 +00:00
pathAdder.adder.Start(pathAdder.firstPoint)
j += 2
case LineTo:
2015-07-10 13:24:23 +00:00
pathAdder.adder.Add1(raster.Point{
X: raster.Fix32(path.Vertices[j] * 256),
Y: raster.Fix32(path.Vertices[j+1] * 256)})
2011-04-27 08:06:14 +00:00
j += 2
case QuadCurveTo:
2015-07-10 13:24:23 +00:00
pathAdder.adder.Add2(
raster.Point{
X: raster.Fix32(path.Vertices[j] * 256),
Y: raster.Fix32(path.Vertices[j+1] * 256)},
raster.Point{
X: raster.Fix32(path.Vertices[j+2] * 256),
Y: raster.Fix32(path.Vertices[j+3] * 256)})
2011-04-27 08:06:14 +00:00
j += 4
case CubicCurveTo:
2015-07-10 13:24:23 +00:00
pathAdder.adder.Add3(
raster.Point{
X: raster.Fix32(path.Vertices[j] * 256),
Y: raster.Fix32(path.Vertices[j+1] * 256)},
raster.Point{
X: raster.Fix32(path.Vertices[j+2] * 256),
Y: raster.Fix32(path.Vertices[j+3] * 256)},
raster.Point{
X: raster.Fix32(path.Vertices[j+4] * 256),
Y: raster.Fix32(path.Vertices[j+5] * 256)})
2011-04-27 08:06:14 +00:00
j += 6
case ArcTo:
lastPoint := arcAdder(pathAdder.adder, path.Vertices[j], path.Vertices[j+1], path.Vertices[j+2], path.Vertices[j+3], path.Vertices[j+4], path.Vertices[j+5], pathAdder.ApproximationScale)
2011-04-27 08:06:14 +00:00
pathAdder.adder.Add1(lastPoint)
j += 6
case Close:
pathAdder.adder.Add1(pathAdder.firstPoint)
}
}
}
}