Skip to content

Commit ef8d221

Browse files
authored
Bugfixes for TTS (#390)
1 parent 4df93f7 commit ef8d221

File tree

3 files changed

+150
-34
lines changed

3 files changed

+150
-34
lines changed

readium/navigator/src/main/java/org/readium/r2/navigator/media3/tts/TtsEngineFacade.kt

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@ internal class TtsEngineFacade<S : TtsEngine.Settings, P : TtsEngine.Preferences
2626
engine.setListener(listener)
2727
}
2828

29-
private var currentTask: UtteranceTask<E>? = null
30-
3129
val voices: Set<V>
3230
get() = engine.voices
3331

@@ -45,54 +43,44 @@ internal class TtsEngineFacade<S : TtsEngine.Settings, P : TtsEngine.Preferences
4543
engine.close()
4644
}
4745

46+
private var currentTask: UtteranceTask<E>? = null
47+
4848
private data class UtteranceTask<E : TtsEngine.Error>(
4949
val requestId: TtsEngine.RequestId,
5050
val continuation: CancellableContinuation<E?>,
5151
val onRange: (IntRange) -> Unit
5252
)
5353

54+
private fun getTask(id: TtsEngine.RequestId) =
55+
currentTask?.takeIf { it.requestId == id }
56+
57+
private fun popTask(id: TtsEngine.RequestId) =
58+
getTask(id)
59+
?.also { currentTask = null }
60+
5461
private inner class EngineListener : TtsEngine.Listener<E> {
5562

5663
override fun onStart(requestId: TtsEngine.RequestId) {
5764
}
5865

5966
override fun onRange(requestId: TtsEngine.RequestId, range: IntRange) {
60-
currentTask
61-
?.takeIf { it.requestId == requestId }
62-
?.onRange
63-
?.invoke(range)
67+
getTask(requestId)?.onRange?.invoke(range)
6468
}
6569

6670
override fun onInterrupted(requestId: TtsEngine.RequestId) {
67-
currentTask
68-
?.takeIf { it.requestId == requestId }
69-
?.continuation
70-
?.cancel()
71-
currentTask = null
71+
popTask(requestId)?.continuation?.cancel()
7272
}
7373

7474
override fun onFlushed(requestId: TtsEngine.RequestId) {
75-
currentTask
76-
?.takeIf { it.requestId == requestId }
77-
?.continuation
78-
?.cancel()
79-
currentTask = null
75+
popTask(requestId)?.continuation?.cancel()
8076
}
8177

8278
override fun onDone(requestId: TtsEngine.RequestId) {
83-
currentTask
84-
?.takeIf { it.requestId == requestId }
85-
?.continuation
86-
?.resume(null) {}
87-
currentTask = null
79+
popTask(requestId)?.continuation?.resume(null) {}
8880
}
8981

9082
override fun onError(requestId: TtsEngine.RequestId, error: E) {
91-
currentTask
92-
?.takeIf { it.requestId == requestId }
93-
?.continuation
94-
?.resume(error) {}
95-
currentTask = null
83+
popTask(requestId)?.continuation?.resume(error) {}
9684
}
9785
}
9886
}

readium/shared/src/main/java/org/readium/r2/shared/publication/Locator.kt

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -134,14 +134,17 @@ public data class Locator(
134134
}
135135

136136
public fun substring(range: IntRange): Text {
137-
highlight ?: return this
138-
return tryOr(this) {
139-
copy(
140-
before = (before ?: "") + highlight.substring(0, range.first),
141-
highlight = highlight.substring(range),
142-
after = highlight.substring(range.last) + (after ?: "")
143-
)
144-
}
137+
if (highlight.isNullOrBlank()) return this
138+
139+
val fixedRange = range.first.coerceIn(0, highlight.length)..range.last.coerceIn(
140+
0,
141+
highlight.length - 1
142+
)
143+
return copy(
144+
before = (before ?: "") + highlight.substring(0, fixedRange.first),
145+
highlight = highlight.substring(fixedRange),
146+
after = highlight.substring((fixedRange.last + 1).coerceAtMost(highlight.length)) + (after ?: "")
147+
)
145148
}
146149

147150
public companion object {

readium/shared/src/test/java/org/readium/r2/shared/publication/LocatorTest.kt

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,131 @@ class LocatorTest {
337337
).toJSON()
338338
)
339339
}
340+
341+
@Test fun `substring from a range`() {
342+
val text = Locator.Text(
343+
before = "before",
344+
highlight = "highlight",
345+
after = "after"
346+
)
347+
348+
assertEquals(
349+
Locator.Text(
350+
before = "before",
351+
highlight = "h",
352+
after = "ighlightafter"
353+
),
354+
text.substring(0..-1)
355+
)
356+
357+
assertEquals(
358+
Locator.Text(
359+
before = "before",
360+
highlight = "h",
361+
after = "ighlightafter"
362+
),
363+
text.substring(0..0)
364+
)
365+
366+
assertEquals(
367+
Locator.Text(
368+
before = "beforehigh",
369+
highlight = "lig",
370+
after = "htafter"
371+
),
372+
text.substring(4..6)
373+
)
374+
375+
assertEquals(
376+
Locator.Text(
377+
before = "before",
378+
highlight = "highlight",
379+
after = "after"
380+
),
381+
text.substring(0..8)
382+
)
383+
384+
assertEquals(
385+
Locator.Text(
386+
before = "beforehighli",
387+
highlight = "ght",
388+
after = "after"
389+
),
390+
text.substring(6..12)
391+
)
392+
393+
assertEquals(
394+
Locator.Text(
395+
before = "beforehighligh",
396+
highlight = "t",
397+
after = "after"
398+
),
399+
text.substring(8..12)
400+
)
401+
402+
assertEquals(
403+
Locator.Text(
404+
before = "beforehighlight",
405+
highlight = "",
406+
after = "after"
407+
),
408+
text.substring(9..12)
409+
)
410+
}
411+
412+
@Test fun `substring from a range with null components`() {
413+
assertEquals(
414+
Locator.Text(
415+
before = "high",
416+
highlight = "lig",
417+
after = "htafter"
418+
),
419+
Locator.Text(
420+
before = null,
421+
highlight = "highlight",
422+
after = "after"
423+
).substring(4..6)
424+
)
425+
426+
assertEquals(
427+
Locator.Text(
428+
before = "beforehigh",
429+
highlight = "lig",
430+
after = "ht"
431+
),
432+
Locator.Text(
433+
before = "before",
434+
highlight = "highlight",
435+
after = null
436+
).substring(4..6)
437+
)
438+
439+
assertEquals(
440+
Locator.Text(
441+
before = "before",
442+
highlight = null,
443+
after = "after"
444+
),
445+
Locator.Text(
446+
before = "before",
447+
highlight = null,
448+
after = "after"
449+
).substring(4..6)
450+
)
451+
452+
assertEquals(
453+
Locator.Text(
454+
before = "before",
455+
highlight = "",
456+
after = "after"
457+
),
458+
Locator.Text(
459+
before = "before",
460+
highlight = "",
461+
after = "after"
462+
).substring(4..6)
463+
)
464+
}
340465
}
341466

342467
@RunWith(RobolectricTestRunner::class)

0 commit comments

Comments
 (0)