mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-12-02 10:58:00 -06:00
Merge main into feat/character-anti-aliasing-method
# Conflicts: # crates/engine/src/cmd/panelcmds.rs # crates/ui-egui/src/panels/character.rs
This commit is contained in:
@@ -212,7 +212,7 @@ impl Renderer {
|
||||
self.draw_kept_node(ctx, f, p);
|
||||
self.fx_owner = owner;
|
||||
} else {
|
||||
self.draw_node(ctx, f, p, true);
|
||||
self.draw_forced_arc(ctx, f, p);
|
||||
}
|
||||
}
|
||||
(self.made_again, self.cur) = outer;
|
||||
@@ -260,6 +260,36 @@ mod tests {
|
||||
assert_eq!(again.pixels, img.pixels);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn art_brush_piece_applies_its_root_mask() {
|
||||
use vectorcraft_brush::{ArtBrush, ArtScale, Brush, BrushKind, Colorization};
|
||||
use vectorcraft_doc::OpacityMask;
|
||||
|
||||
let mut d = doc_with_line(Some("Masked art"), 1.0);
|
||||
let mut art = Node::path(
|
||||
d.alloc_id(),
|
||||
shapes::rectangle(Rect::new(0.0, -5.0, 10.0, 5.0)),
|
||||
Appearance::basic(Paint::solid(Color::BLACK), Paint::None, 0.0),
|
||||
);
|
||||
let black = Node::path(
|
||||
d.alloc_id(),
|
||||
shapes::rectangle(Rect::new(0.0, 0.0, 200.0, 100.0)),
|
||||
Appearance::basic(Paint::solid(Color::BLACK), Paint::None, 0.0),
|
||||
);
|
||||
art.extras_mut().mask = Some(Box::new(OpacityMask::new(black, true)));
|
||||
let brush = Brush {
|
||||
name: "Masked art".into(),
|
||||
kind: BrushKind::Art(ArtBrush { art, scale: ArtScale::Stretch, colorization: Colorization::None, ..Default::default() }),
|
||||
};
|
||||
vectorcraft_brush::store(&mut d, &[brush]);
|
||||
|
||||
let mut renderer = Renderer::new();
|
||||
for _ in 0..2 {
|
||||
let img = render(&mut renderer, &d);
|
||||
assert_eq!(img.pixel(100, 50), [255, 255, 255, 255]);
|
||||
}
|
||||
}
|
||||
|
||||
/// Object opacity fades a brushed stroke's art as a whole, with or without a dash and a fill:
|
||||
/// the same as the object at full opacity inside a group at that opacity.
|
||||
#[test]
|
||||
@@ -340,7 +370,7 @@ mod tests {
|
||||
shapes::rectangle(Rect::new(-10.0, -10.0, 10.0, 10.0)),
|
||||
Appearance::basic(Paint::solid(Color::BLACK), Paint::None, 0.0),
|
||||
);
|
||||
d.symbols.push(vectorcraft_doc::Symbol { name: "S".into(), art: std::sync::Arc::new(art) });
|
||||
d.symbols.push(vectorcraft_doc::Symbol { dynamic: false, name: "S".into(), art: std::sync::Arc::new(art) });
|
||||
let id = d.alloc_id();
|
||||
let mut inst = Node::new(id, vectorcraft_doc::NodeKind::SymbolInstance { symbol: "S".into(), xf: Affine::translate((50.0, 50.0)) });
|
||||
inst.appearance.set_fill(Paint::solid(Color::rgb(1.0, 0.0, 0.0)));
|
||||
@@ -364,7 +394,7 @@ mod tests {
|
||||
let mut ap = Appearance::basic(Paint::None, Paint::solid(Color::BLACK), 1.0);
|
||||
ap.stroke_mut().unwrap().brush = Some("3 pt. Round".into());
|
||||
let art = Node::path(vectorcraft_doc::NodeId(0), shapes::line((0.0, 40.0).into(), (0.0, 60.0).into()), ap);
|
||||
d.symbols.push(vectorcraft_doc::Symbol { name: "Nib".into(), art: std::sync::Arc::new(art) });
|
||||
d.symbols.push(vectorcraft_doc::Symbol { dynamic: false, name: "Nib".into(), art: std::sync::Arc::new(art) });
|
||||
let id = d.alloc_id();
|
||||
let inst = Node::new(id, vectorcraft_doc::NodeKind::SymbolInstance { symbol: "Nib".into(), xf: Affine::translate((-6.0, 0.0)) });
|
||||
let l = d.layers[0].id;
|
||||
@@ -394,7 +424,7 @@ mod tests {
|
||||
let moved = Affine::translate((-8.0, 0.0));
|
||||
let source = if instance {
|
||||
let art = line(&mut d);
|
||||
d.symbols.push(vectorcraft_doc::Symbol { name: "Nib".into(), art: std::sync::Arc::new(art) });
|
||||
d.symbols.push(vectorcraft_doc::Symbol { dynamic: false, name: "Nib".into(), art: std::sync::Arc::new(art) });
|
||||
Node::new(d.alloc_id(), vectorcraft_doc::NodeKind::SymbolInstance { symbol: "Nib".into(), xf: moved })
|
||||
} else {
|
||||
let mut n = line(&mut d);
|
||||
|
||||
@@ -236,7 +236,7 @@ fn art_bounds_take_in_the_effects_in_a_symbols_art() {
|
||||
let mut art = Node::path(d.alloc_id(), shapes::rectangle(Rect::new(0.0, 0.0, 10.0, 10.0)), black);
|
||||
let shadow = vectorcraft_doc::Effect { id: "stylize.dropShadow".into(), params: serde_json::json!({"x": 8, "y": 0, "blur": 0}), visible: true };
|
||||
art.appearance.effects.push(shadow);
|
||||
d.symbols.push(vectorcraft_doc::Symbol { name: "Box".into(), art: std::sync::Arc::new(art) });
|
||||
d.symbols.push(vectorcraft_doc::Symbol { dynamic: false, name: "Box".into(), art: std::sync::Arc::new(art) });
|
||||
let (id, l) = (d.alloc_id(), d.layers[0].id);
|
||||
let xf = vectorcraft_geom::Affine::translate((20.0, 20.0));
|
||||
d.insert(Some(l), 0, Node::new(id, NodeKind::SymbolInstance { symbol: "Box".into(), xf })).unwrap();
|
||||
|
||||
@@ -479,7 +479,7 @@ fn thirty_stacked_glows_draw_quickly_cached_or_not() {
|
||||
shapes::rectangle(Rect::new(0.0, 0.0, 40.0, 40.0)),
|
||||
Appearance::basic(Paint::solid(Color::BLACK), Paint::None, 0.0),
|
||||
);
|
||||
instanced.symbols.push(vectorcraft_doc::Symbol { name: "Square".into(), art: std::sync::Arc::new(art) });
|
||||
instanced.symbols.push(vectorcraft_doc::Symbol { dynamic: false, name: "Square".into(), art: std::sync::Arc::new(art) });
|
||||
let mut instance =
|
||||
Node::new(instanced.alloc_id(), vectorcraft_doc::NodeKind::SymbolInstance { symbol: "Square".into(), xf: Affine::translate((30.0, 30.0)) });
|
||||
instance.appearance.effects = glows.into_iter().map(|(id, p)| Effect { id: id.into(), params: p, visible: true }).collect();
|
||||
@@ -754,7 +754,7 @@ fn a_moved_group_draws_the_shadow_of_the_instance_art_in_it_while_interacting()
|
||||
let mut base = Document::new(100.0, 100.0);
|
||||
let orange = Appearance::basic(Paint::solid(Color::rgb(0.9, 0.4, 0.1)), Paint::None, 0.0);
|
||||
let art = Node::path(base.alloc_id(), shapes::rectangle(Rect::new(100.0, 100.0, 130.0, 126.0)), orange);
|
||||
base.symbols.push(vectorcraft_doc::Symbol { name: "Far".into(), art: std::sync::Arc::new(art) });
|
||||
base.symbols.push(vectorcraft_doc::Symbol { dynamic: false, name: "Far".into(), art: std::sync::Arc::new(art) });
|
||||
// The instance draws the art from (20, 20) to (50, 46), and the 20 × 20 square around the
|
||||
// origin from (-90, -90) to (-70, -70).
|
||||
let xf = Affine::translate((-80.0, -80.0));
|
||||
|
||||
@@ -236,6 +236,6 @@ pub(crate) use text_geom::is_opentype_alternate;
|
||||
#[cfg(test)]
|
||||
use text_geom::text_geom;
|
||||
pub(crate) use threads::single_threaded_context;
|
||||
pub use threads::{default_threads, set_default_threads};
|
||||
pub use threads::{default_threads, set_default_threads, set_usable_cores};
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
|
||||
@@ -771,7 +771,7 @@ mod tests {
|
||||
true => Node::new(d.alloc_id(), NodeKind::SymbolInstance { symbol: format!("S{}", k + 1), xf: Affine::IDENTITY }),
|
||||
false => square(&mut d),
|
||||
};
|
||||
d.symbols.push(Symbol { name: format!("S{k}"), art: Arc::new(art) });
|
||||
d.symbols.push(Symbol { dynamic: false, name: format!("S{k}"), art: Arc::new(art) });
|
||||
}
|
||||
let first = Node::new(d.alloc_id(), NodeKind::SymbolInstance { symbol: "S0".into(), xf: Affine::IDENTITY });
|
||||
let kind = EnvelopeKind::Warp { style: "arc".into(), bend: 30.0, h: 0.0, v: 0.0, horizontal: true };
|
||||
@@ -805,7 +805,7 @@ mod tests {
|
||||
};
|
||||
let (x, y) = (square(&mut d, 0.0), square(&mut d, 20.0));
|
||||
for (name, art) in [("A", &x), ("B", &y), ("C", &x)] {
|
||||
d.symbols.push(Symbol { name: name.into(), art: art.clone() });
|
||||
d.symbols.push(Symbol { dynamic: false, name: name.into(), art: art.clone() });
|
||||
}
|
||||
let content = ["A", "B", "C"].map(|s| Arc::new(Node::new(d.alloc_id(), NodeKind::SymbolInstance { symbol: s.into(), xf: Affine::IDENTITY })));
|
||||
let kind = EnvelopeKind::Warp { style: "arc".into(), bend: 30.0, h: 0.0, v: 0.0, horizontal: true };
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
//! ([`PatternDef::period`]) is rasterized at the device scale it is painted at, rounded up to a
|
||||
//! power of two and capped ([`raster_scale`]), and used as an image paint with `Extend::Repeat`,
|
||||
//! with the paint transform mapping pixels → pattern space → document. Tiles are cached per
|
||||
//! pattern name and raster scale (and ink plane, see [`crate::ink`]) within
|
||||
//! pattern name, raster scale, undecoded-image mark setting and ink plane (see [`crate::ink`]) within
|
||||
//! [`lru::PATTERN_BUDGET`]. A tile is used while the pattern's definition is the one it was
|
||||
//! rasterized from (art `Arc` identity, tile, tile type, overlap): the pattern's "revision", and,
|
||||
//! when its art shows more than itself (a symbol, another pattern, an image, type, a brush), while
|
||||
@@ -33,9 +33,9 @@ use crate::{Frame, RenderOptions, Rendered, Renderer};
|
||||
/// Largest super-tile raster side (pixels).
|
||||
const MAX_TILE_PX: f64 = 2048.0;
|
||||
|
||||
/// What a tile is kept under: the pattern's name and the bits of its raster scale
|
||||
/// ([`raster_scale`]).
|
||||
type TileKey = (String, u64, Ink);
|
||||
/// What a tile is kept under: the pattern's name, the bits of its raster scale
|
||||
/// ([`raster_scale`]), ink plane and undecoded-image mark setting.
|
||||
type TileKey = (String, u64, Ink, bool);
|
||||
|
||||
/// What a tile was drawn from besides its pattern's own art, for art that shows more than itself:
|
||||
/// the art of every pattern and symbol and the pixels of every image in the document, the
|
||||
@@ -169,12 +169,20 @@ pub(crate) fn raster_scale(s: f64, period: (f64, f64)) -> f64 {
|
||||
|
||||
/// Rasterize `region` (pattern space) of the infinite tiling into a `w`×`h` pixmap, colours as
|
||||
/// `ink` paints them.
|
||||
fn rasterize(r: &mut Renderer, doc: &Document, def: &PatternDef, region: Rect, w: u16, h: u16, ink: Ink) -> (Pixmap, bool) {
|
||||
let (w, h) = (w.max(1), h.max(1));
|
||||
fn rasterize(
|
||||
r: &mut Renderer,
|
||||
doc: &Document,
|
||||
def: &PatternDef,
|
||||
region: Rect,
|
||||
size: (u16, u16),
|
||||
ink: Ink,
|
||||
mark_undecoded_images: bool,
|
||||
) -> (Pixmap, bool) {
|
||||
let (w, h) = (size.0.max(1), size.1.max(1));
|
||||
let mut ctx = crate::single_threaded_context(w, h);
|
||||
let view = Affine::scale_non_uniform(w as f64 / region.width().max(1e-9), h as f64 / region.height().max(1e-9))
|
||||
* Affine::translate(-region.origin().to_vec2());
|
||||
let opts = RenderOptions::default();
|
||||
let opts = RenderOptions { mark_undecoded_images, ..Default::default() };
|
||||
r.image_left_out = Some(false);
|
||||
let pattern_images_left_out = images_left_out();
|
||||
r.check_plugins();
|
||||
@@ -255,7 +263,7 @@ pub(crate) fn set_pattern_paint(ctx: &mut RenderContext, name: &str, xf: Affine,
|
||||
let (pw, ph) = def.period();
|
||||
let scale = raster_scale(s, (pw, ph));
|
||||
let params = (def.tile, def.tile_type, def.overlap);
|
||||
let key = (name.to_string(), scale.to_bits(), ink);
|
||||
let key = (name.to_string(), scale.to_bits(), ink, f.opts.mark_undecoded_images);
|
||||
let cms = vectorcraft_color::cms::active();
|
||||
let hit = CACHE.with(|c| {
|
||||
let mut c = c.borrow_mut();
|
||||
@@ -284,7 +292,9 @@ pub(crate) fn set_pattern_paint(ctx: &mut RenderContext, name: &str, xf: Affine,
|
||||
let w = (pw * scale).ceil().clamp(1.0, 4096.0) as u16;
|
||||
let h = (ph * scale).ceil().clamp(1.0, 4096.0) as u16;
|
||||
let before = crate::paint::decode_failures();
|
||||
let Some((pm, image_left_out)) = with_tile_renderer(|r| rasterize(r, doc, def, Rect::new(0.0, 0.0, pw, ph), w, h, ink)) else {
|
||||
let Some((pm, image_left_out)) =
|
||||
with_tile_renderer(|r| rasterize(r, doc, def, Rect::new(0.0, 0.0, pw, ph), (w, h), ink, f.opts.mark_undecoded_images))
|
||||
else {
|
||||
ctx.set_paint(missing());
|
||||
return true;
|
||||
};
|
||||
@@ -323,7 +333,7 @@ pub fn render_pattern_swatch(doc: &Document, name: &str, size: u32) -> Option<Re
|
||||
let side = w.max(h);
|
||||
let region = Rect::from_center_size((w / 2.0, h / 2.0), (side, side));
|
||||
let px = size.clamp(1, 1024) as u16;
|
||||
let (pm, _) = with_tile_renderer(|r| rasterize(r, doc, def, region, px, px, Ink::Display))?;
|
||||
let (pm, _) = with_tile_renderer(|r| rasterize(r, doc, def, region, (px, px), Ink::Display, false))?;
|
||||
Some(Rendered { width: px as u32, height: px as u32, pixels: pm.data_as_u8_slice().to_vec() })
|
||||
}
|
||||
|
||||
|
||||
@@ -356,9 +356,10 @@ impl Renderer {
|
||||
self.check_plugins();
|
||||
self.check_symbols(doc);
|
||||
let b = brush_fx::cull_bounds(&doc.symbol_boxes(), n)?;
|
||||
let s = (size as f64 - 2.0) / b.width().max(b.height()).max(1e-6);
|
||||
let view = Affine::translate((size as f64 / 2.0, size as f64 / 2.0)) * Affine::scale(s) * Affine::translate(-b.center().to_vec2());
|
||||
let w = size.clamp(1, MAX_CONTEXT_SIDE) as u16;
|
||||
// A 1-pixel margin on each side, unless the thumbnail is too small to keep one.
|
||||
let s = (w as f64 - 2.0).max(1.0) / b.width().max(b.height()).max(1e-6);
|
||||
let view = Affine::translate((w as f64 / 2.0, w as f64 / 2.0)) * Affine::scale(s) * Affine::translate(-b.center().to_vec2());
|
||||
let mut ctx = single_threaded_context(w, w);
|
||||
if let Some(bg) = background {
|
||||
ctx.set_paint(peniko::Color::from_rgba8(bg[0], bg[1], bg[2], bg[3]));
|
||||
@@ -380,9 +381,9 @@ impl Renderer {
|
||||
self.begin_frame();
|
||||
(self.knockout, self.nested, self.backdrop, self.picture) = (false, 0, None, None);
|
||||
self.clip_paths.clear();
|
||||
self.draw_node(&mut ctx, &frame, n, true);
|
||||
self.draw_forced_arc(&mut ctx, &frame, &Arc::new(n.clone()));
|
||||
let pm = self.finish(&mut ctx, vello_cpu::RasterizerSettings::default());
|
||||
self.end_frame();
|
||||
Some(Rendered { width: size, height: size, pixels: pm.data_as_u8_slice().to_vec() })
|
||||
Some(Rendered { width: w as u32, height: w as u32, pixels: pm.data_as_u8_slice().to_vec() })
|
||||
}
|
||||
}
|
||||
|
||||
@@ -157,23 +157,39 @@ impl Renderer {
|
||||
self.stats.drawn += 1;
|
||||
return;
|
||||
}
|
||||
if let Some(m) = a.extras().mask.as_deref()
|
||||
&& !m.disabled
|
||||
&& !f.opts.outline
|
||||
&& !self.is_shape(a)
|
||||
{
|
||||
// A masked object is a non-isolated group of its own, drawn over its own pixels: the
|
||||
// mask is rendered over those only.
|
||||
let bounds = self.bounds_of(f.symbols, a);
|
||||
let Some((r, sub)) = group::area(ctx, f, bounds) else { return };
|
||||
let Some(mask) = self.mask_values(&sub, m, r.w, r.h) else { return };
|
||||
let blends = a.blend != vectorcraft_color::BlendMode::Normal || (!a.isolate && self.blends_through(a));
|
||||
self.group(ctx, f, Composite { mask: Some(mask), blends, bounds, ..Default::default() }, &mut |r, c, fr| r.draw_arc_body(c, fr, a));
|
||||
if self.draw_masked_arc(ctx, f, a, false) {
|
||||
return;
|
||||
}
|
||||
self.draw_arc_body(ctx, f, a);
|
||||
}
|
||||
|
||||
/// Draw `a` through its opacity mask when it has one. Forced roots draw regardless of their
|
||||
/// visibility and bounds.
|
||||
fn draw_masked_arc(&mut self, ctx: &mut RenderContext, f: &Frame, a: &Arc<Node>, force: bool) -> bool {
|
||||
let Some(m) = a.extras().mask.as_deref().filter(|m| !m.disabled && !f.opts.outline && !self.is_shape(a)) else { return false };
|
||||
// A masked object is a non-isolated group of its own, drawn over its own pixels: the
|
||||
// mask is rendered over those only.
|
||||
let bounds = self.bounds_of(f.symbols, a);
|
||||
let Some((r, sub)) = group::area(ctx, f, bounds) else { return true };
|
||||
let Some(mask) = self.mask_values(&sub, m, r.w, r.h) else { return true };
|
||||
let blends = a.blend != vectorcraft_color::BlendMode::Normal || (!a.isolate && self.blends_through(a));
|
||||
self.group(ctx, f, Composite { mask: Some(mask), blends, bounds, ..Default::default() }, &mut |r, c, fr| {
|
||||
if force {
|
||||
r.draw_node(c, fr, a, true);
|
||||
} else {
|
||||
r.draw_arc_body(c, fr, a);
|
||||
}
|
||||
});
|
||||
true
|
||||
}
|
||||
|
||||
/// Draw an `Arc` regardless of its visibility and bounds, applying its opacity mask.
|
||||
pub(crate) fn draw_forced_arc(&mut self, ctx: &mut RenderContext, f: &Frame, a: &Arc<Node>) {
|
||||
if !self.draw_masked_arc(ctx, f, a, true) {
|
||||
self.draw_node(ctx, f, a, true);
|
||||
}
|
||||
}
|
||||
|
||||
/// The coverage of opacity mask `m` per output pixel (row-major): its art rendered offscreen,
|
||||
/// as luminance ([`mask_value`]).
|
||||
pub(super) fn mask_values(&mut self, f: &Frame, m: &vectorcraft_doc::OpacityMask, w: u16, h: u16) -> Option<Vec<u8>> {
|
||||
@@ -191,8 +207,6 @@ impl Renderer {
|
||||
let failures = crate::paint::decode_failures();
|
||||
let pattern_images_left_out = crate::pattern::images_left_out();
|
||||
self.draw_kept_node(&mut mctx, &Frame { mt: false, ink: Ink::Display, ..*f }, &m.art);
|
||||
// Pattern tiles are drawn without undecoded-image marks, so an image a tile left out counts
|
||||
// with the marks on too.
|
||||
let incomplete = self.image_left_out.unwrap_or(false)
|
||||
|| crate::pattern::images_left_out() > pattern_images_left_out
|
||||
|| (!f.opts.mark_undecoded_images && crate::paint::decode_failures() > failures);
|
||||
@@ -489,7 +503,7 @@ impl Renderer {
|
||||
if kept {
|
||||
self.draw_kept_node(ctx, f, art);
|
||||
} else {
|
||||
self.draw_node(ctx, f, art, true);
|
||||
self.draw_forced_arc(ctx, f, art);
|
||||
}
|
||||
self.made_again = outer;
|
||||
}
|
||||
@@ -500,7 +514,7 @@ impl Renderer {
|
||||
pub(crate) fn draw_kept_node(&mut self, ctx: &mut RenderContext, f: &Frame, a: &Arc<Node>) {
|
||||
let live = vectorcraft_doc::live::is_live(a).then(|| a.clone());
|
||||
let outer = std::mem::replace(&mut self.live_root, live);
|
||||
self.draw_node(ctx, f, a, true);
|
||||
self.draw_forced_arc(ctx, f, a);
|
||||
self.live_root = outer;
|
||||
}
|
||||
|
||||
@@ -518,7 +532,7 @@ impl Renderer {
|
||||
fn draw_trace_view(&mut self, ctx: &mut RenderContext, f: &Frame, children: &[Arc<Node>], view: TraceView, knockout: bool) {
|
||||
let Some((source, shapes)) = children.split_first() else { return };
|
||||
if view.shows_source() && matches!(source.kind, NodeKind::Image(_)) {
|
||||
self.draw_node(ctx, f, source, true);
|
||||
self.draw_forced_arc(ctx, f, source);
|
||||
}
|
||||
if view.shows_result() {
|
||||
self.draw_children(ctx, f, shapes, knockout);
|
||||
|
||||
@@ -109,8 +109,11 @@ impl Renderer {
|
||||
}
|
||||
ctx.set_transform(f.view * xf);
|
||||
let closed = effects::stroke::is_closed(bp);
|
||||
// Keep hairlines visible when zoomed far out (at least ~1 device pixel).
|
||||
let width = effects::stroke::aligned_width(st, closed).max(f.px * 0.5 / xf.determinant().abs().sqrt().max(1e-9));
|
||||
// Keep hairlines visible when zoomed far out (at least ~1 device pixel). A width profile
|
||||
// multiplies the weight, so it is the profile's widest point that is kept visible: raising
|
||||
// the weight further would swell the whole stroke while the view zooms out (#995).
|
||||
let widest = st.profile.as_ref().map_or(1.0, |p| p.widest().max(1e-3));
|
||||
let width = effects::stroke::aligned_width(st, closed).max(f.px * 0.5 / xf.determinant().abs().sqrt().max(1e-9) / widest);
|
||||
let inside = st.align == StrokeAlign::Inside && closed;
|
||||
if inside {
|
||||
ctx.set_fill_rule(fill_rule(rule));
|
||||
|
||||
@@ -58,7 +58,9 @@ impl Renderer {
|
||||
/// placed again when the fonts change.
|
||||
pub(super) fn placed_art(&mut self, art: &Arc<Node>, xf: Affine, scale_strokes: bool, inst: Option<&Node>) -> (Arc<Node>, bool) {
|
||||
let kept_kind = matches!(art.kind, NodeKind::Group { .. } | NodeKind::Layer { .. }) || vectorcraft_doc::live::is_live(art);
|
||||
if !kept_kind || fx::has_object_fx(art) {
|
||||
// An instance with appearance changes of its own isn't kept: the key doesn't tell them apart.
|
||||
let overridden = inst.is_some_and(|i| !i.extras().symbol_overrides.is_empty());
|
||||
if !kept_kind || overridden || fx::has_object_fx(art) {
|
||||
return (place(art, xf, scale_strokes, inst), false);
|
||||
}
|
||||
let stain = stain_bits(inst.and_then(vectorcraft_brush::stain));
|
||||
|
||||
+212
-12
@@ -30,6 +30,7 @@ mod strokegradient;
|
||||
mod tileedge;
|
||||
mod typeedges;
|
||||
mod viewraster;
|
||||
mod widthzoom;
|
||||
|
||||
fn rect_node(d: &mut Document, r: Rect, fill: Paint, stroke: Paint, w: f64) -> Node {
|
||||
let id = d.alloc_id();
|
||||
@@ -220,7 +221,7 @@ fn hidden_and_culled() {
|
||||
fn far_instance(art: Rect) -> Document {
|
||||
let mut d = Document::new(100.0, 100.0);
|
||||
let art = rect_node(&mut d, art, Paint::solid(Color::rgb(1.0, 0.0, 0.0)), Paint::None, 0.0);
|
||||
d.symbols.push(vectorcraft_doc::Symbol { name: "Far".into(), art: Arc::new(art) });
|
||||
d.symbols.push(vectorcraft_doc::Symbol { dynamic: false, name: "Far".into(), art: Arc::new(art) });
|
||||
let (id, l) = (d.alloc_id(), d.layers[0].id);
|
||||
d.insert(Some(l), 0, Node::new(id, NodeKind::SymbolInstance { symbol: "Far".into(), xf: Affine::translate((-50.0, -50.0)) })).unwrap();
|
||||
d
|
||||
@@ -250,7 +251,7 @@ fn an_instance_is_culled_by_the_effects_in_its_art() {
|
||||
let mut art = rect_node(&mut d, Rect::new(-1.0, -1.0, 1.0, 1.0), Paint::solid(Color::BLACK), Paint::None, 0.0);
|
||||
let shadow = vectorcraft_doc::Effect { id: "stylize.dropShadow".into(), params: serde_json::json!({"x": 8, "y": 0, "blur": 0}), visible: true };
|
||||
art.appearance.effects.push(shadow);
|
||||
d.symbols.push(vectorcraft_doc::Symbol { name: "Dot".into(), art: Arc::new(art) });
|
||||
d.symbols.push(vectorcraft_doc::Symbol { dynamic: false, name: "Dot".into(), art: Arc::new(art) });
|
||||
let (id, l) = (d.alloc_id(), d.layers[0].id);
|
||||
d.insert(Some(l), 0, Node::new(id, NodeKind::SymbolInstance { symbol: "Dot".into(), xf: Affine::translate((-5.0, 20.0)) })).unwrap();
|
||||
let img = Renderer::new().render(&d, 100, 100, Affine::IDENTITY, &RenderOptions::default());
|
||||
@@ -264,7 +265,7 @@ fn a_repeat_of_an_instance_is_culled_by_the_effects_in_its_art() {
|
||||
let mut art = rect_node(&mut d, Rect::new(-1.0, -1.0, 1.0, 1.0), Paint::solid(Color::BLACK), Paint::None, 0.0);
|
||||
let shadow = vectorcraft_doc::Effect { id: "stylize.dropShadow".into(), params: serde_json::json!({"x": 8, "y": 0, "blur": 0}), visible: true };
|
||||
art.appearance.effects.push(shadow);
|
||||
d.symbols.push(vectorcraft_doc::Symbol { name: "Dot".into(), art: Arc::new(art) });
|
||||
d.symbols.push(vectorcraft_doc::Symbol { dynamic: false, name: "Dot".into(), art: Arc::new(art) });
|
||||
let instance = Node::new(d.alloc_id(), NodeKind::SymbolInstance { symbol: "Dot".into(), xf: Affine::translate((-5.0, 20.0)) });
|
||||
let grid = vectorcraft_doc::TileType::Grid;
|
||||
let kind =
|
||||
@@ -287,11 +288,11 @@ fn past_the_measured_placements_an_instance_is_culled_by_an_estimate_of_its_art_
|
||||
let mut art = rect_node(&mut d, Rect::new(-1.0, -1.0, 1.0, 1.0), Paint::solid(Color::BLACK), Paint::None, 0.0);
|
||||
let shadow = vectorcraft_doc::Effect { id: "stylize.dropShadow".into(), params: serde_json::json!({"x": 8, "y": 0, "blur": 0}), visible: true };
|
||||
art.appearance.effects.push(shadow);
|
||||
d.symbols.push(vectorcraft_doc::Symbol { name: "Spot".into(), art: Arc::new(art) });
|
||||
d.symbols.push(vectorcraft_doc::Symbol { dynamic: false, name: "Spot".into(), art: Arc::new(art) });
|
||||
for name in ["Dot", "Late"] {
|
||||
let spot = Node::new(d.alloc_id(), NodeKind::SymbolInstance { symbol: "Spot".into(), xf: Affine::IDENTITY });
|
||||
let group = Node::group(d.alloc_id(), vec![Arc::new(spot)]);
|
||||
d.symbols.push(vectorcraft_doc::Symbol { name: name.into(), art: Arc::new(group) });
|
||||
d.symbols.push(vectorcraft_doc::Symbol { dynamic: false, name: name.into(), art: Arc::new(group) });
|
||||
}
|
||||
let l = d.layers[0].id;
|
||||
for i in 0..4100 {
|
||||
@@ -317,7 +318,7 @@ fn nested_instances_are_culled_in_bounded_time(effect: Option<vectorcraft_doc::E
|
||||
let mut art = rect_node(&mut d, Rect::new(0.0, 0.0, 1.0, 1.0), Paint::solid(Color::BLACK), Paint::None, 0.0);
|
||||
let shadow = vectorcraft_doc::Effect { id: "stylize.dropShadow".into(), params: serde_json::json!({"x": 2, "y": 0, "blur": 0}), visible: true };
|
||||
art.appearance.effects.push(shadow);
|
||||
d.symbols.push(vectorcraft_doc::Symbol { name: "S30".into(), art: Arc::new(art) });
|
||||
d.symbols.push(vectorcraft_doc::Symbol { dynamic: false, name: "S30".into(), art: Arc::new(art) });
|
||||
let instance = |d: &mut Document, symbol: String, xf: Affine| {
|
||||
let mut n = Node::new(d.alloc_id(), NodeKind::SymbolInstance { symbol, xf });
|
||||
n.appearance.effects.extend(effect.clone());
|
||||
@@ -330,7 +331,7 @@ fn nested_instances_are_culled_in_bounded_time(effect: Option<vectorcraft_doc::E
|
||||
pair.push(Arc::new(instance(&mut d, next.clone(), xf)));
|
||||
}
|
||||
let group = Node::group(d.alloc_id(), pair);
|
||||
d.symbols.push(vectorcraft_doc::Symbol { name: format!("S{i}"), art: Arc::new(group) });
|
||||
d.symbols.push(vectorcraft_doc::Symbol { dynamic: false, name: format!("S{i}"), art: Arc::new(group) });
|
||||
}
|
||||
let first = instance(&mut d, "S0".into(), Affine::translate((-100_000.0, 0.0)));
|
||||
let l = d.layers[0].id;
|
||||
@@ -369,7 +370,7 @@ fn a_scaled_instance_is_culled_by_the_effects_in_its_art_as_placed() {
|
||||
let mut art = rect_node(&mut d, Rect::new(-1.0, -1.0, 1.0, 1.0), Paint::solid(Color::rgb(1.0, 0.0, 0.0)), Paint::None, 0.0);
|
||||
let double = vectorcraft_doc::Effect { id: "distort.transform".into(), params: serde_json::json!({"scaleH": 200, "scaleV": 200}), visible: true };
|
||||
art.appearance.effects.push(double);
|
||||
d.symbols.push(vectorcraft_doc::Symbol { name: "Dot".into(), art: Arc::new(art) });
|
||||
d.symbols.push(vectorcraft_doc::Symbol { dynamic: false, name: "Dot".into(), art: Arc::new(art) });
|
||||
let (id, l) = (d.alloc_id(), d.layers[0].id);
|
||||
let xf = Affine::translate((-50.0, 50.0)) * Affine::scale(30.0);
|
||||
d.insert(Some(l), 0, Node::new(id, NodeKind::SymbolInstance { symbol: "Dot".into(), xf })).unwrap();
|
||||
@@ -401,7 +402,7 @@ fn an_instance_with_a_geometry_effect_is_culled_by_the_effects_in_its_reshaped_a
|
||||
let effect = || vectorcraft_doc::Effect { id: "distort.transform".into(), params: scale.clone(), visible: true };
|
||||
let mut art = rect_node(&mut d, Rect::new(-1.0, -1.0, 1.0, 1.0), Paint::solid(Color::rgb(1.0, 0.0, 0.0)), Paint::None, 0.0);
|
||||
art.appearance.effects.push(effect());
|
||||
d.symbols.push(vectorcraft_doc::Symbol { name: "Dot".into(), art: Arc::new(art) });
|
||||
d.symbols.push(vectorcraft_doc::Symbol { dynamic: false, name: "Dot".into(), art: Arc::new(art) });
|
||||
let (id, l) = (d.alloc_id(), d.layers[0].id);
|
||||
let mut instance = Node::new(id, NodeKind::SymbolInstance { symbol: "Dot".into(), xf: Affine::translate((-11.0, 20.0)) });
|
||||
instance.appearance.effects.push(effect());
|
||||
@@ -663,6 +664,39 @@ fn trace_source_draws_the_source_image_of_the_listed_objects() {
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn trace_source_applies_the_hidden_image_mask() {
|
||||
use vectorcraft_doc::{ImageBlob, ImageObject, NodeKind, OpacityMask};
|
||||
|
||||
let mut png = Vec::new();
|
||||
image::RgbaImage::from_pixel(1, 1, image::Rgba([255, 0, 0, 255])).write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png).unwrap();
|
||||
let mut d = Document::new(100.0, 100.0);
|
||||
d.images.insert("trace-source".into(), ImageBlob::new("image/png", png));
|
||||
let image = ImageObject {
|
||||
key: "trace-source".into(),
|
||||
width: 1,
|
||||
height: 1,
|
||||
xf: Affine::translate((10.0, 10.0)) * Affine::scale(80.0),
|
||||
link: None,
|
||||
placement: Default::default(),
|
||||
pixels: None,
|
||||
};
|
||||
let mut source = Node::new(d.alloc_id(), NodeKind::Image(Box::new(image)));
|
||||
source.visible = false;
|
||||
let mask = rect_node(&mut d, Rect::new(10.0, 10.0, 50.0, 90.0), Paint::solid(Color::WHITE), Paint::None, 0.0);
|
||||
source.extras_mut().mask = Some(Box::new(OpacityMask::new(mask, true)));
|
||||
let result = rect_node(&mut d, Rect::new(10.0, 10.0, 90.0, 90.0), Paint::solid(Color::rgb(0.0, 0.0, 1.0)), Paint::None, 0.0);
|
||||
let mut trace = Node::group(d.alloc_id(), vec![Arc::new(source), Arc::new(result)]);
|
||||
trace.extras_mut().trace = Some(Box::new(serde_json::json!({ "view": "tracingResult" })));
|
||||
let trace_id = trace.id;
|
||||
d.insert(Some(d.layers[0].id), 0, trace).unwrap();
|
||||
|
||||
let opts = RenderOptions { trace_views: true, trace_source: Some(Arc::new(vec![trace_id])), background: Some([255; 4]), ..Default::default() };
|
||||
let img = Renderer::new().render(&d, 100, 100, Affine::IDENTITY, &opts);
|
||||
assert_eq!(img.pixel(30, 50), [255, 0, 0, 255]);
|
||||
assert_eq!(img.pixel(70, 50), [255, 255, 255, 255]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn artboards_white_on_pasteboard() {
|
||||
let d = Document::new(50.0, 50.0);
|
||||
@@ -748,6 +782,110 @@ fn opacity_mask_without_clip_and_inverted() {
|
||||
assert_eq!(img.pixel(80, 50), [255, 0, 0, 255], "inverted clip background → visible");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn opacity_mask_art_root_applies_its_own_mask() {
|
||||
use vectorcraft_doc::OpacityMask;
|
||||
|
||||
let mut d = Document::new(100.0, 100.0);
|
||||
let mut object = rect_node(&mut d, Rect::new(10.0, 10.0, 90.0, 90.0), Paint::solid(Color::rgb(1.0, 0.0, 0.0)), Paint::None, 0.0);
|
||||
let mut art = rect_node(&mut d, Rect::new(10.0, 10.0, 90.0, 90.0), Paint::solid(Color::WHITE), Paint::None, 0.0);
|
||||
let inner = rect_node(&mut d, Rect::new(10.0, 10.0, 50.0, 90.0), Paint::solid(Color::WHITE), Paint::None, 0.0);
|
||||
art.extras_mut().mask = Some(Box::new(OpacityMask::new(inner, true)));
|
||||
art.visible = false;
|
||||
object.extras_mut().mask = Some(Box::new(OpacityMask::new(art, true)));
|
||||
let object_id = object.id;
|
||||
d.insert(Some(d.layers[0].id), 0, object).unwrap();
|
||||
|
||||
let opts = RenderOptions { background: Some([255, 255, 255, 255]), ..Default::default() };
|
||||
let mut renderer = Renderer::new();
|
||||
let img = renderer.render(&d, 100, 100, Affine::IDENTITY, &opts);
|
||||
assert_eq!(img.pixel(30, 50), [255, 0, 0, 255]);
|
||||
assert_eq!(img.pixel(70, 50), [255, 255, 255, 255]);
|
||||
let view = renderer.render(&d, 100, 100, Affine::IDENTITY, &RenderOptions { mask_view: Some(object_id), ..opts });
|
||||
assert_eq!(view.pixel(30, 50), [255, 255, 255, 255]);
|
||||
assert_eq!(view.pixel(70, 50), [0, 0, 0, 255]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn undecodable_image_in_mask_art_root_mask_leaves_out_outer_object() {
|
||||
use vectorcraft_doc::{ImageBlob, ImageObject, NodeKind, OpacityMask};
|
||||
|
||||
let mut d = Document::new(100.0, 100.0);
|
||||
d.images.insert("broken-root-mask-image".into(), ImageBlob::new("image/png", b"not a PNG".to_vec()));
|
||||
let mut object = rect_node(&mut d, Rect::new(10.0, 10.0, 90.0, 90.0), Paint::solid(Color::rgb(1.0, 0.0, 0.0)), Paint::None, 0.0);
|
||||
let mut art = rect_node(&mut d, Rect::new(10.0, 10.0, 90.0, 90.0), Paint::solid(Color::WHITE), Paint::None, 0.0);
|
||||
let image = ImageObject {
|
||||
key: "broken-root-mask-image".into(),
|
||||
width: 20,
|
||||
height: 20,
|
||||
xf: Affine::translate((30.0, 30.0)),
|
||||
link: None,
|
||||
placement: Default::default(),
|
||||
pixels: None,
|
||||
};
|
||||
art.extras_mut().mask = Some(Box::new(OpacityMask::new(Node::new(d.alloc_id(), NodeKind::Image(Box::new(image))), false)));
|
||||
object.extras_mut().mask = Some(Box::new(OpacityMask::new(art, true)));
|
||||
let object_id = object.id;
|
||||
d.insert(Some(d.layers[0].id), 0, object).unwrap();
|
||||
|
||||
let opts = RenderOptions { background: Some([255; 4]), ..Default::default() };
|
||||
let mut renderer = Renderer::new();
|
||||
let img = renderer.render(&d, 100, 100, Affine::IDENTITY, &opts);
|
||||
assert_eq!(img.pixel(70, 50), [255, 255, 255, 255]);
|
||||
let view = renderer.render(&d, 100, 100, Affine::IDENTITY, &RenderOptions { mask_view: Some(object_id), ..opts });
|
||||
assert_eq!(view.pixel(70, 50), [0, 0, 0, 255]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn placed_symbol_art_root_applies_its_own_mask() {
|
||||
use vectorcraft_doc::{NodeKind, OpacityMask};
|
||||
|
||||
for grouped in [true, false] {
|
||||
let mut d = Document::new(100.0, 100.0);
|
||||
let red = rect_node(&mut d, Rect::new(10.0, 10.0, 90.0, 90.0), Paint::solid(Color::rgb(1.0, 0.0, 0.0)), Paint::None, 0.0);
|
||||
let mut art = if grouped { Node::group(d.alloc_id(), vec![Arc::new(red)]) } else { red };
|
||||
let inner = rect_node(&mut d, Rect::new(10.0, 10.0, 50.0, 90.0), Paint::solid(Color::WHITE), Paint::None, 0.0);
|
||||
art.extras_mut().mask = Some(Box::new(OpacityMask::new(inner, true)));
|
||||
d.symbols.push(vectorcraft_doc::Symbol { dynamic: false, name: "Masked".into(), art: Arc::new(art) });
|
||||
let instance = Node::new(d.alloc_id(), NodeKind::SymbolInstance { symbol: "Masked".into(), xf: Affine::IDENTITY });
|
||||
d.insert(Some(d.layers[0].id), 0, instance).unwrap();
|
||||
|
||||
let img = render(&d);
|
||||
assert_eq!(img.pixel(30, 50), [255, 0, 0, 255], "grouped {grouped}");
|
||||
assert_eq!(img.pixel(70, 50), [255, 255, 255, 255], "grouped {grouped}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn node_thumbnail_applies_the_root_mask() {
|
||||
use vectorcraft_doc::OpacityMask;
|
||||
|
||||
let mut d = Document::new(100.0, 100.0);
|
||||
let mut node = rect_node(&mut d, Rect::new(10.0, 10.0, 90.0, 90.0), Paint::solid(Color::rgb(1.0, 0.0, 0.0)), Paint::None, 0.0);
|
||||
let mask = rect_node(&mut d, Rect::new(10.0, 10.0, 50.0, 90.0), Paint::solid(Color::WHITE), Paint::None, 0.0);
|
||||
node.extras_mut().mask = Some(Box::new(OpacityMask::new(mask, true)));
|
||||
|
||||
let img = Renderer::new().render_node_thumbnail(&d, &node, 100, Some([255; 4])).unwrap();
|
||||
assert_eq!(img.pixel(25, 50), [255, 0, 0, 255]);
|
||||
assert_eq!(img.pixel(75, 50), [255, 255, 255, 255]);
|
||||
}
|
||||
|
||||
/// #3602: a thumbnail reports the size it was drawn at, and one too small for its 1-pixel margin
|
||||
/// still draws the art.
|
||||
#[test]
|
||||
fn node_thumbnail_reports_the_size_it_draws() {
|
||||
let mut d = Document::new(100.0, 100.0);
|
||||
let node = rect_node(&mut d, Rect::new(10.0, 10.0, 90.0, 90.0), Paint::solid(Color::rgb(1.0, 0.0, 0.0)), Paint::None, 0.0);
|
||||
let mut r = Renderer::new();
|
||||
for size in 0..4 {
|
||||
let img = r.render_node_thumbnail(&d, &node, size, None).unwrap();
|
||||
assert_eq!(img.pixels.len(), img.width as usize * img.height as usize * 4, "size {size}");
|
||||
assert_eq!(img.width, size.max(1), "size {size}");
|
||||
assert_eq!(img.height, size.max(1), "size {size}");
|
||||
assert!(img.pixels.chunks(4).any(|p| p[3] > 0), "size {size}: the art is drawn");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn opacity_mask_with_undecodable_image_leaves_out_object() {
|
||||
use vectorcraft_doc::{Effect, ImageBlob, ImageObject, NodeKind, OpacityMask};
|
||||
@@ -824,9 +962,33 @@ fn opacity_mask_with_missing_image_in_pattern_leaves_out_object() {
|
||||
let img = r.render(&d, 100, 100, Affine::IDENTITY, &opts);
|
||||
assert_eq!(img.pixel(70, 50), [255, 255, 255, 255], "frame {frame}: incomplete pattern mask leaves out the object");
|
||||
}
|
||||
// Pattern tiles draw no undecoded-image mark, so the object stays out with the marks on too.
|
||||
let marked = Renderer::new().render(&d, 100, 100, Affine::IDENTITY, &RenderOptions { mark_undecoded_images: true, ..opts.clone() });
|
||||
assert_eq!(marked.pixel(70, 50), [255, 255, 255, 255], "a tile without the mark leaves out the object");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn opacity_mask_with_marked_image_in_pattern_draws_object() {
|
||||
use vectorcraft_doc::pattern::{PatternDef, pattern_paint};
|
||||
use vectorcraft_doc::{ImageBlob, ImageObject, NodeKind, OpacityMask};
|
||||
|
||||
let mut d = Document::new(100.0, 100.0);
|
||||
d.images.insert("cut-mask".into(), ImageBlob::new("image/png", b"not a PNG: pattern mask".to_vec()));
|
||||
let image =
|
||||
ImageObject { key: "cut-mask".into(), width: 20, height: 20, xf: Affine::IDENTITY, link: None, placement: Default::default(), pixels: None };
|
||||
let image = Node::new(d.alloc_id(), NodeKind::Image(Box::new(image)));
|
||||
let mut pattern = PatternDef::new("Cut mask", vec![Arc::new(image)]);
|
||||
pattern.tile = Rect::new(0.0, 0.0, 20.0, 20.0);
|
||||
d.patterns.push(pattern);
|
||||
let art = rect_node(&mut d, Rect::new(10.0, 10.0, 50.0, 90.0), pattern_paint("Cut mask"), Paint::None, 0.0);
|
||||
let mut object = rect_node(&mut d, Rect::new(10.0, 10.0, 90.0, 90.0), Paint::solid(Color::rgb(1.0, 0.0, 0.0)), Paint::None, 0.0);
|
||||
object.extras_mut().mask = Some(Box::new(OpacityMask::new(art, false)));
|
||||
d.insert(Some(d.layers[0].id), 0, object).unwrap();
|
||||
|
||||
let mut r = Renderer::new();
|
||||
let opts = RenderOptions { background: Some([255, 255, 255, 255]), ..Default::default() };
|
||||
let off = r.render(&d, 100, 100, Affine::IDENTITY, &opts);
|
||||
assert_eq!(off.pixel(70, 50), [255, 255, 255, 255], "an unmarked missing image leaves out the object");
|
||||
let on = r.render(&d, 100, 100, Affine::IDENTITY, &RenderOptions { mark_undecoded_images: true, ..opts });
|
||||
assert_eq!(on.pixel(70, 50), [255, 0, 0, 255], "the marked image keeps the mask complete");
|
||||
assert!(on.pixel(15, 15)[1] < 255, "the marked pattern contributes mask coverage");
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -1368,6 +1530,44 @@ fn an_undecodable_image_is_tried_once_and_marked_on_screen() {
|
||||
assert_eq!((attempts() - tried, decode_failures() - failed), (1, 3), "and counts it without decoding it again");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn pattern_tile_follows_undecoded_image_mark_in_both_cache_orders() {
|
||||
use vectorcraft_doc::pattern::{PatternDef, pattern_paint};
|
||||
use vectorcraft_doc::{ImageBlob, ImageObject, NodeKind};
|
||||
|
||||
let mut d = Document::new(80.0, 80.0);
|
||||
d.images.insert("cut-pattern".into(), ImageBlob::new("image/png", b"not a PNG: pattern fill".to_vec()));
|
||||
let image = ImageObject {
|
||||
key: "cut-pattern".into(),
|
||||
width: 20,
|
||||
height: 20,
|
||||
xf: Affine::IDENTITY,
|
||||
link: None,
|
||||
placement: Default::default(),
|
||||
pixels: None,
|
||||
};
|
||||
let image = Node::new(d.alloc_id(), NodeKind::Image(Box::new(image)));
|
||||
let mut pattern = PatternDef::new("Cut pattern", vec![Arc::new(image)]);
|
||||
pattern.tile = Rect::new(0.0, 0.0, 20.0, 20.0);
|
||||
d.patterns.push(pattern);
|
||||
let shape = rect_node(&mut d, Rect::new(10.0, 10.0, 70.0, 70.0), pattern_paint("Cut pattern"), Paint::None, 0.0);
|
||||
d.insert(Some(d.layers[0].id), 0, shape).unwrap();
|
||||
|
||||
let mut r = Renderer::new();
|
||||
for order in [[false, true, false], [true, false, true]] {
|
||||
for mark in order {
|
||||
let opts = RenderOptions { mark_undecoded_images: mark, ..Default::default() };
|
||||
let pixel = r.render(&d, 80, 80, Affine::IDENTITY, &opts).pixel(15, 15);
|
||||
assert_eq!(pixel[3] > 0, mark, "mark_undecoded_images {mark}: {pixel:?}");
|
||||
if mark {
|
||||
assert_eq!(pixel[0], pixel[1], "the pattern draws a gray mark");
|
||||
assert_eq!(pixel[1], pixel[2], "the pattern draws a gray mark");
|
||||
assert!((120..=140).contains(&pixel[0]), "the pattern draws a gray mark: {pixel:?}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The rasters a renderer keeps of an object's raster effects are made again when
|
||||
/// `mark_undecoded_images` changes: a blurred image whose pixels can't be decoded shows its blurred
|
||||
/// mark only while the option is on.
|
||||
|
||||
@@ -112,7 +112,7 @@ fn an_embedded_image_is_recoloured_and_the_original_pixels_stay() {
|
||||
fn a_symbol_instance_recolours_its_art() {
|
||||
let mut d = Document::new(100.0, 100.0);
|
||||
let art = red_square(-15.0, -15.0);
|
||||
d.symbols.push(Symbol { name: "Red".into(), art: Arc::new(art) });
|
||||
d.symbols.push(Symbol { dynamic: false, name: "Red".into(), art: Arc::new(art) });
|
||||
let mut n = Node::new(NodeId(0), NodeKind::SymbolInstance { symbol: "Red".into(), xf: Affine::translate((50.0, 50.0)) });
|
||||
n.appearance.effects.push(hue());
|
||||
let l = d.layers[0].id;
|
||||
|
||||
@@ -136,17 +136,49 @@ fn lam_alef_is_not_an_alternate() {
|
||||
/// feature. Built in code, so it runs whatever fonts the machine has.
|
||||
#[test]
|
||||
fn lam_alef_in_liga_is_not_an_alternate() {
|
||||
use vectorcraft_text::test_fonts::{lam_alef_family, lam_alef_font};
|
||||
use vectorcraft_text::test_fonts::{lam_alef_family, lam_alef_font, source_sans_gid};
|
||||
let _adding = crate::tests_fontchange::adding_fonts();
|
||||
let db = vectorcraft_text::FontDb::global();
|
||||
let gid = |c| u32::from(source_sans_gid(c).unwrap());
|
||||
// Text, the ligature glyph and its cluster (byte, length): lam + alef, lam + alef with hamza
|
||||
// above, the pair after a beh, and the pair with a fatha on the lam (the ligature skips it).
|
||||
let cases = [
|
||||
("\u{0644}\u{0627}", 'H', (0, 4)),
|
||||
("\u{0644}\u{0623}", 'K', (0, 4)),
|
||||
("\u{0628}\u{0644}\u{0627}", 'H', (2, 4)),
|
||||
("\u{0644}\u{064E}\u{0627}", 'H', (0, 6)),
|
||||
];
|
||||
for feature in [b"liga", b"rlig"] {
|
||||
db.add_font(lam_alef_font(feature).unwrap());
|
||||
let style = vectorcraft_doc::text::CharStyle { size: 36.0, font_family: lam_alef_family(feature).into(), ..Default::default() };
|
||||
for s in ["\u{0644}\u{0627}", "\u{0644}\u{0623}", "\u{0628}\u{0644}\u{0627}", "\u{0644}\u{064E}\u{0627}"] {
|
||||
let family = lam_alef_family(feature);
|
||||
let style = vectorcraft_doc::text::CharStyle { size: 36.0, font_family: family.into(), ..Default::default() };
|
||||
for (s, ligature, (byte, len)) in cases {
|
||||
let t = text(s, style.clone());
|
||||
// The font's ligature glyph stands for the pair.
|
||||
assert!(glyphs(&t).iter().any(|g| g.1 > 2), "{}: {s} is shaped to one ligature glyph", lam_alef_family(feature));
|
||||
assert_eq!(alternates(&t), 0, "{}: {s}", lam_alef_family(feature));
|
||||
// The font's ligature glyph stands for the pair, its cluster lam to alef.
|
||||
let shaped = glyphs(&t);
|
||||
assert!(shaped.contains(&(byte, len, gid(ligature))), "{family}: {s} is shaped to {ligature} over {byte}..{}: {shaped:?}", byte + len);
|
||||
assert_eq!(alternates(&t), 0, "{family}: {s}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// #394: an optional feature that changes the lam-alef ligature (here `salt`) is highlighted:
|
||||
/// only the required ligature itself is left clear, whichever feature the font files it under.
|
||||
#[test]
|
||||
fn an_optional_feature_on_the_lam_alef_ligature_is_an_alternate() {
|
||||
use vectorcraft_text::test_fonts::{LAM_ALEF_SALT, lam_alef_family, lam_alef_font, source_sans_gid};
|
||||
let _adding = crate::tests_fontchange::adding_fonts();
|
||||
let db = vectorcraft_text::FontDb::global();
|
||||
let salted = u32::from(source_sans_gid(LAM_ALEF_SALT).unwrap());
|
||||
for feature in [b"liga", b"rlig"] {
|
||||
db.add_font(lam_alef_font(feature).unwrap());
|
||||
let family = lam_alef_family(feature);
|
||||
let style = vectorcraft_doc::text::CharStyle { size: 36.0, font_family: family.into(), features: vec!["salt".into()], ..Default::default() };
|
||||
for (s, byte) in [("\u{0644}\u{0627}", 0), ("\u{0628}\u{0644}\u{0627}", 2)] {
|
||||
let t = text(s, style.clone());
|
||||
let shaped = glyphs(&t);
|
||||
assert!(shaped.contains(&(byte, 4, salted)), "{family}: salt turns the ligature into {LAM_ALEF_SALT}: {shaped:?}");
|
||||
assert_eq!(alternates(&t), 1, "{family}: {s}: the salted ligature, and nothing else");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -154,11 +186,36 @@ fn lam_alef_in_liga_is_not_an_alternate() {
|
||||
/// Only a lam followed by an alef (marks between or after allowed) is the required lam-alef.
|
||||
#[test]
|
||||
fn only_lam_alef_clusters_are_the_required_ligature() {
|
||||
for s in ["\u{0644}\u{0627}", "\u{0644}\u{0622}", "\u{0644}\u{0623}", "\u{0644}\u{0625}", "\u{0644}\u{064E}\u{0627}", "\u{0644}\u{0627}\u{064B}"]
|
||||
{
|
||||
// Marks of the Arabic block, Arabic Extended-A (U+08E4, U+08F0), Extended-B (U+0898) and
|
||||
// Extended-C (U+10EFD), and Quranic marks (U+0610, U+06ED).
|
||||
for s in [
|
||||
"\u{0644}\u{0627}",
|
||||
"\u{0644}\u{0622}",
|
||||
"\u{0644}\u{0623}",
|
||||
"\u{0644}\u{0625}",
|
||||
"\u{0644}\u{064E}\u{0627}",
|
||||
"\u{0644}\u{0627}\u{064B}",
|
||||
"\u{0644}\u{08E4}\u{0627}",
|
||||
"\u{0644}\u{08F0}\u{0627}",
|
||||
"\u{0644}\u{0898}\u{0627}",
|
||||
"\u{0644}\u{0627}\u{10EFD}",
|
||||
"\u{0644}\u{0610}\u{06ED}\u{0627}",
|
||||
] {
|
||||
assert!(crate::text_geom::is_lam_alef(s), "{s:?}");
|
||||
}
|
||||
for s in ["", "\u{0644}", "\u{0627}\u{0644}", "\u{0644}\u{0628}", "\u{0628}\u{0644}\u{0627}", "\u{0644}\u{0627}\u{0627}", "fi", "\u{064E}"] {
|
||||
// Not marks: tatweel (U+0640) and the number mark above (U+08E2, a format character).
|
||||
for s in [
|
||||
"",
|
||||
"\u{0644}",
|
||||
"\u{0627}\u{0644}",
|
||||
"\u{0644}\u{0628}",
|
||||
"\u{0628}\u{0644}\u{0627}",
|
||||
"\u{0644}\u{0627}\u{0627}",
|
||||
"fi",
|
||||
"\u{064E}",
|
||||
"\u{0644}\u{0640}\u{0627}",
|
||||
"\u{0644}\u{08E2}\u{0627}",
|
||||
] {
|
||||
assert!(!crate::text_geom::is_lam_alef(s), "{s:?}");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -220,7 +220,7 @@ fn an_unchanged_document_changes_nothing_and_a_new_symbol_everything() {
|
||||
assert_eq!(redraw(&old, &old.clone()), Changed::Nothing);
|
||||
let mut new = old.clone();
|
||||
let art = Arc::new(rect(&mut new, Rect::new(0.0, 0.0, 5.0, 5.0), Color::BLACK));
|
||||
new.symbols.push(vectorcraft_doc::Symbol { name: "Dot".into(), art });
|
||||
new.symbols.push(vectorcraft_doc::Symbol { dynamic: false, name: "Dot".into(), art });
|
||||
assert_eq!(redraw(&old, &new), Changed::Everything);
|
||||
}
|
||||
|
||||
|
||||
@@ -118,7 +118,7 @@ fn the_transparency_test_is_kept_while_the_art_stays_the_same() {
|
||||
// A symbol whose art shows transparency, placed nowhere, makes the page draw by ink planes again.
|
||||
let mut art = rect(&mut e, Rect::new(0.0, 0.0, 5.0, 5.0), CYAN);
|
||||
art.opacity = 0.5;
|
||||
e.symbols.push(vectorcraft_doc::Symbol { name: "S".into(), art: Arc::new(art) });
|
||||
e.symbols.push(vectorcraft_doc::Symbol { dynamic: false, name: "S".into(), art: Arc::new(art) });
|
||||
assert!(frame(&mut r, &e) == frame(&mut Renderer::new(), &e));
|
||||
assert_eq!(kept(&r).map(|(_, shows)| shows), Some(true));
|
||||
}
|
||||
|
||||
@@ -162,7 +162,7 @@ fn symbol_type_redraws_when_its_font_becomes_available() {
|
||||
let mut t = Node::new(d.alloc_id(), NodeKind::Text(Box::new(TextObject::point(Point::new(-140.0, 25.0), "Hamburg", style))));
|
||||
t.appearance.effects.push(vectorcraft_doc::Effect { id: "distort.transform".into(), params: serde_json::json!({"moveH": 0.0}), visible: true });
|
||||
let art = Node::group(d.alloc_id(), vec![Arc::new(t)]);
|
||||
d.symbols.push(vectorcraft_doc::Symbol { name: "Word".into(), art: Arc::new(art) });
|
||||
d.symbols.push(vectorcraft_doc::Symbol { dynamic: false, name: "Word".into(), art: Arc::new(art) });
|
||||
let (l, id) = (d.layers[0].id, d.alloc_id());
|
||||
d.insert(Some(l), 0, Node::new(id, NodeKind::SymbolInstance { symbol: "Word".into(), xf: Affine::translate((150.0, 75.0)) })).unwrap();
|
||||
let opts = RenderOptions { background: Some([255, 255, 255, 255]), ..Default::default() };
|
||||
@@ -207,7 +207,7 @@ fn type_with_geometry_effects_redraws_when_its_font_becomes_available() {
|
||||
}
|
||||
3 => {
|
||||
let art = Node::group(d.alloc_id(), vec![Arc::new(object)]);
|
||||
d.symbols.push(vectorcraft_doc::Symbol { name: "Word".into(), art: Arc::new(art) });
|
||||
d.symbols.push(vectorcraft_doc::Symbol { dynamic: false, name: "Word".into(), art: Arc::new(art) });
|
||||
object = Node::new(d.alloc_id(), NodeKind::SymbolInstance { symbol: "Word".into(), xf: Affine::translate((150.0, 75.0)) });
|
||||
object.appearance.effects.push(moved());
|
||||
}
|
||||
@@ -243,7 +243,7 @@ fn enveloped_type_redraws_when_its_font_becomes_available() {
|
||||
let typed = word(&mut d, Point::new(10.0, 60.0));
|
||||
let art = word(&mut d, Point::new(-140.0, 20.0));
|
||||
let art = Node::group(d.alloc_id(), vec![Arc::new(art)]);
|
||||
d.symbols.push(vectorcraft_doc::Symbol { name: "Word".into(), art: Arc::new(art) });
|
||||
d.symbols.push(vectorcraft_doc::Symbol { dynamic: false, name: "Word".into(), art: Arc::new(art) });
|
||||
let shown = Node::new(d.alloc_id(), NodeKind::SymbolInstance { symbol: "Word".into(), xf: Affine::translate((150.0, 120.0)) });
|
||||
let l = d.layers[0].id;
|
||||
for (i, content) in [typed, shown].into_iter().enumerate() {
|
||||
|
||||
@@ -389,7 +389,7 @@ fn type_with_an_inline_graphic_draws_its_symbols_new_size() {
|
||||
for i in 0..4 {
|
||||
let mut d = Document::new(200.0, 100.0);
|
||||
let art = symbol_art(&mut d);
|
||||
d.symbols.push(vectorcraft_doc::Symbol { name: "S".into(), art: Arc::new(art) });
|
||||
d.symbols.push(vectorcraft_doc::Symbol { dynamic: false, name: "S".into(), art: Arc::new(art) });
|
||||
let style = CharStyle { size: 24.0, ..Default::default() };
|
||||
let mut t = TextObject::point(vectorcraft_geom::Point::new(10.0, 50.0), "A", style.clone());
|
||||
t.runs.push(TextRun::inline(InlineArt::new("S"), style.clone()));
|
||||
@@ -428,7 +428,7 @@ fn symbol_art(d: &mut Document) -> Node {
|
||||
fn symbol_doc() -> Document {
|
||||
let mut d = Document::new(200.0, 100.0);
|
||||
let art = symbol_art(&mut d);
|
||||
d.symbols.push(vectorcraft_doc::Symbol { name: "S".into(), art: Arc::new(art) });
|
||||
d.symbols.push(vectorcraft_doc::Symbol { dynamic: false, name: "S".into(), art: Arc::new(art) });
|
||||
let l = d.layers[0].id;
|
||||
let xfs = [
|
||||
Affine::translate((30.0, 30.0)),
|
||||
@@ -579,9 +579,9 @@ fn art_placed_again_in_every_frame_keeps_one_outline_per_stroke() {
|
||||
if let Some(c) = art.children_mut() {
|
||||
c.push(Arc::new(image));
|
||||
}
|
||||
d.symbols.push(vectorcraft_doc::Symbol { name: "I".into(), art: Arc::new(art) });
|
||||
d.symbols.push(vectorcraft_doc::Symbol { dynamic: false, name: "I".into(), art: Arc::new(art) });
|
||||
let art = symbol_art(&mut d);
|
||||
d.symbols.push(vectorcraft_doc::Symbol { name: "S".into(), art: Arc::new(art) });
|
||||
d.symbols.push(vectorcraft_doc::Symbol { dynamic: false, name: "S".into(), art: Arc::new(art) });
|
||||
let l = d.layers[0].id;
|
||||
let instance = |d: &mut Document, symbol: &str, x: f64, y: f64| {
|
||||
let id = d.alloc_id();
|
||||
@@ -707,7 +707,7 @@ fn strokes_of_brush_art_draw_as_a_new_renderer_does() {
|
||||
// Three instances show symbol `B`, a group of a wave. A fourth wave is a path of the document,
|
||||
// and a Mirror repeat draws a fifth wave and its reflection.
|
||||
let art = Node::group(d.alloc_id(), vec![Arc::new(wave(&mut d, (0.0, 0.0)))]);
|
||||
d.symbols.push(vectorcraft_doc::Symbol { name: "B".into(), art: Arc::new(art) });
|
||||
d.symbols.push(vectorcraft_doc::Symbol { dynamic: false, name: "B".into(), art: Arc::new(art) });
|
||||
let l = d.layers[0].id;
|
||||
for at in [(50.0, 30.0), (150.0, 30.0), (50.0, 70.0)] {
|
||||
let id = d.alloc_id();
|
||||
@@ -755,10 +755,10 @@ fn brushed_copies_doc() -> Document {
|
||||
use vectorcraft_doc::text::{CharStyle, InlineArt, TextObject, TextRun};
|
||||
let mut d = Document::new(200.0, 100.0);
|
||||
let line = brushed_line(&mut d);
|
||||
d.symbols.push(vectorcraft_doc::Symbol { name: "Line".into(), art: Arc::new(line) });
|
||||
d.symbols.push(vectorcraft_doc::Symbol { dynamic: false, name: "Line".into(), art: Arc::new(line) });
|
||||
let line = brushed_line(&mut d);
|
||||
let held = Node::group(d.alloc_id(), vec![Arc::new(line)]);
|
||||
d.symbols.push(vectorcraft_doc::Symbol { name: "Held".into(), art: Arc::new(held) });
|
||||
d.symbols.push(vectorcraft_doc::Symbol { dynamic: false, name: "Held".into(), art: Arc::new(held) });
|
||||
let l = d.layers[0].id;
|
||||
for (symbol, at) in [("Line", (20.0, 15.0)), ("Line", (60.0, 15.0)), ("Line", (100.0, 15.0)), ("Held", (20.0, 45.0)), ("Held", (60.0, 45.0))] {
|
||||
let id = d.alloc_id();
|
||||
@@ -856,7 +856,7 @@ fn brush_art_draws_edits_of_the_symbols_and_patterns_it_shows() {
|
||||
let doc = |dot_brushed: bool, fill_brushed: bool| {
|
||||
let mut d = Document::new(200.0, 100.0);
|
||||
let dot = art(&mut d, Paint::solid(Color::rgb(0.0, 0.0, 1.0)), dot_brushed);
|
||||
d.symbols.push(vectorcraft_doc::Symbol { name: "Dot".into(), art: Arc::new(dot) });
|
||||
d.symbols.push(vectorcraft_doc::Symbol { dynamic: false, name: "Dot".into(), art: Arc::new(dot) });
|
||||
let fill = art(&mut d, Paint::solid(Color::rgb(0.0, 0.6, 0.0)), fill_brushed);
|
||||
let mut def = PatternDef::new("Fill", vec![Arc::new(fill)]);
|
||||
def.tile = Rect::new(-6.0, -6.0, 6.0, 6.0);
|
||||
@@ -917,7 +917,7 @@ fn brush_art_draws_renamed_symbols_and_patterns_it_shows() {
|
||||
[("Dot", Paint::solid(Color::rgb(0.0, 0.0, 1.0)), false), ("Other", Paint::solid(Color::rgb(1.0, 0.0, 0.0)), true)]
|
||||
{
|
||||
let a = art(&mut d, paint, brushed);
|
||||
d.symbols.push(vectorcraft_doc::Symbol { name: name.into(), art: Arc::new(a) });
|
||||
d.symbols.push(vectorcraft_doc::Symbol { dynamic: false, name: name.into(), art: Arc::new(a) });
|
||||
}
|
||||
for (name, paint, brushed) in
|
||||
[("Fill", Paint::solid(Color::rgb(0.0, 0.6, 0.0)), false), ("Spare", Paint::solid(Color::rgb(1.0, 0.0, 0.0)), true)]
|
||||
@@ -978,7 +978,7 @@ fn live_objects_in_brush_art_keep_their_evaluations_between_frames() {
|
||||
};
|
||||
let held = bent(&mut d, (-60.0, 0.0), (60.0, 0.0));
|
||||
let held = Node::group(d.alloc_id(), vec![Arc::new(held)]);
|
||||
d.symbols.push(vectorcraft_doc::Symbol { name: "Wave".into(), art: Arc::new(held) });
|
||||
d.symbols.push(vectorcraft_doc::Symbol { dynamic: false, name: "Wave".into(), art: Arc::new(held) });
|
||||
let path = bent(&mut d, (20.0, 25.0), (180.0, 25.0));
|
||||
let l = d.layers[0].id;
|
||||
d.insert(Some(l), usize::MAX, path).unwrap();
|
||||
@@ -1006,7 +1006,7 @@ fn editing_an_instance_or_a_symbol_places_only_its_art_again() {
|
||||
let green = vectorcraft_doc::Appearance::basic(Paint::solid(Color::rgb(0.0, 1.0, 0.0)), Paint::None, 0.0);
|
||||
let square = Node::path(d.alloc_id(), vectorcraft_geom::shapes::rectangle(Rect::new(-5.0, -5.0, 5.0, 5.0)), green);
|
||||
let t = Node::group(d.alloc_id(), vec![Arc::new(square)]);
|
||||
d.symbols.push(vectorcraft_doc::Symbol { name: "T".into(), art: Arc::new(t) });
|
||||
d.symbols.push(vectorcraft_doc::Symbol { dynamic: false, name: "T".into(), art: Arc::new(t) });
|
||||
let (l, id) = (d.layers[0].id, d.alloc_id());
|
||||
d.insert(Some(l), 0, Node::new(id, NodeKind::SymbolInstance { symbol: "T".into(), xf: Affine::translate((30.0, 80.0)) })).unwrap();
|
||||
let opts = RenderOptions::default();
|
||||
@@ -1056,7 +1056,7 @@ fn instances_of_art_outside_the_square_follow_their_symbols_art() {
|
||||
// the one from x 200 lies from (40, 40) to (60, 60), in it. The 20 × 20 square around the
|
||||
// origin lies from (-170, 30) to (-150, 50), left of the view.
|
||||
let (off, on) = (square_at(&mut d, 100.0), square_at(&mut d, 200.0));
|
||||
d.symbols.push(vectorcraft_doc::Symbol { name: "Far".into(), art: off.clone() });
|
||||
d.symbols.push(vectorcraft_doc::Symbol { dynamic: false, name: "Far".into(), art: off.clone() });
|
||||
let (l, id) = (d.layers[0].id, d.alloc_id());
|
||||
d.insert(Some(l), 0, Node::new(id, NodeKind::SymbolInstance { symbol: "Far".into(), xf: Affine::translate((-160.0, 40.0)) })).unwrap();
|
||||
let opts = RenderOptions::default();
|
||||
@@ -1098,7 +1098,7 @@ fn draws_as_new(d: &mut Document, before: &RenderOptions, after: &RenderOptions,
|
||||
/// `d` with symbol `Holder` (a group of `members`) shown by one instance at (100, 50).
|
||||
fn holder_doc(mut d: Document, members: Vec<Node>) -> Document {
|
||||
let holder = Node::group(d.alloc_id(), members.into_iter().map(Arc::new).collect());
|
||||
d.symbols.push(vectorcraft_doc::Symbol { name: "Holder".into(), art: Arc::new(holder) });
|
||||
d.symbols.push(vectorcraft_doc::Symbol { dynamic: false, name: "Holder".into(), art: Arc::new(holder) });
|
||||
let (l, id) = (d.layers[0].id, d.alloc_id());
|
||||
d.insert(Some(l), 0, Node::new(id, NodeKind::SymbolInstance { symbol: "Holder".into(), xf: Affine::translate((100.0, 50.0)) })).unwrap();
|
||||
d
|
||||
@@ -1113,7 +1113,7 @@ fn art_showing_another_symbol_draws_its_edits() {
|
||||
use vectorcraft_doc::text::{CharStyle, InlineArt, TextObject, TextRun};
|
||||
let mut d = Document::new(200.0, 100.0);
|
||||
let art = symbol_art(&mut d);
|
||||
d.symbols.push(vectorcraft_doc::Symbol { name: "S".into(), art: Arc::new(art) });
|
||||
d.symbols.push(vectorcraft_doc::Symbol { dynamic: false, name: "S".into(), art: Arc::new(art) });
|
||||
let style = CharStyle { size: 24.0, ..Default::default() };
|
||||
let mut t = TextObject::point(vectorcraft_geom::Point::new(-60.0, 10.0), "A", style.clone());
|
||||
t.runs.push(TextRun::inline(InlineArt::new("S"), style.clone()));
|
||||
@@ -1234,7 +1234,7 @@ fn live_objects_in_symbol_art_keep_their_evaluations_between_frames() {
|
||||
for (name, art, at) in
|
||||
[("Bent", bent, (40.0, 25.0)), ("Envelope", envelope, (100.0, 25.0)), ("Blend", blend, (160.0, 25.0)), ("Grid", grid, (20.0, 60.0))]
|
||||
{
|
||||
d.symbols.push(vectorcraft_doc::Symbol { name: name.into(), art: Arc::new(art) });
|
||||
d.symbols.push(vectorcraft_doc::Symbol { dynamic: false, name: name.into(), art: Arc::new(art) });
|
||||
let id = d.alloc_id();
|
||||
d.insert(Some(l), usize::MAX, Node::new(id, NodeKind::SymbolInstance { symbol: name.into(), xf: Affine::translate(at) })).unwrap();
|
||||
}
|
||||
@@ -1298,7 +1298,7 @@ fn live_objects_in_symbol_art_draw_symbol_and_pattern_edits() {
|
||||
for bricks in [false, true] {
|
||||
let mut d = Document::new(200.0, 100.0);
|
||||
let art = symbol_art(&mut d);
|
||||
d.symbols.push(vectorcraft_doc::Symbol { name: "S".into(), art: Arc::new(art) });
|
||||
d.symbols.push(vectorcraft_doc::Symbol { dynamic: false, name: "S".into(), art: Arc::new(art) });
|
||||
let xf = Affine::translate((10.0, 10.0)) * Affine::scale(0.5);
|
||||
let dot = Node::new(d.alloc_id(), NodeKind::SymbolInstance { symbol: "S".into(), xf });
|
||||
let mut dots = PatternDef::new("Dots", vec![Arc::new(dot)]);
|
||||
@@ -1307,7 +1307,7 @@ fn live_objects_in_symbol_art_draw_symbol_and_pattern_edits() {
|
||||
let band = filled(&mut d, rectangle(Rect::new(-80.0, -30.0, 80.0, 30.0)), pattern_paint("Dots"), vec![]);
|
||||
let envelope = arc_envelope(&mut d, vec![band], true);
|
||||
let art = if grouped { Node::group(d.alloc_id(), vec![Arc::new(envelope)]) } else { envelope };
|
||||
d.symbols.push(vectorcraft_doc::Symbol { name: "Bent".into(), art: Arc::new(art) });
|
||||
d.symbols.push(vectorcraft_doc::Symbol { dynamic: false, name: "Bent".into(), art: Arc::new(art) });
|
||||
let (l, id) = (d.layers[0].id, d.alloc_id());
|
||||
d.insert(Some(l), 0, Node::new(id, NodeKind::SymbolInstance { symbol: "Bent".into(), xf: Affine::translate((100.0, 50.0)) })).unwrap();
|
||||
let opts = RenderOptions { background: Some([255, 255, 255, 255]), ..Default::default() };
|
||||
@@ -1401,7 +1401,7 @@ fn live_objects_draw_symbol_and_pattern_art_edits() {
|
||||
let with_s = || {
|
||||
let mut d = Document::new(200.0, 100.0);
|
||||
let art = symbol_art(&mut d);
|
||||
d.symbols.push(vectorcraft_doc::Symbol { name: "S".into(), art: Arc::new(art) });
|
||||
d.symbols.push(vectorcraft_doc::Symbol { dynamic: false, name: "S".into(), art: Arc::new(art) });
|
||||
d
|
||||
};
|
||||
let red_bar = |d: &mut Document| {
|
||||
@@ -1415,7 +1415,7 @@ fn live_objects_draw_symbol_and_pattern_art_edits() {
|
||||
let mut d = with_s();
|
||||
let shown = instance(&mut d, "S", Affine::IDENTITY);
|
||||
let a = Node::group(d.alloc_id(), vec![Arc::new(shown)]);
|
||||
d.symbols.push(vectorcraft_doc::Symbol { name: "A".into(), art: Arc::new(a) });
|
||||
d.symbols.push(vectorcraft_doc::Symbol { dynamic: false, name: "A".into(), art: Arc::new(a) });
|
||||
let a = instance(&mut d, "A", Affine::translate((100.0, 50.0)) * Affine::scale(2.0));
|
||||
let envelope = arc_envelope(&mut d, vec![a], false);
|
||||
edit_draws_as_new(with_art(d, vec![envelope]), red_bar);
|
||||
@@ -1430,7 +1430,7 @@ fn live_objects_draw_symbol_and_pattern_art_edits() {
|
||||
let mut d = Document::new(200.0, 100.0);
|
||||
d.patterns.push(checks(Color::rgb(1.0, 0.0, 0.0)));
|
||||
let band = filled(&mut d, rectangle(Rect::new(-80.0, -30.0, 80.0, 30.0)), pattern_paint("Checks"), vec![]);
|
||||
d.symbols.push(vectorcraft_doc::Symbol { name: "Banded".into(), art: Arc::new(band) });
|
||||
d.symbols.push(vectorcraft_doc::Symbol { dynamic: false, name: "Banded".into(), art: Arc::new(band) });
|
||||
let banded = instance(&mut d, "Banded", Affine::translate((100.0, 50.0)));
|
||||
let envelope = arc_envelope(&mut d, vec![banded], true);
|
||||
edit_draws_as_new(with_art(d, vec![envelope]), |d| {
|
||||
@@ -1457,7 +1457,7 @@ fn symbol_in_live_and_effect_art() -> Document {
|
||||
use vectorcraft_geom::shapes::rectangle;
|
||||
let mut d = Document::new(500.0, 100.0);
|
||||
let art = symbol_art(&mut d);
|
||||
d.symbols.push(vectorcraft_doc::Symbol { name: "S".into(), art: Arc::new(art) });
|
||||
d.symbols.push(vectorcraft_doc::Symbol { dynamic: false, name: "S".into(), art: Arc::new(art) });
|
||||
let bent = |d: &mut Document, x: f64| {
|
||||
let xf = Affine::translate((x, 50.0)) * Affine::scale(2.0);
|
||||
let s = Node::new(d.alloc_id(), NodeKind::SymbolInstance { symbol: "S".into(), xf });
|
||||
@@ -1470,7 +1470,7 @@ fn symbol_in_live_and_effect_art() -> Document {
|
||||
group.appearance.effects.push(drop_shadow());
|
||||
let copy = symbol_art(&mut d);
|
||||
let art = arc_envelope(&mut d, vec![copy], false);
|
||||
d.symbols.push(vectorcraft_doc::Symbol { name: "Bent".into(), art: Arc::new(art) });
|
||||
d.symbols.push(vectorcraft_doc::Symbol { dynamic: false, name: "Bent".into(), art: Arc::new(art) });
|
||||
let xf = Affine::translate((250.0, 50.0)) * Affine::scale(2.0);
|
||||
let symbol = Node::new(d.alloc_id(), NodeKind::SymbolInstance { symbol: "Bent".into(), xf });
|
||||
let mask = bent(&mut d, 350.0);
|
||||
@@ -1603,7 +1603,7 @@ fn objects_with_plugin_effects_follow_the_plugins_installed() {
|
||||
let mut d = Document::new(200.0, 100.0);
|
||||
let art = square(&mut d, -20.0, -20.0, vec![]);
|
||||
let art = Node::group(d.alloc_id(), vec![Arc::new(art)]);
|
||||
d.symbols.push(vectorcraft_doc::Symbol { name: "Shaped".into(), art: Arc::new(art) });
|
||||
d.symbols.push(vectorcraft_doc::Symbol { dynamic: false, name: "Shaped".into(), art: Arc::new(art) });
|
||||
let instance = Node::new(d.alloc_id(), NodeKind::SymbolInstance { symbol: "Shaped".into(), xf: Affine::translate((100.0, 50.0)) });
|
||||
plugin_change_draws_as_new(with_art(d, vec![instance]), ID, small.clone(), big);
|
||||
// Left of the view, the square is culled until the plug-in draws it in the view.
|
||||
@@ -1616,7 +1616,7 @@ fn objects_with_plugin_effects_follow_the_plugins_installed() {
|
||||
// effects in the art paint past the art, which the plug-in's first shape does not.
|
||||
let mut d = Document::new(200.0, 100.0);
|
||||
let art = square(&mut d, -20.0, -20.0, vec![]);
|
||||
d.symbols.push(vectorcraft_doc::Symbol { name: "Shaped".into(), art: Arc::new(art) });
|
||||
d.symbols.push(vectorcraft_doc::Symbol { dynamic: false, name: "Shaped".into(), art: Arc::new(art) });
|
||||
let instance = Node::new(d.alloc_id(), NodeKind::SymbolInstance { symbol: "Shaped".into(), xf: Affine::translate((-30.0, 50.0)) });
|
||||
plugin_change_draws_as_new(with_art(d, vec![instance]), ID, Some(triangle(-15.0, -15.0, 10.0)), Some(triangle(0.0, 0.0, 150.0)));
|
||||
}
|
||||
@@ -1676,7 +1676,7 @@ fn effect_art_follows_the_plugins_installed() {
|
||||
plugin_change_draws_as_new(group(vec![feather]), ID, small.clone(), big.clone());
|
||||
let mut d = Document::new(200.0, 100.0);
|
||||
let art = symbol_art(&mut d);
|
||||
d.symbols.push(vectorcraft_doc::Symbol { name: "S".into(), art: Arc::new(art) });
|
||||
d.symbols.push(vectorcraft_doc::Symbol { dynamic: false, name: "S".into(), art: Arc::new(art) });
|
||||
let mut instance = Node::new(d.alloc_id(), NodeKind::SymbolInstance { symbol: "S".into(), xf: Affine::translate((100.0, 50.0)) });
|
||||
instance.appearance.effects.push(plugin());
|
||||
plugin_change_draws_as_new(with_art(d, vec![instance]), ID, small.clone(), big.clone());
|
||||
@@ -1711,7 +1711,7 @@ fn live_objects_follow_the_plugins_installed() {
|
||||
// Symbol `Shaped` is a square with the plug-in effect.
|
||||
let mut d = Document::new(200.0, 100.0);
|
||||
let art = black(&mut d, Rect::new(-20.0, -20.0, 20.0, 20.0));
|
||||
d.symbols.push(vectorcraft_doc::Symbol { name: "Shaped".into(), art: Arc::new(art) });
|
||||
d.symbols.push(vectorcraft_doc::Symbol { dynamic: false, name: "Shaped".into(), art: Arc::new(art) });
|
||||
let instance = Node::new(d.alloc_id(), NodeKind::SymbolInstance { symbol: "Shaped".into(), xf: Affine::translate((100.0, 50.0)) });
|
||||
let envelope = arc_envelope(&mut d, vec![instance], true);
|
||||
plugin_change_draws_as_new(with_art(d, vec![envelope]), ID, small.clone(), big.clone());
|
||||
@@ -1753,7 +1753,7 @@ fn pattern_and_symbol_art_with_plugin_effects_follow_the_plugins_installed() {
|
||||
// the instance's box, from (80, 30) to (120, 70).
|
||||
let mut d = Document::new(200.0, 100.0);
|
||||
let art = filled(&mut d, rectangle(Rect::new(-20.0, -20.0, 20.0, 20.0)), Paint::solid(Color::BLACK), vec![plugin()]);
|
||||
d.symbols.push(vectorcraft_doc::Symbol { name: "Shaped".into(), art: Arc::new(art) });
|
||||
d.symbols.push(vectorcraft_doc::Symbol { dynamic: false, name: "Shaped".into(), art: Arc::new(art) });
|
||||
let instance = Node::new(d.alloc_id(), NodeKind::SymbolInstance { symbol: "Shaped".into(), xf: Affine::translate((100.0, 50.0)) });
|
||||
let mut group = Node::group(d.alloc_id(), vec![Arc::new(instance)]);
|
||||
group.appearance.effects.push(drop_shadow());
|
||||
@@ -1839,7 +1839,7 @@ fn pattern_tiles_draw_edits_to_what_their_art_shows() {
|
||||
let doc = |pattern: &str, effects: Vec<vectorcraft_doc::Effect>| {
|
||||
let mut d = Document::new(200.0, 100.0);
|
||||
let art = symbol_art(&mut d);
|
||||
d.symbols.push(vectorcraft_doc::Symbol { name: "S".into(), art: Arc::new(art) });
|
||||
d.symbols.push(vectorcraft_doc::Symbol { dynamic: false, name: "S".into(), art: Arc::new(art) });
|
||||
let shown = Node::new(d.alloc_id(), NodeKind::SymbolInstance { symbol: "S".into(), xf: Affine::translate((10.0, 10.0)) });
|
||||
d.patterns.push(tile("Stamps", shown));
|
||||
d.patterns.push(checks(Color::rgb(1.0, 0.0, 0.0)));
|
||||
@@ -1890,10 +1890,10 @@ fn rasters_draw_symbol_brush_and_image_edits() {
|
||||
// A group of an instance of symbol `A`, whose art shows symbol `S`, and a green square.
|
||||
let mut d = Document::new(200.0, 100.0);
|
||||
let art = symbol_art(&mut d);
|
||||
d.symbols.push(vectorcraft_doc::Symbol { name: "S".into(), art: Arc::new(art) });
|
||||
d.symbols.push(vectorcraft_doc::Symbol { dynamic: false, name: "S".into(), art: Arc::new(art) });
|
||||
let shown = Node::new(d.alloc_id(), NodeKind::SymbolInstance { symbol: "S".into(), xf: Affine::IDENTITY });
|
||||
let a = Node::group(d.alloc_id(), vec![Arc::new(shown)]);
|
||||
d.symbols.push(vectorcraft_doc::Symbol { name: "A".into(), art: Arc::new(a) });
|
||||
d.symbols.push(vectorcraft_doc::Symbol { dynamic: false, name: "A".into(), art: Arc::new(a) });
|
||||
let instance = Node::new(d.alloc_id(), NodeKind::SymbolInstance { symbol: "A".into(), xf: Affine::translate((50.0, 50.0)) });
|
||||
let green = vectorcraft_doc::Appearance::basic(Paint::solid(Color::rgb(0.0, 0.8, 0.0)), Paint::None, 0.0);
|
||||
let square = Node::path(d.alloc_id(), rectangle(Rect::new(120.0, 30.0, 160.0, 70.0)), green);
|
||||
@@ -2067,7 +2067,7 @@ fn proof_doc() -> Document {
|
||||
let spot = filled(&mut d, rectangle(Rect::new(110.0, 10.0, 150.0, 50.0)), spot, vec![blur]);
|
||||
let yellow = filled(&mut d, rectangle(Rect::new(-10.0, -10.0, 10.0, 10.0)), Paint::solid(Color::cmyk(0.0, 0.0, 1.0, 0.0)), vec![]);
|
||||
let art = Node::group(d.alloc_id(), vec![Arc::new(yellow)]);
|
||||
d.symbols.push(vectorcraft_doc::Symbol { name: "S".into(), art: Arc::new(art) });
|
||||
d.symbols.push(vectorcraft_doc::Symbol { dynamic: false, name: "S".into(), art: Arc::new(art) });
|
||||
let instance = Node::new(d.alloc_id(), NodeKind::SymbolInstance { symbol: "S".into(), xf: Affine::translate((175.0, 75.0)) });
|
||||
with_art(d, vec![cyan, circle, white, spot, instance])
|
||||
}
|
||||
@@ -2228,7 +2228,7 @@ fn symbol_instances_draw_as_groups_of_their_placed_art() {
|
||||
let circle = vectorcraft_geom::shapes::ellipse(Rect::new(-6.0, -6.0, 6.0, 6.0));
|
||||
let ap = vectorcraft_doc::Appearance::basic(Paint::solid(Color::rgb(1.0, 0.8, 0.0)), Paint::solid(Color::BLACK), 1.0);
|
||||
let circle = Node::path(d.alloc_id(), circle, ap);
|
||||
d.symbols.push(vectorcraft_doc::Symbol { name: "U".into(), art: Arc::new(circle) });
|
||||
d.symbols.push(vectorcraft_doc::Symbol { dynamic: false, name: "U".into(), art: Arc::new(circle) });
|
||||
let id = d.alloc_id();
|
||||
d.insert(Some(l), usize::MAX, Node::new(id, NodeKind::SymbolInstance { symbol: "U".into(), xf: Affine::translate((175.0, 25.0)) })).unwrap();
|
||||
// `groups` holds the same art in place of the instances: groups, and the circle as a path.
|
||||
@@ -2272,7 +2272,7 @@ fn inline_graphics_keep_their_placed_art_between_frames() {
|
||||
use vectorcraft_doc::text::{CharStyle, InlineArt, TextObject, TextRun};
|
||||
let mut d = Document::new(200.0, 100.0);
|
||||
let art = symbol_art(&mut d);
|
||||
d.symbols.push(vectorcraft_doc::Symbol { name: "S".into(), art: Arc::new(art) });
|
||||
d.symbols.push(vectorcraft_doc::Symbol { dynamic: false, name: "S".into(), art: Arc::new(art) });
|
||||
let style = CharStyle { size: 24.0, ..Default::default() };
|
||||
let mut t = TextObject::point(vectorcraft_geom::Point::new(10.0, 50.0), "A", style.clone());
|
||||
t.runs.push(TextRun::inline(InlineArt::new("S"), style.clone()));
|
||||
|
||||
@@ -300,7 +300,7 @@ fn symbol_doc(effects: Vec<Effect>) -> Document {
|
||||
shapes::rectangle(Rect::new(-10.0, -10.0, 10.0, 10.0)),
|
||||
Appearance::basic(Paint::solid(Color::BLACK), Paint::None, 0.0),
|
||||
);
|
||||
d.symbols.push(Symbol { name: "Square".into(), art: Arc::new(art) });
|
||||
d.symbols.push(Symbol { dynamic: false, name: "Square".into(), art: Arc::new(art) });
|
||||
let mut n = Node::new(NodeId(0), NodeKind::SymbolInstance { symbol: "Square".into(), xf: Affine::translate((50.0, 50.0)) });
|
||||
n.appearance.effects = effects;
|
||||
doc_with(d, n)
|
||||
@@ -469,7 +469,7 @@ fn a_transform_effect_on_a_group_symbol_or_envelope_moves_and_scales_the_gradien
|
||||
t.id = NodeId(500);
|
||||
// The type in a group, as the art of a symbol, and in an envelope that leaves it as it is.
|
||||
let mut symbols = Document::new(100.0, 100.0);
|
||||
symbols.symbols.push(Symbol { name: "Type".into(), art: Arc::new(t.clone()) });
|
||||
symbols.symbols.push(Symbol { dynamic: false, name: "Type".into(), art: Arc::new(t.clone()) });
|
||||
let holders = [
|
||||
doc_with(Document::new(100.0, 100.0), Node::group(NodeId(0), vec![Arc::new(t.clone())])),
|
||||
doc_with(symbols, Node::new(NodeId(0), NodeKind::SymbolInstance { symbol: "Type".into(), xf: Affine::IDENTITY })),
|
||||
|
||||
@@ -281,7 +281,11 @@ fn a_ready_bitmap_draws_again_where_its_file_shows() {
|
||||
let brush = vectorcraft_brush::Brush { name: "Cards".into(), kind: vectorcraft_brush::BrushKind::Scatter(scatter) };
|
||||
vectorcraft_brush::store(&mut brushed, &[brush]);
|
||||
assert_eq!(placed_area(&brushed, &only_a), Changed::Everything);
|
||||
d.symbols.push(vectorcraft_doc::Symbol { name: "Card".into(), art: Arc::new(placed_node(65, "area-a", 40.0, 20.0, Affine::IDENTITY)) });
|
||||
d.symbols.push(vectorcraft_doc::Symbol {
|
||||
dynamic: false,
|
||||
name: "Card".into(),
|
||||
art: Arc::new(placed_node(65, "area-a", 40.0, 20.0, Affine::IDENTITY)),
|
||||
});
|
||||
assert_eq!(placed_area(&d, &only_a), Changed::Everything);
|
||||
}
|
||||
|
||||
|
||||
@@ -310,7 +310,7 @@ fn rasterize_and_shadows_paint_in_stack_order() {
|
||||
Appearance::basic(Paint::solid(Color::rgb(1.0, 0.0, 0.0)), Paint::None, 0.0),
|
||||
);
|
||||
art.appearance.effects.push(Effect { id: shadow.0.into(), params: shadow.1.clone(), visible: true });
|
||||
d.symbols.push(vectorcraft_doc::Symbol { name: "Shadowed".into(), art: Arc::new(art) });
|
||||
d.symbols.push(vectorcraft_doc::Symbol { dynamic: false, name: "Shadowed".into(), art: Arc::new(art) });
|
||||
let mut instance = Node::new(d.alloc_id(), NodeKind::SymbolInstance { symbol: "Shadowed".into(), xf: Affine::translate((20.0, 20.0)) });
|
||||
instance.appearance.effects.push(Effect { id: "rasterize".into(), params: json!({}), visible: true });
|
||||
let l = d.layers[0].id;
|
||||
|
||||
@@ -214,7 +214,7 @@ fn type_with_an_inline_graphic_is_drawn_as_sharp() {
|
||||
let blue = Appearance::basic(Paint::solid(Color::rgb(0.0, 0.0, 1.0)), Paint::None, 0.0);
|
||||
let mut art = Node::path(d.alloc_id(), rectangle(Rect::new(0.0, -30.0, 30.0, 0.0)), blue);
|
||||
art.blend = vectorcraft_color::BlendMode::Multiply;
|
||||
d.symbols.push(vectorcraft_doc::Symbol { name: "S".into(), art: Arc::new(art) });
|
||||
d.symbols.push(vectorcraft_doc::Symbol { dynamic: false, name: "S".into(), art: Arc::new(art) });
|
||||
let style = CharStyle { size: 40.0, ..Default::default() };
|
||||
let mut t = TextObject::point(Point::new(10.3, 60.4), "I", style.clone());
|
||||
t.runs.push(TextRun::inline(InlineArt::new("S"), style));
|
||||
|
||||
@@ -0,0 +1,63 @@
|
||||
//! A stroke with a width profile keeps its shape when the view zooms out (#995): only a stroke too
|
||||
//! thin to see is thickened, and then by what its widest point needs, not all of it at once.
|
||||
|
||||
use vectorcraft_color::Paint;
|
||||
use vectorcraft_doc::{Appearance, AppearanceItem, Node, StrokeLayer, WidthProfile};
|
||||
use vectorcraft_geom::PathData;
|
||||
|
||||
use crate::*;
|
||||
|
||||
const SIZE: u32 = 200;
|
||||
|
||||
/// A line across a 200×200 page, stroked black `weight` pt with `profile`, drawn at `scale`.
|
||||
fn render(weight: f64, profile: Option<WidthProfile>, scale: f64) -> Rendered {
|
||||
let mut st = StrokeLayer::new(Paint::solid(vectorcraft_color::Color::BLACK), weight);
|
||||
st.profile = profile;
|
||||
let mut p = BezPath::new();
|
||||
p.move_to((20.0, 100.0));
|
||||
p.line_to((180.0, 100.0));
|
||||
let mut d = Document::new(SIZE as f64, SIZE as f64);
|
||||
let n = Node::path(d.alloc_id(), PathData::from_bezpath(&p), Appearance { items: vec![AppearanceItem::Stroke(st)], ..Default::default() });
|
||||
let l = d.layers[0].id;
|
||||
d.insert(Some(l), 0, n).unwrap();
|
||||
Renderer::new().render(&d, SIZE, SIZE, Affine::scale(scale), &RenderOptions { background: Some([255, 255, 255, 255]), ..Default::default() })
|
||||
}
|
||||
|
||||
/// Device pixels of ink in the column through the middle of the line (coverage summed, so a
|
||||
/// stroke narrower than a pixel still counts what it covers).
|
||||
fn ink_at_middle(img: &Rendered, scale: f64) -> f64 {
|
||||
let x = (100.0 * scale) as u32;
|
||||
(0..SIZE).map(|y| 1.0 - img.pixel(x, y)[0] as f64 / 255.0).sum()
|
||||
}
|
||||
|
||||
/// Thin at both ends, six times the weight in the middle.
|
||||
fn swell() -> WidthProfile {
|
||||
WidthProfile { points: vec![(0.0, 1.0, 1.0), (0.5, 6.0, 6.0), (1.0, 1.0, 1.0)] }
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_thin_width_stroke_keeps_its_thickness_in_document_terms_when_zoomed_out() {
|
||||
let whole = ink_at_middle(&render(1.0, Some(swell()), 1.0), 1.0);
|
||||
assert!((whole - 6.0).abs() < 0.3, "six points at full size: {whole}");
|
||||
for scale in [0.5, 0.25, 0.125] {
|
||||
let ink = ink_at_middle(&render(1.0, Some(swell()), scale), scale);
|
||||
let in_points = ink / scale;
|
||||
assert!((in_points - whole).abs() < whole * 0.05, "at {scale}: {in_points} pt against {whole} pt");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_stroke_too_thin_to_see_is_kept_visible_by_its_widest_point() {
|
||||
// 0.1 pt, twice that in the middle: at a quarter size even the middle is a twentieth of a
|
||||
// pixel, so it is thickened to the half pixel a plain hairline gets, and the profile's ends
|
||||
// stay half of it.
|
||||
let thin = WidthProfile { points: vec![(0.0, 0.5, 0.5), (0.5, 1.0, 1.0), (1.0, 0.5, 0.5)] };
|
||||
let plain = ink_at_middle(&render(0.1, None, 0.25), 0.25);
|
||||
let img = render(0.1, Some(thin), 0.25);
|
||||
let shaped = ink_at_middle(&img, 0.25);
|
||||
assert!((plain - 0.5).abs() < 0.1, "a plain hairline gets half a pixel: {plain}");
|
||||
assert!((shaped - 0.5).abs() < 0.1, "the widest point of a profile gets it too: {shaped}");
|
||||
// Seven pixels in, 28 pt along the page: just past the line's start at 20 pt.
|
||||
let end: f64 = (0..SIZE).map(|y| 1.0 - img.pixel(7, y)[0] as f64 / 255.0).sum();
|
||||
assert!(end < shaped * 0.75, "the thin end stays thinner than the middle: {end} against {shaped}");
|
||||
}
|
||||
@@ -48,17 +48,28 @@ impl TextGeom {
|
||||
/// The boxes of the glyphs of `t` (the text this geometry is of) that optional OpenType features
|
||||
/// chose (Preferences › Highlight Alternate Glyphs): candidates the text's required shaping
|
||||
/// alone doesn't give ([`required_shaping`]). Arabic letters joining, Indic conjuncts and the
|
||||
/// lam-alef ligature are what the script needs, not alternates (the lam-alef ligature also when
|
||||
/// a font files it under `liga`: [`is_lam_alef`]). Measured once.
|
||||
/// lam-alef ligature are what the script needs, not alternates. A font may file the lam-alef
|
||||
/// ligature under `liga`, which required shaping turns off, so a lam-alef cluster
|
||||
/// ([`is_lam_alef`]) is also compared with its shaping with `liga` left on: an optional feature
|
||||
/// that changes its glyph beyond that (a stylistic alternate…) is still highlighted. Measured
|
||||
/// once.
|
||||
pub(crate) fn alternate_glyphs(&self, t: &TextObject) -> &BezPath {
|
||||
self.alternate_glyphs.get_or_init(|| {
|
||||
let mut out = BezPath::new();
|
||||
if self.alternate_candidates.is_empty() {
|
||||
return out;
|
||||
}
|
||||
let required = required_shaping(t);
|
||||
let required = required_shaping(t, &["-liga", "-calt"]);
|
||||
let plain = t.plain_text();
|
||||
let in_lam_alef = |c: &AlternateCandidate| plain.get(c.byte..c.byte.saturating_add(c.len)).is_some_and(is_lam_alef);
|
||||
// Laid out only when the text has a lam-alef candidate.
|
||||
let with_liga = self.alternate_candidates.iter().any(in_lam_alef).then(|| required_shaping(t, &["-calt"]));
|
||||
let gives = |map: &std::collections::HashMap<(usize, usize), Vec<(u32, u32)>>, c: &AlternateCandidate| {
|
||||
map.get(&(c.byte, c.len)).is_some_and(|glyphs| glyphs.contains(&(c.font_id, c.gid)))
|
||||
};
|
||||
for c in &self.alternate_candidates {
|
||||
if !required.get(&(c.byte, c.len)).is_some_and(|glyphs| glyphs.contains(&(c.font_id, c.gid))) {
|
||||
let lam_alef_ligature = || in_lam_alef(c) && with_liga.as_ref().is_some_and(|m| gives(m, c));
|
||||
if !gives(&required, c) && !lam_alef_ligature() {
|
||||
out.extend(c.cell.iter());
|
||||
}
|
||||
}
|
||||
@@ -86,12 +97,14 @@ pub(crate) fn is_opentype_alternate(nominal_gid: u32, shaped_gid: u32, cluster_c
|
||||
}
|
||||
|
||||
/// Whether `cluster` is a lam followed by an alef (plain, with madda, or with hamza above or below),
|
||||
/// with any Arabic marks between or after them: the lam-alef ligature, which the Unicode
|
||||
/// Standard's Arabic chapter treats as obligatory, not a stylistic choice. Fonts made before
|
||||
/// `rlig` was common put it in `liga`, which [`required_shaping`] turns off, so it is required
|
||||
/// by its characters whatever feature the font files it under.
|
||||
/// with any combining marks (Unicode general category M: the Arabic harakat, Quranic marks and
|
||||
/// those of Arabic Extended-A, -B and -C) between or after them: the lam-alef ligature, which the
|
||||
/// Unicode Standard's Arabic chapter treats as obligatory, not a stylistic choice. Fonts made
|
||||
/// before `rlig` was common put it in `liga`, which [`required_shaping`] turns off, so
|
||||
/// [`TextGeom::alternate_glyphs`] also accepts its glyph from shaping with `liga` on.
|
||||
pub(crate) fn is_lam_alef(cluster: &str) -> bool {
|
||||
let is_mark = |c: &char| matches!(c, '\u{0610}'..='\u{061A}' | '\u{064B}'..='\u{065F}' | '\u{0670}' | '\u{06D6}'..='\u{06DC}' | '\u{06DF}'..='\u{06E4}' | '\u{06E7}' | '\u{06E8}' | '\u{06EA}'..='\u{06ED}');
|
||||
use unicode_general_category::{GeneralCategory as G, get_general_category};
|
||||
let is_mark = |c: &char| matches!(get_general_category(*c), G::NonspacingMark | G::SpacingMark | G::EnclosingMark);
|
||||
let mut letters = cluster.chars().filter(|c| !is_mark(c));
|
||||
matches!((letters.next(), letters.next(), letters.next()), (Some('\u{0644}'), Some('\u{0622}' | '\u{0623}' | '\u{0625}' | '\u{0627}'), None))
|
||||
}
|
||||
@@ -108,14 +121,16 @@ pub(crate) struct AlternateCandidate {
|
||||
}
|
||||
|
||||
/// The glyphs `t` is shaped to by required shaping alone, by cluster (`byte`, `len`) → `(font,
|
||||
/// glyph)`: laid out again with every optional feature off (standard and contextual ligatures and
|
||||
/// alternates, and the features its characters turn on: stylistic sets, swashes, discretionary
|
||||
/// ligatures, figure styles…). What is left is what the script needs: `ccmp`, `locl`, `rlig`,
|
||||
/// Arabic's `init`, `medi`, `fina` and `isol`, the Indic and other complex scripts' required sets.
|
||||
fn required_shaping(t: &TextObject) -> std::collections::HashMap<(usize, usize), Vec<(u32, u32)>> {
|
||||
/// glyph)`: laid out again with every optional feature off (with `features` `-liga` and `-calt`:
|
||||
/// standard and contextual ligatures and alternates, and the features its characters turn on:
|
||||
/// stylistic sets, swashes, discretionary ligatures, figure styles…). What is left is what the
|
||||
/// script needs: `ccmp`, `locl`, `rlig`, Arabic's `init`, `medi`, `fina` and `isol`, the Indic
|
||||
/// and other complex scripts' required sets. With `features` `-calt` alone, standard ligatures
|
||||
/// stay on (an older font's lam-alef).
|
||||
fn required_shaping(t: &TextObject, features: &[&str]) -> std::collections::HashMap<(usize, usize), Vec<(u32, u32)>> {
|
||||
let mut plain = t.clone();
|
||||
for run in &mut plain.runs {
|
||||
run.style.features = vec!["-liga".into(), "-calt".into()];
|
||||
run.style.features = features.iter().map(|f| (*f).into()).collect();
|
||||
}
|
||||
let layout = vectorcraft_text::layout(vectorcraft_text::FontDb::global(), &plain);
|
||||
let mut glyphs: std::collections::HashMap<(usize, usize), Vec<(u32, u32)>> = std::collections::HashMap::new();
|
||||
@@ -193,7 +208,7 @@ pub(crate) fn text_geom_snapped(t: &TextObject, snap: Option<Affine>) -> TextGeo
|
||||
let cluster_chars = cluster.chars().count();
|
||||
let Some(first) = cluster.chars().next() else { continue };
|
||||
let nominal = db.face_by_id(g.font_id).map(|f| f.glyph_for(first)).unwrap_or(0);
|
||||
if is_opentype_alternate(nominal, g.gid, cluster_chars) && !is_lam_alef(cluster) {
|
||||
if is_opentype_alternate(nominal, g.gid, cluster_chars) {
|
||||
alternate_candidates.push(AlternateCandidate { byte: g.byte, len: g.len, font_id: g.font_id, gid: g.gid, cell });
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,14 +9,25 @@ pub(crate) fn single_threaded_context(w: u16, h: u16) -> RenderContext {
|
||||
/// Preferred rasterizer thread count set by the app (Preferences → Performance); negative = automatic.
|
||||
static THREADS_OVERRIDE: std::sync::atomic::AtomicI32 = std::sync::atomic::AtomicI32::new(-1);
|
||||
|
||||
/// The cores the process may run on, as the app read them from the system; 0 = not known.
|
||||
static USABLE_CORES: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);
|
||||
|
||||
/// Override the worker-thread count used by renderers created from now on (`None` = automatic).
|
||||
pub fn set_default_threads(n: Option<u16>) {
|
||||
THREADS_OVERRIDE.store(n.map_or(-1, i32::from), std::sync::atomic::Ordering::Relaxed);
|
||||
}
|
||||
|
||||
/// Rasterizer worker threads: 0 on wasm; otherwise one per core up to 8, leaving a core free. On a
|
||||
/// 10-core machine 8 workers render a 50,000-path page 1.4× faster than 4 (9 are slower again);
|
||||
/// the pixels don't depend on the count (`pixels_do_not_depend_on_the_thread_count`).
|
||||
/// The number of cores the process may run on, when the app knows a smaller number than
|
||||
/// `available_parallelism` gives (`None` = not known). On Windows `available_parallelism` counts
|
||||
/// every processor in the machine, also when the process's affinity mask allows fewer.
|
||||
pub fn set_usable_cores(n: Option<usize>) {
|
||||
USABLE_CORES.store(n.unwrap_or(0), std::sync::atomic::Ordering::Relaxed);
|
||||
}
|
||||
|
||||
/// Rasterizer worker threads: 0 on wasm; otherwise one per core the process may run on, up to 8,
|
||||
/// leaving a core free. On a 10-core machine 8 workers render a 50,000-path page 1.4× faster than 4
|
||||
/// (9 are slower again); the pixels don't depend on the count
|
||||
/// (`pixels_do_not_depend_on_the_thread_count`).
|
||||
pub fn default_threads() -> u16 {
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
{
|
||||
@@ -31,6 +42,31 @@ pub fn default_threads() -> u16 {
|
||||
if std::env::var_os("VECTORCRAFT_RENDER_THREADS").is_some() {
|
||||
return std::env::var("VECTORCRAFT_RENDER_THREADS").ok().and_then(|v| v.parse().ok()).unwrap_or(0);
|
||||
}
|
||||
std::thread::available_parallelism().map(|n| (n.get().saturating_sub(1)).min(8) as u16).unwrap_or(0)
|
||||
let usable = USABLE_CORES.load(std::sync::atomic::Ordering::Relaxed);
|
||||
std::thread::available_parallelism().map(|n| automatic_threads(n.get(), usable)).unwrap_or(0)
|
||||
}
|
||||
}
|
||||
|
||||
/// [`default_threads`] when automatic, on `cpus` processors of which the process may use `usable`
|
||||
/// (0 = all).
|
||||
fn automatic_threads(cpus: usize, usable: usize) -> u16 {
|
||||
let cores = if usable == 0 { cpus } else { cpus.min(usable) };
|
||||
(cores.saturating_sub(1)).min(8) as u16
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::automatic_threads;
|
||||
|
||||
/// A process that may run on one core renders on the calling thread. With workers, vello_cpu's
|
||||
/// flush spins on that core while the workers it waits for get no time, and a render crawls or
|
||||
/// stalls.
|
||||
#[test]
|
||||
fn workers_follow_the_cores_the_process_may_use() {
|
||||
assert_eq!(automatic_threads(24, 0), 8);
|
||||
assert_eq!(automatic_threads(24, 1), 0);
|
||||
assert_eq!(automatic_threads(24, 4), 3);
|
||||
assert_eq!(automatic_threads(4, 16), 3);
|
||||
assert_eq!(automatic_threads(1, 0), 0);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user