Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ project adheres to [Semantic Versioning](http://semver.org/).
### Added
### Fixed
* Fix dangling env pointer in image MIME data cleanup (#2550)
* Set canvas size on addPage and don't destroy PDF surface on width/height change (#2538)

3.2.1
==================
Expand Down
8 changes: 5 additions & 3 deletions src/Canvas.cc
Original file line number Diff line number Diff line change
Expand Up @@ -671,7 +671,7 @@ str_value(Napi::Maybe<Napi::Value> maybe, const char *fallback, bool can_be_numb
return strdup(fallback);
}
}

return NULL;
}

Expand Down Expand Up @@ -907,8 +907,10 @@ Canvas::resurface(Napi::Object This) {
Napi::Value context;

if (This.Get("context").UnwrapTo(&context) && context.IsObject()) {
backend()->destroySurface();
backend()->ensureSurface();
if (backend()->getName() != "pdf") {
backend()->destroySurface();
backend()->ensureSurface();
}
// Reset context
Context2d *context2d = Context2d::Unwrap(context.As<Napi::Object>());
cairo_t *prev = context2d->context();
Expand Down
2 changes: 1 addition & 1 deletion src/CanvasRenderingContext2d.cc
Original file line number Diff line number Diff line change
Expand Up @@ -763,7 +763,7 @@ Context2d::AddPage(const Napi::CallbackInfo& info) {
int height = info[1].ToNumber().UnwrapOr(zero).Int32Value();
if (width < 1) width = canvas()->getWidth();
if (height < 1) height = canvas()->getHeight();
cairo_pdf_surface_set_size(canvas()->surface(), width, height);
canvas()->backend()->setSize(width, height);
}

/*
Expand Down
14 changes: 11 additions & 3 deletions src/backend/Backend.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ int Backend::getWidth()
}
void Backend::setWidth(int width_)
{
this->destroySurface();
this->width = width_;
setSize(width_, height);
}

int Backend::getHeight()
Expand All @@ -39,8 +38,17 @@ int Backend::getHeight()
}
void Backend::setHeight(int height_)
{
setSize(width, height_);
}

void Backend::setSize(int width_, int height_) {
width = width_;
height = height_;
resetSurface();
}

void Backend::resetSurface() {
this->destroySurface();
this->height = height_;
}

bool Backend::isSurfaceValid() {
Expand Down
3 changes: 3 additions & 0 deletions src/backend/Backend.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class Backend

virtual cairo_surface_t* ensureSurface() = 0;
virtual void destroySurface() = 0;
virtual void resetSurface();

DLL_PUBLIC std::string getName();

Expand All @@ -37,6 +38,8 @@ class Backend
DLL_PUBLIC int getHeight();
virtual void setHeight(int height);

void setSize(int width, int height);

// Overridden by ImageBackend. SVG and PDF thus always return INVALID.
virtual cairo_format_t getFormat() {
return CAIRO_FORMAT_INVALID;
Expand Down
4 changes: 4 additions & 0 deletions src/backend/PdfBackend.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ void PdfBackend::destroySurface() {
}
}

void PdfBackend::resetSurface() {
cairo_pdf_surface_set_size(ensureSurface(), width, height);
}

void
PdfBackend::Initialize(Napi::Object target) {
Napi::Env env = target.Env();
Expand Down
1 change: 1 addition & 0 deletions src/backend/PdfBackend.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class PdfBackend : public Napi::ObjectWrap<PdfBackend>, public Backend
private:
cairo_surface_t* ensureSurface();
void destroySurface();
void resetSurface();
cairo_surface_t* surface = nullptr;

public:
Expand Down
Loading