From 5d0bba740b8c3f5619d5a39b724f4d59e99fe4b4 Mon Sep 17 00:00:00 2001 From: Franz Fangmeyer Date: Sat, 7 Sep 2019 12:14:28 +0200 Subject: Strikeout: use "S" as styleStr --- def.go | 1 + fpdf.go | 28 +++++++++++++++++++++++++--- fpdf_test.go | 26 ++++++++++++++++++++++---- internal/example/example.go | 3 +-- ttfparser_test.go | 5 ++--- 5 files changed, 51 insertions(+), 12 deletions(-) diff --git a/def.go b/def.go index 7fddc82..bff23e7 100644 --- a/def.go +++ b/def.go @@ -542,6 +542,7 @@ type Fpdf struct { fontFamily string // current font family fontStyle string // current font style underline bool // underlining flag + strikeout bool // strike out flag currentFont fontDefType // current font info fontSizePt float64 // current font size in points fontSize float64 // current font size in user unit diff --git a/fpdf.go b/fpdf.go index f77717f..1ef2b1f 100644 --- a/fpdf.go +++ b/fpdf.go @@ -102,6 +102,7 @@ func fpdfNew(orientationStr, unitStr, sizeStr, fontDirStr string, size SizeType) f.fontStyle = "" f.SetFontSize(12) f.underline = false + f.strikeout = false f.setDrawColor(0, 0, 0) f.setFillColor(0, 0, 0) f.setTextColor(0, 0, 0) @@ -726,6 +727,9 @@ func (f *Fpdf) AddPageFormat(orientationStr string, size SizeType) { if f.underline { style += "U" } + if f.strikeout { + style += "S" + } fontsize := f.fontSizePt lw := f.lineWidth dc := f.color.draw @@ -2004,9 +2008,9 @@ func (f *Fpdf) GetFontDesc(familyStr, styleStr string) FontDescType { // insensitive): "Courier" for fixed-width, "Helvetica" or "Arial" for sans // serif, "Times" for serif, "Symbol" or "ZapfDingbats" for symbolic. // -// styleStr can be "B" (bold), "I" (italic), "U" (underscore) or any -// combination. The default value (specified with an empty string) is regular. -// Bold and italic styles do not apply to Symbol and ZapfDingbats. +// styleStr can be "B" (bold), "I" (italic), "U" (underscore), "S" (strike-out) +// or any combination. The default value (specified with an empty string) is +// regular. Bold and italic styles do not apply to Symbol and ZapfDingbats. // // size is the font size measured in points. The default value is the current // size. If no size has been specified since the beginning of the document, the @@ -2029,6 +2033,10 @@ func (f *Fpdf) SetFont(familyStr, styleStr string, size float64) { if f.underline { styleStr = strings.Replace(styleStr, "U", "", -1) } + f.strikeout = strings.Contains(styleStr, "S") + if f.strikeout { + styleStr = strings.Replace(styleStr, "S", "", -1) + } if styleStr == "IB" { styleStr = "BI" } @@ -2198,6 +2206,9 @@ func (f *Fpdf) Text(x, y float64, txtStr string) { if f.underline && txtStr != "" { s += " " + f.dounderline(x, y, txtStr) } + if f.strikeout && txtStr != "" { + s += " " + f.dostrikeout(x, y, txtStr) + } if f.colorFlag { s = sprintf("q %s %s Q", f.color.text.str, s) } @@ -2422,6 +2433,9 @@ func (f *Fpdf) CellFormat(w, h float64, txtStr, borderStr string, ln int, if f.underline { s.printf(" %s", f.dounderline(f.x+dx, f.y+dy+.5*h+.3*f.fontSize, txtStr)) } + if f.strikeout { + s.printf(" %s", f.dostrikeout(f.x+dx, f.y+dy+.5*h+.3*f.fontSize, txtStr)) + } if f.colorFlag { s.printf(" Q") } @@ -3554,6 +3568,14 @@ func (f *Fpdf) dounderline(x, y float64, txt string) string { (f.h-(y-up/1000*f.fontSize))*f.k, w*f.k, -ut/1000*f.fontSizePt) } +func (f *Fpdf) dostrikeout(x, y float64, txt string) string { + up := float64(f.currentFont.Up) + ut := float64(f.currentFont.Ut) + w := f.GetStringWidth(txt) + f.ws*float64(blankCount(txt)) + return sprintf("%.2f %.2f %.2f %.2f re f", x*f.k, + (f.h-(y+4*up/1000*f.fontSize))*f.k, w*f.k, -ut/1000*f.fontSizePt) +} + func bufEqual(buf []byte, str string) bool { return string(buf[0:len(str)]) == str } diff --git a/fpdf_test.go b/fpdf_test.go index 43cea30..267bc6c 100644 --- a/fpdf_test.go +++ b/fpdf_test.go @@ -20,6 +20,9 @@ import ( "bufio" "bytes" "fmt" + "gofpdf" + "gofpdf/internal/example" + "gofpdf/internal/files" "io" "io/ioutil" "math" @@ -31,10 +34,6 @@ import ( "strings" "testing" "time" - - "github.com/jung-kurt/gofpdf" - "github.com/jung-kurt/gofpdf/internal/example" - "github.com/jung-kurt/gofpdf/internal/files" ) func init() { @@ -2711,3 +2710,22 @@ func ExampleFpdf_SetUnderlineThickness() { // Output: // Successfully generated pdf/Fpdf_UnderlineThickness.pdf } + +// ExampleFpdf_strikeout demonstrates striked-out text +func ExampleFpdf_strikeout() { + + pdf := gofpdf.New("P", "mm", "A4", "") // 210mm x 297mm + pdf.AddPage() + + for fontSize := 4; fontSize < 40; fontSize += 10 { + pdf.SetFont("Arial", "S", float64(fontSize)) + pdf.SetXY(0, float64(fontSize)) + pdf.Cell(40, 10, "Hello World\n") + } + + fileStr := example.Filename("Fpdf_strikeout") + err := pdf.OutputFileAndClose(fileStr) + example.Summary(err, fileStr) + // Output: + // Successfully generated pdf/Fpdf_strikeout.pdf +} diff --git a/internal/example/example.go b/internal/example/example.go index 05e4827..3365931 100644 --- a/internal/example/example.go +++ b/internal/example/example.go @@ -19,12 +19,11 @@ package example import ( "fmt" + "gofpdf" "os" "path/filepath" "strings" "time" - - "github.com/jung-kurt/gofpdf" ) var gofpdfDir string diff --git a/ttfparser_test.go b/ttfparser_test.go index 3286db2..b157d98 100644 --- a/ttfparser_test.go +++ b/ttfparser_test.go @@ -19,9 +19,8 @@ package gofpdf_test import ( "bytes" "fmt" - - "github.com/jung-kurt/gofpdf" - "github.com/jung-kurt/gofpdf/internal/example" + "gofpdf" + "gofpdf/internal/example" ) func ExampleTtfParse() { -- cgit v1.2.1-24-ge1ad From e123b55aba2106cced7941e31391fff7e59d200d Mon Sep 17 00:00:00 2001 From: Kurt Date: Sat, 7 Sep 2019 13:01:30 -0400 Subject: Adjust various import paths, rename example function --- fpdf.go | 2 +- fpdf_test.go | 17 +++++++++-------- go.mod | 7 ++----- internal/example/example.go | 3 ++- ttfparser_test.go | 5 +++-- 5 files changed, 17 insertions(+), 17 deletions(-) diff --git a/fpdf.go b/fpdf.go index 2cad2e9..7be0117 100644 --- a/fpdf.go +++ b/fpdf.go @@ -2472,7 +2472,7 @@ func reverseText(text string) string { } // Cell is a simpler version of CellFormat with no fill, border, links or -// special alignment. +// special alignment. The Cell_strikeout() example demonstrates this method. func (f *Fpdf) Cell(w, h float64, txtStr string) { f.CellFormat(w, h, txtStr, "", 0, "L", false, 0, "") } diff --git a/fpdf_test.go b/fpdf_test.go index 267bc6c..5035bd8 100644 --- a/fpdf_test.go +++ b/fpdf_test.go @@ -20,9 +20,6 @@ import ( "bufio" "bytes" "fmt" - "gofpdf" - "gofpdf/internal/example" - "gofpdf/internal/files" "io" "io/ioutil" "math" @@ -34,6 +31,10 @@ import ( "strings" "testing" "time" + + "github.com/jung-kurt/gofpdf" + "github.com/jung-kurt/gofpdf/internal/example" + "github.com/jung-kurt/gofpdf/internal/files" ) func init() { @@ -2711,8 +2712,8 @@ func ExampleFpdf_SetUnderlineThickness() { // Successfully generated pdf/Fpdf_UnderlineThickness.pdf } -// ExampleFpdf_strikeout demonstrates striked-out text -func ExampleFpdf_strikeout() { +// ExampleFpdf_Cell_strikeout demonstrates striked-out text +func ExampleFpdf_Cell_strikeout() { pdf := gofpdf.New("P", "mm", "A4", "") // 210mm x 297mm pdf.AddPage() @@ -2720,12 +2721,12 @@ func ExampleFpdf_strikeout() { for fontSize := 4; fontSize < 40; fontSize += 10 { pdf.SetFont("Arial", "S", float64(fontSize)) pdf.SetXY(0, float64(fontSize)) - pdf.Cell(40, 10, "Hello World\n") + pdf.Cell(40, 10, "Hello World") } - fileStr := example.Filename("Fpdf_strikeout") + fileStr := example.Filename("Fpdf_Cell_strikeout") err := pdf.OutputFileAndClose(fileStr) example.Summary(err, fileStr) // Output: - // Successfully generated pdf/Fpdf_strikeout.pdf + // Successfully generated pdf/Fpdf_Cell_strikeout.pdf } diff --git a/go.mod b/go.mod index cbb6a8c..f11d98c 100644 --- a/go.mod +++ b/go.mod @@ -4,13 +4,10 @@ go 1.12 require ( github.com/boombuler/barcode v1.0.0 - github.com/davecgh/go-spew v1.1.1 // indirect - github.com/kr/pretty v0.1.0 // indirect github.com/phpdave11/gofpdi v1.0.7 github.com/ruudk/golang-pdf417 v0.0.0-20181029194003-1af4ab5afa58 - github.com/stretchr/testify v1.4.0 // indirect + gofpdf v0.0.0-00010101000000-000000000000 golang.org/x/image v0.0.0-20190902063713-cb417be4ba39 - gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect ) -replace github.com/jung-kurt/gopdf => ./ +replace gofpdf => ./ diff --git a/internal/example/example.go b/internal/example/example.go index 3365931..05e4827 100644 --- a/internal/example/example.go +++ b/internal/example/example.go @@ -19,11 +19,12 @@ package example import ( "fmt" - "gofpdf" "os" "path/filepath" "strings" "time" + + "github.com/jung-kurt/gofpdf" ) var gofpdfDir string diff --git a/ttfparser_test.go b/ttfparser_test.go index b157d98..3286db2 100644 --- a/ttfparser_test.go +++ b/ttfparser_test.go @@ -19,8 +19,9 @@ package gofpdf_test import ( "bytes" "fmt" - "gofpdf" - "gofpdf/internal/example" + + "github.com/jung-kurt/gofpdf" + "github.com/jung-kurt/gofpdf/internal/example" ) func ExampleTtfParse() { -- cgit v1.2.1-24-ge1ad