Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes for PDF comparison #484

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
67 changes: 56 additions & 11 deletions internal/cmpimg/cmpimg.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"image"
"image/color"
"image/draw"
"io/ioutil"
"math"
"reflect"
"strings"
Expand Down Expand Up @@ -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
Expand Down
Binary file modified plotter/testdata/polygon_holes_golden.pdf
Binary file not shown.