Skip to content
Merged
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
32 changes: 27 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -340,8 +340,17 @@ TranscriptText(stream: stream)
In UIKit, set `mathRenderer` on `TranscriptTextView`. TranscriptKit uses only the alpha of the image, and draws the
formula in the text color, so it follows light and dark appearance. A formula that is wider than the text column is
scaled down to fit. A paragraph that holds only display math is centered. While a formula streams in, it shows
nothing until it closes, so the text on screen only grows. Copy gives the LaTeX with its delimiters, and VoiceOver
reads the LaTeX.
nothing until it closes, so the text on screen only grows. Copy gives the LaTeX with its delimiters.

VoiceOver reads the LaTeX by default. To give VoiceOver other words, for example from a library that turns LaTeX into
speech, set a label. TranscriptKit calls it on the main thread:

```swift
TranscriptText(stream: stream)
.mathAccessibilityLabel { math in mySpeechText(forLaTeX: math.tex) }
```

In UIKit, set `mathAccessibilityLabel` on `TranscriptTextView`.

Two style options change display math. `displayMathAlignment` puts the formula at the leading edge, the center (the
default) or the trailing edge. With `displayMathScrolls`, a display formula that is wider than the column keeps its
Expand Down Expand Up @@ -396,13 +405,26 @@ them off with `.showsWritingTools(false)`, or with `showsWritingTools` on `Trans
### VoiceOver

VoiceOver reads the text block by block. Headings are in the rotor's list of headings, list items start with their
number or "Checked" or "Unchecked", and each link in a block is an action that opens it. The spoken words "Open %@",
"Checked", "Unchecked" and "Separator" come from your app's `Localizable.strings`, so you can translate them there.
number or "Checked" or "Unchecked", and each link in a block is an action that opens it. A list item also tells its
position in its list and the number of items, such as "Item 2 of 3". A table is a data table with one element for each
cell, so VoiceOver reads it cell by cell, with the row, the column and the column's header. When a table starts a list
item, its first cell reads the item's marker and tells its position. While text streams in, each block keeps its
element, so VoiceOver focus stays on its block. The view tells VoiceOver that the layout changed only when elements
come or go, or when a new layout moves them or changes their size. Text that streams into the last block does not
tell it.

Assistive technologies, such as VoiceOver, Speak Screen and Voice Control, can ask for the elements and the text from
any thread. TranscriptKit then does the work on the main thread and gives the answer to the thread that asked.

The spoken words "Open %@", "Checked", "Unchecked", "Separator" and "Item %1$ld of %2$ld" come from your app's
`Localizable.strings`, so you can translate them there.

### Line limit and alignment

For short text in cards and list rows, set a line limit and an alignment. When text is left out, the last line shown
ends in "…", and selection and copying cover only what is shown. `TranscriptText` also takes SwiftUI's modifiers:
ends in "…". The selection shows only on the text that is shown. A selection that reaches the end of that text, such as
Select All, also copies the text that is left out, and VoiceOver reads all of the text. `TranscriptText` also takes
SwiftUI's modifiers:

```swift
TranscriptText(document)
Expand Down
465 changes: 465 additions & 0 deletions Sources/TranscriptKit/Layout/AccessibilityContent.swift

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions Sources/TranscriptKit/Layout/DocumentLayout.swift
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,18 @@ public final class DocumentLayout: @unchecked Sendable {
a.containers.count > index && b.containers.count > index && a.containers[...index] == b.containers[...index]
}

/// Whether a line limit left some of the document's text out.
var leavesTextOut: Bool { length < document.utf16Count }

/// The part of the document that a selection of this layout stands for.
/// Under a line limit, a selection that reaches the end of the text
/// shown also covers the text that the limit leaves out, so Select All
/// followed by Copy copies all of the text.
func documentRange(forSelection range: Range<Int>) -> Range<Int> {
guard !range.isEmpty, range.upperBound >= length, leavesTextOut else { return range }
return range.lowerBound..<document.utf16Count
}

// MARK: Offsets and geometry

/// The last entry whose `key` is at most `value`, by binary search;
Expand Down
6 changes: 4 additions & 2 deletions Sources/TranscriptKit/Layout/TranscriptStyle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,10 @@ public struct TranscriptStyle: Equatable, Sendable {
public init() {}

/// The most lines to show, or nil for all of them. When text is left out,
/// the last line shown ends in "…". Selection and copying cover only the
/// text that is shown. Tables are shown whole or not at all.
/// the last line shown ends in "…". The selection shows only on the text
/// that is shown, but a selection that reaches its end also copies the
/// text that is left out, and VoiceOver reads all of the text. Tables
/// are shown whole or not at all.
public var lineLimit: Int?
/// Where the lines of paragraphs and headings sit in their column. Lists,
/// quotes, code and tables stay at the leading edge.
Expand Down
25 changes: 25 additions & 0 deletions Sources/TranscriptKit/iOS/MainThread.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#if canImport(UIKit) && !os(watchOS)
import Foundation

/// Runs `body` on the main thread and returns its result: at once on the
/// main thread, and otherwise after the main thread has run it.
///
/// Assistive technologies, such as VoiceOver, Speak Screen, Voice Control
/// and the text rotor, can call accessibility getters and `UITextInput`
/// methods from other threads. In the Swift 6 language mode, a main actor
/// method that Objective-C calls from another thread stops the app, so
/// those methods are `nonisolated` and do their work here. Only the calling
/// thread waits: the main thread never waits for another thread here.
nonisolated func onMainThread<Result>(_ body: @MainActor () -> Result) -> Result {
if Thread.isMainThread {
return MainActor.assumeIsolated { MainThreadResult(value: body()) }.value
}
return DispatchQueue.main.sync { MainActor.assumeIsolated { MainThreadResult(value: body()) } }.value
}

/// A result that goes from the main thread to the thread that waits for it.
/// The main thread hands it over and does not keep it.
private struct MainThreadResult<Value>: @unchecked Sendable {
let value: Value
}
#endif
11 changes: 11 additions & 0 deletions Sources/TranscriptKit/iOS/TranscriptText.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public struct TranscriptText: UIViewRepresentable {
private var highlighter: TranscriptCodeHighlighter?
private var mathRenderer: TranscriptMathRenderer?
private var mathCache: TranscriptMathCache = .shared
private var mathAccessibilityLabel: ((TranscriptMath) -> String)?
private var animatesTags = true
private var textFadeInDuration: TimeInterval?
private var animationTiming: TranscriptAnimationTiming = .immediately
Expand Down Expand Up @@ -92,6 +93,15 @@ public struct TranscriptText: UIViewRepresentable {
return copy
}

/// The words that VoiceOver says for a formula; see
/// `TranscriptTextView.mathAccessibilityLabel`. Read once, when the view
/// is created.
public func mathAccessibilityLabel(_ label: @escaping (TranscriptMath) -> String) -> TranscriptText {
var copy = self
copy.mathAccessibilityLabel = label
return copy
}

/// Fades in text as it streams in; see `TranscriptTextView.textFadeInDuration`.
public func fadesInText(_ fades: Bool = true, duration: TimeInterval = 0.3) -> TranscriptText {
var copy = self
Expand Down Expand Up @@ -184,6 +194,7 @@ public struct TranscriptText: UIViewRepresentable {
view.mathCache = mathCache
view.mathRenderer = mathRenderer
}
if let mathAccessibilityLabel { view.mathAccessibilityLabel = mathAccessibilityLabel }
return view
}

Expand Down
Loading
Loading