add missing apis/derives to minigfx to make it precursor-compatible

This commit is contained in:
bunnie 2025-02-21 17:43:50 +08:00
parent 07563c44db
commit 30c5876ad4
9 changed files with 373 additions and 29 deletions

View file

@ -0,0 +1,43 @@
use crate::minigfx::*;
#[cfg_attr(feature = "derive-rkyv", derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize))]
#[derive(Debug, Copy, Clone)]
pub enum ClipObjectType {
Line(Line),
Circ(Circle),
Rect(Rectangle),
RoundRect(RoundedRectangle),
XorLine(Line),
#[cfg(feature = "ditherpunk")]
Tile(Tile),
}
#[cfg_attr(feature = "derive-rkyv", derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize))]
#[derive(Debug, Copy, Clone)]
pub struct ClipObject {
pub clip: Rectangle,
pub obj: ClipObjectType,
}
#[cfg_attr(feature = "derive-rkyv", derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize))]
#[derive(Debug, Copy, Clone)]
pub struct ClipObjectList {
// ClipObject is 28 bytes, so 32 of these takes 896 bytes, which is less than a 4k page (the minimum
// amount that gets remapped) we limit the length to 32 so we can use the Default initializer to set
// the None's on the array, otherwise it gets a bit painful.
pub list: [Option<ClipObject>; 32],
free: usize,
}
impl ClipObjectList {
pub fn default() -> ClipObjectList { ClipObjectList { list: Default::default(), free: 0 } }
pub fn push(&mut self, item: ClipObjectType, clip: Rectangle) -> Result<(), ClipObjectType> {
if self.free < self.list.len() {
self.list[self.free] = Some(ClipObject { clip, obj: item });
self.free += 1;
Ok(())
} else {
Err(item)
}
}
}

View file

@ -1,6 +1,7 @@
use crate::minigfx::*;
#[derive(Debug, Clone, Copy)]
#[cfg_attr(feature = "derive-rkyv", derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize))]
#[derive(Debug, Copy, Clone)]
pub struct Line {
pub start: Point,
pub end: Point,

View file

@ -8,6 +8,12 @@ pub mod style;
pub use style::*;
pub mod circle;
pub use circle::*;
pub mod clip;
pub use clip::*;
#[cfg(feature = "ditherpunk")]
pub mod tile;
#[cfg(feature = "ditherpunk")]
pub use tile::*;
#[cfg(feature = "std")]
pub mod textview;
@ -18,7 +24,7 @@ pub mod cursor;
#[cfg(feature = "std")]
pub use cursor::*;
#[cfg(feature = "std")]
pub(crate) mod op;
pub mod op;
#[cfg(feature = "std")]
use blitstr2::GlyphSprite;
@ -43,7 +49,7 @@ pub trait FrameBuffer {
/// a rasterizer for rendering.
#[derive(Debug)]
#[cfg(feature = "std")]
pub(crate) struct TypesetWord {
pub struct TypesetWord {
/// glyph data to directly render the word
pub gs: Vec<GlyphSprite>,
/// top left origin point for rendering of the glyphs

View file

@ -1,16 +1,16 @@
use super::*;
/// LCD Frame buffer bounds
pub const LCD_WORDS_PER_LINE: usize = crate::platform::WIDTH as usize / core::mem::size_of::<u32>();
pub const LCD_WORDS_PER_LINE: usize = crate::platform::FB_WIDTH_WORDS;
pub const LCD_PX_PER_LINE: usize = crate::platform::WIDTH as usize;
pub const LCD_LINES: usize = crate::platform::LINES as usize;
pub const LCD_FRAME_BUF_SIZE: usize = LCD_WORDS_PER_LINE * LCD_LINES;
pub const WIDTH: isize = crate::platform::WIDTH;
pub const HEIGHT: isize = crate::platform::LINES;
pub const WIDTH: isize = crate::platform::WIDTH as isize;
pub const HEIGHT: isize = crate::platform::LINES as isize;
/// For passing frame buffer references
pub type LcdFB = [u32; LCD_FRAME_BUF_SIZE];
pub type LcdFB = [u32];
/// Set the expected rectangle length for the busy animation
pub const BUSY_ANIMATION_RECT_WIDTH: isize = 32; // golden ratio off of a height of 20
@ -21,9 +21,9 @@ fn put_pixel(fb: &mut LcdFB, x: isize, y: isize, color: ColorNative) {
clip_y = LCD_LINES - 1;
}
let clip_x: usize = x as usize;
let mut clip_x: usize = x as usize;
if clip_x >= LCD_PX_PER_LINE {
clip_y = LCD_PX_PER_LINE - 1;
clip_x = LCD_PX_PER_LINE - 1;
}
let pc = PixelColor::from(color.0);
@ -43,9 +43,9 @@ fn xor_pixel(fb: &mut LcdFB, x: isize, y: isize) {
clip_y = LCD_LINES - 1;
}
let clip_x: usize = x as usize;
let mut clip_x: usize = x as usize;
if clip_x >= LCD_PX_PER_LINE {
clip_y = LCD_PX_PER_LINE - 1;
clip_x = LCD_PX_PER_LINE - 1;
}
fb[(clip_x + clip_y * LCD_WORDS_PER_LINE * 32) / 32] ^= 1 << (clip_x % 32);
@ -56,7 +56,7 @@ fn xor_pixel(fb: &mut LcdFB, x: isize, y: isize) {
pub fn line(fb: &mut LcdFB, l: Line, clip: Option<Rectangle>, xor: bool) {
let color: ColorNative;
if l.style.stroke_color.is_some() {
color = l.style.stroke_color.unwrap();
color = ColorNative::from(l.style.stroke_color.unwrap());
} else {
return;
}
@ -139,9 +139,15 @@ impl Iterator for CircleIterator {
let is_fill = len <= outer_radius_sq + 1;
item = if is_border && self.style.stroke_color.is_some() {
Some(Pixel(self.center + t, self.style.stroke_color.expect("Border color not defined")))
Some(Pixel(
self.center + t,
self.style.stroke_color.expect("Border color not defined").into(),
))
} else if is_fill && self.style.fill_color.is_some() {
Some(Pixel(self.center + t, self.style.fill_color.expect("Fill color not defined")))
Some(Pixel(
self.center + t,
self.style.fill_color.expect("Fill color not defined").into(),
))
} else {
None
};
@ -227,11 +233,11 @@ impl Iterator for RectangleIterator {
|| (self.p.x <= br.x && self.p.x > br.x - border_width)
) && self.style.stroke_color.is_some()
{
out = Some(Pixel(self.p, self.style.stroke_color.expect("Expected stroke")));
out = Some(Pixel(self.p, self.style.stroke_color.expect("Expected stroke").into()));
}
// Fill
else if let Some(fill) = self.style.fill_color {
out = Some(Pixel(self.p, fill));
out = Some(Pixel(self.p, fill.into()));
}
}
@ -329,9 +335,15 @@ impl Iterator for QuadrantIterator {
let is_fill = len <= outer_radius_sq + 1;
item = if is_border && self.style.stroke_color.is_some() {
Some(Pixel(self.center + t, self.style.stroke_color.expect("Border color not defined")))
Some(Pixel(
self.center + t,
self.style.stroke_color.expect("Border color not defined").into(),
))
} else if is_fill && self.style.fill_color.is_some() {
Some(Pixel(self.center + t, self.style.fill_color.expect("Fill color not defined")))
Some(Pixel(
self.center + t,
self.style.fill_color.expect("Fill color not defined").into(),
))
} else {
None
};
@ -473,11 +485,11 @@ impl Iterator for RoundedRectangleIterator {
|| (self.p.x <= br.x && self.p.x > br.x - border_width)
) && self.style.stroke_color.is_some()
{
out = Some(Pixel(self.p, self.style.stroke_color.expect("Expected stroke")));
out = Some(Pixel(self.p, self.style.stroke_color.expect("Expected stroke").into()));
}
// Fill
else if let Some(fill) = self.style.fill_color {
out = Some(Pixel(self.p, fill));
out = Some(Pixel(self.p, fill.into()));
}
}
}
@ -565,8 +577,6 @@ pub fn rounded_rectangle(fb: &mut LcdFB, rr: RoundedRectangle, clip: Option<Rect
* author: nworbnhoj
*/
#[cfg(feature = "ditherpunk")]
use crate::api::Tile;
#[cfg(feature = "ditherpunk")]
pub fn tile(fb: &mut LcdFB, tile: Tile, clip: Option<Rectangle>) {
use std::cmp::{max, min};
let fb_size: Point = Point::new((LCD_PX_PER_LINE - 1) as isize, (LCD_LINES - 1) as isize);

View file

@ -85,3 +85,53 @@ impl Neg for Point {
fn neg(self) -> Self::Output { Point::new(-self.x, -self.y) }
}
impl From<(i16, i16)> for Point {
fn from(other: (i16, i16)) -> Self { Point::new(other.0 as isize, other.1 as isize) }
}
impl From<[i16; 2]> for Point {
fn from(other: [i16; 2]) -> Self { Point::new(other[0] as isize, other[1] as isize) }
}
impl From<&[i16; 2]> for Point {
fn from(other: &[i16; 2]) -> Self { Point::new(other[0] as isize, other[1] as isize) }
}
impl From<(isize, isize)> for Point {
fn from(other: (isize, isize)) -> Self { Point::new(other.0 as isize, other.1 as isize) }
}
impl From<[isize; 2]> for Point {
fn from(other: [isize; 2]) -> Self { Point::new(other[0] as isize, other[1] as isize) }
}
impl From<&[isize; 2]> for Point {
fn from(other: &[isize; 2]) -> Self { Point::new(other[0] as isize, other[1] as isize) }
}
impl From<Point> for (i16, i16) {
fn from(other: Point) -> (i16, i16) { (other.x as i16, other.y as i16) }
}
impl From<&Point> for (i16, i16) {
fn from(other: &Point) -> (i16, i16) { (other.x as i16, other.y as i16) }
}
impl From<Point> for (isize, isize) {
fn from(other: Point) -> (isize, isize) { (other.x, other.y) }
}
impl From<&Point> for (isize, isize) {
fn from(other: &Point) -> (isize, isize) { (other.x as isize, other.y as isize) }
}
/// Warning: bounds check on x/y is not implemented to improve performance
impl From<Point> for usize {
fn from(point: Point) -> usize { (point.x as usize) << 16 | (point.y as usize) }
}
/// Warning: bounds check on x/y is not implemented to improve performance
impl From<usize> for Point {
fn from(p: usize) -> Point { Point { x: (p >> 16 & 0xffff) as _, y: (p & 0xffff) as _ } }
}

View file

@ -191,6 +191,25 @@ impl Rectangle {
self.br.x += margin.x;
self.br.y += margin.y;
}
/// Make a rectangle of the full screen size
pub fn full_screen() -> Rectangle {
Rectangle {
tl: Point::new(0, 0),
br: Point::new(crate::platform::WIDTH as isize, crate::platform::HEIGHT as isize),
style: DrawStyle::default(),
}
}
/// Make a rectangle of the screen size minus padding
pub fn padded_screen() -> Rectangle {
let pad = 6;
Rectangle {
tl: Point::new(pad, pad),
br: Point::new(crate::platform::WIDTH as isize - pad, crate::platform::HEIGHT as isize - pad),
style: DrawStyle::default(),
}
}
}
//////////////////////// Rounded Rectangle

View file

@ -10,35 +10,84 @@ impl From<usize> for ColorNative {
impl Into<usize> for ColorNative {
fn into(self) -> usize { self.0 }
}
impl From<PixelColor> for ColorNative {
fn from(value: PixelColor) -> Self { Self { 0: value.into() } }
}
impl Into<PixelColor> for ColorNative {
fn into(self) -> PixelColor { PixelColor::from(self.0) }
}
/// Style properties for an object
#[cfg_attr(feature = "derive-rkyv", derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize))]
#[derive(Debug, Copy, Clone)]
pub struct DrawStyle {
/// Fill colour of the object
pub fill_color: Option<ColorNative>,
pub fill_color: Option<PixelColor>,
/// Stroke (border/line) color of the object
pub stroke_color: Option<ColorNative>,
pub stroke_color: Option<PixelColor>,
/// Stroke width
pub stroke_width: isize,
}
impl DrawStyle {
pub fn new(fill: ColorNative, stroke: ColorNative, width: isize) -> Self {
pub fn new(fill: PixelColor, stroke: PixelColor, width: isize) -> Self {
Self { fill_color: Some(fill), stroke_color: Some(stroke), stroke_width: width }
}
/// Create a new style with a given stroke value and defaults for everything else
pub fn stroke_color(stroke_color: ColorNative) -> Self {
pub fn stroke_color(stroke_color: PixelColor) -> Self {
Self { stroke_color: Some(stroke_color), ..DrawStyle::default() }
}
}
impl Default for DrawStyle {
fn default() -> Self {
Self { fill_color: Some(ColorNative(0)), stroke_color: Some(ColorNative(0)), stroke_width: 1 }
Self { fill_color: Some(PixelColor::Dark), stroke_color: Some(PixelColor::Dark), stroke_width: 1 }
}
}
impl From<usize> for DrawStyle {
fn from(s: usize) -> Self {
// usize split into these words:
// 31 ... 16 15 ... 4 3..2 1..0
// width rsvd stroke fill
// where the MSB of stroke/fill encodes Some/None
let fc: ColorNative = (s & 0b00_01).into();
let sc: ColorNative = (s & 0b01_00).into();
DrawStyle {
fill_color: if s & 0b00_10 != 0 { Some(PixelColor::from(fc.0)) } else { None },
stroke_color: if s & 0b10_00 != 0 { Some(PixelColor::from(sc.0)) } else { None },
stroke_width: (s >> 16) as isize,
}
}
}
impl From<DrawStyle> for usize {
fn from(draw_style: DrawStyle) -> usize {
let sc: usize;
if draw_style.stroke_color.is_some() {
if draw_style.stroke_color.unwrap() == PixelColor::Dark {
sc = 0b11;
} else {
sc = 0b10;
}
} else {
sc = 0;
}
let fc: usize;
if draw_style.fill_color.is_some() {
if draw_style.fill_color.unwrap() == PixelColor::Dark {
fc = 0b11;
} else {
fc = 0b10;
}
// fc = 0b10 | self.fill_color.unwrap() as usize; // this isn't working for some reason
} else {
fc = 0;
}
(draw_style.stroke_width as usize) << 16 | sc << 2 | fc
}
}

View file

@ -3,6 +3,7 @@ use core::ops::Add;
use blitstr2::GlyphStyle;
use super::*;
use crate::service::api::Gid;
/// coordinates are local to the canvas, not absolute to the screen
#[cfg_attr(feature = "derive-rkyv", derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize))]
@ -84,8 +85,9 @@ pub struct TextView {
// graphics-server only the GAM should be sending TextViews to the graphics-server, and a different
// coding scheme is used for that link.
operation: TextOp,
canvas: Gid, // GID of the canvas to draw on
pub clip_rect: Option<Rectangle>, /* this is set by the GAM to the canvas' clip_rect; needed by gfx
* for drawing. Note this is in screen coordinates. */
* for drawing. Note this is in screen coordinates. */
pub untrusted: bool, // render content with random stipples to indicate the strings within are untrusted
pub token: Option<[u32; 4]>, // optional 128-bit token which is presented to prove a field's trustability
@ -121,8 +123,9 @@ pub struct TextView {
pub text: String,
}
impl TextView {
pub fn new(bounds_hint: TextBounds) -> Self {
pub fn new(canvas: Gid, bounds_hint: TextBounds) -> Self {
TextView {
canvas,
operation: TextOp::Nop,
untrusted: true,
token: None,
@ -155,11 +158,14 @@ impl TextView {
pub fn get_op(&self) -> TextOp { self.operation }
pub fn get_canvas_gid(&self) -> Gid { self.canvas }
pub fn to_str(&self) -> &str { self.text.as_str() }
pub fn clear_str(&mut self) { self.text.clear() }
pub fn populate_from(&mut self, t: &TextView) {
self.canvas = t.canvas;
self.operation = t.operation;
self.untrusted = t.untrusted;
self.token = t.token;

View file

@ -0,0 +1,160 @@
use std::cmp::max;
use std::convert::TryInto;
use super::*;
//////////////////////// Tile -------- author: nworbnhoj
/*
* A Tile contains an Array of u32 Words, a bounding Rectangle, and a Word width.
* This is arranged to come in just under 4096 bytes, allowing for the rkyv overhead.
* Each line of bits across the Tile is packed into an Integer number of u32 Words.
* Hence, there may be unused bits in the right-most Word in each line, and a few
* unused Words at the end of the Array. This arrangement is similar to the structure
* of the frame-buffer in the graphics-server to facilitate efficient transfer.
*/
pub type Word = u32;
pub const BITS_PER_WORD: usize = Word::BITS as usize;
pub(crate) const META_WORDS_PER_TILE: usize = 4;
pub const WORDS_PER_TILE: usize = (4096 * 8 / BITS_PER_WORD) - META_WORDS_PER_TILE;
pub const BITS_PER_TILE: usize = WORDS_PER_TILE * BITS_PER_WORD;
pub(crate) const OUT_OF_BOUNDS: usize = usize::MAX;
#[derive(Debug, Clone, Copy, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)]
pub struct Tile {
bound: Rectangle,
width_words: isize,
max_bound: Rectangle,
words: [Word; WORDS_PER_TILE],
}
impl Tile {
pub fn new(mut bound: Rectangle) -> Self {
log::trace!("new Tile {:?}", bound);
let bits_per_word = BITS_PER_WORD as isize;
let bits_per_tile = BITS_PER_TILE as isize;
let words_per_tile = WORDS_PER_TILE as isize;
let width_bits = bound.br.x - bound.tl.x + 1;
// establish width limits
let width_words = if width_bits > bits_per_tile {
log::warn!("Tile max width exceeded");
bound.br.x = bound.tl.x + words_per_tile * bits_per_word - 1;
words_per_tile
} else {
match width_bits % bits_per_word {
0 => width_bits / bits_per_word,
_ => width_bits / bits_per_word + 1,
}
};
let max_x = bound.tl.x + width_words * bits_per_word - 1;
// establish height limits
let max_height_bits = words_per_tile / width_words;
let max_y = bound.tl.y + max_height_bits - 1;
if bound.br.y > max_y {
log::warn!("Tile max height exceeded {} > {}", max_y, bound.br.y);
bound.br.y = max_y;
}
Self {
bound,
width_words,
max_bound: Rectangle::new(bound.tl, Point::new(max_x, max_y)),
words: [0u32; WORDS_PER_TILE],
}
}
pub fn bound(&self) -> Rectangle { self.bound }
pub fn set_bound(&mut self, bound: Rectangle) { self.bound = bound }
pub fn max_bound(&self) -> Rectangle { self.max_bound }
pub fn area(&self) -> u32 {
let size = self.size();
(size.x * size.y).try_into().unwrap()
}
pub fn size(&self) -> Point {
let pos = self.bound;
Point::new(pos.br.x - pos.tl.x, pos.br.y - pos.tl.y)
}
fn word_index_mut(&mut self, point: Point) -> usize {
if !self.bound.intersects_point(point) && self.max_bound.intersects_point(point) {
self.bound.br.x = max(point.x, self.bound.br.x);
self.bound.br.y = max(point.y, self.bound.br.y);
}
self.word_index(point)
}
fn word_index(&self, point: Point) -> usize {
if self.bound.intersects_point(point) {
let tile_line: isize = (point.y - self.bound.tl.y).try_into().unwrap();
let first_word_in_line: usize = (tile_line * self.width_words).try_into().unwrap();
let bit_index: usize = (point.x - self.bound.tl.x).try_into().unwrap();
first_word_in_line + (bit_index / BITS_PER_WORD)
} else {
log::warn!("point {:?} out of bounds {:?}", point, self.bound);
OUT_OF_BOUNDS
}
}
pub fn get_word(&self, point: Point) -> Word { self.words[self.word_index(point)] }
/// Adding a word outside of bound (but within max_bound) will expand bound.
pub fn set_word(&mut self, point: Point, word: Word) { self.words[self.word_index_mut(point)] = word; }
pub fn get_line(&self, point: Point) -> Vec<Word> {
let first_pixel_in_line = Point::new(self.bound.tl.x, point.y);
let first_word = self.word_index(first_pixel_in_line);
let width: usize = self.width_words.try_into().unwrap();
match first_word == OUT_OF_BOUNDS {
true => Vec::new(),
false => {
let last_word = first_word + width;
self.words[first_word..last_word].to_vec()
}
}
}
/// Adding a line outside of bound (but within max_bound) will expant bound.
pub fn set_line(&self, _point: Point, _pixels: Vec<PixelColor>) {
log::warn!("not implemented");
}
pub fn get_pixel(&self, point: Point) -> PixelColor {
let word: usize = self.get_word(point).try_into().unwrap();
let bpw: isize = BITS_PER_WORD.try_into().unwrap();
let bit = point.x % bpw;
PixelColor::from((word >> bit) & 1)
}
/// Adding a pixel outside of bound (but within max_bound) will expand bound.
pub fn set_pixel(&mut self, point: Point, color: PixelColor) {
let word_index = self.word_index_mut(point);
let bpw: isize = BITS_PER_WORD.try_into().unwrap();
match word_index == OUT_OF_BOUNDS {
true => {}
false => {
let word = self.words[word_index];
let bit = point.x % bpw;
match color {
PixelColor::Dark => self.words[word_index] = word | 1 << bit,
PixelColor::Light => self.words[word_index] = word & !(1 << bit),
}
}
};
}
pub fn translate(&mut self, offset: Point) {
self.bound.tl.x += offset.x;
self.bound.tl.y += offset.y;
self.bound.br.x += offset.x;
self.bound.br.y += offset.y;
let max_x = self.bound.tl.x + self.width_words * BITS_PER_WORD as isize - 1;
let max_y = self.bound.tl.y + WORDS_PER_TILE as isize / self.width_words - 1;
self.max_bound = Rectangle::new(self.bound.tl, Point::new(max_x, max_y));
}
pub fn crop(&mut self, bound: Rectangle) { self.bound = bound; }
}