Fix the arXiv-template PDF output

The user-supplied template PDF exposed four concrete problems:

- title/headings were letter-spaced: plain text in a large paragraph took the
  SizedText segment path and was scaled twice; the fallback run now stays at
  the base size and is scaled once by the paragraph;
- the table lost its third column because unsupported math glyphs were sent
  through the PDF fallback font; table-cell math now has a readable ASCII
  fallback (Size (um), approx. 100, 10**6);
- fancyhdr's \cfoot{\thepage} was painted together with route-D's default page
  number; default numbering is skipped for a fancy pagestyle;
- .bib data was ignored; simple @article/@inproceedings entries are now parsed
  from project .bib files, inlined as thebibliography entries, and resolved by
  the existing citation pass, producing References and [1]-[3].

The regenerated 3-page PDF was checked for title/heading spacing, table text,
references and a single page number per page. A project regression test covers
the .bib path, and the table/heading fixes have unit tests.

Remaining visible limits in this template: the complex display equation, the
\fbox/rule figure body, vector/PDF images and the author block layout.
Test count moves to 1069 (444 core, 432 engine, 65 server).
This commit is contained in:
gloamxun committed 2026-10-27 13:38:36 +08:00
1 parent fab26234fc
commit b1e3e03f00
9 files changed
+457 -26

No files matched your search

+229 -1
View File
@@ -1170,7 +1170,235 @@ fn store_route_d_preview(key: (String, u64), json: String) {
fn expand_project_source(source: &str, store: &projects::ProjectStore, id: &str) -> String {
let expanded = expand_project_inputs(source, store, id);
let expanded = expand_project_packages(&expanded, store, id);
expand_project_inputs(&expanded, store, id)
let expanded = expand_project_inputs(&expanded, store, id);
expand_project_bibliography(&expanded, store, id)
}
/// Inline a project's BibTeX files as a `thebibliography` block so the
/// route-D citation pass can resolve `\cite` without a BibTeX run.
fn expand_project_bibliography(source: &str, store: &projects::ProjectStore, id: &str) -> String {
let mut output = String::new();
let mut rest = source;
while let Some(index) = rest.find("\\bibliography{") {
let line_start = rest[..index].rfind('\n').map_or(0, |position| position + 1);
let commented = rest[line_start..index].contains('%');
output.push_str(&rest[..index]);
if commented {
output.push_str("\\bibliography{");
rest = &rest[index + "\\bibliography{".len()..];
continue;
}
let after = &rest[index + "\\bibliography{".len()..];
let Some(end) = after.find('}') else {
output.push_str(&rest[index..]);
return output;
};
let names = &after[..end];
let mut entries = Vec::new();
for name in names.split(',') {
let name = name.trim();
if name.is_empty() {
continue;
}
let path = if name.ends_with(".bib") {
name.to_string()
} else {
format!("{name}.bib")
};
let Ok(bytes) = store.read_file(id, &path) else {
continue;
};
let Ok(text) = String::from_utf8(bytes) else {
continue;
};
entries.extend(parse_bib_entries(&text));
}
if entries.is_empty() {
output.push_str("\\bibliography{");
output.push_str(names);
output.push('}');
} else {
output.push_str("\\begin{thebibliography}{99}\n");
for (key, entry) in entries {
output.push_str("\\bibitem{");
output.push_str(&key);
output.push_str("} ");
output.push_str(&entry);
output.push('\n');
}
output.push_str("\\end{thebibliography}\n");
}
rest = &after[end + 1..];
}
output.push_str(rest);
output
}
/// Parse the small BibTeX subset used by paper templates: entries with
/// braced or quoted field values. Unknown entry types are skipped.
fn parse_bib_entries(text: &str) -> Vec<(String, String)> {
let mut entries = Vec::new();
let mut cursor = 0usize;
while let Some(relative) = text[cursor..].find('@') {
let at = cursor + relative;
let after_at = &text[at + 1..];
let Some(open) = after_at.find(['{', '(']) else {
break;
};
let kind = after_at[..open].trim().to_ascii_lowercase();
let open_character = after_at[open..].chars().next().unwrap_or('{');
let close_character = if open_character == '(' { ')' } else { '}' };
let body_start = at + 1 + open + 1;
let mut depth = 0usize;
let mut in_quote = false;
let mut body_end = None;
for (offset, character) in text[body_start..].char_indices() {
if character == '"' {
in_quote = !in_quote;
continue;
}
if in_quote {
continue;
}
if character == '{' {
depth += 1;
} else if character == '}' {
if depth > 0 {
depth -= 1;
} else if close_character == '}' {
body_end = Some(body_start + offset);
break;
}
} else if character == close_character && depth == 0 {
body_end = Some(body_start + offset);
break;
}
}
let Some(body_end) = body_end else {
break;
};
let body = &text[body_start..body_end];
cursor = body_end + 1;
if kind == "comment" || kind == "preamble" || kind == "string" {
continue;
}
let Some(comma) = body.find(',') else {
continue;
};
let key = body[..comma].trim();
if key.is_empty() {
continue;
}
let fields = parse_bib_fields(&body[comma + 1..]);
if let Some(entry) = format_bib_entry(&fields) {
entries.push((key.to_string(), entry));
}
}
entries
}
fn parse_bib_fields(text: &str) -> Vec<(String, String)> {
let mut fields = Vec::new();
let mut rest = text;
loop {
rest = rest
.trim_start_matches(|character: char| character.is_whitespace() || character == ',');
if rest.is_empty() {
break;
}
let name_end = rest
.find(|character: char| !(character.is_ascii_alphanumeric() || character == '_'))
.unwrap_or(rest.len());
if name_end == 0 {
break;
}
let name = rest[..name_end].to_ascii_lowercase();
rest = rest[name_end..].trim_start();
let Some(after_equals) = rest.strip_prefix('=') else {
break;
};
rest = after_equals.trim_start();
let value = if let Some(after_open) = rest.strip_prefix('{') {
let mut depth = 1usize;
let mut end = None;
for (offset, character) in after_open.char_indices() {
match character {
'{' => depth += 1,
'}' => {
depth -= 1;
if depth == 0 {
end = Some(offset);
break;
}
}
_ => {}
}
}
let Some(end) = end else {
break;
};
rest = &after_open[end + 1..];
&after_open[..end]
} else if let Some(after_quote) = rest.strip_prefix('"') {
let Some(end) = after_quote.find('"') else {
break;
};
rest = &after_quote[end + 1..];
&after_quote[..end]
} else {
let end = rest
.find(|character: char| character == ',' || character.is_whitespace())
.unwrap_or(rest.len());
let value = &rest[..end];
rest = &rest[end..];
value
};
let value = value
.replace(['{', '}'], "")
.replace('~', " ")
.replace("--", "-")
.split_whitespace()
.collect::<Vec<_>>()
.join(" ");
fields.push((name, value));
}
fields
}
fn format_bib_entry(fields: &[(String, String)]) -> Option<String> {
let field = |name: &str| {
fields
.iter()
.find(|(field, _)| field == name)
.map(|(_, value)| value.as_str())
};
let title = field("title")?;
let author = field("author").unwrap_or("").replace(" and ", ", ");
let mut entry = String::new();
if !author.is_empty() {
entry.push_str(&author);
entry.push_str(". ");
}
entry.push_str("\\emph{");
entry.push_str(title);
entry.push_str("}.");
if let Some(container) = field("journal").or_else(|| field("booktitle")) {
entry.push(' ');
entry.push_str(container);
}
if let Some(pages) = field("pages") {
entry.push_str(", ");
entry.push_str(pages);
}
if let Some(year) = field("year") {
entry.push_str(", ");
entry.push_str(year);
}
if let Some(publisher) = field("publisher").or_else(|| field("organization")) {
entry.push_str(", ");
entry.push_str(publisher);
}
Some(entry)
}
/// Decode every `\includegraphics` file a project source references.