-
Notifications
You must be signed in to change notification settings - Fork 15
Serialization Proposal 1 #52
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
35871b0
Basic Serializer
iriswebb ab5f0eb
Refactor Proportional Trait
iriswebb 941ceed
remove original BDF renderer and replace it with the generalized prop…
iriswebb e17f5da
Update eg-bdf/src/proportional.rs
iriswebb dc96dcc
Update eg-font-converter/src/serializer.rs
iriswebb bd23ac5
Fix errors in eg-bdf
iriswebb 7eb585f
fix converter and viewer
iriswebb 5c1c7d6
Better error handling and verificaion, remove magic numbers
iriswebb d90124c
Update eg-bdf/src/serialized.rs
iriswebb 386937e
Update eg-bdf/src/serialized.rs
iriswebb 33dc8a8
Make more things const, factor out indexing functions
iriswebb e7a05b8
Add runtime checking to glyph lookup, remove from_unverified_data
iriswebb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,175 @@ | ||
| use crate::{BdfFont, BdfGlyph}; | ||
| use embedded_graphics::{ | ||
| prelude::*, | ||
| primitives::Rectangle, | ||
| text::{ | ||
| renderer::{CharacterStyle, TextMetrics, TextRenderer}, | ||
| Baseline, | ||
| }, | ||
| }; | ||
|
|
||
| /// A proportional font | ||
| pub trait ProportionalFont<'a>: Clone { | ||
| /// Returns the global font ascent | ||
| fn ascent(&self) -> u16; | ||
|
|
||
| /// Returns the global font descent | ||
| fn descent(&self) -> u16; | ||
|
|
||
| /// Returns index of the replacement character | ||
| fn replacement(&self) -> u32; | ||
|
|
||
| /// Data is indexed from the start of the data block, not the start of the font | ||
| /// | ||
| /// Example: `glyph.draw(position, self.color, &self.font.data[self.font.data_offset()..], target)?;` | ||
| fn data_offset(&self) -> usize; | ||
|
|
||
| /// Returns a slice of the binary bitmap data | ||
| fn data(&self) -> &'a [u8]; | ||
|
|
||
| /// Finds the BDF glyph corresponding to a character | ||
| fn lookup(&self, c: char) -> BdfGlyph; | ||
|
|
||
| /// Returns the baseline offset | ||
| fn baseline_offset(&self, baseline: Baseline) -> i32 { | ||
| match baseline { | ||
| Baseline::Top => self.ascent().saturating_sub(1) as i32, | ||
| Baseline::Bottom => -(self.descent() as i32), | ||
| Baseline::Middle => (self.ascent() as i32 - self.descent() as i32) / 2, | ||
| Baseline::Alphabetic => 0, | ||
| } | ||
| } | ||
|
|
||
| /// Returns the default line height | ||
| fn line_height(&self) -> u32 { | ||
| (self.ascent() + self.descent()) as u32 | ||
| } | ||
| } | ||
|
|
||
| impl<'a> ProportionalFont<'a> for BdfFont<'a> { | ||
| fn ascent(&self) -> u16 { | ||
| self.ascent as u16 | ||
| } | ||
|
|
||
| fn descent(&self) -> u16 { | ||
| self.descent as u16 | ||
| } | ||
|
|
||
| fn replacement(&self) -> u32 { | ||
| self.replacement_character as u32 | ||
| } | ||
|
|
||
| fn data_offset(&self) -> usize { | ||
| 0 | ||
| } | ||
|
|
||
| fn data(&self) -> &'a [u8] { | ||
| self.data | ||
| } | ||
|
|
||
| fn lookup(&self, c: char) -> BdfGlyph { | ||
| *self | ||
| .glyphs | ||
| .iter() | ||
| .find(|g| g.character == c) | ||
| // TODO: don't panic if replacement_character is invalid | ||
| .unwrap_or_else(|| &self.glyphs[self.replacement_character]) | ||
| } | ||
| } | ||
|
|
||
| /// A generalized text style for proportional fonts | ||
| #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] | ||
| pub struct ProportionalTextStyle<'a, F: ProportionalFont<'a>, C: PixelColor> { | ||
| font: &'a F, | ||
| color: C, | ||
| } | ||
|
|
||
| impl<'a, F: ProportionalFont<'a>, C: PixelColor> ProportionalTextStyle<'a, F, C> { | ||
| /// Creates a new text style | ||
| pub fn new(font: &'a F, color: C) -> Self { | ||
| Self { font, color } | ||
| } | ||
| } | ||
|
|
||
| impl<'a, C: PixelColor, F: ProportionalFont<'a>> CharacterStyle | ||
| for ProportionalTextStyle<'a, F, C> | ||
| { | ||
| type Color = C; | ||
|
|
||
| fn set_text_color(&mut self, text_color: Option<C>) { | ||
| // TODO: support transparent text | ||
| if let Some(color) = text_color { | ||
| self.color = color; | ||
| } | ||
| } | ||
|
|
||
| // TODO: implement additional methods | ||
| } | ||
|
|
||
| impl<'a, C: PixelColor, F: ProportionalFont<'a>> TextRenderer for ProportionalTextStyle<'a, F, C> { | ||
| type Color = C; | ||
|
|
||
| fn draw_string<D>( | ||
| &self, | ||
| text: &str, | ||
| position: Point, | ||
| baseline: Baseline, | ||
| target: &mut D, | ||
| ) -> Result<Point, D::Error> | ||
| where | ||
| D: DrawTarget<Color = Self::Color>, | ||
| { | ||
| let mut position = position + Point::new(0, self.font.baseline_offset(baseline)); | ||
|
|
||
| for c in text.chars() { | ||
| let glyph = self.font.lookup(c); | ||
|
|
||
| glyph.draw( | ||
| position, | ||
| self.color, | ||
| &self.font.data()[self.font.data_offset()..], | ||
| target, | ||
| )?; | ||
|
|
||
| position.x += glyph.device_width as i32; | ||
| } | ||
|
|
||
| Ok(position) | ||
| } | ||
|
|
||
| fn draw_whitespace<D>( | ||
| &self, | ||
| width: u32, | ||
| position: Point, | ||
| baseline: Baseline, | ||
| _target: &mut D, | ||
| ) -> Result<Point, D::Error> | ||
| where | ||
| D: DrawTarget<Color = Self::Color>, | ||
| { | ||
| let position = position + Point::new(0, self.font.baseline_offset(baseline)); | ||
|
|
||
| Ok(position + Size::new(width, 0)) | ||
| } | ||
|
|
||
| fn measure_string(&self, text: &str, position: Point, baseline: Baseline) -> TextMetrics { | ||
| let position = position + Point::new(0, self.font.baseline_offset(baseline)); | ||
|
|
||
| let dx = text.chars().map(|c| self.font.lookup(c).device_width).sum(); | ||
|
|
||
| // TODO: calculate correct bounding box | ||
| let bounding_box = Rectangle::new( | ||
| position - Size::new(0, self.font.ascent().saturating_sub(1) as u32), | ||
| Size::new(dx, self.line_height()), | ||
| ); | ||
|
|
||
| TextMetrics { | ||
| bounding_box, | ||
| next_position: position + Size::new(dx, 0), | ||
| } | ||
| } | ||
|
|
||
| fn line_height(&self) -> u32 { | ||
| self.font.line_height() | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| use crate::{BdfGlyph, ProportionalFont, ProportionalTextStyle}; | ||
| use embedded_graphics::{ | ||
| prelude::*, | ||
| primitives::Rectangle, | ||
| }; | ||
|
|
||
| /// * Header (12 Bytes): | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In case this format is also used to load a font at runtime it would be a good idea to add some validation that it is a valid font file. A magic value, a version, and the file size should be enough. |
||
| /// - Ascent (pixels, u16 BE) | ||
| /// - Descent (pixels, u16 BE) | ||
| /// - Replacement Character (index into character table, u32 BE) | ||
| /// - Character Table Length (entries, u32 BE) | ||
| /// | ||
| /// * Glyph Table (17 Bytes Per Entry): | ||
| /// - corresponding codepoint (u32 BE) | ||
| /// - top_left.x (i16 BE) | ||
| /// - top_left.y (i16 BE) | ||
| /// - size.width (u16 BE) | ||
| /// - size.height (u16 BE) | ||
| /// - device_width (pixels, u8) | ||
| /// - data index (bytes from start of data, u32 BE) | ||
| /// | ||
| /// Font bitmap data is stored afterwards | ||
| #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] | ||
| pub struct SerializedBdfFont<'a> { | ||
| /// The raw u8 data of the serialized font | ||
| pub data: &'a [u8], | ||
| } | ||
| impl SerializedBdfFont<'_> { | ||
| /// Returns the length of the glyph table | ||
| pub const fn character_count(self) -> u32 { | ||
| u32::from_be_bytes([self.data[8], self.data[9], self.data[10], self.data[11]]) | ||
| } | ||
|
|
||
| /// Returns a BdfGlyph in the glyph table | ||
| pub const fn character_table(self, idx: u32) -> BdfGlyph { | ||
| let offset = 12 + (idx * 17) as usize; | ||
| let corresponding_character = char::from_u32(u32::from_be_bytes([ | ||
| self.data[offset], | ||
| self.data[offset + 1], | ||
| self.data[offset + 2], | ||
| self.data[offset + 3], | ||
| ])); | ||
| let top_left_x = i16::from_be_bytes([self.data[offset + 4], self.data[offset + 5]]); | ||
| let top_left_y = i16::from_be_bytes([self.data[offset + 6], self.data[offset + 7]]); | ||
| let width = u16::from_be_bytes([self.data[offset + 8], self.data[offset + 9]]); | ||
| let height = u16::from_be_bytes([self.data[offset + 10], self.data[offset + 11]]); | ||
| let kerning = self.data[offset + 12]; | ||
| let data_index = u32::from_be_bytes([ | ||
| self.data[offset + 13], | ||
| self.data[offset + 14], | ||
| self.data[offset + 15], | ||
| self.data[offset + 16], | ||
| ]); | ||
|
|
||
| BdfGlyph { | ||
| character: corresponding_character.unwrap(), | ||
| bounding_box: Rectangle { | ||
| top_left: Point { | ||
| x: top_left_x as i32, | ||
| y: top_left_y as i32, | ||
| }, | ||
| size: Size { | ||
| width: width as u32, | ||
| height: height as u32, | ||
| }, | ||
| }, | ||
| device_width: kerning as u32, | ||
| start_index: data_index as usize, | ||
| } | ||
| } | ||
| } | ||
| impl<'a> ProportionalFont<'a> for SerializedBdfFont<'a> { | ||
| fn ascent(&self) -> u16 { | ||
| u16::from_be_bytes([self.data[0], self.data[1]]) | ||
| } | ||
|
|
||
| fn descent(&self) -> u16 { | ||
| u16::from_be_bytes([self.data[2], self.data[3]]) | ||
| } | ||
|
|
||
| fn replacement(&self) -> u32 { | ||
| u32::from_be_bytes([self.data[4], self.data[5], self.data[6], self.data[7]]) | ||
| } | ||
|
|
||
| fn data_offset(&self) -> usize { | ||
| (12 + (self.character_count() * 17)) as usize | ||
| } | ||
|
|
||
| fn data(&self) -> &'a [u8] { | ||
| self.data | ||
| } | ||
|
|
||
| fn lookup(&self, c: char) -> BdfGlyph { | ||
| for i in 0..self.character_count() { | ||
| let tested_character = self.character_table(i); | ||
| if self.character_table(i).character == c { | ||
| return tested_character; | ||
| } | ||
| } | ||
|
|
||
| self.character_table(self.replacement()) | ||
| } | ||
| } | ||
|
|
||
| /// Stylized serialized BDF text | ||
| pub type SerializedBdfTextStyle<'a, C> = ProportionalTextStyle<'a, SerializedBdfFont<'a>, C>; | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.