aboutsummaryrefslogtreecommitdiff
path: root/libjin/Graphics/Font.h
diff options
context:
space:
mode:
Diffstat (limited to 'libjin/Graphics/Font.h')
-rw-r--r--libjin/Graphics/Font.h90
1 files changed, 56 insertions, 34 deletions
diff --git a/libjin/Graphics/Font.h b/libjin/Graphics/Font.h
index fa54337..b71c2cc 100644
--- a/libjin/Graphics/Font.h
+++ b/libjin/Graphics/Font.h
@@ -6,28 +6,31 @@
#include <vector>
#include <map>
#include "drawable.h"
-#include "../3rdparty/stb/stb_truetype.h"
+#include "FontData.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
+ float x, y;
+ float u, v;
+ GlyphVertex(float _x, float _y, float _u, float _v)
+ {
+ set(_x, _y, _u, _v);
+ }
+ void set(float _x, float _y, float _u, float _v)
+ {
+ x = _x;
+ y = _y;
+ u = _u;
+ v = _v;
+ }
};
- /* track when to change texutre binded in render array */
- /* casue switch texture is expensive */
- /* std::vector<GlyphVertex> list */
struct GlyphArrayDrawInfo
{
GLuint texture;
@@ -35,45 +38,64 @@ namespace graphics
int vertexcount;
};
- /* glyph texture */
+ struct GlyphBox
+ {
+ float xoff, yoff;
+ float width, height;
+ void set(float x, float y, float w, float h)
+ {
+ xoff = x;
+ yoff = y;
+ width = w;
+ height = h;
+ }
+ };
+
struct Glyph
{
- GLuint texture; // texture where this glyph rendered
- int spacing; // spacing of glyph
- GlyphVertex vertices[4]; // quad of glyph render region
+ GLuint texture; // texture where this glyph rendered
+ GlyphBox box; // glyph box
};
class Font
{
- public:
- static Font* createFont(const char* file);
- static Font* createFont(const char* data, size_t size);
+ private:
+ static const int DEFAULT_FONT_SIZE = 12;
+
+ public:
+ static Font* createFont(const FontData* fontData, unsigned int fontSzie = DEFAULT_FONT_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;
+ //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;
+ static const int TEXTURE_SIZE = 512;
+
+ Font(const FontData* font, unsigned int fontSize);
+ ~Font();
- /* 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 */
+ //float getCharWidth(int c, int last);
+ //int getTextWidth(const char* text);
+
std::vector<GLuint> textures;
- std::map<unsigned int, Glyph*> glyphs;
- /* mipmap size level */
- int textureLevel;
- int textureWidth;
- int textureHeight;
+ std::map<unsigned int, Glyph*> glyphs; // map glyph codepoint to Glyph
+ const FontData* font;
+ const unsigned int fontsize;
+ unsigned int baseline;
+
+ /* cursor helped render to texture */
+ float xoffset;
+ float yoffset;
};