Registry / RepositoryTypeScriptRustPython
Rollpie
ReadmeFiles
Versions
Info
Download
0.4.01085.8 KB2026-10-160.3.21087.6 KB2026-10-160.3.11087.5 KB2026-10-160.3.0117.8 KB2026-10-160.2.1106.1 KB2026-10-150.2.0105.5 KB2026-10-15
Version
0.3.2
Copyright
Rollpie, Irohabook
Publisher
math
Published
2026-10-16
Size
1087.6 KB
Downloads
24
Checksum
881a4ab17daf310e03467a370f4742ef983298ad90e4ff2c4bba2ff3ea69dafa
Dependencies
rust/[email protected]
rust/[email protected]

index.html

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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Rollpie Hongo</title>
<style>
html, body {
	height: 100%;
}

body {
	margin: 0;
	color: #1f1f1f;
	background: #fff;
	font-family: system-ui, -apple-system, sans-serif;
	font-size: 14px;
}

[hidden] {
	display: none !important;
}

.page {
	display: flex;
	flex-direction: column;
	height: 100%;
}

.bar {
	display: flex;
	align-items: center;
	gap: 16px;
	padding: 10px 20px;
}

h1 {
	margin: 0;
	font-size: 16px;
	font-weight: 500;
}

.state {
	flex: 1;
	overflow: hidden;
	color: #767676;
	font-size: 12px;
	white-space: nowrap;
	text-overflow: ellipsis;
}

button {
	padding: 7px 16px;
	color: #fff;
	background: #1f1f1f;
	border: 0;
	border-radius: 3px;
	font-family: inherit;
	font-size: 13px;
	cursor: pointer;
}

.wrong {
	margin: 0;
	padding: 10px 20px;
	color: #a33;
	background: #fcf4f4;
	font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
	font-size: 12px;
	line-height: 1.7;
	white-space: pre-wrap;
	word-break: break-all;
}

.split {
	flex: 1;
	display: grid;
	grid-template-columns: 1fr 1fr;
	grid-template-rows: minmax(0, 1fr);
	min-height: 0;
}

.source {
	box-sizing: border-box;
	width: 100%;
	height: 100%;
	margin: 0;
	padding: 16px 20px;
	color: #1f1f1f;
	background: #fff;
	border: 0;
	outline: none;
	resize: none;
	font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
	font-size: 13px;
	line-height: 1.75;
}

.view {
	overflow: auto;
	padding: 20px;
	background: #f4f4f4;
}

.sheet {
	max-width: 820px;
	margin: 0 auto 20px;
	color: #000;
	background: #fff;
}

.sheet svg {
	display: block;
	width: 100%;
	height: auto;
}

@media (max-width: 760px) {
	.split {
		grid-template-columns: 1fr;
		grid-template-rows: minmax(0, 1fr) minmax(0, 1fr);
	}
}
</style>
</head>
<body>
<div class="page">
	<div class="bar">
		<h1>Rollpie Hongo</h1>
		<span id="state" class="state"></span>
		<button id="save" type="button">PDF を保存</button>
		<a id="download" download="hongo.pdf" hidden></a>
	</div>
	<pre id="wrong" class="wrong" hidden></pre>
	<div class="split">
		<textarea id="source" class="source" spellcheck="false" aria-label="原稿"></textarea>
		<div id="view" class="view"></div>
	</div>
</div>
<script id="sample" type="text/plain">
\documentclass[a4paper]{article}

\title{A Sample of hongo}
\author{hongo}
\date{Typeset in the browser}

\begin{document}

\maketitle

\section{Introduction}

Write LaTeX on the left, and the pages appear on the right.
They are typeset again whenever you stop typing\footnote{Footnotes go to the bottom of the page.}.

\begin{itemize}
\item Headings, lists, tables and footnotes
\item Math set by suki, and chemical structures set by lina
\end{itemize}

\section{Math}

Inline math such as $e^{i\pi} + 1 = 0$ sits in the text, and display math stands on its own line.

\begin{equation}
x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a}
\end{equation}

\begin{align}
(a+b)^2 &= a^2 + 2ab + b^2 \\
\sum_{k=1}^{n} k &= \frac{n(n+1)}{2}
\end{align}

\section{Chemistry}

\chemname{\chemfig{*6(-=-=-=)}}{benzene}

\section{Tables}

\begin{tabular}{|l|r|}
\hline
Name & Value \\
\hline
Word space & 0.333 em \\
Line spacing & 1.7 \\
\hline
\end{tabular}

\end{document}
</script>
<script id="worker" type="text/plain">
	let hongo = null

	const encoder = new TextEncoder()

	const decoder = new TextDecoder()


	function writeBytes(bytes) {
		const pointer = hongo.hongoAllocate(bytes.length) >>> 0

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

		return pointer
	}


	function readResult() {
		const pointer = hongo.hongoResultPointer() >>> 0
		const length = hongo.hongoResultLength() >>> 0

		return new Uint8Array(hongo.memory.buffer, pointer, length).slice()
	}


	async function start(wasm, math, japanese) {
		const instance = await WebAssembly.instantiate(wasm, {env: {now: () => Date.now() / 1000}})

		hongo = instance.exports

		const mathPointer = writeBytes(math)
		const japanesePointer = japanese.length > 0 ? writeBytes(japanese) : 0
		const status = hongo.hongoLoadFonts(mathPointer, math.length, japanesePointer, japanese.length)

		hongo.hongoDeallocate(mathPointer, math.length)

		if (japanesePointer !== 0) hongo.hongoDeallocate(japanesePointer, japanese.length)

		postMessage({kind: 'ready', ok: status === 0, text: decoder.decode(readResult())})
	}


	function run(kind, source) {
		const bytes = encoder.encode(source)
		const pointer = writeBytes(bytes)
		const status = kind === 'pdf' ? hongo.hongoPdf(pointer, bytes.length) : hongo.hongoTypeset(pointer, bytes.length)

		hongo.hongoDeallocate(pointer, bytes.length)

		const result = readResult()

		if (status !== 0) {
			postMessage({kind: 'wrong', text: decoder.decode(result)})

			return
		}

		if (kind === 'pdf') {
			postMessage({kind: 'pdf', data: result}, [result.buffer])

			return
		}

		const part = decoder.decode(result).split('\0')

		postMessage({kind: 'pages', missing: part[0], page: part.slice(1)})
	}


	async function receive(event) {
		const message = event.data

		try {
			if (message.kind === 'start') {
				await start(message.wasm, message.math, message.japanese)

			} else {
				run(message.kind, message.source)
			}

		} catch (reason) {
			postMessage({kind: 'broken', text: String(reason)})
		}
	}


	addEventListener('message', receive)
</script>
<script src="hongo.js"></script>
<script src="font.js"></script>
<script>
	const source = document.getElementById('source')

	const view = document.getElementById('view')

	const state = document.getElementById('state')

	const wrong = document.getElementById('wrong')

	const save = document.getElementById('save')

	const download = document.getElementById('download')

	const storageKey = 'hongo-source'

	const waitTime = 200

	const giveUpTime = 5000

	let workerAddress = ''

	let wasmModule = null

	let mathBytes = null

	let japaneseBytes = null

	let worker = null

	let ready = false

	let busy = false

	let pending = null

	let wantPdf = false

	let shown = []

	let startedAt = 0

	let inputTimer = 0

	let giveUpTimer = 0


	function decode(text) {
		const binary = atob(text)
		const bytes = new Uint8Array(binary.length)

		for (let index = 0; index < binary.length; index++) {
			bytes[index] = binary.charCodeAt(index)
		}

		return bytes
	}


	function readStored() {
		try {
			return localStorage.getItem(storageKey)

		} catch {
			return null
		}
	}


	function store(text) {
		try {
			localStorage.setItem(storageKey, text)

		} catch {
			return
		}
	}


	function showWrong(text) {
		wrong.textContent = text
		wrong.hidden = false
	}


	function startWorker() {
		ready = false
		busy = false
		worker = new Worker(workerAddress)
		worker.addEventListener('message', receive)
		worker.postMessage({kind: 'start', wasm: wasmModule, math: mathBytes, japanese: japaneseBytes})
	}


	function stopWorker() {
		clearTimeout(giveUpTimer)

		if (worker !== null) worker.terminate()

		worker = null
		ready = false
		busy = false
	}


	function restart(text) {
		stopWorker()
		showWrong(text)
		startWorker()
	}


	function post(message) {
		busy = true
		giveUpTimer = setTimeout(giveUp, giveUpTime)
		worker.postMessage(message)
	}


	function send() {
		if (worker === null || !ready || busy) return

		if (wantPdf) {
			wantPdf = false
			post({kind: 'pdf', source: source.value})

			return
		}

		if (pending === null) return

		const text = pending

		pending = null
		startedAt = performance.now()
		post({kind: 'typeset', source: text})
	}


	function request() {
		pending = source.value
		send()
	}


	function giveUp() {
		restart(`組版が ${giveUpTime / 1000} 秒たっても終わらないので止めました。原稿を直すと組み直します。`)
	}


	function showPages(page, missing) {
		const sheet = view.children

		for (let index = 0; index < page.length; index++) {
			if (shown[index] === page[index]) continue

			if (index < sheet.length) {
				sheet[index].innerHTML = page[index]

			} else {
				view.insertAdjacentHTML('beforeend', `<div class="sheet">${page[index]}</div>`)
			}
		}

		while (view.children.length > page.length) {
			view.lastElementChild.remove()
		}

		shown = page
		wrong.hidden = true

		const time = Math.round(performance.now() - startedAt)
		const lack = missing.length > 0 ? ` / 書体にない字: ${missing}` : ''

		state.textContent = `${page.length} ページ / ${time} ミリ秒${lack}`
	}


	function savePdf(data) {
		const address = URL.createObjectURL(new Blob([data], {type: 'application/pdf'}))

		download.href = address
		download.click()
		setTimeout(() => URL.revokeObjectURL(address), 60000)
	}


	function receive(event) {
		const message = event.data

		if (message.kind === 'ready') {
			ready = message.ok

			if (!ready) {
				showWrong(message.text)

				return
			}

			send()

			return
		}

		if (message.kind === 'broken') {
			if (!ready) {
				stopWorker()
				showWrong(`読み込めませんでした: ${message.text}`)

				return
			}

			restart(`組版の途中で止まりました: ${message.text}`)

			return
		}

		clearTimeout(giveUpTimer)
		busy = false

		if (message.kind === 'pages') {
			showPages(message.page, message.missing)

		} else if (message.kind === 'pdf') {
			savePdf(message.data)

		} else {
			showWrong(message.text)
			state.textContent = '組めません'
		}

		send()
	}


	async function boot() {
		const stored = readStored()
		const sample = document.getElementById('sample').textContent.replace(/^\n/, '')

		source.value = stored === null ? sample : stored

		if (typeof hongoWasm === 'undefined' || typeof mathFont === 'undefined') {
			showWrong('hongo.js と font.js がありません。cargo run --example web で書き出してください。')

			return
		}

		state.textContent = '読み込んでいます'
		workerAddress = URL.createObjectURL(new Blob([document.getElementById('worker').textContent], {type: 'text/javascript'}))
		wasmModule = await WebAssembly.compile(decode(hongoWasm))
		mathBytes = decode(mathFont)
		japaneseBytes = decode(japaneseFont)
		pending = source.value
		startWorker()
	}


	source.addEventListener('input', () => {
		store(source.value)
		clearTimeout(inputTimer)
		inputTimer = setTimeout(request, waitTime)
	})

	save.addEventListener('click', () => {
		wantPdf = true
		send()
	})

	boot().catch((reason) => showWrong(String(reason)))
</script>
</body>
</html>