How to use textWidth on a text generated with Geomerative library?

Hello everybody,
I am looking for a solution to get the width of each of my sentences written with the Geomerative library, is someone know how to proceed?! Thanks a lot in advance for your help!
Best, L

Maybe not relevant How to make a String of any length fit within text box? - Processing 2.x and 3.x Forum

Now, this is what you need. I found it this way:

Libraries / Processing.org

Then

Geomerative

Then I have to found the docs which should be in the folder of the library. I modify the example based on this doc:

RPoint
getBottomLeft() 
Use this method to get the bottom left position of the element.

RPoint
getBottomRight() 
Use this method to get the bottom right position of the element.

geomerative.RRectangle
getBounds() 
Use this method to get the bounding box of the element.

RPoint[]
getBoundsPoints() 
Use this method to get the points of the bounding box of the element.

RPoint
getCenter() 
Use this method to get the center point of the element.

Example below.

Kf

import geomerative.*;

// Declare the objects we are going to use, so that they are accesible from setup() and from draw()
RFont f;
RShape grp;

void setup(){
  // Initilaize the sketch
  size(600,400);
  frameRate(24);

  // VERY IMPORTANT: Allways initialize the library in the setup
  RG.init(this);

  // Choice of colors
  background(255);
  fill(255,102,0);
  stroke(0);
  
  //  Load the font file we want to use (the file must be in the data folder in the sketch floder), with the size 60 and the alignment CENTER
  grp = RG.getText("Hello world!", "FreeSans.ttf", 72, CENTER);

  // Enable smoothing
  smooth();
}

void draw(){
  // Clean frame
  background(255);
  
  // Set the origin to draw in the middle of the sketch
  translate(width/2, height/2);
  
  // Transform at each frame the first letter with a PI/20 radians 
  // rotation around the center of the first letter's center
  grp.children[0].rotate(PI/20, grp.children[0].getCenter());
  
  println(grp.children[0].getCenter());
  println(grp.children[0].getBottomLeft());
  println(grp.children[0].getBottomRight());
  
  pushStyle();
  noStroke();
  
  //Red on right of fifth char
  fill(255,0,0);
  ellipse(grp.children[4].getBottomRight().x,grp.children[4].getBottomRight().y,5,5);
  
  //Blue on left  of fifth char
  fill(0,0,250);
  ellipse(grp.children[4].getBottomLeft().x,grp.children[4].getBottomLeft().y,5,5);
  popStyle();
  
  
  // Draw the group of shapes representing "Hola mundo!" on the PGraphics canvas g (which is the default canvas of the sketch)
  grp.draw();
}
1 Like

Dear kfrajer ,
Thank you very much for you answer and for the time you spent looking for the documentation.
I will try to use the methods you suggest:

looping over the list of my phrases with and index it might work. Asap I will make a test, certainly on monday and will let you know if I managed to solve my problem… Thank you very much for your precious help.Have a good week end.
L

Dear Kfrajer,

Here is my attempt:

1rst when I create my group of points with geomerative, I use the functions: myGroup.getbottomLeft()
myGroup.getbottomRight()
2nd I create two variable called bottomLeftPointsX and bottomRightPointsX

//---------------TEXT ATTRACTOR------------------------------------------------------------------------------------------------------------------------------------------
void createPhrasesPoints(String phrases) {
phraseSetPoints = new RPoint[phrases.length];
for (int i =0; i<phrases.length; i++) {
RGroup myGroup = font.toGroup(phrases[i]);
textW = myGroup.getWidth();
//phraseW = textWidth(phrases[i]);
myGroup = myGroup.toPolygonGroup();
phraseSetPoints[i] = myGroup.getPoints();
myGroup.getBottomLeft();
myGroup.getBottomRight();
bottomLeftPointsX = myGroup.getBottomLeft().x;
// bottomLeftPointsY = myGroup.getBottomLeft().y;
bottomRightPointsX = myGroup.getBottomRight().x;
//bottomRightPointsY = myGroup.getBottomRight().y;

println(myGroup.getBottomLeft().x);
println(myGroup.getBottomRight().x);

}
}

3rd I pass the coordinates bottomLeftPointsX and bottomRightPointsX to my attractors:

void wordAttractorToSound() {

AudioPlayer s = sounds[indexPhraseSet][indexPhrase];
initAttractor(indexPhrase);
//wAttractor.moveTo(map(s.position(), 0, s.length()-1000, 0, width-100)-width/1.5, bandHeight/8-300);
wAttractor.moveTo(map(s.position(), 0, s.length(), bottomLeftPointsX-width/2, bottomRightPointsX), bandHeight/8-300); 
wAttractor.attract();

}

void linesAttractor() {
AudioPlayer s = sounds[indexPhraseSet][indexPhrase];
if (phraseSet[indexPhraseSet].length==4) {
updateAttractorLines( attractor_Lines.x = map(s.position(), 200, s.length(), bottomLeftPointsX+width/2,bottomRightPointsX+width/2), linesYPositions1[indexPhrase]);
} else if (phraseSet[indexPhraseSet].length==3) {
updateAttractorLines( attractor_Lines.x = map(s.position(), 200, s.length(), bottomLeftPointsX+width/2, bottomRightPointsX+width/2), linesYPositions2[indexPhrase]);
}
for (int j = 0; j<myNodes.length; j++) {
attractor_Lines.attract_Lines(myNodes[j]);
myNodes[j].update();
}
}

It somehow works, but the words attractor is always late compare to the lines_attractor and the lines_attractor doesn’t begin where it should but in the middle of a phrase (phrase n°2 / stanza 1) Can I send you the whole code?! If eventualy you can check out why both attractors are not synchronised? I’d be very glad. Thanks a lot
Best wishes,
L

Hi Kfrajer,
I think I manage to use the getBottomLeft() and getBottomRight() methods but somehow my attractor
related to my text generated with Geomerative is not at the wright place?! Could you help me please to solve this problem I can send you the code here or on github. Thanks a lot in advance. Best, L

Format your post. You can edit your previous post, select your code and hit the </> button. Save the changes.

Kf

Dear Kfrajer,
Here is my previous message formated…
Thanks.

Here is my attempt:

1rst when I create my group of points with geomerative, I use the functions: myGroup.getbottomLeft()
myGroup.getbottomRight()
2nd I create two variable called bottomLeftPointsX and bottomRightPointsX

//---------------TEXT ATTRACTOR------------------------------------------------------------------------------------------------------------------------------------------
void createPhrasesPoints(String []phrases) {
phraseSetPoints = new RPoint[phrases.length][];
for (int i =0; i<phrases.length; i++) {
RGroup myGroup = font.toGroup(phrases[i]);
textW = myGroup.getWidth();
//phraseW = textWidth(phrases[i]);
myGroup = myGroup.toPolygonGroup();
phraseSetPoints[i] = myGroup.getPoints();
myGroup.getBottomLeft();
myGroup.getBottomRight();
bottomLeftPointsX = myGroup.getBottomLeft().x;
// bottomLeftPointsY = myGroup.getBottomLeft().y;
bottomRightPointsX = myGroup.getBottomRight().x;
//bottomRightPointsY = myGroup.getBottomRight().y;

println(myGroup.getBottomLeft().x);
println(myGroup.getBottomRight().x);

}
}

3rd I pass the coordinates bottomLeftPointsX and bottomRightPointsX to my attractors:

void wordAttractorToSound() {

AudioPlayer s = sounds[indexPhraseSet][indexPhrase];
initAttractor(indexPhrase);
//wAttractor.moveTo(map(s.position(), 0, s.length()-1000, 0, width-100)-width/1.5, bandHeight/8-300);
wAttractor.moveTo(map(s.position(), 0, s.length(), bottomLeftPointsX-width/2, bottomRightPointsX), bandHeight/8-300); 
wAttractor.attract();

}

void linesAttractor() {
AudioPlayer s = sounds[indexPhraseSet][indexPhrase];
if (phraseSet[indexPhraseSet].length==4) {
updateAttractorLines( attractor_Lines.x = map(s.position(), 200, s.length(), bottomLeftPointsX+width/2,bottomRightPointsX+width/2), linesYPositions1[indexPhrase]);
} else if (phraseSet[indexPhraseSet].length==3) {
updateAttractorLines( attractor_Lines.x = map(s.position(), 200, s.length(), bottomLeftPointsX+width/2, bottomRightPointsX+width/2), linesYPositions2[indexPhrase]);
}
for (int j = 0; j<myNodes.length; j++) {
attractor_Lines.attract_Lines(myNodes[j]);
myNodes[j].update();
}
}

It somehow works, but the words attractor is always late compare to the lines_attractor and the lines_attractor doesn’t begin where it should but in the middle of a phrase (phrase n°2 / stanza 1) Can I send you the whole code?! If eventualy you can check out why both attractors are not synchronised? I’d be very glad. Thanks a lot
Best wishes,
L