summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorClaudio Felber <claudio.felber@perron2.ch>2015-06-27 13:01:14 +0200
committerClaudio Felber <claudio.felber@perron2.ch>2015-06-27 13:01:14 +0200
commit93779987c426b8c09d620aa6bc48b95ffbef282a (patch)
tree65b2d2e3f24b76c22acc4ca59e4e9a04744e9251
parent5ab948816dec877984e3c1c031f423cb7fccf30f (diff)
Add SetDashPattern() method
-rw-r--r--def.go2
-rw-r--r--fpdf.go39
-rw-r--r--util.go13
3 files changed, 54 insertions, 0 deletions
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