From 13b7d73ce91c0f62798a1aece02157c06da0a92f Mon Sep 17 00:00:00 2001 From: bunnie Date: Fri, 21 Jan 2022 02:30:58 +0800 Subject: [PATCH] word wrapping seems to work and is tested now. --- services/graphics-server/Cargo.toml | 2 +- services/graphics-server/src/api/text.rs | 5 + services/graphics-server/src/blitstr2/blit.rs | 275 ++++-------------- .../graphics-server/src/blitstr2/cliprect.rs | 2 +- .../graphics-server/src/blitstr2/cursor.rs | 2 +- services/graphics-server/src/main.rs | 21 +- services/graphics-server/src/testing.rs | 60 +++- services/graphics-server/src/wordwrap.rs | 52 ++-- 8 files changed, 161 insertions(+), 258 deletions(-) diff --git a/services/graphics-server/Cargo.toml b/services/graphics-server/Cargo.toml index 1fb313e06..1e6ca0fe4 100644 --- a/services/graphics-server/Cargo.toml +++ b/services/graphics-server/Cargo.toml @@ -32,4 +32,4 @@ minifb = {version = "0.19.2", git = "https://github.com/xobs/rust_minifb.git", r debugprint = [] braille = [] testing = [] -default = ["testing"] +default = [] diff --git a/services/graphics-server/src/api/text.rs b/services/graphics-server/src/api/text.rs index e9df997dc..ffef39ac7 100644 --- a/services/graphics-server/src/api/text.rs +++ b/services/graphics-server/src/api/text.rs @@ -15,6 +15,8 @@ pub enum TextBounds { GrowableFromTl(Point, u16), // fixed width, grows up from bottom left GrowableFromBl(Point, u16), + // fixed width, grows down from top right + GrowableFromTr(Point, u16), } impl TextBounds { pub fn translate(&self, by: Point) -> TextBounds { @@ -32,6 +34,9 @@ impl TextBounds { TextBounds::GrowableFromBl(origin, width) => { TextBounds::GrowableFromBl(origin.add(by), width) } + TextBounds::GrowableFromTr(origin, width) => { + TextBounds::GrowableFromTr(origin.add(by), width) + } } } } diff --git a/services/graphics-server/src/blitstr2/blit.rs b/services/graphics-server/src/blitstr2/blit.rs index e3c83f35e..93f0b7aca 100644 --- a/services/graphics-server/src/blitstr2/blit.rs +++ b/services/graphics-server/src/blitstr2/blit.rs @@ -2,10 +2,10 @@ // SPDX-License-Identifier: Apache-2.0 OR MIT // use super::cliprect::ClipRect; -use super::cursor::Cursor; use super::fonts; -use super::pt::Pt; +#[allow(unused_imports)] use super::{LINES, WORDS_PER_LINE, WIDTH, FrBuf}; +use crate::api::Point; /// Null glyph to use when everything else fails pub const NULL_GLYPH: [u32; 8] = [0, 0x5500AA, 0x5500AA, 0x5500AA, 0x5500AA, 0x5500AA, 0, 0]; @@ -22,56 +22,6 @@ pub const NULL_GLYPH_SPRITE: fonts::GlyphSprite = fonts::GlyphSprite { /// Unicode replacement character pub const REPLACEMENT: char = '\u{FFFD}'; -/// Clear a screen region bounded by (clip.min.x,clip.min.y)..(clip.min.x,clip.max.y) -pub fn clear_region(fb: &mut FrBuf, clip: ClipRect) { - if clip.max.y > LINES - || clip.min.y >= clip.max.y - || clip.max.x > WIDTH - || clip.min.x >= clip.max.x - { - return; - } - // Calculate word alignment for destination buffer - let dest_low_word = clip.min.x >> 5; - let dest_high_word = clip.max.x >> 5; - let px_in_dest_low_word = 32 - (clip.min.x & 0x1f); - let px_in_dest_high_word = clip.max.x & 0x1f; - // Blit it - for y in clip.min.y..clip.max.y { - let base = y * WORDS_PER_LINE; - fb[base + dest_low_word] |= 0xffffffff << (32 - px_in_dest_low_word); - for w in dest_low_word + 1..dest_high_word { - fb[base + w] = 0xffffffff; - } - if dest_low_word < dest_high_word { - fb[base + dest_high_word] |= 0xffffffff >> (32 - px_in_dest_high_word); - } - } -} - -/// Find glyph for char using latin regular, emoji, ja, zh, and kr font data -pub fn find_glyph(ch: char) -> fonts::GlyphSprite { - match fonts::regular_glyph(ch) { - Ok(g) => g, - _ => match fonts::emoji_glyph(ch) { - Ok(g) => g, - _ => match fonts::ja_glyph(ch) { - Ok(g) => g, - _ => match fonts::zh_glyph(ch) { - Ok(g) => g, - _ => match fonts::kr_glyph(ch) { - Ok(g) => g, - _ => match fonts::regular_glyph(REPLACEMENT) { - Ok(g) => g, - _ => NULL_GLYPH_SPRITE, - }, - }, - }, - }, - }, - } -} - /// Find glyph for char using only the latin small font data pub fn find_glyph_latin_small(ch: char) -> fonts::GlyphSprite { match fonts::small_glyph(ch) { @@ -83,154 +33,22 @@ pub fn find_glyph_latin_small(ch: char) -> fonts::GlyphSprite { } } -/// Find glyph for char using only the latin bold font data -pub fn find_glyph_latin_bold(ch: char) -> fonts::GlyphSprite { - match fonts::bold_glyph(ch) { - Ok(g) => g, - _ => match fonts::bold_glyph(REPLACEMENT) { - Ok(g) => g, - _ => NULL_GLYPH_SPRITE, - }, - } -} - -/// Find glyph for char using only the latin mono font data -pub fn find_glyph_latin_mono(ch: char) -> fonts::GlyphSprite { - match fonts::mono_glyph(ch) { - Ok(g) => g, - _ => match fonts::mono_glyph(REPLACEMENT) { - Ok(g) => g, - _ => NULL_GLYPH_SPRITE, - }, - } -} - -/// XOR blit a string using multi-lingual glyphs with specified clip rect, starting at cursor -pub fn paint_str(fb: &mut FrBuf, clip: ClipRect, c: &mut Cursor, s: &str) { - const KERN: usize = 2; - for ch in s.chars() { - if ch == '\n' { - newline(clip, c); - } else { - // Look up the glyph for this char - let glyph = find_glyph(ch); - let wide = glyph.wide as usize; - let high = glyph.high as usize; - // Adjust for word wrapping - if c.pt.x + wide + KERN >= clip.max.x { - newline(clip, c); - } - // Blit the glyph and advance the cursor - xor_glyph(fb, &c.pt, glyph, false); - c.pt.x += wide + KERN; - if high > c.line_height { - c.line_height = high; - } - } - } -} - -/// XOR blit a string using latin small glyphs with specified clip rect, starting at cursor -pub fn paint_str_latin_small(fb: &mut FrBuf, clip: ClipRect, c: &mut Cursor, s: &str) { - const KERN: usize = 1; - for ch in s.chars() { - if ch == '\n' { - newline(clip, c); - } else { - // Look up the glyph for this char - let glyph = find_glyph_latin_small(ch); - let wide = glyph.wide as usize; - let high = glyph.high as usize; - // Adjust for word wrapping - if c.pt.x + wide + KERN >= clip.max.x { - newline(clip, c); - } - // Blit the glyph and advance the cursor - xor_glyph(fb, &c.pt, glyph, false); - c.pt.x += wide + KERN; - if high > c.line_height { - c.line_height = high; - } - } - } -} - -/// XOR blit a string using latin bold glyphs with specified clip rect, starting at cursor -pub fn paint_str_latin_bold(fb: &mut FrBuf, clip: ClipRect, c: &mut Cursor, s: &str) { - const KERN: usize = 2; - for ch in s.chars() { - if ch == '\n' { - newline(clip, c); - } else { - // Look up the glyph for this char - let glyph = find_glyph_latin_bold(ch); - let wide = glyph.wide as usize; - let high = glyph.high as usize; - // Adjust for word wrapping - if c.pt.x + wide + KERN >= clip.max.x { - newline(clip, c); - } - // Blit the glyph and advance the cursor - xor_glyph(fb, &c.pt, glyph, false); - c.pt.x += wide + KERN; - if high > c.line_height { - c.line_height = high; - } - } - } -} - -/// XOR blit a string using latin mono glyphs with specified clip rect, starting at cursor -pub fn paint_str_latin_mono(fb: &mut FrBuf, clip: ClipRect, c: &mut Cursor, s: &str) { - const KERN: usize = 1; - for ch in s.chars() { - if ch == '\n' { - newline(clip, c); - } else { - // Look up the glyph for this char - let glyph = find_glyph_latin_mono(ch); - let wide = glyph.wide as usize; - let high = glyph.high as usize; - // Adjust for word wrapping - if c.pt.x + wide + KERN >= clip.max.x { - newline(clip, c); - } - // Blit the glyph and advance the cursor - xor_glyph(fb, &c.pt, glyph, false); - c.pt.x += wide + KERN; - if high > c.line_height { - c.line_height = high; - } - } - } -} - -/// Advance the cursor to the start of a new line within the clip rect -pub fn newline(clip: ClipRect, c: &mut Cursor) { - c.pt.x = clip.min.x; - if c.line_height < fonts::small::MAX_HEIGHT as usize { - c.line_height = fonts::small::MAX_HEIGHT as usize; - } - c.pt.y += c.line_height + 2; - c.line_height = 0; -} - /// Blit a glyph with XOR at point; caller is responsible for word wrap. /// /// Examples of word alignment for destination frame buffer: /// 1. Fits in word: xr:1..7 => (data[0].bit_30)->(data[0].bit_26), mask:0x7c00_0000 /// 2. Spans words: xr:30..36 => (data[0].bit_01)->(data[1].bit_29), mask:[0x0000_0003,0xe000_000] /// -pub fn xor_glyph(fb: &mut FrBuf, p: &Pt, gs: fonts::GlyphSprite, xor: bool) { - const SPRITE_PX: usize = 16; - const SPRITE_WORDS: usize = 8; - if gs.glyph.len() < SPRITE_WORDS { +pub fn xor_glyph(fb: &mut FrBuf, p: &Point, gs: fonts::GlyphSprite, xor: bool, cr: ClipRect) { + const SPRITE_PX: i16 = 16; + const SPRITE_WORDS: i16 = 8; + if gs.glyph.len() < SPRITE_WORDS as usize { // Fail silently if the glyph slice was too small // TODO: Maybe return an error? Not sure which way is better. return; } - let high = gs.high as usize; - let wide = gs.wide as usize; + let high = gs.high as i16; + let wide = gs.wide as i16; if high > SPRITE_PX || wide > SPRITE_PX { // Fail silently if glyph height or width is out of spec // TODO: Maybe return an error? @@ -238,44 +56,69 @@ pub fn xor_glyph(fb: &mut FrBuf, p: &Pt, gs: fonts::GlyphSprite, xor: bool) { } // Calculate word alignment for destination buffer let x0 = p.x; + if x0 >= cr.max.x as i16 { log::trace!("out the right"); return } // out the right hand side let x1 = p.x + wide - 1; + if x1 < cr.min.x as i16 { log::trace!("out the left"); return } // out the left hand side let dest_low_word = x0 >> 5; let dest_high_word = x1 >> 5; let px_in_dest_low_word = 32 - (x0 & 0x1f); // Blit it (use glyph height to avoid blitting empty rows) - let mut row_base = p.y * WORDS_PER_LINE; - const ROW_LIMIT: usize = LINES * WORDS_PER_LINE; + let mut row_base = p.y * WORDS_PER_LINE as i16; + let row_upper_limit = cr.max.y as i16 * WORDS_PER_LINE as i16; + let row_lower_limit = cr.min.y as i16 * WORDS_PER_LINE as i16; let glyph = gs.glyph; - for y in 0..high { - if row_base >= ROW_LIMIT { + for y in 0..high as usize { + if row_base >= row_upper_limit { + log::trace!("off the bottom"); // Clip anything that would run off the end of the frame buffer break; } - // Unpack pixels for this glyph row. - // CAUTION: some math magic happening here... - // when y==0, this does (glyph[0] >> 0) & mask, - // when y==1, this does (glyph[0] >> 16) & mask, - // when y==2, this does (glyph[1] >> 0) & mask, - // ... - let mask = 0x0000ffff as u32; - let shift = (y & 1) << 4; - let pattern = (glyph[y >> 1] >> shift) & mask; - // XOR glyph pixels onto destination buffer - if xor { - fb[row_base + dest_low_word] ^= pattern << (32 - px_in_dest_low_word); - } else { - fb[(row_base + dest_low_word) as usize] &= 0xffff_ffff ^ (pattern << (32 - px_in_dest_low_word)); - } - if wide > px_in_dest_low_word { - if xor { - fb[row_base + dest_high_word] ^= pattern >> px_in_dest_low_word; - } else { - fb[(row_base + dest_high_word) as usize] &= 0xffff_ffff ^ (pattern >> px_in_dest_low_word); + if row_base >= row_lower_limit { + // Unpack pixels for this glyph row. + // CAUTION: some math magic happening here... + // when y==0, this does (glyph[0] >> 0) & mask, + // when y==1, this does (glyph[0] >> 16) & mask, + // when y==2, this does (glyph[1] >> 0) & mask, + // ... + let mask = 0x0000ffff as u32; + let shift = (y as u32 & 1) << 4; + let pattern = (glyph[y >> 1] >> shift) & mask; + + // compute partial masks to prevent glyphs from "spilling over" the clip rectangle + let mut partial_mask_lo = 0xffff_ffff; + let mut partial_mask_hi = 0xffff_ffff; + if x0 < cr.min.x as i16 || x1 >= cr.max.x as i16 { + let x0a = if x0 < cr.min.x as i16 { cr.min.x as i16 } else { x0 }; + let x1a = if x1 >= cr.max.x as i16 { cr.max.x as i16 } else { x1 }; + let mut ones = (1u64 << ((x1a - x0a) as u64 + 1)) - 1; + ones <<= x0a as u64 & 0x1f; + partial_mask_lo = ones as u32; + partial_mask_hi = (ones >> 32) as u32; } + // XOR glyph pixels onto destination buffer. Note that despite the masks above, we will not render + // partial glyphs that cross the absolute bounds of the left and right edge of the screen. + if x0 >= 0 && x1 < WIDTH as i16 { + if xor { + fb[(row_base + dest_low_word) as usize] ^= (pattern << (32 - px_in_dest_low_word)) & partial_mask_lo; + } else { + fb[(row_base + dest_low_word) as usize] &= 0xffff_ffff ^ ((pattern << (32 - px_in_dest_low_word)) & partial_mask_lo); + } + if wide > px_in_dest_low_word { + if xor { + fb[(row_base + dest_high_word) as usize] ^= (pattern >> px_in_dest_low_word) & partial_mask_hi; + } else { + fb[(row_base + dest_high_word) as usize] &= 0xffff_ffff ^ ((pattern >> px_in_dest_low_word) & partial_mask_hi); + } + } + } else { + log::trace!("absolute x/y fail"); + } + fb[(row_base as usize + WORDS_PER_LINE - 1) as usize] |= 0x1_0000; // set the dirty bit on the line + } else { + log::trace!("off the top"); } - fb[(row_base + WORDS_PER_LINE - 1) as usize] |= 0x1_0000; // set the dirty bit on the line // Advance destination offset using + instead of * to maybe save some CPU cycles - row_base += WORDS_PER_LINE; + row_base += WORDS_PER_LINE as i16; } } diff --git a/services/graphics-server/src/blitstr2/cliprect.rs b/services/graphics-server/src/blitstr2/cliprect.rs index aebec614c..487de18be 100644 --- a/services/graphics-server/src/blitstr2/cliprect.rs +++ b/services/graphics-server/src/blitstr2/cliprect.rs @@ -14,7 +14,7 @@ pub struct ClipRect { pub min: Pt, pub max: Pt, } - +#[allow(dead_code)] impl ClipRect { /// Initialize a rectangle using automatic min/max fixup for corner points pub fn new(min_x: usize, min_y: usize, max_x: usize, max_y: usize) -> ClipRect { diff --git a/services/graphics-server/src/blitstr2/cursor.rs b/services/graphics-server/src/blitstr2/cursor.rs index b5f0ef996..f86dc1dd3 100644 --- a/services/graphics-server/src/blitstr2/cursor.rs +++ b/services/graphics-server/src/blitstr2/cursor.rs @@ -14,7 +14,7 @@ pub struct Cursor { pub pt: Pt, pub line_height: usize, } - +#[allow(dead_code)] impl Cursor { // Make a new Cursor. When in doubt, set line_height = 0. pub fn new(x: usize, y: usize, line_height: usize) -> Cursor { diff --git a/services/graphics-server/src/main.rs b/services/graphics-server/src/main.rs index 1834ab058..9bed34fce 100644 --- a/services/graphics-server/src/main.rs +++ b/services/graphics-server/src/main.rs @@ -102,7 +102,6 @@ fn map_fonts() -> MemoryRange { #[xous::xous_main] fn xmain() -> ! { log_server::init_wait().unwrap(); - let debugtv = false; log::set_max_level(log::LevelFilter::Info); log::info!("my PID is {}", xous::process::id()); @@ -219,7 +218,9 @@ fn xmain() -> ! { TextBounds::GrowableFromBl(bl, width) => blitstr2::Pt::new(width as usize - tv.margin.x as usize * 2, bl.y as usize - tv.margin.y as usize * 2), TextBounds::GrowableFromTl(tl, width) => - blitstr2::Pt::new(width as usize - tv.margin.x as usize * 2, (clip_rect.br().y - clip_rect.tl().y) as usize - tv.margin.y as usize * 2), + blitstr2::Pt::new(width as usize - tv.margin.x as usize * 2, (clip_rect.br().y - tl.y) as usize - tv.margin.y as usize * 2), + TextBounds::GrowableFromTr(tr, width) => + blitstr2::Pt::new(width as usize - tv.margin.x as usize * 2, (clip_rect.br().y - tr.y) as usize - tv.margin.y as usize * 2), }; let base_style = match tv.style { // a connector for now, we'll eventually depricate the old API GlyphStyle::Small => blitstr2::GlyphStyle::Small, @@ -249,7 +250,9 @@ fn xmain() -> ! { TextBounds::GrowableFromBl(bl, _width) => Point::new(bl.x + tv.margin.x, bl.y - (composition.bb_height() as i16 + tv.margin.y)), TextBounds::GrowableFromTl(tl, _width) => - tl.add(tv.margin) + tl.add(tv.margin), + TextBounds::GrowableFromTr(tr, _width) => + Point::new(tr.x - (composition.bb_width() as i16 + tv.margin.x), tr.y + tv.margin.y), } .add(screen_offset); @@ -270,9 +273,9 @@ fn xmain() -> ! { } }; - log::info!("clip_rect: {:?}", clip_rect); - log::info!("composition_top_left: {:?}", composition_top_left); - log::info!("clear_rect: {:?}", clear_rect); + log::trace!("clip_rect: {:?}", clip_rect); + log::trace!("composition_top_left: {:?}", composition_top_left); + log::trace!("clear_rect: {:?}", clear_rect); // draw the bubble/border and/or clear the background area let bordercolor = if tv.draw_border { Some(PixelColor::Dark) @@ -304,7 +307,7 @@ fn xmain() -> ! { op::rounded_rectangle( display.native_buffer(), RoundedRectangle::new(clear_rect, tv.rounded_border.unwrap() as _), - Some(clear_rect), + tv.clip_rect, ); } else { op::rectangle(display.native_buffer(), clear_rect, tv.clip_rect); @@ -316,7 +319,7 @@ fn xmain() -> ! { } if !tv.dry_run() { - composition.render(display.native_buffer(), composition_top_left, tv.invert); + composition.render(display.native_buffer(), composition_top_left, tv.invert, tv.clip_rect.unwrap()); } // type mismatch for now, replace this with a simple equals once we sort that out tv.cursor.pt.x = composition.final_cursor().pt.x as i32; @@ -326,7 +329,7 @@ fn xmain() -> ! { tv.bounds_computed = Some( clear_rect ); - log::info!("cursor ret {:?}, bounds ret {:?}", tv.cursor, tv.bounds_computed); + log::trace!("cursor ret {:?}, bounds ret {:?}", tv.cursor, tv.bounds_computed); // pack our data back into the buffer to return buffer.replace(tv).unwrap(); } diff --git a/services/graphics-server/src/testing.rs b/services/graphics-server/src/testing.rs index 63f099b4a..0ac16920e 100644 --- a/services/graphics-server/src/testing.rs +++ b/services/graphics-server/src/testing.rs @@ -10,7 +10,9 @@ enum TestType { LowerRight = 1, LowerLeft = 2, TopLeft = 3, - End = 4, + TopRight = 4, + Overflow = 5, + End = 6, } pub fn tests() { let _ = std::thread::spawn({ @@ -19,7 +21,7 @@ pub fn tests() { let gfx = graphics_server::Gfx::new(&xns).unwrap(); let ticktimer = ticktimer_server::Ticktimer::new().expect("Couldn't connect to Ticktimer"); - for index in 0..TestType::End.to_usize().unwrap() { + for index in TestType::BoundingBox.to_usize().unwrap()..TestType::End.to_usize().unwrap() { // show a black screen ticktimer.sleep_ms(1000).unwrap(); // draw a black screen @@ -34,11 +36,12 @@ pub fn tests() { // start the test ticktimer.sleep_ms(1000).unwrap(); - let clipping_area = Rectangle::new_coords(50, 50, 250, 450); + let clipping_area = Rectangle::new_coords(50, 50, 290, 450); let text_bounds = Rectangle::new_coords(10, 10, 240, 300); - let mut checkbound = text_bounds.clone(); + //let mut checkbound = clipping_area.clone(); // this checks against the final clipping area. + let mut checkbound = text_bounds.clone(); // this is just around the bounds specified by the TV checkbound.translate(clipping_area.tl()); checkbound.margin_out(Point::new(1, 1)); checkbound.style = DrawStyle::new(PixelColor::Light, PixelColor::Light, 1); @@ -119,6 +122,55 @@ pub fn tests() { log::info!("rendered: {:?}", tv); ticktimer.sleep_ms(1000).unwrap(); } + Some(TestType::TopRight) => { + let mut tv = TextView::new(Gid::new([0, 0, 0, 0]), + TextBounds::GrowableFromTr( + Point::new(text_bounds.tr().x - 4, text_bounds.tl().y + 4), + ((text_bounds.br().x - text_bounds.tl().x) / 2) as u16 + 30 + )); + tv.clip_rect = Some(clipping_area); + tv.style = GlyphStyle::Regular; + tv.ellipsis = false; + tv.rounded_border = None; + write!(tv, "This is a test of basic word wrapping inside a bounding box.\nThis should be a new line.\n\nTwo new lines.\nNew line\n with a leading space.\nDone.").unwrap(); + log::info!("rendering: {:?}", tv); + tv.insertion = Some(21); + gfx.draw_textview(&mut tv).unwrap(); + gfx.flush().unwrap(); + log::info!("rendered: {:?}", tv); + ticktimer.sleep_ms(1000).unwrap(); + } + Some(TestType::Overflow) => { + //gfx.draw_rectangle(blackout).unwrap(); + //gfx.flush().unwrap(); + + for x_off in 0..32 { + let clipping_area = Rectangle::new_coords(100, 100, 200, 200); + let text_bounds = Rectangle::new_coords(-20 + x_off, -20 + x_off, 150, 150); + + let mut checkbound = clipping_area.clone(); // this checks against the final clipping area. + checkbound.margin_out(Point::new(1, 1)); + checkbound.style = DrawStyle::new(PixelColor::Light, PixelColor::Dark, 1); + gfx.draw_rectangle(checkbound).unwrap(); + gfx.flush().unwrap(); + + let mut tv = TextView::new(Gid::new([0, 0, 0, 0]), + TextBounds::BoundingBox( + text_bounds + )); + tv.clip_rect = Some(clipping_area); + tv.style = GlyphStyle::Regular; + tv.ellipsis = false; + tv.rounded_border = None; + write!(tv, "This is a test of basic word wrapping inside a bounding box.\nThis should be a new line.\n\nTwo new lines.\nNew line\n with a leading space.\nDone.").unwrap(); + log::info!("rendering: {:?}", tv); + tv.insertion = Some(21); + gfx.draw_textview(&mut tv).unwrap(); + gfx.flush().unwrap(); + log::info!("rendered: {:?}", tv); + ticktimer.sleep_ms(200).unwrap(); + } + } _ => {} } } diff --git a/services/graphics-server/src/wordwrap.rs b/services/graphics-server/src/wordwrap.rs index a0778c013..46db68be6 100644 --- a/services/graphics-server/src/wordwrap.rs +++ b/services/graphics-server/src/wordwrap.rs @@ -1,5 +1,6 @@ -use crate::api::{Point, TextBounds}; +use crate::api::{Point, Rectangle}; +#[allow(unused_imports)] use crate::backend::{FB_SIZE, FB_WIDTH_PIXELS, FB_LINES}; /// Wordwrap stratgey /// @@ -85,19 +86,6 @@ impl TypesetWord { // small font text... gs } - /// offset has to take an explicit x/y set because Pt is defined as a usize, so we can't do negative offsets - pub fn offset(&mut self, x: i16, y: i16) { - let ox = self.origin.x as i16 + x; - let oy = self.origin.y as i16 + y; - self.origin.x = if ox > 0 { ox as usize } else { 0 }; - self.origin.y = if oy > 0 { oy as usize } else { 0 }; - } - /// sets the y coordinate to the given value. Returns the y coordinate of the next line based on just this word's height. - /// multi-word lines will want to track the return value and pick the maximum one as the overall line height - pub fn set_y(&mut self, y: usize) -> usize { - self.origin.y = y; - self.origin.y + self.height - } } #[derive(Debug, Copy, Clone, Eq, PartialEq)] @@ -109,6 +97,13 @@ pub enum OverflowStrategy { /// render exactly one line of text; reset the renderer for a new line at the top left of the max bb area /// This will yield only whole words if the next word could fit in a single line; otherwise, it will split /// a longer-than-one-line word unceremoniously into multiple lines + /// + /// This is intended for implementing e.g. scrollable text, where a text area is split + /// into multiple lines of typeset text, and then we can selectively render a range of text at a + /// given offset to simulate scrolling. + /// + /// The function hasn't been tested at all, however. + #[allow(dead_code)] // it's true, we haven't tested this mode yet. Remove this once we have tested it. OneLineIterator, } @@ -133,7 +128,10 @@ impl ComposedType { pub fn bb_height(&self) -> usize { self.bounding_box.max.y - self.bounding_box.min.y } - pub fn render(&self, frbuf: &mut [u32; FB_SIZE], offset: Point, invert: bool) { + /// Note: it is up to the caller to ensure that clip_rect is within the renderable screen area. We do no + /// additional checks around this. + pub fn render(&self, frbuf: &mut [u32; FB_SIZE], offset: Point, invert: bool, clip_rect: Rectangle) { + const MAX_GLYPH_MARGIN: i16 = 16; for word in self.words.iter() { let mut point = word.origin.clone(); for glyph in word.gs.iter() { @@ -142,25 +140,30 @@ impl ComposedType { let maybe_x = offset.x + point.x as i16; let maybe_y = offset.y + point.y as i16; let mut renderable = true; - if maybe_x < 0 || maybe_x > FB_WIDTH_PIXELS as i16 { + // allow MAX_GLYPH_MARGIN so we can get partial rendering of text that's slightly off screen + if maybe_x < (clip_rect.tl().x - MAX_GLYPH_MARGIN) || maybe_x > clip_rect.br().x { + log::trace!("not renderable maybe_x: {}, {:?}", maybe_x, clip_rect); renderable = false; } - if maybe_y < 0 || maybe_y > FB_LINES as i16 as i16 { + if maybe_y < (clip_rect.tl().y - MAX_GLYPH_MARGIN)|| maybe_y > clip_rect.br().y { + log::trace!("not renderable maybe_y: {}, {:?}", maybe_y, clip_rect); renderable = false; } point.x += (glyph.wide + glyph.kern) as usize; // keep scorekeeping on this, because it could eventually become renderable if !renderable { + // quickly short circuit over any text that is definitely outside of our clipping rectangle continue; } else { - // note: I am pretty sure we're going to throw an error for stuff that goes off screen to the - // right and left with the current implemntation, but that clipping should happen within the - // xor_glyph engine and not chekced up here -- because what we want is a partial character to - // render, not have a margin of no-mans land a character width from the edge. + let cr = ClipRect::new( + clip_rect.tl().x as usize, clip_rect.tl().y as usize, + clip_rect.br().x as usize, clip_rect.br().y as usize + ); blitstr2::xor_glyph( frbuf, - &Pt::new(maybe_x as usize, maybe_y as usize), + &Point::new(maybe_x, maybe_y), *glyph, - glyph.invert ^ invert + glyph.invert ^ invert, + cr ); if glyph.insert { // draw the insertion point after the glyph's position @@ -242,9 +245,6 @@ impl Typesetter { } } - pub fn last_line_height(&self) -> usize { - self.cursor.line_height - } /// Wrap the words in the string until the space overflows, leaving ellipsis at the end. /// Any prior result in self.words is overwritten. ///