From d26c80582e8924ed3c8e163ac17f142ee2910f08 Mon Sep 17 00:00:00 2001 From: bunnie Date: Mon, 23 Nov 2020 15:42:58 +0800 Subject: [PATCH] clean up gfx server API, allow for string size setting --- services/graphics-server/src/api.rs | 51 +++++++++++++++++++++++---- services/graphics-server/src/fonts.rs | 8 +---- services/graphics-server/src/lib.rs | 15 +++++++- services/graphics-server/src/main.rs | 18 +++++++++- services/graphics-server/src/op.rs | 8 ++--- services/shell/src/main.rs | 7 ++-- 6 files changed, 85 insertions(+), 22 deletions(-) diff --git a/services/graphics-server/src/api.rs b/services/graphics-server/src/api.rs index 3af44f5de..400d41259 100644 --- a/services/graphics-server/src/api.rs +++ b/services/graphics-server/src/api.rs @@ -2,6 +2,39 @@ use crate::op::PixelColor; use xous::{Message, ScalarMessage}; use core::ops::{Add, AddAssign, Index, Neg, Sub, SubAssign}; + +/// Available typeface glyph sets +#[derive(Debug, Copy, Clone)] +pub enum GlyphSet { + Bold, + Regular, + Small, +} + +pub fn glyph_to_arg(glyph: GlyphSet) -> usize { + match glyph { + GlyphSet::Small => 0, + GlyphSet::Regular => 1, + GlyphSet::Bold => 2, + } +} + +pub fn arg_to_glyph(arg: usize) -> GlyphSet { + match arg { + 0 => GlyphSet::Small, + 1 => GlyphSet::Regular, + 2 => GlyphSet::Bold, + _ => GlyphSet::Regular, + } +} + +pub fn glyph_to_height(glyph: GlyphSet) -> usize { + match glyph { + GlyphSet::Small => crate::fonts::small::MAX_HEIGHT as usize, + GlyphSet::Regular => crate::fonts::regular::MAX_HEIGHT as usize, + GlyphSet::Bold => crate::fonts::bold::MAX_HEIGHT as usize, + } +} /// 2D size. /// /// `Size` is used to define the width and height of an object. @@ -583,11 +616,17 @@ pub enum Opcode<'a> { /// Clear the specified region ClearRegion(Rect), + /// Set the current string glyph set for strings + SetGlyph(GlyphSet), + /// Render the string at the (x,y) coordinates String(&'a str), /// Retrieve the X and Y dimensions of the screen ScreenSize, + + /// Retrieve the current Glyph characteristics + QueryGlyph, } impl<'a> core::convert::TryFrom<&'a Message> for Opcode<'a> { @@ -611,10 +650,12 @@ impl<'a> core::convert::TryFrom<&'a Message> for Opcode<'a> { m.arg3 as _, m.arg4 as _, ))), + 9 => Ok(Opcode::SetGlyph(arg_to_glyph(m.arg1))), _ => Err("unrecognized opcode"), }, Message::BlockingScalar(m) => match m.id { 8 => Ok(Opcode::ScreenSize), + 10 => Ok(Opcode::QueryGlyph), _ => Err("unrecognized opcode"), }, Message::Borrow(m) => match m.id { @@ -688,13 +729,9 @@ impl<'a> Into for Opcode<'a> { arg3: rect.x1 as _, arg4: rect.y1 as _, }), - Opcode::ScreenSize => Message::BlockingScalar(ScalarMessage { - id: 8, - arg1: 0, - arg2: 0, - arg3: 0, - arg4: 0, - }), + Opcode::ScreenSize => Message::BlockingScalar(ScalarMessage {id: 8, arg1: 0, arg2: 0, arg3: 0, arg4: 0}), + Opcode::QueryGlyph => Message::BlockingScalar(ScalarMessage {id: 10, arg1: 0, arg2: 0, arg3: 0, arg4: 0}), + Opcode::SetGlyph(glyph) => Message::Scalar(ScalarMessage { id:9, arg1: glyph_to_arg(glyph), arg2: 0, arg3: 0, arg4: 0 }), Opcode::String(string) => { let region = xous::carton::Carton::from_bytes(string.as_bytes()); Message::Borrow(region.into_message(1)) diff --git a/services/graphics-server/src/fonts.rs b/services/graphics-server/src/fonts.rs index 5ddbaf17a..b3ce64ab2 100644 --- a/services/graphics-server/src/fonts.rs +++ b/services/graphics-server/src/fonts.rs @@ -1,6 +1,7 @@ pub mod bold; pub mod regular; pub mod small; +use crate::api::GlyphSet; /// Strings with Unicode Private Use Area characters for UI Sprites pub mod pua { @@ -35,13 +36,6 @@ impl GlyphHeader { } } -/// Available typeface glyph sets -pub enum GlyphSet { - Bold, - Regular, - Small, -} - /// Abstraction for working with typeface glyph sets #[derive(Copy, Clone)] pub struct Font { diff --git a/services/graphics-server/src/lib.rs b/services/graphics-server/src/lib.rs index 9f59c55ff..4cad0dcb6 100644 --- a/services/graphics-server/src/lib.rs +++ b/services/graphics-server/src/lib.rs @@ -2,7 +2,7 @@ // pub mod size; pub mod api; -pub use api::{Point, Color, Rect}; +pub use api::{Point, Color, Rect, GlyphSet}; use xous::String; pub mod op; pub mod fonts; @@ -37,6 +37,10 @@ pub fn draw_string(cid: CID, s: &String) -> Result<(), xous::Error> { s.lend(cid, 1).map(|_| ()) } +pub fn set_glyph(cid: CID, glyph: GlyphSet) -> Result<(), xous::Error> { + send_message(cid, api::Opcode::SetGlyph(glyph).into()).map( |_| ()) +} + pub fn screen_size(cid: CID) -> Result { let response = send_message(cid, api::Opcode::ScreenSize.into())?; if let xous::Result::Scalar2(x, y) = response { @@ -44,4 +48,13 @@ pub fn screen_size(cid: CID) -> Result { } else { panic!("unexpected return value: {:#?}", response); } +} + +pub fn query_glyph(cid: CID) -> Result<(GlyphSet, usize), xous::Error> { + let response = send_message(cid, api::Opcode::QueryGlyph.into())?; + if let xous::Result::Scalar2(glyph, h) = response { + Ok((api::arg_to_glyph(glyph), h)) + } else { + panic!("unexpected return value: {:#?}", response); + } } \ No newline at end of file diff --git a/services/graphics-server/src/main.rs b/services/graphics-server/src/main.rs index 66c4b7665..59a373ccc 100644 --- a/services/graphics-server/src/main.rs +++ b/services/graphics-server/src/main.rs @@ -34,6 +34,7 @@ fn xmain() -> ! { draw_boot_logo(&mut display); let mut current_color = api::Color::from(0usize); + let mut current_glyph = api::GlyphSet::Regular; display.redraw(); @@ -92,7 +93,14 @@ fn xmain() -> ! { }); } Opcode::String(s) => { - op::string_regular_left(display.native_buffer(), op::ClipRegion::screen(), s); + match current_glyph { + api::GlyphSet::Small => op::string_small_left(display.native_buffer(), op::ClipRegion::screen(), s), + api::GlyphSet::Regular => op::string_regular_left(display.native_buffer(), op::ClipRegion::screen(), s), + api::GlyphSet::Bold => op::string_bold_left(display.native_buffer(), op::ClipRegion::screen(), s), + } + } + Opcode::SetGlyph(glyph) => { + current_glyph = glyph; } Opcode::ScreenSize => { xous::return_scalar2( @@ -102,6 +110,14 @@ fn xmain() -> ! { ) .expect("GFX: couldn't return ScreenSize request"); } + Opcode::QueryGlyph => { + xous::return_scalar2( + msg.sender, + api::glyph_to_arg(current_glyph), + api::glyph_to_height(current_glyph), + ) + .expect("GFX: could not return QueryGlyph request"); + } } } else { // info!("Couldn't convert opcode"); diff --git a/services/graphics-server/src/op.rs b/services/graphics-server/src/op.rs index 2fac527fb..497b38f0b 100644 --- a/services/graphics-server/src/op.rs +++ b/services/graphics-server/src/op.rs @@ -1,6 +1,6 @@ use super::fonts; use super::fonts::{Font, GlyphHeader}; -use crate::api::{Point, Style, Pixel}; +use crate::api::{Point, Style, Pixel, GlyphSet}; /// LCD Frame buffer bounds pub const LCD_WORDS_PER_LINE: usize = 11; @@ -51,7 +51,7 @@ impl ClipRegion { /// Blit string with: XOR, bold font, align xr left yr top pub fn string_bold_left(fb: &mut LcdFB, mut cr: ClipRegion, s: &str) { - let f = Font::new(fonts::GlyphSet::Bold); + let f = Font::new(GlyphSet::Bold); for c in s.chars() { cr.x0 += xor_char(fb, cr, c, f); } @@ -59,7 +59,7 @@ pub fn string_bold_left(fb: &mut LcdFB, mut cr: ClipRegion, s: &str) { /// Blit string with: XOR, regular font, align xr left yr top pub fn string_regular_left(fb: &mut LcdFB, mut cr: ClipRegion, s: &str) { - let f = Font::new(fonts::GlyphSet::Regular); + let f = Font::new(GlyphSet::Regular); for c in s.chars() { cr.x0 += xor_char(fb, cr, c, f); } @@ -67,7 +67,7 @@ pub fn string_regular_left(fb: &mut LcdFB, mut cr: ClipRegion, s: &str) { /// Blit string with: XOR, small font, align xr left yr top pub fn string_small_left(fb: &mut LcdFB, mut cr: ClipRegion, s: &str) { - let f = Font::new(fonts::GlyphSet::Small); + let f = Font::new(GlyphSet::Small); for c in s.chars() { cr.x0 += xor_char(fb, cr, c, f); } diff --git a/services/shell/src/main.rs b/services/shell/src/main.rs index 1f57c0974..9ff50c855 100644 --- a/services/shell/src/main.rs +++ b/services/shell/src/main.rs @@ -11,6 +11,7 @@ use core::fmt::Write; use log::{error, info}; use xous::String; use graphics_server::Point; +use graphics_server::GlyphSet; // fn print_and_yield(index: *mut usize) -> ! { // let num = index as usize; @@ -223,8 +224,10 @@ fn shell_main() -> ! { } string_buffer.clear(); - write!(&mut string_buffer, "Uptime: {:2}s", last_time/1000).expect("Can't write"); - graphics_server::clear_region(graphics_conn, 0, 0, screensize.x as usize - 1, 40) + write!(&mut string_buffer, "Uptime: {:.2}s", last_time as f32 / 1000f32).expect("Can't write"); + graphics_server::set_glyph(graphics_conn, GlyphSet::Small).expect("unable to set glyph"); + let (_, h) = graphics_server::query_glyph(graphics_conn).expect("unable to query glyph"); + graphics_server::clear_region(graphics_conn, 0, 0, screensize.x as usize - 1, h) .expect("unable to clear region"); info!("drawing string: {}", string_buffer); graphics_server::draw_string(graphics_conn, &string_buffer).expect("unable to draw string");