Skip to content

Commit

Permalink
Add support for math CSS functions
Browse files Browse the repository at this point in the history
  • Loading branch information
samuferenc authored and bsweeney committed Jan 8, 2024
1 parent 7c93050 commit 9076856
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
13 changes: 7 additions & 6 deletions src/Css/Style.php
Original file line number Diff line number Diff line change
Expand Up @@ -1272,25 +1272,26 @@ private function evaluate_func(array $rpn, float $ref_size = 0, ?float $font_siz
return null;
}
if ($argv[1] > 0) {
// calc(A - sign(B)*round(down, A*sign(B), B))
$stack[] = $argv[0] - (floor($argv[0] / $argv[1]) * $argv[1]);
$stack[] = $argv[0] - floor($argv[0] / $argv[1]) * $argv[1];
} else {
// calc(A - sign(B)*round(up, A*sign(B), B))
$stack[] = $argv[0] - (ceil($argv[0] * -1 / $argv[1]) * $argv[1] * -1) ;
$stack[] = $argv[0] - ceil($argv[0] * -1 / $argv[1]) * $argv[1] * -1 ;
}
break;
case 'rem':
if ($argc !== 2 || $argv[1] === 0.0) {
return null;
}
// calc(A - round(to-zero, A, B))
$stack[] = $argv[0] - (intval($argv[0] / $argv[1]) * $argv[1]);
break;
case 'round':
if ($argc !== 2 || $argv[1] === 0.0) {
return null;
}
$stack[] = round($argv[0] / $argv[1]) * $argv[1];
if ($argv[0] >= 0) {
$stack[] = round($argv[0] / $argv[1], 0, PHP_ROUND_HALF_UP) * $argv[1];
} else {
$stack[] = round($argv[0] / $argv[1], 0, PHP_ROUND_HALF_DOWN) * $argv[1];
}
break;
case 'calc':
if ($argc !== 1) {
Expand Down
14 changes: 10 additions & 4 deletions tests/Css/StyleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,18 @@ public static function lengthInPtProvider(): array
["round(30%, 0%)", 100, 0.0],
["round(4%, 9%)", 100, 0.0],
["round(6%, 9%)", 100, 9.0],
["round(13.5%, 9%)", 100, 18.0], // Default when exactly between (upper)
["round(13.5%, 9%)", 100, 18.0], // Default when exactly between (nearest)
["round(15%, 9)", 100, 18.0],
["round(5.4, 1)", null, 5.0],
["round(5.5, 1)", null, 6.0], // Default when exactly between (upper)
["round(0.54, 0.1)", null, 0.5],
["round(0.56, 0.1)", null, 0.6],
["round(5.5, 1)", null, 6.0], // Default when exactly between (nearest)
["round(5.6, 1)", null, 6.0],
["round(-5.4, 1)", null, -5.0],
["round(-5.5, 1)", null, -5.0], // Default when exactly between (nearest)
["round(-5.6, 1)", null, -6.0],
["round(-5.5, -1)", null, -5.0], // Default when exactly between (nearest)
["round(5.5, -1)", null, 6.0], // Default when exactly between (nearest)
["round(0.54, 0.1)", null, 0.5, 4],
["round(0.56, 0.1)", null, 0.6, 4],
["mod(30, 0)", null, 0.0],
["mod(18, 5)", null, 3.0],
["mod(-18, 5)", null, 2.0],
Expand Down

0 comments on commit 9076856

Please sign in to comment.