Method to cut a string based on font and available width

Hi all,

I would like to know if you have a method to return a cut position inside a (long) string given a width in pixel and a font.
I thought about making a simple iterative like :

for (i = 0; i < hugeText; i++)
{
   if (font.getStringWidth(hugeText.subtring(0, i)) > availableWidth) return i-1;
}

But I guess it’s clearly inefficient (if the string is huge, it’s O(N) intensive operations).

I don’t understand exactly how the font kerning and letter width work, but would it be possible to extract a letter width in a lookup table, or are they dependent on the previous and the next one ?
Else, I’ve thought about a dichotomic solution, but if the lookup solution works I think it’s easier to implement.
Else, should I add a method in Font for this, so I can copy and adapt getStringWidthFloat() method ?

The kerning all depends on the pair of adjacent letters. Have you looked at the functions available in GlyphArrangement to see if any of those do what you need?

As always, you’ve made useful method in GlyphArrangement, but they don’t exactly fit my needs.

In fact, these methods are terribly efficient for rendering a text, but don’t help me extract information I want.
I just want to know where to cut a long text I’ll render later on.
As I’m using a layout, I’ll have case like :

            // I want to render "My big house is marvelous in summer and wonderful in winter."
            // The renderer will probably render :
            //
            // \/ BoxLeft    \/ Top Left
            // previous line. My 
            // big house is 
            // marvelous in      |< BoxRight
            // summer and 
            // wonderful in
            // winter.
            //       /\ Bottom right

I have to cut 1 sentence in order to create 6 lines here.
The first one is "My ", etc…

I want to find out where to cut the big sentence in smaller “line” boxes.
If I try with the method I’ve posted earlier, I’ll make Juce compute up to length*length computations (because I can’t assume width(“My”) = width(“M”) + width(“y”), so I have to test width(“M”), then width(“My”) then … ).

The glyph arrangement won’t help in that case (they would be perfect if the left position of the first line was the first of the previous line).

I guess I can try the addLineOfText method, and then iterate other getGlyph().getRight().

Will I have any problem (previous computation become wrong) if I cut the string ?

Yes, you could certainly use it to create a line of text and then look at the positions of the glyphs. That’d be pretty quick.