diff options
-rw-r--r-- | bin/Jin.exe | bin | 1647104 -> 628736 bytes | |||
-rw-r--r-- | bin/jin.exe | bin | 1647104 -> 628736 bytes | |||
-rw-r--r-- | bin/main.lua | 6 | ||||
-rw-r--r-- | build/vc++/libs/LuaJIT-2.0.5/src/lua51.dll | bin | 364544 -> 364544 bytes | |||
-rw-r--r-- | build/vc++/libs/LuaJIT-2.0.5/src/lua51.lib | bin | 59514 -> 59514 bytes | |||
-rw-r--r-- | src/libjin/Graphics/Font/Font.h | 10 | ||||
-rw-r--r-- | src/libjin/Graphics/Font/TTF.cpp | 45 | ||||
-rw-r--r-- | src/libjin/Graphics/Font/TTF.h | 1 | ||||
-rw-r--r-- | src/libjin/Graphics/Font/TextureFont.cpp | 2 | ||||
-rw-r--r-- | src/lua/modules/graphics/graphics.cpp | 68 | ||||
-rw-r--r-- | src/lua/modules/graphics/ttf.cpp | 2 |
11 files changed, 73 insertions, 61 deletions
diff --git a/bin/Jin.exe b/bin/Jin.exe Binary files differindex 6e8f904..dacc9cc 100644 --- a/bin/Jin.exe +++ b/bin/Jin.exe diff --git a/bin/jin.exe b/bin/jin.exe Binary files differindex 6e8f904..dacc9cc 100644 --- a/bin/jin.exe +++ b/bin/jin.exe diff --git a/bin/main.lua b/bin/main.lua index bee78ca..f5f2a34 100644 --- a/bin/main.lua +++ b/bin/main.lua @@ -10,7 +10,7 @@ function jin.core.onLoad() local bitmap = jin.graphics.newBitmap("font2.png") local tfdata = jin.graphics.newTTFData("font.ttf") tf = tfdata:newTTF(15) - page = tf:typeset("this is a test", 16) + page = tf:typeset("this is a test") end function jin.core.onEvent(e) @@ -21,6 +21,8 @@ end function jin.core.onDraw() jin.graphics.useShader(shader) - jin.graphics.print("this 你好 is a test", tf, 10, 10, 16) + jin.graphics.setFont(tf) + jin.graphics.print("你好error: this is a test", 0, 0, 16, 0) + jin.graphics.unsetFont() jin.graphics.unuseShader() end
\ No newline at end of file diff --git a/build/vc++/libs/LuaJIT-2.0.5/src/lua51.dll b/build/vc++/libs/LuaJIT-2.0.5/src/lua51.dll Binary files differindex 76a30cc..991a1ba 100644 --- a/build/vc++/libs/LuaJIT-2.0.5/src/lua51.dll +++ b/build/vc++/libs/LuaJIT-2.0.5/src/lua51.dll diff --git a/build/vc++/libs/LuaJIT-2.0.5/src/lua51.lib b/build/vc++/libs/LuaJIT-2.0.5/src/lua51.lib Binary files differindex cdcb9a2..36f6e7c 100644 --- a/build/vc++/libs/LuaJIT-2.0.5/src/lua51.lib +++ b/build/vc++/libs/LuaJIT-2.0.5/src/lua51.lib diff --git a/src/libjin/Graphics/Font/Font.h b/src/libjin/Graphics/Font/Font.h index 424c324..fcc559c 100644 --- a/src/libjin/Graphics/Font/Font.h +++ b/src/libjin/Graphics/Font/Font.h @@ -13,7 +13,10 @@ namespace graphics class Font { public: - Font() {} + Font(unsigned fontsize) + : fontSize(fontsize) + { + } virtual ~Font() {}; virtual Page* typeset(const Text& text, int lineheight, int spacing = 0) = 0; @@ -23,6 +26,11 @@ namespace graphics virtual void print(const Content& text, int x, int y, int lineheight, int spacing = 0) = 0; virtual void print(const Text& text, int x, int y, int lineheight, int spacing = 0) = 0; + inline unsigned getFontSize() { return fontSize; }; + + protected: + unsigned fontSize; + }; } // graphics diff --git a/src/libjin/Graphics/Font/TTF.cpp b/src/libjin/Graphics/Font/TTF.cpp index 5c77e7f..ff4dcc7 100644 --- a/src/libjin/Graphics/Font/TTF.cpp +++ b/src/libjin/Graphics/Font/TTF.cpp @@ -55,12 +55,12 @@ namespace graphics free(raw.data); } - TTF* TTFData::createTTF(unsigned ttfsize) + TTF* TTFData::createTTF(unsigned fontSize) { TTF* ttf; try { - ttf = new TTF(this, ttfsize); + ttf = new TTF(this, fontSize); } catch (...) { @@ -150,31 +150,6 @@ namespace graphics const int TTF::TEXTURE_WIDTHS[] = { 128, 256, 256, 512, 512, 1024, 1024 }; const int TTF::TEXTURE_HEIGHTS[] = { 128, 128, 256, 256, 512, 512, 1024 }; - /* utf8 byte string to unicode codepoint */ - static const char* utf8toCodepoint(const char *p, Codepoint *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 { - if (*(++p) == '\0') { - *res = x; - return p; - } - shift -= 6; - x |= (*p & 0x3f) << shift; - } while (shift); - *res = x; - return p + 1; - } - /* little endian unicode */ static const char* unicodeLittleEndian(const char* p, unsigned* res) { @@ -195,11 +170,11 @@ namespace graphics } TTF::TTF(TTFData* f, unsigned int fontSize) - : cursor(0, 0) + : Font(fontSize) + , cursor(0, 0) , ttf(f) - , ttfsize(fontSize) { - ttf->pushTTFsize(ttfsize); + ttf->pushTTFsize(fontSize); ttf->getVMetrics(&baseline, &descent); estimateSize(); ttf->popTTFsize(); @@ -341,7 +316,7 @@ namespace graphics int TTF::getCharWidth(int c) { int adw, lsb; - ttf->pushTTFsize(ttfsize); + ttf->pushTTFsize(fontSize); ttf->getHMetrics(c, &adw, &lsb); ttf->popTTFsize(); return adw; @@ -354,7 +329,7 @@ namespace graphics int TTF::getTextWidth(const Content& t, int spacing) { - ttf->pushTTFsize(ttfsize); + ttf->pushTTFsize(fontSize); int res = 0; int tmp = 0; for (Codepoint c : t) @@ -376,7 +351,7 @@ namespace graphics int TTF::getTextHeight(const Content& t, int lineheight) { - ttf->pushTTFsize(ttfsize); + ttf->pushTTFsize(fontSize); int res = 0; bool newline = true; for (Codepoint c : t) @@ -396,7 +371,7 @@ namespace graphics void TTF::getTextBox(const Content& text, int* w, int* h, int lineheight, int spacing) { - ttf->pushTTFsize(ttfsize); + ttf->pushTTFsize(fontSize); *w = 0; *h = 0; int tmp = 0; @@ -426,7 +401,7 @@ namespace graphics TTF::TTFGlyph& TTF::bakeGlyph(unsigned int character) { int w, h, xoff, yoff; - ttf->pushTTFsize(ttfsize); + ttf->pushTTFsize(fontSize); GLuint atlas = atlases.back(); const Color* bitmap = ttf->getCodepointBitmap(character, &w, &h, &xoff, &yoff); int adw, lsb; diff --git a/src/libjin/Graphics/Font/TTF.h b/src/libjin/Graphics/Font/TTF.h index 7f4f873..4e9a7ae 100644 --- a/src/libjin/Graphics/Font/TTF.h +++ b/src/libjin/Graphics/Font/TTF.h @@ -117,7 +117,6 @@ namespace graphics std::vector<GLuint> atlases; std::map<Codepoint, TTFGlyph> glyphs; TTFData* ttf; - const unsigned int ttfsize; int baseline; int descent; diff --git a/src/libjin/Graphics/Font/TextureFont.cpp b/src/libjin/Graphics/Font/TextureFont.cpp index 4dde236..7ee1962 100644 --- a/src/libjin/Graphics/Font/TextureFont.cpp +++ b/src/libjin/Graphics/Font/TextureFont.cpp @@ -231,6 +231,7 @@ namespace graphics TextureFont::TextureFont(const Bitmap* bitmap, const Content& codepoints, int cellw, int cellh) : Drawable(bitmap) + , Font(cellh) { TextureGlyph glyph; Vector2<int> count(bitmap->getWidth() / cellw, bitmap->getHeight() / cellh); @@ -251,6 +252,7 @@ namespace graphics TextureFont::TextureFont(const Bitmap* bitmap, const Content& codepoints, Color mask, int cellh) : Drawable(bitmap) + , Font(cellh) { TextureGlyph glyph; glyph.h = cellh; diff --git a/src/lua/modules/graphics/graphics.cpp b/src/lua/modules/graphics/graphics.cpp index a62802c..5d99915 100644 --- a/src/lua/modules/graphics/graphics.cpp +++ b/src/lua/modules/graphics/graphics.cpp @@ -12,6 +12,8 @@ namespace lua using jin::filesystem::Filesystem; using jin::filesystem::Buffer; +#include "../../resources/font.ttf.h" + static struct { Color curRenderColor; @@ -35,6 +37,11 @@ namespace lua luax_pushboolean(L, false); return 1; } + /* load default font */ + TTFData* ttfData = TTFData::createTTFData(font_ttf, sizeof(font_ttf)); + context.defaultFont = ttfData->createTTF(15); + context.curFont = context.defaultFont; + luax_pushboolean(L, true); return 1; } @@ -234,44 +241,43 @@ namespace lua return; Proxy* p = (Proxy*)luax_toudata(L, 1); Text* text = p->getObject<Text>(); - Proxy* p2 = (Proxy*)luax_toudata(L, 2); int x = luax_optnumber(L, 3, 0); int y = luax_optnumber(L, 4, 0); - int lineheight = luax_optnumber(L, 5, 12); int spacing = luax_optnumber(L, 6, 0); + Font* font = nullptr; + Proxy* p2 = (Proxy*)luax_toudata(L, 2); if (luax_istype(L, 2, JIN_GRAPHICS_TEXTUREFONT)) { TextureFont* tf = p2->getObject<TextureFont>(); - tf->print(*text, x, y, lineheight, spacing); + font = tf; } else if (luax_istype(L, 2, JIN_GRAPHICS_TTF)) { TTF* ttf = p2->getObject<TTF>(); - ttf->print(*text, x, y, lineheight, spacing); + font = ttf; } + else + { + font = context.defaultFont; + } + int lineheight = luax_optnumber(L, 5, font->getFontSize()); + font->print(*text, x, y, lineheight, spacing); } - /* print(string[, font], x, y, lineheight, spacing) */ + /* print(string, x, y, lineheight, spacing) */ static int l_print(lua_State* L) { + Font* font = context.curFont; + if (font == nullptr) + return 0; unsigned length; const char* str = luax_checklstring(L, 1, &length); Text text(Encode::UTF8, str, length); - Proxy* p = (Proxy*)luax_toudata(L, 2); - int x = luax_optnumber(L, 3, 0); - int y = luax_optnumber(L, 4, 0); - int lineheight = luax_optnumber(L, 5, 12); - int spacing = luax_optnumber(L, 6, 0); - if (luax_istype(L, 2, JIN_GRAPHICS_TEXTUREFONT)) - { - TextureFont* tf = p->getObject<TextureFont>(); - tf->print(text, x, y, lineheight, spacing); - } - else if (luax_istype(L, 2, JIN_GRAPHICS_TTF)) - { - TTF* ttf = p->getObject<TTF>(); - ttf->print(text, x, y, lineheight, spacing); - } + int x = luax_optnumber(L, 2, 0); + int y = luax_optnumber(L, 3, 0); + int lineheight = luax_optnumber(L, 4, font->getFontSize()); + int spacing = luax_optnumber(L, 5, 0); + font->print(text, x, y, lineheight, spacing); return 0; } @@ -627,11 +633,30 @@ namespace lua return 1; } + /* setFont(font) */ static int l_setFont(lua_State* L) { + if (luax_istype(L, 1, JIN_GRAPHICS_TTF)) + { + Proxy* p = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_TTF); + TTF* ttf = p->getObject<TTF>(); + context.curFont = ttf; + } + else if (luax_istype(L, 1, JIN_GRAPHICS_TEXTUREFONT)) + { + Proxy* p = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_TEXTUREFONT); + TextureFont* tf = p->getObject<TextureFont>(); + context.curFont = tf; + } return 0; } + static int l_unsetFont(lua_State* L) + { + context.curFont = context.defaultFont; + return 0; + } + static const luaL_Reg f[] = { /* window */ { "init", l_init }, @@ -670,7 +695,8 @@ namespace lua { "triangle", l_triangle }, { "polygon", l_polygon }, /* font */ - { "setFont", l_setFont }, + { "setFont", l_setFont }, + { "unsetFont", l_unsetFont }, { 0, 0 } }; diff --git a/src/lua/modules/graphics/ttf.cpp b/src/lua/modules/graphics/ttf.cpp index 19e9e92..3d88a62 100644 --- a/src/lua/modules/graphics/ttf.cpp +++ b/src/lua/modules/graphics/ttf.cpp @@ -28,7 +28,7 @@ namespace lua { Proxy* p = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_TTF); TTF* ttf = p->getObject<TTF>(); - int lineheight = luax_checkinteger(L, 3); + int lineheight = luax_optnumber(L, 3, ttf->getFontSize()); int spacing = luax_optnumber(L, 4, 0); Page* page = nullptr; if (luax_isstring(L, 2)) |