# GlyphArrangement::addFittedText - newline problem

**URL:** <https://forum.juce.com/t/glypharrangement-addfittedtext-newline-problem/10231>\
**Category:** General JUCE discussion\
**Created:** [April 3, 2013, 2:31pm UTC](https://forum.juce.com/t/glypharrangement-addfittedtext-newline-problem/10231 "2013-04-03T14:31:10Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![friscokid](https://avatars.discourse-cdn.com/v4/letter/f/d9b06d/32.png) [@friscokid](https://forum.juce.com/u/friscokid)\
**Post date:** [April 3, 2013, 2:31pm UTC](https://forum.juce.com/t/glypharrangement-addfittedtext-newline-problem/10231/1 "2013-04-03T14:31:10Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [April 3, 2013, 3:18pm UTC](https://forum.juce.com/t/glypharrangement-addfittedtext-newline-problem/10231/2 "2013-04-03T15:18:02Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![friscokid](https://avatars.discourse-cdn.com/v4/letter/f/d9b06d/32.png) [@friscokid](https://forum.juce.com/u/friscokid)\
**Post date:** [April 3, 2013, 4:28pm UTC](https://forum.juce.com/t/glypharrangement-addfittedtext-newline-problem/10231/3 "2013-04-03T16:28:50Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![friscokid](https://avatars.discourse-cdn.com/v4/letter/f/d9b06d/32.png) [@friscokid](https://forum.juce.com/u/friscokid)\
**Post date:** [April 3, 2013, 5:28pm UTC](https://forum.juce.com/t/glypharrangement-addfittedtext-newline-problem/10231/4 "2013-04-03T17:28:17Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [April 3, 2013, 5:36pm UTC](https://forum.juce.com/t/glypharrangement-addfittedtext-newline-problem/10231/5 "2013-04-03T17:36:48Z")

</div>

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]

---

<div class="post-metadata">

**Author:** ![friscokid](https://avatars.discourse-cdn.com/v4/letter/f/d9b06d/32.png) [@friscokid](https://forum.juce.com/u/friscokid)\
**Post date:** [April 3, 2013, 5:43pm UTC](https://forum.juce.com/t/glypharrangement-addfittedtext-newline-problem/10231/6 "2013-04-03T17:43:27Z")

</div>

Nice!  
Works as expected.  
Thanks a lot.
