use std::sync::Arc;
use canvist_layout::DisplayItem;
use canvist_layout::DisplayList;
use canvist_layout::DisplayOptions;
use canvist_layout::DocumentLayout;
use canvist_layout::LayoutError;
use canvist_layout::LayoutOptions;
use canvist_layout::TextMeasurer;
use canvist_layout::Theme;
use canvist_model::Document;
use canvist_scene::NodeId;
use canvist_scene::Scene;
use canvist_scene::Shape;
use canvist_scene::TextSizing;
use kurbo::Vec2;
use rustc_hash::FxHashMap;
const AUTO_WIDTH_LIMIT: f32 = 100_000.0;
#[derive(Debug)]
pub struct TextFrameLayout {
pub layout: DocumentLayout,
pub size: Vec2,
pub display: DisplayList,
}
pub struct TextLayouts {
theme: Arc<Theme>,
measurer: Box<dyn TextMeasurer>,
frames: FxHashMap<NodeId, TextFrameLayout>,
}
impl std::fmt::Debug for TextLayouts {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("TextLayouts")
.field("frames", &self.frames.len())
.finish_non_exhaustive()
}
}
impl TextLayouts {
pub fn new(theme: Arc<Theme>, measurer: impl TextMeasurer + 'static) -> Self {
Self {
theme,
measurer: Box::new(measurer),
frames: FxHashMap::default(),
}
}
#[must_use]
pub fn get(&self, id: NodeId) -> Option<&TextFrameLayout> {
self.frames.get(&id)
}
pub fn remove(&mut self, id: NodeId) {
self.frames.remove(&id);
}
pub fn ensure(&mut self, scene: &Scene, id: NodeId) -> Result<Option<Vec2>, LayoutError> {
let Some(node) = scene.node(id) else {
return Ok(None);
};
let Shape::Text(sizing) = node.props().shape else {
return Ok(None);
};
let size = node.props().size;
match scene.text(id) {
Some(doc) => self.lay_out(id, doc, sizing, size).map(Some),
None => Ok(self.frames.get(&id).map(|frame| frame.size)),
}
}
pub fn update(
&mut self,
scene: &Scene,
id: NodeId,
doc: &Document,
) -> Result<Option<Vec2>, LayoutError> {
let Some(node) = scene.node(id) else {
return Ok(None);
};
let Shape::Text(sizing) = node.props().shape else {
return Ok(None);
};
self.lay_out(id, doc, sizing, node.props().size).map(Some)
}
fn lay_out(
&mut self,
id: NodeId,
doc: &Document,
sizing: TextSizing,
size: Vec2,
) -> Result<Vec2, LayoutError> {
let width = match sizing {
TextSizing::AutoWidth => AUTO_WIDTH_LIMIT,
TextSizing::AutoHeight | TextSizing::Fixed => size.x.max(1.0) as f32,
};
let mut layout = DocumentLayout::new(doc, self.theme.clone(), LayoutOptions::new(width));
layout.layout_all(doc, &mut *self.measurer)?;
let height = layout.height();
let display = layout.display_list(&DisplayOptions::viewport(0.0, height));
let size = match sizing {
TextSizing::Fixed => size,
TextSizing::AutoHeight => Vec2::new(size.x, f64::from(height)),
TextSizing::AutoWidth => Vec2::new(f64::from(extent(&display)), f64::from(height)),
};
self.frames.insert(
id,
TextFrameLayout {
layout,
size,
display,
},
);
Ok(size)
}
}
fn extent(display: &DisplayList) -> f32 {
display
.items
.iter()
.filter_map(|item| {
match item {
DisplayItem::Text(run) => Some(run.x + run.width),
_ => None,
}
})
.fold(0.0, f32::max)
}