Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function genTextAvatar(text, outputFilePathOrStream, size, callback) {
var img = PImage.make(size, size);
var ctx = img.getContext('2d');
var font = PImage.registerFont(`${__dirname}/../public/font/mplus-1m-regular.ttf`, 'mplus-1m-regular');
font.load(function () {
ctx.fillStyle = getColor(text);
ctx.fillRect(0, 0, size, size);
var fontSize = size / 1.5
ctx.font = `${fontSize}pt 'mplus-1m-regular'`;
ctx.fillStyle = "#FFFFFF";
var t = text.substring(0, 2).toUpperCase();
console.log(size , fontSize)
if (t.length == 2) { // 2
ctx.fillText(t, (size / 2) - (fontSize / 2), size / 1.4);
} else if (t.length == 1) { // 1
ctx.fillText(t, size / 2 - (fontSize / 4), size / 1.4);
} else { // 0
ctx.fillText(t, 0, 0);
}
generateFontTexture(callback) {
let fnt = PImage.registerFont(this.fontUrl, 'Source Sans Pro');
this.fontLoading = true;
fnt.load(() => {
this.font = fnt.font;
this.fontScale = 1 / this.font.unitsPerEm * this.fontSize;
let { img, ctx } = this;
ctx.clearRect(0, 0, this.textureWidth, this.textureHeight);
ctx.mode = 'REPLACE'; // replace is used to disable alpha blending
ctx.setFont('Source Sans Pro', this.fontSize);
ctx.fillStyle = 'red';
var charHeight = 20,
charWidth = 20,
return new Promise(resolve => {
const img = PureImage.make(this.width, this.height)
const ctx = img.getContext('2d')
const font = PureImage.registerFont(path.join(__dirname, this.fontFile), this.fontName)
return font.load(() => {
ctx.fillStyle = this.colorBackground
ctx.fillRect(0, 0, this.width, this.height)
ctx.font = `${this.fontSize}px '${this.fontName}'`
ctx.fillStyle = this.colorText
// add text
ctx.fillText(this.metadata.title, this.marginLeft, this.getPosY())
ctx.fillText('', this.marginLeft, this.getPosY())
ctx.fillText('Creator:', this.marginLeft, this.getPosY())
ctx.fillText(this.metadata.creator, this.marginLeft, this.getPosY())
ctx.fillText('', this.marginLeft, this.getPosY())
await new Promise((resolve) => registerFont(fontFilePath, fontFamily).load(resolve));