@@ -2,16 +2,7 @@ use std::io::Read;
22
33use 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
156pub 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