forked from johnfactotum/foliate-js
-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathtts.js
More file actions
476 lines (443 loc) · 17.2 KB
/
tts.js
File metadata and controls
476 lines (443 loc) · 17.2 KB
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
const NS = {
XML: 'http://www.w3.org/XML/1998/namespace',
SSML: 'http://www.w3.org/2001/10/synthesis',
}
const blockTags = new Set([
'article', 'aside', 'audio', 'blockquote', 'caption',
'details', 'dialog', 'div', 'dl', 'dt', 'dd',
'figure', 'footer', 'form', 'figcaption',
'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'header', 'hgroup', 'hr', 'li',
'main', 'math', 'nav', 'ol', 'p', 'pre', 'section', 'tr',
])
const getLang = el => {
const x = el.lang || el?.getAttributeNS?.(NS.XML, 'lang')
return x ? x : el.parentElement ? getLang(el.parentElement) : null
}
const getAlphabet = el => {
const x = el?.getAttributeNS?.(NS.XML, 'lang')
return x ? x : el.parentElement ? getAlphabet(el.parentElement) : null
}
const getSegmenter = (lang, granularity = 'word') => {
const segmenter = new Intl.Segmenter(lang || undefined, { granularity })
const granularityIsWord = granularity === 'word'
return function* (strs, makeRange) {
const str = strs.join('').replace(/\r\n/g, ' ').replace(/\r/g, ' ').replace(/\n/g, ' ')
let name = 0
let strIndex = -1
let sum = 0
const rawSegments = Array.from(segmenter.segment(str))
const mergedSegments = []
for (let i = 0, j = 0; i < rawSegments.length; i++) {
const current = rawSegments[i]
const segment = ' ' + current.segment
const endsWithAbbr = /\s([A-Z]{1,2}[a-z]{0,5}|[a-z]{1,3})\.\s*$/.test(segment)
if (!endsWithAbbr || i >= (rawSegments.length-1)) {
const mergedSegment = {
index: rawSegments[j].index,
segment: '',
isWordLike: (i == j) ? current.isWordLike : true,
}
while (j <= i) {
mergedSegment.segment += rawSegments[j++].segment
}
mergedSegments.push(mergedSegment)
}
}
for (const { index, segment, isWordLike } of mergedSegments) {
if (granularityIsWord && !isWordLike) continue
while (sum <= index) sum += strs[++strIndex].length
const startIndex = strIndex
const startOffset = index - (sum - strs[strIndex].length)
const end = index + segment.length - 1
if (end < str.length) while (sum <= end) sum += strs[++strIndex].length
const endIndex = strIndex
const endOffset = end - (sum - strs[strIndex].length) + 1
yield [(name++).toString(),
makeRange(startIndex, startOffset, endIndex, endOffset)]
}
}
}
const fragmentToSSML = (fragment, nodeFilter, inherited) => {
const ssml = document.implementation.createDocument(NS.SSML, 'speak')
const { lang } = inherited
if (lang) ssml.documentElement.setAttributeNS(NS.XML, 'lang', lang)
const convert = (node, parent, inheritedAlphabet) => {
if (!node) return
if (node.nodeType === 3) return ssml.createTextNode(node.textContent)
if (node.nodeType === 4) return ssml.createCDATASection(node.textContent)
if (node.nodeType !== 1 && node.nodeType !== 11) return
if (nodeFilter && nodeFilter(node) === NodeFilter.FILTER_REJECT) return
let el
const nodeName = node.nodeName.toLowerCase()
if (nodeName === 'foliate-mark') {
el = ssml.createElementNS(NS.SSML, 'mark')
el.setAttribute('name', node.dataset.name)
}
else if (nodeName === 'br')
el = ssml.createElementNS(NS.SSML, 'break')
else if (nodeName === 'em' || nodeName === 'strong')
el = ssml.createElementNS(NS.SSML, 'emphasis')
const lang = node.lang || node.getAttributeNS?.(NS.XML, 'lang')
if (lang) {
if (!el) el = ssml.createElementNS(NS.SSML, 'lang')
el.setAttributeNS(NS.XML, 'lang', lang)
}
const alphabet = node.getAttributeNS?.(NS.SSML, 'alphabet') || inheritedAlphabet
if (!el) {
const ph = node.getAttributeNS?.(NS.SSML, 'ph')
if (ph) {
el = ssml.createElementNS(NS.SSML, 'phoneme')
if (alphabet) el.setAttribute('alphabet', alphabet)
el.setAttribute('ph', ph)
}
}
if (!el) el = parent
let child = node.firstChild
while (child) {
const childEl = convert(child, el, alphabet)
if (childEl && el !== childEl) el.append(childEl)
child = child.nextSibling
}
return el
}
convert(fragment, ssml.documentElement, inherited.alphabet)
return ssml
}
const getFragmentWithMarks = (range, textWalker, nodeFilter, granularity) => {
const lang = getLang(range.commonAncestorContainer)
const alphabet = getAlphabet(range.commonAncestorContainer)
const segmenter = getSegmenter(lang, granularity)
const fragment = range.cloneContents()
// we need ranges on both the original document (for highlighting)
// and the document fragment (for inserting marks)
// so unfortunately need to do it twice, as you can't copy the ranges
const entries = [...textWalker(range, segmenter, nodeFilter)]
const fragmentEntries = [...textWalker(fragment, segmenter, nodeFilter)]
for (const [name, range] of fragmentEntries) {
const mark = document.createElement('foliate-mark')
mark.dataset.name = name
range.insertNode(mark)
}
const ssml = fragmentToSSML(fragment, nodeFilter, { lang, alphabet })
return { entries, ssml }
}
const rangeIsEmpty = range => !range.toString().trim()
// For PDF text layers, split content into sentence-level blocks so TTS
// reads one sentence at a time instead of the whole page in one block.
// Text nodes are split at sentence boundaries so that every block range
// aligns with node edges — this prevents the text walker from including
// text outside the sentence in word marks.
function* getPDFSentenceBlocks(doc, textLayer) {
const collectNodes = () => {
const w = doc.createTreeWalker(textLayer, NodeFilter.SHOW_TEXT)
const res = []
for (let n = w.nextNode(); n; n = w.nextNode()) res.push(n)
return res
}
let nodes = collectNodes()
if (!nodes.length) return
const fullText = nodes.map(n => n.nodeValue).join('')
if (!fullText.trim()) return
// Find sentence boundary positions
const lang = getLang(textLayer) || undefined
const segmenter = new Intl.Segmenter(lang, { granularity: 'sentence' })
const boundaries = new Set()
for (const { index } of segmenter.segment(fullText))
if (index > 0) boundaries.add(index)
// Split text nodes at sentence boundaries so ranges align with node edges.
// Process in reverse order to preserve earlier character positions.
let cum = 0
const nodeStarts = nodes.map(n => { const s = cum; cum += n.nodeValue.length; return s })
for (const pos of [...boundaries].sort((a, b) => b - a)) {
for (let i = 0; i < nodes.length; i++) {
const start = nodeStarts[i]
const end = start + nodes[i].nodeValue.length
if (pos > start && pos < end) {
nodes[i].splitText(pos - start)
break
}
}
}
// Re-collect nodes after splits and group into sentence blocks
nodes = collectNodes()
cum = 0
let groupStart = 0
let blockCount = 0
for (let i = 0; i < nodes.length; i++) {
cum += nodes[i].nodeValue.length
const isEnd = i === nodes.length - 1 || boundaries.has(cum)
if (isEnd) {
const range = doc.createRange()
range.setStart(nodes[groupStart], 0)
range.setEnd(nodes[i], nodes[i].nodeValue.length)
if (!rangeIsEmpty(range)) {
blockCount++
yield range
}
groupStart = i + 1
}
}
}
function* getBlocks(doc, nodeFilter) {
const root = doc.body
?? doc.querySelector('body')
?? doc.documentElement
// For PDF text layers, yield sentence-level blocks
const textLayer = root.querySelector?.('.textLayer')
if (textLayer) {
yield* getPDFSentenceBlocks(doc, textLayer)
return
}
let last
let sawBlock = false
let sawSkipped = false
const walker = doc.createTreeWalker(root, NodeFilter.SHOW_ELEMENT)
let node = walker.nextNode()
while (node) {
const name = node.tagName.toLowerCase()
// A rejected block element (e.g. a footnote/endnote aside) must not be
// read: skip its whole subtree and end the preceding block before it
// so its text doesn't leak into the adjacent block. Inline rejects are
// left to the text walker in getFragmentWithMarks().
if (blockTags.has(name)
&& nodeFilter?.(node) === NodeFilter.FILTER_REJECT) {
sawSkipped = true
if (last) {
last.setEndBefore(node)
if (!rangeIsEmpty(last)) yield last
last = null
}
const skipped = node
do node = walker.nextNode()
while (node && (skipped.compareDocumentPosition(node)
& Node.DOCUMENT_POSITION_CONTAINED_BY))
continue
}
if (blockTags.has(name)) {
if (last) {
last.setEndBefore(node)
if (!rangeIsEmpty(last)) yield last
}
last = doc.createRange()
last.setStart(node, 0)
sawBlock = true
}
node = walker.nextNode()
}
if (last) {
last.setEndAfter(root.lastChild ?? root)
if (!rangeIsEmpty(last)) yield last
} else if (!sawBlock && !sawSkipped) {
last = doc.createRange()
last.setStart(root.firstChild ?? root, 0)
last.setEndAfter(root.lastChild ?? root)
if (!rangeIsEmpty(last)) yield last
}
}
class ListIterator {
#arr = []
#iter
#index = -1
#f
constructor(iter, f = x => x) {
this.#iter = iter
this.#f = f
}
current() {
if (this.#arr[this.#index]) return this.#f(this.#arr[this.#index])
}
first() {
const newIndex = 0
if (this.#arr[newIndex]) {
this.#index = newIndex
return this.#f(this.#arr[newIndex])
}
}
prev() {
const newIndex = this.#index - 1
if (this.#arr[newIndex]) {
this.#index = newIndex
return this.#f(this.#arr[newIndex])
}
}
next() {
const newIndex = this.#index + 1
if (this.#arr[newIndex]) {
this.#index = newIndex
return this.#f(this.#arr[newIndex])
}
while (true) {
const { done, value } = this.#iter.next()
if (done) break
this.#arr.push(value)
if (this.#arr[newIndex]) {
this.#index = newIndex
return this.#f(this.#arr[newIndex])
}
}
}
find(f) {
const index = this.#arr.findIndex(x => f(x))
if (index > -1) {
this.#index = index
return this.#f(this.#arr[index])
}
while (true) {
const { done, value } = this.#iter.next()
if (done) break
this.#arr.push(value)
if (f(value)) {
this.#index = this.#arr.length - 1
return this.#f(value)
}
}
}
}
export class TTS {
#list
#ranges
#lastMark
#serializer = new XMLSerializer()
constructor(doc, textWalker, nodeFilter, highlight, granularity) {
this.doc = doc
this.highlight = highlight
this.#list = new ListIterator(getBlocks(doc, nodeFilter), range => {
const { entries, ssml } = getFragmentWithMarks(range, textWalker, nodeFilter, granularity)
this.#ranges = new Map(entries)
return [ssml, range]
})
}
#getMarkElement(doc, mark) {
if (!mark) return null
return doc.querySelector(`mark[name="${CSS.escape(mark)}"`)
}
#speak(doc, getNode) {
if (!doc) return
if (!getNode) return this.#serializer.serializeToString(doc)
const ssml = document.implementation.createDocument(NS.SSML, 'speak')
ssml.documentElement.replaceWith(ssml.importNode(doc.documentElement, true))
let node = getNode(ssml)?.previousSibling
while (node) {
const next = node.previousSibling ?? node.parentNode?.previousSibling
node.parentNode.removeChild(node)
node = next
}
const ssmlStr = this.#serializer.serializeToString(ssml)
return ssmlStr
}
start() {
this.#lastMark = null
const [doc] = this.#list.first() ?? []
if (!doc) return this.next()
return this.#speak(doc, ssml => this.#getMarkElement(ssml, this.#lastMark))
}
resume() {
const [doc] = this.#list.current() ?? []
if (!doc) return this.next()
return this.#speak(doc, ssml => this.#getMarkElement(ssml, this.#lastMark))
}
prev(paused) {
this.#lastMark = null
const [doc, range] = this.#list.prev() ?? []
if (paused && range) this.highlight(range.cloneRange())
return this.#speak(doc)
}
next(paused) {
this.#lastMark = null
const [doc, range] = this.#list.next() ?? []
if (paused && range) this.highlight(range.cloneRange())
return this.#speak(doc)
}
prevMark(paused) {
const marks = Array.from(this.#ranges.keys())
if (marks.length === 0) return
const currentIndex = this.#lastMark ? marks.indexOf(this.#lastMark) : -1
if (currentIndex > 0) {
const prevMarkName = marks[currentIndex - 1]
const range = this.#ranges.get(prevMarkName)
if (range) {
this.#lastMark = prevMarkName
if (paused) this.highlight(range.cloneRange())
const [doc] = this.#list.current() ?? []
return this.#speak(doc, ssml => this.#getMarkElement(ssml, prevMarkName))
}
} else {
const [doc, range] = this.#list.prev() ?? []
if (doc && range) {
const prevMarks = Array.from(this.#ranges.keys())
if (prevMarks.length > 0) {
const lastMarkName = prevMarks[prevMarks.length - 1]
const lastMarkRange = this.#ranges.get(lastMarkName)
if (lastMarkRange) {
this.#lastMark = lastMarkName
if (paused) this.highlight(lastMarkRange.cloneRange())
return this.#speak(doc, ssml => this.#getMarkElement(ssml, lastMarkName))
}
} else {
this.#lastMark = null
if (paused) this.highlight(range.cloneRange())
return this.#speak(doc)
}
}
}
}
nextMark(paused) {
const marks = Array.from(this.#ranges.keys())
if (marks.length === 0) return
const currentIndex = this.#lastMark ? marks.indexOf(this.#lastMark) : -1
if (currentIndex >= 0 && currentIndex < marks.length - 1) {
const nextMarkName = marks[currentIndex + 1]
const range = this.#ranges.get(nextMarkName)
if (range) {
this.#lastMark = nextMarkName
if (paused) this.highlight(range.cloneRange())
const [doc] = this.#list.current() ?? []
return this.#speak(doc, ssml => this.#getMarkElement(ssml, nextMarkName))
}
} else {
const [doc, range] = this.#list.next() ?? []
if (doc && range) {
const nextMarks = Array.from(this.#ranges.keys())
if (nextMarks.length > 0) {
const firstMarkName = nextMarks[0]
const firstMarkRange = this.#ranges.get(firstMarkName)
if (firstMarkRange) {
this.#lastMark = firstMarkName
if (paused) this.highlight(firstMarkRange.cloneRange())
return this.#speak(doc, ssml => this.#getMarkElement(ssml, firstMarkName))
}
} else {
this.#lastMark = null
if (paused) this.highlight(range.cloneRange())
return this.#speak(doc)
}
}
}
}
from(range) {
this.#lastMark = null
const [doc] = this.#list.find(range_ =>
range.compareBoundaryPoints(Range.END_TO_START, range_) <= 0)
let mark
for (const [name, range_] of this.#ranges.entries())
if (range.compareBoundaryPoints(Range.START_TO_START, range_) <= 0) {
mark = name
break
}
return this.#speak(doc, ssml => this.#getMarkElement(ssml, mark))
}
getLastRange() {
if (this.#lastMark) {
const range = this.#ranges.get(this.#lastMark)
if (range) return range.cloneRange()
}
}
setMark(mark) {
const range = this.#ranges.get(mark)
if (range) {
this.#lastMark = mark
this.highlight(range.cloneRange())
return range
}
}
}