Skip to content

Add codepoints_iter() methods for cmap Subtable4/Subtable12 #174

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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 Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ttf-parser"
version = "0.24.1"
version = "0.25.0"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't change the version number in PRs. Just the other changes will do and we can set the version as appropriate during the release process.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This still needs addressing to make this PR mergable. Would you rather I took care of it directly?

authors = ["Yevhenii Reizner <[email protected]>"]
keywords = ["ttf", "truetype", "opentype"]
categories = ["parser-implementations"]
Expand Down
15 changes: 9 additions & 6 deletions src/tables/cmap/format12.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,16 @@ impl<'a> Subtable12<'a> {
u16::try_from(id).ok().map(GlyphId)
}

/// Iterate over each codepoint defined in this table.
pub fn codepoints_iter(&'a self) -> impl Iterator<Item = u32> + 'a {
self.groups
.into_iter()
.flat_map(|group| group.start_char_code..=group.end_char_code)
}

/// Calls `f` for each codepoint defined in this table.
pub fn codepoints(&self, mut f: impl FnMut(u32)) {
for group in self.groups {
for code_point in group.start_char_code..=group.end_char_code {
f(code_point);
}
}
pub fn codepoints(&self, f: impl FnMut(u32)) {
self.codepoints_iter().for_each(f)
}
}

Expand Down
23 changes: 12 additions & 11 deletions src/tables/cmap/format4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,18 +100,19 @@ impl<'a> Subtable4<'a> {
None
}

/// Calls `f` for each codepoint defined in this table.
pub fn codepoints(&self, mut f: impl FnMut(u32)) {
for (start, end) in self.start_codes.into_iter().zip(self.end_codes) {
// OxFFFF value is special and indicates codes end.
if start == end && start == 0xFFFF {
break;
}
/// Iterate over each codepoint defined in this table.
pub fn codepoints_iter(&'a self) -> impl Iterator<Item = u32> + 'a {
self.start_codes
.into_iter()
.zip(self.end_codes)
.take_while(|&(start, end)| !(start == end && start == 0xFFFF))
.flat_map(|(start, end)| start..=end)
.map(u32::from)
}

for code_point in start..=end {
f(u32::from(code_point));
}
}
/// Calls `f` for each codepoint defined in this table.
pub fn codepoints(&self, f: impl FnMut(u32)) {
self.codepoints_iter().for_each(f)
}
}

Expand Down