diff options
author | Kurt Jung <kurt.w.jung@gmail.com> | 2015-10-10 14:16:23 -0400 |
---|---|---|
committer | Kurt Jung <kurt.w.jung@gmail.com> | 2015-10-10 14:16:23 -0400 |
commit | 5847afd8a205853a7effbf749b7a388ae325eb24 (patch) | |
tree | 4842ab4870a4786a3d6d8218e24e71f1978e00dd | |
parent | e924055b983321336e31f556f5235ea79fe72134 (diff) |
Introduce helper functions to set default catalog sort flag and creation date value. Remove regular expression scheme that effectively ignored the embedded creation date when comparing PDFs.
-rw-r--r-- | compare.go | 15 | ||||
-rw-r--r-- | fpdf.go | 42 | ||||
-rw-r--r-- | fpdf_test.go | 3 |
3 files changed, 42 insertions, 18 deletions
@@ -21,15 +21,6 @@ import ( "fmt" "io" "io/ioutil" - "regexp" -) - -var ( - // 00000230 44 46 20 31 2e 37 29 0a 2f 43 72 65 61 74 69 6f |DF 1.7)./Creatio| - // 00000240 6e 44 61 74 65 20 28 44 3a 32 30 31 35 31 30 30 |nDate (D:2015100| - // 00000250 38 31 32 33 30 34 35 29 0a 3e 3e 0a 65 6e 64 6f |8123045).>>.endo| - creationDateRe = regexp.MustCompile("/CreationDate \\(D:\\d{14}\\)") - fixDate = []byte("/CreationDate (D:20000101000000)") ) func writeBytes(leadStr string, startPos int, sl []byte) { @@ -71,15 +62,11 @@ func checkBytes(pos int, sl1, sl2 []byte) (eq bool) { } // compareBytes compares the bytes referred to by sl1 with those referred to by -// sl2. The comparison is done byte-for-byte with the exception of the -// CreationDate fields which are effectively ignored. Nil is returned if the -// buffers are equal, otherwise an error. +// sl2. Nil is returned if the buffers are equal, otherwise an error. func compareBytes(sl1, sl2 []byte) (err error) { var posStart, posEnd, len1, len2, length int var diffs bool - sl1 = creationDateRe.ReplaceAll(sl1, fixDate) - sl2 = creationDateRe.ReplaceAll(sl2, fixDate) len1 = len(sl1) len2 = len(sl2) length = len1 @@ -42,6 +42,11 @@ import ( "time" ) +var gl struct { + catalogSort bool + creationDate time.Time +} + type fmtBuffer struct { bytes.Buffer } @@ -180,6 +185,8 @@ func fpdfNew(orientationStr, unitStr, sizeStr, fontDirStr string, size SizeType) // Set default PDF version number f.pdfVersion = "1.3" f.layerInit() + f.catalogSort = gl.catalogSort + f.creationDate = gl.creationDate return } @@ -2987,6 +2994,13 @@ func (f *Fpdf) outf(fmtStr string, args ...interface{}) { f.out(sprintf(fmtStr, args...)) } +// SetDefaultCatalogSort sets the default value of the catalog sort flag that +// will be used when initializing a new Fpdf instance. See SetCatalogSort() for +// more details. +func SetDefaultCatalogSort(flag bool) { + gl.catalogSort = flag +} + // SetCatalogSort sets a flag that will be used, if true, to consistently order // the document's internal resource catalogs. This method is typically only // used for test purposes. @@ -2994,10 +3008,17 @@ func (f *Fpdf) SetCatalogSort(flag bool) { f.catalogSort = flag } +// SetDefaultCreationDate sets the default value of the document creation date +// that will be used when initializing a new Fpdf instance. See +// SetCreationDate() for more details. +func SetDefaultCreationDate(tm time.Time) { + gl.creationDate = tm +} + // SetCreationDate fixes the document's internal CreationDate value. By // default, the time when the document is generated is used for this value. // This method is typically only used for testing purposes. Specify a -// zero-value time to revert to the default behavior +// zero-value time to revert to the default behavior. func (f *Fpdf) SetCreationDate(tm time.Time) { f.creationDate = tm } @@ -3346,9 +3367,22 @@ func (f *Fpdf) putxobjectdict() { func (f *Fpdf) putresourcedict() { f.out("/ProcSet [/PDF /Text /ImageB /ImageC /ImageI]") f.out("/Font <<") - for _, font := range f.fonts { - // foreach($this->fonts as $font) - f.outf("/F%d %d 0 R", font.I, font.N) + { + var keyList []string + var font fontDefType + var key string + for key = range f.fonts { + keyList = append(keyList, key) + } + if f.catalogSort { + sort.Strings(keyList) + } + for _, key = range keyList { + font = f.fonts[key] + // for _, font := range f.fonts { + // foreach($this->fonts as $font) + f.outf("/F%d %d 0 R", font.I, font.N) + } } f.out(">>") f.out("/XObject <<") diff --git a/fpdf_test.go b/fpdf_test.go index 39cebc0..cc0b91c 100644 --- a/fpdf_test.go +++ b/fpdf_test.go @@ -28,6 +28,7 @@ import ( "path/filepath" "strconv" "strings" + "time" "github.com/jung-kurt/gofpdf" "github.com/jung-kurt/gofpdf/internal/example" @@ -35,6 +36,8 @@ import ( func init() { cleanup() + gofpdf.SetDefaultCatalogSort(true) + gofpdf.SetDefaultCreationDate(time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC)) } func cleanup() { |