use super::patterns::*;
use super::*;
use crate::analysis::blocks::TextBlock;
use crate::analysis::page::{DocStats, PageData};
use crate::backend::FontInfo;
use crate::common::geom::Rect;
use crate::model::Line;
use crate::model::Loc;
use std::collections::{HashMap, HashSet};
const AUTHOR_ZONE: usize = usize::MAX / 2;
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct FrontMatterConfig {
pub title_max_top_ratio: f32,
pub title_min_chars_count: usize,
pub title_min_size_above_body_pt: f32,
pub title_size_tolerance_pt: f32,
pub section_bold_min_ratio: f32,
pub section_min_size_above_body_pt: f32,
pub section_min_upper_ratio: f32,
pub section_max_chars_count: usize,
pub prose_line_min_chars_count: usize,
pub prose_right_tolerance_pt: f32,
pub prose_left_tolerance_em: f32,
pub prose_min_lines_count: usize,
pub prose_min_full_lines_count: usize,
pub sentence_min_words_count: usize,
pub abstract_max_page_offset_count: usize,
pub abstract_heading_max_chars_count: usize,
pub abstract_max_blocks_count: usize,
pub author_stop_margin_pt: f32,
pub author_title_margin_pt: f32,
pub card_max_gap_em: f32,
pub card_same_row_tolerance_pt: f32,
pub card_min_x_overlap_ratio: f32,
pub card_row_min_y_overlap_ratio: f32,
pub author_zone_max_others_count: usize,
}
impl Default for FrontMatterConfig {
fn default() -> Self {
Self {
title_max_top_ratio: 0.65,
title_min_chars_count: 4,
title_min_size_above_body_pt: 1.4,
title_size_tolerance_pt: 0.6,
section_bold_min_ratio: 0.6,
section_min_size_above_body_pt: 0.5,
section_min_upper_ratio: 0.8,
section_max_chars_count: 80,
prose_line_min_chars_count: 40,
prose_right_tolerance_pt: 1.5,
prose_left_tolerance_em: 2.0,
prose_min_lines_count: 4,
prose_min_full_lines_count: 3,
sentence_min_words_count: 12,
abstract_max_page_offset_count: 1,
abstract_heading_max_chars_count: 80,
abstract_max_blocks_count: 3,
author_stop_margin_pt: 1.0,
author_title_margin_pt: 2.0,
card_max_gap_em: 0.9,
card_same_row_tolerance_pt: 2.0,
card_min_x_overlap_ratio: 0.5,
card_row_min_y_overlap_ratio: 0.4,
author_zone_max_others_count: 3,
}
}
}
struct AuthorCard {
bbox: Rect,
lines: Vec<Line>,
}
struct AuthorEnd {
end: usize,
stopped_by_prose: bool,
}
pub(crate) fn classify_front_matter(
nodes: &mut Vec<Node>,
pages: &[PageData],
stats: &DocStats,
fonts: &[FontInfo],
cfg: &SemanticsConfig,
) {
let front = &cfg.front_matter;
let Some(first_page) = nodes
.iter()
.find(|node| node.text_block().is_some())
.map(|node| node.loc.page)
else {
return;
};
let page_h = pages[first_page].height;
let Some((title_start, title_end)) = find_title(nodes, first_page, page_h, stats, front) else {
return;
};
for node in nodes[..title_start]
.iter_mut()
.filter(|node| node.loc.page == first_page)
{
if node.text_block().is_some() {
node.class = Class::Note;
}
}
for node in &mut nodes[title_start..=title_end] {
node.class = Class::Title;
}
let author_end = find_author_end(nodes, title_end, first_page, stats, fonts, cfg);
let end = author_end.end;
if author_end.stopped_by_prose {
mark_unheaded_abstract(nodes, end, first_page, front);
}
let author_idx = author_indices(nodes, (title_start, title_end), end, first_page, front);
if author_idx.is_empty() {
return;
}
let author_zones: HashSet<usize> = author_idx.iter().map(|&idx| nodes[idx].loc.zone).collect();
replace_author_nodes(nodes, &author_idx, front);
flatten_author_zones(nodes, &author_zones, first_page, front);
}
fn find_title(
nodes: &[Node],
first_page: usize,
page_h: f32,
stats: &DocStats,
cfg: &FrontMatterConfig,
) -> Option<(usize, usize)> {
let on_first = |node: &&Node| node.loc.page == first_page;
let mut title_size = 0.0f32;
for node in nodes.iter().filter(on_first) {
if let Some(block) = node.text_block()
&& block.bbox.y0 < cfg.title_max_top_ratio * page_h
&& block_text(&block.lines).chars().count() >= cfg.title_min_chars_count
{
title_size = title_size.max(block_size(&block.lines));
}
}
let mut title_range: Option<(usize, usize)> = None;
if title_size >= stats.body_size + cfg.title_min_size_above_body_pt {
for (idx, node) in nodes.iter().enumerate() {
if !on_first(&node) {
continue;
}
let is_title = node.text_block().is_some_and(|block| {
block_size(&block.lines) >= title_size - cfg.title_size_tolerance_pt
&& block.bbox.y0 < cfg.title_max_top_ratio * page_h
});
match (&mut title_range, is_title) {
(None, true) => title_range = Some((idx, idx)),
(Some((_, range_end)), true) if *range_end + 1 == idx => *range_end = idx,
_ => {}
}
}
}
title_range.filter(|&(start, end)| {
let text: Vec<String> = nodes[start..=end]
.iter()
.filter_map(|node| node.text_block().map(|block| block_text(&block.lines)))
.collect();
!KNOWN_HEADINGS.is_match(text.join(" ").trim())
})
}
fn find_author_end(
nodes: &[Node],
title_end: usize,
first_page: usize,
stats: &DocStats,
fonts: &[FontInfo],
cfg: &SemanticsConfig,
) -> AuthorEnd {
let mut end = title_end + 1;
while end < nodes.len() && nodes[end].loc.page == first_page {
if let Some(block) = nodes[end].text_block() {
let text = block_text(&block.lines);
let trimmed = text.trim();
let is_abstract = is_abstract_start(trimmed);
let is_section = is_section_start(block, trimmed, stats, fonts, cfg);
let prose = is_running_prose(block, &cfg.front_matter);
if is_abstract || is_section || prose {
return AuthorEnd {
end,
stopped_by_prose: prose && !is_abstract && !is_section,
};
}
}
end += 1;
}
AuthorEnd {
end,
stopped_by_prose: false,
}
}
fn is_abstract_start(trimmed: &str) -> bool {
trimmed.eq_ignore_ascii_case("abstract") || ABSTRACT_INLINE.is_match(trimmed)
}
fn is_section_start(
block: &TextBlock,
trimmed: &str,
stats: &DocStats,
fonts: &[FontInfo],
cfg: &SemanticsConfig,
) -> bool {
let front = &cfg.front_matter;
let bold = frac(&block.lines, fonts, |font| font.bold) >= front.section_bold_min_ratio;
(HEADING_NUM.is_match(trimmed) || HEADING_ROMAN.is_match(trimmed) || INTRO_HEADING.is_match(trimmed))
&& (bold
|| block_size(&block.lines) > stats.body_size + front.section_min_size_above_body_pt
|| upper_ratio(trimmed, cfg) > front.section_min_upper_ratio)
&& trimmed.chars().count() < front.section_max_chars_count
}
fn is_running_prose(block: &TextBlock, cfg: &FrontMatterConfig) -> bool {
is_justified_prose(block, cfg) || holds_sentence(&block_text(&block.lines), cfg)
}
fn holds_sentence(text: &str, cfg: &FrontMatterConfig) -> bool {
let words = text.split_whitespace().count();
let inner_stop = text
.split_whitespace()
.rev()
.skip(1)
.any(|word| word.ends_with('.') && word.chars().filter(|ch| ch.is_alphabetic()).count() > 3);
words >= cfg.sentence_min_words_count && inner_stop && !text.contains('@')
}
fn is_justified_prose(block: &TextBlock, cfg: &FrontMatterConfig) -> bool {
let full = block
.lines
.iter()
.filter(|line| {
line.char_count() >= cfg.prose_line_min_chars_count
&& (line.bbox.x1 - block.right).abs() < cfg.prose_right_tolerance_pt
&& (line.bbox.x0 - block.left).abs() < cfg.prose_left_tolerance_em * line.size
})
.count();
block.lines.len() >= cfg.prose_min_lines_count && full >= cfg.prose_min_full_lines_count
}
fn mark_unheaded_abstract(nodes: &mut [Node], start: usize, first_page: usize, cfg: &FrontMatterConfig) {
let last_page = first_page + cfg.abstract_max_page_offset_count;
let next_heading = nodes[start..].iter().position(|node| {
node.loc.page > last_page
|| node.text_block().is_some_and(|block| {
let text = block_text(&block.lines);
let text = text.trim();
text.chars().count() < cfg.abstract_heading_max_chars_count
&& (HEADING_NUM.is_match(text) || HEADING_ROMAN.is_match(text) || KNOWN_HEADINGS.is_match(text))
})
});
let Some(len) = next_heading else { return };
let abstract_nodes = start..start + len;
let texts = nodes[abstract_nodes.clone()]
.iter()
.filter(|node| node.text_block().is_some())
.count();
if nodes[start + len].loc.page <= last_page && texts <= cfg.abstract_max_blocks_count {
for node in nodes[abstract_nodes]
.iter_mut()
.filter(|node| node.text_block().is_some())
{
node.class = Class::Paragraph { in_abstract: true };
}
}
}
fn author_indices(
nodes: &[Node],
(title_start, title_end): (usize, usize),
end: usize,
first_page: usize,
cfg: &FrontMatterConfig,
) -> Vec<usize> {
let title_bottom = nodes[title_start..=title_end]
.iter()
.filter_map(|node| node.text_block())
.map(|block| block.bbox.y1)
.fold(0.0, f32::max);
let stop_y = nodes
.get(end)
.filter(|node| node.loc.page == first_page)
.and_then(|node| node.text_block())
.map(|block| block.bbox.y0)
.unwrap_or(f32::INFINITY);
(title_end + 1..nodes.len())
.filter(|&idx| {
let node = &nodes[idx];
let Some(block) = node.text_block() else { return false };
node.loc.page == first_page
&& node.class == Class::Unknown
&& (idx < end
|| (stop_y.is_finite()
&& !is_running_prose(block, cfg)
&& block.bbox.y1 <= stop_y - cfg.author_stop_margin_pt
&& block.bbox.y0 >= title_bottom - cfg.author_title_margin_pt))
})
.collect()
}
fn replace_author_nodes(nodes: &mut Vec<Node>, author_idx: &[usize], cfg: &FrontMatterConfig) {
let mut lines: Vec<Line> = Vec::new();
for &idx in author_idx {
lines.extend(nodes[idx].text_block().unwrap().lines.iter().cloned());
}
lines.sort_by(|a, b| a.baseline.total_cmp(&b.baseline).then(a.bbox.x0.total_cmp(&b.bbox.x0)));
let ordered = order_cards(cluster_cards(&lines, cfg), cfg);
let template = &nodes[author_idx[0]];
let template_loc = Loc {
zone_cols: 1,
col: 0,
zone: AUTHOR_ZONE,
..template.loc
};
let (leaf_left, leaf_right) = (template.leaf_left, template.leaf_right);
let new_nodes: Vec<Node> = ordered
.into_iter()
.map(|card| Node {
kind: NodeKind::Text(TextBlock {
lines: card.lines,
loc: template_loc,
left: card.bbox.x0,
right: card.bbox.x1,
centered: true,
gap_before: f32::INFINITY,
bbox: card.bbox,
}),
loc: template_loc,
class: Class::Authors,
tail: (template_loc.page, template_loc.leaf),
leaf_left,
leaf_right,
strip_prefix: 0,
})
.collect();
let first = author_idx[0];
let drop: HashSet<usize> = author_idx.iter().copied().collect();
let mut idx = 0;
nodes.retain(|_| {
let keep = !drop.contains(&idx);
idx += 1;
keep
});
nodes.splice(first..first, new_nodes);
}
fn cluster_cards(lines: &[Line], cfg: &FrontMatterConfig) -> Vec<AuthorCard> {
let mut parent: Vec<usize> = (0..lines.len()).collect();
for i in 0..lines.len() {
for j in i + 1..lines.len() {
let (upper, lower) = (&lines[i], &lines[j]);
let gap = lower.bbox.y0 - upper.bbox.y1;
if gap > cfg.card_max_gap_em * upper.size.max(lower.size) {
break;
}
let same_row = (upper.baseline - lower.baseline).abs() < cfg.card_same_row_tolerance_pt;
if !same_row
&& upper.bbox.x_overlap(&lower.bbox)
>= cfg.card_min_x_overlap_ratio * upper.bbox.width().min(lower.bbox.width())
{
let (root_a, root_b) = (find_root(&mut parent, i), find_root(&mut parent, j));
if root_a != root_b {
parent[root_b] = root_a;
}
}
}
}
let mut cards: Vec<AuthorCard> = Vec::new();
let mut card_of: HashMap<usize, usize> = HashMap::new();
for (i, line) in lines.iter().enumerate() {
let root = find_root(&mut parent, i);
let card_idx = *card_of.entry(root).or_insert_with(|| {
cards.push(AuthorCard {
bbox: Rect::EMPTY,
lines: Vec::new(),
});
cards.len() - 1
});
cards[card_idx].bbox = cards[card_idx].bbox.union(&line.bbox);
cards[card_idx].lines.push(line.clone());
}
cards
}
fn find_root(parent: &mut [usize], i: usize) -> usize {
let mut root = i;
while parent[root] != root {
root = parent[root];
}
parent[i] = root;
root
}
fn order_cards(mut cards: Vec<AuthorCard>, cfg: &FrontMatterConfig) -> Vec<AuthorCard> {
cards.sort_by(|a, b| a.bbox.y0.total_cmp(&b.bbox.y0));
let mut ordered: Vec<AuthorCard> = Vec::new();
let mut row: Vec<AuthorCard> = Vec::new();
let mut row_box = Rect::EMPTY;
for card in cards {
if !row.is_empty()
&& row_box.y_overlap(&card.bbox)
< cfg.card_row_min_y_overlap_ratio * row_box.height().min(card.bbox.height())
{
row.sort_by(|a, b| a.bbox.x0.total_cmp(&b.bbox.x0));
ordered.append(&mut row);
row_box = Rect::EMPTY;
}
row_box = row_box.union(&card.bbox);
row.push(card);
}
row.sort_by(|a, b| a.bbox.x0.total_cmp(&b.bbox.x0));
ordered.append(&mut row);
ordered
}
fn flatten_author_zones(nodes: &mut [Node], author_zones: &HashSet<usize>, first_page: usize, cfg: &FrontMatterConfig) {
for &zone in author_zones {
let others: Vec<usize> = nodes
.iter()
.enumerate()
.filter(|(_, node)| node.loc.page == first_page && node.loc.zone == zone && node.class != Class::Authors)
.map(|(idx, _)| idx)
.collect();
if others.len() <= cfg.author_zone_max_others_count {
for idx in others {
nodes[idx].loc.zone_cols = 1;
nodes[idx].loc.col = 0;
}
}
}
}