-
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 8 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
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,158 @@ | ||
| use crate::{BdfFont, DisplayBdfGlyph}; | ||
| use embedded_graphics::{ | ||
| prelude::*, | ||
| primitives::Rectangle, | ||
| text::{ | ||
| renderer::{CharacterStyle, TextMetrics, TextRenderer}, | ||
| Baseline, | ||
| }, | ||
| }; | ||
|
|
||
| #[derive(Clone, Copy, Eq, PartialEq, Hash, Debug)] | ||
| pub struct Metrics { | ||
| pub ascent: u32, | ||
| pub descent: u32, | ||
| pub line_height: u32, | ||
| } | ||
|
|
||
| /// A proportional font | ||
| pub trait ProportionalFont<'a>: Clone { | ||
| /// Returns a struct containing ascent, descent, baseline_offset, and line_height | ||
| fn metrics(&self) -> Metrics; | ||
| /// Finds a BdfGlyph for a character | ||
| fn lookup(&self, c: char) -> Option<DisplayBdfGlyph<'_>>; | ||
| /// Finds the replacement glyph | ||
| fn replacement_glyph(&'a self) -> DisplayBdfGlyph<'a>; | ||
|
|
||
| /// Returns the baseline offset | ||
| fn baseline_offset(&self, baseline: Baseline) -> i32 { | ||
| match baseline { | ||
| Baseline::Top => self.metrics().ascent.saturating_sub(1) as i32, | ||
| Baseline::Bottom => -(self.metrics().descent as i32), | ||
| Baseline::Middle => (self.metrics().ascent as i32 - self.metrics().descent as i32) / 2, | ||
| Baseline::Alphabetic => 0, | ||
| } | ||
| } | ||
|
|
||
| /// Returns a glyph, or a replacement character if no corresponding glyph exists | ||
| fn glyph_or_replacement(&'a self, c: char) -> DisplayBdfGlyph<'a> { | ||
| self.lookup(c).unwrap_or(self.replacement_glyph()) | ||
| } | ||
| } | ||
|
|
||
| impl<'a> ProportionalFont<'a> for BdfFont<'a> { | ||
| fn metrics(&self) -> Metrics { | ||
| Metrics { | ||
| ascent: self.ascent, | ||
| descent: self.descent, | ||
| line_height: self.ascent + self.descent, | ||
| } | ||
| } | ||
|
|
||
| fn replacement_glyph(&'a self) -> DisplayBdfGlyph<'a> { | ||
| self.glyphs[self.replacement_character].into_glyph(self) | ||
| } | ||
|
|
||
| fn lookup(&self, c: char) -> Option<DisplayBdfGlyph<'_>> { | ||
| if let Some(&g) = self.glyphs.iter().find(|g| g.character == c) { | ||
| Some(g.into_glyph(self)) | ||
| } else { | ||
| None | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// 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.glyph_or_replacement(c); | ||
|
|
||
| glyph.draw(position, self.color, 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.glyph_or_replacement(c).device_width) | ||
| .sum(); | ||
|
|
||
| // TODO: calculate correct bounding box | ||
| let bounding_box = Rectangle::new( | ||
| position - Size::new(0, self.font.metrics().ascent.saturating_sub(1)), | ||
| Size::new(dx, self.font.metrics().line_height), | ||
| ); | ||
|
|
||
| TextMetrics { | ||
| bounding_box, | ||
| next_position: position + Size::new(dx, 0), | ||
| } | ||
| } | ||
|
|
||
| fn line_height(&self) -> u32 { | ||
| self.font.metrics().line_height | ||
| } | ||
| } | ||
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.