Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
89.29% |
25 / 28 |
|
77.78% |
7 / 9 |
CRAP | |
0.00% |
0 / 1 |
| FeConvolveMatrix | |
89.29% |
25 / 28 |
|
77.78% |
7 / 9 |
20.49 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| order | |
85.71% |
6 / 7 |
|
0.00% |
0 / 1 |
4.05 | |||
| kernelMatrix | |
80.00% |
8 / 10 |
|
0.00% |
0 / 1 |
5.20 | |||
| divisor | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
| bias | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| targetX | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
| targetY | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
| edgeMode | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
| preserveAlpha | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Phpdftk\Svg\Filter; |
| 6 | |
| 7 | /** |
| 8 | * SVG 2 Filter Effects §15.13 — `<feConvolveMatrix>`. Applies an |
| 9 | * arbitrary 2D convolution kernel to the input. Edge detection, |
| 10 | * sharpening, custom blurs, embossing, etc. |
| 11 | * |
| 12 | * order: kernel dimensions (one or two integers) |
| 13 | * kernelMatrix: flat list of order×order values |
| 14 | * divisor: post-convolution divisor (default = sum of |
| 15 | * kernel values) |
| 16 | * bias: additive bias (default 0) |
| 17 | * targetX/targetY: kernel anchor point (defaults to centre) |
| 18 | * edgeMode: duplicate | wrap | none (default duplicate) |
| 19 | * preserveAlpha: true | false (default false) |
| 20 | */ |
| 21 | final class FeConvolveMatrix extends FilterPrimitive |
| 22 | { |
| 23 | public function __construct() |
| 24 | { |
| 25 | parent::__construct('feConvolveMatrix'); |
| 26 | } |
| 27 | |
| 28 | /** |
| 29 | * @return array{0: int, 1: int} [orderX, orderY] |
| 30 | */ |
| 31 | public function order(): array |
| 32 | { |
| 33 | $raw = trim($this->getAttribute('order') ?? ''); |
| 34 | if ($raw === '') { |
| 35 | return [3, 3]; |
| 36 | } |
| 37 | $parts = preg_split('/[\s,]+/', $raw) ?: []; |
| 38 | $ox = max(1, (int) ($parts[0] ?? 3)); |
| 39 | $oy = isset($parts[1]) ? max(1, (int) $parts[1]) : $ox; |
| 40 | return [$ox, $oy]; |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * @return list<float> |
| 45 | */ |
| 46 | public function kernelMatrix(): array |
| 47 | { |
| 48 | $raw = trim($this->getAttribute('kernelMatrix') ?? ''); |
| 49 | if ($raw === '') { |
| 50 | return []; |
| 51 | } |
| 52 | $parts = preg_split('/[\s,]+/', $raw) ?: []; |
| 53 | $out = []; |
| 54 | foreach ($parts as $p) { |
| 55 | if ($p === '') { |
| 56 | continue; |
| 57 | } |
| 58 | $out[] = (float) $p; |
| 59 | } |
| 60 | return $out; |
| 61 | } |
| 62 | |
| 63 | public function divisor(): ?float |
| 64 | { |
| 65 | $v = $this->getAttribute('divisor'); |
| 66 | return $v !== null ? (float) $v : null; |
| 67 | } |
| 68 | |
| 69 | public function bias(): float |
| 70 | { |
| 71 | return (float) ($this->getAttribute('bias') ?? 0); |
| 72 | } |
| 73 | |
| 74 | public function targetX(): ?int |
| 75 | { |
| 76 | $v = $this->getAttribute('targetX'); |
| 77 | return $v !== null ? (int) $v : null; |
| 78 | } |
| 79 | |
| 80 | public function targetY(): ?int |
| 81 | { |
| 82 | $v = $this->getAttribute('targetY'); |
| 83 | return $v !== null ? (int) $v : null; |
| 84 | } |
| 85 | |
| 86 | public function edgeMode(): string |
| 87 | { |
| 88 | $v = strtolower($this->getAttribute('edgeMode') ?? 'duplicate'); |
| 89 | return in_array($v, ['duplicate', 'wrap', 'none'], true) ? $v : 'duplicate'; |
| 90 | } |
| 91 | |
| 92 | public function preserveAlpha(): bool |
| 93 | { |
| 94 | return strtolower($this->getAttribute('preserveAlpha') ?? 'false') === 'true'; |
| 95 | } |
| 96 | } |