use std::collections::HashMap;
use std::sync::Arc;
use krilla::color::rgb;
use krilla::image::Image as KrillaImage;
use markdoc::IndexMap;
use markdoc::types::{RenderableTreeNode, Scalar, Tag};
use parley::{FontContext, Layout, LayoutContext};
use usvg::Tree as SvgTree;
use crate::assets::{AssetResolver, MediaFormat, NullAssetResolver, sniff_format};
use super::inline::{InlineProp, InlineRange, Inlines, LinkRange, MidAnchor, collect_inlines};
use super::paginate::paginate as paginate_blocks;
use super::style::MarkerSequence;
use super::style::Style;
use super::style::TableColumnSizing;
use super::text::{
FloatSpec, TextStyle, build_layout, build_layout_aligned, build_layout_float,
build_layout_float_anchored, measure_first_line_width,
};
#[derive(Clone)]
pub struct Block {
pub height: f32,
pub space_after: f32,
pub draw: BlockDraw,
pub outline: Option<OutlineEntry>,
pub anchor_id: Option<String>,
pub tag_role: Option<TagRole>,
pub page_column: u8,
pub column_span: bool,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TagRole {
Note,
}
#[derive(Debug, Clone)]
pub struct OutlineEntry {
pub level: u8,
pub text: String,
}
#[allow(clippy::large_enum_variant)]
pub enum SplitOutcome {
Whole(Block),
Split(Block, Block),
NoFit(Block),
}
impl Block {
pub fn collect_footnote_numbers(&self, out: &mut Vec<u32>) {
match &self.draw {
BlockDraw::Text(slice) => out.extend(slice.footnote_numbers()),
BlockDraw::BoxedGroup { children, .. } => {
for c in children {
c.collect_footnote_numbers(out);
}
}
BlockDraw::ListItem { body, .. } => {
for c in body {
c.collect_footnote_numbers(out);
}
}
BlockDraw::Float { image, wrap } => {
image.collect_footnote_numbers(out);
for c in wrap {
c.collect_footnote_numbers(out);
}
}
BlockDraw::FloatRegion { text, .. } => out.extend(text.footnote_numbers()),
_ => {}
}
}
pub fn is_heading(&self) -> bool {
self.outline.is_some()
}
pub fn is_list_item(&self) -> bool {
matches!(self.draw, BlockDraw::ListItem { .. })
}
pub fn is_spacer(&self) -> bool {
matches!(
self.draw,
BlockDraw::Rule {
width: 0.0,
thickness: 0.0,
..
}
)
}
pub fn is_figure(&self) -> bool {
matches!(
self.draw,
BlockDraw::Image { .. }
| BlockDraw::Svg { .. }
| BlockDraw::Float { .. }
| BlockDraw::FloatRegion { .. }
)
}
pub fn is_table(&self) -> bool {
matches!(self.draw, BlockDraw::Table { .. })
}
pub fn is_boxed_text_block(&self) -> bool {
matches!(self.draw, BlockDraw::BoxedGroup { .. })
}
pub fn is_substantial_follower(&self) -> bool {
self.is_list_item() || self.is_figure() || self.is_table() || self.is_boxed_text_block()
}
pub fn text_line_count(&self) -> usize {
match &self.draw {
BlockDraw::Text(slice) => slice.line_range.end.saturating_sub(slice.line_range.start),
_ => 0,
}
}
pub fn try_split(self, remaining: f32) -> SplitOutcome {
let needed = self.height + self.space_after;
if needed <= remaining {
return SplitOutcome::Whole(self);
}
if self.is_heading() {
let Block {
height,
space_after,
draw,
outline,
anchor_id,
tag_role,
page_column,
column_span,
} = self;
return SplitOutcome::NoFit(Block {
height,
space_after,
draw,
outline,
anchor_id,
tag_role,
page_column,
column_span,
});
}
let Block {
height,
space_after,
draw,
outline,
anchor_id,
tag_role,
page_column,
column_span,
} = self;
match draw {
BlockDraw::Text(slice) => try_split_text(
slice,
space_after,
remaining,
page_column,
column_span,
tag_role,
anchor_id,
outline,
),
BlockDraw::Table {
x,
column_widths,
rows,
cell_padding,
header_bg,
border_color,
border_thickness,
border_style,
edge,
caption,
} => try_split_table(
x,
column_widths,
rows,
cell_padding,
header_bg,
border_color,
border_thickness,
border_style,
edge,
space_after,
remaining,
anchor_id,
caption,
page_column,
column_span,
tag_role,
outline,
),
other => SplitOutcome::NoFit(Block {
height,
space_after,
draw: other,
outline,
anchor_id,
tag_role,
page_column,
column_span,
}),
}
}
}
const TEXT_SPLIT_MIN_LINES: usize = 3;
#[allow(clippy::too_many_arguments)]
fn try_split_text(
slice: TextSlice,
space_after: f32,
remaining: f32,
page_column: u8,
column_span: bool,
tag_role: Option<TagRole>,
anchor_id: Option<String>,
outline: Option<OutlineEntry>,
) -> SplitOutcome {
let start = slice.line_range.start;
let end = slice.line_range.end;
let total = end.saturating_sub(start);
let mut acc = 0.0_f32;
let mut split_at = start;
for i in start..end {
let h = slice.line_heights[i];
if acc + h > remaining {
break;
}
acc += h;
split_at = i + 1;
}
if split_at == start {
return SplitOutcome::NoFit(Block {
height: slice.height(),
space_after,
draw: BlockDraw::Text(slice),
outline,
anchor_id,
tag_role,
page_column,
column_span,
});
}
if split_at == end {
return SplitOutcome::Whole(Block {
height: slice.height(),
space_after,
draw: BlockDraw::Text(slice),
outline,
anchor_id,
tag_role,
page_column,
column_span,
});
}
let head_lines = split_at - start;
let tail_lines = end - split_at;
if total >= TEXT_SPLIT_MIN_LINES
&& (head_lines < TEXT_SPLIT_MIN_LINES || tail_lines < TEXT_SPLIT_MIN_LINES)
{
return SplitOutcome::NoFit(Block {
height: slice.height(),
space_after,
draw: BlockDraw::Text(slice),
outline,
anchor_id,
tag_role,
page_column,
column_span,
});
}
let head_height: f32 = slice.line_heights[start..split_at].iter().sum();
let tail_height: f32 = slice.line_heights[split_at..end].iter().sum();
let head = Block {
height: head_height,
space_after: 0.0,
draw: BlockDraw::Text(TextSlice {
layout: slice.layout.clone(),
text: slice.text.clone(),
links: slice.links.clone(),
mid_anchors: slice.mid_anchors.clone(),
footnote_calls: slice.footnote_calls.clone(),
line_heights: slice.line_heights.clone(),
x: slice.x,
line_range: start..split_at,
skip_y: slice.skip_y,
}),
outline: outline.clone(),
anchor_id: anchor_id.clone(),
tag_role,
page_column,
column_span,
};
let tail = Block {
height: tail_height,
space_after,
draw: BlockDraw::Text(TextSlice {
x: slice.x,
line_range: split_at..end,
skip_y: slice.skip_y + head_height,
layout: slice.layout,
text: slice.text,
links: slice.links,
mid_anchors: slice.mid_anchors,
footnote_calls: slice.footnote_calls,
line_heights: slice.line_heights,
}),
outline,
anchor_id,
tag_role: None,
page_column,
column_span,
};
SplitOutcome::Split(head, tail)
}
#[allow(clippy::too_many_arguments)]
fn try_split_table(
x: f32,
column_widths: Vec<f32>,
rows: Vec<TableRow>,
cell_padding: f32,
header_bg: rgb::Color,
border_color: rgb::Color,
border_thickness: f32,
border_style: super::style::TableBorders,
edge: Option<(rgb::Color, f32)>,
space_after: f32,
remaining: f32,
anchor_id: Option<String>,
caption: Option<String>,
page_column: u8,
column_span: bool,
tag_role: Option<TagRole>,
outline: Option<OutlineEntry>,
) -> SplitOutcome {
let header_count = rows.iter().take_while(|r| r.is_header).count();
let header_height: f32 = rows[..header_count].iter().map(|r| r.height).sum::<f32>()
+ border_thickness * (header_count as f32 + 1.0);
let mut included_body = 0;
let mut footprint = header_height;
for body_row in &rows[header_count..] {
let increment = body_row.height + border_thickness;
if footprint + increment > remaining {
break;
}
footprint += increment;
included_body += 1;
}
if included_body == 0 {
if header_count < rows.len() {
let row_available = remaining - header_height - border_thickness;
if row_available > 0.0
&& let Some((head_row, tail_row)) =
try_split_first_body_row(&rows[header_count], row_available, cell_padding)
{
let mut head_rows: Vec<TableRow> = rows[..header_count].to_vec();
head_rows.push(head_row);
let mut tail_rows: Vec<TableRow> = rows[..header_count].to_vec();
tail_rows.push(tail_row);
let body_after_first: Vec<TableRow> =
rows.into_iter().skip(header_count + 1).collect();
tail_rows.extend(body_after_first);
let head_block = make_table_block(
x,
column_widths.clone(),
head_rows,
cell_padding,
header_bg,
border_color,
border_thickness,
border_style,
edge,
0.0,
anchor_id.clone(),
caption.clone(),
page_column,
column_span,
tag_role,
outline.clone(),
);
let tail_block = make_table_block(
x,
column_widths,
tail_rows,
cell_padding,
header_bg,
border_color,
border_thickness,
border_style,
edge,
space_after,
None,
None,
page_column,
column_span,
None,
None,
);
return SplitOutcome::Split(head_block, tail_block);
}
}
return SplitOutcome::NoFit(make_table_block(
x,
column_widths,
rows,
cell_padding,
header_bg,
border_color,
border_thickness,
border_style,
edge,
space_after,
anchor_id,
caption,
page_column,
column_span,
tag_role,
outline,
));
}
if header_count + included_body == rows.len() {
return SplitOutcome::Whole(make_table_block(
x,
column_widths,
rows,
cell_padding,
header_bg,
border_color,
border_thickness,
border_style,
edge,
space_after,
anchor_id,
caption,
page_column,
column_span,
tag_role,
outline,
));
}
let split_at = header_count + included_body;
let header_clone: Vec<TableRow> = rows[..header_count].to_vec();
let mut head_rows = rows;
let mut tail_rows = head_rows.split_off(split_at);
tail_rows.splice(0..0, header_clone);
let head_block = make_table_block(
x,
column_widths.clone(),
head_rows,
cell_padding,
header_bg,
border_color,
border_thickness,
border_style,
edge,
0.0,
anchor_id,
caption.clone(),
page_column,
column_span,
tag_role,
outline.clone(),
);
let tail_block = make_table_block(
x,
column_widths,
tail_rows,
cell_padding,
header_bg,
border_color,
border_thickness,
border_style,
edge,
space_after,
None,
None,
page_column,
column_span,
None,
None,
);
SplitOutcome::Split(head_block, tail_block)
}
fn try_split_first_body_row(
row: &TableRow,
available: f32,
cell_padding: f32,
) -> Option<(TableRow, TableRow)> {
let inner = available - 2.0 * cell_padding;
if inner <= 0.0 {
return None;
}
let mut head_cells: Vec<TableCell> = Vec::with_capacity(row.cells.len());
let mut tail_cells: Vec<TableCell> = Vec::with_capacity(row.cells.len());
let mut any_split = false;
let mut head_has_content = false;
for cell in &row.cells {
let (col, span, rowspan) = (cell.col, cell.colspan, cell.rowspan);
if cell.blocks.is_empty() {
head_cells.push(TableCell {
blocks: Vec::new(),
col,
colspan: span,
rowspan,
});
tail_cells.push(TableCell {
blocks: Vec::new(),
col,
colspan: span,
rowspan,
});
continue;
}
let pages = paginate_blocks(cell.blocks.clone(), inner);
let mut iter = pages.into_iter();
let head = iter.next().unwrap_or_default();
let rest: Vec<Block> = iter.flatten().collect();
if !head.is_empty() {
head_has_content = true;
}
if !rest.is_empty() {
any_split = true;
}
head_cells.push(TableCell {
blocks: head,
col,
colspan: span,
rowspan,
});
tail_cells.push(TableCell {
blocks: rest,
col,
colspan: span,
rowspan,
});
}
if !any_split || !head_has_content {
return None;
}
let head_height = cells_max_height(&head_cells) + 2.0 * cell_padding;
let tail_height = cells_max_height(&tail_cells) + 2.0 * cell_padding;
Some((
TableRow {
is_header: false,
fill: row.fill,
header_column: row.header_column,
height: head_height,
cells: head_cells,
},
TableRow {
is_header: false,
fill: row.fill,
header_column: row.header_column,
height: tail_height,
cells: tail_cells,
},
))
}
fn cells_max_height(cells: &[TableCell]) -> f32 {
cells
.iter()
.map(|c| sum_block_height_slice(&c.blocks))
.fold(0.0_f32, f32::max)
}
fn sum_block_height_slice(blocks: &[Block]) -> f32 {
let total: f32 = blocks.iter().map(|b| b.height + b.space_after).sum();
total - blocks.last().map(|b| b.space_after).unwrap_or(0.0)
}
#[allow(clippy::too_many_arguments)]
fn make_table_block(
x: f32,
column_widths: Vec<f32>,
rows: Vec<TableRow>,
cell_padding: f32,
header_bg: rgb::Color,
border_color: rgb::Color,
border_thickness: f32,
border_style: super::style::TableBorders,
edge: Option<(rgb::Color, f32)>,
space_after: f32,
anchor_id: Option<String>,
caption: Option<String>,
page_column: u8,
column_span: bool,
tag_role: Option<TagRole>,
outline: Option<OutlineEntry>,
) -> Block {
let height: f32 =
rows.iter().map(|r| r.height).sum::<f32>() + border_thickness * (rows.len() as f32 + 1.0);
Block {
height,
space_after,
draw: BlockDraw::Table {
x,
column_widths,
rows,
cell_padding,
header_bg,
border_color,
border_thickness,
border_style,
edge,
caption,
},
outline,
anchor_id,
tag_role,
page_column,
column_span,
}
}
#[derive(Clone)]
pub struct BoxedGroupIcon {
pub decoded: super::decoration::DecodedMedia,
pub x: f32,
pub size: f32,
}
#[derive(Clone, Copy)]
pub struct MarkerBadge {
pub fill: rgb::Color,
pub diameter: f32,
pub center_dx: f32,
pub center_dy: f32,
}
#[allow(clippy::large_enum_variant)]
#[derive(Clone)]
pub enum BlockDraw {
Text(TextSlice),
Rule {
x: f32,
width: f32,
thickness: f32,
color: rgb::Color,
},
BoxedGroup {
x: f32,
width: f32,
background: Option<rgb::Color>,
border: Option<rgb::Color>,
accent_left: Option<rgb::Color>,
accent_width: f32,
padding: f32,
children: Vec<Block>,
icon: Option<BoxedGroupIcon>,
top_rule: Option<(rgb::Color, f32)>,
bottom_rule: Option<(rgb::Color, f32)>,
},
ListItem {
marker: Layout<rgb::Color>,
marker_text: String,
marker_x: f32,
body: Vec<Block>,
ordered: bool,
badge: Option<MarkerBadge>,
check: Option<rgb::Color>,
},
Image {
image: KrillaImage,
x: f32,
width: f32,
height: f32,
caption: Option<String>,
},
Svg {
tree: Arc<SvgTree>,
x: f32,
width: f32,
height: f32,
caption: Option<String>,
},
PageBreak,
Table {
x: f32,
column_widths: Vec<f32>,
rows: Vec<TableRow>,
cell_padding: f32,
header_bg: rgb::Color,
border_color: rgb::Color,
border_thickness: f32,
border_style: super::style::TableBorders,
edge: Option<(rgb::Color, f32)>,
caption: Option<String>,
},
Float { image: Box<Block>, wrap: Vec<Block> },
FloatRegion {
text: TextSlice,
floats: Vec<FloatItem>,
},
FormField {
label: Option<TextSlice>,
field_x: f32,
field_y: f32,
field_w: f32,
field_h: f32,
border: rgb::Color,
thickness: f32,
hint: Option<TextSlice>,
hint_y: f32,
},
}
#[derive(Clone)]
pub struct FloatItem {
pub image: Box<Block>,
pub y_offset: f32,
}
#[derive(Clone)]
pub struct TableCell {
pub blocks: Vec<Block>,
pub col: usize,
pub colspan: usize,
pub rowspan: usize,
}
#[derive(Clone)]
pub struct TableRow {
pub is_header: bool,
pub fill: Option<rgb::Color>,
pub header_column: bool,
pub height: f32,
pub cells: Vec<TableCell>,
}
#[derive(Clone)]
pub struct TextSlice {
pub layout: Arc<Layout<rgb::Color>>,
pub text: Arc<String>,
pub links: Arc<Vec<LinkRange>>,
pub mid_anchors: Arc<Vec<MidAnchor>>,
pub footnote_calls: Arc<Vec<super::inline::FootnoteCall>>,
pub line_heights: Arc<Vec<f32>>,
pub x: f32,
pub line_range: std::ops::Range<usize>,
pub skip_y: f32,
}
impl TextSlice {
pub fn whole(layout: Layout<rgb::Color>, text: String, links: Vec<LinkRange>, x: f32) -> Self {
Self::whole_with_anchors(layout, text, links, Vec::new(), x)
}
pub fn whole_with_anchors(
layout: Layout<rgb::Color>,
text: String,
links: Vec<LinkRange>,
mid_anchors: Vec<MidAnchor>,
x: f32,
) -> Self {
Self::whole_with_extras(layout, text, links, mid_anchors, Vec::new(), x)
}
pub fn whole_with_extras(
layout: Layout<rgb::Color>,
text: String,
links: Vec<LinkRange>,
mid_anchors: Vec<MidAnchor>,
footnote_calls: Vec<super::inline::FootnoteCall>,
x: f32,
) -> Self {
let line_heights: Vec<f32> = layout
.lines()
.map(|l| {
let m = l.metrics();
m.ascent + m.descent + m.leading
})
.collect();
let n = line_heights.len();
Self {
layout: Arc::new(layout),
text: Arc::new(text),
links: Arc::new(links),
mid_anchors: Arc::new(mid_anchors),
footnote_calls: Arc::new(footnote_calls),
line_heights: Arc::new(line_heights),
x,
line_range: 0..n,
skip_y: 0.0,
}
}
pub fn height(&self) -> f32 {
self.line_heights[self.line_range.clone()]
.iter()
.copied()
.sum()
}
pub fn footnote_numbers(&self) -> Vec<u32> {
if self.footnote_calls.is_empty() {
return Vec::new();
}
let mut out = Vec::new();
for call in self.footnote_calls.iter() {
if let Some(line) = find_line_for_byte(&self.layout, call.byte_offset)
&& line >= self.line_range.start
&& line < self.line_range.end
{
out.push(call.number);
}
}
out
}
}
fn find_line_for_byte(layout: &Layout<rgb::Color>, byte: usize) -> Option<usize> {
let mut last = None;
for (i, line) in layout.lines().enumerate() {
last = Some(i);
for run in line.runs() {
for cluster in run.visual_clusters() {
let r = cluster.text_range();
if byte >= r.start && byte < r.end {
return Some(i);
}
if byte == r.end {
return Some(i);
}
}
}
}
last
}
pub fn build_footnote_pool_blocks(
entries: &[(u32, String)],
style: &Style,
body_families: &'static [&'static str],
font_cx: &mut FontContext,
layout_cx: &mut LayoutContext<rgb::Color>,
inner_x: f32,
inner_w: f32,
) -> Vec<Block> {
if entries.is_empty() {
return Vec::new();
}
let mut out = Vec::new();
out.push(Block {
height: style.footnote.gap_above,
space_after: 0.0,
draw: BlockDraw::Rule {
x: inner_x,
width: 0.0,
thickness: 0.0,
color: style.footnote.rule_color.into(),
},
outline: None,
anchor_id: None,
tag_role: None,
page_column: 0,
column_span: false,
});
let rule_w = (inner_w * style.footnote.rule_width_frac).max(0.0);
out.push(Block {
height: style.footnote.rule_thickness.max(0.5),
space_after: style.footnote.gap_below_rule,
draw: BlockDraw::Rule {
x: inner_x,
width: rule_w,
thickness: style.footnote.rule_thickness,
color: style.footnote.rule_color.into(),
},
outline: None,
anchor_id: None,
tag_role: None,
page_column: 0,
column_span: false,
});
let entry_style = TextStyle {
font_size: style.footnote.font_size,
font_weight: 400.0,
line_height: style.footnote.line_height,
color: style.footnote.text_color.into(),
font_families: body_families,
italic: false,
};
let last = entries.len().saturating_sub(1);
for (i, (number, body)) in entries.iter().enumerate() {
let mark = super::inline::superscript_number(*number);
let text = format!("{mark} {}", body);
let layout = build_layout(&text, &[], &entry_style, inner_w, font_cx, layout_cx);
let slice = TextSlice::whole(layout, text, Vec::new(), inner_x);
let h = slice.height();
out.push(Block {
height: h,
space_after: if i == last {
0.0
} else {
style.footnote.entry_space_after
},
draw: BlockDraw::Text(slice),
outline: None,
anchor_id: None,
tag_role: Some(TagRole::Note),
page_column: 0,
column_span: false,
});
}
out
}
pub fn pool_height(pool: &[Block]) -> f32 {
if pool.is_empty() {
return 0.0;
}
let mut h = 0.0;
for (i, b) in pool.iter().enumerate() {
h += b.height;
if i + 1 < pool.len() {
h += b.space_after;
}
}
h
}
pub struct LayoutCtx<'a> {
pub style: &'a Style,
pub font_cx: &'a mut FontContext,
pub layout_cx: &'a mut LayoutContext<rgb::Color>,
pub assets: &'a dyn AssetResolver,
pub next_heading: usize,
pub next_figure: usize,
pub next_table: usize,
pub pending_table_caption: Option<String>,
pub pending_figure_caption: Option<String>,
pub footnotes: Vec<String>,
pub body_families: &'a [&'static str],
pub fonts: &'a super::fonts::Fonts,
pub hyphenator: Option<&'a super::hyphen::WordHyphenator>,
pub heading_counters: [u32; 6],
pub list_depth: usize,
pub cell_content_align: Option<parley::layout::Alignment>,
pub crossref_labels: &'a HashMap<String, String>,
}
impl<'a> LayoutCtx<'a> {
pub fn next_heading_id(&mut self) -> usize {
let n = self.next_heading;
self.next_heading += 1;
n
}
pub fn next_figure_id(&mut self) -> usize {
let n = self.next_figure;
self.next_figure += 1;
n
}
pub fn next_table_id(&mut self) -> usize {
let n = self.next_table;
self.next_table += 1;
n
}
fn bump_heading_number(&mut self, level: u8) -> Option<String> {
let cfg = &self.style.heading_numbering;
if !cfg.enabled {
return None;
}
bump_heading_counters(&mut self.heading_counters, level, cfg.max_depth)
}
pub fn page_columns(&self) -> u8 {
self.style.page_layout.columns.max(1)
}
pub fn body_full_width(&self) -> f32 {
self.style.page_width - 2.0 * self.style.margin_x
}
pub fn body_column_width(&self) -> f32 {
let cols = self.page_columns();
let full = self.body_full_width();
if cols <= 1 {
return full;
}
let gap = self.style.page_layout.gap;
(full - gap * (cols as f32 - 1.0)) / cols as f32
}
pub fn heading_spans_columns(&self, level: u8) -> bool {
self.page_columns() > 1 && level <= self.style.page_layout.span_heading_through
}
}
pub(crate) fn bump_heading_counters(
counters: &mut [u32; 6],
level: u8,
max_depth: u8,
) -> Option<String> {
if level == 0 || level > max_depth.clamp(1, 6) {
return None;
}
let idx = (level - 1) as usize;
counters[idx] += 1;
for deeper in &mut counters[idx + 1..] {
*deeper = 0;
}
let prefix = counters[..=idx]
.iter()
.map(u32::to_string)
.collect::<Vec<_>>()
.join(".");
Some(prefix)
}
pub fn null_resolver() -> &'static dyn AssetResolver {
static NULL: NullAssetResolver = NullAssetResolver;
&NULL
}
pub fn layout_document(root: &RenderableTreeNode, ctx: &mut LayoutCtx<'_>) -> Vec<Block> {
let column_x = ctx.style.margin_x;
let column_w = ctx.body_column_width();
layout_node(root, column_x, column_w, ctx)
}
fn layout_node(
node: &RenderableTreeNode,
x: f32,
width: f32,
ctx: &mut LayoutCtx<'_>,
) -> Vec<Block> {
let RenderableTreeNode::Tag(tag) = node else {
return Vec::new();
};
match tag.name.as_str() {
"article" | "div" => layout_children(&tag.children, x, width, ctx),
"p" => layout_paragraph(tag, x, width, ctx),
h @ ("h1" | "h2" | "h3" | "h4" | "h5" | "h6") => {
let level = h.as_bytes()[1] - b'0';
layout_heading(tag, level, x, width, ctx)
}
"ul" => layout_list(tag, ListKind::Unordered, None, x, width, ctx),
"ol" => layout_list(tag, ListKind::Ordered, None, x, width, ctx),
"list" => layout_list_tag(tag, x, width, ctx),
"blockquote" => layout_blockquote(tag, x, width, ctx),
"pre" => {
#[cfg(feature = "mermaid")]
if let Some(blocks) = layout_mermaid(tag, x, width, ctx) {
return blocks;
}
layout_code_block(tag, x, width, ctx)
}
"callout" => layout_callout(tag, x, width, ctx),
"table" => layout_table(tag, x, width, ctx),
"columns" => layout_columns(tag, x, width, ctx),
"grid" => layout_grid(tag, x, width, ctx),
"swatch" => layout_swatch(tag, x, width, ctx),
"lightindicators" => layout_lightindicators(x, width, ctx),
"noParaSpaceBox" => layout_no_para_space_box(tag, x, width, ctx),
"imagegrid" => layout_imagegrid(tag, x, width, ctx),
"gridimage" => layout_gridimage(tag, x, width, ctx),
"qr" => layout_qr(tag, x, width, ctx),
"float" => layout_float(tag, x, width, ctx),
"input" => layout_input(tag, x, width, ctx),
"img" | "media" => layout_media(tag, x, width, ctx),
"toc" => vec![toc_marker_block(x), page_break_block()],
"pagebreak" => vec![page_break_block()],
"hr" => vec![layout_rule(x, width, ctx.style)],
_ => layout_children(&tag.children, x, width, ctx),
}
}
fn layout_children(
children: &[RenderableTreeNode],
x: f32,
width: f32,
ctx: &mut LayoutCtx<'_>,
) -> Vec<Block> {
let mut out = Vec::new();
let mut i = 0;
while i < children.len() {
if is_inline_child(&children[i]) {
let end = children[i..]
.iter()
.position(|c| !is_inline_child(c))
.map_or(children.len(), |n| i + n);
let run = &children[i..end];
let prints = run.iter().any(|c| match c {
RenderableTreeNode::Tag(_) => true,
RenderableTreeNode::Scalar(value) => {
super::inline::scalar_text(value).is_some_and(|t| !t.trim().is_empty())
}
});
if prints {
let paragraph = Tag {
name: "p".to_string(),
attributes: IndexMap::new(),
children: run.to_vec(),
};
out.extend(layout_paragraph(¶graph, x, width, ctx));
}
i = end;
continue;
}
if let Some((cap_text, target, override_pos, override_color)) =
caption_before_target(children, i)
{
let caption_block = build_caption_block(
&caption_inlines_for_node(&children[i]),
x,
width,
override_color,
ctx,
);
match target {
CaptionTarget::Table => ctx.pending_table_caption = Some(cap_text.clone()),
CaptionTarget::Figure => ctx.pending_figure_caption = Some(cap_text.clone()),
}
let target_blocks = layout_node(&children[i + 1], x, width, ctx);
let pos = override_pos.unwrap_or(ctx.style.caption_position);
match pos {
super::style::CaptionPosition::Above => {
out.push(caption_block);
out.extend(target_blocks);
}
super::style::CaptionPosition::Below => {
out.extend(target_blocks);
out.push(caption_block);
}
}
i += 2;
continue;
}
if let Some(first) = small_media_tag(&children[i]) {
let mut run: Vec<&Tag> = vec![first];
let mut j = i + 1;
while j < children.len() {
if let Some(next) = small_media_tag(&children[j]) {
run.push(next);
j += 1;
} else {
break;
}
}
if run.len() >= 2 {
out.extend(layout_small_media_row(&run, x, width, ctx));
i = j;
continue;
}
}
out.extend(layout_node(&children[i], x, width, ctx));
i += 1;
}
out
}
#[derive(Copy, Clone)]
enum CaptionTarget {
Table,
Figure,
}
fn build_caption_block(
inlines: &Inlines,
x: f32,
width: f32,
color_override: Option<krilla::color::rgb::Color>,
ctx: &mut LayoutCtx<'_>,
) -> Block {
let color = color_override
.or_else(|| ctx.style.caption_color.map(|c| c.into()))
.unwrap_or_else(|| ctx.style.blockquote_text_color.into());
let style = TextStyle {
font_size: ctx.style.body_font_size * 0.92,
font_weight: 400.0,
line_height: ctx.style.body_line_height,
color,
font_families: ctx.body_families,
italic: true,
};
let layout = build_layout(
&inlines.text,
&inlines.style_ranges,
&style,
width,
ctx.font_cx,
ctx.layout_cx,
);
let slice = TextSlice::whole_with_anchors(
layout,
inlines.text.clone(),
inlines.links.clone(),
inlines.mid_anchors.clone(),
x,
);
let height = slice.height();
Block {
height,
space_after: ctx.style.paragraph_space_after * 0.5,
draw: BlockDraw::Text(slice),
outline: None,
anchor_id: None,
tag_role: None,
page_column: 0,
column_span: false,
}
}
fn caption_block_from_str(text: &str, x: f32, width: f32, ctx: &mut LayoutCtx<'_>) -> Block {
let color = ctx
.style
.caption_color
.map(|c| c.into())
.unwrap_or_else(|| ctx.style.blockquote_text_color.into());
let style = TextStyle {
font_size: ctx.style.body_font_size * 0.92,
font_weight: 400.0,
line_height: ctx.style.body_line_height,
color,
font_families: ctx.body_families,
italic: true,
};
let layout = build_layout(text, &[], &style, width, ctx.font_cx, ctx.layout_cx);
let slice = TextSlice::whole(layout, text.to_string(), Vec::new(), x);
let height = slice.height();
Block {
height,
space_after: ctx.style.paragraph_space_after * 0.5,
draw: BlockDraw::Text(slice),
outline: None,
anchor_id: None,
tag_role: None,
page_column: 0,
column_span: false,
}
}
fn finish_media(
image: Block,
visible_caption: Option<String>,
x: f32,
width: f32,
ctx: &mut LayoutCtx<'_>,
) -> Vec<Block> {
let Some(text) = visible_caption else {
return vec![image];
};
let cap = caption_block_from_str(&text, x, width, ctx);
match ctx.style.caption_position {
super::style::CaptionPosition::Above => vec![cap, image],
super::style::CaptionPosition::Below => vec![image, cap],
}
}
fn caption_before_target(
children: &[RenderableTreeNode],
i: usize,
) -> Option<(
String,
CaptionTarget,
Option<super::style::CaptionPosition>,
Option<krilla::color::rgb::Color>,
)> {
let cap = find_caption_tag(children.get(i)?)?;
let next = children.get(i + 1)?;
let target = classify_caption_target(next)?;
let mut text = String::new();
let mut ranges = Vec::new();
collect_inlines(&mut text, &mut ranges, &cap.children, &mut Vec::new());
if text.trim().is_empty() {
return None;
}
let override_pos = caption_position_attr(cap);
let override_color = caption_color_attr(cap);
Some((text, target, override_pos, override_color))
}
fn caption_position_attr(cap: &Tag) -> Option<super::style::CaptionPosition> {
match cap.attributes.get("position") {
Some(Scalar::String(s)) => match s.as_str() {
"above" => Some(super::style::CaptionPosition::Above),
"below" => Some(super::style::CaptionPosition::Below),
_ => None,
},
_ => None,
}
}
fn caption_color_attr(cap: &Tag) -> Option<krilla::color::rgb::Color> {
match cap.attributes.get("color") {
Some(Scalar::String(s)) => super::inline::parse_css_color(s),
_ => None,
}
}
fn classify_caption_target(node: &RenderableTreeNode) -> Option<CaptionTarget> {
if contains_top_table(node) {
return Some(CaptionTarget::Table);
}
if contains_top_figure(node) {
return Some(CaptionTarget::Figure);
}
None
}
fn contains_top_figure(node: &RenderableTreeNode) -> bool {
if let RenderableTreeNode::Tag(t) = node {
if t.name == "img" || t.name == "media" {
return true;
}
if t.name == "p" || t.name == "div" {
return t.children.iter().any(contains_top_figure);
}
}
false
}
fn find_caption_tag(node: &RenderableTreeNode) -> Option<&Tag> {
if let RenderableTreeNode::Tag(t) = node {
if t.name == "caption" {
return Some(t.as_ref());
}
if t.name == "p" {
let mut found: Option<&Tag> = None;
for c in &t.children {
match c {
RenderableTreeNode::Tag(inner) => {
if inner.name == "caption" && found.is_none() {
found = Some(inner.as_ref());
} else if matches!(inner.name.as_str(), "hardbreak" | "br") {
continue;
} else {
return None;
}
}
RenderableTreeNode::Scalar(Scalar::String(s)) if s.trim().is_empty() => {}
RenderableTreeNode::Scalar(_) => return None,
}
}
return found;
}
}
None
}
fn contains_top_table(node: &RenderableTreeNode) -> bool {
if let RenderableTreeNode::Tag(t) = node {
if t.name == "table" {
return true;
}
if t.name == "p" || t.name == "div" {
return t.children.iter().any(contains_top_table);
}
}
false
}
fn caption_inlines_for_node(node: &RenderableTreeNode) -> Inlines {
if let Some(cap) = find_caption_tag(node) {
Inlines::from(&cap.children, &mut Vec::new())
} else {
Inlines::new()
}
}
fn layout_paragraph(tag: &Tag, x: f32, width: f32, ctx: &mut LayoutCtx<'_>) -> Vec<Block> {
let mut promoted: Vec<Block> = Vec::new();
let mut text_children: Vec<RenderableTreeNode> = Vec::new();
let mut media_run: Vec<&Tag> = Vec::new();
let flush_media_run = |run: &mut Vec<&Tag>, out: &mut Vec<Block>, ctx: &mut LayoutCtx<'_>| {
if run.is_empty() {
return;
}
if run.len() >= 2 && run.iter().all(|t| media_is_small(t)) {
out.extend(layout_small_media_row(run, x, width, ctx));
} else {
for t in run.iter() {
out.extend(layout_media(t, x, width, ctx));
}
}
run.clear();
};
for child in &tag.children {
if let RenderableTreeNode::Tag(t) = child {
match t.name.as_str() {
"img" | "media" => {
if media_is_small(t) {
media_run.push(t);
} else {
flush_media_run(&mut media_run, &mut promoted, ctx);
promoted.extend(layout_media(t, x, width, ctx));
}
continue;
}
"callout" => {
flush_media_run(&mut media_run, &mut promoted, ctx);
promoted.extend(layout_callout(t, x, width, ctx));
continue;
}
"input" => {
flush_media_run(&mut media_run, &mut promoted, ctx);
promoted.extend(layout_input(t, x, width, ctx));
continue;
}
_ => {}
}
}
flush_media_run(&mut media_run, &mut promoted, ctx);
text_children.push(child.clone());
}
flush_media_run(&mut media_run, &mut promoted, ctx);
let mut out = promoted;
let mut inlines = Inlines::from_with_labels(
&text_children,
&mut ctx.footnotes,
Some(ctx.crossref_labels),
);
if inlines.text.trim().is_empty() {
return out;
}
apply_link_style(&mut inlines, &ctx.style.link);
if let Some(h) = ctx.hyphenator
&& inlines.style_ranges.is_empty()
&& inlines.links.is_empty()
&& inlines.mid_anchors.is_empty()
&& inlines.footnote_calls.is_empty()
{
inlines.text = h.hyphenate(&inlines.text);
}
let style = TextStyle {
font_size: ctx.style.body_font_size,
font_weight: 400.0,
line_height: ctx.style.body_line_height,
color: ctx.style.text_color.into(),
font_families: ctx.body_families,
italic: false,
};
let layout = build_layout_aligned(
&inlines.text,
&inlines.style_ranges,
&style,
width,
ctx.cell_content_align
.unwrap_or_else(|| ctx.style.text_align.to_parley()),
ctx.font_cx,
ctx.layout_cx,
);
let slice = TextSlice::whole_with_extras(
layout,
inlines.text,
inlines.links,
inlines.mid_anchors,
inlines.footnote_calls,
x,
);
let height = slice.height();
out.push(Block {
height,
space_after: ctx.style.paragraph_space_after,
draw: BlockDraw::Text(slice),
outline: None,
anchor_id: None,
tag_role: None,
page_column: 0,
column_span: false,
});
out
}
#[derive(Debug, Clone)]
pub struct TocEntry {
pub level: u8,
pub text: String,
pub target_anchor_id: String,
pub page_number: usize,
}
fn format_toc_entry_with_leaders(
title: &str,
page_number: usize,
text_style: &TextStyle<'_>,
available_width: f32,
font_cx: &mut FontContext,
layout_cx: &mut LayoutContext<rgb::Color>,
) -> String {
let page_str = page_number.to_string();
let title_w = measure_first_line_width(title, text_style, font_cx, layout_cx);
let page_w = measure_first_line_width(&page_str, text_style, font_cx, layout_cx);
let gap = measure_first_line_width(" ", text_style, font_cx, layout_cx) * 2.0;
let dot_w = measure_first_line_width(".", text_style, font_cx, layout_cx);
if dot_w <= 0.0 || title_w + page_w + gap >= available_width {
return format!("{title} {page_str}");
}
let dot_room = available_width - title_w - page_w - gap;
let n_dots = (dot_room / dot_w).floor() as usize;
if n_dots == 0 {
return format!("{title} {page_str}");
}
let dots: String = std::iter::repeat_n('.', n_dots).collect();
format!("{title} {dots} {page_str}")
}
pub fn build_toc_blocks(
entries: &[TocEntry],
style: &Style,
body_families: &'static [&'static str],
font_cx: &mut FontContext,
layout_cx: &mut LayoutContext<rgb::Color>,
) -> Vec<Block> {
let mut blocks = Vec::new();
let column_x = style.margin_x;
let column_w = style.page_width - 2.0 * style.margin_x;
if !style.toc.title.is_empty() {
let title_style = TextStyle {
font_size: style.toc.title_font_size,
font_weight: 700.0,
line_height: style.body_line_height,
color: style.text_color.into(),
font_families: body_families,
italic: false,
};
let title_layout = build_layout(
&style.toc.title,
&[],
&title_style,
column_w,
font_cx,
layout_cx,
);
let title_slice =
TextSlice::whole(title_layout, style.toc.title.clone(), Vec::new(), column_x);
blocks.push(Block {
height: title_slice.height(),
space_after: 18.0,
draw: BlockDraw::Text(title_slice),
outline: Some(OutlineEntry {
level: 1,
text: style.toc.title.clone(),
}),
anchor_id: None,
tag_role: None,
page_column: 0,
column_span: style.page_layout.columns > 1,
});
}
let entry_text_style = TextStyle {
font_size: style.toc.entry_font_size,
font_weight: 400.0,
line_height: style.body_line_height,
color: style.text_color.into(),
font_families: body_families,
italic: false,
};
for entry in entries {
if entry.level > style.toc.max_depth {
continue;
}
let indent = (entry.level.saturating_sub(1) as f32) * style.toc.entry_indent_per_level;
let entry_x = column_x + indent;
let entry_w = column_w - indent;
let body = format_toc_entry_with_leaders(
&entry.text,
entry.page_number,
&entry_text_style,
entry_w,
font_cx,
layout_cx,
);
let layout = build_layout(&body, &[], &entry_text_style, entry_w, font_cx, layout_cx);
let links = vec![LinkRange {
start: 0,
end: body.len(),
href: format!("#{}", entry.target_anchor_id),
title: None,
}];
let slice = TextSlice::whole(layout, body, links, entry_x);
blocks.push(Block {
height: slice.height(),
space_after: style.toc.entry_space_after,
draw: BlockDraw::Text(slice),
outline: None,
anchor_id: None,
tag_role: None,
page_column: 0,
column_span: style.page_layout.columns > 1,
});
}
blocks
}
#[allow(clippy::too_many_arguments)]
pub fn build_list_section_blocks(
title: &str,
title_font_size: f32,
entry_font_size: f32,
entry_space_after: f32,
entries: &[TocEntry],
style: &Style,
body_families: &'static [&'static str],
font_cx: &mut FontContext,
layout_cx: &mut LayoutContext<rgb::Color>,
) -> Vec<Block> {
let mut blocks = Vec::new();
let column_x = style.margin_x;
let column_w = style.page_width - 2.0 * style.margin_x;
if !title.is_empty() {
let title_style = TextStyle {
font_size: title_font_size,
font_weight: 700.0,
line_height: style.body_line_height,
color: style.text_color.into(),
font_families: body_families,
italic: false,
};
let title_layout = build_layout(title, &[], &title_style, column_w, font_cx, layout_cx);
let title_slice = TextSlice::whole(title_layout, title.to_string(), Vec::new(), column_x);
blocks.push(Block {
height: title_slice.height(),
space_after: 18.0,
draw: BlockDraw::Text(title_slice),
outline: Some(OutlineEntry {
level: 1,
text: title.to_string(),
}),
anchor_id: None,
tag_role: None,
page_column: 0,
column_span: style.page_layout.columns > 1,
});
}
let entry_text_style = TextStyle {
font_size: entry_font_size,
font_weight: 400.0,
line_height: style.body_line_height,
color: style.text_color.into(),
font_families: body_families,
italic: false,
};
for entry in entries {
let body = format_toc_entry_with_leaders(
&entry.text,
entry.page_number,
&entry_text_style,
column_w,
font_cx,
layout_cx,
);
let layout = build_layout(&body, &[], &entry_text_style, column_w, font_cx, layout_cx);
let links = vec![LinkRange {
start: 0,
end: body.len(),
href: format!("#{}", entry.target_anchor_id),
title: None,
}];
let slice = TextSlice::whole(layout, body, links, column_x);
blocks.push(Block {
height: slice.height(),
space_after: entry_space_after,
draw: BlockDraw::Text(slice),
outline: None,
anchor_id: None,
tag_role: None,
page_column: 0,
column_span: style.page_layout.columns > 1,
});
}
blocks
}
fn attr_is_false(t: &Tag, key: &str) -> bool {
match t.attributes.get(key) {
Some(Scalar::Boolean(b)) => !b,
Some(Scalar::String(s)) => {
let s = s.trim();
s.eq_ignore_ascii_case("false") || s == "0"
}
_ => false,
}
}
fn layout_heading(tag: &Tag, level: u8, x: f32, width: f32, ctx: &mut LayoutCtx<'_>) -> Vec<Block> {
let h = ctx.style.heading.for_level(level).clone();
let column_span = ctx.heading_spans_columns(level);
let lay_w = if column_span {
ctx.body_full_width()
} else {
width
};
let mut heading_anchor: Option<String> = None;
let mut opt_out_numbering = false;
for child in &tag.children {
if let RenderableTreeNode::Tag(t) = child
&& t.name == "tag"
{
if heading_anchor.is_none()
&& let Some(id) = super::inline::anchor_id_attr(t)
{
heading_anchor = Some(id);
}
if attr_is_false(t, "numbered") {
opt_out_numbering = true;
}
}
}
let mut text = String::new();
let mut ranges = Vec::new();
collect_inlines(&mut text, &mut ranges, &tag.children, &mut ctx.footnotes);
if text.trim().is_empty() {
return Vec::new();
}
if !opt_out_numbering && let Some(number) = ctx.bump_heading_number(level) {
let prefix = format!("{number}{}", ctx.style.heading_numbering.separator);
let shift = prefix.len();
for r in &mut ranges {
r.start += shift;
r.end += shift;
}
text.insert_str(0, &prefix);
}
let mut blocks = Vec::new();
if h.space_before > 0.0 {
blocks.push(spacer_block(h.space_before));
}
let style = TextStyle {
font_size: h.font_size,
font_weight: h.font_weight,
line_height: ctx.style.body_line_height,
color: h.color.unwrap_or(ctx.style.text_color).into(),
font_families: ctx.fonts.heading(level),
italic: false,
};
let layout = build_layout(&text, &ranges, &style, lay_w, ctx.font_cx, ctx.layout_cx);
let outline_text = text.clone();
let slice = TextSlice::whole(layout, text, Vec::new(), x);
let height = slice.height();
let heading_id = ctx.next_heading_id();
let resolved_anchor = heading_anchor.unwrap_or_else(|| format!("__heading_{heading_id}"));
blocks.push(Block {
height,
space_after: h.space_after,
draw: BlockDraw::Text(slice),
outline: Some(OutlineEntry {
level,
text: outline_text,
}),
anchor_id: Some(resolved_anchor),
tag_role: None,
page_column: 0,
column_span,
});
blocks
}
#[derive(Copy, Clone)]
enum ListKind {
Unordered,
Ordered,
}
fn layout_list_tag(tag: &Tag, x: f32, width: f32, ctx: &mut LayoutCtx<'_>) -> Vec<Block> {
let marker = list_marker_glyph(tag);
let inner = tag.children.iter().find_map(|c| match c {
RenderableTreeNode::Tag(t) if t.name == "ul" => Some((t, ListKind::Unordered)),
RenderableTreeNode::Tag(t) if t.name == "ol" => Some((t, ListKind::Ordered)),
_ => None,
});
match inner {
Some((ul, kind)) => layout_list(ul, kind, marker, x, width, ctx),
None => layout_children(&tag.children, x, width, ctx),
}
}
fn list_marker_glyph(tag: &Tag) -> Option<&'static str> {
match tag.attributes.get("type") {
Some(Scalar::String(s)) => match s.as_str() {
"checkmark" => Some("✓"),
"dash" => Some("–"),
"none" => Some(""),
_ => None,
},
_ => None,
}
}
fn layout_list(
tag: &Tag,
kind: ListKind,
marker: Option<&str>,
x: f32,
width: f32,
ctx: &mut LayoutCtx<'_>,
) -> Vec<Block> {
let indent = ctx.style.list_indent;
let marker_x = x;
let items: Vec<&RenderableTreeNode> = tag
.children
.iter()
.filter(|c| matches!(c, RenderableTreeNode::Tag(t) if t.name == "li"))
.collect();
let mut out = Vec::new();
for (idx, item_node) in items.iter().enumerate() {
let RenderableTreeNode::Tag(item_tag) = item_node else {
continue;
};
let ordered = matches!(kind, ListKind::Ordered);
let badge_on = marker.is_none() && ordered && ctx.style.list_marker.badge;
let marker_text = match (marker, kind) {
(Some(m), _) => m.to_string(),
(None, ListKind::Unordered) => "•".to_string(),
(None, ListKind::Ordered) => {
let seq = ctx.style.list_marker.sequence_for_depth(ctx.list_depth);
let label = format_ordered_marker(idx + 1, seq);
if badge_on { label } else { format!("{label}.") }
}
};
let marker_color = if badge_on {
ctx.style
.list_marker
.badge_text_color
.unwrap_or(ctx.style.text_color)
.into()
} else {
ctx.style.text_color.into()
};
let check = (marker == Some("✓")).then_some(marker_color);
let marker_style = TextStyle {
font_size: ctx.style.body_font_size,
font_weight: 400.0,
line_height: ctx.style.body_line_height,
color: marker_color,
font_families: ctx.fonts.list_marker,
italic: false,
};
let marker_layout = build_layout(
&marker_text,
&[],
&marker_style,
indent,
ctx.font_cx,
ctx.layout_cx,
);
let badge = if badge_on {
let fs = ctx.style.body_font_size;
let baseline = marker_layout
.lines()
.next()
.map(|l| l.metrics().baseline)
.unwrap_or(fs);
let diameter = fs * ctx.style.list_marker.badge_scale;
Some(MarkerBadge {
fill: ctx.style.list_marker.badge_fill.into(),
diameter,
center_dx: diameter * 0.5,
center_dy: baseline - 0.32 * fs,
})
} else {
None
};
let marker_w = marker_layout
.lines()
.next()
.map(|l| l.metrics().advance)
.unwrap_or(0.0);
let text_indent = if badge_on {
indent
} else {
marker_w + ctx.style.list_marker_gap
};
let content_x = x + text_indent;
let content_w = (width - text_indent).max(0.0);
ctx.list_depth += 1;
let body = layout_li_body(item_tag, content_x, content_w, ctx);
ctx.list_depth -= 1;
let body_height: f32 = body
.iter()
.map(|b| b.height + b.space_after)
.sum::<f32>()
.max(0.0);
let body_height = body_height - body.last().map(|b| b.space_after).unwrap_or(0.0);
out.push(Block {
height: body_height.max(marker_layout.height()),
space_after: ctx.style.list_item_space_after,
draw: BlockDraw::ListItem {
marker: marker_layout,
marker_text,
marker_x,
body,
ordered,
badge,
check,
},
outline: None,
anchor_id: None,
tag_role: None,
page_column: 0,
column_span: false,
});
}
out
}
fn format_ordered_marker(n: usize, seq: MarkerSequence) -> String {
match seq {
MarkerSequence::Decimal => n.to_string(),
MarkerSequence::LowerAlpha => to_alpha(n, false),
MarkerSequence::UpperAlpha => to_alpha(n, true),
MarkerSequence::LowerRoman => to_roman(n, false),
MarkerSequence::UpperRoman => to_roman(n, true),
}
}
fn to_alpha(mut n: usize, upper: bool) -> String {
if n == 0 {
return "0".to_string();
}
let base = if upper { b'A' } else { b'a' };
let mut buf = Vec::new();
while n > 0 {
let rem = (n - 1) % 26;
buf.push(base + rem as u8);
n = (n - 1) / 26;
}
buf.reverse();
String::from_utf8(buf).unwrap()
}
fn to_roman(mut n: usize, upper: bool) -> String {
if n == 0 || n > 3999 {
return n.to_string();
}
const VALS: [(usize, &str); 13] = [
(1000, "m"),
(900, "cm"),
(500, "d"),
(400, "cd"),
(100, "c"),
(90, "xc"),
(50, "l"),
(40, "xl"),
(10, "x"),
(9, "ix"),
(5, "v"),
(4, "iv"),
(1, "i"),
];
let mut s = String::new();
for (v, sym) in VALS {
while n >= v {
s.push_str(sym);
n -= v;
}
}
if upper { s.to_uppercase() } else { s }
}
fn layout_li_body(item: &Tag, x: f32, width: f32, ctx: &mut LayoutCtx<'_>) -> Vec<Block> {
let any_block_child = item.children.iter().any(|c| {
if let RenderableTreeNode::Tag(t) = c {
matches!(
t.name.as_str(),
"p" | "ul"
| "ol"
| "blockquote"
| "pre"
| "callout"
| "h1"
| "h2"
| "h3"
| "h4"
| "h5"
| "h6"
| "hr"
)
} else {
false
}
});
if !any_block_child {
return layout_paragraph(item, x, width, ctx);
}
let wrapped = wrap_inline_runs_in_paragraphs(&item.children);
layout_children(&wrapped, x, width, ctx)
}
fn layout_blockquote(tag: &Tag, x: f32, width: f32, ctx: &mut LayoutCtx<'_>) -> Vec<Block> {
let indent = ctx.style.blockquote_indent;
let inner_x = x + indent;
let inner_w = width - indent;
let children = layout_children(&tag.children, inner_x, inner_w, ctx);
let inner_height: f32 = children
.iter()
.map(|b| b.height + b.space_after)
.sum::<f32>()
- children.last().map(|b| b.space_after).unwrap_or(0.0);
vec![Block {
height: inner_height,
space_after: ctx.style.paragraph_space_after,
draw: BlockDraw::BoxedGroup {
x,
width,
background: None,
border: None,
accent_left: Some(ctx.style.blockquote_bar_color.into()),
accent_width: ctx.style.blockquote_bar_width,
padding: 0.0, children,
icon: None,
top_rule: None,
bottom_rule: None,
},
outline: None,
anchor_id: None,
tag_role: None,
page_column: 0,
column_span: false,
}]
}
fn layout_code_block(tag: &Tag, x: f32, width: f32, ctx: &mut LayoutCtx<'_>) -> Vec<Block> {
let source = extract_code_text(&tag.children);
if source.trim().is_empty() {
return Vec::new();
}
let padding = ctx.style.code_padding;
let inner_x = x + padding;
let inner_w = width - 2.0 * padding;
let style = TextStyle {
font_size: ctx.style.code_font_size,
font_weight: 400.0,
line_height: ctx.style.body_line_height,
color: ctx.style.code_text_color.into(),
font_families: ctx.fonts.code,
italic: false,
};
let lang = match tag.attributes.get("data-language") {
Some(Scalar::String(s)) => s.as_str(),
_ => "",
};
let palette = &ctx.style.code_highlight;
let ranges: Vec<super::inline::InlineRange> = if lang.is_empty() {
Vec::new()
} else {
super::highlight::tokenize(lang, &source)
.into_iter()
.map(|t| {
let color: krilla::color::rgb::Color = match t.class {
super::highlight::TokenClass::Keyword => palette.keyword.into(),
super::highlight::TokenClass::String => palette.string.into(),
super::highlight::TokenClass::Comment => palette.comment.into(),
super::highlight::TokenClass::Number => palette.number.into(),
};
super::inline::InlineRange {
start: t.start,
end: t.end,
prop: super::inline::InlineProp::Color(color),
}
})
.collect()
};
let layout = build_layout(
&source,
&ranges,
&style,
inner_w,
ctx.font_cx,
ctx.layout_cx,
);
let inner_height = layout.height();
let slice = TextSlice::whole(layout, source, Vec::new(), inner_x);
let inner_block = Block {
height: inner_height,
space_after: 0.0,
draw: BlockDraw::Text(slice),
outline: None,
anchor_id: None,
tag_role: None,
page_column: 0,
column_span: false,
};
vec![Block {
height: inner_height + 2.0 * padding,
space_after: ctx.style.paragraph_space_after,
draw: BlockDraw::BoxedGroup {
x,
width,
background: Some(ctx.style.code_background.into()),
border: None,
accent_left: None,
accent_width: 0.0,
padding,
children: vec![inner_block],
icon: None,
top_rule: None,
bottom_rule: None,
},
outline: None,
anchor_id: None,
tag_role: None,
page_column: 0,
column_span: false,
}]
}
fn extract_code_text(children: &[RenderableTreeNode]) -> String {
let mut s = String::new();
for child in children {
match child {
RenderableTreeNode::Scalar(value) => {
if let Some(text) = super::inline::scalar_text(value) {
s.push_str(&text);
}
}
RenderableTreeNode::Tag(t) => s.push_str(&extract_code_text(&t.children)),
}
}
s
}
fn is_inline_child(node: &RenderableTreeNode) -> bool {
match node {
RenderableTreeNode::Scalar(_) => true,
RenderableTreeNode::Tag(t) => matches!(
t.name.as_str(),
"strong"
| "em"
| "s"
| "strikethrough"
| "code"
| "br"
| "hardbreak"
| "a"
| "footnote"
| "tagref"
| "tag"
| "chip"
| "color"
| "c"
| "ref"
),
}
}
fn is_block_tag(name: &str) -> bool {
matches!(
name,
"p" | "h1"
| "h2"
| "h3"
| "h4"
| "h5"
| "h6"
| "ul"
| "ol"
| "blockquote"
| "pre"
| "callout"
| "table"
| "hr"
| "div"
| "img"
| "media"
)
}
fn wrap_inline_runs_in_paragraphs(children: &[RenderableTreeNode]) -> Vec<RenderableTreeNode> {
let mut out: Vec<RenderableTreeNode> = Vec::new();
let mut run: Vec<RenderableTreeNode> = Vec::new();
let flush = |run: &mut Vec<RenderableTreeNode>, out: &mut Vec<RenderableTreeNode>| {
if run.is_empty() {
return;
}
let p = Tag {
name: "p".to_string(),
attributes: IndexMap::new(),
children: std::mem::take(run),
};
out.push(RenderableTreeNode::Tag(Box::new(p)));
};
for child in children {
match child {
RenderableTreeNode::Tag(t) if is_block_tag(&t.name) => {
flush(&mut run, &mut out);
out.push(child.clone());
}
_ => run.push(child.clone()),
}
}
flush(&mut run, &mut out);
out
}
fn layout_callout(tag: &Tag, x: f32, width: f32, ctx: &mut LayoutCtx<'_>) -> Vec<Block> {
let kind = match tag.attributes.get("type") {
Some(Scalar::String(s)) => s.as_str(),
_ => "note",
};
let cs = ctx.style.callout_styles.for_kind(kind).clone();
let padding = ctx.style.callout_padding;
let accent_w = match cs.decoration {
super::style::CalloutDecoration::Box => ctx.style.callout_accent_width,
super::style::CalloutDecoration::Rules => 0.0,
};
let inner_x = x + accent_w + padding;
let inner_w = width - accent_w - 2.0 * padding;
let icon = if cs.icon.trim().is_empty() {
None
} else {
super::decoration::decode_media(cs.icon.trim(), ctx.assets).map(|decoded| BoxedGroupIcon {
decoded,
x: inner_x,
size: ctx.style.callout_icon_size,
})
};
let is_rules = matches!(cs.decoration, super::style::CalloutDecoration::Rules);
let icon_size = icon.as_ref().map(|i| i.size).unwrap_or(0.0);
let icon_gutter = if is_rules {
0.0
} else {
icon.as_ref()
.map(|i| i.size + ctx.style.callout_icon_gap)
.unwrap_or(0.0)
};
let content_x = inner_x + icon_gutter;
let content_w = inner_w - icon_gutter;
let mut children: Vec<Block> = Vec::new();
if !cs.label.trim().is_empty() {
let mut label_block = build_callout_label_block(
cs.label.trim(),
content_x,
content_w,
cs.label_color.unwrap_or(ctx.style.text_color).into(),
cs.label_centered,
ctx.fonts.callout(kind),
ctx,
);
if is_rules && icon_size > label_block.height {
label_block.height = icon_size;
}
children.push(label_block);
} else if is_rules && icon_size > 0.0 {
children.push(spacer_block(icon_size));
}
let wrapped = wrap_inline_runs_in_paragraphs(&tag.children);
children.extend(layout_children(&wrapped, content_x, content_w, ctx));
let content_height: f32 = children
.iter()
.map(|b| b.height + b.space_after)
.sum::<f32>()
- children.last().map(|b| b.space_after).unwrap_or(0.0);
let inner_height = content_height.max(icon.as_ref().map(|i| i.size).unwrap_or(0.0));
let (background, border, accent_left, top_rule, bottom_rule) = match cs.decoration {
super::style::CalloutDecoration::Box => (
Some(cs.background.into()),
Some(cs.border.into()),
Some(cs.accent.into()),
None,
None,
),
super::style::CalloutDecoration::Rules => {
let rule = (cs.accent.into(), ctx.style.callout_rule_thickness);
(None, None, None, Some(rule), Some(rule))
}
};
vec![Block {
height: inner_height + 2.0 * padding,
space_after: ctx.style.callout_space_after,
draw: BlockDraw::BoxedGroup {
x,
width,
background,
border,
accent_left,
accent_width: accent_w,
padding,
children,
icon,
top_rule,
bottom_rule,
},
outline: None,
anchor_id: None,
tag_role: None,
page_column: 0,
column_span: false,
}]
}
fn build_callout_label_block(
label: &str,
x: f32,
width: f32,
color: rgb::Color,
centered: bool,
families: &'static [&'static str],
ctx: &mut LayoutCtx<'_>,
) -> Block {
let style = TextStyle {
font_size: ctx.style.callout_label_size,
font_weight: 700.0,
line_height: ctx.style.body_line_height,
color,
font_families: families,
italic: false,
};
let layout = if centered {
super::text::build_layout_aligned(
label,
&[],
&style,
width,
parley::layout::Alignment::Center,
ctx.font_cx,
ctx.layout_cx,
)
} else {
build_layout(label, &[], &style, width, ctx.font_cx, ctx.layout_cx)
};
let slice = TextSlice::whole(layout, label.to_string(), Vec::new(), x);
let height = slice.height();
Block {
height,
space_after: ctx.style.callout_label_size * 0.4,
draw: BlockDraw::Text(slice),
outline: None,
anchor_id: None,
tag_role: None,
page_column: 0,
column_span: false,
}
}
fn layout_rule(x: f32, width: f32, style: &Style) -> Block {
Block {
height: style.rule_thickness,
space_after: style.rule_space_around,
draw: BlockDraw::Rule {
x,
width,
thickness: style.rule_thickness,
color: style.rule_color.into(),
},
outline: None,
anchor_id: None,
tag_role: None,
page_column: 0,
column_span: false,
}
}
pub const TOC_MARKER_ANCHOR: &str = "\u{0}toc-marker";
fn toc_marker_block(x: f32) -> Block {
Block {
height: 0.0,
space_after: 0.0,
draw: BlockDraw::Rule {
x,
width: 0.0,
thickness: 0.0,
color: rgb::Color::new(0, 0, 0),
},
outline: None,
anchor_id: Some(TOC_MARKER_ANCHOR.to_string()),
tag_role: None,
page_column: 0,
column_span: false,
}
}
fn page_break_block() -> Block {
Block {
height: 0.0,
space_after: 0.0,
draw: BlockDraw::PageBreak,
outline: None,
anchor_id: None,
tag_role: None,
page_column: 0,
column_span: false,
}
}
fn spacer_block(height: f32) -> Block {
Block {
height,
space_after: 0.0,
draw: BlockDraw::Rule {
x: 0.0,
width: 0.0,
thickness: 0.0,
color: rgb::Color::new(0, 0, 0),
},
outline: None,
anchor_id: None,
tag_role: None,
page_column: 0,
column_span: false,
}
}
#[derive(Default)]
struct TableOverride {
borders: Option<super::style::TableBorders>,
border_color: Option<rgb::Color>,
edge_color: Option<rgb::Color>,
header_bg: Option<rgb::Color>,
cell_padding: Option<f32>,
column_weights: Option<Vec<f32>>,
stripe: Option<Option<rgb::Color>>,
header_column: Option<bool>,
}
impl TableOverride {
fn from_attrs(attrs: &IndexMap<String, Scalar>) -> Self {
use super::inline::parse_css_color;
use super::style::TableBorders;
let color = |key: &str| match attrs.get(key) {
Some(Scalar::String(s)) => parse_css_color(s),
_ => None,
};
let borders = match attrs.get("borders") {
Some(Scalar::String(s)) => match s.as_str() {
"grid" => Some(TableBorders::Grid),
"horizontal" => Some(TableBorders::Horizontal),
"none" => Some(TableBorders::None),
_ => None,
},
_ => None,
};
let cell_padding = match attrs.get("cell_padding") {
Some(Scalar::Number(n)) => Some(*n as f32),
Some(Scalar::String(s)) => s.trim().parse().ok(),
_ => None,
};
let column_weights = match attrs.get("column_weights") {
Some(Scalar::String(s)) => {
let ws: Vec<f32> = s
.split([',', ' '])
.filter(|t| !t.is_empty())
.filter_map(|t| t.parse().ok())
.collect();
(!ws.is_empty()).then_some(ws)
}
_ => None,
};
let stripe = match attrs.get("stripe") {
Some(Scalar::String(s)) if s.eq_ignore_ascii_case("none") => Some(None),
Some(Scalar::String(s)) => parse_css_color(s).map(Some),
_ => None,
};
let header_column = match attrs.get("header_column") {
Some(Scalar::Boolean(b)) => Some(*b),
Some(Scalar::String(s)) => Some(s.eq_ignore_ascii_case("true")),
_ => None,
};
TableOverride {
borders,
border_color: color("border_color"),
edge_color: color("edge_color"),
header_bg: color("header_background"),
cell_padding,
column_weights,
stripe,
header_column,
}
}
}
fn table_rows_source(tag: &Tag) -> &Tag {
let has_rows = tag.children.iter().any(|c| {
matches!(c, RenderableTreeNode::Tag(t)
if matches!(t.name.as_str(), "thead" | "tbody" | "tr"))
});
if has_rows {
return tag;
}
for c in &tag.children {
if let RenderableTreeNode::Tag(t) = c
&& t.name == "table"
{
return t;
}
}
tag
}
const PANEL_PAD: f32 = 10.0;
fn columns_from_children(tag: &Tag) -> Vec<Vec<RenderableTreeNode>> {
let list_children = tag.children.iter().find_map(|c| match c {
RenderableTreeNode::Tag(t) if matches!(t.name.as_str(), "ul" | "ol") => {
Some(t.children.as_slice())
}
_ => None,
});
match list_children {
Some(items) => items
.iter()
.filter_map(|li| match li {
RenderableTreeNode::Tag(t) if t.name == "li" => Some(t.children.clone()),
_ => None,
})
.collect(),
None => tag
.children
.iter()
.filter(|c| matches!(c, RenderableTreeNode::Tag(_)))
.map(|c| vec![c.clone()])
.collect(),
}
}
fn attr_points(attrs: &IndexMap<String, Scalar>, key: &str, default: f32) -> f32 {
attrs
.get(key)
.and_then(|s| match s {
Scalar::Number(v) => Some(*v as f32),
Scalar::String(s) => s.trim().parse::<f32>().ok(),
_ => None,
})
.unwrap_or(default)
.max(0.0)
}
fn parse_widths(attrs: &IndexMap<String, Scalar>) -> Option<Vec<f32>> {
match attrs.get("widths") {
Some(Scalar::Array(items)) => Some(
items
.iter()
.filter_map(|it| match it {
Scalar::Number(v) => Some(*v as f32),
_ => None,
})
.collect(),
),
Some(Scalar::String(s)) => Some(
s.split(|c: char| c.is_whitespace() || c == ',')
.filter(|t| !t.is_empty())
.filter_map(|t| t.parse::<f32>().ok())
.collect(),
),
_ => None,
}
}
fn parse_layout_align(attrs: &IndexMap<String, Scalar>) -> Option<parley::layout::Alignment> {
use parley::layout::Alignment;
match attrs.get("align") {
Some(Scalar::String(s)) => match s.trim().to_ascii_lowercase().as_str() {
"center" | "centre" => Some(Alignment::Center),
"right" | "end" => Some(Alignment::End),
"left" | "start" => Some(Alignment::Start),
_ => None,
},
_ => None,
}
}
fn parse_layout_background(attrs: &IndexMap<String, Scalar>) -> Option<rgb::Color> {
match attrs.get("background") {
Some(Scalar::String(s)) => super::inline::parse_css_color(s),
_ => None,
}
}
fn layout_cells_row(
cells: Vec<Vec<RenderableTreeNode>>,
x: f32,
width: f32,
gap: f32,
weights: Option<&[f32]>,
align: Option<parley::layout::Alignment>,
ctx: &mut LayoutCtx<'_>,
) -> Vec<Block> {
use parley::layout::Alignment;
let n = cells.len();
let tds: Vec<RenderableTreeNode> = cells
.into_iter()
.map(|blocks| {
RenderableTreeNode::Tag(Box::new(Tag {
name: "td".to_string(),
attributes: IndexMap::new(),
children: blocks,
}))
})
.collect();
let tr = RenderableTreeNode::Tag(Box::new(Tag {
name: "tr".to_string(),
attributes: IndexMap::new(),
children: tds,
}));
let weights_str = weights
.filter(|w| w.len() == n && w.iter().all(|v| *v > 0.0))
.map(|w| {
w.iter()
.map(|v| v.to_string())
.collect::<Vec<_>>()
.join(" ")
})
.unwrap_or_else(|| vec!["1"; n].join(" "));
let mut table_attrs = IndexMap::new();
table_attrs.insert("borders".to_string(), Scalar::String("none".to_string()));
table_attrs.insert("column_weights".to_string(), Scalar::String(weights_str));
table_attrs.insert(
"cell_padding".to_string(),
Scalar::Number((gap / 2.0) as f64),
);
if let Some(a) = align {
let s = match a {
Alignment::Center => "center",
Alignment::End => "right",
_ => "left",
};
table_attrs.insert(
"align".to_string(),
Scalar::Array(vec![Scalar::String(s.to_string()); n]),
);
}
let table = Tag {
name: "table".to_string(),
attributes: table_attrs,
children: vec![tr],
};
let prev = ctx.cell_content_align;
ctx.cell_content_align = align;
let blocks = layout_table(&table, x, width, ctx);
ctx.cell_content_align = prev;
blocks
}
fn wrap_in_panel(inner: Vec<Block>, x: f32, width: f32, bg: rgb::Color, space_after: f32) -> Block {
let content_height: f32 = inner.iter().map(|b| b.height + b.space_after).sum::<f32>()
- inner.last().map(|b| b.space_after).unwrap_or(0.0);
Block {
height: content_height + 2.0 * PANEL_PAD,
space_after,
draw: BlockDraw::BoxedGroup {
x,
width,
background: Some(bg),
border: None,
accent_left: None,
accent_width: 0.0,
padding: PANEL_PAD,
children: inner,
icon: None,
top_rule: None,
bottom_rule: None,
},
outline: None,
anchor_id: None,
tag_role: None,
page_column: 0,
column_span: false,
}
}
fn layout_columns(tag: &Tag, x: f32, width: f32, ctx: &mut LayoutCtx<'_>) -> Vec<Block> {
let columns = columns_from_children(tag);
if columns.is_empty() {
return layout_children(&tag.children, x, width, ctx);
}
let gap = attr_points(&tag.attributes, "gap", 16.0);
let align = parse_layout_align(&tag.attributes);
let bg = parse_layout_background(&tag.attributes);
let widths = parse_widths(&tag.attributes);
let (inner_x, inner_w) = match bg {
Some(_) => (x + PANEL_PAD, width - 2.0 * PANEL_PAD),
None => (x, width),
};
let rows = layout_cells_row(
columns,
inner_x,
inner_w,
gap,
widths.as_deref(),
align,
ctx,
);
match bg {
Some(c) => vec![wrap_in_panel(
rows,
x,
width,
c,
ctx.style.paragraph_space_after,
)],
None => rows,
}
}
fn atomic_group(inner: Vec<Block>, x: f32, width: f32, space_after: f32) -> Block {
let height: f32 = inner.iter().map(|b| b.height + b.space_after).sum::<f32>()
- inner.last().map(|b| b.space_after).unwrap_or(0.0);
Block {
height,
space_after,
draw: BlockDraw::BoxedGroup {
x,
width,
background: None,
border: None,
accent_left: None,
accent_width: 0.0,
padding: 0.0,
children: inner,
icon: None,
top_rule: None,
bottom_rule: None,
},
outline: None,
anchor_id: None,
tag_role: None,
page_column: 0,
column_span: false,
}
}
fn layout_grid(tag: &Tag, x: f32, width: f32, ctx: &mut LayoutCtx<'_>) -> Vec<Block> {
let cells = columns_from_children(tag);
if cells.is_empty() {
return layout_children(&tag.children, x, width, ctx);
}
let n = cells.len();
let gap = attr_points(&tag.attributes, "gap", 16.0);
let align = parse_layout_align(&tag.attributes);
let bg = parse_layout_background(&tag.attributes);
let min = attr_points(&tag.attributes, "min", 120.0).max(1.0);
let (inner_x, inner_w) = match bg {
Some(_) => (x + PANEL_PAD, width - 2.0 * PANEL_PAD),
None => (x, width),
};
let cols = (((inner_w + gap) / (min + gap)).floor() as usize).clamp(1, n);
let chunks: Vec<Vec<Vec<RenderableTreeNode>>> =
cells.chunks(cols).map(|c| c.to_vec()).collect();
let n_rows = chunks.len();
let mut out: Vec<Block> = Vec::new();
for (i, mut row) in chunks.into_iter().enumerate() {
while row.len() < cols {
row.push(Vec::new());
}
let br = layout_cells_row(row, inner_x, inner_w, gap, None, align, ctx);
let space_after = if i + 1 < n_rows { gap } else { 0.0 };
out.push(atomic_group(br, inner_x, inner_w, space_after));
}
match bg {
Some(c) => vec![wrap_in_panel(
out,
x,
width,
c,
ctx.style.paragraph_space_after,
)],
None => out,
}
}
struct SwatchStop {
color: String,
offset: f32,
opacity: f32,
}
enum SwatchFill {
Solid(String),
Gradient { angle: f32, stops: Vec<SwatchStop> },
}
fn css_hex(s: &str) -> Option<String> {
let s = s.trim();
if let Some(h) = s.strip_prefix('#') {
return match h.len() {
3 if h.bytes().all(|b| b.is_ascii_hexdigit()) => {
Some(h.chars().fold(String::from("#"), |mut a, c| {
a.push(c);
a.push(c);
a
}))
}
6 if h.bytes().all(|b| b.is_ascii_hexdigit()) => {
Some(format!("#{}", h.to_ascii_lowercase()))
}
_ => None,
};
}
Some(
match s.to_ascii_lowercase().as_str() {
"red" => "#cc0000",
"green" => "#008000",
"blue" => "#0000cc",
"orange" => "#ff7800",
"black" => "#000000",
"white" => "#ffffff",
_ => return None,
}
.to_string(),
)
}
fn angle_to_axis(deg: f32) -> (f32, f32, f32, f32) {
let r = deg.to_radians();
let (dx, dy) = (r.sin(), -r.cos());
(
(0.5 - 0.5 * dx).clamp(0.0, 1.0),
(0.5 - 0.5 * dy).clamp(0.0, 1.0),
(0.5 + 0.5 * dx).clamp(0.0, 1.0),
(0.5 + 0.5 * dy).clamp(0.0, 1.0),
)
}
fn parse_gradient(s: &str) -> Option<SwatchFill> {
let mut parts: Vec<&str> = s
.split(',')
.map(str::trim)
.filter(|t| !t.is_empty())
.collect();
if parts.is_empty() {
return None;
}
let mut angle = 90.0_f32;
let first = parts[0];
let is_stop = first.starts_with('#')
|| first.eq_ignore_ascii_case("transparent")
|| css_hex(first.split_whitespace().next().unwrap_or("")).is_some();
if !is_stop {
angle = first.trim_end_matches("deg").trim().parse::<f32>().ok()?;
parts.remove(0);
}
let n = parts.len();
if n < 2 {
return None;
}
let mut colors: Vec<Option<String>> = Vec::with_capacity(n);
let mut offsets: Vec<f32> = Vec::with_capacity(n);
for (i, p) in parts.iter().enumerate() {
let mut it = p.split_whitespace();
let tok = it.next().unwrap_or("");
let off = it
.next()
.and_then(|q| q.trim_end_matches('%').parse::<f32>().ok())
.map(|v| (v / 100.0).clamp(0.0, 1.0))
.unwrap_or(i as f32 / (n as f32 - 1.0));
offsets.push(off);
if tok.eq_ignore_ascii_case("transparent") {
colors.push(None);
} else {
colors.push(Some(css_hex(tok)?)); }
}
let stops = (0..n)
.map(|i| {
if let Some(c) = &colors[i] {
SwatchStop {
color: c.clone(),
offset: offsets[i],
opacity: 1.0,
}
} else {
let near = (0..n)
.filter_map(|j| {
colors[j]
.as_ref()
.map(|c| ((j as i32 - i as i32).unsigned_abs(), c.clone()))
})
.min_by_key(|(d, _)| *d)
.map(|(_, c)| c)
.unwrap_or_else(|| "#ffffff".to_string());
SwatchStop {
color: near,
offset: offsets[i],
opacity: 0.0,
}
}
})
.collect();
Some(SwatchFill::Gradient { angle, stops })
}
fn parse_swatch_fill(attrs: &IndexMap<String, Scalar>) -> Option<SwatchFill> {
if let Some(Scalar::String(g)) = attrs.get("gradient") {
return parse_gradient(g);
}
if let Some(Scalar::String(c)) = attrs.get("color") {
return css_hex(c).map(SwatchFill::Solid);
}
None
}
fn swatch_svg(w: f32, h: f32, radius: f32, fill: &SwatchFill) -> String {
let (defs, paint) = match fill {
SwatchFill::Solid(hex) => (String::new(), format!("fill=\"{hex}\"")),
SwatchFill::Gradient { angle, stops } => {
let (x1, y1, x2, y2) = angle_to_axis(*angle);
let els: String = stops
.iter()
.map(|s| {
format!(
"<stop offset=\"{:.4}\" stop-color=\"{}\" stop-opacity=\"{:.3}\"/>",
s.offset, s.color, s.opacity
)
})
.collect();
(
format!(
"<defs><linearGradient id=\"g\" x1=\"{x1:.4}\" y1=\"{y1:.4}\" x2=\"{x2:.4}\" y2=\"{y2:.4}\">{els}</linearGradient></defs>"
),
"fill=\"url(#g)\"".to_string(),
)
}
};
format!(
"<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"{w:.2}\" height=\"{h:.2}\" viewBox=\"0 0 {w:.2} {h:.2}\">{defs}<rect x=\"0\" y=\"0\" width=\"{w:.2}\" height=\"{h:.2}\" rx=\"{radius:.2}\" ry=\"{radius:.2}\" {paint}/></svg>"
)
}
fn layout_swatch(tag: &Tag, x: f32, width: f32, ctx: &mut LayoutCtx<'_>) -> Vec<Block> {
let height = attr_points(&tag.attributes, "height", 6.0).max(0.5);
let radius = attr_points(&tag.attributes, "radius", 2.0);
let fill = match parse_swatch_fill(&tag.attributes) {
Some(f) => f,
None => return placeholder(x, width, ctx, "[swatch: needs color or gradient]"),
};
let svg = swatch_svg(width, height, radius, &fill);
match usvg::Tree::from_data(svg.as_bytes(), &usvg::Options::default()) {
Ok(tree) => vec![Block {
height,
space_after: 6.0,
draw: BlockDraw::Svg {
tree: Arc::new(tree),
x,
width,
height,
caption: None,
},
outline: None,
anchor_id: None,
tag_role: None,
page_column: 0,
column_span: false,
}],
Err(e) => {
eprintln!("warning: swatch render failed — {e}");
placeholder(x, width, ctx, "[swatch: render failed]")
}
}
}
struct LightIndicatorSpec {
title: &'static str,
description: &'static str,
color: Option<&'static str>,
gradient: Option<&'static str>,
}
const LIGHT_INDICATORS: &[LightIndicatorSpec] = &[
LightIndicatorSpec {
title: "Red",
description: "Emergency stop or Protective stop",
color: Some("#ff0000"),
gradient: None,
},
LightIndicatorSpec {
title: "Green",
description: "Ready for job",
color: Some("#00fa00"),
gradient: None,
},
LightIndicatorSpec {
title: "Cyan",
description: "Drives to destination",
color: Some("#00fafa"),
gradient: None,
},
LightIndicatorSpec {
title: "Purple",
description: "Goal/Path blocked",
color: Some("#f900f9"),
gradient: None,
},
LightIndicatorSpec {
title: "Wavering white",
description: "Planning path",
color: None,
gradient: Some("90deg, white, black, white"),
},
LightIndicatorSpec {
title: "Orange",
description: "Mission paused",
color: Some("#fd7e14"),
gradient: None,
},
LightIndicatorSpec {
title: "Wavering orange",
description: "Startup signal before PC is active",
color: None,
gradient: Some("90deg, #fd7e14, white, #fd7e14"),
},
LightIndicatorSpec {
title: "Fading orange",
description: "Shutting down robot",
color: None,
gradient: Some("180deg, #fd7e14, #fff3e0"),
},
LightIndicatorSpec {
title: "Blinking orange",
description: "Relative move, ignoring obstacles",
color: None,
gradient: Some("90deg, transparent, #fd7e14 15%, #fd7e14 35%, transparent 50%"),
},
LightIndicatorSpec {
title: "Wavering purple and orange",
description: "General error",
color: None,
gradient: Some("90deg, #fd7e14, #a435f0, #fd7e14"),
},
LightIndicatorSpec {
title: "Blue",
description: "Manual drive",
color: Some("#0000ff"),
gradient: None,
},
LightIndicatorSpec {
title: "Wavering blue",
description: "Mapping",
color: None,
gradient: Some("90deg, #0000ff, white, #0000ff"),
},
LightIndicatorSpec {
title: "Battery percentage",
description: "Charging at charging station",
color: None,
gradient: Some("90deg, #ff0000, transparent 25%"),
},
LightIndicatorSpec {
title: "Wavering cyan",
description: "Waiting for a fleet resource or for another robot to move",
color: None,
gradient: Some("90deg, #00fafa, white, #00fafa"),
},
];
const LIGHT_INDICATOR_DESC_COLOR: &str = "#6c757d";
const LIGHT_INDICATOR_BAR_HEIGHT: f64 = 5.0;
const LIGHT_INDICATOR_BAR_RADIUS: f64 = 2.0;
const LIGHT_INDICATOR_MIN: f64 = 150.0;
const LIGHT_INDICATOR_GAP: f64 = 15.0;
fn light_rt_tag(
name: &str,
attrs: IndexMap<String, Scalar>,
children: Vec<RenderableTreeNode>,
) -> RenderableTreeNode {
RenderableTreeNode::Tag(Box::new(Tag {
name: name.to_string(),
attributes: attrs,
children,
}))
}
fn light_rt_text(s: &str) -> RenderableTreeNode {
RenderableTreeNode::Scalar(Scalar::String(s.to_string()))
}
fn light_indicator_cell(spec: &LightIndicatorSpec) -> Vec<RenderableTreeNode> {
let mut swatch_attrs = IndexMap::new();
if let Some(g) = spec.gradient {
swatch_attrs.insert("gradient".to_string(), Scalar::String(g.to_string()));
} else if let Some(c) = spec.color {
swatch_attrs.insert("color".to_string(), Scalar::String(c.to_string()));
}
swatch_attrs.insert(
"height".to_string(),
Scalar::Number(LIGHT_INDICATOR_BAR_HEIGHT),
);
swatch_attrs.insert(
"radius".to_string(),
Scalar::Number(LIGHT_INDICATOR_BAR_RADIUS),
);
let mut color_attrs = IndexMap::new();
color_attrs.insert(
"value".to_string(),
Scalar::String(LIGHT_INDICATOR_DESC_COLOR.to_string()),
);
let label = light_rt_tag(
"p",
IndexMap::new(),
vec![
light_rt_tag("strong", IndexMap::new(), vec![light_rt_text(spec.title)]),
light_rt_tag("hardbreak", IndexMap::new(), Vec::new()),
light_rt_tag("color", color_attrs, vec![light_rt_text(spec.description)]),
],
);
vec![light_rt_tag("swatch", swatch_attrs, Vec::new()), label]
}
fn layout_lightindicators(x: f32, width: f32, ctx: &mut LayoutCtx<'_>) -> Vec<Block> {
let items: Vec<RenderableTreeNode> = LIGHT_INDICATORS
.iter()
.map(|spec| light_rt_tag("li", IndexMap::new(), light_indicator_cell(spec)))
.collect();
let ul = light_rt_tag("ul", IndexMap::new(), items);
let mut grid_attrs = IndexMap::new();
grid_attrs.insert("min".to_string(), Scalar::Number(LIGHT_INDICATOR_MIN));
grid_attrs.insert("gap".to_string(), Scalar::Number(LIGHT_INDICATOR_GAP));
let grid = Tag {
name: "grid".to_string(),
attributes: grid_attrs,
children: vec![ul],
};
let mut blocks = layout_grid(&grid, x, width, ctx);
if let Some(last) = blocks.last_mut() {
last.space_after = LIGHT_INDICATOR_GAP as f32;
}
blocks
}
const NO_PARA_SPACE_BOX_MARGIN: f32 = 11.0;
fn layout_no_para_space_box(tag: &Tag, x: f32, width: f32, ctx: &mut LayoutCtx<'_>) -> Vec<Block> {
let children: Vec<&RenderableTreeNode> = tag
.children
.iter()
.filter(|c| match c {
RenderableTreeNode::Tag(t) => !node_text_is_blank(&t.children),
RenderableTreeNode::Scalar(Scalar::String(s)) => !s.trim().is_empty(),
RenderableTreeNode::Scalar(_) => true,
})
.collect();
let mut blocks = Vec::new();
for child in children {
blocks.extend(layout_node(child, x, width, ctx));
}
if blocks.is_empty() {
return blocks;
}
let last = blocks.len() - 1;
for (i, b) in blocks.iter_mut().enumerate() {
b.space_after = if i == last {
NO_PARA_SPACE_BOX_MARGIN
} else {
0.0
};
}
blocks
}
const IMAGE_GRID_BG: &str = "#f9f9f9";
const IMAGE_GRID_GAP: f32 = 10.0;
fn attr_string<'a>(attrs: &'a IndexMap<String, Scalar>, key: &str) -> Option<&'a str> {
match attrs.get(key) {
Some(Scalar::String(s)) if !s.trim().is_empty() => Some(s.as_str()),
_ => None,
}
}
fn gridimage_cell_nodes(tag: &Tag) -> Vec<RenderableTreeNode> {
let mut nodes = Vec::new();
let mut media_attrs = IndexMap::new();
if let Some(id) = attr_string(&tag.attributes, "id") {
media_attrs.insert("id".to_string(), Scalar::String(id.to_string()));
}
if let Some(alt) = attr_string(&tag.attributes, "alt") {
media_attrs.insert("alt".to_string(), Scalar::String(alt.to_string()));
}
media_attrs.insert("size".to_string(), Scalar::String("large".to_string()));
nodes.push(light_rt_tag("media", media_attrs, Vec::new()));
if let Some(headline) = attr_string(&tag.attributes, "headline") {
nodes.push(light_rt_tag(
"p",
IndexMap::new(),
vec![light_rt_tag(
"strong",
IndexMap::new(),
vec![light_rt_text(headline)],
)],
));
}
if let Some(body) = attr_string(&tag.attributes, "bodytext") {
nodes.push(light_rt_tag(
"p",
IndexMap::new(),
vec![light_rt_text(body)],
));
}
nodes
}
fn layout_imagegrid(tag: &Tag, x: f32, width: f32, ctx: &mut LayoutCtx<'_>) -> Vec<Block> {
let cells: Vec<Vec<RenderableTreeNode>> = tag
.children
.iter()
.filter_map(|c| match c {
RenderableTreeNode::Tag(t) if t.name == "gridimage" => Some(gridimage_cell_nodes(t)),
_ => None,
})
.collect();
if cells.is_empty() {
return layout_children(&tag.children, x, width, ctx);
}
let bg = super::inline::parse_css_color(IMAGE_GRID_BG)
.unwrap_or_else(|| rgb::Color::new(0xF9, 0xF9, 0xF9));
let inner_x = x + PANEL_PAD;
let inner_w = width - 2.0 * PANEL_PAD;
let row = layout_cells_row(cells, inner_x, inner_w, IMAGE_GRID_GAP, None, None, ctx);
vec![wrap_in_panel(
row,
x,
width,
bg,
ctx.style.paragraph_space_after,
)]
}
fn layout_gridimage(tag: &Tag, x: f32, width: f32, ctx: &mut LayoutCtx<'_>) -> Vec<Block> {
let nodes = gridimage_cell_nodes(tag);
layout_children(&nodes, x, width, ctx)
}
fn qr_svg(code: &qrcodegen::QrCode, quiet: i32, dark: &str, light: &str) -> String {
use std::fmt::Write as _;
let n = code.size();
let dim = n + 2 * quiet;
let mut path = String::new();
for y in 0..n {
for x in 0..n {
if code.get_module(x, y) {
let _ = write!(path, "M{} {}h1v1h-1z", x + quiet, y + quiet);
}
}
}
format!(
"<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"{dim}\" height=\"{dim}\" viewBox=\"0 0 {dim} {dim}\">\
<rect width=\"{dim}\" height=\"{dim}\" fill=\"{light}\"/>\
<path d=\"{path}\" fill=\"{dark}\"/></svg>"
)
}
#[cfg(feature = "mermaid")]
fn layout_mermaid(tag: &Tag, x: f32, width: f32, ctx: &mut LayoutCtx<'_>) -> Option<Vec<Block>> {
let is_mermaid = matches!(
tag.attributes.get("data-language"),
Some(Scalar::String(lang)) if lang.trim().eq_ignore_ascii_case("mermaid")
);
if !is_mermaid {
return None;
}
let source = extract_code_text(&tag.children);
if source.trim().is_empty() {
return None;
}
let tree = match super::mermaid::diagram_tree(&source, ctx.crossref_labels) {
Ok(tree) => tree,
Err(why) => {
eprintln!("warning: mermaid render failed -- {why}");
return None;
}
};
let size = tree.size();
if !(size.width() > 0.0 && size.height() > 0.0) {
return None;
}
let page_h = (ctx.style.page_height - 2.0 * ctx.style.margin_y).max(1.0);
let mut draw_w = width.min(size.width());
let mut draw_h = draw_w * size.height() / size.width();
if draw_h > page_h {
let shrink = page_h / draw_h;
draw_w *= shrink;
draw_h = page_h;
if shrink < 0.6 {
eprintln!(
"warning: mermaid diagram scaled to {:.0}% to fit the page -- \
consider splitting it",
shrink * 100.0
);
}
}
let align = parse_layout_align(&tag.attributes).or(ctx.cell_content_align);
Some(vec![Block {
height: draw_h,
space_after: ctx.style.paragraph_space_after,
draw: BlockDraw::Svg {
tree: Arc::new(tree),
x: align_within(x, width, draw_w, align),
width: draw_w,
height: draw_h,
caption: None,
},
outline: None,
anchor_id: None,
tag_role: None,
page_column: 0,
column_span: false,
}])
}
pub(super) fn qr_svg_string(
value: &str,
ecl: &str,
quiet: i32,
dark: &str,
light: &str,
) -> Option<String> {
let ecl = match ecl.trim().to_ascii_lowercase().as_str() {
"low" | "l" => qrcodegen::QrCodeEcc::Low,
"quartile" | "q" => qrcodegen::QrCodeEcc::Quartile,
"high" | "h" => qrcodegen::QrCodeEcc::High,
_ => qrcodegen::QrCodeEcc::Medium,
};
let code = qrcodegen::QrCode::encode_text(value, ecl).ok()?;
Some(qr_svg(&code, quiet.max(0), dark, light))
}
fn layout_qr(tag: &Tag, x: f32, width: f32, ctx: &mut LayoutCtx<'_>) -> Vec<Block> {
let value = match tag.attributes.get("value") {
Some(Scalar::String(s)) if !s.trim().is_empty() => s.clone(),
Some(Scalar::Number(n)) if n.is_finite() => {
if n.fract() == 0.0 {
format!("{}", *n as i64)
} else {
n.to_string()
}
}
_ => return placeholder(x, width, ctx, "[qr: missing value]"),
};
let ecl = match tag.attributes.get("ecl") {
Some(Scalar::String(s)) => s.clone(),
_ => "medium".to_string(),
};
let quiet = (attr_points(&tag.attributes, "quiet_zone", 4.0).round() as i32).max(0);
let hex = |key: &str, fallback: &str| {
tag.attributes
.get(key)
.and_then(|v| match v {
Scalar::String(s) => css_hex(s),
_ => None,
})
.unwrap_or_else(|| fallback.to_string())
};
let svg = match qr_svg_string(
&value,
&ecl,
quiet,
&hex("color", "#000000"),
&hex("background", "#ffffff"),
) {
Some(s) => s,
None => return placeholder(x, width, ctx, "[qr: value too long to encode]"),
};
let size = attr_points(&tag.attributes, "size", 72.0).clamp(8.0, width.max(8.0));
let align = parse_layout_align(&tag.attributes).or(ctx.cell_content_align);
let draw_x = align_within(x, width, size, align);
match usvg::Tree::from_data(svg.as_bytes(), &usvg::Options::default()) {
Ok(tree) => vec![Block {
height: size,
space_after: ctx.style.paragraph_space_after,
draw: BlockDraw::Svg {
tree: Arc::new(tree),
x: draw_x,
width: size,
height: size,
caption: None,
},
outline: None,
anchor_id: None,
tag_role: None,
page_column: 0,
column_span: false,
}],
Err(e) => {
eprintln!("warning: qr render failed — {e}");
placeholder(x, width, ctx, "[qr: render failed]")
}
}
}
fn find_media_tag(node: &RenderableTreeNode) -> Option<&Tag> {
if let RenderableTreeNode::Tag(t) = node {
if t.name == "img" || t.name == "media" {
return Some(t.as_ref());
}
if t.name == "p" || t.name == "div" {
return t.children.iter().find_map(find_media_tag);
}
}
None
}
fn resolve_float_width(attr: Option<&Scalar>, col: f32) -> f32 {
let raw = match attr {
Some(Scalar::Number(v)) => {
let v = *v as f32;
if v <= 1.0 { col * v } else { v }
}
Some(Scalar::String(s)) => {
let s = s.trim();
if let Some(pct) = s.strip_suffix('%') {
pct.trim()
.parse::<f32>()
.ok()
.map(|p| col * p / 100.0)
.unwrap_or(col * 0.4)
} else {
match s.parse::<f32>() {
Ok(v) if v <= 1.0 => col * v,
Ok(v) => v,
Err(_) => col * 0.4,
}
}
}
_ => col * 0.4,
};
raw.clamp(24.0, col * 0.8)
}
fn is_plain_paragraph(node: &RenderableTreeNode) -> bool {
match node {
RenderableTreeNode::Tag(t) if t.name == "p" => !t.children.iter().any(|c| {
matches!(
c,
RenderableTreeNode::Tag(ct)
if matches!(ct.name.as_str(), "img" | "media" | "callout")
)
}),
_ => false,
}
}
#[allow(clippy::too_many_arguments)]
fn layout_paragraph_float(
node: &RenderableTreeNode,
x: f32,
full_w: f32,
narrow_x: f32,
narrow_w: f32,
exclude_h: f32,
ctx: &mut LayoutCtx<'_>,
) -> Vec<Block> {
let RenderableTreeNode::Tag(tag) = node else {
return Vec::new();
};
let mut inlines =
Inlines::from_with_labels(&tag.children, &mut ctx.footnotes, Some(ctx.crossref_labels));
if inlines.text.trim().is_empty() {
return Vec::new();
}
apply_link_style(&mut inlines, &ctx.style.link);
if let Some(h) = ctx.hyphenator
&& inlines.style_ranges.is_empty()
&& inlines.links.is_empty()
&& inlines.mid_anchors.is_empty()
&& inlines.footnote_calls.is_empty()
{
inlines.text = h.hyphenate(&inlines.text);
}
let style = TextStyle {
font_size: ctx.style.body_font_size,
font_weight: 400.0,
line_height: ctx.style.body_line_height,
color: ctx.style.text_color.into(),
font_families: ctx.body_families,
italic: false,
};
let layout = build_layout_float(
&inlines.text,
&inlines.style_ranges,
&style,
full_w,
narrow_w,
narrow_x,
exclude_h,
ctx.font_cx,
ctx.layout_cx,
);
let slice = TextSlice::whole_with_extras(
layout,
inlines.text,
inlines.links,
inlines.mid_anchors,
inlines.footnote_calls,
x,
);
let height = slice.height();
vec![Block {
height,
space_after: ctx.style.paragraph_space_after,
draw: BlockDraw::Text(slice),
outline: None,
anchor_id: None,
tag_role: None,
page_column: 0,
column_span: false,
}]
}
fn layout_float(tag: &Tag, x: f32, width: f32, ctx: &mut LayoutCtx<'_>) -> Vec<Block> {
if has_anchored_floats(&tag.children) {
return layout_float_anchored(tag, x, width, ctx);
}
let Some(fig_idx) = tag
.children
.iter()
.position(|c| find_media_tag(c).is_some())
else {
return layout_children(&tag.children, x, width, ctx);
};
let col = width;
let side_right = matches!(
tag.attributes.get("side"),
Some(Scalar::String(s)) if s.trim() == "right"
);
let gap = tag
.attributes
.get("gap")
.and_then(|s| match s {
Scalar::Number(v) => Some(*v as f32),
Scalar::String(s) => s.trim().parse::<f32>().ok(),
_ => None,
})
.unwrap_or(14.0)
.max(0.0);
let img_target_w = resolve_float_width(tag.attributes.get("width"), col);
let media_tag = find_media_tag(&tag.children[fig_idx]).unwrap();
let mut img_blocks = layout_media(media_tag, x, img_target_w, ctx);
let (img_w, img_h) = match img_blocks.first().map(|b| &b.draw) {
Some(BlockDraw::Image { width, height, .. } | BlockDraw::Svg { width, height, .. }) => {
(*width, *height)
}
_ => return layout_children(&tag.children, x, width, ctx),
};
let mut img_block = img_blocks.remove(0);
let wrap_children = &tag.children[fig_idx + 1..];
let narrow_w = col - img_w - gap;
if wrap_children.is_empty() || narrow_w < ctx.style.body_font_size * 6.0 {
let mut out = vec![img_block];
out.extend(layout_children(wrap_children, x, col, ctx));
return out;
}
let (narrow_x, img_x) = if side_right {
(0.0, x + col - img_w)
} else {
(img_w + gap, x)
};
match &mut img_block.draw {
BlockDraw::Image { x: ix, .. } | BlockDraw::Svg { x: ix, .. } => *ix = img_x,
_ => {}
}
let exclude_h = img_h + ctx.style.body_font_size * 0.3;
let mut wrap: Vec<Block> = Vec::new();
let mut y = 0.0_f32;
let mut k = 0;
while k < wrap_children.len() && y < exclude_h {
let child = &wrap_children[k];
let mut blocks = if is_plain_paragraph(child) {
layout_paragraph_float(child, x, col, narrow_x, narrow_w, exclude_h - y, ctx)
} else {
layout_node(child, x + narrow_x, narrow_w, ctx)
};
for b in &blocks {
y += b.height + b.space_after;
}
wrap.append(&mut blocks);
k += 1;
}
if k < wrap_children.len() {
wrap.extend(layout_children(&wrap_children[k..], x, col, ctx));
}
if wrap.is_empty() {
return vec![img_block];
}
let mut wrap_bottom = 0.0_f32;
let mut acc = 0.0_f32;
for b in &wrap {
acc += b.height;
wrap_bottom = acc;
acc += b.space_after;
}
vec![Block {
height: img_h.max(wrap_bottom),
space_after: ctx.style.paragraph_space_after,
draw: BlockDraw::Float {
image: Box::new(img_block),
wrap,
},
outline: None,
anchor_id: None,
tag_role: None,
page_column: 0,
column_span: false,
}]
}
fn has_anchored_floats(children: &[RenderableTreeNode]) -> bool {
fn node_has(node: &RenderableTreeNode) -> bool {
if let RenderableTreeNode::Tag(t) = node {
if (t.name == "img" || t.name == "media") && t.attributes.contains_key("side") {
return true;
}
return t.children.iter().any(node_has);
}
false
}
children.iter().any(node_has)
}
fn media_side_right(tag: &Tag) -> Option<bool> {
match tag.attributes.get("side") {
Some(Scalar::String(s)) => match s.trim() {
"right" => Some(true),
"left" => Some(false),
_ => None,
},
_ => None,
}
}
fn append_inlines(dst: &mut Inlines, src: Inlines) {
let base = dst.text.len();
dst.text.push_str(&src.text);
for mut r in src.style_ranges {
r.start += base;
r.end += base;
dst.style_ranges.push(r);
}
for mut l in src.links {
l.start += base;
l.end += base;
dst.links.push(l);
}
for mut m in src.mid_anchors {
m.byte_offset += base;
dst.mid_anchors.push(m);
}
for mut f in src.footnote_calls {
f.byte_offset += base;
dst.footnote_calls.push(f);
}
}
fn flatten_anchored(
children: &[RenderableTreeNode],
ctx: &mut LayoutCtx<'_>,
) -> (Inlines, Vec<(usize, Tag)>) {
fn run(
children: &[RenderableTreeNode],
ib: &mut Inlines,
anchors: &mut Vec<(usize, Tag)>,
ctx: &mut LayoutCtx<'_>,
) {
for c in children {
match c {
RenderableTreeNode::Tag(t) if t.name == "img" || t.name == "media" => {
anchors.push((ib.text.len(), (**t).clone()));
}
other => {
let sub = Inlines::from_with_labels(
std::slice::from_ref(other),
&mut ctx.footnotes,
Some(ctx.crossref_labels),
);
append_inlines(ib, sub);
}
}
}
}
let mut ib = Inlines::new();
let mut anchors: Vec<(usize, Tag)> = Vec::new();
for child in children {
match child {
RenderableTreeNode::Tag(t) if t.name == "p" || t.name == "div" => {
if !ib.text.is_empty() {
ib.text.push_str("\n\n");
}
run(&t.children, &mut ib, &mut anchors, ctx);
}
RenderableTreeNode::Tag(t) if t.name == "img" || t.name == "media" => {
anchors.push((ib.text.len(), (**t).clone()));
}
other => {
if !ib.text.is_empty() {
ib.text.push_str("\n\n");
}
let sub = Inlines::from_with_labels(
std::slice::from_ref(other),
&mut ctx.footnotes,
Some(ctx.crossref_labels),
);
append_inlines(&mut ib, sub);
}
}
}
(ib, anchors)
}
fn layout_float_anchored(tag: &Tag, x: f32, width: f32, ctx: &mut LayoutCtx<'_>) -> Vec<Block> {
let col = width;
let gap = tag
.attributes
.get("gap")
.and_then(|s| match s {
Scalar::Number(v) => Some(*v as f32),
Scalar::String(s) => s.trim().parse::<f32>().ok(),
_ => None,
})
.unwrap_or(14.0)
.max(0.0);
let (mut inlines, anchor_tags) = flatten_anchored(&tag.children, ctx);
if anchor_tags.is_empty() || inlines.text.trim().is_empty() {
return layout_children(&tag.children, x, width, ctx);
}
let mut specs: Vec<FloatSpec> = Vec::new();
let mut images: Vec<Block> = Vec::new();
for (offset, mtag) in &anchor_tags {
let side_right = media_side_right(mtag).unwrap_or(false);
let target_w = resolve_float_width(mtag.attributes.get("width"), col);
let mut blocks = layout_media(mtag, x, target_w, ctx);
let (w, h) = match blocks.first().map(|b| &b.draw) {
Some(BlockDraw::Image { width, height, .. } | BlockDraw::Svg { width, height, .. }) => {
(*width, *height)
}
_ => continue,
};
specs.push(FloatSpec {
offset: *offset,
side_right,
width: w,
height: h,
});
images.push(blocks.remove(0));
}
let max_reserve = specs.iter().map(|s| s.width + gap).fold(0.0_f32, f32::max);
if specs.is_empty() || col - max_reserve < ctx.style.body_font_size * 6.0 {
return layout_children(&tag.children, x, width, ctx);
}
apply_link_style(&mut inlines, &ctx.style.link);
let style = TextStyle {
font_size: ctx.style.body_font_size,
font_weight: 400.0,
line_height: ctx.style.body_line_height,
color: ctx.style.text_color.into(),
font_families: ctx.body_families,
italic: false,
};
let (layout, placements) = build_layout_float_anchored(
&inlines.text,
&inlines.style_ranges,
&style,
col,
gap,
&specs,
ctx.font_cx,
ctx.layout_cx,
);
let text_slice = TextSlice::whole_with_extras(
layout,
inlines.text,
inlines.links,
inlines.mid_anchors,
inlines.footnote_calls,
x,
);
let mut region_bottom = text_slice.height();
let mut floats: Vec<FloatItem> = Vec::new();
for (i, mut img) in images.into_iter().enumerate() {
let p = placements[i];
match &mut img.draw {
BlockDraw::Image { x: ix, .. } | BlockDraw::Svg { x: ix, .. } => *ix = x + p.x,
_ => {}
}
region_bottom = region_bottom.max(p.y + specs[i].height);
floats.push(FloatItem {
image: Box::new(img),
y_offset: p.y,
});
}
vec![Block {
height: region_bottom,
space_after: ctx.style.paragraph_space_after,
draw: BlockDraw::FloatRegion {
text: text_slice,
floats,
},
outline: None,
anchor_id: None,
tag_role: None,
page_column: 0,
column_span: false,
}]
}
fn layout_input(tag: &Tag, x: f32, width: f32, ctx: &mut LayoutCtx<'_>) -> Vec<Block> {
let attr_str = |k: &str| match tag.attributes.get(k) {
Some(Scalar::String(s)) if !s.trim().is_empty() => Some(s.trim().to_string()),
_ => None,
};
let attr_num = |k: &str| match tag.attributes.get(k) {
Some(Scalar::Number(n)) => Some(*n),
Some(Scalar::String(s)) => s.trim().parse::<f64>().ok(),
_ => None,
};
let attr_disp = |k: &str| match tag.attributes.get(k) {
Some(Scalar::String(s)) if !s.trim().is_empty() => Some(s.trim().to_string()),
Some(Scalar::Number(n)) => Some(if n.fract() == 0.0 {
format!("{}", *n as i64)
} else {
format!("{n}")
}),
_ => None,
};
let required = matches!(tag.attributes.get("required"), Some(Scalar::Boolean(true)))
|| matches!(tag.attributes.get("required"),
Some(Scalar::String(s)) if s.trim().eq_ignore_ascii_case("true"));
let ty = attr_str("type").unwrap_or_else(|| "text".to_string());
let is_checkbox = ty == "checkbox";
let label_txt = attr_str("label")
.or_else(|| attr_str("name"))
.unwrap_or_default();
let fs = ctx.style.body_font_size;
let border: krilla::color::rgb::Color = ctx.style.blockquote_text_color.into();
let thickness = 0.75;
let label_style = TextStyle {
font_size: fs,
font_weight: 400.0,
line_height: ctx.style.body_line_height,
color: ctx.style.text_color.into(),
font_families: ctx.body_families,
italic: false,
};
let label_slice = if label_txt.is_empty() {
None
} else {
let mut text = label_txt.clone();
let mut ranges: Vec<InlineRange> = Vec::new();
if required {
let star_start = text.len() + 1; text.push_str(" *");
ranges.push(InlineRange {
start: star_start,
end: text.len(),
prop: InlineProp::Color(krilla::color::rgb::Color::new(200, 45, 45)),
});
}
let layout = build_layout(
&text,
&ranges,
&label_style,
width,
ctx.font_cx,
ctx.layout_cx,
);
Some(TextSlice::whole(layout, text, Vec::new(), x))
};
let label_h = label_slice.as_ref().map(|s| s.height()).unwrap_or(0.0);
let gap1 = fs * 0.3;
let field_y = label_h + gap1;
let (field_w, field_h) = if is_checkbox {
let s = fs * 1.05;
(s, s)
} else {
let char_w = fs * 0.62;
let desired = match attr_num("maxlength") {
Some(ml) if ml > 0.0 => (ml as f32 * char_w).max(char_w * 3.0),
_ => width,
};
let fw = desired.min(width).max((fs * 3.0).min(width));
(fw, fs * 1.5)
};
let mut parts: Vec<String> = Vec::new();
if ty != "text" && ty != "checkbox" {
parts.push(ty.clone());
}
match (attr_num("minlength"), attr_num("maxlength")) {
(Some(a), Some(b)) => parts.push(format!("{}\u{2013}{} chars", a as i64, b as i64)),
(None, Some(b)) => parts.push(format!("max {} chars", b as i64)),
(Some(a), None) => parts.push(format!("min {} chars", a as i64)),
(None, None) => {}
}
match (attr_disp("min"), attr_disp("max")) {
(Some(a), Some(b)) => parts.push(format!("{a}\u{2013}{b}")),
(None, Some(b)) => parts.push(format!("\u{2264} {b}")),
(Some(a), None) => parts.push(format!("\u{2265} {a}")),
(None, None) => {}
}
let hint_str = parts.join(" \u{b7} ");
let hint_slice = if hint_str.is_empty() {
None
} else {
let hint_style = TextStyle {
font_size: fs * 0.82,
font_weight: 400.0,
line_height: ctx.style.body_line_height,
color: ctx.style.blockquote_text_color.into(),
font_families: ctx.body_families,
italic: true,
};
let layout = build_layout(
&hint_str,
&[],
&hint_style,
width,
ctx.font_cx,
ctx.layout_cx,
);
Some(TextSlice::whole(layout, hint_str, Vec::new(), x))
};
let gap2 = fs * 0.25;
let hint_y = field_y + field_h + gap2;
let height = match &hint_slice {
Some(h) => hint_y + h.height(),
None => field_y + field_h,
};
vec![Block {
height,
space_after: ctx.style.paragraph_space_after,
draw: BlockDraw::FormField {
label: label_slice,
field_x: x,
field_y,
field_w,
field_h,
border,
thickness,
hint: hint_slice,
hint_y,
},
outline: None,
anchor_id: None,
tag_role: None,
page_column: 0,
column_span: false,
}]
}
fn layout_table(tag: &Tag, x: f32, width: f32, ctx: &mut LayoutCtx<'_>) -> Vec<Block> {
let column_span = ctx.page_columns() > 1;
let lay_w = if column_span {
ctx.body_full_width()
} else {
width
};
let ov = TableOverride::from_attrs(&tag.attributes);
let rows_tag = table_rows_source(tag);
let mut header_rows: Vec<RowSource<'_>> = Vec::new();
let mut body_rows: Vec<RowSource<'_>> = Vec::new();
for child in &rows_tag.children {
let RenderableTreeNode::Tag(t) = child else {
continue;
};
match t.name.as_str() {
"thead" => {
if t.children.iter().any(is_cell_node) {
header_rows.push(RowSource::CellsDirect(t));
} else {
for tr in t.children.iter().filter_map(as_tr) {
header_rows.push(RowSource::Row(tr));
}
}
}
"tbody" => {
for tr in t.children.iter().filter_map(as_tr) {
body_rows.push(RowSource::Row(tr));
}
}
"tr" => body_rows.push(RowSource::Row(t)),
_ => {}
}
}
if header_rows.is_empty() && body_rows.is_empty() {
return Vec::new();
}
if !header_rows.is_empty() && rows_are_blank(&header_rows) {
header_rows.clear();
}
body_rows.retain(|r| !r.cells().all(|c| node_text_is_blank(&c.children)));
let num_cols = grid_num_cols(&header_rows, &body_rows);
if num_cols == 0 {
return Vec::new();
}
let padding = ov.cell_padding.unwrap_or(ctx.style.table_cell_padding);
let border_thickness = ctx.style.table_border_thickness;
let total_borders = border_thickness * (num_cols as f32 + 1.0);
let inner_width = lay_w - total_borders;
let weights = ov
.column_weights
.as_deref()
.unwrap_or(ctx.style.table_column_weights.as_slice());
let column_widths = match weighted_column_widths(weights, inner_width) {
Some(ws) if ws.len() == num_cols => ws,
_ => match ctx.style.table_column_sizing {
TableColumnSizing::Equal => vec![inner_width / num_cols as f32; num_cols],
TableColumnSizing::Auto => compute_auto_column_widths(
&header_rows,
&body_rows,
num_cols,
inner_width,
padding,
ctx,
),
},
};
let mut column_xs = Vec::with_capacity(num_cols);
let mut cursor = x + border_thickness;
for w in &column_widths {
column_xs.push(cursor);
cursor += w + border_thickness;
}
let header_column = ov.header_column.unwrap_or(ctx.style.table_header_column);
let column_aligns = header_column_aligns(&header_rows, num_cols);
let mut occupied = vec![0usize; num_cols];
let mut rows_out: Vec<TableRow> = Vec::new();
for r in header_rows.iter() {
rows_out.push(layout_row_source(
r,
true,
&column_xs,
&column_widths,
padding,
header_column,
&column_aligns,
&mut occupied,
ctx,
));
}
for r in body_rows.iter() {
rows_out.push(layout_row_source(
r,
false,
&column_xs,
&column_widths,
padding,
header_column,
&column_aligns,
&mut occupied,
ctx,
));
}
let stripe = match ov.stripe {
Some(s) => s,
None => ctx.style.table_stripe_color.map(Into::into),
};
if let Some(stripe) = stripe {
for (body_idx, row) in rows_out.iter_mut().filter(|r| !r.is_header).enumerate() {
if body_idx % 2 == 1 {
row.fill = Some(stripe);
}
}
}
let table_height: f32 = rows_out.iter().map(|r| r.height).sum::<f32>()
+ border_thickness * (rows_out.len() as f32 + 1.0);
let table_id = ctx.next_table_id();
let header_bg = ov
.header_bg
.unwrap_or_else(|| ctx.style.table_header_background.into());
let border_color = ov
.border_color
.unwrap_or_else(|| ctx.style.table_border_color.into());
let border_style = ov.borders.unwrap_or(ctx.style.table_borders);
let edge = match ov.edge_color {
Some(c) => Some((
c,
ctx.style.table_edge_thickness.unwrap_or(border_thickness),
)),
None => ctx.style.table_edge_color.map(|c| {
(
c.into(),
ctx.style.table_edge_thickness.unwrap_or(border_thickness),
)
}),
};
vec![Block {
height: table_height,
space_after: ctx.style.table_space_after,
draw: BlockDraw::Table {
x,
column_widths,
rows: rows_out,
cell_padding: padding,
header_bg,
border_color,
border_thickness,
border_style,
edge,
caption: ctx.pending_table_caption.take(),
},
outline: None,
anchor_id: Some(format!("__table_{table_id}")),
tag_role: None,
page_column: 0,
column_span,
}]
}
enum RowSource<'a> {
Row(&'a Tag),
CellsDirect(&'a Tag),
}
impl<'a> RowSource<'a> {
fn cells(&self) -> impl Iterator<Item = &'a Tag> + 'a {
let parent: &'a Tag = match self {
RowSource::Row(t) => t,
RowSource::CellsDirect(t) => t,
};
parent.children.iter().filter_map(|c| {
if let RenderableTreeNode::Tag(t) = c
&& (t.name == "th" || t.name == "td")
{
return Some(t.as_ref());
}
None
})
}
}
fn is_cell_node(c: &RenderableTreeNode) -> bool {
matches!(c, RenderableTreeNode::Tag(t) if t.name == "th" || t.name == "td")
}
fn as_tr(c: &RenderableTreeNode) -> Option<&Tag> {
if let RenderableTreeNode::Tag(t) = c
&& t.name == "tr"
{
return Some(t.as_ref());
}
None
}
fn rows_are_blank(rows: &[RowSource<'_>]) -> bool {
rows.iter()
.all(|r| r.cells().all(|c| node_text_is_blank(&c.children)))
}
fn node_text_is_blank(nodes: &[RenderableTreeNode]) -> bool {
nodes.iter().all(|n| match n {
RenderableTreeNode::Scalar(value) => {
super::inline::scalar_text(value).is_none_or(|text| text.trim().is_empty())
}
RenderableTreeNode::Tag(t) => {
!matches!(t.name.as_str(), "img" | "media") && node_text_is_blank(&t.children)
}
})
}
fn cell_colspan(tag: &Tag) -> usize {
cell_span(tag, "colSpan", "colspan")
}
fn cell_rowspan(tag: &Tag) -> usize {
cell_span(tag, "rowSpan", "rowspan")
}
fn cell_span(tag: &Tag, rendered: &str, written: &str) -> usize {
match tag
.attributes
.get(rendered)
.or_else(|| tag.attributes.get(written))
{
Some(Scalar::Number(n)) if *n >= 1.0 => *n as usize,
_ => 1,
}
}
fn header_column_aligns(
header_rows: &[RowSource<'_>],
num_cols: usize,
) -> Vec<parley::layout::Alignment> {
let mut aligns = vec![parley::layout::Alignment::Start; num_cols];
let Some(first) = header_rows.first() else {
return aligns;
};
let mut col = 0usize;
for cell in first.cells() {
let span = cell_colspan(cell);
if let Some(align) = cell_align(cell) {
for a in aligns.iter_mut().skip(col).take(span) {
*a = align;
}
}
col += span;
}
aligns
}
fn grid_num_cols(header_rows: &[RowSource<'_>], body_rows: &[RowSource<'_>]) -> usize {
let mut occupied: Vec<usize> = Vec::new();
let mut num_cols = 0usize;
for src in header_rows.iter().chain(body_rows.iter()) {
let mut col = 0usize;
for cell in src.cells() {
while col < occupied.len() && occupied[col] > 0 {
col += 1;
}
let span = cell_colspan(cell);
let rowspan = cell_rowspan(cell);
while occupied.len() < col + span {
occupied.push(0);
}
for o in occupied.iter_mut().take(col + span).skip(col) {
*o = rowspan;
}
col += span;
}
num_cols = num_cols.max(occupied.len()).max(col);
for o in occupied.iter_mut() {
*o = o.saturating_sub(1);
}
}
num_cols
}
fn cell_align(cell: &Tag) -> Option<parley::layout::Alignment> {
use parley::layout::Alignment;
match cell.attributes.get("align") {
Some(Scalar::String(s)) => match s.as_str() {
"center" => Some(Alignment::Center),
"right" => Some(Alignment::End),
"left" => Some(Alignment::Start),
_ => None,
},
_ => None,
}
}
#[allow(clippy::too_many_arguments)]
fn layout_row_source(
src: &RowSource<'_>,
is_header: bool,
column_xs: &[f32],
column_widths: &[f32],
padding: f32,
header_column: bool,
column_aligns: &[parley::layout::Alignment],
occupied: &mut [usize],
ctx: &mut LayoutCtx<'_>,
) -> TableRow {
let ncols = column_xs.len();
let mut cells: Vec<TableCell> = Vec::new();
let mut col = 0usize;
for cell_tag in src.cells() {
while col < ncols && occupied.get(col).copied().unwrap_or(0) > 0 {
col += 1;
}
if col >= ncols {
break;
}
let span = cell_colspan(cell_tag).clamp(1, ncols - col);
let rowspan = cell_rowspan(cell_tag).max(1);
let last = col + span - 1;
let cell_x = column_xs[col] + padding;
let spanned_w = (column_xs[last] + column_widths[last]) - column_xs[col];
let cell_text_width = (spanned_w - 2.0 * padding).max(0.0);
let cell_is_header = is_header || (header_column && col == 0);
let align = cell_align(cell_tag).unwrap_or_else(|| {
column_aligns
.get(col)
.copied()
.unwrap_or(parley::layout::Alignment::Start)
});
let blocks = layout_cell_content(
cell_tag,
cell_x,
cell_text_width,
cell_is_header,
align,
ctx,
);
for o in occupied.iter_mut().take(last + 1).skip(col) {
*o = rowspan;
}
cells.push(TableCell {
blocks,
col,
colspan: span,
rowspan,
});
col += span;
}
while col < ncols {
if occupied.get(col).copied().unwrap_or(0) == 0 {
cells.push(TableCell {
blocks: Vec::new(),
col,
colspan: 1,
rowspan: 1,
});
}
col += 1;
}
let mut max_height = cells
.iter()
.filter(|c| c.rowspan <= 1)
.map(|c| sum_block_height(&c.blocks))
.fold(0.0_f32, f32::max);
if max_height == 0.0 {
max_height = cells
.iter()
.map(|c| sum_block_height(&c.blocks))
.fold(0.0_f32, f32::max);
}
for o in occupied.iter_mut() {
*o = o.saturating_sub(1);
}
TableRow {
is_header,
fill: None,
header_column,
height: max_height + 2.0 * padding,
cells,
}
}
fn weighted_column_widths(weights: &[f32], inner_width: f32) -> Option<Vec<f32>> {
if weights.is_empty() || weights.iter().any(|w| *w <= 0.0) {
return None;
}
let sum: f32 = weights.iter().sum();
Some(weights.iter().map(|w| inner_width * w / sum).collect())
}
fn compute_auto_column_widths(
header_rows: &[RowSource<'_>],
body_rows: &[RowSource<'_>],
num_cols: usize,
inner_width: f32,
padding: f32,
ctx: &mut LayoutCtx<'_>,
) -> Vec<f32> {
let mut col_min: Vec<f32> = vec![0.0; num_cols];
let mut col_max: Vec<f32> = vec![0.0; num_cols];
let rows = header_rows
.iter()
.map(|row| (row, true))
.chain(body_rows.iter().map(|row| (row, false)));
for (src, is_header) in rows {
let mut col = 0usize;
for cell_tag in src.cells() {
if col >= num_cols {
break;
}
let span = cell_colspan(cell_tag).clamp(1, num_cols - col);
if span == 1 {
let weight = if is_header { 700.0 } else { 400.0 };
let style = TextStyle {
font_size: ctx.style.body_font_size,
font_weight: weight,
line_height: ctx.style.body_line_height,
color: ctx.style.text_color.into(),
font_families: ctx.body_families,
italic: false,
};
let (cell_min, cell_max) = measure_cell_widths(cell_tag, &style, ctx);
col_min[col] = col_min[col].max(cell_min + 2.0 * padding);
col_max[col] = col_max[col].max(cell_max + 2.0 * padding);
}
col += span;
}
}
let fair_min = inner_width / num_cols as f32;
for m in &mut col_min {
*m = m.min(fair_min);
}
distribute(col_min, col_max, inner_width)
}
fn measure_cell_widths(cell: &Tag, style: &TextStyle<'_>, ctx: &mut LayoutCtx<'_>) -> (f32, f32) {
let mut text = String::new();
let mut ranges = Vec::new();
collect_inlines(&mut text, &mut ranges, &cell.children, &mut Vec::new());
let trimmed = text.trim();
if trimmed.is_empty() {
return (0.0, 0.0);
}
let natural = measure_first_line_width(trimmed, style, ctx.font_cx, ctx.layout_cx);
let min_word = trimmed
.split_whitespace()
.map(|w| measure_first_line_width(w, style, ctx.font_cx, ctx.layout_cx))
.fold(0.0_f32, f32::max);
(min_word, natural)
}
fn distribute(col_min: Vec<f32>, col_max: Vec<f32>, inner_width: f32) -> Vec<f32> {
let n = col_min.len();
let sum_max: f32 = col_max.iter().sum();
if sum_max <= inner_width {
let leftover = inner_width - sum_max;
return col_max
.iter()
.map(|m| m + leftover * (m / sum_max.max(1e-6)))
.collect();
}
let sum_min: f32 = col_min.iter().sum();
if sum_min >= inner_width {
let scale = if sum_min > 0.0 {
inner_width / sum_min
} else {
1.0
};
return col_min.iter().map(|m| m * scale).collect();
}
let extra = inner_width - sum_min;
let total_diff: f32 = col_max
.iter()
.zip(&col_min)
.map(|(a, b)| (a - b).max(0.0))
.sum();
if total_diff <= 0.0 {
return vec![inner_width / n as f32; n];
}
col_min
.iter()
.zip(col_max.iter())
.map(|(mn, mx)| mn + extra * ((mx - mn).max(0.0) / total_diff))
.collect()
}
fn sum_block_height(blocks: &[Block]) -> f32 {
let total: f32 = blocks.iter().map(|b| b.height + b.space_after).sum();
total - blocks.last().map(|b| b.space_after).unwrap_or(0.0)
}
fn layout_cell_content(
cell: &Tag,
x: f32,
width: f32,
is_header: bool,
align: parley::layout::Alignment,
ctx: &mut LayoutCtx<'_>,
) -> Vec<Block> {
let any_block_child = cell.children.iter().any(|c| {
if let RenderableTreeNode::Tag(t) = c {
matches!(
t.name.as_str(),
"p" | "ul"
| "ol"
| "list"
| "blockquote"
| "pre"
| "callout"
| "h1"
| "h2"
| "h3"
| "h4"
| "h5"
| "h6"
| "table"
| "img"
| "media"
)
} else {
false
}
});
if !any_block_child {
return layout_table_cell_paragraph(cell, x, width, is_header, align, ctx);
}
layout_children(&cell.children, x, width, ctx)
}
const IMAGE_H_PAD: f32 = 5.0;
fn media_is_small(tag: &Tag) -> bool {
matches!(
tag.attributes.get("size"),
Some(Scalar::String(s)) if s.trim() == "small"
)
}
fn small_media_tag(node: &RenderableTreeNode) -> Option<&Tag> {
match node {
RenderableTreeNode::Tag(t) if t.name == "img" || t.name == "media" => {
media_is_small(t).then_some(t.as_ref())
}
RenderableTreeNode::Tag(t) if t.name == "p" => {
let mut media: Option<&Tag> = None;
for child in &t.children {
match child {
RenderableTreeNode::Tag(inner)
if inner.name == "img" || inner.name == "media" =>
{
if media.is_some() {
return None;
}
media = Some(inner.as_ref());
}
RenderableTreeNode::Scalar(Scalar::String(s)) if s.trim().is_empty() => {}
RenderableTreeNode::Scalar(Scalar::Null) => {}
_ => return None,
}
}
media.filter(|m| media_is_small(m))
}
_ => None,
}
}
fn layout_small_media_row(
tags: &[&Tag],
x: f32,
width: f32,
ctx: &mut LayoutCtx<'_>,
) -> Vec<Block> {
use parley::layout::Alignment;
let cells: Vec<Vec<RenderableTreeNode>> = tags
.iter()
.map(|t| {
let mut cell_tag = (*t).clone();
cell_tag
.attributes
.insert("size".to_string(), Scalar::String("large".to_string()));
vec![RenderableTreeNode::Tag(Box::new(cell_tag))]
})
.collect();
let rows = layout_cells_row(cells, x, width, 0.0, None, Some(Alignment::Center), ctx);
vec![atomic_group(
rows,
x,
width,
ctx.style.paragraph_space_after,
)]
}
fn layout_media(tag: &Tag, x: f32, width: f32, ctx: &mut LayoutCtx<'_>) -> Vec<Block> {
let x = x + IMAGE_H_PAD;
let width = (width - 2.0 * IMAGE_H_PAD).max(0.0);
let src_attr = tag.attributes.get("src").and_then(|v| match v {
Scalar::String(s) if !s.trim().is_empty() => Some(s.clone()),
_ => None,
});
let id_attr = tag.attributes.get("id").and_then(|v| match v {
Scalar::String(s) if !s.trim().is_empty() => Some(s.trim().to_string()),
_ => None,
});
let candidates: Vec<String> = if let Some(s) = &src_attr {
vec![s.clone()]
} else if let Some(id) = &id_attr {
const EXTS: [&str; 6] = ["webp", "png", "jpg", "jpeg", "gif", "svg"];
let mut v: Vec<String> = EXTS.iter().map(|e| format!("{id}.{e}")).collect();
v.push(id.clone()); v
} else {
return placeholder(x, width, ctx, "[media: missing src or id]");
};
let alt = match tag.attributes.get("alt") {
Some(Scalar::String(s)) if !s.is_empty() => s.clone(),
_ => {
let mut buf = String::new();
for child in &tag.children {
if let RenderableTreeNode::Scalar(Scalar::String(s)) = child {
buf.push_str(s);
}
}
buf
}
};
let title = match tag.attributes.get("title") {
Some(Scalar::String(s)) if !s.trim().is_empty() => Some(s.clone()),
_ => None,
};
let use_title = ctx.style.image_title_fallback;
let caption_attr = match tag.attributes.get("caption") {
Some(Scalar::String(s)) if !s.trim().is_empty() => Some(s.clone()),
_ => None,
};
let had_caption_tag = ctx.pending_figure_caption.is_some();
let avail = match tag.attributes.get("size") {
Some(Scalar::String(s)) => match s.trim() {
"small" => width * 0.5,
"medium" => width * 0.75,
"large" => width,
_ => width,
},
_ => width,
};
let (src, bytes) = {
let mut found: Option<(String, Vec<u8>)> = None;
let mut last_err: Option<String> = None;
for cand in &candidates {
match ctx.assets.fetch(cand) {
Ok(b) => {
found = Some((cand.clone(), b));
break;
}
Err(e) => last_err = Some(e.to_string()),
}
}
if found.is_none()
&& let Some(id) = &id_attr
&& let Some(path) = ctx.assets.resolve_id(id.as_str())
&& let Ok(b) = ctx.assets.fetch(&path)
{
found = Some((path, b));
}
match found {
Some(fb) => fb,
None => {
let what = src_attr
.clone()
.or_else(|| id_attr.clone())
.unwrap_or_default();
let err = last_err.unwrap_or_else(|| "not found".to_string());
eprintln!("warning: media unavailable: {what} — {err}");
let msg = if alt.is_empty() {
format!("[media unavailable: {what}]")
} else {
format!("[{alt}]")
};
return placeholder(x, width, ctx, &msg);
}
}
};
let format = sniff_format(&bytes);
match format {
MediaFormat::Png | MediaFormat::Jpeg | MediaFormat::Gif | MediaFormat::Webp => {
let image = match format {
MediaFormat::Png => KrillaImage::from_png(bytes.into(), false),
MediaFormat::Jpeg => KrillaImage::from_jpeg(bytes.into(), false),
MediaFormat::Gif => KrillaImage::from_gif(bytes.into(), false),
MediaFormat::Webp => KrillaImage::from_webp(bytes.into(), false),
_ => unreachable!(),
};
let image = match image {
Ok(img) => img,
Err(e) => {
eprintln!("warning: media decode failed: {src} — {e}");
return placeholder(
x,
width,
ctx,
&format!("[media decode failed: {src} — {e}]"),
);
}
};
let (px_w, px_h) = image.size();
let (display_w, display_h) = fit_size(px_w as f32, px_h as f32, avail);
let draw_x = align_within(x, width, display_w, ctx.cell_content_align);
let figure_id = ctx.next_figure_id();
let caption = ctx
.pending_figure_caption
.take()
.or_else(|| caption_attr.clone())
.or_else(|| (!alt.is_empty()).then(|| alt.clone()))
.or_else(|| if use_title { title.clone() } else { None });
let block = Block {
height: display_h,
space_after: ctx.style.paragraph_space_after,
draw: BlockDraw::Image {
image,
x: draw_x,
width: display_w,
height: display_h,
caption,
},
outline: None,
anchor_id: Some(format!("__figure_{figure_id}")),
tag_role: None,
page_column: 0,
column_span: false,
};
let visible = if had_caption_tag { None } else { caption_attr };
finish_media(block, visible, x, width, ctx)
}
MediaFormat::Svg => {
let opts = usvg::Options::default();
match usvg::Tree::from_data(&bytes, &opts) {
Ok(tree) => {
let size = tree.size();
let (display_w, display_h) = fit_size(size.width(), size.height(), avail);
let draw_x = align_within(x, width, display_w, ctx.cell_content_align);
let figure_id = ctx.next_figure_id();
let caption = ctx
.pending_figure_caption
.take()
.or_else(|| caption_attr.clone())
.or_else(|| (!alt.is_empty()).then(|| alt.clone()))
.or_else(|| if use_title { title.clone() } else { None });
let block = Block {
height: display_h,
space_after: ctx.style.paragraph_space_after,
draw: BlockDraw::Svg {
tree: Arc::new(tree),
x: draw_x,
width: display_w,
height: display_h,
caption,
},
outline: None,
anchor_id: Some(format!("__figure_{figure_id}")),
tag_role: None,
page_column: 0,
column_span: false,
};
let visible = if had_caption_tag { None } else { caption_attr };
finish_media(block, visible, x, width, ctx)
}
Err(e) => {
eprintln!("warning: svg parse failed: {src} — {e}");
placeholder(x, width, ctx, &format!("[svg parse failed: {src} — {e}]"))
}
}
}
MediaFormat::Unknown => {
eprintln!(
"warning: unknown media format: {src} (sniffed bytes don't match png/jpeg/gif/webp/svg)"
);
placeholder(x, width, ctx, &format!("[unknown media format: {src}]"))
}
}
}
fn align_within(x: f32, avail: f32, content: f32, align: Option<parley::layout::Alignment>) -> f32 {
use parley::layout::Alignment;
let slack = (avail - content).max(0.0);
match align {
Some(Alignment::Center) => x + slack * 0.5,
Some(Alignment::End) => x + slack,
_ => x,
}
}
fn fit_size(natural_w: f32, natural_h: f32, available_w: f32) -> (f32, f32) {
if natural_w <= 0.0 || natural_h <= 0.0 {
return (available_w, available_w * 0.5);
}
if natural_w <= available_w {
return (natural_w, natural_h);
}
let scale = available_w / natural_w;
(available_w, natural_h * scale)
}
fn placeholder(x: f32, width: f32, ctx: &mut LayoutCtx<'_>, message: &str) -> Vec<Block> {
let style = TextStyle {
font_size: ctx.style.body_font_size,
font_weight: 400.0,
line_height: ctx.style.body_line_height,
color: ctx.style.blockquote_text_color.into(),
font_families: ctx.body_families,
italic: false,
};
let text = message.to_string();
let layout = build_layout(&text, &[], &style, width, ctx.font_cx, ctx.layout_cx);
let slice = TextSlice::whole(layout, text, Vec::new(), x);
let height = slice.height();
vec![Block {
height,
space_after: ctx.style.paragraph_space_after,
draw: BlockDraw::Text(slice),
outline: None,
anchor_id: None,
tag_role: None,
page_column: 0,
column_span: false,
}]
}
fn layout_table_cell_paragraph(
cell: &Tag,
x: f32,
width: f32,
is_header: bool,
align: parley::layout::Alignment,
ctx: &mut LayoutCtx<'_>,
) -> Vec<Block> {
let mut inlines =
Inlines::from_with_labels(&cell.children, &mut Vec::new(), Some(ctx.crossref_labels));
if inlines.text.trim().is_empty() {
return Vec::new();
}
apply_link_style(&mut inlines, &ctx.style.link);
let (weight, color) = if is_header {
(700.0, ctx.style.table_header_text_color.into())
} else {
(400.0, ctx.style.text_color.into())
};
let style = TextStyle {
font_size: ctx.style.body_font_size,
font_weight: weight,
line_height: ctx.style.body_line_height,
color,
font_families: ctx.body_families,
italic: false,
};
let layout = build_layout_aligned(
&inlines.text,
&inlines.style_ranges,
&style,
width,
align,
ctx.font_cx,
ctx.layout_cx,
);
let slice = TextSlice::whole(layout, inlines.text, inlines.links, x);
let height = slice.height();
vec![Block {
height,
space_after: 0.0,
draw: BlockDraw::Text(slice),
outline: None,
anchor_id: None,
tag_role: None,
page_column: 0,
column_span: false,
}]
}
fn apply_link_style(inlines: &mut Inlines, link_style: &super::style::LinkStyle) {
let link_color: krilla::color::rgb::Color = link_style.color.into();
for link in &inlines.links {
inlines.style_ranges.push(InlineRange {
start: link.start,
end: link.end,
prop: InlineProp::Color(link_color),
});
if link_style.italic {
inlines.style_ranges.push(InlineRange {
start: link.start,
end: link.end,
prop: InlineProp::Italic,
});
}
if link_style.bold {
inlines.style_ranges.push(InlineRange {
start: link.start,
end: link.end,
prop: InlineProp::Bold,
});
}
}
if link_style.underline {
for link in &inlines.links {
inlines.style_ranges.push(InlineRange {
start: link.start,
end: link.end,
prop: InlineProp::Underline {
thickness: link_style.underline_thickness,
},
});
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use markdoc::types::Tag as MdTag;
#[test]
fn heading_counters_nest_and_reset() {
let mut c = [0u32; 6];
assert_eq!(bump_heading_counters(&mut c, 1, 3).as_deref(), Some("1"));
assert_eq!(bump_heading_counters(&mut c, 2, 3).as_deref(), Some("1.1"));
assert_eq!(
bump_heading_counters(&mut c, 3, 3).as_deref(),
Some("1.1.1")
);
assert_eq!(
bump_heading_counters(&mut c, 3, 3).as_deref(),
Some("1.1.2")
);
assert_eq!(bump_heading_counters(&mut c, 2, 3).as_deref(), Some("1.2"));
assert_eq!(
bump_heading_counters(&mut c, 3, 3).as_deref(),
Some("1.2.1")
);
assert_eq!(bump_heading_counters(&mut c, 1, 3).as_deref(), Some("2"));
assert_eq!(bump_heading_counters(&mut c, 2, 3).as_deref(), Some("2.1"));
}
#[test]
fn ordered_marker_sequences_format() {
use super::super::style::MarkerSequence::*;
assert_eq!(format_ordered_marker(1, Decimal), "1");
assert_eq!(format_ordered_marker(42, Decimal), "42");
assert_eq!(format_ordered_marker(1, LowerAlpha), "a");
assert_eq!(format_ordered_marker(26, LowerAlpha), "z");
assert_eq!(format_ordered_marker(27, LowerAlpha), "aa");
assert_eq!(format_ordered_marker(28, LowerAlpha), "ab");
assert_eq!(format_ordered_marker(2, UpperAlpha), "B");
assert_eq!(format_ordered_marker(4, LowerRoman), "iv");
assert_eq!(format_ordered_marker(9, LowerRoman), "ix");
assert_eq!(format_ordered_marker(2026, LowerRoman), "mmxxvi");
assert_eq!(format_ordered_marker(14, UpperRoman), "XIV");
assert_eq!(format_ordered_marker(4000, LowerRoman), "4000");
}
#[test]
fn overwide_token_does_not_squeeze_other_columns() {
let starved = distribute(vec![30.0, 800.0], vec![30.0, 800.0], 470.0);
assert!(starved[0] < 25.0, "col 0 starved: {}", starved[0]);
let ok = distribute(vec![30.0, 235.0], vec![30.0, 800.0], 470.0);
assert!((ok[0] - 30.0).abs() < 0.5, "col 0 width: {}", ok[0]);
assert!(ok[1] > 400.0, "col 1 width: {}", ok[1]);
}
#[test]
fn blank_cell_detection_drops_empty_headers() {
let txt = |s: &str| RenderableTreeNode::Scalar(Scalar::String(s.to_string()));
let tag = |name: &str, children: Vec<RenderableTreeNode>| {
RenderableTreeNode::Tag(Box::new(MdTag {
name: name.to_string(),
attributes: IndexMap::new(),
children,
}))
};
assert!(node_text_is_blank(&[]));
assert!(node_text_is_blank(&[txt(" ")]));
assert!(node_text_is_blank(&[tag("p", vec![txt("")])]));
assert!(!node_text_is_blank(&[txt("Date")]));
assert!(!node_text_is_blank(&[tag("strong", vec![txt("To:")])]));
assert!(!node_text_is_blank(&[tag("img", vec![])]));
}
#[test]
fn heading_numbering_respects_max_depth() {
let mut c = [0u32; 6];
assert_eq!(bump_heading_counters(&mut c, 1, 2).as_deref(), Some("1"));
assert_eq!(bump_heading_counters(&mut c, 2, 2).as_deref(), Some("1.1"));
assert_eq!(bump_heading_counters(&mut c, 3, 2), None);
assert_eq!(bump_heading_counters(&mut c, 2, 2).as_deref(), Some("1.2"));
}
#[test]
fn heading_numbering_clamps_and_guards() {
let mut c = [0u32; 6];
assert_eq!(bump_heading_counters(&mut c, 0, 3), None);
assert_eq!(bump_heading_counters(&mut c, 1, 0).as_deref(), Some("1"));
}
fn tag_with_attrs(attrs: &[(&str, Scalar)]) -> MdTag {
let mut m = IndexMap::new();
for (k, v) in attrs {
m.insert((*k).to_string(), v.clone());
}
MdTag {
name: "tag".to_string(),
attributes: m,
children: Vec::new(),
}
}
#[test]
fn attr_is_false_recognises_falsey_forms() {
assert!(attr_is_false(
&tag_with_attrs(&[("numbered", Scalar::Boolean(false))]),
"numbered"
));
assert!(attr_is_false(
&tag_with_attrs(&[("numbered", Scalar::String("false".into()))]),
"numbered"
));
assert!(attr_is_false(
&tag_with_attrs(&[("numbered", Scalar::String("False".into()))]),
"numbered"
));
assert!(attr_is_false(
&tag_with_attrs(&[("numbered", Scalar::String("0".into()))]),
"numbered"
));
}
#[test]
fn attr_is_false_rejects_truthy_or_absent() {
assert!(!attr_is_false(
&tag_with_attrs(&[("numbered", Scalar::Boolean(true))]),
"numbered"
));
assert!(!attr_is_false(
&tag_with_attrs(&[("numbered", Scalar::String("true".into()))]),
"numbered"
));
assert!(!attr_is_false(&tag_with_attrs(&[]), "numbered"));
}
#[test]
fn weighted_column_widths_split_in_proportion() {
let ws = weighted_column_widths(&[1.0, 3.0], 400.0).expect("usable weights");
assert_eq!(ws, vec![100.0, 300.0]);
let ws2 = weighted_column_widths(&[2.0, 6.0], 400.0).expect("usable weights");
assert_eq!(ws2, vec![100.0, 300.0]);
assert!(weighted_column_widths(&[], 400.0).is_none());
assert!(weighted_column_widths(&[1.0, 0.0], 400.0).is_none());
assert!(weighted_column_widths(&[1.0, -2.0], 400.0).is_none());
}
#[test]
fn table_override_parses_tag_attributes() {
use crate::render::style::TableBorders;
let mut a = IndexMap::new();
a.insert("borders".to_string(), Scalar::String("horizontal".into()));
a.insert(
"header_background".to_string(),
Scalar::String("#e8e8e8".into()),
);
a.insert("cell_padding".to_string(), Scalar::Number(6.0));
a.insert("column_weights".to_string(), Scalar::String("1 3.5".into()));
a.insert("stripe".to_string(), Scalar::String("#f5f5f5".into()));
a.insert("header_column".to_string(), Scalar::Boolean(true));
let ov = TableOverride::from_attrs(&a);
assert_eq!(ov.borders, Some(TableBorders::Horizontal));
assert!(ov.header_bg.is_some());
assert_eq!(ov.cell_padding, Some(6.0));
assert_eq!(ov.column_weights, Some(vec![1.0, 3.5]));
assert!(matches!(ov.stripe, Some(Some(_))));
assert_eq!(ov.header_column, Some(true));
let mut b = IndexMap::new();
b.insert("stripe".to_string(), Scalar::String("none".into()));
assert!(matches!(TableOverride::from_attrs(&b).stripe, Some(None)));
let none = TableOverride::from_attrs(&IndexMap::new());
assert!(none.borders.is_none());
assert!(none.stripe.is_none());
assert!(none.column_weights.is_none());
assert!(none.header_bg.is_none());
}
}