transform

package
v1.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 9, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package transform parses CSS transform strings (translate, scale, rotate, skew, matrix) and composes them into a single 2D affine matrix expressed in PDF operand order [A B C D E F]. The renderer emits that matrix directly as a PDF `cm` operator to position and orient content.

Matrix convention. A Matrix maps a point (x, y) to (x', y') as

x' = A*x + C*y + E
y' = B*x + D*y + F

which is exactly the PDF `cm` operator's meaning. In 3x3 homogeneous form with row vectors [x y 1]:

[x' y' 1] = [x y 1] * | A B 0 |
                      | C D 0 |
                      | E F 1 |

Coordinate space. waffle's layout space is y-DOWN (origin top-left, like CSS and the screen). All matrices here operate in that y-down space; the renderer applies them BEFORE its own y-flip into PDF's y-up page space. Rotate is defined so a positive angle rotates CLOCKWISE as seen on screen, matching the visual result of CSS `rotate()`. See Rotate for the exact formula.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ParseAngle

func ParseAngle(s string) (float64, error)

ParseAngle parses a CSS angle token and returns the angle in radians. It accepts the units deg, grad, rad, and turn; a bare number is interpreted as degrees. It is the shared angle helper used by rotate and skew parsing.

Types

type Matrix

type Matrix struct{ A, B, C, D, E, F float64 }

Matrix is a 2D affine transform stored in PDF operand order: [A B C D E F]. The zero value is NOT the identity; use Identity.

func Identity

func Identity() Matrix

Identity returns the identity transform (maps every point to itself).

func Parse

func Parse(s string) (Matrix, error)

Parse parses a CSS transform string such as

"rotate(45deg) translate(10, 5px) scale(1.5)"

into a single composed matrix. Functions are read left to right and composed so the composed matrix is M = f1 ∘ f2 ∘ ... : the FIRST (leftmost) function is the outermost and is applied to points LAST, matching CSS. Equivalently, the last function listed transforms the point first.

Supported functions: translate/translateX/translateY, scale/scaleX/scaleY, rotate, skew/skewX/skewY, and matrix(a,b,c,d,e,f). Function names are matched case-insensitively. Lengths accept px, pt, or a bare number, all treated as PDF points (at waffle's default 72 dpi, 1px == 1pt); percentages are NOT supported and produce an error. Angles accept deg (the default for a bare number), rad, grad, and turn.

Parse returns Identity for the empty string. Unknown or unsupported functions (e.g. translate3d, perspective) are ignored. Malformed input — a bad number, a wrong argument count for a known function, a percentage length, or broken syntax such as an unclosed parenthesis — returns (Identity, error). Parse never panics.

func Rotate

func Rotate(deg float64) Matrix

Rotate returns a rotation by deg degrees. In waffle's y-down space a positive angle rotates CLOCKWISE as seen on screen, matching CSS `rotate()`. The matrix uses the standard trigonometric form

A = cos θ   B = sin θ   C = -sin θ   D = cos θ

(θ = deg in radians); it appears clockwise because y increases downward. For example Rotate(90) maps (1,0) -> (0,1), i.e. the +x axis turns toward +y (down on screen).

func Scale

func Scale(sx, sy float64) Matrix

Scale returns a scale by sx horizontally and sy vertically.

func Skew

func Skew(ax, ay float64) Matrix

Skew returns a skew by ax degrees along x and ay degrees along y, matching CSS `skew(ax, ay)` == matrix(1, tan(ay), tan(ax), 1, 0, 0).

func Translate

func Translate(tx, ty float64) Matrix

Translate returns a translation by (tx, ty) in points.

func (Matrix) AboutOrigin

func (m Matrix) AboutOrigin(ox, oy float64) Matrix

AboutOrigin returns m applied about the point (ox, oy) instead of about the coordinate origin, i.e. translate(ox,oy) ∘ m ∘ translate(-ox,-oy). This is what a renderer uses to honor CSS transform-origin: wrap an element's own transform M as element.AboutOrigin(originX, originY).

func (Matrix) Apply

func (m Matrix) Apply(x, y float64) (float64, float64)

Apply maps the point (x, y) through m and returns the transformed point.

func (Matrix) Mul

func (m Matrix) Mul(n Matrix) Matrix

Mul returns the composition m∘n: the transform that applies n first and then m, so (m.Mul(n)).Apply(p) == m.Apply(n.Apply(p)). Composition is associative but not commutative.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL