mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-25 11:05:03 +00:00
Merge main into feat/proof-setup-customize
This commit is contained in:
@@ -58,6 +58,15 @@ pub fn drawn_bounds(doc: &Document, n: &Node) -> Option<Rect> {
|
||||
fx::painted_reach(&doc.symbol_boxes(), n)
|
||||
}
|
||||
|
||||
/// For a path or compound path whose own Rasterize effect is followed by Transform effects that
|
||||
/// move the art once ([`crate::effects::rasterize_stage`]): `n` with those Transform effects
|
||||
/// hidden, which the image is made of, and the map they move the image by. The canvas draws the
|
||||
/// image on its own pixel grid and moves it, and the room around the art, with the map. `None`
|
||||
/// for other objects.
|
||||
pub fn rasterize_stage(doc: &Document, n: &Node) -> Option<(Node, vectorcraft_geom::Affine)> {
|
||||
fx::rasterize_stage(doc, n)
|
||||
}
|
||||
|
||||
/// The outline, with `n`'s geometry effects applied, that the canvas clips `n`'s Rasterize with
|
||||
/// Create Clipping Mask to. Object › Rasterize's clipping mask follows it.
|
||||
pub fn drawn_outline(doc: &Document, n: &Node) -> Option<(BezPath, FillRule)> {
|
||||
|
||||
@@ -72,7 +72,7 @@ mod renderer;
|
||||
pub(crate) use bounds::visual_bounds;
|
||||
pub(crate) use bounds::{
|
||||
brushed_cull_bounds, cull_bounds, drawn_outline, drawn_outlines, painted_brush_width, painted_cull_bounds, painted_reach, path_cull_bounds,
|
||||
three_d_art_bounds, with_own_raster_reach,
|
||||
rasterize_stage, three_d_art_bounds, with_own_raster_reach,
|
||||
};
|
||||
#[cfg(test)]
|
||||
pub(crate) use entries::Drawn;
|
||||
|
||||
@@ -6,7 +6,7 @@ use super::{
|
||||
};
|
||||
use vectorcraft_doc::symbol_overrides::dynamic_art;
|
||||
|
||||
fn node_bezpath(n: &Node) -> Option<BezPath> {
|
||||
pub(super) fn node_bezpath(n: &Node) -> Option<BezPath> {
|
||||
match &n.kind {
|
||||
NodeKind::Path { path, .. } => Some(path.to_bezpath()),
|
||||
NodeKind::Compound { children, .. } => {
|
||||
@@ -48,6 +48,34 @@ pub(crate) fn effected_path_carrying(doc: &Document, n: &Node, bp: &BezPath) ->
|
||||
apply_carrying(&n.appearance.effects, bp, &GeomContext { doc: Some(doc), ..GeomContext::of(n) })
|
||||
}
|
||||
|
||||
/// `n`, a path or compound path whose own Rasterize effect is followed by Transform effects that
|
||||
/// move its image ([`effects::rasterize_stage`]), with those Transform effects hidden, and the map
|
||||
/// they move the image by ([`effects::stage_map`]): the image is made of that node's art on its
|
||||
/// own pixel grid, then drawn through the map. `None` for any other object, when the node without
|
||||
/// those Transform effects is drawn through its art ([`has_object_fx`]: a Transform effect that
|
||||
/// copies it, Crop Marks, a 3D effect…), and when the art before the Transform effects is empty.
|
||||
pub(crate) fn rasterize_stage(doc: &Document, n: &Node) -> Option<(Node, Affine)> {
|
||||
if !has_fx(n) {
|
||||
return None;
|
||||
}
|
||||
let at = effects::rasterize_stage(&n.appearance.effects)?;
|
||||
let mut stage = n.clone();
|
||||
stage.appearance.effects = effects::staged_effects(&n.appearance.effects, &at);
|
||||
if has_object_fx(&stage) {
|
||||
return None;
|
||||
}
|
||||
let g = effected_path(doc, &stage, &node_bezpath(n)?);
|
||||
let map = effects::stage_map(&n.appearance.effects, &at, &vectorcraft_geom::PathData::from_bezpath(&g))?;
|
||||
Some((stage, map))
|
||||
}
|
||||
|
||||
/// The box `map` ([`rasterize_stage`]) takes `b` to; `None` when it flattens it to a line or a
|
||||
/// point, which leaves nothing to draw.
|
||||
pub(crate) fn staged_reach(map: Affine, b: Rect) -> Option<Rect> {
|
||||
let det = map.determinant();
|
||||
(det.is_finite() && det.abs() > 1e-12).then(|| map.transform_rect_bbox(b))
|
||||
}
|
||||
|
||||
pub(super) fn item_effects(item: &AppearanceItem) -> &[Effect] {
|
||||
match item {
|
||||
AppearanceItem::Fill(f) => &f.effects,
|
||||
@@ -74,6 +102,11 @@ pub(crate) fn visual_bounds(symbols: &SymbolBoxes, n: &Node) -> Option<Rect> {
|
||||
{
|
||||
return cull_bounds(symbols, &art);
|
||||
}
|
||||
// A Rasterize image moved by the Transform effects after it reaches as far as the map takes
|
||||
// what the art before them reaches, the image included.
|
||||
if let Some((stage, map)) = rasterize_stage(symbols.document(), n) {
|
||||
return staged_reach(map, visual_bounds(symbols, &stage)?);
|
||||
}
|
||||
let bp = node_bezpath(n)?;
|
||||
let ctx = GeomContext { doc: Some(symbols.document()), ..GeomContext::of(n) };
|
||||
let (g, carry) = effected_path_carrying(symbols.document(), n, &bp);
|
||||
@@ -449,6 +482,9 @@ pub(crate) fn painted_reach(symbols: &SymbolBoxes, n: &Node) -> Option<Rect> {
|
||||
if let Some(art) = effects::crop_marks_art(n) {
|
||||
return drawn_bounds(symbols, &art);
|
||||
}
|
||||
if let Some((stage, map)) = rasterize_stage(symbols.document(), n) {
|
||||
return staged_reach(map, painted_reach(symbols, &stage)?);
|
||||
}
|
||||
let ran = ran(symbols.document(), n);
|
||||
let b = drawn_bounds_ran(symbols, n, Some(&ran))?;
|
||||
Some(spread(symbols, &ran.appearance.effects, b))
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
use super::bounds::{cull_bounds, effected_path_carrying};
|
||||
use super::bounds::{cull_bounds, effected_path_carrying, node_bezpath, rasterize_stage};
|
||||
use super::entries::{Content, Drawn, FX_RASTER_BUDGET, FxGeom, Inputs, ItemGeom, Kept, Made, Reads, Shadow, ShadowEntry};
|
||||
use super::predicates::{evaluates_live, is_container, rasterize_bounds, reads_outside, shifted};
|
||||
use super::raster::{
|
||||
@@ -330,6 +330,11 @@ impl Renderer {
|
||||
|
||||
/// `draw_shape` for nodes with live effects.
|
||||
pub(crate) fn draw_shape_fx(&mut self, ctx: &mut RenderContext, f: &Frame, n: &Node, bp: &BezPath, rule: FillRule) {
|
||||
if !f.opts.outline
|
||||
&& let Some(stage) = self.rasterize_stage_of(f, n, self.fx_owner.as_ref().filter(|o| std::ptr::eq(&***o, n)).cloned())
|
||||
{
|
||||
return self.draw_staged(ctx, f, stage, bp, rule);
|
||||
}
|
||||
// The node `draw_arc` is drawing gets its geometry from the cache: `bp` is its own path,
|
||||
// made from the node alone, so the effects give the same result every frame while the
|
||||
// plug-ins installed stay the same (`check_plugins`).
|
||||
@@ -360,6 +365,47 @@ impl Renderer {
|
||||
}
|
||||
}
|
||||
|
||||
/// [`rasterize_stage`] of `n`, kept across frames when `owner` is `n` as held in its document
|
||||
/// (then `true` in the third place: the stage is kept, and so are its caches).
|
||||
fn rasterize_stage_of(&mut self, f: &Frame, n: &Node, owner: Option<Arc<Node>>) -> Option<(Arc<Node>, Affine, bool)> {
|
||||
// Checked first: this runs for every path with effects drawn.
|
||||
effects::rasterize_stage(&n.appearance.effects)?;
|
||||
let Some(owner) = owner else {
|
||||
return rasterize_stage(f.symbols.document(), n).map(|(stage, map)| (Arc::new(stage), map, false));
|
||||
};
|
||||
let key = Arc::as_ptr(&owner) as usize;
|
||||
let stamp = self.stamp;
|
||||
if let Some(e) = self.fx_stages.get_mut(&key)
|
||||
&& Arc::ptr_eq(&e.0, &owner)
|
||||
{
|
||||
e.3 = stamp;
|
||||
return Some((e.1.clone(), e.2, true));
|
||||
}
|
||||
let (stage, map) = rasterize_stage(f.symbols.document(), n)?;
|
||||
let stage = Arc::new(stage);
|
||||
self.fx_stages.insert(key, (owner, stage.clone(), map, stamp));
|
||||
Some((stage, map, true))
|
||||
}
|
||||
|
||||
/// Draw `stage`, the node a path's Rasterize image is made from ([`rasterize_stage`]), through
|
||||
/// `map`, the Transform effects after that Rasterize effect: its art and its image, on the
|
||||
/// image's own pixel grid, move with the map. A map that flattens the art leaves nothing to
|
||||
/// draw. `bp` is the path's own path; with `kept`, `stage` is kept across frames and so are
|
||||
/// the geometry and rasters drawn for it.
|
||||
fn draw_staged(&mut self, ctx: &mut RenderContext, f: &Frame, (stage, map, kept): (Arc<Node>, Affine, bool), bp: &BezPath, rule: FillRule) {
|
||||
let det = map.determinant();
|
||||
if !(det.is_finite() && det.abs() > 1e-12) {
|
||||
return;
|
||||
}
|
||||
// A pixel's size in document units through the view and the map, measured as a frame
|
||||
// measures it from its view.
|
||||
let px = f.px / det.abs().sqrt();
|
||||
let fr = Frame { view: f.view * map, visible: map.inverse().transform_rect_bbox(f.visible), px, output: f.output * map, ..*f };
|
||||
let outer = if kept { self.fx_owner.replace(stage.clone()) } else { self.fx_owner.take() };
|
||||
self.draw_shape_fx(ctx, &fr, &stage, bp, rule);
|
||||
self.fx_owner = outer;
|
||||
}
|
||||
|
||||
/// [`Self::draw_shape_fx`] on the effected geometry `g`, with the items' cached geometry:
|
||||
/// `paints` is `n` with the fills and strokes it paints.
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
@@ -417,6 +463,15 @@ impl Renderer {
|
||||
if !self.charge_fx_instance(f.doc, a) {
|
||||
return;
|
||||
}
|
||||
// A path whose Transform effects after its Rasterize effect hold options that reach its
|
||||
// fills and strokes: its image moves with them as for any other path.
|
||||
if !f.opts.outline
|
||||
&& let NodeKind::Path { rule, .. } | NodeKind::Compound { rule, .. } = &a.kind
|
||||
&& let Some(stage) = self.rasterize_stage_of(f, a, cache.then(|| a.clone()))
|
||||
&& let Some(bp) = node_bezpath(a)
|
||||
{
|
||||
return self.draw_staged(ctx, f, stage, &bp, *rule);
|
||||
}
|
||||
let art = self.fx_art(f.doc, a, cache);
|
||||
// Art the cache did not keep is made again in the next frame.
|
||||
let kept = cache && self.fx_arts.get(&(Arc::as_ptr(a) as usize), self.stamp).is_some_and(|e| Arc::ptr_eq(&e.1, &art));
|
||||
|
||||
@@ -290,7 +290,7 @@ impl Sub {
|
||||
/// them.
|
||||
fn moved(&mut self, was: &SubPath, now: &SubPath, els: &mut [PathEl], st: Option<&StrokeLayer>) {
|
||||
let (mut first, mut segs) = (false, vec![]);
|
||||
for (k, (a, b)) in was.anchors.iter().zip(&now.anchors).enumerate() {
|
||||
for (k, a, b) in was.anchors.pairs_not_shared(&now.anchors) {
|
||||
if same(a, b) {
|
||||
continue;
|
||||
}
|
||||
@@ -489,7 +489,7 @@ fn differs(was: &PathData, now: &PathData) -> bool {
|
||||
!std::ptr::eq(was, now)
|
||||
&& (was.subpaths.len() != now.subpaths.len()
|
||||
|| was.subpaths.iter().zip(&now.subpaths).any(|(w, p)| {
|
||||
w.closed != p.closed || w.anchors.len() != p.anchors.len() || w.anchors.iter().zip(&p.anchors).any(|(a, b)| !same(a, b))
|
||||
w.closed != p.closed || w.anchors.len() != p.anchors.len() || w.anchors.pairs_not_shared(&p.anchors).any(|(_, a, b)| !same(a, b))
|
||||
}))
|
||||
}
|
||||
|
||||
|
||||
@@ -125,6 +125,10 @@ pub struct Renderer {
|
||||
live_root: Option<Arc<Node>>,
|
||||
/// Live-effect geometry per node ([`fx::FxGeom`]), keyed by `Arc` identity like `geom`.
|
||||
fx_geom: PtrMap<usize, fx::FxGeom>,
|
||||
/// Per node whose Rasterize image moves with the Transform effects after it, keyed by `Arc`
|
||||
/// identity like `fx_geom`: the node, the node it draws its image from and the map that moves
|
||||
/// the image ([`fx::rasterize_stage`]), and the frame it was last drawn in.
|
||||
fx_stages: PtrMap<usize, (Arc<Node>, Arc<Node>, Affine, u64)>,
|
||||
stamp: u64,
|
||||
/// Opacity folded into paint alpha for the leaf being drawn (avoids a compositing layer).
|
||||
alpha: f32,
|
||||
@@ -274,7 +278,7 @@ mod renderer;
|
||||
mod text_geom;
|
||||
mod threads;
|
||||
|
||||
pub use bounds::{drawn_bounds, drawn_outline, drawn_outlines, effect_bounds, painted_bounds};
|
||||
pub use bounds::{drawn_bounds, drawn_outline, drawn_outlines, effect_bounds, painted_bounds, rasterize_stage};
|
||||
pub use frame::FrameStats;
|
||||
use frame::{
|
||||
ClipEntry, ClipPaintEntry, Frame, FxArtEntry, GeomEntry, PlacedEntry, PlacedKey, PtrHasher, PtrMap, SliceEntry, StrokeEntry, StrokeKey,
|
||||
|
||||
@@ -56,6 +56,7 @@ impl Renderer {
|
||||
fx_owner: None,
|
||||
live_root: None,
|
||||
fx_geom: PtrMap::default(),
|
||||
fx_stages: PtrMap::default(),
|
||||
stamp: 0,
|
||||
alpha: 1.0,
|
||||
contexts: Vec::new(),
|
||||
@@ -242,6 +243,9 @@ impl Renderer {
|
||||
if self.fx_geom.len() > 256 {
|
||||
self.fx_geom.retain(|_, e| g - e.stamp <= 3);
|
||||
}
|
||||
if self.fx_stages.len() > 256 {
|
||||
self.fx_stages.retain(|_, e| g - e.3 <= 3);
|
||||
}
|
||||
// Filtered rasters are large: only those of the last frame stay.
|
||||
self.pixel_fx.retain(|_, e| e.stamp == g);
|
||||
self.type_edges.sweep(g);
|
||||
|
||||
@@ -146,6 +146,7 @@ impl Renderer {
|
||||
}
|
||||
if stale {
|
||||
self.fx_geom.retain(|_, e| !effects::has_plugin_effect(&e.node));
|
||||
self.fx_stages.retain(|_, e| !effects::has_plugin_effect(&e.0));
|
||||
self.geom.retain(|_, e| !holds_plugin_effect(&e.node) && !holds_instance(&e.node));
|
||||
// The art of a group holds what its members' plug-in effects draw.
|
||||
self.fx_arts.retain(|(node, ..)| !holds_plugin_effect(node));
|
||||
|
||||
@@ -145,7 +145,7 @@ fn composite_font_parts_are_not_highlighted_as_substituted_glyphs() {
|
||||
let mut parts = CompositeParts::all(CompositePart::new("Source Sans 3", "Regular"));
|
||||
parts.roman = CompositePart::new("JetBrains Mono", "Regular");
|
||||
parts.numerals = CompositePart::new("Inter", "Regular");
|
||||
db.set_composite_font(&CompositeFont { name: "CF Highlight".into(), parts });
|
||||
db.set_composite_font(&CompositeFont { name: "CF Highlight".into(), parts, custom: Vec::new() });
|
||||
let style = CharStyle { size: 40.0, font_family: "CF Highlight".into(), ..Default::default() };
|
||||
let t = TextObject::point(Point::new(10.0, 60.0), "Ab12", style);
|
||||
let g = text_geom(&t);
|
||||
@@ -175,7 +175,7 @@ fn a_documents_own_composite_font_draws_without_being_registered() {
|
||||
let (mut composite, id) = typed(NAME);
|
||||
let mut parts = CompositeParts::all(CompositePart::new("Source Sans 3", "Regular"));
|
||||
parts.roman = CompositePart::new("JetBrains Mono", "Regular");
|
||||
composite.composite_fonts.push(CompositeFont { name: NAME.into(), parts });
|
||||
composite.composite_fonts.push(CompositeFont { name: NAME.into(), parts, custom: Vec::new() });
|
||||
let (plain, plain_id) = typed("JetBrains Mono");
|
||||
assert!(db.composite_font(NAME).is_none());
|
||||
let opts = RenderOptions { background: Some([255, 255, 255, 255]), ..Default::default() };
|
||||
@@ -236,7 +236,7 @@ fn a_placed_document_draws_its_own_definition_of_a_name_the_page_defines() {
|
||||
let definition = |roman: &str| {
|
||||
let mut parts = CompositeParts::all(CompositePart::new("Source Sans 3", "Regular"));
|
||||
parts.roman = CompositePart::new(roman, "Regular");
|
||||
CompositeFont { name: NAME.into(), parts }
|
||||
CompositeFont { name: NAME.into(), parts, custom: Vec::new() }
|
||||
};
|
||||
let typed = |family: &str| {
|
||||
let mut d = Document::new(300.0, 150.0);
|
||||
@@ -319,7 +319,7 @@ fn a_document_draws_type_in_another_documents_composite_font_as_missing() {
|
||||
};
|
||||
let mut parts = CompositeParts::all(CompositePart::new("Source Sans 3", "Regular"));
|
||||
parts.roman = CompositePart::new("JetBrains Mono", "Regular");
|
||||
db.set_composite_font(&CompositeFont { name: NAME.into(), parts });
|
||||
db.set_composite_font(&CompositeFont { name: NAME.into(), parts, custom: Vec::new() });
|
||||
let (drawn, as_missing, in_front) = (draw(&back, id), draw(&missing, missing_id), draw(&plain, plain_id));
|
||||
db.remove_composite_font(NAME);
|
||||
assert!(drawn.0 != in_front.0, "not in the front document's definition");
|
||||
|
||||
@@ -140,7 +140,7 @@ fn a_long_path_draws_and_culls_after_each_edit_as_when_its_path_is_made_again()
|
||||
}),
|
||||
("that anchor moved off it", |p| moved(&mut p.subpaths[0].anchors[1535], Vec2::new(5.0, 5.0))),
|
||||
("a doubled anchor moved off its twin", |p| moved(&mut p.subpaths[0].anchors[1536], Vec2::new(0.0, -12.0))),
|
||||
("1,200 anchors moved", |p| p.subpaths[0].anchors[..1200].iter_mut().for_each(|a| moved(a, Vec2::new(2.0, 1.0)))),
|
||||
("1,200 anchors moved", |p| p.subpaths[0].anchors.iter_mut().take(1200).for_each(|a| moved(a, Vec2::new(2.0, 1.0)))),
|
||||
("an anchor added", |p| _ = p.subpaths[0].insert_anchor(1520, 0.5)),
|
||||
("an anchor deleted", |p| _ = p.subpaths[0].anchors.remove(1521)),
|
||||
("the subpath closed", |p| p.subpaths[0].closed = true),
|
||||
@@ -454,7 +454,7 @@ fn random_edit(g: &mut Seeded, p: &mut PathData) -> String {
|
||||
match g.below(3) {
|
||||
0 => moved(&mut sp.anchors[at], Vec2::new(g.offset(30), g.offset(30))),
|
||||
1 => sp.anchors[at].h_out = sp.anchors[at].p + Vec2::new(g.offset(10), g.offset(10)),
|
||||
_ => sp.anchors[at..].iter_mut().take(n / 10).for_each(|a| moved(a, Vec2::new(1.0, -1.0))),
|
||||
_ => sp.anchors.iter_mut().skip(at).take(n / 10).for_each(|a| moved(a, Vec2::new(1.0, -1.0))),
|
||||
}
|
||||
format!("anchors moved at {at} of subpath {s} of {n}")
|
||||
}
|
||||
@@ -486,7 +486,7 @@ fn same_as_made_again(k: &mut crate::kept_paths::KeptPath, n: &Arc<Node>, st: &S
|
||||
fn random_edits_of_a_long_path_keep_what_making_its_path_again_makes() {
|
||||
let st = stroke(6.0, LineJoin::Miter, LineCap::Butt);
|
||||
let mut g = Seeded(5744);
|
||||
let closed = |sp: SubPath| SubPath::new(sp.anchors, true);
|
||||
let closed = |sp: SubPath| SubPath::of(sp.anchors, true);
|
||||
let path = PathData::new(vec![
|
||||
zigzag(2, 11_990.0),
|
||||
closed(zigzag(3, 12_000.0)),
|
||||
@@ -517,7 +517,7 @@ fn random_edits_of_a_long_compound_path_keep_what_making_its_path_again_makes()
|
||||
Arc::new(Node::path(NodeId(ids.get()), p, Appearance::default()))
|
||||
};
|
||||
let children = vec![
|
||||
member(PathData::new(vec![SubPath::new(zigzag(3, 11_990.0).anchors, true), zigzag(5, 12_010.0)])),
|
||||
member(PathData::new(vec![SubPath::of(zigzag(3, 11_990.0).anchors, true), zigzag(5, 12_010.0)])),
|
||||
member(PathData::single(zigzag(3000, 0.0))),
|
||||
member(PathData::single(ring(40, Point::new(12_000.0, 15.0), 30.0))),
|
||||
member(PathData::single(zigzag(2, 12_020.0))),
|
||||
|
||||
@@ -421,3 +421,99 @@ fn rasterize_fills_its_area_on_white_and_add_around_grows_it() {
|
||||
let stroked = draw(&stroked, 10.0, 400);
|
||||
assert_eq!((pixel(&stroked, 96, 105), pixel(&stroked, 93, 105)[3]), ([255; 4], 0));
|
||||
}
|
||||
|
||||
/// Rasterize params for hard-edged pixels a point wide, with `more`.
|
||||
fn hard(more: Value) -> Value {
|
||||
let mut p = json!({"useDocumentResolution": false, "resolution": 72, "antiAlias": "none"});
|
||||
if let (Some(p), Some(more)) = (p.as_object_mut(), more.as_object()) {
|
||||
p.extend(more.clone());
|
||||
}
|
||||
p
|
||||
}
|
||||
|
||||
/// A Transform effect after Rasterize enlarges the image it is given: each of the image's pixels
|
||||
/// is drawn twice as wide, and so is the room around the art.
|
||||
#[test]
|
||||
fn rasterize_then_scale_uses_the_pretransform_pixels() {
|
||||
for strokes in [false, true] {
|
||||
let scale = ("distort.transform", json!({"scaleH": 200, "scaleV": 200, "strokes": strokes}));
|
||||
let r = draw(&doc(triangle(), &[("rasterize", hard(json!({}))), scale.clone()], 72.0), 10.0, 400);
|
||||
// The 20 × 20 pixels of the image over 10..30 pt, each 2 pt wide over 0..40 pt.
|
||||
assert!(squares_are_flat(&r, 0, 20, 20), "Scale Strokes & Effects {strokes}");
|
||||
assert_eq!((pixel(&r, 10, 10), pixel(&r, 390, 390)[3]), ([255, 0, 0, 255], 0), "Scale Strokes & Effects {strokes}");
|
||||
// 2 pt of white around the art becomes 4 pt: from -4 pt.
|
||||
let framed = doc(triangle(), &[("rasterize", hard(json!({"background": "white", "addAround": 2}))), scale], 72.0);
|
||||
let opts = RenderOptions { background: None, artboards: false, ..Default::default() };
|
||||
let r = Renderer::new().render(&framed, 600, 600, Affine::translate((100.0, 100.0)) * Affine::scale(10.0), &opts);
|
||||
assert_eq!((pixel(&r, 65, 300), pixel(&r, 55, 300)[3]), ([255; 4], 0), "Scale Strokes & Effects {strokes}");
|
||||
// It is measured where it is drawn: the image over the 10..30 pt art and 2 pt around it,
|
||||
// twice as wide about its center.
|
||||
let n = framed.layers[0].children().unwrap()[0].clone();
|
||||
let b = crate::drawn_bounds(&framed, &n).unwrap();
|
||||
assert!((b.x0 + 4.0).abs() < 1e-9 && (b.x1 - 44.0).abs() < 1e-9, "{b:?}");
|
||||
assert!(crate::painted_bounds(&framed, &n).is_some_and(|p| p.x0 <= -4.0 && p.x1 >= 44.0));
|
||||
}
|
||||
}
|
||||
|
||||
/// A Transform effect after Rasterize turns, reflects or stretches the image it is given, its pixel
|
||||
/// grid included: the canvas draws what the art before the Transform effect draws, moved by the
|
||||
/// effect's map.
|
||||
#[test]
|
||||
fn rasterize_then_rotate_and_reflect_moves_the_pixel_grid() {
|
||||
let about = |m: Affine| Affine::translate((20.0, 20.0)) * m * Affine::translate((-20.0, -20.0));
|
||||
let cases = [
|
||||
(json!({"rotate": 30}), about(Affine::rotate(-30f64.to_radians()))),
|
||||
(json!({"reflectX": true}), about(Affine::scale_non_uniform(-1.0, 1.0))),
|
||||
(json!({"scaleH": 200, "scaleV": 100}), about(Affine::scale_non_uniform(2.0, 1.0))),
|
||||
];
|
||||
let opts = RenderOptions { background: None, artboards: false, ..Default::default() };
|
||||
let before = doc(triangle(), &[("rasterize", hard(json!({"background": "white", "addAround": 1})))], 72.0);
|
||||
for (params, map) in cases {
|
||||
let fx = [("rasterize", hard(json!({"background": "white", "addAround": 1}))), ("distort.transform", params.clone())];
|
||||
let r = draw(&doc(triangle(), &fx, 72.0), 10.0, 400);
|
||||
let want = Renderer::new().render(&before, 400, 400, Affine::scale(10.0) * map, &opts);
|
||||
let off = r.pixels.iter().zip(&want.pixels).filter(|(a, b)| a.abs_diff(**b) > 2).count();
|
||||
assert_eq!(off, 0, "{params}");
|
||||
}
|
||||
// Twice as wide: each pixel 20 × 10 device pixels.
|
||||
let r = draw(&doc(triangle(), &[("rasterize", hard(json!({}))), ("distort.transform", json!({"scaleH": 200, "scaleV": 100}))], 72.0), 10.0, 400);
|
||||
let flat = (0..20 * 20).all(|k| {
|
||||
let (x0, y0) = ((k % 20) * 20, 100 + (k / 20) * 10);
|
||||
(0..200).all(|i| pixel(&r, x0 + i % 20, y0 + i / 20) == pixel(&r, x0, y0))
|
||||
});
|
||||
assert!(flat);
|
||||
}
|
||||
|
||||
/// A Transform effect that scales the image to nothing leaves nothing to draw or measure.
|
||||
#[test]
|
||||
fn rasterize_then_nonuniform_scale_and_zero_scale_are_bounded() {
|
||||
let fx = [("rasterize", hard(json!({"background": "white"}))), ("distort.transform", json!({"scaleH": 0, "scaleV": 100}))];
|
||||
let d = doc(triangle(), &fx, 72.0);
|
||||
let r = draw(&d, 10.0, 400);
|
||||
assert!(r.pixels.chunks(4).all(|p| p[3] == 0));
|
||||
let n = d.layers[0].children().unwrap()[0].clone();
|
||||
assert_eq!(crate::drawn_bounds(&d, &n), None);
|
||||
// A huge scale enlarges where the image is drawn, not the image: 20 × 20 pixels still.
|
||||
let fx = [("rasterize", hard(json!({}))), ("distort.transform", json!({"scaleH": 10000, "scaleV": 10000}))];
|
||||
let drawn = || crate::fx::RASTERIZED.with(std::cell::Cell::get);
|
||||
let before = drawn();
|
||||
let opts = RenderOptions { background: None, artboards: false, ..Default::default() };
|
||||
let r = Renderer::new().render(&doc(triangle(), &fx, 72.0), 100, 100, Affine::translate((500.0, 500.0)), &opts);
|
||||
assert_eq!(drawn() - before, 1);
|
||||
// The view, around -450 pt, lies inside the image's pixel at 15..16 pt, drawn 100 pt wide
|
||||
// about the art's center at 20 pt.
|
||||
assert_eq!(pixel(&r, 50, 50), [255, 0, 0, 255]);
|
||||
}
|
||||
|
||||
/// A hidden Transform effect, or one with Transform Objects off, leaves the image where it is.
|
||||
#[test]
|
||||
fn rasterize_before_a_hidden_transform_keeps_its_image() {
|
||||
let plain = draw(&doc(triangle(), &[("rasterize", hard(json!({})))], 72.0), 10.0, 400);
|
||||
let off =
|
||||
draw(&doc(triangle(), &[("rasterize", hard(json!({}))), ("distort.transform", json!({"scaleH": 200, "objects": false}))], 72.0), 10.0, 400);
|
||||
assert!(plain.pixels == off.pixels);
|
||||
let mut hidden = doc(triangle(), &[("rasterize", hard(json!({}))), ("distort.transform", json!({"scaleH": 200}))], 72.0);
|
||||
let l = hidden.layers[0].id;
|
||||
Arc::make_mut(&mut hidden.children_mut(Some(l)).unwrap()[0]).appearance.effects[1].visible = false;
|
||||
assert!(plain.pixels == draw(&hidden, 10.0, 400).pixels);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user