/
lib.rs
43 lines (38 loc) · 1.37 KB
/
lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
use image::codecs::png::PngEncoder;
use image::ColorType;
use magnus::{function, method, prelude::*, r_string::RString, Error};
use reing_text2image::TextImage;
use std::path::Path;
#[magnus::wrap(class = "ReingPrintImage")]
struct ReingPrintImage {
text_image: TextImage,
}
impl ReingPrintImage {
fn internal_new(text: String, brand: String, rgb_color: (u8, u8, u8)) -> Self {
Self {
text_image: TextImage::new(text, brand, rgb_color),
}
}
fn save(&self, path: String) {
self.text_image.save_image(&Path::new(&path))
}
fn encode_as_png(&self) -> RString {
let rgb_image = self.text_image.text2image();
let width = rgb_image.width();
let height = rgb_image.height();
let raw = rgb_image.into_raw();
let mut output = vec![];
PngEncoder::new(&mut output)
.encode(&raw, width, height, ColorType::Rgb8)
.unwrap();
RString::from_slice(&output)
}
}
#[magnus::init]
fn init(ruby: &magnus::Ruby) -> Result<(), Error> {
let class = ruby.define_class("ReingPrintImage", ruby.class_object())?;
class.define_singleton_method("internal_new", function!(ReingPrintImage::internal_new, 3))?;
class.define_method("save", method!(ReingPrintImage::save, 1))?;
class.define_method("encode_as_png", method!(ReingPrintImage::encode_as_png, 0))?;
Ok(())
}