TextLayout empty when only a single character


#1

If I create a TextLayout using the standard method instead of the native method (I’m using Ubuntu) and only put a single character in it, no text runs get created. I went through the code a bit and I think I’ve narrowed down where the problem is:

juce_TextLayout.cpp - Line 504

[code]Font defaultFont;
Array runAttributes;

{
const int stringLength = text.getText().length();
int rangeStart = 0;
FontAndColour lastFontAndColour (nullptr);

// Iterate through every character in the string
for (int i = 0; i < stringLength; ++i)
{
    FontAndColour newFontAndColour (&defaultFont);
    const int numCharacterAttributes = text.getNumAttributes();

    for (int j = 0; j < numCharacterAttributes; ++j)
    {
        const AttributedString::Attribute& attr = *text.getAttribute (j);

        if (attr.range.contains (i))
        {
            if (const Font* f = attr.getFont())      newFontAndColour.font   = f;
            if (const Colour* c = attr.getColour())  newFontAndColour.colour = *c;
        }
    }

    /************************************ PROBLEM RIGHT HERE ********************************************/
    if (i > 0 && (newFontAndColour != lastFontAndColour || i == stringLength - 1))
    {
        runAttributes.add (RunAttribute (lastFontAndColour,
                                         Range<int> (rangeStart, (i < stringLength - 1) ? i : (i + 1))));
        rangeStart = i;
    }

    lastFontAndColour = newFontAndColour;
}

}

for (int i = 0; i < runAttributes.size(); ++i)
{
const RunAttribute& r = runAttributes.getReference(i);
appendText (text, r.range, *(r.fontAndColour.font), r.fontAndColour.colour);
}[/code]

A run attribute never gets added when there’s a single character since the ‘i’ variable will never be greater than 0.


#2

Hmm… Thanks, I think the bracketing is just the wrong way round - should probably be:

if ((i > 0 && newFontAndColour != lastFontAndColour) || i == stringLength - 1)