Skip to content

Commit

Permalink
Conditionally order another map iteration. Fix 'go vet' problem with …
Browse files Browse the repository at this point in the history
…unkeyed field in composite literal.
  • Loading branch information
jung-kurt committed Oct 12, 2015
1 parent 13a5fda commit d51e132
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 16 deletions.
15 changes: 2 additions & 13 deletions fpdf.go
Original file line number Diff line number Diff line change
Expand Up @@ -3367,21 +3367,10 @@ func (f *Fpdf) putxobjectdict() {
}
}
{
var key int64
var keyList []int64
var key int64
var tpl Template
for key = range f.templates {
keyList = append(keyList, key)
}
if f.catalogSort {
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]
})
}
keyList = templateKeyList(f.templates, f.catalogSort)
for _, key = range keyList {
tpl = f.templates[key]
// for _, tpl := range f.templates {
Expand Down
4 changes: 2 additions & 2 deletions fpdf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1734,8 +1734,8 @@ func ExampleFpdf_CreateTemplate() {

pdf.AddPage()
pdf.UseTemplate(template)
pdf.UseTemplateScaled(template, gofpdf.PointType{0, 30}, tplSize)
pdf.UseTemplateScaled(template, gofpdf.PointType{0, 60}, tplSize.ScaleBy(1.4))
pdf.UseTemplateScaled(template, gofpdf.PointType{X: 0, Y: 30}, tplSize)
pdf.UseTemplateScaled(template, gofpdf.PointType{X: 0, Y: 60}, tplSize.ScaleBy(1.4))
pdf.Line(40, 210, 60, 210)
pdf.Text(40, 200, "Template example page 1")

Expand Down
29 changes: 28 additions & 1 deletion template.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,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 {
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, true) // FIXME
for _, key = range keyList {
t = templates[key]
tlist := templateChainDependencies(t)
for _, tt := range tlist {
if tt != nil {
Expand Down Expand Up @@ -219,3 +241,8 @@ 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|
// > 0002640 31 20 31 32 20 30 20 52 0a 2f 54 50 4c 31 20 31 |1 12 0 R./TPL1 1|
// < 0002650 35 20 30 20 52 0a 2f 54 50 4c 31 20 31 34 20 30 |5 0 R./TPL1 14 0|
// > 0002650 34 20 30 20 52 0a 2f 54 50 4c 32 20 31 35 20 30 |4 0 R./TPL2 15 0|

0 comments on commit d51e132

Please sign in to comment.