feat: render interactive manuscript from Endor through generic graphics

This commit is contained in:
2026-11-12 10:52:28 +08:00
parent d29a1be329
commit 2715c1ce2c
71 changed files with 3000 additions and 78 deletions
+142
View File
@@ -0,0 +1,142 @@
//! Width-independent word preparation and variable-width line filling. The
//! exclusion algorithm owns geometry; this module only consumes text in order.
use crate::{display_list::Color, geometry::Rect, render::image::Image, text::TextStyle};
use std::{collections::HashMap, sync::Arc};
use unicode_segmentation::UnicodeSegmentation;
#[derive(Debug)]
pub struct Ink {
pub image: Arc<Image>,
pub rect: Rect,
pub width: f32,
}
#[derive(Debug)]
pub struct Word {
pub ink: Arc<Ink>,
pub letters: Vec<Arc<Ink>>,
pub space: f32,
pub newline: bool,
}
#[derive(Debug)]
pub struct Prepared {
pub words: Vec<Word>,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct Cursor {
pub word: usize,
pub letter: usize,
}
pub struct Placement {
pub word: usize,
pub letter: Option<usize>,
pub x: f32,
pub y: f32,
}
impl Prepared {
pub fn new(
value: &str,
style: TextStyle,
scale: f32,
color: Color,
) -> Result<Self, &'static str> {
let mut cache: HashMap<String, Arc<Ink>> = HashMap::new();
let mut pixels = 0_u64;
let mut ink = |text: &str| -> Result<Arc<Ink>, &'static str> {
if let Some(ink) = cache.get(text) {
return Ok(ink.clone());
}
let (image, rect, width) = Image::text(text, style, scale, color)?;
pixels += u64::from(image.width) * u64::from(image.height);
if pixels > 8_388_608 {
return Err("prepared text exceeds 8M pixel cache budget");
}
let ink = Arc::new(Ink { image, rect, width });
cache.insert(text.to_owned(), ink.clone());
Ok(ink)
};
let mut words = Vec::new();
for para in value.split_inclusive('\n') {
let mut start = 0;
for (end, _) in unicode_linebreak::linebreaks(para) {
let part = &para[start..end];
let body = part.trim_end_matches(char::is_whitespace);
let space =
crate::text::measure(part[body.len()..].trim_matches(['\n', '\r']), style)
.width;
words.push(Word {
ink: ink(body)?,
letters: body
.graphemes(true)
.map(&mut ink)
.collect::<Result<Vec<_>, _>>()?,
space,
newline: part.ends_with('\n'),
});
start = end;
}
}
Ok(Self { words })
}
/// The same cursor crosses left/right fragments. Narrow gaps never discard
/// a word; a word wider than every remaining fragment falls back to graphemes.
pub fn row(
&self,
cursor: &mut Cursor,
ranges: &[(f32, f32)],
y: f32,
out: &mut Vec<Placement>,
) {
for (ri, &(left, right)) in ranges.iter().enumerate() {
let mut x = left;
while let Some(word) = self.words.get(cursor.word) {
if word.letters.is_empty() {
cursor.word += 1;
cursor.letter = 0;
if word.newline {
return;
}
continue;
}
let remaining_max = ranges[ri..].iter().map(|(l, r)| r - l).fold(0., f32::max);
if cursor.letter == 0 && word.ink.width <= right - x + 0.001 {
out.push(Placement {
word: cursor.word,
letter: None,
x,
y,
});
x += word.ink.width + word.space;
cursor.word += 1;
if word.newline {
return;
}
} else if cursor.letter == 0 && word.ink.width <= remaining_max + 0.001 {
break;
} else {
let letter = &word.letters[cursor.letter];
if letter.width > right - x + 0.001 {
break;
}
out.push(Placement {
word: cursor.word,
letter: Some(cursor.letter),
x,
y,
});
x += letter.width;
cursor.letter += 1;
if cursor.letter == word.letters.len() {
cursor.word += 1;
cursor.letter = 0;
x += word.space;
if word.newline {
return;
}
}
}
}
}
}
}