From f2ef568840b750f5086d0eb9c4bbb619d9e61dff Mon Sep 17 00:00:00 2001 From: Gunnsteinn Hall Date: Sun, 3 Jun 2018 02:26:50 +0000 Subject: [PATCH] Add comments. Output generated files to /tmp. --- pdf/creator/curve.go | 15 +++++++++++---- pdf/creator/curve_test.go | 11 +++++++---- pdf/creator/filled_curve.go | 21 ++++++++++++++------- pdf/creator/filled_curve_test.go | 10 +++++++--- testfiles/curve.pdf | Bin 1141 -> 0 bytes testfiles/filledCurve.pdf | Bin 1068 -> 0 bytes 6 files changed, 39 insertions(+), 18 deletions(-) delete mode 100644 testfiles/curve.pdf delete mode 100644 testfiles/filledCurve.pdf 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 f20f1fa60bb98e9e4ea8be4eb228c1c4ee653184..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 1141 zcmZuxT}TvB6c*nctA~Po$lwGmDY1KJcGsT~+sxHfOUu@^BDENI@2;cUjLpo-su77& z5rvc`6k3uP*bmfFCf17>y#%H7kWk=Hp&-e|Sdgo0ne1#-s>r^O7b}obE{S=v7aFV<&cZ?^`5zR$SW>aC zG8N2FqiOY72!$lkCwncToQj+Z7Bo4={LdMOgTa!IhO)*&dK&97Ag7=SK{@0i z@sjAI4lbGCRySB#q;u9Nmcd7tREeaK7(`k+bpsi? zz?d-Ve2p*~8I^^qHN7k}Lk{H%D%KUDLL6+sn&=f!zT}mCq7(phT$Bz-su%@gzG+0m zw@VCar)&E3=87r3qQ=i=uI ze&*a6EWGk<;r8PA<#0)5+rjTOe}0_ID*HS*J3kwlR(2QdpEGr~gmV_eN53B?b|x+C z`8qie@ZT*QyP4DacIel|69aYXMBmKbosR4M=D>$-Q(VjVt`z5`=FzTG9c9AdC+`yO zZEsI_bG7FdKb7BK_j>+(^2@2`*COMIWA@_3Ugtz)YDZ)Lq&ggzlw3a?|EckB(?|bM zPgUZV7mG6w`kvN3d*#}4dbBw%E^cY5v+{io?KC4PqvvSpjdB>N^O>>C&oa3lM!#>) z4j#Wg6f7tKhGT(zJU(XC2!JsiW+=cK2w;o`kY=TWp{|(>@YFK-9_HvqMPUW|H#00` zlCKqm^t3cv8qf{HI3DyBNGlj;BOOg`HO7-hUWZxjqzTqxHh!Z&J5L^gb>}kD$&^Mh vO%cQ(R_Gg1B>82aZ-yJWWmy9{%WhOAHepzQ6L5-f4d diff --git a/testfiles/filledCurve.pdf b/testfiles/filledCurve.pdf deleted file mode 100644 index 614d707e1b939b0186362267660e7674de2bd5e5..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 1068 zcmZuwO-vI(6b26-CW;3VZ<6PsL1Nn3?Y5LAM5#djZ=Gs1L!XEYpMGm;_$HX;ySNs5OxE?*WNoE){Vb)pFEhMb{A3MIW+E z_{DcHt3q7n>=cI2P}3qQR)%3EqWA_|i-a28^tI}nOUcCHqK2TBkZGXRCs-pUlkXrV zE8~IUDEHeUPpIM)@u<%TpQLt)a?2pN+cB7FIR(JyEvL)ztW8HPx2zcC&RRAXQ^i@E zaGIe4O7Usx6!