From 34b7ba571f23c99aebb68e03e8f9ecc0c1dfb622 Mon Sep 17 00:00:00 2001 From: Alexey Palazhchenko Date: Fri, 24 Nov 2017 15:33:08 +0300 Subject: Rename license.txt to LICENSE (#145) So it is automatically parsed by GitHub --- LICENSE | 23 +++++++++++++++++++++++ license.txt | 23 ----------------------- 2 files changed, 23 insertions(+), 23 deletions(-) create mode 100644 LICENSE delete mode 100644 license.txt diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..eb1abac --- /dev/null +++ b/LICENSE @@ -0,0 +1,23 @@ +MIT License + +Copyright (c) 2013-2016 Kurt Jung (Gmail: kurt.w.jung) + +Portions copyright by the contributors acknowledged in the documentation. + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/license.txt b/license.txt deleted file mode 100644 index eb1abac..0000000 --- a/license.txt +++ /dev/null @@ -1,23 +0,0 @@ -MIT License - -Copyright (c) 2013-2016 Kurt Jung (Gmail: kurt.w.jung) - -Portions copyright by the contributors acknowledged in the documentation. - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. -- cgit v1.2.1-24-ge1ad From 8251b8def6a5b73f826587308bcba926a17b6ea9 Mon Sep 17 00:00:00 2001 From: Kurt Jung Date: Fri, 24 Nov 2017 08:08:42 -0500 Subject: Update LICENSE Adjust the wording so GitHub parses file properly --- LICENSE | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/LICENSE b/LICENSE index eb1abac..946bb79 100644 --- a/LICENSE +++ b/LICENSE @@ -1,8 +1,6 @@ MIT License -Copyright (c) 2013-2016 Kurt Jung (Gmail: kurt.w.jung) - -Portions copyright by the contributors acknowledged in the documentation. +Copyright (c) 2017 Kurt Jung and contributors acknowledged in the documentation Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal -- cgit v1.2.1-24-ge1ad From c6b082a6263bbfcfab0dcc4f4c4bb2c024c84f10 Mon Sep 17 00:00:00 2001 From: Wang Guoliang Date: Fri, 24 Nov 2017 22:43:25 +0800 Subject: Replace strings.Index with strings.Contains --- fpdf.go | 10 +++++----- template.go | 4 +--- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/fpdf.go b/fpdf.go index a590581..1503a4e 100644 --- a/fpdf.go +++ b/fpdf.go @@ -1931,19 +1931,19 @@ func (f *Fpdf) CellFormat(w, h float64, txtStr string, borderStr string, ln int, if len(txtStr) > 0 { var dx, dy float64 // Horizontal alignment - if strings.Index(alignStr, "R") != -1 { + if strings.Contains(alignStr, "R") { dx = w - f.cMargin - f.GetStringWidth(txtStr) - } else if strings.Index(alignStr, "C") != -1 { + } else if strings.Contains(alignStr, "C") { dx = (w - f.GetStringWidth(txtStr)) / 2 } else { dx = f.cMargin } // Vertical alignment - if strings.Index(alignStr, "T") != -1 { + if strings.Contains(alignStr, "T") { dy = (f.fontSize - h) / 2.0 - } else if strings.Index(alignStr, "B") != -1 { + } else if strings.Contains(alignStr, "B") { dy = (h - f.fontSize) / 2.0 - } else if strings.Index(alignStr, "A") != -1 { + } else if strings.Contains(alignStr, "A") { var descent float64 d := f.currentFont.Desc if d.Descent == 0 { diff --git a/template.go b/template.go index 1826fd7..60b076b 100644 --- a/template.go +++ b/template.go @@ -254,9 +254,7 @@ func templateChainDependencies(template Template) []Template { requires := template.Templates() chain := make([]Template, len(requires)*2) for _, req := range requires { - for _, sub := range templateChainDependencies(req) { - chain = append(chain, sub) - } + chain = append(chain, templateChainDependencies(req)...) } chain = append(chain, template) return chain -- cgit v1.2.1-24-ge1ad From 1bef4f81c40b64d68788e590003f4f8fbdc4d296 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20S=C5=82omka?= Date: Sat, 2 Dec 2017 20:28:43 +0100 Subject: Add consts for page orientations (#150) --- def.go | 5 +++++ fpdf.go | 5 +++-- fpdf_test.go | 2 +- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/def.go b/def.go index 88e4dea..52e6474 100644 --- a/def.go +++ b/def.go @@ -39,6 +39,11 @@ type gradientType struct { objNum int } +const ( + OrientationPortrait = "portrait" + OrientationLandscape = "landscape" +) + type colorMode int const ( diff --git a/fpdf.go b/fpdf.go index 1503a4e..567311d 100644 --- a/fpdf.go +++ b/fpdf.go @@ -59,7 +59,9 @@ func (b *fmtBuffer) printf(fmtStr string, args ...interface{}) { func fpdfNew(orientationStr, unitStr, sizeStr, fontDirStr string, size SizeType) (f *Fpdf) { f = new(Fpdf) if orientationStr == "" { - orientationStr = "P" + orientationStr = "p" + } else { + orientationStr = strings.ToLower(orientationStr) } if unitStr == "" { unitStr = "mm" @@ -143,7 +145,6 @@ func fpdfNew(orientationStr, unitStr, sizeStr, fontDirStr string, size SizeType) } f.curPageSize = f.defPageSize // Page orientation - orientationStr = strings.ToLower(orientationStr) switch orientationStr { case "p", "portrait": f.defOrientation = "P" diff --git a/fpdf_test.go b/fpdf_test.go index 76ef558..ff41fd2 100644 --- a/fpdf_test.go +++ b/fpdf_test.go @@ -191,7 +191,7 @@ func lorem() string { // finally retrieved with the output call where it can be handled by the // application. func Example() { - pdf := gofpdf.New("P", "mm", "A4", "") + pdf := gofpdf.New(gofpdf.OrientationPortrait, "mm", "A4", "") pdf.AddPage() pdf.SetFont("Arial", "B", 16) pdf.Cell(40, 10, "Hello World!") -- cgit v1.2.1-24-ge1ad From 9b87a30f0671d13b9d4f76780b41eb9e87883935 Mon Sep 17 00:00:00 2001 From: Dan Meyers Date: Mon, 7 Aug 2017 23:54:14 -0400 Subject: make barcode scaling optional, provide a method to get unscaled barcode dimensions --- contrib/barcode/barcode.go | 64 +++++++++++++++++++++++++++++++++++------ contrib/barcode/barcode_test.go | 51 ++++++++++++++++++++++++++++---- 2 files changed, 102 insertions(+), 13 deletions(-) diff --git a/contrib/barcode/barcode.go b/contrib/barcode/barcode.go index 9f8d3ff..26f99c3 100644 --- a/contrib/barcode/barcode.go +++ b/contrib/barcode/barcode.go @@ -56,12 +56,9 @@ type barcodePdf interface { SetError(err error) } -// Barcode puts a registered barcode in the current page. -// -// The size should be specified in the units used to create the PDF document. -// -// Positioning with x, y and flow is inherited from Fpdf.Image(). -func Barcode(pdf barcodePdf, code string, x, y, w, h float64, flow bool) { +// printBarcode internally prints the scaled or unscaled barcode to the PDF. Used by both +// Barcode() and BarcodeUnscalable(). +func printBarcode(pdf barcodePdf, code string, x, y float64, w, h *float64, flow bool) { barcodes.Lock() unscaled, ok := barcodes.cache[code] barcodes.Unlock() @@ -76,10 +73,18 @@ func Barcode(pdf barcodePdf, code string, x, y, w, h float64, flow bool) { info := pdf.GetImageInfo(bname) if info == nil { + scaleToWidth := unscaled.Bounds().Dx() + scaleToHeight := unscaled.Bounds().Dy() + if w != nil { + scaleToWidth = int(convertTo96Dpi(pdf, *w)) + } + if h != nil { + scaleToHeight = int(convertTo96Dpi(pdf, *h)) + } bcode, err := barcode.Scale( unscaled, - int(convertTo96Dpi(pdf, w)), - int(convertTo96Dpi(pdf, h)), + scaleToWidth, + scaleToHeight, ) if err != nil { @@ -95,6 +100,43 @@ func Barcode(pdf barcodePdf, code string, x, y, w, h float64, flow bool) { } pdf.Image(bname, x, y, 0, 0, flow, "jpg", 0, "") + +} + +// BarcodeUnscalable puts a registered barcode in the current page. +// +// Its arguments work in the same way as that of Barcode(). However, it allows for an unscaled +// barcode in the width and/or height dimensions. This can be useful if you want to prevent +// side effects of upscaling. +func BarcodeUnscalable(pdf barcodePdf, code string, x, y float64, w, h *float64, flow bool) { + printBarcode(pdf, code, x, y, w, h, flow) +} + +// Barcode puts a registered barcode in the current page. +// +// The size should be specified in the units used to create the PDF document. +// If width or height are left unspecfied, the barcode is not scaled in the unspecified dimensions. +// +// Positioning with x, y and flow is inherited from Fpdf.Image(). +func Barcode(pdf barcodePdf, code string, x, y, w, h float64, flow bool) { + printBarcode(pdf, code, x, y, &w, &h, flow) +} + +// GetUnscaledBarcodeDimensions returns the width and height of the +// unscaled barcode associated with the given code. +func GetUnscaledBarcodeDimensions(pdf barcodePdf, code string) (w, h float64) { + barcodes.Lock() + unscaled, ok := barcodes.cache[code] + barcodes.Unlock() + + if !ok { + err := errors.New("Barcode not found") + pdf.SetError(err) + return + } + + return convertFrom96Dpi(pdf, float64(unscaled.Bounds().Dx())), + convertFrom96Dpi(pdf, float64(unscaled.Bounds().Dy())) } // Register registers a barcode but does not put it on the page. Use Barcode() @@ -248,3 +290,9 @@ func registerScaledBarcode(pdf barcodePdf, code string, bcode barcode.Barcode) e func convertTo96Dpi(pdf barcodePdf, value float64) float64 { return value * pdf.GetConversionRatio() / 72 * 96 } + +// convertFrom96Dpi converts the given value, which is based on a 96 DPI value +// required for an Image, to a 72 DPI value like the rest of the PDF document. +func convertFrom96Dpi(pdf barcodePdf, value float64) float64 { + return value / pdf.GetConversionRatio() * 72 / 96 +} diff --git a/contrib/barcode/barcode_test.go b/contrib/barcode/barcode_test.go index 5b6c8cf..0a40a09 100644 --- a/contrib/barcode/barcode_test.go +++ b/contrib/barcode/barcode_test.go @@ -27,7 +27,9 @@ func ExampleRegister() { if err == nil { key := barcode.Register(bcode) - barcode.Barcode(pdf, key, 15, 15, 100, 10, false) + var width float64 = 100 + var height float64 = 10.0 + barcode.BarcodeUnscalable(pdf, key, 15, 15, &width, &height, false) } err = pdf.OutputFileAndClose(fileStr) @@ -40,7 +42,9 @@ func ExampleRegisterCodabar() { pdf := createPdf() key := barcode.RegisterCode128(pdf, "codabar") - barcode.Barcode(pdf, key, 15, 15, 100, 10, false) + var width float64 = 100 + var height float64 = 10 + barcode.BarcodeUnscalable(pdf, key, 15, 15, &width, &height, false) fileStr := example.Filename("contrib_barcode_RegisterCodabar") err := pdf.OutputFileAndClose(fileStr) @@ -118,7 +122,7 @@ func ExampleRegisterQR() { pdf := createPdf() key := barcode.RegisterQR(pdf, "qrcode", qr.H, qr.Unicode) - barcode.Barcode(pdf, key, 15, 15, 20, 20, false) + barcode.Barcode(pdf, key, 15, 15, 100, 10, false) fileStr := example.Filename("contrib_barcode_RegisterQR") err := pdf.OutputFileAndClose(fileStr) @@ -131,7 +135,7 @@ func ExampleRegisterTwoOfFive() { pdf := createPdf() key := barcode.RegisterTwoOfFive(pdf, "1234567895", true) - barcode.Barcode(pdf, key, 15, 15, 100, 20, false) + barcode.Barcode(pdf, key, 15, 15, 100, 10, false) fileStr := example.Filename("contrib_barcode_RegisterTwoOfFive") err := pdf.OutputFileAndClose(fileStr) @@ -144,7 +148,7 @@ func ExampleRegisterPdf417() { pdf := createPdf() key := barcode.RegisterPdf417(pdf, "1234567895", 10, 5) - barcode.Barcode(pdf, key, 15, 15, 100, 20, false) + barcode.Barcode(pdf, key, 15, 15, 100, 10, false) fileStr := example.Filename("contrib_barcode_RegisterPdf417") err := pdf.OutputFileAndClose(fileStr) @@ -158,3 +162,40 @@ func TestRegisterCode128(t *testing.T) { pdf := createPdf() barcode.RegisterCode128(pdf, "Invalid character: é") } + +// Shows that the barcode may be scaled or not by providing optional heights and widths. +func TestBarcodeUnscalable(t *testing.T) { + pdf := createPdf() + + key := barcode.RegisterCode128(pdf, "code128") + var width float64 = 100 + var height float64 = 10 + barcode.BarcodeUnscalable(pdf, key, 15, 15, &width, &height, false) + barcode.BarcodeUnscalable(pdf, key, 15, 35, nil, &height, false) + barcode.BarcodeUnscalable(pdf, key, 15, 55, &width, nil, false) + barcode.BarcodeUnscalable(pdf, key, 15, 75, nil, nil, false) + + fileStr := example.Filename("contrib_barcode_Barcode") + err := pdf.OutputFileAndClose(fileStr) + example.Summary(err, fileStr) + // Output: + // Successfully generated ../../pdf/contrib_barcode_Barcode.pdf +} + +// Shows that the width and height returned by the function match that of the barcode +func TestGetUnscaledBarcodeDimensions(t *testing.T) { + pdf := createPdf() + + key := barcode.RegisterQR(pdf, "qrcode", qr.H, qr.Unicode) + barcode.BarcodeUnscalable(pdf, key, 15, 15, nil, nil, false) + w, h := barcode.GetUnscaledBarcodeDimensions(pdf, key) + + pdf.SetDrawColor(255, 0, 0) + pdf.Line(15, 15, 15+w, 15+h) + + fileStr := example.Filename("contrib_barcode_GetBarcodeDimensions") + err := pdf.OutputFileAndClose(fileStr) + example.Summary(err, fileStr) + // Output: + // Successfully generated ../../pdf/contrib_barcode_GetBarcodeDimensions.pdf +} -- cgit v1.2.1-24-ge1ad