diff options
author | Kurt Jung <kurt.w.jung@code.google.com> | 2013-11-07 20:49:59 -0500 |
---|---|---|
committer | Kurt Jung <kurt.w.jung@code.google.com> | 2013-11-07 20:49:59 -0500 |
commit | c99f8c8fbf0124fcd2f509132edad09349aa3710 (patch) | |
tree | e44e5f17d2d48a68259c7b81253e6f4a4f4cfce9 | |
parent | 4188061a94636ec37b26813d79824789f0deb7d5 (diff) |
Lawrence Kesteloot provided code to allow an image's extent to be determined prior to placement.
-rw-r--r-- | def.go | 14 | ||||
-rw-r--r-- | doc.go | 4 | ||||
-rw-r--r-- | fpdf.go | 36 | ||||
-rw-r--r-- | fpdf_test.go | 50 |
4 files changed, 84 insertions, 20 deletions
@@ -61,16 +61,22 @@ type ImageInfoType struct { f string dp string trns []int + scale float64 // document scaling factor +} + +// The width and height of the image in the units of the Fpdf object. +func (info *ImageInfoType) Extent() (wd, ht float64) { + return info.w / info.scale, info.h / info.scale } // The width of the image in the units of the Fpdf object. -func (info *ImageInfoType) Width(f *Fpdf) float64 { - return info.w / f.k +func (info *ImageInfoType) Width() float64 { + return info.w / info.scale } // The height of the image in the units of the Fpdf object. -func (info *ImageInfoType) Height(f *Fpdf) float64 { - return info.h / f.k +func (info *ImageInfoType) Height() float64 { + return info.h / info.scale } type fontFileType struct { @@ -25,7 +25,9 @@ transparency script by Martin Hall-May. Support for gradients and clipping is adapted from FPDF scripts by Andreas Würmser. Support for outline bookmarks is adapted from Olivier Plathey by Manuel Cornes. Support for transformations is adapted from the FPDF transformation script by Moritz Wagner and Andreas -Würmser. Bruno Michel has provided valuable assistance with the code. +Würmser. Lawrence Kesteloot provided code to allow an image's extent to be +determined prior to placement. Bruno Michel has provided valuable assistance +with the code. The FPDF website is http://www.fpdf.org/. @@ -1910,10 +1910,13 @@ func (f *Fpdf) Image(fileStr string, x, y, w, h float64, flow bool, tp string, l return } -// Registers an image, adding it to the PDF file but not adding it to the page. Use -// Image() with the same filename to add the image to the page. Note that Image() -// calls this function, so this function is only necessary if you need information -// about the image. See Image() for restrictions on the image and the "tp" parameters. +// Registers an image, adding it to the PDF file but not adding it to the page. +// Use Image() with the same filename to add the image to the page. Note that +// Image() calls this function, so this function is only necessary if you need +// information about the image before placing it. See Image() for restrictions +// on the image and the "tp" parameters. +// +// See tutorial 18 for an example of this function. func (f *Fpdf) RegisterImage(fileStr, tp string) (info *ImageInfoType) { info, ok := f.images[fileStr] if !ok { @@ -2145,11 +2148,14 @@ func be16(buf []byte) int { return 256*int(buf[0]) + int(buf[1]) } +func (f *Fpdf) newImageInfo() *ImageInfoType { + return &ImageInfoType{scale: f.k} +} + // Extract info from a JPEG file // Thank you, Bruno Michel, for providing this code. func (f *Fpdf) parsejpg(fileStr string) (info *ImageInfoType) { - info = &ImageInfoType{} - + info = f.newImageInfo() var err error info.data, err = ioutil.ReadFile(fileStr) if err != nil { @@ -2204,8 +2210,7 @@ func (f *Fpdf) readByte(buf *bytes.Buffer) (val byte) { } func (f *Fpdf) parsepngstream(buf *bytes.Buffer) (info *ImageInfoType) { - info = &ImageInfoType{} - + info = f.newImageInfo() // Check signature if string(buf.Next(8)) != "\x89PNG\x0d\x0a\x1a\x0a" { f.err = fmt.Errorf("Not a PNG buffer") @@ -2692,13 +2697,14 @@ func (f *Fpdf) putimage(info *ImageInfoType) { // Soft mask if len(info.smask) > 0 { smask := &ImageInfoType{ - w: info.w, - h: info.h, - cs: "DeviceGray", - bpc: 8, - f: info.f, - dp: sprintf("/Predictor 15 /Colors 1 /BitsPerComponent 8 /Columns %d", int(info.w)), - data: info.smask, + w: info.w, + h: info.h, + cs: "DeviceGray", + bpc: 8, + f: info.f, + dp: sprintf("/Predictor 15 /Colors 1 /BitsPerComponent 8 /Columns %d", int(info.w)), + data: info.smask, + scale: f.k, } f.putimage(smask) } diff --git a/fpdf_test.go b/fpdf_test.go index ccdf1bf..b683b9c 100644 --- a/fpdf_test.go +++ b/fpdf_test.go @@ -22,6 +22,7 @@ import ( "fmt" "io/ioutil" "os" + "path/filepath" "regexp" "strings" ) @@ -1083,3 +1084,52 @@ func ExampleFpdf_tutorial17() { // Output: // Successfully generated pdf/tutorial17.pdf } + +// Example to demonstrate Lawrence Kesteloot's image registration code. +func ExampleFpdf_tutorial18() { + const ( + margin = 10 + wd = 210 + ht = 297 + ) + fileList := []string{ + "logo-gray.png", + "logo.jpg", + "logo.png", + "logo-rgb.png", + "logo-progressive.jpg", + } + var infoPtr *gofpdf.ImageInfoType + var fileStr string + var imgWd, imgHt, lf, tp float64 + pdf := gofpdf.New("P", "mm", "A4", FONT_DIR) + pdf.AddPage() + pdf.SetMargins(10, 10, 10) + pdf.SetFont("Helvetica", "", 15) + for j, str := range fileList { + fileStr = filepath.Join(IMG_DIR, str) + infoPtr = pdf.RegisterImage(fileStr, "") + imgWd, imgHt = infoPtr.Extent() + switch j { + case 0: + lf = margin + tp = margin + case 1: + lf = wd - margin - imgWd + tp = margin + case 2: + lf = (wd - imgWd) / 2.0 + tp = (ht - imgHt) / 2.0 + case 3: + lf = margin + tp = ht - imgHt - margin + case 4: + lf = wd - imgWd - margin + tp = ht - imgHt - margin + } + pdf.Image(fileStr, lf, tp, imgWd, imgHt, false, "", 0, "") + } + pdf.OutputAndClose(docWriter(pdf, 18)) + // Output: + // Successfully generated pdf/tutorial18.pdf +} |