Skip to content

Commit 56635e5

Browse files
committed
allow registerFont after a canvas has been created
Fixes #1921
1 parent 52330b8 commit 56635e5

5 files changed

+20
-1
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ project adheres to [Semantic Versioning](http://semver.org/).
1010
### Changed
1111
* Replaced `simple-get ` with ` Node.js builtin` `fetch` (#2309)
1212
* `ctx.font` has a new C++ parser and is 2x-400x faster. Please file an issue if you experience different results, as caching has been removed.
13+
* The restriction of registering fonts before a canvas is created has been removed. You can now register a font as late as right before the `fillText` call ([#1921](https://github.com/Automattic/node-canvas/issues/1921))
1314

1415
### Added
1516
* Support for accessibility and links in PDFs

src/Canvas.cc

+6-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,10 @@
3838

3939
using namespace std;
4040

41-
std::vector<FontFace> font_face_list;
41+
std::vector<FontFace> Canvas::font_face_list;
42+
43+
// Increases each time a font is (de)registered
44+
int Canvas::fontSerial = 1;
4245

4346
/*
4447
* Initialize Canvas.
@@ -734,6 +737,7 @@ Canvas::RegisterFont(const Napi::CallbackInfo& info) {
734737
free(family);
735738
free(weight);
736739
free(style);
740+
fontSerial++;
737741
}
738742

739743
void
@@ -749,6 +753,7 @@ Canvas::DeregisterAllFonts(const Napi::CallbackInfo& info) {
749753
});
750754

751755
font_face_list.clear();
756+
fontSerial++;
752757
if (!success) Napi::Error::New(env, "Could not deregister one or more fonts").ThrowAsJavaScriptException();
753758
}
754759

src/Canvas.h

+2
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,11 @@ class Canvas : public Napi::ObjectWrap<Canvas> {
9191
void resurface(Napi::Object This);
9292

9393
Napi::Env env;
94+
static int fontSerial;
9495

9596
private:
9697
Backend* _backend;
9798
Napi::ObjectReference _jsBackend;
9899
Napi::FunctionReference ctor;
100+
static std::vector<FontFace> font_face_list;
99101
};

src/CanvasRenderingContext2d.cc

+10
Original file line numberDiff line numberDiff line change
@@ -2472,6 +2472,16 @@ Context2d::paintText(const Napi::CallbackInfo&info, bool stroke) {
24722472

24732473
PangoLayout *layout = this->layout();
24742474

2475+
// If fonts have been registered, the PangoContext is using an outdated FontMap
2476+
if (canvas()->fontSerial != fontSerial) {
2477+
pango_context_set_font_map(
2478+
pango_layout_get_context(layout),
2479+
pango_cairo_font_map_get_default()
2480+
);
2481+
2482+
fontSerial = canvas()->fontSerial;
2483+
}
2484+
24752485
pango_layout_set_text(layout, str.c_str(), -1);
24762486
pango_cairo_update_layout(context(), layout);
24772487

src/CanvasRenderingContext2d.h

+1
Original file line numberDiff line numberDiff line change
@@ -227,4 +227,5 @@ class Context2d : public Napi::ObjectWrap<Context2d> {
227227
cairo_t *_context = nullptr;
228228
cairo_path_t *_path;
229229
PangoLayout *_layout = nullptr;
230+
int fontSerial = 0;
230231
};

0 commit comments

Comments
 (0)