2015-06-26 23:13:20 +00:00
|
|
|
// Copyright 2015 The draw2d Authors. All rights reserved.
|
|
|
|
// created: 26/06/2015 by Stani Michiels
|
|
|
|
|
2015-07-06 22:25:24 +00:00
|
|
|
package draw2dpdf
|
2015-06-26 23:13:20 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"math"
|
|
|
|
|
2015-07-06 22:07:44 +00:00
|
|
|
"github.com/llgcode/draw2d"
|
2015-06-26 23:13:20 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const deg = 180 / math.Pi
|
|
|
|
|
2015-06-27 14:21:13 +00:00
|
|
|
// PathConverter converts the paths to the pdf api
|
2015-06-26 23:13:20 +00:00
|
|
|
type PathConverter struct {
|
|
|
|
pdf Vectorizer
|
|
|
|
}
|
|
|
|
|
2015-06-27 14:21:13 +00:00
|
|
|
// NewPathConverter constructs a PathConverter from a pdf vectorizer
|
2015-06-26 23:13:20 +00:00
|
|
|
func NewPathConverter(pdf Vectorizer) *PathConverter {
|
|
|
|
return &PathConverter{pdf: pdf}
|
|
|
|
}
|
|
|
|
|
2015-06-27 14:21:13 +00:00
|
|
|
// Convert converts the paths to the pdf api
|
2015-06-26 23:13:20 +00:00
|
|
|
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:]...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-27 14:21:13 +00:00
|
|
|
// ConvertCommand converts a single path segment to the pdf api
|
2015-06-26 23:13:20 +00:00
|
|
|
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
|
|
|
|
}
|