Place standalone graphics as in-text floats

This commit is contained in:
gloamxun committed 2026-10-24 23:42:14 +08:00
1 parent a9614ccee2
commit 476be05163
4 files changed
+101 -5

No files matched your search

+97 -1
View File
@@ -1690,7 +1690,7 @@ struct TheoremSpec {
}
/// A width or height requested by `\includegraphics`.
#[derive(Clone, Copy, PartialEq, Eq)]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum ImageLength {
Absolute(i64),
TextWidthPercent(i64),
@@ -2718,10 +2718,63 @@ fn list_blocks(
}
}
fn standalone_graphic(raw: &str) -> Option<SourceParagraph> {
let trimmed = raw.trim();
let rest = trimmed.strip_prefix("\\includegraphics")?;
let mut rest = rest.trim_start();
if let Some(options) = rest.strip_prefix('[') {
let close = options.find(']')?;
rest = options[close + 1..].trim_start();
}
let (_path, consumed) = braced_group(rest)?;
if !rest[consumed..].trim().is_empty() {
return None;
}
let (width, height, image, scale, keep_aspectratio) = figure_graphic(trimmed)?;
Some(SourceParagraph {
text: String::new(),
font: ParagraphFont::Roman,
line_height: 12,
math: false,
left_skip: 0,
centered: false,
right_aligned: false,
space_before: 8 * SCALE,
space_stretch: 2 * SCALE,
space_shrink: 0,
page_break: false,
equation_number: None,
equation_tag: None,
equation_rows: Vec::new(),
align_centered: false,
heading_level: None,
heading_starred: false,
heading_number: None,
footnotes: Vec::new(),
theorem: None,
font_size: None,
float: Some(FloatSpec {
kind: FloatKind::Figure,
placement: FloatPlacement::Here,
width,
height,
scale,
keep_aspectratio,
caption: None,
caption_starred: false,
image,
table: None,
}),
})
}
fn parse_blocks_raw(raw: &str, definitions: &[TheoremDefinition]) -> Vec<SourceParagraph> {
if let Some(block) = float_block(raw) {
return vec![block];
}
if let Some(block) = standalone_graphic(raw) {
return vec![block];
}
if let Some(blocks) = theorem_block(raw, definitions) {
return blocks;
}
@@ -5002,6 +5055,49 @@ mod source_tests {
assert_eq!(document.units[0].source, "See 1.");
}
#[test]
fn standalone_graphics_place_image_items() {
let source = "\\includegraphics[width=100pt]{pixel.bmp}";
let blocks = paragraphs(source).expect("paragraphs");
let (_, mut geometry) = geometry_and_body(source);
geometry.images.insert(
"pixel.bmp".to_string(),
RasterImage {
width: 2,
height: 2,
rgb: vec![0; 12],
},
);
let document = build_document(&blocks, &geometry);
let image = document
.units
.iter()
.flat_map(|unit| unit.items.iter())
.find_map(|item| match item {
VerticalItem::Float(float) => {
float.content.iter().find_map(|content| match content {
VerticalItem::Image { width, height, .. } => Some((*width, *height)),
_ => None,
})
}
_ => None,
})
.expect("image item inside the graphic float");
assert_eq!(image.0, 100 * SCALE);
assert_eq!(image.1, 100 * SCALE);
}
#[test]
fn standalone_graphics_become_in_text_floats() {
let blocks = paragraphs("\\includegraphics[width=100pt]{pixel.bmp}").expect("paragraphs");
assert_eq!(blocks.len(), 1);
let spec = blocks[0].float.as_ref().expect("graphic float");
assert_eq!(spec.placement, FloatPlacement::Here);
assert_eq!(spec.image.as_deref(), Some("pixel.bmp"));
assert_eq!(spec.width, Some(ImageLength::Absolute(100 * SCALE)));
assert!(spec.caption.is_none());
}
#[test]
fn figure_graphics_place_decoded_images() {
let blocks = paragraphs(