Skip to content
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

Added textAscent and textDescent functions on Webgl #7187

Merged
merged 2 commits into from
Sep 20, 2024

Conversation

Forchapeatl
Copy link
Contributor

@Forchapeatl Forchapeatl commented Aug 20, 2024

Added textAscent and textDescent functions on Webgl

Resolves #4958
Changes:

Screenshots of the change:

Before :

image

After :
image

example

let font;

function preload() {
  font = loadFont('consola_mono/ConsolaMono-Bold.ttf'); 
}

function setup() {
  createCanvas(900, 900, WEBGL);
  textFont(font); // Set the loaded font
}

function draw() {
  translate(-width/2, -height/2);
  background(0);
  fill('white');
  
  textSize(12);
  text('WEBGL : textAscent', 200, 20);
  text('12: ' + textAscent(), 20, 20);
  
  textSize(14);
  text('14: ' + textAscent(), 20, 40);
  
  textSize(16);
  text('16: ' + textAscent(), 20, 60);
  
  textSize(32);
  text('32: ' + textAscent(), 20, 100);
}

PR Checklist

@Forchapeatl Forchapeatl marked this pull request as ready for review August 22, 2024 10:20
@Forchapeatl
Copy link
Contributor Author

Forchapeatl commented Sep 3, 2024

@davepagurek , Please for a review. I hope you don't mind.

@davepagurek
Copy link
Contributor

Thanks for making the changes! Looks like this code will work, which is great. Currently it will recreate the value every time instead of using the same caching flow as 2D mode, so it's probably worth looking into whether we can get the two systems working the same way. If there are any unforeseen issues when we look into that, we can fall back to the technique you're using here.

It seems the underlying issue is that 2D mode sets the cached ascent and descent values back to null when font things change, via _applyTextProperties:

_applyTextProperties() {
let font;
const p = this._pInst;
this._setProperty('_textAscent', null);
this._setProperty('_textDescent', null);

Meanwhile, the WebGL implementation of this is empty:

p5.js/src/webgl/text.js

Lines 8 to 11 in 0b01116

p5.RendererGL.prototype._applyTextProperties = function() {
//@TODO finish implementation
//console.error('text commands not yet implemented in webgl');
};

So rather than overriding the ascent and descent functions, we just need to also set them to null in the WebGL implementation of _applyTextProperties?

@Forchapeatl
Copy link
Contributor Author

Hi @davepagurek , thank you for the help. I have done as instructed.

Copy link
Contributor

@perminder-17 perminder-17 left a comment

Choose a reason for hiding this comment

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

Thanks for solving a long lasting issue haha ! Looks good to me.

Copy link
Contributor

@davepagurek davepagurek left a comment

Choose a reason for hiding this comment

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

Looks good, thanks!

@davepagurek davepagurek merged commit 7e20954 into processing:main Sep 20, 2024
2 checks passed
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.

in Webgl Mode, textAscent() stays sticked to the first call result and is never updated.
3 participants