github.com/unidoc/unidoc@v2.2.0+incompatible/pdf/creator/curve.go (about) 1 /* 2 * This file is subject to the terms and conditions defined in 3 * file 'LICENSE.md', which is part of this source code package. 4 */ 5 6 package creator 7 8 import ( 9 "fmt" 10 "strings" 11 12 "github.com/unidoc/unidoc/pdf/model" 13 ) 14 15 // NewCurve returns new instance of Curve between points (x1,y1) and (x2, y2) with control point (cx,cy). 16 func NewCurve(x1, y1, cx, cy, x2, y2 float64) *Curve { 17 c := &Curve{} 18 19 c.x1 = x1 20 c.y1 = y1 21 22 c.cx = cx 23 c.cy = cy 24 25 c.x2 = x2 26 c.y2 = y2 27 28 c.lineColor = model.NewPdfColorDeviceRGB(0, 0, 0) 29 c.lineWidth = 1.0 30 return c 31 } 32 33 // Curve represents a cubic Bezier curve with a control point. 34 type Curve struct { 35 x1 float64 36 y1 float64 37 cx float64 // control point 38 cy float64 39 x2 float64 40 y2 float64 41 42 lineColor *model.PdfColorDeviceRGB 43 lineWidth float64 44 } 45 46 // SetWidth sets line width. 47 func (c *Curve) SetWidth(width float64) { 48 c.lineWidth = width 49 } 50 51 // SetColor sets the line color. 52 func (c *Curve) SetColor(col Color) { 53 c.lineColor = model.NewPdfColorDeviceRGB(col.ToRGB()) 54 } 55 56 // GeneratePageBlocks draws the curve onto page blocks. 57 func (c *Curve) GeneratePageBlocks(ctx DrawContext) ([]*Block, DrawContext, error) { 58 block := NewBlock(ctx.PageWidth, ctx.PageHeight) 59 60 var ops []string 61 ops = append(ops, fmt.Sprintf("%.2f w", c.lineWidth)) // line widtdh 62 ops = append(ops, fmt.Sprintf("%.3f %.3f %.3f RG", c.lineColor[0], c.lineColor[1], c.lineColor[2])) // line color 63 ops = append(ops, fmt.Sprintf("%.2f %.2f m", c.x1, ctx.PageHeight-c.y1)) // move to 64 ops = append(ops, fmt.Sprintf("%.5f %.5f %.5f %.5f v S", c.cx, ctx.PageHeight-c.cy, c.x2, ctx.PageHeight-c.y2)) 65 66 err := block.addContentsByString(strings.Join(ops, "\n")) 67 if err != nil { 68 return nil, ctx, err 69 } 70 return []*Block{block}, ctx, nil 71 }