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

Centering text vertically does not work correctly #739

Closed
ThexXTURBOXx opened this issue Jul 5, 2023 · 1 comment
Closed

Centering text vertically does not work correctly #739

ThexXTURBOXx opened this issue Jul 5, 2023 · 1 comment

Comments

@ThexXTURBOXx
Copy link

Description

textAlign(CENTER, CENTER) does not center text vertically correctly

Expected Behavior

After textAlign(CENTER, CENTER), text should be aligned in the center.

Current Behavior

The text is not centered vertically:

grafik

Steps to Reproduce

  1. Run the following sketch:
    void setup() {
      size(100,100);
    }
    
    void draw() {
      background(0);
      textAlign(CENTER,CENTER);
      textSize(100);
      text("Ap", width/2, height/2);
    }

Your Environment

  • Processing version: 4.2
  • Operating System and OS version: Windows 11 22621.1928

Possible Causes / Solutions

The issue lies most likely somewhere within these lines: https://github.com/processing/processing4/blob/main/core/src/processing/core/PGraphics.java#L4920-L4926
But I am not sure

@ThexXTURBOXx ThexXTURBOXx changed the title Centering text vertically does not work Centering text vertically does not work correctly Jul 7, 2023
@SableRaf
Copy link
Contributor

SableRaf commented Jul 7, 2023

Hi @ThexXTURBOXx,

Thanks for raising this issue. You're correct that Processing's vertical alignment may not center text perfectly due to the way it handles characters that extend above or below the baseline.

A potential workaround involves calculating the full height of the text, including both ascent and descent, and adjusting the y-position accordingly. However, this method is not ideal as the ascent and descent are based on the tallest and deepest characters (d and p), which may not represent the actual height of your specific string.

A more accurate workaround to get the exact string height involves generating a vector representation for each character, identifying the vertices with the highest and lowest Y positions.

I would recommend taking a look at this excellent StackOverflow post by @micycle. It provides a detailed explanation on this matter and presents potential solutions.

@benfry benfry closed this as completed in c57069f Jul 16, 2023
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 16, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants