From c1ca61f01c0a6d72f0b6034f50dd9b5848587830 Mon Sep 17 00:00:00 2001 From: Hipperooni Date: Sun, 26 Nov 2023 20:10:32 +1100 Subject: [PATCH] 3rd time lucky --- src/discord/commands/guild/d.profile.ts | 12 +--------- src/discord/utils/canvasUtils.ts | 29 ++++++++++--------------- 2 files changed, 13 insertions(+), 28 deletions(-) diff --git a/src/discord/commands/guild/d.profile.ts b/src/discord/commands/guild/d.profile.ts index e13dd1f6f..0ee96c5b4 100644 --- a/src/discord/commands/guild/d.profile.ts +++ b/src/discord/commands/guild/d.profile.ts @@ -443,22 +443,12 @@ export const dProfile: SlashCommand = { // WIP: Check to see if a user has bought a title in the shop // If so, move Username Text up so the title can fit underneath - // Username Text Resize to fit - const applyUsername = (canvas:Canvas.Canvas, text:string) => { - const usernameContext = canvas.getContext('2d'); - do { - fontSize -= 1; - usernameContext.font = `${fontSize}px ${userFont}`; - } while (usernameContext.measureText(text).width > 508); - return usernameContext.font; - }; - // Username Text const filteredDisplayName = await deFuckifyText(target.displayName); context.font = `50px ${userFont}`; context.fillStyle = textColor; context.textBaseline = 'middle'; - let fontSize = 50; + const fontSize = 50; const maxLength = 508; context.font = resizeText(canvasObj, filteredDisplayName, fontSize, userFont, maxLength); context.fillText(`${filteredDisplayName}`, 146, 76); diff --git a/src/discord/utils/canvasUtils.ts b/src/discord/utils/canvasUtils.ts index 53d687792..8d8a0c0be 100644 --- a/src/discord/utils/canvasUtils.ts +++ b/src/discord/utils/canvasUtils.ts @@ -49,25 +49,20 @@ const fancyAlphabets = [ ]; export function deFuckifyText(text: string): string { - let normalText = ''; const normalAlphabetArray = Array.from(normalAlphabet); - for (const char of Array.from(text)) { + return Array.from(text).map(char => { if (/^[\x20-\x7E]$/g.test(char)) { - normalText += char; - } else { - let found = false; - for (const fancyAlphabet of fancyAlphabets) { - const index = fancyAlphabet.indexOf(char); - if (index !== -1) { - normalText += normalAlphabetArray[index]; - found = true; - break; - } - } - if (!found && normalAlphabet.includes(char)) { - normalText += char; + return char; + } + for (let i = 0; i < fancyAlphabets.length; i += 1) { + const index = fancyAlphabets[i].indexOf(char); + if (index !== -1) { + return normalAlphabetArray[index]; } } - } - return normalText; + if (normalAlphabet.includes(char)) { + return char; + } + return ''; + }).join(''); }