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 ¶
- Variables
- func ApplyMedia(style map[string]any, ctx MediaContext) map[string]any
- func ExpandShorthands(in map[string]any) map[string]any
- func Flatten(style any) map[string]any
- func Inherit(parent, child map[string]any, isText bool) map[string]any
- func IsMediaKey(k string) bool
- func MatchMedia(key string, ctx MediaContext) (bool, error)
- func ParseFontWeight(v any) (int, bool)
- func Resolve(style any, media MediaContext) map[string]any
- type Color
- type Context
- type MediaContext
- type Unit
- type Value
Constants ¶
This section is empty.
Variables ¶
var Auto = Value{Unit: UnitAuto}
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
Color is an RGBA color. R, G, B are 0–255; A is 0–1 (1 = opaque).
func ParseColor ¶
ParseColor parses a CSS color string: #hex (3/4/6/8 digits), rgb()/rgba(), hsl()/hsla(), a named color, or "transparent".
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 Value ¶
Value is a dimensioned style value, e.g. 10pt, 50%, 2in, or auto.
func ParseValue ¶
ParseValue parses a style value from a number or a CSS-ish string.
func (Value) IsRelative ¶
IsRelative reports whether resolving the value needs a layout-time basis (percentages) or page dimensions (vw/vh).
func (Value) Resolve ¶
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.