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

Update PFont.java #6132

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Update PFont.java #6132

wants to merge 1 commit into from

Conversation

dhowe
Copy link
Contributor

@dhowe dhowe commented Dec 5, 2020

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...

Screenshot 2020-12-05 at 9 48 09 PM


// 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);
  }
}  

This PR adds PFont.textBounds and PFont.textToPoints from p5.js, as well as glyphBounds (each 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 fully sure the output is totally correct
@micycle1
Copy link

micycle1 commented Jan 9, 2021

Maybe worth opening a PR for this on Processing 4.

@clankill3r
Copy link

Hi, I think this is really nice. I can see this being used a lot by students from our graphic design department.

Some feedback, I think the Rectangle2D class is super unfriendly for the majority of the processing users.
That is specifically this part: Rectangle2D.Float[].
I think it would be the best to just return a float array.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants