Skip to content

Commit

Permalink
Merge commit 'adf73ee39e2676b5c67b02bef5742b941033495c'
Browse files Browse the repository at this point in the history
* commit 'adf73ee39e2676b5c67b02bef5742b941033495c':
  Add node 20 to CI
  Upgrade GitHub Actions to use checkout@v3 (Automattic#2213)
  add string tags for browser polyglot classes (Automattic#2214)
  src: refactor and apply fixes
  v2.11.2
  fix not compiling on certain windows versions
  v2.11.1
  move ctx.font string to the state struct

# Conflicts:
#	package.json
  • Loading branch information
phipla committed Sep 7, 2023
2 parents a99a655 + adf73ee commit 54f38c4
Show file tree
Hide file tree
Showing 18 changed files with 137 additions and 41 deletions.
16 changes: 8 additions & 8 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@ on:
paths-ignore:
- ".github/workflows/prebuild.yaml"

jobs:
jobs:
Linux:
name: Test on Linux
runs-on: ubuntu-latest
strategy:
matrix:
node: [10, 12, 14, 16, 18]
node: [10, 12, 14, 16, 18, 20]
steps:
- uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node }}
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- name: Install Dependencies
run: |
sudo apt update
Expand All @@ -33,12 +33,12 @@ jobs:
runs-on: windows-2019
strategy:
matrix:
node: [10, 12, 14, 16, 18]
node: [10, 12, 14, 16, 18, 20]
steps:
- uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node }}
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- name: Install Dependencies
run: |
Invoke-WebRequest "https://ftp-osl.osuosl.org/pub/gnome/binaries/win64/gtk+/2.22/gtk+-bundle_2.22.1-20101229_win64.zip" -OutFile "gtk.zip"
Expand All @@ -57,12 +57,12 @@ jobs:
runs-on: macos-latest
strategy:
matrix:
node: [10, 12, 14, 16, 18]
node: [10, 12, 14, 16, 18, 20]
steps:
- uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node }}
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- name: Install Dependencies
run: |
brew update
Expand All @@ -80,7 +80,7 @@ jobs:
- uses: actions/setup-node@v3
with:
node-version: 14
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- name: Install
run: npm install --ignore-scripts
- name: Lint
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/prebuild.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
Linux:
strategy:
matrix:
node: [8, 9, 10, 11, 12, 13, 14, 16, 18]
node: [8, 9, 10, 11, 12, 13, 14, 16, 18, 20]
canvas_tag: [] # e.g. "v2.6.1"
name: ${{ matrix.canvas_tag}}, Node.js ${{ matrix.node }}, Linux
runs-on: ubuntu-latest
Expand Down Expand Up @@ -97,7 +97,7 @@ jobs:
macOS:
strategy:
matrix:
node: [8, 9, 10, 11, 12, 13, 14, 16, 18]
node: [8, 9, 10, 11, 12, 13, 14, 16, 18, 20]
canvas_tag: [] # e.g. "v2.6.1"
name: ${{ matrix.canvas_tag}}, Node.js ${{ matrix.node }}, macOS
runs-on: macos-latest
Expand Down Expand Up @@ -163,7 +163,7 @@ jobs:
Win:
strategy:
matrix:
node: [8, 9, 10, 11, 12, 13, 14, 16, 18]
node: [8, 9, 10, 11, 12, 13, 14, 16, 18, 20]
canvas_tag: [] # e.g. "v2.6.1"
name: ${{ matrix.canvas_tag}}, Node.js ${{ matrix.node }}, Windows
runs-on: windows-latest
Expand Down
20 changes: 20 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,30 @@ project adheres to [Semantic Versioning](http://semver.org/).
(Unreleased)
==================
### Changed
* Defer the initialization of the `op` variable to the `default` switch case to avoid a compiler warning. (#2229)
* Use a `default` switch case with a null statement if some enum values aren't suppsed to be handled, this avoids a compiler warning. (#2229)
* Migrate from librsvg's deprecated `rsvg_handle_get_dimensions()` and `rsvg_handle_render_cairo()` functions to the new `rsvg_handle_get_intrinsic_size_in_pixels()` and `rsvg_handle_render_document()` respectively. (#2229)
* Avoid calling virtual methods in constructors/destructors to avoid bypassing virtual dispatch. (#2229)
* Remove unused private field `backend` in the `Backend` class. (#2229)
* Add Node.js v20 to CI. (#2237)
### Added
* Added string tags to support class detection
### Fixed
* Fix a case of use-after-free. (#2229)
* Fix usage of garbage value by filling the allocated memory entirely with zeros if it's not modified. (#2229)
* Fix a potential memory leak. (#2229)

2.11.2
==================
### Fixed
* Building on Windows in CI (and maybe other Windows configurations?) (#2216)

2.11.1
==================
### Fixed
* Add missing property `canvas` to the `CanvasRenderingContext2D` type
* Fixed glyph positions getting rounded, resulting text having a slight `letter-spacing` effect
* Fixed `ctx.font` not being restored correctly after `ctx.restore()` (#1946)

2.11.0
==================
Expand Down
30 changes: 30 additions & 0 deletions lib/bindings.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,40 @@ const bindings = require('../build/Release/canvas.node')

module.exports = bindings

Object.defineProperty(bindings.Canvas.prototype, Symbol.toStringTag, {
value: 'HTMLCanvasElement',
configurable: true
})

Object.defineProperty(bindings.Image.prototype, Symbol.toStringTag, {
value: 'HTMLImageElement',
configurable: true
})

bindings.ImageData.prototype.toString = function () {
return '[object ImageData]'
}

Object.defineProperty(bindings.ImageData.prototype, Symbol.toStringTag, {
value: 'ImageData',
configurable: true
})

bindings.CanvasGradient.prototype.toString = function () {
return '[object CanvasGradient]'
}

Object.defineProperty(bindings.CanvasGradient.prototype, Symbol.toStringTag, {
value: 'CanvasGradient',
configurable: true
})

Object.defineProperty(bindings.CanvasPattern.prototype, Symbol.toStringTag, {
value: 'CanvasPattern',
configurable: true
})

Object.defineProperty(bindings.CanvasRenderingContext2d.prototype, Symbol.toStringTag, {
value: 'CanvasRenderingContext2d',
configurable: true
})
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@ecervo/canvas",
"description": "Canvas graphics API backed by Cairo",
"version": "2.10.1-ecervo.1",
"version": "2.11.2-ecervo.1",
"author": "TJ Holowaychuk <tj@learnboost.com>",
"main": "index.js",
"browser": "browser.js",
Expand Down
3 changes: 2 additions & 1 deletion src/Canvas.cc
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,9 @@ NAN_METHOD(Canvas::New) {
}

if (!backend->isSurfaceValid()) {
const char *error = backend->getError();
delete backend;
return Nan::ThrowError(backend->getError());
return Nan::ThrowError(error);
}

Canvas* canvas = new Canvas(backend);
Expand Down
27 changes: 14 additions & 13 deletions src/CanvasRenderingContext2d.cc
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,6 @@ void Context2d::resetState() {
void Context2d::_resetPersistentHandles() {
_fillStyle.Reset();
_strokeStyle.Reset();
_font.Reset();
}

/*
Expand Down Expand Up @@ -608,8 +607,8 @@ Context2d::blur(cairo_surface_t *surface, int radius) {
// get width, height
int width = cairo_image_surface_get_width( surface );
int height = cairo_image_surface_get_height( surface );
unsigned* precalc =
(unsigned*)malloc(width*height*sizeof(unsigned));
const unsigned int size = width * height * sizeof(unsigned);
unsigned* precalc = (unsigned*)malloc(size);
cairo_surface_flush( surface );
unsigned char* src = cairo_image_surface_get_data( surface );
double mul=1.f/((radius*2)*(radius*2));
Expand All @@ -628,6 +627,8 @@ Context2d::blur(cairo_surface_t *surface, int radius) {
unsigned char* pix = src;
unsigned* pre = precalc;

bool modified = false;

pix += channel;
for (y=0;y<height;y++) {
for (x=0;x<width;x++) {
Expand All @@ -636,10 +637,15 @@ Context2d::blur(cairo_surface_t *surface, int radius) {
if (y>0) tot+=pre[-width];
if (x>0 && y>0) tot-=pre[-width-1];
*pre++=tot;
if (!modified) modified = true;
pix += 4;
}
}

if (!modified) {
memset(precalc, 0, size);
}

// blur step.
pix = src + (int)radius * width * 4 + (int)radius * 4 + channel;
for (y=radius;y<height-radius;y++) {
Expand Down Expand Up @@ -1433,7 +1439,7 @@ NAN_GETTER(Context2d::GetGlobalCompositeOperation) {
Context2d *context = Nan::ObjectWrap::Unwrap<Context2d>(info.This());
cairo_t *ctx = context->context();

const char *op = "source-over";
const char *op{};
switch (cairo_get_operator(ctx)) {
// composite modes:
case CAIRO_OPERATOR_CLEAR: op = "clear"; break;
Expand Down Expand Up @@ -1470,6 +1476,7 @@ NAN_GETTER(Context2d::GetGlobalCompositeOperation) {
case CAIRO_OPERATOR_HSL_LUMINOSITY: op = "luminosity"; break;
// non-standard:
case CAIRO_OPERATOR_SATURATE: op = "saturate"; break;
default: op = "source-over";
}

info.GetReturnValue().Set(Nan::New(op).ToLocalChecked());
Expand Down Expand Up @@ -2508,6 +2515,7 @@ Context2d::setTextPath(double x, double y) {
pango_layout_get_pixel_extents(_layout, NULL, &logical_rect);
x -= logical_rect.width;
break;
default: ;
}

y -= getBaselineAdjustment(_layout, state->textBaseline);
Expand Down Expand Up @@ -2553,15 +2561,8 @@ NAN_METHOD(Context2d::MoveTo) {
NAN_GETTER(Context2d::GetFont) {
CHECK_RECEIVER(Context2d.GetFont);
Context2d *context = Nan::ObjectWrap::Unwrap<Context2d>(info.This());
Isolate *iso = Isolate::GetCurrent();
Local<Value> font;

if (context->_font.IsEmpty())
font = Nan::New("10px sans-serif").ToLocalChecked();
else
font = context->_font.Get(iso);

info.GetReturnValue().Set(font);
info.GetReturnValue().Set(Nan::New(context->state->font).ToLocalChecked());
}

/*
Expand Down Expand Up @@ -2624,7 +2625,7 @@ NAN_SETTER(Context2d::SetFont) {
context->state->fontDescription = sys_desc;
pango_layout_set_font_description(context->_layout, sys_desc);

context->_font.Reset(value);
context->state->font = *Nan::Utf8String(value);
}

/*
Expand Down
3 changes: 2 additions & 1 deletion src/CanvasRenderingContext2d.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ struct canvas_state_t {
cairo_pattern_t* fillGradient = nullptr;
cairo_pattern_t* strokeGradient = nullptr;
PangoFontDescription* fontDescription = nullptr;
std::string font = "10px sans-serif";
cairo_filter_t patternQuality = CAIRO_FILTER_GOOD;
float globalAlpha = 1.f;
int shadowBlur = 0;
Expand Down Expand Up @@ -57,6 +58,7 @@ struct canvas_state_t {
shadowOffsetY = other.shadowOffsetY;
textDrawingMode = other.textDrawingMode;
fontDescription = pango_font_description_copy(other.fontDescription);
font = other.font;
imageSmoothingEnabled = other.imageSmoothingEnabled;
}

Expand Down Expand Up @@ -216,7 +218,6 @@ class Context2d : public Nan::ObjectWrap {
void _setStrokePattern(v8::Local<v8::Value> arg);
Nan::Persistent<v8::Value> _fillStyle;
Nan::Persistent<v8::Value> _strokeStyle;
Nan::Persistent<v8::Value> _font;
Canvas *_canvas;
cairo_t *_context;
cairo_path_t *_path;
Expand Down
18 changes: 13 additions & 5 deletions src/Image.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1192,11 +1192,13 @@ Image::loadSVGFromBuffer(uint8_t *buf, unsigned len) {
return CAIRO_STATUS_READ_ERROR;
}

RsvgDimensionData *dims = new RsvgDimensionData();
rsvg_handle_get_dimensions(_rsvg, dims);
double d_width;
double d_height;

width = naturalWidth = dims->width;
height = naturalHeight = dims->height;
rsvg_handle_get_intrinsic_size_in_pixels(_rsvg, &d_width, &d_height);

width = naturalWidth = d_width;
height = naturalHeight = d_height;

status = renderSVGToSurface();
if (status != CAIRO_STATUS_SUCCESS) {
Expand Down Expand Up @@ -1232,7 +1234,13 @@ Image::renderSVGToSurface() {
return status;
}

gboolean render_ok = rsvg_handle_render_cairo(_rsvg, cr);
RsvgRectangle viewport = {
.x = 0,
.y = 0,
.width = static_cast<double>(width),
.height = static_cast<double>(height),
};
gboolean render_ok = rsvg_handle_render_document(_rsvg, cr, &viewport, nullptr);
if (!render_ok) {
g_object_unref(_rsvg);
cairo_destroy(cr);
Expand Down
5 changes: 2 additions & 3 deletions src/backend/Backend.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Backend::Backend(std::string name, int width, int height)

Backend::~Backend()
{
this->destroySurface();
Backend::destroySurface();
}

void Backend::init(const Nan::FunctionCallbackInfo<v8::Value> &info) {
Expand Down Expand Up @@ -97,8 +97,7 @@ bool Backend::isSurfaceValid(){

BackendOperationNotAvailable::BackendOperationNotAvailable(Backend* backend,
std::string operation_name)
: backend(backend)
, operation_name(operation_name)
: operation_name(operation_name)
{
msg = "operation " + operation_name +
" not supported by backend " + backend->getName();
Expand Down
1 change: 0 additions & 1 deletion src/backend/Backend.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ class Backend : public Nan::ObjectWrap
class BackendOperationNotAvailable: public std::exception
{
private:
Backend* backend;
std::string operation_name;
std::string msg;

Expand Down
2 changes: 1 addition & 1 deletion src/backend/PdfBackend.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ using namespace v8;

PdfBackend::PdfBackend(int width, int height)
: Backend("pdf", width, height) {
createSurface();
PdfBackend::createSurface();
}

PdfBackend::~PdfBackend() {
Expand Down
2 changes: 1 addition & 1 deletion src/backend/SvgBackend.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ using namespace v8;

SvgBackend::SvgBackend(int width, int height)
: Backend("svg", width, height) {
createSurface();
SvgBackend::createSurface();
}

SvgBackend::~SvgBackend() {
Expand Down
3 changes: 2 additions & 1 deletion src/register_font.cc
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ get_pango_font_description(unsigned char* filepath) {
FILE_SHARE_READ,
NULL,
OPEN_EXISTING,
NULL,
FILE_ATTRIBUTE_NORMAL,
NULL
);
if(!hFile){
Expand Down Expand Up @@ -304,6 +304,7 @@ get_pango_font_description(unsigned char* filepath) {
}

pango_font_description_set_family_static(desc, family);
free(family);
pango_font_description_set_weight(desc, get_pango_weight(table->usWeightClass));
pango_font_description_set_stretch(desc, get_pango_stretch(table->usWidthClass));
pango_font_description_set_style(desc, get_pango_style(face->style_flags));
Expand Down

0 comments on commit 54f38c4

Please sign in to comment.