From 55a774389a811b454a9ee3dfa78bd28fc5d0ab18 Mon Sep 17 00:00:00 2001 From: d1ngd0 Date: Mon, 17 Dec 2018 16:26:42 -0600 Subject: Fixed id collisions with fonts when encoding and decoding (#220) - Made font id a string which is a sha1 sum of the contents of the json file. --- fpdf.go | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) (limited to 'fpdf.go') diff --git a/fpdf.go b/fpdf.go index 46fe007..400c83f 100644 --- a/fpdf.go +++ b/fpdf.go @@ -23,6 +23,7 @@ package gofpdf import ( "bytes" + "crypto/sha1" "encoding/binary" "encoding/json" "fmt" @@ -1578,7 +1579,7 @@ func (f *Fpdf) AddFontFromBytes(familyStr, styleStr string, jsonFileBytes, zFile } // search existing encodings - info.I = len(f.fonts) + info.i = fmt.Sprintf("%x", sha1.Sum(jsonFileBytes)) if len(info.Diff) > 0 { n := -1 @@ -1648,7 +1649,6 @@ func (f *Fpdf) AddFontFromReader(familyStr, styleStr string, r io.Reader) { if f.err != nil { return } - info.I = len(f.fonts) if len(info.Diff) > 0 { // Search existing encodings n := -1 @@ -1783,7 +1783,7 @@ func (f *Fpdf) SetFont(familyStr, styleStr string, size float64) { f.fontSize = size / f.k f.currentFont = f.fonts[fontkey] if f.page > 0 { - f.outf("BT /F%d %.2f Tf ET", f.currentFont.I, f.fontSizePt) + f.outf("BT /F%s %.2f Tf ET", f.currentFont.i, f.fontSizePt) } return } @@ -1797,7 +1797,7 @@ func (f *Fpdf) SetFontSize(size float64) { f.fontSizePt = size f.fontSize = size / f.k if f.page > 0 { - f.outf("BT /F%d %.2f Tf ET", f.currentFont.I, f.fontSizePt) + f.outf("BT /F%s %.2f Tf ET", f.currentFont.i, f.fontSizePt) } } @@ -1810,7 +1810,7 @@ func (f *Fpdf) SetFontUnitSize(size float64) { f.fontSizePt = size * f.k f.fontSize = size if f.page > 0 { - f.outf("BT /F%d %.2f Tf ET", f.currentFont.I, f.fontSizePt) + f.outf("BT /F%s %.2f Tf ET", f.currentFont.i, f.fontSizePt) } } @@ -2953,6 +2953,7 @@ func (f *Fpdf) loadfont(r io.Reader) (def fontDefType) { if err != nil { f.err = err } + def.i = fmt.Sprintf("%x", sha1.Sum(buf.Bytes())) // dump(def) return } @@ -3585,11 +3586,11 @@ func (f *Fpdf) putresourcedict() { keyList = append(keyList, key) } if f.catalogSort { - sort.SliceStable(keyList, func(i, j int) bool { return f.fonts[keyList[i]].I < f.fonts[keyList[j]].I }) + sort.SliceStable(keyList, func(i, j int) bool { return f.fonts[keyList[i]].i < f.fonts[keyList[j]].i }) } for _, key = range keyList { font = f.fonts[key] - f.outf("/F%d %d 0 R", font.I, font.N) + f.outf("/F%s %d 0 R", font.i, font.N) } } f.out(">>") -- cgit v1.2.1-24-ge1ad