forked from signintech/gopdf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache_content_rotate.go
42 lines (32 loc) · 896 Bytes
/
cache_content_rotate.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package gopdf
import (
"fmt"
"io"
"math"
)
type cacheContentRotate struct {
isReset bool
pageHeight float64
angle, x, y float64
}
func (cc *cacheContentRotate) write(w io.Writer, protection *PDFProtection) error {
if cc.isReset == true {
if _, err := io.WriteString(w, "Q\n"); err != nil {
return err
}
return nil
}
matrix := computeRotateTransformationMatrix(cc.x, cc.y, cc.angle, cc.pageHeight)
contentStream := fmt.Sprintf("q\n %s", matrix)
if _, err := io.WriteString(w, contentStream); err != nil {
return err
}
return nil
}
func computeRotateTransformationMatrix(x, y, degreeAngle, pageHeight float64) string {
radianAngle := degreeAngle * (math.Pi / 180)
c := math.Cos(radianAngle)
s := math.Sin(radianAngle)
cy := pageHeight - y
return fmt.Sprintf("%.5f %.5f %.5f\n %.5f %.2f %.2f cm\n 1 0 0\n 1 %.2f %.2f cm\n", c, s, -s, c, x, cy, -x, -cy)
}