Support resizebox content and PDF image placeholders

This commit is contained in:
gloamxun committed 2026-10-20 00:03:10 +08:00
1 parent 3be8cefe46
commit 9e3164adf6
4 files changed
+84 -7

No files matched your search

-3
View File
@@ -145,9 +145,6 @@ const PRELUDE: &str = r#"
\def\balance{}
\def\noalign#1{}
\def\resizebox#1#2#3{#3}
\def\scalebox#1#2{#2}
\def\rotatebox#1#2{#2}
\def\hsize{\textwidth}
\def\odot{\mathbin{\circ}}
\def\otimes{\mathbin{\times}}
+16 -4
View File
@@ -2674,10 +2674,22 @@ pub(super) fn render_pdf(
} else {
source_dir.join(image_path)
};
let (pixel_width, pixel_height) =
image::image_dimensions(&image_path).map_err(|error| {
format!("could not read image `{}`: {error}", image_path.display())
})?;
let (pixel_width, pixel_height) = match image::image_dimensions(&image_path) {
Ok(size) => size,
Err(error) => {
if image_path
.extension()
.and_then(|extension| extension.to_str())
.is_some_and(|extension| extension.eq_ignore_ascii_case("pdf"))
{
continue;
}
return Err(format!(
"could not read image `{}`: {error}",
image_path.display()
));
}
};
let available_width =
model.page.width_mm - model.margins.left - model.margins.right;
let available_height =
+56
View File
@@ -3775,6 +3775,50 @@ fn vbox_end_primitive(
Ok(())
}
fn graphic_box_content(
input: &mut vm::ExecutionInput<CompilerState>,
token: token::Token,
arguments: usize,
) -> texlang::prelude::Result<()> {
let mut content = Vec::new();
for index in 0..arguments {
let argument = read_unexpanded_argument(input, token, "expects a box argument")?;
if index + 1 == arguments {
content = strip_braced_tokens(&argument)
.iter()
.filter(|token| token.char() != Some('$'))
.cloned()
.collect();
}
}
input
.as_mut()
.expansions_mut()
.extend(content.into_iter().rev());
Ok(())
}
fn resizebox_primitive(
token: token::Token,
input: &mut vm::ExecutionInput<CompilerState>,
) -> texlang::prelude::Result<()> {
graphic_box_content(input, token, 3)
}
fn scalebox_primitive(
token: token::Token,
input: &mut vm::ExecutionInput<CompilerState>,
) -> texlang::prelude::Result<()> {
graphic_box_content(input, token, 2)
}
fn rotatebox_primitive(
token: token::Token,
input: &mut vm::ExecutionInput<CompilerState>,
) -> texlang::prelude::Result<()> {
graphic_box_content(input, token, 2)
}
fn href_primitive(
token: token::Token,
input: &mut vm::ExecutionInput<CompilerState>,
@@ -7172,6 +7216,18 @@ fn built_in_commands() -> HashMap<&'static str, texlang::command::BuiltIn<Compil
texlang_stdlib::input::get_read::<16, CompilerState>()
.with_tag(engine_prefix::global_assignment_tag()),
);
built_ins.insert(
"resizebox",
texlang::command::BuiltIn::new_execution(resizebox_primitive),
);
built_ins.insert(
"scalebox",
texlang::command::BuiltIn::new_execution(scalebox_primitive),
);
built_ins.insert(
"rotatebox",
texlang::command::BuiltIn::new_execution(rotatebox_primitive),
);
built_ins.insert(
"char",
texlang::command::BuiltIn::new_execution(char_primitive),
+12
View File
@@ -1740,3 +1740,15 @@ Dendrite & Input terminal & 100 & a & b \\
let svg = page_svg(&value, 0);
assert!(svg.contains(">Dendrite</text>"));
}
#[test]
fn resizebox_strips_nested_math_delimiters() {
let source = r"\documentclass{article}
\begin{document}
\begin{equation}
\resizebox{1\hsize}{!}{$x_i + y_i$}
\end{equation}
\end{document}";
let value = fresh_preview(source, "resizebox-math.tex");
assert_eq!(value["status"], "ok");
}