Skip to content

Add method to retrieve information about what characters in the original string were replaced by each glyph. #798

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

opengraphica
Copy link

@opengraphica opengraphica commented Mar 24, 2025

Add a stringToGlyphMapping API to font.mjs to retrieve information about what characters in the original string were replaced by each glyph.

Description

There exists stringToGlyphIndexes and stringToGlyphs 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

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist:

  • I did npm run test and all tests passed green (including code styling checks).
  • I have added tests to cover my changes.
  • My change requires a change to the documentation.
  • I have updated the README accordingly.
  • I have read the Contribute README section.

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.

1 participant