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

[skrifa] autohint: cycle detection for GSUB lookups #1296

Merged
merged 3 commits into from
Jan 6, 2025
Merged

Conversation

dfrg
Copy link
Member

@dfrg dfrg commented Dec 20, 2024

Adds a stack to detect cycles in GSUB lookups while computing shaper coverage for autohinting.

Fixes #1295

dfrg added 2 commits December 20, 2024 12:59
Adds a stack to detect cycles in GSUB lookups while computing shaper coverage for autohinting.

Fixes #1295
/// Called when we finish processing a single lookup.
fn exit_lookup(&mut self) {
self.lookup_depth = self.lookup_depth.saturating_sub(1);
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't love that this makes it relatively easy to add code that enters without calling exit. This would be somewhat harder if it was directly in the process lookup fn? - depicted below. Or by a guard that reduces depth on drop but I suppose that path takes you towards multiple mut refs.

    fn process_lookup(&mut self, lookup_index: u16) {
        // Guard: don't cycle and don't go exceed depth limit
        // Note: we use a linear search here under the assumption that
        // most fonts have shallow contextual lookup chains
        if self.lookup_depth != 0
            && (self.lookup_depth == MAX_NESTING_DEPTH
                || self.lookup_stack[..self.lookup_depth].contains(&lookup_index))
        {
            return;
        }
        self.lookup_stack[self.lookup_depth] = lookup_index;
        self.lookup_depth += 1;

        // Actually process the lookup
        self.process_lookup_inner(lookup_index);

        // Out we go again
        self.lookup_depth -= 1;
    }

Copy link
Member Author

Choose a reason for hiding this comment

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

I agree with the sentiment but I kept the stack ops separate to support isolated unit testing of that functionality. If we embed them then we need to build a lookup list with the desired structure which seems less clear and more fragile to me.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I would have thought we need to build lookups with good/bad structure (one that cycles, one that has max depth, one that has max depth + 1, etc) to unit test regardless so... that's kind of fine?

}
}

fn process_lookup_inner(&mut self, lookup_index: u16) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

might as well mark inline always?

Copy link
Member Author

Choose a reason for hiding this comment

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

sgtm

@dfrg
Copy link
Member Author

dfrg commented Dec 21, 2024

For reference, contextual lookup depth in all of Google Fonts and Noto (10604 font files):

depth # top level lookups
2 76338
3 933
4 14
5 14
6 42

inline stack ops and add tests for bad lookups that cycle and exceed max depth
@dfrg dfrg merged commit 9340b7c into main Jan 6, 2025
10 checks passed
@dfrg dfrg deleted the autohint_gsub_cycles branch January 6, 2025 17:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Suspected endless loop in autohinting Shaper coverage computation with Poppins Semibold
2 participants