Skip to content

Evaluate calc(), min(), max() and clamp() in lengths #594

Open

Description

@jakejackson1

Part of #552 (step 28, missing support, high priority).

calc(), min(), max() and clamp() are not evaluated. Framework CSS and hand-written templates use them for widths, gutters and fluid type (width: calc(50% - 10mm), padding: max(4mm, 2%), font-size: clamp(9pt, 1vw + 6pt, 14pt)).

Measured on gravitypdf at 60b82630, PHP 8.5.4.

Rule Browser mPDF today After #565
div { width: calc(50% - 10mm) }, 180 mm page area 80 mm full width: the value converts to 0, which means auto dropped: auto
p { margin: calc(5mm + 5mm) } 10 mm margins 0: the default margins are gone dropped: the default margins
p { margin-top: max(5mm, 10mm) } 10 mm 0 dropped
p { padding-left: calc(10% + 2mm) } 20 mm 0 dropped
p { font-size: clamp(8pt, 1vw, 20pt) } 8pt on A4, where 1vw is about 6pt ignored dropped

Cause. SizeConverter::convert() matches a number and a unit, and anything else converts to 0.0 with a warning to the PSR logger (src/SizeConverter.php:46-53). #565 adds NormalizeProperties::canParse(), which drops a declaration that calls one of these functions, so that it no longer overrides an earlier value. That makes them harmless, not supported.

Fix.

  • Evaluate the functions in SizeConverter::convert(), which already gets what they need:
  • Parse each function into a small expression tree with +, -, *, / and nested parentheses, following the type rules: add and subtract lengths, multiply and divide by numbers. Convert each length term with the existing unit code, then evaluate. min(), max() and clamp() take comma-separated arguments of the same kind.
  • Resolve at the time the value is used, not when it is parsed. Mixed % and length terms need the reference size, which is only known then.
  • Let canParse() accept a well-formed expression, and keep rejecting a malformed one, so the fallback declaration still applies.

Places that assume a value is one token:

  • The 1–4 value shorthands in NormalizeProperties split on whitespace: margin: calc(1mm + 2mm) 5mm has to split on spaces outside parentheses. The same goes for border, background-position, box-shadow and text-shadow.
  • Table and cell widths test for % with strpos() and store a percentage apart from a length (src/Tag/Td.php:381-386, src/Tag/Table.php:159 onwards). calc(50% - 10mm) is both. Resolve it against the block around the table, as cells' % padding does, or store it for _tableWidth() to resolve.
  • line-height and font-size go through their own code (fixLineheight(), setCSS()), not convert() alone.
  • Colour functions with calc() inside, as in rgb(calc(…) 0 0), are rare. Leave them out.

var() inside calc() is #595.

Tests. A unit test of SizeConverter over each function, nesting, % against different reference sizes, em and rem, whitespace around operators, division by zero and malformed input. Then one rendering test each for a block width, a shorthand margin, a table width and a font-size.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions