pub(crate) mod math;
mod text;
mod vietnamese;
use super::markdown::{linked_anchors, strip_label};
use super::sequence::{AuthorMarkup, Step, author_lines, linked_anchor, push_line, steps, title_spans, write_authors};
use super::{
CAP_LABEL, Equations, HtmlOptions, ImageStore, Mode, PageImage, caption_first, code, doc_title, equation_math,
float_class, heading_tag, is_scene_break,
};
use crate::analysis::pipeline::Analysis;
use crate::backend::PdfFile;
use crate::model::{BOLD, Block, BlockKind, Document, FloatKind, ImageSpec, MONO, Span, Table, TableCell};
use crate::render::Pictures;
use math::latex;
use std::cell::RefCell;
use std::collections::BTreeSet;
use text::{LAST_LATIN, LATIN_GAPS, Text, escape, label};
const PREAMBLE: &str = include_str!("preamble.tex");
const PREAMBLE_HEAD_END: &str = "folder next to this one.\n";
const CJK_NOTE: &str = "% Its Chinese, Japanese or Korean text needs LuaLaTeX or XeLaTeX: pdfLaTeX\n\
% has no font for it and writes a ? for each of its characters.\n";
const CJK_TEXT_SYMBOLS: &[(char, &str)] = &[
('△', "\\triangle"),
('□', "\\square"),
('〈', "\\langle"),
('〉', "\\rangle"),
];
const FONTS_END: &str = " \\usepackage{fontspec}\n\\fi\n";
const CJK_FONTS: &str = r#"\ifLuaTeX
\usepackage[match]{luatexja-fontspec}
\IfFontExistsTF{Noto Serif CJK JP}{\setmainjfont{Noto Serif CJK JP}}{}
\IfFontExistsTF{Noto Sans CJK JP}{\setsansjfont{Noto Sans CJK JP}}{}
\fi
\ifXeTeX
\usepackage{xeCJK}
\xeCJKsetup{CJKspace=true}
\xeCJKDeclareCharClass{CJK}{"2015, "2160->"216B, "2170->"217B, "2194, "2196->"2199,
"21C4->"21C6, "21CB->"21CC, "21D0, "21D2, "21D4, "21E6->"21E9, "21F5, "2200,
"2202->"2203, "2205->"220B, "220F, "2211, "2213, "221D, "221F->"2220, "2225->"222E,
"2234->"2235, "223D, "2243, "2245, "2248, "2260->"2262, "2264->"2267, "226A->"226B,
"2272->"2273, "2276->"2277, "2282->"2287, "228A->"228B, "2295->"2298, "22A0, "22A5,
"22BF, "22DA->"22DB, "22EF, "2460->"2492, "249C->"254B, "2550, "255E, "2561, "256A,
"256D->"2573, "25A0->"25A2, "25AA->"25AB, "25B1->"25B3, "25B6->"25B7, "25BC->"25BD,
"25C0->"25C1, "25C6->"25C7, "25C9->"25CC, "25CE->"25D3, "25E2->"25E5, "25EF,
"2600->"2603, "2605->"2606, "260E, "2616->"2617, "261C->"261F, "2640, "2642,
"2660->"2669, "266B->"266F, "2672->"267D, "26A0, "26BD->"26BE}
\IfFontExistsTF{Noto Serif CJK JP}
{\setCJKmainfont{Noto Serif CJK JP}}
{\setCJKmainfont[BoldFont=HaranoAjiGothic-Bold.otf]{HaranoAjiMincho-Regular.otf}}
\IfFontExistsTF{Noto Sans CJK JP}
{\setCJKsansfont{Noto Sans CJK JP}\setCJKmonofont{Noto Sans CJK JP}}
{\setCJKsansfont[BoldFont=HaranoAjiGothic-Bold.otf]{HaranoAjiGothic-Medium.otf}%
\setCJKmonofont{HaranoAjiGothic-Medium.otf}}
\fi
% A symbol of CJK text: the character #1, or the math symbol #2 under pdfLaTeX.
\newcommand{\kakemonotext}[2]{\ifPDFTeX\ensuremath{#2}\else#1\fi}
"#;
const MAX_BOXED_TABLE_BODY_ROWS: usize = 30;
const LONG_TABLE_SIZES: &[(usize, &str, f32, f32)] = &[
(4, "", 10.95, 6.0),
(6, "\\small", 10.0, 4.0),
(9, "\\footnotesize", 9.0, 3.0),
(usize::MAX, "\\scriptsize", 8.0, 2.0),
(usize::MAX, "\\tiny", 6.0, 1.0),
];
fn too_wide_to_box(table: &Table) -> bool {
let readable = &LONG_TABLE_SIZES[..LONG_TABLE_SIZES.len() - 1];
table.geom.col_bounds.is_empty()
&& !readable
.iter()
.any(|size| Columns::fitted(table, size.2, size.3, TEXT_WIDTH_PT).fits())
}
const TEXT_HEIGHT_PT: f32 = 650.0;
const LINE_HEIGHT_RATIO: f32 = 1.2;
const LONG_TABLE_MAX_HEAD_RATIO: f32 = 0.25;
const TYPICAL_CELL_MAX_EM: f32 = 6.0;
const TEXT_WIDTH_PT: f32 = 472.0;
const CJK_CHAR_EM: f32 = 1.0;
const WIDE_CHAR_EM: f32 = 0.75;
const NARROW_CHAR_EM: f32 = 0.33;
const OTHER_CHAR_EM: f32 = 0.5;
const BOLD_WIDTH_RATIO: f32 = 1.15;
const SIZE_WIDTH_RATIOS: &[(f32, f32)] = &[(10.0, 1.0), (9.0, 1.03), (8.0, 1.06), (0.0, 1.22)];
const MIN_COLUMN_EM: f32 = 1.0;
const LONG_TABLE_FILL_RATIO: f32 = 0.9;
const FIT_TOLERANCE_RATIO: f32 = 0.001;
const COLUMN_GROUP_FILL_RATIO: f32 = 0.85;
const MIN_WRAPPED_COLUMN_EM: f32 = 4.0;
const LONG_TABLE_PACKAGES: &str = "\\usepackage{longtable}\n\\usepackage{array}\n";
const LANDSCAPE_PACKAGE: &str = "\\usepackage{pdflscape}\n";
const TABLE_PACKAGES_END: &str = "\\usepackage{multirow}\n";
const MAX_LIST_NESTING: u8 = 3;
const T1_FONTENC: &str = "\\usepackage[T1]{fontenc}";
const T5_FONTENC: &str = "\\usepackage[T5,T1]{fontenc}";
const CONTENTS_INDENT_EM: f32 = 1.5;
const REFERENCE_INDENT: &str = "1.5em";
const RAGGED_MONO_RATIO: f32 = 0.5;
const AUTHOR_GAP: &str = "2em";
const TAB_SPACES: &str = " ";
#[derive(Debug, Clone)]
pub struct LatexOptions {
pub asset_dir: String,
pub scale: f32,
pub jobs: usize,
pub equations: Equations,
}
impl Default for LatexOptions {
fn default() -> Self {
Self {
asset_dir: "images".into(),
scale: 3.0,
jobs: 0,
equations: Equations::MathMl,
}
}
}
pub struct LatexOutput {
pub latex: String,
pub files: Vec<(String, Vec<u8>)>,
}
pub fn render_latex(analysis: &Analysis, pdf: Option<&PdfFile>, opts: &LatexOptions) -> LatexOutput {
let attached = crate::render::notes::footnotes_attached(analysis);
let analysis = attached.as_ref().unwrap_or(analysis);
let html_opts = HtmlOptions {
mode: Mode::Friendly,
inline_images: false,
asset_dir: tex_path(&opts.asset_dir),
scale: opts.scale,
page_image: PageImage::Svg,
jobs: opts.jobs,
embed_fonts: false,
equations: opts.equations,
};
let mut images = ImageStore {
opts: &html_opts,
jobs: Vec::new(),
svg_as_pdf: true,
};
let doc = &analysis.doc;
let anchors = linked_anchors(doc);
let mut writer = Writer {
opts,
text: Text {
anchors: &anchors,
math: opts.equations == Equations::MathMl,
table_notes: RefCell::default(),
},
images: &mut images,
out: String::new(),
group: Group::None,
item_open: false,
nested: 0,
after_text: false,
};
writer.write(doc);
let body = writer.out.trim_end().to_string();
let out = images.finish(Pictures::new(pdf, &analysis.doc), body);
LatexOutput {
latex: document(doc, &out.html),
files: out.files,
}
}
fn document(doc: &Document, body: &str) -> String {
let title = escape(&doc_title(doc));
let cjk = title.chars().chain(body.chars()).any(is_cjk);
let (title, body) = if cjk {
let as_text = |text: &str| {
CJK_TEXT_SYMBOLS
.iter()
.fold(text.to_string(), |text, (symbol, command)| {
text.replace(
&format!("\\ensuremath{{{command}}}"),
&format!("\\kakemonotext{{{symbol}}}{{{command}}}"),
)
})
};
(as_text(&title), as_text(body))
} else {
(title, body.to_string())
};
let body = body.as_str();
let (vietnamese, unknown): (Vec<char>, Vec<char>) = unknown_chars(&title, body)
.into_iter()
.partition(|&ch| vietnamese::t5_letter(ch).is_some());
let mut out = if vietnamese.is_empty() {
String::from(PREAMBLE)
} else {
PREAMBLE.replacen(T1_FONTENC, T5_FONTENC, 1)
};
out = out.replacen(
"Converted from a PDF",
&format!("Converted from {}", crate::render::source_phrase(doc)),
1,
);
if body.contains("\\begin{longtable}") {
let landscape = if body.contains("\\begin{landscape}") {
LANDSCAPE_PACKAGE
} else {
""
};
out = out.replacen(
TABLE_PACKAGES_END,
&format!("{TABLE_PACKAGES_END}{LONG_TABLE_PACKAGES}{landscape}"),
1,
);
}
if cjk {
out = out.replacen(PREAMBLE_HEAD_END, &format!("{PREAMBLE_HEAD_END}{CJK_NOTE}"), 1);
out = out.replacen(FONTS_END, &format!("{FONTS_END}{CJK_FONTS}"), 1);
}
if !vietnamese.is_empty() {
out.push_str(&format!(
"% Vietnamese letters: pdfLaTeX sets them in the T5 encoding (vntex).\n\\ifPDFTeX\n \\newcommand{{{cmd}}}[1]{{{{\\fontencoding{{T5}}\\selectfont#1}}}}\n",
cmd = vietnamese::T5_COMMAND
));
for ch in vietnamese {
let letter = vietnamese::t5_letter(ch).expect("partitioned on t5_letter");
out.push_str(&format!(
" \\DeclareUnicodeCharacter{{{:04X}}}{{{}{{{letter}}}}}\n",
u32::from(ch),
vietnamese::T5_COMMAND
));
}
out.push_str("\\fi\n");
}
if !unknown.is_empty() {
out.push_str("% Characters that the fonts of pdfLaTeX do not have.\n\\ifPDFTeX\n");
for ch in unknown {
out.push_str(&format!(
" \\DeclareUnicodeCharacter{{{:04X}}}{{\\kakemonomissing}}\n",
u32::from(ch)
));
}
out.push_str("\\fi\n");
}
out.push_str(&format!(
"\\hypersetup{{pdftitle={{{title}}}}}\n\n\\begin{{document}}\n\n"
));
out.push_str(body);
out.push_str("\n\n\\end{document}\n");
out
}
fn is_cjk(ch: char) -> bool {
matches!(
u32::from(ch),
0x1100..=0x11FF | 0x2E80..=0x2FDF | 0x3000..=0x303F | 0x3040..=0x30FF | 0x3100..=0x312F | 0x3130..=0x31FF | 0x3200..=0x33FF | 0x3400..=0x4DBF | 0x4E00..=0x9FFF | 0xA960..=0xA97F | 0xAC00..=0xD7FF | 0xF900..=0xFAFF | 0xFE10..=0xFE1F | 0xFE30..=0xFE4F | 0xFF00..=0xFFEF | 0x20000..=0x3FFFF )
}
fn unknown_chars(title: &str, body: &str) -> BTreeSet<char> {
title
.chars()
.chain(body.chars())
.filter(|&ch| !ch.is_ascii() && (ch > LAST_LATIN || LATIN_GAPS.contains(&ch)))
.collect()
}
fn tex_path(dir: &str) -> String {
dir.trim_end_matches(['/', '\\'])
.chars()
.map(|ch| {
if ch.is_ascii_alphanumeric() || matches!(ch, '.' | '_' | '-' | '/') {
ch
} else {
'_'
}
})
.collect()
}
#[derive(PartialEq, Clone, Copy)]
enum Group {
None,
List,
Abstract,
}
impl Group {
fn of(block: &Block, current: Self) -> Self {
match &block.kind {
BlockKind::ListItem { .. } | BlockKind::ListContinuation => Self::List,
BlockKind::Paragraph { in_abstract: true } => Self::Abstract,
BlockKind::Equation { .. } if current == Self::Abstract => current,
_ => Self::None,
}
}
fn environment(self) -> Option<&'static str> {
match self {
Self::None => None,
Self::List => Some("itemize"),
Self::Abstract => Some("abstract"),
}
}
}
struct Writer<'a, 'r> {
opts: &'a LatexOptions,
text: Text<'a>,
images: &'a mut ImageStore<'r>,
out: String,
group: Group,
item_open: bool,
nested: u8,
after_text: bool,
}
impl Writer<'_, '_> {
fn write(&mut self, doc: &Document) {
let blocks: Vec<&Block> = doc.blocks.iter().collect();
for step in steps(&blocks, doc.meta.arxiv_id.is_some()) {
crate::common::cancel::check();
match step {
Step::Source => self.arxiv_link(doc),
Step::Code(run) => {
self.set_group(Group::None);
self.anchor(run[0]);
self.verbatim(&code::listing_lines(run));
}
Step::Title(run) => {
self.set_group(Group::of(run[0], self.group));
self.title(doc, run);
}
Step::Authors(run) => {
self.set_group(Group::of(run[0], self.group));
write_authors(self, run);
}
Step::AbstractHeading(block) => {
self.set_group(Group::of(block, self.group));
self.anchor(block);
}
Step::NumberedEquation(block, number) => {
self.set_group(Group::of(block, self.group));
if let BlockKind::Equation { image, .. } = &block.kind {
self.equation(block, image.as_ref(), Some(&number));
}
}
Step::Contents(run) => {
for block in run {
self.set_group(Group::of(block, self.group));
self.block(block);
}
}
Step::Block(block) => {
self.set_group(Group::of(block, self.group));
self.block(block);
}
}
}
self.set_group(Group::None);
}
fn set_group(&mut self, group: Group) {
if group == self.group {
return;
}
self.nest(0);
if let Some(env) = self.group.environment() {
self.paragraph(&format!("\\end{{{env}}}"));
}
if let Some(env) = group.environment() {
self.paragraph(&format!("\\begin{{{env}}}"));
}
self.group = group;
self.item_open = false;
}
fn block(&mut self, block: &Block) {
if !matches!(
block.kind,
BlockKind::ListItem { .. } | BlockKind::ListContinuation | BlockKind::Equation { .. }
) {
self.anchor(block);
}
match &block.kind {
BlockKind::Title => self.title_text(&self.text.spans(&block.spans)),
BlockKind::Authors => write_authors(self, std::slice::from_ref(&block)),
BlockKind::Note => self.text_paragraph(&block.spans),
BlockKind::Heading { level } => self.heading(block, *level),
BlockKind::Paragraph { .. } if is_scene_break(&block.spans) => {
self.paragraph("\\begin{center}\n* * *\n\\end{center}");
}
BlockKind::Paragraph { .. } => self.text_paragraph(&block.spans),
BlockKind::ListItem { marker, level } => self.list_item(block, marker.as_deref(), *level),
BlockKind::ListContinuation => self.list_continuation(block),
BlockKind::Reference { .. } => {
let text = self.text.spans(&block.spans);
if !text.is_empty() {
self.paragraph(&format!(
"{{\\small\\noindent\\hangindent={REFERENCE_INDENT}\\relax {text}\\par}}"
));
}
}
BlockKind::Footnote { marker } => self.footnote(block, marker),
BlockKind::Equation { image, number } => self.equation(block, image.as_ref(), number.as_deref()),
BlockKind::Figure { kind, image, .. } => self.figure(block, *kind, image.as_ref()),
BlockKind::ContentsEntry { level, page } => self.contents_entry(block, *level, page),
BlockKind::Code => {
let lines: Vec<String> = block
.rows
.iter()
.map(|row| row.iter().map(|span| span.text.as_str()).collect())
.collect();
self.verbatim(&lines);
}
}
}
fn paragraph(&mut self, text: &str) {
self.out.push_str(text);
self.out.push_str("\n\n");
self.after_text = false;
}
fn text_paragraph(&mut self, spans: &[Span]) {
let text = self.text.spans(spans);
if text.is_empty() {
return;
}
if mono_ratio(spans) >= RAGGED_MONO_RATIO {
self.paragraph(&format!("{{\\raggedright {text}\\par}}"));
} else {
self.paragraph(&text);
}
self.after_text = true;
}
fn anchor_label(&self, block: &Block) -> String {
linked_anchor(block, self.text.anchors)
.map(|anchor| format!("\\phantomsection\\label{{{}}}", label(anchor)))
.unwrap_or_default()
}
fn anchor(&mut self, block: &Block) {
let label = self.anchor_label(block);
push_line(&mut self.out, &label);
}
fn arxiv_link(&mut self, doc: &Document) {
if let Some(id) = &doc.meta.arxiv_id {
let link = format!("\\href{{https://arxiv.org/abs/{}}}{{arXiv:{}}}", escape(id), escape(id));
self.paragraph(&format!("\\begin{{center}}\n{link}\n\\end{{center}}"));
}
}
fn title(&mut self, doc: &Document, run: &[&Block]) {
for block in run {
self.anchor(block);
}
let text = self.text.spans(&title_spans(run));
if text.is_empty() {
self.title_text(&escape(&doc_title(doc)));
} else {
self.title_text(&text);
}
}
fn title_text(&mut self, text: &str) {
self.paragraph(&format!(
"\\begin{{center}}\n{{\\LARGE\\bfseries {text}\\par}}\n\\end{{center}}"
));
}
fn heading(&mut self, block: &Block, level: u8) {
let text = self.text.spans(&block.spans);
if text.is_empty() {
return;
}
let command = match heading_tag(level) {
"h2" => "section",
"h3" => "subsection",
"h4" => "subsubsection",
_ => "paragraph",
};
self.paragraph(&format!("\\{command}*{{{text}}}"));
}
fn list_item(&mut self, block: &Block, marker: Option<&str>, level: u8) {
self.nest(level.min(MAX_LIST_NESTING));
let rest = marker.and_then(|marker| strip_label(&block.spans, marker));
let item = match (marker, &rest) {
(Some(marker), Some(_)) => format!("\\item[{{{}}}]", escape(marker)),
_ => "\\item".to_string(),
};
let text = self.text.spans(rest.as_deref().unwrap_or(&block.spans));
let text = if rest.is_some() { text } else { protect_start(&text) };
let label = self.anchor_label(block);
self.paragraph(format!("{item} {label}{text}").trim_end());
self.item_open = true;
}
fn nest(&mut self, level: u8) {
while self.nested > level {
self.paragraph("\\end{itemize}");
self.nested -= 1;
self.item_open = true;
}
while self.nested < level {
if !self.item_open {
self.out.push_str("\\item[]\n");
}
self.paragraph("\\begin{itemize}");
self.nested += 1;
self.item_open = false;
}
}
fn list_continuation(&mut self, block: &Block) {
if !self.item_open {
self.out.push_str("\\item[]\n");
self.item_open = true;
}
let text = self.text.spans(&block.spans);
let label = self.anchor_label(block);
if !text.is_empty() || !label.is_empty() {
self.paragraph(&format!("{label}{text}"));
}
}
fn footnote(&mut self, block: &Block, marker: &str) {
let text = self.text.spans(&block.spans);
if text.is_empty() {
return;
}
if marker.is_empty() {
self.paragraph(&format!("\\begin{{quote}}\n\\small {text}\n\\end{{quote}}"));
} else {
self.paragraph(&format!(
"{{\\footnotesize\\noindent\\textsuperscript{{{}}}\\,{text}\\par}}",
escape(marker)
));
}
}
fn contents_entry(&mut self, block: &Block, level: u8, page: &str) {
let text = self.text.spans(&block.spans);
if text.is_empty() {
return;
}
let indent = f32::from(level.saturating_sub(1)) * CONTENTS_INDENT_EM;
let page = if page.is_empty() {
String::new()
} else {
format!("\\dotfill {}", escape(page))
};
self.paragraph(&format!("\\noindent\\hspace*{{{indent}em}}{text}{page}\\par"));
}
fn verbatim(&mut self, lines: &[String]) {
let body: Vec<String> = lines
.iter()
.map(|line| {
line.replace('\t', TAB_SPACES)
.replace("\\end{verbatim}", "\\end {verbatim}")
})
.collect();
let body = body.join("\n");
let body = body.trim_end();
if body.trim().is_empty() {
return;
}
self.paragraph(&format!(
"\\begin{{small}}\n\\begin{{verbatim}}\n{body}\n\\end{{verbatim}}\n\\end{{small}}"
));
}
fn equation(&mut self, block: &Block, image: Option<&ImageSpec>, number: Option<&str>) {
let body = match (equation_math(block, image, self.opts.equations), image) {
(Some(math), _) => latex(math).replace(['\n', '\r'], " "),
(None, Some(image)) => format!("\\vcenter{{\\hbox{{{}}}}}", self.picture(image, "eq")),
(None, None) => format!("\\text{{{}}}", self.text.spans(&block.spans)),
};
let tag = number
.map(|number| number.trim().trim_start_matches('(').trim_end_matches(')').trim())
.filter(|tag| !tag.is_empty());
let display = match tag {
Some(tag) => format!(
"\\begin{{equation*}}\n{body}\n\\tag{{{}}}\n\\end{{equation*}}",
escape(tag)
),
None => format!("\\[\n{body}\n\\]"),
};
if self.after_text && self.out.ends_with("\n\n") {
self.out.pop();
}
let label = self.anchor_label(block);
if label.is_empty() {
self.paragraph(&display);
} else {
self.paragraph(&format!("{label}\n{display}"));
}
}
fn figure(&mut self, block: &Block, kind: FloatKind, image: Option<&ImageSpec>) {
let caption = (!block.spans.is_empty()).then(|| self.caption(&block.spans));
if kind == FloatKind::Algorithm
&& block.table.is_none()
&& let Some(lines) = code::float_listing_lines(block)
{
if let Some(caption) = &caption {
self.paragraph(caption);
}
return self.verbatim(&lines);
}
let caption_first = caption_first(block, image);
let content = if let Some(table) = block.table.as_ref().filter(|table| !table.rows.is_empty()) {
Some(
if table.rows.len().saturating_sub(table.header_rows) > MAX_BOXED_TABLE_BODY_ROWS
|| too_wide_to_box(table)
{
return self.paragraph(&self.longtable(table, caption.as_deref(), caption_first));
} else {
*self.text.table_notes.borrow_mut() = Some(Vec::new());
let tabular = self.tabular(table);
let notes = self.text.table_notes.borrow_mut().take().unwrap_or_default();
format!(
"\\begin{{center}}\n\\begin{{kakemonotable}}\n{tabular}\n\\end{{kakemonotable}}\n\\end{{center}}{}",
footnote_texts(¬es)
)
},
)
} else {
image.map(|spec| {
format!(
"\\begin{{center}}\n{}\n\\end{{center}}",
self.picture(spec, float_class(kind))
)
})
};
let mut parts: Vec<String> = Vec::new();
if caption_first && let Some(caption) = &caption {
parts.push(caption.clone());
}
parts.extend(content);
if !caption_first && let Some(caption) = caption {
parts.push(caption);
}
if parts.is_empty() {
return;
}
if block.table.is_none() && image.is_some() {
self.paragraph(&format!(
"\\noindent\\begin{{minipage}}{{\\linewidth}}\n{}\n\\end{{minipage}}\n\\medskip",
parts.join("\n\n")
));
} else {
for part in parts {
self.paragraph(&part);
}
}
}
fn caption(&self, spans: &[Span]) -> String {
let text = if let Some(first) = spans.first()
&& let Some(found) = CAP_LABEL.find(&first.text)
{
let label = first.text[..found.end()].trim_end();
let mut rest = spans.to_vec();
rest[0].text = first.text[label.len()..].to_string();
format!("\\textbf{{{}}} {}", escape(label), self.text.spans(&rest))
.trim_end()
.to_string()
} else {
self.text.spans(spans)
};
format!("{{\\small {text}\\par}}")
}
fn picture(&mut self, spec: &ImageSpec, hint: &str) -> String {
let width = spec.rect.width().max(1.0);
let src = self.images.region(spec, hint);
format!("\\kakemonopicture{{{width:.1}bp}}{{{src}}}")
}
fn tabular(&self, table: &Table) -> String {
let cols = Columns::plain(table);
let spec: String = (0..cols.align.len()).map(|col| cols.spec(col)).collect();
let mut lines = vec![format!("\\begin{{tabular}}{{{spec}}}"), "\\toprule".to_string()];
lines.extend(self.rows(table, &cols, false));
lines.push("\\bottomrule".to_string());
lines.push("\\end{tabular}".to_string());
lines.join("\n")
}
fn longtable(&self, table: &Table, caption: Option<&str>, caption_first: bool) -> String {
let n_cols = table.n_cols.max(1);
let start = LONG_TABLE_SIZES
.iter()
.position(|(most, ..)| n_cols <= *most)
.expect("the last sizes take any count");
let portrait = LONG_TABLE_SIZES[start..].iter().find_map(|&size| {
let cols = Columns::fitted(table, size.2, size.3, TEXT_WIDTH_PT);
cols.fits().then_some((size, cols))
});
let (above, below) = match caption {
Some(caption) if caption_first => (Some(caption), None),
Some(caption) => (None, Some(caption)),
None => (None, None),
};
let (tables, landscape) = match portrait {
Some((size, cols)) => (
self.longtable_part(table, &cols, size, TEXT_HEIGHT_PT, (above, below)),
false,
),
None => {
let size = *LONG_TABLE_SIZES.last().expect("sizes");
let cols = Columns::fitted(table, size.2, size.3, TEXT_HEIGHT_PT);
let tables = if cols.fits() {
self.longtable_part(table, &cols, size, TEXT_WIDTH_PT, (above, below))
} else {
let groups = column_groups(&cols);
groups
.iter()
.enumerate()
.map(|(i, group)| {
let part = table_columns(table, group);
let cols = Columns::fitted(&part, size.2, size.3, TEXT_HEIGHT_PT);
let captions = (above.filter(|_| i == 0), below.filter(|_| i + 1 == groups.len()));
self.longtable_part(&part, &cols, size, TEXT_WIDTH_PT, captions)
})
.collect::<Vec<_>>()
.join("\n\n")
};
(tables, true)
}
};
if landscape {
format!("\\begin{{landscape}}\n{tables}\n\\end{{landscape}}")
} else {
tables
}
}
fn longtable_part(
&self,
table: &Table,
cols: &Columns,
size: (usize, &str, f32, f32),
page_height_pt: f32,
(above, below): (Option<&str>, Option<&str>),
) -> String {
let (_, size, size_pt, tabcolsep_pt) = size;
let n_cols = table.n_cols.max(1);
let spec: String = (0..n_cols).map(|col| cols.spec(col)).collect();
let mut lines = Vec::new();
if !size.is_empty() {
lines.push(format!(
"\\begingroup{size}\\setlength{{\\tabcolsep}}{{{tabcolsep_pt}pt}}"
));
}
let captioned = above.is_some() || below.is_some();
if captioned {
lines.push("\\begingroup\\setlength{\\LTleft}{0pt}\\setlength{\\LTright}{\\fill}".to_string());
}
lines.push(format!("\\begin{{longtable}}{{{spec}}}"));
let caption_row = |caption: &str| {
format!(
"\\multicolumn{{{n_cols}}}{{@{{}}l@{{}}}}{{\\makebox[0pt][l]{{\\parbox[t]{{\\linewidth}}{{{caption}}}}}}} \\\\*"
)
};
if let Some(caption) = above {
lines.push(caption_row(caption));
}
lines.push("\\toprule".to_string());
let rows = self.rows(table, cols, true);
let header = table.header_rows.min(table.rows.len());
let (head, body) = if header > 0 && header < table.rows.len() {
(&rows[..=header], &rows[header + 1..])
} else {
(&rows[..0], &rows[..])
};
let mut head = head.to_vec();
let head_spans = table.rows[..header].iter().flatten().any(|cell| cell.colspan > 1);
if head_spans && !head.is_empty() && cols.wrap.iter().any(|&wrap| wrap) {
head.insert(0, format!("{}\\kill", " & ".repeat(n_cols - 1)));
}
lines.extend(head.iter().cloned());
let repeat = !head.is_empty()
&& cols.header_lines(table) as f32 * size_pt * LINE_HEIGHT_RATIO
<= LONG_TABLE_MAX_HEAD_RATIO * page_height_pt;
if above.is_some() || (!head.is_empty() && !repeat) {
lines.push("\\endfirsthead".to_string());
lines.push("\\toprule".to_string());
if repeat {
lines.extend(head.iter().cloned());
}
}
if above.is_some() || !head.is_empty() {
lines.push("\\endhead".to_string());
}
lines.push("\\bottomrule".to_string());
if let Some(caption) = below {
lines.push(caption_row(caption));
}
lines.push("\\endlastfoot".to_string());
lines.extend(body.iter().cloned());
lines.push("\\end{longtable}".to_string());
if captioned {
lines.push("\\endgroup".to_string());
}
if !size.is_empty() {
lines.push("\\endgroup".to_string());
}
lines.join("\n")
}
fn rows(&self, table: &Table, cols: &Columns, split_header: bool) -> Vec<String> {
let n_cols = table.n_cols.max(1);
let n_rows = table.rows.len();
let mut covered: Vec<Vec<Option<(usize, String)>>> = vec![vec![None; n_cols]; n_rows];
let mut lines = Vec::new();
let mut pace = crate::common::cancel::Pace::default();
for (row_idx, row) in table.rows.iter().enumerate() {
pace.tick();
let last_row = if split_header && row_idx < table.header_rows {
table.header_rows.min(n_rows)
} else {
n_rows
};
let mut cells: Vec<String> = Vec::new();
let mut col = 0;
let mut row_cells = row.iter().filter(|cell| cell.col < n_cols).peekable();
while col < n_cols {
while row_cells.next_if(|cell| cell.col < col).is_some() {}
if let Some((width, spec)) = covered[row_idx][col].take() {
cells.push(multicolumn(width, &spec, String::new()));
col += width;
} else if let Some(cell) = row_cells.next_if(|cell| cell.col == col) {
let colspan = cell.colspan.clamp(1, n_cols - col);
let rowspan = cell.rowspan.clamp(1, last_row - row_idx);
let spec = cols.cell_spec(col, colspan, &cell.spans);
for below in covered.iter_mut().take(row_idx + rowspan).skip(row_idx + 1) {
below[col] = Some((colspan, spec.clone()));
}
cells.push(self.cell(cell, colspan, rowspan, &spec));
col += colspan;
} else {
cells.push(String::new());
col += 1;
}
}
lines.push(format!("{} \\\\", cells.join(" & ")));
if row_idx + 1 == table.header_rows && row_idx + 1 < n_rows {
lines.push("\\midrule".to_string());
}
}
lines
}
fn cell(&self, cell: &TableCell, colspan: usize, rowspan: usize, spec: &str) -> String {
let text = self.text.spans(&cell.spans);
let text = if rowspan > 1 {
let width = if spec.contains("p{") { "=" } else { "*" };
format!("\\multirow{{{rowspan}}}{{{width}}}{{{text}}}")
} else {
text
};
multicolumn(colspan, spec, protect_start(&text))
}
}
struct Columns {
align: Vec<char>,
wrap: Vec<bool>,
share: Vec<f32>,
em_share: f32,
pad_share: f32,
least: Vec<f32>,
}
impl Columns {
fn plain(table: &Table) -> Self {
let n_cols = table.n_cols.max(1);
Self {
align: (0..n_cols).map(|col| align_letter(table, col)).collect(),
wrap: vec![false; n_cols],
share: vec![0.0; n_cols],
em_share: 0.0,
pad_share: 0.0,
least: vec![0.0; n_cols],
}
}
fn fits(&self) -> bool {
self.share.iter().sum::<f32>() <= LONG_TABLE_FILL_RATIO + FIT_TOLERANCE_RATIO
}
fn header_lines(&self, table: &Table) -> usize {
let n_cols = self.align.len();
table
.rows
.iter()
.take(table.header_rows)
.map(|row| {
row.iter()
.filter(|cell| cell.col < n_cols)
.map(|cell| {
let width = cell.colspan.clamp(1, n_cols - cell.col);
if !self.wraps(cell.col, width) {
return 1;
}
let text: String = cell.spans.iter().map(|span| span.text.as_str()).collect();
let share: f32 = self.share[cell.col..cell.col + width].iter().sum();
let room_em = ((share - 2.0 * self.pad_share) / self.em_share).max(1.0);
(text_em(&text) / room_em).ceil().max(1.0) as usize
})
.max()
.unwrap_or(1)
})
.sum()
}
fn fitted(table: &Table, size_pt: f32, tabcolsep_pt: f32, width_pt: f32) -> Self {
let mut cols = Self::plain(table);
let n_cols = cols.align.len();
let TextWidths {
natural,
body,
word,
breaks,
spans,
} = TextWidths::of(table, n_cols);
let em_pt = em_width_pt(size_pt);
cols.em_share = em_pt / width_pt;
cols.pad_share = tabcolsep_pt / width_pt;
let share_of = |em: f32| (em * em_pt + 2.0 * tabcolsep_pt) / width_pt;
cols.share = natural.iter().map(|&em| share_of(em)).collect();
let mean = |col: usize| {
let (sum, count) = body[col];
if count == 0 { 0.0 } else { sum / count as f32 }
};
let typical = |col: usize| mean(col).max(word[col]).clamp(MIN_COLUMN_EM, TYPICAL_CELL_MAX_EM);
cols.least = (0..n_cols)
.map(|col| {
if breaks[col] {
share_of(typical(col))
} else {
share_of(natural[col].max(MIN_COLUMN_EM))
}
})
.collect();
if cols.share.iter().sum::<f32>() <= LONG_TABLE_FILL_RATIO {
return cols;
}
let floors = span_floors(&spans, &cols.least, |em| share_of(em.min(TYPICAL_CELL_MAX_EM)));
for (col, floor) in floors.into_iter().enumerate() {
cols.least[col] = cols.least[col].max(floor);
}
let even = 1.0 / n_cols as f32;
cols.wrap = (0..n_cols).map(|col| cols.share[col] > even && breaks[col]).collect();
for col in (0..n_cols).filter(|&col| !cols.wrap[col]) {
cols.share[col] = cols.share[col].max(cols.least[col]);
}
let wrapped: Vec<usize> = (0..n_cols).filter(|&col| cols.wrap[col]).collect();
let kept: f32 = (0..n_cols)
.filter(|&col| !cols.wrap[col])
.map(|col| cols.share[col])
.sum();
let left = LONG_TABLE_FILL_RATIO - kept;
let weight = |col: usize| mean(col).max(MIN_WRAPPED_COLUMN_EM);
if left <= 0.0 {
for &col in &wrapped {
cols.share[col] = cols.least[col];
}
return cols;
}
let floor = |col: usize| cols.least[col];
let mut floored = vec![false; n_cols];
loop {
let free: Vec<usize> = wrapped.iter().copied().filter(|&col| !floored[col]).collect();
let rest = left
- wrapped
.iter()
.filter(|&&col| floored[col])
.map(|&col| floor(col))
.sum::<f32>();
let total: f32 = free.iter().map(|&col| weight(col)).sum();
let mut changed = false;
for &col in &free {
cols.share[col] = rest * weight(col) / total;
if cols.share[col] < floor(col) {
floored[col] = true;
changed = true;
}
}
for &col in wrapped.iter().filter(|&&col| floored[col]) {
cols.share[col] = floor(col);
}
if !changed || free.is_empty() {
break;
}
}
cols
}
fn wraps(&self, col: usize, width: usize) -> bool {
self.wrap[col..col + width].iter().any(|&wrap| wrap)
}
fn spec(&self, col: usize) -> String {
self.span_spec(col, 1)
}
fn paragraph(&self, col: usize, width: usize) -> bool {
let fitted = self.share.iter().any(|&share| share > 0.0);
let wrapping = self.wrap.iter().any(|&wrap| wrap);
self.wraps(col, width) || (fitted && (width > 1 || wrapping))
}
fn cell_spec(&self, col: usize, width: usize, spans: &[Span]) -> String {
let wrapping = self.wrap.iter().any(|&wrap| wrap);
if width > 1 && !wrapping && self.paragraph(col, width) {
let text: String = spans.iter().map(|span| span.text.as_str()).collect();
let share: f32 = self.share[col..col + width].iter().sum();
if text_em(&text) * BOLD_WIDTH_RATIO * self.em_share + 2.0 * self.pad_share <= share {
return self.align[col].to_string();
}
}
self.span_spec(col, width)
}
fn span_spec(&self, col: usize, width: usize) -> String {
let align = self.align[col];
if !self.paragraph(col, width) {
return align.to_string();
}
let share: f32 = self.share[col..col + width].iter().sum();
let ragged = match align {
'c' => "\\centering",
'r' => "\\raggedleft",
_ => "\\raggedright",
};
format!(">{{{ragged}\\arraybackslash}}p{{\\dimexpr{share:.4}\\linewidth-2\\tabcolsep\\relax}}")
}
}
struct TextWidths {
natural: Vec<f32>,
body: Vec<(f32, usize)>,
word: Vec<f32>,
breaks: Vec<bool>,
spans: Vec<(usize, usize, f32)>,
}
impl TextWidths {
fn of(table: &Table, n_cols: usize) -> Self {
let mut widths = Self {
natural: vec![0.0; n_cols],
body: vec![(0.0, 0); n_cols],
word: vec![0.0; n_cols],
breaks: vec![false; n_cols],
spans: Vec::new(),
};
let mut pace = crate::common::cancel::Pace::default();
for (row, cells) in table.rows.iter().enumerate() {
pace.tick();
for cell in cells.iter().filter(|cell| cell.colspan > 1 && cell.col < n_cols) {
let text: String = cell.spans.iter().map(|span| span.text.as_str()).collect();
let bold = row < table.header_rows || cell.spans.iter().any(|span| span.style & BOLD != 0);
let ratio = if bold { BOLD_WIDTH_RATIO } else { 1.0 };
let width = cell.colspan.min(n_cols - cell.col);
widths.spans.push((cell.col, width, text_em(&text) * ratio));
}
for cell in cells.iter().filter(|cell| cell.colspan <= 1 && cell.col < n_cols) {
let text: String = cell.spans.iter().map(|span| span.text.as_str()).collect();
let col = cell.col;
let bold = row < table.header_rows || cell.spans.iter().any(|span| span.style & BOLD != 0);
let ratio = if bold { BOLD_WIDTH_RATIO } else { 1.0 };
let (em, word) = (text_em(&text) * ratio, word_em(&text) * ratio);
widths.natural[col] = widths.natural[col].max(em);
widths.word[col] = widths.word[col].max(word);
widths.breaks[col] |= word < em;
if row >= table.header_rows && em > 0.0 {
widths.body[col].0 += em;
widths.body[col].1 += 1;
}
}
}
widths
}
}
fn em_width_pt(size_pt: f32) -> f32 {
size_pt
* SIZE_WIDTH_RATIOS
.iter()
.find(|(least, _)| size_pt >= *least)
.map_or(1.0, |&(_, ratio)| ratio)
}
fn span_floors(spans: &[(usize, usize, f32)], least: &[f32], share_of: impl Fn(f32) -> f32) -> Vec<f32> {
let mut floors = vec![0.0f32; least.len()];
for &(col, width, em) in spans {
let have: f32 = least[col..col + width].iter().sum();
let missing = share_of(em) - have;
if missing > 0.0 {
for column in col..col + width {
floors[column] = floors[column].max(least[column] + missing / width as f32);
}
}
}
floors
}
fn column_groups(cols: &Columns) -> Vec<Vec<usize>> {
let mut groups: Vec<Vec<usize>> = Vec::new();
let mut width = 0.0;
for col in 1..cols.least.len() {
match groups.last_mut() {
Some(group) if width + cols.least[col] <= COLUMN_GROUP_FILL_RATIO => group.push(col),
_ => {
groups.push(vec![0, col]);
width = cols.least[0];
}
}
width += cols.least[col];
}
if groups.is_empty() {
groups.push(vec![0]);
}
groups
}
fn table_columns(table: &Table, keep: &[usize]) -> Table {
let rows = table
.rows
.iter()
.map(|row| {
row.iter()
.filter_map(|cell| {
let covered: Vec<usize> = (0..keep.len())
.filter(|&new| (cell.col..cell.col + cell.colspan.max(1)).contains(&keep[new]))
.collect();
let first = *covered.first()?;
Some(TableCell {
col: first,
colspan: covered.len(),
rowspan: cell.rowspan,
spans: cell.spans.clone(),
})
})
.collect()
})
.collect();
Table {
n_cols: keep.len(),
header_rows: table.header_rows,
col_align: keep
.iter()
.map(|&col| table.col_align.get(col).copied().unwrap_or("left"))
.collect(),
rows,
rule_above: Vec::new(),
rule_bottom: Vec::new(),
rule_left: Vec::new(),
rule_right: Vec::new(),
geom: table.geom.clone(),
}
}
fn word_em(text: &str) -> f32 {
text.split(|ch: char| ch.is_whitespace() || is_cjk(ch))
.map(text_em)
.fold(if text.chars().any(is_cjk) { CJK_CHAR_EM } else { 0.0 }, f32::max)
}
fn text_em(text: &str) -> f32 {
text.chars()
.map(|ch| match ch {
_ if is_cjk(ch) => CJK_CHAR_EM,
'A'..='Z' | 'm' | 'w' => WIDE_CHAR_EM,
'f' | 'i' | 'j' | 'l' | 'r' | 't' | ' ' | '.' | ',' | ':' | ';' | '\'' | '(' | ')' => NARROW_CHAR_EM,
_ => OTHER_CHAR_EM,
})
.sum()
}
impl AuthorMarkup for Writer<'_, '_> {
fn anchor(&mut self, block: &Block) {
Writer::anchor(self, block);
}
fn lines(&self, block: &Block) -> Vec<String> {
author_lines(
block,
|row| self.text.spans(row),
"\\textbf{",
|name| format!("\\textbf{{{name}}}"),
|line| protect_start(&line),
)
}
fn card(&self, lines: &[String]) -> String {
format!(
"\\begin{{tabular}}[t]{{@{{}}c@{{}}}}\n{}\n\\end{{tabular}}",
lines.join("\\\\\n")
)
}
fn cards(&mut self, cards: &[String]) {
let row = cards.join(&format!("\n\\hspace{{{AUTHOR_GAP}}}\n"));
self.paragraph(&format!("\\begin{{center}}\n{row}\n\\end{{center}}"));
}
fn byline(&mut self, lines: &[String]) {
self.paragraph(&format!("\\begin{{center}}\n{}\n\\end{{center}}", lines.join("\\\\\n")));
}
}
fn mono_ratio(spans: &[Span]) -> f32 {
let count = |mono_only: bool| {
spans
.iter()
.filter(|span| !mono_only || span.style & MONO != 0)
.map(|span| span.text.chars().filter(|ch| !ch.is_whitespace()).count())
.sum::<usize>()
};
let total = count(false);
if total == 0 {
return 0.0;
}
count(true) as f32 / total as f32
}
fn align_letter(table: &Table, col: usize) -> char {
match table.col_align.get(col).copied() {
Some("center") => 'c',
Some("right") => 'r',
_ => 'l',
}
}
fn multicolumn(width: usize, spec: &str, text: String) -> String {
if width > 1 {
format!("\\multicolumn{{{width}}}{{{spec}}}{{{text}}}")
} else {
text
}
}
fn protect_start(text: &str) -> String {
if text.starts_with(['[', '*']) {
format!("{{}}{text}")
} else {
text.to_string()
}
}
fn footnote_texts(notes: &[String]) -> String {
if notes.is_empty() {
return String::new();
}
let texts: String = notes
.iter()
.map(|note| format!("\\stepcounter{{footnote}}\\footnotetext{{{note}}}"))
.collect();
format!("\n\\addtocounter{{footnote}}{{-{}}}{texts}", notes.len())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn picture_folder_is_safe_for_tex() {
assert_eq!(
tex_path("my images/1706.03762v7_assets/"),
"my_images/1706.03762v7_assets"
);
assert_eq!(tex_path("a%b#c"), "a_b_c");
}
#[test]
fn unknown_characters_are_outside_latin() {
let chars: Vec<char> = unknown_chars("Erdős", "é ž 中 ſ").into_iter().collect();
assert_eq!(chars, vec!['ſ', '中']);
}
fn doc_of(text: &str) -> Document {
let mut block = Block::new(0, BlockKind::Paragraph { in_abstract: false });
block.spans = vec![Span::plain(text)];
Document {
meta: crate::model::Meta::default(),
pages: Vec::new(),
body_size: 10.0,
leading: 12.0,
body_font: String::new(),
math_font: String::new(),
blocks: vec![block],
pictures: Vec::new(),
}
}
#[test]
fn cjk_text_gets_cjk_fonts_for_lualatex_and_xelatex() {
for text in ["一貫性のある操作", "ひらがな", "カタカナ", "한국어", "全角,"] {
let tex = document(&doc_of(text), text);
assert!(tex.contains("\\usepackage[match]{luatexja-fontspec}"), "{text}: {tex}");
assert!(tex.contains("\\usepackage{xeCJK}"), "{text}");
assert!(tex.contains("\\setCJKmainfont"), "{text}");
assert!(tex.contains("needs LuaLaTeX or XeLaTeX"), "{text}");
}
}
#[test]
fn xelatex_sets_the_symbols_of_cjk_text_in_the_cjk_font() {
let tex = document(&doc_of("①②の手順 ≦ ― ●"), "①②の手順 ≦ ― ●");
let declared = tex
.lines()
.skip_while(|line| !line.contains("\\xeCJKDeclareCharClass{CJK}"))
.take_while(|line| !line.contains('}') || line.contains("DeclareCharClass"))
.collect::<String>();
for code in ["\"2015", "\"2264->\"2267", "\"2460->", "\"25CE->\"25D3"] {
assert!(declared.contains(code), "{code} not declared: {declared}");
}
assert!(is_cjk('\u{3190}') && is_cjk('\u{319F}') && is_cjk('\u{FE10}') && is_cjk('\u{FE19}'));
}
#[test]
fn symbols_of_cjk_text_are_characters_under_lualatex_and_xelatex() {
let body = escape("△11 523 □ 〈注〉");
let tex = document(&doc_of("人口 △11 523"), &body);
for (symbol, command) in [
("△", "\\triangle"),
("□", "\\square"),
("〈", "\\langle"),
("〉", "\\rangle"),
] {
assert!(
tex.contains(&format!("\\kakemonotext{{{symbol}}}{{{command}}}")),
"{symbol}: {tex}"
);
}
let latin = document(&doc_of("x"), &escape("△ x"));
assert!(latin.contains("\\ensuremath{\\triangle}") && !latin.contains("kakemonotext"));
}
#[test]
fn text_without_cjk_keeps_the_plain_preamble() {
let text = "Erdős café ž";
let tex = document(&doc_of(text), text);
let plain = PREAMBLE.replacen("Converted from a PDF", "Converted from a Word document", 1);
assert!(tex.starts_with(&plain), "{tex}");
assert!(!tex.contains("luatexja") && !tex.contains("xeCJK"));
}
fn table_of(n_cols: usize, body: usize, text: impl Fn(usize, usize) -> String) -> Table {
let row = |row: usize| {
(0..n_cols)
.map(|col| TableCell {
col,
colspan: 1,
rowspan: 1,
spans: vec![Span::plain(text(row, col))],
})
.collect()
};
Table {
n_cols,
header_rows: 1,
col_align: vec!["left"; n_cols],
rows: (0..=body).map(row).collect(),
rule_above: Vec::new(),
rule_bottom: Vec::new(),
rule_left: Vec::new(),
rule_right: Vec::new(),
geom: crate::model::TableGeom::default(),
}
}
fn table_latex(table: Table) -> String {
let mut block = Block::new(
0,
BlockKind::Figure {
kind: FloatKind::Table,
image: None,
label: None,
},
);
block.table = Some(table);
figure_latex(&block)
}
fn figure_latex(block: &Block) -> String {
let anchors = std::collections::HashSet::new();
let html = HtmlOptions::default();
let mut images = ImageStore {
opts: &html,
jobs: Vec::new(),
svg_as_pdf: true,
};
let opts = LatexOptions::default();
let mut writer = Writer {
opts: &opts,
text: Text {
anchors: &anchors,
math: true,
table_notes: std::cell::RefCell::default(),
},
images: &mut images,
out: String::new(),
group: Group::None,
item_open: false,
nested: 0,
after_text: false,
};
writer.figure(block, FloatKind::Table, None);
writer.out
}
#[test]
fn a_short_table_is_one_box() {
let latex = table_latex(table_of(3, 30, |row, col| format!("r{row}c{col}")));
assert!(latex.contains("\\begin{kakemonotable}"), "{latex}");
assert!(!latex.contains("longtable"));
}
#[test]
fn a_long_table_breaks_across_pages_with_its_header_repeated() {
let latex = table_latex(table_of(3, 31, |row, col| format!("r{row}c{col}")));
assert!(!latex.contains("kakemonotable"), "{latex}");
assert!(latex.contains("\\begin{longtable}{lll}"), "{latex}");
let head = latex
.find("r0c0 & r0c1 & r0c2 \\\\\n\\midrule\n\\endhead")
.expect("no repeated header");
assert!(latex.find("r1c0").unwrap() > head);
assert!(latex.contains("\\endhead\n\\bottomrule\n\\endlastfoot\n"), "{latex}");
assert!(
latex.contains("r31c0 & r31c1 & r31c2 \\\\\n\\end{longtable}"),
"{latex}"
);
}
#[test]
fn a_long_table_with_many_columns_is_set_smaller() {
let latex = table_latex(table_of(8, 40, |row, col| format!("{row}.{col}")));
assert!(
latex.contains("\\begingroup\\footnotesize\\setlength{\\tabcolsep}{3pt}\n\\begin{longtable}"),
"{latex}"
);
}
#[test]
fn a_long_table_too_wide_for_the_page_wraps_its_long_columns() {
let latex = table_latex(table_of(4, 40, |row, col| {
if col == 2 {
format!("{row} {}", "a long description ".repeat(10))
} else {
format!("{row}")
}
}));
let spec_line = latex
.lines()
.find(|line| line.starts_with("\\begin{longtable}"))
.unwrap();
let shares: Vec<f32> = spec_line
.split("p{\\dimexpr")
.skip(1)
.map(|part| part.split("\\linewidth").next().unwrap().parse().unwrap())
.collect();
assert_eq!(shares.len(), 4, "{spec_line}");
assert!(shares[2] > shares[0] + shares[1] + shares[3], "{spec_line}");
}
#[test]
fn a_long_table_with_a_header_taller_than_a_quarter_page_shows_it_once() {
let latex = table_latex(table_of(16, 40, |row, col| {
if row == 0 {
"a long header of many words ".repeat(30)
} else {
format!("{row} {col} text of the cell {}", "word ".repeat(col))
}
}));
assert!(
latex.contains("\\midrule\n\\endfirsthead\n\\toprule\n\\endhead"),
"{latex}"
);
let short = table_latex(table_of(16, 40, |row, col| {
format!("{row} {col} text {}", "word ".repeat(col))
}));
assert!(!short.contains("\\endfirsthead") && short.contains("\\endhead"));
}
fn number(row: usize, col: usize) -> String {
format!("{}.{col:02}", (row * 1000 + col * 37) % 99_999)
}
#[test]
fn a_long_table_of_unbreakable_columns_too_wide_is_turned() {
let latex = table_latex(table_of(20, 31, number));
assert!(
latex.contains("\\begin{landscape}") && latex.contains("\\end{landscape}"),
"{latex}"
);
assert!(latex.contains("\\tiny"), "{latex}");
assert!(latex.contains(&number(31, 19)));
}
#[test]
fn a_long_table_too_wide_even_turned_is_cut_into_groups_of_columns() {
let latex = table_latex(table_of(60, 31, number));
let tables = latex.matches("\\begin{longtable}").count();
assert!(tables >= 2, "{tables} tables");
for col in 0..60 {
assert!(latex.contains(&number(31, col)), "column {col} lost");
}
assert_eq!(latex.matches(&format!("{} &", number(31, 0))).count(), tables);
}
#[test]
fn wrapped_columns_are_at_least_one_em_wide() {
let table = table_of(16, 40, |row, col| format!("{row} {col} {}", "語".repeat(10 + col * 20)));
for &(_, _, size_pt, tabcolsep_pt) in LONG_TABLE_SIZES {
let cols = Columns::fitted(&table, size_pt, tabcolsep_pt, TEXT_WIDTH_PT);
for col in (0..16).filter(|&col| cols.wrap[col]) {
let content_pt = cols.share[col] * TEXT_WIDTH_PT - 2.0 * tabcolsep_pt;
assert!(
content_pt >= size_pt * 0.999,
"column {col}: {content_pt} pt at {size_pt} pt"
);
}
}
}
#[test]
fn every_column_of_a_long_table_keeps_one_em_inside_its_padding() {
let table = table_of(16, 40, |row, col| match col {
0 => String::new(),
1 => "x".to_string(),
_ => format!("{row} {col} {}", "語".repeat(10 + col * 20)),
});
for &(_, _, size_pt, tabcolsep_pt) in LONG_TABLE_SIZES {
for width_pt in [TEXT_WIDTH_PT, TEXT_HEIGHT_PT] {
let cols = Columns::fitted(&table, size_pt, tabcolsep_pt, width_pt);
if !cols.wrap.iter().any(|&wrap| wrap) {
continue;
}
for (col, share) in cols.share.iter().enumerate().take(16) {
let content_pt = share * width_pt - 2.0 * tabcolsep_pt;
assert!(
content_pt >= size_pt * 0.999,
"column {col}: {content_pt} pt at {size_pt} pt in {width_pt} pt"
);
}
}
}
}
fn compiled_pages(name: &str, tex: &str) -> Option<Vec<String>> {
let dir = std::env::temp_dir().join(format!("kakemono-{name}-{}", std::process::id()));
std::fs::create_dir_all(&dir).unwrap();
std::fs::write(dir.join("t.tex"), tex).unwrap();
let Ok(out) = std::process::Command::new("pdflatex")
.args(["-interaction=nonstopmode", "-halt-on-error", "t.tex"])
.current_dir(&dir)
.output()
else {
eprintln!("SKIPPED {name}: pdflatex is not installed, the LaTeX was not compiled");
return None;
};
assert!(
out.status.success() && dir.join("t.pdf").is_file(),
"pdflatex failed:\n{}",
String::from_utf8_lossy(&out.stdout)
);
let Ok(text) = std::process::Command::new("pdftotext")
.args(["-layout", "t.pdf", "-"])
.current_dir(&dir)
.output()
else {
eprintln!("SKIPPED {name}: pdftotext is not installed, the PDF text was not checked");
return None;
};
let _ = std::fs::remove_dir_all(&dir);
Some(
String::from_utf8_lossy(&text.stdout)
.split('\u{c}')
.map(str::to_string)
.collect(),
)
}
fn first_run_overfull_alignments(name: &str, tex: &str) -> Option<Vec<f32>> {
let dir = std::env::temp_dir().join(format!("kakemono-{name}-{}", std::process::id()));
std::fs::create_dir_all(&dir).unwrap();
std::fs::write(dir.join("t.tex"), tex).unwrap();
let Ok(out) = std::process::Command::new("pdflatex")
.args(["-interaction=nonstopmode", "-halt-on-error", "t.tex"])
.current_dir(&dir)
.output()
else {
eprintln!("SKIPPED {name}: pdflatex is not installed, the LaTeX was not compiled");
return None;
};
assert!(
out.status.success(),
"pdflatex failed:\n{}",
String::from_utf8_lossy(&out.stdout)
);
let log = std::fs::read(dir.join("t.log")).unwrap();
let _ = std::fs::remove_dir_all(&dir);
let overfull = String::from_utf8_lossy(&log)
.lines()
.filter(|line| line.starts_with("Overfull \\hbox (") && line.contains("in alignment"))
.filter_map(|line| line["Overfull \\hbox (".len()..].split("pt").next()?.parse().ok())
.collect();
Some(overfull)
}
#[test]
fn a_long_table_with_spans_in_its_head_fits_on_the_first_run() {
let span = |col: usize, colspan: usize, rowspan: usize, text: &str| TableCell {
col,
colspan,
rowspan,
spans: vec![Span::plain(text)],
};
let mut form = table_of(20, 40, |row, col| match col {
1 => format!("{row} {}", "a note on what to do ".repeat(4)),
5..=12 if row % 3 == 0 => "y".to_string(),
14 => "Labour office".to_string(),
16 => format!("{row} the witness, with address and phone number"),
19 => "TEL".to_string(),
_ => String::new(),
});
form.rows[0] = vec![
span(2, 4, 1, "Work name"),
span(6, 1, 1, ":"),
span(7, 13, 1, "Outer walls"),
];
let months = [
"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
];
let mut prices = table_of(14, 40, |row, col| match (row, col) {
(_, 0) => format!("{} ...", 1990 + row),
(..=20, _) => "-".to_string(),
_ => format!("1{}.{}0", 80 + (row * 7 + col) % 40, col % 10),
});
prices.header_rows = 2;
prices.rows[0] = vec![
span(0, 1, 2, "reporting year"),
span(1, 12, 1, "reporting month"),
span(13, 1, 2, "yearly average"),
];
prices.rows[1] = months
.iter()
.enumerate()
.map(|(i, month)| span(i + 1, 1, 1, month))
.collect();
prices.rows[2] = vec![span(1, 13, 1, "Wood products for energy production 1 2015 = 100")];
let body = format!("{}\n\n{}", table_latex(form), table_latex(prices));
let tex = document(&doc_of("x"), &body);
let Some(overfull) = first_run_overfull_alignments("long-table-heads", &tex) else {
return;
};
assert!(overfull.iter().all(|&pt| pt <= 5.0), "too wide by {overfull:?} pt");
}
#[test]
fn the_caption_of_a_long_table_stays_with_its_first_row() {
let mut block = Block::new(
0,
BlockKind::Figure {
kind: FloatKind::Table,
image: None,
label: None,
},
);
block.spans = vec![Span::plain("Table 1: CAPTIONMARK")];
block.caption_above = true;
block.table = Some(table_of(2, 31, |row, col| {
if row == 1 && col == 0 {
"FIRSTROW".to_string()
} else {
format!("r{row}c{col}")
}
}));
let table = figure_latex(&block);
for room in (10..=90).step_by(8) {
let body = format!("\\noindent\\rule{{1pt}}{{\\dimexpr\\textheight-{room}pt\\relax}}\n\n{table}");
let tex = document(&doc_of("x"), &body);
let Some(pages) = compiled_pages(&format!("caption-{room}"), &tex) else {
return;
};
let page_of = |mark: &str| pages.iter().position(|page| page.contains(mark));
assert_eq!(
page_of("CAPTIONMARK"),
page_of("FIRSTROW"),
"{room} pt left: the caption is not on the page of the first row"
);
}
}
#[test]
fn long_tables_compile_and_keep_every_column() {
let mut wrapped = table_of(3, 40, |row, col| match col {
0 => format!("{row}"),
1 => format!("row {row} {}", "has a long description ".repeat(6)),
_ => format!("end{row} {}", "and some more words ".repeat(5)),
});
wrapped.header_rows = 2;
wrapped.rows[0] = vec![
TableCell {
col: 0,
colspan: 1,
rowspan: 2,
spans: vec![Span::plain("Key")],
},
TableCell {
col: 1,
colspan: 2,
rowspan: 1,
spans: vec![Span::plain("Both texts")],
},
];
wrapped.rows[1].remove(0);
wrapped.rows[5][1].rowspan = 3;
wrapped.rows[6].remove(1);
wrapped.rows[7].remove(1);
let first = table_latex(wrapped);
assert!(
first.contains("\\multicolumn{2}{>{\\raggedright\\arraybackslash}p{"),
"{first}"
);
assert!(first.contains("\\multirow{3}{=}"), "{first}");
let second = table_latex(table_of(20, 31, number));
let tex = document(&doc_of("x"), &format!("{first}\n\n{second}"));
let Some(pages) = compiled_pages("long-tables", &tex) else {
return;
};
let text = pages.concat();
for value in ["end40", "Both texts", &number(31, 19), &number(31, 0)] {
assert!(text.contains(value), "{value} is not in the PDF");
}
}
fn form() -> Table {
table_of(20, 40, |row, col| match col {
1 => format!("{row} {}", "注意事項の説明文".repeat(4)),
2 => "会社名".to_string(),
5..=12 => {
if row % 3 == 0 {
"年".to_string()
} else {
String::new()
}
}
14 => "〇〇労働基準監督署".to_string(),
15 => "〇〇水道局".to_string(),
16 => format!("{row} 現認者(参考人)の確認(住所、氏名、電話番号)"),
19 => "TEL".to_string(),
_ => String::new(),
})
}
#[test]
fn a_long_table_with_wrapped_columns_sets_every_column_in_its_width() {
let latex = table_latex(form());
let spec = latex
.lines()
.find(|line| line.starts_with("\\begin{longtable}"))
.unwrap();
let columns = spec.matches("p{\\dimexpr").count();
assert_eq!(columns, 20, "{spec}");
}
#[test]
fn short_labels_of_a_wrapped_column_stay_on_one_line() {
let mut table = form();
table.rows[3].retain(|cell| cell.col != 17 && cell.col != 18);
table.rows[3].push(TableCell {
col: 17,
colspan: 2,
rowspan: 1,
spans: vec![Span::plain("〇〇監理役")],
});
for &(_, _, size_pt, tabcolsep_pt) in LONG_TABLE_SIZES {
for width_pt in [TEXT_WIDTH_PT, TEXT_HEIGHT_PT] {
let cols = Columns::fitted(&table, size_pt, tabcolsep_pt, width_pt);
if cols.fits() {
let room_em = (cols.share[15] * width_pt - 2.0 * tabcolsep_pt) / size_pt;
assert!(room_em >= 5.0 - 1e-3, "〇〇水道局 in {room_em} em at {size_pt} pt");
let room_em = ((cols.share[17] + cols.share[18]) * width_pt - 2.0 * tabcolsep_pt) / size_pt;
assert!(room_em >= 5.0 - 1e-3, "〇〇監理役 in {room_em} em at {size_pt} pt");
}
}
}
}
#[test]
fn long_tables_load_their_packages() {
let text = "\\begin{longtable}{ll}\na & b \\\\\n\\end{longtable}";
let tex = document(&doc_of("x"), text);
assert!(tex.contains("\\usepackage{longtable}") && tex.contains("\\usepackage{array}"));
assert!(!document(&doc_of("x"), "x").contains("longtable"));
}
#[test]
fn options_of_items_are_protected() {
assert_eq!(protect_start("[1] Smith"), "{}[1] Smith");
assert_eq!(protect_start("Smith [1]"), "Smith [1]");
}
#[test]
fn table_with_spanning_cells() {
let cell = |col: usize, text: &str, colspan: usize, rowspan: usize| TableCell {
col,
colspan,
rowspan,
spans: vec![Span::plain(text)],
};
let table = Table {
n_cols: 3,
header_rows: 2,
col_align: vec!["left", "center", "right"],
rows: vec![
vec![cell(0, "Model", 1, 2), cell(1, "Score", 2, 1)],
vec![cell(1, "A", 1, 1), cell(2, "B", 1, 1)],
vec![cell(0, "x & y", 1, 1), cell(2, "[3]", 1, 1)],
],
rule_above: Vec::new(),
rule_bottom: Vec::new(),
rule_left: Vec::new(),
rule_right: Vec::new(),
geom: crate::model::TableGeom::default(),
};
let anchors = std::collections::HashSet::new();
let mut images = ImageStore {
opts: &HtmlOptions::default(),
jobs: Vec::new(),
svg_as_pdf: true,
};
let opts = LatexOptions::default();
let writer = Writer {
opts: &opts,
text: Text {
anchors: &anchors,
math: true,
table_notes: RefCell::default(),
},
images: &mut images,
out: String::new(),
group: Group::None,
item_open: false,
nested: 0,
after_text: false,
};
assert_eq!(
writer.tabular(&table),
"\\begin{tabular}{lcr}\n\\toprule\n\\multirow{2}{*}{Model} & \\multicolumn{2}{c}{Score} \\\\\n \
& A & B \\\\\n\\midrule\nx \\& y & & {}[3] \\\\\n\\bottomrule\n\\end{tabular}"
);
}
}