-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain_test.go
214 lines (178 loc) · 4.81 KB
/
main_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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package goweasyprint
import (
"fmt"
"io"
"log"
"os"
"path/filepath"
"testing"
"github.com/benoitkugler/go-weasyprint/pdf/test"
"github.com/benoitkugler/pdf/reader/file"
"github.com/benoitkugler/webrender/logger"
"github.com/benoitkugler/webrender/text"
"github.com/benoitkugler/webrender/utils"
ot "github.com/go-text/typesetting/font/opentype"
)
var fontconfig text.FontConfiguration
func init() {
logger.ProgressLogger.SetOutput(io.Discard)
var err error
fontconfig, err = test.LoadTestFontConfig("pdf/test/")
if err != nil {
log.Fatalf("creating font configuration: %s", err)
}
}
func tempFile(s string) string {
dir := os.TempDir()
return filepath.Join(dir, s)
}
func TestRealPage(t *testing.T) {
t.Skip()
path := tempFile("test_real_page.pdf")
f, err := os.Create(path)
if err != nil {
t.Fatal(err)
}
// url := "http://www.google.com"
// url := "https://weasyprint.org/"
// url := "https://en.wikipedia.org/wiki/Go_(programming_language)" // rather big document
// url := "https://golang.org/doc/go1.17" // slow because of text layout
url := "https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/preserveAspectRatio"
log.SetOutput(io.Discard)
// url := "https://github.com/Kozea/WeasyPrint"
err = HtmlToPdf(f, utils.InputUrl(url), fontconfig)
if err != nil {
t.Fatal(err)
}
f.Close()
_, err = file.ReadFile(path, nil)
if err != nil {
t.Fatal(err)
}
}
func TestSVGMask(t *testing.T) {
path := tempFile("test_svg_mask.pdf")
f, err := os.Create(path)
if err != nil {
t.Fatal(err)
}
input := `
<style>
@page { size: 300px }
svg { display: block }
</style>
<svg viewBox="-10 -10 120 120">
<mask id="myMask">
<!-- Everything under a white pixel will be visible -->
<rect x="0" y="0" width="100" height="100" fill="white" />
<!-- Everything under a black pixel will be invisible -->
<path d="M10,35 A20,20,0,0,1,50,35 A20,20,0,0,1,90,35 Q90,65,50,95 Q10,65,10,35 Z" fill="black" />
</mask>
<polygon points="-10,110 110,110 110,-10" fill="orange" />
<!-- with this mask applied, we "punch" a heart shape hole into the circle -->
<circle cx="50" cy="50" r="50" mask="url(#myMask)" />
</svg>
`
err = HtmlToPdf(f, utils.InputString(input), fontconfig)
if err != nil {
t.Fatal(err)
}
f.Close()
_, err = file.ReadFile(path, nil)
if err != nil {
t.Fatal(err)
}
}
func TestSVGText(t *testing.T) {
path := tempFile("test_svg_text.pdf")
f, err := os.Create(path)
if err != nil {
t.Fatal(err)
}
input := `
<style>
@page { size: 400px }
svg { display: block }
</style>
<svg viewBox="0 0 240 80" xmlns="http://www.w3.org/2000/svg">
<style>
.small { font: italic 13px sans-serif; font-style: italic }
.heavy { font: bold 30px sans-serif; }
/* Note that the color of the text is set with the *
* fill property, the color property is for HTML only */
.Rrrrr { font: italic 40px serif; fill: red; }
</style>
<text x="20" y="35" class="small">My</text>
<text x="40" y="35" class="heavy">cat</text>
<text x="55" y="55" class="small">is</text>
<text x="65" y="55" class="Rrrrr">Grumpy!</text>
</svg>
`
err = HtmlToPdf(f, utils.InputString(input), fontconfig)
if err != nil {
t.Fatal(err)
}
f.Close()
_, err = file.ReadFile(path, nil)
if err != nil {
t.Fatal(err)
}
}
func TestSVGGradient(t *testing.T) {
path := tempFile("test_svg_gradient.pdf")
f, err := os.Create(path)
if err != nil {
t.Fatal(err)
}
input := `
<svg width="500" height="500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<radialGradient id="rg2" cx="50%" cy="50%" r="40%" gradientUnits="objectBoundingBox"
spreadMethod="repeat" >
<stop offset="10%" stop-color="goldenrod" />
<stop offset="30%" stop-color="seagreen" />
<stop offset="50%" stop-color="cyan" />
<stop offset="70%" stop-color="black" />
<stop offset="100%" stop-color="orange" />
</radialGradient>
<ellipse cx="300" cy="150" rx="120" ry="100" style="fill:url(#rg2)" />
</svg>
`
err = HtmlToPdf(f, utils.InputString(input), fontconfig)
if err != nil {
t.Fatal(err)
}
f.Close()
_, err = file.ReadFile(path, nil)
if err != nil {
t.Fatal(err)
}
}
func TestFixUpstreamFont(t *testing.T) {
t.Skip()
f, _ := os.Open("resources_test/weasyprint.otb")
ft, err := ot.NewLoader(f)
if err != nil {
t.Fatal(err)
}
fmt.Println(ft.RawTable(ot.MustNewTag("loca")))
fmt.Println(ft.HasTable(ot.MustNewTag("glyf")))
var allTables []ot.Table
for _, table := range ft.Tables() {
if table == ot.MustNewTag("loca") || table == ot.MustNewTag("glyf") {
continue
}
fmt.Println(table)
b, err := ft.RawTable(table)
if err != nil {
t.Fatal(err)
}
allTables = append(allTables, ot.Table{
Tag: table,
Content: b,
})
}
err = os.WriteFile("weasyprint.otb_fixed", ot.WriteTTF(allTables), os.ModePerm)
if err != nil {
t.Fatal(err)
}
}