mirror of
https://github.com/storytold/effectcraft.git
synced 2026-11-19 08:53:09 +01:00
Merge tested main for v0.24.1
This commit is contained in:
113 files changed
+3499
-419
No files matched your search
@@ -28,6 +28,10 @@ jobs:
|
||||
name: Windows installation policy (no app build)
|
||||
runs-on: windows-latest
|
||||
steps:
|
||||
- name: Harden runner
|
||||
uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4
|
||||
with:
|
||||
egress-policy: audit
|
||||
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
||||
with:
|
||||
persist-credentials: false
|
||||
|
||||
@@ -1,6 +1,10 @@
|
||||
name: Release
|
||||
|
||||
on:
|
||||
# A nightly dry run on main: every release build except macOS, nothing signed or released
|
||||
# (craft-common-actions release.yml `dry-run`). Finds release-only breakage before release day.
|
||||
schedule:
|
||||
- cron: "32 1 * * *"
|
||||
push:
|
||||
branches: [release]
|
||||
workflow_dispatch:
|
||||
@@ -29,6 +33,7 @@ jobs:
|
||||
# Intentionally tracked at @main (not SHA-pinned) so org-wide CI improvements, security fixes, and build optimizations propagate without PR churn across all craftapp repos.
|
||||
uses: storytold/craft-common-actions/.github/workflows/release.yml@main
|
||||
with:
|
||||
dry-run: ${{ github.event_name == 'schedule' }}
|
||||
app-name: effectcraft
|
||||
app-title: EffectCraft
|
||||
version: ${{ inputs.version }}
|
||||
|
||||
Generated
+45
-40
@@ -1184,7 +1184,7 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "craft-update"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=1b34eba41e62138fc2438d2734523b302840b45d#1b34eba41e62138fc2438d2734523b302840b45d"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=6e03f65bf45181db77ba6229716e6624db9eb25b#6e03f65bf45181db77ba6229716e6624db9eb25b"
|
||||
dependencies = [
|
||||
"chrono",
|
||||
"dunce",
|
||||
@@ -1194,6 +1194,7 @@ dependencies = [
|
||||
"image",
|
||||
"log",
|
||||
"plist",
|
||||
"rustix 1.1.5",
|
||||
"semver",
|
||||
"serde",
|
||||
"serde_json",
|
||||
@@ -1420,7 +1421,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "effectcraft"
|
||||
version = "0.24.0"
|
||||
version = "0.24.1"
|
||||
dependencies = [
|
||||
"chrono",
|
||||
"cpal",
|
||||
@@ -1448,7 +1449,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "effectcraft-aep"
|
||||
version = "0.24.0"
|
||||
version = "0.24.1"
|
||||
dependencies = [
|
||||
"effectcraft-color",
|
||||
"effectcraft-effects",
|
||||
@@ -1462,7 +1463,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "effectcraft-automation"
|
||||
version = "0.24.0"
|
||||
version = "0.24.1"
|
||||
dependencies = [
|
||||
"effectcraft-engine",
|
||||
"effectcraft-host",
|
||||
@@ -1476,14 +1477,15 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "effectcraft-av1enc"
|
||||
version = "0.24.0"
|
||||
version = "0.24.1"
|
||||
dependencies = [
|
||||
"filmcraft-av1",
|
||||
"tempfile",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "effectcraft-cli"
|
||||
version = "0.24.0"
|
||||
version = "0.24.1"
|
||||
dependencies = [
|
||||
"cpu-time",
|
||||
"effectcraft-automation",
|
||||
@@ -1502,7 +1504,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "effectcraft-color"
|
||||
version = "0.24.0"
|
||||
version = "0.24.1"
|
||||
dependencies = [
|
||||
"proptest",
|
||||
"serde",
|
||||
@@ -1510,7 +1512,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "effectcraft-effects"
|
||||
version = "0.24.0"
|
||||
version = "0.24.1"
|
||||
dependencies = [
|
||||
"chrono",
|
||||
"effectcraft-color",
|
||||
@@ -1528,7 +1530,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "effectcraft-engine"
|
||||
version = "0.24.0"
|
||||
version = "0.24.1"
|
||||
dependencies = [
|
||||
"chrono",
|
||||
"craft-docpath",
|
||||
@@ -1567,7 +1569,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "effectcraft-export"
|
||||
version = "0.24.0"
|
||||
version = "0.24.1"
|
||||
dependencies = [
|
||||
"effectcraft-av1enc",
|
||||
"effectcraft-color",
|
||||
@@ -1592,13 +1594,14 @@ dependencies = [
|
||||
"image",
|
||||
"log",
|
||||
"rayon",
|
||||
"tempfile",
|
||||
"thiserror 2.0.21",
|
||||
"web-time",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "effectcraft-expr"
|
||||
version = "0.24.0"
|
||||
version = "0.24.1"
|
||||
dependencies = [
|
||||
"boa_engine",
|
||||
"effectcraft-color",
|
||||
@@ -1615,7 +1618,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "effectcraft-geom"
|
||||
version = "0.24.0"
|
||||
version = "0.24.1"
|
||||
dependencies = [
|
||||
"proptest",
|
||||
"serde",
|
||||
@@ -1623,7 +1626,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "effectcraft-gpu"
|
||||
version = "0.24.0"
|
||||
version = "0.24.1"
|
||||
dependencies = [
|
||||
"bytemuck",
|
||||
"effectcraft-color",
|
||||
@@ -1646,7 +1649,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "effectcraft-helper-host"
|
||||
version = "0.24.0"
|
||||
version = "0.24.1"
|
||||
dependencies = [
|
||||
"bytemuck",
|
||||
"effectcraft-effects",
|
||||
@@ -1657,14 +1660,14 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "effectcraft-hevcenc"
|
||||
version = "0.24.0"
|
||||
version = "0.24.1"
|
||||
dependencies = [
|
||||
"filmcraft-hevc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "effectcraft-host"
|
||||
version = "0.24.0"
|
||||
version = "0.24.1"
|
||||
dependencies = [
|
||||
"effectcraft-engine",
|
||||
"effectcraft-export",
|
||||
@@ -1691,7 +1694,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "effectcraft-interchange"
|
||||
version = "0.24.0"
|
||||
version = "0.24.1"
|
||||
dependencies = [
|
||||
"effectcraft-color",
|
||||
"effectcraft-keyframe",
|
||||
@@ -1710,7 +1713,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "effectcraft-keyframe"
|
||||
version = "0.24.0"
|
||||
version = "0.24.1"
|
||||
dependencies = [
|
||||
"effectcraft-color",
|
||||
"effectcraft-geom",
|
||||
@@ -1722,7 +1725,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "effectcraft-lottie"
|
||||
version = "0.24.0"
|
||||
version = "0.24.1"
|
||||
dependencies = [
|
||||
"craft-docpath",
|
||||
"effectcraft-color",
|
||||
@@ -1740,7 +1743,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "effectcraft-media"
|
||||
version = "0.24.0"
|
||||
version = "0.24.1"
|
||||
dependencies = [
|
||||
"craft-docpath",
|
||||
"effectcraft-color",
|
||||
@@ -1768,12 +1771,13 @@ dependencies = [
|
||||
"log",
|
||||
"rayon",
|
||||
"serde_json",
|
||||
"tempfile",
|
||||
"thiserror 2.0.21",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "effectcraft-model"
|
||||
version = "0.24.0"
|
||||
version = "0.24.1"
|
||||
dependencies = [
|
||||
"effectcraft-geom",
|
||||
"image",
|
||||
@@ -1786,14 +1790,14 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "effectcraft-opusenc"
|
||||
version = "0.24.0"
|
||||
version = "0.24.1"
|
||||
dependencies = [
|
||||
"filmcraft-opus",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "effectcraft-path"
|
||||
version = "0.24.0"
|
||||
version = "0.24.1"
|
||||
dependencies = [
|
||||
"effectcraft-geom",
|
||||
"effectcraft-keyframe",
|
||||
@@ -1805,7 +1809,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "effectcraft-pdf"
|
||||
version = "0.24.0"
|
||||
version = "0.24.1"
|
||||
dependencies = [
|
||||
"craft-pdf",
|
||||
"effectcraft-raster",
|
||||
@@ -1821,7 +1825,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "effectcraft-plugin"
|
||||
version = "0.24.0"
|
||||
version = "0.24.1"
|
||||
dependencies = [
|
||||
"effectcraft-effects",
|
||||
"effectcraft-keyframe",
|
||||
@@ -1833,7 +1837,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "effectcraft-project"
|
||||
version = "0.24.0"
|
||||
version = "0.24.1"
|
||||
dependencies = [
|
||||
"craft-ecproj-format",
|
||||
"effectcraft-color",
|
||||
@@ -1847,14 +1851,14 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "effectcraft-psd"
|
||||
version = "0.24.0"
|
||||
version = "0.24.1"
|
||||
dependencies = [
|
||||
"miniz_oxide 0.9.1",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "effectcraft-raster"
|
||||
version = "0.24.0"
|
||||
version = "0.24.1"
|
||||
dependencies = [
|
||||
"effectcraft-color",
|
||||
"effectcraft-geom",
|
||||
@@ -1864,7 +1868,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "effectcraft-render"
|
||||
version = "0.24.0"
|
||||
version = "0.24.1"
|
||||
dependencies = [
|
||||
"effectcraft-color",
|
||||
"effectcraft-effects",
|
||||
@@ -1887,7 +1891,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "effectcraft-sam2"
|
||||
version = "0.24.0"
|
||||
version = "0.24.1"
|
||||
dependencies = [
|
||||
"bytemuck",
|
||||
"candle-core",
|
||||
@@ -1902,7 +1906,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "effectcraft-script"
|
||||
version = "0.24.0"
|
||||
version = "0.24.1"
|
||||
dependencies = [
|
||||
"boa_engine",
|
||||
"effectcraft-engine",
|
||||
@@ -1920,7 +1924,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "effectcraft-segment"
|
||||
version = "0.24.0"
|
||||
version = "0.24.1"
|
||||
dependencies = [
|
||||
"miniz_oxide 0.9.1",
|
||||
"ndarray",
|
||||
@@ -1929,19 +1933,20 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "effectcraft-svg"
|
||||
version = "0.24.0"
|
||||
version = "0.24.1"
|
||||
dependencies = [
|
||||
"effectcraft-geom",
|
||||
"effectcraft-path",
|
||||
"effectcraft-raster",
|
||||
"kurbo",
|
||||
"log",
|
||||
"rayon",
|
||||
"roxmltree",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "effectcraft-text"
|
||||
version = "0.24.0"
|
||||
version = "0.24.1"
|
||||
dependencies = [
|
||||
"effectcraft-keyframe",
|
||||
"harfrust",
|
||||
@@ -1953,7 +1958,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "effectcraft-time"
|
||||
version = "0.24.0"
|
||||
version = "0.24.1"
|
||||
dependencies = [
|
||||
"proptest",
|
||||
"serde",
|
||||
@@ -1961,7 +1966,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "effectcraft-track"
|
||||
version = "0.24.0"
|
||||
version = "0.24.1"
|
||||
dependencies = [
|
||||
"effectcraft-raster",
|
||||
"effectcraft-segment",
|
||||
@@ -1973,7 +1978,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "effectcraft-ui-egui"
|
||||
version = "0.24.0"
|
||||
version = "0.24.1"
|
||||
dependencies = [
|
||||
"bytemuck",
|
||||
"chrono",
|
||||
@@ -2014,11 +2019,11 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "effectcraft-vp9enc"
|
||||
version = "0.24.0"
|
||||
version = "0.24.1"
|
||||
|
||||
[[package]]
|
||||
name = "effectcraft-web"
|
||||
version = "0.24.0"
|
||||
version = "0.24.1"
|
||||
dependencies = [
|
||||
"effectcraft-engine",
|
||||
"effectcraft-export",
|
||||
@@ -7835,7 +7840,7 @@ checksum = "e450f9b2ed1dff33c94c12589a87338689467b9c4f5d8a5710bd09a847d2c8a7"
|
||||
|
||||
[[package]]
|
||||
name = "xtask"
|
||||
version = "0.24.0"
|
||||
version = "0.24.1"
|
||||
dependencies = [
|
||||
"chrono",
|
||||
"pkg-config",
|
||||
|
||||
+2
-2
@@ -4,7 +4,7 @@ members = ["crates/*", "apps/*", "xtask"]
|
||||
default-members = ["crates/*", "apps/*"]
|
||||
|
||||
[workspace.package]
|
||||
version = "0.24.0"
|
||||
version = "0.24.1"
|
||||
edition = "2024"
|
||||
license = "MIT OR Apache-2.0"
|
||||
rust-version = "1.95"
|
||||
@@ -16,7 +16,7 @@ craft-docpath = { version = "0.1.0", git = "https://github.com/storytold/craft-l
|
||||
craft-quit = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "e11f9d9aebea5f6b8e74643c1ea1ed172ba5f545" }
|
||||
craft-ecproj-format = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "ca1b5e2edb8568335c6c14f390307bd5beae8238" }
|
||||
# craft-update: app-specific pretend versions apply to startup and manual checks.
|
||||
craft-update = { git = "https://github.com/storytold/craft-libs", version = "=0.1.0", rev = "1b34eba41e62138fc2438d2734523b302840b45d", features = ["egui"] }
|
||||
craft-update = { git = "https://github.com/storytold/craft-libs", version = "=0.1.0", rev = "6e03f65bf45181db77ba6229716e6624db9eb25b", features = ["egui"] }
|
||||
# internal
|
||||
effectcraft-time = { path = "crates/time" }
|
||||
effectcraft-geom = { path = "crates/geom" }
|
||||
|
||||
@@ -231,15 +231,21 @@ pub(crate) fn import_transform(
|
||||
}
|
||||
import_expression(&node, prop, col);
|
||||
}
|
||||
if let Some(PropNode::Other(_)) = group.get("ADBE Orientation") {
|
||||
if let Some(PropNode::Other(otst)) = group.get("ADBE Orientation") {
|
||||
match (layer.orientation(), target.get_mut("orientation")) {
|
||||
(Some(o), Some(prop)) if !o.animated && !o.has_expression && sane(&Value::Vec3(o.degrees)) => prop.value = Value::Vec3(o.degrees),
|
||||
(Some(o), Some(prop)) if o.animated || sane(&Value::Vec3(o.degrees)) => {
|
||||
if o.animated {
|
||||
import_orientation_keys(layer, layer_name, tm, prop, col);
|
||||
} else {
|
||||
prop.value = Value::Vec3(o.degrees);
|
||||
}
|
||||
// The expression is stored in the `otst`'s `tdbs` as on any property (#3087).
|
||||
if let Some(node) = otst.child_list(b"tdbs").and_then(AepProp::from_chunk) {
|
||||
import_expression(&node, prop, col);
|
||||
}
|
||||
}
|
||||
// A layer without an orientation (an ambient light) loses nothing with the default.
|
||||
(Some(o), None) if !o.animated && !o.has_expression && o.degrees == [0.0; 3] => {}
|
||||
(Some(o), Some(prop)) if o.animated && !o.has_expression => import_orientation_keys(layer, layer_name, tm, prop, col),
|
||||
(Some(o), Some(_)) if o.has_expression => {
|
||||
col.lose(LossCategory::TransformProperties, format!("{layer_name}: ADBE Orientation expression (not decoded)"));
|
||||
}
|
||||
_ => col.lose(LossCategory::TransformProperties, format!("{layer_name}: ADBE Orientation")),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -98,8 +98,12 @@ pub struct Loss {
|
||||
pub category: LossCategory,
|
||||
/// How many occurrences (exact).
|
||||
pub count: usize,
|
||||
/// Up to [`MAX_DETAILS`] example lines.
|
||||
/// Example lines: up to [`MAX_DETAILS`], and for effects with no counterpart one more line per
|
||||
/// effect (with its number of instances).
|
||||
pub details: Vec<String>,
|
||||
/// How many of the `count` occurrences no line of `details` describes.
|
||||
#[serde(default)]
|
||||
pub unlisted: usize,
|
||||
}
|
||||
|
||||
/// What arrived in the project.
|
||||
@@ -281,7 +285,7 @@ const MAX_UNMAPPED_EFFECTS: usize = 100;
|
||||
impl Collector {
|
||||
/// Records one loss.
|
||||
pub fn lose(&mut self, category: LossCategory, detail: impl Into<String>) {
|
||||
let loss = self.losses.entry(category).or_insert_with(|| Loss { category, count: 0, details: Vec::new() });
|
||||
let loss = self.losses.entry(category).or_insert_with(|| Loss { category, count: 0, details: Vec::new(), unlisted: 0 });
|
||||
loss.count = loss.count.saturating_add(1);
|
||||
if loss.details.len() < MAX_DETAILS {
|
||||
loss.details.push(detail.into());
|
||||
@@ -292,7 +296,8 @@ impl Collector {
|
||||
/// made in [`Collector::finish`], one per effect with the number of instances, so that many
|
||||
/// other losses cannot hide which effects were dropped.
|
||||
pub fn lose_unmapped_effect(&mut self, match_name: &str, detail: String) {
|
||||
let loss = self.losses.entry(LossCategory::Effects).or_insert_with(|| Loss { category: LossCategory::Effects, count: 0, details: Vec::new() });
|
||||
let loss =
|
||||
self.losses.entry(LossCategory::Effects).or_insert_with(|| Loss { category: LossCategory::Effects, count: 0, details: Vec::new(), unlisted: 0 });
|
||||
loss.count = loss.count.saturating_add(1);
|
||||
if let Some(seen) = self.unmapped_effects.get_mut(match_name) {
|
||||
seen.1 = seen.1.saturating_add(1);
|
||||
@@ -303,12 +308,22 @@ impl Collector {
|
||||
}
|
||||
|
||||
pub fn finish(mut self) -> ImportReport {
|
||||
// Every line describes one occurrence, except an unmapped effect's, which describes all
|
||||
// its instances.
|
||||
for loss in self.losses.values_mut() {
|
||||
loss.unlisted = loss.count.saturating_sub(loss.details.len());
|
||||
}
|
||||
if let Some(loss) = self.losses.get_mut(&LossCategory::Effects) {
|
||||
let named = std::mem::take(&mut self.unmapped_effects).into_values().map(|(first, n)| match n {
|
||||
// The most used effects first.
|
||||
let mut named: Vec<(String, usize)> = std::mem::take(&mut self.unmapped_effects).into_values().collect();
|
||||
named.sort_by_key(|n| std::cmp::Reverse(n.1));
|
||||
let described = named.iter().fold(0usize, |sum, (_, n)| sum.saturating_add(*n));
|
||||
loss.unlisted = loss.unlisted.saturating_sub(described);
|
||||
let lines = named.into_iter().map(|(first, n)| match n {
|
||||
1 => first,
|
||||
_ => format!("{first}, and {} more like it", n - 1),
|
||||
});
|
||||
loss.details.splice(0..0, named);
|
||||
loss.details.splice(0..0, lines);
|
||||
}
|
||||
ImportReport {
|
||||
imported: self.imported,
|
||||
@@ -355,6 +370,25 @@ mod tests {
|
||||
// One instance stays the single line it was.
|
||||
assert_eq!(named("(Pseudo/)"), ["C: 'D' has the effect 'Dropdown' (Pseudo/)"]);
|
||||
assert!(effects.details.len() <= MAX_DETAILS + 2, "the examples stay bounded");
|
||||
// 20 parameter lines, the cylinders' line and the dropdown's describe 46; 10 parameters
|
||||
// have no line.
|
||||
assert_eq!(effects.unlisted, 10);
|
||||
// The most used effect comes first.
|
||||
assert!(effects.details[0].contains("(CC Cylinder)"), "{:?}", effects.details);
|
||||
}
|
||||
|
||||
/// #3365: each category says how many of its occurrences have no example line.
|
||||
#[test]
|
||||
fn a_loss_counts_the_occurrences_without_a_line() {
|
||||
let mut col = Collector::default();
|
||||
for i in 0..27 {
|
||||
col.lose(LossCategory::Masks, format!("mask {i}"));
|
||||
}
|
||||
col.lose(LossCategory::ShapeLayers, "one");
|
||||
let report = col.finish();
|
||||
let unlisted = |c| report.losses.iter().find(|l| l.category == c).map(|l| (l.details.len(), l.unlisted));
|
||||
assert_eq!(unlisted(LossCategory::Masks), Some((MAX_DETAILS, 7)));
|
||||
assert_eq!(unlisted(LossCategory::ShapeLayers), Some((1, 0)));
|
||||
}
|
||||
|
||||
/// A file with more than [`MAX_UNMAPPED_EFFECTS`] different unmapped effects names that many
|
||||
@@ -369,5 +403,6 @@ mod tests {
|
||||
let effects = report.losses.iter().find(|l| l.category == LossCategory::Effects).unwrap();
|
||||
assert_eq!(effects.count, MAX_UNMAPPED_EFFECTS + 1, "every instance is counted");
|
||||
assert_eq!(effects.details.len(), MAX_UNMAPPED_EFFECTS, "{} lines", effects.details.len());
|
||||
assert_eq!(effects.unlisted, 1, "the effect past the limit has no line");
|
||||
}
|
||||
}
|
||||
@@ -457,18 +457,22 @@ fn shift_copy_offset(p: &mut effectcraft_project::Property, cx: &mut Cx<'_, '_>)
|
||||
}
|
||||
}
|
||||
|
||||
/// Reports a property whose only sampled value is the default 1.
|
||||
/// Reports a property whose only sampled value is the default 1. A record in the default state
|
||||
/// holds After Effects' default (docs/aep-format.md 3.4, state), so it holds nothing to lose.
|
||||
fn not_default(g: &AepGroup<'_, '_>, match_name: &str, what: &str, cx: &mut Cx<'_, '_>) {
|
||||
if let Some(PropNode::Property(p)) = g.get(match_name)
|
||||
&& stored(&p)
|
||||
&& (p.is_animated() || p.expression().is_some() || p.static_value().and_then(|v| v.first().copied()).is_some_and(|v| v != 1.0))
|
||||
{
|
||||
cx.col.lose(LossCategory::ShapeLayers, format!("{}: {what} not imported", cx.what));
|
||||
}
|
||||
}
|
||||
|
||||
/// Reports a property whose only sampled value is 0.
|
||||
/// Reports a property whose only sampled value is 0 (a record in the default state, as above,
|
||||
/// holds nothing to lose).
|
||||
fn not_zero(g: &AepGroup<'_, '_>, match_name: &str, what: &str, cx: &mut Cx<'_, '_>) {
|
||||
if let Some(PropNode::Property(p)) = g.get(match_name)
|
||||
&& stored(&p)
|
||||
&& (p.is_animated() || p.expression().is_some() || p.static_value().and_then(|v| v.first().copied()).is_some_and(|v| v != 0.0))
|
||||
{
|
||||
cx.col.lose(LossCategory::ShapeLayers, format!("{}: {what} not imported", cx.what));
|
||||
@@ -490,3 +494,41 @@ fn unknown(g: &AepGroup<'_, '_>, known: &[&str], what: &str, cx: &mut Cx<'_, '_>
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::Chunk;
|
||||
|
||||
/// A group holding `match_name` as a one-component property in state `tdsb` with value `v`.
|
||||
fn group_with(match_name: &str, tdsb: u32, v: f64) -> Chunk<'static> {
|
||||
let mut name = match_name.as_bytes().to_vec();
|
||||
name.resize(40, 0);
|
||||
let mut tdb4 = vec![0u8; 124];
|
||||
tdb4[3] = 1;
|
||||
let tdbs = Chunk::list(
|
||||
*b"tdbs",
|
||||
vec![Chunk::data(*b"tdsb", tdsb.to_be_bytes().to_vec()), Chunk::data(*b"tdb4", tdb4), Chunk::data(*b"cdat", v.to_be_bytes().to_vec())],
|
||||
);
|
||||
Chunk::list(*b"tdgp", vec![Chunk::data(*b"tdmn", name), tdbs])
|
||||
}
|
||||
|
||||
fn reported(match_name: &str, tdsb: u32, v: f64, check: fn(&AepGroup<'_, '_>, &str, &str, &mut Cx<'_, '_>)) -> usize {
|
||||
let chunk = group_with(match_name, tdsb, v);
|
||||
let g = AepGroup::from_chunk(&chunk).unwrap();
|
||||
let tm = TimeMap { timescale: 30, frame_duration: 1, rate: crate::import::time::frame_rate(30, 1).unwrap() };
|
||||
let (mut next, mut col) = (1, Collector::default());
|
||||
check(&g, match_name, "it", &mut Cx { what: "L", tm: &tm, ids: &mut Ids(&mut next), col: &mut col });
|
||||
col.finish().losses.iter().map(|l| l.count).sum()
|
||||
}
|
||||
|
||||
/// #3087: a guide rectangle's Shape Direction recorded in the default state (`tdsb` 3, holding
|
||||
/// 2) is After Effects' default and is no loss; written (`tdsb` 1) with 2, it is one.
|
||||
#[test]
|
||||
fn a_default_state_record_is_no_loss() {
|
||||
assert_eq!(reported("ADBE Vector Shape Direction", 3, 2.0, not_default), 0);
|
||||
assert_eq!(reported("ADBE Vector Shape Direction", 1, 2.0, not_default), 1);
|
||||
assert_eq!(reported("ADBE Vector Grad HiLite Length", 3, 5.0, not_zero), 0);
|
||||
assert_eq!(reported("ADBE Vector Grad HiLite Length", 1, 5.0, not_zero), 1);
|
||||
}
|
||||
}
|
||||
@@ -843,6 +843,48 @@ fn a_default_state_colour_record_holds_the_value() {
|
||||
assert_ne!(colour(&default_state), colour(&unchanged), "the recorded colour, not the default");
|
||||
}
|
||||
|
||||
/// #3087: an expression on Orientation arrives with the stored value, as on any property; it was
|
||||
/// dropped together with the value and reported as "not decoded".
|
||||
#[test]
|
||||
fn an_orientation_expression_is_imported_with_its_value() {
|
||||
let bytes = mutate(&mut |c| {
|
||||
if !c.is_list(b"otst") {
|
||||
return;
|
||||
}
|
||||
for child in c.children_mut() {
|
||||
if child.is_list(b"tdbs") {
|
||||
let Body::List { children, .. } = &mut child.body else { continue };
|
||||
for k in children.iter_mut() {
|
||||
if k.is(b"tdsb") {
|
||||
set_data(k, 0, &1u32.to_be_bytes());
|
||||
} else if k.is(b"tdb4") {
|
||||
set_data(k, 120, &[1]);
|
||||
}
|
||||
}
|
||||
children.push(Chunk::data(*b"Utf8", b"[0, time * 90, 0]".to_vec()));
|
||||
} else if child.is_list(b"otky") {
|
||||
for k in child.children_mut() {
|
||||
let degrees: Vec<u8> = [10.0f64, 20.0, 30.0].iter().flat_map(|d| d.to_be_bytes()).collect();
|
||||
set_data(k, 0, °rees);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
let (p, r) = run(&bytes, true);
|
||||
assert_eq!(losses(&r.unwrap(), LossCategory::TransformProperties), Vec::<String>::new());
|
||||
let mut seen = 0;
|
||||
for item in p.items.values() {
|
||||
let ItemKind::Comp(c) = &item.kind else { continue };
|
||||
for layer in &c.layers {
|
||||
let Some(o) = layer.transform().and_then(|t| t.prop("orientation")).filter(|o| o.expr.is_some()) else { continue };
|
||||
assert_eq!(o.value, effectcraft_keyframe::Value::Vec3([10.0, 20.0, 30.0]), "{}", layer.name);
|
||||
assert_eq!(o.expr.as_ref().map(|e| (e.text.as_str(), e.enabled)), Some(("[0, time * 90, 0]", true)), "{}", layer.name);
|
||||
seen += 1;
|
||||
}
|
||||
}
|
||||
assert!(seen > 0);
|
||||
}
|
||||
|
||||
/// `SYNTHETIC` with Levels' Channel and Clip to Output Black / White popups' item names (`pdnm`)
|
||||
/// rewritten to the French labels a French-language After Effects stores there, keeping the same
|
||||
/// item order so the index each popup is set to is unaffected.
|
||||
|
||||
@@ -75,6 +75,9 @@ const REFUSED: &[&str] = &[
|
||||
"library.preview",
|
||||
];
|
||||
|
||||
/// Most numbered folders `check_render_folder` inspects for one output; more are refused.
|
||||
const MAX_NUMBERED_FOLDERS: usize = 4096;
|
||||
|
||||
/// Commands that read files named by these parameters.
|
||||
const READS: &[(&str, &[&str])] = &[
|
||||
("file.open", &["path"]),
|
||||
@@ -202,6 +205,80 @@ impl Confinement {
|
||||
if self.roots(access).any(|r| real.starts_with(r)) { Ok(real) } else { Err(denied("it is outside the automation roots")) }
|
||||
}
|
||||
|
||||
/// A render output's folder holds no symbolic link that leads outside the write roots. An
|
||||
/// image sequence is written to numbered names (`out_[#####].png` → `out_00001.png`, and a
|
||||
/// sequence output without `#` gets `_00001` too) that the check of its template never
|
||||
/// resolved, and each frame file is created through whatever link already sits at its name,
|
||||
/// truncating the file it points to. A link that can't be resolved counts as outside: the
|
||||
/// render would create its target. When the frame number is in a folder name, the same goes for
|
||||
/// each existing numbered folder and the folder it holds the frames in.
|
||||
fn check_render_folder(&self, output: &str) -> Result<()> {
|
||||
let real = self.check(output, Access::Write)?;
|
||||
if let Some(dir) = real.parent() {
|
||||
self.check_no_outward_links(dir, output)?;
|
||||
}
|
||||
// The frame number may sit in a folder name (`shots_[##]/f_[#####].png`): the folders the
|
||||
// frames go to are the numbered ones that exist, which the template's own parent is not.
|
||||
let (before, _, after) = effectcraft_engine::project::render_queue::sequence_parts(output);
|
||||
let sep = ['/', '\\'];
|
||||
let Some((dir_tail, rest)) = after.split_once(sep) else { return Ok(()) };
|
||||
let (parent, prefix) = before.rsplit_once(sep).unwrap_or(("", &before));
|
||||
// A parent that doesn't exist has no numbered folders; one that can't be listed might.
|
||||
let scan = if parent.is_empty() { Path::new("/") } else { Path::new(parent) };
|
||||
let entries = match std::fs::read_dir(scan) {
|
||||
Ok(entries) => entries,
|
||||
Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(()),
|
||||
Err(e) => return Err(Error::BadArgs(format!("cannot render to `{output}`: its numbered folders in `{}` can't be checked: {e}", scan.display()))),
|
||||
};
|
||||
let mut seen = 0;
|
||||
for e in entries {
|
||||
let e =
|
||||
e.map_err(|e| Error::BadArgs(format!("cannot render to `{output}`: its numbered folders in `{}` can't be checked: {e}", scan.display())))?;
|
||||
let name = e.file_name().to_string_lossy().into_owned();
|
||||
let Some(digits) = name.strip_prefix(prefix).and_then(|n| n.strip_suffix(dir_tail)) else { continue };
|
||||
if !digits.strip_prefix('-').unwrap_or(digits).bytes().all(|b| b.is_ascii_digit()) || digits.is_empty() {
|
||||
continue;
|
||||
}
|
||||
seen += 1;
|
||||
if seen > MAX_NUMBERED_FOLDERS {
|
||||
return Err(Error::BadArgs(format!("cannot render to `{output}`: more than {MAX_NUMBERED_FOLDERS} numbered folders already exist to check")));
|
||||
}
|
||||
// The numbered folder itself may be a link out; `check` resolves every existing part.
|
||||
let frame_file = e.path().join(rest);
|
||||
let frame_file = frame_file.to_string_lossy();
|
||||
self.check(&frame_file, Access::Write)?;
|
||||
if let Some(dir) = Path::new(&*frame_file).parent().and_then(resolve) {
|
||||
self.check_no_outward_links(&dir, output)?;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// `dir` holds no symbolic link that leads outside the write roots.
|
||||
fn check_no_outward_links(&self, dir: &Path, output: &str) -> Result<()> {
|
||||
// A folder that does not exist yet holds no link; one that can't be listed might.
|
||||
let cannot = |e: std::io::Error| Error::BadArgs(format!("cannot render to `{output}`: its folder `{}` can't be checked for links: {e}", dir.display()));
|
||||
let entries = match std::fs::read_dir(dir) {
|
||||
Ok(entries) => entries,
|
||||
Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(()),
|
||||
Err(e) => return Err(cannot(e)),
|
||||
};
|
||||
for e in entries {
|
||||
let e = e.map_err(cannot)?;
|
||||
if !e.file_type().map_err(cannot)?.is_symlink() {
|
||||
continue;
|
||||
}
|
||||
let link = e.path();
|
||||
if !link.canonicalize().is_ok_and(|r| self.write.iter().any(|w| r.starts_with(w))) {
|
||||
return Err(Error::BadArgs(format!(
|
||||
"cannot render to `{output}`: its folder holds `{}`, a link that leads outside the automation write roots, where a numbered frame could be written",
|
||||
link.display()
|
||||
)));
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Check engine command `id` with `params` before it runs. Returns what must still be looked
|
||||
/// up and passed to [`Confinement::check_lookup`] for commands that write implicitly.
|
||||
pub fn check_command(&self, id: &str, params: &Value) -> Result<Vec<Lookup>> {
|
||||
@@ -221,6 +298,12 @@ impl Confinement {
|
||||
for step in params.get("steps").and_then(Value::as_array).into_iter().flatten() {
|
||||
let Some(inner) = step.get("command").or_else(|| step.get("id")).and_then(Value::as_str) else { continue };
|
||||
let inner_params = step.get("params").cloned().unwrap_or(Value::Null);
|
||||
// A whole `params` given as a `$N` reference becomes an earlier step's result
|
||||
// only as the batch runs, so none of its file paths would have been checked
|
||||
// (`essential.templateInfo` returns a manifest naming any file at all).
|
||||
if !matches!(inner_params, Value::Null | Value::Object(_)) {
|
||||
return Err(Error::BadArgs(format!("`{inner}` in a batch: `params` must be an object while file access is confined")));
|
||||
}
|
||||
let named = keys_of(READS, inner).or_else(|| keys_of(WRITES, inner)).unwrap_or(&[]);
|
||||
let mixed = keys_of(MIXED_WRITES, inner).unwrap_or(&[]);
|
||||
// A step's `$N` reference is resolved only as the batch runs: never for a file.
|
||||
@@ -258,6 +341,9 @@ impl Confinement {
|
||||
Some(s) => {
|
||||
gave_a_file |= v.is_string();
|
||||
self.check(s, access)?;
|
||||
if QUEUES_AN_OUTPUT.contains(&id) || id.starts_with("renderQueue.") {
|
||||
self.check_render_folder(s)?;
|
||||
}
|
||||
}
|
||||
None => return Err(Error::BadArgs(format!("`{id}`: `{key}` must be a path"))),
|
||||
}
|
||||
@@ -321,11 +407,15 @@ impl Confinement {
|
||||
let items = found.get("items").or(Some(found)).and_then(Value::as_array).cloned().unwrap_or_default();
|
||||
for it in items.iter().filter(|it| it.get("render").and_then(Value::as_bool) != Some(false)) {
|
||||
let extra = it.get("extraOutputs").and_then(Value::as_array).into_iter().flatten().map(|o| o.get("outputPath"));
|
||||
for p in std::iter::once(it.get("outputPath")).chain(extra).chain([it.get("logPath")]).flatten() {
|
||||
for p in std::iter::once(it.get("outputPath")).chain(extra).flatten() {
|
||||
if let Some(p) = p.as_str() {
|
||||
self.check(p, Access::Write)?;
|
||||
self.check_render_folder(p)?;
|
||||
}
|
||||
}
|
||||
if let Some(p) = it.get("logPath").and_then(Value::as_str) {
|
||||
self.check(p, Access::Write)?;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
@@ -757,6 +847,112 @@ mod tests {
|
||||
] {
|
||||
assert!(c.check_command("effect.plugins.install", ¶ms).is_err(), "{params}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_batch_step_whose_params_are_a_reference_is_refused() {
|
||||
let base = std::env::temp_dir().join(format!("ec-confine-batch-ref-{}", std::process::id()));
|
||||
let _ = std::fs::remove_dir_all(&base);
|
||||
let (read, write) = (base.join("in"), base.join("out"));
|
||||
for d in [&read, &write] {
|
||||
std::fs::create_dir_all(d).unwrap();
|
||||
}
|
||||
let c = Confinement::new(std::slice::from_ref(&read), std::slice::from_ref(&write)).unwrap();
|
||||
let info = read.join("t.ectemplate").to_string_lossy().into_owned();
|
||||
// Step 2's params are step 1's result, which can name any `path`.
|
||||
let steps = serde_json::json!({"steps": [
|
||||
{"command": "essential.templateInfo", "params": {"path": info}},
|
||||
{"command": "file.saveAs", "params": "$1"},
|
||||
]});
|
||||
let e = c.check_command("engine.batch", &steps).unwrap_err().to_string();
|
||||
assert!(e.contains("must be an object"), "{e}");
|
||||
for params in [serde_json::json!("$1.manifest"), serde_json::json!(["$1"]), serde_json::json!(7)] {
|
||||
let steps = serde_json::json!({"steps": [{"command": "file.saveAs", "params": params}]});
|
||||
assert!(c.check_command("engine.batch", &steps).is_err(), "{params}");
|
||||
}
|
||||
// Object params (or none) are checked as before.
|
||||
let saved = write.join("p.ecproj").to_string_lossy().into_owned();
|
||||
let steps = serde_json::json!({"steps": [{"command": "file.saveAs", "params": {"path": saved}}, {"command": "edit.undo"}]});
|
||||
assert!(c.check_command("engine.batch", &steps).is_ok());
|
||||
let _ = std::fs::remove_dir_all(&base);
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
#[test]
|
||||
fn a_render_folder_with_a_link_out_of_the_write_roots_is_refused() {
|
||||
let base = std::env::temp_dir().join(format!("ec-confine-seq-link-{}", std::process::id()));
|
||||
let _ = std::fs::remove_dir_all(&base);
|
||||
let (read, write, outside) = (base.join("in"), base.join("out"), base.join("elsewhere"));
|
||||
for d in [&read, &write, &outside] {
|
||||
std::fs::create_dir_all(d).unwrap();
|
||||
}
|
||||
let c = Confinement::new(std::slice::from_ref(&read), std::slice::from_ref(&write)).unwrap();
|
||||
let template = write.join("frames_[#####].png").to_string_lossy().into_owned();
|
||||
let add = serde_json::json!({"output": template});
|
||||
let queue = serde_json::json!({"items": [{"outputPath": template}]});
|
||||
assert!(c.check_command("renderQueue.add", &add).is_ok());
|
||||
assert!(c.check_lookup(&Lookup::RenderOutputs, &queue).is_ok());
|
||||
// A link inside the write root is fine.
|
||||
std::os::unix::fs::symlink(write.join("kept.png"), write.join("frames_00002.png")).unwrap();
|
||||
std::fs::write(write.join("kept.png"), b"x").unwrap();
|
||||
assert!(c.check_lookup(&Lookup::RenderOutputs, &queue).is_ok());
|
||||
// Frame 1's name leads outside: rendering would truncate that file.
|
||||
std::os::unix::fs::symlink(outside.join("victim"), write.join("frames_00001.png")).unwrap();
|
||||
assert!(c.check_command("renderQueue.add", &add).is_err());
|
||||
assert!(c.check_command("render.preRender", &add).is_err());
|
||||
let e = c.check_lookup(&Lookup::RenderOutputs, &queue).unwrap_err().to_string();
|
||||
assert!(e.contains("frames_00001.png"), "{e}");
|
||||
let _ = std::fs::remove_dir_all(&base);
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
#[test]
|
||||
fn a_render_folder_that_cannot_be_listed_is_refused() {
|
||||
use std::os::unix::fs::PermissionsExt;
|
||||
let base = std::env::temp_dir().join(format!("ec-confine-seq-nolist-{}", std::process::id()));
|
||||
let _ = std::fs::remove_dir_all(&base);
|
||||
let write = base.join("out");
|
||||
std::fs::create_dir_all(&write).unwrap();
|
||||
let c = Confinement::new(&[], std::slice::from_ref(&write)).unwrap();
|
||||
let template = write.join("frames_[#####].png").to_string_lossy().into_owned();
|
||||
let add = serde_json::json!({"output": template});
|
||||
// Writable and searchable but not listable: a link in it could not be seen.
|
||||
std::fs::set_permissions(&write, std::fs::Permissions::from_mode(0o300)).unwrap();
|
||||
// A user who can list it anyway (root) sees no difference to test.
|
||||
if std::fs::read_dir(&write).is_err() {
|
||||
assert!(c.check_command("renderQueue.add", &add).is_err());
|
||||
}
|
||||
std::fs::set_permissions(&write, std::fs::Permissions::from_mode(0o700)).unwrap();
|
||||
assert!(c.check_command("renderQueue.add", &add).is_ok());
|
||||
let _ = std::fs::remove_dir_all(&base);
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
#[test]
|
||||
fn numbered_render_folders_with_links_out_of_the_write_roots_are_refused() {
|
||||
let base = std::env::temp_dir().join(format!("ec-confine-seq-dir-{}", std::process::id()));
|
||||
let _ = std::fs::remove_dir_all(&base);
|
||||
let (write, outside) = (base.join("out"), base.join("elsewhere"));
|
||||
for d in [&write, &outside] {
|
||||
std::fs::create_dir_all(d).unwrap();
|
||||
}
|
||||
let c = Confinement::new(&[], std::slice::from_ref(&write)).unwrap();
|
||||
let template = write.join("shot_[###]/f_[#####].png").to_string_lossy().into_owned();
|
||||
let add = serde_json::json!({"output": template});
|
||||
assert!(c.check_command("renderQueue.add", &add).is_ok(), "no numbered folder exists yet");
|
||||
// A numbered folder inside the roots holding a link out is refused.
|
||||
std::fs::create_dir(write.join("shot_001")).unwrap();
|
||||
assert!(c.check_command("renderQueue.add", &add).is_ok());
|
||||
std::os::unix::fs::symlink(outside.join("victim"), write.join("shot_001/f_00001.png")).unwrap();
|
||||
assert!(c.check_command("renderQueue.add", &add).is_err());
|
||||
std::fs::remove_file(write.join("shot_001/f_00001.png")).unwrap();
|
||||
// A numbered folder that is itself a link out is refused.
|
||||
std::os::unix::fs::symlink(&outside, write.join("shot_002")).unwrap();
|
||||
assert!(c.check_command("render.preRender", &add).is_err());
|
||||
// Unrelated names are left alone.
|
||||
std::fs::remove_file(write.join("shot_002")).unwrap();
|
||||
std::os::unix::fs::symlink(&outside, write.join("shot_notes")).unwrap();
|
||||
assert!(c.check_command("renderQueue.add", &add).is_ok());
|
||||
let _ = std::fs::remove_dir_all(&base);
|
||||
}
|
||||
}
|
||||
@@ -12,4 +12,5 @@ workspace = true
|
||||
[dependencies]
|
||||
|
||||
[dev-dependencies]
|
||||
tempfile = { workspace = true }
|
||||
filmcraft-av1 = { workspace = true }
|
||||
@@ -57,8 +57,8 @@ fn check(name: &str, cfg: EncoderConfig, pics: &[Pic]) -> Vec<u32> {
|
||||
recons.push(enc.last_reconstruction().expect("reconstruction"));
|
||||
levels.push(enc.last_loop_filter_level().expect("level"));
|
||||
}
|
||||
let dir = std::env::temp_dir().join(format!("effectcraft-av1enc-{}-{name}", std::process::id()));
|
||||
std::fs::create_dir_all(&dir).expect("temp dir");
|
||||
let guard = tempfile::tempdir().expect("temp dir");
|
||||
let dir = guard.path().to_path_buf();
|
||||
let input = dir.join("in.ivf");
|
||||
let output = dir.join("out.yuv");
|
||||
std::fs::write(&input, ivf(w, h, &packets)).expect("write ivf");
|
||||
|
||||
@@ -228,8 +228,21 @@ fn compound_blur(ctx: &EffectCtx, mut b: Buf) -> Buf {
|
||||
// ---------------------------------------------------------------------------------------------
|
||||
// Camera Lens Blur
|
||||
|
||||
/// Half-open horizontal spans (dy, x_left, x_right) of a polygonal / round iris of radius `r`.
|
||||
/// Largest Camera Lens Blur radius in buffer pixels. Blur Radius goes to 500 in the UI; a project
|
||||
/// or expression value far beyond it would otherwise build an iris table of one row per pixel of
|
||||
/// radius (and sum that many rows per output pixel) before any image size check.
|
||||
pub const CAMERA_LENS_MAX_RADIUS: f64 = 2000.0;
|
||||
|
||||
/// Camera Lens Blur's iris radius in buffer pixels: Blur Radius at the buffer's `scale`, capped
|
||||
/// at [`CAMERA_LENS_MAX_RADIUS`]. Shared by the CPU and GPU paths.
|
||||
pub fn camera_lens_radius(ctx: &EffectCtx, scale: f64) -> f64 {
|
||||
(ctx.params.f("blurRadius").max(0.0) * scale).clamp(0.0, CAMERA_LENS_MAX_RADIUS)
|
||||
}
|
||||
|
||||
/// Half-open horizontal spans (dy, x_left, x_right) of a polygonal / round iris of radius `r`
|
||||
/// (capped at [`CAMERA_LENS_MAX_RADIUS`]).
|
||||
fn iris_spans(r: f64, sides: u32, rotation_deg: f64, roundness: f64) -> Vec<(i64, i64, i64)> {
|
||||
let r = if r.is_nan() { 0.0 } else { r.clamp(0.0, CAMERA_LENS_MAX_RADIUS) };
|
||||
let ri = r.ceil() as i64;
|
||||
let rot = rotation_deg.to_radians();
|
||||
let verts: Vec<(f64, f64)> = (0..sides)
|
||||
@@ -292,7 +305,8 @@ pub fn camera_lens_spans(ctx: &EffectCtx, r: f64) -> (Vec<(i64, i64, i64)>, f64)
|
||||
s.2 = (s.2 as f64 * aspect).round() as i64;
|
||||
}
|
||||
}
|
||||
(spans, r * aspect.max(1.0))
|
||||
// Same cap as iris_spans, so the reach matches the spans for unbounded radii.
|
||||
(spans, r.clamp(0.0, CAMERA_LENS_MAX_RADIUS) * aspect.max(1.0))
|
||||
}
|
||||
|
||||
/// Whether Camera Lens Blur needs more than plain iris spans (Diffraction Fringe or a Blur
|
||||
@@ -302,7 +316,7 @@ pub fn camera_lens_plain(ctx: &EffectCtx) -> bool {
|
||||
}
|
||||
|
||||
fn camera_lens_blur(ctx: &EffectCtx, mut b: Buf) -> Buf {
|
||||
let r = ctx.params.f("blurRadius").max(0.0) * b.scale;
|
||||
let r = camera_lens_radius(ctx, b.scale);
|
||||
if r < 0.5 {
|
||||
return b;
|
||||
}
|
||||
@@ -910,6 +924,19 @@ mod tests {
|
||||
assert!(out.img.get(15 + o, 10 + d)[0] < 0.9, "bright map blurs");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn iris_spans_cap_a_huge_radius() {
|
||||
// A project's Blur Radius far beyond the UI's 500 builds a bounded table.
|
||||
let max_rows = 2 * CAMERA_LENS_MAX_RADIUS as usize + 1;
|
||||
assert!(iris_spans(1e12, 6, 0.0, 0.0).len() <= max_rows);
|
||||
assert!(iris_spans(f64::INFINITY, 0, 0.0, 0.0).len() <= max_rows);
|
||||
assert_eq!(iris_spans(f64::NAN, 0, 0.0, 0.0), vec![(0, 0, 0)]);
|
||||
let params = Params { values: [("blurRadius".into(), num(1e15))].into_iter().collect() };
|
||||
let ctx = EffectCtx { params: ¶ms, time: 0.0, layer_size: [4.0, 4.0], seed: 1, adjustment: false, env: Default::default() };
|
||||
assert_eq!(camera_lens_radius(&ctx, 1.0), CAMERA_LENS_MAX_RADIUS);
|
||||
assert!(camera_lens_spans(&ctx, camera_lens_radius(&ctx, 1.0)).0.len() <= max_rows);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn iris_spans_hexagon_is_symmetric() {
|
||||
let s = iris_spans(10.0, 6, 0.0, 0.0);
|
||||
|
||||
@@ -98,14 +98,14 @@ pub use sim::particle_state;
|
||||
pub use sim3::{PgBlit, PgPlan, playground_plan, playground_state};
|
||||
pub use stylize2::strobe_on;
|
||||
// CPU helpers the GPU kernels share (effectcraft-gpu).
|
||||
pub use blur2::{camera_lens_plain, camera_lens_spans, radial_fast_pad};
|
||||
pub use blur2::{CAMERA_LENS_MAX_RADIUS, camera_lens_plain, camera_lens_radius, camera_lens_spans, radial_fast_pad};
|
||||
pub use generate::{four_color_softening, gen_mode};
|
||||
pub use generate2::pattern_kind as cell_pattern_kind;
|
||||
pub use generate2::{BoltSeg, Contour, Ghost, RadioPlan, Wave, flare_ghosts, lightning_segments, radio_plan};
|
||||
pub use generate3::{
|
||||
Coverage, MarksPlan, PaintPlan, Seg, eyedropper_color, marks_plan, paint_bucket_region, paint_plan, plan_reads_pixels, poly_segs, scribble_style,
|
||||
};
|
||||
pub use noise::GrainLook;
|
||||
pub use noise::{GrainLook, MEDIAN_MAX_RADIUS};
|
||||
pub use textfx::{BoltPlan, TextLook, TextPass, bolt_plan, text_passes};
|
||||
pub use transition::{GridWipe, place_layer};
|
||||
// effectcraft-gpu fx_noise.
|
||||
|
||||
@@ -370,9 +370,16 @@ fn quant(v: f32) -> u16 {
|
||||
(v.clamp(0.0, 1.0) * (NB - 1) as f32).round() as u16
|
||||
}
|
||||
|
||||
/// Largest median window radius (buffer pixels) for Median, Median (Legacy), Dust & Scratches
|
||||
/// and the other median users. Radius goes to 255 in the UI; filling one (2r + 1)² window per
|
||||
/// row costs the square of the radius, so a project value far beyond it is held here (also
|
||||
/// keeping the window's pixel count well inside `u32` / `i32` on the GPU).
|
||||
pub const MEDIAN_MAX_RADIUS: usize = 512;
|
||||
|
||||
/// Per-channel median over a (2r+1)² window of the premultiplied channels (values quantised to
|
||||
/// 512 levels in 0..1, edges repeated).
|
||||
pub(crate) fn median_image(img: &Image, r: usize) -> Image {
|
||||
let r = r.min(MEDIAN_MAX_RADIUS);
|
||||
let (w, h) = (img.width as usize, img.height as usize);
|
||||
if r == 0 || w == 0 || h == 0 {
|
||||
return img.clone();
|
||||
@@ -871,6 +878,17 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn median_radius_is_capped() {
|
||||
// A project's Radius far beyond the UI's 255 takes the capped window, not a
|
||||
// (2r + 1)² fill per row (or an overflow) on a tiny image.
|
||||
let mut img = Image::filled(4, 3, [0.2, 0.3, 0.4, 1.0]);
|
||||
img.set(1, 1, [1.0, 1.0, 1.0, 1.0]);
|
||||
let capped = median_image(&img, MEDIAN_MAX_RADIUS);
|
||||
assert_eq!(median_image(&img, usize::MAX).data, capped.data);
|
||||
assert_eq!(median_image(&img, MEDIAN_MAX_RADIUS + 1000).data, capped.data);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn median_matches_brute_force() {
|
||||
let (w, h) = (9u32, 7u32);
|
||||
|
||||
@@ -301,7 +301,8 @@ pub fn frozen_alpha(params: &Params, layer_size: [f64; 2], fps: f64, f: i64, w:
|
||||
let fr = fc.frames.get(&f)?;
|
||||
let [fw, fh] = fc.size;
|
||||
let expected = fw.checked_mul(fh)?;
|
||||
if fw == 0 || fh == 0 || expected > rle::MAX_PLANE_BYTES {
|
||||
// Only the layer's own full-resolution grid (see Roto Brush's frozen mattes).
|
||||
if fc.size != roto::frozen_grid(layer_size) || fw == 0 || fh == 0 || expected > rle::MAX_PLANE_BYTES {
|
||||
return None;
|
||||
}
|
||||
let a = rle::decode_bounded(&fr.a, expected)?;
|
||||
@@ -719,6 +720,12 @@ mod tests {
|
||||
p.values.insert("objectPoint".into(), Value::Vec2([40.0, 40.0]));
|
||||
p.values.insert(FROZEN.into(), Value::Str(fc2.to_json()));
|
||||
assert_eq!(frozen_alpha(&p, [100.0, 80.0], 25.0, 2, 100, 80, None), None);
|
||||
// A compact plane claiming a size other than the layer's grid is not decoded.
|
||||
let mut fc3 = fc.clone();
|
||||
fc3.size = [4000, 4000];
|
||||
fc3.frames.insert(2, roto::freeze_frame(&vec![1.0; 4000 * 4000], &[]));
|
||||
p.values.insert(FROZEN.into(), Value::Str(fc3.to_json()));
|
||||
assert_eq!(frozen_alpha(&p, [100.0, 80.0], 25.0, 2, 100, 80, None), None);
|
||||
}
|
||||
|
||||
/// Mattes frozen with a model stay usable in a session where that model is not loaded (a
|
||||
|
||||
@@ -614,11 +614,72 @@ pub fn triangulate(outline: &[[f64; 2]], opts: &MeshOpts) -> Mesh {
|
||||
Mesh { verts, tris, outline: poly }
|
||||
}
|
||||
|
||||
/// The Expansion range the outline honours, in layer pixels (the property's slider range). A
|
||||
/// project file can store any number; outside this range it is clamped, so the padded grid
|
||||
/// below stays a few hundred pixels larger than the buffer.
|
||||
pub const EXPANSION_RANGE: (f64, f64) = (-100.0, 200.0);
|
||||
/// Most buffer pixels of padding the outline grid takes on each side (Expansion × buffer scale).
|
||||
const MAX_PAD: f64 = 4096.0;
|
||||
/// Most cells the padded outline grid may have (it is expanded with two `f64` distance buffers).
|
||||
const MAX_GRID_CELLS: usize = 1 << 25;
|
||||
/// Most the alpha is shrunk by, per axis, to make the padded grid fit; a layer needing more has no
|
||||
/// outline.
|
||||
const MAX_DOWNSAMPLE: usize = 8;
|
||||
|
||||
/// The outline of the alpha region under `seed` (buffer pixels → layer space via `scale` and
|
||||
/// `offset`), expanded by `expansion` layer pixels.
|
||||
/// `offset`), expanded by `expansion` layer pixels (clamped to [`EXPANSION_RANGE`]). When the
|
||||
/// padded grid would pass [`MAX_PAD`] or [`MAX_GRID_CELLS`] (a high-scale layer with a large
|
||||
/// Expansion), the alpha is traced at a coarser resolution instead of being skipped.
|
||||
pub fn outline(alpha: &[f32], w: usize, h: usize, scale: f64, offset: [f64; 2], seed: [f64; 2], expansion: f64) -> Vec<[f64; 2]> {
|
||||
// Pad so expansion can grow past the buffer edge.
|
||||
let pad = (expansion.max(0.0) * scale).ceil() as usize + 2;
|
||||
let expansion = if expansion.is_finite() { expansion.clamp(EXPANSION_RANGE.0, EXPANSION_RANGE.1) } else { 0.0 };
|
||||
if alpha.len() < w.saturating_mul(h) {
|
||||
return vec![];
|
||||
}
|
||||
let want = (expansion.max(0.0) * scale).ceil();
|
||||
if !want.is_finite() {
|
||||
return vec![];
|
||||
}
|
||||
for k in 1..=MAX_DOWNSAMPLE {
|
||||
let pad = (want / k as f64).ceil();
|
||||
let (kw, kh) = (w.div_ceil(k), h.div_ceil(k));
|
||||
let fits = pad <= MAX_PAD && {
|
||||
let pad = pad as usize + 2;
|
||||
kw.checked_add(2 * pad).zip(kh.checked_add(2 * pad)).and_then(|(pw, ph)| pw.checked_mul(ph)).is_some_and(|c| c <= MAX_GRID_CELLS)
|
||||
};
|
||||
if !fits {
|
||||
continue;
|
||||
}
|
||||
if k == 1 {
|
||||
return outline_padded(alpha, [w, h], scale, offset, seed, expansion, pad as usize + 2);
|
||||
}
|
||||
let small = shrink(alpha, w, h, k);
|
||||
let kf = k as f64;
|
||||
return outline_padded(&small, [kw, kh], scale / kf, [offset[0] / kf, offset[1] / kf], seed, expansion, pad as usize + 2);
|
||||
}
|
||||
vec![]
|
||||
}
|
||||
|
||||
/// `alpha` (`w × h`) with every `k × k` block averaged into one pixel.
|
||||
fn shrink(alpha: &[f32], w: usize, h: usize, k: usize) -> Vec<f32> {
|
||||
let (kw, kh) = (w.div_ceil(k), h.div_ceil(k));
|
||||
let mut out = vec![0.0f32; kw * kh];
|
||||
for y in 0..kh {
|
||||
for x in 0..kw {
|
||||
let (x1, y1) = (((x + 1) * k).min(w), ((y + 1) * k).min(h));
|
||||
let (mut sum, mut n) = (0.0f32, 0u32);
|
||||
for yy in y * k..y1 {
|
||||
for xx in x * k..x1 {
|
||||
sum += alpha[yy * w + xx];
|
||||
n += 1;
|
||||
}
|
||||
}
|
||||
out[y * kw + x] = sum / n.max(1) as f32;
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
fn outline_padded(alpha: &[f32], [w, h]: [usize; 2], scale: f64, offset: [f64; 2], seed: [f64; 2], expansion: f64, pad: usize) -> Vec<[f64; 2]> {
|
||||
let (pw, ph) = (w + 2 * pad, h + 2 * pad);
|
||||
let mut on = vec![false; pw * ph];
|
||||
for y in 0..h {
|
||||
|
||||
@@ -106,6 +106,42 @@ fn expansion_grows_the_outline() {
|
||||
assert!((am - pi * 225.0).abs() < 60.0, "{am}");
|
||||
}
|
||||
|
||||
/// An imported Expansion far past the property's range is clamped instead of padding the grid
|
||||
/// by that many pixels (a 1e12 expansion would ask for terabytes).
|
||||
#[test]
|
||||
fn hostile_expansion_is_clamped() {
|
||||
let (w, h) = (40, 40);
|
||||
let alpha = disc_alpha(w, h, [20.0, 20.0], 10.0);
|
||||
let max = signed_area(&outline(&alpha, w, h, 1.0, [0.0; 2], [20.0, 20.0], EXPANSION_RANGE.1)).abs();
|
||||
// A non-finite Expansion is taken as zero (the unexpanded outline), not clamped to the maximum.
|
||||
let zero = signed_area(&outline(&alpha, w, h, 1.0, [0.0; 2], [20.0, 20.0], 0.0)).abs();
|
||||
for e in [1e12, f64::MAX, f64::INFINITY, f64::NAN] {
|
||||
let a = signed_area(&outline(&alpha, w, h, 1.0, [0.0; 2], [20.0, 20.0], e)).abs();
|
||||
let want = if e.is_finite() { max } else { zero };
|
||||
assert!((a - want).abs() < 1.0, "{e}: {a} vs {want}");
|
||||
}
|
||||
assert!(outline(&alpha, w, h, 1e9, [0.0; 2], [20.0, 20.0], 100.0).is_empty());
|
||||
let _ = outline(&alpha, w, h, 1.0, [0.0; 2], [20.0, 20.0], -1e12);
|
||||
}
|
||||
|
||||
/// A high-scale layer with a large Expansion is traced at a coarser resolution, not skipped, and a
|
||||
/// tiny buffer with a huge padded grid stays within the grid budget.
|
||||
#[test]
|
||||
fn high_scale_expansion_is_traced_coarser_instead_of_skipped() {
|
||||
let (w, h) = (40, 40);
|
||||
let alpha = disc_alpha(w, h, [20.0, 20.0], 10.0);
|
||||
// Scale 32, Expansion 130: 4160 px of padding, past MAX_PAD, so it is shrunk by 2.
|
||||
let scale = 32.0;
|
||||
let o = outline(&alpha, w, h, scale, [0.0; 2], [20.0, 20.0], 130.0);
|
||||
let area = signed_area(&o).abs();
|
||||
// The disc has radius 10 / 32 layer px; expanded by 130.
|
||||
let r = 10.0 / scale + 130.0;
|
||||
assert!((area - std::f64::consts::PI * r * r).abs() < 0.03 * std::f64::consts::PI * r * r, "{area}");
|
||||
// One pixel at scale 20.48 and Expansion 1e12 (clamped): its 8197 px square grid is over budget.
|
||||
let o = outline(&[1.0], 1, 1, 20.48, [0.0; 2], [0.5, 0.5], 1e12);
|
||||
assert!(o.len() >= 3);
|
||||
}
|
||||
|
||||
fn rect_mesh(w: f64, h: f64, tris: f64) -> Mesh {
|
||||
triangulate(&[[0.0, 0.0], [w, 0.0], [w, h], [0.0, h]], &MeshOpts { triangles: tris, density: 50.0, expansion: 0.0 })
|
||||
}
|
||||
|
||||
@@ -378,10 +378,22 @@ pub fn purge() {
|
||||
c.bytes = 0;
|
||||
}
|
||||
|
||||
/// The pixel size of the layer-space grid at `scale` (`round(size · scale)`, at least 1).
|
||||
fn grid_size(layer_size: [f64; 2], scale: f64) -> (u32, u32) {
|
||||
(((layer_size[0] * scale).round() as u32).max(1), ((layer_size[1] * scale).round() as u32).max(1))
|
||||
}
|
||||
|
||||
/// The full-resolution layer grid frozen mattes are stored at. A frozen cache claiming any
|
||||
/// other size did not come from freezing this layer and is not decoded.
|
||||
pub fn frozen_grid(layer_size: [f64; 2]) -> [usize; 2] {
|
||||
let (w, h) = grid_size(layer_size, 1.0);
|
||||
[w as usize, h as usize]
|
||||
}
|
||||
|
||||
/// The layer-space grid (`round(size · scale)` pixels) of a buffer.
|
||||
pub fn layer_grid(buf: &Buf, layer_size: [f64; 2]) -> Image {
|
||||
let s = if buf.scale > 0.0 { buf.scale } else { 1.0 };
|
||||
let (w, h) = (((layer_size[0] * s).round() as u32).max(1), ((layer_size[1] * s).round() as u32).max(1));
|
||||
let (w, h) = grid_size(layer_size, s);
|
||||
let (ox, oy) = (buf.offset[0].round() as i64, buf.offset[1].round() as i64);
|
||||
let mut img = Image::new(w, h);
|
||||
img.rows_mut().for_each(|(y, row)| {
|
||||
@@ -1212,8 +1224,11 @@ pub fn freeze_frame(alpha: &[f32], band: &[u8]) -> FrozenFrame {
|
||||
fn matte_for(ctx: &EffectCtx, f: i64, img: &Arc<Image>, scale: f64) -> Option<(Vec<f32>, Vec<u8>, f64)> {
|
||||
let (w, h) = (img.width as usize, img.height as usize);
|
||||
// Frozen mattes.
|
||||
// Only the layer's own full-resolution grid: a project cannot make a tiny layer decode (and
|
||||
// convert to floats) planes as large as the RLE allows. Any other size falls through to a computed matte.
|
||||
if let Some(fc) = frozen(ctx.params)
|
||||
&& frozen_applies(&fc, ctx.params, ctx.layer_size, ctx.fps())
|
||||
&& fc.size == frozen_grid(ctx.layer_size)
|
||||
&& let Some(fr) = fc.frames.get(&f)
|
||||
{
|
||||
let [fw, fh] = fc.size;
|
||||
@@ -1409,6 +1424,33 @@ mod tests {
|
||||
params
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mismatched_frozen_size_falls_back_to_a_computed_matte() {
|
||||
let mut d = RotoData::default();
|
||||
d.add(Stroke { kind: StrokeKind::Fg, frame: 0, radius: 3.0, points: vec![[35.0, 40.0], [45.0, 40.0]], object: 0 }, 20, [0, 24]);
|
||||
let mut params = params_with(&d);
|
||||
let fc =
|
||||
FrozenCache { key: frozen_key(¶ms, [100.0, 80.0], 25.0), size: [10, 8], frames: BTreeMap::from([(5, freeze_frame(&[1.0; 80], &[0; 80]))]) };
|
||||
let host = Seq;
|
||||
let render = |params: &Params| {
|
||||
let ctx = EffectCtx {
|
||||
params,
|
||||
time: 5.0 / 25.0 + 0.001,
|
||||
layer_size: [100.0, 80.0],
|
||||
seed: 1,
|
||||
adjustment: false,
|
||||
env: EffectEnv { host: Some(&host), frame_rate: 25.0, ..Default::default() },
|
||||
};
|
||||
crate::apply(&specs()[0], &ctx, Buf { img: frame(5.0 / 25.0 + 0.001), offset: [0.0; 2], scale: 1.0 }).img
|
||||
};
|
||||
let computed = render(¶ms);
|
||||
params.values.insert(FROZEN.into(), Value::Str(fc.to_json()));
|
||||
let out = render(¶ms);
|
||||
assert!(out.data[(40 * 100 + 50) as usize][3] > 0.5, "the disk is missing from the output");
|
||||
// The cache's all-opaque matte would leave the background opaque too; the fallback must match a render with no cache.
|
||||
assert!(out.data == computed.data, "the mismatched frozen cache was used");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn renders_the_segmented_disk_on_demand() {
|
||||
let mut d = RotoData::default();
|
||||
@@ -1980,6 +2022,18 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
/// Frozen mattes are only decoded at the size the freeze pass stores: the layer grid at
|
||||
/// full resolution.
|
||||
#[test]
|
||||
fn frozen_grid_is_the_full_resolution_layer_grid() {
|
||||
for size in [[100.0, 80.0], [99.6, 0.2], [1.0, 1.0]] {
|
||||
let buf = Buf { img: Image::new(3, 3), offset: [0.0; 2], scale: 1.0 };
|
||||
let g = layer_grid(&buf, size);
|
||||
assert_eq!(frozen_grid(size), [g.width as usize, g.height as usize]);
|
||||
}
|
||||
assert_eq!(frozen_grid([100.0, 80.0]), [100, 80]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn saved_tracking_decoders_validate_dimensions_and_plane_lengths() {
|
||||
let valid = rle::encode(&[255, 0, 255, 0]);
|
||||
|
||||
@@ -8,9 +8,11 @@ use super::{Buf, EffectCtx, Image, IntoParallelRefMutIterator, ParallelIterator,
|
||||
/// Steps per second of the stepped particle simulations.
|
||||
pub(crate) const SPS: f64 = 60.0;
|
||||
|
||||
/// Number of whole steps reached at layer time `t`.
|
||||
/// Number of whole steps reached at layer time `t` (at most [`crate::util::MAX_SIM_STEPS`]: the
|
||||
/// per-step histories built from it, such as [`rate_history`], stay bounded however late a
|
||||
/// project places the layer's time).
|
||||
pub(crate) fn steps_at(t: f64) -> u64 {
|
||||
if t <= 0.0 { 0 } else { (t * SPS + 1e-6).floor() as u64 }
|
||||
if t <= 0.0 { 0 } else { ((t * SPS + 1e-6).floor() as u64).min(crate::util::MAX_SIM_STEPS) }
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
|
||||
+72
-13
@@ -562,13 +562,22 @@ struct SimEntry<S> {
|
||||
/// Checkpoint every this many steps.
|
||||
pub const SIM_CHECKPOINT: u64 = 15;
|
||||
|
||||
/// Most steps a simulation runs: three hours of layer time at 60 steps per second. The layer
|
||||
/// time comes from the project (start time, stretch), so without a limit a tiny file could ask
|
||||
/// for years of steps; past this the state stays where it is.
|
||||
pub const MAX_SIM_STEPS: u64 = 60 * 60 * 60 * 3;
|
||||
|
||||
/// Most checkpoints kept per simulation (and made by one run, which spaces them further apart
|
||||
/// as it goes).
|
||||
const MAX_SIM_CHECKPOINTS: usize = 256;
|
||||
|
||||
impl<S: Clone> SimCache<S> {
|
||||
pub const fn new(cap: usize) -> SimCache<S> {
|
||||
SimCache { inner: std::sync::Mutex::new(Vec::new()), cap }
|
||||
}
|
||||
|
||||
/// State after `target` steps. `init` builds step 0; `step(state, i)` advances from step
|
||||
/// `i` to `i + 1`.
|
||||
/// State after `target` steps (at most [`MAX_SIM_STEPS`]). `init` builds step 0;
|
||||
/// `step(state, i)` advances from step `i` to `i + 1`.
|
||||
pub fn run(&self, key: u64, target: u64, init: impl FnOnce() -> S, step: impl FnMut(&mut S, u64)) -> std::sync::Arc<S> {
|
||||
self.run_where(key, target, init, step, |_, _| true)
|
||||
}
|
||||
@@ -586,6 +595,7 @@ impl<S: Clone> SimCache<S> {
|
||||
valid: impl Fn(u64, &S) -> bool,
|
||||
) -> std::sync::Arc<S> {
|
||||
use std::sync::Arc;
|
||||
let target = target.min(MAX_SIM_STEPS);
|
||||
let start = {
|
||||
let mut g = self.inner.lock().unwrap_or_else(std::sync::PoisonError::into_inner);
|
||||
let tick = g.iter().map(|e| e.used).max().unwrap_or(0) + 1;
|
||||
@@ -604,11 +614,17 @@ impl<S: Clone> SimCache<S> {
|
||||
if !had_start {
|
||||
fresh.push((0, Arc::new(st.clone())));
|
||||
}
|
||||
let mut every = SIM_CHECKPOINT;
|
||||
while i < target {
|
||||
step(&mut st, i);
|
||||
i += 1;
|
||||
if i % SIM_CHECKPOINT == 0 && i != target {
|
||||
if i % every == 0 && i != target {
|
||||
fresh.push((i, Arc::new(st.clone())));
|
||||
// A long run keeps a bounded number of states: spread them twice as far apart.
|
||||
if fresh.len() > MAX_SIM_CHECKPOINTS {
|
||||
every = every.saturating_mul(2);
|
||||
fresh.retain(|(s, _)| s % every == 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
let out = Arc::new(st);
|
||||
@@ -628,19 +644,23 @@ impl<S: Clone> SimCache<S> {
|
||||
};
|
||||
let e = &mut g[idx];
|
||||
e.used = tick;
|
||||
for (s, st) in fresh {
|
||||
e.checkpoints.retain(|(c, _)| *c != s);
|
||||
e.checkpoints.push((s, st));
|
||||
}
|
||||
// Keep memory bounded: drop unaligned (exact-target) checkpoints first, then thin.
|
||||
if e.checkpoints.len() > 256 {
|
||||
e.checkpoints.retain(|(s, _)| s % SIM_CHECKPOINT == 0);
|
||||
// Fresh checkpoints replace those at the same step (one pass, not one per checkpoint).
|
||||
let steps: std::collections::HashSet<u64> = fresh.iter().map(|(s, _)| *s).collect();
|
||||
e.checkpoints.retain(|(c, _)| !steps.contains(c));
|
||||
e.checkpoints.extend(fresh);
|
||||
// Keep memory bounded: drop unaligned (exact-target) checkpoints first, then thin. The
|
||||
// state just asked for keeps its slot, so asking for it again is answered from the cache.
|
||||
if e.checkpoints.len() > MAX_SIM_CHECKPOINTS {
|
||||
e.checkpoints.retain(|(s, _)| s % SIM_CHECKPOINT == 0 && *s != target);
|
||||
e.checkpoints.sort_by_key(|(s, _)| *s);
|
||||
let mut k = 0;
|
||||
// Drop only the excess, evenly spaced, so the rest keep their seek distance.
|
||||
let (len, excess) = (e.checkpoints.len(), (e.checkpoints.len() + 1).saturating_sub(MAX_SIM_CHECKPOINTS));
|
||||
let mut i = 0;
|
||||
e.checkpoints.retain(|_| {
|
||||
k += 1;
|
||||
k % 2 == 1
|
||||
i += 1;
|
||||
(i * excess) / len == ((i - 1) * excess) / len
|
||||
});
|
||||
e.checkpoints.push((target, out.clone()));
|
||||
}
|
||||
out
|
||||
}
|
||||
@@ -818,6 +838,45 @@ mod tests {
|
||||
assert_eq!(d, e);
|
||||
}
|
||||
|
||||
/// Layer time from a project cannot run a simulation without end, and a long run keeps a
|
||||
/// bounded number of checkpoints that still give the same answer when seeking.
|
||||
#[test]
|
||||
fn sim_cache_bounds_steps_and_checkpoints() {
|
||||
let c: SimCache<u64> = SimCache::new(2);
|
||||
let count = |s: &mut u64, _: u64| *s += 1;
|
||||
assert_eq!(*c.run(1, u64::MAX, || 0, count), MAX_SIM_STEPS);
|
||||
let n = c.inner.lock().map(|g| g.iter().map(|e| e.checkpoints.len()).max().unwrap_or(0)).unwrap_or(0);
|
||||
assert!(n <= MAX_SIM_CHECKPOINTS + 1, "{n} checkpoints");
|
||||
let fresh: SimCache<u64> = SimCache::new(2);
|
||||
let step = |s: &mut u64, i: u64| *s = s.wrapping_mul(6364136223846793005).wrapping_add(i);
|
||||
let far = *fresh.run(1, 100_000, || 7, step);
|
||||
let mid = *fresh.run(1, 54_321, || 7, step);
|
||||
let again: SimCache<u64> = SimCache::new(2);
|
||||
assert_eq!(*again.run(1, 54_321, || 7, step), mid);
|
||||
assert_eq!(*again.run(1, 100_000, || 7, step), far);
|
||||
}
|
||||
|
||||
/// A request that leaves the cache over its checkpoint cap still finds its own state there.
|
||||
#[test]
|
||||
fn sim_cache_keeps_the_requested_state_when_trimming() {
|
||||
let c: SimCache<u64> = SimCache::new(2);
|
||||
let steps = std::cell::Cell::new(0u64);
|
||||
let count = |s: &mut u64, _: u64| {
|
||||
steps.set(steps.get() + 1);
|
||||
*s += 1;
|
||||
};
|
||||
let target = SIM_CHECKPOINT * (MAX_SIM_CHECKPOINTS as u64 - 1) + SIM_CHECKPOINT - 1;
|
||||
assert_eq!(*c.run(1, target, || 0, count), target);
|
||||
let n = c.inner.lock().map(|g| g.iter().map(|e| e.checkpoints.len()).max().unwrap_or(0)).unwrap_or(0);
|
||||
assert!(n <= MAX_SIM_CHECKPOINTS, "{n} checkpoints");
|
||||
steps.set(0);
|
||||
assert_eq!(*c.run(1, target, || 0, count), target);
|
||||
assert_eq!(steps.get(), 0, "the second request is answered from the cache");
|
||||
// Only the excess is dropped: the other aligned checkpoints stay.
|
||||
let kept = c.inner.lock().map(|g| g[0].checkpoints.len()).unwrap_or(0);
|
||||
assert_eq!(kept, MAX_SIM_CHECKPOINTS, "trimmed to the cap, not halved");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn poly_helpers() {
|
||||
let sq = [[0.0, 0.0], [10.0, 0.0], [10.0, 10.0], [0.0, 10.0]];
|
||||
|
||||
@@ -395,6 +395,9 @@ fn collect_files(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
// reads (other footage, a proxy, the project file itself), and then nothing is written.
|
||||
let protected = s.protected_files();
|
||||
let mut used_names = BTreeSet::new();
|
||||
// Where each file name's numbering resumes: a name repeated many times is numbered from its
|
||||
// own file name, not by prefixing the previous candidate again.
|
||||
let mut next_k: BTreeMap<String, usize> = BTreeMap::new();
|
||||
let mut plan: Vec<(ItemId, Vec<(String, String)>)> = vec![];
|
||||
for it in s.project.items.values() {
|
||||
let ItemKind::Footage(f) = &it.kind else { continue };
|
||||
@@ -404,11 +407,17 @@ fn collect_files(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let files: Vec<&String> = if f.sequence.is_empty() { vec![&f.path] } else { f.sequence.iter().collect() };
|
||||
let mut pairs = vec![];
|
||||
for src in files {
|
||||
let mut name = std::path::Path::new(src).file_name().map(|n| n.to_string_lossy().to_string()).unwrap_or_else(|| "footage".into());
|
||||
let mut k = 2;
|
||||
while !used_names.insert(name.clone()) {
|
||||
name = format!("{k}_{name}");
|
||||
k += 1;
|
||||
let file = std::path::Path::new(src).file_name().map(|n| n.to_string_lossy().to_string()).unwrap_or_else(|| "footage".into());
|
||||
let mut name = file.clone();
|
||||
if !used_names.insert(name.clone()) {
|
||||
let k = next_k.entry(file.clone()).or_insert(2);
|
||||
loop {
|
||||
name = format!("{k}_{file}");
|
||||
*k += 1;
|
||||
if used_names.insert(name.clone()) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
let dst = format!("{folder}/(Footage)/{name}");
|
||||
// A file that is already where its copy goes stays (it is not copied onto itself).
|
||||
|
||||
@@ -9,6 +9,8 @@
|
||||
//! premultiplied): the composite as `R`, `G`, `B`, `A` plus one `<layer name>.R/G/B/A` channel
|
||||
//! set per layer (the layer-prefixed channel convention multi-layer EXR readers use).
|
||||
|
||||
use std::collections::{HashMap, HashSet};
|
||||
|
||||
use effectcraft_color::BlendMode;
|
||||
use effectcraft_project::{ItemId, LayerId, Project};
|
||||
use effectcraft_raster::Image;
|
||||
@@ -141,17 +143,22 @@ pub fn layered_psd(s: &Session, cid: ItemId, t: Tick, scale: f64) -> Result<(Vec
|
||||
|
||||
/// A unique EXR layer name (no dots: they separate the layer from the channel). Printable ASCII only:
|
||||
/// the `exr` crate panics on characters it cannot store in a channel name (#1702).
|
||||
fn exr_name(name: &str, used: &mut Vec<String>) -> String {
|
||||
/// `used` holds the names given so far, `next` the suffix each base's search resumes at: many
|
||||
/// layers of one name take a name each without retrying every suffix before theirs.
|
||||
fn exr_name(name: &str, used: &mut HashSet<String>, next: &mut HashMap<String, usize>) -> String {
|
||||
let base: String = name.chars().map(|c| if c == '.' || !(c == ' ' || c.is_ascii_graphic()) { '_' } else { c }).collect();
|
||||
let base = if base.trim().is_empty() { "Layer".to_string() } else { base };
|
||||
let mut n = base.clone();
|
||||
let mut k = 2;
|
||||
while used.contains(&n) {
|
||||
n = format!("{base} {k}");
|
||||
k += 1;
|
||||
if used.insert(base.clone()) {
|
||||
return base;
|
||||
}
|
||||
let k = next.entry(base.clone()).or_insert(2);
|
||||
loop {
|
||||
let n = format!("{base} {k}");
|
||||
*k += 1;
|
||||
if used.insert(n.clone()) {
|
||||
return n;
|
||||
}
|
||||
}
|
||||
used.push(n.clone());
|
||||
n
|
||||
}
|
||||
|
||||
/// The multi-layer EXR bytes of a frame and the channel names.
|
||||
@@ -186,9 +193,9 @@ pub fn layered_exr(s: &Session, cid: ItemId, t: Tick, scale: f64) -> Result<(Vec
|
||||
}
|
||||
};
|
||||
push("", &full);
|
||||
let mut used = vec![];
|
||||
let (mut used, mut next) = (HashSet::new(), HashMap::new());
|
||||
for l in &layers {
|
||||
let n = exr_name(&l.name, &mut used);
|
||||
let n = exr_name(&l.name, &mut used, &mut next);
|
||||
push(&n, &l.image);
|
||||
}
|
||||
let layer = Layer::new((w, h), LayerAttributes::default(), Encoding::SMALL_LOSSLESS, AnyChannels::sort(channels.into()));
|
||||
@@ -248,3 +255,23 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
),
|
||||
]
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::collections::{HashMap, HashSet};
|
||||
|
||||
use super::exr_name;
|
||||
|
||||
#[test]
|
||||
fn many_layers_of_one_name_take_a_name_each_in_order() {
|
||||
let (mut used, mut next) = (HashSet::new(), HashMap::new());
|
||||
// `A 3` is taken by a layer of its own before the repeats reach it.
|
||||
let names: Vec<String> = ["A", "A", "A 3", "A", "A.b"].iter().map(|n| exr_name(n, &mut used, &mut next)).collect();
|
||||
assert_eq!(names, ["A", "A 2", "A 3", "A 4", "A_b"]);
|
||||
// 20 000 layers of one name: each found without retrying the suffixes before it.
|
||||
for _ in 0..20_000 {
|
||||
exr_name("Same", &mut used, &mut next);
|
||||
}
|
||||
assert_eq!(next["Same"], 20_001);
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
//! Recover moved media without replacing interpretation, sequence ranges or layer timing.
|
||||
|
||||
use std::collections::BTreeMap;
|
||||
use std::collections::{BTreeMap, HashMap};
|
||||
use std::path::Path;
|
||||
|
||||
use effectcraft_project::{Footage, FootageKind, ItemId, ItemKind};
|
||||
@@ -11,6 +11,8 @@ use crate::{Result, Session, cmd};
|
||||
|
||||
const COMMAND: &str = "file.relinkFootage";
|
||||
const ENTRY_LIMIT: usize = 100_000;
|
||||
/// Candidates listed for one missing item (more than one already makes it ambiguous).
|
||||
const MAX_CANDIDATES: usize = 32;
|
||||
|
||||
fn enabled(_: &Session) -> std::result::Result<(), String> {
|
||||
if cfg!(target_arch = "wasm32") { Err("folder relinking requires the desktop filesystem".into()) } else { Ok(()) }
|
||||
@@ -95,6 +97,8 @@ fn run(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let wait = p.get("wait").and_then(Value::as_bool).unwrap_or(false);
|
||||
s.spawn_task("relink", "Relinking missing footage", wait, move |ctl| {
|
||||
let index = files(&folder, ctl)?;
|
||||
// Each candidate file is probed once, however many missing items share its name.
|
||||
let mut probes: HashMap<String, std::result::Result<Footage, String>> = HashMap::new();
|
||||
let mut rows = vec![];
|
||||
let mut updates = vec![];
|
||||
for (n, (id, old)) in missing.iter().enumerate() {
|
||||
@@ -104,19 +108,21 @@ fn run(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let supported = matches!(old.kind, FootageKind::Still | FootageKind::Sequence | FootageKind::Video | FootageKind::Audio)
|
||||
&& old.layer.is_none() && old.page == 0;
|
||||
let name = Path::new(&old.path).file_name().and_then(std::ffi::OsStr::to_str).unwrap_or("");
|
||||
let paths = index.get(name).cloned().unwrap_or_default();
|
||||
let paths = index.get(name).map_or(&[][..], Vec::as_slice);
|
||||
let mut candidates = vec![];
|
||||
let mut accepted = vec![];
|
||||
for path in paths {
|
||||
// Past a few candidates the item is ambiguous anyway: list no more of them.
|
||||
for path in paths.iter().take(MAX_CANDIDATES) {
|
||||
if ctl.cancelled() {
|
||||
return Err(crate::render_queue::CANCELLED.into());
|
||||
}
|
||||
let mut reason = "source type requires manual replacement".to_string();
|
||||
let mut confidence = "low";
|
||||
if supported {
|
||||
match importer.probe(&path) {
|
||||
Ok(f) if compatible(old, &f) => {
|
||||
if old.kind != FootageKind::Sequence || (!old.sequence.is_empty() && sequence_at(old, &path).is_some()) {
|
||||
let probed = probes.entry(path.clone()).or_insert_with(|| importer.probe(path));
|
||||
match probed {
|
||||
Ok(f) if compatible(old, f) => {
|
||||
if old.kind != FootageKind::Sequence || (!old.sequence.is_empty() && sequence_at(old, path).is_some()) {
|
||||
confidence = "high";
|
||||
reason = "exact filename and compatible source metadata".into();
|
||||
accepted.push(path.clone());
|
||||
@@ -284,6 +290,19 @@ mod tests {
|
||||
assert_eq!(get(&s, id), expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn many_same_named_candidates_list_at_most_the_cap() {
|
||||
let dir = Fixture::new();
|
||||
let (mut s, id, old) = setup(&dir, "plate.png");
|
||||
for i in 0..MAX_CANDIDATES + 8 {
|
||||
dir.file(&format!("new/{i}/plate.png"), b"valid");
|
||||
}
|
||||
let r = search(&mut s, &dir, false);
|
||||
assert_eq!(r["items"][0]["status"], "ambiguous");
|
||||
assert_eq!(r["items"][0]["candidates"].as_array().unwrap().len(), MAX_CANDIDATES);
|
||||
assert_eq!(get(&s, id), old);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn duplicates_and_invalid_media_are_reported_without_edits() {
|
||||
let dir = Fixture::new();
|
||||
|
||||
@@ -11,6 +11,8 @@
|
||||
//! The browser has no folders: its file table lists the files picked or dropped so far, so
|
||||
//! picking every frame of a sequence imports it there too.
|
||||
|
||||
use std::collections::HashMap;
|
||||
|
||||
use effectcraft_project::sequence::{frame_number, missing_frame_ranges, sequence_name, split_frame_number};
|
||||
use effectcraft_project::{Footage, FootageKind};
|
||||
use effectcraft_time::FrameRate;
|
||||
@@ -126,10 +128,22 @@ fn sort_run(files: &mut Vec<String>, alphabetical: bool) {
|
||||
files.dedup();
|
||||
}
|
||||
|
||||
/// The files of the run `path` belongs to, from its folder.
|
||||
fn run_files(services: &dyn Services, path: &str, alphabetical: bool) -> Vec<String> {
|
||||
type RunKey = (String, String, String);
|
||||
|
||||
/// The files of the run `path` belongs to, from its folder. `folders` keeps each folder's files
|
||||
/// by run, listed once for all the files picked from it.
|
||||
fn run_files(services: &dyn Services, path: &str, alphabetical: bool, folders: &mut HashMap<String, HashMap<RunKey, Vec<String>>>) -> Vec<String> {
|
||||
let Some(key) = run_key(path, alphabetical) else { return vec![] };
|
||||
let mut files: Vec<String> = services.list_dir(&key.0).into_iter().filter(|p| run_key(p, alphabetical).as_ref() == Some(&key)).collect();
|
||||
let runs = folders.entry(key.0.clone()).or_insert_with(|| {
|
||||
let mut runs: HashMap<RunKey, Vec<String>> = HashMap::new();
|
||||
for p in services.list_dir(&key.0) {
|
||||
if let Some(k) = run_key(&p, alphabetical) {
|
||||
runs.entry(k).or_default().push(p);
|
||||
}
|
||||
}
|
||||
runs
|
||||
});
|
||||
let mut files = runs.get(&key).cloned().unwrap_or_default();
|
||||
sort_run(&mut files, alphabetical);
|
||||
files
|
||||
}
|
||||
@@ -138,27 +152,41 @@ fn run_files(services: &dyn Services, path: &str, alphabetical: bool) -> Vec<Str
|
||||
/// and (with the Sequence option, or [`SequenceOptions::folders`] for a folder's files) numbered
|
||||
/// stills of one run one [`Source::Sequence`].
|
||||
pub fn group(services: &dyn Services, paths: &[String], opts: SequenceOptions) -> Vec<Source> {
|
||||
// Each folder's runs, listed once for everything that needs them (a picked folder's listing
|
||||
// is kept for the run lookup of its lone files).
|
||||
let mut folders: HashMap<String, HashMap<RunKey, Vec<String>>> = HashMap::new();
|
||||
// Each file, and whether it may join a sequence.
|
||||
let mut files: Vec<(String, bool)> = vec![];
|
||||
for p in paths {
|
||||
if services.is_dir(p) {
|
||||
let mut inside: Vec<String> =
|
||||
services.list_dir(p).into_iter().filter(|f| matches!(crate::media_browser::kind_of(f), Some(k) if k != "project")).collect();
|
||||
let listing = services.list_dir(p);
|
||||
let mut runs: HashMap<RunKey, Vec<String>> = HashMap::new();
|
||||
for f in &listing {
|
||||
if let Some(k) = run_key(f, opts.alphabetical) {
|
||||
runs.entry(k).or_default().push(f.clone());
|
||||
}
|
||||
}
|
||||
if let Some(dir) = runs.keys().next().map(|k| k.0.clone()) {
|
||||
folders.entry(dir).or_insert(runs);
|
||||
}
|
||||
let mut inside: Vec<String> = listing.into_iter().filter(|f| matches!(crate::media_browser::kind_of(f), Some(k) if k != "project")).collect();
|
||||
inside.sort_by_key(|f| file_name(f).to_lowercase());
|
||||
files.extend(inside.into_iter().map(|f| (f, opts.folders)));
|
||||
} else {
|
||||
files.push((p.clone(), opts.sequence));
|
||||
}
|
||||
}
|
||||
// Runs in the order their first file was picked.
|
||||
let mut runs: Vec<((String, String, String), Vec<String>)> = vec![];
|
||||
// Runs in the order their first file was picked (found by key, not by a scan of the runs).
|
||||
let mut runs: Vec<(RunKey, Vec<String>)> = vec![];
|
||||
let mut run_of: HashMap<RunKey, usize> = HashMap::new();
|
||||
let mut out: Vec<Option<Source>> = vec![];
|
||||
let mut slot: Vec<usize> = vec![];
|
||||
for (f, joins) in files {
|
||||
match run_key(&f, opts.alphabetical).filter(|_| joins) {
|
||||
Some(key) => match runs.iter_mut().find(|(k, _)| *k == key) {
|
||||
Some((_, run)) => run.push(f),
|
||||
Some(key) => match run_of.get(&key) {
|
||||
Some(&i) => runs[i].1.push(f),
|
||||
None => {
|
||||
run_of.insert(key.clone(), runs.len());
|
||||
runs.push((key, vec![f]));
|
||||
slot.push(out.len());
|
||||
out.push(None);
|
||||
@@ -173,7 +201,7 @@ pub fn group(services: &dyn Services, paths: &[String], opts: SequenceOptions) -
|
||||
if let [one] = picked.as_slice()
|
||||
&& opts.sequence
|
||||
{
|
||||
let all = run_files(services, one, opts.alphabetical);
|
||||
let all = run_files(services, one, opts.alphabetical, &mut folders);
|
||||
if all.len() >= 2 {
|
||||
picked = all;
|
||||
}
|
||||
@@ -315,6 +343,59 @@ mod tests {
|
||||
assert_eq!(sequences(&t, &strs(&["/s/a_0001.png", "/s/c.png"]), false).len(), 1);
|
||||
}
|
||||
|
||||
/// Thousands of files picked one by one from one folder: the folder is listed once, not
|
||||
/// once per file, and each finds its run by key.
|
||||
#[test]
|
||||
fn picked_files_of_one_folder_list_it_once() {
|
||||
struct Counting(Table, std::sync::atomic::AtomicUsize);
|
||||
impl Services for Counting {
|
||||
fn read_file(&self, path: &str) -> std::io::Result<Vec<u8>> {
|
||||
self.0.read_file(path)
|
||||
}
|
||||
fn write_file(&self, p: &str, b: &[u8]) -> std::io::Result<()> {
|
||||
self.0.write_file(p, b)
|
||||
}
|
||||
fn list_dir(&self, dir: &str) -> Vec<String> {
|
||||
self.1.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
|
||||
self.0.list_dir(dir)
|
||||
}
|
||||
fn is_dir(&self, path: &str) -> bool {
|
||||
self.0.is_dir(path)
|
||||
}
|
||||
}
|
||||
let names: Vec<&'static str> = (0..2000).map(|i| &*Box::leak(format!("/s/x{i}_1.png").into_boxed_str())).collect();
|
||||
let t = Counting(Table(names.clone()), Default::default());
|
||||
let picked: Vec<String> = names.iter().map(|s| s.to_string()).collect();
|
||||
let g = group(&t, &picked, SequenceOptions::default());
|
||||
assert_eq!(g.len(), 2000);
|
||||
assert_eq!(t.1.load(std::sync::atomic::Ordering::Relaxed), 1);
|
||||
}
|
||||
|
||||
/// A picked folder's listing serves the run lookup of its lone files: one listing in all.
|
||||
#[test]
|
||||
fn a_picked_folder_is_listed_once() {
|
||||
struct Counting(Table, std::sync::atomic::AtomicUsize);
|
||||
impl Services for Counting {
|
||||
fn read_file(&self, path: &str) -> std::io::Result<Vec<u8>> {
|
||||
self.0.read_file(path)
|
||||
}
|
||||
fn write_file(&self, p: &str, b: &[u8]) -> std::io::Result<()> {
|
||||
self.0.write_file(p, b)
|
||||
}
|
||||
fn list_dir(&self, dir: &str) -> Vec<String> {
|
||||
self.1.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
|
||||
self.0.list_dir(dir)
|
||||
}
|
||||
fn is_dir(&self, path: &str) -> bool {
|
||||
self.0.is_dir(path)
|
||||
}
|
||||
}
|
||||
let t = Counting(Table(vec!["/s/a_0001.png", "/s/b_0001.png", "/s/b_0002.png"]), Default::default());
|
||||
let g = group(&t, &strs(&["/s"]), SequenceOptions { sequence: true, folders: true, alphabetical: false });
|
||||
assert_eq!(g.len(), 2, "{g:?}");
|
||||
assert_eq!(t.1.load(std::sync::atomic::Ordering::Relaxed), 1);
|
||||
}
|
||||
|
||||
/// #431: `file.import` brings picked or dropped numbered stills in as stills; only a folder or
|
||||
/// an explicit `sequence: true` makes a sequence, as After Effects' Sequence checkbox does.
|
||||
#[test]
|
||||
|
||||
@@ -751,6 +751,9 @@ fn footage_dir(s: &Session, name: &str, footage_dir: Option<&str>, project_path:
|
||||
fn extract_footage(s: &Session, proj: &mut Project, entries: &[(String, Vec<u8>)], dir: &str) -> Result<Vec<String>> {
|
||||
let mut written = std::collections::HashMap::new();
|
||||
let mut used = std::collections::HashSet::new();
|
||||
// Where each (item, file) pair's numbering resumes, so repeated names don't retry every
|
||||
// number before theirs.
|
||||
let mut next = std::collections::HashMap::new();
|
||||
for (k, v) in entries.iter().filter(|(k, _)| k.starts_with("media/")) {
|
||||
// `media/<item>/<file>` → one folder; a name another item took gets the item's id.
|
||||
let file = safe_file_name(&base_name(k));
|
||||
@@ -763,10 +766,10 @@ fn extract_footage(s: &Session, proj: &mut Project, entries: &[(String, Vec<u8>)
|
||||
// case-insensitive volume (Windows and macOS as they come), and Win32 drops a trailing
|
||||
// dot or space, so `a.png.` is `a.png` there. The name written stays as it is.
|
||||
let mut name = file.clone();
|
||||
let mut n = 0usize;
|
||||
let n = next.entry((item, same_file_key(&file))).or_insert(0usize);
|
||||
while !used.insert(same_file_key(&name)) {
|
||||
n += 1;
|
||||
name = if n == 1 { format!("{item}-{file}") } else { format!("{item}-{n}-{file}") };
|
||||
*n += 1;
|
||||
name = if *n == 1 { format!("{item}-{file}") } else { format!("{item}-{n}-{file}") };
|
||||
}
|
||||
let out = format!("{dir}/{name}");
|
||||
s.services.store_file(&out, v).map_err(|e| EngineError::Other(format!("cannot write {out}: {e}")))?;
|
||||
|
||||
@@ -477,35 +477,51 @@ pub fn split_at_images(doc: &Doc) -> Vec<Doc> {
|
||||
})
|
||||
.sum()
|
||||
}
|
||||
/// Keep the shapes between images `seg − 1` and `seg`; `seen` counts images passed.
|
||||
fn prune(g: &effectcraft_svg::Group, seg: usize, seen: &mut usize) -> effectcraft_svg::Group {
|
||||
let mut out = g.clone();
|
||||
out.children = vec![];
|
||||
/// `g` without its children, holding `children` instead (nothing below `g` is copied).
|
||||
fn shell(g: &effectcraft_svg::Group, children: Vec<Node>) -> effectcraft_svg::Group {
|
||||
effectcraft_svg::Group {
|
||||
name: g.name.clone(),
|
||||
transform: g.transform,
|
||||
opacity: g.opacity,
|
||||
children,
|
||||
clip: g.clip.clone(),
|
||||
blend: g.blend,
|
||||
mask: g.mask.clone(),
|
||||
isolated: g.isolated,
|
||||
knockout: g.knockout,
|
||||
}
|
||||
}
|
||||
/// One traversal for every segment: the pieces of `g` as (segment, group), by ascending
|
||||
/// segment; `seen` counts images passed. Each shape is copied once, into its own segment.
|
||||
fn split(g: &effectcraft_svg::Group, seen: &mut usize) -> Vec<(usize, effectcraft_svg::Group)> {
|
||||
let mut by: Vec<(usize, Vec<Node>)> = vec![];
|
||||
let mut put = |seg: usize, node: Node| match by.last_mut() {
|
||||
Some(last) if last.0 == seg => last.1.push(node),
|
||||
_ => by.push((seg, vec![node])),
|
||||
};
|
||||
for c in &g.children {
|
||||
match c {
|
||||
Node::Image(_) => *seen += 1,
|
||||
Node::Shape(_) => {
|
||||
if *seen == seg {
|
||||
out.children.push(c.clone());
|
||||
}
|
||||
}
|
||||
Node::Shape(_) => put(*seen, c.clone()),
|
||||
Node::Group(s) => {
|
||||
let p = prune(s, seg, seen);
|
||||
if !p.children.is_empty() {
|
||||
out.children.push(Node::Group(p));
|
||||
for (seg, part) in split(s, seen) {
|
||||
put(seg, Node::Group(part));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
out
|
||||
by.into_iter().map(|(seg, children)| (seg, shell(g, children))).collect()
|
||||
}
|
||||
let n = count(&doc.root);
|
||||
(0..=n)
|
||||
.map(|seg| {
|
||||
let mut d = doc.clone();
|
||||
d.root = prune(&doc.root, seg, &mut 0);
|
||||
d
|
||||
})
|
||||
let mut roots: Vec<Option<effectcraft_svg::Group>> = vec![None; n + 1];
|
||||
for (seg, part) in split(&doc.root, &mut 0) {
|
||||
if let Some(slot) = roots.get_mut(seg) {
|
||||
*slot = Some(part);
|
||||
}
|
||||
}
|
||||
roots
|
||||
.into_iter()
|
||||
.map(|root| Doc { width: doc.width, height: doc.height, root: root.unwrap_or_else(|| shell(&doc.root, vec![])), skipped: doc.skipped.clone() })
|
||||
.collect()
|
||||
}
|
||||
|
||||
@@ -655,3 +671,73 @@ pub fn masks_layer(proj: &mut Project, comp: &Comp, name: &str, outlines: Vec<Sh
|
||||
}
|
||||
l
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use effectcraft_svg::{Geom, Group, Image, Shape};
|
||||
|
||||
fn shape(name: &str) -> Node {
|
||||
let geom = Geom::Rect { x: 0.0, y: 0.0, w: 1.0, h: 1.0, rx: 0.0, ry: 0.0 };
|
||||
Node::Shape(Shape {
|
||||
name: name.into(),
|
||||
geom,
|
||||
transform: Affine::IDENTITY,
|
||||
fill: None,
|
||||
fill_opacity: 1.0,
|
||||
fill_rule: effectcraft_svg::FillRule::NonZero,
|
||||
stroke: None,
|
||||
opacity: 1.0,
|
||||
})
|
||||
}
|
||||
fn image() -> Node {
|
||||
Node::Image(Image { name: "i".into(), transform: Affine::IDENTITY, width: 1, height: 1, rgba: std::sync::Arc::new(vec![0; 4]), opacity: 1.0 })
|
||||
}
|
||||
fn group(name: &str, children: Vec<Node>) -> Node {
|
||||
let mut g = Group::new(name);
|
||||
g.children = children;
|
||||
Node::Group(g)
|
||||
}
|
||||
fn names(g: &Group, out: &mut Vec<String>) {
|
||||
for c in &g.children {
|
||||
match c {
|
||||
Node::Shape(s) => out.push(s.name.clone()),
|
||||
Node::Group(s) => {
|
||||
out.push(format!("<{}", s.name));
|
||||
names(s, out);
|
||||
out.push(format!("{}>", s.name));
|
||||
}
|
||||
Node::Image(_) => out.push("IMG".into()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Segment `k` keeps the shapes between image `k - 1` and `k`, with the groups that hold them.
|
||||
#[test]
|
||||
fn split_keeps_each_segments_shapes_and_their_groups() {
|
||||
let mut root = Group::new("root");
|
||||
root.children = vec![
|
||||
shape("a"),
|
||||
image(),
|
||||
group("g1", vec![shape("b"), image(), shape("c"), group("g2", vec![shape("d"), image(), shape("e")])]),
|
||||
image(),
|
||||
group("empty", vec![image()]),
|
||||
shape("f"),
|
||||
];
|
||||
let doc = Doc { width: 10.0, height: 10.0, root, skipped: vec![] };
|
||||
let segs = split_at_images(&doc);
|
||||
assert_eq!(segs.len(), 6);
|
||||
let got: Vec<Vec<String>> = segs
|
||||
.iter()
|
||||
.map(|d| {
|
||||
let mut v = vec![];
|
||||
names(&d.root, &mut v);
|
||||
v
|
||||
})
|
||||
.collect();
|
||||
let want: Vec<Vec<&str>> =
|
||||
vec![vec!["a"], vec!["<g1", "b", "g1>"], vec!["<g1", "c", "<g2", "d", "g2>", "g1>"], vec!["<g1", "<g2", "e", "g2>", "g1>"], vec![], vec!["f"]];
|
||||
assert_eq!(got, want);
|
||||
assert!(segs.iter().all(|d| d.root.name == "root" && d.width == 10.0));
|
||||
}
|
||||
}
|
||||
@@ -195,6 +195,15 @@ impl ImageRef {
|
||||
}
|
||||
}
|
||||
|
||||
/// Most edges a ScriptUI `fillPath` is filled with (a path past it fills its first edges).
|
||||
pub const MAX_FILL_EDGES: usize = 65_536;
|
||||
/// Most horizontal bands a ScriptUI fill is cut into (vertices and edge crossings); the topmost are filled.
|
||||
pub const MAX_FILL_BANDS: usize = 262_144;
|
||||
/// Most triangles a ScriptUI fill produces.
|
||||
pub const MAX_FILL_TRIANGLES: usize = 1 << 22;
|
||||
/// Most band-by-edge steps a fill takes while cutting bands.
|
||||
pub const MAX_FILL_WORK: usize = 1 << 31;
|
||||
|
||||
impl PathSeg {
|
||||
/// The subpaths of `path` as polylines (ellipses flattened to `n` segments), each with
|
||||
/// whether it is closed.
|
||||
@@ -248,8 +257,16 @@ impl PathSeg {
|
||||
/// self-intersecting and holed paths fill correctly (frontends' polygon fills are often
|
||||
/// convex-only). Exact trapezoid decomposition: the plane is cut into horizontal bands at
|
||||
/// every vertex and edge crossing; inside a band no edges cross, so each inside span is a
|
||||
/// trapezoid (two triangles).
|
||||
/// trapezoid (two triangles). Edges are compared pairwise and every band looks at every edge,
|
||||
/// on the UI thread, for geometry a script chooses, so a path past [`MAX_FILL_EDGES`] edges,
|
||||
/// [`MAX_FILL_BANDS`] bands, [`MAX_FILL_TRIANGLES`] triangles or [`MAX_FILL_WORK`] steps (edge
|
||||
/// pairs compared, then edges looked at per band) is filled only as far as the limit allows
|
||||
/// (topmost part first) and a warning is logged.
|
||||
pub fn fill_triangles(path: &[PathSeg], n: usize, even_odd: bool) -> Vec<[[f64; 2]; 3]> {
|
||||
Self::fill_triangles_within(path, n, even_odd, [MAX_FILL_EDGES, MAX_FILL_BANDS, MAX_FILL_TRIANGLES, MAX_FILL_WORK])
|
||||
}
|
||||
|
||||
fn fill_triangles_within(path: &[PathSeg], n: usize, even_odd: bool, [max_edges, max_bands, max_triangles, max_work]: [usize; 4]) -> Vec<[[f64; 2]; 3]> {
|
||||
// Non-horizontal edges, top to bottom, with their winding direction.
|
||||
let mut edges: Vec<([f64; 2], [f64; 2], i32)> = vec![];
|
||||
for (pts, _) in PathSeg::polylines(path, n) {
|
||||
@@ -261,15 +278,37 @@ impl PathSeg {
|
||||
edges.push(if a[1] < b[1] { (a, b, 1) } else { (b, a, -1) });
|
||||
}
|
||||
}
|
||||
if edges.len() > max_edges {
|
||||
log::warn!("fillPath: {} edges, filling only the first {max_edges}", edges.len());
|
||||
edges.truncate(max_edges);
|
||||
}
|
||||
if edges.len() < 2 {
|
||||
return vec![];
|
||||
}
|
||||
let x_at = |e: &([f64; 2], [f64; 2], i32), y: f64| e.0[0] + (e.1[0] - e.0[0]) * (y - e.0[1]) / (e.1[1] - e.0[1]);
|
||||
let mut ys: Vec<f64> = edges.iter().flat_map(|e| [e.0[1], e.1[1]]).collect();
|
||||
// Where two edges cross, a band boundary.
|
||||
for i in 0..edges.len() {
|
||||
for j in i + 1..edges.len() {
|
||||
// Where two edges cross, a band boundary. Edges by top, so each is compared only with the
|
||||
// ones that start above its bottom (the pairs that share some height); the pairs and the
|
||||
// boundaries found are counted against the budgets, and past one the rest are not looked
|
||||
// for. A band that holds a crossing that was not looked for is not filled (its edges
|
||||
// change order inside it, which the band loop below tells), so what is drawn is never
|
||||
// outside the path.
|
||||
let mut truncated = false;
|
||||
let mut order: Vec<usize> = (0..edges.len()).collect();
|
||||
order.sort_by(|&a, &b| edges[a].0[1].total_cmp(&edges[b].0[1]));
|
||||
let mut pairs = 0usize;
|
||||
'cross: for (k, &i) in order.iter().enumerate() {
|
||||
for &j in &order[k + 1..] {
|
||||
let (a, b) = (&edges[i], &edges[j]);
|
||||
if b.0[1] >= a.1[1] {
|
||||
break;
|
||||
}
|
||||
pairs += 1;
|
||||
if pairs > max_work || ys.len() > max_bands {
|
||||
log::warn!("fillPath: over its crossing budget; bands with crossings that were not cut are left unfilled");
|
||||
truncated = true;
|
||||
break 'cross;
|
||||
}
|
||||
let (lo, hi) = (a.0[1].max(b.0[1]), a.1[1].min(b.1[1]));
|
||||
if hi <= lo {
|
||||
continue;
|
||||
@@ -282,6 +321,11 @@ impl PathSeg {
|
||||
}
|
||||
ys.sort_by(f64::total_cmp);
|
||||
ys.dedup_by(|a, b| (*a - *b).abs() < 1e-9);
|
||||
if ys.len() > max_bands {
|
||||
log::warn!("fillPath: {} bands, filling only the topmost {max_bands}", ys.len());
|
||||
ys.truncate(max_bands);
|
||||
}
|
||||
let mut work = 0usize;
|
||||
let mut out = vec![];
|
||||
let mut row: Vec<(f64, f64, f64, i32)> = vec![];
|
||||
for w in ys.windows(2) {
|
||||
@@ -290,8 +334,14 @@ impl PathSeg {
|
||||
row.clear();
|
||||
row.extend(edges.iter().filter(|e| e.0[1] < ym && ym < e.1[1]).map(|e| (x_at(e, ym), x_at(e, ya), x_at(e, yb), e.2)));
|
||||
row.sort_by(|a, b| a.0.total_cmp(&b.0));
|
||||
// With crossings missing, edges that swap places inside the band make its trapezoids
|
||||
// wrong: it is left out.
|
||||
let swapped = truncated && !row.windows(2).all(|w| w[0].1 <= w[1].1 + 1e-9 && w[0].2 <= w[1].2 + 1e-9);
|
||||
let mut wind = 0;
|
||||
for k in 0..row.len().saturating_sub(1) {
|
||||
if swapped {
|
||||
break;
|
||||
}
|
||||
wind += row[k].3;
|
||||
let inside = if even_odd { wind % 2 != 0 } else { wind != 0 };
|
||||
if inside {
|
||||
@@ -300,6 +350,13 @@ impl PathSeg {
|
||||
out.push([[l.1, ya], [r.2, yb], [l.2, yb]]);
|
||||
}
|
||||
}
|
||||
// (Every band's edge scan counts, a band left out included.)
|
||||
work = work.saturating_add(edges.len());
|
||||
if out.len() > max_triangles || work > max_work {
|
||||
log::warn!("fillPath: over its triangle or work budget, filled down to y = {yb}");
|
||||
out.truncate(max_triangles);
|
||||
break;
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
@@ -951,4 +1008,50 @@ mod tests {
|
||||
let open = vec![PathSeg::M { x: 0.0, y: 0.0 }, PathSeg::L { x: 4.0, y: 0.0 }, PathSeg::L { x: 0.0, y: 4.0 }];
|
||||
assert!((area(&PathSeg::fill_triangles(&open, 48, false)) - 8.0).abs() < 1e-9);
|
||||
}
|
||||
|
||||
/// A script's path past the fill budgets is filled as far as they allow, never emptied.
|
||||
#[test]
|
||||
fn a_fill_past_its_budgets_draws_what_fits() {
|
||||
let area = |t: &[[[f64; 2]; 3]]| {
|
||||
t.iter().map(|t| ((t[1][0] - t[0][0]) * (t[2][1] - t[0][1]) - (t[2][0] - t[0][0]) * (t[1][1] - t[0][1])).abs() / 2.0).sum::<f64>()
|
||||
};
|
||||
let rect = |x: f64, y: f64, w: f64, h: f64| PathSeg::R { x, y, w, h };
|
||||
let rects: Vec<PathSeg> = (0..8).map(|i| rect(2.0 * i as f64, 0.0, 1.0, 10.0)).collect();
|
||||
let full = PathSeg::fill_triangles(&rects, 48, false);
|
||||
assert!((area(&full) - 80.0).abs() < 1e-9);
|
||||
// Edges over the limit: the first edges still fill (4 edges = 2 rectangles).
|
||||
let t = PathSeg::fill_triangles_within(&rects, 48, false, [4, usize::MAX, usize::MAX, usize::MAX]);
|
||||
assert!((area(&t) - 20.0).abs() < 1e-9, "{}", area(&t));
|
||||
// Triangles over the limit: truncated, not empty.
|
||||
let t = PathSeg::fill_triangles_within(&rects, 48, false, [usize::MAX, usize::MAX, 6, usize::MAX]);
|
||||
assert_eq!(t.len(), 6);
|
||||
// Bands over the limit: the topmost band fills.
|
||||
let stair: Vec<PathSeg> = (0..4).map(|i| rect(0.0, i as f64, 1.0, 1.0)).collect();
|
||||
let t = PathSeg::fill_triangles_within(&stair, 48, false, [usize::MAX, 2, usize::MAX, usize::MAX]);
|
||||
assert!(!t.is_empty() && area(&t) < 4.0);
|
||||
// Work over the limit stops the cut.
|
||||
let t = PathSeg::fill_triangles_within(&stair, 48, false, [usize::MAX, usize::MAX, usize::MAX, 1]);
|
||||
assert!(!t.is_empty() && area(&t) < 4.0);
|
||||
// Many crossing edges (every pair of 120 diagonals meets): the crossing scan is held to the
|
||||
// band and work budgets too.
|
||||
let fan: Vec<PathSeg> = (0..60)
|
||||
.flat_map(|i| {
|
||||
let x = i as f64;
|
||||
[PathSeg::M { x, y: 0.0 }, PathSeg::L { x: 60.0 - x, y: 100.0 }, PathSeg::L { x: x + 0.5, y: 100.0 }]
|
||||
})
|
||||
.collect();
|
||||
for limits in [[usize::MAX, 50, usize::MAX, usize::MAX], [usize::MAX, usize::MAX, usize::MAX, 500]] {
|
||||
let t = PathSeg::fill_triangles_within(&fan, 48, false, limits);
|
||||
assert!(area(&t) <= area(&PathSeg::fill_triangles(&fan, 48, false)) + 1e-6, "{limits:?}");
|
||||
}
|
||||
// Bands whose crossings were not looked for are not filled: a bow tie (area 50, crossing at
|
||||
// y = 5) under a crossing budget of one boundary gives no overlapping triangles.
|
||||
let bow = vec![PathSeg::M { x: 0.0, y: 0.0 }, PathSeg::L { x: 10.0, y: 10.0 }, PathSeg::L { x: 0.0, y: 10.0 }, PathSeg::L { x: 10.0, y: 0.0 }];
|
||||
assert!((area(&PathSeg::fill_triangles(&bow, 48, false)) - 50.0).abs() < 1e-9);
|
||||
let t = PathSeg::fill_triangles_within(&bow, 48, false, [usize::MAX, 2, usize::MAX, usize::MAX]);
|
||||
assert!(area(&t) <= 50.0 + 1e-9, "{}", area(&t));
|
||||
// Bands left out still count against the work budget (it ends the scan after the first).
|
||||
let t = PathSeg::fill_triangles_within(&bow, 48, false, [usize::MAX, 2, usize::MAX, 1]);
|
||||
assert!(area(&t) <= 50.0 + 1e-9, "{}", area(&t));
|
||||
}
|
||||
}
|
||||
@@ -103,15 +103,22 @@ impl Session {
|
||||
effectcraft_project::ItemKind::Footage(f) if f.has_video => {
|
||||
let f = f.clone();
|
||||
Some(Box::new(move || {
|
||||
let img = effectcraft_render::cineon_frame(footage.frame(item, &f, Tick(0))?, &f);
|
||||
// Point-sample down to at most `max_side` on the long side.
|
||||
let step = (img.width.max(img.height) as f32 / max_side.max(1) as f32).ceil().max(1.0) as u32;
|
||||
let (w, h) = (img.width.div_ceil(step), img.height.div_ceil(step));
|
||||
// Vector footage is drawn at the thumbnail's size: its declared size can be a
|
||||
// 16384² canvas, gigabytes of pixels to point-sample down to a few dozen.
|
||||
let scale = (max_side.max(1) as f64 / f.width.max(f.height).max(1) as f64).min(1.0);
|
||||
let src = footage.vector_frame(item, &f, scale).or_else(|| footage.frame(item, &f, Tick(0)))?;
|
||||
let img = effectcraft_render::cineon_frame(src, &f);
|
||||
// Point-sample down to at most `max_side` on the long side (a drawn size one
|
||||
// pixel over it, from rounding, is shrunk by that pixel, not halved).
|
||||
let (long, max) = (u64::from(img.width.max(img.height)), u64::from(max_side.max(1)));
|
||||
let fit = |side: u32| if long <= max { side } else { ((u64::from(side) * max + long / 2) / long).max(1) as u32 };
|
||||
let (w, h) = (fit(img.width), fit(img.height));
|
||||
let full = img.to_rgba8();
|
||||
let mut px = Vec::with_capacity((w * h * 4) as usize);
|
||||
for y in 0..h {
|
||||
for x in 0..w {
|
||||
let i = (((y * step) * img.width + x * step) * 4) as usize;
|
||||
let (sx, sy) = (u64::from(x) * u64::from(img.width) / u64::from(w), u64::from(y) * u64::from(img.height) / u64::from(h));
|
||||
let i = ((sy * u64::from(img.width) + sx) * 4) as usize;
|
||||
px.extend_from_slice(&full[i..i + 4]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -550,6 +550,10 @@ impl Keymaps {
|
||||
}
|
||||
}
|
||||
|
||||
/// Most conflicts [`ShortcutTable::all_conflicts`] lists: a preset binding every command to one
|
||||
/// key would otherwise list every pair of them.
|
||||
pub const MAX_CONFLICTS: usize = 10_000;
|
||||
|
||||
/// The active preset resolved against every bindable command.
|
||||
#[derive(Clone, Debug, Default)]
|
||||
pub struct ShortcutTable {
|
||||
@@ -584,8 +588,15 @@ impl ShortcutTable {
|
||||
}
|
||||
|
||||
/// Every active binding: (keys, bindable).
|
||||
/// Each bindable's keys once: an imported preset can repeat a binding any number of times.
|
||||
pub fn bindings(&self) -> Vec<(&str, &Bindable)> {
|
||||
self.bindables.iter().flat_map(|b| self.keys.get(&b.key).into_iter().flatten().map(move |k| (k.as_str(), b))).collect()
|
||||
self.bindables
|
||||
.iter()
|
||||
.flat_map(|b| {
|
||||
let mut seen = std::collections::HashSet::new();
|
||||
self.keys.get(&b.key).into_iter().flatten().filter(move |k| seen.insert(k.as_str())).map(move |k| (k.as_str(), b))
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// Other bindables using `keys` in an overlapping scope.
|
||||
@@ -607,6 +618,7 @@ impl ShortcutTable {
|
||||
/// Every pair of commands sharing keys in the same scope, or in two contexts (which can be
|
||||
/// active at once): only one of the two can run, or neither ([`pick`]). (Other pairs in
|
||||
/// different scopes are intended, see [`Conflict::overrides`].)
|
||||
/// At most [`MAX_CONFLICTS`] are listed.
|
||||
pub fn all_conflicts(&self) -> Vec<(String, Conflict)> {
|
||||
let mut by_keys: BTreeMap<&str, Vec<&Bindable>> = BTreeMap::new();
|
||||
for (k, b) in self.bindings() {
|
||||
@@ -618,6 +630,9 @@ impl ShortcutTable {
|
||||
for (k, bs) in by_keys {
|
||||
for (i, a) in bs.iter().enumerate() {
|
||||
for b in &bs[i + 1..] {
|
||||
if out.len() >= MAX_CONFLICTS {
|
||||
return out;
|
||||
}
|
||||
if a.scope == b.scope || (is_context(&a.scope) && is_context(&b.scope)) {
|
||||
out.push((
|
||||
a.key.clone(),
|
||||
|
||||
@@ -125,7 +125,13 @@ fn utc_parts(secs: u64, nanos: u32) -> (i64, chrono::NaiveDateTime) {
|
||||
|
||||
/// One log line (without the newline): `<timestamp> <LEVEL> [<thread>] <target>: <message>`.
|
||||
fn format_line(ts: &str, level: log::Level, thread: &str, target: &str, message: &str) -> String {
|
||||
let message = message.strip_suffix('\n').unwrap_or(message);
|
||||
// CRLF becomes LF first, so a message ending in "\r\n" loses its final break like one ending
|
||||
// in "\n" (and its carriage return is not taken for a lone one).
|
||||
let message = message.replace("\r\n", "\n");
|
||||
let message = message.strip_suffix('\n').unwrap_or(&message);
|
||||
// A message's own line breaks (a path or name from a project or a script) continue it on
|
||||
// indented lines, so they can't pass for records of their own.
|
||||
let message = message.replace('\r', "\\r").replace('\n', "\n\t");
|
||||
format!("{ts} {level:<5} [{thread}] {target}: {message}")
|
||||
}
|
||||
|
||||
@@ -523,4 +529,22 @@ mod tests {
|
||||
"2026-10-08T07:59:17.728Z WARN [main] effectcraft_engine::autosave: disk full"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn line_breaks_in_a_message_continue_it_on_indented_lines() {
|
||||
let forged = "cache at /tmp/x\n2026-10-08T07:59:18.000Z ERROR [main] effectcraft_engine: forged\rend";
|
||||
let line = format_line("2026-10-08T07:59:17.728Z", log::Level::Info, "main", "t", forged);
|
||||
assert_eq!(line.lines().count(), 2);
|
||||
assert!(line.lines().nth(1).unwrap().starts_with('\t'), "{line}");
|
||||
assert!(!line.contains('\r'), "{line}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_trailing_crlf_is_one_line_break_not_a_lone_carriage_return() {
|
||||
let line = |m| format_line("t", log::Level::Info, "main", "t", m);
|
||||
assert_eq!(line("cache\r\n"), line("cache"));
|
||||
assert_eq!(line("a\r\nb\r\n"), line("a\nb"));
|
||||
// A lone one, even at the end, is still escaped.
|
||||
assert!(line("cache\r").ends_with("cache\\r"));
|
||||
}
|
||||
}
|
||||
@@ -331,3 +331,17 @@ fn background_pre_render_movies_are_checked_again_before_publication() {
|
||||
assert!(listing(&f.dir).iter().all(|name| !name.starts_with(".effectcraft-prerender-")));
|
||||
}
|
||||
}
|
||||
|
||||
/// Footage files sharing a name are numbered from that name (`3_a.mov`, not `3_2_a.mov`).
|
||||
#[test]
|
||||
fn collect_files_numbers_repeated_names_from_the_name() {
|
||||
let mut f = collecting("collect-repeated", "a.mov");
|
||||
for d in ["in2", "in3"] {
|
||||
std::fs::create_dir_all(f.path(d)).unwrap();
|
||||
f.footage(&format!("{d}/a.mov"));
|
||||
}
|
||||
collect(&mut f).unwrap();
|
||||
let mut names = listing(&f.path("out/(Footage)"));
|
||||
names.sort();
|
||||
assert_eq!(names, ["2_a.mov", "3_a.mov", "a.mov"]);
|
||||
}
|
||||
@@ -788,6 +788,26 @@ fn context_shortcuts_on_the_same_keys_conflict() {
|
||||
assert!(t.conflicts(b, "F9").iter().any(|c| c.key == "test.paint" && !c.overrides));
|
||||
}
|
||||
|
||||
/// A preset repeating one binding many times lists it once, and conflicts stay bounded.
|
||||
#[test]
|
||||
fn repeated_bindings_are_one_binding() {
|
||||
let mut s = Session::default();
|
||||
s.set_ui_commands(vec![
|
||||
crate::shortcuts::UiCommand { id: "test.a".into(), label: "A".into(), shortcut: Some("F9".into()), scope: Some("Timeline".into()) },
|
||||
crate::shortcuts::UiCommand { id: "test.b".into(), label: "B".into(), shortcut: Some("F9".into()), scope: Some("Timeline".into()) },
|
||||
]);
|
||||
let mut t = s.shortcuts().clone();
|
||||
t.keys.insert("test.a".into(), vec!["F9".into(); 10_000]);
|
||||
assert_eq!(t.bindings().iter().filter(|(_, b)| b.key == "test.a").count(), 1);
|
||||
assert_eq!(t.all_conflicts().iter().filter(|(a, c)| a == "test.a" && c.key == "test.b").count(), 1);
|
||||
// Many commands on one key: the listing stops at its cap.
|
||||
for i in 0..200 {
|
||||
t.bindables.push(crate::shortcuts::Bindable { key: format!("test.many{i}"), ..t.find("test.b").unwrap().clone() });
|
||||
t.keys.insert(format!("test.many{i}"), vec!["F9".into()]);
|
||||
}
|
||||
assert_eq!(t.all_conflicts().len(), crate::shortcuts::MAX_CONFLICTS);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------- auto-save / recovery
|
||||
|
||||
/// The names in `d`, sorted.
|
||||
|
||||
@@ -383,3 +383,58 @@ fn psd_with_damaged_layer_section_imports_the_flattened_image() {
|
||||
let ItemKind::Footage(f) = &s.project.item(item).unwrap().kind else { panic!() };
|
||||
assert!(f.layer.is_none(), "the merged image, not a Photoshop layer");
|
||||
}
|
||||
|
||||
/// A vector footage thumbnail is drawn at the thumbnail's size, never at its declared size.
|
||||
#[test]
|
||||
fn vector_footage_thumbnails_are_drawn_at_thumbnail_size() {
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
use effectcraft_project::{Footage, ItemId};
|
||||
use effectcraft_raster::Image;
|
||||
use effectcraft_time::Tick;
|
||||
|
||||
struct Vector(Mutex<Vec<f64>>);
|
||||
impl crate::render::FootageSource for Vector {
|
||||
fn frame(&self, _: ItemId, _: &Footage, _: Tick) -> Option<Arc<Image>> {
|
||||
panic!("the full-size frame was decoded");
|
||||
}
|
||||
fn vector_frame(&self, _: ItemId, f: &Footage, scale: f64) -> Option<Arc<Image>> {
|
||||
self.0.lock().unwrap().push(scale);
|
||||
Some(Arc::new(Image::new((f.width as f64 * scale).ceil() as u32, (f.height as f64 * scale).ceil() as u32)))
|
||||
}
|
||||
}
|
||||
let source = Arc::new(Vector(Mutex::new(vec![])));
|
||||
let mut s = Session { footage: source.clone(), ..Default::default() };
|
||||
let f = Footage { path: "/m/huge.svg".into(), codec: "SVG".into(), width: 16_000, height: 8_000, has_video: true, ..Default::default() };
|
||||
let item = std::sync::Arc::make_mut(&mut s.project).add_item("huge.svg", effectcraft_color::Label::Sandstone, None, ItemKind::Footage(f));
|
||||
let (w, h, _) = (s.thumbnail_job(item, Tick::ZERO, 64).unwrap())().unwrap();
|
||||
assert_eq!((w, h), (64, 32));
|
||||
assert_eq!(*source.0.lock().unwrap(), [64.0 / 16_000.0]);
|
||||
}
|
||||
|
||||
/// A vector footage thumbnail whose drawn size is a pixel over the requested one is shrunk by
|
||||
/// that pixel, not halved by an integer step.
|
||||
#[test]
|
||||
fn a_vector_thumbnail_one_pixel_over_is_not_halved() {
|
||||
use std::sync::Arc;
|
||||
|
||||
use effectcraft_project::{Footage, ItemId};
|
||||
use effectcraft_raster::Image;
|
||||
use effectcraft_time::Tick;
|
||||
|
||||
struct Over;
|
||||
impl crate::render::FootageSource for Over {
|
||||
fn frame(&self, _: ItemId, _: &Footage, _: Tick) -> Option<Arc<Image>> {
|
||||
None
|
||||
}
|
||||
fn vector_frame(&self, _: ItemId, _: &Footage, _: f64) -> Option<Arc<Image>> {
|
||||
Some(Arc::new(Image::new(64, 64)))
|
||||
}
|
||||
}
|
||||
let mut s = Session { footage: Arc::new(Over), ..Default::default() };
|
||||
let f = Footage { path: "/m/sq.svg".into(), codec: "SVG".into(), width: 121, height: 121, has_video: true, ..Default::default() };
|
||||
let item = std::sync::Arc::make_mut(&mut s.project).add_item("sq.svg", effectcraft_color::Label::Sandstone, None, ItemKind::Footage(f));
|
||||
let (w, h, px) = (s.thumbnail_job(item, Tick::ZERO, 63).unwrap())().unwrap();
|
||||
assert_eq!((w, h), (63, 63));
|
||||
assert_eq!(px.len(), 63 * 63 * 4);
|
||||
}
|
||||
@@ -31,6 +31,7 @@ thiserror = { workspace = true }
|
||||
log = { workspace = true }
|
||||
|
||||
[dev-dependencies]
|
||||
tempfile = { workspace = true }
|
||||
effectcraft-media = { workspace = true }
|
||||
effectcraft-effects = { workspace = true }
|
||||
effectcraft-color = { workspace = true }
|
||||
|
||||
@@ -342,7 +342,8 @@ fn ffmpeg_oracle_decodes_ogg_opus() {
|
||||
}
|
||||
}
|
||||
let (enc, packets) = encode(&pcm, channels, rate);
|
||||
let path = std::env::temp_dir().join(format!("effectcraft-opusenc-{}-{channels}.opus", std::process::id()));
|
||||
let guard = tempfile::tempdir().unwrap();
|
||||
let path = guard.path().join("out.opus");
|
||||
std::fs::write(&path, ogg_opus(&enc, &packets, len)).unwrap();
|
||||
let out = Command::new("ffmpeg")
|
||||
.args(["-v", "error", "-i"])
|
||||
|
||||
@@ -389,8 +389,8 @@ fn wav_and_aiff_channels_and_formats() {
|
||||
#[test]
|
||||
fn cancelled_export_removes_only_files_it_opened() {
|
||||
use effectcraft_export::ExportError;
|
||||
let dir = std::env::temp_dir().join(format!("ec-cancel-owned-{}", std::process::id()));
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
let guard = tempfile::tempdir().unwrap();
|
||||
let dir = guard.path().to_path_buf();
|
||||
let (p, cid) = project(FrameRate::new(25, 1), |_, _| {});
|
||||
struct Overflow;
|
||||
impl StorageQuota for Overflow {
|
||||
|
||||
@@ -130,8 +130,8 @@ fn ffmpeg_decodes_our_stream() {
|
||||
ivf.extend_from_slice(&(frame.len() as u32).to_le_bytes());
|
||||
ivf.extend_from_slice(&0u64.to_le_bytes());
|
||||
ivf.extend_from_slice(&frame);
|
||||
let dir = std::env::temp_dir().join(format!("effectcraft-vp9-{}", std::process::id()));
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
let guard = tempfile::tempdir().unwrap();
|
||||
let dir = guard.path().to_path_buf();
|
||||
let path = dir.join("a.ivf");
|
||||
std::fs::write(&path, &ivf).unwrap();
|
||||
let out = std::process::Command::new("ffmpeg")
|
||||
@@ -291,8 +291,8 @@ fn ffmpeg_matches(w: usize, h: usize, frames: &[Vec<u8>], recon: &[[Vec<u8>; 3]]
|
||||
ivf.extend_from_slice(&(t as u64).to_le_bytes());
|
||||
ivf.extend_from_slice(f);
|
||||
}
|
||||
let dir = std::env::temp_dir().join(format!("effectcraft-vp9-{name}-{}", std::process::id()));
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
let guard = tempfile::Builder::new().prefix(&format!("effectcraft-vp9-{name}-")).tempdir().unwrap();
|
||||
let dir = guard.path().to_path_buf();
|
||||
let path = dir.join("a.ivf");
|
||||
std::fs::write(&path, &ivf).unwrap();
|
||||
let out = std::process::Command::new("ffmpeg")
|
||||
|
||||
@@ -133,7 +133,7 @@ fn radial_fast(e: &mut Enc, ctx: &EffectCtx, mut b: GBuf) -> Option<GBuf> {
|
||||
}
|
||||
|
||||
fn camera_lens(e: &mut Enc, ctx: &EffectCtx, mut b: GBuf) -> Option<GBuf> {
|
||||
let r = ctx.params.f("blurRadius").max(0.0) * b.scale;
|
||||
let r = effectcraft_effects::camera_lens_radius(ctx, b.scale);
|
||||
if r < 0.5 {
|
||||
return Some(b);
|
||||
}
|
||||
|
||||
@@ -202,6 +202,8 @@ pub(crate) fn fractal(e: &mut Enc, ctx: &EffectCtx, b: GBuf, turbulent: bool) ->
|
||||
|
||||
/// noise::median_image (of the straight colour with alpha 1 when `straight`).
|
||||
pub(crate) fn median_image(e: &mut Enc, img: &GpuImage, r: usize, straight: bool) -> GpuImage {
|
||||
// As on the CPU: the window's radius is capped, so its first fill stays bounded.
|
||||
let r = r.min(effectcraft_effects::MEDIAN_MAX_RADIUS);
|
||||
let q = run(e, "fxn_quant", &with_u0([straight as u32, 0, 0, 0]), img, None, None);
|
||||
// Preserve the CPU's rounded level / 511 values before the strict Dust & Scratches
|
||||
// threshold. GPU division (even residual-corrected) may differ by one ulp.
|
||||
|
||||
@@ -276,8 +276,8 @@ pub(super) fn wat_plugin(id: &str, api: i32, render: &str) -> String {
|
||||
|
||||
#[test]
|
||||
fn plugin_loading_reports_bad_modules_and_survives_failing_renders() {
|
||||
let dir = std::env::temp_dir().join(format!("effectcraft-host-plugins-{}", std::process::id()));
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
let guard = tempfile::tempdir().unwrap();
|
||||
let dir = guard.path().to_path_buf();
|
||||
let files = [
|
||||
("a-good.wasm", wat_plugin("org.test.host.good", 1, "i32.const 0").into_bytes()),
|
||||
("b-garbage.wasm", b"\0asm\x01\0\0\0garbage".to_vec()),
|
||||
|
||||
@@ -303,16 +303,41 @@ impl Exporter<'_> {
|
||||
}
|
||||
|
||||
/// The bin mirroring a Project panel folder.
|
||||
///
|
||||
/// Walks up the folder's parents without recursion: a damaged or hostile project can hold a
|
||||
/// parent cycle (or a very long chain), which recursed until the stack ran out. A folder seen
|
||||
/// twice in one walk ends it, and the cycle's topmost folder becomes a top-level bin.
|
||||
fn bin(&mut self, folder: Option<ItemId>) -> Option<fp::BinId> {
|
||||
let f = folder?;
|
||||
if let Some(b) = self.bins.get(&f) {
|
||||
return Some(*b);
|
||||
// The uncached folders from `f` up, and the bin they hang from.
|
||||
let mut chain: Vec<ItemId> = vec![];
|
||||
let mut in_chain = std::collections::HashSet::new();
|
||||
let mut parent = None;
|
||||
let mut at = Some(f);
|
||||
while let Some(id) = at {
|
||||
if let Some(b) = self.bins.get(&id) {
|
||||
parent = Some(*b);
|
||||
break;
|
||||
}
|
||||
if !in_chain.insert(id) {
|
||||
self.warn("a folder that contains itself was exported as a top-level bin");
|
||||
break;
|
||||
}
|
||||
let Some(it) = self.p.item(id) else { break };
|
||||
chain.push(id);
|
||||
at = it.parent;
|
||||
}
|
||||
let it = self.p.item(f)?;
|
||||
let parent = self.bin(it.parent);
|
||||
let b = self.fp.add_bin(&it.name, parent);
|
||||
self.bins.insert(f, b);
|
||||
Some(b)
|
||||
if chain.is_empty() {
|
||||
// `f` is cached already (`parent` is its bin), or is not an item (`None`).
|
||||
return parent;
|
||||
}
|
||||
for id in chain.into_iter().rev() {
|
||||
let name = self.p.item(id).map(|it| it.name.clone()).unwrap_or_default();
|
||||
let b = self.fp.add_bin(&name, parent);
|
||||
self.bins.insert(id, b);
|
||||
parent = Some(b);
|
||||
}
|
||||
parent
|
||||
}
|
||||
|
||||
fn footage_item(&mut self, item: ItemId, f: &Footage) -> fp::ItemId {
|
||||
@@ -615,12 +640,34 @@ impl Exporter<'_> {
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether `c` or a comp nested in it (at any depth) has a layer of footage with audio.
|
||||
///
|
||||
/// A walk with a stack and a visited set, as in `Project::comp_contains`: recursion never ended
|
||||
/// on a precomp cycle, and re-walked a comp shared by many layers once per path (exponential
|
||||
/// for a chain of comps that each use the next one twice).
|
||||
fn comp_has_audio(p: &Project, c: &Comp) -> bool {
|
||||
c.layers.iter().any(|l| match &l.source {
|
||||
LayerSource::Footage { item } => matches!(p.item(*item).map(|i| &i.kind), Some(ItemKind::Footage(f)) if f.has_audio),
|
||||
LayerSource::Comp { item } => p.comp(*item).is_some_and(|c| comp_has_audio(p, c)),
|
||||
_ => false,
|
||||
})
|
||||
let mut seen = std::collections::HashSet::new();
|
||||
let mut stack = vec![c];
|
||||
while let Some(c) = stack.pop() {
|
||||
for l in &c.layers {
|
||||
match &l.source {
|
||||
LayerSource::Footage { item } => {
|
||||
if matches!(p.item(*item).map(|i| &i.kind), Some(ItemKind::Footage(f)) if f.has_audio) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
LayerSource::Comp { item } => {
|
||||
if seen.insert(*item)
|
||||
&& let Some(nested) = p.comp(*item)
|
||||
{
|
||||
stack.push(nested);
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
false
|
||||
}
|
||||
|
||||
fn differs(v: &Value, def: [f64; 2]) -> bool {
|
||||
@@ -703,3 +750,37 @@ fn media_info(name: &str, kind: MediaKind, duration: Tick, video: Option<VideoSp
|
||||
fn media_clip(media: fp::MediaRef, info: MediaInfo) -> fp::MediaClip {
|
||||
fp::MediaClip { media, info, interpret: Default::default(), mark_in: None, mark_out: None, markers: vec![], offline: false, proxy: None, identity: None }
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
/// A folder's bin is the same one each time it is asked for (a cached folder once answered
|
||||
/// `None`, so a folder's second item went to the project root); a non-item has no bin.
|
||||
#[test]
|
||||
fn folder_bins_are_reused_once_cached() {
|
||||
let mut p = Project::default();
|
||||
let a = p.add_item("A", effectcraft_color::Label::None, None, ItemKind::Folder);
|
||||
let b = p.add_item("B", effectcraft_color::Label::None, Some(a), ItemKind::Folder);
|
||||
let (opts, pre) = (ExportOptions::default(), HashMap::new());
|
||||
let mut ex = Exporter {
|
||||
p: &p,
|
||||
opts: &opts,
|
||||
pre: &pre,
|
||||
fp: fp::Project::new("x"),
|
||||
out: ExportOutput::default(),
|
||||
seqs: HashMap::new(),
|
||||
media: HashMap::new(),
|
||||
bins: HashMap::new(),
|
||||
stack: vec![],
|
||||
};
|
||||
let bb = ex.bin(Some(b));
|
||||
assert!(bb.is_some());
|
||||
assert_eq!(ex.bin(Some(b)), bb);
|
||||
let ba = ex.bin(Some(a));
|
||||
assert!(ba.is_some() && ba != bb);
|
||||
assert_eq!(ex.bin(Some(a)), ba);
|
||||
assert_eq!(ex.bin(Some(ItemId(u64::MAX))), None);
|
||||
assert_eq!(ex.bin(None), None);
|
||||
}
|
||||
}
|
||||
@@ -177,10 +177,12 @@ pub fn import(
|
||||
}
|
||||
let folder_name = if opts.name.is_empty() { "Imported Timeline".to_string() } else { opts.name.clone() };
|
||||
res.folder = project.add_item(&folder_name, Label::None, None, ItemKind::Folder);
|
||||
let warned = res.warnings.iter().cloned().collect();
|
||||
let mut im = Importer {
|
||||
src: &imported.project,
|
||||
project,
|
||||
res,
|
||||
warned,
|
||||
items: HashMap::new(),
|
||||
solids: HashMap::new(),
|
||||
solids_folder: None,
|
||||
@@ -273,6 +275,8 @@ struct Importer<'a> {
|
||||
src: &'a fp::Project,
|
||||
project: &'a mut Project,
|
||||
res: ImportResult,
|
||||
/// The warnings in `res`, to say each once without searching them all every time.
|
||||
warned: HashSet<String>,
|
||||
/// Source item → created item (footage, comp).
|
||||
items: HashMap<fp::ItemId, ItemId>,
|
||||
/// Generator / adjustment solids by (source item, width, height).
|
||||
@@ -290,7 +294,7 @@ struct Importer<'a> {
|
||||
impl Importer<'_> {
|
||||
fn warn(&mut self, m: impl Into<String>) {
|
||||
let m = m.into();
|
||||
if !self.res.warnings.contains(&m) {
|
||||
if self.warned.insert(m.clone()) {
|
||||
self.res.warnings.push(m);
|
||||
}
|
||||
}
|
||||
@@ -429,23 +433,8 @@ impl Importer<'_> {
|
||||
comp.markers = seq.markers.iter().map(marker_in).collect();
|
||||
// Video: the top track's clips first (layer #1); later clips of a track above earlier ones
|
||||
// so incoming clips of dissolves cover outgoing ones.
|
||||
for track in seq.video_tracks.iter().rev() {
|
||||
let mut items: Vec<&fp::TrackItem> = track.items.iter().collect();
|
||||
items.sort_by_key(|i| std::cmp::Reverse(i.start));
|
||||
for ti in items {
|
||||
if let Some(l) = self.clip_layer(&comp, seq, track, ti) {
|
||||
comp.layers.push(l);
|
||||
}
|
||||
}
|
||||
}
|
||||
for track in &seq.audio_tracks {
|
||||
let mut items: Vec<&fp::TrackItem> = track.items.iter().collect();
|
||||
items.sort_by_key(|i| std::cmp::Reverse(i.start));
|
||||
for ti in items {
|
||||
if let Some(l) = self.clip_layer(&comp, seq, track, ti) {
|
||||
comp.layers.push(l);
|
||||
}
|
||||
}
|
||||
for track in seq.video_tracks.iter().rev().chain(&seq.audio_tracks) {
|
||||
self.track_layers(&mut comp, seq, track);
|
||||
}
|
||||
if !seq.caption_tracks.is_empty() {
|
||||
self.warn("caption tracks are not imported");
|
||||
@@ -508,7 +497,45 @@ impl Importer<'_> {
|
||||
}
|
||||
}
|
||||
|
||||
fn clip_layer(&mut self, comp: &Comp, seq: &fp::Sequence, track: &fp::Track, ti: &fp::TrackItem) -> Option<Layer> {
|
||||
/// A track's clips as layers of `comp`, the latest first.
|
||||
fn track_layers(&mut self, comp: &mut Comp, seq: &fp::Sequence, track: &fp::Track) {
|
||||
let mut items: Vec<&fp::TrackItem> = track.items.iter().collect();
|
||||
items.sort_by_key(|i| std::cmp::Reverse(i.start));
|
||||
// Each clip's transitions, found once per track: scanning every transition of the track
|
||||
// for every clip made a long flat edit (many clips and many transitions) quadratic.
|
||||
let mut transitions: HashMap<fp::ClipId, Vec<&fp::Transition>> = HashMap::new();
|
||||
for tr in &track.transitions {
|
||||
for clip in [tr.from, tr.to].into_iter().flatten() {
|
||||
let list = transitions.entry(clip).or_default();
|
||||
// A transition from and to the same clip still counts once.
|
||||
if !list.last().is_some_and(|last| std::ptr::eq(*last, tr)) {
|
||||
list.push(tr);
|
||||
}
|
||||
}
|
||||
}
|
||||
let mut any = false;
|
||||
for ti in items {
|
||||
let own = transitions.get(&ti.id).map(Vec::as_slice).unwrap_or_default();
|
||||
if let Some(l) = self.clip_layer(comp, seq, track, ti, own) {
|
||||
comp.layers.push(l);
|
||||
any = true;
|
||||
}
|
||||
}
|
||||
// What the track's transitions become, said once a clip of the track was imported.
|
||||
if any && track.kind != fp::TrackKind::Audio {
|
||||
for tr in &track.transitions {
|
||||
let id = tr.effect.effect.as_str();
|
||||
if !matches!(id, "cross_dissolve" | "dip_to_black" | "dip_to_white" | "film_dissolve" | "additive_dissolve" | "non_additive_dissolve") {
|
||||
self.warn(format!("transition \"{}\" was imported as a cross dissolve", tr.effect.def().map(|d| d.name).unwrap_or(id)));
|
||||
}
|
||||
}
|
||||
}
|
||||
if any && track.transitions.iter().any(|tr| tr.effect.effect == "dip_to_white") {
|
||||
self.warn("Dip to White was imported as a dip to the background");
|
||||
}
|
||||
}
|
||||
|
||||
fn clip_layer(&mut self, comp: &Comp, seq: &fp::Sequence, track: &fp::Track, ti: &fp::TrackItem, transitions: &[&fp::Transition]) -> Option<Layer> {
|
||||
let audio_track = track.kind == fp::TrackKind::Audio;
|
||||
let (source, size, has_audio) = self.source_of(ti, comp.width, comp.height)?;
|
||||
let is_adjustment = matches!(self.src.item(ti.item).map(|i| &i.kind), Some(fp::ItemKind::AdjustmentLayer { .. }));
|
||||
@@ -561,18 +588,9 @@ impl Importer<'_> {
|
||||
}
|
||||
// Transitions extend the layer over their handles.
|
||||
let mut fades: Vec<(Tick, Tick, f64, f64)> = vec![];
|
||||
for tr in &track.transitions {
|
||||
for tr in transitions {
|
||||
let (ts, te) = (tick_in(tr.start), tick_in(tr.end()));
|
||||
let id = tr.effect.effect.as_str();
|
||||
let dip = id.starts_with("dip_to");
|
||||
if !audio_track
|
||||
&& !matches!(id, "cross_dissolve" | "dip_to_black" | "dip_to_white" | "film_dissolve" | "additive_dissolve" | "non_additive_dissolve")
|
||||
{
|
||||
self.warn(format!("transition \"{}\" was imported as a cross dissolve", tr.effect.def().map(|d| d.name).unwrap_or(id)));
|
||||
}
|
||||
if dip && id == "dip_to_white" {
|
||||
self.warn("Dip to White was imported as a dip to the background");
|
||||
}
|
||||
let dip = tr.effect.effect.starts_with("dip_to");
|
||||
let mid = ts + Tick((te - ts).0 / 2);
|
||||
if tr.to == Some(ti.id) {
|
||||
l.in_point = l.in_point.min(ts);
|
||||
|
||||
@@ -787,3 +787,98 @@ fn deeply_nested_documents_error_instead_of_overflowing_the_stack() {
|
||||
assert_eq!(super::import::xml_depth(b"<a><!-- <b><b> --><c x='>'/><![CDATA[<d>]]><e></e></a>"), 2);
|
||||
assert_eq!(super::import::json_depth(br#"{"a": "[[[{{", "b": [[1], {"c": "\\\"["}]}"#), 3);
|
||||
}
|
||||
|
||||
/// A hostile project: footage in a folder whose parents loop back to it. Export walked the
|
||||
/// folder's parents recursively and never reached the cache insert, so the stack ran out.
|
||||
#[test]
|
||||
fn export_survives_a_folder_parent_cycle() {
|
||||
let (mut p, cid) = ae_project();
|
||||
let a = p.add_item("A", effectcraft_color::Label::None, None, ItemKind::Folder);
|
||||
let b = p.add_item("B", effectcraft_color::Label::None, Some(a), ItemKind::Folder);
|
||||
p.items.get_mut(&a).unwrap().parent = Some(b);
|
||||
let footage: Vec<ItemId> = p.items.values().filter(|i| matches!(i.kind, ItemKind::Footage(_))).map(|i| i.id).collect();
|
||||
for f in footage {
|
||||
p.items.get_mut(&f).unwrap().parent = Some(a);
|
||||
}
|
||||
let out = export(&p, cid, &ExportOptions::default(), &HashMap::new()).unwrap();
|
||||
assert!(out.warnings.iter().any(|w| w.contains("contains itself")), "{:?}", out.warnings);
|
||||
}
|
||||
|
||||
/// Precomps that use each other, and a chain of comps that each use the next one twice: the
|
||||
/// audio check recursed forever on the first and 2^depth times on the second.
|
||||
#[test]
|
||||
fn export_audio_check_ends_on_precomp_cycles_and_shared_chains() {
|
||||
let mut p = Project::default();
|
||||
let a = footage_item(&mut p, "/media/a.mov");
|
||||
let comp = || Comp::new(W, H, FrameRate::new(25, 1), t(2.0));
|
||||
// A chain of 60 comps, each using the next twice; the last one is silent footage.
|
||||
let mut next = {
|
||||
let mut c = comp();
|
||||
let l = effectcraft_project::build::layer(&mut p, &c, "a", LayerSource::Footage { item: a }, (W, H), None);
|
||||
c.layers.push(l);
|
||||
comp_item(&mut p, "Leaf", c)
|
||||
};
|
||||
for i in 0..60 {
|
||||
let mut c = comp();
|
||||
for n in 0..2 {
|
||||
let l = effectcraft_project::build::layer(&mut p, &c, &format!("use {n}"), LayerSource::Comp { item: next }, (W, H), None);
|
||||
c.layers.push(l);
|
||||
}
|
||||
next = comp_item(&mut p, &format!("Chain {i}"), c);
|
||||
}
|
||||
let started = std::time::Instant::now();
|
||||
export(&p, next, &ExportOptions::default(), &HashMap::new()).unwrap();
|
||||
assert!(started.elapsed() < std::time::Duration::from_secs(30), "{:?}", started.elapsed());
|
||||
// Two comps nesting each other.
|
||||
let x = comp_item(&mut p, "X", comp());
|
||||
let mut y = comp();
|
||||
let l = effectcraft_project::build::layer(&mut p, &y, "x", LayerSource::Comp { item: x }, (W, H), None);
|
||||
y.layers.push(l);
|
||||
let y = comp_item(&mut p, "Y", y);
|
||||
let mut xc = comp();
|
||||
let l = effectcraft_project::build::layer(&mut p, &xc, "y", LayerSource::Comp { item: y }, (W, H), None);
|
||||
xc.layers.push(l);
|
||||
if let Some(ItemKind::Comp(c)) = p.items.get_mut(&x).map(|i| &mut i.kind) {
|
||||
*c = Arc::new(xc);
|
||||
}
|
||||
export(&p, x, &ExportOptions::default(), &HashMap::new()).unwrap();
|
||||
}
|
||||
|
||||
/// A flat edit with many clips and many transitions: each clip finds its own transitions (it
|
||||
/// scanned every transition of the track per clip), and they still extend and fade it.
|
||||
#[test]
|
||||
fn import_finds_each_clips_transitions_without_scanning_the_track() {
|
||||
let mut p = fp::Project::new("Flat");
|
||||
let a = media(&mut p, "a.png", "/media/a.mov", filmcraft_media::MediaKind::Movie, true, false, None);
|
||||
let settings = fp::SequenceSettings { width: W, height: H, frame_rate: rate(), ..Default::default() };
|
||||
let main = p.new_sequence("Main", settings, 1, 0, None);
|
||||
const N: usize = 500;
|
||||
let mut items = Vec::with_capacity(N);
|
||||
let mut transitions = Vec::with_capacity(N);
|
||||
for i in 0..N {
|
||||
let ti = item(&mut p, a, fp::TrackKind::Video, i as f64 * 0.4, 0.0, 0.4);
|
||||
if let Some(prev) = items.last().map(|x: &fp::TrackItem| x.id) {
|
||||
transitions.push(fp::Transition {
|
||||
id: fp::TransitionId(p.alloc_id()),
|
||||
effect: fp::find_effect("cross_dissolve").unwrap().instance(),
|
||||
start: secs(i as f64 * 0.4 - 0.08),
|
||||
duration: secs(0.16),
|
||||
from: Some(prev),
|
||||
to: Some(ti.id),
|
||||
align: fp::TransitionAlign::CenterAtCut,
|
||||
reverse: false,
|
||||
});
|
||||
}
|
||||
items.push(ti);
|
||||
}
|
||||
let seq = p.sequence_mut(main).unwrap();
|
||||
seq.video_tracks[0].items = items;
|
||||
seq.video_tracks[0].transitions = transitions;
|
||||
let (bytes, _) = fc::export(&p, main, fc::Format::Otio, &Default::default()).unwrap();
|
||||
let (q, r) = import_doc(&bytes, "Flat.otio", &mut probe);
|
||||
let m = q.comp(r.comps[0]).unwrap();
|
||||
assert_eq!(m.layers.len(), N);
|
||||
// A middle clip reaches over both of its transitions' handles.
|
||||
let mid = m.layers.iter().find(|l| near(l.in_point, t(100.0 - 0.08))).expect("clip 250 extended into its incoming transition");
|
||||
assert!(near(mid.out_point, t(100.4 + 0.08)), "{}", mid.out_point.seconds());
|
||||
}
|
||||
@@ -21,6 +21,9 @@ use serde_json::{Value as Json, json};
|
||||
|
||||
use crate::Timebase;
|
||||
|
||||
/// Most distinct warnings an import or export reports.
|
||||
const MAX_WARNINGS: usize = 200;
|
||||
|
||||
/// Export state shared by the whole document.
|
||||
pub(crate) struct Ex {
|
||||
pub tb: Timebase,
|
||||
@@ -31,7 +34,8 @@ pub(crate) struct Ex {
|
||||
impl Ex {
|
||||
pub fn warn(&mut self, w: impl Into<String>) {
|
||||
let w = w.into();
|
||||
if !self.warnings.contains(&w) {
|
||||
// Each warning is looked for among the earlier ones: past a screenful, more add nothing.
|
||||
if self.warnings.len() < MAX_WARNINGS && !self.warnings.contains(&w) {
|
||||
self.warnings.push(w);
|
||||
}
|
||||
}
|
||||
@@ -46,7 +50,8 @@ pub(crate) struct Im {
|
||||
impl Im {
|
||||
pub fn warn(&mut self, w: impl Into<String>) {
|
||||
let w = w.into();
|
||||
if !self.warnings.contains(&w) {
|
||||
// Each warning is looked for among the earlier ones: past a screenful, more add nothing.
|
||||
if self.warnings.len() < MAX_WARNINGS && !self.warnings.contains(&w) {
|
||||
self.warnings.push(w);
|
||||
}
|
||||
}
|
||||
|
||||
+30
-20
@@ -1,6 +1,6 @@
|
||||
//! Lottie document → EffectCraft compositions (the inverse of [`crate::export`]).
|
||||
|
||||
use std::collections::BTreeMap;
|
||||
use std::collections::{BTreeMap, HashMap};
|
||||
use std::sync::Arc;
|
||||
|
||||
use effectcraft_color::Label;
|
||||
@@ -17,6 +17,9 @@ use crate::anim::{self, Im, import_prop};
|
||||
use crate::export::{fx_by_id, fx_by_type, fx_params, mask_mode_from};
|
||||
use crate::{LottieError, Timebase, shapes, text};
|
||||
|
||||
/// Longest font family or style name kept (each text key using the font holds a copy).
|
||||
const MAX_FONT_NAME_CHARS: usize = 256;
|
||||
|
||||
/// An embedded image asset handed to the caller to store (the returned path becomes the
|
||||
/// footage item's file).
|
||||
#[derive(Clone, Debug)]
|
||||
@@ -129,9 +132,12 @@ pub fn import(
|
||||
Ctx { im: Im { tb, warnings: vec![] }, assets: BTreeMap::new(), fonts: vec![], store, base_dir, items: vec![], folder: None, name: name.clone() };
|
||||
if let Some(list) = doc.get("fonts").and_then(|f| f.get("list")).and_then(Json::as_array) {
|
||||
for fo in list {
|
||||
// Every text key referring to the font copies its family and style: keep them to
|
||||
// a font name's length.
|
||||
let short = |v: &str| v.chars().take(MAX_FONT_NAME_CHARS).collect::<String>();
|
||||
let n = s(fo, "fName").unwrap_or("").to_string();
|
||||
let fam = s(fo, "fFamily").unwrap_or(&n).to_string();
|
||||
let st = s(fo, "fStyle").unwrap_or("Regular").to_string();
|
||||
let fam = short(s(fo, "fFamily").unwrap_or(&n));
|
||||
let st = short(s(fo, "fStyle").unwrap_or("Regular"));
|
||||
cx.fonts.push((n, fam, st));
|
||||
}
|
||||
}
|
||||
@@ -150,11 +156,12 @@ pub fn import(
|
||||
let duration = tb.tick(op);
|
||||
// Pass 1: asset items (precomps empty for now, images stored).
|
||||
let assets = doc.get("assets").and_then(Json::as_array).cloned().unwrap_or_default();
|
||||
let layer_sizes = precomp_layer_sizes(&doc, &assets);
|
||||
for a in &assets {
|
||||
let Some(id) = s(a, "id") else { continue };
|
||||
if a.get("layers").is_some() {
|
||||
let folder = cx.folder(project);
|
||||
let layer = precomp_layer_size(&doc, &assets, id);
|
||||
let layer = layer_sizes.get(id).copied();
|
||||
let aw = f(a, "w").map(|v| v as u32).or(layer.map(|l| l.0)).unwrap_or(w);
|
||||
let ah = f(a, "h").map(|v| v as u32).or(layer.map(|l| l.1)).unwrap_or(h);
|
||||
let arate = f(a, "fr").map(FrameRate::from_f64).unwrap_or(rate);
|
||||
@@ -255,17 +262,20 @@ pub fn import(
|
||||
Ok(ImportResult { comp: id, items: cx.items, warnings: cx.im.warnings })
|
||||
}
|
||||
|
||||
/// The `w`/`h` of the first precomp layer, in the main comp or in any asset, that shows asset `id`.
|
||||
/// lottie-spec gives a precomposition asset no size: the layer's `w`/`h` are its clipping rect.
|
||||
fn precomp_layer_size(doc: &Json, assets: &[Json], id: &str) -> Option<(u32, u32)> {
|
||||
/// By asset id, the `w`/`h` of the first precomp layer, in the main comp or in any asset, that
|
||||
/// shows it. lottie-spec gives a precomposition asset no size: the layer's `w`/`h` are its
|
||||
/// clipping rect. One pass over all layers, not one per asset.
|
||||
fn precomp_layer_sizes<'a>(doc: &'a Json, assets: &'a [Json]) -> HashMap<&'a str, (u32, u32)> {
|
||||
let mut out = HashMap::new();
|
||||
let lists = doc.get("layers").into_iter().chain(assets.iter().filter_map(|a| a.get("layers")));
|
||||
lists.filter_map(Json::as_array).flatten().find_map(|l| {
|
||||
if l.get("ty").and_then(Json::as_u64) != Some(0) || s(l, "refId") != Some(id) {
|
||||
return None;
|
||||
for l in lists.filter_map(Json::as_array).flatten() {
|
||||
if l.get("ty").and_then(Json::as_u64) != Some(0) {
|
||||
continue;
|
||||
}
|
||||
let (lw, lh) = f(l, "w").zip(f(l, "h"))?;
|
||||
Some((lw.round().max(1.0) as u32, lh.round().max(1.0) as u32))
|
||||
})
|
||||
let (Some(id), Some((lw, lh))) = (s(l, "refId"), f(l, "w").zip(f(l, "h"))) else { continue };
|
||||
out.entry(id).or_insert((lw.round().max(1.0) as u32, lh.round().max(1.0) as u32));
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
fn transform(im: &Im, ks: &Json, tr: &mut PropGroup, next: &mut u64) {
|
||||
@@ -411,8 +421,8 @@ fn layer_source(cx: &mut Ctx, project: &mut Project, comp: &Comp, j: &Json) -> O
|
||||
fn build_layers(cx: &mut Ctx, project: &mut Project, comp: &mut Comp, layers: &[Json]) {
|
||||
let tb = cx.im.tb;
|
||||
let mut by_ind: BTreeMap<i64, LayerId> = BTreeMap::new();
|
||||
// (layer id, parent ind, matte: (kind, explicit tp ind), json position)
|
||||
let mut pending: Vec<(LayerId, Option<i64>, Option<(MatteKind, Option<i64>)>, usize)> = vec![];
|
||||
// (layer id, its index in `comp.layers`, parent ind, matte: (kind, explicit tp ind), json position)
|
||||
let mut pending: Vec<(LayerId, usize, Option<i64>, Option<(MatteKind, Option<i64>)>, usize)> = vec![];
|
||||
let mut pos_ind: Vec<Option<i64>> = vec![];
|
||||
for (ji, j) in layers.iter().enumerate() {
|
||||
let ind = j.get("ind").and_then(Json::as_i64);
|
||||
@@ -463,8 +473,8 @@ fn build_layers(cx: &mut Ctx, project: &mut Project, comp: &mut Comp, layers: &[
|
||||
let mut ids = Ids(&mut next);
|
||||
match &source {
|
||||
LayerSource::Shape => {
|
||||
let items = j.get("shapes").and_then(Json::as_array).cloned().unwrap_or_default();
|
||||
let groups = shapes::import_items(&mut cx.im, &mut ids, &items);
|
||||
let mut items = j.get("shapes").and_then(Json::as_array).into_iter().flatten();
|
||||
let groups = shapes::import_items(&mut cx.im, &mut ids, &mut items);
|
||||
if let Some(c) = l.props.sub_mut("contents") {
|
||||
c.children.extend(groups.into_iter().map(Node::Group));
|
||||
}
|
||||
@@ -507,10 +517,10 @@ fn build_layers(cx: &mut Ctx, project: &mut Project, comp: &mut Comp, layers: &[
|
||||
_ => None,
|
||||
}
|
||||
.map(|k| (k, j.get("tp").and_then(Json::as_i64)));
|
||||
pending.push((l.id, parent, matte, ji));
|
||||
pending.push((l.id, comp.layers.len(), parent, matte, ji));
|
||||
comp.layers.push(l);
|
||||
}
|
||||
for (id, parent, matte, ji) in pending {
|
||||
for (id, at, parent, matte, ji) in pending {
|
||||
let parent_id = parent.and_then(|p| by_ind.get(&p).copied()).filter(|p| *p != id);
|
||||
let matte_id = matte.and_then(|(k, tp)| {
|
||||
let target = match tp {
|
||||
@@ -520,7 +530,7 @@ fn build_layers(cx: &mut Ctx, project: &mut Project, comp: &mut Comp, layers: &[
|
||||
};
|
||||
target.filter(|t| *t != id).map(|t| TrackMatte { layer: t, kind: k })
|
||||
});
|
||||
if let Some(l) = comp.layer_mut(id) {
|
||||
if let Some(l) = comp.layers.get_mut(at) {
|
||||
l.parent = parent_id;
|
||||
l.track_matte = matte_id;
|
||||
}
|
||||
|
||||
@@ -436,7 +436,7 @@ fn import_shape_transform(im: &Im, j: &Json, tr: &mut PropGroup) {
|
||||
}
|
||||
|
||||
/// Import Lottie shape items into shape contents groups.
|
||||
pub(crate) fn import_items(im: &mut Im, ids: &mut Ids, items: &[Json]) -> Vec<PropGroup> {
|
||||
pub(crate) fn import_items(im: &mut Im, ids: &mut Ids, items: &mut dyn Iterator<Item = &Json>) -> Vec<PropGroup> {
|
||||
let mut out = vec![];
|
||||
for j in items {
|
||||
let ty = j.get("ty").and_then(Json::as_str).unwrap_or("");
|
||||
@@ -447,9 +447,10 @@ pub(crate) fn import_items(im: &mut Im, ids: &mut Ids, items: &[Json]) -> Vec<Pr
|
||||
};
|
||||
let mut g = match ty {
|
||||
"gr" => {
|
||||
let list = j.get("it").and_then(Json::as_array).cloned().unwrap_or_default();
|
||||
let inner: Vec<Json> = list.iter().filter(|i| i.get("ty").and_then(Json::as_str) != Some("tr")).cloned().collect();
|
||||
let children = import_items(im, ids, &inner);
|
||||
// Borrowed, not cloned: each nesting level copying its subtree costs memory
|
||||
// quadratic in the depth.
|
||||
let list = j.get("it").and_then(Json::as_array).map(Vec::as_slice).unwrap_or_default();
|
||||
let children = import_items(im, ids, &mut list.iter().filter(|i| i.get("ty").and_then(Json::as_str) != Some("tr")));
|
||||
let mut g = build::shape_group(ids, "Group 1", children);
|
||||
if let Some(tr) = list.iter().find(|i| i.get("ty").and_then(Json::as_str) == Some("tr"))
|
||||
&& let Some(t) = g.sub_mut("transform")
|
||||
|
||||
@@ -997,6 +997,53 @@ fn per_corner_roundness_exports_equal_radii_and_warns_when_mixed() {
|
||||
assert!(!warnings.iter().any(|w| w.contains("per-corner")), "{warnings:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn many_layers_and_precomps_import_with_their_parents_and_sizes() {
|
||||
// 5 000 null layers each parented to the previous, and 2 000 empty precomp assets: the
|
||||
// relationship and size passes index the layers instead of searching them per layer. (Each
|
||||
// has its own name: `Comp::unique_layer_name` numbering one repeated name is a separate cost.)
|
||||
let n = 5_000;
|
||||
let layers: Vec<Json> = (1..=n).map(|i| json!({"ty": 3, "nm": format!("N{i}"), "ind": i, "parent": i - 1, "ip": 0, "op": 30, "st": 0, "ks": {}})).collect();
|
||||
let mut assets: Vec<Json> = (0..2_000).map(|i| json!({"id": format!("c{i}"), "layers": []})).collect();
|
||||
assets.push(json!({"id": "last", "layers": []}));
|
||||
let mut layers = layers;
|
||||
layers.push(json!({"ty": 0, "ind": n + 1, "refId": "last", "w": 50, "h": 40, "ip": 0, "op": 30, "st": 0, "ks": {}}));
|
||||
let doc = json!({"w": 200, "h": 120, "fr": 30, "ip": 0, "op": 30, "assets": assets, "layers": layers});
|
||||
let mut q = Project::default();
|
||||
let nc = import(&mut q, doc.to_string().as_bytes(), "Imported", "", &mut |_| None).unwrap().comp;
|
||||
let c = q.comp(nc).unwrap();
|
||||
assert_eq!(c.layers.len(), n as usize + 1);
|
||||
assert_eq!(c.layers.get(0).unwrap().parent, None);
|
||||
assert_eq!(c.layers.get(1).unwrap().parent, Some(c.layers.get(0).unwrap().id));
|
||||
let LayerSource::Comp { item } = c.layers.get(n as usize).unwrap().source else { panic!("not a precomp") };
|
||||
let pc = q.comp(item).unwrap();
|
||||
assert_eq!((pc.width, pc.height), (50, 40));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn deeply_nested_shape_groups_and_long_font_names_import() {
|
||||
let mut g = json!({"ty": "rc", "s": {"a": 0, "k": [10, 10]}, "p": {"a": 0, "k": [0, 0]}, "r": {"a": 0, "k": 0}});
|
||||
// 50 levels: each is two levels of JSON, within serde_json's recursion limit of 128.
|
||||
for _ in 0..50 {
|
||||
g = json!({"ty": "gr", "it": [g, {"ty": "tr", "o": {"a": 0, "k": 100}}]});
|
||||
}
|
||||
let family = "F".repeat(100_000);
|
||||
let keys: Vec<Json> = (0..50).map(|i| json!({"t": i, "s": {"t": "x", "f": "A", "s": 10}})).collect();
|
||||
let doc = json!({"w": 200, "h": 120, "fr": 30, "ip": 0, "op": 60,
|
||||
"fonts": {"list": [{"fName": "A", "fFamily": family, "fStyle": "Regular"}]},
|
||||
"layers": [
|
||||
{"ty": 4, "ind": 1, "ip": 0, "op": 60, "st": 0, "ks": {}, "shapes": [g]},
|
||||
{"ty": 5, "ind": 2, "ip": 0, "op": 60, "st": 0, "ks": {}, "t": {"d": {"k": keys}}}
|
||||
]});
|
||||
let mut q = Project::default();
|
||||
let nc = import(&mut q, doc.to_string().as_bytes(), "Imported", "", &mut |_| None).unwrap().comp;
|
||||
let c = q.comp(nc).unwrap();
|
||||
let st = c.layers.get(1).unwrap().props.prop("text/sourceText").unwrap();
|
||||
assert_eq!(st.keys.len(), 50);
|
||||
let Value::Text(d) = &st.keys[0].value else { panic!("not text") };
|
||||
assert_eq!(d.font.len(), 256);
|
||||
}
|
||||
|
||||
/// A footage item called a still whose file is not an image is not embedded.
|
||||
#[test]
|
||||
fn only_image_bytes_are_embedded() {
|
||||
|
||||
@@ -310,7 +310,8 @@ pub(crate) fn import_text(im: &mut Im, ids: &mut Ids, t: &Json, tg: &mut PropGro
|
||||
st.value = Value::Text(Box::new(first.clone()));
|
||||
}
|
||||
if docs.len() > 1 {
|
||||
st.keys = docs.iter().map(|(f, d)| effectcraft_keyframe::Keyframe::new(im.tb.tick(*f), Value::Text(Box::new(d.clone()))).hold()).collect();
|
||||
// Moved into the keys, not cloned: the documents are not kept twice.
|
||||
st.keys = docs.into_iter().map(|(f, d)| effectcraft_keyframe::Keyframe::new(im.tb.tick(f), Value::Text(Box::new(d))).hold()).collect();
|
||||
}
|
||||
}
|
||||
if let Some(m) = t.get("m")
|
||||
|
||||
@@ -38,5 +38,6 @@ thiserror = { workspace = true }
|
||||
log = { workspace = true }
|
||||
|
||||
[dev-dependencies]
|
||||
tempfile = { workspace = true }
|
||||
# (tests render comps that read layers through effects; already a dependency of effectcraft-render)
|
||||
effectcraft-effects = { workspace = true }
|
||||
@@ -747,8 +747,8 @@ fn sequence_gaps_hold_the_previous_frame() {
|
||||
/// parallax project's 21120-px plates were decoded and resampled for every frame at Quarter).
|
||||
#[test]
|
||||
fn stills_at_reduced_sizes_are_resampled_once() {
|
||||
let dir = std::env::temp_dir().join(format!("ec-still-size-{}", std::process::id()));
|
||||
std::fs::create_dir_all(&dir).expect("temp dir");
|
||||
let guard = tempfile::tempdir().expect("temp dir");
|
||||
let dir = guard.path().to_path_buf();
|
||||
let p = dir.join("plate.png");
|
||||
let px = |x: u32, y: u32| {
|
||||
image::Rgba([(x * 7 % 256) as u8, (y * 13 % 256) as u8, ((x ^ y) % 256) as u8, if (x + y).is_multiple_of(5) { 0 } else { 255 - (x % 128) as u8 }])
|
||||
|
||||
@@ -311,6 +311,9 @@ fn frame(v: &[P]) -> (P, f64) {
|
||||
(c, r)
|
||||
}
|
||||
|
||||
/// Vertex counts above this skip the shape contexts (quadratic to build) and search the starting
|
||||
/// vertex coarsely; no mask comes near it (the Add Vertices option itself stops at 2000).
|
||||
const MAX_CONTEXT_VERTICES: usize = 4096;
|
||||
const RBINS: usize = 5;
|
||||
const ABINS: usize = 12;
|
||||
|
||||
@@ -412,10 +415,18 @@ pub fn correspond(a: &ShapePath, b: &ShapePath, o: &InterpOpts) -> (ShapePath, S
|
||||
}
|
||||
if !o.first_vertices_match {
|
||||
let n = ra.len();
|
||||
let use_sc = o.quality > 0.0;
|
||||
let large = n > MAX_CONTEXT_VERTICES;
|
||||
let use_sc = o.quality > 0.0 && !large;
|
||||
let (sa, sb) = if use_sc { (Some(shape_contexts(&ra.vertices)), Some(shape_contexts(&rb.vertices))) } else { (None, None) };
|
||||
// Quality limits the offsets examined exhaustively (coarse step), then refines.
|
||||
let step = if o.quality >= 50.0 { 1 } else { ((n as f64 / 24.0).ceil() as usize).max(1) };
|
||||
// (A very large shape tries about 3 √n offsets, not n, each costing a pass over it.)
|
||||
let step = if large {
|
||||
(n as f64).sqrt().ceil() as usize
|
||||
} else if o.quality >= 50.0 {
|
||||
1
|
||||
} else {
|
||||
((n as f64 / 24.0).ceil() as usize).max(1)
|
||||
};
|
||||
let mut best = (f64::MAX, 0usize);
|
||||
let mut k = 0;
|
||||
while k < n {
|
||||
@@ -592,4 +603,22 @@ mod tests {
|
||||
let l2 = (lin.vertices[1][0] - lin.vertices[0][0]).hypot(lin.vertices[1][1] - lin.vertices[0][1]);
|
||||
assert!(l2 < 80.0);
|
||||
}
|
||||
|
||||
/// A shape with tens of thousands of vertices is matched without the quadratic shape contexts
|
||||
/// and exhaustive offset search.
|
||||
#[test]
|
||||
fn very_large_shapes_are_matched_in_bounded_time() {
|
||||
let n = 30_000;
|
||||
let circle = |rot: f64| -> ShapePath {
|
||||
let pts: Vec<P> = (0..n)
|
||||
.map(|i| [100.0 * (rot + i as f64 * std::f64::consts::TAU / n as f64).cos(), 100.0 * (rot + i as f64 * std::f64::consts::TAU / n as f64).sin()])
|
||||
.collect();
|
||||
ShapePath::polygon(&pts, true)
|
||||
};
|
||||
let o = InterpOpts { one_to_one: true, quality: 100.0, ..Default::default() };
|
||||
let t = std::time::Instant::now();
|
||||
let (a, b) = correspond(&circle(0.0), &circle(1.0), &o);
|
||||
assert_eq!((a.len(), b.len()), (n, n));
|
||||
assert!(t.elapsed().as_secs() < 30, "{:?}", t.elapsed());
|
||||
}
|
||||
}
|
||||
@@ -220,6 +220,56 @@ fn disc(out: &mut BezPath, c: Point, r: f64) {
|
||||
}
|
||||
}
|
||||
|
||||
/// `path` with each curve whose extent is over 10⁷ × `tol` replaced by lines flattened at 10⁻⁷ of
|
||||
/// that extent. `None` when a coordinate is not finite.
|
||||
fn coarsen_huge(path: &BezPath, tol: f64) -> Option<BezPath> {
|
||||
use kurbo::Shape as _;
|
||||
let mut out = BezPath::new();
|
||||
let (mut cur, mut start) = (Point::ZERO, Point::ZERO);
|
||||
for el in path.elements() {
|
||||
let seg = match *el {
|
||||
PathEl::MoveTo(p) => {
|
||||
(cur, start) = (p, p);
|
||||
out.push(*el);
|
||||
continue;
|
||||
}
|
||||
PathEl::LineTo(p) => {
|
||||
cur = p;
|
||||
out.push(*el);
|
||||
continue;
|
||||
}
|
||||
PathEl::ClosePath => {
|
||||
cur = start;
|
||||
out.push(*el);
|
||||
continue;
|
||||
}
|
||||
PathEl::QuadTo(_, p) | PathEl::CurveTo(_, _, p) => {
|
||||
let mut one = BezPath::new();
|
||||
one.move_to(cur);
|
||||
one.push(*el);
|
||||
(one, p)
|
||||
}
|
||||
};
|
||||
let (one, end) = seg;
|
||||
let bb = one.bounding_box();
|
||||
let extent = bb.width().max(bb.height());
|
||||
if !extent.is_finite() {
|
||||
return None;
|
||||
}
|
||||
if extent * 1e-7 > tol {
|
||||
kurbo::flatten(one.elements().iter().copied(), extent * 1e-7, |e| {
|
||||
if let PathEl::LineTo(p) = e {
|
||||
out.line_to(p);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
out.push(*el);
|
||||
}
|
||||
cur = end;
|
||||
}
|
||||
Some(out)
|
||||
}
|
||||
|
||||
/// Outline of a tapered / waved stroke (local space; `res_scale` = device/local scale).
|
||||
pub fn outline(paths: &[BezPath], style: &StrokeStyle, res_scale: f64) -> Option<BezPath> {
|
||||
if style.width <= 0.0 {
|
||||
@@ -230,7 +280,11 @@ pub fn outline(paths: &[BezPath], style: &StrokeStyle, res_scale: f64) -> Option
|
||||
let half = style.width / 2.0;
|
||||
for path in paths {
|
||||
// Fine enough for the width profile: ½ device pixel, at most ~20 000 samples per path.
|
||||
let rough = flatten(path, 0.1 / rs, f64::INFINITY);
|
||||
// Curves with huge extents are first cut into lines at a tolerance of 10⁻⁷ of their own
|
||||
// extent, so none subdivides into billions of points before the step below applies;
|
||||
// every other segment (small curves in a large path included) keeps the ½-pixel tolerance.
|
||||
let Some(path) = coarsen_huge(path, 0.1 / rs) else { continue };
|
||||
let rough = flatten(&path, 0.1 / rs, f64::INFINITY);
|
||||
for poly0 in rough {
|
||||
let len0 = poly0.len();
|
||||
if len0 <= 0.0 {
|
||||
@@ -396,4 +450,37 @@ mod tests {
|
||||
let area: f32 = m.data.iter().sum();
|
||||
assert!(area > 240.0 * 6.0 * 0.8 && area < 240.0 * 6.0 * 1.05, "{area}");
|
||||
}
|
||||
|
||||
/// A curve with enormous coordinates is approximated within a bounded number of points.
|
||||
#[test]
|
||||
fn huge_coordinates_do_not_explode_the_first_approximation() {
|
||||
let mut p = BezPath::new();
|
||||
p.move_to((0.0, 0.0));
|
||||
p.curve_to((1e13, 0.0), (-1e13, 1e13), (1e13, 1e13));
|
||||
let st = StrokeStyle {
|
||||
width: 4.0,
|
||||
taper: Some(Taper { percent: true, start_len: 50.0, end_len: 50.0, start_width: 0.0, end_width: 0.0, start_ease: 0.0, end_ease: 0.0 }),
|
||||
..Default::default()
|
||||
};
|
||||
let t = std::time::Instant::now();
|
||||
let o = outline(&[p], &st, 1.0).unwrap();
|
||||
assert!(o.elements().len() < 200_000, "{}", o.elements().len());
|
||||
assert!(t.elapsed().as_secs() < 20);
|
||||
}
|
||||
|
||||
/// Small curves keep their shape in a path that also has a huge segment.
|
||||
#[test]
|
||||
fn small_curves_survive_a_huge_segment() {
|
||||
let mut p = BezPath::new();
|
||||
p.move_to((0.0, 0.0));
|
||||
p.curve_to((100.0, 0.0), (0.0, 100.0), (0.0, 0.0));
|
||||
p.line_to((1e10, 0.0));
|
||||
let c = coarsen_huge(&p, 0.1).unwrap();
|
||||
assert_eq!(c.elements().len(), p.elements().len(), "nothing here is huge enough to cut");
|
||||
let mut q = BezPath::new();
|
||||
q.move_to((0.0, 0.0));
|
||||
q.curve_to((1e13, 0.0), (-1e13, 1e13), (1e13, 1e13));
|
||||
let c = coarsen_huge(&q, 0.1).unwrap();
|
||||
assert!(c.elements().iter().skip(1).all(|e| matches!(e, PathEl::LineTo(_))) && c.elements().len() < 10_000);
|
||||
}
|
||||
}
|
||||
+32
-4
@@ -7,6 +7,9 @@ use effectcraft_svg::{Affine, BezPath, FillRule, Geom, Group, Node, Paint, Shape
|
||||
/// PostScript program, or the PDF content stream.
|
||||
pub(crate) const MAX_PATH: usize = 1 << 20;
|
||||
|
||||
/// The deepest the open groups nest (the render tree and its consumers recurse through them).
|
||||
const MAX_DEPTH: usize = 256;
|
||||
|
||||
#[derive(Clone, Copy, PartialEq, Eq)]
|
||||
enum Kind {
|
||||
Plain,
|
||||
@@ -20,6 +23,9 @@ pub(crate) struct Builder {
|
||||
pub top_layers: Vec<bool>,
|
||||
pub skipped: Vec<String>,
|
||||
pub shapes: usize,
|
||||
/// Groups the finished nodes will sit under once placed (an enclosing builder's open groups
|
||||
/// and the pattern or mask groups around them), counted against [`MAX_DEPTH`].
|
||||
base: usize,
|
||||
}
|
||||
|
||||
pub(crate) fn group(name: &str) -> Group {
|
||||
@@ -28,13 +34,25 @@ pub(crate) fn group(name: &str) -> Group {
|
||||
|
||||
impl Builder {
|
||||
pub fn new() -> Builder {
|
||||
Builder { stack: vec![(group("page"), Kind::Plain)], top_layers: vec![], skipped: vec![], shapes: 0 }
|
||||
Builder { stack: vec![(group("page"), Kind::Plain)], top_layers: vec![], skipped: vec![], shapes: 0, base: 0 }
|
||||
}
|
||||
|
||||
/// A builder for content that will be placed inside this one's open groups and `wrap` more
|
||||
/// groups (a pattern's `Pattern` and `Tile`, a soft mask's two), so its nesting limit counts
|
||||
/// them.
|
||||
pub fn nested(&self, wrap: usize) -> Builder {
|
||||
Builder { base: self.base.saturating_add(self.stack.len()).saturating_add(wrap), ..Builder::new() }
|
||||
}
|
||||
|
||||
pub fn depth(&self) -> usize {
|
||||
self.stack.len()
|
||||
}
|
||||
|
||||
/// `wrap` more groups fit under the open ones within [`MAX_DEPTH`].
|
||||
pub fn has_room(&self, wrap: usize) -> bool {
|
||||
self.base.saturating_add(self.stack.len()).saturating_add(wrap) <= MAX_DEPTH
|
||||
}
|
||||
|
||||
pub fn skip(&mut self, what: &str) {
|
||||
if !self.skipped.iter().any(|s| s == what) {
|
||||
self.skipped.push(what.to_string());
|
||||
@@ -62,23 +80,33 @@ impl Builder {
|
||||
}
|
||||
}
|
||||
|
||||
/// Open `g` as the group drawn into from now on; past [`MAX_DEPTH`] the group is dropped
|
||||
/// (what would have been drawn inside it goes to the open group) and noted as skipped.
|
||||
fn push_group(&mut self, g: Group, kind: Kind) {
|
||||
if self.base.saturating_add(self.stack.len()) >= MAX_DEPTH {
|
||||
self.skip("group nesting limit");
|
||||
return;
|
||||
}
|
||||
self.stack.push((g, kind));
|
||||
}
|
||||
|
||||
/// Children drawn from now on are clipped by `path` (page space) until the group closes.
|
||||
pub fn clip(&mut self, path: BezPath, rule: FillRule) {
|
||||
let mut g = group("Clip Group");
|
||||
g.clip.push((path, rule));
|
||||
// Clipping is not a transparency group: blend modes inside reach the backdrop.
|
||||
g.isolated = false;
|
||||
self.stack.push((g, Kind::Clip));
|
||||
self.push_group(g, Kind::Clip);
|
||||
}
|
||||
|
||||
/// Open an optional-content layer (or a named group).
|
||||
pub fn open(&mut self, name: &str, layer: bool) {
|
||||
self.stack.push((group(name), if layer { Kind::Layer } else { Kind::Plain }));
|
||||
self.push_group(group(name), if layer { Kind::Layer } else { Kind::Plain });
|
||||
}
|
||||
|
||||
/// Open a group (blend mode, soft mask) closed with the graphics state.
|
||||
pub fn open_group(&mut self, g: Group) {
|
||||
self.stack.push((g, Kind::Plain));
|
||||
self.push_group(g, Kind::Plain);
|
||||
}
|
||||
|
||||
/// Add a finished node (an image, pattern tiles).
|
||||
|
||||
+122
-26
@@ -215,19 +215,44 @@ pub enum PsOp {
|
||||
Proc(Vec<PsOp>),
|
||||
}
|
||||
|
||||
fn ps_parse(lx: &mut crate::object::Lexer, depth: usize) -> Vec<PsOp> {
|
||||
/// Parse a procedure. `ops` counts the tokens read (those skipped as unsupported included); past
|
||||
/// [`MAX_CALC_OPS`] the rest is not read, so an oversized program is never fully scanned or built
|
||||
/// (the caller rejects it when `ops` ends past the cap).
|
||||
fn ps_parse(lx: &mut crate::object::Lexer, depth: usize, ops: &mut usize) -> Vec<PsOp> {
|
||||
let mut out = vec![];
|
||||
while let Some(o) = lx.next() {
|
||||
match o {
|
||||
Obj::Num(n) => out.push(PsOp::Num(n)),
|
||||
Obj::Bool(b) => out.push(PsOp::Num(if b { 1.0 } else { 0.0 })),
|
||||
Obj::Op(op) if op == "{" && depth < 32 => out.push(PsOp::Proc(ps_parse(lx, depth + 1))),
|
||||
// Nested too deep: skip the procedure's opening brace (never push it as an operator).
|
||||
Obj::Op(op) if op == "{" => {}
|
||||
Obj::Op(op) if op == "}" => return out,
|
||||
Obj::Op(op) => out.push(PsOp::Op(op)),
|
||||
_ => {}
|
||||
loop {
|
||||
// A calculator holds no arrays or dictionaries: one is skipped as a single token without
|
||||
// being built (the lexer would allocate every element before this counted it).
|
||||
if lx.at_compound() {
|
||||
if *ops > MAX_CALC_OPS {
|
||||
break;
|
||||
}
|
||||
lx.skip_compound();
|
||||
*ops += 1;
|
||||
continue;
|
||||
}
|
||||
let Some(o) = lx.next() else { break };
|
||||
if *ops > MAX_CALC_OPS {
|
||||
break;
|
||||
}
|
||||
let op = match o {
|
||||
Obj::Num(n) => PsOp::Num(n),
|
||||
Obj::Bool(b) => PsOp::Num(if b { 1.0 } else { 0.0 }),
|
||||
Obj::Op(op) if op == "{" && depth < 32 => PsOp::Proc(ps_parse(lx, depth + 1, ops)),
|
||||
// Nested too deep: skip the procedure's opening brace (never push it as an operator).
|
||||
Obj::Op(op) if op == "{" => {
|
||||
*ops += 1;
|
||||
continue;
|
||||
}
|
||||
Obj::Op(op) if op == "}" => return out,
|
||||
Obj::Op(op) => PsOp::Op(op),
|
||||
_ => {
|
||||
*ops += 1;
|
||||
continue;
|
||||
}
|
||||
};
|
||||
*ops += 1;
|
||||
out.push(op);
|
||||
}
|
||||
out
|
||||
}
|
||||
@@ -243,6 +268,15 @@ fn ps_push(st: &mut Vec<f64>, v: f64) {
|
||||
}
|
||||
}
|
||||
|
||||
/// The most operators and operands a calculator program may hold (nested procedures included).
|
||||
/// It has no loops, so this bounds what one evaluation runs; real programs hold a few dozen.
|
||||
const MAX_CALC_OPS: usize = 4096;
|
||||
|
||||
/// The operators and operands in `code`, nested procedures included.
|
||||
fn ps_len(code: &[PsOp]) -> usize {
|
||||
code.iter().map(|op| if let PsOp::Proc(p) = op { 1 + ps_len(p) } else { 1 }).sum()
|
||||
}
|
||||
|
||||
fn ps_run(code: &[PsOp], st: &mut Vec<f64>, depth: usize) {
|
||||
if depth > 32 {
|
||||
return;
|
||||
@@ -431,6 +465,12 @@ impl Budget {
|
||||
Budget { nodes: MAX_NODES, samples: MAX_SAMPLES, overrun: false }
|
||||
}
|
||||
|
||||
/// Charge `n` retained values (coefficients, bounds, program operators) as nodes, eight to a
|
||||
/// node, so a cached copy of a function is charged for what it holds, not just its node.
|
||||
fn take_values(&mut self, n: usize) -> bool {
|
||||
self.take_nodes(n.div_ceil(8))
|
||||
}
|
||||
|
||||
/// Charge `n` nodes; `false` (and the read is abandoned) once the cap is passed.
|
||||
fn take_nodes(&mut self, n: usize) -> bool {
|
||||
match self.nodes.checked_sub(n) {
|
||||
@@ -484,7 +524,8 @@ impl Func {
|
||||
/// [`Func::read`] nested `depth` levels deep, charging `budget`: unsupported past [`MAX_DEPTH`]
|
||||
/// or once the budget is spent.
|
||||
fn read_at(file: &File, o: &Obj, depth: usize, budget: &mut Budget) -> Func {
|
||||
if depth >= MAX_DEPTH || !budget.take_nodes(1) {
|
||||
// Charged first: the fallback past the depth limit is a node a copy keeps too.
|
||||
if !budget.take_nodes(1) || depth >= MAX_DEPTH {
|
||||
return Func::Unsupported;
|
||||
}
|
||||
let o = file.resolve(o);
|
||||
@@ -499,12 +540,14 @@ impl Func {
|
||||
let dom = file.get(d, "Domain").map(|x| file.nums(x)).unwrap_or_default();
|
||||
let domain = [dom.first().copied().unwrap_or(0.0), dom.get(1).copied().unwrap_or(1.0)];
|
||||
match file.get_num(d, "FunctionType").unwrap_or(-1.0) as i32 {
|
||||
2 => Func::Exp {
|
||||
domain,
|
||||
c0: file.get(d, "C0").map(|x| file.nums(x)).unwrap_or_else(|| vec![0.0]),
|
||||
c1: file.get(d, "C1").map(|x| file.nums(x)).unwrap_or_else(|| vec![1.0]),
|
||||
n: file.get_num(d, "N").unwrap_or(1.0),
|
||||
},
|
||||
2 => {
|
||||
let c0 = file.get(d, "C0").map(|x| file.nums(x)).unwrap_or_else(|| vec![0.0]);
|
||||
let c1 = file.get(d, "C1").map(|x| file.nums(x)).unwrap_or_else(|| vec![1.0]);
|
||||
if !budget.take_values(c0.len() + c1.len()) {
|
||||
return Func::Unsupported;
|
||||
}
|
||||
Func::Exp { domain, c0, c1, n: file.get_num(d, "N").unwrap_or(1.0) }
|
||||
}
|
||||
3 => {
|
||||
// A stitching function's pieces: no element cap (a repeating gradient lists one
|
||||
// piece once per repeat); the budget bounds the total work.
|
||||
@@ -513,12 +556,12 @@ impl Func {
|
||||
None => Some(vec![]),
|
||||
};
|
||||
let Some(funcs) = funcs else { return Func::Unsupported };
|
||||
Func::Stitch {
|
||||
domain,
|
||||
funcs,
|
||||
bounds: file.get(d, "Bounds").map(|x| file.nums(x)).unwrap_or_default(),
|
||||
encode: file.get(d, "Encode").map(|x| file.nums(x)).unwrap_or_default(),
|
||||
let bounds = file.get(d, "Bounds").map(|x| file.nums(x)).unwrap_or_default();
|
||||
let encode = file.get(d, "Encode").map(|x| file.nums(x)).unwrap_or_default();
|
||||
if !budget.take_values(bounds.len() + encode.len()) {
|
||||
return Func::Unsupported;
|
||||
}
|
||||
Func::Stitch { domain, funcs, bounds, encode }
|
||||
}
|
||||
0 => {
|
||||
let Obj::Stream(sd, raw) = o else { return Func::Unsupported };
|
||||
@@ -537,6 +580,10 @@ impl Func {
|
||||
if !budget.take_samples(count) {
|
||||
return Func::Unsupported;
|
||||
}
|
||||
// The stream must hold the table: missing bytes are not made up as zeros.
|
||||
if count.checked_mul(bps).is_none_or(|bits| bits.div_ceil(8) > data.len()) {
|
||||
return Func::Unsupported;
|
||||
}
|
||||
let enc = file.get(d, "Encode").map(|x| file.nums(x)).unwrap_or_default();
|
||||
let encode = [enc.first().copied().unwrap_or(0.0), enc.get(1).copied().unwrap_or(size as f64 - 1.0)];
|
||||
let decode = file.get(d, "Decode").map(|x| file.nums(x)).filter(|v| v.len() >= outputs * 2).unwrap_or_else(|| range.clone());
|
||||
@@ -567,10 +614,15 @@ impl Func {
|
||||
let Some(data) = decode_stream(file, sd, raw) else { return Func::Unsupported };
|
||||
let mut lx = crate::object::Lexer::new(&data, 0);
|
||||
// The program is one procedure: `{ … }`.
|
||||
let code = match ps_parse(&mut lx, 0).into_iter().next() {
|
||||
let mut scanned = 0;
|
||||
let code = match ps_parse(&mut lx, 0, &mut scanned).into_iter().next() {
|
||||
Some(PsOp::Proc(p)) => p,
|
||||
_ => return Func::Unsupported,
|
||||
};
|
||||
let ops = ps_len(&code);
|
||||
if scanned > MAX_CALC_OPS || ops > MAX_CALC_OPS || !budget.take_values(ops) {
|
||||
return Func::Unsupported;
|
||||
}
|
||||
Func::Calc { domain: dom, range: file.get(d, "Range").map(|x| file.nums(x)).unwrap_or_default(), code }
|
||||
}
|
||||
_ => Func::Unsupported,
|
||||
@@ -756,12 +808,56 @@ mod tests {
|
||||
assert_eq!(sample_count(1 << 22, usize::MAX / (1 << 22) + 1), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn oversized_calculator_program_is_cut_short() {
|
||||
let src = format!("{{ {}}}", "1 ".repeat(MAX_CALC_OPS + 10));
|
||||
let mut lx = crate::object::Lexer::new(src.as_bytes(), 0);
|
||||
let Some(PsOp::Proc(code)) = ps_parse(&mut lx, 0, &mut 0).into_iter().next() else { panic!() };
|
||||
assert_eq!(ps_len(&code), MAX_CALC_OPS + 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn arrays_and_dictionaries_in_a_calculator_are_skipped_whole() {
|
||||
// Strings with brackets, nested arrays and a dictionary are one skipped token each; the
|
||||
// operators around them still parse.
|
||||
let src = b"{ 1 [ (a]b) [1 2] << /K [3 4] /S (>>) >> 5 ] 2 add << /A 1 >> mul }";
|
||||
let mut lx = crate::object::Lexer::new(src, 0);
|
||||
let Some(PsOp::Proc(code)) = ps_parse(&mut lx, 0, &mut 0).into_iter().next() else { panic!() };
|
||||
assert_eq!(code, vec![PsOp::Num(1.0), PsOp::Num(2.0), PsOp::Op("add".into()), PsOp::Op("mul".into())]);
|
||||
// A huge array is skipped once the cap is passed, not read to its end.
|
||||
let src = format!("{{ [ {}] 1 }}", "0 ".repeat(MAX_CALC_OPS * 100));
|
||||
let mut lx = crate::object::Lexer::new(src.as_bytes(), 0);
|
||||
let mut scanned = 0;
|
||||
let Some(PsOp::Proc(code)) = ps_parse(&mut lx, 0, &mut scanned).into_iter().next() else { panic!() };
|
||||
assert_eq!(code, vec![PsOp::Num(1.0)]);
|
||||
// A large string inside a skipped array is passed over, not decoded: escapes, nested
|
||||
// parentheses, a hexadecimal string and names all end where the lexer would end them.
|
||||
let big = format!("{{ [ ({}) <{}> /{} (a\\)b(c)) 7 ] 1 }}", "x".repeat(1 << 20), "ab".repeat(1 << 19), "N".repeat(1 << 10));
|
||||
let mut lx = crate::object::Lexer::new(big.as_bytes(), 0);
|
||||
let Some(PsOp::Proc(code)) = ps_parse(&mut lx, 0, &mut 0).into_iter().next() else { panic!() };
|
||||
assert_eq!(code, vec![PsOp::Num(1.0)]);
|
||||
// An unclosed one ends at the end of the data.
|
||||
let mut lx = crate::object::Lexer::new(b"[ 1 2 ", 0);
|
||||
lx.skip_compound();
|
||||
assert_eq!(lx.pos, 6);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn skipped_calculator_tokens_count_toward_the_cap() {
|
||||
// `null` is not a calculator token: skipped, but still scanned.
|
||||
let src = format!("{{ {}}}", "null ".repeat(MAX_CALC_OPS + 10));
|
||||
let mut lx = crate::object::Lexer::new(src.as_bytes(), 0);
|
||||
let mut scanned = 0;
|
||||
ps_parse(&mut lx, 0, &mut scanned);
|
||||
assert!(scanned > MAX_CALC_OPS && scanned < MAX_CALC_OPS + 10, "parsing stops once the cap is passed ({scanned})");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn calculator_functions() {
|
||||
// Two inputs → three outputs: { 2 copy mul 3 1 roll add 2 div dup 0.5 gt { pop 1 } if 0 }
|
||||
let code = b"{ 2 copy mul 3 1 roll add 2 div dup 0.5 gt { pop 1 } { 0.25 mul } ifelse 0 }";
|
||||
let mut lx = crate::object::Lexer::new(code, 0);
|
||||
let Some(PsOp::Proc(code)) = ps_parse(&mut lx, 0).into_iter().next() else { panic!() };
|
||||
let Some(PsOp::Proc(code)) = ps_parse(&mut lx, 0, &mut 0).into_iter().next() else { panic!() };
|
||||
let f = Func::Calc { domain: vec![0.0, 1.0, 0.0, 1.0], range: vec![0.0, 1.0, 0.0, 1.0, 0.0, 1.0], code };
|
||||
assert_eq!(f.eval(&[0.5, 0.4]), vec![0.2, 0.1125, 0.0]);
|
||||
assert_eq!(f.eval(&[1.0, 0.8]), vec![0.8, 1.0, 0.0]);
|
||||
@@ -771,7 +867,7 @@ mod tests {
|
||||
fn run(src: &str, mut st: Vec<f64>) -> Vec<f64> {
|
||||
let src = format!("{{ {src} }}");
|
||||
let mut lx = crate::object::Lexer::new(src.as_bytes(), 0);
|
||||
let Some(PsOp::Proc(code)) = ps_parse(&mut lx, 0).into_iter().next() else { panic!() };
|
||||
let Some(PsOp::Proc(code)) = ps_parse(&mut lx, 0, &mut 0).into_iter().next() else { panic!() };
|
||||
ps_run(&code, &mut st, 0);
|
||||
st
|
||||
}
|
||||
|
||||
@@ -82,6 +82,91 @@ impl<'a> Lexer<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether the next token opens an array or a dictionary (skipping white space first).
|
||||
pub fn at_compound(&mut self) -> bool {
|
||||
self.skip_ws();
|
||||
match self.data.get(self.pos) {
|
||||
Some(b'[') => true,
|
||||
Some(b'<') => self.data.get(self.pos + 1) == Some(&b'<'),
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
/// Skip the array or dictionary at the current position (see [`Lexer::at_compound`]) as
|
||||
/// [`Lexer::next`] would read it, without building it: for readers that have no use for one
|
||||
/// and must not allocate a vector per element of a hostile stream.
|
||||
pub fn skip_compound(&mut self) {
|
||||
let mut open = 0usize;
|
||||
loop {
|
||||
self.skip_ws();
|
||||
match self.data.get(self.pos) {
|
||||
None => return,
|
||||
Some(b'[') => {
|
||||
open += 1;
|
||||
self.pos += 1;
|
||||
}
|
||||
Some(b'<') if self.data.get(self.pos + 1) == Some(&b'<') => {
|
||||
open += 1;
|
||||
self.pos += 2;
|
||||
}
|
||||
Some(b']') | Some(b'>') if self.data[self.pos] == b']' || self.data.get(self.pos + 1) == Some(&b'>') => {
|
||||
open = open.saturating_sub(1);
|
||||
self.pos += if self.data[self.pos] == b']' { 1 } else { 2 };
|
||||
if open == 0 {
|
||||
return;
|
||||
}
|
||||
}
|
||||
// Anything else is one token (a string, name, number or word), passed over
|
||||
// without building it.
|
||||
Some(_) => self.skip_scalar(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Pass over one token that is not an array or dictionary bracket, as [`Lexer::next`] reads
|
||||
/// it but without building its value.
|
||||
fn skip_scalar(&mut self) {
|
||||
let Some(&c) = self.data.get(self.pos) else { return };
|
||||
match c {
|
||||
b'(' => {
|
||||
// A literal string: balanced parentheses, a backslash escapes the next byte.
|
||||
let mut depth = 0usize;
|
||||
while let Some(&d) = self.data.get(self.pos) {
|
||||
self.pos += 1;
|
||||
match d {
|
||||
b'\\' => self.pos += 1,
|
||||
b'(' => depth += 1,
|
||||
b')' => {
|
||||
depth -= 1;
|
||||
if depth == 0 {
|
||||
break;
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
self.pos = self.pos.min(self.data.len());
|
||||
}
|
||||
b'<' => {
|
||||
// A hexadecimal string.
|
||||
while self.data.get(self.pos).is_some_and(|d| *d != b'>') {
|
||||
self.pos += 1;
|
||||
}
|
||||
self.pos = (self.pos + 1).min(self.data.len());
|
||||
}
|
||||
b'/' => {
|
||||
self.pos += 1;
|
||||
self.word();
|
||||
}
|
||||
b']' | b'{' | b'}' | b')' | b'>' => self.pos += 1,
|
||||
_ => {
|
||||
if self.word().is_empty() {
|
||||
self.pos += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn word(&mut self) -> &'a [u8] {
|
||||
let s = self.pos;
|
||||
while self.pos < self.data.len() && !is_white(self.data[self.pos]) && !is_delim(self.data[self.pos]) {
|
||||
|
||||
+14
-5
@@ -669,7 +669,12 @@ impl<'a> Interp<'a> {
|
||||
let bc = f.get(&sm, "BC").map(|b| f.nums(b)).unwrap_or_default();
|
||||
let gcs = f.get_dict(&gd, "Group").and_then(|gr| f.get(gr, "CS")).map(|c| color_space(f, c, Some(res))).unwrap_or(Cs::Rgb);
|
||||
let backdrop = if bc.is_empty() { [0.0; 3] } else { gcs.to_rgb(&bc) };
|
||||
let saved_b = std::mem::replace(&mut self.b, Builder::new());
|
||||
// The mask it replaces closes first: its groups don't enclose the new one.
|
||||
if let Some(d) = self.gs.mask_depth {
|
||||
self.close_to(d);
|
||||
}
|
||||
let inner = self.b.nested(2);
|
||||
let saved_b = std::mem::replace(&mut self.b, inner);
|
||||
let ctm = self.gs.ctm;
|
||||
let saved_gs = std::mem::replace(&mut self.gs, GState { ctm, ..GState::default() });
|
||||
let saved_stack = std::mem::take(&mut self.saved);
|
||||
@@ -681,9 +686,6 @@ impl<'a> Interp<'a> {
|
||||
for s in sk {
|
||||
self.b.skip(&s);
|
||||
}
|
||||
if let Some(d) = self.gs.mask_depth {
|
||||
self.close_to(d);
|
||||
}
|
||||
let mut content = Group::new("Soft Mask");
|
||||
content.children = nodes;
|
||||
let mut grp = Group::new("Soft Mask Group");
|
||||
@@ -784,8 +786,15 @@ impl<'a> Interp<'a> {
|
||||
if depth > 8 {
|
||||
return;
|
||||
}
|
||||
// The `Pattern` and `Tile` wrappers must fit under the open groups too.
|
||||
if !self.b.has_room(2) {
|
||||
self.b.skip("group nesting limit");
|
||||
return;
|
||||
}
|
||||
// The cell, drawn once in pattern space.
|
||||
let saved_b = std::mem::replace(&mut self.b, Builder::new());
|
||||
// The cell ends up under the open groups and this pattern's `Pattern` and `Tile`.
|
||||
let inner = self.b.nested(2);
|
||||
let saved_b = std::mem::replace(&mut self.b, inner);
|
||||
let saved_gs = std::mem::take(&mut self.gs);
|
||||
let saved_stack = std::mem::take(&mut self.saved);
|
||||
let saved_path = std::mem::take(&mut self.path);
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
use super::eps::find_from;
|
||||
use super::exec::MAX_STACK;
|
||||
use super::scanner::Src;
|
||||
use super::text::encoding_array;
|
||||
use super::value::{Flow, V, array, matrix_of, matrix_val, new_dict};
|
||||
@@ -181,8 +182,13 @@ impl Interp {
|
||||
match self.pop() {
|
||||
V::Num(k) => {
|
||||
let k = (k.max(0.0) as usize).min(self.st.len());
|
||||
let tail: Vec<V> = self.st[self.st.len() - k..].to_vec();
|
||||
self.st.extend(tail);
|
||||
// A copy past the operand limit copies nothing, and the values copied
|
||||
// count as operations (one for eight): repeated doubling spends the budget.
|
||||
if self.st.len().saturating_add(k) <= MAX_STACK {
|
||||
self.charge(k / 8);
|
||||
let tail: Vec<V> = self.st[self.st.len() - k..].to_vec();
|
||||
self.st.extend(tail);
|
||||
}
|
||||
}
|
||||
V::Array(dst) => {
|
||||
let s = self.pop().items().unwrap_or_default();
|
||||
@@ -507,12 +513,16 @@ impl Interp {
|
||||
fn try_collection_op(&mut self, n: &str) -> bool {
|
||||
match n {
|
||||
// Arrays and strings.
|
||||
// The storage made counts as operations (a value, or eight bytes, each), so a loop
|
||||
// of large constructors spends the budget rather than the memory.
|
||||
"array" => {
|
||||
let k = self.popn().clamp(0.0, 1e6) as usize;
|
||||
self.charge(k);
|
||||
self.push(array(vec![V::Null; k]));
|
||||
}
|
||||
"string" => {
|
||||
let k = self.popn().clamp(0.0, 1e7) as usize;
|
||||
self.charge(k / 8);
|
||||
self.push(V::Str(Rc::new(RefCell::new(vec![0; k]))));
|
||||
}
|
||||
"length" => {
|
||||
|
||||
@@ -19,7 +19,8 @@ pub fn eps_parts(bytes: &[u8]) -> Option<(&[u8], [f64; 4])> {
|
||||
if v.len() == 4 && v[2] > v[0] && v[3] > v[1] {
|
||||
return Some([v[0], v[1], v[2], v[3]]);
|
||||
}
|
||||
from = s;
|
||||
// A rejected comment is done with: the scan goes on past its line, not over it again.
|
||||
from = e;
|
||||
}
|
||||
None
|
||||
};
|
||||
@@ -31,8 +32,15 @@ pub(super) fn find_from(hay: &[u8], needle: &[u8], from: usize) -> Option<usize>
|
||||
hay.get(from..)?.windows(needle.len()).position(|w| w == needle).map(|p| p + from)
|
||||
}
|
||||
|
||||
fn rfind_before(hay: &[u8], needle: &[u8], to: usize) -> Option<usize> {
|
||||
hay.get(..to)?.windows(needle.len()).rposition(|w| w == needle)
|
||||
/// Every position of `needle` in `hay`, in order (one pass over the file).
|
||||
fn positions(hay: &[u8], needle: &[u8]) -> Vec<usize> {
|
||||
let mut out = vec![];
|
||||
let mut from = 0;
|
||||
while let Some(p) = find_from(hay, needle, from) {
|
||||
out.push(p);
|
||||
from = p + needle.len();
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
/// The name after `key` (`/FontName /Name`).
|
||||
@@ -48,20 +56,26 @@ fn name_after(b: &[u8], key: &[u8]) -> Option<String> {
|
||||
/// … cleartomark`, hexadecimal or binary) and CFF font sets (`… <length> StartData <bytes>`).
|
||||
pub(super) fn embedded_fonts(ps: &[u8]) -> HashMap<String, (Rc<Vec<u8>>, bool)> {
|
||||
let mut out = HashMap::new();
|
||||
// The headers are found once, in one pass each; every `eexec` then takes the last header
|
||||
// before it that no earlier `eexec` has used, so no prefix of the file is searched again.
|
||||
let mut heads = positions(ps, b"%!PS-AdobeFont");
|
||||
heads.extend(positions(ps, b"%!FontType1"));
|
||||
heads.sort_unstable();
|
||||
let names = positions(ps, b"/FontName");
|
||||
let mut from = 0;
|
||||
let mut used = 0;
|
||||
while let Some(ee) = find_from(ps, b"eexec", from) {
|
||||
from = ee + 5;
|
||||
let start = [b"%!PS-AdobeFont".as_slice(), b"%!FontType1"]
|
||||
.iter()
|
||||
.filter_map(|h| rfind_before(ps, h, ee))
|
||||
.max()
|
||||
.or_else(|| rfind_before(ps, b"/FontName", ee).map(|p| p.saturating_sub(512)));
|
||||
let last_before = |v: &[usize]| v[..v.partition_point(|&p| p < ee)].last().copied().filter(|&p| p >= used);
|
||||
// The fallback look-back stops at the previous font's end, so it cannot pick up its header.
|
||||
let start = last_before(&heads).or_else(|| last_before(&names).map(|p| p.saturating_sub(512).max(used)));
|
||||
let Some(start) = start else { continue };
|
||||
let end = find_from(ps, b"cleartomark", ee).map_or(ps.len(), |p| p + 11);
|
||||
if let Some(name) = name_after(&ps[start..ee], b"/FontName") {
|
||||
out.insert(name, (Rc::new(ps[start..end].to_vec()), false));
|
||||
}
|
||||
from = end;
|
||||
used = end;
|
||||
}
|
||||
let mut from = 0;
|
||||
while let Some(sd) = find_from(ps, b"StartData", from) {
|
||||
@@ -77,3 +91,30 @@ pub(super) fn embedded_fonts(ps: &[u8]) -> HashMap<String, (Rc<Vec<u8>>, bool)>
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::embedded_fonts;
|
||||
|
||||
#[test]
|
||||
fn font_discovery_looks_back_once_per_header() {
|
||||
// 20,000 `eexec` words before any font header: each used to search the whole prefix
|
||||
// for a header again. The font after them is still found.
|
||||
let mut ps = b"%!PS-Adobe-3.0 EPSF-3.0\n".to_vec();
|
||||
ps.extend_from_slice("eexec ".repeat(20_000).as_bytes());
|
||||
ps.extend_from_slice(b"\n%!PS-AdobeFont-1.0: Real\n/FontName /Real def\ncurrentfile eexec\n0000\ncleartomark\n");
|
||||
let fonts = embedded_fonts(&ps);
|
||||
assert_eq!(fonts.len(), 1);
|
||||
assert!(fonts.contains_key("Real"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn header_less_font_after_a_short_one_does_not_take_its_header() {
|
||||
let a = "%!PS-AdobeFont-1.0: A\n/FontName /A def\ncurrentfile eexec\n0000\ncleartomark\n";
|
||||
let b = "/FontName /B def\ncurrentfile eexec\n0000\ncleartomark\n";
|
||||
let ps = format!("%!PS-Adobe-3.0 EPSF-3.0\n{a}{b}");
|
||||
let fonts = embedded_fonts(ps.as_bytes());
|
||||
let (data, _) = fonts.get("B").expect("second font found");
|
||||
assert!(!String::from_utf8_lossy(data).contains("AdobeFont"), "{}", String::from_utf8_lossy(data));
|
||||
}
|
||||
}
|
||||
@@ -6,6 +6,10 @@ use super::{Affine, BezPath, Builder, Cap, HashMap, Interp, Join};
|
||||
/// The deepest procedure nesting run (deeper recursion stops the program).
|
||||
const MAX_CALL_DEPTH: usize = 256;
|
||||
|
||||
/// The most values the operand stack holds: a push past it is dropped, and `copy` copies
|
||||
/// nothing rather than grow the stack past it.
|
||||
pub(super) const MAX_STACK: usize = 100_000;
|
||||
|
||||
impl Interp {
|
||||
pub fn new(page: [f64; 4]) -> Interp {
|
||||
Interp {
|
||||
@@ -56,11 +60,17 @@ impl Interp {
|
||||
}
|
||||
|
||||
pub(super) fn push(&mut self, v: V) {
|
||||
if self.st.len() < 100_000 {
|
||||
if self.st.len() < MAX_STACK {
|
||||
self.st.push(v);
|
||||
}
|
||||
}
|
||||
|
||||
/// Count `n` units of work (values copied, storage allocated) against the budget without
|
||||
/// stopping the program here: once it is spent, the next operator stops it.
|
||||
pub(super) fn charge(&mut self, n: usize) {
|
||||
self.budget = self.budget.saturating_sub(n);
|
||||
}
|
||||
|
||||
pub(super) fn lookup(&self, k: &str) -> Option<V> {
|
||||
self.dicts.iter().rev().find_map(|d| d.borrow().get(k).cloned())
|
||||
}
|
||||
|
||||
@@ -22,7 +22,7 @@ impl Interp {
|
||||
}
|
||||
|
||||
/// Stop the program when a path of `len` elements would be longer than [`MAX_PATH`].
|
||||
fn path_fits(&mut self, len: usize) -> Result<(), Flow> {
|
||||
pub(super) fn path_fits(&mut self, len: usize) -> Result<(), Flow> {
|
||||
if len <= MAX_PATH {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
@@ -95,7 +95,14 @@ impl Interp {
|
||||
let ctm = self.gs.ctm;
|
||||
for (i, &code) in bytes.iter().enumerate() {
|
||||
let code = code as u32;
|
||||
// Every input character costs at least one unit, so empty outlines are not free.
|
||||
self.spend(1)?;
|
||||
if let Some(g) = font.glyph(code) {
|
||||
// Each outline element counts against the path limit and the budget before it
|
||||
// is copied, so a long string cannot expand into unbounded geometry.
|
||||
let n = g.elements().len();
|
||||
self.path_fits(path.elements().len().saturating_add(n))?;
|
||||
self.spend(n)?;
|
||||
path.extend(ctm * Affine::translate(p.to_vec2()) * m * (*g).clone());
|
||||
}
|
||||
text.push_str(&font.unicode(code));
|
||||
|
||||
@@ -18,6 +18,8 @@ const MAX_SIDE: f64 = 1024.0;
|
||||
const MAX_SCALE: f64 = 4.0;
|
||||
/// The most triangles one shading draws.
|
||||
const MAX_TRIANGLES: usize = 2_000_000;
|
||||
/// How many times over the mesh raster may be covered by triangle bounding boxes in all.
|
||||
const MAX_RASTER_PASSES: usize = 64;
|
||||
|
||||
/// A mesh vertex: page-space position and its value (RGB, or `t` in `[0]` with a function).
|
||||
#[derive(Clone, Copy, Debug, PartialEq)]
|
||||
@@ -234,13 +236,17 @@ fn free_form(r: &mut Bits, l: &Layout, colors: &Colors, m: Affine) -> Vec<[V; 3]
|
||||
|
||||
/// Type 5: a lattice of `per_row` vertices per row, two triangles per cell.
|
||||
fn lattice(r: &mut Bits, l: &Layout, colors: &Colors, m: Affine, per_row: usize) -> Vec<[V; 3]> {
|
||||
if per_row < 2 {
|
||||
if !(2..=MAX_TRIANGLES).contains(&per_row) {
|
||||
return vec![];
|
||||
}
|
||||
let mut rows: Vec<Vec<V>> = vec![];
|
||||
let mut row = vec![];
|
||||
while let (Some(p), Some(c)) = (l.point(r), l.comps(r)) {
|
||||
r.align();
|
||||
// Vertices kept, the unfinished row's included, stay within the triangle limit.
|
||||
if (rows.len() * per_row).saturating_add(row.len()) >= MAX_TRIANGLES {
|
||||
break;
|
||||
}
|
||||
row.push(V { p: m * p, c: colors.value(&c) });
|
||||
if row.len() == per_row {
|
||||
rows.push(std::mem::take(&mut row));
|
||||
@@ -447,6 +453,9 @@ fn rasterize(tris: &[[V; 3]], colors: &Colors, page_box: [f64; 4]) -> Option<Svg
|
||||
let ss = s * 2.0;
|
||||
let mut val = vec![[0.0f32; 3]; sw * sh];
|
||||
let mut cov = vec![false; sw * sh];
|
||||
// Samples tested across all triangles: overlapping triangles that each cover the raster
|
||||
// would otherwise test it once per triangle. Past the budget the later triangles are left out.
|
||||
let mut work = (sw * sh).saturating_mul(MAX_RASTER_PASSES).saturating_add(tris.len().saturating_mul(16));
|
||||
for t in tris {
|
||||
let q = t.map(|v| Point::new((v.p.x - o.x) * ss, (o.y - v.p.y) * ss));
|
||||
let area = (q[1].x - q[0].x) * (q[2].y - q[0].y) - (q[2].x - q[0].x) * (q[1].y - q[0].y);
|
||||
@@ -457,6 +466,8 @@ fn rasterize(tris: &[[V; 3]], colors: &Colors, page_box: [f64; 4]) -> Option<Svg
|
||||
let by0 = q.iter().map(|p| p.y).fold(f64::MAX, f64::min).floor().max(0.0) as usize;
|
||||
let bx1 = (q.iter().map(|p| p.x).fold(f64::MIN, f64::max).ceil().max(0.0) as usize).min(sw);
|
||||
let by1 = (q.iter().map(|p| p.y).fold(f64::MIN, f64::max).ceil().max(0.0) as usize).min(sh);
|
||||
let Some(left) = work.checked_sub(bx1.saturating_sub(bx0).saturating_mul(by1.saturating_sub(by0))) else { break };
|
||||
work = left;
|
||||
let eps = 1e-9 * area.abs();
|
||||
for y in by0..by1 {
|
||||
for x in bx0..bx1 {
|
||||
|
||||
@@ -748,6 +748,63 @@ fn tiling_patterns_coloured_and_uncoloured() {
|
||||
assert_eq!(at(115, 15)[3], 0.0);
|
||||
}
|
||||
|
||||
/// A pattern cell nests under the page's open clipping groups and the pattern's own groups, and
|
||||
/// the render tree stays within the group depth limit that counts all of them.
|
||||
#[test]
|
||||
fn a_pattern_cell_counts_the_groups_it_is_nested_in() {
|
||||
fn depth(g: &effectcraft_svg::Group) -> usize {
|
||||
1 + g.children.iter().filter_map(|c| if let effectcraft_svg::Node::Group(g) = c { Some(depth(g)) } else { None }).max().unwrap_or(0)
|
||||
}
|
||||
let clips = "0 0 200 100 re W n ".repeat(250);
|
||||
let cell = format!("{} 1 0 0 rg 0 0 10 10 re f", "0 0 20 20 re W n ".repeat(250)).into_bytes();
|
||||
let bytes = page_pdf(
|
||||
&format!("{clips} /Pattern cs /P1 scn 0 0 100 100 re f"),
|
||||
"/Pattern << /P1 10 0 R >>",
|
||||
vec![(10, "<< /Type /Pattern /PatternType 1 /PaintType 1 /TilingType 1 /BBox [0 0 20 20] /XStep 20 /YStep 20 /Resources << >> >>".into(), Some(cell))],
|
||||
);
|
||||
let doc = parse(&bytes).unwrap();
|
||||
assert!(depth(&doc.root) <= 260, "{}", depth(&doc.root));
|
||||
assert!(doc.skipped.iter().any(|s| s == "group nesting limit"), "{:?}", doc.skipped);
|
||||
}
|
||||
|
||||
/// A pattern whose `Pattern` and `Tile` wrappers would pass the depth limit is skipped, not built
|
||||
/// past it.
|
||||
#[test]
|
||||
fn a_pattern_whose_wrappers_pass_the_depth_limit_is_skipped() {
|
||||
let clips = "0 0 200 100 re W n ".repeat(255);
|
||||
let bytes = page_pdf(
|
||||
&format!("{clips} /Pattern cs /P1 scn 0 0 100 100 re f"),
|
||||
"/Pattern << /P1 10 0 R >>",
|
||||
vec![(
|
||||
10,
|
||||
"<< /Type /Pattern /PatternType 1 /PaintType 1 /TilingType 1 /BBox [0 0 20 20] /XStep 20 /YStep 20 /Resources << >> >>".into(),
|
||||
Some(b"0 0 10 10 re f".to_vec()),
|
||||
)],
|
||||
);
|
||||
let doc = parse(&bytes).unwrap();
|
||||
assert!(doc.skipped.iter().any(|s| s == "group nesting limit"), "{:?}", doc.skipped);
|
||||
}
|
||||
|
||||
/// A soft mask replacing another is nested under what is open once the old one has closed, so
|
||||
/// groups inside the old mask don't count against the new mask's content.
|
||||
#[test]
|
||||
fn a_replaced_soft_mask_does_not_count_against_the_new_one() {
|
||||
let form = |n: &str| {
|
||||
(
|
||||
n.parse().unwrap(),
|
||||
"<< /Type /XObject /Subtype /Form /BBox [0 0 200 100] /Group << /S /Transparency /CS /DeviceGray >> >>".to_string(),
|
||||
Some(format!("{} 1 g 0 0 100 100 re f", "0 0 200 100 re W n ".repeat(250)).into_bytes()),
|
||||
)
|
||||
};
|
||||
let bytes = page_pdf(
|
||||
&format!("/GS1 gs {} /GS2 gs 1 0 0 rg 0 0 200 100 re f", "0 0 200 100 re W n ".repeat(250)),
|
||||
"/ExtGState << /GS1 << /SMask << /S /Luminosity /G 10 0 R >> >> /GS2 << /SMask << /S /Luminosity /G 11 0 R >> >> >>",
|
||||
vec![form("10"), form("11")],
|
||||
);
|
||||
let doc = parse(&bytes).unwrap();
|
||||
assert!(doc.skipped.iter().all(|s| s != "group nesting limit"), "{:?}", doc.skipped);
|
||||
}
|
||||
|
||||
/// Build isolated interpreter objects without presenting a function or colour space as a
|
||||
/// document catalog. Complete-document parsing is covered by the page and reader fixtures.
|
||||
fn object_file(objs: Objs) -> object::File {
|
||||
@@ -1068,6 +1125,35 @@ fn a_fanning_out_reference_graph_is_refused_within_the_budget() {
|
||||
assert_eq!(read_function(objs), color::Func::Unsupported);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_sampled_function_shorter_than_its_table_is_refused() {
|
||||
// 4,096 8-bit samples declared, ten bytes given: the missing ones are not made up.
|
||||
let f = "<< /FunctionType 0 /Domain [0 1] /Size [4096] /Range [0 1] /BitsPerSample 8 >>";
|
||||
assert_eq!(read_function(vec![(1, f.into(), Some(vec![7; 10]))]), color::Func::Unsupported);
|
||||
assert_ne!(read_function(vec![(1, f.into(), Some(vec![7; 4096]))]), color::Func::Unsupported);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_calculator_program_past_the_operator_limit_is_refused() {
|
||||
let f = "<< /FunctionType 4 /Domain [0 1] /Range [0 1] >>";
|
||||
let long = format!("{{ {} }}", "1 pop ".repeat(3000));
|
||||
assert_eq!(read_function(vec![(1, f.into(), Some(long.into_bytes()))]), color::Func::Unsupported);
|
||||
assert_ne!(read_function(vec![(1, f.into(), Some(b"{ 1 exch sub }".to_vec()))]), color::Func::Unsupported);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn copies_of_a_function_are_charged_for_its_coefficients() {
|
||||
// A stitching function listing one object 1,000 times, whose coefficient arrays hold 2,000
|
||||
// values: each copy is charged its values, so the budget refuses it instead of copying
|
||||
// two million values.
|
||||
let c = format!("[{}]", "0.5 ".repeat(1000));
|
||||
let objs: Objs = vec![
|
||||
(1, format!("<< /FunctionType 3 /Domain [0 1] /Functions [{}] >>", "2 0 R ".repeat(1000)), None),
|
||||
(2, format!("<< /FunctionType 2 /Domain [0 1] /C0 {c} /C1 {c} /N 1 >>"), None),
|
||||
];
|
||||
assert_eq!(read_function(objs), color::Func::Unsupported);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn colour_space_chain_of_depth_3() {
|
||||
// Indexed → Separation → ICCBased → DeviceRGB: entry 0 is tint 1 (blue), entry 1 tint 0 (white).
|
||||
|
||||
@@ -513,6 +513,9 @@ fn paths_stop_at_the_path_limit() {
|
||||
// The outlines of 100,000 H's, 13 elements each.
|
||||
"/Helvetica findfont 10 scalefont setfont /s 100000 string def 0 1 99999 { s exch 72 put } for \
|
||||
0 0 moveto s false charpath",
|
||||
// The same outlines shown: the text path is bounded like the current path.
|
||||
"/Helvetica findfont 10 scalefont setfont /s 100000 string def 0 1 99999 { s exch 72 put } for \
|
||||
0 0 moveto s show",
|
||||
// 210,000 rectangles of five elements each.
|
||||
"/a 840000 array def 0 1 839999 { a exch 0 put } for a rectfill",
|
||||
] {
|
||||
@@ -528,6 +531,45 @@ fn paths_stop_at_the_path_limit() {
|
||||
assert_eq!(doc.flatten().len(), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn copies_past_the_operand_limit_copy_nothing() {
|
||||
// `count copy` doubles the operand stack: 40 doublings would be 2^40 values. A copy that would
|
||||
// pass the operand limit (100,000) copies nothing, so the program runs on to its end.
|
||||
let doc = eps_soon("0 0 10 10 rectfill 1 40 { count copy } repeat clear 20 20 10 10 rectfill");
|
||||
assert!(doc.skipped.is_empty(), "{:?}", doc.skipped);
|
||||
assert_eq!(doc.flatten().len(), 2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn storage_made_in_a_loop_runs_out_of_the_budget() {
|
||||
// Each value of an array and each eight bytes of a string count as an operation when they
|
||||
// are made, so a loop keeping large arrays or strings spends the budget, not the memory.
|
||||
for body in ["/a 1000 array def 0 1 999 { a exch 1000000 array put } for", "/a 1000 array def 0 1 999 { a exch 10000000 string put } for"] {
|
||||
runs_out_of_budget(body);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn groups_stop_nesting_at_the_depth_limit() {
|
||||
// Every `rectclip` opens a clipping group inside the last: past 256 open groups the group is
|
||||
// dropped (and noted), and what is drawn inside goes to the open one.
|
||||
let doc = eps_soon("300 { 0 0 100 100 rectclip } repeat 0 0 10 10 rectfill");
|
||||
assert_eq!(doc.skipped, ["group nesting limit"]);
|
||||
assert_eq!(doc.flatten().len(), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bounding_box_comments_are_scanned_once() {
|
||||
// A rejected `%%BoundingBox:` comment is not scanned again from each later marker on its
|
||||
// line: 20,000 markers on one line would otherwise take quadratic work.
|
||||
let mut src = b"%!PS-Adobe-3.0 EPSF-3.0\n".to_vec();
|
||||
src.extend_from_slice("%%BoundingBox: x ".repeat(20_000).as_bytes());
|
||||
src.extend_from_slice(b"\n%%BoundingBox: 0 0 50 50\n0 0 10 10 rectfill\n");
|
||||
let doc = parse(&src).unwrap();
|
||||
assert_eq!((doc.width, doc.height), (50.0, 50.0));
|
||||
assert_eq!(doc.flatten().len(), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn pdf_paths_stop_at_the_path_limit() {
|
||||
// A content stream's current path holds at most 1,048,576 elements: `re` (five elements each)
|
||||
|
||||
@@ -262,3 +262,32 @@ fn corrupt_mesh_data_draws_what_parses() {
|
||||
let doc = parse(&bytes).unwrap();
|
||||
assert!(doc.skipped.iter().any(|s| s.contains("layout")), "{:?}", doc.skipped);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn overlapping_mesh_triangles_stop_at_the_raster_budget() {
|
||||
// 20,000 triangles each covering the whole page would test every sample 20,000 times; the
|
||||
// raster budget stops after about 64 passes, and the shading is still drawn.
|
||||
let mut m = Mesh::default();
|
||||
for _ in 0..20_000 {
|
||||
m.flag(0).pt(0, 0).c(&[255, 0, 0]);
|
||||
m.flag(0).pt(400, 0).c(&[255, 0, 0]);
|
||||
m.flag(0).pt(0, 400).c(&[255, 0, 0]);
|
||||
}
|
||||
let sh = format!("<< /ShadingType 4 /ColorSpace /DeviceRGB /BitsPerCoordinate 16 /BitsPerComponent 8 /BitsPerFlag 8 {RGB_DECODE} >>");
|
||||
let bytes = page_pdf("/Sh sh", "/Shading << /Sh 10 0 R >>", vec![(10, sh, Some(m.0))]);
|
||||
let (tx, rx) = std::sync::mpsc::channel();
|
||||
std::thread::spawn(move || tx.send(parse(&bytes)).ok());
|
||||
let doc = rx.recv_timeout(std::time::Duration::from_secs(20)).expect("the mesh finishes").unwrap();
|
||||
assert!(render(&doc).get(50, 50)[0] > 0.99);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_lattice_row_longer_than_the_triangle_limit_draws_nothing() {
|
||||
let mut m = Mesh::default();
|
||||
for i in 0..8 {
|
||||
m.pt(i * 10, 0).c(&[255, 0, 0]);
|
||||
}
|
||||
let sh = format!("<< /ShadingType 5 /ColorSpace /DeviceRGB /BitsPerCoordinate 16 /BitsPerComponent 8 /VerticesPerRow 3000000 {RGB_DECODE} >>");
|
||||
let doc = parse(&page_pdf("/Sh sh", "/Shading << /Sh 10 0 R >>", vec![(10, sh, Some(m.0))])).unwrap();
|
||||
assert!(doc.flatten().is_empty());
|
||||
}
|
||||
@@ -13,6 +13,8 @@ const FUEL_BASE: u64 = 50_000_000;
|
||||
const FUEL_PER_PIXEL: u64 = 20_000;
|
||||
/// The most linear memory a plug-in instance may have (a 4K float frame is ~130 MB).
|
||||
const MAX_MEMORY: usize = 1 << 30;
|
||||
/// The most elements one table of a plug-in may hold.
|
||||
const MAX_TABLE_ELEMENTS: usize = 1 << 16;
|
||||
/// The longest manifest read.
|
||||
const MAX_MANIFEST: i32 = 1 << 20;
|
||||
|
||||
@@ -84,7 +86,10 @@ fn err(e: impl std::fmt::Display) -> String {
|
||||
|
||||
/// Instantiate `module` in a fresh, limited store and read its memory and allocator.
|
||||
fn begin(engine: &Engine, module: &Module) -> Result<Pending, String> {
|
||||
let mut store = Store::new(engine, StoreLimitsBuilder::new().memory_size(MAX_MEMORY).instances(1).build());
|
||||
// One memory and a few small tables: a module declaring many of them would otherwise make
|
||||
// the host allocate and initialise each before an export or fuel check could refuse it.
|
||||
let limits = StoreLimitsBuilder::new().memory_size(MAX_MEMORY).memories(1).tables(4).table_elements(MAX_TABLE_ELEMENTS).instances(1).build();
|
||||
let mut store = Store::new(engine, limits);
|
||||
store.limiter(|l| l);
|
||||
store.set_fuel(FUEL_BASE).map_err(err)?;
|
||||
let linker = Linker::<StoreLimits>::new(engine);
|
||||
|
||||
@@ -268,8 +268,19 @@ impl Comp {
|
||||
/// `base`, or the next free numbered name when a layer already has it (`Shape Layer 2`,
|
||||
/// `v-b1.mp4 2`; see `numbered_name`).
|
||||
pub fn unique_layer_name(&self, base: &str) -> String {
|
||||
let taken = |n: &str| self.layers.iter().any(|l| l.name == n);
|
||||
if taken(base) { crate::item::numbered_name(base, taken) } else { base.to_string() }
|
||||
if !self.layers.iter().any(|l| l.name == base) {
|
||||
return base.to_string();
|
||||
}
|
||||
let stem = crate::item::name_stem(base);
|
||||
// The suffixes in use, gathered in one pass: trying each candidate against every layer
|
||||
// made importing many layers of one name cubic.
|
||||
let taken: std::collections::HashSet<u64> = self
|
||||
.layers
|
||||
.iter()
|
||||
.filter_map(|l| l.name.strip_prefix(stem)?.strip_prefix(' '))
|
||||
.filter_map(|s| s.parse::<u64>().ok().filter(|n| n.to_string() == s))
|
||||
.collect();
|
||||
(2..).find(|i| !taken.contains(i)).map(|i| format!("{stem} {i}")).unwrap_or_default()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -8,12 +8,18 @@ use super::{Arc, BTreeMap, Deserialize, FrameRate, Label, Project, Serialize, Ti
|
||||
/// another layer of the same name. A number set off by a space is a counter and is replaced
|
||||
/// (`Red Solid 1` → `Red Solid 2`, `shot 2` → `shot 3`); digits that end a word belong to the
|
||||
/// name and stay (`v-b1.mp4` → `v-b1.mp4 2`, `clip2.mov` → `clip2.mov 2`).
|
||||
pub(crate) fn numbered_name(base: &str, taken: impl Fn(&str) -> bool) -> String {
|
||||
/// `base` without a trailing ` N`: digits are dropped only when a space separates them from a
|
||||
/// nonempty stem, so `v-b1.mp4` and `Layer1` keep theirs.
|
||||
pub(crate) fn name_stem(base: &str) -> &str {
|
||||
let head = base.trim_end_matches(|c: char| c.is_ascii_digit());
|
||||
let stem = match head.strip_suffix(' ') {
|
||||
match head.strip_suffix(' ') {
|
||||
Some(stem) if head.len() < base.len() && !stem.trim_end().is_empty() => stem.trim_end(),
|
||||
_ => base.trim_end(),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn numbered_name(base: &str, taken: impl Fn(&str) -> bool) -> String {
|
||||
let stem = name_stem(base);
|
||||
(2u64..).map(|k| format!("{stem} {k}")).find(|n| !taken(n)).unwrap_or_default()
|
||||
}
|
||||
|
||||
|
||||
@@ -421,7 +421,12 @@ impl RenderSettings {
|
||||
/// The output frame rate: the Frame Rate override, unless the comp preserves its frame rate
|
||||
/// (Composition Settings ▸ Preserve frame rate when nested or in render queue).
|
||||
pub fn rate(&self, comp: &Comp) -> FrameRate {
|
||||
if comp.preserve_frame_rate { comp.frame_rate } else { self.frame_rate.unwrap_or(comp.frame_rate) }
|
||||
if comp.preserve_frame_rate {
|
||||
return comp.frame_rate;
|
||||
}
|
||||
// A saved override is held to the supported rates, as a comp's own rate is: its frame count
|
||||
// is the export's work.
|
||||
self.frame_rate.map_or(comp.frame_rate, |r| crate::limits::frame_rate(r, comp.frame_rate))
|
||||
}
|
||||
/// First output-rate frame starting at or after `t`.
|
||||
fn ceil_frame(r: FrameRate, t: Tick) -> i64 {
|
||||
@@ -480,6 +485,8 @@ impl RenderSettings {
|
||||
TimeSpan::Custom { start, end } => format!("Custom ({:.3}s – {:.3}s)", start.seconds(), end.seconds()),
|
||||
};
|
||||
let rate = match (self.frame_rate, comp) {
|
||||
// A saved override outside the supported rates renders at the clamped one: say so.
|
||||
(Some(r), Some(c)) if self.rate(c) != r => format!("{:.3} fps (renders at {:.3})", r.as_f64(), self.rate(c).as_f64()),
|
||||
(Some(r), _) => format!("{:.3} fps", r.as_f64()),
|
||||
(None, Some(c)) => format!("Use comp's frame rate ({:.3})", c.frame_rate.as_f64()),
|
||||
(None, None) => "Use comp's frame rate".into(),
|
||||
@@ -1713,7 +1720,22 @@ pub fn numbered_template(template: &str, n: u32) -> String {
|
||||
|
||||
/// Make a name safe as a file name component.
|
||||
pub fn sanitize(name: &str) -> String {
|
||||
name.chars().map(|c| if matches!(c, '/' | '\\' | ':' | '*' | '?' | '"' | '<' | '>' | '|') { '_' } else { c }).collect()
|
||||
let mut s: String = name.chars().map(|c| if matches!(c, '/' | '\\' | ':' | '*' | '?' | '"' | '<' | '>' | '|') { '_' } else { c }).collect();
|
||||
// At most a file name's worth (255 UTF-8 bytes, whole characters only): a template repeating
|
||||
// a long name token must not expand to gigabytes. A cut name ends in a hash of the whole
|
||||
// name so long names that share a prefix stay distinct.
|
||||
if s.len() > 255 {
|
||||
// shortcut: 64-bit FNV-1a, not collision-resistant against crafted names; use a real hash if names become untrusted keys.
|
||||
let hash = name.bytes().fold(0xcbf2_9ce4_8422_2325u64, |h, b| (h ^ u64::from(b)).wrapping_mul(0x0100_0000_01b3));
|
||||
let mut end = 255 - 17;
|
||||
while !s.is_char_boundary(end) {
|
||||
end -= 1;
|
||||
}
|
||||
s.truncate(end);
|
||||
s.push_str(&format!("-{hash:016x}"));
|
||||
}
|
||||
// `.` and `..` would name the folder itself or its parent when the token is a path component.
|
||||
if !s.is_empty() && s.chars().all(|c| c == '.') { "_".repeat(s.len()) } else { s }
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
@@ -1737,10 +1759,32 @@ mod tests {
|
||||
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn sanitize_limits_the_name_in_bytes() {
|
||||
// 255 two-byte characters are cut to whole characters plus a 17-byte hash suffix.
|
||||
let s = sanitize(&"é".repeat(255));
|
||||
assert!(s.len() <= 255 && s.len() > 235, "{}", s.len());
|
||||
assert!(s.starts_with("éé") && s.is_char_boundary(s.len() - 17));
|
||||
// Names that share their first 255 bytes stay distinct, and a name that fits is untouched.
|
||||
let (a, b) = (sanitize(&format!("{}A", "界".repeat(85))), sanitize(&format!("{}B", "界".repeat(85))));
|
||||
assert_ne!(a, b);
|
||||
assert!(a.len() <= 255 && b.len() <= 255);
|
||||
assert_eq!(sanitize(&"n".repeat(255)), "n".repeat(255));
|
||||
assert_eq!(sanitize(&"é".repeat(127)), "é".repeat(127));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn templates_and_sequences() {
|
||||
let v = TemplateVars { comp_name: "Main/Title", project_name: "P", width: 1920, height: 1080, frame_rate: 29.97, start_frame: 0, end_frame: 9 };
|
||||
assert_eq!(expand_template(DEFAULT_TEMPLATE, OutputFormat::H264, &v), "Main_Title.mp4");
|
||||
// A dot-only name is not a parent-folder component; a long one is cut to a file name.
|
||||
for name in ["..", "."] {
|
||||
let v = TemplateVars { comp_name: name, ..v };
|
||||
assert_eq!(expand_template("out/[compName]/x.[fileExtension]", OutputFormat::Gif, &v), format!("out/{}/x.gif", "_".repeat(name.len())));
|
||||
}
|
||||
let long = "n".repeat(100_000);
|
||||
let v2 = TemplateVars { comp_name: &long, ..v };
|
||||
assert_eq!(expand_template(&"[compName]".repeat(100), OutputFormat::Gif, &v2).len(), 255 * 100);
|
||||
assert_eq!(expand_template("[compName]_[width]x[height]@[frameRate].[fileExtension]", OutputFormat::Gif, &v), "Main_Title_1920x1080@29.97.gif");
|
||||
assert_eq!(sequence_path("out/a_[#####].png", 42), "out/a_00042.png");
|
||||
assert_eq!(sequence_path("a###.tif", 7), "a007.tif");
|
||||
@@ -1890,6 +1934,22 @@ mod tests {
|
||||
assert_eq!(p.sample(&c, 3), FrameSample::Progressive(Tick::from_seconds_f64(0.12)));
|
||||
}
|
||||
|
||||
/// A saved Frame Rate override outside the supported rates is clamped, so a short comp cannot
|
||||
/// ask for billions of output frames.
|
||||
#[test]
|
||||
fn frame_rate_override_is_held_to_the_supported_rates() {
|
||||
let c = Comp::new(64, 48, FrameRate::new(25, 1), Tick::from_seconds_f64(2.0));
|
||||
let s = |r| RenderSettings { frame_rate: Some(r), time_span: TimeSpan::LengthOfComp, ..Default::default() };
|
||||
let huge = s(FrameRate::new(1_000_000_000_000, 1));
|
||||
assert_eq!(huge.rate(&c).as_f64(), crate::limits::MAX_FPS);
|
||||
assert!(huge.frame_count(&c) <= 2001, "{}", huge.frame_count(&c));
|
||||
assert_eq!(s(FrameRate::new(0, 1)).rate(&c), c.frame_rate);
|
||||
assert_eq!(s(FrameRate::new(60, 1)).rate(&c), FrameRate::new(60, 1));
|
||||
let shown = |r| s(r).describe(Some(&c)).into_iter().find(|l| l.0 == "Frame Rate").map(|l| l.1).unwrap_or_default();
|
||||
assert!(shown(FrameRate::new(1_000_000_000_000, 1)).contains("renders at 1000.000"), "{}", shown(FrameRate::new(1_000_000_000_000, 1)));
|
||||
assert_eq!(shown(FrameRate::new(60, 1)), "60.000 fps");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn crop_and_resize_sizes() {
|
||||
let c = Comp::new(1920, 1080, FrameRate::new(25, 1), Tick::from_seconds_f64(1.0));
|
||||
|
||||
@@ -146,6 +146,18 @@ fn unique_names_and_cycles() {
|
||||
let c = p.comp(cid).unwrap();
|
||||
assert_eq!(c.unique_layer_name("Red Solid 1"), "Red Solid 2");
|
||||
assert_eq!(c.unique_layer_name("Shape Layer 1"), "Shape Layer 1");
|
||||
// A suffix written another way (`02`) is not the number taken.
|
||||
let mut d = c.clone();
|
||||
for n in ["Red Solid 02", "Red Solid 3"] {
|
||||
let mut l = d.layers[0].clone();
|
||||
l.name = n.into();
|
||||
d.layers.push(l);
|
||||
}
|
||||
assert_eq!(d.unique_layer_name("Red Solid 1"), "Red Solid 2");
|
||||
// Digits that end a word stay in the stem.
|
||||
let mut e = c.clone();
|
||||
e.layers[0].name = "v-b1.mp4".into();
|
||||
assert_eq!(e.unique_layer_name("v-b1.mp4"), "v-b1.mp4 2");
|
||||
assert!(p.comp_contains(cid, cid));
|
||||
}
|
||||
|
||||
|
||||
@@ -107,7 +107,7 @@ impl Budget {
|
||||
|
||||
/// Inflate a zlib stream to at most `limit` bytes.
|
||||
pub(crate) fn inflate(src: &[u8], limit: usize) -> Result<Vec<u8>> {
|
||||
miniz_oxide::inflate::decompress_to_vec_zlib_with_limit(src, limit).map_err(|e| Error::Invalid(format!("zip: {e:?}")))
|
||||
miniz_oxide::inflate::decompress_to_vec_zlib_with_limit(src, limit).map_err(|e| Error::Invalid(format!("zip: {:?}", e.status)))
|
||||
}
|
||||
|
||||
/// Give back what a channel buffer from `Psd::channel_bytes` / `Psd::decode_plane` was charged:
|
||||
|
||||
@@ -28,10 +28,11 @@ impl Psd {
|
||||
}
|
||||
2 | 3 => {
|
||||
let rest = r.bytes(r.remaining())?;
|
||||
let mut v = miniz_oxide::inflate::decompress_to_vec_zlib_with_limit(rest, rowb * h).map_err(|e| Error::Invalid(format!("zip: {e:?}")))?;
|
||||
let mut v =
|
||||
miniz_oxide::inflate::decompress_to_vec_zlib_with_limit(rest, rowb * h).map_err(|e| Error::Invalid(format!("zip: {:?}", e.status)))?;
|
||||
v.resize(rowb * h, 0);
|
||||
if comp == 3 {
|
||||
self.unpredict(&mut v, w, h);
|
||||
self.unpredict(&mut v, w, h, &mut Budget::new(u64::MAX))?;
|
||||
}
|
||||
v
|
||||
}
|
||||
@@ -128,11 +129,12 @@ impl Psd {
|
||||
}
|
||||
2 | 3 => {
|
||||
let rest = r.bytes(r.remaining())?;
|
||||
let v = miniz_oxide::inflate::decompress_to_vec_zlib_with_limit(rest, want * rowb * h).map_err(|e| Error::Invalid(format!("zip: {e:?}")))?;
|
||||
let v = miniz_oxide::inflate::decompress_to_vec_zlib_with_limit(rest, want * rowb * h)
|
||||
.map_err(|e| Error::Invalid(format!("zip: {:?}", e.status)))?;
|
||||
for (c, p) in planes.iter_mut().enumerate().take(want) {
|
||||
let mut plane = v.get(c * rowb * h..(c + 1) * rowb * h).ok_or(Error::Truncated)?.to_vec();
|
||||
if comp == 3 {
|
||||
self.unpredict(&mut plane, w, h);
|
||||
self.unpredict(&mut plane, w, h, &mut Budget::new(u64::MAX))?;
|
||||
}
|
||||
*p = Some(self.ref_samples(&plane, w, h));
|
||||
}
|
||||
|
||||
@@ -37,7 +37,7 @@ impl Psd {
|
||||
PlaneStream::Rle(counts) => Cow::Owned(Self::unpack_plane(&counts, 0, r, rowb, h, budget)?),
|
||||
PlaneStream::Zip(mut v) => {
|
||||
v.resize(size, 0);
|
||||
self.zip_plane(comp, &mut v, 0, w, h)?;
|
||||
self.zip_plane(comp, &mut v, 0, w, h, budget)?;
|
||||
Cow::Owned(v)
|
||||
}
|
||||
})
|
||||
@@ -90,7 +90,7 @@ impl Psd {
|
||||
Ok(match planes {
|
||||
PlaneStream::Raw => Cow::Borrowed(r.bytes(size)?),
|
||||
PlaneStream::Rle(counts) => Cow::Owned(Self::unpack_plane(counts, c, r, rowb, h, budget)?),
|
||||
PlaneStream::Zip(v) => Cow::Borrowed(self.zip_plane(comp, v, c, w, h)?),
|
||||
PlaneStream::Zip(v) => Cow::Borrowed(self.zip_plane(comp, v, c, w, h, budget)?),
|
||||
})
|
||||
}
|
||||
|
||||
@@ -106,11 +106,11 @@ impl Psd {
|
||||
}
|
||||
|
||||
/// Plane `c` of the inflated ZIP data of consecutive planes, un-predicted in place (`comp` 3).
|
||||
pub(crate) fn zip_plane<'v>(&self, comp: u16, v: &'v mut [u8], c: usize, w: usize, h: usize) -> Result<&'v [u8]> {
|
||||
pub(crate) fn zip_plane<'v>(&self, comp: u16, v: &'v mut [u8], c: usize, w: usize, h: usize, budget: &mut Budget) -> Result<&'v [u8]> {
|
||||
let rowb = self.row_bytes(w);
|
||||
let plane = v.get_mut(c * rowb * h..(c + 1) * rowb * h).ok_or(Error::Truncated)?;
|
||||
if comp == 3 {
|
||||
self.unpredict(plane, w, h);
|
||||
self.unpredict(plane, w, h, budget)?;
|
||||
}
|
||||
Ok(plane)
|
||||
}
|
||||
@@ -132,7 +132,8 @@ impl Psd {
|
||||
if self.depth == 1 { w.div_ceil(8) } else { w * self.bytes_per_sample() }
|
||||
}
|
||||
|
||||
pub(crate) fn unpredict(&self, v: &mut [u8], w: usize, h: usize) {
|
||||
/// Undo ZIP prediction in place; the 32-bit scratch row is charged to `budget`.
|
||||
pub(crate) fn unpredict(&self, v: &mut [u8], w: usize, h: usize, budget: &mut Budget) -> Result<()> {
|
||||
match self.depth {
|
||||
16 => {
|
||||
for row in v.chunks_exact_mut(w * 2).take(h) {
|
||||
@@ -145,8 +146,9 @@ impl Psd {
|
||||
}
|
||||
}
|
||||
32 => {
|
||||
// One scratch row for the whole plane.
|
||||
let mut planar = vec![0u8; w * 4];
|
||||
// One scratch row for the whole plane, charged: a wide, short layer passes the
|
||||
// decode's peak check and would otherwise allocate it on top.
|
||||
let mut planar = budget.filled(w.checked_mul(4).ok_or_else(|| too_large(w, h))?, 0u8)?;
|
||||
#[cfg(test)]
|
||||
counters::bump(&counters::SCRATCH);
|
||||
for row in v.chunks_exact_mut(w * 4).take(h) {
|
||||
@@ -161,6 +163,7 @@ impl Psd {
|
||||
}
|
||||
}
|
||||
}
|
||||
budget.release(planar.len() as u64);
|
||||
}
|
||||
_ => {
|
||||
for row in v.chunks_exact_mut(w).take(h) {
|
||||
@@ -170,6 +173,7 @@ impl Psd {
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Hand every sample of `raw` (`w`×`h`, row-major, in order) to `f` as a float. Samples a short
|
||||
@@ -483,15 +487,17 @@ pub(crate) fn unpack_bits(src: &[u8], row: usize, out: &mut Vec<u8>) -> Result<(
|
||||
while i < src.len() && out.len() - start < row {
|
||||
let n = src[i] as i8;
|
||||
i += 1;
|
||||
// A packet is written only up to the row's end, so the reserved plane never grows.
|
||||
let room = row - (out.len() - start);
|
||||
if n >= 0 {
|
||||
let k = n as usize + 1;
|
||||
let s = src.get(i..i + k).ok_or(Error::Truncated)?;
|
||||
out.extend_from_slice(s);
|
||||
out.extend_from_slice(&s[..k.min(room)]);
|
||||
i += k;
|
||||
} else if n != -128 {
|
||||
let k = (1 - n as isize) as usize;
|
||||
let v = *src.get(i).ok_or(Error::Truncated)?;
|
||||
out.extend(std::iter::repeat_n(v, k));
|
||||
out.extend(std::iter::repeat_n(v, k.min(room)));
|
||||
i += 1;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -493,3 +493,29 @@ fn rle_composite_matches_the_raw_one() {
|
||||
assert_eq!(rle.layer_pixels(2, true).unwrap().data, raw.layer_pixels(2, true).unwrap().data, "{depth} bit");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn packbits_packets_stop_at_the_row_end() {
|
||||
// A 128-byte repeat and a 128-byte literal into 3-byte rows: the reserved row is never
|
||||
// outgrown (capacity stays what was reserved).
|
||||
for src in [vec![0x81u8, 7], {
|
||||
let mut v = vec![0x7fu8];
|
||||
v.extend([1u8; 128]);
|
||||
v
|
||||
}] {
|
||||
let mut out = Vec::with_capacity(3);
|
||||
unpack_bits(&src, 3, &mut out).unwrap();
|
||||
assert_eq!(out.len(), 3);
|
||||
assert_eq!(out.capacity(), 3);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_failed_inflate_reports_its_status_not_its_output() {
|
||||
// A stream that inflates a megabyte of zeros, then breaks its checksum.
|
||||
let mut z = miniz_oxide::deflate::compress_to_vec_zlib(&vec![0u8; 1 << 20], 6);
|
||||
let n = z.len();
|
||||
z[n - 1] ^= 0xff;
|
||||
let Err(Error::Invalid(msg)) = crate::limits::inflate(&z, 1 << 21) else { panic!("the checksum fails") };
|
||||
assert!(msg.len() < 100, "{} bytes", msg.len());
|
||||
}
|
||||
@@ -69,7 +69,7 @@ fn ref_decode_channel(p: &Psd, offset: usize, len: usize, w: usize, h: usize) ->
|
||||
}
|
||||
2 | 3 => {
|
||||
let rest = r.bytes(r.remaining())?;
|
||||
let mut v = miniz_oxide::inflate::decompress_to_vec_zlib_with_limit(rest, rowb * h).map_err(|e| Error::Invalid(format!("zip: {e:?}")))?;
|
||||
let mut v = miniz_oxide::inflate::decompress_to_vec_zlib_with_limit(rest, rowb * h).map_err(|e| Error::Invalid(format!("zip: {:?}", e.status)))?;
|
||||
v.resize(rowb * h, 0);
|
||||
if comp == 3 {
|
||||
ref_unpredict(p.depth, &mut v, w, h);
|
||||
@@ -112,7 +112,8 @@ fn ref_composite(p: &Psd) -> Result<Pixels> {
|
||||
}
|
||||
2 | 3 => {
|
||||
let rest = r.bytes(r.remaining())?;
|
||||
let v = miniz_oxide::inflate::decompress_to_vec_zlib_with_limit(rest, want * rowb * h).map_err(|e| Error::Invalid(format!("zip: {e:?}")))?;
|
||||
let v =
|
||||
miniz_oxide::inflate::decompress_to_vec_zlib_with_limit(rest, want * rowb * h).map_err(|e| Error::Invalid(format!("zip: {:?}", e.status)))?;
|
||||
for (c, pl) in planes.iter_mut().enumerate().take(want) {
|
||||
let mut plane = v.get(c * rowb * h..(c + 1) * rowb * h).ok_or(Error::Truncated)?.to_vec();
|
||||
if comp == 3 {
|
||||
@@ -448,7 +449,7 @@ fn unpredict_matches_the_reference_and_allocates_one_scratch_row() {
|
||||
let mut a: Vec<u8> = (0..w * h * bps).map(|i| (i * 31 % 251) as u8).collect();
|
||||
let mut b = a.clone();
|
||||
counters::take(&SCRATCH);
|
||||
p.unpredict(&mut a, w, h);
|
||||
p.unpredict(&mut a, w, h, &mut Budget::new(u64::MAX)).unwrap();
|
||||
let scratch = counters::take(&SCRATCH);
|
||||
let old_allocs = ref_unpredict(depth, &mut b, w, h);
|
||||
assert_eq!(a, b, "{depth} bit");
|
||||
@@ -473,11 +474,11 @@ fn raw_planes_are_borrowed_and_zip_planes_are_unpredicted_in_place() {
|
||||
let mut v: Vec<u8> = (0..3 * 40 * 30).map(|i| (i % 255) as u8).collect();
|
||||
let range = v.as_ptr_range();
|
||||
for comp in [2, 3] {
|
||||
let got = q.zip_plane(comp, &mut v, 1, 40, 30).unwrap();
|
||||
let got = q.zip_plane(comp, &mut v, 1, 40, 30, &mut Budget::new(u64::MAX)).unwrap();
|
||||
assert_eq!(got.len(), 1200);
|
||||
assert!(range.contains(&got.as_ptr()), "compression {comp}");
|
||||
}
|
||||
assert!(q.zip_plane(2, &mut v, 3, 40, 30).is_err());
|
||||
assert!(q.zip_plane(2, &mut v, 3, 40, 30, &mut Budget::new(u64::MAX)).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
@@ -27,13 +27,25 @@ fn box_h_into(src: &[Px], out: &mut [Px], w: usize, r: usize, repeat: bool) {
|
||||
row[i as usize]
|
||||
}
|
||||
};
|
||||
// The starting window: the pixels inside the row once each, and the repeated edge pixels
|
||||
// counted rather than visited (a radius far past the row would otherwise loop over it).
|
||||
let mut acc = [0.0f32; 4];
|
||||
for i in -(r as isize)..=(r as isize) {
|
||||
let p = get(i);
|
||||
let inside = r.min(w - 1);
|
||||
for p in &row[..=inside] {
|
||||
for c in 0..4 {
|
||||
acc[c] += p[c];
|
||||
}
|
||||
}
|
||||
if repeat {
|
||||
let (left, right) = (r as f32, (r - inside) as f32);
|
||||
for c in 0..4 {
|
||||
acc[c] += row[0][c] * left;
|
||||
// A zero count adds nothing (0 * NaN would be NaN).
|
||||
if right > 0.0 {
|
||||
acc[c] += row[w - 1][c] * right;
|
||||
}
|
||||
}
|
||||
}
|
||||
for x in 0..w {
|
||||
o[x] = acc.map(|v| v * norm);
|
||||
let add = get(x as isize + r as isize + 1);
|
||||
@@ -65,14 +77,27 @@ fn box_v_into(src: &[Px], out: &mut [Px], w: usize, h: usize, r: usize, repeat:
|
||||
out.par_chunks_mut(w * block).enumerate().for_each(|(bi, chunk)| {
|
||||
let ya = (bi * block) as isize;
|
||||
let mut acc = vec![[0.0f32; 4]; w];
|
||||
for i in ya - r as isize..=ya + r as isize {
|
||||
if let Some(rw) = row(i) {
|
||||
for (a, p) in acc.iter_mut().zip(rw) {
|
||||
for c in 0..4 {
|
||||
a[c] += p[c];
|
||||
}
|
||||
// The starting window: rows inside the image once each, the repeated edge rows counted.
|
||||
let (lo, hi) = (ya - r as isize, ya + r as isize);
|
||||
let add = |acc: &mut [Px], rw: &[Px], k: f32| {
|
||||
// A zero count adds nothing (0 * NaN would be NaN).
|
||||
if k == 0.0 {
|
||||
return;
|
||||
}
|
||||
for (a, p) in acc.iter_mut().zip(rw) {
|
||||
for c in 0..4 {
|
||||
a[c] += p[c] * k;
|
||||
}
|
||||
}
|
||||
};
|
||||
for i in lo.max(0)..=hi.min(h as isize - 1) {
|
||||
add(&mut acc, &src[i as usize * w..(i as usize + 1) * w], 1.0);
|
||||
}
|
||||
if repeat {
|
||||
let above = (-lo).max(0) as f32;
|
||||
let below = (hi - (h as isize - 1)).max(0) as f32;
|
||||
add(&mut acc, &src[0..w], above);
|
||||
add(&mut acc, &src[(h - 1) * w..h * w], below);
|
||||
}
|
||||
for (yy, o) in chunk.chunks_mut(w).enumerate() {
|
||||
let y = ya + yy as isize;
|
||||
@@ -360,6 +385,59 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_non_finite_edge_pixel_outside_every_window_is_not_counted() {
|
||||
// 9 wide, radius 1: the last pixel is in no window of the first pixels; likewise the last row.
|
||||
let mut src = vec![[1.0f32; 4]; 81];
|
||||
src[80] = [f32::NAN; 4];
|
||||
let mut out = vec![[0.0f32; 4]; 81];
|
||||
box_h_into(&src, &mut out, 9, 1, true);
|
||||
assert!(out[0].iter().all(|v| v.is_finite()), "{:?}", out[0]);
|
||||
box_v_into(&src, &mut out, 9, 9, 1, true);
|
||||
assert!(out[0].iter().all(|v| v.is_finite()), "{:?}", out[0]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn box_windows_count_repeated_edges_and_take_any_radius() {
|
||||
// Against a window summed pixel by pixel, for radii inside and far past the image.
|
||||
let (w, h) = (7usize, 5usize);
|
||||
let src: Vec<Px> = (0..w * h).map(|i| [i as f32, (i % 3) as f32, 1.0, 0.5]).collect();
|
||||
let at = |x: isize, y: isize, repeat: bool| -> Px {
|
||||
if !repeat && (x < 0 || y < 0 || x >= w as isize || y >= h as isize) {
|
||||
return [0.0; 4];
|
||||
}
|
||||
src[y.clamp(0, h as isize - 1) as usize * w + x.clamp(0, w as isize - 1) as usize]
|
||||
};
|
||||
for repeat in [true, false] {
|
||||
for r in [1usize, 3, 6, 9, 40] {
|
||||
let (mut hx, mut vx) = (vec![[0.0; 4]; w * h], vec![[0.0; 4]; w * h]);
|
||||
box_h_into(&src, &mut hx, w, r, repeat);
|
||||
box_v_into(&src, &mut vx, w, h, r, repeat);
|
||||
for y in 0..h as isize {
|
||||
for x in 0..w as isize {
|
||||
let (mut eh, mut ev) = ([0.0f32; 4], [0.0f32; 4]);
|
||||
for d in -(r as isize)..=r as isize {
|
||||
for c in 0..4 {
|
||||
eh[c] += at(x + d, y, repeat)[c];
|
||||
ev[c] += at(x, y + d, repeat)[c];
|
||||
}
|
||||
}
|
||||
let n = (2 * r + 1) as f32;
|
||||
let i = y as usize * w + x as usize;
|
||||
for c in 0..4 {
|
||||
assert!((hx[i][c] - eh[c] / n).abs() < 1e-3, "h r={r} repeat={repeat}");
|
||||
assert!((vx[i][c] - ev[c] / n).abs() < 1e-3, "v r={r} repeat={repeat}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// A radius of 2^40 finishes at once.
|
||||
let mut out = vec![[0.0; 4]; w * h];
|
||||
box_h_into(&src, &mut out, w, 1 << 40, true);
|
||||
box_v_into(&src, &mut out.clone(), w, h, 1 << 40, true);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn gaussian_preserves_mass() {
|
||||
let mut img = Image::new(64, 64);
|
||||
|
||||
@@ -292,6 +292,34 @@ function Folder(path) {
|
||||
if (!(this instanceof Folder)) return new Folder(path);
|
||||
this.__path = path === undefined ? "" : __str(path);
|
||||
}
|
||||
|
||||
// ECMAScript's legacy case canonicalisation (what a `/i` regular expression compares with): the
|
||||
// upper case form when it is one code unit, and not an ASCII letter for a non-ASCII one.
|
||||
function __canon(ch) {
|
||||
var u = ch.toUpperCase();
|
||||
if (u.length !== 1) return ch;
|
||||
if (ch.charCodeAt(0) >= 128 && u.charCodeAt(0) < 128) return ch;
|
||||
return u;
|
||||
}
|
||||
|
||||
// A case-insensitive `*` / `?` file mask match in time bounded by mask × name length (a
|
||||
// regular expression of `.*` runs backtracks combinatorially on overlapping wildcards).
|
||||
function __globMatch(mask, name) {
|
||||
// Wildcards run over the original name (lowercasing it can change its length: "İ" becomes
|
||||
// two code units); literal characters compare one at a time, case-insensitively as `/i` does
|
||||
// ("ς" and "σ" and "Σ" match each other).
|
||||
var p = mask, s = name;
|
||||
var i = 0, j = 0, star = -1, mark = 0;
|
||||
while (j < s.length) {
|
||||
if (i < p.length && (p[i] === "?" || p[i] === s[j] || __canon(p[i]) === __canon(s[j]))) { i++; j++; }
|
||||
else if (i < p.length && p[i] === "*") { star = i++; mark = j; }
|
||||
else if (star !== -1) { i = star + 1; j = ++mark; }
|
||||
else return false;
|
||||
}
|
||||
while (i < p.length && p[i] === "*") i++;
|
||||
return i === p.length;
|
||||
}
|
||||
|
||||
Folder.prototype = {
|
||||
constructor: Folder,
|
||||
get fsName() { return this.__path; },
|
||||
@@ -310,10 +338,7 @@ Folder.prototype = {
|
||||
var out = [];
|
||||
for (var i = 0; i < names.length; i++) {
|
||||
var n = names[i];
|
||||
if (typeof mask === "string" && mask !== "*") {
|
||||
var re = new RegExp("^" + mask.replace(/[.+^${}()|[\]\\]/g, "\\$&").replace(/\*/g, ".*").replace(/\?/g, ".") + "$", "i");
|
||||
if (!re.test(n.split(/[\/\\]/).pop())) continue;
|
||||
}
|
||||
if (typeof mask === "string" && mask !== "*" && !__globMatch(mask, n.split(/[\/\\]/).pop())) continue;
|
||||
var isDir = JSON.parse(__file("isDir", n));
|
||||
var f = isDir ? new Folder(n) : new File(n);
|
||||
if (typeof mask === "function" && !mask(f)) continue;
|
||||
|
||||
@@ -532,8 +532,33 @@ fn native_eval_file(_: &JsValue, args: &[JsValue], ctx: &mut Context) -> JsResul
|
||||
r
|
||||
}
|
||||
|
||||
/// Host hooks: code a script compiles at run time (`eval`, `new Function`, …) passes the same
|
||||
/// nesting check as the script itself, since the parser recurses per level and a deep string
|
||||
/// built at run time would overflow the stack.
|
||||
struct Hooks;
|
||||
|
||||
impl boa_engine::context::HostHooks for Hooks {
|
||||
fn ensure_can_compile_strings(
|
||||
&self,
|
||||
_realm: boa_engine::realm::Realm,
|
||||
parameters: &[JsString],
|
||||
body: &JsString,
|
||||
_direct: bool,
|
||||
_context: &mut Context,
|
||||
) -> JsResult<()> {
|
||||
let mut code: String = parameters.iter().map(|p| p.to_std_string_escaped()).collect::<Vec<_>>().join(",");
|
||||
// With no parameters (`eval`) the body is the source as it stands, so a leading `#!` line
|
||||
// is still its first line; with parameters it sits inside a function, where it is no comment.
|
||||
if !parameters.is_empty() {
|
||||
code.push('\n');
|
||||
}
|
||||
code.push_str(&body.to_std_string_escaped());
|
||||
check_nesting(&code).map_err(|e| JsNativeError::syntax().with_message(e).into())
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn new_context() -> Result<Context, String> {
|
||||
let mut ctx = Context::default();
|
||||
let mut ctx = boa_engine::context::ContextBuilder::new().host_hooks(std::rc::Rc::new(Hooks)).build().map_err(|e| e.to_string())?;
|
||||
let limits = ctx.runtime_limits_mut();
|
||||
limits.set_loop_iteration_limit(200_000_000);
|
||||
limits.set_recursion_limit(2000);
|
||||
@@ -771,19 +796,129 @@ fn run_expanded(session: &mut Session, req: &ScriptRequest) -> Outcome {
|
||||
/// 8 MiB thread such as the Script Console's, so deeper scripts are rejected up front.
|
||||
pub const MAX_NESTING: usize = 96;
|
||||
|
||||
/// An error when `code` nests brackets deeper than [`MAX_NESTING`] (strings, template literals
|
||||
/// and comments are skipped).
|
||||
/// Skip template-literal text from `*i` (just past a backtick or an interpolation's `}`). Returns
|
||||
/// true when it stopped past a `${` (code follows), false at the closing backtick or the end.
|
||||
fn skip_template_text(b: &[u8], i: &mut usize) -> bool {
|
||||
while let Some(&d) = b.get(*i) {
|
||||
match d {
|
||||
b'\\' => *i += 2,
|
||||
b'`' => {
|
||||
*i += 1;
|
||||
return false;
|
||||
}
|
||||
b'$' if b.get(*i + 1) == Some(&b'{') => {
|
||||
*i += 2;
|
||||
return true;
|
||||
}
|
||||
_ => *i += 1,
|
||||
}
|
||||
}
|
||||
false
|
||||
}
|
||||
|
||||
/// Whether a `/` after the last significant byte `prev` (at `at` in `b`) starts a regular
|
||||
/// expression literal rather than dividing: after an operator, an opening bracket, a separator,
|
||||
/// a keyword that takes an expression, or at the start. After a name, a number, a string or a
|
||||
/// closing bracket it divides (a regular expression after `)` is the one case this misreads).
|
||||
/// `after_dot`: the word ending at `at` follows a `.` (a property name, never a keyword); a `++`
|
||||
/// or `--` before the `/` is a postfix update, so it divides.
|
||||
fn regex_may_start(b: &[u8], prev: u8, at: usize, after_dot: bool) -> bool {
|
||||
match prev {
|
||||
0 => true,
|
||||
b'+' | b'-' if at > 0 && b.get(at - 1) == Some(&prev) => false,
|
||||
b'(' | b',' | b'=' | b':' | b'[' | b'!' | b'&' | b'|' | b'?' | b'{' | b';' | b'+' | b'-' | b'*' | b'%' | b'<' | b'>' | b'~' | b'^' => true,
|
||||
_ if after_dot => false,
|
||||
c if c.is_ascii_alphanumeric() || c == b'_' || c == b'$' => {
|
||||
let start = (0..=at).rev().find(|&k| !b.get(k).is_some_and(|d| d.is_ascii_alphanumeric() || *d == b'_' || *d == b'$')).map_or(0, |k| k + 1);
|
||||
let word = b.get(start..=at).unwrap_or_default();
|
||||
[&b"return"[..], b"typeof", b"instanceof", b"in", b"of", b"new", b"delete", b"void", b"throw", b"case", b"do", b"else", b"yield", b"await"]
|
||||
.contains(&word)
|
||||
}
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether the `.` at `at` is a property access: not the decimal point of a number (`1.`, `12.`)
|
||||
/// that ends there. A number written `1.5.x` has a fraction before the dot, so that dot accesses.
|
||||
fn dot_is_property(b: &[u8], at: usize) -> bool {
|
||||
let word = |d: u8| d.is_ascii_alphanumeric() || d == b'_' || d == b'$';
|
||||
let start = (0..at).rev().find(|&k| !b.get(k).is_some_and(|d| word(*d))).map_or(0, |k| k + 1);
|
||||
let before = b.get(start..at).unwrap_or_default();
|
||||
let integer = !before.is_empty() && before.iter().all(u8::is_ascii_digit);
|
||||
// `1.5.x`: digits that follow another `.` are a fraction.
|
||||
!(integer && !(start > 0 && b.get(start - 1) == Some(&b'.')))
|
||||
}
|
||||
|
||||
/// Skip a regular expression literal that starts at `b[*i]` (a `/`), to its closing `/` (flags
|
||||
/// follow as ordinary letters). A `/` inside a class `[…]` or after a backslash does not end it,
|
||||
/// and neither a quote nor a backtick inside it starts a string; a line break ends it.
|
||||
fn skip_regex(b: &[u8], i: &mut usize) {
|
||||
*i += 1;
|
||||
let mut class = false;
|
||||
while let Some(&d) = b.get(*i) {
|
||||
match d {
|
||||
b'\\' => *i += 1,
|
||||
b'[' => class = true,
|
||||
b']' => class = false,
|
||||
b'/' if !class => return,
|
||||
b'\n' => return,
|
||||
_ => {}
|
||||
}
|
||||
*i += 1;
|
||||
}
|
||||
}
|
||||
|
||||
/// An error when `code` nests brackets deeper than [`MAX_NESTING`] (strings, regular expression
|
||||
/// literals, template literal text and comments are skipped; the code inside `${…}`
|
||||
/// interpolations is checked).
|
||||
pub fn check_nesting(code: &str) -> Result<(), String> {
|
||||
let b = code.as_bytes();
|
||||
let (mut i, mut depth, mut max) = (0usize, 0usize, 0usize);
|
||||
// The last significant byte (not space or comment), and where it is.
|
||||
let (mut prev, mut prev_at) = (0u8, 0usize);
|
||||
// A `#!` line at the very start is a comment (Boa reads it so): its text is not scanned.
|
||||
if b.starts_with(b"#!") {
|
||||
i = b.iter().position(|d| *d == b'\n').unwrap_or(b.len());
|
||||
}
|
||||
// Whether the word that ends at `prev_at` came right after a `.` token (comments and
|
||||
// whitespace between them do not matter).
|
||||
let mut word_after_dot = false;
|
||||
// The bracket depth of each open `${`, to tell its closing `}` from an inner one.
|
||||
let mut interpolations: Vec<usize> = vec![];
|
||||
while let Some(&c) = b.get(i) {
|
||||
match c {
|
||||
b'`' => {
|
||||
i += 1;
|
||||
if skip_template_text(b, &mut i) {
|
||||
depth += 1;
|
||||
max = max.max(depth);
|
||||
interpolations.push(depth);
|
||||
(prev, prev_at) = (b'{', i - 1);
|
||||
} else {
|
||||
(prev, prev_at) = (b'`', i.saturating_sub(1));
|
||||
}
|
||||
continue;
|
||||
}
|
||||
b'}' if interpolations.last() == Some(&depth) => {
|
||||
interpolations.pop();
|
||||
depth -= 1;
|
||||
i += 1;
|
||||
if skip_template_text(b, &mut i) {
|
||||
depth += 1;
|
||||
max = max.max(depth);
|
||||
interpolations.push(depth);
|
||||
(prev, prev_at) = (b'{', i - 1);
|
||||
} else {
|
||||
(prev, prev_at) = (b'`', i.saturating_sub(1));
|
||||
}
|
||||
continue;
|
||||
}
|
||||
b'(' | b'[' | b'{' => {
|
||||
depth += 1;
|
||||
max = max.max(depth);
|
||||
}
|
||||
b')' | b']' | b'}' => depth = depth.saturating_sub(1),
|
||||
b'"' | b'\'' | b'`' => {
|
||||
b'"' | b'\'' => {
|
||||
// Skip to the closing quote (escapes skip a byte).
|
||||
i += 1;
|
||||
while let Some(&d) = b.get(i) {
|
||||
@@ -799,16 +934,29 @@ pub fn check_nesting(code: &str) -> Result<(), String> {
|
||||
while b.get(i).is_some_and(|d| *d != b'\n') {
|
||||
i += 1;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
b'/' if b.get(i + 1) == Some(&b'*') => {
|
||||
i += 2;
|
||||
while i < b.len() && !(b.get(i) == Some(&b'*') && b.get(i + 1) == Some(&b'/')) {
|
||||
i += 1;
|
||||
}
|
||||
i += 1;
|
||||
i += 2;
|
||||
continue;
|
||||
}
|
||||
b'/' if regex_may_start(b, prev, prev_at, word_after_dot) => skip_regex(b, &mut i),
|
||||
_ => {}
|
||||
}
|
||||
if !c.is_ascii_whitespace() {
|
||||
let word = |d: u8| d.is_ascii_alphanumeric() || d == b'_' || d == b'$';
|
||||
// A word starts where the byte before it is not a word byte (white space and comments
|
||||
// end a word too): it is a property name when a `.` that is not part of a number
|
||||
// comes right before it.
|
||||
if word(c) && (i == 0 || !b.get(i - 1).is_some_and(|d| word(*d))) {
|
||||
word_after_dot = prev == b'.' && dot_is_property(b, prev_at);
|
||||
}
|
||||
(prev, prev_at) = (c, i);
|
||||
}
|
||||
i += 1;
|
||||
}
|
||||
if max > MAX_NESTING {
|
||||
@@ -882,6 +1030,44 @@ mod nesting_tests {
|
||||
assert!(check_nesting(&("(".repeat(MAX_NESTING) + &")".repeat(MAX_NESTING))).is_ok());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn regular_expression_literals_are_skipped_not_read_as_strings_or_templates() {
|
||||
let deep = |n: usize| format!("{}1{}", "(".repeat(n), ")".repeat(n));
|
||||
// A backtick, a quote or a slash inside a class in a regular expression opens nothing, so
|
||||
// the deep expression after it is still counted.
|
||||
for re in ["/`/", "/\"/", "/'/", "/[/]/", "/\\//"] {
|
||||
assert!(check_nesting(&format!("`x${{ {re}.test('x') ? {} : 0 }}`", deep(5000))).is_err(), "{re} in an interpolation");
|
||||
assert!(check_nesting(&format!("x = {re}.test(y) ? {} : 0", deep(5000))).is_err(), "{re}");
|
||||
assert!(check_nesting(&format!("x = {re}.test(y) ? {} : 0", deep(10))).is_ok(), "{re} shallow");
|
||||
}
|
||||
// Division is not a regular expression: what follows it is still scanned.
|
||||
assert!(check_nesting(&format!("a = 4 / 2; b = {}", deep(5000))).is_err());
|
||||
assert!(check_nesting(&format!("a = (4) / 2 / 3; b = {}", deep(5000))).is_err());
|
||||
// After a keyword that takes an expression a slash starts a regular expression.
|
||||
assert!(check_nesting(&format!("typeof /`/ ; {}", deep(5000))).is_err());
|
||||
// A postfix update or a property named like a keyword is followed by a division, not a
|
||||
// regular expression (the deep brackets after it must still be counted).
|
||||
for head in ["i++ /", "i-- /", "obj.in /", "obj . in /", "obj . /* c */ in /", "obj.\n typeof /", "a1.in /", "1.5.in /"] {
|
||||
assert!(check_nesting(&format!("x = {head} {} / 2", deep(5000))).is_err(), "{head}");
|
||||
}
|
||||
// A keyword after a property name is still a keyword (`obj.in in /re/`), and the dot of a
|
||||
// number does not make the word after it a property (`1. in /re/`): the regular expression
|
||||
// is skipped, so the backtick inside it opens nothing and the brackets after it count.
|
||||
for head in ["obj.in in", "1. in", "12. typeof"] {
|
||||
assert!(check_nesting(&format!("x = {head} /`/; y = {}", deep(5000))).is_err(), "{head}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn template_interpolations_are_checked_but_template_text_is_not() {
|
||||
let deep = |n: usize| format!("`a${{{}1{}}}b`", "(".repeat(n), ")".repeat(n));
|
||||
assert!(check_nesting(&deep(5000)).is_err());
|
||||
assert!(check_nesting(&deep(10)).is_ok());
|
||||
// Brackets in the literal text, and a nested template, are fine; depth carries across `}`.
|
||||
assert!(check_nesting(&format!("`{} ${{ `x${{1+(2)}}y` }} {}`", "(".repeat(500), "{".repeat(500))).is_ok());
|
||||
assert!(check_nesting(&format!("`${{1}}${{{}2{}}}`", "[".repeat(MAX_NESTING), "]".repeat(MAX_NESTING))).is_err());
|
||||
}
|
||||
|
||||
/// A canonical drive path and a lexical one compare once the `\\?\` prefix is gone; a UNC
|
||||
/// share and a Unix path are left as they are.
|
||||
#[test]
|
||||
|
||||
@@ -56,6 +56,10 @@ pub(crate) fn read(s: &Session, path: &Path) -> Result<String, String> {
|
||||
if std::fs::metadata(path).is_ok_and(|m| m.len() > MAX_FILE_BYTES as u64) {
|
||||
return Err(too_big());
|
||||
}
|
||||
// A pipe or device reports no size and may never end: only regular files are read.
|
||||
if std::fs::metadata(path).is_ok_and(|m| !m.is_file()) {
|
||||
return Err(format!("Unable to read {}: not a regular file", path.display()));
|
||||
}
|
||||
let bytes = s.services.read_file(&path.to_string_lossy()).map_err(|e| format!("Unable to read {}: {e}", path.display()))?;
|
||||
if bytes.len() > MAX_FILE_BYTES {
|
||||
return Err(too_big());
|
||||
|
||||
@@ -142,7 +142,7 @@ mod imp {
|
||||
|
||||
/// Fill a stream's buffer until `done(buf)` or the timeout / end of stream. Fails only when
|
||||
/// the read deadline can't be set.
|
||||
fn fill(s: &mut TcpStream, buf: &mut Vec<u8>, eof: &mut bool, limit: Duration, done: impl Fn(&[u8]) -> bool) -> std::io::Result<()> {
|
||||
fn fill(s: &mut TcpStream, buf: &mut Vec<u8>, eof: &mut bool, limit: Duration, mut done: impl FnMut(&[u8]) -> bool) -> std::io::Result<()> {
|
||||
let end = Instant::now() + limit;
|
||||
let mut chunk = [0u8; 4096];
|
||||
while !done(buf) && !*eof {
|
||||
@@ -322,7 +322,15 @@ mod imp {
|
||||
}
|
||||
}
|
||||
("readln", Sock::Stream { s, buf, eof }) => {
|
||||
if let Err(e) = fill(s, buf, eof, timeout(p), |b| b.contains(&b'\n')) {
|
||||
// Only the bytes that arrived since the last look are searched: a peer
|
||||
// sending no newline must not make every read rescan the whole buffer.
|
||||
let mut seen = 0;
|
||||
let newline = |b: &[u8]| {
|
||||
let hit = b.get(seen..).is_some_and(|new| new.contains(&b'\n'));
|
||||
seen = b.len();
|
||||
hit
|
||||
};
|
||||
if let Err(e) = fill(s, buf, eof, timeout(p), newline) {
|
||||
return err(format!("cannot set the read timeout: {e}"));
|
||||
}
|
||||
let line: Vec<u8> = match buf.iter().position(|&c| c == b'\n') {
|
||||
|
||||
@@ -2000,6 +2000,51 @@ fn setting_comp_width_and_height_leaves_the_layers_where_they_are() {
|
||||
assert_eq!(layer(c, "s").props.prop("transform/position").unwrap().value, KV::Vec3([100.0, 100.0, 0.0]));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_hashbang_line_does_not_hide_deep_nesting_from_eval() {
|
||||
// Boa reads a leading `#!` line of eval source as a comment, so the backtick on it opens no
|
||||
// template: the brackets after it are counted and refused.
|
||||
let mut s = session();
|
||||
let code = "var deep = new Array(5000).join('(') + '1' + new Array(5000).join(')'); var r; \
|
||||
try { eval('#! `\\n' + deep); r = 'ran'; } catch (e) { r = 'refused'; } r";
|
||||
assert_eq!(ok(&mut s, code).result.as_str(), Some("refused"));
|
||||
assert!(crate::runtime::check_nesting(&format!("#! `\n{}1{}", "(".repeat(5000), ")".repeat(5000))).is_err());
|
||||
assert!(crate::runtime::check_nesting("#! `\n(1)").is_ok());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn code_compiled_at_run_time_passes_the_nesting_check() {
|
||||
// `eval` and `new Function` of a string nested past the limit are refused with a catchable
|
||||
// error instead of overflowing the parser's stack; shallow code still compiles.
|
||||
let mut s = session();
|
||||
for call in ["eval(deep)", "new Function(deep)", "Function('a', deep)"] {
|
||||
let code = format!(
|
||||
"var deep = new Array(5000).join('(') + '1' + new Array(5000).join(')'); var r; try {{ {call}; r = 'ran'; }} catch (e) {{ r = 'refused'; }} r"
|
||||
);
|
||||
assert_eq!(ok(&mut s, &code).result.as_str(), Some("refused"), "{call}");
|
||||
}
|
||||
assert_eq!(ok(&mut s, "eval('1 + (2 * 3)') + new Function('return 4')()").result.as_f64(), Some(11.0));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn file_masks_match_without_backtracking() {
|
||||
let mut s = session();
|
||||
let r = ok(
|
||||
&mut s,
|
||||
"[__globMatch('*.JSX', 'a.jsx'), __globMatch('a?c*', 'abcdef'), __globMatch('*a*b', 'xaxb'), __globMatch('*.png', 'a.jsx'), __globMatch('*a*a*a*a*a*a*a*a*a*a*a*b', new Array(60).join('a'))].join()",
|
||||
);
|
||||
assert_eq!(r.result.as_str(), Some("true,true,true,false,false"));
|
||||
// `?` still matches one character that lowercases to two.
|
||||
let r = ok(&mut s, "[__globMatch('?.txt', '\\u0130.txt'), __globMatch('\\u0130*', '\\u0130x')].join()");
|
||||
assert_eq!(r.result.as_str(), Some("true,true"));
|
||||
// Greek sigma matches in every case form, as the case-insensitive regular expression did.
|
||||
let r = ok(
|
||||
&mut s,
|
||||
"[__globMatch('\\u03bf\\u03c2.txt', '\\u039f\\u03a3.txt'), __globMatch('\\u03a3*', '\\u03c2x'), __globMatch('k', '\\u212a'), __globMatch('a', 'b')].join()",
|
||||
);
|
||||
assert_eq!(r.result.as_str(), Some("true,true,false,false"));
|
||||
}
|
||||
|
||||
/// #3395: setValue on a string effect parameter stores the text, or a File's path, as `prop.set`
|
||||
/// does. Before, every string fell through to the number check ("value must be a number").
|
||||
/// Hidden string parameters (Mesh Warp's Distortion Mesh) stay refused, keys included.
|
||||
|
||||
@@ -19,6 +19,9 @@ const IMG: usize = 1024;
|
||||
const EMB: usize = 64;
|
||||
const DIM: usize = 256;
|
||||
const LOW: usize = 256;
|
||||
/// Most prompt points the decoder takes (each is an attention token, and self-attention is
|
||||
/// quadratic in them); SAM is used with a handful. More are thinned evenly, keeping both ends.
|
||||
const MAX_POINTS: usize = 256;
|
||||
const MEAN: [f32; 3] = [123.675, 116.28, 103.53];
|
||||
const STD: [f32; 3] = [58.395, 57.12, 57.375];
|
||||
|
||||
@@ -609,7 +612,7 @@ impl MobileSam {
|
||||
/// Foreground probability (`w×h`) for a prompt on an embedded image, and the predicted IoU.
|
||||
pub fn predict(&self, e: &Embedding, prompt: &Prompt) -> (Vec<f32>, f32) {
|
||||
let s = e.scale;
|
||||
let points: Vec<([f32; 2], bool)> = prompt.points.iter().map(|(p, fg)| ([p[0] * s, p[1] * s], *fg)).collect();
|
||||
let points: Vec<([f32; 2], bool)> = thin(&prompt.points).iter().map(|(p, fg)| ([p[0] * s, p[1] * s], *fg)).collect();
|
||||
let bbox = prompt.bbox.map(|b| b.map(|v| v * s));
|
||||
// A prior mask (probabilities at image size) → logits on the low-res grid.
|
||||
let mask: Option<Vec<f32>> = prompt.mask.as_ref().filter(|m| m.len() == e.w * e.h).map(|m| {
|
||||
@@ -675,3 +678,28 @@ fn sample(rgb: &[[f32; 3]], w: usize, h: usize, x: f32, y: f32, foot: f32) -> [f
|
||||
}
|
||||
s.map(|v| v / f32::max(n, 1.0))
|
||||
}
|
||||
|
||||
/// `points`, or `MAX_POINTS` evenly spaced ones (first and last kept) when there are more.
|
||||
fn thin(points: &[([f32; 2], bool)]) -> Vec<([f32; 2], bool)> {
|
||||
let n = points.len();
|
||||
if n <= MAX_POINTS {
|
||||
return points.to_vec();
|
||||
}
|
||||
(0..MAX_POINTS).filter_map(|i| points.get(i * (n - 1) / (MAX_POINTS - 1)).copied()).collect()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn many_prompt_points_are_thinned_to_the_budget() {
|
||||
let few: Vec<_> = (0..MAX_POINTS).map(|i| ([i as f32, 0.0], i % 2 == 0)).collect();
|
||||
assert_eq!(thin(&few), few, "a prompt within the budget is untouched");
|
||||
let many: Vec<_> = (0..1_000_000).map(|i| ([i as f32, 0.0], i % 2 == 0)).collect();
|
||||
let t = thin(&many);
|
||||
assert_eq!(t.len(), MAX_POINTS);
|
||||
assert_eq!((t[0].0, t[MAX_POINTS - 1].0), (many[0].0, many[many.len() - 1].0));
|
||||
assert!(t.windows(2).all(|w| w[0].0[0] < w[1].0[0]), "spread in order");
|
||||
}
|
||||
}
|
||||
@@ -131,6 +131,8 @@ const MAX_VALUES: u64 = 1 << 30;
|
||||
const MAX_LIVE: u64 = 1 << 29;
|
||||
/// The most constant values decoded and laid out at load (a weight shared by several tensors or
|
||||
/// operators counts once per use).
|
||||
/// Bytes of tensor names and shapes a model may carry in all (the official ones take well under 1 MB).
|
||||
const MAX_META: usize = 1 << 24;
|
||||
const MAX_CONSTS: usize = 1 << 27;
|
||||
|
||||
/// Tensor types (`TensorType`).
|
||||
@@ -261,6 +263,7 @@ impl Model {
|
||||
let mut ints: Vec<Option<Vec<i32>>> = vec![None; n];
|
||||
let mut data: Vec<(usize, u8, usize, &[u8])> = vec![];
|
||||
let mut consts = 0usize;
|
||||
let mut meta = 0usize;
|
||||
for (i, t) in tensors.iter().enumerate() {
|
||||
let shape: Vec<usize> = fb
|
||||
.ints(*t, 0)?
|
||||
@@ -269,7 +272,13 @@ impl Model {
|
||||
.collect::<std::result::Result<_, _>>()?;
|
||||
let ty = fb.byte(*t, 1, FLOAT32)?;
|
||||
let buf = fb.int(*t, 2, 0)? as u32 as usize;
|
||||
names.push(fb.string(*t, 3)?);
|
||||
let name = fb.string(*t, 3)?;
|
||||
// Names and shapes are copied per tensor: bound them in all.
|
||||
meta = meta.saturating_add(name.len()).saturating_add(shape.len().saturating_mul(8));
|
||||
if meta > MAX_META {
|
||||
return Err("the model's tensor metadata is too large".into());
|
||||
}
|
||||
names.push(name);
|
||||
// Every size computed from shapes later stays in range, even a product of some of the
|
||||
// dimensions (a zero dimension does not let the others grow without bound).
|
||||
let count = shape.iter().try_fold(1usize, |a, &d| a.checked_mul(d)).ok_or("a tensor is too large")?;
|
||||
@@ -329,6 +338,11 @@ impl Model {
|
||||
&& let Some(i) = ins.first().copied().flatten()
|
||||
&& let Some(len) = floats[i].as_ref().map(Vec::len)
|
||||
{
|
||||
// The folded constant is what the graph outputs and the run's memory estimate count:
|
||||
// it must be the declared size.
|
||||
if shapes[output].iter().try_fold(1usize, |a, &d| a.checked_mul(d)) != Some(len) {
|
||||
return Err(format!("{}: DEQUANTIZE output size disagrees with its input", names[output]));
|
||||
}
|
||||
spend(len)?;
|
||||
floats[output] = floats[i].clone();
|
||||
continue;
|
||||
|
||||
@@ -334,6 +334,26 @@ fn shapes_computed_at_run_time_must_match_the_declared_ones() {
|
||||
refused(&zero, "tensor is too large");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_folded_dequantize_has_its_declared_size() {
|
||||
let half = |n: usize| vec![0u8; 2 * n];
|
||||
let fold = |declared: Vec<i32>| {
|
||||
model(vec![half(4)], vec![(vec![1, 4], FLOAT16, Some(0)), (declared, FLOAT32, None)], vec![(op::DEQUANTIZE, vec![0], vec![1], vec![])], vec![], vec![1])
|
||||
};
|
||||
// A declared output of one value would let the folded four-value constant be copied out per reference.
|
||||
refused(&fold(vec![1, 1]), "DEQUANTIZE output size");
|
||||
assert!(Model::read(&fold(vec![1, 4])).is_ok());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tensor_metadata_is_bounded_in_all() {
|
||||
// A rank of 4 million dimensions of 1 is a valid shape by size, but copied per tensor.
|
||||
let m = model(vec![], vec![(vec![1; 1 << 22], FLOAT32, None)], vec![], vec![], vec![]);
|
||||
refused(&m, "metadata is too large");
|
||||
let ok = model(vec![], (0..70).map(|_| (vec![1, 2, 3], FLOAT32, None)).collect(), vec![], vec![], vec![]);
|
||||
assert!(Model::read(&ok).is_ok());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn shared_weights_count_once_per_use() {
|
||||
// One 128 KB buffer decoded for 2100 tensors would be 2100 copies of 2¹⁶ floats.
|
||||
|
||||
@@ -16,3 +16,4 @@ effectcraft-path = { workspace = true }
|
||||
kurbo = { workspace = true }
|
||||
roxmltree = "0.21"
|
||||
rayon = { workspace = true }
|
||||
log = { workspace = true }
|
||||
@@ -603,8 +603,15 @@ struct Parser<'a, 'i> {
|
||||
/// The `<use>` targets being expanded, outermost first: a reference back into one is a
|
||||
/// cycle and is skipped.
|
||||
using: Vec<roxmltree::NodeId>,
|
||||
/// Path elements and gradient stops kept so far, `<use>` copies and repeated gradient
|
||||
/// references included; past [`MAX_RETAINED`] the import errs.
|
||||
retained: std::cell::Cell<usize>,
|
||||
}
|
||||
|
||||
/// The most path elements and gradient stops an import keeps: a complex path reused many times,
|
||||
/// or a gradient with many stops referenced by many shapes, is copied for each use.
|
||||
const MAX_RETAINED: usize = 8_000_000;
|
||||
|
||||
fn is_svg_ns(n: &roxmltree::Node) -> bool {
|
||||
n.is_element()
|
||||
}
|
||||
@@ -628,6 +635,13 @@ impl<'a, 'i> Parser<'a, 'i> {
|
||||
out
|
||||
}
|
||||
|
||||
/// Charge `n` kept path elements or gradient stops; false once [`MAX_RETAINED`] is passed.
|
||||
fn charge_retained(&self, n: usize) -> bool {
|
||||
let r = self.retained.get().saturating_add(n);
|
||||
self.retained.set(r);
|
||||
r <= MAX_RETAINED
|
||||
}
|
||||
|
||||
/// Charge `n` units of CSS work; false once the budget is spent.
|
||||
fn charge_css(&self, n: usize) -> bool {
|
||||
let w = self.css_work.get().saturating_add(n);
|
||||
@@ -875,6 +889,10 @@ impl<'a, 'i> Parser<'a, 'i> {
|
||||
// Stops: from the first element in the chain that has any.
|
||||
let mut stops = vec![];
|
||||
if let Some(src) = chain.iter().find(|e| e.children().any(|c| c.is_element() && c.tag_name().name() == "stop")) {
|
||||
// Charged before the stops are read: every reference reads and keeps them again.
|
||||
if !self.charge_retained(src.children().filter(|c| c.is_element()).count()) {
|
||||
return None;
|
||||
}
|
||||
let mut last = 0.0f64;
|
||||
for s in src.children().filter(|c| c.is_element() && c.tag_name().name() == "stop") {
|
||||
let d = self.declared(s);
|
||||
@@ -1026,6 +1044,11 @@ impl<'a, 'i> Parser<'a, 'i> {
|
||||
return;
|
||||
}
|
||||
let Some(geom) = self.geometry(n, &st) else { return };
|
||||
if let Geom::Path(p) = &geom
|
||||
&& !self.charge_retained(p.elements().len())
|
||||
{
|
||||
return;
|
||||
}
|
||||
let fill = if matches!(tag, "line") { None } else { st.fill.as_ref().and_then(|p| self.resolve_paint(p, &st)) };
|
||||
let (fill, fill_alpha) = match fill {
|
||||
Some((p, a)) => (Some(p), a),
|
||||
@@ -1304,6 +1327,7 @@ pub fn parse(bytes: &[u8]) -> Result<Doc, Error> {
|
||||
work: 0,
|
||||
budget,
|
||||
using: vec![],
|
||||
retained: std::cell::Cell::new(0),
|
||||
};
|
||||
let st = Style::default();
|
||||
let pct_or = |name: &str, axis: char, def: f64| -> f64 {
|
||||
@@ -1344,6 +1368,9 @@ pub fn parse(bytes: &[u8]) -> Result<Doc, Error> {
|
||||
// Never a silently partial drawing.
|
||||
return Err(Error::Xml(format!("its <use> references expand to more than {} elements", p.budget)));
|
||||
}
|
||||
if p.retained.get() > MAX_RETAINED {
|
||||
return Err(Error::Xml(format!("its paths and gradients, with their copies, hold more than {MAX_RETAINED} elements")));
|
||||
}
|
||||
if p.css_work.get() > MAX_CSS_WORK {
|
||||
// Never a silently unstyled drawing.
|
||||
return Err(Error::Xml(format!("matching its <style> rules takes more than {MAX_CSS_WORK} steps")));
|
||||
|
||||
@@ -20,7 +20,10 @@ pub fn rasterize(doc: &Doc, w: u32, h: u32, scale: f64) -> Image {
|
||||
|
||||
/// Rasterise `doc` with `m` mapping document pixels to image pixels.
|
||||
pub fn rasterize_with(doc: &Doc, w: u32, h: u32, m: Affine) -> Image {
|
||||
let mut budget = SurfaceBudget { live_bytes: 0, limit_bytes: MAX_LIVE_SURFACE_BYTES };
|
||||
// Nested knockout and non-isolated groups draw their children more than once; the number of
|
||||
// node draws is bounded by a multiple of the tree's size, past which drawing stops.
|
||||
let draws_left = node_count(&doc.root).saturating_mul(MAX_DRAWS_PER_NODE).max(MIN_DRAWS);
|
||||
let mut budget = SurfaceBudget { live_bytes: 0, limit_bytes: MAX_LIVE_SURFACE_BYTES, draws_left };
|
||||
let Some(mut img) = budget.image(w, h, None) else { return Image::default() };
|
||||
draw_group(&doc.root, m, 1.0, &mut img, &mut budget);
|
||||
img
|
||||
@@ -29,9 +32,31 @@ pub fn rasterize_with(doc: &Doc, w: u32, h: u32, m: Affine) -> Image {
|
||||
/// Bound the simultaneously live offscreen image surfaces created by nested groups.
|
||||
const MAX_LIVE_SURFACE_BYTES: usize = 512 * 1024 * 1024;
|
||||
|
||||
/// How many times each node of a document may be drawn, on average, in one rasterisation.
|
||||
const MAX_DRAWS_PER_NODE: usize = 64;
|
||||
const MIN_DRAWS: usize = 100_000;
|
||||
|
||||
/// The nodes in `g`'s tree, `g` included.
|
||||
fn node_count(g: &Group) -> usize {
|
||||
let mut n = 0usize;
|
||||
let mut stack = vec![g];
|
||||
while let Some(g) = stack.pop() {
|
||||
n = n.saturating_add(1);
|
||||
for c in &g.children {
|
||||
match c {
|
||||
Node::Group(cg) => stack.push(cg),
|
||||
_ => n = n.saturating_add(1),
|
||||
}
|
||||
}
|
||||
}
|
||||
n
|
||||
}
|
||||
|
||||
struct SurfaceBudget {
|
||||
live_bytes: usize,
|
||||
limit_bytes: usize,
|
||||
/// Node draws left (see [`MAX_DRAWS_PER_NODE`]).
|
||||
draws_left: usize,
|
||||
}
|
||||
|
||||
impl SurfaceBudget {
|
||||
@@ -394,6 +419,11 @@ fn set_sat(c: [f32; 3], s: f32) -> [f32; 3] {
|
||||
}
|
||||
|
||||
fn draw_node(n: &Node, m: Affine, opacity: f64, dst: &mut Image, budget: &mut SurfaceBudget) {
|
||||
let Some(left) = budget.draws_left.checked_sub(1) else { return };
|
||||
budget.draws_left = left;
|
||||
if left == 0 {
|
||||
log::warn!("svg: draw budget reached, the rest of the drawing is skipped");
|
||||
}
|
||||
match n {
|
||||
Node::Group(g) => draw_group(g, m, opacity, dst, budget),
|
||||
Node::Shape(s) => {
|
||||
|
||||
@@ -446,3 +446,40 @@ fn at_rule_blocks_are_skipped_by_brace_depth() {
|
||||
assert_eq!(sheet.rules.len(), 1);
|
||||
assert_eq!(sheet.rules[0].classes, vec!["e".to_string()]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn gradient_stops_copied_past_the_budget_are_refused() {
|
||||
// 4,000 stops referenced by 2,100 shapes would keep 8.4 million stops.
|
||||
let stops: String = (0..4000).map(|i| format!("<stop offset=\"{}\" stop-color=\"red\"/>", i as f64 / 4000.0)).collect();
|
||||
let rects = "<rect width=\"1\" height=\"1\" fill=\"url(#g)\"/>".repeat(2100);
|
||||
let svg = format!("<svg xmlns=\"http://www.w3.org/2000/svg\"><defs><linearGradient id=\"g\">{stops}</linearGradient></defs>{rects}</svg>");
|
||||
assert!(matches!(parse(svg.as_bytes()), Err(Error::Xml(e)) if e.contains("gradients")));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn nested_knockout_groups_stop_at_the_draw_budget() {
|
||||
// Each knockout level draws its children twice and each non-isolated level twice more:
|
||||
// 30 nested levels would be 4^30 draws. The draw budget ends it.
|
||||
let mut g = Group::new("leaf");
|
||||
g.children.push(Node::Shape(Shape {
|
||||
name: "s".into(),
|
||||
geom: Geom::Rect { x: 0.0, y: 0.0, w: 4.0, h: 4.0, rx: 0.0, ry: 0.0 },
|
||||
transform: Affine::IDENTITY,
|
||||
fill: Some(Paint::Color([1.0, 0.0, 0.0])),
|
||||
fill_opacity: 1.0,
|
||||
fill_rule: FillRule::NonZero,
|
||||
stroke: None,
|
||||
opacity: 1.0,
|
||||
}));
|
||||
for _ in 0..30 {
|
||||
let mut p = Group::new("k");
|
||||
p.knockout = true;
|
||||
p.isolated = false;
|
||||
p.children.push(Node::Group(g));
|
||||
g = p;
|
||||
}
|
||||
let doc = Doc { width: 4.0, height: 4.0, root: g, skipped: vec![] };
|
||||
let (tx, rx) = std::sync::mpsc::channel();
|
||||
std::thread::spawn(move || tx.send(rasterize(&doc, 4, 4, 1.0).width).ok());
|
||||
assert_eq!(rx.recv_timeout(std::time::Duration::from_secs(20)).expect("rendering finishes"), 4);
|
||||
}
|
||||
+55
-26
@@ -107,17 +107,29 @@ fn extra_dirs(var: Option<&std::ffi::OsStr>) -> Vec<PathBuf> {
|
||||
var.map(|v| std::env::split_paths(v).filter(|p| !p.as_os_str().is_empty()).collect()).unwrap_or_default()
|
||||
}
|
||||
|
||||
fn walk(dir: &std::path::Path, depth: u32, out: &mut Vec<PathBuf>) {
|
||||
let Ok(rd) = std::fs::read_dir(dir) else { return };
|
||||
for e in rd.flatten() {
|
||||
let p = e.path();
|
||||
if p.is_dir() {
|
||||
// Bounded, so a symlink loop ends.
|
||||
if depth < 8 {
|
||||
walk(&p, depth + 1, out);
|
||||
/// Font files under `roots`, each real folder listed once. Breadth first from every root, so a
|
||||
/// folder reached by several routes is listed at its shallowest and keeps the most depth below it.
|
||||
fn walk(roots: &[PathBuf], out: &mut Vec<PathBuf>) {
|
||||
let mut seen = std::collections::HashSet::new();
|
||||
let mut queue: std::collections::VecDeque<(PathBuf, u32)> = roots.iter().map(|r| (r.clone(), 0)).collect();
|
||||
while let Some((dir, depth)) = queue.pop_front() {
|
||||
// Symlinks fanning out to the same folders would otherwise be walked again along every
|
||||
// path to them (exponentially many within the depth bound).
|
||||
if !seen.insert(std::fs::canonicalize(&dir).unwrap_or_else(|_| dir.clone())) {
|
||||
continue;
|
||||
}
|
||||
let Ok(rd) = std::fs::read_dir(&dir) else { continue };
|
||||
for e in rd.flatten() {
|
||||
let p = e.path();
|
||||
if p.is_dir() {
|
||||
// Bounded, so a symlink loop ends.
|
||||
if depth < 8 {
|
||||
queue.push_back((p, depth + 1));
|
||||
}
|
||||
} else if p.extension().and_then(std::ffi::OsStr::to_str).is_some_and(|x| matches!(x.to_ascii_lowercase().as_str(), "ttf" | "otf" | "ttc" | "otc"))
|
||||
{
|
||||
out.push(p);
|
||||
}
|
||||
} else if p.extension().and_then(std::ffi::OsStr::to_str).is_some_and(|x| matches!(x.to_ascii_lowercase().as_str(), "ttf" | "otf" | "ttc" | "otc")) {
|
||||
out.push(p);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -125,9 +137,7 @@ fn walk(dir: &std::path::Path, depth: u32, out: &mut Vec<PathBuf>) {
|
||||
impl FontSource for DirectorySource {
|
||||
fn faces(&self) -> Vec<FaceInfo> {
|
||||
let mut files = Vec::new();
|
||||
for d in &self.dirs {
|
||||
walk(d, 0, &mut files);
|
||||
}
|
||||
walk(&self.dirs, &mut files);
|
||||
files.sort();
|
||||
files.dedup();
|
||||
let mut out = Vec::new();
|
||||
@@ -158,17 +168,6 @@ fn info_from(n: FaceNames, data: FaceData, origin: &'static str) -> FaceInfo {
|
||||
}
|
||||
}
|
||||
|
||||
/// A font file's bytes and its metadata from the same open file (so the metadata describes the
|
||||
/// bytes, not a later version of the file).
|
||||
fn read_with_metadata(path: &std::path::Path) -> std::io::Result<(Vec<u8>, std::fs::Metadata)> {
|
||||
use std::io::Read;
|
||||
let mut f = std::fs::File::open(path)?;
|
||||
let meta = f.metadata()?;
|
||||
let mut bytes = Vec::new();
|
||||
f.read_to_end(&mut bytes)?;
|
||||
Ok((bytes, meta))
|
||||
}
|
||||
|
||||
/// A loaded (or loadable) face.
|
||||
pub struct Face {
|
||||
pub id: FaceId,
|
||||
@@ -207,6 +206,36 @@ pub struct VMetrics {
|
||||
pub underline_thickness: f32,
|
||||
}
|
||||
|
||||
/// A font file's bytes, shared by every face of it that is loaded (the faces of a collection
|
||||
/// each held their own copy of the whole file), with the metadata of the open file they were read
|
||||
/// from (so it describes the bytes, not a later version of the file).
|
||||
fn file_bytes(p: &std::path::Path) -> std::io::Result<(Arc<Vec<u8>>, std::fs::Metadata)> {
|
||||
use std::io::Read;
|
||||
type Key = (PathBuf, u64, Option<std::time::SystemTime>);
|
||||
type Entry = (std::sync::Weak<Vec<u8>>, std::fs::Metadata);
|
||||
static FILES: std::sync::LazyLock<std::sync::Mutex<std::collections::HashMap<Key, Entry>>> = std::sync::LazyLock::new(Default::default);
|
||||
let lock = || FILES.lock().unwrap_or_else(std::sync::PoisonError::into_inner);
|
||||
let hit = |files: &std::collections::HashMap<Key, Entry>, key: &Key| files.get(key).and_then(|(w, m)| Some((w.upgrade()?, m.clone())));
|
||||
let mut f = std::fs::File::open(p)?;
|
||||
// Keyed by size and modification time too: a file replaced at the same path is read afresh.
|
||||
let m = f.metadata()?;
|
||||
let key: Key = (p.to_path_buf(), m.len(), m.modified().ok());
|
||||
if let Some(found) = hit(&lock(), &key) {
|
||||
return Ok(found);
|
||||
}
|
||||
// Read outside the lock: a slow file must not hold up every other face's first load.
|
||||
let mut bytes = Vec::new();
|
||||
f.read_to_end(&mut bytes)?;
|
||||
let b = Arc::new(bytes);
|
||||
let mut files = lock();
|
||||
if let Some(found) = hit(&files, &key) {
|
||||
return Ok(found);
|
||||
}
|
||||
files.retain(|_, (w, _)| w.strong_count() > 0);
|
||||
files.insert(key, (Arc::downgrade(&b), m.clone()));
|
||||
Ok((b, m))
|
||||
}
|
||||
|
||||
impl Face {
|
||||
/// The font file bytes, loaded once on first use, or `None` when they cannot be read.
|
||||
fn bytes(&self) -> Option<&[u8]> {
|
||||
@@ -215,9 +244,9 @@ impl Face {
|
||||
FaceData::Static(b) => Some(FaceBytes::Static(b)),
|
||||
FaceData::Shared(b) => Some(FaceBytes::Shared(b.clone())),
|
||||
FaceData::File(p) => {
|
||||
let (bytes, meta) = read_with_metadata(p).ok()?;
|
||||
let (bytes, meta) = file_bytes(p).ok()?;
|
||||
let _ = self.file_meta.set(meta);
|
||||
Some(FaceBytes::Shared(Arc::new(bytes)))
|
||||
Some(FaceBytes::Shared(bytes))
|
||||
}
|
||||
})
|
||||
.as_ref()
|
||||
|
||||
@@ -567,3 +567,72 @@ fn unknown_face_id_returns_bundled_default() {
|
||||
assert!(fallback.font().is_some());
|
||||
assert!(Arc::ptr_eq(&fallback, &face(0)));
|
||||
}
|
||||
|
||||
/// A scratch folder this test created itself (never one that already exists), removed on drop.
|
||||
struct Scratch(std::path::PathBuf);
|
||||
impl Scratch {
|
||||
fn new(tag: &str) -> Self {
|
||||
let d = (0u32..)
|
||||
.map(|n| std::env::temp_dir().join(format!("effectcraft-{tag}-{}-{n}", std::process::id())))
|
||||
.find_map(|d| match std::fs::create_dir(&d) {
|
||||
Ok(()) => Some(d),
|
||||
Err(e) if e.kind() == std::io::ErrorKind::AlreadyExists => None,
|
||||
Err(e) => panic!("create scratch dir {}: {e}", d.display()),
|
||||
})
|
||||
.unwrap();
|
||||
Scratch(d)
|
||||
}
|
||||
}
|
||||
impl Drop for Scratch {
|
||||
fn drop(&mut self) {
|
||||
let _ = std::fs::remove_dir_all(&self.0);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
#[test]
|
||||
fn a_folder_reached_through_many_links_is_listed_once() {
|
||||
// Six links back to the folder itself: 6^8 listings within the depth bound before.
|
||||
let scratch = Scratch::new("font-walk");
|
||||
let d = &scratch.0;
|
||||
std::fs::write(d.join("a.ttf"), b"x").unwrap();
|
||||
for i in 0..6 {
|
||||
std::os::unix::fs::symlink(d, d.join(format!("l{i}"))).unwrap();
|
||||
}
|
||||
let mut out = Vec::new();
|
||||
let start = std::time::Instant::now();
|
||||
walk(std::slice::from_ref(d), &mut out);
|
||||
assert!(start.elapsed() < std::time::Duration::from_secs(5));
|
||||
assert_eq!(out.len(), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_folder_inside_another_root_keeps_its_own_depth() {
|
||||
// root/a/b/c/d/e/f/g/h is also a root: the walk of `root` reaches it at depth 8 and could not
|
||||
// go below it, so it must be listed from the shallower visit.
|
||||
let scratch = Scratch::new("font-depth");
|
||||
let root = &scratch.0;
|
||||
let deep = root.join("a/b/c/d/e/f/g/h");
|
||||
std::fs::create_dir_all(deep.join("sub")).unwrap();
|
||||
std::fs::write(deep.join("sub/x.ttf"), b"x").unwrap();
|
||||
let mut out = Vec::new();
|
||||
walk(&[root.clone(), deep.clone()], &mut out);
|
||||
assert_eq!(out, vec![deep.join("sub/x.ttf")]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_font_file_replaced_at_the_same_path_is_read_again() {
|
||||
let scratch = Scratch::new("font-bytes");
|
||||
let p = scratch.0.join("a.ttf");
|
||||
std::fs::write(&p, b"old").unwrap();
|
||||
let (old, old_meta) = file_bytes(&p).unwrap();
|
||||
// Faces of one file share its bytes, and the metadata is that of the file the bytes came from.
|
||||
let (again, _) = file_bytes(&p).unwrap();
|
||||
assert!(Arc::ptr_eq(&old, &again));
|
||||
assert_eq!(old_meta.len(), 3);
|
||||
std::fs::write(&p, b"new, longer").unwrap();
|
||||
let (new, new_meta) = file_bytes(&p).unwrap();
|
||||
assert_eq!(new.as_slice(), b"new, longer");
|
||||
assert_eq!(new_meta.len(), 11);
|
||||
assert_eq!(old.as_slice(), b"old");
|
||||
}
|
||||
+37
-21
@@ -628,9 +628,31 @@ fn is_hanging_open(c: char) -> bool {
|
||||
matches!(c, '"' | '\'' | '\u{2018}' | '\u{201c}' | '(' | '\u{ab}')
|
||||
}
|
||||
|
||||
/// Whether no run is reversed and each starts at or after the end of the one before it (the runs
|
||||
/// of a document): the precondition of [`run_at`]'s bisection.
|
||||
fn runs_ordered(runs: &[(Range<usize>, TextStyle)]) -> bool {
|
||||
runs.iter().all(|(r, _)| r.start <= r.end) && runs.windows(2).all(|w| w[0].0.end <= w[1].0.start)
|
||||
}
|
||||
|
||||
/// Style runs with whether they are [`runs_ordered`].
|
||||
#[derive(Clone, Copy)]
|
||||
struct Runs<'a> {
|
||||
list: &'a [(Range<usize>, TextStyle)],
|
||||
ordered: bool,
|
||||
}
|
||||
|
||||
/// The style run index covering global byte `b` (the run before it at a boundary, for empty
|
||||
/// paragraphs at the end).
|
||||
fn run_at(runs: &[(Range<usize>, TextStyle)], b: usize) -> usize {
|
||||
/// paragraphs at the end). `ordered` is [`runs_ordered`] of `runs`.
|
||||
fn run_at(runs: &[(Range<usize>, TextStyle)], ordered: bool, b: usize) -> usize {
|
||||
// Ordered, non-overlapping runs: the last one starting at or before `b` is the only one that
|
||||
// can hold it, found by bisection (a scan from the first run per character was quadratic).
|
||||
// Anything else (a gap, overlapping or unordered runs) takes the scan, whose first match wins.
|
||||
if ordered {
|
||||
let i = runs.partition_point(|(r, _)| r.start <= b).saturating_sub(1);
|
||||
if runs.get(i).is_some_and(|(r, _)| r.contains(&b)) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
runs.iter().position(|(r, _)| b >= r.start && b < r.end).unwrap_or_else(|| runs.iter().rposition(|(r, _)| r.start <= b).unwrap_or(0))
|
||||
}
|
||||
|
||||
@@ -706,7 +728,7 @@ impl<'a> Para<'a> {
|
||||
fn new(
|
||||
text: &'a str,
|
||||
base: usize,
|
||||
runs: &'a [(Range<usize>, TextStyle)],
|
||||
runs: Runs<'a>,
|
||||
primaries: &'a [Resolved],
|
||||
variation_ids: &'a [u32],
|
||||
para: &'a ParagraphStyle,
|
||||
@@ -716,7 +738,7 @@ impl<'a> Para<'a> {
|
||||
let bidi = BidiInfo::new(text, default_level);
|
||||
let base_rtl = bidi.paragraphs[0].level.is_rtl();
|
||||
let chars = char_forms(text, base, runs, primaries, &bidi);
|
||||
let items = build_items(text, &chars, runs);
|
||||
let items = build_items(text, &chars, runs.list);
|
||||
// Prefix widths and the preceding non-whitespace boundary make candidate line widths O(1).
|
||||
// Every-line composition evaluates O(n²) candidate breaks; summing each candidate's bytes
|
||||
// or rescanning its trailing whitespace would otherwise add another factor of text length.
|
||||
@@ -739,7 +761,7 @@ impl<'a> Para<'a> {
|
||||
}
|
||||
previous_non_ws[b + c.len_utf8()] = non_ws_end;
|
||||
}
|
||||
Para { text, base, runs, primaries, variation_ids, para, width, bidi, base_rtl, chars, items, char_adv, prefix_adv, previous_non_ws }
|
||||
Para { text, base, runs: runs.list, primaries, variation_ids, para, width, bidi, base_rtl, chars, items, char_adv, prefix_adv, previous_non_ws }
|
||||
}
|
||||
|
||||
fn range_w(&self, r: Range<usize>) -> f32 {
|
||||
@@ -1021,11 +1043,11 @@ impl<'a> Para<'a> {
|
||||
}
|
||||
|
||||
/// Each char with its shaped form, face, direction and run.
|
||||
fn char_forms(text: &str, base: usize, runs: &[(Range<usize>, TextStyle)], primaries: &[Resolved], bidi: &BidiInfo) -> Vec<CharInfo> {
|
||||
fn char_forms(text: &str, base: usize, runs: Runs, primaries: &[Resolved], bidi: &BidiInfo) -> Vec<CharInfo> {
|
||||
text.char_indices()
|
||||
.map(|(b, c)| {
|
||||
let si = run_at(runs, base + b);
|
||||
let style = &runs[si].1;
|
||||
let si = run_at(runs.list, runs.ordered, base + b);
|
||||
let style = &runs.list[si].1;
|
||||
let primary = primaries[si].face;
|
||||
let face_of = |ch: char| if ch.is_whitespace() { primary } else { fonts::fallback_for(ch, primary) };
|
||||
let all_small = style.opentype.all_small_caps && style.caps != Caps::All;
|
||||
@@ -1079,17 +1101,9 @@ fn build_items(text: &str, chars: &[CharInfo], runs: &[(Range<usize>, TextStyle)
|
||||
items
|
||||
}
|
||||
|
||||
fn paragraph(
|
||||
text: &str,
|
||||
base: usize,
|
||||
runs: &[(Range<usize>, TextStyle)],
|
||||
primaries: &[Resolved],
|
||||
variation_ids: &[u32],
|
||||
para: &ParagraphStyle,
|
||||
width: Option<f32>,
|
||||
) -> Vec<ParaLine> {
|
||||
fn paragraph(text: &str, base: usize, runs: Runs, primaries: &[Resolved], variation_ids: &[u32], para: &ParagraphStyle, width: Option<f32>) -> Vec<ParaLine> {
|
||||
if text.is_empty() {
|
||||
let si = run_at(runs, base);
|
||||
let (runs, si) = (runs.list, run_at(runs.list, runs.ordered, base));
|
||||
let pm = style_metrics(primaries, runs, si);
|
||||
// No text to resolve a direction from: the explicit one.
|
||||
let rtl = para.rtl == Some(true);
|
||||
@@ -1167,18 +1181,20 @@ pub fn layout_rich_uncached(text: &str, runs: &[(Range<usize>, TextStyle)], para
|
||||
});
|
||||
let runs = cleared.as_deref().unwrap_or(runs);
|
||||
let variation_ids: Vec<u32> = runs.iter().map(|(_, s)| crate::variable::intern(&s.variations)).collect();
|
||||
let ordered = runs_ordered(runs);
|
||||
let mut lay = Layout { missing_font: primaries.iter().any(|p| p.missing), text_len: text.len(), ..Default::default() };
|
||||
let mut plines: Vec<(usize, ParaLine)> = Vec::new();
|
||||
for (pi, span) in paragraph_ranges(text).enumerate() {
|
||||
let p = text.get(span.bytes.clone()).unwrap_or_default();
|
||||
plines.extend(paragraph(p, span.bytes.start, runs, &primaries, &variation_ids, para_at(pi), width).into_iter().map(|l| (pi, l)));
|
||||
plines
|
||||
.extend(paragraph(p, span.bytes.start, Runs { list: runs, ordered }, &primaries, &variation_ids, para_at(pi), width).into_iter().map(|l| (pi, l)));
|
||||
}
|
||||
let mut baseline = 0.0f32;
|
||||
for (i, (pi, pl)) in plines.into_iter().enumerate() {
|
||||
let para = para_at(pi);
|
||||
if i == 0 {
|
||||
// Vertical type keeps its first column at the font ascent (not measured in AE).
|
||||
let vertical = runs.get(run_at(runs, pl.range.start)).is_some_and(|r| r.1.vertical)
|
||||
let vertical = runs.get(run_at(runs, ordered, pl.range.start)).is_some_and(|r| r.1.vertical)
|
||||
|| runs.iter().any(|r| r.1.vertical && r.0.start < pl.range.end && r.0.end > pl.range.start);
|
||||
baseline = if width.is_some() { if vertical { pl.ascent } else { pl.first } } else { 0.0 };
|
||||
} else {
|
||||
@@ -1198,7 +1214,7 @@ pub fn layout_rich_uncached(text: &str, runs: &[(Range<usize>, TextStyle)], para
|
||||
}));
|
||||
let x = shift + pl.left_trim;
|
||||
if let Some((u0, u1)) = pl.underline {
|
||||
let si = run_at(runs, pl.range.start);
|
||||
let si = run_at(runs, ordered, pl.range.start);
|
||||
let pm = fonts::face(primaries[si].face).metrics(runs[si].1.size);
|
||||
lay.underlines.push([u0 + shift, baseline + pm.underline_pos, u1 + shift, baseline + pm.underline_pos + pm.underline_thickness]);
|
||||
}
|
||||
|
||||
@@ -505,3 +505,29 @@ fn a_later_vertical_run_keeps_the_first_line_baseline() {
|
||||
let mixed = layout_rich_uncached(text, &[(0..4, horiz), (4..text.len(), vert)], &[p]);
|
||||
assert!((mixed.lines[0].baseline - plain.lines[0].baseline).abs() < 1e-3, "{} vs {}", mixed.lines[0].baseline, plain.lines[0].baseline);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn run_lookup_by_bisection_matches_the_scan() {
|
||||
let scan = |runs: &[(Range<usize>, TextStyle)], b: usize| {
|
||||
runs.iter().position(|(r, _)| b >= r.start && b < r.end).unwrap_or_else(|| runs.iter().rposition(|(r, _)| r.start <= b).unwrap_or(0))
|
||||
};
|
||||
let runs: Vec<(Range<usize>, TextStyle)> = (0..50).map(|i| (i * 3..i * 3 + 3, st(10.0))).collect();
|
||||
let gapped: Vec<(Range<usize>, TextStyle)> = (0..50).map(|i| (i * 4..i * 4 + 3, st(10.0))).collect();
|
||||
// Overlapping runs: the first run holding a byte wins, as in the scan.
|
||||
let overlapping: Vec<(Range<usize>, TextStyle)> = vec![(0..10, st(10.0)), (5..15, st(20.0)), (12..20, st(30.0))];
|
||||
assert!(runs_ordered(&runs) && runs_ordered(&gapped) && !runs_ordered(&overlapping));
|
||||
for b in 0..220 {
|
||||
assert_eq!(run_at(&runs, true, b), scan(&runs, b), "{b}");
|
||||
assert_eq!(run_at(&gapped, true, b), scan(&gapped, b), "{b}");
|
||||
assert_eq!(run_at(&overlapping, false, b), scan(&overlapping, b), "{b}");
|
||||
}
|
||||
assert_eq!(run_at(&overlapping, false, 6), 0);
|
||||
// A reversed range (20..0) among others is not ordered either, and takes the scan.
|
||||
#[allow(clippy::reversed_empty_ranges)]
|
||||
let reversed: Vec<(Range<usize>, TextStyle)> = vec![(0..10, st(10.0)), (20..0, st(20.0)), (5..15, st(30.0)), (15..20, st(40.0))];
|
||||
assert!(!runs_ordered(&reversed));
|
||||
for b in 0..30 {
|
||||
assert_eq!(run_at(&reversed, runs_ordered(&reversed), b), scan(&reversed, b), "{b}");
|
||||
}
|
||||
assert_eq!(run_at(&[], true, 5), 0);
|
||||
}
|
||||
@@ -19,7 +19,26 @@ impl PathMeasure {
|
||||
let mut closed = false;
|
||||
let mut started = false;
|
||||
let mut done = false;
|
||||
kurbo::flatten(path.iter(), 0.05, |el| {
|
||||
// The tolerance grows with the path's size (0.05 for ordinary layer-sized paths): a
|
||||
// path with huge coordinates would otherwise flatten into hundreds of millions of points.
|
||||
// Only the first subpath is measured, so only it sets the tolerance and gets flattened.
|
||||
let mut first = BezPath::new();
|
||||
for el in path.iter() {
|
||||
if matches!(el, PathEl::MoveTo(_)) && !first.elements().is_empty() {
|
||||
break;
|
||||
}
|
||||
first.push(el);
|
||||
if matches!(el, PathEl::ClosePath) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
// A non-finite coordinate (an infinite control point too) measures nothing.
|
||||
if !first.is_finite() {
|
||||
return PathMeasure::default();
|
||||
}
|
||||
let bbox = kurbo::Shape::bounding_box(&first);
|
||||
let size = bbox.width().max(bbox.height());
|
||||
kurbo::flatten(first.iter(), 0.05_f64.max(size * 1e-6), |el| {
|
||||
if done {
|
||||
return;
|
||||
}
|
||||
@@ -128,6 +147,38 @@ pub fn arc_positions(xs: &[f64], line: [f64; 2], align: u8, length: f64, first_m
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn a_huge_curve_flattens_into_a_bounded_number_of_points() {
|
||||
let mut p = BezPath::new();
|
||||
p.move_to((0.0, 0.0));
|
||||
p.curve_to((1e15, 0.0), (1e15, 1e15), (0.0, 1e15));
|
||||
let m = PathMeasure::new(&p, false);
|
||||
assert!(m.pts.len() < 100_000, "{}", m.pts.len());
|
||||
assert!(m.length() > 1e15);
|
||||
// Non-finite coordinates measure nothing.
|
||||
let mut q = BezPath::new();
|
||||
q.move_to((0.0, 0.0));
|
||||
q.curve_to((f64::INFINITY, 0.0), (1.0, 1.0), (2.0, 0.0));
|
||||
assert_eq!(PathMeasure::new(&q, false).pts.len(), 0);
|
||||
// Even with finite endpoints, an infinite control point is refused before flattening.
|
||||
let mut r = BezPath::new();
|
||||
r.move_to((0.0, 0.0));
|
||||
r.curve_to((f64::INFINITY, f64::INFINITY), (f64::INFINITY, 0.0), (1.0, 0.0));
|
||||
assert_eq!(PathMeasure::new(&r, false).pts.len(), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_distant_second_subpath_does_not_coarsen_the_first() {
|
||||
let mut p = BezPath::new();
|
||||
p.move_to((0.0, 0.0));
|
||||
p.curve_to((0.0, 100.0), (100.0, 100.0), (100.0, 0.0));
|
||||
p.move_to((1e9, 1e9));
|
||||
p.line_to((1e9 + 1.0, 1e9));
|
||||
let m = PathMeasure::new(&p, false);
|
||||
assert!(m.pts.len() > 10, "{}", m.pts.len());
|
||||
assert!(m.length() > 150.0, "{}", m.length());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn measures_a_line_and_a_circle() {
|
||||
let mut p = BezPath::new();
|
||||
|
||||
@@ -257,7 +257,12 @@ fn name_string(b: &[u8], id: u16) -> Option<String> {
|
||||
// Best rank first; a blank record falls through to the next. Decoding is lazy, so aliased
|
||||
// records are not decoded once a usable one is found.
|
||||
cands.sort_by_key(|c| c.0);
|
||||
// Records may alias one string: each (encoding, bytes) is decoded at most once.
|
||||
let mut tried = std::collections::HashSet::new();
|
||||
cands.into_iter().find_map(|(_, plat, enc, raw)| {
|
||||
if !tried.insert((plat, enc, raw.as_ptr(), raw.len())) {
|
||||
return None;
|
||||
}
|
||||
let s = match (plat, enc) {
|
||||
(3, 1 | 10) | (0, _) => {
|
||||
let u: Vec<u16> = raw.as_chunks::<2>().0.iter().map(|c| u16::from_be_bytes([c[0], c[1]])).collect();
|
||||
|
||||
@@ -485,6 +485,14 @@ fn adjust_impl(cams: &mut [BaCam], pts: &mut [V3], obs: &[BaObs], o: &BaOpts, al
|
||||
let build_point = |j: usize, s_acc: &mut Vec<f64>, bc: &mut Vec<f64>| -> PtSys {
|
||||
let mut ps = PtSys { v: [[0.0; 3]; 3], bp: [0.0; 3], w: vec![] };
|
||||
let mut jc = CameraJacobian::new();
|
||||
// Observations that repeat a camera add to the same parameters: combine them, so the
|
||||
// point's entries stay at most one per parameter (the reduction is quadratic in them).
|
||||
let mut cams_seen: Vec<u32> = by_pt[j].iter().map(|&oi| obs[oi as usize].cam).collect();
|
||||
cams_seen.sort_unstable();
|
||||
let repeated_camera = cams_seen.windows(2).any(|p| p[0] == p[1]);
|
||||
// Parameter index → entry in `ps.w`, for points that repeat a camera (a linear search
|
||||
// per entry would be quadratic in their observations).
|
||||
let mut slot: std::collections::HashMap<usize, usize> = std::collections::HashMap::new();
|
||||
for &oi in &by_pt[j] {
|
||||
let ob = &obs[oi as usize];
|
||||
let ci = ob.cam as usize;
|
||||
@@ -537,6 +545,16 @@ fn adjust_impl(cams: &mut [BaCam], pts: &mut [V3], obs: &[BaObs], o: &BaOpts, al
|
||||
// Each observation of a point is on another camera: only the shared focal
|
||||
// length repeats.
|
||||
let repeats = Some(*ia) == l.shared || l.dist.is_some_and(|g| *ia == g || *ia == g + 1);
|
||||
if repeated_camera {
|
||||
match slot.get(ia) {
|
||||
Some(&k) => ps.w[k].1 = add(ps.w[k].1, wv),
|
||||
None => {
|
||||
slot.insert(*ia, ps.w.len());
|
||||
ps.w.push((*ia, wv));
|
||||
}
|
||||
}
|
||||
continue;
|
||||
}
|
||||
let found = if repeats { ps.w.iter_mut().find(|e| e.0 == *ia) } else { None };
|
||||
match found {
|
||||
Some(e) => e.1 = add(e.1, wv),
|
||||
@@ -820,6 +838,25 @@ mod tests {
|
||||
BaCam { pose: Pose::IDENTITY, f: 900.0, dist: Distortion::NONE }
|
||||
}
|
||||
|
||||
/// Many observations of one point by one camera combine into one contribution per parameter:
|
||||
/// the point's reduction used to compare every duplicate with every later one.
|
||||
#[test]
|
||||
fn repeated_observations_of_one_camera_are_combined() {
|
||||
let cams = [test_camera(), BaCam { pose: Pose { c: [1.0, 0.0, 0.0], ..Pose::IDENTITY }, ..test_camera() }];
|
||||
let truth = [0.2, -0.1, 5.0];
|
||||
let mut obs = Vec::new();
|
||||
for _ in 0..30_000 {
|
||||
for (cam, c) in cams.iter().enumerate() {
|
||||
let uv = residual(c, truth, [0.0; 2]).unwrap();
|
||||
obs.push(BaObs { cam: cam as u32, pt: 0, uv });
|
||||
}
|
||||
}
|
||||
let (mut cams, mut pts) = (cams, [[0.25, -0.05, 5.2]]);
|
||||
let o = BaOpts::default();
|
||||
let r = adjust(&mut cams, &mut pts, &obs, &o).unwrap();
|
||||
assert!(r.cost <= r.initial_cost, "{r:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cost_rejects_invalid_observation_indices() {
|
||||
for (cam, pt) in [(1, 0), (0, 1)] {
|
||||
|
||||
@@ -932,7 +932,10 @@ fn solve_core(tracks: &CameraTracks, s: &SolveSettings, cancel: Option<&AtomicBo
|
||||
}
|
||||
lap("keyframe adjustment");
|
||||
// Every other frame: resect from the points (nearest registered camera first).
|
||||
let mut todo: Vec<usize> = (0..d.n).filter(|k| !r.cams.contains_key(k)).collect();
|
||||
// A frame seeing fewer than six reconstructed points cannot be resected (`register`): it is
|
||||
// skipped before the reconstruction is copied for it.
|
||||
let mut todo: Vec<usize> =
|
||||
(0..d.n).filter(|k| !r.cams.contains_key(k) && d.by_frame[*k].iter().filter(|(ti, _)| r.pts.contains_key(ti)).nth(5).is_some()).collect();
|
||||
todo.sort_by_key(|k| r.cams.keys().map(|c| c.abs_diff(*k)).min().unwrap_or(usize::MAX));
|
||||
let base = r.clone();
|
||||
let resected: Vec<(usize, Option<BaCam>)> = todo
|
||||
|
||||
@@ -558,16 +558,19 @@ fn fit_from_face(face: &Face, topo: &Topology, offset: [f64; 2]) -> Option<FaceF
|
||||
fn nearest_skin(skin: &[bool], size: (usize, usize), center: (usize, usize), radius: i64) -> Option<(usize, usize)> {
|
||||
let (w, h) = size;
|
||||
let (ci, cj) = center;
|
||||
// The radius comes from mask geometry: past the window's larger side every ring is outside it.
|
||||
let radius = radius.min(w.max(h) as i64 + 1);
|
||||
for r in 0..radius {
|
||||
for dj in -r..=r {
|
||||
for di in -r..=r {
|
||||
if di.abs() != r && dj.abs() != r {
|
||||
continue;
|
||||
}
|
||||
// Only the ring's own pixels: the full rows at the top and bottom, the two ends between.
|
||||
let step = if dj.abs() == r { 1 } else { (2 * r).max(1) };
|
||||
let mut di = -r;
|
||||
while di <= r {
|
||||
let (i, j) = (ci as i64 + di, cj as i64 + dj);
|
||||
if i >= 0 && j >= 0 && (i as usize) < w && (j as usize) < h && skin[j as usize * w + i as usize] {
|
||||
return Some((i as usize, j as usize));
|
||||
}
|
||||
di += step;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1187,6 +1190,16 @@ pub fn measure(l: &[[f64; 2]; N], reference_height: f64) -> [f64; 14] {
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn the_skin_search_stays_within_the_window_for_a_huge_radius() {
|
||||
let mut skin = vec![false; 64 * 64];
|
||||
skin[60 * 64 + 5] = true;
|
||||
// A radius from hostile mask geometry costs no more than one that covers the window.
|
||||
assert_eq!(nearest_skin(&skin, (64, 64), (32, 32), i64::MAX), Some((5, 60)));
|
||||
assert_eq!(nearest_skin(&skin, (64, 64), (5, 60), 3), Some((5, 60)));
|
||||
assert_eq!(nearest_skin(&skin, (64, 64), (0, 0), 10), None);
|
||||
}
|
||||
|
||||
/// Pearson correlation.
|
||||
fn corr(a: &[f64], b: &[f64]) -> f64 {
|
||||
let n = a.len() as f64;
|
||||
|
||||
@@ -359,6 +359,9 @@ impl<'a> Scorer<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
/// The largest pre-tracking blur (pixels), as the options command allows.
|
||||
const MAX_BLUR: f64 = 100.0;
|
||||
|
||||
/// The pre-processed pixels around one point: a pyramid of a crop. Plane coordinates are image
|
||||
/// pixels minus `origin`, divided by `2^level`.
|
||||
struct Crop {
|
||||
@@ -369,7 +372,10 @@ struct Crop {
|
||||
impl Crop {
|
||||
/// Crop image pixels `[c - ext, c + ext]` (image pixel coordinates), pre-processed.
|
||||
fn new(frame: &Frame, opts: &TrackOptions, c: [f64; 2], ext: [f64; 2], levels: usize) -> Crop {
|
||||
let sigma = opts.blur.max(0.0) / 2.0;
|
||||
// A stored blur or search area past the frame adds only repeated edge pixels: both are
|
||||
// held to what the frame can use, so a few project numbers cannot size a huge crop.
|
||||
let sigma = opts.blur.clamp(0.0, MAX_BLUR) / 2.0;
|
||||
let ext = [ext[0].clamp(0.0, frame.img.width as f64), ext[1].clamp(0.0, frame.img.height as f64)];
|
||||
let margin = (sigma * 3.0).ceil() + 3.0 + if opts.enhance { 6.0 } else { 0.0 } + (1 << levels) as f64;
|
||||
let x0 = (c[0] - ext[0] - margin).floor();
|
||||
let y0 = (c[1] - ext[1] - margin).floor();
|
||||
|
||||
@@ -80,6 +80,10 @@ impl StrokeKind {
|
||||
}
|
||||
}
|
||||
|
||||
/// The most frames a propagation chain reaches on either side of its base frame (about two
|
||||
/// hours at 24 fps).
|
||||
const MAX_CHAIN: i64 = 200_000;
|
||||
|
||||
/// One stroke: a polyline swept by a disc of `radius` layer pixels, on layer frame `frame`.
|
||||
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
@@ -151,8 +155,16 @@ impl RotoData {
|
||||
pub fn strokes_of(&self, frame: i64, object: u32) -> Vec<&Stroke> {
|
||||
self.strokes.iter().filter(|s| s.frame == frame && (s.object == object || matches!(s.kind, StrokeKind::Refine | StrokeKind::RefineErase))).collect()
|
||||
}
|
||||
/// The stored span cut to at most MAX_CHAIN frames either side of the base: a project's
|
||||
/// numbers must not make one render build billions of keys. Frames past it have no key.
|
||||
fn bounded_span(&self, base: i64) -> [i64; 2] {
|
||||
[self.span[0].max(base.saturating_sub(MAX_CHAIN)), self.span[1].min(base.saturating_add(MAX_CHAIN))]
|
||||
}
|
||||
pub fn in_span(&self, f: i64) -> bool {
|
||||
self.base.is_some() && f >= self.span[0] && f <= self.span[1]
|
||||
self.base.is_some_and(|b| {
|
||||
let span = self.bounded_span(b);
|
||||
f >= span[0] && f <= span[1]
|
||||
})
|
||||
}
|
||||
/// Add a stroke: the first segmentation stroke sets the base frame and a span of
|
||||
/// `default_span` frames each side (clamped to `limits`); strokes outside the span extend it.
|
||||
@@ -181,10 +193,15 @@ impl RotoData {
|
||||
pub fn chain_keys(&self, seed: u64) -> BTreeMap<i64, u64> {
|
||||
let mut out = BTreeMap::new();
|
||||
let Some(base) = self.base else { return out };
|
||||
// The strokes by frame, grouped once (each frame looked through every stroke before).
|
||||
let mut by_frame: BTreeMap<i64, Vec<&Stroke>> = BTreeMap::new();
|
||||
for s in &self.strokes {
|
||||
by_frame.entry(s.frame).or_default().push(s);
|
||||
}
|
||||
let key = |prev: u64, f: i64| {
|
||||
let mut h = prev ^ 0x9e37_79b9_7f4a_7c15;
|
||||
fnv(&mut h, &f.to_le_bytes());
|
||||
for s in self.strokes.iter().filter(|s| s.frame == f) {
|
||||
for s in by_frame.get(&f).into_iter().flatten() {
|
||||
fnv(&mut h, serde_json::to_string(s).unwrap_or_default().as_bytes());
|
||||
}
|
||||
h
|
||||
@@ -192,12 +209,16 @@ impl RotoData {
|
||||
let k0 = key(seed, base);
|
||||
out.insert(base, k0);
|
||||
let mut k = k0;
|
||||
for f in base + 1..=self.span[1] {
|
||||
k = key(k, f);
|
||||
out.insert(f, k);
|
||||
let span = self.bounded_span(base);
|
||||
// `base + 1` only when there is a frame after the base (it overflows at i64::MAX).
|
||||
if base < span[1] {
|
||||
for f in base + 1..=span[1] {
|
||||
k = key(k, f);
|
||||
out.insert(f, k);
|
||||
}
|
||||
}
|
||||
k = k0;
|
||||
for f in (self.span[0]..base).rev() {
|
||||
for f in (span[0]..base).rev() {
|
||||
k = key(k, f);
|
||||
out.insert(f, k);
|
||||
}
|
||||
|
||||
@@ -723,6 +723,29 @@ fn the_union_of_objects_is_their_largest_extent() {
|
||||
assert_eq!(FrameSeg::union(vec![seg(&[1]), odd]).matte, vec![0, 255, 0, 0]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_stored_span_reaches_a_bounded_number_of_frames() {
|
||||
// A project can store any span: the chain stops MAX_CHAIN frames either side of the base.
|
||||
let d = RotoData { base: Some(0), span: [i64::MIN, i64::MAX], strokes: vec![] };
|
||||
let k = d.chain_keys(1);
|
||||
assert_eq!(k.len() as i64, 2 * super::MAX_CHAIN + 1);
|
||||
assert_eq!(k.keys().next(), Some(&-super::MAX_CHAIN));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn in_span_matches_the_frames_that_have_keys_and_extreme_bases_do_not_overflow() {
|
||||
let d = RotoData { base: Some(0), span: [i64::MIN, i64::MAX], strokes: vec![] };
|
||||
assert!(d.in_span(super::MAX_CHAIN) && !d.in_span(super::MAX_CHAIN + 1));
|
||||
assert!(d.in_span(-super::MAX_CHAIN) && !d.in_span(-super::MAX_CHAIN - 1));
|
||||
assert_eq!(d.source_of(super::MAX_CHAIN + 1), None);
|
||||
assert!(d.chain_keys(1).keys().all(|f| d.in_span(*f)));
|
||||
// A base at the end of i64: no forward frames, and no `base + 1` overflow.
|
||||
let d = RotoData { base: Some(i64::MAX), span: [i64::MIN, i64::MAX], strokes: vec![] };
|
||||
let k = d.chain_keys(1);
|
||||
assert_eq!(k.len() as i64, super::MAX_CHAIN + 1);
|
||||
assert!(k.contains_key(&i64::MAX));
|
||||
}
|
||||
|
||||
/// A scene for the band re-cut: `rgb` of a `w`×`h` frame where the foreground is `inside`, with
|
||||
/// a background that turns red left of `x = 30` (far from most shapes), and a soft prior that is
|
||||
/// `inside` shifted by (3, 2) px with an edge `soft_edge` px wide.
|
||||
|
||||
Loaded 100 of 113 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user