Skip to content

Commit e0487b9

Browse files
qxliu76garretrieger
authored andcommitted
[glyph closure] address review comments
1 parent 950fd5f commit e0487b9

3 files changed

Lines changed: 40 additions & 54 deletions

File tree

read-fonts/src/tables/gsub/closure.rs

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use super::{
2020
#[cfg(feature = "std")]
2121
use crate::tables::layout::{
2222
ContextFormat1, ContextFormat2, ContextFormat3, Intersect, LayoutLookupList, LookupClosure,
23-
LookupClosureCtx,
23+
LookupClosureCtx, SeqCache,
2424
};
2525

2626
// we put ClosureCtx in its own module to enforce visibility rules;
@@ -133,12 +133,7 @@ mod ctx {
133133

134134
// Return true if we have visited this lookup with current set of glyphs
135135
pub(super) fn is_lookup_done(&mut self, lookup_index: u16) -> bool {
136-
let cur_active_glyphs = if let Some(cur_active_glyphs) = self.active_glyphs_stack.last()
137-
{
138-
cur_active_glyphs
139-
} else {
140-
&*self.glyphs
141-
};
136+
let cur_active_glyphs = self.active_glyphs_stack.last().unwrap_or(self.glyphs);
142137

143138
let (count, covered) = self
144139
.done_lookups_glyphs
@@ -888,10 +883,8 @@ impl GlyphClosure for ContextFormat2<'_> {
888883
let lookups = lookup_list.lookups();
889884
let mut seen_sequence_indices = IntSet::new();
890885

891-
let mut input_cache = FnvHashMap::default();
892-
let mut backtrack_cache = FnvHashMap::default();
893-
let mut lookahead_cache = FnvHashMap::default();
894886
let mut intersected_class_cache = FnvHashMap::default();
887+
let mut seq_cache = SeqCache::default();
895888
for (i, rule_set) in self
896889
.rule_sets()
897890
.enumerate()
@@ -916,9 +909,7 @@ impl GlyphClosure for ContextFormat2<'_> {
916909
&input_class_def,
917910
backtrack_class_def.as_ref(),
918911
lookahead_class_def.as_ref(),
919-
&mut input_cache,
920-
&mut backtrack_cache,
921-
&mut lookahead_cache,
912+
&mut seq_cache,
922913
) {
923914
continue;
924915
}

read-fonts/src/tables/layout.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use crate::collections::IntSet;
2020
#[cfg(feature = "std")]
2121
pub(crate) use closure::{
2222
ContextFormat1, ContextFormat2, ContextFormat3, LayoutLookupList, LookupClosure,
23-
LookupClosureCtx, MAX_LOOKUP_VISIT_COUNT, MAX_NESTING_LEVEL,
23+
LookupClosureCtx, SeqCache, MAX_LOOKUP_VISIT_COUNT, MAX_NESTING_LEVEL,
2424
};
2525

2626
#[cfg(feature = "std")]

read-fonts/src/tables/layout/closure.rs

Lines changed: 35 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -631,6 +631,13 @@ pub(crate) enum Format2Rule<'a> {
631631
Chain(ChainedClassSequenceRule<'a>),
632632
}
633633

634+
#[derive(Default)]
635+
pub(crate) struct SeqCache {
636+
input: FnvHashMap<u16, bool>,
637+
backtrack: FnvHashMap<u16, bool>,
638+
lookahead: FnvHashMap<u16, bool>,
639+
}
640+
634641
impl ContextFormat2<'_> {
635642
pub(crate) fn coverage(&self) -> Option<Result<CoverageTable<'_>, ReadError>> {
636643
match self {
@@ -735,20 +742,16 @@ impl Format2Rule<'_> {
735742
input_class_def: &ClassDef,
736743
backtrack_class_def: Option<&ClassDef>,
737744
lookahead_class_def: Option<&ClassDef>,
738-
input_cache: &mut FnvHashMap<u16, bool>,
739-
backtrack_cache: &mut FnvHashMap<u16, bool>,
740-
lookahead_cache: &mut FnvHashMap<u16, bool>,
745+
seq_cache: &mut SeqCache,
741746
) -> bool {
742747
match self {
743-
Self::Plain(table) => table.intersects(glyphs, input_class_def, input_cache),
748+
Self::Plain(table) => table.intersects(glyphs, input_class_def, &mut seq_cache.input),
744749
Self::Chain(table) => table.intersects(
745750
glyphs,
746751
input_class_def,
747752
backtrack_class_def,
748753
lookahead_class_def,
749-
input_cache,
750-
backtrack_cache,
751-
lookahead_cache,
754+
seq_cache,
752755
),
753756
}
754757
}
@@ -760,13 +763,9 @@ fn intersects_class(
760763
class: u16,
761764
cache: &mut FnvHashMap<u16, bool>,
762765
) -> bool {
763-
if let Some(v) = cache.get(&class) {
764-
return *v;
765-
}
766-
767-
let v = class_def.intersects_class_glyphs(glyphs, class);
768-
cache.insert(class, v);
769-
v
766+
*cache
767+
.entry(class)
768+
.or_insert_with(|| class_def.intersects_class_glyphs(glyphs, class))
770769
}
771770
impl ClassSequenceRule<'_> {
772771
fn intersects(
@@ -789,36 +788,40 @@ impl ChainedClassSequenceRule<'_> {
789788
input_class_def: &ClassDef,
790789
backtrack_class_def: Option<&ClassDef>,
791790
lookahead_class_def: Option<&ClassDef>,
792-
input_cache: &mut FnvHashMap<u16, bool>,
793-
backtrack_cache: &mut FnvHashMap<u16, bool>,
794-
lookahead_cache: &mut FnvHashMap<u16, bool>,
791+
seq_cache: &mut SeqCache,
795792
) -> bool {
796793
if !self
797794
.input_sequence()
798795
.iter()
799-
.all(|c| intersects_class(input_class_def, glyphs, c.get(), input_cache))
796+
.all(|c| intersects_class(input_class_def, glyphs, c.get(), &mut seq_cache.input))
800797
{
801798
return false;
802799
}
803800

804801
if let Some(backtrack_class_def) = backtrack_class_def {
805-
if !self
806-
.backtrack_sequence()
807-
.iter()
808-
.all(|c| intersects_class(backtrack_class_def, glyphs, c.get(), backtrack_cache))
809-
{
802+
if !self.backtrack_sequence().iter().all(|c| {
803+
intersects_class(
804+
backtrack_class_def,
805+
glyphs,
806+
c.get(),
807+
&mut seq_cache.backtrack,
808+
)
809+
}) {
810810
return false;
811811
}
812812
} else if self.backtrack_glyph_count() != 0 {
813813
return false;
814814
}
815815

816816
if let Some(lookahead_class_def) = lookahead_class_def {
817-
if !self
818-
.lookahead_sequence()
819-
.iter()
820-
.all(|c| intersects_class(lookahead_class_def, glyphs, c.get(), lookahead_cache))
821-
{
817+
if !self.lookahead_sequence().iter().all(|c| {
818+
intersects_class(
819+
lookahead_class_def,
820+
glyphs,
821+
c.get(),
822+
&mut seq_cache.lookahead,
823+
)
824+
}) {
822825
return false;
823826
}
824827
} else if self.lookahead_glyph_count() != 0 {
@@ -961,9 +964,7 @@ impl Intersect for ContextFormat2<'_> {
961964
}
962965
};
963966

964-
let mut input_cache = FnvHashMap::default();
965-
let mut backtrack_cache = FnvHashMap::default();
966-
let mut lookahead_cache = FnvHashMap::default();
967+
let mut seq_cache = SeqCache::default();
967968
for rule_set in self.rule_sets().enumerate().filter_map(|(c, rule_set)| {
968969
input_class_def
969970
.intersects_class_glyphs(&retained_coverage_glyphs, c as u16)
@@ -979,9 +980,7 @@ impl Intersect for ContextFormat2<'_> {
979980
&input_class_def,
980981
backtrack_class_def.as_ref(),
981982
lookahead_class_def.as_ref(),
982-
&mut input_cache,
983-
&mut backtrack_cache,
984-
&mut lookahead_cache,
983+
&mut seq_cache,
985984
) {
986985
return Ok(true);
987986
}
@@ -1025,9 +1024,7 @@ impl LookupClosure for ContextFormat2<'_> {
10251024
}
10261025
};
10271026

1028-
let mut input_cache = FnvHashMap::default();
1029-
let mut backtrack_cache = FnvHashMap::default();
1030-
let mut lookahead_cache = FnvHashMap::default();
1027+
let mut seq_cache = SeqCache::default();
10311028
for rule_set in self.rule_sets().enumerate().filter_map(|(c, rule_set)| {
10321029
input_class_def
10331030
.intersects_class_glyphs(&retained_coverage_glyphs, c as u16)
@@ -1051,9 +1048,7 @@ impl LookupClosure for ContextFormat2<'_> {
10511048
&input_class_def,
10521049
backtrack_class_def.as_ref(),
10531050
lookahead_class_def.as_ref(),
1054-
&mut input_cache,
1055-
&mut backtrack_cache,
1056-
&mut lookahead_cache,
1051+
&mut seq_cache,
10571052
) {
10581053
continue;
10591054
}

0 commit comments

Comments
 (0)