@strategicprojects/rpic 0.11.3 → 0.12.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/index.d.ts CHANGED
@@ -43,6 +43,95 @@ export interface ObjectGeometry {
43
43
  file?: string;
44
44
  }
45
45
 
46
+ /** One subpath of a scene item: a cubic Bézier chain or a polyline. */
47
+ export interface ScenePath {
48
+ closed?: true;
49
+ /** cubic form: start point + `[c1, c2, end]` triples */
50
+ start?: [number, number];
51
+ cubics?: [[number, number], [number, number], [number, number]][];
52
+ /** polyline form (inherently straight, or `flatten`ed) */
53
+ points?: [number, number][];
54
+ }
55
+
56
+ export interface SceneItem {
57
+ /** `"s<N>"` — matches the SVG's `<g id>` — or `"blk<N>"` for blocks */
58
+ id: string;
59
+ kind:
60
+ | 'box' | 'circle' | 'ellipse' | 'arc' | 'line' | 'arrow'
61
+ | 'spline' | 'text' | 'dot' | 'brace' | 'block';
62
+ /** draw-order index (dense over shape items; absent on blocks) */
63
+ z?: number;
64
+ label?: string;
65
+ /** enclosing block item ids, outermost first */
66
+ block?: string[];
67
+ class?: string;
68
+ link?: string;
69
+ /** the item paints nothing (`move`, invisible helpers) */
70
+ hidden?: true;
71
+ at?: [number, number];
72
+ w?: number;
73
+ h?: number;
74
+ r?: number;
75
+ rad?: number;
76
+ points?: [number, number][];
77
+ closed?: true;
78
+ paths?: ScenePath[];
79
+ fillPaths?: ScenePath[];
80
+ fillPoints?: [number, number][];
81
+ heads?: {
82
+ at: 'start' | 'end';
83
+ filled: boolean;
84
+ points?: [number, number][];
85
+ path?: ScenePath;
86
+ }[];
87
+ stroke?: {
88
+ color: string;
89
+ /** points */
90
+ width: number;
91
+ dash?: { kind: 'dashed' | 'dotted'; len?: number; gap?: number };
92
+ };
93
+ fill?: {
94
+ color?: string;
95
+ gradient?: { angle: number; stops: { offset: number; color: string }[] };
96
+ hatch?: { angle: number; spacing: number; width: number; color: string; cross: boolean };
97
+ };
98
+ opacity?: number;
99
+ text?: {
100
+ text: string;
101
+ /** the line's baseline anchor point (see docs/scene.md §5) */
102
+ at: [number, number];
103
+ anchor: 'c' | 'n' | 's' | 'e' | 'w' | 'ne' | 'nw' | 'se' | 'sw';
104
+ font: { size: number; family?: string; bold?: true; italic?: true };
105
+ rotation?: number;
106
+ math?: { width: number; height: number; depth: number };
107
+ outline?: string;
108
+ }[];
109
+ /** block items: the child item ids */
110
+ items?: string[];
111
+ }
112
+
113
+ /**
114
+ * The device-independent resolved draw list (docs/scene.md). Coordinates in
115
+ * inches, y-up; curves as cubic Béziers; style as descriptors.
116
+ */
117
+ export interface Scene {
118
+ /** format version (currently 1) */
119
+ scene: number;
120
+ units: 'in';
121
+ y_up: true;
122
+ canvas: {
123
+ w: number;
124
+ h: number;
125
+ margin: { top: number; right: number; bottom: number; left: number };
126
+ /** where the pic origin sits, from the canvas lower-left corner */
127
+ origin: [number, number];
128
+ };
129
+ source?: string;
130
+ /** final values of every variable (user + pic built-ins) */
131
+ vars: Record<string, number>;
132
+ items: SceneItem[];
133
+ }
134
+
46
135
  export interface Bundle {
47
136
  svg: string;
48
137
  animations: Anim[];
@@ -59,6 +148,8 @@ export interface Bundle {
59
148
  * the timeline on scroll rather than autoplay. Present only when set.
60
149
  */
61
150
  scroll?: boolean;
151
+ /** the resolved draw list; present only when compiled with `scene: true` */
152
+ scene?: Scene;
62
153
  }
63
154
 
64
155
  export interface CompileError extends Error {
@@ -78,6 +169,12 @@ export interface CompileOptions {
78
169
  * labels fall back to literal text plus a diagnostic.
79
170
  */
80
171
  texlabels?: boolean;
172
+ /** also compute the device-independent `scene` draw list (docs/scene.md) */
173
+ scene?: boolean;
174
+ /** scene: deliver curves as polylines within this tolerance (inches); implies `scene` */
175
+ flatten?: number;
176
+ /** scene: embed typeset math as SVG glyph fragments (`outline` keys) */
177
+ outline?: boolean;
81
178
  }
82
179
 
83
180
  export interface ReadyOptions {
package/index.js CHANGED
@@ -66,16 +66,26 @@ function ensure() {
66
66
  * Compile pic source into `{ svg, animations, diagnostics, warnings, objects }`
67
67
  * (throws on a pic error). A top-level `scroll: true` is present when the source
68
68
  * used `animate scroll` — a hint to scrub the timeline on scroll (see `animate`).
69
+ *
70
+ * Pass `scene: true` for the device-independent resolved draw list as a
71
+ * `scene` key (see docs/scene.md; versioned, y-up inches, cubic curves);
72
+ * `flatten` (inches) delivers its curves as polylines instead, and
73
+ * `outline` embeds typeset math as SVG glyph fragments.
69
74
  * @param {string} src
70
- * @param {{circuits?: boolean, texlabels?: boolean}} [opts]
75
+ * @param {{circuits?: boolean, texlabels?: boolean, scene?: boolean, flatten?: number, outline?: boolean}} [opts]
71
76
  */
72
77
  export function compile(src, opts = {}) {
73
78
  ensure();
74
- const json = opts.texlabels
75
- ? wasm.compile_with(src, !!opts.circuits, true)
76
- : opts.circuits
77
- ? wasm.compile_circuits(src)
78
- : wasm.compile(src);
79
+ if (opts.flatten !== undefined && !(Number.isFinite(opts.flatten) && opts.flatten > 0)) {
80
+ throw new Error('rpic: flatten must be a positive finite tolerance in inches');
81
+ }
82
+ const json = opts.scene || opts.flatten !== undefined
83
+ ? wasm.compile_scene(src, !!opts.circuits, !!opts.texlabels, opts.flatten, !!opts.outline)
84
+ : opts.texlabels
85
+ ? wasm.compile_with(src, !!opts.circuits, true)
86
+ : opts.circuits
87
+ ? wasm.compile_circuits(src)
88
+ : wasm.compile(src);
79
89
  const out = JSON.parse(json);
80
90
  if (out.error) {
81
91
  const err = new Error(out.error);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@strategicprojects/rpic",
3
- "version": "0.11.3",
3
+ "version": "0.12.0",
4
4
  "description": "The pic graphics language compiled to SVG with animation manifests, via WebAssembly. Browser + Node.",
5
5
  "type": "module",
6
6
  "main": "./index.js",
@@ -14,6 +14,14 @@ export function compile(src: string): string;
14
14
  */
15
15
  export function compile_circuits(src: string): string;
16
16
 
17
+ /**
18
+ * Like [`compile_with`], but the bundle also carries the device-independent
19
+ * `scene` draw list (docs/scene.md); `flatten` (inches) delivers curves as
20
+ * polylines. The plain entry points above stay byte-identical — the scene is
21
+ * computed only here.
22
+ */
23
+ export function compile_scene(src: string, circuits: boolean, texlabels: boolean, flatten: number | null | undefined, outline: boolean): string;
24
+
17
25
  /**
18
26
  * Like [`compile`], with options: `circuits` loads the circuit-element
19
27
  * library; `texlabels` sets `texlabels = 1` so `$…$` labels are typeset as
@@ -30,6 +38,7 @@ export interface InitOutput {
30
38
  readonly memory: WebAssembly.Memory;
31
39
  readonly compile: (a: number, b: number) => [number, number];
32
40
  readonly compile_circuits: (a: number, b: number) => [number, number];
41
+ readonly compile_scene: (a: number, b: number, c: number, d: number, e: number, f: number, g: number) => [number, number];
33
42
  readonly compile_with: (a: number, b: number, c: number, d: number) => [number, number];
34
43
  readonly __wbindgen_externrefs: WebAssembly.Table;
35
44
  readonly __wbindgen_malloc: (a: number, b: number) => number;
package/pkg/rpic_wasm.js CHANGED
@@ -43,6 +43,33 @@ export function compile_circuits(src) {
43
43
  }
44
44
  }
45
45
 
46
+ /**
47
+ * Like [`compile_with`], but the bundle also carries the device-independent
48
+ * `scene` draw list (docs/scene.md); `flatten` (inches) delivers curves as
49
+ * polylines. The plain entry points above stay byte-identical — the scene is
50
+ * computed only here.
51
+ * @param {string} src
52
+ * @param {boolean} circuits
53
+ * @param {boolean} texlabels
54
+ * @param {number | null | undefined} flatten
55
+ * @param {boolean} outline
56
+ * @returns {string}
57
+ */
58
+ export function compile_scene(src, circuits, texlabels, flatten, outline) {
59
+ let deferred2_0;
60
+ let deferred2_1;
61
+ try {
62
+ const ptr0 = passStringToWasm0(src, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
63
+ const len0 = WASM_VECTOR_LEN;
64
+ const ret = wasm.compile_scene(ptr0, len0, circuits, texlabels, !isLikeNone(flatten), isLikeNone(flatten) ? 0 : flatten, outline);
65
+ deferred2_0 = ret[0];
66
+ deferred2_1 = ret[1];
67
+ return getStringFromWasm0(ret[0], ret[1]);
68
+ } finally {
69
+ wasm.__wbindgen_free(deferred2_0, deferred2_1, 1);
70
+ }
71
+ }
72
+
46
73
  /**
47
74
  * Like [`compile`], with options: `circuits` loads the circuit-element
48
75
  * library; `texlabels` sets `texlabels = 1` so `$…$` labels are typeset as
@@ -100,6 +127,10 @@ function getUint8ArrayMemory0() {
100
127
  return cachedUint8ArrayMemory0;
101
128
  }
102
129
 
130
+ function isLikeNone(x) {
131
+ return x === undefined || x === null;
132
+ }
133
+
103
134
  function passStringToWasm0(arg, malloc, realloc) {
104
135
  if (realloc === undefined) {
105
136
  const buf = cachedTextEncoder.encode(arg);
Binary file
@@ -3,6 +3,7 @@
3
3
  export const memory: WebAssembly.Memory;
4
4
  export const compile: (a: number, b: number) => [number, number];
5
5
  export const compile_circuits: (a: number, b: number) => [number, number];
6
+ export const compile_scene: (a: number, b: number, c: number, d: number, e: number, f: number, g: number) => [number, number];
6
7
  export const compile_with: (a: number, b: number, c: number, d: number) => [number, number];
7
8
  export const __wbindgen_externrefs: WebAssembly.Table;
8
9
  export const __wbindgen_malloc: (a: number, b: number) => number;
@@ -14,6 +14,14 @@ export function compile(src: string): string;
14
14
  */
15
15
  export function compile_circuits(src: string): string;
16
16
 
17
+ /**
18
+ * Like [`compile_with`], but the bundle also carries the device-independent
19
+ * `scene` draw list (docs/scene.md); `flatten` (inches) delivers curves as
20
+ * polylines. The plain entry points above stay byte-identical — the scene is
21
+ * computed only here.
22
+ */
23
+ export function compile_scene(src: string, circuits: boolean, texlabels: boolean, flatten: number | null | undefined, outline: boolean): string;
24
+
17
25
  /**
18
26
  * Like [`compile`], with options: `circuits` loads the circuit-element
19
27
  * library; `texlabels` sets `texlabels = 1` so `$…$` labels are typeset as
@@ -38,6 +46,7 @@ export interface InitOutput {
38
46
  readonly memory: WebAssembly.Memory;
39
47
  readonly compile: (a: number, b: number) => [number, number];
40
48
  readonly compile_circuits: (a: number, b: number) => [number, number];
49
+ readonly compile_scene: (a: number, b: number, c: number, d: number, e: number, f: number, g: number) => [number, number];
41
50
  readonly compile_with: (a: number, b: number, c: number, d: number) => [number, number];
42
51
  readonly init_math: () => void;
43
52
  readonly __wbindgen_externrefs: WebAssembly.Table;
@@ -43,6 +43,33 @@ export function compile_circuits(src) {
43
43
  }
44
44
  }
45
45
 
46
+ /**
47
+ * Like [`compile_with`], but the bundle also carries the device-independent
48
+ * `scene` draw list (docs/scene.md); `flatten` (inches) delivers curves as
49
+ * polylines. The plain entry points above stay byte-identical — the scene is
50
+ * computed only here.
51
+ * @param {string} src
52
+ * @param {boolean} circuits
53
+ * @param {boolean} texlabels
54
+ * @param {number | null | undefined} flatten
55
+ * @param {boolean} outline
56
+ * @returns {string}
57
+ */
58
+ export function compile_scene(src, circuits, texlabels, flatten, outline) {
59
+ let deferred2_0;
60
+ let deferred2_1;
61
+ try {
62
+ const ptr0 = passStringToWasm0(src, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
63
+ const len0 = WASM_VECTOR_LEN;
64
+ const ret = wasm.compile_scene(ptr0, len0, circuits, texlabels, !isLikeNone(flatten), isLikeNone(flatten) ? 0 : flatten, outline);
65
+ deferred2_0 = ret[0];
66
+ deferred2_1 = ret[1];
67
+ return getStringFromWasm0(ret[0], ret[1]);
68
+ } finally {
69
+ wasm.__wbindgen_free(deferred2_0, deferred2_1, 1);
70
+ }
71
+ }
72
+
46
73
  /**
47
74
  * Like [`compile`], with options: `circuits` loads the circuit-element
48
75
  * library; `texlabels` sets `texlabels = 1` so `$…$` labels are typeset as
@@ -113,6 +140,10 @@ function getUint8ArrayMemory0() {
113
140
  return cachedUint8ArrayMemory0;
114
141
  }
115
142
 
143
+ function isLikeNone(x) {
144
+ return x === undefined || x === null;
145
+ }
146
+
116
147
  function passStringToWasm0(arg, malloc, realloc) {
117
148
  if (realloc === undefined) {
118
149
  const buf = cachedTextEncoder.encode(arg);
Binary file
@@ -3,6 +3,7 @@
3
3
  export const memory: WebAssembly.Memory;
4
4
  export const compile: (a: number, b: number) => [number, number];
5
5
  export const compile_circuits: (a: number, b: number) => [number, number];
6
+ export const compile_scene: (a: number, b: number, c: number, d: number, e: number, f: number, g: number) => [number, number];
6
7
  export const compile_with: (a: number, b: number, c: number, d: number) => [number, number];
7
8
  export const init_math: () => void;
8
9
  export const __wbindgen_externrefs: WebAssembly.Table;