stylesheet

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Oct 5, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package stylesheet parses react-pdf-shaped style values into typed, resolved forms for the layout engine: dimensioned values and their unit conversions, colors, font weights, and style flattening. Values arrive from the contract as numbers (json.Number) or CSS-ish strings and are interpreted here, keeping the engine the single source of truth for style semantics.

Index

Constants

This section is empty.

Variables

Auto is the "auto" value.

Functions

func ApplyMedia

func ApplyMedia(style map[string]any, ctx MediaContext) map[string]any

ApplyMedia merges matching @media blocks into the base style and drops the @media keys. Base props apply first; matching blocks are then applied in sorted key order (deterministic) so a later block overrides an earlier one. The input map is not modified.

func ExpandShorthands

func ExpandShorthands(in map[string]any) map[string]any

ExpandShorthands rewrites CSS-style shorthands in a flattened style map into their longhand properties, returning a new map. Non-shorthand properties pass through unchanged. More specific inputs win over less specific ones (an explicit marginTop overrides margin; a per-side border overrides the border shorthand).

Handled: margin/padding (+Horizontal/Vertical, 1–4 value box), border and its per-side forms (width/style/color, incl. "1pt solid red"), borderRadius (single value) and per-corner radii, and gap (row/column). flex and transform string shorthands are handled in their own passes and are left untouched here.

func Flatten

func Flatten(style any) map[string]any

Flatten merges a react-pdf style value into a single property map. A style may be a single object or an array of objects (and nested arrays); later entries win, matching react-pdf's `style={[a, b]}` semantics. nil and non-map leaves are ignored. The returned map is always non-nil and freshly allocated.

func Inherit

func Inherit(parent, child map[string]any, isText bool) map[string]any

Inherit returns the child's effective style map: inheritable properties from the parent's effective style fill in properties the child has not set (the child's own values always win). isText enables the Text-only inherited properties. textDecoration is merged so nested underline/line-through combine, unless the child sets "none". Apply this top-down, passing each node's returned effective style as the parent for its own children. SVG subtrees skip this pass entirely.

func IsMediaKey

func IsMediaKey(k string) bool

IsMediaKey reports whether a style key is an @media block.

func MatchMedia

func MatchMedia(key string, ctx MediaContext) (bool, error)

MatchMedia evaluates a media query key such as "@media max-width: 400 and orientation: portrait" against ctx. A bare "@media" matches everything. Supported features: min-width, max-width, min-height, max-height, orientation.

func ParseFontWeight

func ParseFontWeight(v any) (int, bool)

ParseFontWeight resolves a font weight from a number or a keyword string. A bare or "normal" weight is 400; "bold" is 700.

func Resolve

func Resolve(style any, media MediaContext) map[string]any

Resolve turns a raw react-pdf style value into a flat map of longhand properties: it flattens object/array styles (later wins), merges matching @media blocks for the page context, then expands shorthands. Unit conversion (which needs a layout-time basis) and inheritance (which needs the parent) are applied later, by the caller.

Types

type Color

type Color struct {
	R, G, B uint8
	A       float64
}

Color is an RGBA color. R, G, B are 0–255; A is 0–1 (1 = opaque).

func ParseColor

func ParseColor(s string) (Color, error)

ParseColor parses a CSS color string: #hex (3/4/6/8 digits), rgb()/rgba(), hsl()/hsla(), a named color, or "transparent".

func (Color) RGB

func (c Color) RGB() (r, g, b float64)

RGB returns the color's components normalized to 0–1, ignoring alpha.

type Context

type Context struct {
	DPI     float64 // device pixels per inch for px; default 72
	RemBase float64 // point size of 1rem; default 18
	PageW   float64 // page width in points, for vw
	PageH   float64 // page height in points, for vh
}

Context carries the resolution environment for units. Zero fields fall back to react-pdf's defaults via Context.withDefaults.

type MediaContext

type MediaContext struct {
	Width       float64
	Height      float64
	Orientation string // "portrait" or "landscape"
}

MediaContext is the environment a media query is evaluated against: the page dimensions (in points) and orientation.

type Unit

type Unit int

Unit is a length unit. The zero value is points, react-pdf's default.

const (
	UnitPt Unit = iota // points (default for bare numbers)
	UnitIn
	UnitMm
	UnitCm
	UnitPx
	UnitRem
	UnitPercent
	UnitVw
	UnitVh
	UnitAuto
)

type Value

type Value struct {
	Amount float64
	Unit   Unit
}

Value is a dimensioned style value, e.g. 10pt, 50%, 2in, or auto.

func ParseValue

func ParseValue(v any) (Value, error)

ParseValue parses a style value from a number or a CSS-ish string.

func (Value) IsAuto

func (v Value) IsAuto() bool

IsAuto reports whether the value is "auto".

func (Value) IsRelative

func (v Value) IsRelative() bool

IsRelative reports whether resolving the value needs a layout-time basis (percentages) or page dimensions (vw/vh).

func (Value) Resolve

func (v Value) Resolve(ctx Context, basis float64) float64

Resolve converts the value to points. basis is the reference length for percentages (e.g. the parent's content width); vw/vh use the page dimensions from ctx. Auto resolves to 0.

NOTE: px uses value*72/dpi (no rounding); verify against react-pdf's units.ts when wiring the full style pass.

func (Value) ToPoints

func (v Value) ToPoints(ctx Context) (float64, bool)

ToPoints converts an absolute value to points. It returns ok=false for auto and for relative units (percent/vw/vh), which require Resolve with a basis.

Jump to

Keyboard shortcuts

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