Skip to content

Commit b700b77

Browse files
committed
Set canvas size on addPage and don't destroy PDF surface on width/height change
1 parent c1d99bd commit b700b77

File tree

7 files changed

+26
-7
lines changed

7 files changed

+26
-7
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ project adheres to [Semantic Versioning](http://semver.org/).
1111
### Added
1212
### Fixed
1313
* Fix dangling env pointer in image MIME data cleanup (#2550)
14+
* Set canvas size on addPage and don't destroy PDF surface on width/height change (#2538)
1415

1516
3.2.1
1617
==================

src/Canvas.cc

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -671,7 +671,7 @@ str_value(Napi::Maybe<Napi::Value> maybe, const char *fallback, bool can_be_numb
671671
return strdup(fallback);
672672
}
673673
}
674-
674+
675675
return NULL;
676676
}
677677

@@ -907,8 +907,10 @@ Canvas::resurface(Napi::Object This) {
907907
Napi::Value context;
908908

909909
if (This.Get("context").UnwrapTo(&context) && context.IsObject()) {
910-
backend()->destroySurface();
911-
backend()->ensureSurface();
910+
if (backend()->getName() != "pdf") {
911+
backend()->destroySurface();
912+
backend()->ensureSurface();
913+
}
912914
// Reset context
913915
Context2d *context2d = Context2d::Unwrap(context.As<Napi::Object>());
914916
cairo_t *prev = context2d->context();

src/CanvasRenderingContext2d.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -763,7 +763,7 @@ Context2d::AddPage(const Napi::CallbackInfo& info) {
763763
int height = info[1].ToNumber().UnwrapOr(zero).Int32Value();
764764
if (width < 1) width = canvas()->getWidth();
765765
if (height < 1) height = canvas()->getHeight();
766-
cairo_pdf_surface_set_size(canvas()->surface(), width, height);
766+
canvas()->backend()->setSize(width, height);
767767
}
768768

769769
/*

src/backend/Backend.cc

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@ int Backend::getWidth()
2929
}
3030
void Backend::setWidth(int width_)
3131
{
32-
this->destroySurface();
33-
this->width = width_;
32+
setSize(width_, height);
3433
}
3534

3635
int Backend::getHeight()
@@ -39,8 +38,17 @@ int Backend::getHeight()
3938
}
4039
void Backend::setHeight(int height_)
4140
{
41+
setSize(width, height_);
42+
}
43+
44+
void Backend::setSize(int width_, int height_) {
45+
width = width_;
46+
height = height_;
47+
resetSurface();
48+
}
49+
50+
void Backend::resetSurface() {
4251
this->destroySurface();
43-
this->height = height_;
4452
}
4553

4654
bool Backend::isSurfaceValid() {

src/backend/Backend.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class Backend
2828

2929
virtual cairo_surface_t* ensureSurface() = 0;
3030
virtual void destroySurface() = 0;
31+
virtual void resetSurface();
3132

3233
DLL_PUBLIC std::string getName();
3334

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

41+
void setSize(int width, int height);
42+
4043
// Overridden by ImageBackend. SVG and PDF thus always return INVALID.
4144
virtual cairo_format_t getFormat() {
4245
return CAIRO_FORMAT_INVALID;

src/backend/PdfBackend.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ void PdfBackend::destroySurface() {
3131
}
3232
}
3333

34+
void PdfBackend::resetSurface() {
35+
cairo_pdf_surface_set_size(ensureSurface(), width, height);
36+
}
37+
3438
void
3539
PdfBackend::Initialize(Napi::Object target) {
3640
Napi::Env env = target.Env();

src/backend/PdfBackend.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ class PdfBackend : public Napi::ObjectWrap<PdfBackend>, public Backend
99
private:
1010
cairo_surface_t* ensureSurface();
1111
void destroySurface();
12+
void resetSurface();
1213
cairo_surface_t* surface = nullptr;
1314

1415
public:

0 commit comments

Comments
 (0)