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 --- fpdf.go | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) (limited to 'fpdf.go') 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) { -- cgit v1.2.1-24-ge1ad From 5cb99b114890dc1ef96e06b70f26fe5cfae63db7 Mon Sep 17 00:00:00 2001 From: Claudio Felber Date: Sat, 27 Jun 2015 18:20:22 +0200 Subject: Add SetFontLoader() method to load fonts from arbitrary locations --- fpdf.go | 37 +++++++++++++++++++++++++++++++++++-- 1 file changed, 35 insertions(+), 2 deletions(-) (limited to 'fpdf.go') diff --git a/fpdf.go b/fpdf.go index 1508548..b2872e6 100644 --- a/fpdf.go +++ b/fpdf.go @@ -313,6 +313,14 @@ func (f *Fpdf) SetFontLocation(fontDirStr string) { f.fontpath = fontDirStr } +// SetFontLoader sets a loader used to load font files (.json and .z) from +// arbitrary locations. If a font loader has been specified, Fpdf first tries +// to load files using the font loader. If the loading files Fpdf tries to +// load the font from the configured fonts directory (see SetFontLocation). +func (f *Fpdf) SetFontLoader(loader FontLoader) { + f.fontLoader = loader +} + // SetHeaderFunc sets the function that lets the application render the page // header. The specified function is automatically called by AddPage() and // should not be called directly by the application. The implementation in Fpdf @@ -1358,8 +1366,19 @@ func (f *Fpdf) AddFont(familyStr, styleStr, fileStr string) { if fileStr == "" { fileStr = strings.Replace(familyStr, " ", "", -1) + strings.ToLower(styleStr) + ".json" } - fileStr = path.Join(f.fontpath, fileStr) + if f.fontLoader != nil { + reader, err := f.fontLoader.Open(fileStr) + if err == nil { + f.AddFontFromReader(familyStr, styleStr, reader) + if closer, ok := reader.(io.Closer); ok { + closer.Close() + } + return + } + } + + fileStr = path.Join(f.fontpath, fileStr) file, err := os.Open(fileStr) if err != nil { f.err = err @@ -2971,7 +2990,7 @@ func (f *Fpdf) putfonts() { f.newobj() info.n = f.n f.fontFiles[file] = info - font, err := ioutil.ReadFile(path.Join(f.fontpath, file)) + font, err := f.loadFontFile(file) if err != nil { f.err = err return @@ -3071,6 +3090,20 @@ func (f *Fpdf) putfonts() { return } +func (f *Fpdf) loadFontFile(name string) ([]byte, error) { + if f.fontLoader != nil { + reader, err := f.fontLoader.Open(name) + if err == nil { + data, err := ioutil.ReadAll(reader) + if closer, ok := reader.(io.Closer); ok { + closer.Close() + } + return data, err + } + } + return ioutil.ReadFile(path.Join(f.fontpath, name)) +} + func (f *Fpdf) putimages() { for _, img := range f.images { f.putimage(img) -- cgit v1.2.1-24-ge1ad From f0690e7eabd6a1dc812923ce417ebdb7da490f14 Mon Sep 17 00:00:00 2001 From: Kurt Jung Date: Sat, 4 Jul 2015 13:14:40 -0400 Subject: Dashed lines: tutorial example and tip of hat to Claudio Felber --- fpdf.go | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) (limited to 'fpdf.go') diff --git a/fpdf.go b/fpdf.go index b2872e6..e516931 100644 --- a/fpdf.go +++ b/fpdf.go @@ -788,10 +788,14 @@ 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. +// SetDashPattern sets the dash pattern that is used to draw lines. The +// dashArray elements are numbers, in units established in New(), 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. Call this method with an empty array to +// restore solid line drawing. +// +// See tutorial 28 for an example of this function. func (f *Fpdf) SetDashPattern(dashArray []float64, dashPhase float64) { scaled := make([]float64, len(dashArray)) for i, value := range dashArray { -- cgit v1.2.1-24-ge1ad