I am building a small program using a .csv file to display the text information of each row. The program display one row/line of text for each mouse click…so far so good. I then tried to adapt the type size of each line of text based on the relationship between the number of words of each line of text and the area of the rectangle where the sentence is drawn.
This seems not working as the only sentence I visualize is the name of the csv file, “test1.csv”.
I think that the problem is on this line: sentence = filename.split(" ");
Any suggestion?? Thanks!
String filename= "test1.csv";
String [] sentence;
PFont f;
int n;
float currentSize = 5;
float bestSize = 5;
float sizeIncrement = 0.5;
void setup() {
size(600, 600);
pixelDensity(displayDensity());
colorMode(RGB);
f = createFont("Helvetica", 40);
sentence=loadStrings(filename);
}
void draw() {
background(#1D1D1B);
String i = sentence[n];
sentence = filename.split(" ");
boolean searching = true;
while(searching){
if(testFontSize(currentSize)){
bestSize = currentSize;
currentSize += sizeIncrement;
}else{
searching = false;
}
}
textFont(f);
textSize(bestSize);
textAlign(CENTER);
rectMode(CENTER);
text(i, width/2, height/2,width/2, height/2);
noFill();
//rect(width/2, height/2,width/2, height/2);
}
boolean testFontSize(float s) {
textSize(s);
// calculate max lines
int currentLine = 1;
int maxLines = floor( height/2 / g.textLeading);
boolean fitHeight = true;
int nextWord = 0;
while (fitHeight) {
if (currentLine > maxLines) {
fitHeight = false;
} else {
String temp = sentence[nextWord];
// check if single word is already too wide
if (textWidth(temp)>width/2)
return false;
boolean fitWidth = true;
// add words until string is too wide
while (fitWidth) {
if (textWidth(temp) > width/2) {
currentLine++;
fitWidth = false;
} else {
if (nextWord < sentence.length -1) {
nextWord++;
temp += " "+sentence[nextWord];
} else
return true;
}
}
}
}
return false;
}
void mousePressed() {
n = (n+1) % sentence.length;
redraw();
}
Yes, testFontSize() I think is fine, what I am trying to achieve is that each time I draw a sentence (or more) the rectangle is filled with the sentence(s) at the maximum point size possible without losing any word.
I am sharing the source of the initial code, as I would like to have the same result but using sentences coming from a .csv, where everytime you click on the canvas the senses change from row to row adapting the size to the rectangle area.
String str ="Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.";
int x = 20;
int y = 20;
int w = 250;
int h = 290;
String[] words;
float currentSize = 5;
float bestSize = 5;
float sizeIncrement = 0.5;
void setup() {
size(400, 400);
words = str.split(" ");
boolean searching = true;
while(searching){
if(testFontSize(currentSize)){
bestSize = currentSize;
currentSize += sizeIncrement;
}else{
searching = false;
}
}
println("Best size: "+ bestSize);
textSize(bestSize);
fill(0);
text(str, x, y, w, h);
noFill();
rect(x, y, w, h);
}
boolean testFontSize(float s) {
textSize(s);
// calculate max lines
int currentLine = 1;
int maxLines = floor( h / g.textLeading);
boolean fitHeight = true;
int nextWord = 0;
while (fitHeight) {
if (currentLine > maxLines) {
fitHeight = false;
} else {
String temp = words[nextWord];
// check if single word is already too wide
if (textWidth(temp)>w)
return false;
boolean fitWidth = true;
// add words until string is too wide
while (fitWidth) {
if (textWidth(temp) > w) {
currentLine++;
fitWidth = false;
} else {
if (nextWord < words.length -1) {
nextWord++;
temp += " "+words[nextWord];
} else
return true;
}
}
}
}
return false;
} ```