Formalize the behavioral specification for off-thread Knuth-Plass text chunk layout and virtualized QML rendering in specs/knuth-plass-rendering.allium, and introduce a test suite covering all 66 specification obligations across value types, state machine transitions, invariants, and surface contracts. To satisfy the specification's precondition and invariant contracts: - Validate and clamp initial restore chunk bounds when opening documents in ReadingModel to ensure anchor chunks remain strictly within bounds. - Guard scroll position updates against negative and out-of-bounds chunk indices to honor the ScrollPositionChanged preconditions. - Emit positionSaved on publisher color toggles to fulfill the PositionPersisted contract without triggering unnecessary relayouts. - Register KnuthPlassRenderingSpecTest in CMakeLists.txt and document the spec and test hierarchy in AGENTS.md.
381 lines
12 KiB
Plaintext
381 lines
12 KiB
Plaintext
-- allium: 3
|
|
-- knuth-plass-rendering.allium
|
|
|
|
-- Scope: Off-thread Knuth-Plass text layout and virtualized QML rendering pipeline
|
|
-- Includes:
|
|
-- - Text chunk layout measurement and two-phase execution (synchronous visible window + asynchronous background)
|
|
-- - Knuth-Plass optimal line breaking (boxes, glues, penalties, two tolerance passes, emergency overfull rescue, character splitting)
|
|
-- - On-demand chunk layout fallback for scrolling into unmeasured content
|
|
-- - Fast-path layout reflow on viewport width and line spacing adjustments
|
|
-- - Virtualized viewport rendering, clip-rect culling, and WCAG contrast safety
|
|
-- - Reading position persistence and restoration
|
|
-- Excludes:
|
|
-- - Comic and Manga spread pagination and RTL reading mode (separate comic reading spec)
|
|
-- - Document file format parsing and archive extraction (separate document intake spec)
|
|
-- - Application shell chrome, navigation sidebars, and file chooser dialogs
|
|
|
|
------------------------------------------------------------
|
|
-- External Entities
|
|
------------------------------------------------------------
|
|
|
|
external entity BookDocument {
|
|
content_hash: String
|
|
title: String
|
|
total_chunks: Integer
|
|
}
|
|
|
|
external entity ReaderTheme {
|
|
name: String
|
|
reading_text: String
|
|
button_accent: String
|
|
dim_text: String
|
|
surface: String
|
|
}
|
|
|
|
------------------------------------------------------------
|
|
-- Value Types
|
|
------------------------------------------------------------
|
|
|
|
value LayoutMetrics {
|
|
content_width: Decimal
|
|
font_size: Decimal
|
|
line_spacing: Decimal
|
|
horizontal_margin: Decimal
|
|
vertical_margin: Decimal
|
|
}
|
|
|
|
value ReadingPosition {
|
|
chunk_index: Integer
|
|
intra_offset: Decimal
|
|
progress: Decimal
|
|
}
|
|
|
|
value FormattedRun {
|
|
text: String
|
|
width: Decimal
|
|
is_link: Boolean
|
|
publisher_color: String?
|
|
publisher_background: String?
|
|
}
|
|
|
|
value FormattedLine {
|
|
runs: List<FormattedRun>
|
|
start_token: Integer
|
|
end_token: Integer
|
|
adjustment_ratio: Decimal
|
|
is_hyphenated: Boolean
|
|
top: Decimal
|
|
bottom: Decimal
|
|
}
|
|
|
|
value PositionedDecoration {
|
|
top: Decimal
|
|
bottom: Decimal
|
|
left: Decimal
|
|
right: Decimal
|
|
border_left: Decimal
|
|
border_color: String?
|
|
background_color: String?
|
|
is_semantic_quote_bar: Boolean
|
|
}
|
|
|
|
value PositionedImage {
|
|
url: String
|
|
top: Decimal
|
|
bottom: Decimal
|
|
width: Decimal
|
|
height: Decimal
|
|
}
|
|
|
|
value ChunkLayoutResult {
|
|
lines: List<FormattedLine>
|
|
decorations: List<PositionedDecoration>
|
|
images: List<PositionedImage>
|
|
break_pass: LineBreakPass
|
|
total_height: Decimal
|
|
content_width: Decimal
|
|
}
|
|
|
|
value ViewportClip {
|
|
top: Decimal
|
|
bottom: Decimal
|
|
}
|
|
|
|
------------------------------------------------------------
|
|
-- Enumerations
|
|
------------------------------------------------------------
|
|
|
|
enum LayoutPhase {
|
|
unmeasured
|
|
measuring_visible
|
|
visible_ready
|
|
completed
|
|
}
|
|
|
|
enum ChunkState {
|
|
unmeasured
|
|
measured
|
|
}
|
|
|
|
enum LineBreakPass {
|
|
standard_tolerance
|
|
loose_tolerance
|
|
emergency_rescue
|
|
}
|
|
|
|
------------------------------------------------------------
|
|
-- Entities and Variants
|
|
------------------------------------------------------------
|
|
|
|
entity TextReadingSession {
|
|
document: BookDocument
|
|
metrics: LayoutMetrics
|
|
theme: ReaderTheme
|
|
use_book_colours: Boolean
|
|
status: LayoutPhase
|
|
anchor_chunk: Integer
|
|
intra_offset: Decimal
|
|
progress: Decimal
|
|
current_generation: Integer
|
|
visible_window_start: Integer
|
|
visible_window_end: Integer
|
|
chunks: List<TextChunk> with session = this
|
|
|
|
transitions status {
|
|
unmeasured -> measuring_visible
|
|
measuring_visible -> visible_ready
|
|
visible_ready -> completed
|
|
completed -> measuring_visible
|
|
visible_ready -> measuring_visible
|
|
}
|
|
}
|
|
|
|
entity TextChunk {
|
|
session: TextReadingSession
|
|
index: Integer
|
|
section_href: String
|
|
state: ChunkState
|
|
generation: Integer
|
|
layout_height: Decimal
|
|
layout: ChunkLayoutResult?
|
|
|
|
transitions state {
|
|
unmeasured -> measured
|
|
measured -> unmeasured
|
|
}
|
|
}
|
|
|
|
------------------------------------------------------------
|
|
-- Config
|
|
------------------------------------------------------------
|
|
|
|
config {
|
|
visible_window_before_anchor: Integer = 5
|
|
visible_window_after_anchor: Integer = 25
|
|
default_tolerance: Decimal = 2.0
|
|
loose_tolerance: Decimal = 10.0
|
|
wcag_min_contrast_ratio: Decimal = 3.0
|
|
default_placeholder_height: Decimal = 60.0
|
|
oversized_word_fallback_enabled: Boolean = true
|
|
}
|
|
|
|
------------------------------------------------------------
|
|
-- Rules
|
|
------------------------------------------------------------
|
|
|
|
rule OpenDocument {
|
|
when: OpenDocumentRequested(reader, document, initial_chunk, initial_offset, metrics, theme, use_book_colours)
|
|
requires: initial_chunk >= 0
|
|
requires: initial_chunk < document.total_chunks
|
|
ensures:
|
|
let session = TextReadingSession.created(
|
|
document: document,
|
|
metrics: metrics,
|
|
theme: theme,
|
|
use_book_colours: use_book_colours,
|
|
status: measuring_visible,
|
|
anchor_chunk: initial_chunk,
|
|
intra_offset: initial_offset,
|
|
progress: 0.0,
|
|
current_generation: 1,
|
|
visible_window_start: initial_chunk - config.visible_window_before_anchor,
|
|
visible_window_end: initial_chunk + config.visible_window_after_anchor
|
|
)
|
|
session.status = visible_ready
|
|
RestoreReady(session: session, chunk_index: initial_chunk, intra_offset: initial_offset)
|
|
}
|
|
|
|
rule CompleteBackgroundLayout {
|
|
when: session: TextReadingSession.status transitions_to visible_ready
|
|
ensures:
|
|
for chunk in session.chunks:
|
|
chunk.state = measured
|
|
session.status = completed
|
|
}
|
|
|
|
rule ScrollPositionChanged {
|
|
when: ScrollPositionUpdated(reader, session, top_chunk_index, intra_offset, proportion)
|
|
requires: top_chunk_index >= 0
|
|
requires: top_chunk_index < session.document.total_chunks
|
|
ensures:
|
|
session.anchor_chunk = top_chunk_index
|
|
session.intra_offset = intra_offset
|
|
session.progress = proportion
|
|
PositionPersisted(
|
|
content_hash: session.document.content_hash,
|
|
position: ReadingPosition{
|
|
chunk_index: top_chunk_index,
|
|
intra_offset: intra_offset,
|
|
progress: proportion
|
|
}
|
|
)
|
|
}
|
|
|
|
rule EnsureChunkLayoutOnDemand {
|
|
when: ChunkLayoutRequested(reader, session, chunk)
|
|
requires: chunk.session = session
|
|
requires: chunk.state = unmeasured
|
|
ensures:
|
|
chunk.state = measured
|
|
chunk.generation = session.current_generation
|
|
}
|
|
|
|
rule TogglePublisherColors {
|
|
when: SetPublisherColors(reader, session, enabled)
|
|
ensures:
|
|
session.use_book_colours = enabled
|
|
PositionPersisted(
|
|
content_hash: session.document.content_hash,
|
|
position: ReadingPosition{
|
|
chunk_index: session.anchor_chunk,
|
|
intra_offset: session.intra_offset,
|
|
progress: session.progress
|
|
}
|
|
)
|
|
}
|
|
|
|
rule UpdateFontSizeMetrics {
|
|
when: FontSizeChanged(reader, session, new_font_size)
|
|
requires: new_font_size > 0.0
|
|
ensures:
|
|
session.metrics.font_size = new_font_size
|
|
session.current_generation = session.current_generation + 1
|
|
session.status = measuring_visible
|
|
for chunk in session.chunks:
|
|
chunk.state = unmeasured
|
|
chunk.generation = session.current_generation
|
|
session.status = visible_ready
|
|
RestoreReady(session: session, chunk_index: session.anchor_chunk, intra_offset: session.intra_offset)
|
|
}
|
|
|
|
rule UpdateViewportWidthOrSpacing {
|
|
when: ViewportGeometryReflow(reader, session, new_content_width, new_line_spacing, clip)
|
|
requires: new_content_width > 0.0
|
|
requires: new_line_spacing > 0.0
|
|
ensures:
|
|
session.metrics.content_width = new_content_width
|
|
session.metrics.line_spacing = new_line_spacing
|
|
session.current_generation = session.current_generation + 1
|
|
session.status = measuring_visible
|
|
session.status = visible_ready
|
|
RestoreReady(session: session, chunk_index: session.anchor_chunk, intra_offset: session.intra_offset)
|
|
}
|
|
|
|
------------------------------------------------------------
|
|
-- Invariants
|
|
------------------------------------------------------------
|
|
|
|
invariant ReadingProgressBounded {
|
|
for s in TextReadingSession:
|
|
s.progress >= 0.0 and s.progress <= 1.0
|
|
}
|
|
|
|
invariant ChunkHeightPositive {
|
|
for c in TextChunk:
|
|
c.state = measured implies c.layout_height > 0.0
|
|
}
|
|
|
|
invariant AnchorChunkInBounds {
|
|
for s in TextReadingSession:
|
|
s.anchor_chunk >= 0 and s.anchor_chunk < s.document.total_chunks
|
|
}
|
|
|
|
invariant ChunkIndexingValid {
|
|
for c in TextChunk:
|
|
c.index >= 0 and c.index < c.session.document.total_chunks
|
|
}
|
|
|
|
invariant ChunkSectionHrefPresent {
|
|
for c in TextChunk:
|
|
c.section_href != ""
|
|
}
|
|
|
|
invariant MeasuredChunkHasLayout {
|
|
for c in TextChunk:
|
|
c.state = measured implies c.layout != null
|
|
}
|
|
|
|
------------------------------------------------------------
|
|
-- Actor Declarations
|
|
------------------------------------------------------------
|
|
|
|
actor Reader {
|
|
identified_by: BookDocument where total_chunks > 0
|
|
}
|
|
|
|
------------------------------------------------------------
|
|
-- Surfaces
|
|
------------------------------------------------------------
|
|
|
|
surface ReadingView {
|
|
facing reader: Reader
|
|
context session: TextReadingSession
|
|
|
|
exposes:
|
|
session.document.title
|
|
session.progress
|
|
session.anchor_chunk
|
|
session.use_book_colours
|
|
session.status
|
|
session.theme.name
|
|
session.theme.reading_text
|
|
session.theme.button_accent
|
|
session.theme.dim_text
|
|
session.theme.surface
|
|
|
|
provides:
|
|
OpenDocumentRequested(reader, session.document, session.anchor_chunk, session.intra_offset, session.metrics, session.theme, session.use_book_colours)
|
|
ScrollPositionUpdated(reader, session, session.anchor_chunk, session.intra_offset, session.progress)
|
|
SetPublisherColors(reader, session, session.use_book_colours)
|
|
FontSizeChanged(reader, session, session.metrics.font_size)
|
|
ViewportGeometryReflow(reader, session, session.metrics.content_width, session.metrics.line_spacing, ViewportClip{top: 0.0, bottom: 1000.0})
|
|
ChunkLayoutRequested(reader, session, chunk) when chunk.state = unmeasured
|
|
|
|
@guarantee InstantVisibleFrameOnMetricsChange
|
|
-- Visible chunk window layout is guaranteed to resolve synchronously (<1ms)
|
|
-- before the next frame is rendered, preventing visual flashes or blank screens.
|
|
|
|
@guarantee BackgroundThreadIsolation
|
|
-- Off-screen chunks must be calculated asynchronously off the UI thread
|
|
-- ensuring scrolling and window resizing maintain 60fps+ without UI stutters.
|
|
|
|
@guarantee ZeroLayoutOnColorToggle
|
|
-- Toggling publisher colors executes at paint time only
|
|
-- and does not trigger chunk relayout or viewport position shifts.
|
|
|
|
@guarantee ContrastSafety
|
|
-- When publisher colors are enabled, text failing WCAG contrast
|
|
-- ratio (3.0) against its effective background is painted in black or white.
|
|
|
|
@guarantee ClipRectCulling
|
|
-- The rendering delegate uses binary search to draw only text lines
|
|
-- intersecting the viewport clip bounding rect, culling off-screen decorations and images.
|
|
}
|
|
|
|
------------------------------------------------------------
|
|
-- Open Questions
|
|
------------------------------------------------------------
|
|
|
|
open question "Debounce interval for continuous window resizing - should it scale with chunk count or remain fixed at 150ms?"
|
|
open question "Character-level breakdown - should emergency letter splitting insert hyphens or break without punctuation?"
|