Skip to content

Commit

Permalink
feat: add PathKit.dash() (#238)
Browse files Browse the repository at this point in the history
* feat: add PathKit.dash()
  • Loading branch information
yisibl authored Apr 23, 2021
1 parent d2c2bd5 commit c238113
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 0 deletions.
7 changes: 7 additions & 0 deletions __test__/pathkit.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,13 @@ test('trim', (t) => {
t.snapshot(svg)
})

test('dash', (t) => {
const box = new Path2D()
box.rect(0, 0, 100, 100)
box.dash(20, 10, 3)
t.is(box.toSVGString(), 'M20 0L40 0M50 0L70 0M80 0L100 0M100 10L100 30M100 40L100 60M100 70L100 90M100 100L100 100L80 100M70 100L50 100M40 100L20 100M10 100L0 100L0 90M0 80L0 60M0 50L0 30M0 20L0 0L10 0')
})

function drawSimplePath() {
const path = new Path2D()
path.moveTo(0, 0)
Expand Down
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ export class Path2D {
getBounds(): [left: number, top: number, right: number, bottom: number]
computeTightBounds(): [left: number, top: number, right: number, bottom: number]
trim(start: number, end: number, isComplement?: boolean): Path2D
dash(on: number, off: number, phase: number): Path2D
equals(path: Path2D): boolean
}

Expand Down
15 changes: 15 additions & 0 deletions skia-c/skia_c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,21 @@ extern "C"
return false;
}

bool skiac_path_dash(skiac_path *c_path, float on, float off, float phase)
{
float intervals[] = {on, off};
auto pe = SkDashPathEffect::Make(intervals, 2, phase);
if (!pe) {
return false;
}
SkStrokeRec rec(SkStrokeRec::InitStyle::kHairline_InitStyle);
if (pe->filterPath(PATH_CAST, *PATH_CAST, &rec, nullptr))
{
return true;
}
return false;
}

bool skiac_path_equals(skiac_path *c_path, skiac_path *other_path)
{
return *PATH_CAST == *reinterpret_cast<SkPath *>(other_path);
Expand Down
1 change: 1 addition & 0 deletions skia-c/skia_c.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ extern "C"
void skiac_path_get_bounds(skiac_path *c_path, skiac_rect *c_rect);
void skiac_path_compute_tight_bounds(skiac_path *c_path, skiac_rect *c_rect);
bool skiac_path_trim(skiac_path *c_path, float start_t, float stop_t, bool is_complement);
bool skiac_path_dash(skiac_path *c_path, float on, float off, float phase);
bool skiac_path_equals(skiac_path *c_path, skiac_path *other_path);
void skiac_path_destroy(skiac_path *c_path);
void skiac_path_set_fill_type(skiac_path *c_path, int type);
Expand Down
13 changes: 13 additions & 0 deletions src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ impl Path {
Property::new(&env, "computeTightBounds")?.with_method(compute_tight_bounds),
Property::new(&env, "transform")?.with_method(transform),
Property::new(&env, "trim")?.with_method(trim),
Property::new(&env, "dash")?.with_method(dash),
Property::new(&env, "equals")?.with_method(equals),
Property::new(&env, "_stroke")?.with_method(stroke),
],
Expand Down Expand Up @@ -418,6 +419,18 @@ fn trim(ctx: CallContext) -> Result<JsObject> {
Ok(this)
}

#[js_function(3)]
fn dash(ctx: CallContext) -> Result<JsObject> {
let this: JsObject = ctx.this_unchecked();
let path_2d = ctx.env.unwrap::<Path>(&this)?;
let on = ctx.get::<JsNumber>(0)?.get_double()?;
let off = ctx.get::<JsNumber>(1)?.get_double()?;
let phase = ctx.get::<JsNumber>(1)?.get_double()?;

path_2d.dash(on as f32, off as f32, phase as f32,);
Ok(this)
}

#[js_function(1)]
fn equals(ctx: CallContext) -> Result<JsBoolean> {
let this: JsObject = ctx.this_unchecked();
Expand Down
12 changes: 12 additions & 0 deletions src/sk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,13 @@ mod ffi {
is_complement: bool,
) -> bool;

pub fn skiac_path_dash(
path: *mut skiac_path,
on: f32,
off: f32,
phase: f32,
) -> bool;

pub fn skiac_path_equals(path: *mut skiac_path, other: *mut skiac_path) -> bool;

pub fn skiac_path_destroy(path: *mut skiac_path);
Expand Down Expand Up @@ -1986,6 +1993,11 @@ impl Path {
pub fn trim(&mut self, start: f32, end: f32, is_complement: bool) -> bool {
unsafe { ffi::skiac_path_trim(self.0, start, end, is_complement) }
}

#[inline]
pub fn dash(&mut self, on: f32, off: f32, phase: f32) -> bool {
unsafe { ffi::skiac_path_dash(self.0, on, off, phase) }
}
}

impl PartialEq for Path {
Expand Down

0 comments on commit c238113

Please sign in to comment.