draw2d/path_adder.go

73 lines
2.3 KiB
Go
Raw 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 {
adder raster.Adder
2011-04-27 08:06:14 +00:00
}
func NewVertexAdder(adder raster.Adder) *VertexAdder {
return &VertexAdder{adder}
2011-04-27 08:06:14 +00:00
}
func (vertexAdder *VertexAdder) MoveTo(x, y float64) {
vertexAdder.adder.Start(raster.Point{raster.Fix32(x * 256), raster.Fix32(y * 256)})
2011-04-27 08:06:14 +00:00
}
func (vertexAdder *VertexAdder) LineTo(x, y float64) {
vertexAdder.adder.Add1(raster.Point{raster.Fix32(x * 256), raster.Fix32(y * 256)})
2011-04-27 08:06:14 +00:00
}
2015-04-23 15:43:26 +00:00
func (vertexAdder *VertexAdder) LineJoin() {
}
func (vertexAdder *VertexAdder) Close() {
}
2015-04-23 14:18:21 +00:00
func (vertexAdder *VertexAdder) End() {
}
2011-04-27 08:06:14 +00:00
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 {
return &PathAdder{adder, raster.Point{0, 0}, 1}
}
2015-04-23 16:12:31 +00:00
func (pathAdder *PathAdder) Convert(paths ...*Path) {
2011-04-27 08:06:14 +00:00
for _, path := range paths {
j := 0
for _, cmd := range path.commands {
switch cmd {
case MoveTo:
pathAdder.firstPoint = raster.Point{raster.Fix32(path.vertices[j] * 256), raster.Fix32(path.vertices[j+1] * 256)}
pathAdder.adder.Start(pathAdder.firstPoint)
j += 2
case LineTo:
pathAdder.adder.Add1(raster.Point{raster.Fix32(path.vertices[j] * 256), raster.Fix32(path.vertices[j+1] * 256)})
j += 2
case QuadCurveTo:
pathAdder.adder.Add2(raster.Point{raster.Fix32(path.vertices[j] * 256), raster.Fix32(path.vertices[j+1] * 256)}, raster.Point{raster.Fix32(path.vertices[j+2] * 256), raster.Fix32(path.vertices[j+3] * 256)})
j += 4
case CubicCurveTo:
pathAdder.adder.Add3(raster.Point{raster.Fix32(path.vertices[j] * 256), raster.Fix32(path.vertices[j+1] * 256)}, raster.Point{raster.Fix32(path.vertices[j+2] * 256), raster.Fix32(path.vertices[j+3] * 256)}, raster.Point{raster.Fix32(path.vertices[j+4] * 256), raster.Fix32(path.vertices[j+5] * 256)})
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)
pathAdder.adder.Add1(lastPoint)
j += 6
case Close:
pathAdder.adder.Add1(pathAdder.firstPoint)
}
}
}
}