Skip to content

Commit

Permalink
Fix #119
Browse files Browse the repository at this point in the history
  • Loading branch information
hhrutter committed Nov 13, 2019
1 parent 9575f75 commit c961839
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 2 deletions.
31 changes: 31 additions & 0 deletions pkg/api/api_test.go
Expand Up @@ -348,6 +348,37 @@ func TestAddWatermarks(t *testing.T) {
}
}

func TestFontStamps(t *testing.T) {

inFile := filepath.Join(inDir, "empty.pdf")

var sb strings.Builder
for i := 32; i <= 255; i++ {
if i%8 == 0 {
sb.WriteString(fmt.Sprintf("\n%03o ", i))
}
if i == 44 || i == 58 {
sb.WriteString("?")
} else {
sb.WriteRune(rune(i))
}
sb.WriteString(" ")
}

a := sb.String()

wmConf := "Times-Roman\n\n" + a + ", font:Times-Roman, rot:0, scal:0.8 abs, pos:tl"
wm, err := pdf.ParseWatermarkDetails(wmConf, true)
if err != nil {
t.Fatalf("%v\n", err)
}
err = AddWatermarksFile(inFile, "", []string{}, wm, nil)
if err != nil {
t.Fatalf("%v\n", err)
}

}

func TestStampingLifecyle(t *testing.T) {
msg := "TestStampingLifecyle"
inFile := filepath.Join(inDir, "Acroforms2.pdf")
Expand Down
23 changes: 21 additions & 2 deletions pkg/pdfcpu/stamp.go
Expand Up @@ -597,7 +597,7 @@ func (wm *Watermark) calcBoundingBox() {
w = wm.Scale * wm.vp.Width()
wm.ScaledFontSize = wm.calcMinFontSize(w)
}
h := float64(len(wm.TextLines)) * float64(wm.ScaledFontSize)
h := (float64(wm.ScaledFontSize) * 1.156) + float64(len(wm.TextLines)-1)*float64(wm.ScaledFontSize)
wm.bb = Rect(0, 0, w, h)

return
Expand Down Expand Up @@ -671,7 +671,17 @@ func setWatermarkType(s string, wm *Watermark) error {

wm.TextString = ss[0]

for _, l := range strings.Split(ss[0], `\n`) {
bb := []byte{}
for _, r := range ss[0] {
fmt.Printf("%#U %d %x %d %x %0o\n", r, r, r, byte(r), byte(r), byte(r))
bb = append(bb, byte(r))
}
fmt.Printf("% x\n", string(bb))

ss[0] = strings.ReplaceAll(string(bb), "\\n", "\n")
fmt.Printf("ss[0]: % x\n", ss[0])
for _, l := range strings.FieldsFunc(ss[0], func(c rune) bool { return c == 0x0a }) {
fmt.Printf("l: % x\n", l)
wm.TextLines = append(wm.TextLines, l)
}

Expand Down Expand Up @@ -708,10 +718,19 @@ func supportedWatermarkFont(fn string) bool {

func createFontResForWM(xRefTable *XRefTable, wm *Watermark) error {

encDict := Dict(
map[string]Object{
"Type": Name("Encoding"),
"BaseEncoding": Name("WinAnsiEncoding"),
"Differences": Array{Integer(172), Name("Euro")},
},
)

d := NewDict()
d.InsertName("Type", "Font")
d.InsertName("Subtype", "Type1")
d.InsertName("BaseFont", wm.FontName)
d.Insert("Encoding", encDict)

ir, err := xRefTable.IndRefForNewObject(d)
if err != nil {
Expand Down

0 comments on commit c961839

Please sign in to comment.