Repository navigation
Expand file tree
/
Copy pathpattern_test.go
More file actions
399 lines (382 loc) · 14.9 KB
/
Copy pathpattern_test.go
File metadata and controls
399 lines (382 loc) · 14.9 KB
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
package render
import (
"image/color"
"testing"
"github.com/go-gfx/gfx/geometry"
"github.com/go-pdfkit/reader"
)
func TestAShadingPatternFillingAShape(t *testing.T) {
// A gradient used as a fill colour: the shape is the path, the colour
// comes from the shading.
d := shadedPage(t, "/Pattern cs /P1 scn 10 10 80 80 re f", func(w *reader.Writer) reader.Dict {
return reader.Dict{"Pattern": reader.Dict{"P1": w.Add(reader.Dict{
"Type": reader.Name("Pattern"), "PatternType": reader.Integer(2),
"Shading": reader.Dict{
"ShadingType": reader.Integer(2), "ColorSpace": reader.Name("DeviceRGB"),
"Coords": nums(0, 0, 100, 0), "Function": rampFunction(w),
"Extend": reader.Array{reader.Bool(true), reader.Bool(true)},
},
})}}
})
img, err := Page(d, 1, Options{Scale: 1})
if err != nil {
t.Fatal(err)
}
if c := img.At(15, 50); c.R < 150 {
t.Errorf("the left of the shape is %v, want red", c)
}
if c := img.At(85, 50); !isBlue(c) {
t.Errorf("the right of the shape is %v, want blue", c)
}
if !isWhite(img, 3, 50) {
t.Errorf("outside the shape it painted %v", img.At(3, 50))
}
}
func TestAShadingPatternStrokingAShape(t *testing.T) {
d := shadedPage(t, "/Pattern CS /P1 SCN 8 w 10 50 m 90 50 l S", func(w *reader.Writer) reader.Dict {
return reader.Dict{"Pattern": reader.Dict{"P1": w.Add(reader.Dict{
"Type": reader.Name("Pattern"), "PatternType": reader.Integer(2),
"Shading": reader.Dict{
"ShadingType": reader.Integer(2), "ColorSpace": reader.Name("DeviceRGB"),
"Coords": nums(0, 0, 100, 0), "Function": rampFunction(w),
"Extend": reader.Array{reader.Bool(true), reader.Bool(true)},
},
})}}
})
img, err := Page(d, 1, Options{Scale: 1})
if err != nil {
t.Fatal(err)
}
if c := img.At(15, 50); c.R < 150 {
t.Errorf("the left of the line is %v, want red", c)
}
if c := img.At(85, 50); !isBlue(c) {
t.Errorf("the right of the line is %v, want blue", c)
}
if !isWhite(img, 50, 20) {
t.Error("the line was painted where it is not")
}
}
// hatchPattern is a tiling pattern drawing one horizontal bar per cell.
func hatchPattern(w *reader.Writer, paintType int, content string) reader.Object {
return w.Add(&reader.Stream{Dict: reader.Dict{
"Type": reader.Name("Pattern"), "PatternType": reader.Integer(1),
"PaintType": reader.Integer(int64(paintType)), "TilingType": reader.Integer(1),
"BBox": nums(0, 0, 20, 20), "XStep": reader.Integer(20), "YStep": reader.Integer(20),
"Resources": reader.Dict{},
}, Raw: []byte(content)})
}
func TestATilingPatternFillingAShape(t *testing.T) {
// A coloured tiling pattern: the cell draws its own colours, repeated
// across the shape and clipped to it.
d := shadedPage(t, "/Pattern cs /P1 scn 10 10 80 80 re f", func(w *reader.Writer) reader.Dict {
return reader.Dict{"Pattern": reader.Dict{
"P1": hatchPattern(w, 1, "0 0 1 rg 0 0 20 8 re f"),
}}
})
img, err := Page(d, 1, Options{Scale: 1})
if err != nil {
t.Fatal(err)
}
// The bars repeat, so some rows inside the shape are blue and some are
// paper — and none of it reaches outside.
blue, paper := 0, 0
for y := 12; y < 88; y++ {
if isBlue(img.At(50, y)) {
blue++
} else if isWhite(img, 50, y) {
paper++
}
}
if blue == 0 {
t.Error("the tiling pattern drew nothing")
}
if paper == 0 {
t.Error("the tiling pattern filled everything rather than repeating")
}
if !isWhite(img, 3, 50) {
t.Errorf("outside the shape it painted %v", img.At(3, 50))
}
}
func TestAnUncolouredTilingPatternTakesTheColourItIsGiven(t *testing.T) {
// A pattern of the second paint type is a shape only: the colour is the
// one named where it is used, and every colour operator inside it is to
// be passed over. One that is not passed over paints white over the
// page, which is exactly what was found happening.
d := shadedPage(t, "/Cs cs 0 0.6 0 /P1 scn 10 10 80 80 re f", func(w *reader.Writer) reader.Dict {
return reader.Dict{
"ColorSpace": reader.Dict{"Cs": reader.Array{reader.Name("Pattern"), reader.Name("DeviceRGB")}},
"Pattern": reader.Dict{
// The white fill inside must not reach the page.
"P1": hatchPattern(w, 2, "1 1 1 rg 0 0 20 8 re f"),
},
}
})
img, err := Page(d, 1, Options{Scale: 1})
if err != nil {
t.Fatal(err)
}
green := 0
for y := 12; y < 88; y++ {
if c := img.At(50, y); c.G > 100 && c.R < 100 && c.B < 100 {
green++
}
}
if green == 0 {
t.Error("an uncoloured pattern did not draw in the colour it was given")
}
}
func TestATilingPatternIsNotDrawnInsideItself(t *testing.T) {
// A pattern whose cell fills with the pattern in force would start the
// whole thing again at every tile, which on a real page ran eleven deep
// and rubbed the page out. The cell begins from a clean colour instead.
d := shadedPage(t, "/Pattern cs /P1 scn 0 0 100 100 re f", func(w *reader.Writer) reader.Dict {
return reader.Dict{"Pattern": reader.Dict{
"P1": hatchPattern(w, 1, "0 0 20 8 re f"),
}}
})
img, err := Page(d, 1, Options{Scale: 1})
if err != nil {
t.Fatal(err)
}
dark := 0
for y := 0; y < 100; y++ {
if c := img.At(50, y); c.R < 60 {
dark++
}
}
if dark == 0 {
t.Error("the pattern drew nothing")
}
if dark > 90 {
t.Errorf("the pattern filled %d of a hundred rows, which is not a repeat", dark)
}
}
func TestPatternsThatCannotBeDrawn(t *testing.T) {
cases := []struct {
name string
content string
build func(w *reader.Writer) reader.Dict
}{
{"no Pattern resources", "/Pattern cs /P1 scn 10 10 80 80 re f",
func(w *reader.Writer) reader.Dict { return reader.Dict{} }},
{"a name nothing answers to", "/Pattern cs /Missing scn 10 10 80 80 re f",
func(w *reader.Writer) reader.Dict {
return reader.Dict{"Pattern": reader.Dict{"P1": reader.Integer(3)}}
}},
{"a pattern that is not a dictionary", "/Pattern cs /P1 scn 10 10 80 80 re f",
func(w *reader.Writer) reader.Dict {
return reader.Dict{"Pattern": reader.Dict{"P1": reader.Integer(3)}}
}},
{"a kind of pattern that does not exist", "/Pattern cs /P1 scn 10 10 80 80 re f",
func(w *reader.Writer) reader.Dict {
return reader.Dict{"Pattern": reader.Dict{"P1": w.Add(reader.Dict{
"PatternType": reader.Integer(9)})}}
}},
{"a shading pattern whose shading is not one", "/Pattern cs /P1 scn 10 10 80 80 re f",
func(w *reader.Writer) reader.Dict {
return reader.Dict{"Pattern": reader.Dict{"P1": w.Add(reader.Dict{
"PatternType": reader.Integer(2), "Shading": reader.Integer(3)})}}
}},
{"a tiling pattern that is not a stream", "/Pattern cs /P1 scn 10 10 80 80 re f",
func(w *reader.Writer) reader.Dict {
return reader.Dict{"Pattern": reader.Dict{"P1": w.Add(reader.Dict{
"PatternType": reader.Integer(1), "BBox": nums(0, 0, 10, 10)})}}
}},
{"a tiling pattern with no box", "/Pattern cs /P1 scn 10 10 80 80 re f",
func(w *reader.Writer) reader.Dict {
return reader.Dict{"Pattern": reader.Dict{"P1": w.Add(&reader.Stream{
Dict: reader.Dict{"PatternType": reader.Integer(1)},
Raw: []byte("0 0 10 10 re f")})}}
}},
{"a pattern named with no name at all", "/Pattern cs scn 10 10 80 80 re f",
func(w *reader.Writer) reader.Dict { return reader.Dict{} }},
{"a pattern flattened to nothing by its own matrix", "/Pattern cs /P1 scn 10 10 80 80 re f",
func(w *reader.Writer) reader.Dict {
return reader.Dict{"Pattern": reader.Dict{"P1": w.Add(&reader.Stream{
Dict: reader.Dict{"PatternType": reader.Integer(1), "BBox": nums(0, 0, 10, 10),
"Matrix": nums(0, 0, 0, 0, 0, 0)},
Raw: []byte("0 0 10 10 re f")})}}
}},
{"a pattern whose step is nothing at all", "/Pattern cs /P1 scn 10 10 80 80 re f",
func(w *reader.Writer) reader.Dict {
return reader.Dict{"Pattern": reader.Dict{"P1": w.Add(&reader.Stream{
Dict: reader.Dict{"PatternType": reader.Integer(1), "BBox": nums(0, 0, 0, 0)},
Raw: []byte("0 0 10 10 re f")})}}
}},
}
for _, c := range cases {
d := shadedPage(t, c.content, c.build)
img, err := Page(d, 1, Options{Scale: 1})
if err != nil {
t.Fatalf("%s: %v", c.name, err)
}
// A pattern that cannot be drawn leaves a mid grey rather than
// nothing, so that what was asked for does not simply vanish; what
// matters is that the page came out at all.
_ = img
}
}
func TestAPatternAskingForMoreTilesThanAnyoneNeeds(t *testing.T) {
// A pattern whose step is a hair across would tile a page millions of
// times; past a limit it is left undrawn rather than allowed to take the
// afternoon.
d := shadedPage(t, "/Pattern cs /P1 scn 0 0 100 100 re f", func(w *reader.Writer) reader.Dict {
return reader.Dict{"Pattern": reader.Dict{"P1": w.Add(&reader.Stream{
Dict: reader.Dict{"Type": reader.Name("Pattern"),
"PatternType": reader.Integer(1), "PaintType": reader.Integer(1),
"BBox": nums(0, 0, 0.01, 0.01), "XStep": reader.Real(0.01), "YStep": reader.Real(0.01)},
Raw: []byte("0 0 0.01 0.01 re f")})}}
})
if _, err := Page(d, 1, Options{Scale: 1}); err != nil {
t.Fatal(err)
}
}
func TestAPatternNestedTooDeeply(t *testing.T) {
// A pattern drawn from inside a form drawn from inside a pattern, past
// the depth a page is allowed to nest, is left undrawn.
r := &renderer{depth: maxFormDepth}
r.tile(&gstate{}, &pattern{}, geometry.Identity(), nil, 0, 0, 0, 0, 1)
}
func TestAnUncolouredPatternUsedForStroking(t *testing.T) {
// The colour an uncoloured pattern draws in is given where it is used,
// and a stroking use names it in the stroking space.
d := shadedPage(t, "/Cs CS 0 0 1 /P1 SCN 10 w 10 50 m 90 50 l S", func(w *reader.Writer) reader.Dict {
return reader.Dict{
"ColorSpace": reader.Dict{"Cs": reader.Array{reader.Name("Pattern"), reader.Name("DeviceRGB")}},
"Pattern": reader.Dict{"P1": hatchPattern(w, 2, "0 0 20 20 re f")},
}
})
img, err := Page(d, 1, Options{Scale: 1})
if err != nil {
t.Fatal(err)
}
if c := img.At(50, 50); !isBlue(c) {
t.Errorf("the line is %v, want blue", c)
}
}
func TestATilingPatternFilteredAsAnImage(t *testing.T) {
// A pattern whose content is filtered as an image is not a pattern.
d := shadedPage(t, "/Pattern cs /P1 scn 10 10 80 80 re f", func(w *reader.Writer) reader.Dict {
return reader.Dict{"Pattern": reader.Dict{"P1": w.Add(&reader.Stream{
Dict: reader.Dict{"PatternType": reader.Integer(1), "BBox": nums(0, 0, 20, 20),
"Filter": reader.Name("DCTDecode")},
Raw: []byte("not a jpeg")})}}
})
if _, err := Page(d, 1, Options{Scale: 1}); err != nil {
t.Fatal(err)
}
}
func TestAPatternInsideAFormIsPlacedInTheFormsSpace(t *testing.T) {
// A pattern is placed in the space the page is in, and a form's own
// matrix and the transform that drew it are both part of that space. A
// gradient used inside a form that has been moved must move with it:
// files written by plotting tools put every figure in a form and fill its
// panels with patterns, and a pattern that stayed at the page's origin
// would miss the shape it was asked to fill.
d := shadedPage(t, "q 1 0 0 1 50 50 cm /F1 Do Q", func(w *reader.Writer) reader.Dict {
shading := w.Add(reader.Dict{
"ShadingType": reader.Integer(2), "ColorSpace": reader.Name("DeviceRGB"),
"Coords": nums(0, 0, 40, 0),
"Function": rampFunction(w),
"Extend": reader.Array{reader.Bool(true), reader.Bool(true)},
})
pattern := w.Add(reader.Dict{"PatternType": reader.Integer(2), "Shading": shading})
form := w.Add(&reader.Stream{
Dict: reader.Dict{
"Type": reader.Name("XObject"), "Subtype": reader.Name("Form"),
"BBox": reader.Array{reader.Integer(0), reader.Integer(0), reader.Integer(40), reader.Integer(40)},
"Resources": reader.Dict{"Pattern": reader.Dict{"P1": pattern}},
},
Raw: []byte("/Pattern cs /P1 scn 0 0 40 40 re f"),
})
return reader.Dict{"XObject": reader.Dict{"F1": form}}
})
img, err := Page(d, 1, Options{Scale: 1})
if err != nil {
t.Fatal(err)
}
// The form sits from 50 to 90 across the page, and the gradient runs from
// red to blue over exactly that width.
wantColour(t, img, 52, 30, color.RGBA{R: 242, B: 12, A: 255}, 24)
wantColour(t, img, 87, 30, color.RGBA{R: 12, B: 242, A: 255}, 24)
}
// helvetica is the simplest font a test can name: one of the fourteen every
// reader carries, so no programme has to be built to draw with it.
func helvetica(w *reader.Writer) reader.Object {
return w.Add(reader.Dict{"Type": reader.Name("Font"),
"Subtype": reader.Name("Type1"), "BaseFont": reader.Name("Helvetica")})
}
// TestATilingPatternFillingText. A pattern is a COLOUR, so it fills a glyph
// exactly as it fills a path. Filling a path honoured it and filling a glyph
// did not, which on the page that measures it drew solid letters where poppler
// drew patterned ones -- 31% of the page's pixels.
func TestATilingPatternFillingText(t *testing.T) {
d := shadedPage(t, "/Pattern cs /P1 scn BT /F1 90 Tf 5 20 Td (H) Tj ET",
func(w *reader.Writer) reader.Dict {
return reader.Dict{
"Pattern": reader.Dict{"P1": hatchPattern(w, 1, "0 0 1 rg 0 0 20 20 re f")},
"Font": reader.Dict{"F1": helvetica(w)},
}
})
img, err := Page(d, 1, Options{Scale: 1})
if err != nil {
t.Fatal(err)
}
blue := 0
for y := range img.H {
for x := range img.W {
if isBlue(img.At(x, y)) {
blue++
}
}
}
if blue == 0 {
t.Error("the glyph was not filled with the pattern: a pattern is a colour, " +
"and filling a glyph has to honour it as filling a path does")
}
}
// TestAPatternDoesNotInheritTheTextStateThatUsedIt. A pattern's content runs
// in the DEFAULT graphics state (8.7.3.1), not in the one that used it. The
// colour was already reset here and the text state was not, so a page that
// set `2 Tr` -- fill AND stroke -- had every glyph of the PATTERN stroked as
// well, in a colour the pattern never chose.
//
// The page fills a RECTANGLE here, not text, so that it draws no outline of
// its own: `2 Tr` is set and then nothing on the page reads it. Every mark
// inside the shape is the pattern's, and the pattern draws in red only.
func TestAPatternDoesNotInheritTheTextStateThatUsedIt(t *testing.T) {
d := shadedPage(t, "2 Tr /Pattern cs /P1 scn 5 5 90 90 re f",
func(w *reader.Writer) reader.Dict {
cell := w.Add(&reader.Stream{Dict: reader.Dict{
"Type": reader.Name("Pattern"), "PatternType": reader.Integer(1),
"PaintType": reader.Integer(1), "TilingType": reader.Integer(1),
"BBox": nums(0, 0, 20, 20), "XStep": reader.Integer(20), "YStep": reader.Integer(20),
"Resources": reader.Dict{"Font": reader.Dict{"F2": helvetica(w)}},
}, Raw: []byte("1 0 0 rg BT /F2 18 Tf 1 3 Td (o) Tj ET")})
return reader.Dict{"Pattern": reader.Dict{"P1": cell}}
})
img, err := Page(d, 1, Options{Scale: 1})
if err != nil {
t.Fatal(err)
}
red, dark := 0, 0
for y := range img.H {
for x := range img.W {
c := img.At(x, y)
switch {
case c.R > 150 && c.G < 100 && c.B < 100:
red++
case c.R < 100 && c.G < 100 && c.B < 100:
dark++
}
}
}
if red == 0 {
t.Fatal("the pattern drew no red: this test no longer measures what it says")
}
if dark > 0 {
t.Errorf("%d dark pixels beside %d red: the pattern inherited `2 Tr` and "+
"stroked its own glyphs in a colour it never set", dark, red)
}
}