From 309225dfe629c9c91745d9b99a1d8e1065eb60d0 Mon Sep 17 00:00:00 2001 From: Dragon Fire Date: Sun, 21 Apr 2024 16:45:32 -0400 Subject: [PATCH] Attempt to draw emoji with color in tweet --- commands/edit-image-text/tweet.js | 37 ++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/commands/edit-image-text/tweet.js b/commands/edit-image-text/tweet.js index 662e7e4a..d3776684 100644 --- a/commands/edit-image-text/tweet.js +++ b/commands/edit-image-text/tweet.js @@ -2,6 +2,8 @@ const Command = require('../../framework/Command'); const { PermissionFlagsBits } = require('discord.js'); const { createCanvas, loadImage } = require('canvas'); const { TwitterOpenApi } = require('twitter-openapi-typescript'); +const emojiRegex = require('emoji-regex'); +const twemoji = require('@twemoji/parser'); const api = new TwitterOpenApi(); const moment = require('moment'); const request = require('node-superfetch'); @@ -131,7 +133,7 @@ module.exports = class TweetCommand extends Command { ctx.fillText(`@${userData.screenName}`, 80, 113); ctx.fillStyle = 'white'; ctx.font = this.client.fonts.get('ChirpRegular.ttf').toCanvasString(23); - ctx.fillText(lines.join('\n'), 17, 160); + this.fillTextWithEmoji(ctx, text, 17, 160, 710, 23); ctx.fillStyle = '#71767b'; ctx.font = this.client.fonts.get('Noto-Regular.ttf').toCanvasString(18); const time = moment().format('h:mm A ∙ MMM D, YYYY ∙'); @@ -196,6 +198,39 @@ module.exports = class TweetCommand extends Command { return msg.say({ files: [{ attachment: canvas.toBuffer(), name: 'tweet.png' }] }); } + async fillTextWithEmoji(ctx, text, x, y, maxLineLen, emojiSize) { + const wrapped = await wrapText(ctx, text, maxLineLen); + const emoji = text.match(emojiRegex()); + if (!emoji.length) { + ctx.fillText(lines.join('\n'), x, y); + return ctx; + } + for (let currentLine = 0; currentLine < wrapped.length; currentLine++) { + const line = wrapped[currentLine]; + const lineNoEmoji = line.split(emojiRegex()); + const lineEmoji = line.match(emojiRegex()); + if (!lineEmoji.length) { + ctx.fillText(line, x, y + (23 * currentLine) + (9 * currentLine)); + continue; + } + let currentX = x; + for (let i = 0; i < lineNoEmoji.length; i++) { + const linePart = lineNoEmoji[i]; + ctx.fillText(linePart, currentX, y + (23 * currentLine) + (9 * currentLine)); + currentX += ctx.measureText(linePart).width; + const parsedEmoji = twemoji.parse(emoji[i]); + if (!parsedEmoji.length || !parsedEmoji[0].url) continue; + const { body } = await request.get(parsedEmoji[0].url); + const loadedEmoji = await loadImage(body); + loadedEmoji.width = emojiSize; + loadedEmoji.height = emojiSize; + ctx.drawImage(loadedEmoji, currentX, y + (23 * currentLine) + (9 * currentLine), emojiSize, emojiSize); + currentX += emojiSize; + } + } + return ctx; + } + async fetchUser(msg, user) { try { const { data } = await this.guestClient.getUserApi().getUserByScreenName({ screenName: user });