mrmBot-Matrix/natives/sonic.cc

51 lines
1.4 KiB
C++
Raw Normal View History

2020-07-22 18:12:38 +00:00
#include <napi.h>
#include <list>
#include <Magick++.h>
using namespace std;
using namespace Magick;
class SonicWorker : public Napi::AsyncWorker {
public:
SonicWorker(Napi::Function& callback, string text)
: Napi::AsyncWorker(callback), text(text) {}
~SonicWorker() {}
void Execute() {
Image image;
Image text_image;
text_image.backgroundColor("none");
text_image.fontPointsize(72);
text_image.textGravity(Magick::CenterGravity);
text_image.font("Bitstream Vera Sans");
text_image.read("pango:<span foreground='white'>" + text + "</span>");
text_image.resize(Geometry(474, 332));
text_image.extent(Geometry("1024x538-435-145"), Magick::CenterGravity);
image.read("./assets/images/sonic.jpg");
image.composite(text_image, Geometry("+160+10"), Magick::OverCompositeOp);
image.magick("PNG");
image.write(&blob);
}
void OnOK() {
Callback().Call({Env().Undefined(), Napi::Buffer<char>::Copy(Env(), (char *)blob.data(), blob.length())});
}
private:
string text, type;
2020-07-28 14:38:55 +00:00
int delay;
2020-07-22 18:12:38 +00:00
Blob blob;
};
Napi::Value Sonic(const Napi::CallbackInfo &info)
{
Napi::Env env = info.Env();
Napi::Object obj = info[0].As<Napi::Object>();
2020-07-22 18:12:38 +00:00
Napi::Function cb = info[1].As<Napi::Function>();
string text = obj.Get("text").As<Napi::String>().Utf8Value();
2020-07-22 18:12:38 +00:00
SonicWorker* explodeWorker = new SonicWorker(cb, text);
explodeWorker->Queue();
return env.Undefined();
}