diff --git a/src/core/operations/AddTextToImage.mjs b/src/core/operations/AddTextToImage.mjs index 3963919b..c137b492 100644 --- a/src/core/operations/AddTextToImage.mjs +++ b/src/core/operations/AddTextToImage.mjs @@ -133,11 +133,12 @@ class AddTextToImage extends Operation { } catch (err) { throw new OperationError(`Error loading image. (${err})`); } - try { - if (isWorkerEnvironment()) - self.sendStatusMessage("Adding text to image..."); - const fontsMap = {}; + if (isWorkerEnvironment()) + self.sendStatusMessage("Adding text to image..."); + + const fontsMap = {}; + try { const fonts = [ import( /* webpackMode: "eager" */ "../../web/static/fonts/bmfonts/Roboto72White.fnt" @@ -159,7 +160,6 @@ class AddTextToImage extends Operation { fontsMap["Roboto Mono"] = fonts[2]; fontsMap["Roboto Slab"] = fonts[3]; }); - // Make Webpack load the png font images await Promise.all([ import( @@ -175,11 +175,16 @@ class AddTextToImage extends Operation { /* webpackMode: "eager" */ "../../web/static/fonts/bmfonts/RobotoBlack72White.png" ), ]); + } catch (err) { + throw new OperationError(`Error preparing fonts. (${err})`); + } + let jimpFont; + try { const font = fontsMap[fontFace]; // LoadFont needs an absolute url, so append the font name to self.docURL - const jimpFont = await loadFont(self.docURL + "/" + font.default); + jimpFont = await loadFont(self.docURL + "/" + font.default); jimpFont.pages.forEach(function (page) { if (page.bitmap) { @@ -210,7 +215,11 @@ class AddTextToImage extends Operation { } } }); + } catch (err) { + throw new OperationError(`Error loading font. (${err})`); + } + try { // Create a temporary image to hold the rendered text const textImage = new Jimp({ width: measureText(jimpFont, text), @@ -271,7 +280,11 @@ class AddTextToImage extends Operation { x: xPos, y: yPos, }); + } catch (err) { + throw new OperationError(`Error adding text to image. (${err})`); + } + try { let imageBuffer; if (image.mime === "image/gif") { imageBuffer = await image.getBuffer(JimpMime.png); @@ -280,7 +293,7 @@ class AddTextToImage extends Operation { } return imageBuffer.buffer; } catch (err) { - throw new OperationError(`Error adding text to image. (${err})`); + throw new OperationError(`Error exporting image. (${err})`); } } diff --git a/src/web/static/fonts/bmfonts/RobotoMono72White.fnt b/src/web/static/fonts/bmfonts/RobotoMono72White.fnt index 7c7bd812..10ccee57 100644 --- a/src/web/static/fonts/bmfonts/RobotoMono72White.fnt +++ b/src/web/static/fonts/bmfonts/RobotoMono72White.fnt @@ -104,6 +104,7 @@ - + + diff --git a/tests/browser/02_ops.js b/tests/browser/02_ops.js index c340fca4..cc1bc4cf 100644 --- a/tests/browser/02_ops.js +++ b/tests/browser/02_ops.js @@ -34,7 +34,10 @@ module.exports = { testOp(browser, "AES Encrypt", "test input", "e42eb8fbfb7a98fff061cd2c1a794d92", [{"option": "Hex", "string": "00112233445566778899aabbccddeeff"}, {"option": "Hex", "string": "00000000000000000000000000000000"}, "CBC", "Raw", "Hex"]); testOp(browser, "AND", "test input", "4$04 $044", [{ "option": "Hex", "string": "34" }]); testOp(browser, "Add line numbers", "test input", "1 test input"); - testOp(browser, ["From Hex", "Add Text To Image", "To Base64"], Images.PNG_HEX, Images.PNG_CHEF_B64, [[], ["Chef", "Center", "Middle", 0, 0, 16], []]); + testOp(browser, ["From Hex", "Add Text To Image", "To Base64"], Images.PNG_HEX, Images.PNG_CHEF_B64, [[], ["Chef", "Center", "Middle", 0, 0, 16, "Roboto"], []]); + testOp(browser, ["From Hex", "Add Text To Image", "To Base64"], Images.PNG_HEX, Images.PNG_CHEF_B64, [[], ["Chef", "Center", "Middle", 0, 0, 16, "Roboto Black"], []]); + testOp(browser, ["From Hex", "Add Text To Image", "To Base64"], Images.PNG_HEX, Images.PNG_CHEF_B64, [[], ["Chef", "Center", "Middle", 0, 0, 16, "Roboto Mono"], []]); + testOp(browser, ["From Hex", "Add Text To Image", "To Base64"], Images.PNG_HEX, Images.PNG_CHEF_B64, [[], ["Chef", "Center", "Middle", 0, 0, 16, "Roboto Slab"], []]); testOp(browser, ["From Hex", "Dither Image", "SHA2"], Images.PNG_HEX, "cbf587a78915cfb14546ba83080b13e5054800802488dd0cb786b8951e7dc0b48f055260917bd0ccfc075e422b9d6aff112948562653995d74e70f0b66367ac3", [[], [], []]); testOp(browser, ["From Hex", "Generate Image", "SHA2"], Images.PNG_HEX, "2c451762a6c9192fd31dc80765eab3f447be70ea51f6fdb6911ade4d89d4a98bd0a1ff00b08d76aac472faeceb54b66092e3f3be7bbf899bf3e55ca9c96a56aa", [[], [], []]); testOp(browser, ["From Hex", "Image Hue/Saturation/Lightness", "SHA2"], Images.PNG_HEX, "522dfc0bbef00e05c5d6861a002039fa2952e4bbb7fe8d21d0d538ef6f9d65da82065929b4150dc5b8b49460ee6c9bef7f660b86f8d4e7442a07c61c0a152a4b", [[], [50, 50, 50], []]);