forked from ajaxray/markpdf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
img_watermark.go
61 lines (54 loc) · 1.67 KB
/
img_watermark.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
package main
import (
"fmt"
"math"
"github.com/unidoc/unidoc/pdf/creator"
)
func drawImage(watermarkImg *creator.Image, c *creator.Creator) {
watermarkImg.SetOpacity(opacity)
watermarkImg.SetAngle(angle)
if tiles {
repeatTiles(watermarkImg, c)
return
}
watermarkImg.SetPos(offsetX, offsetY)
_ = c.Draw(watermarkImg)
}
func adjustImagePosition(watermarkImg *creator.Image, c *creator.Creator) {
debugInfo(fmt.Sprintf("Watermark Width : %v", watermarkImg.Width()))
debugInfo(fmt.Sprintf("Watermark Height : %v", watermarkImg.Height()))
if scaleImage != 100 {
debugInfo(fmt.Sprintf("Scaling to %v%%", scaleImage))
watermarkImg.ScaleToHeight(scaleImage * watermarkImg.Width() / 100)
}
if tiles {
offsetX, offsetY = 0, 0
return
}
if scaleWCenter {
watermarkImg.ScaleToWidth(c.Context().PageWidth)
offsetX = 0
offsetY = (c.Context().PageHeight - watermarkImg.Height()) / 2
} else if scaleHCenter {
watermarkImg.ScaleToHeight(c.Context().PageHeight)
offsetX = (c.Context().PageWidth - watermarkImg.Width()) / 2
offsetY = 0
} else if scaleW {
watermarkImg.ScaleToWidth(c.Context().PageWidth)
offsetX = 0
} else if scaleH {
watermarkImg.ScaleToHeight(c.Context().PageHeight)
offsetY = 0
} else if center {
offsetX = (c.Context().PageWidth - watermarkImg.Width()) / 2
offsetY = (c.Context().PageHeight - watermarkImg.Height()) / 2
}
// None of the above logic is setting negative position
// So, if found, that must be set from command line flags
if offsetX < 0 {
offsetX = c.Context().PageWidth - (watermarkImg.Width() + math.Abs(offsetX))
}
if offsetY < 0 {
offsetY = c.Context().PageHeight - (watermarkImg.Height() + math.Abs(offsetY))
}
}