forked from ajaxray/markpdf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
text_watermark.go
117 lines (100 loc) · 3.36 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package main
import (
"bytes"
"fmt"
"math"
"text/template"
"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)
// Encountering problem with tiles and text watermark. Contributions welcome.
//if tiles {
// repeatTiles(p, c)
// return
//}
_ = c.Draw(p)
}
func adjustTextPosition(p *creator.Paragraph, c *creator.Creator) {
p.SetTextAlignment(creator.TextAlignmentLeft)
p.SetEnableWrap(false)
if tiles {
p.SetWidth(p.Width()) // Not working without setting it manually
p.SetTextAlignment(creator.TextAlignmentCenter)
offsetX, offsetY = 0, 0
} else 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("Paragraph height: %f", p.Height()))
debugInfo(fmt.Sprintf("Offsets x: %f, y: %f", offsetX, offsetY))
}
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()
}
func isWatermarkATemplate(watermark string) bool {
t := template.Must(template.New("watermark").Parse(watermark))
buf := new(bytes.Buffer)
err := t.Execute(buf, Recipient{0, 0, "out.pdf"})
if err != nil {
fatalIfError(err, fmt.Sprintf("Error parsing the template. [%s]", err))
}
return buf.String() != watermark
}
func applyTemplate(t *template.Template, rec *Recipient, para *creator.Paragraph) {
buf := new(bytes.Buffer)
err := t.Execute(buf, rec)
fatalIfError(err, fmt.Sprintf("Failed to execute watermark template: [%s]", err))
para.SetText(buf.String())
}