'How to find missing font glyphs using node canvas?

I am trying to find missing glyphs with node canvas by comparing the rendered glyph with a known missing glyph, but it's not working. Any ideas?

const { createCanvas, loadImage } = require('canvas')
const fs = require('fs')

const testCanvas = createCanvas(200, 200)
const referenceCanvas = createCanvas(200, 200)
const ctxTest = testCanvas.getContext('2d')
const ctxRef = referenceCanvas.getContext('2d')

ctxRef.fillText('𠆼', 0, 0)

const refData = ctxRef.getImageData(0, 0, referenceCanvas.width, referenceCanvas.height).data.join('')

fs.readFileSync('path-to-chars.txt', 'utf-8').trim()
  .split(/\n+/)
  .filter(x => x.startsWith('U+'))
  .map(x => {
    let [a, b, c] = x.split(/\t/)
    let q = String.fromCodePoint(parseInt(a.replace('U+', ''), 16))
    if (test(q)) {
      console.log('skip', a, q)
      return
    }
    c.match(/\^([^\$]+)\$/)
    const fullText = RegExp.$1
    const radicals = [...fullText.replace(/[⿰⿱⿲⿳⿴⿵⿶⿷⿸⿹⿺⿻]/g, '').replace(/\{[^\}]+\}/g, '')]
    for (let i = 0, n = radicals.length; i < n; i++) {
      const char = radicals[i]
      if (test(char)) return
    }
    console.log(radicals, radicals.map(x => x.codePointAt(0).toString(16)))
    return q
  })

function test(char) {
  ctxTest.clearRect(0, 0, testCanvas.width, testCanvas.height)
  ctxTest.fillText(char, 0, 0)
  const testData = ctxTest.getImageData(0, 0, testCanvas.width, testCanvas.height).data.join('')
  console.log(char, testData === refData)
  return testData === refData
}

enter image description here

I don't know what ones are missing in advance. I am just trying to filter them out.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source