1111 * This source code is licensed under the permissive MIT license.
1212 */
1313
14- import { Image , CanvasRenderingContext2D } from "canvas"
14+ import * as Path from "path"
15+ import { Image , SKRSContext2D , GlobalFonts } from "@napi-rs/canvas"
1516import { Rect } from "../../util/rect"
1617import { Drawable } from "./drawable"
1718
@@ -20,8 +21,15 @@ interface WrapTextResult {
2021 adjustedFontSize : number
2122}
2223
23- function renderTextInRect ( ctx : CanvasRenderingContext2D ,
24+ let fontsRegistered = false
25+
26+ function renderTextInRect ( ctx : SKRSContext2D ,
2427 text : string , font : string , rect : Rect , maxFontSize ?: number ) : void {
28+ if ( ! fontsRegistered ) {
29+ const fontPath = Path . join ( __dirname , ".." , ".." , ".." , "res" , "font" , "OpenSans-SemiBold.ttf" )
30+ GlobalFonts . registerFromPath ( fontPath , "OpenSans" )
31+ fontsRegistered = true
32+ }
2533 // start with a large font size
2634 let fontSize = maxFontSize || rect . height
2735 let res = wrappedTextAtSize ( ctx , text , font , rect . width , fontSize )
@@ -57,7 +65,7 @@ function renderTextInRect(ctx: CanvasRenderingContext2D,
5765 }
5866}
5967
60- function wrappedTextAtSize ( ctx : CanvasRenderingContext2D ,
68+ function wrappedTextAtSize ( ctx : SKRSContext2D ,
6169 text : string , fontFace : string , maxWidth : number , fontSize : number ) : WrapTextResult | null {
6270 const words = text . split ( " " )
6371 const lines = [ ]
@@ -113,11 +121,11 @@ export class MemeTile implements Drawable {
113121 this . width = width
114122 }
115123
116- public drawInContext ( ctx : CanvasRenderingContext2D , offset ?: { x : number ; y : number } ) : void {
124+ public drawInContext ( ctx : SKRSContext2D , offset ?: { x : number ; y : number } ) : void {
117125 offset = offset || { x : 0 , y : 0 }
118126 const textRect = new Rect ( offset . x , offset . y , this . width / 2 , this . height )
119127 ctx . fillStyle = "#000"
120- renderTextInRect ( ctx , this . text , "Arial " , textRect , 36 )
128+ renderTextInRect ( ctx , this . text , "OpenSans " , textRect , 36 )
121129 ctx . drawImage ( this . image , this . width / 2 , offset . y , this . width / 2 , this . height )
122130 }
123131}
0 commit comments