When I try to draw text in the P2D renderer, it draws slightly below where I want it, and the line spacing is greatly increased compared to the default renderer (see the image)
Does anyone know why it does this? Thanks
Edit:
Here’s the code for the sketch
size(854, 480);
background(#000000);
textSize(min(width,height)/2);
textAlign(CENTER, CENTER);
text("Test", width/2,height/2);
2 Likes
It seems like P2D treats the text as having a larger vertical size than it appears. I did find a work around/solution. If you create a font and then use that font, the text is centered as expected with P2D.
You can create a font by going to “Tools” then “Create Font”
PFont arial;
void setup(){
size(854, 480,P2D);
background(#000000);
arial = loadFont("Arial-Black-48.vlw");
}
void draw(){
textFont(arial);
textSize(min(width,height)/2);
textAlign(CENTER, CENTER);
text("Test", width/2,height/2);
}
1 Like
glv
October 14, 2023, 12:56pm
4
Hello @Username3 ,
There is a related issue here:
opened 08:08PM - 10 Aug 23 UTC
closed 10:05PM - 10 Aug 23 UTC
Processing 4.2:
![mask_4 2](https://github.com/processing/processing4/assets/… 738650/a914e1fd-64b0-4d70-8cbb-46350991aa14)
Processing 4.3:
![mask_4 3](https://github.com/processing/processing4/assets/738650/70c74593-9436-44d4-a175-e24ef38c461b)
Ok, I will skip the part explaining that I'm an idiot.
The above difference comes from the following revision in 4.3:
> "Use calculated text height instead of OS ascent for better vertical centering. Note: this may cause some sketches to look slightly different if textAlign(..., CENTER) is being used."
https://github.com/processing/processing4/issues/739
Which is amazing it has been fixed!
Downside is that for OpenGL it's still like the old way.
Looking at the code it seems an easy fix (same as the fix for JAVA2D), which is commenting most code so it's just using `super.textAscent();`
https://github.com/processing/processing4/blob/c57069fdca888e614bf52caa4cb7999277ae32e0/core/src/processing/opengl/PGraphicsOpenGL.java#L3430C14-L3430C14
If such a fix would be implemented, then one extra thing that could be considered is deprecating the following two functions in `PJOGL.java`:
```java
@Override
protected int getFontAscent(Object font) {
return getFontMetrics((Font) font).getAscent();
}
@Override
protected int getFontDescent(Object font) {
return getFontMetrics((Font) font).getDescent();
}
```
:)