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
+21 -3
View File
@@ -4,7 +4,7 @@
use crate::geometry::{Point, Rect};
use crate::text::TextStyle;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[derive(Clone, Copy, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
/// 设备绘制使用的 RGBA 字节通道,区别于源代码的颜色文本。
pub struct Color {
pub red: u8,
@@ -38,6 +38,15 @@ impl Color {
#[derive(Clone, Debug)]
/// 原生画布最终顺序执行的绘制原语,已经使用对应设备比例。
pub enum DrawCommand {
/// Immutable RGBA image mapped from its local rectangle by rotation.
Image {
image: std::sync::Arc<crate::render::image::Image>,
origin: Point,
rect: Rect,
angle: f32,
opacity: f32,
tint: Option<Color>,
},
Scene3D {
rect: Rect,
view: std::sync::Arc<crate::scene3d::View>,
@@ -82,7 +91,7 @@ impl DrawCommand {
rect.x += delta.x;
rect.y += delta.y;
}
Self::Text { origin, .. } => {
Self::Text { origin, .. } | Self::Image { origin, .. } => {
origin.x += delta.x;
origin.y += delta.y;
}
@@ -123,6 +132,15 @@ impl DisplayList {
*width *= scale;
Some(rect)
}
DrawCommand::Image { origin, rect, .. } => {
origin.x *= scale;
origin.y *= scale;
rect.x *= scale;
rect.y *= scale;
rect.width *= scale;
rect.height *= scale;
None
}
DrawCommand::Text { origin, style, .. } => {
origin.x *= scale;
origin.y *= scale;
@@ -156,7 +174,7 @@ impl DisplayList {
rect.x += delta.x;
rect.y += delta.y;
}
DrawCommand::Text { origin, .. } => {
DrawCommand::Text { origin, .. } | DrawCommand::Image { origin, .. } => {
origin.x += delta.x;
origin.y += delta.y;
}
+193
View File
@@ -0,0 +1,193 @@
//! Immutable, bounded RGBA images shared by display lists. Decoding and text
//! preparation happen outside the frame loop; the CPU backend samples them.
use crate::{
display_list::Color,
geometry::Rect,
text::{self, TextStyle},
};
use std::{io::Cursor, sync::Arc};
#[derive(Debug, serde::Serialize)]
pub struct Image {
pub width: u32,
pub height: u32,
pub id: u64,
#[serde(skip)]
pixels: Vec<u8>,
#[serde(skip)]
pub outline: Vec<crate::geometry::Point>,
}
impl Image {
pub fn png(bytes: &[u8]) -> anyhow::Result<Arc<Self>> {
let mut decoder = png::Decoder::new(Cursor::new(bytes));
decoder.set_transformations(png::Transformations::EXPAND | png::Transformations::STRIP_16);
let mut reader = decoder.read_info()?;
let info = reader.info();
anyhow::ensure!(
info.width > 0
&& info.height > 0
&& u64::from(info.width) * u64::from(info.height) <= 4_194_304,
"image exceeds pixel budget"
);
let mut buffer = vec![
0;
reader
.output_buffer_size()
.ok_or_else(|| anyhow::anyhow!("image size overflow"))?
];
let info = reader.next_frame(&mut buffer)?;
let mut pixels = Vec::with_capacity(info.width as usize * info.height as usize * 4);
for chunk in buffer[..info.buffer_size()].chunks_exact(info.color_type.samples()) {
let rgba = match info.color_type {
png::ColorType::Rgba => [chunk[0], chunk[1], chunk[2], chunk[3]],
png::ColorType::Rgb => [chunk[0], chunk[1], chunk[2], 255],
png::ColorType::Grayscale => [chunk[0], chunk[0], chunk[0], 255],
png::ColorType::GrayscaleAlpha => [chunk[0], chunk[0], chunk[0], chunk[1]],
png::ColorType::Indexed => anyhow::bail!("unexpanded indexed PNG"),
};
pixels.extend_from_slice(&rgba);
}
let outline = alpha_hull(&pixels, info.width, info.height);
Ok(Arc::new(Self {
width: info.width,
height: info.height,
pixels,
id: next_id(),
outline,
}))
}
/// Cache a shaped word at the actual device resolution. Padding preserves
/// italic/serif overhangs. Returns its logical image rectangle and advance.
pub fn text(
value: &str,
style: TextStyle,
scale: f32,
color: Color,
) -> Result<(Arc<Self>, Rect, f32), &'static str> {
let size = text::measure(value, style);
let pad = (style.font_size * 0.3).ceil();
let width = ((size.width + pad * 2.) * scale).ceil().max(1.) as u32;
let height = ((size.height + pad * 2.) * scale).ceil().max(1.) as u32;
if u64::from(width) * u64::from(height) > 1_048_576 {
return Err("prepared text image exceeds pixel budget");
}
let mut pixels = vec![0; width as usize * height as usize * 4];
text::draw_clipped(
value,
style.scaled(scale),
color,
None,
Rect::new(-pad * scale, -pad * scale, width as f32, height as f32),
|x, y, c| {
let x = x + (pad * scale).round() as i32;
let y = y + (pad * scale).round() as i32;
if x < 0 || y < 0 || x >= width as i32 || y >= height as i32 {
return;
}
let i = (y as usize * width as usize + x as usize) * 4;
// All glyphs have the same ink color; alpha-over preserves overlaps.
pixels[i..i + 3].copy_from_slice(&[c.red, c.green, c.blue]);
pixels[i + 3] = (u16::from(c.alpha)
+ u16::from(pixels[i + 3]) * (255 - u16::from(c.alpha)) / 255)
as u8;
},
);
Ok((
Arc::new(Self {
width,
height,
pixels,
id: next_id(),
outline: vec![],
}),
Rect::new(-pad, -pad, width as f32 / scale, height as f32 / scale),
size.width,
))
}
pub fn sample(&self, u: f32, v: f32, opacity: f32) -> Color {
let x = u * self.width as f32 - 0.5;
let y = v * self.height as f32 - 0.5;
let (ix, iy) = (x.floor() as i32, y.floor() as i32);
let (fx, fy) = (x - x.floor(), y - y.floor());
let mut sum = [0.; 4];
for (dx, dy, w) in [
(0, 0, (1. - fx) * (1. - fy)),
(1, 0, fx * (1. - fy)),
(0, 1, (1. - fx) * fy),
(1, 1, fx * fy),
] {
let px = (ix + dx).clamp(0, self.width as i32 - 1) as usize;
let py = (iy + dy).clamp(0, self.height as i32 - 1) as usize;
let i = (py * self.width as usize + px) * 4;
let a = f32::from(self.pixels[i + 3]) * w;
for (j, s) in sum.iter_mut().enumerate().take(3) {
*s += f32::from(self.pixels[i + j]) * a;
}
sum[3] += a;
}
if sum[3] <= 0. {
return Color::rgba(0, 0, 0, 0);
}
Color::rgba(
(sum[0] / sum[3]).round() as u8,
(sum[1] / sum[3]).round() as u8,
(sum[2] / sum[3]).round() as u8,
(sum[3] * opacity).round() as u8,
)
}
}
/// Conservative convex silhouette from opaque row endpoints. Unlike an image's
/// rectangle, this excludes empty transparent margins. Generated once per PNG.
fn alpha_hull(pixels: &[u8], width: u32, height: u32) -> Vec<crate::geometry::Point> {
use crate::geometry::Point;
let mut points = Vec::new();
for y in 0..height {
let xs: Vec<_> = (0..width)
.filter(|&x| pixels[((y * width + x) * 4 + 3) as usize] >= 16)
.collect();
if let (Some(&left), Some(&right)) = (xs.first(), xs.last()) {
for x in [left as f32, right as f32 + 1.] {
for yy in [y as f32, y as f32 + 1.] {
points.push(Point::new(x, yy));
}
}
}
}
points.sort_by(|a, b| a.x.total_cmp(&b.x).then(a.y.total_cmp(&b.y)));
points.dedup();
if points.len() < 3 {
return points;
}
let cross =
|a: Point, b: Point, c: Point| (b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x);
let mut hull: Vec<Point> = Vec::new();
for &p in &points {
while hull.len() >= 2 && cross(hull[hull.len() - 2], hull[hull.len() - 1], p) <= 0. {
hull.pop();
}
hull.push(p);
}
let lower = hull.len();
for &p in points.iter().rev().skip(1) {
while hull.len() > lower && cross(hull[hull.len() - 2], hull[hull.len() - 1], p) <= 0. {
hull.pop();
}
hull.push(p);
}
hull.pop();
hull
}
fn next_id() -> u64 {
static NEXT: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(1);
NEXT.fetch_add(1, std::sync::atomic::Ordering::Relaxed)
}
impl PartialEq for Image {
fn eq(&self, other: &Self) -> bool {
self.id == other.id
}
}
+1
View File
@@ -1104,6 +1104,7 @@ impl LayoutRuntime {
width: size.width,
height: size.height,
interactive: item.node.interactive,
pointer_events: item.node.pointer_events,
});
if !structural {
commands.extend(paint_commands(item.node, x, y, size));
+3
View File
@@ -16,3 +16,6 @@ pub(crate) mod source_layout;
pub(crate) mod source_paint;
pub(crate) mod text;
pub(crate) mod viewport;
pub mod image;
pub mod text_flow;
+62
View File
@@ -69,6 +69,16 @@ impl SoftwareRenderer {
for command in list.commands() {
match command {
DrawCommand::Image {
image,
origin,
rect,
angle,
opacity,
tint,
} => {
canvas.draw_image(image, *origin, *rect, *angle, *opacity, *tint);
}
DrawCommand::Scene3D { rect, view } => {
let left = rect.x.max(canvas.clip.x).max(0.).floor() as u32;
let top = rect.y.max(canvas.clip.y).max(0.).floor() as u32;
@@ -163,6 +173,58 @@ struct Canvas<'a> {
}
impl Canvas<'_> {
/// Inverse-map only clipped destination pixels. Images use straight RGBA;
/// bilinear interpolation happens in premultiplied alpha to avoid dark halos.
fn draw_image(
&mut self,
image: &crate::render::image::Image,
origin: Point,
rect: Rect,
angle: f32,
opacity: f32,
tint: Option<Color>,
) {
if rect.width <= 0. || rect.height <= 0. || !angle.is_finite() || !opacity.is_finite() {
return;
}
let (sin, cos) = angle.sin_cos();
let corners = [
(rect.x, rect.y),
(rect.x + rect.width, rect.y),
(rect.x, rect.y + rect.height),
(rect.x + rect.width, rect.y + rect.height),
];
let mut lo = Point::new(f32::INFINITY, f32::INFINITY);
let mut hi = Point::new(f32::NEG_INFINITY, f32::NEG_INFINITY);
for (x, y) in corners {
let px = origin.x + cos * x - sin * y;
let py = origin.y + sin * x + cos * y;
lo.x = lo.x.min(px);
lo.y = lo.y.min(py);
hi.x = hi.x.max(px);
hi.y = hi.y.max(py);
}
let (left, top, right, bottom) =
self.clamped_bounds(Rect::new(lo.x, lo.y, hi.x - lo.x, hi.y - lo.y));
for y in top..bottom {
for x in left..right {
let dx = x as f32 + 0.5 - origin.x;
let dy = y as f32 + 0.5 - origin.y;
let u = (cos * dx + sin * dy - rect.x) / rect.width;
let v = (-sin * dx + cos * dy - rect.y) / rect.height;
if (0. ..1.).contains(&u) && (0. ..1.).contains(&v) {
let mut sample = image.sample(u, v, opacity.clamp(0., 1.));
if let Some(tint) = tint {
sample.red = tint.red;
sample.green = tint.green;
sample.blue = tint.blue;
}
self.blend_pixel(x, y, sample);
}
}
}
}
/// 清空本对象维护的当前内容或缓存,后续状态由调用方重新建立。
fn clear(&mut self, color: Color) {
self.pixels.fill(pack(color));
+2 -1
View File
@@ -79,12 +79,13 @@ fn decode(
let x = number(store, command, "x")?;
let y = number(store, command, "y")?;
let kind = text(store, command, "kind")?;
let color = if kind == "scene3d" {
let color = if matches!(kind.as_str(), "scene3d" | "image") {
String::new()
} else {
text(store, command, "color")?
};
let result = match kind.as_str() {
"image" => crate::bridge::graphics::decode_image(store, command)?,
"scene3d" => {
let snapshot = object(store.get_attr(command, "scene")?)?;
RenderCommand::Scene3D {
+36
View File
@@ -17,6 +17,8 @@ use crate::display_list::Color;
pub enum TextFamily {
Interface,
Code,
/// A registered family loaded through the generic font-resource API.
Custom(u32),
}
#[derive(Clone, Copy, Debug)]
@@ -82,6 +84,7 @@ struct PreparedText {
/// 共享字体系统、字形缓存和整形缓存;调用通过全局锁串行访问。
struct TextEngine {
fonts: FontSystem,
custom: Vec<(String, u64)>,
code_family: Option<String>,
swash: SwashCache,
buffers: HashMap<TextKey, PreparedText>,
@@ -121,6 +124,7 @@ impl TextEngine {
}
Self {
fonts,
custom: Vec::new(),
code_family: mono,
swash: SwashCache::new(),
buffers: HashMap::new(),
@@ -152,6 +156,7 @@ impl TextEngine {
}
let family = match style.family {
TextFamily::Interface => Family::SansSerif,
TextFamily::Custom(id) => Family::Name(&self.custom[id as usize].0),
// Keep Latin source on the selected code face, and allow ordinary
// script fallback for CJK instead of searching all monospace faces.
TextFamily::Code => self
@@ -365,6 +370,37 @@ pub fn fit(value: &str, width: f32, style: TextStyle) -> String {
format!("{}{ellipsis}", &value[..end])
}
/// Register an explicit face for graphics resources. Repeated identical loads
/// reuse the family; conflicting content and unbounded global growth fail.
pub fn register_font(bytes: Vec<u8>, family: &str) -> anyhow::Result<TextFamily> {
use std::hash::{Hash, Hasher};
let mut hash = std::collections::hash_map::DefaultHasher::new();
bytes.hash(&mut hash);
let hash = hash.finish();
let mut e = engine().lock().expect("text engine lock");
if let Some((id, (_, old))) = e.custom.iter().enumerate().find(|(_, v)| v.0 == family) {
anyhow::ensure!(*old == hash, "font family already has different content");
return Ok(TextFamily::Custom(id as u32));
}
anyhow::ensure!(e.custom.len() < 32, "font registry exceeds 32 families");
let previous = e.fonts.db().clone();
e.fonts.db_mut().load_font_data(bytes);
if !e
.fonts
.db()
.faces()
.any(|f| f.families.iter().any(|(n, _)| n == family))
{
*e.fonts.db_mut() = previous;
anyhow::bail!("font data does not contain requested family");
}
let id = e.custom.len() as u32;
e.custom.push((family.into(), hash));
e.buffers.clear();
e.source_bytes = 0;
Ok(TextFamily::Custom(id))
}
#[cfg(test)]
mod tests {
use super::*;
+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;
}
}
}
}
}
}
}