pub fn blend_text_rect(&self, frame: &mut [u8], rect: &(usize, usize, usize, usize), stride: usize, font: &Font, size: f32, text: &str, color: &[u8; 4], align: TextAlignment) {

        let mut text_to_use = text.trim_end().to_string().clone();
        if text_to_use.trim_end().is_empty() { return; }

        let mut text_size = self.get_text_size(font, size, text_to_use.as_str());

        let mut add_trail = false;
        // Text is too long ??
        while text_size.0 >= rect.2 {
            text_to_use.pop();
            text_size = self.get_text_size(font, size, (text_to_use.clone() + "...").as_str());
            add_trail = true;
        }

        if add_trail {
            text_to_use = text_to_use + "...";
        }

        let fonts = &[font];

        let mut layout = Layout::new(CoordinateSystem::PositiveYDown);
        layout.reset(&LayoutSettings {
            max_width : Some(rect.2 as f32),
            max_height : Some(rect.3 as f32),
            horizontal_align : if align ==  TextAlignment::Left { HorizontalAlign::Left } else { HorizontalAlign::Center },
            vertical_align : VerticalAlign::Middle,
            ..LayoutSettings::default()
        });
        layout.append(fonts, &TextStyle::new(&text_to_use.as_str(), size, 0));

        for glyph in layout.glyphs() {
            let (metrics, alphamap) = font.rasterize(glyph.parent, glyph.key.px);
            //println!("Metrics: {:?}", glyph);

            for y in 0..metrics.height {
                for x in 0..metrics.width {
                    let i = (x+rect.0+glyph.x as usize) * 4 + (y + rect.1 + glyph.y as usize) * stride * 4;
                    let m = alphamap[x + y * metrics.width];

                    let background = &[frame[i], frame[i+1], frame[i+2], frame[i+3]];
                    frame[i..i + 4].copy_from_slice(&self.mix_color(&background, &color, m as f64 / 255.0));
                }
            }
        }
    }
/// Blends a text aligned inside a rect and blends it with the existing background
Blends a text aligned inside a rect and blends it with the existing background
Blends a text aligned inside a rect and blends it with the existing background
pub fn blend_text_rect(&self, frame: &mut [u8], rect: &(usize, usize, usize, usize), stride: usize, font: &Font, size: f32, text: &str, color: &[u8; 4], align: TextAlignment) {
        let mut text_to_use = text.trim_end().to_string().clone();
        if text_to_use.trim_end().is_empty() { return; }
        let mut text_size = self.get_text_size(font, size, text_to_use.as_str());
        let mut add_trail = false;
        // Text is too long ??
        while text_size.0 >= rect.2 {
            text_to_use.pop();
            text_size = self.get_text_size(font, size, (text_to_use.clone() + "...").as_str());
            add_trail = true;
        }
        if add_trail {
            text_to_use = text_to_use + "...";
        }
        let fonts = &[font];
        let mut layout = Layout::new(CoordinateSystem::PositiveYDown);
        layout.reset(&LayoutSettings {
            max_width : Some(rect.2 as f32),
            max_height : Some(rect.3 as f32),
            horizontal_align : if align ==  TextAlignment::Left { HorizontalAlign::Left } else { HorizontalAlign::Center },
            vertical_align : VerticalAlign::Middle,
            ..LayoutSettings::default()
        });
        layout.append(fonts, &TextStyle::new(&text_to_use.as_str(), size, 0));
        for glyph in layout.glyphs() {
            let (metrics, alphamap) = font.rasterize(glyph.parent, glyph.key.px);
            //println!("Metrics: {:?}", glyph);
            for y in 0..metrics.height {
                for x in 0..metrics.width {
                    let i = (x+rect.0+glyph.x as usize) * 4 + (y + rect.1 + glyph.y as usize) * stride * 4;
                    let m = alphamap[x + y * metrics.width];
                    let background = &[frame[i], frame[i+1], frame[i+2], frame[i+3]];
                    frame[i..i + 4].copy_from_slice(&self.mix_color(&background, &color, m as f64 / 255.0));
                }
            }
        }
    }
pub fn blend_text_rect ( & self , frame : & mut [ u8 ] , rect : & ( usize , usize , usize , usize ) , stride : usize , font : & Font , size : f32 , text : & str , color : & [ u8 ; 4 ] , align : TextAlignment ) { let mut text_to_use = text . trim_end ( ) . to_string ( ) . clone ( ) ; if text_to_use . trim_end ( ) . is_empty ( ) { return ; } let mut text_size = self . get_text_size ( font , size , text_to_use . as_str ( ) ) ; let mut add_trail = false ; // Text is too long ?? while text_size . 0 >= rect . 2 { text_to_use . pop ( ) ; text_size = self . get_text_size ( font , size , ( text_to_use . clone ( ) + " " ) . as_str ( ) ) ; add_trail = true ; } if add_trail { text_to_use = text_to_use + " " ; } let fonts = & [ font ] ; let mut layout = Layout :: new ( CoordinateSystem :: PositiveYDown ) ; layout . reset ( & LayoutSettings { max_width : Some ( rect . 2 as f32 ) , max_height : Some ( rect . 3 as f32 ) , horizontal_align : if align == TextAlignment :: Left { HorizontalAlign :: Left } else { HorizontalAlign :: Center } , vertical_align : VerticalAlign :: Middle , .. LayoutSettings :: default ( ) } ) ; layout . append ( fonts , & TextStyle :: new ( & text_to_use . as_str ( ) , size , 0 ) ) ; for glyph in layout . glyphs ( ) { let ( metrics , alphamap ) = font . rasterize ( glyph . parent , glyph . key . px ) ; //println!("Metrics: {:?}", glyph); for y in 0 .. metrics . height { for x in 0 .. metrics . width { let i = ( x + rect . 0 + glyph . x as usize ) * 4 + ( y + rect . 1 + glyph . y as usize ) * stride * 4 ; let m = alphamap [ x + y * metrics . width ] ; let background = & [ frame [ i ] , frame [ i + 1 ] , frame [ i + 2 ] , frame [ i + 3 ] ] ; frame [ i .. i + 4 ] . copy_from_slice ( & self . mix_color ( & background , & color , m as f64 / 255.0 ) ) ; } } } }
Blends a text aligned inside a rect and blends it with the existing background
Blends a text aligned inside a rect and blends it with the existing background
&self
frame
&mut [u8]
rect
&(usize, usize, usize, usize)
stride
usize
font
&Font
size
f32
text
&str
color
&[u8; 4]
align
TextAlignment
&self
frame
&mut [u8]
rect
&(usize, usize, usize, usize)
stride
usize
font
&Font
size
f32
text
&str
color
&[u8; 4]
align
TextAlignment