mrmBot-Matrix/natives/homebrew.cc

47 lines
1.2 KiB
C++
Raw Normal View History

2020-07-21 14:03:08 +00:00
#include <napi.h>
#include <list>
2020-07-21 14:03:08 +00:00
#include <Magick++.h>
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<char>::Copy(Env(), (char *)blob.data(), blob.length())});
}
private:
2020-07-28 14:38:55 +00:00
string text;
2020-07-21 14:03:08 +00:00
Blob blob;
};
Napi::Value Homebrew(const Napi::CallbackInfo &info)
{
Napi::Env env = info.Env();
Napi::Object obj = info[0].As<Napi::Object>();
2020-07-21 14:03:08 +00:00
Napi::Function cb = info[1].As<Napi::Function>();
string caption = obj.Get("caption").As<Napi::String>().Utf8Value();
2020-07-21 14:03:08 +00:00
HomebrewWorker* explodeWorker = new HomebrewWorker(cb, caption);
2020-07-21 14:03:08 +00:00
explodeWorker->Queue();
return env.Undefined();
}