summaryrefslogtreecommitdiff
path: root/src/graphics/text.cpp
blob: 51b71ae852a2f6b3b3de57ebc12c42691efb7034 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include "text.h"

using namespace Graphics;

template <>
void Text<TextureFont>::arrange(TextureFont &font){
    // TODO: this is really awful rendering but works a little bit
    // Add getLineSpacing + getKerning and so on
    unsigned int filledX = 0;
    unsigned int row = 0;
    unsigned int col = 0;

    for(unsigned int i=0; i < text_.length(); i++){
        sf::Glyph glyph = font.getGlyph( text_[i] );
        filledX += glyph.advance;
        if (maxWidth_ != 0 && ( text_[i] == '\n' || filledX > maxWidth_)){
            row++;
            filledX = 0;
            col = 0;
            if(text_[i] == '\n') continue;
        }

        // Position the character in the text
        vertices_[4*i+0].position = sf::Vector2f(
                col*glyph.advance + glyph.bounds.left,
                (glyph.bounds.top) + row * font.getLineSpacing() * lineHeightRatio_
            );
        vertices_[4*i+1].position = sf::Vector2f(
                col*glyph.advance + glyph.bounds.left+glyph.bounds.width,
                (glyph.bounds.top) + row * font.getLineSpacing() * lineHeightRatio_
            );
        vertices_[4*i+2].position = sf::Vector2f(
                col*glyph.advance + glyph.bounds.left+glyph.bounds.width,
                (glyph.bounds.top+glyph.bounds.height) + row * font.getLineSpacing() * lineHeightRatio_
            );
        vertices_[4*i+3].position = sf::Vector2f(
                col*glyph.advance + glyph.bounds.left,
                (glyph.bounds.top+glyph.bounds.height) + row * font.getLineSpacing() * lineHeightRatio_
            );
        col++;

        // Choose the texture rectangle for this character
        vertices_[4*i+0].texCoords = sf::Vector2f(
                glyph.textureRect.left,
                glyph.textureRect.top
            );
        vertices_[4*i+1].texCoords = sf::Vector2f(
                glyph.textureRect.left+glyph.textureRect.width,
                glyph.textureRect.top
            );
        vertices_[4*i+2].texCoords = sf::Vector2f(
                glyph.textureRect.left+glyph.textureRect.width,
                glyph.textureRect.top+glyph.textureRect.height
            );
        vertices_[4*i+3].texCoords = sf::Vector2f(
                glyph.textureRect.left,
                glyph.textureRect.top+glyph.textureRect.height
            );
    }
}

template <>
void Text<sf::Font>::arrange(sf::Font &font){
    // TODO: Implement this function for a generic sf::Font
}