aboutsummaryrefslogtreecommitdiff
path: root/src/libjin/Graphics/Font.h
blob: 10fd2422a997c118afbf2817dcc6c18176d0037b (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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#ifndef __LIBJIN_FONT_H
#define __LIBJIN_FONT_H
#include "../modules.h"
#if LIBJIN_MODULES_RENDER

#include <vector>
#include <map>
#include "drawable.h"
#include "../3rdparty/stb/stb_truetype.h"
#include "../math/quad.h"

namespace jin
{
namespace graphics
{  
    /**
    * original from love2d font and graphics modules
    * the basic idea is storing glyphs in several mipmap
    * http://www.ruanyifeng.com/blog/2007/10/ascii_unicode_and_utf-8.html
    */

    struct GlyphVertex
    {
        float x, y; // screen coordinates
        float u, v; // texture coordinates
    };

    /* track when to change texutre binded in render array */
    /* casue switch texture is expensive */
    /* std::vector<GlyphVertex> list */
    struct GlyphArrayDrawInfo
    {
        GLuint texture; 
        int startvertex; 
        int vertexcount;
    };

    /* glyph texture */
    struct Glyph
    {
        GLuint texture;          // texture where this glyph rendered
        int spacing;             // spacing of glyph
        GlyphVertex vertices[4]; // quad of glyph render region 
    };

    class Font
    {
    public: 
        static Font* createFont(const char* file);
        static Font* createFont(const char* data, size_t size);

        void print(const char* text, int x, int y);

    private:
        /* font atlas levels */
        static const int TEXTURE_SIZE_LEVELS_COUNT = 7;
        static const int TEXTURE_SIZE_LEVEL_MAX = TEXTURE_SIZE_LEVELS_COUNT - 1;
        static const int TEXTURE_WIDTHS[TEXTURE_SIZE_LEVELS_COUNT];
        static const int TEXTURE_HEIGHTS[TEXTURE_SIZE_LEVELS_COUNT];
        static const int SPACES_PER_TAB = 4;

        /* create a new mipmap to render glyph and push it on textures */
        bool createTexture();
        /* create a glyph for a unicode and return it */
        Glyph* addGlyph(unsigned int character);
        /* find glyph by unicode */
        Glyph* findGlyph(unsigned int character);

        /* list of textures where glyphs rendered, always operate the last one */
        /* map character to its render area */
        std::vector<GLuint> textures;
        std::map<unsigned int, Glyph*> glyphs;
        /* mipmap size level */
        int textureLevel;
        int textureWidth; 
        int textureHeight;

    };

} // graphics
} // jin

#endif // LIBJIN_MODULES_RENDER
#endif // __LIBJIN_FONT_H