Skip to content

Commit

Permalink
PDF: write out fonts in order of reference, making output more determ…
Browse files Browse the repository at this point in the history
…inistic
  • Loading branch information
tdewolff committed Apr 20, 2024
1 parent 79842ec commit d5a04e3
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions renderers/pdf/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,22 @@ end`, bfRangeCount, bfRange.String(), bfCharCount, bfChar.String())
w.write("\nendobj\n")
}

func (w *pdfWriter) writeFonts(fontMap map[*canvas.Font]pdfRef, vertical bool) {
// sort fonts by ref to make PDF deterministic
refs := make([]pdfRef, 0, len(fontMap))
refMap := make(map[pdfRef]*canvas.Font, len(fontMap))
for font, ref := range fontMap {
refs = append(refs, ref)
refMap[ref] = font
}
sort.Slice(refs, func(i, j int) bool {
return refs[i] < refs[j]
})
for _, ref := range refs {
w.writeFont(ref, refMap[ref], vertical)
}
}

// Close finished the document.
func (w *pdfWriter) Close() error {
// TODO: support cross reference table streams and compressed objects for all dicts
Expand All @@ -471,12 +487,9 @@ func (w *pdfWriter) Close() error {
kids = append(kids, page)
}

for font, ref := range w.fontsH {
w.writeFont(ref, font, false)
}
for font, ref := range w.fontsV {
w.writeFont(ref, font, true)
}
// write fonts
w.writeFonts(w.fontsH, false)
w.writeFonts(w.fontsV, false)

// document catalog
w.objOffsets[0] = w.pos
Expand Down

0 comments on commit d5a04e3

Please sign in to comment.