mirror of
https://github.com/penpot/penpot.git
synced 2025-02-01 20:09:04 -05:00
🎉 Stroke style support for wasm render
This commit is contained in:
parent
e551dd5a99
commit
2467e033b7
3 changed files with 70 additions and 19 deletions
|
@ -211,6 +211,14 @@
|
||||||
(store-image id))))))
|
(store-image id))))))
|
||||||
fills))
|
fills))
|
||||||
|
|
||||||
|
(defn- translate-stroke-style
|
||||||
|
[stroke-style]
|
||||||
|
(case stroke-style
|
||||||
|
:dotted 1
|
||||||
|
:dashed 2
|
||||||
|
:mixed 3
|
||||||
|
0))
|
||||||
|
|
||||||
(defn set-shape-strokes
|
(defn set-shape-strokes
|
||||||
[strokes]
|
[strokes]
|
||||||
(h/call internal-module "_clear_shape_strokes")
|
(h/call internal-module "_clear_shape_strokes")
|
||||||
|
@ -220,11 +228,12 @@
|
||||||
gradient (:stroke-color-gradient stroke)
|
gradient (:stroke-color-gradient stroke)
|
||||||
image (:stroke-image stroke)
|
image (:stroke-image stroke)
|
||||||
width (:stroke-width stroke)
|
width (:stroke-width stroke)
|
||||||
align (:stroke-alignment stroke)]
|
align (:stroke-alignment stroke)
|
||||||
|
style (-> stroke :stroke-style translate-stroke-style)]
|
||||||
(case align
|
(case align
|
||||||
:inner (h/call internal-module "_add_shape_inner_stroke" width)
|
:inner (h/call internal-module "_add_shape_inner_stroke" width style)
|
||||||
:outer (h/call internal-module "_add_shape_outer_stroke" width)
|
:outer (h/call internal-module "_add_shape_outer_stroke" width style)
|
||||||
(h/call internal-module "_add_shape_center_stroke" width))
|
(h/call internal-module "_add_shape_center_stroke" width style))
|
||||||
|
|
||||||
(cond
|
(cond
|
||||||
(some? gradient)
|
(some? gradient)
|
||||||
|
|
|
@ -347,26 +347,26 @@ pub extern "C" fn set_shape_path_content() {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
pub extern "C" fn add_shape_center_stroke(width: f32) {
|
pub extern "C" fn add_shape_center_stroke(width: f32, style: i32) {
|
||||||
let state = unsafe { STATE.as_mut() }.expect("got an invalid state pointer");
|
let state = unsafe { STATE.as_mut() }.expect("got an invalid state pointer");
|
||||||
if let Some(shape) = state.current_shape() {
|
if let Some(shape) = state.current_shape() {
|
||||||
shape.add_stroke(shapes::Stroke::new_center_stroke(width))
|
shape.add_stroke(shapes::Stroke::new_center_stroke(width, style));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
pub extern "C" fn add_shape_inner_stroke(width: f32) {
|
pub extern "C" fn add_shape_inner_stroke(width: f32, style: i32) {
|
||||||
let state = unsafe { STATE.as_mut() }.expect("got an invalid state pointer");
|
let state = unsafe { STATE.as_mut() }.expect("got an invalid state pointer");
|
||||||
if let Some(shape) = state.current_shape() {
|
if let Some(shape) = state.current_shape() {
|
||||||
shape.add_stroke(shapes::Stroke::new_inner_stroke(width))
|
shape.add_stroke(shapes::Stroke::new_inner_stroke(width, style))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
pub extern "C" fn add_shape_outer_stroke(width: f32) {
|
pub extern "C" fn add_shape_outer_stroke(width: f32, style: i32) {
|
||||||
let state = unsafe { STATE.as_mut() }.expect("got an invalid state pointer");
|
let state = unsafe { STATE.as_mut() }.expect("got an invalid state pointer");
|
||||||
if let Some(shape) = state.current_shape() {
|
if let Some(shape) = state.current_shape() {
|
||||||
shape.add_stroke(shapes::Stroke::new_outer_stroke(width))
|
shape.add_stroke(shapes::Stroke::new_outer_stroke(width, style))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -5,9 +5,20 @@ use skia_safe as skia;
|
||||||
#[derive(Debug, Clone, PartialEq)]
|
#[derive(Debug, Clone, PartialEq)]
|
||||||
pub enum StrokeStyle {
|
pub enum StrokeStyle {
|
||||||
Solid,
|
Solid,
|
||||||
// Dotted,
|
Dotted,
|
||||||
// Dashed,
|
Dashed,
|
||||||
// Mixed,
|
Mixed,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<i32> for StrokeStyle {
|
||||||
|
fn from(value: i32) -> Self {
|
||||||
|
match value {
|
||||||
|
1 => StrokeStyle::Dotted,
|
||||||
|
2 => StrokeStyle::Dashed,
|
||||||
|
3 => StrokeStyle::Mixed,
|
||||||
|
_ => StrokeStyle::Solid,
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq)]
|
#[derive(Debug, Clone, PartialEq)]
|
||||||
|
@ -39,36 +50,36 @@ pub struct Stroke {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Stroke {
|
impl Stroke {
|
||||||
pub fn new_center_stroke(width: f32) -> Self {
|
pub fn new_center_stroke(width: f32, style: i32) -> Self {
|
||||||
let transparent = skia::Color::from_argb(0, 0, 0, 0);
|
let transparent = skia::Color::from_argb(0, 0, 0, 0);
|
||||||
Stroke {
|
Stroke {
|
||||||
fill: Fill::Solid(transparent),
|
fill: Fill::Solid(transparent),
|
||||||
width: width,
|
width: width,
|
||||||
style: StrokeStyle::Solid,
|
style: StrokeStyle::from(style),
|
||||||
cap_end: StrokeCap::None,
|
cap_end: StrokeCap::None,
|
||||||
cap_start: StrokeCap::None,
|
cap_start: StrokeCap::None,
|
||||||
kind: StrokeKind::CenterStroke,
|
kind: StrokeKind::CenterStroke,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn new_inner_stroke(width: f32) -> Self {
|
pub fn new_inner_stroke(width: f32, style: i32) -> Self {
|
||||||
let transparent = skia::Color::from_argb(0, 0, 0, 0);
|
let transparent = skia::Color::from_argb(0, 0, 0, 0);
|
||||||
Stroke {
|
Stroke {
|
||||||
fill: Fill::Solid(transparent),
|
fill: Fill::Solid(transparent),
|
||||||
width: width,
|
width: width,
|
||||||
style: StrokeStyle::Solid,
|
style: StrokeStyle::from(style),
|
||||||
cap_end: StrokeCap::None,
|
cap_end: StrokeCap::None,
|
||||||
cap_start: StrokeCap::None,
|
cap_start: StrokeCap::None,
|
||||||
kind: StrokeKind::InnerStroke,
|
kind: StrokeKind::InnerStroke,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn new_outer_stroke(width: f32) -> Self {
|
pub fn new_outer_stroke(width: f32, style: i32) -> Self {
|
||||||
let transparent = skia::Color::from_argb(0, 0, 0, 0);
|
let transparent = skia::Color::from_argb(0, 0, 0, 0);
|
||||||
Stroke {
|
Stroke {
|
||||||
fill: Fill::Solid(transparent),
|
fill: Fill::Solid(transparent),
|
||||||
width: width,
|
width: width,
|
||||||
style: StrokeStyle::Solid,
|
style: StrokeStyle::from(style),
|
||||||
cap_end: StrokeCap::None,
|
cap_end: StrokeCap::None,
|
||||||
cap_start: StrokeCap::None,
|
cap_start: StrokeCap::None,
|
||||||
kind: StrokeKind::OuterStroke,
|
kind: StrokeKind::OuterStroke,
|
||||||
|
@ -108,6 +119,37 @@ impl Stroke {
|
||||||
paint.set_style(skia::PaintStyle::Stroke);
|
paint.set_style(skia::PaintStyle::Stroke);
|
||||||
paint.set_stroke_width(self.width);
|
paint.set_stroke_width(self.width);
|
||||||
paint.set_anti_alias(true);
|
paint.set_anti_alias(true);
|
||||||
|
|
||||||
|
if self.style != StrokeStyle::Solid {
|
||||||
|
let path_effect = match self.style {
|
||||||
|
StrokeStyle::Dotted => {
|
||||||
|
let mut circle_path = skia::Path::new();
|
||||||
|
circle_path.add_circle((0.0, 0.0), self.width / 2.0, None);
|
||||||
|
let advance = self.width + 5.0;
|
||||||
|
skia::PathEffect::path_1d(
|
||||||
|
&circle_path,
|
||||||
|
advance,
|
||||||
|
0.0,
|
||||||
|
skia::path_1d_path_effect::Style::Translate,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
StrokeStyle::Dashed => {
|
||||||
|
skia::PathEffect::dash(&[self.width + 10., self.width + 10.], 0.)
|
||||||
|
}
|
||||||
|
StrokeStyle::Mixed => skia::PathEffect::dash(
|
||||||
|
&[
|
||||||
|
self.width + 5.,
|
||||||
|
self.width + 5.,
|
||||||
|
self.width + 1.,
|
||||||
|
self.width + 5.,
|
||||||
|
],
|
||||||
|
0.,
|
||||||
|
),
|
||||||
|
_ => None,
|
||||||
|
};
|
||||||
|
paint.set_path_effect(path_effect);
|
||||||
|
}
|
||||||
|
|
||||||
paint
|
paint
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue