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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
use crate::point::Point;
use crate::rect::Rect;
use crate::traits::*;
use num_traits::NumCast;
use std::fmt;
use std::hash::Hash;
use std::ops::Deref;
pub trait TextType: Eq + Hash + Clone + fmt::Debug {}
impl<T: Eq + Hash + Clone + fmt::Debug> TextType for T {}
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Text<T, S = String> {
location: Point<T>,
text: S,
}
impl<T, S> fmt::Display for Text<T, S>
where
T: fmt::Display,
S: TextType + fmt::Display,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Text({}, {})", self.text, self.location)
}
}
impl<T, S> Deref for Text<T, S>
where
S: Deref<Target = String>,
{
type Target = String;
fn deref(&self) -> &Self::Target {
self.text.deref()
}
}
impl<T, S: TextType> Text<T, S> {
pub fn new(text: S, location: Point<T>) -> Self {
Text { location, text }
}
pub fn text(&self) -> &S {
&self.text
}
}
impl<T: Copy, S: TextType> Text<T, S> {
#[inline]
pub fn location(&self) -> Point<T> {
self.location
}
#[inline]
pub fn x(&self) -> T {
self.location.x
}
#[inline]
pub fn y(&self) -> T {
self.location.y
}
}
impl<T: Copy + PartialOrd, S> TryBoundingBox<T> for Text<T, S> {
fn try_bounding_box(&self) -> Option<Rect<T>> {
Some(Rect::new(self.location, self.location))
}
}
impl<T, Dst, S> TryCastCoord<T, Dst> for Text<T, S>
where
T: Copy + NumCast,
Dst: Copy + NumCast,
S: Clone,
{
type Output = Text<Dst, S>;
fn try_cast(&self) -> Option<Self::Output> {
self.location.try_cast().map(|loc| Text {
location: loc,
text: self.text.clone(),
})
}
}
impl<T, S> MapPointwise<T> for Text<T, S>
where
T: Copy,
S: Clone,
{
#[inline]
fn transform<F>(&self, transformation: F) -> Self
where
F: Fn(Point<T>) -> Point<T>,
{
Text {
location: transformation(self.location),
text: self.text.clone(),
}
}
}