use cairo::{Context, Matrix, MatrixTrait};
use cairo::enums::{FontSlant, FontWeight};
#[derive(Clone, Debug)]
pub struct Text {
content: String,
font_size: f64,
angle: f64,
hor_offset: f64,
ver_offset: f64,
font_matrix: Matrix,
}
impl Text {
pub fn new(content: &str) -> Text {
Text {
content: String::from(content),
font_size: 0.03,
angle: 0.0,
hor_offset: 0.0,
ver_offset: 0.0,
font_matrix: Matrix::new(1.0, 0.0, 0.0, 1.0, 0.0, 0.0),
}
}
pub fn content(&self) -> String {
self.content.clone()
}
pub fn font_size(&self) -> f64 {
self.font_size
}
pub fn angle(&self) -> f64 {
self.angle
}
pub fn hor_offset(&self) -> f64 {
self.hor_offset
}
pub fn ver_offset(&self) -> f64 {
self.ver_offset
}
pub fn set_content(&mut self, content: &str) {
self.content = String::from(content);
}
pub fn set_font_size(&mut self, size: f64) {
self.font_size = size;
}
pub fn set_angle(&mut self, val: f64) {
self.angle = val;
}
pub fn set_offset(&mut self, hor: f64, ver: f64) {
self.hor_offset = hor;
self.ver_offset = ver;
}
pub fn scale_offset(&mut self, factor: f64) {
self.hor_offset *= factor;
self.ver_offset *= factor;
}
pub fn scale_size(&mut self, factor: f64) {
self.font_size *= factor;
self.scale_offset(factor);
}
pub fn draw(&self, cr: &Context, fig_rel_height: f64, fig_rel_width: f64) {
cr.select_font_face("Serif", FontSlant::Normal, FontWeight::Normal);
cr.set_font_size(self.font_size);
let curr_font_matrix = cr.get_font_matrix();
cr.set_font_matrix(Matrix::new(fig_rel_height * curr_font_matrix.xx,
1.0 * curr_font_matrix.yx,
1.0 * curr_font_matrix.xy,
fig_rel_width * curr_font_matrix.yy,
1.0 * curr_font_matrix.x0,
1.0 * curr_font_matrix.y0));
cr.transform(Matrix::new(1.0, 0.0, 0.0, -1.0, 0.0, 0.0));
cr.rotate(self.angle);
cr.show_text(&self.content);
cr.rotate(-self.angle);
cr.transform(Matrix::new(1.0, 0.0, 0.0, -1.0, 0.0, 0.0));
}
}