From bf19e240630e1e606984fec5110ee4ce4d672898 Mon Sep 17 00:00:00 2001 From: TheEssem Date: Tue, 21 Jul 2020 09:03:08 -0500 Subject: [PATCH] Ported homebrew, fixed explode --- commands/explode.js | 2 +- commands/homebrew.js | 10 ++++----- natives/homebrew.cc | 49 ++++++++++++++++++++++++++++++++++++++++++++ natives/homebrew.h | 8 ++++++++ natives/image.cc | 2 ++ 5 files changed, 64 insertions(+), 7 deletions(-) create mode 100644 natives/homebrew.cc create mode 100644 natives/homebrew.h diff --git a/commands/explode.js b/commands/explode.js index 9ccf09d..2fc287e 100644 --- a/commands/explode.js +++ b/commands/explode.js @@ -5,7 +5,7 @@ exports.run = async (message) => { message.channel.sendTyping(); const image = await require("../utils/imagedetect.js")(message); if (image === undefined) return `${message.author.mention}, you need to provide an image to explode!`; - const buffer = await promisify(magick.explode)(image.path, image.type.toUpperCase(), image.delay ? (100 / image.delay.split("/")[0]) * image.delay.split("/")[1] : 0); + const buffer = await promisify(magick.explode)(image.path, -1, image.type.toUpperCase(), image.delay ? (100 / image.delay.split("/")[0]) * image.delay.split("/")[1] : 0); return { file: buffer, name: `explode.${image.type}` diff --git a/commands/homebrew.js b/commands/homebrew.js index 6b2818f..a09a7cc 100644 --- a/commands/homebrew.js +++ b/commands/homebrew.js @@ -1,13 +1,11 @@ -const gm = require("gm").subClass({ - imageMagick: true -}); +const magick = require("../build/Release/image.node"); +const { promisify } = require("util"); exports.run = async (message, args) => { if (args.length === 0) return `${message.author.mention}, you need to provide some text to make a Homebrew Channel edit!`; message.channel.sendTyping(); - const template = "./assets/images/hbc.png"; - const cleanedMessage = args.join(" ").toLowerCase().replace(/\n/g, " "); - const buffer = await gm(template).gravity("Center").font("./assets/hbc.ttf").out("-kerning", "-5").fill("white").pointSize(96).drawText(0, 0, cleanedMessage).bufferPromise("png"); + //const buffer = await gm(template).gravity("Center").font("./assets/hbc.ttf").out("-kerning", "-5").fill("white").pointSize(96).drawText(0, 0, cleanedMessage).bufferPromise("png"); + const buffer = await promisify(magick.homebrew)(args.join(" ").toLowerCase().replace(/\n/g, " ")); return { file: buffer, name: "homebrew.png" diff --git a/natives/homebrew.cc b/natives/homebrew.cc new file mode 100644 index 0000000..88de908 --- /dev/null +++ b/natives/homebrew.cc @@ -0,0 +1,49 @@ +#include +#include +#include +#include + +using namespace std; +using namespace Magick; + +class HomebrewWorker : public Napi::AsyncWorker { + public: + HomebrewWorker(Napi::Function& callback, string text) + : Napi::AsyncWorker(callback), text(text) {} + ~HomebrewWorker() {} + + void Execute() { + Image image; + image.read("./assets/images/hbc.png"); + image.textGravity(Magick::CenterGravity); + image.font("./assets/hbc.ttf"); + image.textKerning(-5); + image.fillColor("white"); + image.fontPointsize(96); + image.draw(DrawableText(0, 0, text)); + image.magick("PNG"); + image.write(&blob); + } + + void OnOK() { + Callback().Call({Env().Undefined(), Napi::Buffer::Copy(Env(), (char *)blob.data(), blob.length())}); + } + + private: + string text, type; + int delay, wordlength, i, n; + size_t bytes, type_size; + Blob blob; +}; + +Napi::Value Homebrew(const Napi::CallbackInfo &info) +{ + Napi::Env env = info.Env(); + + string text = info[0].As().Utf8Value(); + Napi::Function cb = info[1].As(); + + HomebrewWorker* explodeWorker = new HomebrewWorker(cb, text); + explodeWorker->Queue(); + return env.Undefined(); +} \ No newline at end of file diff --git a/natives/homebrew.h b/natives/homebrew.h new file mode 100644 index 0000000..25ac50e --- /dev/null +++ b/natives/homebrew.h @@ -0,0 +1,8 @@ +#ifndef ESMBOT_NATIVES_HOMEBREW_H_ +#define ESMBOT_NATIVES_HOMEBREW_H_ + +#include + +Napi::Value Homebrew(const Napi::CallbackInfo& info); + +#endif \ No newline at end of file diff --git a/natives/image.cc b/natives/image.cc index 30b9285..d46c67f 100644 --- a/natives/image.cc +++ b/natives/image.cc @@ -11,6 +11,7 @@ #include "freeze.h" #include "gamexplain.h" #include "globe.h" +#include "homebrew.h" #include "invert.h" #include "jpeg.h" #include "leak.h" @@ -33,6 +34,7 @@ Napi::Object Init(Napi::Env env, Napi::Object exports) exports.Set(Napi::String::New(env, "freeze"), Napi::Function::New(env, Freeze)); exports.Set(Napi::String::New(env, "gamexplain"), Napi::Function::New(env, Gamexplain)); exports.Set(Napi::String::New(env, "globe"), Napi::Function::New(env, Globe)); + exports.Set(Napi::String::New(env, "homebrew"), Napi::Function::New(env, Homebrew)); exports.Set(Napi::String::New(env, "invert"), Napi::Function::New(env, Invert)); exports.Set(Napi::String::New(env, "jpeg"), Napi::Function::New(env, Jpeg)); exports.Set(Napi::String::New(env, "leak"), Napi::Function::New(env, Leak));