From bbaeac8fa0475f2ff753a65b2dff495c5fe2a3ca Mon Sep 17 00:00:00 2001 From: Igor Mikushkin Date: Sun, 25 Nov 2018 15:11:10 +0400 Subject: [PATCH] internal/cmpimg: PDF comparison fixed --- internal/cmpimg/cmpimg.go | 67 ++++++++++++++++++---- plotter/testdata/polygon_holes_golden.pdf | Bin 214258 -> 214250 bytes 2 files changed, 56 insertions(+), 11 deletions(-) diff --git a/internal/cmpimg/cmpimg.go b/internal/cmpimg/cmpimg.go index 3d0c3394..907e9499 100644 --- a/internal/cmpimg/cmpimg.go +++ b/internal/cmpimg/cmpimg.go @@ -12,6 +12,7 @@ import ( "image" "image/color" "image/draw" + "io/ioutil" "math" "reflect" "strings" @@ -81,23 +82,67 @@ func Equal(typ string, raw1, raw2 []byte) (bool, error) { } func cmpPdf(pdf1, pdf2 *pdf.Reader) bool { - n1 := pdf1.NumPage() - n2 := pdf2.NumPage() - if n1 != n2 { + return cmpPdfValues(pdf1.Trailer(), pdf2.Trailer()) +} + +func cmpPdfValues(v1, v2 pdf.Value) bool { + if v1.Kind() != v2.Kind() { return false } - for i := 1; i <= n1; i++ { - p1 := pdf1.Page(i).Content() - p2 := pdf2.Page(i).Content() - if !reflect.DeepEqual(p1, p2) { + switch v1.Kind() { + case pdf.String: + return v1.String() == v2.String() + case pdf.Integer: + return v1.Int64() == v2.Int64() + case pdf.Real: + return v1.Float64() == v2.Float64() + case pdf.Name: + return v1.Name() == v2.Name() + case pdf.Stream: + r1 := v1.Reader() + s1, err1 := ioutil.ReadAll(r1) + r1.Close() + r2 := v2.Reader() + s2, err2 := ioutil.ReadAll(r2) + r2.Close() + if err1 != nil || err2 != nil || len(s1) != len(s2) { + return false + } + if !bytes.Equal(s1, s2) { + return false + } + fallthrough + case pdf.Dict: + keys1, keys2 := v1.Keys(), v2.Keys() + if len(keys1) != len(keys2) { + return false + } + for i, k := range keys1 { + if k != keys2[i] { + return false + } + if k == "CreationDate" || k == "Parent" || k == "Font" { + continue + } + if !cmpPdfValues(v1.Key(k), v2.Key(k)) { + return false + } + } + return true + case pdf.Array: + if v1.Len() != v2.Len() { return false } + count := v1.Len() + for i := 0; i < count; i++ { + if !cmpPdfValues(v1.Index(i), v2.Index(i)) { + return false + } + } + return true } - - t1 := pdf1.Trailer().String() - t2 := pdf2.Trailer().String() - return t1 == t2 + return false } // Diff calculates an intensity-scaled difference between images a and b diff --git a/plotter/testdata/polygon_holes_golden.pdf b/plotter/testdata/polygon_holes_golden.pdf index 58dd95ad3a10b1f198a40bf4244b19da3adc8a2b..dc7608fa17f134562c8b19410ee24ac223354d41 100644 GIT binary patch delta 916 zcmey=$@{94cS2pgnWeFv9anKlQEFl?R|VsqFyDM-bDq7g!#~`RITI8=rG$3^+l!U! zo?n=E`AA+_TJsw{fhYI%SM*POd&J{SMe(HjHj_S|5X=<%vy$W8R5zuHpcWUF>tAD5 zANM)2`HaBXH-huSuO|0grgZvXi0H!IKgMa4h)mgX2#{Iufkp15`E{(zVEu@S4Y z&ON;wTDoH9$pcQ+DVOHi>zOIczFWU()5+AOdf(ozKFe3TWr@A(OiQ=OPnVXv%wKe- zG__^k1J6h|J^`6FhG5q-O>SF*WF|a1BX;Gch>F{1-4&bTl6eF?l6*x=&7@2+)3{&d z1crY2qVy^G<=>diQG3cxmFga`>G`#O_MN%M`MKX4Se%#I;`Uxvy>rX|WB;CMt+c%% zS>K({*IoZxJZj#xmRpN2@8n#2^g*&k^PSHoiQ&&=qt3tN+5Bd~rp2>1Xmj&T{w`ww zuajTvc9v7Jfn-8&T-uzaeM^3Gz5aB0+YIDRI&(>Er$XZP zQj=}&=f3Z{^X^jFhupuX4)0$dEx!HDrzM&hIkt1_cedTK?s}@1HBI`d%q)g^7f&6z z<+k_}XY867=e~(dJy(mwr)n&|wPJ>WU_|_~t7cN61t(S(@k;jlEfPB&8?wmb%Jkb^ zKJArnrY(8KSYBcnX1a3Hv67-+t7o|h+RdHw;nmdYf)_<9JKgs(NmgptyM4CW!T$VL z%I&teXwL1r%evg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