We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi everyone.
Basically I am using string arrays to generate a piece of text randomly and am calling them into a text function by writing;
text(sentences1[n] + sentences2[m], 400, 400);
Is there any way of colouring the output of each array with a different colour? So that one sentence could be blue and the next red for example?
Answers
You need to call fill() before each array element. The following is an example of the concept:
For the map function, I am printing all the array items across the height of the screen, with a margin of 10 pixels.
Kf
Keyword: kf_keyword text textbox improved text feature
Ok, so how would I be able to get the sentences from the second array to fit onto the end of the ones of the first array. Considering my sentences are of different lengths and I want them to fit into a paragraph seamlessly.
Thanks for the help!
You can use textWidth() to find out the width of a string with current font and fontsize settings. Here is an example:
Getting a little more complex, if you have multiple lines and ant to handle word-wrapping. But this might be a starting point.
Thanks for the reply.
This is very helpful to see, but you were spot on about having multiple lines. If I attempt to make the sentences longer there is no way I can see of having the second sentence continue on the line bellow, as each text function within the drawText function is treated as separate.
I get the effect above.
Any thoughts on fixing this?
Thanks!
Ok so this is my code so far...
`` int n;
int m;
int time = millis();
float wait1 = random(3000, 4000);
float wait2 = random(3000, 4000);
String [] sentences1 = { "And the man said that he wasnt happy.", "We Travelled three thousand miles.", "God sent to us a blessing that shook the ground.", "To dwell in compassion is the absolute." };
String[] sentences2 = {" Forward to victory over the pigs!", " Do not march quickly toward the valley of damnation", " Lucozade is arrogant"};
void setup() { background(0); fullScreen();
}
void draw() {
background(0);
if(millis() - time >= wait1){
n = int(random(3)); / time = millis();
}
if(millis() - time >= wait2){ m = int(random(2)); time = millis();
}
textSize(26); text(sentences1[n] + sentences2[m],width/3, height/3, 400, 400);
}``
The sentences are just random nonsense, but what I am aiming for is to have the sentences from different arrays have different colours.
My approach:
Try implementing it yourself, and tell us about the results.
Would this work using a randomised array ? So if I am randomly getting sentences of different lengths from the array. Is this method not contingent on the selection remaining the same?
@jabobob -- The left margin problem means that you can't allow line wrapping, ever. Instead you probably need to implement text wrapping for single-line
text()
blocks using a bricklaying algorithm with partial-bricks.For example:
Edit: simplified this for clarity
This should with text of any length, although the checking method is really inefficient if all your text is paragraph length -- in that case you might want to pre-chunk your text or do a better pre-approximation of line length.
Or, to make all of this much much less fiddly, use a fixed-distance font.
This is my visually desired goal, with the sentences being pulled randomly from arrays at time intervals.
@jabobob -- right, that's why I described text wrap through bricklaying. That should solve your problem. I've updated my description above to simplify it, so that you aren't dynamically searching for wrap points -- instead you are printing each word with a separate call to
text()
.Your example screenshot includes dynamic in-word hyphenation. Implementing rules for acceptable hyphenation is going to be a bit tricky -- you might want to add it after everything else works.
Thanks for the help, guna try to figure this out and see what I can come up with.
@jeremydouglass - What would you recommend is "acceptable" in-word hyphenation?
@Lord_of_the_Galaxy -- automatic hyphenation is hard. There are many rules in English, and one rule in "between syllables" -- and English syllable detection is very hard to code! For example, you can only break the word "syllables" like this:
...but not like this:
https://www.howmanysyllables.com/words/syllables
For a small art project with a limited number of sentences you might just keep a lookup list of every word in the project text and how it could be hyphenated. Or you could just do the hyphenation wrong by breaking words at arbitrary letter lengths. However, this will create "bad breaks" like:
If that isn't a big problem, then hyphenation can be relatively simple. If it is a problem, it is complex, and even professional auto-hyphenation software often gets it wrong.
The RiTa library might have some tools to help with this? I'm not sure.
@jeremydouglass I didn't know that hyphenation is so complex. Isn't it amazing how we can do it without much thought.....
No hyphenation, but I ran a test and this is definitely doable otherwise, using the approach exactly as described by either @Lord_of_the_Galaxy or me. @jabobob, feel free to share your latest progress for feedback -- and be sure to format your code
Looks beautiful. Could try implementing with hyphenation? Thanks in advance.
@Lord_of_the_Galaxy -- Ha! ;)
Im still struggling to work it out, ive created a brick laying structure for sentences but I am trying to figure out how to increment the position of individual words from a list without the code being too long. This is my first project in programming so im learning as I go. You got any more tips?
I'll post my code so far tonight.
This is where I am at with it, I dont know whether this is the fastest way to do it or not but its working using a couple sentences. I know that this wont work with an array of sentences that are longer than a line, which is what I am aiming for, so I am far from finishing.
Any ideas on how to improve what I've done so far?
You should use splitTokens().
Hmm.. not really sure that i understand everything you do there. Seems like too much hard-coded numbers involved. You don't need a 3rd and 4th parameter for text() here, because you want to handle line-breaks for yourself.
Here is an example for only laying out one sentence into a paragraph. Maybe that is a starting point for you.
Ok thanks @benja im going to take a look at your code and compare to mine as I actually have it working. Ive probably used a really overly complicated method. Still i've learnt a lot in the process of this!
It is working, isn't it?
Yep!
Thank you @Lord_of_the_Galaxy , @jeremydouglass and @benja !
Glad to hear it worked out, @jabobob! Re:
If you are willing to share your solution I'm happy to give feedback -- and/or share my solution if you would also like to see it.
Also, if you are still looking to add hyphenation of your strings through an external library, I recently learned that there is a Java implementation of a hyphenation algorithm available here:
https://github.com/mfietz/JHyphenator
It returns a list of valid break-points in words based on the language you select. If you are doing word-at-a-time parsing, you can then break the word at a breakpoint -- or just line-wrap at the word if it doesn't have any breakpoints.
I'm assuming that the library can take a word as input and provide as output the set of points at which the word can be hyphenated. Is that right?
That's right. I haven't tested it, but
Hyphenator.hyphenate(word)
"Returns a list of syllables that indicates at which points the word can be broken with a hyphen."https://github.com/mfietz/JHyphenator/blob/master/src/main/java/de/mfietz/jhyphenator/Hyphenator.java
Note that if you are using p5 instead of Java Processing you can also do this with the "nlp-compromise" library.
Because of recent interest from new users I'm sharing an example sketch, SentenceColoredText.pde, with a simple example function sentenceBox that handles the per-word layout and the changing color.
This general approach could be adapted to work with words and phrases rather than sentence, and to handle bold, italics, text size, keyword highlighting, etc. rather than color.
See the screenshot above: https://forum.processing.org/two/discussion/comment/80952/#Comment_80952
Nice example @jeremydouglass!
I made a small mark up language:
https://forum.processing.org/two/discussion/comment/81436/#latest