From 73114f8d8df0d791882231c96b268f93cdf1b259 Mon Sep 17 00:00:00 2001 From: bunnie Date: Mon, 17 Feb 2025 18:27:55 +0800 Subject: [PATCH] clean up ux-api integration with blitstr2 a bunch of imports were in here that should be in blitstr2 let's test this out and see how well these abstractions hold up as we configure the rest of the OS around it. --- libs/ux-api/Cargo.toml | 11 +- libs/ux-api/src/cursor.rs | 66 ++++++++++++ libs/ux-api/src/lib.rs | 7 +- libs/ux-api/src/minigfx/cliprect.rs | 115 -------------------- libs/ux-api/src/minigfx/cursor.rs | 19 ++-- libs/ux-api/src/minigfx/glyphstyle.rs | 65 ----------- libs/ux-api/src/minigfx/mod.rs | 39 +------ libs/ux-api/src/minigfx/pt.rs | 36 ------- libs/ux-api/src/minigfx/textview.rs | 1 + libs/ux-api/src/style_macros.rs | 148 -------------------------- libs/ux-api/src/widgets/modal.rs | 2 +- libs/ux-api/src/wordwrap.rs | 58 +++++----- 12 files changed, 126 insertions(+), 441 deletions(-) create mode 100644 libs/ux-api/src/cursor.rs delete mode 100644 libs/ux-api/src/minigfx/cliprect.rs delete mode 100644 libs/ux-api/src/minigfx/glyphstyle.rs delete mode 100644 libs/ux-api/src/minigfx/pt.rs delete mode 100644 libs/ux-api/src/style_macros.rs diff --git a/libs/ux-api/Cargo.toml b/libs/ux-api/Cargo.toml index 2add8e571..14163a07d 100644 --- a/libs/ux-api/Cargo.toml +++ b/libs/ux-api/Cargo.toml @@ -15,6 +15,7 @@ rkyv = { version = "0.8.8", default-features = false, features = [ ], optional = true } zeroize = "1.8.1" enum_dispatch = "0.3.7" +blitstr2 = { path = "../blitstr2", optional = true } # used in notifications locales = { path = "../../locales" } @@ -48,15 +49,15 @@ default-widgets = [ ] # board definition is used to define things like the size of the screen. -board-baosec = [] +board-baosec = ["blitstr2/board-baosec"] board-baosor = [] -hosted-baosec = [] +hosted-baosec = ["blitstr2/hosted-baosec"] # Used by font codegen routine -hosted = [] -renode = [] +hosted = ["blitstr2/hosted"] +renode = ["blitstr2/renode"] cramium-soc = [] -precursor = [] +precursor = ["blitstr2/precursor"] std = ["rkyv"] default = ["std", "default-widgets"] diff --git a/libs/ux-api/src/cursor.rs b/libs/ux-api/src/cursor.rs new file mode 100644 index 000000000..4914bc805 --- /dev/null +++ b/libs/ux-api/src/cursor.rs @@ -0,0 +1,66 @@ +// Copyright (c) 2022 Sam Blenny +// SPDX-License-Identifier: Apache-2.0 OR MIT +// +use blitstr2::{ClipRect, GlyphSprite}; + +use crate::{minigfx::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)] +pub struct Cursor { + 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: isize, y: isize, line_height: usize) -> Cursor { + Cursor { pt: Point { x, y }, 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: Point::new(r.min.x, r.min.y), line_height: 0 } + } + + pub fn update_glyph(&mut self, glyph: &GlyphSprite) { + self.pt.x += glyph.wide as isize; + self.line_height = self.line_height.max(glyph.high as usize); + } + + pub(crate) fn update_word(&mut self, word: &TypesetWord) { + self.pt.x += word.width; + self.line_height = self.line_height.max(word.height as usize); + } + + pub fn add(&self, other: Cursor) -> Cursor { + Cursor { + pt: Point::new(self.pt.x + other.pt.x, self.pt.y + other.pt.y), + line_height: self.line_height.max(other.line_height), + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_cursor_equivalence() { + let c1 = Cursor { pt: Pt { x: 1, y: 2 }, line_height: 0 }; + let c2 = Cursor::new(1, 2, 0); + assert_eq!(c1, c2); + let clip = ClipRect::new(1, 2, 3, 4); + let c3 = Cursor::from_top_left_of(clip); + assert_eq!(c1, c3); + } + + #[test] + fn test_cursor_from_clip_rect() { + let cr = ClipRect::new(1, 2, 8, 9); + let c = Cursor::from_top_left_of(cr); + assert_eq!(c.pt, cr.min); + } +} diff --git a/libs/ux-api/src/lib.rs b/libs/ux-api/src/lib.rs index d04146d7d..5b31f1fdb 100644 --- a/libs/ux-api/src/lib.rs +++ b/libs/ux-api/src/lib.rs @@ -1,12 +1,11 @@ #![cfg_attr(not(feature = "std"), no_std)] -mod blitstr2; pub mod minigfx; pub mod widgets; mod wordwrap; -#[macro_use] -mod style_macros; + +pub mod cursor; mod fontmap; pub mod platform; -pub const SYSTEM_STYLE: crate::minigfx::GlyphStyle = crate::minigfx::GlyphStyle::Tall; +pub const SYSTEM_STYLE: blitstr2::GlyphStyle = blitstr2::GlyphStyle::Tall; diff --git a/libs/ux-api/src/minigfx/cliprect.rs b/libs/ux-api/src/minigfx/cliprect.rs deleted file mode 100644 index 94675e3b4..000000000 --- a/libs/ux-api/src/minigfx/cliprect.rs +++ /dev/null @@ -1,115 +0,0 @@ -// Copyright (c) 2022 Sam Blenny -// SPDX-License-Identifier: Apache-2.0 OR MIT -// -use super::*; -use crate::platform::{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: isize, min_y: isize, max_x: isize, max_y: isize) -> 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) -> Rectangle { - Rectangle::new_coords( - self.min.x as isize, - self.min.y as isize, - self.max.x as isize, - self.max.y as isize, - ) - } - - /// 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/libs/ux-api/src/minigfx/cursor.rs b/libs/ux-api/src/minigfx/cursor.rs index 927e4646b..bd445eff1 100644 --- a/libs/ux-api/src/minigfx/cursor.rs +++ b/libs/ux-api/src/minigfx/cursor.rs @@ -1,26 +1,31 @@ // Copyright (c) 2022 Sam Blenny // SPDX-License-Identifier: Apache-2.0 OR MIT // +use blitstr2::ClipRect; + use super::GlyphSprite; use super::TypesetWord; -use super::cliprect::ClipRect; -use super::pt::Pt; +use crate::minigfx::Point; /// 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: isize, y: isize, 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, y }, 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, r.min.y), line_height: 0 } + } pub fn update_glyph(&mut self, glyph: &GlyphSprite) { self.pt.x += glyph.wide as isize; @@ -34,7 +39,7 @@ impl Cursor { 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), } } @@ -46,7 +51,7 @@ mod tests { #[test] fn test_cursor_equivalence() { - let c1 = Cursor { pt: Pt { x: 1, y: 2 }, line_height: 0 }; + let c1 = Cursor { pt: Point { x: 1, y: 2 }, line_height: 0 }; let c2 = Cursor::new(1, 2, 0); assert_eq!(c1, c2); let clip = ClipRect::new(1, 2, 3, 4); diff --git a/libs/ux-api/src/minigfx/glyphstyle.rs b/libs/ux-api/src/minigfx/glyphstyle.rs deleted file mode 100644 index 8263e9ca8..000000000 --- a/libs/ux-api/src/minigfx/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/libs/ux-api/src/minigfx/mod.rs b/libs/ux-api/src/minigfx/mod.rs index edc2c3422..b790cd655 100644 --- a/libs/ux-api/src/minigfx/mod.rs +++ b/libs/ux-api/src/minigfx/mod.rs @@ -8,18 +8,14 @@ pub mod style; pub use style::*; pub mod circle; pub use circle::*; -pub mod glyphstyle; -pub use glyphstyle::*; pub mod textview; pub use textview::*; -pub mod cliprect; -pub use cliprect::*; pub mod cursor; pub use cursor::*; -pub mod pt; -pub use pt::*; pub(crate) mod op; +use blitstr2::GlyphSprite; + /// Abstract trait for a FrameBuffer. Slower than native manipulation /// of the [u8] contents of a frame buffer, but more portable. pub trait FrameBuffer { @@ -35,35 +31,6 @@ pub trait FrameBuffer { fn dimensions(&self) -> Point; } -// 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. @@ -72,7 +39,7 @@ 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, + pub origin: Point, /// width of the word pub width: isize, /// overall height for the word diff --git a/libs/ux-api/src/minigfx/pt.rs b/libs/ux-api/src/minigfx/pt.rs deleted file mode 100644 index 9d9977427..000000000 --- a/libs/ux-api/src/minigfx/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: isize, - pub y: isize, -} - -impl Pt { - /// Make a new point - pub fn new(x: isize, y: isize) -> 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/libs/ux-api/src/minigfx/textview.rs b/libs/ux-api/src/minigfx/textview.rs index 745feb05d..4644d7506 100644 --- a/libs/ux-api/src/minigfx/textview.rs +++ b/libs/ux-api/src/minigfx/textview.rs @@ -1,6 +1,7 @@ use core::ops::Add; use String; +use blitstr2::GlyphStyle; use super::*; diff --git a/libs/ux-api/src/style_macros.rs b/libs/ux-api/src/style_macros.rs deleted file mode 100644 index 0ffad6517..000000000 --- a/libs/ux-api/src/style_macros.rs +++ /dev/null @@ -1,148 +0,0 @@ -// A set of macros that define the priority order for resolving fonts across language definitions. - -macro_rules! style_wrapper { - ($rule:ident, $base_style:ident, $ch:ident) => { - match &$base_style { - GlyphStyle::Small => { - $rule!(small_glyph, emoji_glyph, $ch) - } - GlyphStyle::Bold => { - $rule!(bold_glyph, emoji_glyph, $ch) - } - GlyphStyle::Monospace => { - $rule!(mono_glyph, emoji_glyph, $ch) - } - GlyphStyle::Large => { - $rule!(large_glyph, emoji_large_glyph, $ch) - } - GlyphStyle::ExtraLarge => { - $rule!(extra_large_glyph, emoji_large_glyph, $ch) - } - GlyphStyle::Tall => { - $rule!(tall_glyph, emoji_glyph, $ch) - } - // default to regular - _ => { - $rule!(regular_glyph, emoji_glyph, $ch) - } - } - }; -} - -macro_rules! zh_rules { - ($base_style:expr, $emoji_style:expr, $ch:ident) => { - match zh_glyph($ch) { - Ok(g) => g, - _ => match $base_style($ch) { - Ok(g) => g, - _ => match $emoji_style($ch) { - Ok(g) => g, - _ => match ja_glyph($ch) { - Ok(g) => g, - _ => match kr_glyph($ch) { - Ok(g) => g, - _ => match $base_style(REPLACEMENT) { - Ok(g) => g, - _ => NULL_GLYPH_SPRITE, - }, - }, - }, - }, - }, - } - }; -} - -macro_rules! jp_rules { - ($base_style:expr, $emoji_style:expr, $ch:ident) => { - match ja_glyph($ch) { - Ok(g) => g, - _ => match $base_style($ch) { - Ok(g) => g, - _ => match $emoji_style($ch) { - Ok(g) => g, - _ => match zh_glyph($ch) { - Ok(g) => g, - _ => match kr_glyph($ch) { - Ok(g) => g, - _ => match $base_style(REPLACEMENT) { - Ok(g) => g, - _ => NULL_GLYPH_SPRITE, - }, - }, - }, - }, - }, - } - }; -} - -macro_rules! kr_rules { - ($base_style:expr, $emoji_style:expr, $ch:ident) => { - match kr_glyph($ch) { - Ok(g) => g, - _ => match $base_style($ch) { - Ok(g) => g, - _ => match $emoji_style($ch) { - Ok(g) => g, - _ => match ja_glyph($ch) { - Ok(g) => g, - _ => match zh_glyph($ch) { - Ok(g) => g, - _ => match $base_style(REPLACEMENT) { - Ok(g) => g, - _ => NULL_GLYPH_SPRITE, - }, - }, - }, - }, - }, - } - }; -} - -macro_rules! en_audio_rules { - ($base_style:expr, $emoji_style:expr, $ch:ident) => { - match $base_style($ch) { - Ok(g) => g, - _ => match $emoji_style($ch) { - Ok(g) => g, - _ => match $base_style(REPLACEMENT) { - Ok(g) => g, - _ => NULL_GLYPH_SPRITE, - }, - }, - } - }; -} - -macro_rules! english_rules { - ($base_style:expr, $emoji_style:expr, $ch:ident) => { - match $base_style($ch) { - Ok(g) => g, - _ => match $emoji_style($ch) { - Ok(g) => g, - _ => match ja_glyph($ch) { - Ok(g) => g, - _ => match zh_glyph($ch) { - Ok(g) => g, - _ => match kr_glyph($ch) { - Ok(g) => g, - _ => match $base_style(REPLACEMENT) { - Ok(g) => g, - _ => NULL_GLYPH_SPRITE, - }, - }, - }, - }, - }, - } - }; -} - -pub(crate) use en_audio_rules; -pub(crate) use english_rules; -pub(crate) use jp_rules; -pub(crate) use kr_rules; -pub(crate) use style_wrapper; -pub(crate) use zh_rules; diff --git a/libs/ux-api/src/widgets/modal.rs b/libs/ux-api/src/widgets/modal.rs index 852f4cffb..96a08b2c2 100644 --- a/libs/ux-api/src/widgets/modal.rs +++ b/libs/ux-api/src/widgets/modal.rs @@ -1,9 +1,9 @@ use std::fmt::Write; +use blitstr2::{GlyphStyle, glyph_height_hint}; use xous_ipc::Buffer; use super::*; -use crate::blitstr2::glyph_height_hint; use crate::minigfx::*; use crate::platform::{LINES, WIDTH}; diff --git a/libs/ux-api/src/wordwrap.rs b/libs/ux-api/src/wordwrap.rs index 11f4b93c0..fa1c7f026 100644 --- a/libs/ux-api/src/wordwrap.rs +++ b/libs/ux-api/src/wordwrap.rs @@ -24,13 +24,34 @@ /// /// 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 blitstr2::*; + use crate::minigfx::*; use crate::platform::FB_SIZE; -use crate::style_macros::*; + +/// 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: isize, + /// overall height for the word + pub height: isize, + /// 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, @@ -41,7 +62,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, @@ -91,7 +112,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, @@ -142,27 +163,15 @@ impl ComposedType { if glyph.large { blitstr2::xor_glyph_large( frbuf, - &Point::new(maybe_x, maybe_y), + (maybe_x, maybe_y), *glyph, glyph.invert ^ invert, cr, ); } else if !glyph.double { - blitstr2::xor_glyph( - frbuf, - &Point::new(maybe_x, maybe_y), - *glyph, - glyph.invert ^ invert, - cr, - ); + blitstr2::xor_glyph(frbuf, (maybe_x, maybe_y), *glyph, glyph.invert ^ invert, cr); } else { - blitstr2::xor_glyph_2x( - frbuf, - &Point::new(maybe_x, maybe_y), - *glyph, - glyph.invert ^ invert, - cr, - ); + blitstr2::xor_glyph_2x(frbuf, (maybe_x, maybe_y), *glyph, glyph.invert ^ invert, cr); } if glyph.insert { // log::info!("insert at {},{}", glyph.ch, strpos - 1); @@ -207,7 +216,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. */ @@ -224,7 +233,7 @@ 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 { + pub fn setup(s: &str, extent: &Point, base_style: &GlyphStyle, insertion_point: Option) -> Self { let bb = ClipRect::new(0, 0, extent.x, extent.y); let mut space = style_glyph(' ', base_style); space.kern = 0; @@ -245,8 +254,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,