From 633413e6622a6c833a17ea18b08301e33c6c48fe Mon Sep 17 00:00:00 2001 From: Mohammad AlSaleh Date: Sun, 29 Sep 2024 06:20:36 +0300 Subject: [PATCH 1/2] Add codepoints_iter() methods for cmap Subtable4/Subtable12 This allows early exit unlike `codepoints()`. Signed-off-by: Mohammad AlSaleh --- src/tables/cmap/format12.rs | 15 +++++++++------ src/tables/cmap/format4.rs | 23 ++++++++++++----------- 2 files changed, 21 insertions(+), 17 deletions(-) diff --git a/src/tables/cmap/format12.rs b/src/tables/cmap/format12.rs index 86134954..450684de 100644 --- a/src/tables/cmap/format12.rs +++ b/src/tables/cmap/format12.rs @@ -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 + '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) } } diff --git a/src/tables/cmap/format4.rs b/src/tables/cmap/format4.rs index d1d432b2..92d92ac6 100644 --- a/src/tables/cmap/format4.rs +++ b/src/tables/cmap/format4.rs @@ -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 + '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) } } From af508e33d47ad23a6d75d63cc49042faa9623c69 Mon Sep 17 00:00:00 2001 From: Mohammad AlSaleh Date: Mon, 30 Sep 2024 03:53:39 +0300 Subject: [PATCH 2/2] Bump version Signed-off-by: Mohammad AlSaleh --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 2bd42826..3ef007dc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "ttf-parser" -version = "0.24.1" +version = "0.25.0" authors = ["Yevhenii Reizner "] keywords = ["ttf", "truetype", "opentype"] categories = ["parser-implementations"]