Loading...
Logo
Processing Forum
textDescent() seems to work very unreliably for the same character across different sizes, even when using a scalar as in the example.  Is there a simpler way or a trick that I'm missing?

Replies(3)

Can you show a simple example illustrating what you try to do and the bad behavior of textDescent()?
Thanks PhiLho - I've solved the issue for the same character (I think - I haven't tested it for every character), so I've modified the code to illustrate the essential problem - if I have a class that might be any character - 'o' or 'p' or 'd' or '☻' - how can I ensure that it will appear to rest with its bottom flush at a certain position?

I assume the solution is to include scalar in the class but I'm not sure how to programmatically set it for each character.
Copy code
  1. float scalar = 0.0;
  2. ArrayList<Ball> balls;

  3. void setup() {
  4.   size(640, 480);
  5.   balls = new ArrayList();
  6.   for (int i = 0; i < 50; i++) {
  7.     balls.add(new Ball());
  8.   }
  9. }

  10. void draw() {
  11.   background(0);
  12.   for (int i = 0; i < 50; i++) {
  13.     balls.get(i).put();
  14.   }
  15. }

  16. class Ball {
  17.   char a;
  18.   float x, y, size, desc;
  19.   Ball () {
  20.     a = char(ceil(random(80)) + 32);
  21.     y = 0;
  22.     size = random(64) + 1;
  23.     x = random(width - size);
  24.     desc = random(10)+1;
  25.   }

  26.   void put() {
  27.     textSize(size);
  28.     if (y < height - textDescent() * scalar) y += desc;
  29.     else y = height - textDescent() * scalar;
  30.     text(a, x, y);
  31.   }
  32. }
Here is an even more illustrative example that the whole principle of relying on the y coordinate of the text() along with textDescent() does not work correctly across font sizes.  There must be a simpler way of determining the bottom-most pixel of a character!

Copy code
  1. size(400,400);
  2. background(0);
  3. stroke(255,0,0);
  4. float base = height * 0.75;
  5. float scalar = 0.8; // Different for each font
  6. String c = "oQq☻";
  7. textSize(32);  // Set initial text size
  8. float a = textDescent() * scalar;  // Calc ascent
  9. line(0, base+a, width, base+a);
  10. text(c, 0, base);  // Draw text on baseline

  11. textSize(64);  // Increase text size
  12. a = textDescent() * scalar;  // Recalc ascent
  13. line(200, base+a, width, base+a);
  14. text(c, 200, base);  // Draw text on baseline

  15. base = height * 0.25;

  16. textSize(128);  // Set initial text size
  17. a = textDescent() * scalar;  // Calc ascent
  18. stroke(0,255,0);
  19. line(0, base+a, width, base+a);
  20. stroke(0,0,255);
  21. line(0,base,width,base);
  22. text(c, 0, base);  // Draw text on baseline