bm-render 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/js/full.d.mts ADDED
@@ -0,0 +1,16 @@
1
+ // bm-render/full: the full build profile (bm_render.wasm) with its own module instance. Same API as bm-render
2
+ // (./index.d.mts); init() may be called without an argument (and on Node.js it already ran on import).
3
+ import type { InitArgument, WasmExports } from "./index.mjs";
4
+
5
+ export * from "./index.mjs";
6
+ /**
7
+ * Instantiates the full wasm. Without an argument it loads bm_render.wasm from next to the module
8
+ * (`new URL("./bm_render.wasm", import.meta.url)`, which bundlers emit as an asset). On Cloudflare Workers pass
9
+ * the module: `import wasm from "bm-render/full.wasm"; await init(wasm)`. Idempotent.
10
+ */
11
+ export declare function init(input?: InitArgument): Promise<WasmExports>;
12
+ export default init;
13
+ /** The URL init() loads by default (throws where `import.meta.url` is unusable). */
14
+ export declare function wasmUrl(): URL;
15
+ /** Build profile of this entry. */
16
+ export declare const profile: "full";
package/js/full.mjs ADDED
@@ -0,0 +1,20 @@
1
+ // bm-render/full (browsers, bundlers, Deno, edge runtimes): the full build profile (bm_render.wasm) with its own
2
+ // module instance. `await init()` without an argument loads bm_render.wasm from next to this file through
3
+ // `new URL("./bm_render.wasm", import.meta.url)`, a pattern Vite, webpack 5, Rollup and Parcel turn into an emitted
4
+ // asset. Where that cannot work (Cloudflare Workers / workerd), pass the precompiled module instead:
5
+ //
6
+ // import wasm from "bm-render/full.wasm"; // a WebAssembly.Module under wrangler
7
+ // import { init, Renderer } from "bm-render/full";
8
+ // await init(wasm);
9
+ //
10
+ // On Node.js this subpath resolves to ./node-full.mjs, which reads the file and initialises on import.
11
+ // API: see ./index.mjs and ./index.d.mts.
12
+ import { createBinding } from "./engine.mjs";
13
+
14
+ const binding = createBinding(() => new URL("./bm_render.wasm", import.meta.url));
15
+
16
+ export const { init, initSync, Renderer, features, setGlyphCacheMaxBytes, wasmUrl, backend } = binding;
17
+ export { Prepared, setWoffDecoder } from "./engine.mjs";
18
+ /** Build profile of this entry. */
19
+ export const profile = "full";
20
+ export default init;
package/js/index.d.mts ADDED
@@ -0,0 +1,381 @@
1
+ // Public API of bm-render (js/index.mjs = `bm-render`; the profile entries bm-render/core, /standard, /full
2
+ // add `profile`, `wasmUrl` and a default for init(), see core.d.mts). The node format and options follow
3
+ // `@takumi-rs/wasm` 2.20; see the parity table at the top of js/index.mjs.
4
+
5
+ declare global {
6
+ // `Symbol.dispose` for projects whose `lib` predates esnext.disposable (same declaration as TypeScript 5.2+ / @types/node).
7
+ interface SymbolConstructor {
8
+ readonly dispose: unique symbol;
9
+ }
10
+ // Lets the typings compile without the DOM lib (Node-only projects); merges with lib.dom / workers types.
11
+ namespace WebAssembly {
12
+ interface Module {}
13
+ }
14
+ }
15
+
16
+ import type { PdfMeasureOptions, PdfRenderOptions, MeasuredSize } from "./pdf-types.mjs";
17
+
18
+ export type ByteBuf = Uint8Array | ArrayBuffer | ArrayBufferView;
19
+ /**
20
+ * A fresh Uint8Array that owns its ArrayBuffer (`Uint8Array<ArrayBuffer>` on TypeScript 5.7+, so it can be passed
21
+ * straight to `new Response()` / `Blob`; plain `Uint8Array` on older versions).
22
+ */
23
+ export type OutputBytes = ReturnType<Uint8Array["slice"]>;
24
+
25
+ /** A CSS value: a string as in CSS, or a number (px for lengths, a plain number otherwise). */
26
+ export type StyleValue = string | number | null | undefined;
27
+
28
+ /**
29
+ * CSS declarations with camelCase keys: the properties bm-render parses (src/style/props.zig). Properties
30
+ * of a feature compiled out of the loaded build are accepted and ignored. Custom properties (`"--x"`) and
31
+ * any other key are allowed by the index signature.
32
+ */
33
+ export interface Style {
34
+ WebkitBackdropFilter?: StyleValue; WebkitBoxDecorationBreak?: StyleValue; WebkitBoxOrient?: StyleValue;
35
+ WebkitClipPath?: StyleValue; WebkitHyphens?: StyleValue; WebkitLineClamp?: StyleValue; WebkitMask?: StyleValue;
36
+ WebkitMaskClip?: StyleValue; WebkitMaskComposite?: StyleValue; WebkitMaskImage?: StyleValue;
37
+ WebkitMaskOrigin?: StyleValue; WebkitMaskPosition?: StyleValue; WebkitMaskRepeat?: StyleValue;
38
+ WebkitMaskSize?: StyleValue; WebkitTextFillColor?: StyleValue; WebkitTextStroke?: StyleValue;
39
+ WebkitTextStrokeColor?: StyleValue; WebkitTextStrokeWidth?: StyleValue; WebkitUserSelect?: StyleValue;
40
+ alignContent?: StyleValue; alignItems?: StyleValue; alignSelf?: StyleValue; animation?: StyleValue;
41
+ animationDelay?: StyleValue; animationDirection?: StyleValue; animationDuration?: StyleValue;
42
+ animationFillMode?: StyleValue; animationIterationCount?: StyleValue; animationName?: StyleValue;
43
+ animationPlayState?: StyleValue; animationTimingFunction?: StyleValue; aspectRatio?: StyleValue;
44
+ backdropFilter?: StyleValue; background?: StyleValue; backgroundAttachment?: StyleValue;
45
+ backgroundBlendMode?: StyleValue; backgroundClip?: StyleValue; backgroundColor?: StyleValue;
46
+ backgroundImage?: StyleValue; backgroundOrigin?: StyleValue; backgroundPosition?: StyleValue;
47
+ backgroundPositionX?: StyleValue; backgroundPositionY?: StyleValue; backgroundRepeat?: StyleValue;
48
+ backgroundSize?: StyleValue; blockSize?: StyleValue; border?: StyleValue; borderBlock?: StyleValue;
49
+ borderBlockColor?: StyleValue; borderBlockEnd?: StyleValue; borderBlockEndColor?: StyleValue;
50
+ borderBlockEndStyle?: StyleValue; borderBlockEndWidth?: StyleValue; borderBlockStart?: StyleValue;
51
+ borderBlockStartColor?: StyleValue; borderBlockStartStyle?: StyleValue; borderBlockStartWidth?: StyleValue;
52
+ borderBlockStyle?: StyleValue; borderBlockWidth?: StyleValue; borderBottom?: StyleValue;
53
+ borderBottomColor?: StyleValue; borderBottomLeftRadius?: StyleValue; borderBottomRightRadius?: StyleValue;
54
+ borderBottomStyle?: StyleValue; borderBottomWidth?: StyleValue; borderCollapse?: StyleValue;
55
+ borderColor?: StyleValue; borderEndEndRadius?: StyleValue; borderEndStartRadius?: StyleValue;
56
+ borderInline?: StyleValue; borderInlineColor?: StyleValue; borderInlineEnd?: StyleValue;
57
+ borderInlineEndColor?: StyleValue; borderInlineEndStyle?: StyleValue; borderInlineEndWidth?: StyleValue;
58
+ borderInlineStart?: StyleValue; borderInlineStartColor?: StyleValue; borderInlineStartStyle?: StyleValue;
59
+ borderInlineStartWidth?: StyleValue; borderInlineStyle?: StyleValue; borderInlineWidth?: StyleValue;
60
+ borderLeft?: StyleValue; borderLeftColor?: StyleValue; borderLeftStyle?: StyleValue; borderLeftWidth?: StyleValue;
61
+ borderRadius?: StyleValue; borderRight?: StyleValue; borderRightColor?: StyleValue; borderRightStyle?: StyleValue;
62
+ borderRightWidth?: StyleValue; borderSpacing?: StyleValue; borderStartEndRadius?: StyleValue;
63
+ borderStartStartRadius?: StyleValue; borderStyle?: StyleValue; borderTop?: StyleValue; borderTopColor?: StyleValue;
64
+ borderTopLeftRadius?: StyleValue; borderTopRightRadius?: StyleValue; borderTopStyle?: StyleValue;
65
+ borderTopWidth?: StyleValue; borderWidth?: StyleValue; bottom?: StyleValue; boxDecorationBreak?: StyleValue;
66
+ boxShadow?: StyleValue; boxSizing?: StyleValue; captionSide?: StyleValue; clear?: StyleValue;
67
+ clipPath?: StyleValue; color?: StyleValue; columnGap?: StyleValue; contain?: StyleValue; content?: StyleValue;
68
+ counterIncrement?: StyleValue; counterReset?: StyleValue; counterSet?: StyleValue; cursor?: StyleValue;
69
+ direction?: StyleValue; display?: StyleValue; emptyCells?: StyleValue; filter?: StyleValue; flex?: StyleValue;
70
+ flexBasis?: StyleValue; flexDirection?: StyleValue; flexFlow?: StyleValue; flexGrow?: StyleValue;
71
+ flexShrink?: StyleValue; flexWrap?: StyleValue; float?: StyleValue; font?: StyleValue; fontFamily?: StyleValue;
72
+ fontFeatureSettings?: StyleValue; fontKerning?: StyleValue; fontOpticalSizing?: StyleValue; fontSize?: StyleValue;
73
+ fontStretch?: StyleValue; fontStyle?: StyleValue; fontSynthesis?: StyleValue; fontSynthesisSmallCaps?: StyleValue;
74
+ fontSynthesisStyle?: StyleValue; fontSynthesisWeight?: StyleValue; fontVariant?: StyleValue;
75
+ fontVariantCaps?: StyleValue; fontVariantEastAsian?: StyleValue; fontVariantLigatures?: StyleValue;
76
+ fontVariantNumeric?: StyleValue; fontVariantPosition?: StyleValue; fontVariationSettings?: StyleValue;
77
+ fontWeight?: StyleValue; gap?: StyleValue; grid?: StyleValue; gridArea?: StyleValue; gridAutoColumns?: StyleValue;
78
+ gridAutoFlow?: StyleValue; gridAutoRows?: StyleValue; gridColumn?: StyleValue; gridColumnEnd?: StyleValue;
79
+ gridColumnStart?: StyleValue; gridRow?: StyleValue; gridRowEnd?: StyleValue; gridRowStart?: StyleValue;
80
+ gridTemplate?: StyleValue; gridTemplateAreas?: StyleValue; gridTemplateColumns?: StyleValue;
81
+ gridTemplateRows?: StyleValue; height?: StyleValue; hyphens?: StyleValue; imageRendering?: StyleValue;
82
+ inlineSize?: StyleValue; inset?: StyleValue; insetBlock?: StyleValue; insetBlockEnd?: StyleValue;
83
+ insetBlockStart?: StyleValue; insetInline?: StyleValue; insetInlineEnd?: StyleValue; insetInlineStart?: StyleValue;
84
+ isolation?: StyleValue; justifyContent?: StyleValue; justifyItems?: StyleValue; justifySelf?: StyleValue;
85
+ left?: StyleValue; letterSpacing?: StyleValue; lineBreak?: StyleValue; lineClamp?: StyleValue;
86
+ lineHeight?: StyleValue; listStyle?: StyleValue; listStyleImage?: StyleValue; listStylePosition?: StyleValue;
87
+ listStyleType?: StyleValue; margin?: StyleValue; marginBlock?: StyleValue; marginBlockEnd?: StyleValue;
88
+ marginBlockStart?: StyleValue; marginBottom?: StyleValue; marginInline?: StyleValue; marginInlineEnd?: StyleValue;
89
+ marginInlineStart?: StyleValue; marginLeft?: StyleValue; marginRight?: StyleValue; marginTop?: StyleValue;
90
+ mask?: StyleValue; maskClip?: StyleValue; maskComposite?: StyleValue; maskImage?: StyleValue;
91
+ maskMode?: StyleValue; maskOrigin?: StyleValue; maskPosition?: StyleValue; maskRepeat?: StyleValue;
92
+ maskSize?: StyleValue; maskType?: StyleValue; maxBlockSize?: StyleValue; maxHeight?: StyleValue;
93
+ maxInlineSize?: StyleValue; maxWidth?: StyleValue; minBlockSize?: StyleValue; minHeight?: StyleValue;
94
+ minInlineSize?: StyleValue; minWidth?: StyleValue; mixBlendMode?: StyleValue; objectFit?: StyleValue;
95
+ objectPosition?: StyleValue; opacity?: StyleValue; order?: StyleValue; outline?: StyleValue;
96
+ outlineColor?: StyleValue; outlineOffset?: StyleValue; outlineStyle?: StyleValue; outlineWidth?: StyleValue;
97
+ overflow?: StyleValue; overflowBlock?: StyleValue; overflowInline?: StyleValue; overflowWrap?: StyleValue;
98
+ overflowX?: StyleValue; overflowY?: StyleValue; padding?: StyleValue; paddingBlock?: StyleValue;
99
+ paddingBlockEnd?: StyleValue; paddingBlockStart?: StyleValue; paddingBottom?: StyleValue;
100
+ paddingInline?: StyleValue; paddingInlineEnd?: StyleValue; paddingInlineStart?: StyleValue;
101
+ paddingLeft?: StyleValue; paddingRight?: StyleValue; paddingTop?: StyleValue; paintOrder?: StyleValue;
102
+ placeContent?: StyleValue; placeItems?: StyleValue; placeSelf?: StyleValue; pointerEvents?: StyleValue;
103
+ position?: StyleValue; right?: StyleValue; rotate?: StyleValue; rowGap?: StyleValue; scale?: StyleValue;
104
+ tabSize?: StyleValue; tableLayout?: StyleValue; textAlign?: StyleValue; textAlignLast?: StyleValue;
105
+ textDecoration?: StyleValue; textDecorationColor?: StyleValue; textDecorationLine?: StyleValue;
106
+ textDecorationSkipInk?: StyleValue; textDecorationStyle?: StyleValue; textDecorationThickness?: StyleValue;
107
+ textIndent?: StyleValue; textOverflow?: StyleValue; textShadow?: StyleValue; textTransform?: StyleValue;
108
+ textUnderlineOffset?: StyleValue; textUnderlinePosition?: StyleValue; textWrap?: StyleValue;
109
+ textWrapMode?: StyleValue; textWrapStyle?: StyleValue; top?: StyleValue; transform?: StyleValue;
110
+ transformOrigin?: StyleValue; transition?: StyleValue; transitionBehavior?: StyleValue;
111
+ transitionDelay?: StyleValue; transitionDuration?: StyleValue; transitionProperty?: StyleValue;
112
+ transitionTimingFunction?: StyleValue; translate?: StyleValue; unicodeBidi?: StyleValue; userSelect?: StyleValue;
113
+ verticalAlign?: StyleValue; visibility?: StyleValue; whiteSpace?: StyleValue; whiteSpaceCollapse?: StyleValue;
114
+ width?: StyleValue; willChange?: StyleValue; wordBreak?: StyleValue; wordSpacing?: StyleValue;
115
+ wordWrap?: StyleValue; writingMode?: StyleValue; zIndex?: StyleValue;
116
+ [property: string]: StyleValue;
117
+ }
118
+
119
+ export type NodeMetadata = {
120
+ style?: Style;
121
+ /** user-agent style layer, applied below `style` (takumi `preset`) */
122
+ preset?: Style;
123
+ /** Tailwind utility classes (`tw` feature: standard / full) */
124
+ tw?: string;
125
+ /**
126
+ * element name, used only to match type selectors (`p { ... }`, `div > span`) in `css`; it does NOT apply
127
+ * the HTML user-agent default styles of that element to a node tree (set them in `style` / `preset`)
128
+ */
129
+ tagName?: string;
130
+ className?: string;
131
+ id?: string;
132
+ attributes?: Record<string, string>;
133
+ dir?: "ltr" | "rtl" | "auto";
134
+ lang?: string;
135
+ };
136
+ export type ContainerNode = NodeMetadata & { type: "container"; children?: (Node | null | undefined)[] };
137
+ export type TextNode = NodeMetadata & { type: "text"; text: string };
138
+ /** Raw pixels as an image source (straight alpha unless `premultiplied`). */
139
+ export type RgbaImage = { width: number; height: number; data: ByteBuf; premultiplied?: boolean };
140
+ export type ImageNode = NodeMetadata & {
141
+ type: "image";
142
+ /** data: URI, inline `<svg>` markup, a key of the `images` option, encoded bytes, or raw RGBA. */
143
+ src: string | ByteBuf | RgbaImage;
144
+ width?: number;
145
+ height?: number;
146
+ };
147
+ export type Node = ContainerNode | TextNode | ImageNode;
148
+ /** A node tree, `{ json }` (the tree as JSON text) or an HTML string (`html` feature: standard / full). */
149
+ export type Source = Node | string | { json: string };
150
+
151
+ /** An entry of the `images` option: `src` is the key nodes refer to; `data` the encoded bytes. */
152
+ export type ImageSource = { src: string; data: ByteBuf | (() => ByteBuf | Promise<ByteBuf>) | Promise<ByteBuf>; cache?: "auto" | "none" };
153
+ export type ImagesInput = ImageSource[] | { sources?: ImageSource[]; cache?: "auto" | "none" };
154
+ /** takumi rule object (`{ selector, style }`, `{ keyframes, steps }`, ...) or stylesheet text (`css` feature). */
155
+ export type CssInput = string | Record<string, unknown>;
156
+ export type KeyframesMap = Record<string, Record<string, Style>>;
157
+ export type KeyframesRuleList = { name: string; keyframes: { offsets: number[]; declarations: Style }[] }[];
158
+ export type Keyframes = KeyframesMap | KeyframesRuleList;
159
+
160
+ export type FontData = ByteBuf | (() => ByteBuf | Promise<ByteBuf>) | Promise<ByteBuf>;
161
+ export type FontDetails = {
162
+ /** family name (default: the font's own family name) */
163
+ name?: string;
164
+ data: FontData;
165
+ /** weight override (default: from the font) */
166
+ weight?: number;
167
+ /** "normal" | "italic" | "oblique" override (default: from the font) */
168
+ style?: string;
169
+ /** PDF async preparation/rendering skips subsets whose characters are unused; image rendering registers eagerly. */
170
+ unicodeRange?: [number, number][] | string;
171
+ /** accepted, ignored */
172
+ subsetOf?: string;
173
+ /** accepted, ignored */
174
+ generic?: string;
175
+ };
176
+ /** FontDetails whose `data` is available synchronously (registerFont then returns synchronously). */
177
+ export type SyncFontDetails = FontDetails & { data: ByteBuf };
178
+ /** A font: bytes, a URL (fetched), or details. */
179
+ export type FontLoader = string | URL | ByteBuf | FontDetails;
180
+ export type RegisteredFace = { weight: number; style: string; width: number; index: number };
181
+ export type RegisteredFamily = { name: string; faces: RegisteredFace[]; weight?: number; style?: string };
182
+
183
+ /** Output formats. `ico` needs the full profile; `raw` = straight RGBA8, width*height*4 bytes. */
184
+ export type OutputFormat = "png" | "jpeg" | "webp" | "ico" | "raw";
185
+ export type AnimationOutputFormat = "webp" | "apng" | "gif";
186
+
187
+ export type CommonOptions = {
188
+ devicePixelRatio?: number;
189
+ images?: ImagesInput;
190
+ /** stylesheets / rule objects (`css` feature: standard / full) */
191
+ css?: CssInput[] | string;
192
+ /** deprecated alias of `css` (strings) */
193
+ stylesheets?: string[];
194
+ /** deprecated top-level keyframes */
195
+ keyframes?: Keyframes;
196
+ /** root font-family fallback stack */
197
+ fontFamilies?: string[];
198
+ /** root language (`:lang()`, OpenType language system) */
199
+ lang?: string;
200
+ /** Fonts registered before rendering, once per renderer (deduped across calls). The sync methods take bytes and `{ data: bytes }` entries; a URL, a promise or a lazy `data()` loader needs the async API. */
201
+ fonts?: FontLoader[] | Promise<FontLoader[]>;
202
+ /** checked before rendering */
203
+ signal?: AbortSignal;
204
+ /** accepted, ignored */
205
+ drawDebugBorder?: boolean;
206
+ };
207
+
208
+ export type RenderOptions = CommonOptions & {
209
+ /** output width in pixels (default: the root's laid-out width); `devicePixelRatio` scales CSS lengths, not the image */
210
+ width?: number;
211
+ /** output height in pixels (default: the root's laid-out height) */
212
+ height?: number;
213
+ format?: OutputFormat;
214
+ /** jpeg quality 1..100 (default 75); ignored for webp (always lossless) */
215
+ quality?: number;
216
+ /** accepted, ignored (webp is always lossless, like takumi-wasm) */
217
+ lossless?: boolean;
218
+ /** CSS animation timeline position in ms (integer; default 0) */
219
+ timeMs?: number;
220
+ /** accepted, ignored */
221
+ dithering?: "none" | "ordered-bayer" | "floyd-steinberg";
222
+ };
223
+
224
+ export type AnimationScene = { node: Source; durationMs: number };
225
+ export type RenderAnimationOptions = CommonOptions & {
226
+ scenes: AnimationScene[];
227
+ width: number;
228
+ height: number;
229
+ /** frames per second (gif <= 50, webp / apng <= 90) */
230
+ fps: number;
231
+ format?: AnimationOutputFormat;
232
+ /** accepted, ignored */
233
+ quality?: number;
234
+ /** accepted, ignored */
235
+ lossless?: boolean;
236
+ };
237
+
238
+ export type MeasuredTextRun = { text?: string; x: number; y: number; width: number; height: number };
239
+ export type MeasuredNode = {
240
+ width: number;
241
+ height: number;
242
+ /** transform: 2D affine matrix [a, b, c, d, e, f] of the node relative to the canvas, not to its parent (e, f = its absolute position) */
243
+ transform: [number, number, number, number, number, number];
244
+ children: MeasuredNode[];
245
+ runs: MeasuredTextRun[];
246
+ };
247
+
248
+ /** Opaque handle returned by `prepare` / `prepareAnimation`. */
249
+ export declare class Prepared {
250
+ private constructor();
251
+ /** nominal brand (not a runtime property) */
252
+ private readonly __bmEnginePrepared: true;
253
+ }
254
+
255
+ /** What init() accepts: wasm bytes, a URL / Request / Response to fetch, or a compiled module. */
256
+ export type InitInput = string | URL | Request | Response | ArrayBuffer | ArrayBufferView | WebAssembly.Module;
257
+ export type InitArgument = InitInput | Promise<InitInput> | { module_or_path: InitInput | Promise<InitInput> };
258
+ export type InitSyncArgument = ArrayBuffer | ArrayBufferView | WebAssembly.Module | { module: ArrayBuffer | ArrayBufferView | WebAssembly.Module };
259
+ /** The raw exports of the instantiated wasm module (C ABI; not a stable API). */
260
+ export type WasmExports = Record<string, unknown>;
261
+
262
+ /**
263
+ * Instantiates the wasm module. Idempotent: once initialised, further calls resolve to the same instance
264
+ * (their argument is ignored); concurrent calls share one instantiation; a failed call can be retried.
265
+ * The `bm-render` entry needs the argument; bm-render/core, /standard and /full default to their own wasm
266
+ * file. On Node.js every entry is already initialised on import.
267
+ */
268
+ export declare function init(input: InitArgument): Promise<WasmExports>;
269
+ /** Synchronous variant of init (bytes or a compiled module only). */
270
+ export declare function initSync(input: InitSyncArgument): WasmExports;
271
+ export default init;
272
+ /** accepted and ignored (fixed 8 MiB glyph budget) */
273
+ export declare function setGlyphCacheMaxBytes(bytes: number): void;
274
+ /** @internal used by bm-render/woff (installs the WOFF -> sfnt converter shared by every entry) */
275
+ export declare function setWoffDecoder(fn: ((bytes: Uint8Array) => Uint8Array) | null): void;
276
+
277
+ /** Optional features of the loaded binary (filled by init / initSync; all false before). */
278
+ export type Features = {
279
+ /** stylesheets (`css` option, HTML `<style>`), selectors, @keyframes */
280
+ css: boolean;
281
+ /** Tailwind `tw` utilities */
282
+ tw: boolean;
283
+ /** HTML string sources */
284
+ html: boolean;
285
+ /** SVG images */
286
+ svg: boolean;
287
+ /** WOFF / WOFF2 fonts built in (otherwise see bm-render/woff) */
288
+ woff: boolean;
289
+ variableFonts: boolean;
290
+ /** CFF / CFF2 outlines (.otf) */
291
+ cff: boolean;
292
+ /** clip-path, masks, blend modes, backdrop-filter */
293
+ effects: boolean;
294
+ /** lab / lch / oklab / oklch / color() / color-mix() */
295
+ modernColor: boolean;
296
+ tables: boolean;
297
+ floats: boolean;
298
+ /** WebP (lossless) and GIF image sources */
299
+ webpGifDecode: boolean;
300
+ /** text-wrap: balance / pretty */
301
+ textWrapBalance: boolean;
302
+ /** colour emoji (COLR / CBDT / sbix) */
303
+ colorFonts: boolean;
304
+ /** bidi, Arabic / Hebrew / Indic / Thai shaping */
305
+ complexText: boolean;
306
+ /** CSS animations, renderAnimation, animated image sources */
307
+ animation: boolean;
308
+ /** lossy WebP decode, animated codecs, ICO output */
309
+ extraCodecs: boolean;
310
+ /** renderSvg */
311
+ renderSvg: boolean;
312
+ /** renderPdf / measurePdf: true only on bm-render/pdf (its wasm profile, or the native addon on Node.js); false on every other entry */
313
+ pdf: boolean;
314
+ };
315
+ export declare const features: Readonly<Features>;
316
+ /**
317
+ * Which engine build serves this entry: "wasm", or "native" when `bm-render` on Node.js (Bun, Deno) found the
318
+ * Node-API addon for the platform (optional package `@hunghg255/bm-render-<platform>-<arch>`). Both have the same API and
319
+ * produce the same bytes; natively the async methods run on the thread pool and init() / initSync() are no-ops.
320
+ * Force one with the environment variable BM_RENDER_BACKEND=wasm|native, or by importing `bm-render/full`
321
+ * (always wasm) / `bm-render/native` (native or an import error).
322
+ */
323
+ export declare const backend: "wasm" | "native";
324
+
325
+ export type RendererOptions = {
326
+ /** accepted, ignored (fixed 96 MiB decoded-image budget) */
327
+ cacheMaxBytes?: number;
328
+ /** accepted, ignored (fixed 8 MiB glyph budget) */
329
+ fontCacheMaxBytes?: number;
330
+ };
331
+
332
+ export declare class Renderer {
333
+ /** Throws unless the entry is initialised. */
334
+ constructor(options?: RendererOptions);
335
+ /** Registers every face of a TTF / OTF / TTC (WOFF / WOFF2 with the `woff` feature or bm-render/woff). */
336
+ registerFont(font: ByteBuf | SyncFontDetails): RegisteredFamily[];
337
+ /** URL strings, promises and lazy `data` make it asynchronous. */
338
+ registerFont(font: FontLoader | Promise<FontLoader>): RegisteredFamily[] | Promise<RegisteredFamily[]>;
339
+ /** takumi-compatible alias of registerFont */
340
+ loadFont(font: ByteBuf | SyncFontDetails): RegisteredFamily[];
341
+ loadFont(font: FontLoader | Promise<FontLoader>): RegisteredFamily[] | Promise<RegisteredFamily[]>;
342
+ /** Removes every face of the family; true when something was removed. */
343
+ unregisterFont(family: string | { name: string }): boolean;
344
+ clearFonts(): void;
345
+
346
+ /** Renders to an encoded image (PNG by default) or raw RGBA. */
347
+ renderSync(source: Source | Prepared, options?: RenderOptions): OutputBytes;
348
+ render(source: Source | Prepared, options?: RenderOptions): Promise<OutputBytes>;
349
+ /** `data:<mime>;base64,...` (not for `raw`) */
350
+ renderAsDataUrlSync(source: Source | Prepared, options?: RenderOptions): string;
351
+ renderAsDataUrl(source: Source | Prepared, options?: RenderOptions): Promise<string>;
352
+ /** SVG document output (`renderSvg` feature: the full profile; otherwise throws) */
353
+ renderSvgSync(source: Source | Prepared, options?: RenderOptions): string;
354
+ renderSvg(source: Source | Prepared, options?: RenderOptions): Promise<string>;
355
+ /** takumi-pdf output: a PDF file (`pdf` feature: bm-render/pdf; otherwise throws "renderPdf is not supported by this build"). */
356
+ renderPdfSync(source: Source | Prepared, options?: PdfRenderOptions): OutputBytes;
357
+ renderPdf(source: Source | Prepared, options?: PdfRenderOptions): Promise<OutputBytes>;
358
+ /** takumi-pdf `measure`: laid-out size in CSS px at the page width (`pdf` feature). */
359
+ measurePdfSync(source: Source | Prepared, options?: PdfMeasureOptions): MeasuredSize;
360
+ measurePdf(source: Source | Prepared, options?: PdfMeasureOptions): Promise<MeasuredSize>;
361
+ /** Layout only. */
362
+ measureSync(source: Source | Prepared, options?: RenderOptions): MeasuredNode;
363
+ measure(source: Source | Prepared, options?: RenderOptions): Promise<MeasuredNode>;
364
+
365
+ /** Resolves `fonts` / lazy `images` once; render*, measure* accept the handle. */
366
+ prepare(source: Source, options?: RenderOptions): Promise<Prepared>;
367
+ prepareAnimation(options: RenderAnimationOptions): Promise<Prepared>;
368
+ /** Animated WebP (default) / APNG / GIF (`animation` feature: the full profile). */
369
+ renderAnimationSync(options: RenderAnimationOptions | Prepared): OutputBytes;
370
+ renderAnimation(options: RenderAnimationOptions): Promise<OutputBytes>;
371
+ renderAnimation(prepared: Prepared, options?: { signal?: AbortSignal }): Promise<OutputBytes>;
372
+
373
+ /** current wasm linear memory size in bytes (shared by every renderer of this entry); native backend: bytes allocated by the addon */
374
+ memoryBytes(): number;
375
+ /** same as memoryBytes(); 0 before init */
376
+ static memoryBytes(): number;
377
+ /** Releases the renderer's fonts and caches; later calls on it throw. */
378
+ free(): void;
379
+ /** `using r = new Renderer()` calls free() (falls back to Symbol.for("nodejs.dispose") on old runtimes) */
380
+ [Symbol.dispose](): void;
381
+ }
package/js/index.mjs ADDED
@@ -0,0 +1,64 @@
1
+ // bm-render — dependency-free ESM wrapper around bm_render.wasm (the environment-neutral entry,
2
+ // `import ... from "bm-render"` everywhere except Node.js). Works in browsers, Node.js, Bun, Deno and edge
3
+ // runtimes. The wasm module has NO imports. You pass the wasm binary to init() yourself:
4
+ //
5
+ // import { init, Renderer } from "bm-render";
6
+ // await init(wasm); // bytes | URL | Request | Response | Promise<Response> | WebAssembly.Module
7
+ // const r = new Renderer();
8
+ // r.registerFont(fontBytes);
9
+ // const png = r.renderSync(node, { width: 1200, height: 630 });
10
+ //
11
+ // (bm-render/core, bm-render/standard, bm-render/full find their wasm file themselves: `await init()`;
12
+ // on Node.js `bm-render` and every profile entry are initialised on import.)
13
+ //
14
+ // The node format and render options follow takumi (`@takumi-rs/wasm` 2.20), so the same input renders on
15
+ // both: a source is a node object, `{ json: string }`, or a string of HTML; `css`, `tw`, `className` etc. are
16
+ // honoured when the wasm build includes them. Typings: ./index.d.mts. Implementation: ./engine.mjs.
17
+ //
18
+ // takumi `Renderer` API parity (async methods resolve immediately; every one has a *Sync twin here):
19
+ //
20
+ // takumi bm-render
21
+ // ---------------------------- ---------------------------------------------------------------------------
22
+ // new Renderer(options) works; `cacheMaxBytes` / `fontCacheMaxBytes` accepted and ignored (fixed
23
+ // budgets: 96 MiB decoded images, 8 MiB glyph masks)
24
+ // registerFont(font) works for bytes, `{ name, data, weight, style }` (data may be a function /
25
+ // promise -> async) and URL strings (fetched); `unicodeRange`, `subsetOf`,
26
+ // `generic` ignored (every face is registered eagerly)
27
+ // unregisterFont(family) works (`{ name }` or a string)
28
+ // clearFonts() works
29
+ // render(source, options) works: png | jpeg | webp (always lossless, like takumi-wasm) | ico | raw;
30
+ // `timeMs` samples CSS animations; `fonts` (FontLoader[]) registers the fonts
31
+ // and, without `fontFamilies`, makes them the fallback stack; `images`
32
+ // (array or `{ sources }`, lazy `data()` allowed); `signal` checked before
33
+ // rendering. Remote image URLs are NOT fetched (as in takumi without prepare)
34
+ // renderAsDataUrl works (raw rejected)
35
+ // renderSvg works (builds with `render_svg`: the full profile); renderSvgSync twin
36
+ // measure works
37
+ // (takumi-pdf) renderPdf / measurePdf (+ Sync twins): builds with `pdf`, i.e. bm-render/pdf (js/pdf.mjs)
38
+ // prepare(source, options) returns a prepared handle; `render`/`renderAsDataUrl`/`measure` accept it and
39
+ // merge its options. NOTE: unlike takumi it fetches nothing (no `needs` pass):
40
+ // remote images / @font-face files must be passed as `images` / fonts
41
+ // prepareAnimation(options) same, for renderAnimation
42
+ // renderAnimation(options) works: webp (default, lossless) | apng | gif; takumi's frame schedule
43
+ // read / readAnimation private in takumi: not provided
44
+ // free / [Symbol.dispose] works
45
+ // setGlyphCacheMaxBytes(bytes) module function accepted and ignored (fixed 8 MiB glyph budget)
46
+ //
47
+ // Build profiles (scripts/build.mjs): bm_render.core.wasm (JSON nodes + inline styles, flex/grid/block,
48
+ // static TTF, PNG/JPEG in), bm_render.standard.wasm (+ css, tw, html, svg images, woff2, variable fonts,
49
+ // cff, effects, modern colours, tables, floats, webp/gif in, text-wrap balance) and bm_render.wasm = full
50
+ // (+ complex text, colour emoji, animation, extra codecs, renderSvg). `features` (filled by init) tells
51
+ // which optional features the loaded binary has; using a missing one throws a clear error.
52
+ //
53
+ // One module instance per entry: this entry (and js/node.mjs, which initialises it on Node) share one;
54
+ // bm-render/core, /standard and /full each have their own, so several profiles can coexist in a process.
55
+ // Within one entry the first successful init wins; later init() calls return the same instance.
56
+ import { createBinding } from "./engine.mjs";
57
+
58
+ const binding = createBinding();
59
+
60
+ export const { init, initSync, Renderer, features, setGlyphCacheMaxBytes } = binding;
61
+ /** Which engine build is active: "wasm" here; "native" when Node.js loaded the Node-API addon (js/node.mjs). */
62
+ export const backend = "wasm";
63
+ export { Prepared, setWoffDecoder } from "./engine.mjs";
64
+ export default init;
@@ -0,0 +1,4 @@
1
+ // bm-render/native: the native Node-API addon (Node.js, Bun, Deno). Same API as bm-render (./index.d.mts); the
2
+ // import throws when no addon is installed for the platform. init() / initSync() are accepted and do nothing.
3
+ export * from "./index.mjs";
4
+ export { default } from "./index.mjs";