mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-12-04 04:52:54 -05:00
Export: warn when the pixel scale limit changes a requested width or height
A raster width or height that needs more than 64 or less than 0.01 pixels per point was written at the clamped size with no note. The export now adds a warning naming the requested and the written size, for MCP export and the CLI's --width and --height alike.
This commit is contained in:
1 parent
89509c31e4
commit
eb544fee30
4 files changed
+59
-4
No files matched your search
@@ -196,6 +196,19 @@ fn raster_export_takes_width_height_and_background() {
|
||||
assert_eq!(std::fs::read(&svg).unwrap(), std::fs::read(&plain).unwrap());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn raster_export_warns_when_width_is_clamped() {
|
||||
let source = tmp("clamped-size.vectorcraft");
|
||||
ok(&["run", "--cmd", "file.new", "--params", r#"{"width":16,"height":8}"#, "--export", &source]);
|
||||
let output = tmp("clamped-width.png");
|
||||
let r: Value = serde_json::from_str(&ok(&["convert", &source, &output, "--width", "1920"])).unwrap();
|
||||
assert_eq!(png_size(&output).0, 1024);
|
||||
assert!(r["warnings"].as_array().unwrap().iter().any(|w| w.as_str().is_some_and(|s| s.contains("1920"))), "{r}");
|
||||
for path in [source, output] {
|
||||
let _ = std::fs::remove_file(path);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn raster_export_rejects_invalid_width_height_and_background() {
|
||||
for (flag, value, message) in [
|
||||
|
||||
@@ -1210,12 +1210,29 @@ fn encode_files(doc: &Document, f: &Format, p: &Value) -> Result<Encoded> {
|
||||
vectorcraft_doc::cancel::progress(done as u64, total, &format!("Rendering artboard {} of {total}", done + 1));
|
||||
let region = doc.artboards.get(b).ok_or_else(|| bad(C, format!("no artboard {}", b + 1)))?.rect;
|
||||
// `width` or `height`: each file gets the scale that makes its region that size.
|
||||
if let Some(size) = size {
|
||||
settings.ppi = clamped_ppi(72.0 * size.scale_for(C, region)?);
|
||||
let requested_scale = size.map(|size| size.scale_for(C, region)).transpose()?;
|
||||
if let Some(scale) = requested_scale {
|
||||
settings.ppi = clamped_ppi(72.0 * scale);
|
||||
}
|
||||
let scale = settings.scale();
|
||||
check_format_size(f, region.width() * scale, region.height() * scale)?;
|
||||
vectorcraft_render::raster_size(region, scale).map_err(|e| bad(C, e))?;
|
||||
let (width, height) = vectorcraft_render::raster_size(region, scale).map_err(|e| bad(C, e))?;
|
||||
if let Some(requested_scale) = requested_scale.filter(|scale| !(0.01..=64.0).contains(scale)) {
|
||||
let dimension = match size {
|
||||
Some(ScreenSize::Width(requested)) => Some(("width", requested, width, "wide")),
|
||||
Some(ScreenSize::Height(requested)) => Some(("height", requested, height, "high")),
|
||||
_ => None,
|
||||
};
|
||||
if let Some((axis, requested, written, direction)) = dimension
|
||||
&& f64::from(written) != requested
|
||||
{
|
||||
let limit = if requested_scale > 64.0 { "more than 64" } else { "less than 0.01" };
|
||||
enc.add_setting_notes([format!(
|
||||
"{axis} {requested} px needs {limit} pixels per point: artboard {} was written {written} px {direction}",
|
||||
b + 1
|
||||
)]);
|
||||
}
|
||||
}
|
||||
enc.maps.extend(imagemap::build(doc, region, scale, map, enc.files.len()));
|
||||
let bytes = renderer.export_region(doc, region, format, &settings).map_err(EngineError::Other)?;
|
||||
// File Info as PNG text chunks.
|
||||
|
||||
@@ -397,6 +397,30 @@ fn width_or_height_sets_the_pixel_size_of_a_raster_export() {
|
||||
assert_eq!(size(&mut s, json!({"format": "png", "width": 1})), (2, 1), "the scale stays at 0.01 pixels per point or more");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn raster_pixel_size_clamping_reports_the_requested_and_written_size() {
|
||||
for (points, axis, requested, written, limit, direction) in [
|
||||
((16.0, 8.0), "width", 1920, 1024, "more than 64", "wide"),
|
||||
((16.0, 8.0), "height", 1920, 512, "more than 64", "high"),
|
||||
((200.0, 100.0), "width", 1, 2, "less than 0.01", "wide"),
|
||||
((100.0, 200.0), "height", 1, 2, "less than 0.01", "high"),
|
||||
] {
|
||||
let mut s = session(points.0, points.1, 1);
|
||||
s.paint.stroke = vectorcraft_color::Paint::None;
|
||||
s.execute("shape.rectangle", &json!({"x": 0, "y": 0, "width": points.0, "height": points.1})).unwrap();
|
||||
for extra in [json!({}), json!({"rect": [0, 0, points.0, points.1]}), json!({"useArtboards": false})] {
|
||||
let mut p = json!({"format": "png"});
|
||||
p[axis] = json!(requested);
|
||||
p.as_object_mut().unwrap().extend(extra.as_object().unwrap().clone());
|
||||
let r = s.execute("document.export", &p).unwrap();
|
||||
let size = rgba(&b64(&r)).dimensions();
|
||||
assert_eq!(if axis == "width" { size.0 } else { size.1 }, written, "{p}");
|
||||
let note = format!("{axis} {requested} px needs {limit} pixels per point: artboard 1 was written {written} px {direction}");
|
||||
assert!(r["warnings"].as_array().unwrap().iter().any(|w| w.as_str() == Some(note.as_str())), "{p}: {r}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_width_or_height_that_is_not_a_positive_number_is_refused() {
|
||||
let mut s = session(200.0, 100.0, 1);
|
||||
|
||||
+2
-1
@@ -1578,7 +1578,8 @@ Targa), `background` (`transparent`, `white`, `black` or `"#rrggbb"`; `#rrggbbaa
|
||||
snapped to pixels) and, for PNG, `interlaced`. `width` or `height` sets the size of each file in pixels: each artboard
|
||||
(with `useArtboards`, each file), `rect` or art bounds gets the scale that makes it that wide or high, within 0.01–64
|
||||
pixels per point, and the file stores the resolution that results, as with `ppi`. `width` wins over `height`, and
|
||||
either one wins over `ppi` and `scale`. A value that is not a positive number is refused. CLI `run --export` and
|
||||
either one wins over `ppi` and `scale`. A value that is not a positive number is refused. When that range keeps a
|
||||
file from the requested size, the reply's `warnings` name the requested and the written size. CLI `run --export` and
|
||||
`convert` pass `--width N`, `--height N` and `--background V` on as these options for raster outputs. Export for Screens rows keep
|
||||
their own size (`scale: "512w"`).
|
||||
|
||||
|
||||
Reference in new issue
Block a user