draw2d/pdf2d/path_converter.go

57 lines
1.3 KiB
Go
Raw Normal View History

// Copyright 2015 The draw2d Authors. All rights reserved.
// created: 26/06/2015 by Stani Michiels
package pdf2d
import (
"math"
"github.com/stanim/draw2d"
)
const deg = 180 / math.Pi
type PathConverter struct {
pdf Vectorizer
}
func NewPathConverter(pdf Vectorizer) *PathConverter {
return &PathConverter{pdf: pdf}
}
func (c *PathConverter) Convert(paths ...*draw2d.PathStorage) {
for _, path := range paths {
j := 0
for _, cmd := range path.Commands {
j = j + c.ConvertCommand(cmd, path.Vertices[j:]...)
}
}
}
func (c *PathConverter) ConvertCommand(cmd draw2d.PathCmd, vertices ...float64) int {
switch cmd {
case draw2d.MoveTo:
c.pdf.MoveTo(vertices[0], vertices[1])
return 2
case draw2d.LineTo:
c.pdf.LineTo(vertices[0], vertices[1])
return 2
case draw2d.QuadCurveTo:
c.pdf.CurveTo(vertices[0], vertices[1], vertices[2], vertices[3])
return 4
case draw2d.CubicCurveTo:
c.pdf.CurveBezierCubicTo(vertices[0], vertices[1], vertices[2], vertices[3], vertices[4], vertices[5])
return 6
case draw2d.ArcTo:
c.pdf.ArcTo(vertices[0], vertices[1], vertices[2], vertices[3],
0, // degRotate
vertices[4]*deg, // degStart = startAngle
(vertices[4]-vertices[5])*deg) // degEnd = startAngle-angle
return 6
case draw2d.Close:
c.pdf.ClosePath()
return 0
}
return 0
}