Skip to content
Merged
31 changes: 28 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,9 @@ A single `$` is text, so prices stay as written. TranscriptKit does not read Mar

Without a renderer, a formula shows its LaTeX in the code font. A renderer gets the formula and the font size of the
text around it. It returns an image, the image's size in points, and its baseline: the distance from the bottom edge
up to the baseline. TranscriptKit calls it off the main thread, once for each formula and font size.
up to the baseline. TranscriptKit calls it off the main thread, once for each formula and font size. If it returns
nil, the formula shows its LaTeX as written once that LaTeX stays the same for a second. LaTeX that is not complete yet
can fail while it streams in, and the formula then keeps what it showed.

```swift
TranscriptText(stream: stream)
Expand All @@ -271,6 +273,28 @@ scaled down to fit. A paragraph that holds only display math is centered. While
nothing until it closes, so the text on screen only grows. Copy gives the LaTeX with its delimiters, and VoiceOver
reads the LaTeX.

Until the renderer draws a formula, the formula is empty space of about its size. The LaTeX of a formula can change
from one update to the next, for example when your app closes an open formula in each new snapshot of the text. Then
the formula keeps its last image until the new one is drawn. Each formula has at most one drawing in progress, and
newer LaTeX replaces the LaTeX that waits for its turn. Images that arrive together cause one layout. A view keeps
the images of the formulas in the document it shows, also when the cache removes them, so it does not draw them again.

Drawn formulas are kept in a `TranscriptMathCache`. All views share `TranscriptMathCache.shared` by default, so a
formula that shows in many messages is drawn once. The cache removes the least recently used formulas when they use
more than its byte limit, 16 MB by default. The cache does not keep a nil result: each view keeps it for the formulas
it shows. A view shows the formulas that any renderer of its cache drew, so give views whose renderers draw a formula
differently a cache each:

```swift
let cache = TranscriptMathCache(byteLimit: 8 * 1024 * 1024)

TranscriptText(stream: stream)
.mathRenderer(myRenderer, cache: cache)
```

In UIKit, set `mathCache` on `TranscriptTextView`. Setting a renderer does not empty the cache. Call `removeAll()` on
a cache after your renderer changes how it draws: the views that use the cache then draw their formulas again.

### Selection

The text is selectable by default. Turn selection off for text inside a view that reacts to taps, such as a card or a
Expand Down Expand Up @@ -408,8 +432,9 @@ let image = UIGraphicsImageRenderer(bounds: view.bounds).image { view.layer.rend
```

While a stream that has not finished goes on, `contentSettled()` returns at the first moment when nothing is pending.
A cancelled task returns at once. An image or a formula that comes back as nil counts as done. A load, a formula or
a highlight that never ends holds back `contentSettled()`, so use a timeout or cancel the task. Give the view its size
A cancelled task returns at once. An image that comes back as nil counts as done. A formula that comes back as nil
counts as done when it shows its LaTeX as written, about a second later. A load, a formula or a highlight that never
ends holds back `contentSettled()`, so use a timeout or cancel the task. Give the view its size
before the call: a view with no width lays out nothing and settles at once. Fades and tag animations that play are not
waited for, so turn off `textFadeInDuration` and `animatesTags` to compare pixels.

Expand Down
5 changes: 3 additions & 2 deletions Sources/TranscriptKit/Layout/DocumentLayout.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ public final class DocumentLayout: @unchecked Sendable {
self.document = document
let imageRevision = images?.revision ?? 0
self.imageRevision = imageRevision
images?.keepFormulas(of: document)

// A line-limited layout may hold a block cut short, so it is not reused.
let previous = style.lineLimit == nil ? previous.flatMap { $0.width == width && $0.style == style ? $0 : nil } : nil
Expand Down Expand Up @@ -718,11 +719,11 @@ public final class DocumentLayout: @unchecked Sendable {
}
return
}
// No picture: a chip with the alt text.
// No picture: a chip with the alt text, or for a formula being drawn, nothing.
guard let label = attachment.label else { return }
context.setFillColor(background)
context.addPath(CGPath(roundedRect: rect.insetBy(dx: 0, dy: 1), cornerWidth: 5, cornerHeight: 5, transform: nil))
context.fillPath()
guard let label = attachment.label else { return }
var ascent: CGFloat = 0
var descent: CGFloat = 0
CTLineGetTypographicBounds(label, &ascent, &descent, nil)
Expand Down
Loading
Loading