Skip to content

Add specialized glyf loader for the autohinter #1497

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

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion fauntlet/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ repository.workspace = true

[dependencies]
skrifa = { workspace = true }
freetype-rs = { version = "0.38.0", features = ["bundled"] }
freetype-rs = { version = "0.37.0", features = ["bundled"]}
memmap2 = "0.5.10"
rayon = "1.8.0"
clap = { version = "4.4.7", features = ["derive"] }
Expand Down
3 changes: 2 additions & 1 deletion fauntlet/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ fn main() {

// We don't currently test hinting.
// Waiting on <https://github.com/googlefonts/fontations/issues/620>
let hinting = None;
// let hinting = None;
let hinting = Some(fauntlet::Hinting::Auto(fauntlet::HintingTarget::Light));

use clap::Parser as _;
let args = Args::parse_from(wild::args());
Expand Down
87 changes: 87 additions & 0 deletions read-fonts/src/tables/glyf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,15 @@ impl PointFlags {
}
}

pub trait PointWithFlags<C>: Copy + Sized {
fn x(&self) -> C;
fn y(&self) -> C;
fn x_mut(&mut self) -> &mut C;
fn y_mut(&mut self) -> &mut C;
fn flags(&self) -> PointFlags;
fn flags_mut(&mut self) -> &mut PointFlags;
}

/// Trait for types that are usable for TrueType point coordinates.
pub trait PointCoord:
Copy
Expand Down Expand Up @@ -288,6 +297,84 @@ impl<'a> SimpleGlyph<'a> {
Ok(())
}

pub fn read_points_with_flags_fast<P: PointWithFlags<C>, C: PointCoord>(
&self,
points: &mut [P],
) -> Result<(), ReadError> {
let n_points = self.num_points();
if points.len() != n_points {
return Err(ReadError::InvalidArrayLen);
}
let mut cursor = FontData::new(self.glyph_data()).cursor();
// We'll need at most n_points flags, but fewer if there are repeats
let flags_data = cursor.read_array::<u8>(n_points.min(cursor.remaining_bytes()))?;
let mut flags_iter = flags_data.iter().copied();
// Keep track of the actual number of flag bytes read so that we can
// create a new cursor for reading coordinates
let mut read_flags_bytes = 0;
let mut i = 0;
while let Some(flag_bits) = flags_iter.next() {
read_flags_bytes += 1;
if SimpleGlyphFlags::from_bits_truncate(flag_bits)
.contains(SimpleGlyphFlags::REPEAT_FLAG)
{
let count = (flags_iter.next().ok_or(ReadError::OutOfBounds)? as usize + 1)
.min(n_points - i);
read_flags_bytes += 1;
for f in &mut points[i..i + count] {
f.flags_mut().0 = flag_bits;
}
i += count;
} else {
points[i].flags_mut().0 = flag_bits;
i += 1;
}
if i == n_points {
break;
}
}
let mut cursor = FontData::new(self.glyph_data()).cursor();
cursor.advance_by(read_flags_bytes);
let mut x = 0i32;
for point in points.iter_mut() {
let mut delta = 0i32;
let flag = SimpleGlyphFlags::from_bits_truncate(point.flags().0);
if flag.contains(SimpleGlyphFlags::X_SHORT_VECTOR) {
delta = cursor.read::<u8>()? as i32;
if !flag.contains(SimpleGlyphFlags::X_IS_SAME_OR_POSITIVE_X_SHORT_VECTOR) {
delta = -delta;
}
} else if !flag.contains(SimpleGlyphFlags::X_IS_SAME_OR_POSITIVE_X_SHORT_VECTOR) {
delta = cursor.read::<i16>()? as i32;
}
x = x.wrapping_add(delta);
*point.x_mut() = C::from_i32(x);
}
let mut y = 0i32;
for point in points.iter_mut() {
let mut delta = 0i32;
let flag = SimpleGlyphFlags::from_bits_truncate(point.flags().0);
if flag.contains(SimpleGlyphFlags::Y_SHORT_VECTOR) {
delta = cursor.read::<u8>()? as i32;
if !flag.contains(SimpleGlyphFlags::Y_IS_SAME_OR_POSITIVE_Y_SHORT_VECTOR) {
delta = -delta;
}
} else if !flag.contains(SimpleGlyphFlags::Y_IS_SAME_OR_POSITIVE_Y_SHORT_VECTOR) {
delta = cursor.read::<i16>()? as i32;
}
y = y.wrapping_add(delta);
*point.y_mut() = C::from_i32(y);
let flags_mask = if cfg!(feature = "spec_next") {
PointFlags::CURVE_MASK
} else {
// Drop the cubic bit if the spec_next feature is not enabled
PointFlags::ON_CURVE
};
point.flags_mut().0 &= flags_mask;
}
Ok(())
}

/// Returns an iterator over the points in the glyph.
///
/// ## Performance
Expand Down
29 changes: 29 additions & 0 deletions skrifa/src/collections.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,35 @@ where
true
}

pub fn resize(&mut self, new_len: usize) {
if new_len > N {
self.spill_to_heap(new_len);
}
match &mut self.0 {
Storage::Inline(buf, len) => {
if new_len > *len {
for value in &mut buf[*len..new_len] {
*value = Default::default();
}
}
*len = new_len;
}
Storage::Heap(vec) => vec.resize(new_len, T::default()),
}
}

fn spill_to_heap(&mut self, capacity: usize) {
match &mut self.0 {
Storage::Inline(buf, len) => {
let mut vec = Vec::new();
vec.reserve(capacity);
vec.extend_from_slice(&buf[..*len]);
self.0 = Storage::Heap(vec);
}
_ => {}
}
}

/// Appends an element to the back of the collection.
pub fn push(&mut self, value: T) {
match &mut self.0 {
Expand Down
Loading