diff --git a/soft-skia-wasm/src/lib.rs b/soft-skia-wasm/src/lib.rs index 100a2334..59c6213c 100644 --- a/soft-skia-wasm/src/lib.rs +++ b/soft-skia-wasm/src/lib.rs @@ -221,6 +221,11 @@ impl SoftSkiaWASM { Self::recursive_rasterization_node_to_pixmap(item, pixmap); } + if let Some(context) = context { + if let Some(mask) = &context.mask { + pixmap.apply_mask(mask) + } + } } #[wasm_bindgen(js_name = setAttrBySerde)] @@ -339,7 +344,13 @@ impl SoftSkiaWASM { }) => { let color = parse_color(color); let style = parse_style(style); - let mut clip = GroupClip::default(); + let provider = self.0.get_tree_node_by_id(id).unwrap().provider.as_ref(); + // reuse original clip + let mut clip = if let Some(Providers::G(group)) = provider { + group.clip.clone().unwrap_or_default() + } else { + GroupClip::default() + }; clip.invert = invert_clip; self.0.set_provider_to_child( @@ -406,6 +417,28 @@ impl SoftSkiaWASM { }), ) } + WASMShapesAttr::T(WASMTextAttr { + x, + y, + text, + font_size, + color, + max_width, + }) => { + let color = parse_color(color); + let font_size = font_size.unwrap_or(16.0); + self.0.set_shape_to_child( + id, + Shapes::T(Text { + text, + x, + y, + font_size, + color, + max_width, + }), + ) + } }; } } diff --git a/soft-skia/src/provider.rs b/soft-skia/src/provider.rs index a7906790..2c82d66a 100644 --- a/soft-skia/src/provider.rs +++ b/soft-skia/src/provider.rs @@ -29,7 +29,7 @@ impl Providers { } } -#[derive(Debug)] +#[derive(Debug, Default, Clone)] pub struct GroupClip { pub id: Option, pub invert: Option, diff --git a/soft-skia/src/shape.rs b/soft-skia/src/shape.rs index e5479f76..c090842b 100644 --- a/soft-skia/src/shape.rs +++ b/soft-skia/src/shape.rs @@ -2,14 +2,12 @@ use std::collections::HashMap; use fontdue::{ layout::{CoordinateSystem, Layout, LayoutSettings, TextStyle}, - Font, Metrics, + Font, }; use std::iter::zip; pub use tiny_skia::{ColorU8, FillRule, Mask, Paint, PathBuilder, Pixmap, Stroke, Transform}; use tiny_skia::{LineCap, LineJoin, Path, PixmapPaint}; -use crate::log; - #[derive(Debug)] pub enum Shapes { R(Rect), @@ -169,7 +167,6 @@ impl Shape for Rect { color, style, stroke_width, - mask, .. } = context; @@ -180,7 +177,6 @@ impl Shape for Rect { .color .unwrap_or(color.unwrap_or(ColorU8::from_rgba(0, 0, 0, 255))); let style = self.style.unwrap_or(style.unwrap_or(PaintStyle::Fill)); - let mask = mask.as_ref(); paint.set_color_rgba8(color.red(), color.green(), color.blue(), color.alpha()); @@ -192,7 +188,7 @@ impl Shape for Rect { stroke.width = w as f32 } - pixmap.stroke_path(&path, &paint, &stroke, Transform::identity(), mask); + pixmap.stroke_path(&path, &paint, &stroke, Transform::identity(), None); } PaintStyle::Fill => { paint.anti_alias = true; @@ -201,7 +197,7 @@ impl Shape for Rect { &paint, FillRule::Winding, Transform::identity(), - mask, + None, ); } _ => {} @@ -239,7 +235,6 @@ impl Shape for Circle { color, style, stroke_width, - mask, .. } = context; @@ -250,7 +245,6 @@ impl Shape for Circle { .color .unwrap_or(color.unwrap_or(ColorU8::from_rgba(0, 0, 0, 255))); let style = self.style.unwrap_or(style.unwrap_or(PaintStyle::Fill)); - let mask = mask.as_ref(); paint.set_color_rgba8(color.red(), color.green(), color.blue(), color.alpha()); paint.anti_alias = true; @@ -263,7 +257,7 @@ impl Shape for Circle { stroke.width = w as f32 } - pixmap.stroke_path(&path, &paint, &stroke, Transform::identity(), mask); + pixmap.stroke_path(&path, &paint, &stroke, Transform::identity(), None); } PaintStyle::Fill => { paint.anti_alias = true; @@ -272,7 +266,7 @@ impl Shape for Circle { &paint, FillRule::Winding, Transform::identity(), - mask, + None, ); } _ => {} @@ -306,7 +300,6 @@ impl Shape for RoundRect { color, style, stroke_width, - mask, .. } = context; @@ -317,7 +310,6 @@ impl Shape for RoundRect { .color .unwrap_or(color.unwrap_or(ColorU8::from_rgba(0, 0, 0, 255))); let style = self.style.unwrap_or(style.unwrap_or(PaintStyle::Fill)); - let mask = mask.as_ref(); paint.set_color_rgba8(color.red(), color.green(), color.blue(), color.alpha()); paint.anti_alias = true; @@ -330,7 +322,7 @@ impl Shape for RoundRect { stroke.width = w as f32 } - pixmap.stroke_path(&path, &paint, &stroke, Transform::identity(), mask); + pixmap.stroke_path(&path, &paint, &stroke, Transform::identity(), None); } PaintStyle::Fill => { paint.anti_alias = true; @@ -339,7 +331,7 @@ impl Shape for RoundRect { &paint, FillRule::Winding, Transform::identity(), - mask, + None, ); } _ => {} @@ -395,7 +387,6 @@ impl Shape for Line { let DrawContext { color, stroke_width, - mask, .. } = context; @@ -406,7 +397,6 @@ impl Shape for Line { .color .unwrap_or(color.unwrap_or(ColorU8::from_rgba(0, 0, 0, 255))); let stroke_width = self.stroke_width.unwrap_or(stroke_width.unwrap_or(1)); - let mask = mask.as_ref(); let stroke = Stroke { width: stroke_width as f32, @@ -417,7 +407,7 @@ impl Shape for Line { }; paint.set_color_rgba8(color.red(), color.green(), color.blue(), color.alpha()); - pixmap.stroke_path(&path, &paint, &stroke, Transform::identity(), mask); + pixmap.stroke_path(&path, &paint, &stroke, Transform::identity(), None); } fn get_path( @@ -448,7 +438,6 @@ impl Shape for Points { color, style, stroke_width, - mask, .. } = context; @@ -460,7 +449,6 @@ impl Shape for Points { .unwrap_or(color.unwrap_or(ColorU8::from_rgba(0, 0, 0, 255))); let style = self.style.unwrap_or(style.unwrap_or(PaintStyle::Fill)); let stroke_width = self.stroke_width.unwrap_or(stroke_width.unwrap_or(1)); - let mask = mask.as_ref(); paint.set_color_rgba8(color.red(), color.green(), color.blue(), color.alpha()); @@ -473,7 +461,7 @@ impl Shape for Points { line_join: LineJoin::Miter, dash: None, }; - pixmap.stroke_path(&path, &paint, &stroke, Transform::identity(), mask); + pixmap.stroke_path(&path, &paint, &stroke, Transform::identity(), None); } PaintStyle::Fill => { paint.anti_alias = true; @@ -482,7 +470,7 @@ impl Shape for Points { &paint, FillRule::Winding, Transform::identity(), - mask, + None, ); } _ => {} @@ -587,7 +575,6 @@ impl Shape for Text { rgba_bitmap.extend([0, 0, 0, bitmap[i]].iter()); } } - let p = Pixmap::from_vec( rgba_bitmap, tiny_skia::IntSize::from_wh(dim.0 as u32, dim.1 as u32).unwrap(),