Skip to content

Conversation

NalinDalal
Copy link
Contributor

Resolves #7745

This pull request introduces a new user-facing function, fontWidth(), to the p5.js library and updates the documentation to clearly distinguish between advance width and tight bounding box measurements for text. All changes are made in src/typography/attributes.js.

Changes

  • New Function:
    • fontWidth() returns the advance width (loose measurement) of a string, including all leading and trailing whitespace, as measured by the browser's measureText().width.
  • Documentation Updates:
    • Added comprehensive JSDoc and usage examples for fontWidth().
    • Updated textWidth() documentation to clarify that it returns the tight bounding box width, which may ignore leading/trailing whitespace.
    • Both docs now include a direct comparison, cross-references, and clear examples to help users choose the correct function.
  • No changes to p5.Font.js:
    • All documentation and code changes are limited to attributes.js as per maintainer feedback.

Motivation

  • Addresses confusion in p5.js 2.0 about the difference between textWidth() and the new fontWidth() regarding whitespace handling.
  • Provides clear, side-by-side documentation for both functions to help users understand which measurement to use.
  • Implements feedback from maintainers to keep documentation changes only in attributes.js.

Example Usage

let s = '   x y   ';
let advance = fontWidth(s); // includes all whitespace
let tight = textWidth(s);   // may ignore whitespace

Related Issues

  • Resolves: [GitHub issue about textWidth vs fontWidth whitespace handling]

Checklist

  • Added new function fontWidth()
  • Updated documentation and examples in attributes.js
  • No changes to p5.Font.js
  • All changes on dev-2.0 branch

This PR ensures p5.js 2.0 users have clear, accurate, and maintainers-aligned documentation for text measurement functions.

PR Checklist

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