forked from signintech/gopdf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kern_test.go
83 lines (69 loc) · 1.78 KB
/
kern_test.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
package gopdf
import (
"errors"
"fmt"
"log"
"testing"
)
func TestKern01(t *testing.T) {
Wo, err := kern01("test/res/times.ttf", "times", 'W', 'o')
if err != nil {
t.Error(err)
return
}
if Wo != -80 {
t.Error(fmt.Sprintf("Wo must be -80 (but %d)", Wo))
//return
}
Wi, err := kern01("test/res/times.ttf", "times", 'W', 'i')
if err != nil {
t.Error(err)
return
}
if Wi != -40 {
t.Error(fmt.Sprintf("Wi must be -40 (but %d)", Wi))
//return
}
}
func kern01(font string, prefix string, leftRune rune, rightRune rune) (int, error) {
pdf := GoPdf{}
pdf.Start(Config{Unit: Unit_PT, PageSize: Rect{W: 595.28, H: 841.89}}) //595.28, 841.89 = A4
pdf.AddPage()
err := pdf.AddTTFFontWithOption(prefix, font, TtfOption{
UseKerning: true,
})
if err != nil {
log.Print(err.Error())
return 0, err
}
err = pdf.SetFont(prefix, "", 50)
if err != nil {
log.Print(err.Error())
return 0, err
}
gindexleftRune, err := pdf.curr.Font_ISubset.CharCodeToGlyphIndex(leftRune)
if err != nil {
return 0, err
}
gindexrightRune, err := pdf.curr.Font_ISubset.CharCodeToGlyphIndex(rightRune)
if err != nil {
return 0, err
}
//fmt.Printf("gindexleftRune = %d gindexrightRune=%d \n", gindexleftRune, gindexrightRune)
kernTb := pdf.curr.Font_ISubset.ttfp.Kern()
//fmt.Printf("UnitsPerEm = %d\n", pdf.Curr.Font_ISubset.ttfp.UnitsPerEm())
//fmt.Printf("len =%d\n", len(kernTb.Kerning))
for left, kval := range kernTb.Kerning {
if left == gindexleftRune {
for right, val := range kval {
if right == gindexrightRune {
//fmt.Printf("left=%d right= %d val=%d\n", left, right, val)
valPdfUnit := convertTTFUnit2PDFUnit(int(val), int(pdf.curr.Font_ISubset.ttfp.UnitsPerEm()))
return valPdfUnit, nil
}
}
break
}
}
return 0, errors.New("not found")
}