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.3
Copyright
Rollpie, Irohabook
Publisher
math
Published
2026-10-16
Size
555.5 KB
Downloads
26
Checksum
c6c938cc3641f85bee86c9360c2356581a4b1efb11333b9d97b3c5605c8bb71c
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
export interface NotationGlyph {
	outline: number
	scaleX: number
	scaleY: number
	offsetX: number
	offsetY: number
}

export interface NotationRule {
	offsetX: number
	offsetY: number
	width: number
	height: number
}

export interface NotationText {
	value: string
	size: number
	offsetX: number
	offsetY: number
}

export interface Notation {
	width: number
	height: number
	depth: number
	glyph: NotationGlyph[]
	rule: NotationRule[]
	text: NotationText[]
}

export interface NotationTable {
	outline: string[]
	notation: Record<string, Notation>
	darken: number
}

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
}

interface PlacedGlyph {
	glyph: number
	scaleX: number
	scaleY: number
	offsetX: number
	offsetY: number
}

interface PlacedNotation {
	width: number
	height: number
	depth: number
	glyph: PlacedGlyph[]
	rule: NotationRule[]
	text: NotationText[]
}


const encoder: TextEncoder = new TextEncoder()

const decoder: TextDecoder = new TextDecoder()

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
}


// 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
}


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
}


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)
}


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))
}


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()}
}