Skip to content

Added textAscent and textDescent functions on Webgl#7187

Merged
davepagurek merged 2 commits into
processing:mainfrom
Forchapeatl:patch-6
Sep 20, 2024
Merged

Added textAscent and textDescent functions on Webgl#7187
davepagurek merged 2 commits into
processing:mainfrom
Forchapeatl:patch-6

Conversation

@Forchapeatl
Copy link
Copy Markdown
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
Copy Markdown
Contributor Author

Forchapeatl commented Sep 3, 2024

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

@davepagurek
Copy link
Copy Markdown
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
Copy Markdown
Contributor Author

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

Copy link
Copy Markdown
Collaborator

@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
Copy Markdown
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
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