Skip to content

Commit

Permalink
feat: text states and font styles
Browse files Browse the repository at this point in the history
  • Loading branch information
Brooooooklyn committed Feb 2, 2021
1 parent 89dd09e commit a175cf7
Show file tree
Hide file tree
Showing 7 changed files with 633 additions and 5 deletions.
70 changes: 70 additions & 0 deletions .github/workflows/cargo-test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
name: Cargo test

env:
DEBUG: 'napi:*'
APP_NAME: 'skia'

on:
push:
branches:
- main
tags-ignore:
- '**'
pull_request:

jobs:
build:
if: "!contains(github.event.head_commit.message, 'skip ci')"
name: stable - Linux - cargo - test
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
with:
submodules: true

- name: Install
uses: actions-rs/toolchain@v1
with:
toolchain: stable
profile: minimal
override: true

- name: Generate Cargo.lock
uses: actions-rs/cargo@v1
with:
command: generate-lockfile

- name: Cache cargo registry
uses: actions/cache@v1
with:
path: ~/.cargo/registry
key: stable-${{ matrix.os }}-node@14-cargo-registry-trimmed-${{ hashFiles('**/Cargo.lock') }}

- name: Cache cargo index
uses: actions/cache@v1
with:
path: ~/.cargo/git
key: stable-${{ matrix.os }}-node@14-cargo-index-trimmed-${{ hashFiles('**/Cargo.lock') }}

- name: Cache NPM dependencies
uses: actions/cache@v1
with:
path: node_modules
key: npm-cache-${{ matrix.os }}-node@14-${{ hashFiles('yarn.lock') }}
restore-keys: |
npm-cache-
- name: 'Install dependencies'
run: yarn install --frozen-lockfile --registry https://registry.npmjs.org --network-timeout 300000

- name: Download skia binary
run: node ./scripts/release-skia-binary.js --download

- name: Test
run: cargo test -- --nocapture

- name: Clear the cargo caches
run: |
cargo install cargo-cache --no-default-features --features ci-autoclean
cargo-cache
6 changes: 4 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ version = "0.1.0"
crate-type = ["cdylib"]

[dependencies]
anyhow = "1"
anyhow = "1.0"
cssparser = "0.28"
napi = "1"
napi-derive = "1"
thiserror = "1"
once_cell = "1.5"
regex = "1.4"
thiserror = "1.0"

[target.'cfg(all(unix, not(target_env = "musl"), not(target_arch = "aarch64")))'.dependencies]
jemallocator = {version = "0.3", features = ["disable_initial_exec_tls"]}
Expand Down
14 changes: 14 additions & 0 deletions __test__/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,4 +136,18 @@ test('lineDash state should be ok', (t) => {
t.deepEqual(ctx.getLineDash(), lineDash)
})

test('textAlign state should be ok', (t) => {
const { ctx } = t.context
t.is(ctx.textAlign, 'start')
ctx.textAlign = 'center'
t.is(ctx.textAlign, 'center')
})

test('textBaseline state should be ok', (t) => {
const { ctx } = t.context
t.is(ctx.textBaseline, 'alphabetic')
ctx.textBaseline = 'hanging'
t.is(ctx.textBaseline, 'hanging')
})

test.todo('getTransform')
73 changes: 73 additions & 0 deletions src/ctx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use cssparser::{Color as CSSColor, Parser, ParserInput};
use napi::*;

use crate::error::SkError;
use crate::font::Font;
use crate::gradient::CanvasGradient;
use crate::image::*;
use crate::pattern::Pattern;
Expand Down Expand Up @@ -65,6 +66,9 @@ impl Context {
Property::new(&env, "fillStyle")?
.with_setter(set_fill_style)
.with_getter(get_fill_style),
Property::new(&env, "font")?
.with_setter(set_font)
.with_getter(get_font),
Property::new(&env, "strokeStyle")?
.with_setter(set_stroke_style)
.with_getter(get_stroke_style),
Expand All @@ -80,6 +84,12 @@ impl Context {
Property::new(&env, "shadowOffsetY")?
.with_setter(set_shadow_offset_y)
.with_getter(get_shadow_offset_y),
Property::new(&env, "textAlign")?
.with_setter(set_text_align)
.with_getter(get_text_align),
Property::new(&env, "textBaseline")?
.with_setter(set_text_baseline)
.with_getter(get_text_baseline),
// methods
Property::new(&env, "arc")?.with_method(arc),
Property::new(&env, "arcTo")?.with_method(arc_to),
Expand Down Expand Up @@ -1427,6 +1437,29 @@ fn get_fill_style(ctx: CallContext) -> Result<JsUnknown> {
this.get_named_property("_fillStyle")
}

#[js_function]
fn get_font(ctx: CallContext) -> Result<JsString> {
let this = ctx.this_unchecked::<JsObject>();
let context_2d = ctx.env.unwrap::<Context>(&this)?;

ctx
.env
.create_string(context_2d.states.last().unwrap().font.as_str())
}

#[js_function(1)]
fn set_font(ctx: CallContext) -> Result<JsUndefined> {
let this = ctx.this_unchecked::<JsObject>();
let context_2d = ctx.env.unwrap::<Context>(&this)?;

let last_state = context_2d.states.last_mut().unwrap();
let font_style = ctx.get::<JsString>(0)?.into_utf8()?.into_owned()?;
last_state.font_style =
Font::new(font_style.as_str()).map_err(|e| Error::new(Status::InvalidArg, format!("{}", e)))?;
last_state.font = font_style;
ctx.env.get_undefined()
}

#[js_function(1)]
fn set_stroke_style(ctx: CallContext) -> Result<JsUndefined> {
let mut this = ctx.this_unchecked::<JsObject>();
Expand Down Expand Up @@ -1579,6 +1612,46 @@ fn set_shadow_offset_y(ctx: CallContext) -> Result<JsUndefined> {
ctx.env.get_undefined()
}

#[js_function(1)]
fn set_text_align(ctx: CallContext) -> Result<JsUndefined> {
let this = ctx.this_unchecked::<JsObject>();
let context_2d = ctx.env.unwrap::<Context>(&this)?;

context_2d.states.last_mut().unwrap().text_align =
TextAlign::from_str(ctx.get::<JsString>(0)?.into_utf8()?.as_str()?)?;
ctx.env.get_undefined()
}

#[js_function]
fn get_text_align(ctx: CallContext) -> Result<JsString> {
let this = ctx.this_unchecked::<JsObject>();
let context_2d = ctx.env.unwrap::<Context>(&this)?;

ctx
.env
.create_string(context_2d.states.last().unwrap().text_align.as_str())
}

#[js_function(1)]
fn set_text_baseline(ctx: CallContext) -> Result<JsUndefined> {
let this = ctx.this_unchecked::<JsObject>();
let context_2d = ctx.env.unwrap::<Context>(&this)?;

context_2d.states.last_mut().unwrap().text_baseline =
TextBaseline::from_str(ctx.get::<JsString>(0)?.into_utf8()?.as_str()?)?;
ctx.env.get_undefined()
}

#[js_function]
fn get_text_baseline(ctx: CallContext) -> Result<JsString> {
let this = ctx.this_unchecked::<JsObject>();
let context_2d = ctx.env.unwrap::<Context>(&this)?;

ctx
.env
.create_string(context_2d.states.last().unwrap().text_baseline.as_str())
}

pub enum ContextData {
PNG(SurfaceRef),
JPEG(SurfaceRef, u8),
Expand Down
Loading

1 comment on commit a175cf7

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmark

Benchmark suite Current: a175cf7 Previous: 2f9d306 Ratio
Draw house#@napi-rs/skia 27 ops/sec (±0.46%) 25 ops/sec (±1.22%) 0.93
Draw house#node-canvas 23 ops/sec (±0.91%) 24 ops/sec (±1.44%) 1.04
Draw gradient#@napi-rs/skia 27 ops/sec (±0.05%) 24 ops/sec (±1.16%) 0.89
Draw gradient#node-canvas 23 ops/sec (±0.26%) 23 ops/sec (±1.15%) 1

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.