Skip to content

feat: add setMeasureFunction for custom measurement backends#17

Open
intergalacticspacehighway wants to merge 2 commits into
chenglou:mainfrom
intergalacticspacehighway:feat/set-measure-function
Open

feat: add setMeasureFunction for custom measurement backends#17
intergalacticspacehighway wants to merge 2 commits into
chenglou:mainfrom
intergalacticspacehighway:feat/set-measure-function

Conversation

@intergalacticspacehighway
Copy link
Copy Markdown

@intergalacticspacehighway intergalacticspacehighway commented Mar 29, 2026

Why?

  • The idea is borrowed from Yoga which has a concept of measure function for measureable nodes (Text). This allows us to plug pretext into non browser environments like React Native.

How?

Exported setMeasureFunction function which can be used to provide a custom measure function.

Test plan

Alternatives

Alternate solution for cross platform would be to polyfill the OffscreenCanvas. This also works, can just be documented.

globalThis.OffscreenCanvas = class {
  getContext() {
    let currentFont = '';
    return {
      set font(f: string) { currentFont = f; },
      get font() { return currentFont; },
      measureText(text: string) {
        return { width: MyMeasureFunction() };
      }
    };
  }
};

Comment thread src/measurement.ts
if (cachedEngineProfile !== null) return cachedEngineProfile

if (typeof navigator === 'undefined') {
if (typeof navigator === 'undefined' || typeof navigator.userAgent !== 'string') {
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

React native has a navigator object but no userAgent string 😅

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