|
| 1 | +//! Structs for including text in a figure. |
| 2 | +
|
| 3 | +use std::convert::Infallible; |
| 4 | + |
| 5 | +use pyo3::{intern, prelude::*, sync::PyOnceLock}; |
| 6 | + |
| 7 | +pub struct Text { |
| 8 | + text: Py<PyAny>, |
| 9 | +} |
| 10 | + |
| 11 | +impl FromPyObject<'_, '_> for Text { |
| 12 | + type Error = Infallible; |
| 13 | + |
| 14 | + fn extract(obj: Borrowed<'_, '_, PyAny>) -> Result<Self, Self::Error> { |
| 15 | + Ok(Self { |
| 16 | + text: obj.to_owned().unbind(), |
| 17 | + }) |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +/// Text alignment specification. |
| 22 | +#[derive(Clone, Copy, Debug)] |
| 23 | +pub enum Align { |
| 24 | + Left, |
| 25 | + Center, |
| 26 | + Right, |
| 27 | +} |
| 28 | + |
| 29 | +impl Align { |
| 30 | + #[inline] |
| 31 | + fn to_str(&self) -> &str { |
| 32 | + match self { |
| 33 | + Align::Left => "left", |
| 34 | + Align::Center => "center", |
| 35 | + Align::Right => "right", |
| 36 | + } |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +macro_rules! set { |
| 41 | + ($(#[$doc: meta])* $name: ident $(, $v: ident, $t: ty, $conv: expr)*) => { |
| 42 | + $(#[$doc])* |
| 43 | + pub fn $name(&self, $($v: $t)*) -> &Self { |
| 44 | + Python::attach(|py| { |
| 45 | + self.text.bind(py) |
| 46 | + .call_method1(stringify!($name), ($($conv,)*)) |
| 47 | + .unwrap(); |
| 48 | + }); |
| 49 | + self |
| 50 | + }}} |
| 51 | + |
| 52 | +impl Text { |
| 53 | + fn cls(py: Python<'_>) -> &Bound<'_, PyAny> { |
| 54 | + static TEXT: PyOnceLock<Py<PyAny>> = PyOnceLock::new(); |
| 55 | + TEXT.import(py, "matplotlib.text", "Text") |
| 56 | + .expect("Cannot find matplotlib.text.Text") |
| 57 | + } |
| 58 | + |
| 59 | + // FIXME: x: impl Coord |
| 60 | + pub fn new(x: f64, y: f64, text: &str) -> Self { |
| 61 | + Python::attach(|py| { |
| 62 | + Self::cls(py) |
| 63 | + .call1((x, y, text)) |
| 64 | + .expect("New Text") |
| 65 | + .unbind() |
| 66 | + .extract(py) |
| 67 | + .unwrap() |
| 68 | + }) |
| 69 | + } |
| 70 | + |
| 71 | + /// Return whether antialiased rendering is used. |
| 72 | + pub fn get_antialiased(&self) -> bool { |
| 73 | + Python::attach(|py| { |
| 74 | + self.text |
| 75 | + .call_method1(py, intern!(py, "get_antialiased"), ()) |
| 76 | + .unwrap() |
| 77 | + .extract(py) |
| 78 | + .unwrap() |
| 79 | + }) |
| 80 | + } |
| 81 | + |
| 82 | + set!( |
| 83 | + /// Set the horizontal alignment relative to the anchor point. |
| 84 | + set_horizontalalignment, align, Align, align.to_str()); |
| 85 | + |
| 86 | + set!( |
| 87 | + /// Set the rotation of the text. |
| 88 | + set_rotation, s, f64, s); |
| 89 | +} |
| 90 | + |
| 91 | +#[cfg(test)] |
| 92 | +mod test { |
| 93 | + use super::*; |
| 94 | + |
| 95 | + #[test] |
| 96 | + fn test_antialiased() { |
| 97 | + let _ = Text::new(0., 0., "Hello").get_antialiased(); |
| 98 | + } |
| 99 | +} |
0 commit comments