Skip to content
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

[fea-rs] Support inline 'by Null' in contextual sub #718

Merged
merged 1 commit into from
Feb 13, 2024
Merged
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
13 changes: 8 additions & 5 deletions fea-rs/src/compile/compile_ctx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -700,7 +700,7 @@ impl<'a, F: FeatureProvider, V: VariationInfo> CompilationCtx<'a, F, V> {
} else {
let target = input.items().next().unwrap().target();
let arity = rule.replacements().count();
if arity == 1 {
if arity == 1 && rule.null().is_none() {
let replacement = rule.replacements().next().unwrap();
if let Some((target, replacement)) =
self.validate_single_sub_inputs(&target, Some(&replacement))
Expand All @@ -721,10 +721,13 @@ impl<'a, F: FeatureProvider, V: VariationInfo> CompilationCtx<'a, F, V> {
"Inline multiple substitution must have a single glyph target",
);
}
let replacements = rule
.replacement_glyphs()
.map(|g| self.resolve_glyph(&g))
.collect();
let replacements = if rule.null().is_some() {
Vec::new()
} else {
rule.replacement_glyphs()
.map(|g| self.resolve_glyph(&g))
.collect()
};
if let Some(target_id) = self.resolve_glyph_or_class(&target).iter().next() {
let lookup = self.ensure_current_lookup_type(Kind::GsubType6);
Some(
Expand Down
7 changes: 6 additions & 1 deletion fea-rs/src/parse/grammar/gsub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,9 @@ fn finish_chain_rule(parser: &mut Parser, recovery: TokenSet) -> AstKind {

// now we may be done, or we may have a single inline rule
if parser.eat(Kind::ByKw) {
if glyph::eat_glyph_name_like(parser) {
if parser.eat(Kind::NullKw) {
// allowed, continue down to 'expect_semi'
} else if glyph::eat_glyph_name_like(parser) {
while glyph::eat_glyph_name_like(parser) {
continue;
}
Expand All @@ -145,6 +147,9 @@ fn finish_chain_rule(parser: &mut Parser, recovery: TokenSet) -> AstKind {
}

if parser.expect_semi() {
// this looks like a valid contextual rule, but we will need to
// reparse it. This happens automatically when the node is added to
// the `AstSink`
AstKind::GsubNodeNeedsRewrite
} else {
AstKind::GsubNode
Expand Down
3 changes: 3 additions & 0 deletions fea-rs/src/token_tree/rewrite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,9 @@ pub(crate) fn reparse_contextual_sub_rule(rewriter: &mut ReparseCtx) -> Kind {
if rewriter.matches(0, Kind::ByKw) {
rewriter.in_node(Kind::InlineSubNode, |rewriter| {
rewriter.expect(Kind::ByKw);
if rewriter.eat(Kind::NullKw) {
return;
}
expect_glyph_or_glyph_class(rewriter);
while at_glyph_or_glyph_class(rewriter.nth_kind(0)) {
eat_glyph_or_glyph_class(rewriter);
Expand Down
4 changes: 4 additions & 0 deletions fea-rs/src/token_tree/typed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -890,6 +890,10 @@ impl InlineSubRule {
pub(crate) fn replacements(&self) -> impl Iterator<Item = GlyphOrClass> + '_ {
self.iter().filter_map(GlyphOrClass::cast)
}

pub(crate) fn null(&self) -> Option<Null> {
self.iter().find_map(Null::cast)
}
}

impl Gpos1 {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
feature test {
sub a b c' by NULL;
} test;
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?xml version="1.0" encoding="UTF-8"?>
<ttFont>

<GSUB>
<Version value="0x00010000"/>
<ScriptList>
<!-- ScriptCount=1 -->
<ScriptRecord index="0">
<ScriptTag value="DFLT"/>
<Script>
<DefaultLangSys>
<ReqFeatureIndex value="65535"/>
<!-- FeatureCount=1 -->
<FeatureIndex index="0" value="0"/>
</DefaultLangSys>
<!-- LangSysCount=0 -->
</Script>
</ScriptRecord>
</ScriptList>
<FeatureList>
<!-- FeatureCount=1 -->
<FeatureRecord index="0">
<FeatureTag value="test"/>
<Feature>
<!-- LookupCount=1 -->
<LookupListIndex index="0" value="0"/>
</Feature>
</FeatureRecord>
</FeatureList>
<LookupList>
<!-- LookupCount=2 -->
<Lookup index="0">
<LookupType value="6"/>
<LookupFlag value="0"/>
<!-- SubTableCount=1 -->
<ChainContextSubst index="0" Format="1">
<Coverage>
<Glyph value="c"/>
</Coverage>
<!-- ChainSubRuleSetCount=1 -->
<ChainSubRuleSet index="0">
<!-- ChainSubRuleCount=1 -->
<ChainSubRule index="0">
<!-- BacktrackGlyphCount=2 -->
<Backtrack index="0" value="b"/>
<Backtrack index="1" value="a"/>
<!-- InputGlyphCount=1 -->
<!-- LookAheadGlyphCount=0 -->
<!-- SubstCount=1 -->
<SubstLookupRecord index="0">
<SequenceIndex value="0"/>
<LookupListIndex value="1"/>
</SubstLookupRecord>
</ChainSubRule>
</ChainSubRuleSet>
</ChainContextSubst>
</Lookup>
<Lookup index="1">
<LookupType value="2"/>
<LookupFlag value="0"/>
<!-- SubTableCount=1 -->
<MultipleSubst index="0">
<Substitution in="c" out=""/>
</MultipleSubst>
</Lookup>
</LookupList>
</GSUB>

</ttFont>
Loading