Example #1
0
PGE_Size FontManager::textSize(QString &text, int fontID, int max_line_lenght, bool cut, int ttfFontPixelSize)
{
    if(!isInit) return PGE_Size(text.length() * 20, text.split(QChar(QChar::LineFeed)).size() * 20);

    if(text.isEmpty()) return PGE_Size(0, 0);

    if(max_line_lenght <= 0)
        max_line_lenght = 1000;

    if(cut)
    {
        for(int i = 0; i < text.size(); i++)
        {
            if(text[i] == QChar(QChar::LineFeed))
            {
                text.remove(i, text.size() - i);
                break;
            }
        }
    }

    //Use Raster font
    if((fontID >= 0) && (fontID < rasterFonts.size()))
    {
        if(rasterFonts[fontID].isLoaded())
            return rasterFonts[fontID].textSize(text, max_line_lenght);
    }

    //Use TTF font
    QFont fnt = font();

    if(ttfFontPixelSize > 0)
        fnt.setPixelSize(ttfFontPixelSize);

    QFontMetrics meter(fnt);
    optimizeText(text, max_line_lenght);
    QSize meterSize = meter.boundingRect(text).size();
    return PGE_Size(meterSize.width(), meterSize.height());
}
Example #2
0
PGE_Size RasterFont::textSize(QString &text, int max_line_lenght, bool cut)
{
    if(text.isEmpty())
        return PGE_Size(0, 0);

    int lastspace = 0; //!< index of last found space character
    int count = 1;  //!< Count of lines
    int maxWidth = 0; //!< detected maximal width of message
    int widthSumm = 0;
    int widthSummMax = 0;

    if(cut)
    {
        for(int i = 0; i < text.size(); i++)
        {
            if(text[i] == QChar(QChar::LineFeed))
            {
                text.remove(i, text.size() - i);
                break;
            }
        }
    }

    /****************Word wrap*********************/
    for(int x = 0, i = 0; i < text.size(); i++, x++)
    {
        switch(text[i].toLatin1())
        {
        case '\t':
        case ' ':
            lastspace = i;
            widthSumm += space_width + interletter_space / 2;

            if(widthSumm > widthSummMax) widthSummMax = widthSumm;

            break;

        case '\n':
            lastspace = 0;

            if((maxWidth < x) && (maxWidth < max_line_lenght)) maxWidth = x;

            x = 0;
            widthSumm = 0;
            count++;
            break;

        default:
            RasChar rch = fontMap[text[i]];

            if(rch.valid)
            {
                widthSumm += (letter_width - rch.padding_left - rch.padding_right + interletter_space);

                if(widthSumm > widthSummMax) widthSummMax = widthSumm;
            }
            else
            {
                widthSumm += (letter_width + interletter_space);

                if(widthSumm > widthSummMax) widthSummMax = widthSumm;
            }

            break;
        }

        if((max_line_lenght > 0) && (x >= max_line_lenght)) //If lenght more than allowed
        {
            maxWidth = x;

            if(lastspace > 0)
            {
                text[lastspace] = '\n';
                i = lastspace - 1;
                lastspace = 0;
            }
            else
            {
                text.insert(i, QChar('\n'));
                x = 0;
                count++;
            }
        }
    }

    if(count == 1)
        maxWidth = text.length();

    /****************Word wrap*end*****************/
    return PGE_Size(widthSummMax, newline_offset * count);
}
Example #3
0
PGE_Size TtfFont::textSize(std::string &text,
                           uint32_t max_line_lenght,
                           bool cut, uint32_t fontSize)
{
    SDL_assert_release(g_ft);
    if(text.empty())
        return PGE_Size(0, 0);

    size_t lastspace = 0; //!< index of last found space character
    size_t count     = 1; //!< Count of lines
    uint32_t maxWidth     = 0; //!< detected maximal width of message

    uint32_t widthSumm    = 0;
    uint32_t widthSummMax = 0;

    if(cut)
    {
        std::string::size_type i = text.find('\n');
        if(i != std::string::npos)
            text.erase(i, text.size() - i);
    }

    /****************Word wrap*********************/
    uint32_t x = 0;
    for(size_t i = 0; i < text.size(); i++, x++)
    {
        char &cx = text[i];
        UTF8 uch = static_cast<unsigned char>(cx);

        switch(cx)
        {
        case '\r':
            break;

        case '\t':
        {
            //Fake tabulation
            size_t space = (4 * fontSize);
            widthSumm += (space - ((widthSumm / space) % 4));
            if(widthSumm > widthSummMax)
                widthSummMax = widthSumm;
            break;
        }

        case '\n':
        {
            lastspace = 0;
            if((maxWidth < x) && (maxWidth < max_line_lenght))
                maxWidth = x;
            x = 0;
            widthSumm = 0;
            count++;
            break;
        }

        default:
        {
            if(' ' == cx)
                lastspace = x;
            TheGlyph &glyph = getGlyph(fontSize, get_utf8_char(&cx));
            widthSumm += uint32_t(glyph.advance>>6);
            if(widthSumm > widthSummMax)
                widthSummMax = widthSumm;
            break;
        }

        }//Switch

        if((max_line_lenght > 0) && (x >= max_line_lenght)) //If lenght more than allowed
        {
            maxWidth = x;
            if(lastspace > 0)
            {
                text[lastspace] = '\n';
                i = lastspace - 1;
                lastspace = 0;
            }
            else
            {
                text.insert(i, 1, '\n');
                x = 0;
                count++;
            }
        }
        i += static_cast<size_t>(trailingBytesForUTF8[uch]);
    }

    if(count == 1)
        maxWidth = static_cast<uint32_t>(text.length());

    /****************Word wrap*end*****************/
    return PGE_Size(static_cast<int32_t>(widthSummMax), static_cast<int32_t>((fontSize * 1.5) * count));
}