2020-07-21 14:03:08 +00:00
|
|
|
#include <napi.h>
|
2021-04-24 22:54:47 +00:00
|
|
|
|
2022-02-24 19:03:36 +00:00
|
|
|
#include <vips/vips8>
|
2020-07-21 14:03:08 +00:00
|
|
|
|
|
|
|
using namespace std;
|
2022-02-24 19:03:36 +00:00
|
|
|
using namespace vips;
|
2020-07-21 14:03:08 +00:00
|
|
|
|
2021-04-24 22:54:47 +00:00
|
|
|
Napi::Value Homebrew(const Napi::CallbackInfo &info) {
|
2020-07-21 14:03:08 +00:00
|
|
|
Napi::Env env = info.Env();
|
2022-06-28 03:50:53 +00:00
|
|
|
Napi::Object result = Napi::Object::New(env);
|
2020-07-21 14:03:08 +00:00
|
|
|
|
2021-04-26 14:47:03 +00:00
|
|
|
try {
|
2022-11-27 19:03:07 +00:00
|
|
|
Napi::Object obj = info[1].As<Napi::Object>();
|
2021-04-26 14:47:03 +00:00
|
|
|
string caption = obj.Get("caption").As<Napi::String>().Utf8Value();
|
2022-04-17 15:40:56 +00:00
|
|
|
string basePath = obj.Get("basePath").As<Napi::String>().Utf8Value();
|
2020-07-21 14:03:08 +00:00
|
|
|
|
2022-04-17 15:40:56 +00:00
|
|
|
string assetPath = basePath + "assets/images/hbc.png";
|
2022-02-24 19:03:36 +00:00
|
|
|
VImage bg = VImage::new_from_file(assetPath.c_str());
|
|
|
|
|
2022-09-23 04:44:54 +00:00
|
|
|
VImage text = VImage::text(
|
|
|
|
".", VImage::option()->set(
|
|
|
|
"fontfile", (basePath + "assets/fonts/hbc.ttf").c_str()));
|
|
|
|
text = VImage::text(
|
|
|
|
("<span letter_spacing=\"-5120\" color=\"white\">" + caption +
|
|
|
|
"</span>")
|
|
|
|
.c_str(),
|
|
|
|
VImage::option()
|
|
|
|
->set("rgba", true)
|
|
|
|
->set("align", VIPS_ALIGN_CENTRE)
|
2022-09-23 22:24:28 +00:00
|
|
|
->set("font", "PF Square Sans Pro, Twemoji Color Font 96")
|
2022-09-23 04:44:54 +00:00
|
|
|
->set("fontfile", (basePath + "assets/fonts/twemoji.otf").c_str()));
|
2022-02-24 19:03:36 +00:00
|
|
|
|
|
|
|
VImage out = bg.composite2(text, VIPS_BLEND_MODE_OVER,
|
|
|
|
VImage::option()
|
|
|
|
->set("x", 400 - (text.width() / 2))
|
|
|
|
->set("y", 300 - (text.height() / 2) - 8));
|
|
|
|
|
|
|
|
void *buf;
|
|
|
|
size_t length;
|
|
|
|
out.write_to_buffer(".png", &buf, &length);
|
|
|
|
|
|
|
|
result.Set("data", Napi::Buffer<char>::Copy(env, (char *)buf, length));
|
2021-04-26 14:47:03 +00:00
|
|
|
result.Set("type", "png");
|
|
|
|
} catch (std::exception const &err) {
|
2022-06-28 03:50:53 +00:00
|
|
|
Napi::Error::New(env, err.what()).ThrowAsJavaScriptException();
|
2021-05-03 21:01:48 +00:00
|
|
|
} catch (...) {
|
2022-06-28 03:50:53 +00:00
|
|
|
Napi::Error::New(env, "Unknown error").ThrowAsJavaScriptException();
|
2021-04-26 14:47:03 +00:00
|
|
|
}
|
2022-06-28 03:50:53 +00:00
|
|
|
|
|
|
|
vips_error_clear();
|
|
|
|
vips_thread_shutdown();
|
|
|
|
return result;
|
2020-07-21 14:03:08 +00:00
|
|
|
}
|