use super::GraphMaker;
use num_traits::Num;
use std::fmt::Write;
pub struct Text {
color: String, align_horizontal: String, align_vertical: String, fontsize: f64, rotation: Option<f64>,
bbox: bool, bbox_facecolor: String, bbox_edgecolor: String, bbox_alpha: f64, bbox_style: String,
extra: String, buffer: String, }
impl Text {
pub fn new() -> Self {
Text {
color: String::new(),
align_horizontal: String::new(),
align_vertical: String::new(),
fontsize: 0.0,
rotation: None,
bbox: false,
bbox_facecolor: String::new(),
bbox_edgecolor: String::new(),
bbox_alpha: 1.0,
bbox_style: String::new(),
extra: String::new(),
buffer: String::new(),
}
}
pub fn draw<T>(&mut self, x: T, y: T, message: &str)
where
T: std::fmt::Display + Num,
{
let opt = self.options();
write!(&mut self.buffer, "t=plt.text({},{},r'{}'{})\n", x, y, message, &opt).unwrap();
if self.bbox {
let opt_bbox = self.options_bbox();
write!(&mut self.buffer, "t.set_bbox(dict({}))\n", opt_bbox).unwrap();
}
}
pub fn draw_3d<T>(&mut self, x: T, y: T, z: T, message: &str)
where
T: std::fmt::Display + Num,
{
let opt = self.options();
write!(
&mut self.buffer,
"t=ax3d().text({},{},{},r'{}'{})\n",
x, y, z, message, &opt
)
.unwrap();
if self.bbox {
let opt_bbox = self.options_bbox();
write!(&mut self.buffer, "t.set_bbox(dict({}))\n", opt_bbox).unwrap();
}
}
pub fn set_color(&mut self, color: &str) -> &mut Self {
self.color = String::from(color);
self
}
pub fn set_align_horizontal(&mut self, option: &str) -> &mut Self {
self.align_horizontal = String::from(option);
self
}
pub fn set_align_vertical(&mut self, option: &str) -> &mut Self {
self.align_vertical = String::from(option);
self
}
pub fn set_fontsize(&mut self, fontsize: f64) -> &mut Self {
self.fontsize = fontsize;
self
}
pub fn set_rotation(&mut self, rotation: f64) -> &mut Self {
self.rotation = Some(rotation);
self
}
pub fn set_bbox(&mut self, flag: bool) -> &mut Self {
self.bbox = flag;
self
}
pub fn set_bbox_facecolor(&mut self, color: &str) -> &mut Self {
self.bbox_facecolor = String::from(color);
self
}
pub fn set_bbox_edgecolor(&mut self, color: &str) -> &mut Self {
self.bbox_edgecolor = String::from(color);
self
}
pub fn set_bbox_alpha(&mut self, value: f64) -> &mut Self {
self.bbox_alpha = value;
self
}
pub fn set_bbox_style(&mut self, style: &str) -> &mut Self {
self.bbox_style = String::from(style);
self
}
pub fn set_extra(&mut self, extra: &str) -> &mut Self {
self.extra = extra.to_string();
self
}
fn options(&self) -> String {
let mut opt = String::new();
if self.color != "" {
write!(&mut opt, ",color='{}'", self.color).unwrap();
}
if self.align_horizontal != "" {
write!(&mut opt, ",ha='{}'", self.align_horizontal).unwrap();
}
if self.align_vertical != "" {
write!(&mut opt, ",va='{}'", self.align_vertical).unwrap();
}
if self.fontsize > 0.0 {
write!(&mut opt, ",fontsize={}", self.fontsize).unwrap();
}
if let Some(rotation) = self.rotation {
write!(&mut opt, ",rotation={}", rotation).unwrap();
}
if self.extra != "" {
write!(&mut opt, ",{}", self.extra).unwrap();
}
opt
}
fn options_bbox(&self) -> String {
let mut opt = String::new();
if self.bbox_facecolor != "" {
write!(&mut opt, "facecolor='{}',", self.bbox_facecolor).unwrap();
}
if self.bbox_edgecolor != "" {
write!(&mut opt, "edgecolor='{}',", self.bbox_edgecolor).unwrap();
}
write!(&mut opt, "alpha={},", self.bbox_alpha).unwrap();
if self.bbox_style != "" {
write!(&mut opt, "boxstyle='{}',", self.bbox_style).unwrap();
}
opt
}
}
impl GraphMaker for Text {
fn get_buffer<'a>(&'a self) -> &'a String {
&self.buffer
}
fn clear_buffer(&mut self) {
self.buffer.clear();
}
}
#[cfg(test)]
mod tests {
use super::Text;
use crate::GraphMaker;
#[test]
fn new_works() {
let text = Text::new();
assert_eq!(text.color.len(), 0);
assert_eq!(text.align_horizontal.len(), 0);
assert_eq!(text.align_vertical.len(), 0);
assert_eq!(text.fontsize, 0.0);
assert_eq!(text.rotation, None);
assert_eq!(text.buffer.len(), 0);
}
#[test]
fn options_works() {
let mut text = Text::new();
text.set_color("red")
.set_align_horizontal("center")
.set_align_vertical("center")
.set_fontsize(8.0)
.set_rotation(45.0);
let opt = text.options();
assert_eq!(
opt,
",color='red'\
,ha='center'\
,va='center'\
,fontsize=8\
,rotation=45"
);
}
#[test]
fn options_box_works() {
let mut text = Text::new();
text.set_bbox(true)
.set_bbox_facecolor("pink")
.set_bbox_edgecolor("black")
.set_bbox_alpha(0.3)
.set_bbox_style("round,pad=0.4");
assert_eq!(text.bbox, true);
let opt = text.options_bbox();
assert_eq!(
opt,
"facecolor='pink',\
edgecolor='black',\
alpha=0.3,\
boxstyle='round,pad=0.4',"
);
}
#[test]
fn draw_works() {
let mut text = Text::new();
text.draw(1.2, 3.4, &"message".to_string());
let b: &str = "t=plt.text(1.2,3.4,r'message')\n";
assert_eq!(text.buffer, b);
text.clear_buffer();
assert_eq!(text.buffer, "");
}
#[test]
fn draw_3d_works() {
let mut text = Text::new();
text.draw_3d(1.2, 3.4, 5.6, &"message".to_string());
let b: &str = "t=ax3d().text(1.2,3.4,5.6,r'message')\n";
assert_eq!(text.buffer, b);
}
}