GlyphArrangement::addFittedText - newline problem


#1

I’m using Graphics::drawFittedText to draw the text of a label that contains newline characters.
The text is submitted correctly to GlyphArrangement::addFittedText but in the output that is drawn there are some characters missing after each newline character.

When the the following text is submitted to GlyphArrangement::addFittedText:
chicken\nchicken\nchicken\nchicken\nchicken
then the result that is drawn is:
chicken
hicken
icken
cken
ken

Obviously I’m expecting the output:
chicken
chicken
chicken
chicken
chicken

I’ve tried to use different ways to get a correct newline:
\n
\r\n
newline

But the result is always the same. For each newline in the string one additional character at the beginning of the next line is truncated.
Any idea what could be wrong?


#2

Bizarre! Can you give me a code snippet to try, e.g. that I could paste into the juce demo?


#3

I found out that it only happens when I’m using custom serialized fonts. So it deosn’t seem to be related to the GlyphArrangement.
I’ll post my results when I’ve found the exact reason for the problem.
Thanks.


#4

Is it possible that the CustomTypeface::getGlyphPositions doesn’t handle special characters correctly?
When I pass the String:
chicken\nchicken\nchicken
to the CustomTypeface::getGlyphPositions it tries to find a fallbackTypeface for the \n and nothing is added to the resultGlyphs and xOffsets.

So there are the following characters added to the resultGlyphs:
chickenchickenchicken
which is a total of 21 characters which do not include any whitespace anymore.

However when these 21 characters are passed to the GlyphArrangement::addCurtailedLineOfText then the loop iterates over the original characters String::CharPointerType t (text.getCharPointer()); which are still 23 with two whitespaces included.
As the loop iterates over the original text with size 23 but only up to textLen which now is 21 with whitespaces removed, the String::CharPointerType t (text.getCharPointer()); test returns wrong whitespaces and the loop stops too early.

I think the GlyphArrangement::addCurtailedLineOfText is working correctly but the CustomTypeface::getGlyphPositions needs to handle special characters (whitespaces) to resolve this issue.


#5

Hmm. How about this:

[code]void CustomTypeface::getGlyphPositions (const String& text, Array & resultGlyphs, Array& xOffsets)
{
xOffsets.add (0);
float x = 0;

for (String::CharPointerType t (text.getCharPointer()); ! t.isEmpty();)
{
    float width = 0.0f;
    int glyphChar = 0;

    const juce_wchar c = t.getAndAdvance();

    if (const GlyphInfo* const glyph = findGlyph (c, true))
    {
        width = glyph->getHorizontalSpacing (*t);
        glyphChar = (int) glyph->character;
    }
    else
    {
        const Typeface::Ptr fallbackTypeface (Typeface::getFallbackTypeface());

        if (fallbackTypeface != nullptr && fallbackTypeface != this)
        {
            Array <int> subGlyphs;
            Array <float> subOffsets;
            fallbackTypeface->getGlyphPositions (String::charToString (c), subGlyphs, subOffsets);

            if (subGlyphs.size() > 0)
            {
                glyphChar = subGlyphs.getFirst();
                width = subOffsets[1];
            }
        }
    }

    x += width;
    resultGlyphs.add (glyphChar);
    xOffsets.add (x);
}

}
[/code]


#6

Nice!
Works as expected.
Thanks a lot.