From 10a1c72f35e112cb4b4aae25e41cb8d97e1fefc1 Mon Sep 17 00:00:00 2001 From: bunnie Date: Mon, 17 Feb 2025 18:25:18 +0800 Subject: [PATCH] refactor graphics-server to use new blitstr2 crate --- services/graphics-server/Cargo.toml | 11 +- services/graphics-server/src/api.rs | 7 +- services/graphics-server/src/api/blitstr2.rs | 56 --------- .../src/api/blitstr2/cliprect.rs | 115 ------------------ .../graphics-server/src/api/blitstr2/pt.rs | 36 ------ .../src/api/{blitstr2 => }/cursor.rs | 21 ++-- .../graphics-server/src/api/glyphstyle.rs | 65 ---------- services/graphics-server/src/api/shapes.rs | 4 +- services/graphics-server/src/api/text.rs | 3 +- services/graphics-server/src/backend/mod.rs | 4 +- services/graphics-server/src/lib.rs | 10 +- services/graphics-server/src/main.rs | 22 ++-- .../graphics-server/src/panic/betrusted.rs | 6 +- services/graphics-server/src/wordwrap.rs | 103 ++++++++++------ 14 files changed, 107 insertions(+), 356 deletions(-) delete mode 100644 services/graphics-server/src/api/blitstr2.rs delete mode 100644 services/graphics-server/src/api/blitstr2/cliprect.rs delete mode 100644 services/graphics-server/src/api/blitstr2/pt.rs rename services/graphics-server/src/api/{blitstr2 => }/cursor.rs (76%) delete mode 100644 services/graphics-server/src/api/glyphstyle.rs diff --git a/services/graphics-server/Cargo.toml b/services/graphics-server/Cargo.toml index 3b48d135f..298eafd24 100644 --- a/services/graphics-server/Cargo.toml +++ b/services/graphics-server/Cargo.toml @@ -14,6 +14,7 @@ xous = "0.9.64" susres = { package = "xous-api-susres", version = "0.9.63" } ticktimer-server = { package = "xous-api-ticktimer", version = "0.9.63" } locales = { path = "../../locales" } +blitstr2 = { path = "../../libs/blitstr2" } xous-ipc = "0.10.4" xous-names = { package = "xous-api-names", version = "0.9.65" } @@ -43,13 +44,11 @@ cramium-soc = [ "cramium-hal", "cramium-hal/std", ] -board-baosec = ["cramium-hal/board-baosec"] -board-baosor = ["cramium-hal/board-baosor"] -board-dabao = ["cramium-hal/board-dabao"] +board-baosor = ["cramium-hal/board-baosor", "blitstr2/board-baosor"] -precursor = ["utralib/precursor"] -hosted = ["utralib/hosted"] -renode = ["utralib/renode"] +precursor = ["utralib/precursor", "blitstr2/precursor"] +hosted = ["utralib/hosted", "blitstr2/hosted"] +renode = ["utralib/renode", "blitstr2/renode"] debugprint = [] braille = [] gfx-testing = [] diff --git a/services/graphics-server/src/api.rs b/services/graphics-server/src/api.rs index 06a8ed030..3936986a2 100644 --- a/services/graphics-server/src/api.rs +++ b/services/graphics-server/src/api.rs @@ -22,10 +22,9 @@ pub mod shapes; pub use shapes::*; pub mod text; pub use text::*; -pub mod glyphstyle; -pub use glyphstyle::*; -pub mod blitstr2; -pub use blitstr2::*; +pub mod cursor; +pub use cursor::*; + #[cfg(feature = "ditherpunk")] pub mod tile; use std::hash::{Hash, Hasher}; diff --git a/services/graphics-server/src/api/blitstr2.rs b/services/graphics-server/src/api/blitstr2.rs deleted file mode 100644 index f831b63b6..000000000 --- a/services/graphics-server/src/api/blitstr2.rs +++ /dev/null @@ -1,56 +0,0 @@ -mod cliprect; -pub use cliprect::*; -mod cursor; -pub use cursor::*; -mod pt; -pub use pt::*; - -// Font data is stored as CODEPOINTS and GLYPHS arrays. CODEPOINTS holds sorted -// Unicode codepoints for characters included in the font, and GLYPHS holds -// 16*16px sprites (pixels packed in row-major order, LSB of first word is top -// left pixel of sprite). The order of codepoints and glyphs is the same, but, -// each codepoint is one u32 word long while each glyph is eight u32 words -// long. So, to find a glyph we do: -// 1. Binary search CODEPOINTS for the codepoint of interest -// 2. Multiply the codepoint index by 8, yielding an offset into GLYPHS -// 3. Slice 8 u32 words from GLYPHS starting at the offset - -/// Struct to hold sprite pixel reference and associated metadata for glyphs -#[derive(Copy, Clone, Debug)] -pub struct GlyphSprite { - pub glyph: &'static [u32], - pub wide: u8, - pub high: u8, - pub kern: u8, - // the original character - pub ch: char, - // invert rendering for the character - for copy/paste selection regions - pub invert: bool, - // drawn an insertion point after this character - pub insert: bool, - // 2x flag for the back-end rendering (wide/high should be pre-computed to match this) - pub double: bool, - // flag for 32-bit wide glyph sets - pub large: bool, -} - -/// A TypesetWord is a Word that has beet turned into sprites and placed at a specific location on the canvas, -/// defined by its `bb` record. The intention is that this abstract representation can be passed directly to -/// a rasterizer for rendering. -#[derive(Debug)] -pub(crate) struct TypesetWord { - /// glyph data to directly render the word - pub gs: Vec, - /// top left origin point for rendering of the glyphs - pub origin: Pt, - /// width of the word - pub width: i16, - /// overall height for the word - pub height: i16, - /// set if this `word` is not drawable, e.g. a newline placeholder. - /// *however* the Vec should still be checked for an insertion point, so that - /// successive newlines properly get their insertion point drawn - pub non_drawable: bool, - /// the position in the originating abstract string of the first character in the word - pub strpos: usize, -} diff --git a/services/graphics-server/src/api/blitstr2/cliprect.rs b/services/graphics-server/src/api/blitstr2/cliprect.rs deleted file mode 100644 index ebe29260a..000000000 --- a/services/graphics-server/src/api/blitstr2/cliprect.rs +++ /dev/null @@ -1,115 +0,0 @@ -// Copyright (c) 2022 Sam Blenny -// SPDX-License-Identifier: Apache-2.0 OR MIT -// -use crate::api::Pt; -use crate::api::{LINES, WIDTH}; - -/// ClipRect specifies a region of pixels. X and y pixel ranges are inclusive of -/// min and exclusive of max (i.e. it's min.x..max.x rather than min.x..=max.x) -/// Coordinate System Notes: -/// - (0,0) is top left -/// - Increasing Y moves downward on the screen, increasing X moves right -#[derive(Copy, Clone, Debug, PartialEq, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)] -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: i16, min_y: i16, max_x: i16, max_y: i16) -> ClipRect { - // Make sure min_x <= max_x && min_y <= max_y - let mut min = Pt { x: min_x, y: min_y }; - let mut max = Pt { x: max_x, y: max_y }; - if min_x > max_x { - min.x = max_x; - max.x = min_x; - } - if min_y > max_y { - min.y = max_y; - max.y = min_y; - } - ClipRect { min, max } - } - - pub fn to_rect(&self) -> crate::api::Rectangle { - crate::api::Rectangle::new_coords( - self.min.x as i16, - self.min.y as i16, - self.max.x as i16, - self.max.y as i16, - ) - } - - /// Make a rectangle of the full screen size (0,0)..(WIDTH,LINES) - pub fn full_screen() -> ClipRect { ClipRect::new(0, 0, WIDTH, LINES) } - - /// Make a rectangle of the screen size minus padding (6,6)..(WIDTH-6,LINES-6) - pub fn padded_screen() -> ClipRect { - let pad = 6; - ClipRect::new(pad, pad, WIDTH - pad, LINES - pad) - } - - pub fn intersects(&self, other: ClipRect) -> bool { - !(self.max.x < other.min.x - || self.min.y > other.max.y - || self.max.y < other.min.y - || self.min.x > other.max.x) - } - - pub fn intersects_point(&self, point: Pt) -> bool { - ((point.x >= self.min.x) && (point.x <= self.max.x)) - && ((point.y >= self.min.y) && (point.y <= self.max.y)) - } - - /// takes the current Rectangle, and clips it with a clipping Rectangle; returns a new rectangle as the - /// result - pub fn clip_with(&self, clip: ClipRect) -> Option { - // check to see if we even overlap; if not, don't do any computation - if !self.intersects(clip) { - return None; - } - let tl: Pt = Pt::new( - if self.min.x < clip.min.x { clip.min.x } else { self.min.x }, - if self.min.y < clip.min.y { clip.min.y } else { self.min.y }, - ); - let br: Pt = Pt::new( - if self.max.x > clip.max.x { clip.max.x } else { self.max.x }, - if self.max.y > clip.max.y { clip.max.y } else { self.max.y }, - ); - Some(ClipRect::new(tl.x, tl.y, br.x, br.y)) - } -} - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn test_cliprect_equivalence() { - let cr1 = ClipRect { min: Pt { x: 1, y: 2 }, max: Pt { x: 8, y: 9 } }; - // Called properly: - let cr2 = ClipRect::new(1, 2, 8, 9); - // Called with mixed up corners that should get auto-corrected - let cr3 = ClipRect::new(8, 2, 1, 9); - let cr4 = ClipRect::new(1, 9, 8, 2); - assert_eq!(cr1, cr2); - assert_eq!(cr2, cr3); - assert_eq!(cr3, cr4); - } - - #[test] - fn test_cliprect_full_screen() { - let clip = ClipRect::full_screen(); - assert_eq!(clip.min, Pt::new(0, 0)); - assert_eq!(clip.max, Pt::new(WIDTH, LINES)); - } - - #[test] - fn test_cliprect_padded_screen() { - let c1 = ClipRect::full_screen(); - let c2 = ClipRect::padded_screen(); - assert!(c2.min > c1.min); - assert!(c2.max < c1.max); - } -} diff --git a/services/graphics-server/src/api/blitstr2/pt.rs b/services/graphics-server/src/api/blitstr2/pt.rs deleted file mode 100644 index 8e47da5ee..000000000 --- a/services/graphics-server/src/api/blitstr2/pt.rs +++ /dev/null @@ -1,36 +0,0 @@ -// Copyright (c) 2022 Sam Blenny -// SPDX-License-Identifier: Apache-2.0 OR MIT -// - -/// Point specifies a pixel coordinate -#[derive(Copy, Clone, Debug, PartialEq, PartialOrd, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)] -pub struct Pt { - pub x: i16, - pub y: i16, -} - -impl Pt { - /// Make a new point - pub fn new(x: i16, y: i16) -> Pt { Pt { x, y } } -} - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn test_pt_equivalence() { - let p1 = Pt { x: 1, y: 2 }; - let p2 = Pt::new(1, 2); - assert_eq!(p1, p2); - } - - #[test] - fn test_pt_ordering() { - let p1 = Pt { x: 1, y: 2 }; - let p2 = Pt::new(1, 3); - let p3 = Pt::new(0, 0); - assert!(p1 < p2); - assert!(p1 > p3); - } -} diff --git a/services/graphics-server/src/api/blitstr2/cursor.rs b/services/graphics-server/src/api/cursor.rs similarity index 76% rename from services/graphics-server/src/api/blitstr2/cursor.rs rename to services/graphics-server/src/api/cursor.rs index f27a353be..f6e2ce8a0 100644 --- a/services/graphics-server/src/api/blitstr2/cursor.rs +++ b/services/graphics-server/src/api/cursor.rs @@ -1,26 +1,29 @@ // Copyright (c) 2022 Sam Blenny // SPDX-License-Identifier: Apache-2.0 OR MIT // -use super::GlyphSprite; -use super::TypesetWord; -use super::cliprect::ClipRect; -use super::pt::Pt; +use blitstr2::{ClipRect, GlyphSprite}; + +use crate::{Point, wordwrap::TypesetWord}; /// Cursor specifies a drawing position along a line of text. Lines of text can /// be different heights. Line_height is for keeping track of the tallest /// character that has been drawn so far on the current line. #[derive(Copy, Clone, Debug, PartialEq, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)] pub struct Cursor { - pub pt: Pt, + pub pt: Point, pub line_height: usize, } #[allow(dead_code)] impl Cursor { // Make a new Cursor. When in doubt, set line_height = 0. - pub fn new(x: i16, y: i16, line_height: usize) -> Cursor { Cursor { pt: Pt { x, y }, line_height } } + pub fn new(x: isize, y: isize, line_height: usize) -> Cursor { + Cursor { pt: Point { x: x as i16, y: y as i16 }, line_height } + } // Make a Cursor aligned at the top left corner of a ClipRect - pub fn from_top_left_of(r: ClipRect) -> Cursor { Cursor { pt: r.min, line_height: 0 } } + pub fn from_top_left_of(r: ClipRect) -> Cursor { + Cursor { pt: Point::new(r.min.x as i16, r.min.y as i16), line_height: 0 } + } pub fn update_glyph(&mut self, glyph: &GlyphSprite) { self.pt.x += glyph.wide as i16; @@ -28,13 +31,13 @@ impl Cursor { } pub(crate) fn update_word(&mut self, word: &TypesetWord) { - self.pt.x += word.width; + self.pt.x += word.width as i16; self.line_height = self.line_height.max(word.height as usize); } pub fn add(&self, other: Cursor) -> Cursor { Cursor { - pt: Pt::new(self.pt.x + other.pt.x, self.pt.y + other.pt.y), + pt: Point::new(self.pt.x + other.pt.x, self.pt.y + other.pt.y), line_height: self.line_height.max(other.line_height), } } diff --git a/services/graphics-server/src/api/glyphstyle.rs b/services/graphics-server/src/api/glyphstyle.rs deleted file mode 100644 index 8263e9ca8..000000000 --- a/services/graphics-server/src/api/glyphstyle.rs +++ /dev/null @@ -1,65 +0,0 @@ -/// Style options for Latin script fonts -#[derive(Copy, Clone, Debug, PartialEq, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)] -pub enum GlyphStyle { - Small = 0, - Regular = 1, - Bold = 2, - Monospace = 3, - Cjk = 4, - Large = 5, - ExtraLarge = 6, - Tall = 7, -} - -/// Convert number to style for use with register-based message passing sytems -// [by bunnie for Xous] -impl From for GlyphStyle { - fn from(gs: usize) -> Self { - match gs { - 0 => GlyphStyle::Small, - 1 => GlyphStyle::Regular, - 2 => GlyphStyle::Bold, - 3 => GlyphStyle::Monospace, - 4 => GlyphStyle::Cjk, - 5 => GlyphStyle::Large, - 6 => GlyphStyle::ExtraLarge, - 7 => GlyphStyle::Tall, - _ => GlyphStyle::Regular, - } - } -} - -/// Convert style to number for use with register-based message passing sytems -// [by bunnie for Xous] -impl From for usize { - fn from(g: GlyphStyle) -> usize { - match g { - GlyphStyle::Small => 0, - GlyphStyle::Regular => 1, - GlyphStyle::Bold => 2, - GlyphStyle::Monospace => 3, - GlyphStyle::Cjk => 4, - GlyphStyle::Large => 5, - GlyphStyle::ExtraLarge => 6, - GlyphStyle::Tall => 7, - } - } -} - -/// Estimate line-height for Latin script text in the given style -/// These are hard-coded in because we want to keep the rest of the font data -/// structures private to this crate. Moving the font files out of their -/// current location would also require modifying a bunch of codegen infrastruture, -/// so, this is one spot where we have to manually maintain a link. -pub fn glyph_to_height_hint(g: GlyphStyle) -> usize { - match g { - GlyphStyle::Small => 12, // crate::blitstr2::fonts::small::MAX_HEIGHT as usize, - GlyphStyle::Regular => 15, // crate::blitstr2::fonts::regular::MAX_HEIGHT as usize, - GlyphStyle::Bold => 15, // crate::blitstr2::fonts::regular::MAX_HEIGHT as usize, - GlyphStyle::Monospace => 15, // crate::blitstr2::fonts::mono::MAX_HEIGHT as usize, - GlyphStyle::Cjk => 16, // crate::blistr2::fonts::emoji::MAX_HEIGHT as usize, - GlyphStyle::Large => 24, // 2x of small - GlyphStyle::ExtraLarge => 30, // 2x of regular - GlyphStyle::Tall => 19, - } -} diff --git a/services/graphics-server/src/api/shapes.rs b/services/graphics-server/src/api/shapes.rs index 1eac9a5b1..17264e9a7 100644 --- a/services/graphics-server/src/api/shapes.rs +++ b/services/graphics-server/src/api/shapes.rs @@ -1,6 +1,8 @@ use core::cmp::{max, min}; -use crate::api::{ClipRect, DrawStyle, Point}; +use blitstr2::ClipRect; + +use crate::api::{DrawStyle, Point}; use crate::op::{HEIGHT, WIDTH}; #[derive(Debug, Clone, Copy, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)] diff --git a/services/graphics-server/src/api/text.rs b/services/graphics-server/src/api/text.rs index 3dd8c0b44..918dd5f08 100644 --- a/services/graphics-server/src/api/text.rs +++ b/services/graphics-server/src/api/text.rs @@ -1,8 +1,9 @@ use core::ops::Add; use String; +use blitstr2::GlyphStyle; -use crate::api::{Cursor, Gid, GlyphStyle, Point, Rectangle}; +use crate::api::{Cursor, Gid, Point, Rectangle}; /// coordinates are local to the canvas, not absolute to the screen #[derive(Debug, Copy, Clone, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)] diff --git a/services/graphics-server/src/backend/mod.rs b/services/graphics-server/src/backend/mod.rs index 18562146e..bd00ffcbb 100644 --- a/services/graphics-server/src/backend/mod.rs +++ b/services/graphics-server/src/backend/mod.rs @@ -8,7 +8,7 @@ mod betrusted; #[cfg(any(feature = "precursor", feature = "renode"))] pub use crate::backend::betrusted::*; -#[cfg(feature = "cramium-soc")] +#[cfg(feature = "board-baosor")] mod cramium; -#[cfg(feature = "cramium-soc")] +#[cfg(feature = "board-baosor")] pub use crate::backend::cramium::*; diff --git a/services/graphics-server/src/lib.rs b/services/graphics-server/src/lib.rs index ec7aee79e..0c8f9b3e0 100644 --- a/services/graphics-server/src/lib.rs +++ b/services/graphics-server/src/lib.rs @@ -1,20 +1,18 @@ #![cfg_attr(target_os = "none", no_std)] -// pub mod size; pub mod api; #[cfg(feature = "ditherpunk")] pub use api::Tile; pub use api::{ - Circle, ClipObject, ClipObjectList, ClipObjectType, ClipRect, Cursor, DrawStyle, Gid, GlyphStyle, Line, - PixelColor, Point, Rectangle, RoundedRectangle, TextBounds, TextOp, TextView, TokenClaim, + Circle, ClipObject, ClipObjectList, ClipObjectType, DrawStyle, Gid, Line, PixelColor, Point, Rectangle, + RoundedRectangle, TextBounds, TextOp, TextView, TokenClaim, }; pub mod op; - -pub mod fontmap; +pub mod wordwrap; pub use api::ArchivedBulkRead; pub use api::BulkRead; use api::{Opcode, TEXTVIEW_LEN}; // if you prefer to map the api into your local namespace -pub use fontmap::*; +use blitstr2::GlyphStyle; use num_traits::ToPrimitive; use xous::{Message, send_message}; use xous_ipc::Buffer; diff --git a/services/graphics-server/src/main.rs b/services/graphics-server/src/main.rs index 1b57b01cf..f178e2209 100644 --- a/services/graphics-server/src/main.rs +++ b/services/graphics-server/src/main.rs @@ -19,18 +19,14 @@ mod sleep_note; use api::*; -mod blitstr2; mod wordwrap; -#[macro_use] -mod style_macros; +use api::BulkRead; +use blitstr2::{GlyphStyle, fontmap, glyph_to_height_hint}; use num_traits::FromPrimitive; use xous::{MemoryRange, msg_blocking_scalar_unpack, msg_scalar_unpack}; use xous_ipc::Buffer; -mod fontmap; -use api::BulkRead; - #[cfg(any(feature = "precursor", feature = "renode", feature = "cramium-soc"))] // only install for hardware targets; hosted mode uses host's panic handler mod panic; @@ -311,29 +307,29 @@ fn wrapped_main(main_thread_token: backend::MainThreadToken) -> ! { let screen_offset: Point = tv.clip_rect.unwrap().tl; let typeset_extent = match tv.bounds_hint { - TextBounds::BoundingBox(r) => Pt::new( + TextBounds::BoundingBox(r) => Point::new( r.br().x - r.tl().x - tv.margin.x * 2, r.br().y - r.tl().y - tv.margin.y * 2, ), TextBounds::GrowableFromBr(br, width) => { - Pt::new(width as i16 - tv.margin.x * 2, br.y - tv.margin.y * 2) + Point::new(width as i16 - tv.margin.x * 2, br.y - tv.margin.y * 2) } TextBounds::GrowableFromBl(bl, width) => { - Pt::new(width as i16 - tv.margin.x * 2, bl.y - tv.margin.y * 2) + Point::new(width as i16 - tv.margin.x * 2, bl.y - tv.margin.y * 2) } - TextBounds::GrowableFromTl(tl, width) => Pt::new( + TextBounds::GrowableFromTl(tl, width) => Point::new( width as i16 - tv.margin.x * 2, (clip_rect.br().y - clip_rect.tl().y - tl.y) - tv.margin.y * 2, ), - TextBounds::GrowableFromTr(tr, width) => Pt::new( + TextBounds::GrowableFromTr(tr, width) => Point::new( width as i16 - tv.margin.x * 2, (clip_rect.br().y - clip_rect.tl().y - tr.y) - tv.margin.y * 2, ), - TextBounds::CenteredTop(r) => Pt::new( + TextBounds::CenteredTop(r) => Point::new( r.br().x - r.tl().x - tv.margin.x * 2, r.br().y - r.tl().y - tv.margin.y * 2, ), - TextBounds::CenteredBot(r) => Pt::new( + TextBounds::CenteredBot(r) => Point::new( r.br().x - r.tl().x - tv.margin.x * 2, r.br().y - r.tl().y - tv.margin.y * 2, ), diff --git a/services/graphics-server/src/panic/betrusted.rs b/services/graphics-server/src/panic/betrusted.rs index ec73a6d86..8263850fe 100644 --- a/services/graphics-server/src/panic/betrusted.rs +++ b/services/graphics-server/src/panic/betrusted.rs @@ -2,6 +2,7 @@ use core::sync::atomic::{AtomicBool, Ordering}; use std::sync::Arc; use std::thread; +use blitstr2::{GlyphSprite, NULL_GLYPH_SPRITE}; use utralib::generated::*; /// We can have no allocations inside this, and ideally, it's as minimal as possible. @@ -23,9 +24,8 @@ use utralib::generated::*; /// Note that the frame buffer is 336 px wide, which is 10.5 32-bit words. /// The excess 16 bits are the dirty bit field. use crate::{ - api::{GlyphSprite, PixelColor}, + api::PixelColor, backend::{FB_SIZE, FB_WIDTH_PIXELS, FB_WIDTH_WORDS}, - blitstr2::NULL_GLYPH_SPRITE, }; /// How far down the screen the panic box draws const TOP_OFFSET: usize = 48; @@ -224,7 +224,7 @@ impl<'a> PanicDisplay<'a> { self.x = TEXT_MARGIN; continue; } - self.draw_glyph(self.x, self.y, crate::blitstr2::mono_glyph(ch).unwrap_or(NULL_GLYPH_SPRITE)); + self.draw_glyph(self.x, self.y, blitstr2::mono_glyph(ch).unwrap_or(NULL_GLYPH_SPRITE)); self.x += GLYPH_WIDTH; if self.x >= WIDTH_CHARS * GLYPH_WIDTH { self.x = TEXT_MARGIN; diff --git a/services/graphics-server/src/wordwrap.rs b/services/graphics-server/src/wordwrap.rs index c8ced274a..88bb8f404 100644 --- a/services/graphics-server/src/wordwrap.rs +++ b/services/graphics-server/src/wordwrap.rs @@ -1,6 +1,5 @@ -use crate::api::{Cursor, GlyphSprite, GlyphStyle, Point, Pt, Rectangle, TypesetWord, glyph_to_height_hint}; -#[allow(unused_imports)] -use crate::backend::{FB_LINES, FB_SIZE, FB_WIDTH_PIXELS}; +use blitstr2::platform::FB_SIZE; +use blitstr2::style_macros::*; /// Wordwrap stratgey /// /// Strings are submitted to the Wordwrapper, and they are split into lines, and then into lexical words. @@ -27,11 +26,33 @@ use crate::backend::{FB_LINES, FB_SIZE, FB_WIDTH_PIXELS}; /// /// If the overall string cannot fit within the absolute bounds defined by the `max` area and/or the /// `bounds`, the rendering is halted, and ellipses are inserted at the end. -use crate::blitstr2::{self, *}; -use crate::style_macros::*; +use blitstr2::*; + +use crate::api::*; + +/// A TypesetWord is a Word that has beet turned into sprites and placed at a specific location on the canvas, +/// defined by its `bb` record. The intention is that this abstract representation can be passed directly to +/// a rasterizer for rendering. +#[derive(Debug)] +pub struct TypesetWord { + /// glyph data to directly render the word + pub gs: Vec, + /// top left origin point for rendering of the glyphs + pub origin: Point, + /// width of the word + pub width: i16, + /// overall height for the word + pub height: i16, + /// set if this `word` is not drawable, e.g. a newline placeholder. + /// *however* the Vec should still be checked for an insertion point, so that + /// successive newlines properly get their insertion point drawn + pub non_drawable: bool, + /// the position in the originating abstract string of the first character in the word + pub strpos: usize, +} impl TypesetWord { - pub fn new(origin: Pt, strpos: usize) -> Self { + pub fn new(origin: Point, strpos: usize) -> Self { TypesetWord { gs: Vec::::new(), origin, @@ -42,7 +63,7 @@ impl TypesetWord { } } - pub fn one_glyph(origin: Pt, gs: GlyphSprite, strpos: usize) -> Self { + pub fn one_glyph(origin: Point, gs: GlyphSprite, strpos: usize) -> Self { TypesetWord { gs: vec![gs], origin, @@ -92,7 +113,7 @@ pub enum OverflowStrategy { /// ComposedType is text that has been laid out and wrapped, along with metadata about the bounds /// of the final composition. ComposedType coordinates are always in screen-space. -pub(crate) struct ComposedType { +pub struct ComposedType { words: Vec, bounding_box: ClipRect, cursor: Cursor, @@ -103,9 +124,9 @@ impl ComposedType { ComposedType { words, bounding_box: bounds, cursor, overflow } } - pub fn bb_width(&self) -> i16 { self.bounding_box.max.x - self.bounding_box.min.x } + pub fn bb_width(&self) -> i16 { self.bounding_box.max.x as i16 - self.bounding_box.min.x as i16 } - pub fn bb_height(&self) -> i16 { self.bounding_box.max.y - self.bounding_box.min.y } + pub fn bb_height(&self) -> i16 { self.bounding_box.max.y as i16 - self.bounding_box.min.y as i16 } /// 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. @@ -138,12 +159,16 @@ impl ComposedType { // rectangle continue; } else { - let cr = - ClipRect::new(clip_rect.tl().x, clip_rect.tl().y, clip_rect.br().x, clip_rect.br().y); + let cr = ClipRect::new( + clip_rect.tl().x as isize, + clip_rect.tl().y as isize, + clip_rect.br().x as isize, + clip_rect.br().y as isize, + ); if glyph.large { blitstr2::xor_glyph_large( frbuf, - &Point::new(maybe_x, maybe_y), + (maybe_x as isize, maybe_y as isize), *glyph, glyph.invert ^ invert, cr, @@ -151,7 +176,7 @@ impl ComposedType { } else if !glyph.double { blitstr2::xor_glyph( frbuf, - &Point::new(maybe_x, maybe_y), + (maybe_x as isize, maybe_y as isize), *glyph, glyph.invert ^ invert, cr, @@ -159,7 +184,7 @@ impl ComposedType { } else { blitstr2::xor_glyph_2x( frbuf, - &Point::new(maybe_x, maybe_y), + (maybe_x as isize, maybe_y as isize), *glyph, glyph.invert ^ invert, cr, @@ -170,12 +195,9 @@ impl ComposedType { // draw the insertion point after the glyph's position crate::op::line( frbuf, - crate::api::Line::new( - crate::api::Point::new(maybe_x as i16 - 1, maybe_y as _), - crate::api::Point::new( - maybe_x as i16 - 1, - maybe_y as i16 + glyph.high as i16, - ), + Line::new( + Point::new(maybe_x as i16 - 1, maybe_y as _), + Point::new(maybe_x as i16 - 1, maybe_y as i16 + glyph.high as i16), ), Some(clip_rect), invert, @@ -211,7 +233,7 @@ impl ComposedType { /// /// An insertion point cursor will be injected into the TypesetWord stream at the character offset in /// the input `string` if it is specified as `Some(usize)`. -pub(crate) struct Typesetter { +pub struct Typesetter { charpos: usize, cursor: Cursor, /* indicates the current insertion point for a candidate. it is not updated as the * candidates are formed. */ @@ -228,8 +250,8 @@ pub(crate) struct Typesetter { last_line_height: usize, // scorecarding for the very last line on the loop exit } impl Typesetter { - pub fn setup(s: &str, extent: &Pt, base_style: &GlyphStyle, insertion_point: Option) -> Self { - let bb = ClipRect::new(0, 0, extent.x, extent.y); + pub fn setup(s: &str, extent: &Point, base_style: &GlyphStyle, insertion_point: Option) -> Self { + let bb = ClipRect::new(0, 0, extent.x as isize, extent.y as isize); let mut space = style_glyph(' ', base_style); space.kern = 0; let mut ellipsis = style_glyph('…', base_style); @@ -249,8 +271,9 @@ impl Typesetter { Typesetter { charpos: 0, cursor: Cursor::new(0, 0, 0), - candidate: TypesetWord::new(Pt::new(0, 0), 0), /* first word candidate starts at the top left - * corner */ + candidate: TypesetWord::new(Point::new(0, 0), 0), /* first word candidate starts at the top + * left + * corner */ bb, space, ellipsis, @@ -280,7 +303,7 @@ impl Typesetter { // going out of scope at the end of the call. let mut composition = Vec::::new(); - if self.bb.max.x - self.bb.min.x < glyph_to_height_hint(GlyphStyle::Regular) as i16 { + if self.bb.max.x - self.bb.min.x < glyph_to_height_hint(GlyphStyle::Regular) as isize { // we flag this because the typesetter algorithm may never converge if it can't set any characters // because the region is just too narrow. log::error!("Words cannot be typset because the width of the typset region is too narrow."); @@ -468,10 +491,10 @@ impl Typesetter { let ret = ComposedType::new( composition, ClipRect::new( - self.bb.min.x, - self.bb.min.y, - self.max_width as i16, - self.cursor.pt.y + self.last_line_height as i16, + self.bb.min.x as isize, + self.bb.min.y as isize, + self.max_width as isize, + self.cursor.pt.y as isize + self.last_line_height as isize, ), self.cursor, self.overflow, @@ -481,7 +504,7 @@ impl Typesetter { OverflowStrategy::OneLineIterator => { self.max_width = 0; // it's a fresh line every time assert!( - self.cursor.pt.x == self.bb.min.x, + self.cursor.pt.x as isize == self.bb.min.x, "internal logic did not clean up the cursor state for the one line iterator" ) } @@ -502,12 +525,14 @@ impl Typesetter { corrected_height + self.cursor.pt.y + self.cursor.line_height as i16, self.bb.max.y ); - corrected_height + self.cursor.pt.y + (self.cursor.line_height as i16) < self.bb.max.y + corrected_height + self.cursor.pt.y + (self.cursor.line_height as i16) < self.bb.max.y as i16 } - fn does_word_fit_on_line(&self) -> bool { self.candidate.width + self.cursor.pt.x < self.bb.max.x } + fn does_word_fit_on_line(&self) -> bool { self.candidate.width + self.cursor.pt.x < self.bb.max.x as i16 } - fn is_word_longer_than_line(&self) -> bool { self.candidate.width >= (self.bb.max.x - self.bb.min.x) } + fn is_word_longer_than_line(&self) -> bool { + self.candidate.width >= (self.bb.max.x as i16 - self.bb.min.x as i16) + } fn is_insert_point(&self) -> bool { if let Some(ip) = self.insertion_point { @@ -550,7 +575,7 @@ impl Typesetter { // advance the rendering line, without inserting a newline placeholder self.last_line_height = self.cursor.line_height; self.cursor.pt.y += self.cursor.line_height as i16; - self.cursor.pt.x = self.bb.min.x; + self.cursor.pt.x = self.bb.min.x as i16; self.cursor.line_height = self.candidate.height as usize; // now set the current candidate word's origin to the beginning of this new line self.candidate.origin = self.cursor.pt; @@ -581,7 +606,7 @@ impl Typesetter { /// set to our space point, because the caller will have already stashed the previously formed word fn try_append_space(&mut self, composition: &mut Vec) -> bool { assert!(self.candidate.gs.len() == 0, "self.candidate was not set to a new state prior to this call"); - if (self.cursor.pt.x + self.space.wide as i16) < self.bb.max.x { + if (self.cursor.pt.x + self.space.wide as i16) < self.bb.max.x as i16 { // our candidate word is "just as space" let mut candidate_space = self.space.clone(); if self.is_insert_point() { @@ -592,7 +617,7 @@ impl Typesetter { self.cursor.line_height = self.cursor.line_height.max(self.space.high as usize); // if we're at the beginning of a line, mark the candidate word (that just contains a space) as // non-drawable - if self.cursor.pt.x == self.bb.min.x { + if self.cursor.pt.x == self.bb.min.x as i16 { self.candidate.non_drawable = true; } // now commit it @@ -606,7 +631,7 @@ impl Typesetter { /// resets the cursor state to the top left of the box for the next line to render. fn oneline_epilogue(&mut self) { self.cursor.pt.y = 0; // this should be redundant, as we never have more than one line in this mode - self.cursor.pt.x = self.bb.min.x; + self.cursor.pt.x = self.bb.min.x as i16; if self.cursor.line_height == 0 { // in case we have successive newlines, just default to the "regular" height self.cursor.line_height = glyph_to_height_hint(GlyphStyle::Regular);