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

extern crate image;
extern crate imageproc;
extern crate rusttype;

use std::path::Path;
use std::env;
use imageproc::drawing::draw_text_mut;
use image::{Rgb, RgbImage};
use rusttype::{FontCollection, Scale};

// External ffi bindings will be written in ext.rs
pub mod ext;

extern crate cgui;
use cgui::CWinRep;
use cgui::RenderBack;

pub trait TextRenderer {
  fn write_text(&mut self, text: String, x: u32, y: u32, fontsize: u16, color: [u8; 3]);
  fn write_text_simple(&mut self, text: String, x: u32, y: u32);
}

impl TextRenderer for CWinRep {
  fn write_text(&mut self, text: String, x: u32, y: u32, fontsize: u16, color: [u8; 3]) {
    let font = Vec::from(include_bytes!("../SourceCodePro-Regular.ttf") as &[u8]);
    let font = FontCollection::from_bytes(font).unwrap().into_font().unwrap();
    
    let height = fontsize as f32;
    let scale = Scale { x: height, y: height };
    draw_text_mut(&mut self.pixels, Rgb(color), x, y, scale, &font, &text);
    
  }
  fn write_text_simple(&mut self, text: String, x: u32, y: u32) {
    self.write_text(text, x, y, 16, [0, 0, 0]);
  }
}