diff --git a/package.json b/package.json index e02b3da..b863eea 100644 --- a/package.json +++ b/package.json @@ -3,7 +3,10 @@ "version": "0.1.0", "private": "true", "scripts": { - "serve": "pnpm -C vue-playground serve" + "serve": "pnpm --filter vue-playground serve", + "build": "pnpm build:wasm && pnpm build:vue", + "build:vue": "pnpm --filter vue-skia build", + "build:wasm": "cd soft-skia-wasm && wasm-pack build" }, "packageManager": "pnpm@7.32.0" } diff --git a/soft-skia-wasm/src/lib.rs b/soft-skia-wasm/src/lib.rs index d38c16c..fa87b87 100644 --- a/soft-skia-wasm/src/lib.rs +++ b/soft-skia-wasm/src/lib.rs @@ -100,7 +100,8 @@ pub struct WASMTextAttr { x: i32, y: i32, font_size: Option, - color: Option + color: Option, + max_width: Option, } @@ -294,11 +295,12 @@ impl SoftSkiaWASM { y, text, font_size, - color + 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 })) + self.0.set_shape_to_child(id, Shapes::T(Text { text, x, y, font_size, color, max_width })) } }; } diff --git a/soft-skia/Cargo.toml b/soft-skia/Cargo.toml index 8c36ca0..d0d25a2 100644 --- a/soft-skia/Cargo.toml +++ b/soft-skia/Cargo.toml @@ -11,4 +11,10 @@ license = "MIT" png = "0.17.5" tiny-skia = "0.10.0" base64 = "0.21.0" -fontdue = "0.7.3" \ No newline at end of file +fontdue = "0.7.3" + +[dependencies.web-sys] +version = "0.3" +features = [ + "console", +] \ No newline at end of file diff --git a/soft-skia/src/lib.rs b/soft-skia/src/lib.rs index f4e3d10..e222b44 100644 --- a/soft-skia/src/lib.rs +++ b/soft-skia/src/lib.rs @@ -1,4 +1,5 @@ pub mod tree; pub mod shape; pub mod instance; -pub mod provider; \ No newline at end of file +pub mod provider; +mod util; \ No newline at end of file diff --git a/soft-skia/src/shape.rs b/soft-skia/src/shape.rs index 8ffe05e..bc3e205 100644 --- a/soft-skia/src/shape.rs +++ b/soft-skia/src/shape.rs @@ -5,6 +5,8 @@ pub use tiny_skia::{ColorU8, FillRule, Mask, Paint, PathBuilder, Pixmap, Stroke, use tiny_skia::{LineCap, LineJoin, Path, PixmapPaint}; use std::iter::zip; +use crate::log; + #[derive(Debug)] pub enum Shapes { R(Rect), @@ -118,6 +120,7 @@ pub struct Text { pub y: i32, pub font_size: f32, pub color: Option, + pub max_width: Option, } impl Shapes { @@ -541,6 +544,7 @@ impl Shape for Text { let fonts = &[roboto_regular]; let mut layout = Layout::new(CoordinateSystem::PositiveYDown); layout.reset(&LayoutSettings { + max_width: self.max_width, ..LayoutSettings::default() }); layout.append(fonts, &TextStyle::new(&self.text, self.font_size, 0)); @@ -571,6 +575,10 @@ impl Shape for Text { } let mut rgba_bitmap:Vec = vec![]; for i in 0..bitmap.len() { + if bitmap[i] == 0 { + rgba_bitmap.extend([0, 0, 0, 0].iter()); + continue; + } if let Some(color) = self.color { rgba_bitmap.extend([color.red(), color.green(), color.blue(), bitmap[i]].iter()); } else { @@ -587,7 +595,6 @@ impl Shape for Text { Transform::from_row(1.0, 0.0, 0.0, 1.0, 0.0, 0.0), None, ); - } } diff --git a/soft-skia/src/util.rs b/soft-skia/src/util.rs new file mode 100644 index 0000000..ea3bb89 --- /dev/null +++ b/soft-skia/src/util.rs @@ -0,0 +1,9 @@ +extern crate web_sys; + +// A macro to provide `println!(..)`-style syntax for `console.log` logging. +#[macro_export] +macro_rules! log { + ( $( $t:tt )* ) => { + web_sys::console::log_1(&format!( $( $t )* ).into()); + } +} \ No newline at end of file diff --git a/vue-playground/src/code.ts b/vue-playground/src/code.ts index ca02782..998721f 100644 --- a/vue-playground/src/code.ts +++ b/vue-playground/src/code.ts @@ -28,7 +28,7 @@ export default ` - + `; diff --git a/vue-skia-framework/components/VText.vue b/vue-skia-framework/components/VText.vue index 03871ec..9fc44b4 100644 --- a/vue-skia-framework/components/VText.vue +++ b/vue-skia-framework/components/VText.vue @@ -1,5 +1,5 @@