Registry / RepositoryTypeScriptRustPython
Rollpie
ReadmeFiles
Versions
Info
Download
0.2.3555.5 KB2026-10-160.2.2572.8 KB2026-10-150.2.1580.5 KB2026-10-150.2.0579.9 KB2026-09-150.1.1576.6 KB2026-09-12
Version
0.2.2
Copyright
Rollpie, Irohabook
Publisher
math
Published
2026-10-15
Size
572.8 KB
Downloads
28
Checksum
ce26f898ba26eb49c71ebc68597f62f268b83e41bb265b1b5a01688966054588
Dependencies
None

suki.ts

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
// Calls suki compiled to wasm from JS. The wasm and the font are loaded once with loadFont and kept by this module.
// The wasm is built with `cargo build --lib --release --target wasm32-unknown-unknown` (src/wasm.rs).
//
// Strings are passed as UTF-8 bytes. Each wasm function returns 0 on success and 1 on failure,
// and puts the result (the error message on failure) where resultPointer and resultLength point.


// How to place one glyph. outline is an index into the outline of NotationTable.
// Outlines are in font units, so multiplying by scaleX and scaleY gives em. offsetX and offsetY are in em.
export interface NotationGlyph {
	outline: number
	scaleX: number
	scaleY: number
	offsetX: number
	offsetY: number
}

// A fraction bar or the bar of a radical. The top left position and the size, all in em.
export interface NotationRule {
	offsetX: number
	offsetY: number
	width: number
	height: number
}

// A character the typeface lacks (such as Japanese inside \text). It has no outline, so it is drawn as text.
// size is the character size (em), and offsetY is the position of the baseline.
export interface NotationText {
	value: string
	size: number
	offsetX: number
	offsetY: number
}

// One formula. Sizes and positions are in em; positive y points down and the baseline is 0.
export interface Notation {
	width: number
	height: number
	depth: number
	glyph: NotationGlyph[]
	rule: NotationRule[]
	text: NotationText[]
}

// The table of formulas. outline holds glyph outlines (SVG d, with y pointing down), kept once across formulas.
// darken is the width (em) of the stroke drawn around the outlines; with it they look as heavy as the SVG output.
export interface NotationTable {
	outline: string[]
	notation: Record<string, Notation>
	darken: number
}

// What the wasm exports (src/wasm.rs).
interface SukiExports {
	memory: WebAssembly.Memory
	allocate(length: number): number
	deallocate(pointer: number, length: number): void
	resultPointer(): number
	resultLength(): number
	loadFont(pointer: number, length: number): number
	renderSvg(pointer: number, length: number, display: number): number
	layoutNotation(pointer: number, length: number, display: number): number
	glyphOutline(glyph: number): number
	darken(): number
}

// How layoutNotation places one glyph. A glyph is given by its glyph ID in the font, not by an outline index.
interface PlacedGlyph {
	glyph: number
	scaleX: number
	scaleY: number
	offsetX: number
	offsetY: number
}

// One formula as returned by layoutNotation.
interface PlacedNotation {
	width: number
	height: number
	depth: number
	glyph: PlacedGlyph[]
	rule: NotationRule[]
	text: NotationText[]
}


const encoder: TextEncoder = new TextEncoder()

const decoder: TextDecoder = new TextDecoder()

// The loaded wasm. It is null until loadFont is called.
let loadedSuki: SukiExports | null = null


function useSuki(): SukiExports {
	if (loadedSuki === null) throw new Error('suki was called before it was loaded. Call loadFont first.')

	return loadedSuki
}


// Writes bytes into the wasm memory and returns where they are. Allocating can grow the memory and make
// an older view unusable, so the view for writing is made after allocating.
function writeBytes(suki: SukiExports, bytes: Uint8Array): number {
	const pointer = suki.allocate(bytes.length) >>> 0

	new Uint8Array(suki.memory.buffer, pointer, bytes.length).set(bytes)

	return pointer
}


// Reads the result of a call. On failure, throws with the message in the result.
function readResult(suki: SukiExports, status: number): string {
	const pointer = suki.resultPointer() >>> 0
	const length = suki.resultLength() >>> 0
	const text = decoder.decode(new Uint8Array(suki.memory.buffer, pointer, length))

	if (status !== 0) throw new Error(text)

	return text
}


// Loads the wasm and the font. The wasm is instantiated only the first time; later calls only reload the font.
export async function loadFont(wasmBytes: BufferSource, fontBytes: Uint8Array): Promise<void> {
	if (loadedSuki === null) {
		const source = await WebAssembly.instantiate(wasmBytes)
		loadedSuki = source.instance.exports as unknown as SukiExports
	}

	const suki = useSuki()
	const pointer = writeBytes(suki, fontBytes)
	const status = suki.loadFont(pointer, fontBytes.length)

	suki.deallocate(pointer, fontBytes.length)
	readResult(suki, status)
}


// Turns a formula into SVG drawn with outlines. When display is true, it is set on a line of its own.
// If the formula has a wrapper ($…$, \[…\] and so on), the wrapper decides instead.
export function renderSvg(tex: string, display: boolean): string {
	const suki = useSuki()
	const bytes = encoder.encode(tex)
	const pointer = writeBytes(suki, bytes)
	const status = suki.renderSvg(pointer, bytes.length, display ? 1 : 0)

	suki.deallocate(pointer, bytes.length)

	return readResult(suki, status)
}


function layoutNotation(suki: SukiExports, tex: string, display: boolean): PlacedNotation {
	const bytes = encoder.encode(tex)
	const pointer = writeBytes(suki, bytes)
	const status = suki.layoutNotation(pointer, bytes.length, display ? 1 : 0)

	suki.deallocate(pointer, bytes.length)

	return JSON.parse(readResult(suki, status)) as PlacedNotation
}


function glyphOutline(suki: SukiExports, glyph: number): string {
	return readResult(suki, suki.glyphOutline(glyph))
}


// Builds the table of formulas for a canvas. display works as in renderSvg.
// Glyph outlines repeat across formulas, so each goes into the table once and the formulas refer to it by index.
export function buildNotationTable(texList: string[], display: boolean): NotationTable {
	const suki = useSuki()
	const outline: string[] = []
	const outlineIndex: Map<number, number> = new Map()
	const notation: Record<string, Notation> = {}

	for (const tex of texList) {
		const placed = layoutNotation(suki, tex, display)
		const glyph: NotationGlyph[] = []

		for (const item of placed.glyph) {
			let index = outlineIndex.get(item.glyph)

			if (index === undefined) {
				index = outline.length
				outlineIndex.set(item.glyph, index)
				outline.push(glyphOutline(suki, item.glyph))
			}

			glyph.push({outline: index, scaleX: item.scaleX, scaleY: item.scaleY, offsetX: item.offsetX, offsetY: item.offsetY})
		}

		notation[tex] = {width: placed.width, height: placed.height, depth: placed.depth, glyph, rule: placed.rule, text: placed.text}
	}

	return {outline, notation, darken: suki.darken()}
}