use crate::compositor::layout::Rect;
use crate::compositor::layout::border::draw_hr;
use crate::compositor::layout::cell::{CellBuffer, CellStyle};
use crate::compositor::layout::engine::{
self, LayoutCtx, Placement, resolve_or_inherit, try_temp_vec_from_slice,
};
use crate::compositor::scene::{NodeId, NodeKind, Scene, TextContent, TextSource};
use crate::parser::ast::{Alignment, HrStyle};
pub(crate) fn layout(
buf: &mut CellBuffer,
ctx: &mut LayoutCtx,
scene: &mut Scene,
node_id: NodeId,
) -> Placement {
let (data, children) = {
let Some(node) = scene.get(node_id) else {
return Placement::empty_at(ctx.x, ctx.y);
};
let NodeKind::Text(d) = node.kind() else {
return Placement::empty_at(ctx.x, ctx.y);
};
let Some(children) = try_temp_vec_from_slice(node.children(), ctx.governor.as_ref()) else {
buf.record_allocation_failure();
scene.record_resource_error();
return Placement::empty_at(ctx.x, ctx.y);
};
(d.clone(), children)
};
let placement = match data.source {
TextSource::Pre => layout_pre(buf, ctx, scene, node_id, &data),
TextSource::Art => layout_art(buf, ctx, scene, node_id, &data),
TextSource::ElementDef => layout_element_def(buf, ctx, scene, node_id, &data),
TextSource::TextAnimate => layout_text_animate(buf, ctx, scene, node_id, &data),
TextSource::Heading(level) => {
layout_heading(buf, ctx, scene, node_id, &data, level, &children)
}
TextSource::Text => layout_text_plain(buf, ctx, scene, &data, node_id, &children),
};
scene.update_placement(node_id, placement);
placement
}
fn layout_pre(
buf: &mut CellBuffer,
ctx: &mut LayoutCtx,
scene: &mut Scene,
node_id: NodeId,
data: &TextContent,
) -> Placement {
let start_x = ctx.x;
let start_y = ctx.y;
let width = ctx.width;
let mut max_w: u16 = 0;
let mut line_w: u16 = 0;
let mut newlines: u16 = 0;
for run in &data.runs {
let mut first = true;
for segment in run.text.split('\n') {
if !first {
max_w = max_w.max(line_w);
line_w = 0;
newlines = newlines.saturating_add(1);
}
first = false;
line_w = line_w.saturating_add(crate::compositor::layout::text::display_width(
segment, ctx.wcfg,
) as u16);
}
}
max_w = max_w.max(line_w);
let block_offset = match data.align {
Alignment::Left => 0u16,
Alignment::Center => ctx.width.saturating_sub(max_w) / 2,
Alignment::Right => ctx.width.saturating_sub(max_w),
};
scene.allocate_buffer(node_id, width.max(1), 1);
let line_count = newlines.saturating_add(1);
let mut local_y: u16 = 0;
if let Some(text_buf) = scene.layout_buffer_mut(node_id) {
text_buf.ensure_height(line_count.max(1));
let mut local_x: u16 = block_offset;
for run in &data.runs {
let style = CellStyle {
fg: resolve_or_inherit(run.fg.as_ref(), ctx.style.fg, ctx.color_support),
bg: resolve_or_inherit(run.bg.as_ref(), ctx.style.bg, ctx.color_support),
bold: run.bold || ctx.style.bold,
italic: run.italic || ctx.style.italic,
underline: run.underline || ctx.style.underline,
strikethrough: run.strikethrough || ctx.style.strikethrough,
dim: run.dim || ctx.style.dim,
blink: run.blink || ctx.style.blink,
};
let mut first_segment = true;
for segment in run.text.split('\n') {
if !first_segment {
local_y = local_y.saturating_add(1);
local_x = block_offset;
}
first_segment = false;
text_buf.ensure_height(local_y.saturating_add(1));
if segment.is_empty() {
continue;
}
engine::put_str_clipped(
text_buf, local_x, local_y, segment, &style, width, ctx.wcfg,
);
local_x = local_x.saturating_add(crate::compositor::layout::text::display_width(
segment, ctx.wcfg,
) as u16);
}
}
local_y = local_y.saturating_add(1);
}
let h = local_y;
ctx.y = start_y.saturating_add(h);
buf.ensure_height(ctx.y);
let rect = Rect::new(start_x, start_y, width, h);
Placement {
rect,
flow_advance: h,
bbox: rect,
}
}
fn layout_art(
buf: &mut CellBuffer,
ctx: &mut LayoutCtx,
scene: &mut Scene,
node_id: NodeId,
data: &TextContent,
) -> Placement {
let start_x = ctx.x;
let start_y = ctx.y;
let ctx_width = ctx.width;
let content = data.runs.first().map(|r| r.text.as_str()).unwrap_or("");
scene.allocate_buffer(node_id, ctx_width.max(1), 1);
let line_count = content.split('\n').count() as u16;
let mut local_y: u16 = 0;
let style = ctx.style.clone();
if let Some(text_buf) = scene.layout_buffer_mut(node_id) {
text_buf.ensure_height(line_count.max(1));
for line in content.split('\n') {
text_buf.ensure_height(local_y.saturating_add(1));
engine::put_str_clipped(text_buf, 0, local_y, line, &style, ctx_width, ctx.wcfg);
local_y = local_y.saturating_add(1);
}
}
let h = local_y;
ctx.y = start_y.saturating_add(h);
buf.ensure_height(ctx.y);
let rect = Rect::new(start_x, start_y, ctx_width, h);
Placement {
rect,
flow_advance: h,
bbox: rect,
}
}
fn layout_element_def(
buf: &mut CellBuffer,
ctx: &mut LayoutCtx,
scene: &mut Scene,
node_id: NodeId,
data: &TextContent,
) -> Placement {
let start_x = ctx.x;
let start_y = ctx.y;
let ctx_width = ctx.width;
let run = data.runs.first();
let content = run.map(|r| r.text.as_str()).unwrap_or("").trim();
let style = CellStyle {
fg: resolve_or_inherit(
run.and_then(|r| r.fg.as_ref()),
ctx.style.fg,
ctx.color_support,
),
..ctx.style.clone()
};
let content_w = (crate::compositor::layout::text::display_width(content, ctx.wcfg)
.min(ctx_width as usize)) as u16;
let buf_w = content_w.max(ctx_width).max(1);
scene.allocate_buffer(node_id, buf_w, 1);
if let Some(text_buf) = scene.layout_buffer_mut(node_id) {
engine::put_str_clipped(text_buf, 0, 0, content, &style, buf_w, ctx.wcfg);
}
ctx.y = start_y.saturating_add(1);
buf.ensure_height(ctx.y);
let rect = Rect::new(start_x, start_y, ctx_width, 1);
let bbox_rect = Rect::new(start_x, start_y, content_w.max(1), 1);
Placement {
rect,
flow_advance: 1,
bbox: rect.union(bbox_rect),
}
}
fn layout_text_animate(
buf: &mut CellBuffer,
ctx: &mut LayoutCtx,
scene: &mut Scene,
node_id: NodeId,
data: &TextContent,
) -> Placement {
let start_x = ctx.x;
let start_y = ctx.y;
let ctx_width = ctx.width;
let content = data.runs.first().map(|r| r.text.as_str()).unwrap_or("");
scene.allocate_buffer(node_id, ctx_width.max(1), 1);
let style = ctx.style.clone();
if let Some(text_buf) = scene.layout_buffer_mut(node_id) {
engine::put_str_clipped(text_buf, 0, 0, content, &style, ctx_width, ctx.wcfg);
}
ctx.y = start_y.saturating_add(1);
buf.ensure_height(ctx.y);
let rect = Rect::new(start_x, start_y, ctx_width, 1);
Placement {
rect,
flow_advance: 1,
bbox: rect,
}
}
fn layout_heading(
buf: &mut CellBuffer,
ctx: &mut LayoutCtx,
scene: &mut Scene,
node_id: NodeId,
data: &TextContent,
level: u8,
children: &[NodeId],
) -> Placement {
let start_x = ctx.x;
let start_y = ctx.y;
let width = ctx.width;
let run = data.runs.first();
let content = run.map(|r| r.text.as_str().trim()).unwrap_or("");
let style = CellStyle {
fg: resolve_or_inherit(
run.and_then(|r| r.fg.as_ref()),
ctx.style.fg,
ctx.color_support,
),
bold: true,
..ctx.style.clone()
};
let inline_lines = if children.is_empty() {
None
} else {
let Some(segments) = super::try_collect_inline_segments(
children,
scene,
&style,
ctx.color_support,
None,
ctx.governor.as_ref(),
) else {
buf.record_allocation_failure();
scene.record_resource_error();
return Placement::empty_at(start_x, start_y);
};
if segments.is_empty() {
None
} else {
let Some(lines) = engine::try_wrap_inline_segments(
&segments,
width as usize,
ctx.wcfg,
ctx.governor.as_ref(),
) else {
buf.record_allocation_failure();
scene.record_resource_error();
return Placement::empty_at(start_x, start_y);
};
Some(lines)
}
};
scene.allocate_buffer(node_id, width.max(1), 1);
let mut local_ctx = LayoutCtx {
x: 0,
y: 0,
width,
viewport_height: ctx.viewport_height,
color_support: ctx.color_support,
wcfg: ctx.wcfg,
style: style.clone(),
governor: ctx.governor.clone(),
};
let wrapped = match scene.layout_buffer_mut(node_id) {
Some(text_buf) if !content.is_empty() => {
engine::render_wrapped_text(text_buf, &mut local_ctx, content, &style, Alignment::Left)
}
_ => true,
};
if !wrapped {
buf.record_allocation_failure();
scene.record_resource_error();
return Placement::empty_at(start_x, start_y);
}
if let Some(lines) = inline_lines {
let placeholder = CellBuffer::new(1, 1);
let mut text_buf = match scene.layout_buffer_mut(node_id) {
Some(b) => std::mem::replace(b, placeholder),
None => placeholder,
};
engine::render_inline_lines_with_origin(
&mut text_buf,
&mut local_ctx,
scene,
&lines,
Alignment::Left,
(start_x, start_y),
);
if let Some(b) = scene.layout_buffer_mut(node_id) {
*b = text_buf;
}
}
if level == 1 && local_ctx.y > 0 {
if let Some(text_buf) = scene.layout_buffer_mut(node_id) {
text_buf.ensure_height(local_ctx.y.saturating_add(1));
draw_hr(text_buf, local_ctx.y, 0, width, HrStyle::Heavy, &style);
}
local_ctx.y = local_ctx.y.saturating_add(1);
}
let h = local_ctx.y;
ctx.y = start_y.saturating_add(h);
buf.ensure_height(ctx.y);
let rect = Rect::new(start_x, start_y, width, h);
Placement {
rect,
flow_advance: h,
bbox: rect,
}
}
fn layout_text_plain(
buf: &mut CellBuffer,
ctx: &mut LayoutCtx,
scene: &mut Scene,
data: &TextContent,
node_id: NodeId,
children: &[NodeId],
) -> Placement {
let has_children = !children.is_empty();
let multi_run = data.runs.len() > 1;
if has_children || multi_run {
let start_x = ctx.x;
let start_y = ctx.y;
let width = ctx.width;
let Some(segments) = super::try_collect_inline_segments(
&[node_id],
scene,
&ctx.style,
ctx.color_support,
None,
ctx.governor.as_ref(),
) else {
buf.record_allocation_failure();
scene.record_resource_error();
return Placement::empty_at(start_x, start_y);
};
let lines = if segments.is_empty() {
None
} else {
let Some(lines) = engine::try_wrap_inline_segments(
&segments,
width as usize,
ctx.wcfg,
ctx.governor.as_ref(),
) else {
buf.record_allocation_failure();
scene.record_resource_error();
return Placement::empty_at(start_x, start_y);
};
Some(lines)
};
drop(segments);
scene.allocate_buffer(node_id, width.max(1), 1);
let mut local_ctx = LayoutCtx {
x: 0,
y: 0,
width,
viewport_height: ctx.viewport_height,
color_support: ctx.color_support,
wcfg: ctx.wcfg,
style: ctx.style.clone(),
governor: ctx.governor.clone(),
};
if let Some(lines) = lines {
let placeholder = crate::compositor::layout::cell::CellBuffer::new(1, 1);
let mut text_buf = match scene.layout_buffer_mut(node_id) {
Some(b) => std::mem::replace(b, placeholder),
None => placeholder,
};
engine::render_inline_lines_with_origin(
&mut text_buf,
&mut local_ctx,
scene,
&lines,
data.align,
(start_x, start_y),
);
if let Some(b) = scene.layout_buffer_mut(node_id) {
*b = text_buf;
}
}
let h = local_ctx.y;
ctx.y = start_y.saturating_add(h);
buf.ensure_height(ctx.y);
let rect = Rect::new(start_x, start_y, width, h);
return Placement {
rect,
flow_advance: h,
bbox: rect,
};
}
let start_x = ctx.x;
let start_y = ctx.y;
let width = ctx.width;
let run = data.runs.first();
let content = run.map(|r| r.text.as_str()).unwrap_or("");
let style = run
.map(|r| CellStyle {
fg: resolve_or_inherit(r.fg.as_ref(), ctx.style.fg, ctx.color_support),
bg: resolve_or_inherit(r.bg.as_ref(), ctx.style.bg, ctx.color_support),
bold: r.bold || ctx.style.bold,
italic: r.italic || ctx.style.italic,
underline: r.underline || ctx.style.underline,
strikethrough: r.strikethrough || ctx.style.strikethrough,
dim: r.dim || ctx.style.dim,
blink: r.blink || ctx.style.blink,
})
.unwrap_or_else(|| ctx.style.clone());
scene.allocate_buffer(node_id, width.max(1), 1);
let mut local_ctx = LayoutCtx {
x: 0,
y: 0,
width,
viewport_height: ctx.viewport_height,
color_support: ctx.color_support,
wcfg: ctx.wcfg,
style: ctx.style.clone(),
governor: ctx.governor.clone(),
};
let wrapped = if content.is_empty() {
true
} else {
let text = content.trim_matches(|c: char| c == '\t' || c == ' ');
match scene.layout_buffer_mut(node_id) {
Some(text_buf) if !text.is_empty() => {
engine::render_wrapped_text(text_buf, &mut local_ctx, text, &style, data.align)
}
_ => true,
}
};
if !wrapped {
buf.record_allocation_failure();
scene.record_resource_error();
return Placement::empty_at(start_x, start_y);
}
let h = local_ctx.y;
ctx.y = start_y.saturating_add(h);
buf.ensure_height(ctx.y);
let rect = Rect::new(start_x, start_y, width, h);
Placement {
rect,
flow_advance: h,
bbox: rect,
}
}