use super::*;
pub(super) fn collect_hf_runs(
dom: &Dom,
node: NodeId,
sheet: &StyleSheet,
text_w: f32,
media: Option<(&PartFs, &str)>,
) -> Vec<TextRun> {
let header = dom.name_is(node, &W::hdr());
let empty_break = |para: NodeId, pstyle: &std::rc::Rc<ParaStyle>, prun: &RunStyle| {
let mut mark = prun.clone();
if let Some(rpr) = dom
.element(para, &W::p_pr())
.and_then(|ppr| dom.element(ppr, &W::r_pr()))
{
apply_mark_rpr(dom, rpr, &mut mark, sheet);
}
let mut br = TextRun::new(HF_LINE_BREAK, mark);
br.para_gap = pstyle.before + pstyle.after;
br.hf_para = Some(pstyle.clone());
br
};
let mut runs = Vec::new();
let mut pending = Vec::new();
let mut last: Option<std::rc::Rc<ParaStyle>> = None;
let mut framed: Vec<TextRun> = Vec::new();
for para in dom.descendants(node, Some(&W::p())) {
if hf_para_is_shape_text(dom, para)
|| hf_para_in_table(dom, node, para)
|| hf_text_frame_key(dom, para, sheet).is_some()
{
continue;
}
let (mut pstyle, prun) = para_base(dom, para, sheet, None);
let ptabs: Vec<TabStop> = dom
.descendants(para, Some(&W::name("ptab")))
.into_iter()
.filter_map(|n| {
let (pos, align) = match attr_any(dom, n, "alignment") {
Some("center") => (PTAB_CENTER, TabAlign::Center),
Some("right") => (PTAB_RIGHT, TabAlign::Right),
_ => return None,
};
Some(TabStop {
pos,
align,
leader: TabLeader::None,
numbering: false,
})
})
.collect();
if !ptabs.is_empty() {
pstyle.tab_stops = ptabs;
}
let pstyle = std::rc::Rc::new(pstyle);
let flow_pics = media.is_some()
&& (!para_shown_text(dom, para).trim().is_empty()
|| [W::fld_simple(), W::fld_char()].iter().any(|name| {
dom.descendants(para, Some(name))
.into_iter()
.any(|field| dom.ancestors(field, Some(&W::txbx_content())).is_empty())
}));
let mut scan = FieldScan {
flow_pics,
..FieldScan::default()
};
let mut line = Vec::new();
collect_hf_rec(dom, para, &prun, sheet, &mut scan, &mut line);
if let Some((pkg, part)) = media {
let mut images = collect_images(pkg, part, dom, para, &|drawing| {
picture_run_style(dom, drawing, &prun, &sheet.theme, &sheet.by_id)
});
bind_flow_pictures(&mut line, &mut images);
}
let spliced = line.iter().any(|r| r.pic.is_some());
let spans_margins = dom
.descendants(para, Some(&W::name("ptab")))
.into_iter()
.any(|n| {
attr_any(dom, n, "relativeTo") == Some("margin")
&& matches!(attr_any(dom, n, "alignment"), Some("right" | "center"))
});
let frame_align = dom
.element(para, &W::p_pr())
.and_then(|ppr| first_named(dom, ppr, "framePr"))
.and_then(|fp| attr_any(dom, fp, "xAlign"))
.filter(|_| !spans_margins);
if matches!(frame_align, Some("right" | "center")) && !line.is_empty() {
for mut run in line {
run.frame_right = frame_align == Some("right");
run.frame_center = frame_align == Some("center");
framed.push(run);
}
continue;
}
if !framed.is_empty()
&& line
.iter()
.all(|r| r.text.trim().is_empty() && matches!(r.field, FieldKind::None))
{
line = std::mem::take(&mut framed);
}
let border = hf_border_pad(last.as_deref(), &pstyle)
+ last
.as_deref()
.map_or(0.0, |l| hf_bottom_pad(l, Some(&pstyle)));
if line
.iter()
.all(|r| r.text.trim().is_empty() && matches!(r.field, FieldKind::None))
{
if hf_para_is_bare_line(dom, node, para) {
let mut first = empty_break(para, &pstyle, &prun);
if !runs.is_empty()
&& let Some(p) = last.as_deref()
{
first.para_gap = contextual_gap(p, &pstyle, p.after, pstyle.before);
} else if let Some(p) = last.as_deref() {
let before = if same_contextual_pair(p, &pstyle) {
0.0
} else {
(pstyle.before - p.after).max(0.0)
};
first.para_gap = before + pstyle.after;
} else if header && pstyle.before_auto {
first.para_gap = pstyle.after;
}
first.para_gap += border;
pending.push(first);
for r in line.iter().filter(|r| r.text.contains('\n')) {
for _ in 0..r.text.matches('\n').count() {
let mut extra = empty_break(para, &pstyle, &prun);
extra.style = r.style.clone();
extra.para_gap = 0.0;
pending.push(extra);
}
}
last = Some(pstyle);
}
continue;
}
let trails = !spliced && hf_pic_trails_line(dom, para, sheet, text_w);
let pic_h = if spliced {
let bound: HashSet<_> = line
.iter()
.filter_map(|r| r.pic.as_ref().map(|p| p.src))
.collect();
hf_unbound_inline_pic_h(dom, para, &bound)
} else if trails || hf_pic_owns_line(dom, para, sheet, text_w) {
0.0
} else {
hf_inline_pic_h(dom, para)
};
if trails {
let mark = hf_flat_pic_run(dom, para, &prun, &sheet.theme).unwrap_or_else(|| {
line.last()
.map_or_else(|| prun.clone(), |r| r.style.clone())
});
let mut pic_line = TextRun::new("", mark.clone());
pic_line.hf_pic_h = hf_inline_pic_h(dom, para);
pic_line.ends_line = true;
line.push(TextRun::new(HF_LINE_BREAK, mark));
line.push(pic_line);
}
for run in &mut line {
run.hf_para = Some(pstyle.clone());
if !trails {
run.hf_pic_h = pic_h;
}
run.hf_tab_wrap = !sheet.defaults.legacy_compat;
run.hf_default_tab = sheet.defaults.page.default_tab;
}
if runs.is_empty() {
if let Some(br) = pending.last_mut()
&& let Some(p) = last.as_deref()
{
br.para_gap += (pstyle.before - p.after).max(0.0) + border;
}
runs.append(&mut pending);
} else {
for br in pending.drain(..) {
let mut mark = TextRun::new("", br.style.clone());
mark.hf_para = br.hf_para.clone();
mark.ends_line = true;
runs.push(br);
runs.push(mark);
}
let mut br = TextRun::new(HF_LINE_BREAK, prun.clone());
br.para_gap = match last.as_deref() {
Some(p) => contextual_gap(p, &pstyle, p.after, pstyle.before),
None => pstyle.before,
} + border;
runs.push(br);
}
runs.extend(line);
if !framed.is_empty() {
for mut run in framed.drain(..) {
run.hf_para = Some(pstyle.clone());
runs.push(run);
}
}
last = Some(pstyle);
}
runs.append(&mut pending);
runs.append(&mut framed);
runs
}
pub(super) fn hf_box_x(page: &PageSetup, para: &ParaStyle) -> (f32, f32) {
let space = |edge: Option<([f32; 3], f32, f32)>| edge.map_or(0.0, |(_, _, s)| s);
(
page.margin_l + para.indent_left - 1.44 - space(para.border_left),
page.width - page.margin_r - para.indent_right + 1.44 + space(para.border_right),
)
}
pub(super) fn hf_bottom_pad(para: &ParaStyle, next: Option<&ParaStyle>) -> f32 {
match para.border_bottom {
Some(edge) if next.is_none_or(|n| n.border_bottom != Some(edge)) => edge.1 + edge.2,
_ => 0.0,
}
}
pub(super) fn hf_border_pad(above: Option<&ParaStyle>, para: &ParaStyle) -> f32 {
match para.border_top {
Some(edge) if above.is_none_or(|a| a.border_top != Some(edge)) => edge.1 + edge.2,
_ => 0.0,
}
}
pub(super) fn hf_inline_pic_h(dom: &Dom, para: NodeId) -> f32 {
hf_unbound_inline_pic_h(dom, para, &HashSet::new())
}
fn hf_unbound_inline_pic_h(dom: &Dom, para: NodeId, bound: &HashSet<u32>) -> f32 {
dom.descendants(para, Some(&WP::inline()))
.into_iter()
.filter(|inl| dom.ancestors(*inl, Some(&W::txbx_content())).is_empty())
.filter(|inl| {
let blips = dom.descendants(*inl, Some(&A::name("blip")));
blips.is_empty() || blips.iter().any(|b| !bound.contains(&b.0))
})
.filter_map(|inl| first_named_any(dom, inl, "extent"))
.filter_map(|ext| attr_any(dom, ext, "cy").and_then(parse_finite_f32))
.map(|cy| cy / 12700.0)
.fold(0.0_f32, f32::max)
}
fn hf_flat_pic_run(
dom: &Dom,
para: NodeId,
prun: &RunStyle,
theme: &ThemeFonts,
) -> Option<RunStyle> {
if hf_inline_pic_h(dom, para) >= 0.5 {
return None;
}
let inl = dom
.descendants(para, Some(&WP::inline()))
.into_iter()
.find(|inl| dom.ancestors(*inl, Some(&W::txbx_content())).is_empty())?;
let mut run = prun.clone();
if let Some(rpr) = dom
.ancestors(inl, Some(&W::r()))
.first()
.and_then(|r| dom.element(*r, &W::r_pr()))
{
apply_rpr(dom, rpr, &mut run, theme);
}
Some(run)
}
pub(super) fn hf_pic_owns_line(dom: &Dom, para: NodeId, sheet: &StyleSheet, text_w: f32) -> bool {
let mut pics_w = 0.0_f32;
for n in dom.descendants(para, None) {
if !dom.ancestors(n, Some(&W::txbx_content())).is_empty() {
continue;
}
if (dom.name_is(n, &W::t()) || dom.name_is(n, &W::del_text()))
&& !element_text(dom, n).trim().is_empty()
{
break;
}
if dom.name_is(n, &WP::inline())
&& let Some(cx) = first_named_any(dom, n, "extent")
.and_then(|ext| attr_any(dom, ext, "cx"))
.and_then(parse_finite_f32)
{
pics_w += cx / 12700.0;
}
}
if pics_w <= 0.0 {
return false;
}
let (pstyle, prun) = para_base(dom, para, sheet, None);
let room = text_w - pstyle.indent_left - pstyle.indent_right - pstyle.indent_first;
room - pics_w < prun.size * 0.5
}
pub(super) fn hf_pic_trails_line(dom: &Dom, para: NodeId, sheet: &StyleSheet, text_w: f32) -> bool {
let mut seen_text = false;
let mut text = String::new();
let mut pics_w = 0.0_f32;
let mut pics_h = 0.0_f32;
for n in dom.descendants(para, None) {
if !dom.ancestors(n, Some(&W::txbx_content())).is_empty() {
continue;
}
if (dom.name_is(n, &W::t()) || dom.name_is(n, &W::del_text()))
&& !element_text(dom, n).trim().is_empty()
{
if pics_w > 0.0 {
return false;
}
seen_text = true;
text.push_str(&element_text(dom, n));
} else if seen_text
&& dom.name_is(n, &WP::inline())
&& let Some(ext) = first_named_any(dom, n, "extent")
&& let Some(cx) = attr_any(dom, ext, "cx").and_then(parse_finite_f32)
{
pics_w += cx / 12700.0;
let cy = attr_any(dom, ext, "cy").and_then(parse_finite_f32);
pics_h = pics_h.max(cy.unwrap_or(0.0) / 12700.0);
}
}
if pics_w <= 0.0 {
return false;
}
let (pstyle, prun) = para_base(dom, para, sheet, None);
let room = text_w - pstyle.indent_left - pstyle.indent_right - pstyle.indent_first;
let face = fonts().get(fonts().resolve(&prun.family, prun.bold, prun.italic));
let text_end = face.width_pt(&text, prun.layout_size());
let last_line = if room > 0.0 { text_end % room } else { 0.0 };
room - last_line - pics_w < prun.size * 0.5
}
pub(super) fn hf_para_in_table(dom: &Dom, root: NodeId, para: NodeId) -> bool {
let mut cur = dom.parent(para);
while let Some(id) = cur {
if id == root {
return false;
}
if dom.name_is(id, &W::tbl()) {
return true;
}
cur = dom.parent(id);
}
false
}
pub(super) fn hf_para_is_bare_line(dom: &Dom, root: NodeId, para: NodeId) -> bool {
if dom
.element(para, &W::p_pr())
.is_some_and(|ppr| first_named(dom, ppr, "framePr").is_some())
{
return false;
}
let mut cur = dom.parent(para);
while let Some(id) = cur {
if id == root {
return !dom.descendants(para, None).into_iter().any(|d| {
(dom.name_is(d, &W::pict()) && para_hrule(dom, d).is_none()
|| (dom.name_is(d, &W::drawing())
&& dom
.descendants(d, Some(&WP::inline()))
.into_iter()
.any(|i| !inside_text_box(dom, i, d))))
&& !dom.ancestors(d, None).iter().any(|a| {
local_name_is(dom, *a, "Fallback") || local_name_is(dom, *a, "txbxContent")
})
});
}
if !(dom.name_is(id, &W::sdt()) || dom.name_is(id, &W::sdt_content())) {
return false;
}
cur = dom.parent(id);
}
false
}
pub(super) fn hf_styled_lines(
fonts: &Fonts,
runs: &[TextRun],
width: f32,
) -> Vec<(Vec<TextRun>, f32)> {
let mut out = Vec::new();
for (line, gap) in hf_paragraph_lines(runs) {
let (first, room) =
line.iter()
.find_map(|r| r.hf_para.as_deref())
.map_or((width, width), |p| {
let rest = width - p.indent_left - p.indent_right;
(rest - p.indent_first, rest)
});
let pieces = if line.iter().any(|r| r.text.contains('\t')) {
let cut = hf_tab_line_breaks(fonts, &line, width);
let cut = if cut.is_empty() {
vec![line.clone()]
} else {
cut
};
hf_wrap_tab_pieces(fonts, cut, width)
} else {
wrap_runs(fonts, &line, first, room, false)
};
if pieces.len() < 2 {
out.push((line, gap));
continue;
}
let last = pieces.len() - 1;
out.extend(pieces.into_iter().enumerate().map(|(i, mut piece)| {
if i > 0 {
for run in &mut piece {
run.hf_pic_h = 0.0;
}
if let Some(head) = piece.first_mut() {
head.hf_cont = true;
}
}
(piece, if i == last { gap } else { 0.0 })
}));
}
out
}
fn hf_line_chars(fonts: &Fonts, line: &[TextRun]) -> Vec<(usize, char, f32)> {
line.iter()
.enumerate()
.flat_map(|(ri, run)| {
let face = fonts.get(ink_face(fonts, &run.style, &run.text));
let size = run.style.layout_size();
run.text.chars().map(move |ch| {
let w = if let Some(pic) = &run.pic {
pic.w.max(1.0)
} else if ch == '\t' {
0.0
} else {
face.width_pt(ch.encode_utf8(&mut [0; 4]), size) * run.style.hscale()
};
(ri, ch, w)
})
})
.collect()
}
pub(super) fn hf_tab_piece_end(
fonts: &Fonts,
piece: &[TextRun],
para: &ParaStyle,
start: f32,
) -> f32 {
let chars = hf_line_chars(fonts, piece);
let mut pos = start;
let mut k = 0;
while k < chars.len() {
let (_, ch, w) = chars[k];
k += 1;
if ch != '\t' {
pos += w;
continue;
}
let seg_n = chars[k..].iter().take_while(|(_, c, _)| *c != '\t').count();
let seg: f32 = chars[k..k + seg_n].iter().map(|(_, _, w)| w).sum();
pos = match para.tab_stops.iter().find(|t| t.pos > pos + 0.01) {
Some(t) => match t.align {
TabAlign::Right => t.pos.max(pos + seg),
TabAlign::Center => (t.pos + seg * 0.5).max(pos + seg),
_ => t.pos + seg,
},
None => pos + seg,
};
k += seg_n;
}
pos - trailing_ws_pt(fonts, piece)
}
fn hf_wrap_tab_pieces(fonts: &Fonts, pieces: Vec<Vec<TextRun>>, width: f32) -> Vec<Vec<TextRun>> {
let mut out = Vec::new();
for (i, piece) in pieces.into_iter().enumerate() {
let Some(para) = piece.iter().find_map(|r| r.hf_para.clone()) else {
out.push(piece);
continue;
};
if para.tab_stops.iter().any(|t| t.pos < 0.0) {
out.push(piece);
continue;
}
let first_start = if i == 0 {
para.indent_left + para.indent_first
} else {
para.indent_left
};
let default_tab = piece.first().map_or(0.0, |r| r.hf_default_tab);
let tabs = WrapTabs {
stops: ¶.tab_stops,
default_tab,
first_start,
start: para.indent_left,
squeeze: Squeeze::NONE,
};
let right = width - para.indent_right;
let room = para
.tab_stops
.iter()
.filter(|t| t.align == TabAlign::Right)
.fold(right, |room, t| room.max(t.pos));
if hf_tab_piece_end(fonts, &piece, ¶, first_start) <= room + 0.5 {
out.push(piece);
continue;
}
let (lines, _) = wrap_runs_tabbed(
fonts,
&piece,
right - first_start,
right - para.indent_left,
false,
Some(&tabs),
);
if lines.len() < 2 {
out.push(piece);
} else {
out.extend(lines);
}
}
out
}
pub(super) fn hf_tab_line_breaks(fonts: &Fonts, line: &[TextRun], width: f32) -> Vec<Vec<TextRun>> {
let Some(para) = line.iter().find_map(|r| r.hf_para.clone()) else {
return Vec::new();
};
if para.tab_stops.iter().any(|t| t.pos < 0.0) {
return Vec::new();
}
let right = width - para.indent_right;
let wrap15 = line.iter().any(|r| r.hf_tab_wrap);
let chars = hf_line_chars(fonts, line);
let seg_after = |k: usize| -> f32 {
chars[k + 1..]
.iter()
.take_while(|(_, ch, _)| *ch != '\t')
.map(|(_, _, w)| w)
.sum()
};
let stop_after = |from: f32| para.tab_stops.iter().find(|t| t.pos > from + 0.01);
let mut cuts = Vec::new();
let mut pos = hf_line_indent(¶, line);
let mut k = 0;
while k < chars.len() {
let (_, ch, w) = chars[k];
if ch != '\t' {
pos += w;
k += 1;
continue;
}
let seg = seg_after(k);
let end = match stop_after(pos) {
Some(t) => match t.align {
TabAlign::Right => t.pos.max(pos + seg),
TabAlign::Center => (t.pos + seg * 0.5).max(pos + seg),
_ => t.pos + seg,
},
None if pos > para.indent_left + 0.5
&& (pos + seg > right + 0.5 || wrap15 && pos >= right - 0.5) =>
{
let mut at = k;
while wrap15 && seg <= 0.01 && at > 0 && !matches!(chars[at - 1].1, '\t' | ' ') {
at -= 1;
}
if at == 0 || chars[at - 1].1 == '\t' || cuts.contains(&at) {
at = k;
}
cuts.push(at);
pos = para.indent_left;
k = at;
continue;
}
None => pos + seg,
};
pos = end;
k += 1 + chars[k + 1..]
.iter()
.take_while(|(_, c, _)| *c != '\t')
.count();
}
if cuts.is_empty() {
return Vec::new();
}
let mut pieces: Vec<Vec<TextRun>> = vec![Vec::new()];
let mut buf = String::new();
let mut buf_run: Option<usize> = None;
let flush = |pieces: &mut Vec<Vec<TextRun>>, buf: &mut String, run: Option<usize>| {
if let (Some(ri), false) = (run, buf.is_empty())
&& let Some(piece) = pieces.last_mut()
{
piece.push(line[ri].with_text(std::mem::take(buf)));
}
};
for (idx, &(ri, ch, _)) in chars.iter().enumerate() {
if buf_run != Some(ri) || cuts.contains(&idx) {
flush(&mut pieces, &mut buf, buf_run);
if cuts.contains(&idx) {
pieces.push(Vec::new());
}
buf_run = Some(ri);
}
buf.push(ch);
}
flush(&mut pieces, &mut buf, buf_run);
pieces.retain(|p| !p.is_empty());
pieces
}
pub(super) fn hf_line_indent(para: &ParaStyle, line: &[TextRun]) -> f32 {
let first = !line.first().is_some_and(|r| r.hf_cont);
para.indent_left + if first { para.indent_first } else { 0.0 }
}
fn hf_paragraph_lines(runs: &[TextRun]) -> Vec<(Vec<TextRun>, f32)> {
let mut lines = vec![(Vec::new(), 0.0)];
for run in runs {
if run.text == HF_LINE_BREAK {
if let Some(last) = lines.last_mut() {
last.1 = run.para_gap;
}
lines.push((Vec::new(), 0.0));
} else if let Some(line) = lines.last_mut() {
line.0.push(run.clone());
}
}
lines.retain(|(line, _)| {
line.iter().any(|r| {
!r.text.trim().is_empty() || r.ends_line || !matches!(r.field, FieldKind::None)
})
});
lines
}
#[derive(Default)]
pub(super) struct FieldScan {
flow_pics: bool,
pub(super) kind: Option<FieldKind>,
pub(super) result: bool,
pub(super) emitted: bool,
instr: String,
styleref: Option<std::rc::Rc<StyleRef>>,
}
fn left_ptab(dom: &Dom, node: NodeId) -> bool {
dom.name_is(node, &W::name("ptab"))
&& matches!(attr_any(dom, node, "alignment"), None | Some("left"))
}
fn line_has_content(runs: &[TextRun]) -> bool {
for r in runs.iter().rev() {
if let Some((_, tail)) = r.text.rsplit_once('\n') {
return !tail.trim().is_empty();
}
if !r.text.trim().is_empty() || r.field != FieldKind::None {
return true;
}
}
false
}
pub(super) fn collect_hf_rec(
dom: &Dom,
node: NodeId,
base: &RunStyle,
sheet: &StyleSheet,
scan: &mut FieldScan,
runs: &mut Vec<TextRun>,
) {
collect_hf_rev(dom, node, base, sheet, scan, runs, RevMark::None);
}
pub(super) fn collect_hf_rev(
dom: &Dom,
node: NodeId,
base: &RunStyle,
sheet: &StyleSheet,
scan: &mut FieldScan,
runs: &mut Vec<TextRun>,
mark: RevMark,
) {
let start = runs.len();
if dom.name_is(node, &W::fld_simple()) {
let outer = std::mem::take(scan);
scan.flow_pics = outer.flow_pics;
let instr = attr_any(dom, node, "instr").unwrap_or("");
scan.result = true;
scan.kind = numeric_field_kind(instr);
scan.styleref = styleref_target(instr, Some(&sheet.by_id)).map(std::rc::Rc::new);
for idx in 0..dom.child_count(node) {
collect_hf_rev(dom, dom.child_at(node, idx), base, sheet, scan, runs, mark);
}
if !scan.emitted
&& let Some(kind) = scan.kind
{
let mut run = field_run(String::new(), base);
run.field = kind;
runs.push(run);
}
*scan = outer;
} else {
collect_hf_rev_runs(dom, node, base, sheet, scan, runs, mark);
}
for run in &mut runs[start..] {
run.rev |= mark != RevMark::None;
if scan.result && run.styleref.is_none() {
run.styleref.clone_from(&scan.styleref);
}
}
}
fn numeric_field_kind(instr: &str) -> Option<FieldKind> {
match field_first_token(instr).to_ascii_uppercase().as_str() {
"NUMPAGES" => Some(FieldKind::NumPages),
"NUMWORDS" => Some(FieldKind::NumWords),
"PAGE" => Some(FieldKind::Page),
_ => None,
}
}
fn collect_hf_rev_runs(
dom: &Dom,
node: NodeId,
base: &RunStyle,
sheet: &StyleSheet,
scan: &mut FieldScan,
runs: &mut Vec<TextRun>,
mark: RevMark,
) {
let theme = &sheet.theme;
if dom.name_is(node, &W::instr_text()) || dom.name_is(node, &W::del_instr_text()) {
let raw = element_text(dom, node);
scan.instr.push_str(&raw);
scan.kind = numeric_field_kind(&scan.instr);
return;
}
if skip_non_text(dom, node) {
return;
}
if dom.name_is(node, &W::fld_char()) {
match attr_any(dom, node, "fldCharType").unwrap_or("") {
"begin" => {
*scan = FieldScan {
flow_pics: scan.flow_pics,
..FieldScan::default()
};
}
"separate" => {
scan.result = true;
scan.styleref =
styleref_target(&scan.instr, Some(&sheet.by_id)).map(std::rc::Rc::new);
}
"end" => {
if !scan.emitted
&& let Some(kind) = scan.kind
{
let mut run = field_run(String::new(), base);
run.field = kind;
runs.push(run);
} else if !scan.emitted && is_datetime_field(&scan.instr) {
runs.push(field_run(datetime_field_text(&scan.instr), base));
}
*scan = FieldScan {
flow_pics: scan.flow_pics,
..FieldScan::default()
};
}
_ => {}
}
return;
}
if dom.name_is(node, &W::r()) {
let mut fieldish = false;
for i in 0..dom.child_count(node) {
let c = dom.child_at(node, i);
if dom.name_is(c, &W::fld_char())
|| dom.name_is(c, &W::instr_text())
|| dom.name_is(c, &W::del_instr_text())
{
fieldish = true;
break;
}
}
let mut style = base.clone();
if let Some(rpr) = dom.element(node, &W::r_pr()) {
if let Some(named) = first_named(dom, rpr, "rStyle")
.and_then(|n| dom.attribute(n, &W::val()))
.and_then(|sid| sheet.by_id.get(sid))
{
apply_named_char_style(&mut style, named);
}
apply_rpr(dom, rpr, &mut style, theme);
}
let unmarked = style.clone();
apply_rev(&mut style, mark, HF_REV_COLOR);
let mut number = style.clone();
if mark != RevMark::None {
unmark_field_number(&mut number, &unmarked, mark);
}
if fieldish {
let before = runs.len();
for i in 0..dom.child_count(node) {
collect_hf_rev(dom, dom.child_at(node, i), &style, sheet, scan, runs, mark);
}
for run in &mut runs[before..] {
if run.field != FieldKind::None {
run.style = number.clone();
}
}
return;
}
if scan.result
&& let Some(kind) = scan.kind
{
let text = visible_text(dom, node, mark, false);
if !text.is_empty() {
let mut run = field_run(text, &number);
run.field = kind;
runs.push(run);
scan.emitted = true;
}
return;
}
if (0..dom.child_count(node)).any(|i| left_ptab(dom, dom.child_at(node, i))) {
let preserve = run_preserves_space(dom, node);
for i in 0..dom.child_count(node) {
let kid = dom.child_at(node, i);
if !dom.is_element(kid) || dom.name_is(kid, &W::r_pr()) {
continue;
}
if left_ptab(dom, kid) {
if line_has_content(runs) {
runs.push(TextRun::new("\n", style.clone()));
}
continue;
}
let text = visible_text(dom, kid, mark, preserve);
if !text.is_empty() {
runs.push(TextRun::new(text, style.clone()));
if scan.result {
scan.emitted = true;
}
}
}
return;
}
if scan.flow_pics
&& !matches!(mark, RevMark::Del | RevMark::MoveFrom)
&& let Some(pieces) = flow_run_pieces(dom, node, BreakText::Line)
{
for piece in pieces {
let mut run = match piece {
FlowPiece::Pic(src) => {
let mut run = TextRun::new(INLINE_PIC, style.clone());
run.pic_src = Some(src);
run
}
FlowPiece::Text(raw) => TextRun::new(
rev_text(&raw, mark, run_preserves_space(dom, node)),
style.clone(),
),
};
if run.text.is_empty() {
continue;
}
if run.text == "\n" && !line_has_content(runs) {
let mut blank = TextRun::new("", style.clone());
blank.ends_line = true;
blank.rev = mark != RevMark::None;
runs.push(blank);
}
run.rev = mark != RevMark::None;
runs.push(run);
if scan.result {
scan.emitted = true;
}
}
return;
}
let text = visible_text(dom, node, mark, run_preserves_space(dom, node));
if !text.is_empty() {
if text == "\n" && !line_has_content(runs) {
let mut blank = TextRun::new("", style.clone());
blank.ends_line = true;
runs.push(blank);
}
runs.push(TextRun::new(text, style));
if scan.result {
scan.emitted = true;
}
}
return;
}
let mark = rev_mark_of(dom, node).unwrap_or(mark);
for idx in 0..dom.child_count(node) {
collect_hf_rev(dom, dom.child_at(node, idx), base, sheet, scan, runs, mark);
}
}
pub(super) fn chrome_size(runs: &[TextRun]) -> f32 {
let inked = runs
.iter()
.filter(|r| !r.text.trim().is_empty())
.map(|r| r.style.size)
.fold(0.0_f32, f32::max);
let text = runs
.iter()
.filter(|r| r.text != HF_LINE_BREAK)
.map(|r| r.style.size)
.fold(0.0_f32, f32::max);
if inked > 0.0 {
return inked;
}
let any = runs.iter().map(|r| r.style.size).fold(0.0_f32, f32::max);
if text > 0.0 {
text
} else if any > 0.0 {
any
} else {
11.0
}
}
fn chrome_one_line_pt(fonts: &Fonts, runs: &[TextRun]) -> f32 {
let size = chrome_size(runs);
let fid = runs
.iter()
.find(|r| !r.text.trim().is_empty())
.or_else(|| runs.iter().find(|r| r.text != HF_LINE_BREAK))
.map_or(FaceId::CarlitoRegular.into(), |r| {
fonts.resolve(&r.style.family, r.style.bold, r.style.italic)
});
fonts.get(fid).single_line_pt(size).max(size)
}
pub(super) fn chrome_images_h(fonts: &Fonts, images: &[LaidImage]) -> f32 {
images
.iter()
.filter(|img| img.chrome_flow)
.map(|img| chrome_pic_line(fonts, img))
.fold(0.0_f32, f32::max)
}
pub(super) fn chrome_pic_line(fonts: &Fonts, img: &LaidImage) -> f32 {
let mult = 1.0 + img.chrome_leading.as_ref().map_or(0.0, |(extra, _)| *extra);
chrome_pic_top(fonts, img)
+ img.chrome_after
+ chrome_pic_h(fonts, img)
+ chrome_pic_descent(fonts, img)
+ img.chrome_leading.as_ref().map_or(0.0, |(extra, mark)| {
let face = fonts.get(fonts.resolve(&mark.family, mark.bold, mark.italic));
extra * face.single_line_pt(mark.size)
})
+ img.chrome_tab_line.as_ref().map_or(0.0, |mark| {
let face = fonts.get(fonts.resolve(&mark.family, mark.bold, mark.italic));
face.line_descent_pt(mark.size) + mult * face.single_line_pt(mark.size)
})
}
pub(super) fn chrome_pic_h(fonts: &Fonts, img: &LaidImage) -> f32 {
img.chrome_flat_run.as_ref().map_or(img.h, |run| {
let face = fonts.get(fonts.resolve(&run.family, run.bold, run.italic));
img.h.max(face.single_line_pt(run.size))
})
}
pub(super) fn chrome_pic_descent(fonts: &Fonts, img: &LaidImage) -> f32 {
img.chrome_text_under.as_ref().map_or(0.0, |mark| {
let face = fonts.get(fonts.resolve(&mark.family, mark.bold, mark.italic));
face.line_descent_pt(mark.size)
})
}
pub(super) fn chrome_pic_top(fonts: &Fonts, img: &LaidImage) -> f32 {
let tab = img.chrome_drop_tab.as_ref().map_or(0.0, |(mark, tab)| {
let face = fonts.get(fonts.resolve(&mark.family, mark.bold, mark.italic));
face.line_descent_pt(mark.size)
+ if *tab {
face.single_line_pt(mark.size)
} else {
0.0
}
});
img.chrome_drop + tab
}
pub(super) fn chrome_band(
fonts: &Fonts,
runs: &[TextRun],
tables: &[ChromeTable],
avail: f32,
) -> f32 {
let lines = if runs.is_empty() {
0.0
} else {
chrome_line_pt(fonts, runs, avail)
};
lines + chrome_tables_h(fonts, tables, avail, None)
}
pub(super) fn chrome_tables_h(
fonts: &Fonts,
tables: &[ChromeTable],
avail: f32,
before: Option<bool>,
) -> f32 {
tables
.iter()
.filter(|t| before.is_none_or(|b| t.before_text == b))
.filter_map(|t| t.block.as_deref())
.map(|b| table_rows_height(fonts, b, avail))
.sum()
}
fn table_rows_height(fonts: &Fonts, block: &Block, avail: f32) -> f32 {
let Block::Table {
cols, rows, geom, ..
} = block
else {
return 0.0;
};
let col_w = resolved_col_widths(fonts, cols, rows, geom, avail);
table_row_heights(fonts, rows, &col_w, geom).iter().sum()
}
pub(super) fn chrome_line_metrics(fonts: &Fonts, line: &[TextRun]) -> (f32, f32) {
let size = chrome_size(line);
let fid = line
.iter()
.find(|r| !r.text.trim().is_empty())
.or_else(|| line.iter().find(|r| r.text != HF_LINE_BREAK))
.map_or(FaceId::CarlitoRegular.into(), |r| {
fonts.resolve(&r.style.family, r.style.bold, r.style.italic)
});
let face = fonts.get(fid);
let para = line.iter().find_map(|r| r.hf_para.as_deref());
let line_box = para.map_or_else(
|| chrome_one_line_pt(fonts, line),
|p| para_line_box(face, size, p),
);
let ascent = face.ascent_pt(size);
let drop = if para.is_some_and(|p| p.line_exact.is_some()) {
exact_baseline(line_box)
} else {
ascent
};
let pic = line
.iter()
.map(|r| r.hf_pic_h.max(r.pic.as_ref().map_or(0.0, |p| p.h)))
.fold(0.0_f32, f32::max);
if pic > drop && para.is_none_or(|p| p.line_exact.is_none()) {
return (pic, line_box + pic - drop);
}
(drop, line_box)
}
pub(super) fn split_at_breaks(runs: &[TextRun]) -> Vec<Vec<TextRun>> {
let mut lines = vec![Vec::new()];
for run in runs {
let mut pieces = run.text.split('\n');
if let Some(first) = pieces.next()
&& let Some(line) = lines.last_mut()
{
line.push(run.with_text(first));
}
for piece in pieces {
lines.push(vec![run.with_text(piece)]);
}
}
lines
}
pub(super) fn rejoin_lines(lines: &[Vec<TextRun>]) -> Vec<TextRun> {
let mut runs = Vec::new();
for (j, line) in lines.iter().enumerate() {
for (r, run) in line.iter().enumerate() {
if r + 1 == line.len() && run.ends_line && run.text.is_empty() {
if j + 1 < lines.len() {
let mut br = run.with_text("\n");
br.ends_line = false;
runs.push(br);
}
} else {
runs.push(run.clone());
}
}
}
runs
}
pub(super) fn ended_by_breaks(lines: &[Vec<TextRun>]) -> Vec<bool> {
lines
.iter()
.map(|l| l.last().is_some_and(|r| r.ends_line && r.text.is_empty()))
.collect()
}
pub(super) fn exact_baseline(line_box: f32) -> f32 {
line_box * 0.8
}
pub(super) fn chrome_line_pt(fonts: &Fonts, runs: &[TextRun], width: f32) -> f32 {
let lines = hf_styled_lines(fonts, runs, width);
let heights: f32 = lines
.iter()
.map(|(line, _)| chrome_line_metrics(fonts, line).1)
.sum();
let gaps: f32 = lines
.iter()
.take(lines.len().saturating_sub(1))
.map(|(_, gap)| *gap)
.sum();
let (lead, trail) = chrome_empty_pads(fonts, runs);
if runs.iter().all(|r| r.text == HF_LINE_BREAK) {
return lead;
}
let lines_h = if lines.is_empty() {
chrome_one_line_pt(fonts, runs)
} else {
heights
};
lines_h + gaps + lead + trail + hf_opening_pad(runs) + hf_closing_after(runs)
}
pub(super) fn hf_closing_after(runs: &[TextRun]) -> f32 {
runs.last()
.and_then(|r| r.hf_para.as_deref())
.map_or(0.0, |p| p.after + hf_bottom_pad(p, None))
}
pub(super) fn hf_opening_pad(runs: &[TextRun]) -> f32 {
runs.first()
.filter(|r| r.text != HF_LINE_BREAK)
.and_then(|r| r.hf_para.as_deref())
.map_or(0.0, |p| {
hf_border_pad(None, p) + if p.before_auto { 0.0 } else { p.before }
})
}
pub(super) fn hf_break_box(fonts: &Fonts, r: &TextRun) -> f32 {
let face = fonts.get(fonts.resolve(&r.style.family, r.style.bold, r.style.italic));
let natural = face.single_line_pt(r.style.size).max(r.style.size);
r.hf_para
.as_deref()
.map_or(natural, |p| para_line_box(face, r.style.size, p))
}
pub(super) fn chrome_empty_pads(fonts: &Fonts, runs: &[TextRun]) -> (f32, f32) {
let line = |r: &TextRun| hf_break_box(fonts, r) + r.para_gap;
let lead: f32 = runs
.iter()
.take_while(|r| r.text == HF_LINE_BREAK)
.map(line)
.sum();
if runs.iter().all(|r| r.text == HF_LINE_BREAK) {
return (lead, 0.0);
}
let trail: f32 = runs
.iter()
.rev()
.take_while(|r| r.text == HF_LINE_BREAK)
.map(line)
.sum();
(lead, trail)
}
pub(super) fn chrome_line_units(fonts: &Fonts, runs: &[TextRun], width: f32) -> Vec<f32> {
let empty = |r: &TextRun| hf_break_box(fonts, r) + r.para_gap;
if runs.iter().all(|r| r.text == HF_LINE_BREAK) {
return runs.iter().map(empty).collect();
}
let lead = runs.iter().take_while(|r| r.text == HF_LINE_BREAK).count();
let trail = runs
.iter()
.rev()
.take_while(|r| r.text == HF_LINE_BREAK)
.count();
let mut units: Vec<f32> = runs[..lead].iter().map(empty).collect();
let lines = hf_styled_lines(fonts, runs, width);
let first_text = units.len();
if lines.is_empty() {
units.push(chrome_one_line_pt(fonts, runs));
}
for (i, (line, gap)) in lines.iter().enumerate() {
let tail = if i + 1 < lines.len() { *gap } else { 0.0 };
units.push(chrome_line_metrics(fonts, line).1 + tail);
}
units[first_text] += hf_opening_pad(runs);
units.extend(runs[runs.len() - trail..].iter().map(empty));
if let Some(last) = units.last_mut() {
*last += hf_closing_after(runs);
}
units
}