From 92858a8b5ff04e4196597d9d517f3b2a0d651579 Mon Sep 17 00:00:00 2001 From: Kurt Jung Date: Sat, 10 Oct 2015 15:24:43 -0400 Subject: Conditionally sort image catalog. Correct small typos. --- compare.go | 23 +++++++++++++++++++++++ def.go | 4 ++-- fpdf.go | 20 ++++++++++++++------ fpdf_test.go | 1 - template.go | 6 +++--- 5 files changed, 42 insertions(+), 12 deletions(-) diff --git a/compare.go b/compare.go index 3fd7eaf..64cad1f 100644 --- a/compare.go +++ b/compare.go @@ -21,8 +21,31 @@ import ( "fmt" "io" "io/ioutil" + "sort" ) +type sortType struct { + length int + less func(int, int) bool + swap func(int, int) +} + +func (s *sortType) Len() int { + return s.length +} + +func (s *sortType) Less(i, j int) bool { + return s.less(i, j) +} + +func (s *sortType) Swap(i, j int) { + s.swap(i, j) +} + +func gensort(Len int, Less func(int, int) bool, Swap func(int, int)) { + sort.Sort(&sortType{length: Len, less: Less, swap: Swap}) +} + func writeBytes(leadStr string, startPos int, sl []byte) { var pos, max int var b byte diff --git a/def.go b/def.go index 2c4d33d..4c99733 100644 --- a/def.go +++ b/def.go @@ -151,7 +151,7 @@ type InitType struct { // from arbitrary locations (e.g. files, zip files, embedded font resources). // // Open provides an io.Reader for the specified font file (.json or .z). The file name -// does never include a path. Open returns an error if the specified file cannot be opened. +// never includes a path. Open returns an error if the specified file cannot be opened. type FontLoader interface { Open(name string) (io.Reader, error) } @@ -162,7 +162,7 @@ type Fpdf struct { n int // current object number offsets []int // array of object offsets templates map[int64]Template // templates used in this document - templateObjects map[int64]int //template object IDs within this document + templateObjects map[int64]int // template object IDs within this document buffer fmtBuffer // buffer holding in-memory PDF pages []*bytes.Buffer // slice[page] of page content; 1-based state int // current document state diff --git a/fpdf.go b/fpdf.go index aeb0c3e..b5ed8e6 100644 --- a/fpdf.go +++ b/fpdf.go @@ -3140,7 +3140,6 @@ func (f *Fpdf) putfonts() { sort.Strings(fileList) } for _, file = range fileList { - // for file, info := range f.fontFiles { info = f.fontFiles[file] // Font file embedding f.newobj() @@ -3352,9 +3351,20 @@ func (f *Fpdf) putimage(info *ImageInfoType) { } func (f *Fpdf) putxobjectdict() { - for _, image := range f.images { - // foreach($this->images as $image) - f.outf("/I%d %d 0 R", image.i, image.n) + { + var image *ImageInfoType + var key string + var keyList []string + for key = range f.images { + keyList = append(keyList, key) + } + if f.catalogSort { + sort.Strings(keyList) + } + for _, key = range keyList { + image = f.images[key] + f.outf("/I%d %d 0 R", image.i, image.n) + } } for _, tpl := range f.templates { id := tpl.ID() @@ -3379,8 +3389,6 @@ func (f *Fpdf) putresourcedict() { } for _, key = range keyList { font = f.fonts[key] - // for _, font := range f.fonts { - // foreach($this->fonts as $font) f.outf("/F%d %d 0 R", font.I, font.N) } } diff --git a/fpdf_test.go b/fpdf_test.go index cc0b91c..2238ccd 100644 --- a/fpdf_test.go +++ b/fpdf_test.go @@ -113,7 +113,6 @@ func Example() { // This example demonsrates the generation of headers, footers and page breaks. func ExampleFpdf_AddPage() { pdf := gofpdf.New("P", "mm", "A4", "") - pdf.SetCatalogSort(true) pdf.SetHeaderFunc(func() { pdf.Image(example.ImageFile("logo.png"), 10, 6, 30, 0, false, "", 0, "") pdf.SetY(5) diff --git a/template.go b/template.go index 7561ae1..342032e 100644 --- a/template.go +++ b/template.go @@ -36,7 +36,7 @@ func CreateTemplate(corner PointType, size SizeType, unitStr, fontDirStr string, // using the size and position at which it was originally written. func (f *Fpdf) UseTemplate(t Template) { if t == nil { - f.SetErrorf("Template is nil") + f.SetErrorf("template is nil") return } corner, size := t.Size() @@ -47,13 +47,13 @@ func (f *Fpdf) UseTemplate(t Template) { // using the given page coordinates. func (f *Fpdf) UseTemplateScaled(t Template, corner PointType, size SizeType) { if t == nil { - f.SetErrorf("Template is nil") + f.SetErrorf("template is nil") return } // You have to add at least a page first if f.page <= 0 { - f.SetErrorf("Cannot use a template without first adding a page") + f.SetErrorf("cannot use a template without first adding a page") return } -- cgit v1.2.1-24-ge1ad