Skip to content

Run linear gradients the way CSS Images gives for "to" keywords and angles - #577

Merged
jakejackson1 merged 2 commits into
gravitypdffrom
fix/552-gradient-directions
Sep 29, 2026
Merged

jakejackson1 merged 2 commits into
gravitypdffrom
fix/552-gradient-directions

Conversation

@jakejackson1

@jakejackson1 jakejackson1 commented Sep 29, 2026 •

Copy link
Copy Markdown
Member

Problem

parseMozLinearGradient() read every gradient with the legacy syntax of the old -moz- and -webkit- functions. With a red to blue gradient on a 60 × 30 mm box, measured by rasterising the page:

Gradient Drawn CSS Images
linear-gradient(to bottom, …) red at the bottom red at the top
linear-gradient(to top, …) red at the top red at the bottom
linear-gradient(to bottom right, …) red bottom left, blue top right red top left, blue bottom right, the other two corners the same purple
linear-gradient(0deg, …) left to right bottom to top
linear-gradient(90deg, …) bottom to top left to right
linear-gradient(180deg, …) right to left top to bottom
linear-gradient(0.25turn, …) left to right, by luck: turn was not read and fell back to 0 left to right
-moz-linear-gradient(left, …) red at the right red at the left
  • to left and to right came out right only because the legacy reading of left was reversed too.
  • -webkit- and -o- were stripped by NormalizeProperties, so a prefixed gradient could not be told from an unprefixed one.
  • A fractional angle reached %=, which is deprecated for floats on PHP 8.1+.

Part of #552 (Backgrounds, bug 5 in the audit). Part of #520.

Change

In Gradient::parseMozLinearGradient():

  • Angles. An unprefixed gradient's angle is turned into the legacy angle Gradient() draws from (90 - angle). So 0deg points up and angles run clockwise. turn is read alongside deg, grad and rad, and the angle is wrapped with fmod().
  • Side keywords. With to, the keyword names the side the gradient ends at. Without it, as in the legacy syntax and mPDF's documented linear-gradient(top, …), the keyword names the side it starts from. That fixes the reversed legacy left and right.
  • Corners. to top right and the other corners set both ends, corner to corner across the box. In the unit box the shading is drawn in, the other two corners then sit halfway along the axis, as CSS's "magic corners" require. Before, Gradient() squared the box, which gave a different angle on a box that is not square.
  • Prefixes. -moz-, -webkit- and -o- functions keep the legacy angles. NormalizeProperties now keeps the -webkit- and -o- prefixes on background and background-image so they reach the parser.

Nothing else is affected:

  • Radial gradients, the drawing code in Gradient() and mPDF's own background-gradient property are unchanged.
  • SVG gradients do not go through this parser; Svg builds its own coordinates.
  • gradient-mask on images goes through parseMozGradient(), so it follows the same rules.

Documents that set an unprefixed angle, a to top/to bottom keyword or a legacy left/right keyword will look different after this change, and will match what a browser draws. No existing snapshot fixture changed.

Not changed here: absolute stop positions on a corner gradient are still measured along the corner-to-corner line rather than the gradient line, and repeating-radial-gradient() still draws one colour (audit bug 6).

Tests

  • LinearGradientDirectionTest renders a block for each case and reads the axis Gradient() registers:
    • every side keyword and right angle, including unitless 0, -90deg, 450deg, turn, grad and rad;
    • each to corner;
    • -moz- and -webkit- keywords and angles;
    • the quarter each diagonal angle runs into, including 22.5deg.
  • LinearGradientDirectionSnapshotTest (linear-gradient-direction) draws each case under a caption saying where the red and blue should be.

With the source change reverted, most of the cases fail. Those that pass on both are ones the old reading already got right, such as to left, to right, -moz- top and the default direction.

Checks

  • composer test: OK. 4486 tests, 31 skipped for missing optional packages or ungenerated data. The snapshot suite ran with Imagick and Ghostscript.
  • composer cs: clean.
  • PHPStan: no errors. The parsed angle is cast to float, so the baseline entry for $m[1] + 0 in Gradient.php is removed.

🤖 Generated with Claude Code

…ngles

"to bottom" and "to top" were read as the legacy start sides, so they drew upside down, and so did the vertical half of the corner keywords. Angles followed the legacy convention, 0deg pointing right and turning counter-clockwise, so 90deg drew bottom to top.

An unprefixed gradient now points up at 0deg and turns clockwise, and reads the turn unit. A "to" keyword names the side the gradient ends at, and a corner runs corner to corner across the box, so the other two corners meet halfway. -moz-, -webkit- and -o- gradients keep the legacy angles, and their side keyword names the side they start from. The -webkit- and -o- prefixes are kept through NormalizeProperties so they can be told apart.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@jakejackson1
jakejackson1 force-pushed the fix/552-gradient-directions branch from ec4991f to 5288c87 Compare September 29, 2026 23:45
@jakejackson1
jakejackson1 merged commit 4402e6c into gravitypdf Sep 29, 2026
11 checks passed
@jakejackson1
jakejackson1 deleted the fix/552-gradient-directions branch September 30, 2026 01:13
jakejackson1 added a commit that referenced this pull request Sep 30, 2026
…er (#583)

* Read the border and background shorthands with their parts in any order

border and its sides take the width, style and colour in any order, so
solid #c00 2mm, 2mm #c00 solid and red 2mm solid draw a border.
background takes a colour after url(), a size after the position, and
its repeat, attachment and origin and clip boxes in any order. Only the
last layer may carry a colour; mPDF still draws only the first layer,
over that colour.

A shorthand with a part that is none of its parts is dropped, as a
browser drops it, so the value it would replace still applies.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Keep the -webkit- and -o- prefix of a gradient in the background shorthand

Since #577, a -webkit- or -o- gradient is told apart from an unprefixed
one by its prefix, and keeps the legacy angles and start side. The
background shorthand parser dropped every prefix but -moz-, so
`background: -webkit-linear-gradient(left, …)` was read as an unprefixed
gradient. It now keeps -moz-, -webkit- and -o-, and still drops any other.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
jakejackson1 added a commit that referenced this pull request Sep 30, 2026
Under CssMode::LEGACY a linear gradient reads its direction as mPDF v7 did, prefixed or not: "left" and "right" name the side it ends at and "top" and "bottom" the side it starts from, with or without "to"; angles run counter-clockwise from pointing right in whole degrees; and turn is not read. The output is byte-identical to the code before #577. The angle is wrapped with fmod() and cut with an explicit cast, so PHP 8.1 raises no deprecation for a fractional angle.

Under CssMode::STANDARD gradients run as CSS Images gives, as #577 made them.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
jakejackson1 added a commit that referenced this pull request Sep 30, 2026
Under CssMode::LEGACY a linear gradient reads its direction as mPDF v7 did, prefixed or not: "left" and "right" name the side it ends at and "top" and "bottom" the side it starts from, with or without "to"; angles run counter-clockwise from pointing right in whole degrees; and turn is not read. The output is byte-identical to the code before #577. The angle is wrapped with fmod() and cut with an explicit cast, so PHP 8.1 raises no deprecation for a fractional angle.

Under CssMode::STANDARD gradients run as CSS Images gives, as #577 made them.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
jakejackson1 added a commit that referenced this pull request Sep 30, 2026
)

Under CssMode::LEGACY a linear gradient reads its direction as mPDF v7 did, prefixed or not: "left" and "right" name the side it ends at and "top" and "bottom" the side it starts from, with or without "to"; angles run counter-clockwise from pointing right in whole degrees; and turn is not read. The output is byte-identical to the code before #577. The angle is wrapped with fmod() and cut with an explicit cast, so PHP 8.1 raises no deprecation for a fractional angle.

Under CssMode::STANDARD gradients run as CSS Images gives, as #577 made them.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working create-upstream-pr

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant