diff options
author | Paul Montag <paul.david.montag@gmail.com> | 2018-12-22 18:54:08 -0600 |
---|---|---|
committer | Kurt Jung <kurt.w.jung@gmail.com> | 2018-12-22 19:54:08 -0500 |
commit | a3cf901b6d1246d3e00875a3e88c69bee068156d (patch) | |
tree | 3ae6c45af72e394965ec34fc4bd519b364422786 /def.go | |
parent | 55a774389a811b454a9ee3dfa78bd28fc5d0ab18 (diff) |
Updated font id generation and image id (#222)
Generated font ID will now be the same across machines when generating
the same font file.
Generated image Id will now be the same across machines when generating
the same image file.
Diffstat (limited to 'def.go')
-rw-r--r-- | def.go | 24 |
1 files changed, 21 insertions, 3 deletions
@@ -18,7 +18,10 @@ package gofpdf import ( "bytes" + "crypto/sha1" "encoding/gob" + "encoding/json" + "fmt" "io" "time" ) @@ -164,7 +167,6 @@ func (p PointType) XY() (float64, float64) { type ImageInfoType struct { data []byte smask []byte - i int n int w float64 h float64 @@ -176,11 +178,17 @@ type ImageInfoType struct { trns []int scale float64 // document scaling factor dpi float64 + i string +} + +func generateImageID(info *ImageInfoType) (string, error) { + b, err := info.GobEncode() + return fmt.Sprintf("%x", sha1.Sum(b)), err } // GobEncode encodes the receiving image to a byte slice. func (info *ImageInfoType) GobEncode() (buf []byte, err error) { - fields := []interface{}{info.data, info.smask, info.i, info.n, info.w, info.h, info.cs, + fields := []interface{}{info.data, info.smask, info.n, info.w, info.h, info.cs, info.pal, info.bpc, info.f, info.dp, info.trns, info.scale, info.dpi} w := new(bytes.Buffer) encoder := gob.NewEncoder(w) @@ -196,13 +204,15 @@ func (info *ImageInfoType) GobEncode() (buf []byte, err error) { // GobDecode decodes the specified byte buffer (generated by GobEncode) into // the receiving image. func (info *ImageInfoType) GobDecode(buf []byte) (err error) { - fields := []interface{}{&info.data, &info.smask, &info.i, &info.n, &info.w, &info.h, + fields := []interface{}{&info.data, &info.smask, &info.n, &info.w, &info.h, &info.cs, &info.pal, &info.bpc, &info.f, &info.dp, &info.trns, &info.scale, &info.dpi} r := bytes.NewBuffer(buf) decoder := gob.NewDecoder(r) for j := 0; j < len(fields) && err == nil; j++ { err = decoder.Decode(fields[j]) } + + info.i, err = generateImageID(info) return } @@ -686,6 +696,14 @@ type fontDefType struct { i string // 1-based position in font list, set by font loader, not this program } +// generateFontID generates a font Id from the font definition +func generateFontID(fdt fontDefType) (string, error) { + // file can be different if generated in different instance + fdt.File = "" + b, err := json.Marshal(&fdt) + return fmt.Sprintf("%x", sha1.Sum(b)), err +} + type fontInfoType struct { Data []byte File string |