move default system font to gam/src/lib.rs SYSTEM_STYLE

default system font can be statically changed by modifying
the SYSTEM_STYLE constant.

This of course will lead to a request for a dynamically
configurable preference for font size. However, early
on it was decided this is a bad idea. Here's why.

We don't provide for dynamic system-wide font selection
because this adds a lot of complexity to what would otherwise
be static code: this is a deliberate choice to improve code
density and performance. If the system font were dynamically
adjustable, then every character rendered we would potentially
have to query the PDDB for the current system font setting.

The impact of this could be mitigated by caching values but
then what happens when you actually change the font? now
you need a registry to fire messages off to all dependent
crates with cached values to flush their caches and redraw
(or you punt and force a reboot if you change font sizes,
but still, you're creating a dependency on a lot of plumbing
for even the simplest crates).

Plus, with a static font selection, the compiler can take constants
like the font height and compose them with other fitting
parameters into optimized load/stores of borders and spacings
for the static case; if you make it dynamic, all those multiplies,
adds and divides now get pulled into the main rendering loop
and are recomputed all the time. For example, if you have a static
const list of items that need to be rendered, even if the code to
compute the height of the list is written as a for loop, the compiler
will know the list is constant, and the font height is constant,
and it can condense that all into a single number at compile time,
eliminating all the code for the for loop.

If you leave the option open to change that, that entire piece
of code has to be generated and executed even though in practice
nobody changes the parameter.
This commit is contained in:
bunnie 2023-07-05 13:39:11 +08:00
parent 30c752db75
commit dfffa48715
7 changed files with 15 additions and 12 deletions

View file

@ -70,13 +70,14 @@ pub struct VaultUx {
}
pub const DEFAULT_FONT: GlyphStyle = GlyphStyle::Regular;
pub const FONT_LIST: [&'static str; 6] = [
"regular", "mono", "cjk",
pub const FONT_LIST: [&'static str; 7] = [
"regular", "tall", "mono", "cjk",
"bold", "large", "small"
];
pub fn name_to_style(name: &str) -> Option<GlyphStyle> {
match name {
"regular" => Some(GlyphStyle::Regular),
"tall" => Some(GlyphStyle::Tall),
"mono" => Some(GlyphStyle::Monospace),
"cjk" => Some(GlyphStyle::Cjk),
"bold" => Some(GlyphStyle::Bold),
@ -93,6 +94,7 @@ fn style_to_name(style: &GlyphStyle) -> String {
GlyphStyle::Bold => "bold".to_string(),
GlyphStyle::Large => "large".to_string(),
GlyphStyle::Small => "small".to_string(),
GlyphStyle::Tall => "tall".to_string(),
_ => "regular".to_string(),
}
}

View file

@ -1,7 +1,7 @@
use std::collections::HashMap;
use crate::api::CanvasType;
use crate::{Canvas, GlyphStyle};
use crate::Canvas;
use graphics_server::*;
@ -24,7 +24,7 @@ impl ModalLayout {
pub fn init(gfx: &graphics_server::Gfx, trng: &trng::Trng, canvases: &mut HashMap<Gid, Canvas>) -> Result<ModalLayout, xous::Error> {
let screensize = gfx.screen_size().expect("Couldn't get screen size");
// get the height of various text regions to compute the layout
let height: i16 = gfx.glyph_height_hint(GlyphStyle::Regular).expect("couldn't get glyph height") as i16;
let height: i16 = gfx.glyph_height_hint(gam::SYSTEM_STYLE).expect("couldn't get glyph height") as i16;
const MODAL_Y_PAD: i16 = 80;
const MODAL_X_PAD: i16 = 20;

View file

@ -31,6 +31,8 @@ use ime_plugin_api::{ImefCallback, ApiToken};
#[doc = include_str!("../README.md")]
pub const SYSTEM_STYLE: GlyphStyle = GlyphStyle::Tall;
// Add names here and insert them into the EXPECTED_BOOT_CONTEXTS structure below.
pub const MAIN_MENU_NAME: &'static str = "main menu";
pub const PDDB_MODAL_NAME: &'static str = "pddb modal";

View file

@ -9,7 +9,7 @@ pub use graphics_server::*;
use xous_ipc::{String, Buffer};
use num_traits::*;
use graphics_server::api::{PixelColor, TextBounds, DrawStyle, GlyphStyle};
use graphics_server::api::{PixelColor, TextBounds, DrawStyle};
#[cfg(feature = "tts")]
use tts_frontend::*;
@ -60,7 +60,7 @@ impl<'a> Menu<'a> {
assert!(authtoken.is_some(), "Couldn't register menu. Did you remember to add the app_name to the tokens.rs expected boot contexts list?");
log::debug!("requesting content canvas for menu");
let canvas = gam.request_content_canvas(authtoken.unwrap()).expect("couldn't get my content canvas from GAM");
let line_height = gam.glyph_height_hint(GlyphStyle::Cjk).expect("couldn't get glyph height hint") as i16 + 2;
let line_height = gam.glyph_height_hint(crate::SYSTEM_STYLE).expect("couldn't get glyph height hint") as i16 + 2;
#[cfg(feature="tts")]
let tts = TtsFrontend::new(&xns).unwrap();
Menu {
@ -210,7 +210,7 @@ impl<'a> Menu<'a> {
}
write!(item_tv.text, "{}", item.name.as_str().unwrap()).unwrap();
item_tv.draw_border = false;
item_tv.style = GlyphStyle::Regular;
item_tv.style = crate::SYSTEM_STYLE;
item_tv.margin = Point::new(0, 0);
item_tv.ellipsis = true;

View file

@ -34,4 +34,4 @@ debugprint = []
braille = []
gfx-testing = []
ditherpunk = []
default = []
default = ["gfx-testing"]

View file

@ -10,7 +10,6 @@ use ime_plugin_api::{ImefCallback, ImefDescriptor, ImefOpcode};
use log::{error, info};
use graphics_server::{Gid, Line, PixelColor, Point, Rectangle, TextBounds, TextView, DrawStyle};
use gam::GlyphStyle;
use ime_plugin_api::{PredictionTriggers, PredictionPlugin, PredictionApi, ApiToken};
use num_traits::{ToPrimitive,FromPrimitive};
@ -262,6 +261,7 @@ impl InputTracker {
input_tv.draw_border = false;
input_tv.border_width = 1;
input_tv.clear_area = true; // need this so that the insertion point is cleared and moved
input_tv.style = gam::SYSTEM_STYLE;
let mut do_redraw = false;
for &k in newkeys.iter() {
@ -657,7 +657,7 @@ impl InputTracker {
p_tv.border_width = 1;
p_tv.clear_area = false;
p_tv.ellipsis = true;
p_tv.style = GlyphStyle::Regular;
p_tv.style = gam::SYSTEM_STYLE;
write!(p_tv.text, "{}", pred_str).expect("can't write the prediction string");
log::trace!("posting string with length {}", p_tv.text.as_str().unwrap().len());
self.gam.post_textview(&mut p_tv).expect("couldn't post prediction text");

View file

@ -36,7 +36,6 @@ use core::sync::atomic::{AtomicBool, Ordering};
use gam::UxRegistration;
use graphics_server::{Gid, Point, Rectangle, TextBounds, TextView, DrawStyle, PixelColor};
use graphics_server::api::GlyphStyle;
use xous::MessageEnvelope;
use xous_ipc::Buffer;
@ -339,7 +338,7 @@ impl Repl{
bubble_tv.draw_border = true;
bubble_tv.clear_area = true;
bubble_tv.rounded_border = Some(self.bubble_radius);
bubble_tv.style = GlyphStyle::Regular;
bubble_tv.style = gam::SYSTEM_STYLE;
bubble_tv.margin = self.bubble_margin;
bubble_tv.ellipsis = false; bubble_tv.insertion = None;
write!(bubble_tv.text, "{}", h.text.as_str()).expect("couldn't write history text to TextView");