Skip to content

Commit

Permalink
Merge pull request #90 from LoganDark/optional-font
Browse files Browse the repository at this point in the history
make Roboto optional
  • Loading branch information
LinusCDE authored Jan 13, 2022
2 parents 02b5e3e + ecc2ba1 commit da9fe2e
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 28 deletions.
10 changes: 4 additions & 6 deletions src/appctx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ unsafe impl<'a> Send for ApplicationContext<'a> {}
unsafe impl<'a> Sync for ApplicationContext<'a> {}

pub struct ApplicationContext<'a> {
framebuffer: Box<core::Framebuffer<'a>>,
framebuffer: Box<core::Framebuffer>,
yres: u32,
xres: u32,

Expand Down Expand Up @@ -83,7 +83,7 @@ impl Default for ApplicationContext<'static> {

// Reluctantly resort to using a static global to associate the lua context with the
// one and only framebuffer that's going to be used
unsafe { luaext::G_FB = res.framebuffer.deref_mut() as *mut core::Framebuffer<'_> };
unsafe { luaext::G_FB = res.framebuffer.deref_mut() as *mut core::Framebuffer };

let mut nms = lua.empty_array("fb");
// Clears and refreshes the entire screen
Expand All @@ -104,11 +104,9 @@ impl Default for ApplicationContext<'static> {
}

impl<'a> ApplicationContext<'a> {
pub fn get_framebuffer_ref(&mut self) -> &'static mut core::Framebuffer<'static> {
pub fn get_framebuffer_ref(&mut self) -> &'static mut core::Framebuffer {
unsafe {
std::mem::transmute::<_, &'static mut core::Framebuffer<'static>>(
self.framebuffer.deref_mut(),
)
std::mem::transmute::<_, &'static mut core::Framebuffer>(self.framebuffer.deref_mut())
}
}

Expand Down
16 changes: 5 additions & 11 deletions src/framebuffer/core.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use libc::ioctl;
use memmap2::{MmapOptions, MmapRaw};
use rusttype::Font;

use std::fs::{File, OpenOptions};
use std::os::unix::io::AsRawFd;
Expand All @@ -16,11 +15,10 @@ use crate::framebuffer::swtfb_client::SwtfbClient;

/// Framebuffer struct containing the state (latest update marker etc.)
/// along with the var/fix screeninfo structs.
pub struct Framebuffer<'a> {
pub struct Framebuffer {
pub device: File,
pub frame: MmapRaw,
pub marker: AtomicU32,
pub default_font: Font<'a>,
/// Not updated as a result of calling `Framebuffer::put_var_screeninfo(..)`.
/// It is your responsibility to update this when you call into that function
/// like it has been done in `Framebuffer::new(..)`.
Expand All @@ -29,11 +27,11 @@ pub struct Framebuffer<'a> {
pub swtfb_client: Option<super::swtfb_client::SwtfbClient>,
}

unsafe impl<'a> Send for Framebuffer<'a> {}
unsafe impl<'a> Sync for Framebuffer<'a> {}
unsafe impl Send for Framebuffer {}
unsafe impl Sync for Framebuffer {}

impl<'a> framebuffer::FramebufferBase<'a> for Framebuffer<'a> {
fn from_path(path_to_device: &str) -> Framebuffer<'_> {
impl framebuffer::FramebufferBase for Framebuffer {
fn from_path(path_to_device: &str) -> Framebuffer {
let swtfb_client = if path_to_device == crate::device::Model::Gen2.framebuffer_path() {
Some(SwtfbClient::default())
} else {
Expand Down Expand Up @@ -85,14 +83,10 @@ impl<'a> framebuffer::FramebufferBase<'a> for Framebuffer<'a> {
.expect("Unable to map provided path")
};

// Load the font
let font_data = include_bytes!("../../assets/Roboto-Regular.ttf");
let default_font = Font::try_from_bytes(font_data as &[u8]).expect("corrupted font data");
Framebuffer {
marker: AtomicU32::new(1),
device,
frame: mem_map,
default_font,
var_screen_info,
fix_screen_info,
swtfb_client,
Expand Down
14 changes: 9 additions & 5 deletions src/framebuffer/draw.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use image::RgbImage;
use rusttype::{point, Scale};
use once_cell::sync::Lazy;
use rusttype::{point, Font, Scale};

use crate::framebuffer;
use crate::framebuffer::cgmath::*;
Expand All @@ -8,7 +9,12 @@ use crate::framebuffer::core;
use crate::framebuffer::graphics;
use crate::framebuffer::FramebufferIO;

impl<'a> framebuffer::FramebufferDraw for core::Framebuffer<'a> {
pub static DEFAULT_FONT: Lazy<Font<'static>> = Lazy::new(|| {
Font::try_from_bytes(include_bytes!("../../assets/Roboto-Regular.ttf").as_slice())
.expect("corrupted font data")
});

impl<'a> framebuffer::FramebufferDraw for core::Framebuffer {
fn draw_image(&mut self, img: &RgbImage, pos: Point2<i32>) -> mxcfb_rect {
for (x, y, pixel) in img.enumerate_pixels() {
let pixel_pos = pos + vec2(x as i32, y as i32);
Expand Down Expand Up @@ -145,8 +151,6 @@ impl<'a> framebuffer::FramebufferDraw for core::Framebuffer<'a> {
// The starting positioning of the glyphs (top left corner)
let start = point(pos.x, pos.y);

let dfont = &mut self.default_font.clone();

let mut min_y = pos.y.floor().max(0.0) as u32;
let mut max_y = pos.y.ceil().max(0.0) as u32;
let mut min_x = pos.x.floor().max(0.0) as u32;
Expand All @@ -158,7 +162,7 @@ impl<'a> framebuffer::FramebufferDraw for core::Framebuffer<'a> {
let c3 = f32::from(255 - components[2]);

// Loop through the glyphs in the text, positing each one on a line
for glyph in dfont.layout(text, scale, start) {
for glyph in DEFAULT_FONT.layout(text, scale, start) {
if let Some(bounding_box) = glyph.pixel_bounding_box() {
// Draw the glyph into the image per-pixel by using the draw closure
let bbmax_y = bounding_box.max.y as u32;
Expand Down
2 changes: 1 addition & 1 deletion src/framebuffer/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::framebuffer;
use crate::framebuffer::cgmath;
use crate::framebuffer::common;

impl<'a> framebuffer::FramebufferIO for framebuffer::core::Framebuffer<'a> {
impl<'a> framebuffer::FramebufferIO for framebuffer::core::Framebuffer {
fn write_frame(&mut self, frame: &[u8]) {
let begin = self.frame.as_mut_ptr();
unsafe {
Expand Down
4 changes: 2 additions & 2 deletions src/framebuffer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,9 @@ pub trait FramebufferDraw {
}

pub mod core;
pub trait FramebufferBase<'a> {
pub trait FramebufferBase {
/// Creates a new instance of Framebuffer
fn from_path(path_to_device: &str) -> core::Framebuffer<'_>;
fn from_path(path_to_device: &str) -> core::Framebuffer;
/// Toggles the EPD Controller (see https://wiki.mobileread.com/wiki/EPD_controller)
fn set_epdc_access(&mut self, state: bool);
/// Toggles autoupdate mode
Expand Down
2 changes: 1 addition & 1 deletion src/framebuffer/refresh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub enum PartialRefreshMode {
Wait,
}

impl<'a> framebuffer::FramebufferRefresh for core::Framebuffer<'a> {
impl<'a> framebuffer::FramebufferRefresh for core::Framebuffer {
fn full_refresh(
&self,
waveform_mode: common::waveform_mode,
Expand Down
4 changes: 2 additions & 2 deletions src/ui_extensions/luaext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ use crate::framebuffer::FramebufferRefresh;

/// We reluctantly resort to a static global here to associate the lua context
/// with the only active framebuffer we will have
pub static mut G_FB: *mut core::Framebuffer<'_> = std::ptr::null_mut();
pub static mut G_FB: *mut core::Framebuffer = std::ptr::null_mut();

/// A macro to utilize this static global only inside this file.
macro_rules! get_current_framebuffer {
() => {
unsafe { &mut *(G_FB as *mut core::Framebuffer<'_>) }
unsafe { &mut *(G_FB as *mut core::Framebuffer) }
};
}

Expand Down

0 comments on commit da9fe2e

Please sign in to comment.