forked from signintech/gopdf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fontsizefloat64_test.go
132 lines (120 loc) · 3.7 KB
/
fontsizefloat64_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
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package gopdf
import (
"fmt"
"testing"
)
func TestContentObjCalTextHeight(t *testing.T) {
intfontsize := 7
have := ContentObjCalTextHeight(intfontsize)
want := float64(intfontsize) * 0.7
if have != want {
t.Errorf("ContentObjCalTextHeight(%d) = %f; want %f\n", intfontsize, have, want)
}
floatfontsize := 7.2
have = ContentObjCalTextHeightPrecise(floatfontsize)
want = float64(floatfontsize) * 0.7
if have != want {
t.Errorf("ContentObjCalTextHeight(%d) = %f; want %f\n", intfontsize, have, want)
}
}
func TestSetFontCheckGetX(t *testing.T) {
prefix := "Afont"
font := "test/res/LiberationSerif-Regular.ttf"
pdf := GoPdf{}
pdf.Start(Config{Unit: UnitPT, PageSize: Rect{W: 595.28, H: 841.89}})
pdf.AddPage()
if err := pdf.AddTTFFontWithOption(prefix, font, TtfOption{UseKerning: true}); err != nil {
t.Error(err)
}
// Baseline: SetFont(int) + AddText
if err := pdf.SetFont(prefix, "", int(50)); err != nil {
t.Error(err)
}
pdf.SetXY(30.0, 30.0)
pdf.Text(prefix)
wantx, wanty := pdf.GetX(), pdf.GetY()
// ensure that GetX, GetY work as expected
pdf.SetXY(30.0, 30.0)
pdf.Text(prefix + prefix)
havex, havey := pdf.GetX(), pdf.GetY()
if havex <= wantx || havey != wanty {
t.Errorf("wanted (x,y) => (x', y') with x<x' and y==y', got (%f, %f) => (%f, %f)\n", wantx, wanty, havex, havey)
}
}
func moveAndAdd(pdf *GoPdf, prefix string) (float64, float64) {
pdf.SetXY(30.0, 30.0)
pdf.Text(prefix)
return pdf.GetX(), pdf.GetY()
}
func setup(t *testing.T) (string, *GoPdf, float64, float64) {
prefix := "Afont"
font := "test/res/LiberationSerif-Regular.ttf"
pdf := GoPdf{}
pdf.Start(Config{Unit: UnitPT, PageSize: Rect{W: 595.28, H: 841.89}})
pdf.AddPage()
if err := pdf.AddTTFFontWithOption(prefix, font, TtfOption{UseKerning: true}); err != nil {
t.Error(err)
}
// Baseline: SetFont(int) + AddText
if err := pdf.SetFont(prefix, "", int(50)); err != nil {
t.Error(err)
}
hx, hy := moveAndAdd(&pdf, prefix)
return prefix, &pdf, hx, hy
}
func TestSetFontWithFloat(t *testing.T) {
prefix, pdf, wantx, wanty := setup(t)
// try it with fontsize = float64(50)
if err := pdf.SetFont(prefix, "", float64(50)); err != nil {
t.Error(err)
}
havex, havey := moveAndAdd(pdf, prefix)
if (havex != wantx) || (havey != wanty) {
t.Errorf("SetFont(float64) + '%s' => \nhave = %f, %f;\nwant = %f,%f\n",
prefix, havex, havey, wantx, wanty)
}
}
func TestSetFontWithUint(t *testing.T) {
prefix, pdf, wantx, wanty := setup(t)
// try it with fontsize = uint8(50)
if err := pdf.SetFont(prefix, "", uint8(50)); err != nil {
t.Error(err)
}
havex, havey := moveAndAdd(pdf, prefix)
if (havex != wantx) || (havey != wanty) {
t.Errorf("SetFont(uint8) + AddText => %f, %f; want = %f,%f\n",
havex, havey, wantx, wanty)
}
}
func TestSetFontWithString(t *testing.T) {
prefix, pdf, _, _ := setup(t)
// Try with a string
err := pdf.SetFont(prefix, "", string("50"))
if err == nil {
t.Errorf("SetFont(string) + AddText: Should have gotten an error!\n")
}
}
func ExampleFormatFloatTrim() {
fmt.Printf("/F1 %s Tf\n", FormatFloatTrim(10))
fmt.Printf("/F1 %s Tf\n", FormatFloatTrim(10.0))
fmt.Printf("/F1 %s Tf\n", FormatFloatTrim(10.01))
fmt.Printf("/F1 %s Tf\n", FormatFloatTrim(10.001))
fmt.Printf("/F1 %s Tf\n", FormatFloatTrim(10.0001))
fmt.Printf("/F1 %s Tf\n", FormatFloatTrim(10.00001))
fmt.Printf("/F1 %s Tf\n", FormatFloatTrim(9.99999))
fmt.Printf("/F1 %s Tf\n", FormatFloatTrim(9.9999))
fmt.Printf("/F1 %s Tf\n", FormatFloatTrim(9.999))
fmt.Printf("/F1 %s Tf\n", FormatFloatTrim(9.99))
fmt.Printf("/F1 %s Tf\n", FormatFloatTrim(9.9))
// Output: /F1 10 Tf
// /F1 10 Tf
// /F1 10.01 Tf
// /F1 10.001 Tf
// /F1 10 Tf
// /F1 10 Tf
// /F1 10 Tf
// /F1 10 Tf
// /F1 9.999 Tf
// /F1 9.99 Tf
// /F1 9.9 Tf
}