diff options
Diffstat (limited to 'template.go')
-rw-r--r-- | template.go | 61 |
1 files changed, 53 insertions, 8 deletions
diff --git a/template.go b/template.go index 7561ae1..17354fa 100644 --- a/template.go +++ b/template.go @@ -17,6 +17,10 @@ package gofpdf * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ +import ( + "sort" +) + // CreateTemplate defines a new template using the current page size. func (f *Fpdf) CreateTemplate(fn func(*Tpl)) Template { return newTpl(PointType{0, 0}, f.curPageSize, f.unitStr, f.fontDirStr, fn, f) @@ -36,7 +40,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 +51,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 } @@ -112,7 +116,7 @@ func (f *Fpdf) putTemplates() { filter = "/Filter /FlateDecode " } - templates := sortTemplates(f.templates) + templates := sortTemplates(f.templates, f.catalogSort) var t Template for _, t = range templates { corner, size := t.Size() @@ -135,8 +139,21 @@ func (f *Fpdf) putTemplates() { tTemplates := t.Templates() if len(tImages) > 0 || len(tTemplates) > 0 { f.out("/XObject <<") - for _, ti := range tImages { - f.outf("/I%d %d 0 R", ti.i, ti.n) + { + var key string + var keyList []string + var ti *ImageInfoType + for key = range tImages { + keyList = append(keyList, key) + } + if gl.catalogSort { + sort.Strings(keyList) + } + for _, key = range keyList { + // for _, ti := range tImages { + ti = tImages[key] + f.outf("/I%d %d 0 R", ti.i, ti.n) + } } for _, tt := range tTemplates { id := tt.ID() @@ -161,12 +178,34 @@ func (f *Fpdf) putTemplates() { } } +func templateKeyList(mp map[int64]Template, sort bool) (keyList []int64) { + var key int64 + for key = range mp { + keyList = append(keyList, key) + } + if sort { + gensort(len(keyList), + func(a, b int) bool { + return keyList[a] < keyList[b] + }, + func(a, b int) { + keyList[a], keyList[b] = keyList[b], keyList[a] + }) + } + return +} + // sortTemplates puts templates in a suitable order based on dependices -func sortTemplates(templates map[int64]Template) []Template { +func sortTemplates(templates map[int64]Template, catalogSort bool) []Template { chain := make([]Template, 0, len(templates)*2) // build a full set of dependency chains - for _, t := range templates { + var keyList []int64 + var key int64 + var t Template + keyList = templateKeyList(templates, catalogSort) + for _, key = range keyList { + t = templates[key] tlist := templateChainDependencies(t) for _, tt := range tlist { if tt != nil { @@ -202,3 +241,9 @@ func templateChainDependencies(template Template) []Template { chain = append(chain, template) return chain } + +// < 0002640 31 20 31 32 20 30 20 52 0a 2f 54 50 4c 32 20 31 |1 12 0 R./TPL2 1| +// < 0002650 35 20 30 20 52 0a 2f 54 50 4c 31 20 31 34 20 30 |5 0 R./TPL1 14 0| + +// > 0002640 31 20 31 32 20 30 20 52 0a 2f 54 50 4c 31 20 31 |1 12 0 R./TPL1 1| +// > 0002650 34 20 30 20 52 0a 2f 54 50 4c 32 20 31 35 20 30 |4 0 R./TPL2 15 0| |