From a4b0fd17b669b43f74a6baa7e93b71d765b82d97 Mon Sep 17 00:00:00 2001 From: Kurt Date: Fri, 15 Jun 2018 21:44:55 -0400 Subject: Support negative X position when placing image --- fpdf.go | 18 ++++++++++++------ fpdf_test.go | 20 ++++++++++++++++++++ 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/fpdf.go b/fpdf.go index f6ec511..494f1b2 100644 --- a/fpdf.go +++ b/fpdf.go @@ -2421,7 +2421,7 @@ func (f *Fpdf) ImageTypeFromMime(mimeStr string) (tp string) { return } -func (f *Fpdf) imageOut(info *ImageInfoType, x, y, w, h float64, flow bool, link int, linkStr string) { +func (f *Fpdf) imageOut(info *ImageInfoType, x, y, w, h float64, allowNegativeX, flow bool, link int, linkStr string) { // Automatic width and height calculation if needed if w == 0 && h == 0 { // Put image at 96 dpi @@ -2464,8 +2464,10 @@ func (f *Fpdf) imageOut(info *ImageInfoType, x, y, w, h float64, flow bool, link y = f.y f.y += h } - if x < 0 { - x = f.x + if !allowNegativeX { + if x < 0 { + x = f.x + } } // dbg("h %.2f", h) // q 85.04 0 0 NaN 28.35 NaN cm /I2 Do Q @@ -2531,7 +2533,7 @@ func (f *Fpdf) ImageOptions(imageNameStr string, x, y, w, h float64, flow bool, if f.err != nil { return } - f.imageOut(info, x, y, w, h, flow, link, linkStr) + f.imageOut(info, x, y, w, h, options.AllowNegativePosition, flow, link, linkStr) return } @@ -2559,9 +2561,13 @@ func (f *Fpdf) RegisterImageReader(imgName, tp string, r io.Reader) (info *Image // to true (understanding that not all images will have this info // available). However, for backwards compatibility with previous // versions of the API, it defaults to false. +// +// AllowNegativePosition can be set to true in order to prevent the default +// coercion of negative x values to the current x position. type ImageOptions struct { - ImageType string - ReadDpi bool + ImageType string + ReadDpi bool + AllowNegativePosition bool } // RegisterImageOptionsReader registers an image, reading it from Reader r, adding it diff --git a/fpdf_test.go b/fpdf_test.go index 9f0ee96..d9fa429 100644 --- a/fpdf_test.go +++ b/fpdf_test.go @@ -714,6 +714,26 @@ func ExampleFpdf_Image() { // Successfully generated pdf/Fpdf_Image.pdf } +// This example demonstrates how the AllowNegativePosition field of the +// ImageOption struct can be used to affect horizontal image placement. +func ExampleFpdf_ImageOption() { + var opt gofpdf.ImageOptions + + pdf := gofpdf.New("P", "mm", "A4", "") + pdf.AddPage() + pdf.SetFont("Arial", "", 11) + pdf.SetX(60) + opt.ImageType = "png" + pdf.ImageOptions(example.ImageFile("logo.png"), -10, 10, 30, 0, false, opt, 0, "") + opt.AllowNegativePosition = true + pdf.ImageOptions(example.ImageFile("logo.png"), -10, 50, 30, 0, false, opt, 0, "") + fileStr := example.Filename("Fpdf_ImageOption") + err := pdf.OutputFileAndClose(fileStr) + example.Summary(err, fileStr) + // Output: + // Successfully generated pdf/Fpdf_ImageOption.pdf +} + // This examples demonstrates Landscape mode with images. func ExampleFpdf_SetAcceptPageBreakFunc() { var y0 float64 -- cgit v1.2.1-24-ge1ad