Add method to retrieve information about what characters in the original string were replaced by each glyph. #798
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Add a
stringToGlyphMapping
API tofont.mjs
to retrieve information about what characters in the original string were replaced by each glyph.Description
There exists
stringToGlyphIndexes
andstringToGlyphs
APIs, which convert a string to an array of glyphs. Unfortunately, information about which character is transformed into each glyph is lost when using these methods.This PR adds a
stringToGlyphMapping
method which returns a list of glyphs, as well as character indices in the original string that was provided to be able to track which characters were replaced by each glyph.stringToGlyphMapping(s: string): Array<{ glyph: Glyph, replaced: number[] }>;
Example Input:
"fla"
Example Output:
[ { glyph: Glyph, replaced: [0, 1] }, { glyph: Glyph, replaced: [2] } ]
Motivation and Context
A text editor needs to know which characters the user typed are converted to each glyph, otherwise it is impossible to implement text highlighting and cursor placement.
For example, if a text document has ["f", "l"] and that is transformed into a single glyph for display ["fl"]. Pressing backspace should operate on the original array ["f", "l"] and modify the text document into ["f"]. It can't operate on the "fl" glyph.
How Has This Been Tested?
Beyond the unit tests, checked in editor code https://github.com/opengraphica/opengraphica. This adds a new API that does not touch existing APIs, so it should not negatively impact any other part of the codebase.
Types of changes
Checklist:
npm run test
and all tests passed green (including code styling checks).