# Text rendering off-center in P2D renderer

**URL:** <https://discourse.processing.org/t/text-rendering-off-center-in-p2d-renderer/42947>\
**Category:** Processing\
**Created:** [October 12, 2023, 12:00am UTC](https://discourse.processing.org/t/text-rendering-off-center-in-p2d-renderer/42947 "2023-10-12T00:00:21Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Username3](https://avatars.discourse-cdn.com/v4/letter/u/f9ae1b/32.png) [@Username3](https://discourse.processing.org/u/Username3)\
**Post date:** [October 12, 2023, 12:00am UTC](https://discourse.processing.org/t/text-rendering-off-center-in-p2d-renderer/42947/1 "2023-10-12T00:00:22Z")

</div>

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)

![](https://i.imgur.com/yUW3zli.png)

Does anyone know why it does this? Thanks

Edit:  
Here’s the code for the sketch

```processing
size(854, 480);
background(#000000);
textSize(min(width,height)/2);
textAlign(CENTER, CENTER);
text("Test", width/2,height/2);

```

---

<div class="post-metadata">

**Author:** ![User442B](https://avatars.discourse-cdn.com/v4/letter/u/db5fbb/32.png) [@User442B](https://discourse.processing.org/u/User442B)\
**Post date:** [October 13, 2023, 7:17pm UTC](https://discourse.processing.org/t/text-rendering-off-center-in-p2d-renderer/42947/2 "2023-10-13T19:17:56Z")

</div>

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”

```auto
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);
}

```

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/b/1/b164b0306de2bbc1dd6f384e4b34b86748db84eb.png)

---

<div class="post-metadata">

**Author:** ![Username3](https://avatars.discourse-cdn.com/v4/letter/u/f9ae1b/32.png) [@Username3](https://discourse.processing.org/u/Username3)\
**Post date:** [October 13, 2023, 11:13pm UTC](https://discourse.processing.org/t/text-rendering-off-center-in-p2d-renderer/42947/3 "2023-10-13T23:13:02Z")

</div>

Weird, but thank you

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [October 14, 2023, 12:56pm UTC](https://discourse.processing.org/t/text-rendering-off-center-in-p2d-renderer/42947/4 "2023-10-14T12:56:19Z")

</div>

Hello @Username3,

There is a related issue here:

> <https://github.com/benfry/processing4/issues/768>
>
> 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();
> }
> \`\`\`

`:)`
