Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion tts.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@ const getAlphabet = el => {
}

const getSegmenter = (lang = 'en', granularity = 'word') => {
const segmenter = new Intl.Segmenter(lang, { granularity })
// `lang` may be explicitly null when getLang() walks to the document
// root without finding a language attribute — common for PDFs, which
// almost never declare a language. The default parameter above only
// applies when the argument is `undefined`, not `null`, so we need a
// separate fallback here to avoid `Intl.Segmenter(null, ...)` throwing.
const segmenter = new Intl.Segmenter(lang || 'en', { granularity })
const granularityIsWord = granularity === 'word'
return function* (strs, makeRange) {
const str = strs.join('')
Expand Down
10 changes: 8 additions & 2 deletions view.js
Original file line number Diff line number Diff line change
Expand Up @@ -585,8 +585,14 @@ export class View extends HTMLElement {
const doc = this.renderer.getContents()[0].doc
if (this.tts && this.tts.doc === doc) return
const { TTS } = await import('./tts.js')
this.tts = new TTS(doc, textWalker, highlight || (range =>
this.renderer.scrollToAnchor(range, true)), granularity)
this.tts = new TTS(doc, textWalker, highlight || (range => {
// Not every renderer implements scrollToAnchor — in particular,
// the PDF renderer is fixed-layout and has no notion of an
// anchor to scroll to. Guard the call so that TTS still works
// on PDFs, just without sentence-highlight sync.
if (typeof this.renderer.scrollToAnchor === 'function')
this.renderer.scrollToAnchor(range, true)
}), granularity)
}
startMediaOverlay() {
const { index } = this.renderer.getContents()[0]
Expand Down