-
Notifications
You must be signed in to change notification settings - Fork 28
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
Add gasp to write-fonts #1308
Merged
Add gasp to write-fonts #1308
Changes from all commits
Commits
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 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 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,127 @@ | ||
// THIS FILE IS AUTOGENERATED. | ||
// Any changes to this file will be overwritten. | ||
// For more information about how codegen works, see font-codegen/README.md | ||
|
||
#[allow(unused_imports)] | ||
use crate::codegen_prelude::*; | ||
|
||
pub use read_fonts::tables::gasp::GaspRangeBehavior; | ||
|
||
/// [gasp](https://learn.microsoft.com/en-us/typography/opentype/spec/gasp#gasp-table-formats) | ||
#[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] | ||
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] | ||
pub struct Gasp { | ||
/// Version number (set to 1) | ||
pub version: u16, | ||
/// Number of records to follow | ||
pub num_ranges: u16, | ||
/// Sorted by ppem | ||
pub gasp_ranges: Vec<GaspRange>, | ||
} | ||
|
||
impl Gasp { | ||
/// Construct a new `Gasp` | ||
pub fn new(version: u16, num_ranges: u16, gasp_ranges: Vec<GaspRange>) -> Self { | ||
Self { | ||
version, | ||
num_ranges, | ||
gasp_ranges: gasp_ranges.into_iter().map(Into::into).collect(), | ||
} | ||
} | ||
} | ||
|
||
impl FontWrite for Gasp { | ||
fn write_into(&self, writer: &mut TableWriter) { | ||
self.version.write_into(writer); | ||
self.num_ranges.write_into(writer); | ||
self.gasp_ranges.write_into(writer); | ||
} | ||
fn table_type(&self) -> TableType { | ||
TableType::TopLevel(Gasp::TAG) | ||
} | ||
} | ||
|
||
impl Validate for Gasp { | ||
fn validate_impl(&self, ctx: &mut ValidationCtx) { | ||
ctx.in_table("Gasp", |ctx| { | ||
ctx.in_field("gasp_ranges", |ctx| { | ||
if self.gasp_ranges.len() > (u16::MAX as usize) { | ||
ctx.report("array exceeds max length"); | ||
} | ||
self.gasp_ranges.validate_impl(ctx); | ||
}); | ||
}) | ||
} | ||
} | ||
|
||
impl TopLevelTable for Gasp { | ||
const TAG: Tag = Tag::new(b"gasp"); | ||
} | ||
|
||
impl<'a> FromObjRef<read_fonts::tables::gasp::Gasp<'a>> for Gasp { | ||
fn from_obj_ref(obj: &read_fonts::tables::gasp::Gasp<'a>, _: FontData) -> Self { | ||
let offset_data = obj.offset_data(); | ||
Gasp { | ||
version: obj.version(), | ||
num_ranges: obj.num_ranges(), | ||
gasp_ranges: obj.gasp_ranges().to_owned_obj(offset_data), | ||
} | ||
} | ||
} | ||
|
||
#[allow(clippy::needless_lifetimes)] | ||
impl<'a> FromTableRef<read_fonts::tables::gasp::Gasp<'a>> for Gasp {} | ||
|
||
impl<'a> FontRead<'a> for Gasp { | ||
fn read(data: FontData<'a>) -> Result<Self, ReadError> { | ||
<read_fonts::tables::gasp::Gasp as FontRead>::read(data).map(|x| x.to_owned_table()) | ||
} | ||
} | ||
|
||
#[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] | ||
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] | ||
pub struct GaspRange { | ||
/// Upper limit of range, in PPEM | ||
pub range_max_ppem: u16, | ||
/// Flags describing desired rasterizer behavior. | ||
pub range_gasp_behavior: GaspRangeBehavior, | ||
} | ||
|
||
impl GaspRange { | ||
/// Construct a new `GaspRange` | ||
pub fn new(range_max_ppem: u16, range_gasp_behavior: GaspRangeBehavior) -> Self { | ||
Self { | ||
range_max_ppem, | ||
range_gasp_behavior, | ||
} | ||
} | ||
} | ||
|
||
impl FontWrite for GaspRange { | ||
fn write_into(&self, writer: &mut TableWriter) { | ||
self.range_max_ppem.write_into(writer); | ||
self.range_gasp_behavior.write_into(writer); | ||
} | ||
fn table_type(&self) -> TableType { | ||
TableType::Named("GaspRange") | ||
} | ||
} | ||
|
||
impl Validate for GaspRange { | ||
fn validate_impl(&self, _ctx: &mut ValidationCtx) {} | ||
} | ||
|
||
impl FromObjRef<read_fonts::tables::gasp::GaspRange> for GaspRange { | ||
fn from_obj_ref(obj: &read_fonts::tables::gasp::GaspRange, _: FontData) -> Self { | ||
GaspRange { | ||
range_max_ppem: obj.range_max_ppem(), | ||
range_gasp_behavior: obj.range_gasp_behavior(), | ||
} | ||
} | ||
} | ||
|
||
impl FontWrite for GaspRangeBehavior { | ||
fn write_into(&self, writer: &mut TableWriter) { | ||
writer.write_slice(&self.bits().to_be_bytes()) | ||
} | ||
} |
This file contains 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 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,43 @@ | ||
//! The gasp table | ||
|
||
include!("../../generated/generated_gasp.rs"); | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn gasping() { | ||
let gasp = Gasp { | ||
version: 1, | ||
num_ranges: 2, | ||
gasp_ranges: vec![ | ||
GaspRange { | ||
range_max_ppem: 42, | ||
range_gasp_behavior: GaspRangeBehavior::GASP_GRIDFIT, | ||
}, | ||
GaspRange { | ||
range_max_ppem: 4242, | ||
range_gasp_behavior: GaspRangeBehavior::GASP_SYMMETRIC_SMOOTHING, | ||
}, | ||
], | ||
}; | ||
|
||
let _dumped = crate::write::dump_table(&gasp).unwrap(); | ||
let data = FontData::new(&_dumped); | ||
let loaded = read_fonts::tables::gasp::Gasp::read(data).unwrap(); | ||
|
||
assert_eq!(1, loaded.version()); | ||
assert_eq!( | ||
vec![ | ||
(42, GaspRangeBehavior::GASP_GRIDFIT), | ||
(4242, GaspRangeBehavior::GASP_SYMMETRIC_SMOOTHING) | ||
], | ||
loaded | ||
.gasp_ranges() | ||
.iter() | ||
.map(|r| (r.range_max_ppem(), r.range_gasp_behavior())) | ||
.collect::<Vec<_>>() | ||
); | ||
} | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1