diff options
-rw-r--r-- | README.md | 4 | ||||
-rw-r--r-- | def.go | 103 | ||||
-rw-r--r-- | doc.go | 5 | ||||
-rw-r--r-- | fpdf_test.go | 2 | ||||
-rw-r--r-- | template_impl.go | 8 |
5 files changed, 33 insertions, 89 deletions
@@ -221,7 +221,9 @@ account when calculating image size. Paulo Coutinho provided support for static embedded fonts. Dan Meyers added support for embedded JavaScript. David Fish added a generic alias-replacement function to enable, among other things, table of contents functionality. Andy Bakun identified and corrected a problem in -which the internal catalogs were not sorted stably. +which the internal catalogs were not sorted stably. d1ngd0 added encoding and +decoding functionality for templates, including images that are embedded in +templates; this allows templates to be stored independently of gofpdf. ## Roadmap @@ -158,7 +158,9 @@ func (p PointType) XY() (float64, float64) { return p.X, p.Y } -// ImageInfoType contains size, color and other information about an image +// ImageInfoType contains size, color and other information about an image. +// Changes to this structure should be reflected in its GobEncode and GobDecode +// methods. type ImageInfoType struct { data []byte smask []byte @@ -176,99 +178,32 @@ type ImageInfoType struct { dpi float64 } -func (info *ImageInfoType) GobEncode() ([]byte, error) { +// 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, + info.pal, info.bpc, info.f, info.dp, info.trns, info.scale, info.dpi} w := new(bytes.Buffer) encoder := gob.NewEncoder(w) - - err := encoder.Encode(info.data) - if err == nil { - err = encoder.Encode(info.smask) - } - if err == nil { - err = encoder.Encode(info.i) - } - if err == nil { - err = encoder.Encode(info.n) - } - if err == nil { - err = encoder.Encode(info.w) - } - if err == nil { - err = encoder.Encode(info.h) - } - if err == nil { - err = encoder.Encode(info.cs) - } - if err == nil { - err = encoder.Encode(info.pal) - } - if err == nil { - err = encoder.Encode(info.bpc) - } - if err == nil { - err = encoder.Encode(info.f) - } - if err == nil { - err = encoder.Encode(info.dp) + for j := 0; j < len(fields) && err == nil; j++ { + err = encoder.Encode(fields[j]) } if err == nil { - err = encoder.Encode(info.trns) + buf = w.Bytes() } - if err == nil { - err = encoder.Encode(info.scale) - } - if err == nil { - err = encoder.Encode(info.dpi) - } - - return w.Bytes(), err + return } -func (info *ImageInfoType) GobDecode(buf []byte) 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, + &info.cs, &info.pal, &info.bpc, &info.f, &info.dp, &info.trns, &info.scale, &info.dpi} r := bytes.NewBuffer(buf) decoder := gob.NewDecoder(r) - - err := decoder.Decode(&info.data) - if err == nil { - err = decoder.Decode(&info.smask) - } - if err == nil { - err = decoder.Decode(&info.i) - } - if err == nil { - err = decoder.Decode(&info.n) - } - if err == nil { - err = decoder.Decode(&info.w) - } - if err == nil { - err = decoder.Decode(&info.h) - } - if err == nil { - err = decoder.Decode(&info.cs) - } - if err == nil { - err = decoder.Decode(&info.pal) - } - if err == nil { - err = decoder.Decode(&info.bpc) - } - if err == nil { - err = decoder.Decode(&info.f) - } - if err == nil { - err = decoder.Decode(&info.dp) - } - if err == nil { - err = decoder.Decode(&info.trns) - } - if err == nil { - err = decoder.Decode(&info.scale) - } - if err == nil { - err = decoder.Decode(&info.dpi) + for j := 0; j < len(fields) && err == nil; j++ { + err = decoder.Decode(fields[j]) } - return err + return } // PointConvert returns the value of pt, expressed in points (1/72 inch), as a @@ -235,7 +235,10 @@ account when calculating image size. Paulo Coutinho provided support for static embedded fonts. Dan Meyers added support for embedded JavaScript. David Fish added a generic alias-replacement function to enable, among other things, table of contents functionality. Andy Bakun identified and corrected a problem in -which the internal catalogs were not sorted stably. +which the internal catalogs were not sorted stably. d1ngd0 added encoding and +decoding functionality for templates, including images that are embedded in +templates; this allows templates to be stored independently of gofpdf. + Roadmap diff --git a/fpdf_test.go b/fpdf_test.go index 19bc533..68a26ad 100644 --- a/fpdf_test.go +++ b/fpdf_test.go @@ -1957,12 +1957,12 @@ func ExampleFpdf_CreateTemplate() { pdf.AddPage() pdf.UseTemplate(template3) pdf.UseTemplateScaled(template3, gofpdf.PointType{X: 0, Y: 30}, tplSize) - pdf.UseTemplateScaled(template3, gofpdf.PointType{X: 0, Y: 60}, tplSize.ScaleBy(1.4)) pdf.Line(40, 210, 60, 210) pdf.Text(40, 200, "Template example page 1") pdf.AddPage() pdf.UseTemplate(template2) + pdf.UseTemplateScaled(template3, gofpdf.PointType{X: 0, Y: 30}, tplSize.ScaleBy(1.4)) pdf.Line(60, 210, 80, 210) pdf.Text(40, 200, "Template example page 2") diff --git a/template_impl.go b/template_impl.go index 1a91112..c34e688 100644 --- a/template_impl.go +++ b/template_impl.go @@ -82,7 +82,7 @@ func (t *FpdfTpl) Templates() []Template { return t.templates } -// Turn a template into a byte string for later deserialization +// Serialize turns a template into a byte string for later deserialization func (t *FpdfTpl) Serialize() ([]byte, error) { b := new(bytes.Buffer) enc := gob.NewEncoder(b) @@ -91,7 +91,8 @@ func (t *FpdfTpl) Serialize() ([]byte, error) { return b.Bytes(), err } -// Create a template from a previously serialized template +// DeserializeTemplate creaties a template from a previously serialized +// template func DeserializeTemplate(b []byte) (Template, error) { tpl := new(FpdfTpl) dec := gob.NewDecoder(bytes.NewBuffer(b)) @@ -116,6 +117,8 @@ func (t *FpdfTpl) childrenImages() map[string]*ImageInfoType { return childrenImgs } +// GobEncode encodes the receiving template into a byte buffer. Use GobDecode +// to decode the byte buffer back to a template. func (t *FpdfTpl) GobEncode() ([]byte, error) { w := new(bytes.Buffer) encoder := gob.NewEncoder(w) @@ -161,6 +164,7 @@ func (t *FpdfTpl) GobEncode() ([]byte, error) { return w.Bytes(), err } +// GobDecode decodes the specified byte buffer into the receiving template. func (t *FpdfTpl) GobDecode(buf []byte) error { r := bytes.NewBuffer(buf) decoder := gob.NewDecoder(r) |