Add comments. Output generated files to /tmp.

This commit is contained in:
Gunnsteinn Hall 2018-06-03 02:26:50 +00:00
parent 0785daf45f
commit f2ef568840
6 changed files with 39 additions and 18 deletions

View File

@ -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 package creator
import ( import (
"strings"
"fmt" "fmt"
"strings"
"github.com/unidoc/unidoc/pdf/model" "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 { func NewCurve(x1, y1, cx, cy, x2, y2 float64) *Curve {
c := &Curve{} c := &Curve{}
@ -24,6 +30,7 @@ func NewCurve(x1, y1, cx, cy, x2, y2 float64) *Curve {
return c return c
} }
// Curve represents a cubic Bezier curve with a control point.
type Curve struct { type Curve struct {
x1 float64 x1 float64
y1 float64 y1 float64
@ -36,7 +43,7 @@ type Curve struct {
lineWidth float64 lineWidth float64
} }
// SetWidth sets line width // SetWidth sets line width.
func (c *Curve) SetWidth(width float64) { func (c *Curve) SetWidth(width float64) {
c.lineWidth = width c.lineWidth = width
} }
@ -46,7 +53,7 @@ func (c *Curve) SetColor(col Color) {
c.lineColor = model.NewPdfColorDeviceRGB(col.ToRGB()) 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) { func (c *Curve) GeneratePageBlocks(ctx DrawContext) ([]*Block, DrawContext, error) {
block := NewBlock(ctx.PageWidth, ctx.PageHeight) block := NewBlock(ctx.PageWidth, ctx.PageHeight)

View File

@ -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 package creator
import "testing" import "testing"
const testPdfFileWithCurve = "../../testfiles/curve.pdf"
func TestNewCurve(t *testing.T) { func TestNewCurve(t *testing.T) {
creator := New() creator := New()
creator.NewPage() creator.NewPage()
@ -16,7 +19,7 @@ func TestNewCurve(t *testing.T) {
return return
} }
err = creator.WriteToFile(testPdfFileWithCurve) err = creator.WriteToFile("/tmp/curve.pdf")
if err != nil { if err != nil {
t.Errorf("Fail: %v", err) t.Errorf("Fail: %v", err)
return return
@ -60,7 +63,7 @@ func TestNewCurveWithGlass(t *testing.T) {
creator.Draw(CreateCurve(51, 399, 75, 445, 150, 450, ColorRed)) creator.Draw(CreateCurve(51, 399, 75, 445, 150, 450, ColorRed))
creator.Draw(CreateCurve(150, 450, 225, 445, 251, 399, ColorGreen)) creator.Draw(CreateCurve(150, 450, 225, 445, 251, 399, ColorGreen))
err := creator.WriteToFile(testPdfFileWithCurve) err := creator.WriteToFile("/tmp/curve_glass.pdf")
if err != nil { if err != nil {
t.Errorf("Fail: %v", err) t.Errorf("Fail: %v", err)
return return

View File

@ -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 package creator
import ( import (
"github.com/unidoc/unidoc/pdf/contentstream/draw"
pdfcontent "github.com/unidoc/unidoc/pdf/contentstream" pdfcontent "github.com/unidoc/unidoc/pdf/contentstream"
"github.com/unidoc/unidoc/pdf/contentstream/draw"
pdfcore "github.com/unidoc/unidoc/pdf/core" pdfcore "github.com/unidoc/unidoc/pdf/core"
pdf "github.com/unidoc/unidoc/pdf/model" pdf "github.com/unidoc/unidoc/pdf/model"
) )
// FilledCurve represents a closed path of Bezier curves with a border and fill.
type FilledCurve struct { type FilledCurve struct {
curves []draw.CubicBezierCurve curves []draw.CubicBezierCurve
FillEnabled bool // Show fill? FillEnabled bool // Show fill?
@ -14,31 +20,32 @@ type FilledCurve struct {
BorderEnabled bool // Show border? BorderEnabled bool // Show border?
BorderWidth float64 BorderWidth float64
borderColor *pdf.PdfColorDeviceRGB 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 { func NewFilledCurve() *FilledCurve {
curve := FilledCurve{} curve := FilledCurve{}
curve.curves = []draw.CubicBezierCurve{} curve.curves = []draw.CubicBezierCurve{}
return &curve 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 { func (this *FilledCurve) AppendCurve(curve draw.CubicBezierCurve) *FilledCurve {
this.curves = append(this.curves, curve) this.curves = append(this.curves, curve)
return this return this
} }
// SetFillColor sets the fill color for the path.
func (this *FilledCurve) SetFillColor(color Color) { func (this *FilledCurve) SetFillColor(color Color) {
this.fillColor = pdf.NewPdfColorDeviceRGB(color.ToRGB()) this.fillColor = pdf.NewPdfColorDeviceRGB(color.ToRGB())
} }
// SetBorderColor sets the border color for the path.
func (this *FilledCurve) SetBorderColor(color Color) { func (this *FilledCurve) SetBorderColor(color Color) {
this.borderColor = pdf.NewPdfColorDeviceRGB(color.ToRGB()) 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. // 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) { func (this *FilledCurve) draw(gsName string) ([]byte, *pdf.PdfRectangle, error) {
bpath := draw.NewCubicBezierPath() bpath := draw.NewCubicBezierPath()
@ -57,7 +64,7 @@ func (this *FilledCurve) draw(gsName string) ([]byte, *pdf.PdfRectangle, error)
creator.Add_w(this.BorderWidth) creator.Add_w(this.BorderWidth)
} }
if len(gsName) > 1 { 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)) 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 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) { func (this *FilledCurve) GeneratePageBlocks(ctx DrawContext) ([]*Block, DrawContext, error) {
block := NewBlock(ctx.PageWidth, ctx.PageHeight) block := NewBlock(ctx.PageWidth, ctx.PageHeight)

View File

@ -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 package creator
import ( import (
"testing" "testing"
"github.com/unidoc/unidoc/pdf/contentstream/draw" "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 { func CreateFillCurve(x0, y0, x1, y1, x2, y2, x3, y3 float64) draw.CubicBezierCurve {
return draw.NewCubicBezierCurve(x0, y0, x1, y1, x2, y2, x3, y3) return draw.NewCubicBezierCurve(x0, y0, x1, y1, x2, y2, x3, y3)
} }
@ -34,7 +38,7 @@ func TestNewFilledCurve(t *testing.T) {
creator.NewPage() creator.NewPage()
creator.Draw(filledCurve) creator.Draw(filledCurve)
err := creator.WriteToFile(testPdfFileWithFilledCurve) err := creator.WriteToFile("/tmp/filledCurve.pdf")
if err != nil { if err != nil {
t.Errorf("Fail: %v", err) t.Errorf("Fail: %v", err)
return return

Binary file not shown.

Binary file not shown.