5 Geometry, Viewports, and Transforms
This group covers the pieces that turn SVG’s various coordinate-system
concepts —
Every matrix in this document and in svg/svg uses that same six-element vector format throughout, matching dc<%>’s convention exactly:
x' = a·x + c·y + e |
y' = b·x + d·y + f |
so a matrix produced by one of these functions can always be passed directly to (send dc transform matrix).
procedure
(parse-length s [ default #:reference reference]) → real? s : (or/c string? #f) default : real? = 0 reference : (or/c real? #f) = #f
A percentage resolves against reference if one is given
("50%" with reference 200 gives 100);
with no reference, a percentage is treated as a literal number
instead of raising or resolving to default (so "50%" with
no reference gives 50) —
> (parse-length "10px") 10
> (parse-length "1in") 96.0
> (parse-length "50%" #:reference 200) 100.0
> (parse-length #f 42) 42
> (parse-transform-list "translate(10,20) rotate(45)") '((translate 10 20) (rotate 45))

Note on rotate: SVG defines a positive angle as clockwise in its y-down
coordinate system. dc<%>’s own rotate method turns out to
spin the opposite direction for a positive angle (confirmed
empirically) —
> (parse-preserve-aspect-ratio "xMidYMid slice") (preserve-ar "xMidYMid" 'slice)
> (parse-preserve-aspect-ratio #f) (preserve-ar "xMidYMid" 'meet)
procedure
(compute-viewbox-matrix vb-x vb-y vb-w vb-h vp-w vp-h par) → (vector/c real? real? real? real? real? real?) vb-x : real? vb-y : real? vb-w : real? vb-h : real? vp-w : real? vp-h : real? par : any/c
A non-positive vb-w or vb-h returns the identity matrix (an invalid viewBox per spec is treated as if there were none).
> (compute-viewbox-matrix 0 0 100 100 200 200 (parse-preserve-aspect-ratio #f)) '#(2 0 0 2 0 0)
A 100×100 viewBox mapped into a 140×100 viewport, under
"xMidYMid meet" (left) versus "xMidYMid slice" (right) —

procedure
(viewport-instantiation-matrix target-attrs override-width override-height) → (vector/c real? real? real? real? real? real?) target-attrs : (listof (list/c symbol? string?)) override-width : (or/c real? #f) override-height : (or/c real? #f)
> (viewport-instantiation-matrix '((viewBox "0 0 100 100") (width "50") (height "50")) #f #f) '#(1/2 0 0 1/2 0 0)
Uses the exact same mapping algorithm as compute-viewbox-matrix
above (see that entry’s images) —