Skip to content

Commit c5f12ce

Browse files
committed
Remove Text type, add Image::draw_text
1 parent ae6e6a5 commit c5f12ce

File tree

3 files changed

+34
-48
lines changed

3 files changed

+34
-48
lines changed

src/lib.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,6 @@ pub use pixel::Pixel;
8686
pub use r#type::Type;
8787
pub use transform::Transform;
8888

89-
#[cfg(feature = "text")]
90-
pub use text::Text;
91-
9289
#[cfg(test)]
9390
mod tests;
9491

src/tests.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,9 @@ fn test_text() {
257257
let font = include_bytes!("../images/OpenSans-Regular.ttf");
258258
let font = text::font(font).unwrap();
259259
let px = Pixel::from(vec![1.0, 0.0, 0.0]);
260-
let text = Text::new(font, "Testing123", 64.0).with_color(px);
261-
text.draw(&mut image, (100, 100));
260+
image.draw_text("Testing123", &font, 64.0, (100, 100), &px);
261+
println!("{:?}", text::width("Testing123", &font, 64.0));
262+
let width = image.width();
263+
image.draw_text("Testing123", &font, 64.0, (width - 100, 700), &px);
262264
image.save("images/test-text.png").unwrap();
263265
}

src/text.rs

Lines changed: 30 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,7 @@ use std::io::Read;
22

33
use crate::*;
44

5-
/// Defines size. location and contents for drawing text
6-
pub struct Text<'a, C: Color> {
7-
text: String,
8-
color: Pixel<C>,
9-
width: Option<usize>,
10-
height: Option<usize>,
11-
size: f32,
12-
font: Font<'a>,
13-
}
14-
5+
// Re-export `Font` type
156
pub use rusttype::Font;
167

178
/// Load font from disk
@@ -33,48 +24,44 @@ pub fn font(data: &[u8]) -> Result<Font<'_>, Error> {
3324
}
3425
}
3526

36-
impl<'a, C: Color> Text<'a, C> {
37-
/// Create new `Text` from the given font, text and size (in pixels)
38-
pub fn new(font: Font<'a>, text: impl Into<String>, size: f32) -> Self {
39-
Text {
40-
text: text.into(),
41-
color: Pixel::new(),
42-
width: None,
43-
height: None,
44-
size,
45-
font,
46-
}
27+
/// Get size of text to be drawn
28+
pub fn width<'a>(text: impl AsRef<str>, font: &Font<'a>, size: f32) -> usize {
29+
if text.as_ref().is_empty() {
30+
return 0;
4731
}
4832

49-
/// Set bounding width
50-
pub fn with_max_width(mut self, w: usize) -> Self {
51-
self.width = Some(w);
52-
self
53-
}
33+
let scale = rusttype::Scale::uniform(size);
34+
let layout = font.layout(text.as_ref(), scale, rusttype::point(0., 0.));
35+
let mut w = 0;
5436

55-
/// Set bounding height
56-
pub fn with_max_height(mut self, h: usize) -> Self {
57-
self.height = Some(h);
58-
self
37+
for glyph in layout {
38+
if let Some(bounding_box) = glyph.pixel_bounding_box() {
39+
w = bounding_box.max.x as usize;
40+
}
5941
}
6042

61-
/// Set text color
62-
pub fn with_color(mut self, color: Pixel<Rgb>) -> Self {
63-
self.color = color.convert();
64-
self
65-
}
43+
w
44+
}
6645

46+
impl<T: Type, C: Color> Image<T, C> {
6747
/// Draw text on image
68-
pub fn draw<T: Type>(&self, image: &mut Image<T, C>, pos: impl Into<Point>) {
48+
pub fn draw_text<'a>(
49+
&mut self,
50+
text: impl AsRef<str>,
51+
font: &Font<'a>,
52+
size: f32,
53+
pos: impl Into<Point>,
54+
color: &Pixel<C>,
55+
) {
6956
let pos = pos.into();
70-
let scale = rusttype::Scale::uniform(self.size);
71-
let layout = self.font.layout(
72-
&self.text,
57+
let scale = rusttype::Scale::uniform(size);
58+
let layout = font.layout(
59+
text.as_ref(),
7360
scale,
7461
rusttype::point(pos.x as f32, pos.y as f32),
7562
);
7663

77-
let mut data = vec![0.convert(); C::CHANNELS];
64+
let mut data = vec![T::from_f64(0.0); C::CHANNELS];
7865
let mut tmp = Pixel::new();
7966
for glyph in layout {
8067
if let Some(bounding_box) = glyph.pixel_bounding_box() {
@@ -83,10 +70,10 @@ impl<'a, C: Color> Text<'a, C> {
8370
(x as isize + bounding_box.min.x as isize) as usize,
8471
(y as isize + bounding_box.min.y as isize) as usize,
8572
);
86-
if image.at(pt, &mut data) {
73+
if self.at(pt, &mut data) {
8774
tmp.copy_from_slice(&data);
88-
let color = &tmp * (1.0 - v) + &self.color * v;
89-
image.set_pixel(pt, &color);
75+
let color = &tmp * (1.0 - v) + color * v;
76+
self.set_pixel(pt, &color);
9077
}
9178
});
9279
}

0 commit comments

Comments
 (0)