Skip to content

Commit

Permalink
Reorganize rules
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanUkhov committed Sep 1, 2024
1 parent eb0dc42 commit ddb46b9
Showing 1 changed file with 65 additions and 15 deletions.
80 changes: 65 additions & 15 deletions src/formats/opentype/features/rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,13 @@ pub trait Table: Sized {
}

#[inline]
fn expand(value: Vec<Glyph>, _: &[Action], _: &Directory<Self>) -> Rule {
Rule::Simple((value, Default::default()))
fn expand(value: Vec<Glyph>, _: &Directory<Self>) -> Vec<Glyph> {
value
}

#[inline]
fn target(_: &[Action], _: &Directory<Self>) -> Vec<Glyph> {
Default::default()
}
}

Expand Down Expand Up @@ -90,7 +95,10 @@ impl Table for opentype::tables::glyph_substitution::Type {
values.extend(uncover(&table.coverage).filter_map(|glyph_id| {
let other_id = glyph_id as isize + table.delta_glyph_id as isize;
GlyphID::try_from(other_id).ok().map(|other_id| {
Rule::Simple((vec![glyph_id.into()], vec![other_id.into()]))
Rule::Simple((
Self::expand(vec![glyph_id.into()], directory),
Self::expand(vec![other_id.into()], directory),
))
})
}));
}
Expand All @@ -99,16 +107,22 @@ impl Table for opentype::tables::glyph_substitution::Type {
uncover(&table.coverage)
.zip(table.glyph_ids.iter().cloned())
.map(|(glyph_id, other_id)| {
Rule::Simple((vec![glyph_id.into()], vec![other_id.into()]))
Rule::Simple((
Self::expand(vec![glyph_id.into()], directory),
Self::expand(vec![other_id.into()], directory),
))
}),
);
}
Type::MultipleSubstitution(table) => {
values.extend(uncover(&table.coverage).zip(&table.records).map(
|(glyph_id, record)| {
Rule::Simple((
vec![glyph_id.into()],
record.glyph_ids.iter().cloned().map(Into::into).collect(),
Self::expand(vec![glyph_id.into()], directory),
Self::expand(
record.glyph_ids.iter().cloned().map(Into::into).collect(),
directory,
),
))
},
));
Expand All @@ -125,7 +139,10 @@ impl Table for opentype::tables::glyph_substitution::Type {
let mut value = Vec::with_capacity(record.glyph_count as usize);
value.push(glyph_id.into());
value.extend(record.glyph_ids.iter().cloned().map(Into::into));
Rule::Simple((value, vec![record.glyph_id.into()]))
Rule::Simple((
Self::expand(value, directory),
Self::expand(vec![record.glyph_id.into()], directory),
))
})
},
));
Expand All @@ -141,7 +158,13 @@ impl Table for opentype::tables::glyph_substitution::Type {
let mut value = Vec::with_capacity(record.glyph_count as usize);
value.push(glyph_id.into());
value.extend(record.glyph_ids.iter().cloned().map(Into::into));
Self::expand(value, &record.actions, directory)
Rule::Simple((
Self::expand(value, directory),
Self::expand(
Self::target(&record.actions, directory),
directory,
),
))
})
},
));
Expand Down Expand Up @@ -171,7 +194,13 @@ impl Table for opentype::tables::glyph_substitution::Type {
for class_index in &record.indices {
value.push(classes.get(class_index)?.clone());
}
Some(Self::expand(value, &record.actions, directory))
Some(Rule::Simple((
Self::expand(value, directory),
Self::expand(
Self::target(&record.actions, directory),
directory,
),
)))
})
})
.collect::<Option<Vec<_>>>()?,
Expand All @@ -180,7 +209,10 @@ impl Table for opentype::tables::glyph_substitution::Type {
Type::ContextualSubstitution(Context::Format3(table)) => {
if table.action_count > 0 {
let value = table.coverages.iter().cloned().map(Glyph::from).collect();
values.insert(Self::expand(value, &table.actions, directory));
values.insert(Rule::Simple((
Self::expand(value, directory),
Self::expand(Self::target(&table.actions, directory), directory),
)));
}
}
Type::ChainedContextualSubstitution(ChainedContext::Format1(table)) => {
Expand Down Expand Up @@ -209,7 +241,13 @@ impl Table for opentype::tables::glyph_substitution::Type {
value.extend(
record.forward_glyph_ids.iter().cloned().map(Into::into),
);
Self::expand(value, &record.actions, directory)
Rule::Simple((
Self::expand(value, directory),
Self::expand(
Self::target(&record.actions, directory),
directory,
),
))
})
},
));
Expand Down Expand Up @@ -256,7 +294,13 @@ impl Table for opentype::tables::glyph_substitution::Type {
for class_index in &record.forward_indices {
value.push(forward_classes.get(class_index)?.clone());
}
Some(Self::expand(value, &record.actions, directory))
Some(Rule::Simple((
Self::expand(value, directory),
Self::expand(
Self::target(&record.actions, directory),
directory,
),
)))
})
})
.collect::<Option<Vec<_>>>()?,
Expand All @@ -273,7 +317,10 @@ impl Table for opentype::tables::glyph_substitution::Type {
.collect::<Vec<_>>();
value.extend(table.coverages.iter().cloned().map(Glyph::from));
value.extend(table.forward_coverages.iter().cloned().map(Glyph::from));
values.insert(Self::expand(value, &table.actions, directory));
values.insert(Rule::Simple((
Self::expand(value, directory),
Self::expand(Self::target(&table.actions, directory), directory),
)));
}
}
Type::ReverseChainedContextualSubstibution(table) => {
Expand All @@ -287,8 +334,11 @@ impl Table for opentype::tables::glyph_substitution::Type {
value.push(table.coverage.clone().into());
value.extend(table.forward_coverages.iter().cloned().map(Glyph::from));
values.insert(Rule::Simple((
value,
table.glyph_ids.iter().cloned().map(Into::into).collect(),
Self::expand(value, directory),
Self::expand(
table.glyph_ids.iter().cloned().map(Into::into).collect(),
directory,
),
)));
}
_ => {
Expand Down

0 comments on commit ddb46b9

Please sign in to comment.