Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
91.30% covered (success)
91.30%
21 / 23
88.89% covered (warning)
88.89%
8 / 9
CRAP
0.00% covered (danger)
0.00%
0 / 1
Pattern
91.30% covered (success)
91.30%
21 / 23
88.89% covered (warning)
88.89%
8 / 9
14.13
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 x
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 y
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 width
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 height
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 patternUnits
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 patternContentUnits
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 viewBox
83.33% covered (warning)
83.33%
10 / 12
0.00% covered (danger)
0.00%
0 / 1
4.07
 href
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3declare(strict_types=1);
4
5namespace Phpdftk\Svg;
6
7/**
8 * SVG 2 §13.3 — `<pattern>` element. Defines a tile that paints
9 * into the fill or stroke of any shape that references it via
10 * `fill="url(#patternId)"`. Pattern attributes mirror `<marker>`
11 * and `<use>`:
12 *
13 *   - x, y, width, height — the tile rectangle in user space
14 *     (or pattern units, depending on patternUnits).
15 *   - patternUnits — `objectBoundingBox` (default, %-relative
16 *     to the referencing shape's bbox) or `userSpaceOnUse`.
17 *   - patternContentUnits — same flag for child positioning,
18 *     defaults to `userSpaceOnUse`.
19 *   - viewBox + preserveAspectRatio for content scaling.
20 *   - patternTransform — additional transform on the tile.
21 *
22 * For the static print renderer the typed class lifts these
23 * attributes off the generic Element surface; PDF Tiling Pattern
24 * (Pattern Type 1) emission is the next deliverable.
25 */
26final class Pattern extends Element
27{
28    public function __construct()
29    {
30        parent::__construct('pattern');
31    }
32
33    public function x(): float
34    {
35        return (float) ($this->getAttribute('x') ?? 0);
36    }
37
38    public function y(): float
39    {
40        return (float) ($this->getAttribute('y') ?? 0);
41    }
42
43    public function width(): float
44    {
45        return (float) ($this->getAttribute('width') ?? 0);
46    }
47
48    public function height(): float
49    {
50        return (float) ($this->getAttribute('height') ?? 0);
51    }
52
53    public function patternUnits(): string
54    {
55        $u = strtolower($this->getAttribute('patternUnits') ?? 'objectboundingbox');
56        return $u === 'userspaceonuse' ? 'userSpaceOnUse' : 'objectBoundingBox';
57    }
58
59    public function patternContentUnits(): string
60    {
61        $u = strtolower($this->getAttribute('patternContentUnits') ?? 'userspaceonuse');
62        return $u === 'objectboundingbox' ? 'objectBoundingBox' : 'userSpaceOnUse';
63    }
64
65    /**
66     * @return array{0: float, 1: float, 2: float, 3: float}|null
67     */
68    public function viewBox(): ?array
69    {
70        $vb = $this->getAttribute('viewBox');
71        if ($vb === null) {
72            return null;
73        }
74        $parts = preg_split('/[\s,]+/', trim($vb)) ?: [];
75        if (count($parts) !== 4) {
76            return null;
77        }
78        return [
79            (float) $parts[0],
80            (float) $parts[1],
81            (float) $parts[2],
82            (float) $parts[3],
83        ];
84    }
85
86    /**
87     * `href` (or legacy `xlink:href`) — when present, the
88     * referenced pattern's attributes inherit into this one.
89     * See SVG 2 §13.3 chain-resolution rules.
90     */
91    public function href(): ?string
92    {
93        return $this->getAttribute('href')
94            ?? $this->getAttribute('xlink:href');
95    }
96}