|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +// SPDX-FileCopyrightText: 2026 LibreSign |
| 6 | +// SPDX-License-Identifier: AGPL-3.0-or-later |
| 7 | + |
| 8 | +namespace LibreSign\XObjectTemplate\Pdf\Svg; |
| 9 | + |
| 10 | +use LibreSign\XObjectTemplate\Pdf\Svg\SvgArcMath; |
| 11 | + |
| 12 | +/** |
| 13 | + * Converts SVG arc commands to cubic Bézier curve approximations. |
| 14 | + * |
| 15 | + * This class encapsulates the mathematical transformation of SVG arc path |
| 16 | + * commands (A/a) into a series of cubic Bézier curves, which are directly |
| 17 | + * supported by the PDF specification. |
| 18 | + * |
| 19 | + * The algorithm implements the SVG 2 specification's arc-to-Bézier conversion, |
| 20 | + * decomposing the arc into multiple segments for accurate curve approximation. |
| 21 | + */ |
| 22 | +final readonly class SvgArcConverter |
| 23 | +{ |
| 24 | + public function __construct( |
| 25 | + private SvgArcMath $math = new SvgArcMath(), |
| 26 | + ) { |
| 27 | + } |
| 28 | + |
| 29 | + /** |
| 30 | + * Convert SVG arc command parameters to cubic Bézier curves. |
| 31 | + * |
| 32 | + * This method takes the arc parameters as specified in SVG and converts |
| 33 | + * them to an array of cubic Bézier curve control points that approximate |
| 34 | + * the arc within the PDF coordinate space. |
| 35 | + * |
| 36 | + * @param float $fromX Starting X coordinate |
| 37 | + * @param float $fromY Starting Y coordinate |
| 38 | + * @param float $rx X-axis radius |
| 39 | + * @param float $ry Y-axis radius |
| 40 | + * @param float $rotation Rotation angle in degrees |
| 41 | + * @param int $largeArc Large arc flag (0 or 1) |
| 42 | + * @param int $sweep Sweep flag (0 or 1) |
| 43 | + * @param float $toX Ending X coordinate |
| 44 | + * @param float $toY Ending Y coordinate |
| 45 | + * @return array<int, array<int, float>> Array of cubic Bézier control points |
| 46 | + */ |
| 47 | + public function arcToBezierCurves( |
| 48 | + float $fromX, |
| 49 | + float $fromY, |
| 50 | + float $radiusX, |
| 51 | + float $radiusY, |
| 52 | + float $rotation, |
| 53 | + int $largeArc, |
| 54 | + int $sweep, |
| 55 | + float $toX, |
| 56 | + float $toY, |
| 57 | + ): array { |
| 58 | + if (abs($toX - $fromX) < 1e-10 && abs($toY - $fromY) < 1e-10) { |
| 59 | + return []; |
| 60 | + } |
| 61 | + |
| 62 | + if ($radiusX < 1e-10 || $radiusY < 1e-10) { |
| 63 | + return [[$toX, $toY, $toX, $toY, $toX, $toY]]; |
| 64 | + } |
| 65 | + |
| 66 | + $theta = deg2rad($rotation); |
| 67 | + $params = new ArcParams( |
| 68 | + $fromX, |
| 69 | + $fromY, |
| 70 | + $toX, |
| 71 | + $toY, |
| 72 | + $radiusX, |
| 73 | + $radiusY, |
| 74 | + cos($theta), |
| 75 | + sin($theta), |
| 76 | + $largeArc, |
| 77 | + $sweep, |
| 78 | + ); |
| 79 | + |
| 80 | + $params = $this->math->normalizeArcRadii($params); |
| 81 | + |
| 82 | + [$centerX, $centerY] = $this->math->calculateArcCenter($params); |
| 83 | + |
| 84 | + [$startAngle, $deltaAngle] = $this->math->calculateArcAngles($params); |
| 85 | + |
| 86 | + return $this->math->generateArcCurves( |
| 87 | + $params, |
| 88 | + $centerX, |
| 89 | + $centerY, |
| 90 | + $startAngle, |
| 91 | + $deltaAngle, |
| 92 | + ); |
| 93 | + } |
| 94 | +} |
0 commit comments