From 93779987c426b8c09d620aa6bc48b95ffbef282a Mon Sep 17 00:00:00 2001 From: Claudio Felber Date: Sat, 27 Jun 2015 13:01:14 +0200 Subject: Add SetDashPattern() method --- def.go | 2 ++ fpdf.go | 39 +++++++++++++++++++++++++++++++++++++++ util.go | 13 +++++++++++++ 3 files changed, 54 insertions(+) diff --git a/def.go b/def.go index 47b399f..df21e05 100644 --- a/def.go +++ b/def.go @@ -195,6 +195,8 @@ type Fpdf struct { fontDirStr string // location of font definition files capStyle int // line cap style: butt 0, round 1, square 2 joinStyle int // line segment join style: miter 0, round 1, bevel 2 + dashArray []float64 // dash array + dashPhase float64 // dash phase blendList []blendModeType // slice[idx] of alpha transparency modes, 1-based blendMap map[string]int // map into blendList gradientList []gradientType // slice[idx] of gradient records diff --git a/fpdf.go b/fpdf.go index 3f6f511..1508548 100644 --- a/fpdf.go +++ b/fpdf.go @@ -36,6 +36,7 @@ import ( "math" "os" "path" + "strconv" "strings" "time" ) @@ -565,6 +566,10 @@ func (f *Fpdf) AddPageFormat(orientationStr string, size SizeType) { // Set line width f.lineWidth = lw f.outf("%.2f w", lw*f.k) + // Set dash pattern + if len(f.dashArray) > 0 { + f.outputDashPattern() + } // Set font if familyStr != "" { f.SetFont(familyStr, style, fontsize) @@ -775,6 +780,40 @@ func (f *Fpdf) SetLineCapStyle(styleStr string) { } } +// SetDashPattern sets the dash pattern that is used to draw lines. +// The dashArray elements are numbers that specify the lengths of alternating +// dashes and gaps. The dash phase specifies the distance into the dash pattern +// at which to start the dash. The dash pattern is retained from page to page. +func (f *Fpdf) SetDashPattern(dashArray []float64, dashPhase float64) { + scaled := make([]float64, len(dashArray)) + for i, value := range dashArray { + scaled[i] = value * f.k + } + dashPhase *= f.k + if !slicesEqual(scaled, f.dashArray) || dashPhase != f.dashPhase { + f.dashArray = scaled + f.dashPhase = dashPhase + if f.page > 0 { + f.outputDashPattern() + } + } +} + +func (f *Fpdf) outputDashPattern() { + var buf bytes.Buffer + buf.WriteByte('[') + for i, value := range f.dashArray { + if i > 0 { + buf.WriteByte(' ') + } + buf.WriteString(strconv.FormatFloat(value, 'f', 2, 64)) + } + buf.WriteString("] ") + buf.WriteString(strconv.FormatFloat(f.dashPhase, 'f', 2, 64)) + buf.WriteString(" d") + f.outbuf(&buf) +} + // Line draws a line between points (x1, y1) and (x2, y2) using the current // draw color, line width and cap style. func (f *Fpdf) Line(x1, y1, x2, y2 float64) { diff --git a/util.go b/util.go index d3b114f..ed4ffdd 100644 --- a/util.go +++ b/util.go @@ -69,6 +69,19 @@ func bufferFromReader(r io.Reader) (b *bytes.Buffer, err error) { return } +// Returns true if the two specified integer slices are equal +func slicesEqual(a, b []float64) bool { + if len(a) != len(b) { + return false + } + for i := range a { + if a[i] != b[i] { + return false + } + } + return true +} + // Returns a zlib-compressed copy of the specified byte array func sliceCompress(data []byte) []byte { var buf bytes.Buffer -- cgit v1.2.1-24-ge1ad