Skip to content

Commit 9ce6813

Browse files
committedJan 18, 2025
Tweaks
1 parent 0d48a42 commit 9ce6813

File tree

4 files changed

+56
-6
lines changed

4 files changed

+56
-6
lines changed
 

‎helpers_test.go

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package canvas
2+
3+
import "math/rand/v2"
4+
5+
func RandomPath(n int, closed bool) *Path {
6+
p := &Path{}
7+
if 0 < n {
8+
p.d = append(p.d, MoveToCmd, rand.NormFloat64(), rand.NormFloat64(), MoveToCmd)
9+
for i := 1; i < n; i++ {
10+
switch rand.IntN(4) {
11+
case 0:
12+
p.d = append(p.d, LineToCmd, rand.NormFloat64(), rand.NormFloat64(), LineToCmd)
13+
case 1:
14+
p.d = append(p.d, QuadToCmd, rand.NormFloat64(), rand.NormFloat64(), rand.NormFloat64(), rand.NormFloat64(), QuadToCmd)
15+
case 2:
16+
p.d = append(p.d, CubeToCmd, rand.NormFloat64(), rand.NormFloat64(), rand.NormFloat64(), rand.NormFloat64(), rand.NormFloat64(), rand.NormFloat64(), CubeToCmd)
17+
case 3:
18+
large, sweep := rand.IntN(2) == 0, rand.IntN(2) == 0
19+
p.d = append(p.d, ArcToCmd, rand.NormFloat64(), rand.NormFloat64(), rand.NormFloat64(), fromArcFlags(large, sweep), rand.NormFloat64(), rand.NormFloat64(), ArcToCmd)
20+
}
21+
}
22+
if closed {
23+
p.d = append(p.d, CloseCmd, p.d[1], p.d[2], CloseCmd)
24+
}
25+
}
26+
return p
27+
}

‎path_scanner_test.go

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package canvas
2+
3+
import "testing"
4+
5+
func BenchmarkScanner(b *testing.B) {
6+
p := RandomPath(1000, true)
7+
b.Run("Manual", func(b *testing.B) {
8+
for i := 0; i < b.N; i++ {
9+
for j := 0; j < len(p.d); {
10+
j += cmdLen(p.d[j])
11+
_, _ = p.d[j-3], p.d[j-2]
12+
}
13+
}
14+
})
15+
16+
b.Run("Scanner", func(b *testing.B) {
17+
for i := 0; i < b.N; i++ {
18+
for s := p.Scanner(); s.Scan(); {
19+
_ = s.End()
20+
}
21+
}
22+
})
23+
}

‎renderers/pdf/writer.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ func standardFontName(font *canvas.Font) string {
302302

303303
func (w *pdfWriter) getFont(font *canvas.Font, vertical bool) pdfRef {
304304
if standardFont := standardFontName(font); standardFont != "" {
305-
// handle 14 embedded standard fonts in PDF if name and style matches
305+
// handle 14 embedded standard fonts in PDF if name and style match
306306
if ref, ok := w.fontsStd[font]; ok {
307307
return ref
308308
}

‎util.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,11 @@ func (r Rect) H() float64 {
405405
return r.Y1 - r.Y0
406406
}
407407

408+
// Area returns the area of the rectangle.
409+
func (r Rect) Area() float64 {
410+
return (r.X1 - r.X0) * (r.Y1 - r.Y0)
411+
}
412+
408413
// Center returns the center point.
409414
func (r Rect) Center() Point {
410415
return Point{(r.X0 + r.X1) / 2.0, (r.Y0 + r.Y1) / 2.0}
@@ -592,11 +597,6 @@ func (r Rect) And(q Rect) Rect {
592597

593598
}
594599

595-
// Area returns the area of the rectangle.
596-
func (r Rect) Area() float64 {
597-
return (r.X1 - r.X0) * (r.Y1 - r.Y0)
598-
}
599-
600600
// ToPath converts the rectangle to a path.
601601
func (r Rect) ToPath() *Path {
602602
return Rectangle(r.X1-r.X0, r.Y1-r.Y0).Translate(r.X0, r.Y0)

0 commit comments

Comments
 (0)
Please sign in to comment.