From 6b80c9707645a13cf5ed6eb0259334fce8f95599 Mon Sep 17 00:00:00 2001 From: Daniel Odendahl Jr Date: Wed, 13 Feb 2019 14:37:37 +0000 Subject: [PATCH] Don't allow skinny images in meme-gen --- commands/image-edit/meme-gen.js | 2 ++ commands/image-edit/nike-ad.js | 1 + package.json | 2 +- util/Canvas.js | 1 + 4 files changed, 5 insertions(+), 1 deletion(-) diff --git a/commands/image-edit/meme-gen.js b/commands/image-edit/meme-gen.js index 8e3467cc..2e23d2d7 100644 --- a/commands/image-edit/meme-gen.js +++ b/commands/image-edit/meme-gen.js @@ -56,6 +56,7 @@ module.exports = class MemeGenCommand extends Command { ctx.textAlign = 'center'; ctx.textBaseline = 'top'; const topLines = await wrapText(ctx, top, base.width - 10); + if (!topLines) return msg.reply('There\'s not enough width to make a meme with this image.'); for (let i = 0; i < topLines.length; i++) { const textHeight = (i * fontSize) + (i * 10); ctx.strokeStyle = 'black'; @@ -65,6 +66,7 @@ module.exports = class MemeGenCommand extends Command { ctx.fillText(topLines[i], base.width / 2, textHeight); } const bottomLines = await wrapText(ctx, bottom, base.width - 10); + if (!bottomLines) return msg.reply('There\'s not enough width to make a meme with this image.'); ctx.textBaseline = 'bottom'; const initial = base.height - ((bottomLines.length - 1) * fontSize) - ((bottomLines.length - 1) * 10); for (let i = 0; i < bottomLines.length; i++) { diff --git a/commands/image-edit/nike-ad.js b/commands/image-edit/nike-ad.js index 3db9c450..670c3718 100644 --- a/commands/image-edit/nike-ad.js +++ b/commands/image-edit/nike-ad.js @@ -61,6 +61,7 @@ module.exports = class NikeAdCommand extends Command { ctx.fillStyle = 'white'; ctx.textAlign = 'center'; const lines = await wrapText(ctx, `Believe in ${something}. Even if it means ${sacrifice}.`, data.width - 20); + if (!lines) return msg.reply('There\'s not enough width to make a Nike ad with this image.'); const initial = data.height / 2; for (let i = 0; i < lines.length; i++) { const textHeight = initial + (i * fontSize) + (i * 10); diff --git a/package.json b/package.json index e8abc323..eca27627 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "xiao", - "version": "101.1.0", + "version": "101.1.1", "description": "Your personal server companion.", "main": "Xiao.js", "scripts": { diff --git a/util/Canvas.js b/util/Canvas.js index e88b56ef..0784bea3 100644 --- a/util/Canvas.js +++ b/util/Canvas.js @@ -99,6 +99,7 @@ module.exports = class CanvasUtil { static wrapText(ctx, text, maxWidth) { return new Promise(resolve => { if (ctx.measureText(text).width < maxWidth) return resolve([text]); + if (ctx.measureText('W').width > maxWidth) return resolve(null); const words = text.split(' '); const lines = []; let line = '';