2018-06-03 02:26:50 +00:00
|
|
|
/*
|
|
|
|
* This file is subject to the terms and conditions defined in
|
|
|
|
* file 'LICENSE.md', which is part of this source code package.
|
|
|
|
*/
|
|
|
|
|
2018-05-31 03:48:26 +06:00
|
|
|
package creator
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2018-06-03 02:26:50 +00:00
|
|
|
"strings"
|
|
|
|
|
2019-05-16 23:44:51 +03:00
|
|
|
"github.com/unidoc/unipdf/v3/model"
|
2018-05-31 03:48:26 +06:00
|
|
|
)
|
|
|
|
|
2018-10-12 23:00:02 +03:00
|
|
|
// newCurve returns new instance of Curve between points (x1,y1) and (x2, y2) with control point (cx,cy).
|
|
|
|
func newCurve(x1, y1, cx, cy, x2, y2 float64) *Curve {
|
2018-05-31 03:48:26 +06:00
|
|
|
c := &Curve{}
|
|
|
|
|
|
|
|
c.x1 = x1
|
|
|
|
c.y1 = y1
|
|
|
|
|
|
|
|
c.cx = cx
|
|
|
|
c.cy = cy
|
|
|
|
|
|
|
|
c.x2 = x2
|
|
|
|
c.y2 = y2
|
|
|
|
|
|
|
|
c.lineColor = model.NewPdfColorDeviceRGB(0, 0, 0)
|
|
|
|
c.lineWidth = 1.0
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2018-06-03 02:26:50 +00:00
|
|
|
// Curve represents a cubic Bezier curve with a control point.
|
2018-05-31 03:48:26 +06:00
|
|
|
type Curve struct {
|
|
|
|
x1 float64
|
|
|
|
y1 float64
|
|
|
|
cx float64 // control point
|
|
|
|
cy float64
|
|
|
|
x2 float64
|
|
|
|
y2 float64
|
|
|
|
|
|
|
|
lineColor *model.PdfColorDeviceRGB
|
|
|
|
lineWidth float64
|
|
|
|
}
|
|
|
|
|
2018-06-03 02:26:50 +00:00
|
|
|
// SetWidth sets line width.
|
2018-05-31 03:48:26 +06:00
|
|
|
func (c *Curve) SetWidth(width float64) {
|
|
|
|
c.lineWidth = width
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetColor sets the line color.
|
|
|
|
func (c *Curve) SetColor(col Color) {
|
|
|
|
c.lineColor = model.NewPdfColorDeviceRGB(col.ToRGB())
|
|
|
|
}
|
|
|
|
|
2018-06-03 02:26:50 +00:00
|
|
|
// GeneratePageBlocks draws the curve onto page blocks.
|
2018-05-31 03:48:26 +06:00
|
|
|
func (c *Curve) GeneratePageBlocks(ctx DrawContext) ([]*Block, DrawContext, error) {
|
|
|
|
block := NewBlock(ctx.PageWidth, ctx.PageHeight)
|
|
|
|
|
|
|
|
var ops []string
|
|
|
|
ops = append(ops, fmt.Sprintf("%.2f w", c.lineWidth)) // line widtdh
|
|
|
|
ops = append(ops, fmt.Sprintf("%.3f %.3f %.3f RG", c.lineColor[0], c.lineColor[1], c.lineColor[2])) // line color
|
|
|
|
ops = append(ops, fmt.Sprintf("%.2f %.2f m", c.x1, ctx.PageHeight-c.y1)) // move to
|
|
|
|
ops = append(ops, fmt.Sprintf("%.5f %.5f %.5f %.5f v S", c.cx, ctx.PageHeight-c.cy, c.x2, ctx.PageHeight-c.y2))
|
|
|
|
|
|
|
|
err := block.addContentsByString(strings.Join(ops, "\n"))
|
|
|
|
if err != nil {
|
|
|
|
return nil, ctx, err
|
|
|
|
}
|
|
|
|
return []*Block{block}, ctx, nil
|
|
|
|
}
|