mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-03 06:31:42 +01:00
chore: cut over remaining resvg and roxmltree usages to craft-svg and craft-xml
This commit is contained in:
1 parent
5b27ff6fe3
commit
f0534fe6c1
6 files changed
+30
-25
No files matched your search
Generated
+2
-3
@@ -5414,7 +5414,9 @@ dependencies = [
|
||||
"craft-cms",
|
||||
"craft-flags",
|
||||
"craft-image-codecs",
|
||||
"craft-svg",
|
||||
"craft-update",
|
||||
"craft-xml",
|
||||
"dispatch2",
|
||||
"eframe",
|
||||
"egui",
|
||||
@@ -5438,10 +5440,8 @@ dependencies = [
|
||||
"photocraft-tablet",
|
||||
"photocraft-ui-egui",
|
||||
"plist",
|
||||
"resvg 0.45.1",
|
||||
"rfd",
|
||||
"ron",
|
||||
"roxmltree 0.21.2",
|
||||
"rustix 1.1.5",
|
||||
"serde",
|
||||
"serde_json",
|
||||
@@ -5705,7 +5705,6 @@ dependencies = [
|
||||
"photocraft-text",
|
||||
"photocraft-vector",
|
||||
"proptest",
|
||||
"roxmltree 0.21.2",
|
||||
"serde_json",
|
||||
"thiserror 2.0.21",
|
||||
"tiff",
|
||||
|
||||
@@ -42,7 +42,7 @@ thiserror = { workspace = true }
|
||||
|
||||
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
|
||||
tempfile = "=3.27.0"
|
||||
roxmltree = "0.21"
|
||||
craft-xml = { workspace = true }
|
||||
craft-update = { workspace = true }
|
||||
rfd = { workspace = true }
|
||||
arboard = { version = "3.7", features = ["wayland-data-control"] }
|
||||
@@ -94,8 +94,8 @@ wl-clipboard-rs = { version = "=0.9.4", default-features = false }
|
||||
# Wayland desktop color selection through the existing XDG portal transport.
|
||||
zbus = "=5.19.0"
|
||||
# Caption button glyphs from the desktop's icon theme (`caption_style.rs`): SVG paths only, no
|
||||
# text or images. Already in Cargo.lock (photocraft-io); pinned exactly.
|
||||
resvg = { version = "=0.45.1", default-features = false }
|
||||
# text or images. Already in Cargo.lock (photocraft-io).
|
||||
craft-svg = { workspace = true }
|
||||
# Already used by the portal stack; bounded waits let cancelling close the OS request.
|
||||
futures-lite = "=2.6.1"
|
||||
async-io = "=2.6.0"
|
||||
|
||||
@@ -200,7 +200,7 @@ impl IconTheme {
|
||||
/// An SVG's coverage (alpha) as a `px` × `px` mask, the drawing fitted and centred. `None` for an
|
||||
/// unreadable SVG or an absurd size.
|
||||
pub fn rasterize_mask(svg: &[u8], px: u32) -> Option<Vec<u8>> {
|
||||
use resvg::{tiny_skia, usvg};
|
||||
use craft_svg::tree::{draw, draw::tiny_skia, usvg};
|
||||
// usvg inflates gzip (`.svgz`) input without a decoded-size limit, and icons are plain `.svg`.
|
||||
if !(1..=256).contains(&px) || svg.starts_with(&[0x1f, 0x8b]) {
|
||||
return None;
|
||||
@@ -221,8 +221,7 @@ pub fn rasterize_mask(svg: &[u8], px: u32) -> Option<Vec<u8>> {
|
||||
}
|
||||
let s = px as f32 / side;
|
||||
let (dx, dy) = ((px as f32 - size.width() * s) / 2.0, (px as f32 - size.height() * s) / 2.0);
|
||||
let mut pixmap = tiny_skia::Pixmap::new(px, px)?;
|
||||
resvg::render(&tree, tiny_skia::Transform::from_scale(s, s).post_translate(dx, dy), &mut pixmap.as_mut());
|
||||
let pixmap = draw::render(&tree, tiny_skia::Transform::from_scale(s, s).post_translate(dx, dy), px, px, 0, 64 * 1024 * 1024).ok()?;
|
||||
Some(pixmap.data().as_chunks::<4>().0.iter().map(|[_, _, _, a]| *a).collect())
|
||||
}))
|
||||
.ok()
|
||||
|
||||
@@ -147,9 +147,9 @@ fn export_commands(input: &Path, output: &Path, format: &str) -> Result<[(u8, &'
|
||||
fn source_svg(bytes: &[u8], size: Option<(f64, f64)>) -> Result<Vec<u8>, String> {
|
||||
let text = std::str::from_utf8(bytes).map_err(|_| "Converted SVG is not UTF-8")?;
|
||||
photocraft_io::svg::import_svg_bounded_with_work("Artwork.svg", bytes, 96_000_000).map_err(|e| format!("Converted SVG is unsupported: {e}"))?;
|
||||
let doc = roxmltree::Document::parse(text).map_err(|e| format!("Invalid converted SVG: {e}"))?;
|
||||
let root = doc.root_element();
|
||||
if root.tag_name().name() != "svg" {
|
||||
let root = craft_xml::parse(text.as_bytes()).map_err(|e| format!("Invalid converted SVG: {e}"))?;
|
||||
let local_name = root.name.rsplit_once(':').map_or(root.name.as_str(), |(_, l)| l);
|
||||
if local_name != "svg" {
|
||||
return Err("VectorCraft did not return SVG".into());
|
||||
}
|
||||
let Some((w, h)) = size else {
|
||||
@@ -161,12 +161,12 @@ fn source_svg(bytes: &[u8], size: Option<(f64, f64)>) -> Result<Vec<u8>, String>
|
||||
// The nested root keeps its own width/height, so the outer viewBox must use the same CSS
|
||||
// pixels: `612pt` is 816 user units at SVG's 96 dpi. Other units are rejected below.
|
||||
let dimension = |key| {
|
||||
let value = root.attribute(key)?;
|
||||
let value = root.attr(key)?;
|
||||
let (number, scale) = value.strip_suffix("pt").map_or((value, 1.0), |number| (number, 4.0 / 3.0));
|
||||
number.parse::<f64>().ok().map(|v| v * scale).filter(|v| v.is_finite() && *v > 0.0)
|
||||
};
|
||||
let (iw, ih) = dimension("width").zip(dimension("height")).ok_or("Converted SVG has no finite dimensions")?;
|
||||
let inner = text.get(root.range()).ok_or("Invalid SVG root range")?;
|
||||
let inner = root.to_xml();
|
||||
let result =
|
||||
format!(r#"<svg xmlns="http://www.w3.org/2000/svg" width="{w}" height="{h}" viewBox="0 0 {iw} {ih}" preserveAspectRatio="none">{inner}</svg>"#)
|
||||
.into_bytes();
|
||||
|
||||
@@ -64,8 +64,6 @@ kurbo = { version = "0.13", optional = true }
|
||||
|
||||
[dev-dependencies]
|
||||
lopdf = { version = "0.38", default-features = false }
|
||||
# PDF XMP serialization assertions parse output XML in tests.
|
||||
roxmltree = "0.21"
|
||||
miniz_oxide = "0.9"
|
||||
crc32fast = "1.5"
|
||||
craft-affinity = { workspace = true, features = ["synth"] }
|
||||
|
||||
@@ -442,15 +442,24 @@ fn the_plate_description_joins_the_rdf_element_however_the_packet_spells_it() {
|
||||
d.channels.push(spot("Varnish", photocraft_color::Color::rgb(0.9, 0.9, 0.6), &[1.0; 8]));
|
||||
// The packet must parse as namespaced XML with the plates a description inside its RDF element.
|
||||
let plates_in_rdf = |xmp: &str| {
|
||||
let tree = roxmltree::Document::parse(xmp.trim_start_matches('\u{feff}')).unwrap_or_else(|e| panic!("{e}: {xmp}"));
|
||||
let plates = tree
|
||||
.descendants()
|
||||
.find(|n| n.tag_name().namespace() == Some(crate::pdf::SPOT_PLATES_NS) && n.tag_name().name() == "plates")
|
||||
.unwrap_or_else(|| panic!("no pcsp:plates: {xmp}"));
|
||||
let description = plates.parent_element().unwrap();
|
||||
assert_eq!((description.tag_name().namespace(), description.tag_name().name()), (Some(RDF), "Description"), "{xmp}");
|
||||
let rdf = description.parent_element().unwrap();
|
||||
assert_eq!((rdf.tag_name().namespace(), rdf.tag_name().name()), (Some(RDF), "RDF"), "{xmp}");
|
||||
let canon = craft_xml::Canonical::new(&[("rdf", RDF), ("pcsp", crate::pdf::SPOT_PLATES_NS)]);
|
||||
let tree = craft_xml::Options::canonical(&canon).parse(xmp.trim_start_matches('\u{feff}').as_bytes()).unwrap_or_else(|e| panic!("{e}: {xmp}"));
|
||||
fn child_els(el: &craft_xml::El) -> impl Iterator<Item = &craft_xml::El> {
|
||||
el.children.iter().filter_map(|n| match n {
|
||||
craft_xml::Node::El(e) => Some(e),
|
||||
_ => None,
|
||||
})
|
||||
}
|
||||
let mut stack = vec![&tree];
|
||||
let mut found = false;
|
||||
while let Some(el) = stack.pop() {
|
||||
if el.name == "rdf:RDF" && child_els(el).any(|d| d.name == "rdf:Description" && child_els(d).any(|p| p.name == "pcsp:plates")) {
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
stack.extend(child_els(el));
|
||||
}
|
||||
assert!(found, "no rdf:RDF/rdf:Description/pcsp:plates: {xmp}");
|
||||
assert_eq!(xmp.matches("<?xpacket begin").count(), 1, "one packet, not two: {xmp}");
|
||||
};
|
||||
let head = "<?xpacket begin=\"\u{feff}\" id=\"W5M0MpCehiHzreSzNTczkc9d\"?>\n<x:xmpmeta xmlns:x=\"adobe:ns:meta/\">\n";
|
||||
|
||||
Reference in new issue
Block a user