summaryrefslogtreecommitdiff
path: root/fpdf.go
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 /fpdf.go
parent5ab948816dec877984e3c1c031f423cb7fccf30f (diff)
Add SetDashPattern() method
Diffstat (limited to 'fpdf.go')
-rw-r--r--fpdf.go39
1 files changed, 39 insertions, 0 deletions
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) {