-
Notifications
You must be signed in to change notification settings - Fork 0
/
image.go
62 lines (49 loc) · 1007 Bytes
/
image.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package pdfrenderer
import (
"github.com/signintech/gopdf"
)
type Image struct {
size Size
path string
position Vec2
}
func (i *Image) Position(position Vec2) {
i.position = position
}
func (i *Image) GetPosition() Vec2 {
return i.position
}
func (i *Image) Size(width float64, height float64) *Image {
i.size = Size{
Width: width,
Height: height,
}
return i
}
func (i *Image) GetSize() Size {
return i.size
}
func (i *Image) Measure(pdf *gopdf.GoPdf) Size {
imgh, err := gopdf.ImageHolderByPath(i.path)
if err != nil {
panic(err)
}
imgobj := new(gopdf.ImageObj)
err = imgobj.SetImage(imgh)
if err != nil {
panic(err)
}
if i.size.Width == 0 || i.size.Height == 0 {
rect := imgobj.GetRect()
i.size.Width = rect.H
i.size.Height = rect.W
}
return i.size
}
func (i *Image) Draw(pdf *gopdf.GoPdf) {
pdfSize := &gopdf.Rect{W: i.size.Width, H: i.size.Height}
err := pdf.Image(i.path, i.position.X, i.position.Y, pdfSize)
if err != nil {
panic(err)
}
}