From e81ae48a71444249fb3bc96c20632af689144260 Mon Sep 17 00:00:00 2001 From: Nathan Rajlich Date: Thu, 9 Nov 2023 02:24:45 -0800 Subject: [PATCH] Add `CanvasRenderingContext2D#font` getter --- .changeset/breezy-brooms-rest.md | 5 +++++ packages/runtime/src/canvas.ts | 11 ++++++++--- 2 files changed, 13 insertions(+), 3 deletions(-) create mode 100644 .changeset/breezy-brooms-rest.md diff --git a/.changeset/breezy-brooms-rest.md b/.changeset/breezy-brooms-rest.md new file mode 100644 index 00000000..5e823216 --- /dev/null +++ b/.changeset/breezy-brooms-rest.md @@ -0,0 +1,5 @@ +--- +'nxjs-runtime': patch +--- + +Add `CanvasRenderingContext2D#font` getter diff --git a/packages/runtime/src/canvas.ts b/packages/runtime/src/canvas.ts index f36114a3..623127ee 100644 --- a/packages/runtime/src/canvas.ts +++ b/packages/runtime/src/canvas.ts @@ -47,6 +47,7 @@ interface CanvasRenderingContext2DInternal { fillStyle: RGBA; strokeStyle: RGBA; currentStyle?: RGBA; + font: string; } const ctxInternalMap = new WeakMap< @@ -86,6 +87,7 @@ export class CanvasRenderingContext2D { ctx: Switch.native.canvasNewContext(w, h), strokeStyle: [0, 0, 0, 1], fillStyle: [0, 0, 0, 1], + font: '' }); this.font = '10px system-ui'; @@ -644,12 +646,14 @@ export class CanvasRenderingContext2D { } get font(): string { - // TODO: implement - return ''; + return internal(this).font; } set font(v: string) { if (!v) return; + const i = internal(this); + if (i.font === v) return; + const parsed = parseCssFont(v); if ('system' in parsed) { // "system" fonts are not supported @@ -677,8 +681,9 @@ export class CanvasRenderingContext2D { return; } } + i.font = v; native.canvasSetFont( - internal(this).ctx, + i.ctx, fontFaceInternal.get(font)!.fontFace, px );