Skip to content

Commit

Permalink
Merge branch 'develop' into rewrite-swiftui
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenzeck committed Sep 19, 2024
2 parents 7c53197 + 0cfb105 commit a579cb5
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 35 deletions.
9 changes: 8 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,14 @@ All notable changes to this project will be documented in this file. Take a look

**Warning:** Features marked as *alpha* may change or be removed in a future release without notice. Use with caution.

<!-- ## [Unreleased] -->
## [Unreleased]

### Fixed

#### Navigator

* [#459](https://github.com/readium/swift-toolkit/issues/459) Fixed the stack overflow issue that occurred when running the text-to-speech on an EPUB file with many empty resources.


## [3.0.0-alpha.2]

Expand Down
15 changes: 7 additions & 8 deletions Sources/Navigator/TTS/PublicationSpeechSynthesizer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -339,15 +339,14 @@ public class PublicationSpeechSynthesizer: Loggable {
/// Loads the utterances for the next publication `ContentElement` item in the given `direction`.
private func loadNextUtterances(_ direction: Direction) async -> Bool {
do {
guard let content = try await publicationIterator?.next(direction) else {
return false
}

let nextUtterances = try tokenize(content)
.flatMap { utterances(for: $0) }
var nextUtterances: [Utterance] = []
while nextUtterances.isEmpty {
guard let content = try await publicationIterator?.next(direction) else {
return false
}

if nextUtterances.isEmpty {
return await loadNextUtterances(direction)
nextUtterances = try tokenize(content)
.flatMap { utterances(for: $0) }
}

utterances = CursorList(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,37 +102,37 @@ public class StringSearchService: SearchService {
private var index = -1

func next() async -> SearchResult<LocatorCollection?> {
guard index < publication.readingOrder.count - 1 else {
return .success(nil)
}
while index < publication.readingOrder.count - 1 {
index += 1

let link = publication.readingOrder[index]

guard
let resource = publication.get(link),
let mediaType = link.mediaType,
let extractor = extractorFactory.makeExtractor(for: resource, mediaType: mediaType)
else {
log(.warning, "Cannot extract text from resource: \(link.href)")
continue
}

index += 1
switch await extractor.extractText(of: resource) {
case let .success(text):
let locators = await findLocators(in: link, resourceIndex: index, text: text)
// If no occurrences were found in the current resource, skip to the next one automatically.
guard !locators.isEmpty else {
continue
}

let link = publication.readingOrder[index]
resultCount = (resultCount ?? 0) + locators.count
return .success(LocatorCollection(locators: locators))

guard
let resource = publication.get(link),
let mediaType = link.mediaType,
let extractor = extractorFactory.makeExtractor(for: resource, mediaType: mediaType)
else {
log(.warning, "Cannot extract text from resource: \(link.href)")
return await next()
}

switch await extractor.extractText(of: resource) {
case let .success(text):
let locators = await findLocators(in: link, resourceIndex: index, text: text)
// If no occurrences were found in the current resource, skip to the next one automatically.
guard !locators.isEmpty else {
return await next()
case let .failure(error):
return .failure(.reading(error))
}

resultCount = (resultCount ?? 0) + locators.count
return .success(LocatorCollection(locators: locators))

case let .failure(error):
return .failure(.reading(error))
}

return .success(nil)
}

private func findLocators(in link: Link, resourceIndex: Int, text: String) async -> [Locator] {
Expand Down

0 comments on commit a579cb5

Please sign in to comment.