Skip to content

Commit

Permalink
feat: simple example
Browse files Browse the repository at this point in the history
  • Loading branch information
Brooooooklyn committed Dec 2, 2020
1 parent 30ffabc commit 5b09b4b
Show file tree
Hide file tree
Showing 19 changed files with 1,223 additions and 135 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -119,4 +119,4 @@ dist
/target
Cargo.lock

*.node
*.node
3 changes: 3 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
target
depot_tools
skia
skia-c
3 changes: 0 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@ napi = {git = "https://github.com/napi-rs/napi-rs"}
napi-derive = {git = "https://github.com/napi-rs/napi-rs"}
thiserror = "1"

[target.'cfg(all(unix, not(target_env = "musl")))'.dependencies]
jemallocator = {version = "0.3", features = ["disable_initial_exec_tls"]}

[build-dependencies]
cc = "1"
napi-build = {git = "https://github.com/napi-rs/napi-rs"}
Expand Down
28 changes: 28 additions & 0 deletions example/simple.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const { writeFileSync } = require('fs')
const { join } = require('path')

const { createCanvas } = require('../index')

const canvas = createCanvas(1024, 768)

const ctx = canvas.getContext('2d')

ctx.lineWidth = 10

// Wall
ctx.strokeRect(75, 140, 150, 110)

ctx.fillStyle = '#03a9f4'

// Door
ctx.fillRect(130, 190, 40, 60)

// Roof
ctx.beginPath()
ctx.moveTo(50, 140)
ctx.lineTo(150, 60)
ctx.lineTo(250, 140)
ctx.closePath()
ctx.stroke()

writeFileSync(join(__dirname, 'simple.png'), canvas.toBuffer())
Binary file added example/simple.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 16 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,22 @@ const { CanvasRenderingContext2D, CanvasElement } = loadBinding(__dirname, 'skia
function createCanvas(width, height) {
const canvasElement = new CanvasElement(width, height)
const ctx = new CanvasRenderingContext2D(width, height)

// napi can not define writable: true but enumerable: false property
Object.defineProperty(ctx, '_fillStyle', {
value: '#000',
configurable: false,
enumerable: false,
writable: true,
})

Object.defineProperty(ctx, '_strokeStyle', {
value: '#000',
configurable: false,
enumerable: false,
writable: true,
})

Object.defineProperty(canvasElement, 'ctx', {
value: ctx,
enumerable: false,
Expand Down
5 changes: 0 additions & 5 deletions simple-test.js

This file was deleted.

2 changes: 1 addition & 1 deletion skia
Submodule skia updated 1503 files
80 changes: 79 additions & 1 deletion skia-c/skia_c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#define PAINT_CAST reinterpret_cast<SkPaint *>(c_paint)
#define PATH_CAST reinterpret_cast<SkPath *>(c_path)
#define MATRIX_CAST reinterpret_cast<SkMatrix *>(c_matrix)
#define MASK_FILTER_CAST reinterpret_cast<SkMaskFilter *>(c_mask_filter)

extern "C"
{
Expand Down Expand Up @@ -92,6 +93,19 @@ extern "C"
return false;
}

void skiac_surface_png_data(skiac_surface *c_surface, skiac_surface_data *data)
{
auto image = SURFACE_CAST->makeImageSnapshot();
auto png_data = image->encodeToData();
data->ptr = nullptr;
data->size = 0;
if (png_data)
{
data->ptr = const_cast<uint8_t *>(png_data->bytes());
data->size = png_data->size();
}
}

void skiac_surface_destroy(skiac_surface *c_surface)
{
// SkSurface is ref counted.
Expand Down Expand Up @@ -143,7 +157,7 @@ extern "C"
if (SURFACE_CAST->peekPixels(&pixmap))
{
data->ptr = static_cast<uint8_t *>(pixmap.writable_addr());
data->size = static_cast<uint32_t>(pixmap.computeByteSize());
data->size = pixmap.computeByteSize();
}
}

Expand Down Expand Up @@ -184,6 +198,12 @@ extern "C"
CANVAS_CAST->translate(dx, dy);
}

skiac_matrix *skiac_canvas_get_total_transform_matrix(skiac_canvas *c_canvas)
{
auto martix = CANVAS_CAST->getTotalMatrix();
return reinterpret_cast<skiac_matrix *>(new SkMatrix(martix));
}

skiac_transform skiac_canvas_get_total_transform(skiac_canvas *c_canvas)
{
return conv_to_transform(CANVAS_CAST->getTotalMatrix());
Expand Down Expand Up @@ -272,6 +292,12 @@ extern "C"
return reinterpret_cast<skiac_paint *>(new SkPaint());
}

skiac_paint *skiac_paint_clone(skiac_paint *c_paint)
{
auto cloned_paint = new SkPaint(*PAINT_CAST);
return reinterpret_cast<skiac_paint *>(cloned_paint);
}

void skiac_paint_destroy(skiac_paint *c_paint)
{
// Will unref() Shader and PathEffect.
Expand Down Expand Up @@ -332,6 +358,14 @@ extern "C"
PAINT_CAST->setPathEffect(pathEffect);
}

void skiac_paint_set_mask_filter(skiac_paint *c_paint, skiac_mask_filter *c_mask_filter)
{
sk_sp<SkMaskFilter> maskFilter(reinterpret_cast<SkMaskFilter *>(c_mask_filter));
maskFilter->ref();

PAINT_CAST->setMaskFilter(maskFilter);
}

void skiac_paint_set_style(skiac_paint *c_paint, int style)
{
PAINT_CAST->setStyle((SkPaint::Style)style);
Expand All @@ -342,6 +376,11 @@ extern "C"
PAINT_CAST->setStrokeWidth(width);
}

float skiac_paint_get_stroke_width(skiac_paint *c_paint)
{
return PAINT_CAST->getStrokeWidth();
}

void skiac_paint_set_stroke_cap(skiac_paint *c_paint, int cap)
{
PAINT_CAST->setStrokeCap((SkPaint::Cap)cap);
Expand Down Expand Up @@ -419,6 +458,11 @@ extern "C"
PATH_CAST->cubicTo(x1, y1, x2, y2, x3, y3);
}

void skiac_path_quad_to(skiac_path *c_path, float cpx, float cpy, float x, float y)
{
PATH_CAST->quadTo(cpx, cpy, x, y);
}

void skiac_path_close(skiac_path *c_path)
{
PATH_CAST->close();
Expand Down Expand Up @@ -583,6 +627,11 @@ extern "C"
return reinterpret_cast<skiac_matrix *>(new SkMatrix());
}

skiac_matrix *skiac_matrix_clone(skiac_matrix *c_matrix)
{
return reinterpret_cast<skiac_matrix *>(new SkMatrix(*MATRIX_CAST));
}

void skiac_matrix_pre_translate(skiac_matrix *c_matrix, float dx, float dy)
{
MATRIX_CAST->preTranslate(dx, dy);
Expand All @@ -597,4 +646,33 @@ extern "C"
{
return MATRIX_CAST->invert(reinterpret_cast<SkMatrix *>(inverse));
}

skiac_transform skiac_matrix_to_transform(skiac_matrix *c_matrix)
{
return conv_to_transform(*MATRIX_CAST);
}

void skiac_matrix_destroy(skiac_matrix *c_matrix)
{
delete MATRIX_CAST;
}

skiac_mask_filter *skiac_mask_filter_make_blur(float radius)
{
auto mask_filter = SkMaskFilter::MakeBlur(SkBlurStyle::kNormal_SkBlurStyle, radius, false).release();
if (mask_filter)
{
return reinterpret_cast<skiac_mask_filter *>(mask_filter);
}
else
{
return nullptr;
}
}

void skiac_mask_filter_destroy(skiac_mask_filter *c_mask_filter)
{
auto mask_filter = MASK_FILTER_CAST;
SkSafeUnref(mask_filter);
}
}
23 changes: 22 additions & 1 deletion skia-c/skia_c.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <include/core/SkGraphics.h>
#include <include/core/SkPaint.h>
#include <include/core/SkSurface.h>
#include <include/core/SkMaskFilter.h>
#include <include/effects/SkDashPathEffect.h>
#include <include/effects/SkGradientShader.h>

Expand All @@ -19,6 +20,7 @@ typedef struct skiac_path skiac_path;
typedef struct skiac_shader skiac_shader;
typedef struct skiac_path_effect skiac_path_effect;
typedef struct skiac_matrix skiac_matrix;
typedef struct skiac_mask_filter skiac_mask_filter;

struct skiac_transform
{
Expand All @@ -39,7 +41,7 @@ struct skiac_point
struct skiac_surface_data
{
uint8_t *ptr;
uint32_t size;
size_t size;
};

extern "C"
Expand All @@ -59,6 +61,7 @@ extern "C"
int skiac_surface_get_width(skiac_surface *c_surface);
int skiac_surface_get_height(skiac_surface *c_surface);
void skiac_surface_read_pixels(skiac_surface *c_surface, skiac_surface_data *data);
void skiac_surface_png_data(skiac_surface *c_surface, skiac_surface_data *data);
int skiac_surface_get_alpha_type(skiac_surface *c_surface);
bool skiac_surface_save(skiac_surface *c_surface, const char *path);

Expand All @@ -70,6 +73,7 @@ extern "C"
void skiac_canvas_scale(skiac_canvas *c_canvas, float sx, float sy);
void skiac_canvas_translate(skiac_canvas *c_canvas, float dx, float dy);
skiac_transform skiac_canvas_get_total_transform(skiac_canvas *c_canvas);
skiac_matrix *skiac_canvas_get_total_transform_matrix(skiac_canvas *c_canvas);
void skiac_canvas_draw_color(skiac_canvas *c_canvas, float r, float g, float b, float a);
void skiac_canvas_draw_path(skiac_canvas *c_canvas, skiac_path *c_path, skiac_paint *c_paint);
void skiac_canvas_draw_rect(
Expand Down Expand Up @@ -97,6 +101,7 @@ extern "C"

// Paint
skiac_paint *skiac_paint_create();
skiac_paint *skiac_paint_clone(skiac_paint *c_paint);
void skiac_paint_destroy(skiac_paint *c_paint);
void skiac_paint_set_style(skiac_paint *c_paint, int style);
void skiac_paint_set_color(skiac_paint *c_paint, uint8_t r, uint8_t g, uint8_t b, uint8_t a);
Expand All @@ -107,11 +112,13 @@ extern "C"
int skiac_paint_get_blend_mode(skiac_paint *c_paint);
void skiac_paint_set_shader(skiac_paint *c_paint, skiac_shader *c_shader);
void skiac_paint_set_stroke_width(skiac_paint *c_paint, float width);
float skiac_paint_get_stroke_width(skiac_paint *c_paint);
void skiac_paint_set_stroke_cap(skiac_paint *c_paint, int cap);
void skiac_paint_set_stroke_join(skiac_paint *c_paint, int join);
void skiac_paint_set_stroke_miter(skiac_paint *c_paint, float miter);
float skiac_paint_get_stroke_miter(skiac_paint *c_paint);
void skiac_paint_set_path_effect(skiac_paint *c_paint, skiac_path_effect *c_path_effect);
void skiac_paint_set_mask_filter(skiac_paint *c_paint, skiac_mask_filter *c_mask_filter);

// Path
skiac_path *skiac_path_create();
Expand All @@ -126,6 +133,7 @@ extern "C"
void skiac_path_cubic_to(
skiac_path *c_path,
float x1, float y1, float x2, float y2, float x3, float y3);
void skiac_path_quad_to(skiac_path *c_path, float cpx, float cpy, float x, float y);
void skiac_path_close(skiac_path *c_path);
void skiac_path_add_rect(skiac_path *c_path, float l, float t, float r, float b);
void skiac_path_add_circle(skiac_path *c_path, float x, float y, float r);
Expand Down Expand Up @@ -166,13 +174,26 @@ extern "C"

void skiac_shader_destroy(skiac_shader *c_shader);

// Matrix
skiac_matrix *skiac_matrix_create();

skiac_matrix *skiac_matrix_clone(skiac_matrix *c_matrix);

void skiac_matrix_pre_translate(skiac_matrix *c_matrix, float dx, float dy);

void skiac_matrix_pre_rotate(skiac_matrix *c_matrix, float degrees);

bool skiac_matrix_invert(skiac_matrix *c_matrix, skiac_matrix *inverse);

skiac_transform skiac_matrix_to_transform(skiac_matrix *c_matrix);

void skiac_matrix_destroy(skiac_matrix *c_matrix);

// MaskFilter

skiac_mask_filter *skiac_mask_filter_make_blur(float radius);

void skiac_mask_filter_destroy(skiac_mask_filter *c_mask_filter);
}

#endif // SKIA_CAPI_H
Loading

0 comments on commit 5b09b4b

Please sign in to comment.