aboutsummaryrefslogtreecommitdiff
path: root/src/libjin/Graphics/Font.cpp
blob: bb767dbb1e670783fc20a64644d298a64e61f7aa (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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include "../modules.h"
#if LIBJIN_MODULES_RENDER

#include "font.h"
#include <stdio.h>
#define STB_TRUETYPE_IMPLEMENTATION
#include "../3rdparty/stb/stb_truetype.h"
#include "color.h"

namespace jin
{
namespace graphics
{

    using namespace std;
    using namespace jin::math;
    
    const int Font::TEXTURE_WIDTHS[] = { 128, 256, 256, 512, 512, 1024, 1024 };
    const int Font::TEXTURE_HEIGHTS[] = { 128, 128, 256, 256, 512, 512,  1024 };

    static const char *ttf_utf8toCodepoint(const char *p, unsigned *res) {
        unsigned x, mask, shift;
        switch (*p & 0xf0) {
        case 0xf0:  mask = 0x07;  shift = 18;   break;
        case 0xe0:  mask = 0x0f;  shift = 12;   break;
        case 0xc0:
        case 0xd0:  mask = 0x1f;  shift = 6;   break;
        default:
            *res = *p;
            return p + 1;
        }
        x = (*p & mask) << shift;
        do {
            /* Return early if we reach an unexpected NULL */
            if (*(++p) == '\0') {
                *res = x;
                return p;
            }
            shift -= 6;
            x |= (*p & 0x3f) << shift;
        } while (shift);
        *res = x;
        return p + 1;
    }

    /*static*/ Font* Font::createFont(const char* font, size_t size)
    {

    }

    /*static*/ Font* Font::createFont(const char* file)
    {

    }

    Font::Font()
        : textureLevel(TEXTURE_SIZE_LEVEL_MAX)
    {
        
    }

    bool Font::createTexture()
    {
        GLuint t;
        glGenTextures(1, &t);
        glBindTexture(GL_TEXTURE_2D, t);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
        /*Initialize the texture, attempting smaller sizes if initialization fails.*/
        bool initialized = false;
        while (textureLevel >= 0)
        {
            /*clear errors before initializing*/
            while (glGetError() != GL_NO_ERROR);
            textureWidth = TEXTURE_WIDTHS[textureLevel];
            textureHeight = TEXTURE_HEIGHTS[textureLevel];
            glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, textureWidth, textureHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
            initialized = (glGetError() == GL_NO_ERROR);
            if (initialized || textureLevel <= 0)
                break;
            --textureLevel;
        }
        if (!initialized)
        {
            glDeleteTextures(1, &t);
            glBindTexture(GL_TEXTURE_2D, 0);
            return false; 
        }
        textures.push_back(t);
        glBindTexture(GL_TEXTURE_2D, 0);
        return true;
    }

    void Font::print(const char* text, int x, int y)
    {
        int len = strlen(text);
        /* xy and uv list */
        vector<GlyphVertex> glyphvertices(len*4);
        /* texture binded along with glyphvertices */
        vector<GlyphArrayDrawInfo> glyphinfolist;
        float dx = 0; 
        float dy = 0;
        //float lineheihgt = ;
    }

    /**
    * unicodeȾļtextureϣglyphs
    */
    Glyph* Font::addGlyph(unsigned int character)
    {
        GLuint texture = textures.back();

    }

    Glyph* Font::findGlyph(unsigned int character)
    {

    }

} // graphics
} // jin

#endif // LIBJIN_MODULES_RENDER