samizdatco / skia-canvas

A GPU-accelerated 2D graphics environment for Node.js
MIT License
1.7k stars 66 forks source link

Removing a font from (or clearing) the FontLibrary #94

Closed Mat-thieu closed 2 years ago

Mat-thieu commented 2 years ago

Is it possible to remove an already registered font? My concern is that the library will fill up quite a lot for my use case. It's a long running instance with many fonts being loaded, it would be ideal if I could periodically clear the fonts library.

aastefian commented 2 years ago

Yes, this is very much needed. Besides memory hogging, we also receive sometimes patched version of a font, so we need to be able to remove the existing font and replace it with the patched one, which seems we cannot do atm.

aastefian commented 2 years ago

Hello, first of all thanks a lot for your time, I really appreciate you taking a look at this and making an implementation.

That being said, i have either trouble understanding how to use the reset function or there is an issue with it. Using this snippet below, i am loading two versions of the same font (original, and patched which has some characters changed) and writing two lines of text with each. It seems that the patched font is not used and the original font characters are shown even when calling the reset function added by this commit.

I also tried the functionality added by this commit and when calling FontLibrary.use with the same alias (which is supposed to trigger the replacement of the original font by the patched one) the second call does not override the initially registered font.

What would you think is the issue here ?

const fs = require('fs')
const { fabric } = require('fabric');

// Skia
const { Canvas, FontLibrary } = require('skia-canvas')

const skia = { 
  patchFontTest: function(ctx) {
    let fontFamily = 'custom-CAMetro__Superfat';
    let originalFontFilePath = __dirname + '/../fonts/BodoniFLF-Bold.ttf';
    let patchedFontFilePath = __dirname + '/../fonts/BodoniFLF-Bold-patched.ttf';

    FontLibrary.use(fontFamily, [originalFontFilePath]);
    ctx.font = `100px ${fontFamily}`
    ctx.fillText('Superfat', 30, 340)

    // Works - loads the patched font by appending random string to alias
    // if (FontLibrary.has(fontFamily)) {
      // fontFamily += '-06gff4';
    // }

    // Doesn't work, patched font is not loaded
    FontLibrary.reset(fontFamily);

    FontLibrary.use(fontFamily, [patchedFontFilePath]);
    ctx.font = `100px ${fontFamily}`
    ctx.fillText('Superfat', 30, 460)
  }
}

async function testSkia() {
  let canvas = new Canvas(500, 500),
    ctx = canvas.getContext("2d");

  // Set background color
  ctx.fillStyle = "white";
  ctx.fillRect(0, 0, canvas.width, canvas.height);
  ctx.fillStyle = "black";

  skia.patchFontTest(ctx);

  await canvas.saveAs(__dirname + '/../output/test-skia.png')
}

testSkia();

Expected: image

Rceived: image

samizdatco commented 2 years ago

Could you send me a pair of fonts where one is the original and one is 'patched' in the way you've been doing? Part of the problem is that I don't have multiple fonts with the same metadata but different outlines to test with...

aastefian commented 2 years ago

Hello, sure.

In the zip you will find two versions of a font. The letter S (capital s) was the one that was patched and should look weird like this: image

BodoniFLF-Bold.zip

samizdatco commented 2 years ago

Thanks, that helped a lot! I think I've got both the FontLibrary.reset() method (which doesn't take any arguments, it just wipes out everything) and the replacement functionality working now.

aastefian commented 2 years ago

Hello, that is great news, thank you.

I will give it a go as soon as i can. 🙏

aastefian commented 2 years ago

Do you have an idea about when you will be able to do a release ?

samizdatco commented 2 years ago

Do you have an idea about when you will be able to do a release ?

As soon as #105 wraps up. In the meantime, you can test out the font changes by installing the release-candidate build from that branch:

npm install https://github.com/samizdatco/skia-canvas#gpu
aastefian commented 2 years ago

Awesome, thank you !