Skip to content

@0xdolan/deq-core

The reference renderer for Deq shapes. Pure ESM, no DOM, no I/O.

import { render, listShapes, compose } from "@0xdolan/deq-core";
const svg = render("roj-half-sun", { size: 48, color: "#ED2024" });

Source: packages/core. Install with npm install @0xdolan/deq-core.

Functions

compose

function compose(parts: readonly CompositionPart[], options?: RenderOptions): string

Render several catalog shapes placed on one 24 x 24 canvas. Stroke widths and dot radii are not scaled. options.params is not used (give each part its own params).

@throws {DeqError} INVALID_OPTIONS, UNKNOWN_SHAPE or INVALID_PARAMETER.

composeShape

function composeShape(parts: readonly CompositionPart[]): ShapeData

Build the shape description of a composition: one use primitive per part, each on its own layer. The result can be passed to prepare or saved as a shape spec.

@throws {DeqError} INVALID_OPTIONS or UNKNOWN_SHAPE.

cosSin

function cosSin(deg: number): Vec

Cosine and sine of deg degrees, exact at multiples of 90.

element

function element(item: Flat, o: ResolvedOptions): string

One SVG element; stroke attributes are inherited from the root.

escapeXml

function escapeXml(s: string): string

Escape text for XML character data and double-quoted attributes; drop invalid characters.

expand

function expand(id: string, params?: Readonly<Record<string, ParamValue>>): Flat[]

Expand the catalog shape id into flat geometry.

expandShape

function expandShape(shape: ShapeData, params?: Readonly<Record<string, ParamValue>>): Flat[]

Expand shape into flat geometry with the given parameter values.

flagSize

function flagSize(variant: FlagVariant, width: number): [number, number]

The pixel size of the flag at width px for a variant.

getShape

function getShape(id: string): ShapeData

The shape with this id. Throws DeqError (UNKNOWN_SHAPE) with suggestions otherwise.

hasShape

function hasShape(id: string): boolean

True when a shape with this id exists.

listCategories

function listCategories(): readonly string[]

Category ids in catalog order.

listFlagVariants

function listFlagVariants(): readonly string[]

Variant ids in spec order.

listShapes

function listShapes(category?: string | undefined): readonly ShapeData[]

Every shape, in catalog order, optionally only one category.

num

function num(x: number): string

Format a number with at most 3 decimals: roundHalfEven(x * 1000) thousandths, printed without trailing zeros, without a leading +, and with -0 as 0.

prepare

function prepare(shape: ShapeData, options?: RenderOptions): Prepared

Validate options for shape and expand its geometry, ready for svgDocument or another output format.

@throws {DeqError} INVALID_OPTIONS, INVALID_PARAMETER or LIMIT_EXCEEDED.

render

function render(id: string, options?: RenderOptions): string

Render the catalog shape id as a standalone SVG string.

render("roj-half-sun", { size: 48, strokeWidth: 1.5, color: "#ED2024", params: { rays: 9 } });

@throws {DeqError} UNKNOWN_SHAPE, INVALID_OPTIONS or INVALID_PARAMETER.

renderFlag

function renderFlag(options?: FlagOptions): string

Render the Kurdish flag as a standalone SVG string.

renderFlag({ variant: "outline", width: 300 });

@throws {DeqError} INVALID_OPTIONS for an unknown variant or a bad width.

renderShapeData

function renderShapeData(shape: ShapeData, options?: RenderOptions): string

Render a shape description (already validated) to an SVG string.

resolveOptions

function resolveOptions(options?: unknown): ResolvedOptions

Validate options and fill in defaults. Throws DeqError (INVALID_OPTIONS).

resolveParams

function resolveParams(shape: ShapeData, overrides?: Readonly<Record<string, ParamValue>>): Record<string, ParamValue>

Parameter defaults merged with overrides, type- and range-checked.

roundHalfEven

function roundHalfEven(v: number): number

Round half to even (banker’s rounding) on a finite double.

svgDocument

function svgDocument(p: Prepared, hooks?: SvgHooks): string

Build the SVG document for prepared geometry. With no hooks this is exactly what render returns.

svgRoot

function svgRoot(o: ResolvedOptions, defaultTitle: string, hooks?: SvgHooks, width?: number, height?: number): string

The opening <svg> tag with its title and background, for a canvas of width x height grid units (24 x 24 for a shape). o.size is the pixel size of the padded 24-unit box, so wider canvases keep the same scale. Used by svgDocument and by scenes made of several shapes, such as Deq Leet text.

Classes

DeqError

class DeqError

The only error type the library throws. code is stable; message is for people.

  • name: 'DeqError'
  • code: DeqErrorCode
  • issues: readonly string[]: Validation problems, one line each (for INVALID_OPTIONS).

Constants

CANVAS

const CANVAS: 24

Canvas size in grid units.

DEFAULT_OPTIONS

const DEFAULT_OPTIONS: ResolvedOptions

The documented default of every option (used by “reset” controls).

FLAG

const FLAG: FlagData

The render-relevant part of spec/flag.json, paint resolved to colours.

MAX_DEPTH

const MAX_DEPTH: 6

Deepest nesting of generators and references.

MAX_FLAT

const MAX_FLAT: 2000

Most flat items one render may produce.

packageName

const packageName: '@0xdolan/deq-core'

The npm package name.

Interfaces

ArcPrimitive

interface ArcPrimitive
  • type: 'arc'
  • cx: number
  • cy: number
  • r: number
  • start: number: Degrees, clockwise on screen from the positive x axis.
  • end: number
  • id?: string
  • layer?: string
  • when?: Readonly<Record<string, ParamValue | readonly (string | number)[]>>: Draw only when every listed parameter has the value (or one of the values).

CirclePrimitive

interface CirclePrimitive
  • type: 'circle'
  • cx: number
  • cy: number
  • r: number
  • paint?: Paint
  • id?: string
  • layer?: string
  • when?: Readonly<Record<string, ParamValue | readonly (string | number)[]>>: Draw only when every listed parameter has the value (or one of the values).

CompositionPart

interface CompositionPart

One shape placed in a composition.

  • shape: string: Catalog shape id.
  • params?: Readonly<Record<string, ParamValue>> | undefined: Parameter values of that shape.
  • transform?: UseTransform | undefined: Placement: p' = R(rotate) * (scale * (p - origin)) + origin + translate.
  • layer?: string: Layer id for this part. Default: the shape id.

ConcentricPrimitive

interface ConcentricPrimitive
  • type: 'concentric'
  • count: Count
  • center: Vec
  • scaleStep: number
  • element: Primitive
  • id?: string
  • layer?: string
  • when?: Readonly<Record<string, ParamValue | readonly (string | number)[]>>: Draw only when every listed parameter has the value (or one of the values).

DotPrimitive

interface DotPrimitive
  • type: 'dot'
  • cx: number
  • cy: number
  • r?: number
  • id?: string
  • layer?: string
  • when?: Readonly<Record<string, ParamValue | readonly (string | number)[]>>: Draw only when every listed parameter has the value (or one of the values).

EllipsePrimitive

interface EllipsePrimitive
  • type: 'ellipse'
  • cx: number
  • cy: number
  • rx: number
  • ry: number
  • paint?: Paint
  • id?: string
  • layer?: string
  • when?: Readonly<Record<string, ParamValue | readonly (string | number)[]>>: Draw only when every listed parameter has the value (or one of the values).

FlagData

interface FlagData

The render-relevant part of spec/flag.json.

  • name: string
  • viewBox: readonly [number, number]
  • bands: readonly { readonly y: number; readonly height: number; readonly color: string; }[]
  • sun: { readonly rays: number; readonly rotation: number; readonly center: readonly [number, number]; readonly outerRadius: number; readonly innerRadius: number; }
  • variants: readonly FlagVariant[]

FlagOptions

interface FlagOptions

Options of renderFlag.

  • variant?: string: Variant id from spec/flag.json. Default full-color.
  • width?: number: Output width in px (an integer); the height follows the aspect ratio. Default 900.
  • title?: string: Accessible title. Default “Flag of Kurdistan”. "" marks the image decorative.

FlagVariant

interface FlagVariant

One flag variant with its paint resolved to colours (null = not drawn).

  • id: string
  • description: string
  • crop: readonly [number, number]: Visible part of the 900 x 600 field: left edge and width.
  • field: string | null
  • bands: boolean: Paint the red, white and green bands.
  • dividers: string | null
  • frame: string | null
  • sun: string | null
  • sunOutline: string | null
  • strokeWidth: number: Width of frame, divider and outline strokes, in field units.

FlatArc

interface FlatArc
  • kind: 'arc'
  • cx: number
  • cy: number
  • r: number
  • start: number
  • end: number
  • layer: string
  • paint: Paint

FlatCircle

interface FlatCircle

A circle; dot circles are filled and keep their radius under scaling.

  • kind: 'circle'
  • cx: number
  • cy: number
  • r: number
  • layer: string
  • paint: Paint
  • dot: boolean

FlatEllipse

interface FlatEllipse
  • kind: 'ellipse'
  • cx: number
  • cy: number
  • rx: number
  • ry: number
  • rotation: number
  • layer: string
  • paint: Paint

FlatPath

interface FlatPath
  • kind: 'path'
  • segments: readonly Segment[]
  • layer: string
  • paint: Paint

FlatPoly

interface FlatPoly

A line, polyline or polygon.

  • kind: 'poly'
  • points: readonly Vec[]
  • closed: boolean
  • layer: string
  • paint: Paint

KurdishName

interface KurdishName

A Kurdish name. text is null until a native speaker has reviewed it.

  • text: string | null
  • status: string

LinePrimitive

interface LinePrimitive
  • type: 'line'
  • x1: number
  • y1: number
  • x2: number
  • y2: number
  • id?: string
  • layer?: string
  • when?: Readonly<Record<string, ParamValue | readonly (string | number)[]>>: Draw only when every listed parameter has the value (or one of the values).

PathPrimitive

interface PathPrimitive
  • type: 'path'
  • d: string
  • paint?: Paint
  • id?: string
  • layer?: string
  • when?: Readonly<Record<string, ParamValue | readonly (string | number)[]>>: Draw only when every listed parameter has the value (or one of the values).

PolygonPrimitive

interface PolygonPrimitive
  • type: 'polygon'
  • points: readonly Vec[]
  • paint?: Paint
  • id?: string
  • layer?: string
  • when?: Readonly<Record<string, ParamValue | readonly (string | number)[]>>: Draw only when every listed parameter has the value (or one of the values).

PolylinePrimitive

interface PolylinePrimitive
  • type: 'polyline'
  • points: readonly Vec[]
  • id?: string
  • layer?: string
  • when?: Readonly<Record<string, ParamValue | readonly (string | number)[]>>: Draw only when every listed parameter has the value (or one of the values).

Prepared

interface Prepared

A shape ready to draw: resolved options and flat geometry in canvas units.

  • options: ResolvedOptions: The validated options with every default filled in.
  • items: readonly Flat[]: The geometry, in drawing order.
  • layers: readonly string[]: The layer ids of the shape, in declaration order.
  • defaultTitle: string: The title used when options.title is not set (the English name).
  • width?: number: Canvas width in grid units when it is not 24 (scenes). Default: 24.
  • height?: number: Canvas height in grid units when it is not 24 (scenes). Default: 24.

RadialPrimitive

interface RadialPrimitive
  • type: 'radial'
  • count: Count
  • center: Vec
  • start: number
  • sweep: number
  • element: Primitive
  • id?: string
  • layer?: string
  • when?: Readonly<Record<string, ParamValue | readonly (string | number)[]>>: Draw only when every listed parameter has the value (or one of the values).

RectPrimitive

interface RectPrimitive
  • type: 'rect'
  • x: number
  • y: number
  • width: number
  • height: number
  • paint?: Paint
  • id?: string
  • layer?: string
  • when?: Readonly<Record<string, ParamValue | readonly (string | number)[]>>: Draw only when every listed parameter has the value (or one of the values).

RenderOptions

interface RenderOptions

Options accepted by render and compose. Every option is optional; see DEFAULT_OPTIONS for the defaults.

  • size?: number: Output width and height in px. Default 24.
  • color?: string: Stroke and dot colour (CSS colour). Default currentColor.
  • strokeWidth?: number: Stroke width in grid units. Default 2.
  • absoluteStrokeWidth?: boolean: Keep the stroke strokeWidth px wide at any size. Default false.
  • fill?: string: Fill for closed outlines (circles, polygons, closed paths). Default none.
  • fillOpacity?: number: Opacity of fill, 0 to 1. Default 1.
  • strokeOpacity?: number: Opacity of the ink (strokes and dots), 0 to 1. Default 1.
  • linecap?: 'round' | 'butt' | 'square': Default round.
  • linejoin?: 'round' | 'miter' | 'bevel': Default round.
  • dotScale?: number: Multiplies every dot radius. Default 1.
  • rotation?: number: Degrees clockwise about the canvas centre. Default 0.
  • flipX?: boolean: Mirror left to right. Default false.
  • flipY?: boolean: Mirror top to bottom. Default false.
  • padding?: number: Extra space around the 24 x 24 canvas, in grid units. Default 0.
  • background?: string: Background colour painted behind the shape, or none. Default none.
  • layerColors?: Readonly<Record<string, string>>: Colour per layer id, overriding color for that layer.
  • title?: string: Accessible title. Default: the shape’s English name. "" marks the image decorative.
  • ariaLabel?: string: Accessible label. Default: the title.
  • params?: Readonly<Record<string, ParamValue>>: Shape parameter values, for example { rays: 9 }. Default: each parameter’s default.

RepeatPrimitive

interface RepeatPrimitive
  • type: 'repeat'
  • count: Count
  • step?: Vec
  • spread?: Vec
  • align?: 'start' | 'center'
  • element: Primitive
  • id?: string
  • layer?: string
  • when?: Readonly<Record<string, ParamValue | readonly (string | number)[]>>: Draw only when every listed parameter has the value (or one of the values).

ResolvedOptions

interface ResolvedOptions

Options after validation, with every default filled in.

  • size: number
  • color: string
  • strokeWidth: number
  • absoluteStrokeWidth: boolean
  • fill: string
  • fillOpacity: number
  • strokeOpacity: number
  • linecap: 'round' | 'butt' | 'square'
  • linejoin: 'round' | 'miter' | 'bevel'
  • dotScale: number
  • rotation: number
  • flipX: boolean
  • flipY: boolean
  • padding: number
  • background: string
  • layerColors: Readonly<Record<string, string>>
  • title: string | undefined
  • ariaLabel: string | undefined
  • params: Readonly<Record<string, ParamValue>>

ShapeData

interface ShapeData

A shape as shipped with the library.

  • id: string
  • version: number
  • category: string
  • names: { readonly en: string; readonly ckb: KurdishName; readonly kmr: KurdishName; }
  • aliases: readonly string[]
  • tags: readonly string[]
  • layers: readonly string[]
  • symmetry: string
  • parameters: Readonly<Record<string, ParameterDecl>>
  • primitives: readonly Primitive[]

StarPrimitive

interface StarPrimitive
  • type: 'star'
  • cx: number
  • cy: number
  • points: Count
  • outerRadius: number
  • innerRadius: number
  • rotation: number
  • paint?: Paint
  • id?: string
  • layer?: string
  • when?: Readonly<Record<string, ParamValue | readonly (string | number)[]>>: Draw only when every listed parameter has the value (or one of the values).

SvgHooks

interface SvgHooks

Hooks for svgDocument; used by exporters that need named layer groups.

  • rootAttrs?: string: Extra attributes on the root <svg>, starting with a space.
  • layerAttrs?: (layer: string) => string: Extra attributes for the group of layer, starting with a space. When given, every layer gets its own group and the rotation/flip transform moves onto each group.

UsePrimitive

interface UsePrimitive
  • type: 'use'
  • shape: string
  • params?: Readonly<Record<string, ParamValue>>
  • transform?: UseTransform
  • id?: string
  • layer?: string
  • when?: Readonly<Record<string, ParamValue | readonly (string | number)[]>>: Draw only when every listed parameter has the value (or one of the values).

UseTransform

interface UseTransform

Placement of a referenced shape: p' = R(rotate) * (scale * (p - origin)) + origin + translate.

  • translate?: Vec
  • scale?: number
  • rotate?: number
  • origin?: Vec

Types

Count

type Count = number | { readonly param: string }

A count, either fixed or read from a parameter.

DeqErrorCode

type DeqErrorCode = "UNKNOWN_SHAPE" | "INVALID_OPTIONS" | "INVALID_PARAMETER" | "INVALID_SHAPE" | "LIMIT_EXCEEDED"

Why an operation failed.

Flat

type Flat = FlatPoly | FlatCircle | FlatArc | FlatEllipse | FlatPath

One drawable item in canvas units.

Paint

type Paint = "stroke" | "fill"

How a closed primitive is painted.

ParamValue

type ParamValue = number | boolean | string

A shape parameter value.

ParameterDecl

type ParameterDecl = | { readonly type: "integer"; readonly min: number; readonly max: number; readonly default: number; readonly description: string; } | { readonly type: "boolean"; readonly default: boolean; readonly description: string } | { readonly type: "enum"; readonly values: readonly string[]; readonly default: string; readonly description: string; }

A shape-specific parameter declaration.

Params

type Params = Readonly<Record<string, ParamValue>>

Parameter values by name.

Primitive

type Primitive = | LinePrimitive | PolylinePrimitive | PolygonPrimitive | PathPrimitive | CirclePrimitive | DotPrimitive | ArcPrimitive | EllipsePrimitive | RectPrimitive | StarPrimitive | RadialPrimitive | RepeatPrimitive | ConcentricPrimitive | UsePrimitive

One typed element of a shape.

Segment

type Segment = | readonly ["M", Vec] | readonly ["L", Vec] | readonly ["Q", Vec, Vec] | readonly ["C", Vec, Vec, Vec] | readonly ["Z"]

A path segment with absolute points.

Vec

type Vec = readonly [number, number]

A point or vector in grid units.