Size included graphics from their TeX options
This commit is contained in:
1 parent
0f8aff7290
commit
46f86c771e
4 files changed
+166
-24
No files matched your search
+161
-19
@@ -1656,13 +1656,35 @@ struct TheoremSpec {
|
||||
labels: Vec<String>,
|
||||
}
|
||||
|
||||
/// A width or height requested by `\includegraphics`.
|
||||
#[derive(Clone, Copy, PartialEq, Eq)]
|
||||
enum ImageLength {
|
||||
Absolute(i64),
|
||||
TextWidthPercent(i64),
|
||||
TextHeightPercent(i64),
|
||||
}
|
||||
|
||||
impl ImageLength {
|
||||
fn resolve(self, line_width: i64, text_height: i64) -> i64 {
|
||||
match self {
|
||||
ImageLength::Absolute(value) => value,
|
||||
ImageLength::TextWidthPercent(percent) => line_width.saturating_mul(percent) / 100,
|
||||
ImageLength::TextHeightPercent(percent) => text_height.saturating_mul(percent) / 100,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A `figure` or `table` environment: placement, graphics size and caption.
|
||||
#[derive(Clone, PartialEq, Eq)]
|
||||
struct FloatSpec {
|
||||
kind: FloatKind,
|
||||
placement: FloatPlacement,
|
||||
width: Option<i64>,
|
||||
height: Option<i64>,
|
||||
width: Option<ImageLength>,
|
||||
height: Option<ImageLength>,
|
||||
/// `scale=...` percentage of the image's natural size.
|
||||
scale: Option<i64>,
|
||||
/// `keepaspectratio` when both width and height are requested.
|
||||
keep_aspectratio: bool,
|
||||
caption: Option<String>,
|
||||
caption_starred: bool,
|
||||
/// `\includegraphics` path for a figure.
|
||||
@@ -1962,7 +1984,8 @@ fn float_block(raw: &str) -> Option<SourceParagraph> {
|
||||
}
|
||||
caption = Some(text);
|
||||
}
|
||||
let (width, height, image) = figure_graphic(inner).unwrap_or((None, None, None));
|
||||
let (width, height, image, scale, keep_aspectratio) =
|
||||
figure_graphic(inner).unwrap_or((None, None, None, None, false));
|
||||
let table = embedded_table(inner);
|
||||
Some(SourceParagraph {
|
||||
text: String::new(),
|
||||
@@ -1991,6 +2014,8 @@ fn float_block(raw: &str) -> Option<SourceParagraph> {
|
||||
placement,
|
||||
width,
|
||||
height,
|
||||
scale,
|
||||
keep_aspectratio,
|
||||
caption,
|
||||
caption_starred,
|
||||
image,
|
||||
@@ -2052,7 +2077,15 @@ fn figure_labels(inner: &str) -> Vec<String> {
|
||||
}
|
||||
|
||||
/// Read the path and placeholder dimensions from an `\includegraphics`.
|
||||
fn figure_graphic(inner: &str) -> Option<(Option<i64>, Option<i64>, Option<String>)> {
|
||||
fn figure_graphic(
|
||||
inner: &str,
|
||||
) -> Option<(
|
||||
Option<ImageLength>,
|
||||
Option<ImageLength>,
|
||||
Option<String>,
|
||||
Option<i64>,
|
||||
bool,
|
||||
)> {
|
||||
let index = inner.find("\\includegraphics")?;
|
||||
let after = inner[index + "\\includegraphics".len()..].trim_start();
|
||||
let (options, after) = match after.strip_prefix('[') {
|
||||
@@ -2065,16 +2098,58 @@ fn figure_graphic(inner: &str) -> Option<(Option<i64>, Option<i64>, Option<Strin
|
||||
let (path, _) = braced_group(after)?;
|
||||
let mut width = None;
|
||||
let mut height = None;
|
||||
let mut scale = None;
|
||||
let mut keep_aspectratio = false;
|
||||
for part in options.split(',') {
|
||||
let part = part.trim();
|
||||
if let Some(value) = part.strip_prefix("width=") {
|
||||
width = parse_dimension(value.trim());
|
||||
width = parse_image_length(value.trim());
|
||||
} else if let Some(value) = part.strip_prefix("height=") {
|
||||
height = parse_dimension(value.trim());
|
||||
height = parse_image_length(value.trim());
|
||||
} else if let Some(value) = part.strip_prefix("scale=") {
|
||||
scale = value
|
||||
.trim()
|
||||
.parse::<f64>()
|
||||
.ok()
|
||||
.map(|factor| (factor * 100.0).round().max(1.0) as i64);
|
||||
} else if part == "keepaspectratio" {
|
||||
keep_aspectratio = true;
|
||||
}
|
||||
}
|
||||
let path = path.trim();
|
||||
Some((width, height, (!path.is_empty()).then(|| path.to_string())))
|
||||
Some((
|
||||
width,
|
||||
height,
|
||||
(!path.is_empty()).then(|| path.to_string()),
|
||||
scale,
|
||||
keep_aspectratio,
|
||||
))
|
||||
}
|
||||
|
||||
/// Parse `width=`/`height=` values: dimensions, fractions of the measure, or
|
||||
/// fractions of the text height.
|
||||
fn parse_image_length(value: &str) -> Option<ImageLength> {
|
||||
if let Some(dimension) = parse_dimension(value) {
|
||||
return Some(ImageLength::Absolute(dimension));
|
||||
}
|
||||
for (suffix, height) in [
|
||||
("\\textwidth", false),
|
||||
("\\linewidth", false),
|
||||
("\\hsize", false),
|
||||
("\\textheight", true),
|
||||
("\\vsize", true),
|
||||
] {
|
||||
if let Some(factor) = value.strip_suffix(suffix) {
|
||||
let factor = factor.trim().parse::<f64>().ok()?;
|
||||
let percent = (factor * 100.0).round() as i64;
|
||||
return Some(if height {
|
||||
ImageLength::TextHeightPercent(percent)
|
||||
} else {
|
||||
ImageLength::TextWidthPercent(percent)
|
||||
});
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
/// Every distinct `\includegraphics` path in a source file.
|
||||
@@ -4269,28 +4344,49 @@ fn build_document(paragraphs: &[SourceParagraph], geometry: &PageGeometry) -> Do
|
||||
content.extend(caption.items.clone());
|
||||
}
|
||||
}
|
||||
let text_height = geometry
|
||||
.textheight
|
||||
.or(geometry.vsize)
|
||||
.unwrap_or(550 * SCALE);
|
||||
let resolve = |length: &ImageLength| length.resolve(width, text_height);
|
||||
let mut placed_image = false;
|
||||
if let Some(path) = &spec.image {
|
||||
if let Some(image) = geometry.images.get(path) {
|
||||
let image_width = spec.width.unwrap_or(width).min(width);
|
||||
let image_height = spec.height.unwrap_or_else(|| {
|
||||
if image.width > 0 {
|
||||
(image_width * image.height as i64 / image.width as i64).max(1)
|
||||
} else {
|
||||
image_width / 2
|
||||
}
|
||||
});
|
||||
// Images have no intrinsic TeX resolution, so treat one
|
||||
// pixel as one point at 100%.
|
||||
let natural_width = (image.width as i64).max(1) * SCALE;
|
||||
let natural_height = (image.height as i64).max(1) * SCALE;
|
||||
let scale = spec.scale.unwrap_or(100).max(1);
|
||||
let mut image_width = spec
|
||||
.width
|
||||
.as_ref()
|
||||
.map(resolve)
|
||||
.unwrap_or(natural_width.saturating_mul(scale) / 100);
|
||||
let mut image_height =
|
||||
spec.height.as_ref().map(resolve).unwrap_or_else(|| {
|
||||
if spec.width.is_some() || spec.scale.is_some() {
|
||||
(image_width * natural_height / natural_width).max(1)
|
||||
} else {
|
||||
natural_height.saturating_mul(scale) / 100
|
||||
}
|
||||
});
|
||||
if spec.keep_aspectratio && spec.width.is_some() && spec.height.is_some() {
|
||||
let scale = (image_width as f64 / natural_width as f64)
|
||||
.min(image_height as f64 / natural_height as f64);
|
||||
image_width = (natural_width as f64 * scale).round() as i64;
|
||||
image_height = (natural_height as f64 * scale).round() as i64;
|
||||
}
|
||||
content.push(VerticalItem::Image {
|
||||
width: image_width,
|
||||
height: image_height,
|
||||
width: image_width.max(1),
|
||||
height: image_height.max(1),
|
||||
image: image.clone(),
|
||||
});
|
||||
placed_image = true;
|
||||
}
|
||||
}
|
||||
if !placed_image && (spec.height.is_some() || spec.width.is_some()) {
|
||||
let rule_width = spec.width.unwrap_or(width).min(width);
|
||||
let rule_height = spec.height.unwrap_or(rule_width / 2);
|
||||
let rule_width = spec.width.as_ref().map(resolve).unwrap_or(width);
|
||||
let rule_height = spec.height.as_ref().map(resolve).unwrap_or(rule_width / 2);
|
||||
content.push(VerticalItem::Rule {
|
||||
width: rule_width,
|
||||
height: rule_height,
|
||||
@@ -4856,6 +4952,52 @@ mod source_tests {
|
||||
assert_eq!(image.2.rgb.len(), 200 * 100 * 3);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn graphics_options_size_the_image() {
|
||||
let mut geometry = PageGeometry::default();
|
||||
geometry.line_width = Some(480 * SCALE);
|
||||
geometry.textheight = Some(600 * SCALE);
|
||||
geometry.images.insert(
|
||||
"plot.png".to_string(),
|
||||
RasterImage {
|
||||
width: 200,
|
||||
height: 100,
|
||||
rgb: vec![9; 200 * 100 * 3],
|
||||
},
|
||||
);
|
||||
let cases = [
|
||||
("width=0.5\\textwidth", (240 * SCALE, 120 * SCALE)),
|
||||
(
|
||||
"width=100pt,height=100pt,keepaspectratio",
|
||||
(100 * SCALE, 50 * SCALE),
|
||||
),
|
||||
("scale=0.5", (100 * SCALE, 50 * SCALE)),
|
||||
("", (200 * SCALE, 100 * SCALE)),
|
||||
];
|
||||
for (options, expected) in cases {
|
||||
let source = format!(
|
||||
"\\begin{{figure}}[t]\n\\includegraphics[{options}]{{plot.png}}\n\\end{{figure}}"
|
||||
);
|
||||
let blocks = paragraphs(&source).expect("paragraphs");
|
||||
let document = build_document(&blocks, &geometry);
|
||||
let image = document
|
||||
.units
|
||||
.iter()
|
||||
.flat_map(|unit| &unit.items)
|
||||
.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 in the float");
|
||||
assert_eq!(image, expected, "options: {options}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn figure_graphics_reserve_a_placeholder() {
|
||||
let source = "\\begin{figure}[b]\n\\includegraphics[width=200pt]{plot.pdf}\\caption{Plot}\n\\end{figure}";
|
||||
|
||||
Reference in new issue
Block a user