forked from ajaxray/markpdf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
text_watermark.go
87 lines (73 loc) · 2.43 KB
/
text_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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package main
import (
"fmt"
"math"
"github.com/unidoc/unidoc/pdf/creator"
"github.com/unidoc/unidoc/pdf/model/fonts"
)
var fontList = []string{
"courier", "courier_bold", "courier_oblique", "courier_bold_oblique",
"helvetica", "helvetica_bold", "helvetica_oblique", "helvetica_bold_oblique",
"times", "times_bold", "times_italic", "times_bold_italic",
}
func drawText(p *creator.Paragraph, c *creator.Creator) {
// Change to times bold font (default is helvetica).
p.SetFont(getFontByName(font))
p.SetFontSize(fontSize)
p.SetPos(offsetX, offsetY)
p.SetColor(creator.ColorRGBFromHex("#" + color))
p.SetAngle(angle)
_ = c.Draw(p)
}
func adjustTextPosition(p *creator.Paragraph, c *creator.Creator) {
p.SetTextAlignment(creator.TextAlignmentLeft)
p.SetEnableWrap(false)
if center {
p.SetWidth(p.Width()) // Not working without setting it manually
p.SetTextAlignment(creator.TextAlignmentCenter)
offsetX = (c.Context().PageWidth / 2) - (p.Width() / 2)
offsetY = (c.Context().PageHeight / 2) - (p.Height() / 2)
} else {
if offsetX < 0 {
p.SetWidth(p.Width()) // Not working without setting it manually
p.SetTextAlignment(creator.TextAlignmentRight)
offsetX = c.Context().PageWidth - (math.Abs(offsetX) + p.Width())
}
if offsetY < 0 {
offsetY = c.Context().PageHeight - (math.Abs(offsetY) + p.Height())
}
}
debugInfo(fmt.Sprintf("Paragraph width: %f", p.Width()))
debugInfo(fmt.Sprintf("Paragrapg height: %f", p.Height()))
}
func getFontByName(fontName string) fonts.Font {
switch fontName {
case "courier":
return fonts.NewFontCourier()
case "courier_bold":
return fonts.NewFontCourierBold()
case "courier_oblique":
return fonts.NewFontCourierOblique()
case "courier_bold_oblique":
return fonts.NewFontCourierBoldOblique()
case "helvetica":
return fonts.NewFontHelvetica()
case "helvetica_bold":
return fonts.NewFontHelveticaBold()
case "helvetica_oblique":
return fonts.NewFontHelveticaOblique()
case "helvetica_bold_oblique":
return fonts.NewFontHelveticaBoldOblique()
case "times":
return fonts.NewFontTimesRoman()
case "times_bold":
return fonts.NewFontTimesBold()
case "times_italic":
return fonts.NewFontTimesItalic()
case "times_bold_italic":
return fonts.NewFontTimesBoldItalic()
}
debugInfo("No allowed font name didn't match the allowed list. Using helvetica_bold as default.")
debugInfo(fmt.Sprintf("Allowed font names: %s", fontList))
return fonts.NewFontHelveticaBold()
}