Get location of added letter to FormattedString
I'm adding letters to a FormattedString one by one (with append), but I want to save the position of each letter when I do that. Is something like this possible, and how would I go about it?
It doesn't even have to be a FormattedString per se. I could also just type the text beforehand (because it is always centered), and then get the position of each letter.
Thanks in advance!
I don't know what you are trying to do but
textBoxCharacterBoundsis really handy to retrieve typesetted information.
help(textBoxCharacterBounds) t = FormattedString() t.fontSize(50) t += "hello" t.font("Times") t.fill(1, 1, 0) t += " world" t.font("Times-Bold") t.fill(1, 0, 0) t += " fooj" textBox(t, (0, 0, 400, 400)) textRects = textBoxCharacterBounds(t, (0, 400, 400, 400)) for textRect in textRects: x, y, w, h = textRect.bounds fill(random(), random(), random(), .3) rect(x, y - textRect.baselineOffset, w, h) text(textRect.formattedSubString, (x, y))
Hey @frederik, always happy when you reply because you seem to be right all the time haha.
It sounds like textBoxCharacterBounds is about what I was looking for, but I want to do the following:
I have a word or sentence and put it in a font, align it center and now I want to have the actual x, y position of every letter on the screen. From your code it looks like it should be possible with textBoxCharacterBounds, but I want it separately for every letter.
Because later on I use these positions to check PNG's I load every frame to determine what the weight of every letter should be. That last part is working, but now I put the location of every letter by hand. But I of course want to automatically get the position of every letter in a string because it will be variabel. Does that makes sense?
Looking at your code, it should be doable by maybe changing the fill for every letter, right? That makes every letter an independant textBox.
I only use it virtually in the beginning to get the position, I don't actually draw the letters then anyway, so the fill won't influence the end result. Will try to do this, thanks again!
indeed, letters are collected into glyph runs with the same drawing attributes, so if you change a thing (like fill color) then you will get a box for each letter.