p5.js icon indicating copy to clipboard operation
p5.js copied to clipboard

Improve Text Measuring API

Open araid opened this issue 5 months ago • 3 comments

Increasing access

This would make the text API more redundant and accessible to those not familiar with OOP.

Most appropriate sub-area of p5.js?

  • [ ] Accessibility
  • [ ] Color
  • [ ] Core/Environment/Rendering
  • [ ] Data
  • [ ] DOM
  • [ ] Events
  • [ ] Image
  • [ ] IO
  • [ ] Math
  • [X] Typography
  • [ ] Utilities
  • [ ] WebGL
  • [ ] Build process
  • [ ] Unit testing
  • [ ] Internationalization
  • [ ] Friendly errors
  • [ ] Other (specify if possible)

Feature enhancement details

Problem

Right now, there are 2 ways to measure text in p5.js: textWidth() and Font.textBounds(). These 2 calls sit in different parts of the API, and have slightly different behaviors across renderers and font types:

textWidth textBounds
P2D system font
P2D opentype
WGL system font
WGL opentype

A couple additional issues:

  • textWidth and textBounds.w show slightly different values (bounds often feels like the more precise one)
  • textWidth on WebGL + system font returns 0 with no error

Suggestion

This separation of functionality feels arbitrary and potentially confusing to new users. These small changes could be implemented as part of the 2.0 typography push or just as an improvement to the current API:

  1. Make both functions available in the global scope and in p5.Font. This should ensure backwards compatibility.

    • Add textWidth to p5.Font
    • Add textBounds to p5.Renderer
  2. Allow textBounds to work with system fonts in P2D by using the native measureText

araid avatar Sep 23 '24 04:09 araid