Steady read-aloud highlight and a calmer default pace

The spoken word is now painted behind unchanged text instead of
restyling the paragraph, so words never reshape, shift or rewrap as the
highlight moves. Revealing the word measures its own boxes against the
visible area, so a reversed transcript no longer jumps for a word already
in view. Sentence breaks the reading adds no longer map onto the next
item's first letter. 1.0x now reads at 0.8 of the platform's normal rate.
This commit is contained in:
2026-11-13 09:41:02 +00:00
parent b2736683e7
commit df0614e2f7
7 changed files with 310 additions and 78 deletions
@@ -14,6 +14,7 @@ class SpeechDocument {
required this.text,
required this.parts,
required this.sources,
this.added = const {},
});
final String text;
@@ -22,6 +23,11 @@ class SpeechDocument {
final List<String> parts;
final List<SpeechSource> sources;
/// Spoken indexes the reading added (a sentence break between blocks, the
/// "Code block." phrase): they come from no visible text, so a word that
/// reaches into them highlights only its own characters.
final Set<int> added;
/// The index of the part whose markdown is [markdown], or -1.
int partOf(String markdown) => parts.indexOf(markdown.trim());
@@ -33,7 +39,7 @@ class SpeechDocument {
final last = range.end.clamp(0, sources.length);
for (var index = range.start.clamp(0, last); index < last; index += 1) {
final source = sources[index];
if (source.part != part) continue;
if (source.part != part || added.contains(index)) continue;
if (start == null || source.offset < start) start = source.offset;
if (end == null || source.offset + 1 > end) end = source.offset + 1;
}
@@ -61,12 +67,14 @@ SpeechDocument speechDocumentFor(List<String> markdownParts) {
text: builder.spoken.toString(),
parts: parts,
sources: builder.sources,
added: builder.added,
);
}
class _SpeechBuilder {
final spoken = StringBuffer();
final sources = <SpeechSource>[];
final added = <int>{};
String _markdown = '';
var _part = 0;
var _cursor = 0;
@@ -103,11 +111,13 @@ class _SpeechBuilder {
_write(' ', at);
}
/// Writes text the reading adds, which no visible character stands for.
void _write(String value, int at) {
spoken.write(value);
for (var index = 0; index < value.length; index += 1) {
added.add(spoken.length + index);
sources.add((part: _part, offset: at));
}
spoken.write(value);
}
void _phrase(String value, int at) {