Skip to content

Commit

Permalink
feat: Add type declarations for rasterization classes (#194)
Browse files Browse the repository at this point in the history
  • Loading branch information
meyfa authored Jan 2, 2023
1 parent 204bab5 commit c2c59ec
Show file tree
Hide file tree
Showing 21 changed files with 216 additions and 192 deletions.
30 changes: 23 additions & 7 deletions src/Rasterization/Path/ArcApproximator.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,16 @@ class ArcApproximator
*
* @return array[] An approximation for the curve, as an array of points.
*/
public function approximate($start, $end, $large, $sweep, $radiusX, $radiusY, $rotation, $scale = 1.0)
{
public function approximate(
array $start,
array $end,
bool $large,
bool $sweep,
float $radiusX,
float $radiusY,
float $rotation,
float $scale = 1.0
): array {
// out-of-range parameter handling according to W3; see
// https://www.w3.org/TR/SVG11/implnote.html#ArcImplementationNotes
if (self::pointsClose($start, $end)) {
Expand Down Expand Up @@ -100,8 +108,16 @@ public function approximate($start, $end, $large, $sweep, $radiusX, $radiusY, $r
*
* @return float[] A tuple with (center(cx,cy), radiusX, radiusY, angleStart, angleDelta).
*/
private static function endpointToCenter($start, $end, $large, $sweep, $radiusX, $radiusY, $cosr, $sinr)
{
private static function endpointToCenter(
array $start,
array $end,
bool $large,
bool $sweep,
float $radiusX,
float $radiusY,
float $cosr,
float $sinr
): array {
// Step 1: Compute (x1', y1') [F.6.5.1]
$xsubhalf = ($start[0] - $end[0]) / 2;
$ysubhalf = ($start[1] - $end[1]) / 2;
Expand Down Expand Up @@ -166,7 +182,7 @@ private static function endpointToCenter($start, $end, $large, $sweep, $radiusX,
*
* @return float The angle, in radians.
*/
private static function vectorAngle($vecx, $vecy)
private static function vectorAngle(float $vecx, float $vecy): float
{
$norm = hypot($vecx, $vecy);
return ($vecy >= 0 ? 1 : -1) * acos($vecx / $norm);
Expand All @@ -182,7 +198,7 @@ private static function vectorAngle($vecx, $vecy)
*
* @return float The angle, in radians.
*/
private static function vectorAngle2($vec1x, $vec1y, $vec2x, $vec2y)
private static function vectorAngle2(float $vec1x, float $vec1y, float $vec2x, float $vec2y): float
{
// see W3C [F.6.5.4]
$dotprod = $vec1x * $vec2x + $vec1y * $vec2y;
Expand All @@ -201,7 +217,7 @@ private static function vectorAngle2($vec1x, $vec1y, $vec2x, $vec2y)
* @param float[] $vec2 The end point (x1, y1).
* @return bool Whether the points are close.
*/
private static function pointsClose($vec1, $vec2)
private static function pointsClose(array $vec1, array $vec2): bool
{
$distanceX = abs($vec1[0] - $vec2[0]);
$distanceY = abs($vec1[1] - $vec2[1]);
Expand Down
10 changes: 5 additions & 5 deletions src/Rasterization/Path/BezierApproximator.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class BezierApproximator
*
* @return array[] An approximation for the curve, as an array of points.
*/
public function quadratic($p0, $p1, $p2, $accuracy = 1.0)
public function quadratic(array $p0, array $p1, array $p2, float $accuracy = 1.0): array
{
$t = 0;
$prev = $p0;
Expand Down Expand Up @@ -71,7 +71,7 @@ public function quadratic($p0, $p1, $p2, $accuracy = 1.0)
*
* @return array[] An approximation for the curve, as an array of points.
*/
public function cubic($p0, $p1, $p2, $p3, $accuracy = 1.0)
public function cubic(array $p0, array $p1, array $p2, array $p3, float $accuracy = 1.0): array
{
$t = 0;
$prev = $p0;
Expand Down Expand Up @@ -116,7 +116,7 @@ public function cubic($p0, $p1, $p2, $p3, $accuracy = 1.0)
*
* @return float[] The point on the curve (0 => x, 1 => y).
*/
private static function calculateQuadratic($p0, $p1, $p2, $t)
private static function calculateQuadratic(array $p0, array $p1, array $p2, float $t): array
{
$ti = 1 - $t;

Expand All @@ -138,7 +138,7 @@ private static function calculateQuadratic($p0, $p1, $p2, $t)
*
* @return float[] The point on the curve (0 => x, 1 => y).
*/
private static function calculateCubic($p0, $p1, $p2, $p3, $t)
private static function calculateCubic(array $p0, array $p1, array $p2, array $p3, float $t): array
{
$ti = 1 - $t;

Expand Down Expand Up @@ -173,7 +173,7 @@ private static function calculateCubic($p0, $p1, $p2, $p3, $t)
*
* @return float The squared distance between the two points.
*/
private static function getDistanceSquared($p1, $p2)
private static function getDistanceSquared(array $p1, array $p2): float
{
$dx = $p2[0] - $p1[0];
$dy = $p2[1] - $p1[1];
Expand Down
30 changes: 15 additions & 15 deletions src/Rasterization/Path/PathApproximator.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public function __construct(Transform $transform)
*
* @return void
*/
public function approximate(array $commands)
public function approximate(array $commands): void
{
// https://www.w3.org/TR/SVG/paths.html#PathDataMovetoCommands
// "A path data segment (if there is one) must begin with a "moveto" command."
Expand Down Expand Up @@ -154,7 +154,7 @@ public function approximate(array $commands)
*
* @return float[][][] The approximated subpaths.
*/
public function getSubpaths()
public function getSubpaths(): array
{
return $this->subpaths;
}
Expand All @@ -166,7 +166,7 @@ public function getSubpaths()
*
* @return void
*/
private function appendSubpath()
private function appendSubpath(): void
{
if (isset($this->builder)) {
$points = $this->builder->build();
Expand All @@ -182,7 +182,7 @@ private function appendSubpath()
*
* @return void
*/
private function newSubpath()
private function newSubpath(): void
{
$this->appendSubpath();

Expand All @@ -201,7 +201,7 @@ private function newSubpath()
*
* @return float[] The reflected point (x, y).
*/
private static function reflectPoint(array $p, array $r)
private static function reflectPoint(array $p, array $r): array
{
return [
2 * $r[0] - $p[0],
Expand All @@ -220,7 +220,7 @@ private static function reflectPoint(array $p, array $r)
* @SuppressWarnings("unused")
* @noinspection PhpUnusedPrivateMethodInspection
*/
private function moveTo($id, array $args)
private function moveTo(string $id, array $args): void
{
list($x, $y) = $args;
if ($id === 'm') {
Expand All @@ -243,7 +243,7 @@ private function moveTo($id, array $args)
* @SuppressWarnings("unused")
* @noinspection PhpUnusedPrivateMethodInspection
*/
private function lineTo($id, array $args)
private function lineTo(string $id, array $args): void
{
list($x, $y) = $args;
if ($id === 'l') {
Expand All @@ -267,7 +267,7 @@ private function lineTo($id, array $args)
* @SuppressWarnings("unused")
* @noinspection PhpUnusedPrivateMethodInspection
*/
private function lineToHorizontal($id, array $args)
private function lineToHorizontal(string $id, array $args): void
{
$x = $args[0];
$y = $this->posY;
Expand All @@ -290,7 +290,7 @@ private function lineToHorizontal($id, array $args)
* @SuppressWarnings("unused")
* @noinspection PhpUnusedPrivateMethodInspection
*/
private function lineToVertical($id, array $args)
private function lineToVertical(string $id, array $args): void
{
$x = $this->posX;
$y = $args[0];
Expand All @@ -313,7 +313,7 @@ private function lineToVertical($id, array $args)
* @SuppressWarnings("unused")
* @noinspection PhpUnusedPrivateMethodInspection
*/
private function curveToCubic($id, array $args)
private function curveToCubic(string $id, array $args): void
{
// NOTE: Bézier curves are invariant under affine transforms.
// This means transforming the control points vs. transforming the final approximated pixels does not
Expand Down Expand Up @@ -358,7 +358,7 @@ private function curveToCubic($id, array $args)
* @SuppressWarnings("unused")
* @noinspection PhpUnusedPrivateMethodInspection
*/
private function curveToCubicSmooth($id, array $args)
private function curveToCubicSmooth(string $id, array $args): void
{
$p1 = [$this->posX, $this->posY]; // first control point defaults to current point
$p2 = [$args[0], $args[1]];
Expand Down Expand Up @@ -400,7 +400,7 @@ private function curveToCubicSmooth($id, array $args)
* @SuppressWarnings("unused")
* @noinspection PhpUnusedPrivateMethodInspection
*/
private function curveToQuadratic($id, array $args)
private function curveToQuadratic(string $id, array $args): void
{
$p1 = [$args[0], $args[1]];
$p2 = [$args[2], $args[3]];
Expand Down Expand Up @@ -434,7 +434,7 @@ private function curveToQuadratic($id, array $args)
* @SuppressWarnings("unused")
* @noinspection PhpUnusedPrivateMethodInspection
*/
private function curveToQuadraticSmooth($id, array $args)
private function curveToQuadraticSmooth(string $id, array $args): void
{
$p1 = [$this->posX, $this->posY]; // control point defaults to current point
$p2 = [$args[0], $args[1]];
Expand Down Expand Up @@ -471,7 +471,7 @@ private function curveToQuadraticSmooth($id, array $args)
* @SuppressWarnings("unused")
* @noinspection PhpUnusedPrivateMethodInspection
*/
private function arcTo($id, array $args)
private function arcTo(string $id, array $args): void
{
// NOTE: Unfortunately, it seems that arc segments are not invariant under affine transforms, as opposed to
// Bézier curves. Currently, our best strategy is to approximate the curve with path coordinates and
Expand Down Expand Up @@ -521,7 +521,7 @@ private function arcTo($id, array $args)
* @SuppressWarnings("unused")
* @noinspection PhpUnusedPrivateMethodInspection
*/
private function closePath($id, array $args)
private function closePath(string $id, array $args): void
{
$first = $this->builder->getFirstPoint();
$this->builder->addPoint($first[0], $first[1]);
Expand Down
6 changes: 3 additions & 3 deletions src/Rasterization/Path/PathParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class PathParser
*
* @return array[] An array of commands (structure: see above).
*/
public function parse($description)
public function parse(string $description): array
{
$commands = [];

Expand Down Expand Up @@ -65,7 +65,7 @@ public function parse($description)
*
* @return string[] The split arguments.
*/
private function splitArguments($str)
private function splitArguments(string $str): array
{
$str = Str::trim($str);

Expand All @@ -88,7 +88,7 @@ private function splitArguments($str)
*
* @return bool Whether the command is known AND the arg count is correct.
*/
private function parseCommandChain($id, array $args, array &$commands)
private function parseCommandChain(string $id, array $args, array &$commands): bool
{
if (!isset(self::$commandLengths[$id])) {
// unknown command
Expand Down
16 changes: 8 additions & 8 deletions src/Rasterization/Path/PolygonBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class PolygonBuilder
* @param float $posX The starting x position.
* @param float $posY The starting y position.
*/
public function __construct($posX = 0.0, $posY = 0.0)
public function __construct(float $posX = 0.0, float $posY = 0.0)
{
$this->posX = $posX;
$this->posY = $posY;
Expand All @@ -39,7 +39,7 @@ public function __construct($posX = 0.0, $posY = 0.0)
*
* @return array[] An array of absolute points (which are float 2-tuples).
*/
public function build()
public function build(): array
{
return $this->points;
}
Expand All @@ -49,7 +49,7 @@ public function build()
*
* @return float[]|null The first point, or null.
*/
public function getFirstPoint()
public function getFirstPoint(): ?array
{
if (empty($this->points)) {
return null;
Expand All @@ -62,7 +62,7 @@ public function getFirstPoint()
*
* @return float[]|null The last point, or null.
*/
public function getLastPoint()
public function getLastPoint(): ?array
{
if (empty($this->points)) {
return null;
Expand All @@ -79,7 +79,7 @@ public function getLastPoint()
*
* @return float[] The current position (either last point, or initial pos).
*/
public function getPosition()
public function getPosition(): array
{
return [$this->posX, $this->posY];
}
Expand All @@ -95,7 +95,7 @@ public function getPosition()
*
* @return void
*/
public function addPoint($x, $y)
public function addPoint(?float $x, ?float $y): void
{
$x = $x ?? $this->posX;
$y = $y ?? $this->posY;
Expand All @@ -119,7 +119,7 @@ public function addPoint($x, $y)
*
* @return void
*/
public function addPointRelative($x, $y)
public function addPointRelative(?float $x, ?float $y): void
{
$this->posX += $x ?: 0;
$this->posY += $y ?: 0;
Expand All @@ -134,7 +134,7 @@ public function addPointRelative($x, $y)
*
* @return void
*/
public function addPoints(array $points)
public function addPoints(array $points): void
{
$this->points = array_merge($this->points, $points);

Expand Down
6 changes: 3 additions & 3 deletions src/Rasterization/Renderers/EllipseRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class EllipseRenderer extends MultiPassRenderer
/**
* @inheritdoc
*/
protected function prepareRenderParams(array $options, Transform $transform, ?FontRegistry $fontRegistry)
protected function prepareRenderParams(array $options, Transform $transform, ?FontRegistry $fontRegistry): array
{
$cx = $options['cx'];
$cy = $options['cy'];
Expand All @@ -40,15 +40,15 @@ protected function prepareRenderParams(array $options, Transform $transform, ?Fo
/**
* @inheritdoc
*/
protected function renderFill($image, array $params, $color)
protected function renderFill($image, $params, int $color): void
{
imagefilledellipse($image, $params['cx'], $params['cy'], $params['width'], $params['height'], $color);
}

/**
* @inheritdoc
*/
protected function renderStroke($image, array $params, $color, $strokeWidth)
protected function renderStroke($image, $params, int $color, float $strokeWidth): void
{
imagesetthickness($image, round($strokeWidth));

Expand Down
6 changes: 3 additions & 3 deletions src/Rasterization/Renderers/ImageRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class ImageRenderer extends Renderer
/**
* @inheritdoc
*/
public function render(SVGRasterizer $rasterizer, array $options, SVGNode $context)
public function render(SVGRasterizer $rasterizer, array $options, SVGNode $context): void
{
$transform = $rasterizer->getCurrentTransform();

Expand Down Expand Up @@ -67,7 +67,7 @@ public function render(SVGRasterizer $rasterizer, array $options, SVGNode $conte
*
* @return resource The loaded image.
*/
private function loadImage($href, $w, $h)
private function loadImage(string $href, int $w, int $h)
{
$content = $this->loadImageContent($href);

Expand All @@ -86,7 +86,7 @@ private function loadImage($href, $w, $h)
*
* @return string The image content.
*/
private function loadImageContent($href)
private function loadImageContent(string $href): string
{
$dataPrefix = 'data:';

Expand Down
Loading

0 comments on commit c2c59ec

Please sign in to comment.