summaryrefslogtreecommitdiff
path: root/src/graphics/textureFont.cpp
blob: d39677e493c89c3f8e2f14ea0ebd1a883a9e3c3a (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
#include "textureFont.h"

using namespace Graphics;

TextureFont::TextureFont(const sf::Texture &texture,
        TextureFont::Descr description)
    : tex_(texture)
    , description_(description)
{
    sf::Vector2u totalSize = tex_.getSize();
    glyphSize_ = sf::Vector2i( totalSize.x / description_.numcols,
                               totalSize.y / description_.numrows );

    for(unsigned int i=0; i<description.mapping.size(); i++){
        sf::Glyph glyph;
        glyph.advance = glyphSize_.x;

        unsigned int x = i % description_.numcols;
        unsigned int y = (i - x) / description_.numcols;

        glyph.textureRect = sf::IntRect(x*glyphSize_.x,
                                        y*glyphSize_.y,
                                        glyphSize_.x,
                                        glyphSize_.y);

        glyph.bounds = sf::FloatRect(0,
                                     0,
                                     glyphSize_.x,
                                     glyphSize_.y);

        glyphTable_.insert(std::make_pair(description.mapping[i], glyph));
    }
}

const sf::Glyph & TextureFont::getGlyph(char ch) const{
    auto pair = glyphTable_.find(ch);
    return pair->second;
}

const sf::Texture * TextureFont::getTexture(...){
    return &tex_;
}

float TextureFont::getLineSpacing(...){
    // TODO make sure this is the appropiate measurement, it might be
    // smaller...
    // SFML obtains that from freetype face->metrics.height which only includes
    // the height from the baseline, then that's corrected in sf::Text with an
    // spacing ratio -> just test with a font and see what's supposed to do
    return glyphSize_.y;
}

float TextureFont::getKerning(...){
    // Monospaced fonts have 0 kerning
    return 0;
}