99import _positions from "../../positions.json" with { type : "json" } ;
1010import _sizes from "../../sizes.json" with { type : "json" } ;
1111import {
12- Canvas ,
12+ type Canvas ,
1313 type CanvasRenderingContext2D ,
1414 type ImageData ,
1515} from "skia-canvas" ;
1616import { type TextNode , type Token , tokens } from "./tokens.js" ;
17+ import { createCanvas } from "../canvas.js" ;
1718import { join } from "node:path" ;
1819import { loadImage } from "#hooks" ;
1920import { readdir } from "node:fs/promises" ;
@@ -34,9 +35,13 @@ interface Sizes {
3435
3536export class FontRenderer {
3637 private images : Map < string , CanvasRenderingContext2D > ;
38+ private canvases : WeakMap < CanvasRenderingContext2D , Canvas > ;
39+ private scales : WeakMap < CanvasRenderingContext2D , number > ;
3740
3841 public constructor ( private gradient : boolean ) {
3942 this . images = new Map ( ) ;
43+ this . canvases = new WeakMap ( ) ;
44+ this . scales = new WeakMap ( ) ;
4045 }
4146
4247 public async loadImages ( fontPath : string ) {
@@ -47,13 +52,16 @@ export class FontRenderer {
4752 for ( const file of pictures ) {
4853 const image = await loadImage ( join ( fontPath , file ) ) ;
4954
50- const canvas = new Canvas ( image . width , image . height ) ;
55+ const canvas = createCanvas ( image . width , image . height ) ;
5156 const ctx = canvas . getContext ( "2d" ) ;
5257
5358 ctx . imageSmoothingEnabled = false ;
5459
5560 ctx . drawImage ( image , 0 , 0 ) ;
5661
62+ this . canvases . set ( ctx , canvas ) ;
63+ this . scales . set ( ctx , image . width / 256 ) ;
64+
5765 this . images . set (
5866 file . replace ( "unicode_page_" , "" ) . replace ( ".png" , "" ) ,
5967 ctx
@@ -62,6 +70,8 @@ export class FontRenderer {
6270 }
6371
6472 public measureText ( nodes : TextNode [ ] ) : { width : number ; height : number } {
73+ if ( ! nodes . length ) return { width : 0 , height : 0 } ;
74+
6575 let width = 0 ;
6676 let largestSize = nodes [ 0 ] . size ;
6777
@@ -189,7 +199,18 @@ export class FontRenderer {
189199 }
190200
191201 private getTextureScale ( image : CanvasRenderingContext2D ) {
192- return image . canvas . width / 256 ;
202+ return this . scales . get ( image ) ?? image . canvas . width / 256 ;
203+ }
204+
205+ private getImageData (
206+ image : CanvasRenderingContext2D ,
207+ x : number ,
208+ y : number ,
209+ width : number ,
210+ height : number
211+ ) {
212+ const ctx = this . canvases . get ( image ) ?. getContext ( "2d" ) ?? image ;
213+ return ctx . getImageData ( x , y , width , height ) ;
193214 }
194215
195216 private getCharacterIndexLocation ( unicode : string , isAscii : boolean ) {
@@ -306,7 +327,7 @@ export class FontRenderer {
306327 size,
307328 } = metadata ;
308329
309- const imageData = image . getImageData ( charX , charY , width , height ) ;
330+ const imageData = this . getImageData ( image , charX , charY , width , height ) ;
310331
311332 ctx . filter = this . gradient ? "brightness(15%)" : "brightness(25%)" ;
312333
0 commit comments