Skip to content

Commit fdf709a

Browse files
chearonzbjornson
authored andcommitted
move ctx.font string to the state struct
fixes #1946
1 parent 4c276e0 commit fdf709a

File tree

4 files changed

+6
-12
lines changed

4 files changed

+6
-12
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ project adheres to [Semantic Versioning](http://semver.org/).
1212
### Fixed
1313
* Add missing property `canvas` to the `CanvasRenderingContext2D` type
1414
* Fixed glyph positions getting rounded, resulting text having a slight `letter-spacing` effect
15+
* Fixed `ctx.font` not being restored correctly after `ctx.restore()` (#1946)
1516

1617
2.11.0
1718
==================

src/CanvasRenderingContext2d.cc

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,6 @@ void Context2d::resetState() {
207207
void Context2d::_resetPersistentHandles() {
208208
_fillStyle.Reset();
209209
_strokeStyle.Reset();
210-
_font.Reset();
211210
}
212211

213212
/*
@@ -2553,15 +2552,8 @@ NAN_METHOD(Context2d::MoveTo) {
25532552
NAN_GETTER(Context2d::GetFont) {
25542553
CHECK_RECEIVER(Context2d.GetFont);
25552554
Context2d *context = Nan::ObjectWrap::Unwrap<Context2d>(info.This());
2556-
Isolate *iso = Isolate::GetCurrent();
2557-
Local<Value> font;
2558-
2559-
if (context->_font.IsEmpty())
2560-
font = Nan::New("10px sans-serif").ToLocalChecked();
2561-
else
2562-
font = context->_font.Get(iso);
25632555

2564-
info.GetReturnValue().Set(font);
2556+
info.GetReturnValue().Set(Nan::New(context->state->font).ToLocalChecked());
25652557
}
25662558

25672559
/*
@@ -2624,7 +2616,7 @@ NAN_SETTER(Context2d::SetFont) {
26242616
context->state->fontDescription = sys_desc;
26252617
pango_layout_set_font_description(context->_layout, sys_desc);
26262618

2627-
context->_font.Reset(value);
2619+
context->state->font = *Nan::Utf8String(value);
26282620
}
26292621

26302622
/*

src/CanvasRenderingContext2d.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ struct canvas_state_t {
2727
cairo_pattern_t* fillGradient = nullptr;
2828
cairo_pattern_t* strokeGradient = nullptr;
2929
PangoFontDescription* fontDescription = nullptr;
30+
std::string font = "10px sans-serif";
3031
cairo_filter_t patternQuality = CAIRO_FILTER_GOOD;
3132
float globalAlpha = 1.f;
3233
int shadowBlur = 0;
@@ -57,6 +58,7 @@ struct canvas_state_t {
5758
shadowOffsetY = other.shadowOffsetY;
5859
textDrawingMode = other.textDrawingMode;
5960
fontDescription = pango_font_description_copy(other.fontDescription);
61+
font = other.font;
6062
imageSmoothingEnabled = other.imageSmoothingEnabled;
6163
}
6264

@@ -216,7 +218,6 @@ class Context2d : public Nan::ObjectWrap {
216218
void _setStrokePattern(v8::Local<v8::Value> arg);
217219
Nan::Persistent<v8::Value> _fillStyle;
218220
Nan::Persistent<v8::Value> _strokeStyle;
219-
Nan::Persistent<v8::Value> _font;
220221
Canvas *_canvas;
221222
cairo_t *_context;
222223
cairo_path_t *_path;

test/canvas.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1918,7 +1918,7 @@ describe('Canvas', function () {
19181918
['shadowBlur', 5],
19191919
['shadowColor', '#ff0000'],
19201920
['globalCompositeOperation', 'copy'],
1921-
// ['font', '25px serif'], // TODO #1946
1921+
['font', '25px serif'],
19221922
['textAlign', 'center'],
19231923
['textBaseline', 'bottom'],
19241924
// Added vs. WPT

0 commit comments

Comments
 (0)