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

textAscent() and textDescent() broken if textSize > 200

Open JuanIrache opened this issue 6 months ago • 15 comments

Most appropriate sub-area of p5.js?

  • [ ] Accessibility
  • [ ] Color
  • [ ] Core/Environment/Rendering
  • [ ] Data
  • [ ] DOM
  • [ ] Events
  • [ ] Image
  • [ ] IO
  • [ ] Math
  • [x] Typography
  • [ ] Utilities
  • [ ] p5.strands
  • [ ] WebGL
  • [ ] DevOps, Build process, Unit testing
  • [ ] Internationalization (i18n)
  • [ ] Friendly Errors
  • [ ] Other (specify if possible)

p5.js version

1.11.13

Web browser and version

Chrome latest

Operating system

MacOS

Steps to reproduce this

Steps:

  1. textSize(201)
  2. textDescent()

Snippet:

https://editor.p5js.org/JuanIrache/sketches/2VVe5sram

When text size is larger than 200, trextAscent() and textDescent() break. Their total sum is correct, but they don't point to the top and bottom of the font any more.

JuanIrache avatar May 08 '26 10:05 JuanIrache

Do you know if this happens in p5 2.x too?

davepagurek avatar May 10 '26 12:05 davepagurek

V2 is fine

JuanIrache avatar May 10 '26 12:05 JuanIrache

Cool, I think we can look into what a fix might look like, but since 1.x is reaching a point where we are no longer doing significant refactors to it and are only doing bug fixes, we'll need to consider how feasible a 1.x fix would be. If someone wants to look into this and finds that a small scoped change is sufficient then that would be fine though!

davepagurek avatar May 10 '26 12:05 davepagurek

I will look into it. What I do in my setup is replace it with a function that checks the font size, if it’s smaller than 150 it returns the standard textAscent and textDescent, otherwise it remembers the sum of the two and the text size, changes text size to a small value, gets ascent and descent again, computes their proportions over the total, multiplies those proportions by the stored original total to get new (good) ascent and descent, and restores the original text size. Hacky but seems to work fine.

Message ID: @.***>

JuanIrache avatar May 10 '26 12:05 JuanIrache

Thanks @JuanIrache!

davepagurek avatar May 10 '26 12:05 davepagurek

So far I narrowed the problem down to the calculateOffset(object) function in src/core/p5.Renderer.js

The object.offsetLeft and object.offsetTop values are incorrect when textSize is bigger than certain values, but I don't know why (maybe some outdated DOM-related dependency? as this code is the same in v2), and the textSize value where results change from good to bad seems to change depending on the environment (I think probably screen pixel density), so It's hard to tell for sure when a workaround needs to happen.

I'm not sure I have the time to work on a solution right now. Maybe someone that knows the codebase better is be able to solve this easily.

JuanIrache avatar May 11 '26 12:05 JuanIrache

Thanks for taking a look into it @JuanIrache! I didn't realize we were also using the DOM to measure that in 1.x, not just 2.x. I wonder if the cutoff point, 200, is related to the size of any of the container elements used in the measuring. Could also be worth comparing to v2 to see what's different in our measurement to narrow down why one works but the other doesn't.

davepagurek avatar May 12 '26 15:05 davepagurek

I took a quick look, and I think 2.x has different implementation from 1.x.

Both approaches are mentioned in this stackoverflow answer https://stackoverflow.com/questions/1134586/how-can-you-find-the-height-of-text-on-an-html-canvas/25355178

1.x implementation

1.x drew some invisible DOM elements to measure ascender, descender height based on the left, top offset of boxes.

https://github.com/processing/p5.js/blob/7efbb2e30f796b893d11fe3e370c5c75828b599e/src/core/p5.Renderer.js#L524-L537

https://github.com/processing/p5.js/blob/7efbb2e30f796b893d11fe3e370c5c75828b599e/src/core/p5.Renderer.js#L475-L519

2.x implementation

1.x implementations are gone, and browser's measureText API is used

https://github.com/processing/p5.js/blob/40e80169d167e46eb406a199a5244e6af35cae54/src/type/textCore.js#L1428-L1431

https://github.com/processing/p5.js/blob/40e80169d167e46eb406a199a5244e6af35cae54/src/type/textCore.js#L1447-L1450

Note

I think 2.x solution can be applied to 1.x.

_updateTextMetrics was removed at 3dd571c925082e92c64f397ce8d9487e1f5f0e94, maybe we can see what happened back then. The issue was related to some google fonts not loading.

Also, calculateOffset could be removed from 2.x if it's no longer needed

eupthere avatar May 12 '26 17:05 eupthere

I think that commit removing _updateTextMetrics was just cleaning up unused code, as the renderer text methods were already being overridden in textCore.js, e.g.: https://github.com/dhowe/p5.js/blob/3dd571c925082e92c64f397ce8d9487e1f5f0e94/src/type/textCore.js#L173-L176

So it sounds like basically 2.x just uses a fundamentally different approach here. Since 1.x is going to be sunset soon, I'm a bit hesitant to go and change the approach to measurement in 1.x, as it would mean adding in all the code translating font styles to the canvas, and having an offscreen 2D canvas used for text measurement in the WebGL renderer. @ksen0 and @dhowe let me know what your thoughts are there too on our approach to 1.x bugfixes and the size/risk of a change -- basically we don't want to accidentally introduce new incompatibilities or bugs due to a bigger switch.

A possible other approach could be to see if there's a minimal change to the existing text measurement of 1.x that removes this 200 limit, e.g. if some property of a parent element is cutting off parts of child elements.

davepagurek avatar May 13 '26 13:05 davepagurek

I've been avoiding making any changes to 1.x code unless it is critical

dhowe avatar May 13 '26 13:05 dhowe

I’d like to investigate and fix this typography bug. I can reproduce the >200 textSize failure, patch the metric path, and add tests for large-size text behavior.

puneetdixit200 avatar May 16 '26 07:05 puneetdixit200

@davepagurek @dhowe sounds to me that what the issue describes is more of a "limitation" in 1.x rather than a "bug." We have already feature-frozen 1.x with only bugfixes considered, and soon will also freeze bugfixes. I would also worry about sideeffects that a fix to this would introduce, which we would not have capacity to properly address. So I also think that this change should not be made in 1.x.

Maybe its worth it to document the limitation though? On textAscent and textDescent note "supported for textSize up to 200"?

ksen0 avatar May 16 '26 14:05 ksen0

Hi, I’m a first-time contributor. I read the discussion and understand that the intended change is to document the p5.js 1.x limitation rather than modify the implementation. Since the earlier volunteer was investigating a code fix, is the documentation task still available? If so, I’d like to work on it and be assigned.

dubeyharshit0605 avatar Jul 18 '26 05:07 dubeyharshit0605

@JuanIrache

dubeyharshit0605 avatar Jul 18 '26 05:07 dubeyharshit0605

I haven't been paying attention to this lately, sorry. Since this won't be fixed, I settled for my local workaround.

JuanIrache avatar Jul 18 '26 09:07 JuanIrache