diff --git a/pdf/creator/curve.go b/pdf/creator/curve.go index c1acf27f..072be42c 100644 --- a/pdf/creator/curve.go +++ b/pdf/creator/curve.go @@ -1,12 +1,18 @@ +/* + * This file is subject to the terms and conditions defined in + * file 'LICENSE.md', which is part of this source code package. + */ + package creator import ( - "strings" "fmt" + "strings" + "github.com/unidoc/unidoc/pdf/model" ) -// NewCurve returns new instance of Curve +// 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 { c := &Curve{} @@ -24,6 +30,7 @@ func NewCurve(x1, y1, cx, cy, x2, y2 float64) *Curve { return c } +// Curve represents a cubic Bezier curve with a control point. type Curve struct { x1 float64 y1 float64 @@ -36,7 +43,7 @@ type Curve struct { lineWidth float64 } -// SetWidth sets line width +// SetWidth sets line width. func (c *Curve) SetWidth(width float64) { c.lineWidth = width } @@ -46,7 +53,7 @@ func (c *Curve) SetColor(col Color) { c.lineColor = model.NewPdfColorDeviceRGB(col.ToRGB()) } -// GeneratePageBlocks generates page blocks +// GeneratePageBlocks draws the curve onto page blocks. func (c *Curve) GeneratePageBlocks(ctx DrawContext) ([]*Block, DrawContext, error) { block := NewBlock(ctx.PageWidth, ctx.PageHeight) diff --git a/pdf/creator/curve_test.go b/pdf/creator/curve_test.go index 37033a08..338ac41e 100644 --- a/pdf/creator/curve_test.go +++ b/pdf/creator/curve_test.go @@ -1,9 +1,12 @@ +/* + * This file is subject to the terms and conditions defined in + * file 'LICENSE.md', which is part of this source code package. + */ + package creator import "testing" -const testPdfFileWithCurve = "../../testfiles/curve.pdf" - func TestNewCurve(t *testing.T) { creator := New() creator.NewPage() @@ -16,7 +19,7 @@ func TestNewCurve(t *testing.T) { return } - err = creator.WriteToFile(testPdfFileWithCurve) + err = creator.WriteToFile("/tmp/curve.pdf") if err != nil { t.Errorf("Fail: %v", err) return @@ -60,7 +63,7 @@ func TestNewCurveWithGlass(t *testing.T) { creator.Draw(CreateCurve(51, 399, 75, 445, 150, 450, ColorRed)) creator.Draw(CreateCurve(150, 450, 225, 445, 251, 399, ColorGreen)) - err := creator.WriteToFile(testPdfFileWithCurve) + err := creator.WriteToFile("/tmp/curve_glass.pdf") if err != nil { t.Errorf("Fail: %v", err) return diff --git a/pdf/creator/filled_curve.go b/pdf/creator/filled_curve.go index 9c29469f..5714ff7e 100644 --- a/pdf/creator/filled_curve.go +++ b/pdf/creator/filled_curve.go @@ -1,12 +1,18 @@ +/* + * This file is subject to the terms and conditions defined in + * file 'LICENSE.md', which is part of this source code package. + */ + package creator import ( - "github.com/unidoc/unidoc/pdf/contentstream/draw" pdfcontent "github.com/unidoc/unidoc/pdf/contentstream" + "github.com/unidoc/unidoc/pdf/contentstream/draw" pdfcore "github.com/unidoc/unidoc/pdf/core" pdf "github.com/unidoc/unidoc/pdf/model" ) +// FilledCurve represents a closed path of Bezier curves with a border and fill. type FilledCurve struct { curves []draw.CubicBezierCurve FillEnabled bool // Show fill? @@ -14,31 +20,32 @@ type FilledCurve struct { BorderEnabled bool // Show border? BorderWidth float64 borderColor *pdf.PdfColorDeviceRGB - Opacity float64 // Alpha value (0-1). } -// NewFilledCurve returns a instance of filled curve +// NewFilledCurve returns a instance of filled curve. func NewFilledCurve() *FilledCurve { curve := FilledCurve{} curve.curves = []draw.CubicBezierCurve{} return &curve } -// AppendCurve appends curve to filled curve +// AppendCurve appends a Bezier curve to the filled curve. func (this *FilledCurve) AppendCurve(curve draw.CubicBezierCurve) *FilledCurve { this.curves = append(this.curves, curve) return this } +// SetFillColor sets the fill color for the path. func (this *FilledCurve) SetFillColor(color Color) { this.fillColor = pdf.NewPdfColorDeviceRGB(color.ToRGB()) } +// SetBorderColor sets the border color for the path. func (this *FilledCurve) SetBorderColor(color Color) { this.borderColor = pdf.NewPdfColorDeviceRGB(color.ToRGB()) } -// Draw a circle. Can specify a graphics state (gsName) for setting opacity etc. Otherwise leave empty (""). +// draw draws the filled curve. Can specify a graphics state (gsName) for setting opacity etc. Otherwise leave empty (""). // Returns the content stream as a byte array, the bounding box and an error on failure. func (this *FilledCurve) draw(gsName string) ([]byte, *pdf.PdfRectangle, error) { bpath := draw.NewCubicBezierPath() @@ -57,7 +64,7 @@ func (this *FilledCurve) draw(gsName string) ([]byte, *pdf.PdfRectangle, error) creator.Add_w(this.BorderWidth) } if len(gsName) > 1 { - // If a graphics state is provided, use it. (Used for transparency settings here). + // If a graphics state is provided, use it. (can support transparency). creator.Add_gs(pdfcore.PdfObjectName(gsName)) } @@ -92,7 +99,7 @@ func (this *FilledCurve) draw(gsName string) ([]byte, *pdf.PdfRectangle, error) return creator.Bytes(), bbox, nil } -// GeneratePageBlocks generates page blocks +// GeneratePageBlocks draws the filled curve on page blocks. func (this *FilledCurve) GeneratePageBlocks(ctx DrawContext) ([]*Block, DrawContext, error) { block := NewBlock(ctx.PageWidth, ctx.PageHeight) diff --git a/pdf/creator/filled_curve_test.go b/pdf/creator/filled_curve_test.go index 25e8ccb2..dc38e014 100644 --- a/pdf/creator/filled_curve_test.go +++ b/pdf/creator/filled_curve_test.go @@ -1,12 +1,16 @@ +/* + * This file is subject to the terms and conditions defined in + * file 'LICENSE.md', which is part of this source code package. + */ + package creator import ( "testing" + "github.com/unidoc/unidoc/pdf/contentstream/draw" ) -const testPdfFileWithFilledCurve = "../../testfiles/filledCurve.pdf" - func CreateFillCurve(x0, y0, x1, y1, x2, y2, x3, y3 float64) draw.CubicBezierCurve { return draw.NewCubicBezierCurve(x0, y0, x1, y1, x2, y2, x3, y3) } @@ -34,7 +38,7 @@ func TestNewFilledCurve(t *testing.T) { creator.NewPage() creator.Draw(filledCurve) - err := creator.WriteToFile(testPdfFileWithFilledCurve) + err := creator.WriteToFile("/tmp/filledCurve.pdf") if err != nil { t.Errorf("Fail: %v", err) return diff --git a/testfiles/curve.pdf b/testfiles/curve.pdf deleted file mode 100644 index f20f1fa6..00000000 Binary files a/testfiles/curve.pdf and /dev/null differ diff --git a/testfiles/filledCurve.pdf b/testfiles/filledCurve.pdf deleted file mode 100644 index 614d707e..00000000 Binary files a/testfiles/filledCurve.pdf and /dev/null differ