Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tdewolff committed Sep 16, 2019
1 parent 0768e1d commit 08620d9
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 8 deletions.
4 changes: 2 additions & 2 deletions path_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@ func TestPathCCW(t *testing.T) {
}

func TestPathFilling(t *testing.T) {
FillRule = NonZero
fillings := MustParseSVG("M0 0").Filling()
test.T(t, len(fillings), 0)

Expand All @@ -132,10 +131,10 @@ func TestPathFilling(t *testing.T) {
fillings = MustParseSVG("L10 0L10 10L0 10zM2 2L2 8L8 8L8 2z").Filling() // outer CCW, inner CW
test.T(t, fillings[0], true)
test.T(t, fillings[1], false)
FillRule = NonZero
}

func TestPathInterior(t *testing.T) {
FillRule = NonZero
test.That(t, MustParseSVG("L10 0L10 10L0 10zM2 2L8 2L8 8L2 8z").Interior(1, 1))
test.That(t, MustParseSVG("L10 0L10 10L0 10zM2 2L8 2L8 8L2 8z").Interior(3, 3))
test.That(t, MustParseSVG("L10 0L10 10L0 10zM2 2L2 8L8 8L8 2z").Interior(1, 1))
Expand All @@ -146,6 +145,7 @@ func TestPathInterior(t *testing.T) {
test.That(t, !MustParseSVG("L10 0L10 10L0 10zM2 2L8 2L8 8L2 8z").Interior(3, 3))
test.That(t, MustParseSVG("L10 0L10 10L0 10zM2 2L2 8L8 8L8 2z").Interior(1, 1))
test.That(t, !MustParseSVG("L10 0L10 10L0 10zM2 2L2 8L8 8L8 2z").Interior(3, 3))
FillRule = NonZero
}

func TestPathBounds(t *testing.T) {
Expand Down
14 changes: 9 additions & 5 deletions pdf.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import (
"golang.org/x/image/font"
)

var PDFCompress = true

type pdfWriter struct {
w io.Writer
err error
Expand Down Expand Up @@ -357,12 +359,14 @@ func (w *pdfPageWriter) writePage(parent pdfRef) pdfRef {
if 0 < len(b) && b[0] == ' ' {
b = b[1:]
}
contents := w.pdf.writeObject(pdfStream{
dict: pdfDict{
"Filter": pdfFilterFlate,
},
stream := pdfStream{
dict: pdfDict{},
stream: b,
})
}
if PDFCompress {
stream.dict["Filter"] = pdfFilterFlate
}
contents := w.pdf.writeObject(stream)
return w.pdf.writeObject(pdfDict{
"Type": pdfName("Page"),
"Parent": parent,
Expand Down
44 changes: 44 additions & 0 deletions pdf_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package canvas

import (
"strings"
"testing"

"github.com/tdewolff/test"
)

func TestPDF(t *testing.T) {
c := New(10, 10)
c.DrawPath(0, 0, MustParseSVG("L10 0"))

PDFCompress = false
sb := strings.Builder{}
c.WritePDF(&sb)
test.T(t, sb.String(), `%PDF-1.7
1 0 obj
<< /Length 38 >> stream
0.00000 0.00000 m 10.00000 0.00000 l f
endstream
endobj
2 0 obj
<< /Type /Page /Contents 1 0 R /Group << /Type /Group /CS /DeviceRGB /I true /S /Transparency >> /MediaBox [0.00000 0.00000 10.00000 10.00000] /Parent 2 0 R /Resources << >> >>
endobj
3 0 obj
<< /Type /Pages /Count 1 /Kids [2 0 R] >>
endobj
4 0 obj
<< /Type /Catalog /Pages 3 0 R >>
endobj
xref
0 5
0000000000 65535 f
0000000009 00000 n
0000000097 00000 n
0000000289 00000 n
0000000346 00000 n
trailer
<< /Root 4 0 R /Size 4 >>
starxref
395
%%EOF`)
}
2 changes: 1 addition & 1 deletion polyline.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func (p *Polyline) Interior(x, y float64) bool {
// TODO: write functions when they appear to be needed: Bounds, ConvexHull, Centeroid? IsSimple (ie. non-intersecting)?
// TODO: write function to simplify polyline using Ramer-Douglas-Peucker algorithm

// Smoothen returns a new path that smoothens out a path using cubic Béziers between all the path points. This is equivalent of saying all path commands are linear and are replaced by cubic Béziers so that the curvature at is smooth along the whole path.
// Smoothen returns a new path that smoothens out a path using cubic Béziers between all the path points. It makes sure that the curvature is smooth along the whole path. If the path is closed, it will be smooth between start and end segment too.
func (p *Polyline) Smoothen() *Path {
K := p.coords
if len(K) < 2 {
Expand Down
36 changes: 36 additions & 0 deletions polyline_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package canvas

import (
"testing"

"github.com/tdewolff/test"
)

func TestPolyline(t *testing.T) {
p := &Polyline{}
p.Add(10, 0)
p.Add(20, 10)
test.T(t, len(p.Coords()), 2)
test.T(t, p.Coords()[0], Point{10, 0})
test.T(t, p.Coords()[1], Point{20, 10})

test.T(t, (&Polyline{}).ToPath(), MustParseSVG(""))
test.T(t, (&Polyline{}).Add(10, 0).ToPath(), MustParseSVG(""))
test.T(t, (&Polyline{}).Add(10, 0).Add(20, 10).ToPath(), MustParseSVG("M10 0L20 10"))
test.T(t, (&Polyline{}).Add(10, 0).Add(20, 10).Add(10, 0).ToPath(), MustParseSVG("M10 0L20 10z"))

test.That(t, (&Polyline{}).Add(10, 0).Add(20, 10).Add(10, 10).Add(10, 0).Interior(12, 5))
test.That(t, !(&Polyline{}).Add(10, 0).Add(20, 10).Add(10, 10).Add(10, 0).Interior(5, 5))

FillRule = EvenOdd
test.That(t, (&Polyline{}).Add(10, 0).Add(20, 10).Add(10, 10).Add(10, 0).Interior(12, 5))
test.That(t, !(&Polyline{}).Add(10, 0).Add(20, 10).Add(10, 10).Add(10, 0).Interior(5, 5))
FillRule = NonZero
}

func TestPolylineSmoothen(t *testing.T) {
test.T(t, (&Polyline{}).Smoothen(), MustParseSVG(""))
test.T(t, (&Polyline{}).Add(0, 0).Add(10, 0).Smoothen(), MustParseSVG("M0 0L10 0"))
test.T(t, (&Polyline{}).Add(0, 0).Add(5, 10).Add(10, 0).Add(5, -10).Smoothen(), MustParseSVG("M0 0C1.4444 5.1111 2.8889 10.222 5 10C7.1111 9.7778 9.8889 4.2222 10 0C10.111 -4.2222 7.5556 -7.1111 5 -10"))
test.T(t, (&Polyline{}).Add(0, 0).Add(5, 10).Add(10, 0).Add(5, -10).Add(0, 0).Smoothen(), MustParseSVG("M0 0C0 5 2.5 10 5 10C7.5 10 10 5 10 0C10 -5 7.5 -10 5 -10C2.5 -10 0 -5 0 0z"))
}

0 comments on commit 08620d9

Please sign in to comment.