Update PFont.java by dhowe · Pull Request #6132 · processing/processing
This PR adds PFont.textBounds, PFont.textToPoints from p5.js, and PFont.glyphBounds, as shown in the sketch below (there may be more efficient ways to do these). Also, I attempted to use the suggested formatter in Eclipse, but not sure the output is totally correct -- happy to get feedback/improvements...
// DEMO SKETCH
import java.awt.geom.*;
String str = "Lorem ipsum.";
float x = 20, y = 250, fsize = 150;
PFont pf;
void setup() {
size(1000, 400);
background(240);
pf = createFont("Georgia", 300);
fill(0);
textFont(pf);
textSize(fsize);
text(str, x, y);
noFill();
Rectangle2D.Float[] r = pf.glyphBounds(str, x, y, fsize);
for (int i = 0; i < str.length(); i++) {
rect(r[i].x, r[i].y, r[i].width, r[i].height);
}
Rectangle2D.Float rb = pf.textBounds(str, x, y, fsize);
rect(rb.x, rb.y, rb.width, rb.height);
stroke(200,0,0);
strokeWeight(3);
PVector[] pts = pf.textToPoints(str, x, y, fsize, 0.5);
for (int i = 0; i < pts.length; i++) {
point(pts[i].x, pts[i].y);
}
}