Skip to content
This repository has been archived by the owner on Nov 13, 2021. It is now read-only.

Commit

Permalink
Updated font id generation and image id (#222)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
d1ngd0 authored and jung-kurt committed Dec 23, 2018
1 parent 55a7743 commit a3cf901
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 11 deletions.
24 changes: 21 additions & 3 deletions def.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ package gofpdf

import (
"bytes"
"crypto/sha1"
"encoding/gob"
"encoding/json"
"fmt"
"io"
"time"
)
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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
}

Expand Down Expand Up @@ -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
Expand Down
23 changes: 16 additions & 7 deletions fpdf.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ package gofpdf

import (
"bytes"
"crypto/sha1"
"encoding/binary"
"encoding/json"
"fmt"
Expand Down Expand Up @@ -1578,9 +1577,12 @@ func (f *Fpdf) AddFontFromBytes(familyStr, styleStr string, jsonFileBytes, zFile
return
}

// search existing encodings
info.i = fmt.Sprintf("%x", sha1.Sum(jsonFileBytes))
if info.i, err = generateFontID(info); err != nil {
f.err = err
return
}

// search existing encodings
if len(info.Diff) > 0 {
n := -1

Expand Down Expand Up @@ -2523,7 +2525,7 @@ func (f *Fpdf) imageOut(info *ImageInfoType, x, y, w, h float64, allowNegativeX,
}
// dbg("h %.2f", h)
// q 85.04 0 0 NaN 28.35 NaN cm /I2 Do Q
f.outf("q %.5f 0 0 %.5f %.5f %.5f cm /I%d Do Q", w*f.k, h*f.k, x*f.k, (f.h-(y+h))*f.k, info.i)
f.outf("q %.5f 0 0 %.5f %.5f %.5f cm /I%s Do Q", w*f.k, h*f.k, x*f.k, (f.h-(y+h))*f.k, info.i)
if link > 0 || len(linkStr) > 0 {
f.newLink(x, y, w, h, link, linkStr)
}
Expand Down Expand Up @@ -2660,7 +2662,10 @@ func (f *Fpdf) RegisterImageOptionsReader(imgName string, options ImageOptions,
if f.err != nil {
return
}
info.i = len(f.images) + 1

if info.i, f.err = generateImageID(info); f.err != nil {
return
}
f.images[imgName] = info

return
Expand Down Expand Up @@ -2952,8 +2957,12 @@ func (f *Fpdf) loadfont(r io.Reader) (def fontDefType) {
err = json.Unmarshal(buf.Bytes(), &def)
if err != nil {
f.err = err
return
}

if def.i, err = generateFontID(def); err != nil {
f.err = err
}
def.i = fmt.Sprintf("%x", sha1.Sum(buf.Bytes()))
// dump(def)
return
}
Expand Down Expand Up @@ -3556,7 +3565,7 @@ func (f *Fpdf) putxobjectdict() {
}
for _, key = range keyList {
image = f.images[key]
f.outf("/I%d %d 0 R", image.i, image.n)
f.outf("/I%s %d 0 R", image.i, image.n)
}
}
{
Expand Down
2 changes: 1 addition & 1 deletion template.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ func (f *Fpdf) putTemplates() {
for _, key = range keyList {
// for _, ti := range tImages {
ti = tImages[key]
f.outf("/I%d %d 0 R", ti.i, ti.n)
f.outf("/I%s %d 0 R", ti.i, ti.n)
}
}
for _, tt := range tTemplates {
Expand Down

0 comments on commit a3cf901

Please sign in to comment.