Skip to content

Commit

Permalink
static loaded font
Browse files Browse the repository at this point in the history
  • Loading branch information
matiasvlevi committed May 9, 2024
1 parent 680a9df commit adb784f
Show file tree
Hide file tree
Showing 7 changed files with 54,930 additions and 72 deletions.
3 changes: 1 addition & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ BINDIR = bin

DOC_BUILD_SCRIPT = build_docs.lua

DEFAULT_FONT=/usr/share/fonts/TTF/Arial.TTF

MACROS=-D'LU5_VERSION="$(VERSION)"' -D'LU5_DEFAULT_FONT_PATH="$(DEFAULT_FONT)"'
MACROS=-D'LU5_VERSION="$(VERSION)"'

ifeq ($(PLATFORM), win)
CC := x86_64-w64-mingw32-gcc
Expand Down
2 changes: 1 addition & 1 deletion docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ <h1 class="module">Reference</h1>
<span class="text version">v0.0.3</span>
</header>

<div class="modules" id="reactive"><div class="summary"><h3>math</h3><ul><li class="index"><a class="dark index" href="./math.html#round">round</a></li><li class="index"><a class="dark index" href="./math.html#floor">floor</a></li><li class="index"><a class="dark index" href="./math.html#ceil">ceil</a></li><li class="index"><a class="dark index" href="./math.html#sin">sin</a></li><li class="index"><a class="dark index" href="./math.html#cos">cos</a></li><li class="index"><a class="dark index" href="./math.html#tan">tan</a></li></ul></div><div class="summary"><h3>shapes</h3><ul><li class="index"><a class="dark index" href="./shapes.html#circle">circle</a></li><li class="index"><a class="dark index" href="./shapes.html#rect">rect</a></li><li class="index"><a class="dark index" href="./shapes.html#square">square</a></li><li class="index"><a class="dark index" href="./shapes.html#line">line</a></li><li class="index"><a class="dark index" href="./shapes.html#quad">quad</a></li><li class="index"><a class="dark index" href="./shapes.html#text">text</a></li><li class="index"><a class="dark index" href="./shapes.html#point">point</a></li><li class="index"><a class="dark index" href="./shapes.html#arc">arc</a></li><li class="index"><a class="dark index" href="./shapes.html#ellipse">ellipse</a></li><li class="index"><a class="dark index" href="./shapes.html#triangle">triangle</a></li></ul></div><div class="summary"><h3>classes</h3><ul><li class="index"><a class="dark index" href="./classes.html#class">class</a></li></ul></div><div class="summary"><h3>setting</h3><ul><li class="index"><a class="dark index" href="./setting.html#fill">fill</a></li><li class="index"><a class="dark index" href="./setting.html#strokeWeight">strokeWeight</a></li><li class="index"><a class="dark index" href="./setting.html#noFill">noFill</a></li><li class="index"><a class="dark index" href="./setting.html#stroke">stroke</a></li><li class="index"><a class="dark index" href="./setting.html#noStroke">noStroke</a></li><li class="index"><a class="dark index" href="./setting.html#textSize">textSize</a></li><li class="index"><a class="dark index" href="./setting.html#textFont">textFont</a></li></ul></div><div class="summary"><h3>window</h3><ul><li class="index"><a class="dark index" href="./window.html#createWindow">createWindow</a></li><li class="index"><a class="dark index" href="./window.html#background">background</a></li><li class="index"><a class="dark index" href="./window.html#frameRate">frameRate</a></li></ul></div><div class="summary"><h3>io</h3><ul><li class="index"><a class="dark index" href="./io.html#print">print</a></li></ul></div><div class="summary"><h3>loaders</h3><ul><li class="index"><a class="dark index" href="./loaders.html#loadFont">loadFont</a></li></ul></div><div class="summary"><h3>events</h3><ul><li class="index"><a class="dark index" href="./events.html#isKeyDown">isKeyDown</a></li><li class="index"><a class="dark index" href="./events.html#mousePressed">mousePressed</a></li><li class="index"><a class="dark index" href="./events.html#mouseReleased">mouseReleased</a></li><li class="index"><a class="dark index" href="./events.html#keyPressed">keyPressed</a></li><li class="index"><a class="dark index" href="./events.html#keyReleased">keyReleased</a></li><li class="index"><a class="dark index" href="./events.html#keyHeld">keyHeld</a></li></ul></div></div>
<div class="modules" id="reactive"><div class="summary"><h3>events</h3><ul><li class="index"><a class="dark index" href="./events.html#isKeyDown">isKeyDown</a></li><li class="index"><a class="dark index" href="./events.html#mousePressed">mousePressed</a></li><li class="index"><a class="dark index" href="./events.html#mouseReleased">mouseReleased</a></li><li class="index"><a class="dark index" href="./events.html#keyPressed">keyPressed</a></li><li class="index"><a class="dark index" href="./events.html#keyReleased">keyReleased</a></li><li class="index"><a class="dark index" href="./events.html#keyHeld">keyHeld</a></li></ul></div><div class="summary"><h3>shapes</h3><ul><li class="index"><a class="dark index" href="./shapes.html#circle">circle</a></li><li class="index"><a class="dark index" href="./shapes.html#rect">rect</a></li><li class="index"><a class="dark index" href="./shapes.html#square">square</a></li><li class="index"><a class="dark index" href="./shapes.html#line">line</a></li><li class="index"><a class="dark index" href="./shapes.html#quad">quad</a></li><li class="index"><a class="dark index" href="./shapes.html#text">text</a></li><li class="index"><a class="dark index" href="./shapes.html#point">point</a></li><li class="index"><a class="dark index" href="./shapes.html#arc">arc</a></li><li class="index"><a class="dark index" href="./shapes.html#ellipse">ellipse</a></li><li class="index"><a class="dark index" href="./shapes.html#triangle">triangle</a></li></ul></div><div class="summary"><h3>classes</h3><ul><li class="index"><a class="dark index" href="./classes.html#class">class</a></li></ul></div><div class="summary"><h3>window</h3><ul><li class="index"><a class="dark index" href="./window.html#createWindow">createWindow</a></li><li class="index"><a class="dark index" href="./window.html#background">background</a></li><li class="index"><a class="dark index" href="./window.html#frameRate">frameRate</a></li></ul></div><div class="summary"><h3>math</h3><ul><li class="index"><a class="dark index" href="./math.html#round">round</a></li><li class="index"><a class="dark index" href="./math.html#floor">floor</a></li><li class="index"><a class="dark index" href="./math.html#ceil">ceil</a></li><li class="index"><a class="dark index" href="./math.html#sin">sin</a></li><li class="index"><a class="dark index" href="./math.html#cos">cos</a></li><li class="index"><a class="dark index" href="./math.html#tan">tan</a></li></ul></div><div class="summary"><h3>io</h3><ul><li class="index"><a class="dark index" href="./io.html#print">print</a></li></ul></div><div class="summary"><h3>loaders</h3><ul><li class="index"><a class="dark index" href="./loaders.html#loadFont">loadFont</a></li></ul></div><div class="summary"><h3>setting</h3><ul><li class="index"><a class="dark index" href="./setting.html#fill">fill</a></li><li class="index"><a class="dark index" href="./setting.html#strokeWeight">strokeWeight</a></li><li class="index"><a class="dark index" href="./setting.html#noFill">noFill</a></li><li class="index"><a class="dark index" href="./setting.html#stroke">stroke</a></li><li class="index"><a class="dark index" href="./setting.html#noStroke">noStroke</a></li><li class="index"><a class="dark index" href="./setting.html#textSize">textSize</a></li><li class="index"><a class="dark index" href="./setting.html#textFont">textFont</a></li></ul></div></div>
</main>
</body>
</html>
83 changes: 57 additions & 26 deletions src/bindings/loaders.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,41 +6,72 @@

int loadFont(lua_State *L) {

const char *str = luaL_checkstring(L, 1);
if (!str) {
luaL_error(L, "Expected first argument to be a string");
const char *font_path = lua_tostring(L, 1);

// Default font
lua_pushinteger(L, 0);
if (!font_path) {
// If default font is defined
if (lu5.font_count >= 1) {
LU5_WARN_TRACE(L, "Returning default font, Expected first argument to be a string");

// return default font
lua_pushinteger(L, 0);
return 1;
} else {

// Throw
luaL_error(L, "Expected first argument to be a string");
return 0;
}

}

int fontId;
lu5_loadfont_err err = lu5_load_font(&lu5, &fontId, font_path, false);
if (err == LU5_LOADFONT_NONE) {
// Return the fontId
lua_pushinteger(L, fontId);
return 1;
}

// Throw
switch(err) {
case LU5_LOADFONT_NOT_FOUND: {
// If default font is defined
if (lu5.font_count >= 1) {
LU5_WARN_TRACE(L, "Could not find '%s' font, returning default font", font_path);

// return default font
lua_pushinteger(L, 0);
return 1;
} else {

int fontId = 0;
lu5_loadfont_err err = LOADFONT_NONE;
if ((err = lu5_load_font(&lu5, str, &fontId)) != 0) {
switch(err) {
case LOADFONT_UNKNOWN: {
luaL_error(L, "Could not find font '%s'", str);
break;
}
case LOADFONT_UNKNOWN_FORMAT: {
luaL_error(L, "Unknown file format for '%s'", str);
break;
// Throw
luaL_error(L, "Could not find '%s' font", font_path);
return 0;
}
case LOADFONT_MAX: {
luaL_error(L, "Cannot load more than %d fonts", MAX_LOADED_FONTS);
break;
}
default: break;
break;
}
// Default font
lua_pushinteger(L, 0);
return 1;
case LU5_LOADFONT_MAX: {
luaL_error(L, "Could not load '%s' since we cannot load more than %d fonts", font_path, MAX_LOADED_FONTS);
break;
}
case LU5_LOADFONT_UNKNOWN: {
luaL_error(L, "Could not find font '%s'", font_path);
break;
}
case LU5_LOADFONT_UNKNOWN_FORMAT: {
luaL_error(L, "Unknown file format for '%s'", font_path);
break;
}
case LU5_LOADFONT_NO_MEM: {
luaL_error(L, "No memory left to load '%s'", font_path);
break;
}
case LU5_LOADFONT_NONE: break;
}

// Return the fontId
lua_pushinteger(L, fontId);
// Return default font
lua_pushinteger(L, 0);
return 1;
}

Expand Down
17 changes: 5 additions & 12 deletions src/bindings/window.c
Original file line number Diff line number Diff line change
Expand Up @@ -114,18 +114,11 @@ int createWindow(lua_State *L) {
glDisable( GL_DEPTH_TEST );
glEnable( GL_ALPHA_TEST );

// TODO: Have a built in static font instead of a TTF.
// this macro comes from the compiler flags,
// if you wan to use `text` without using `loadFont`, please define your default font path
lu5_load_font(&lu5, LU5_DEFAULT_FONT_PATH, NULL);
FT_Set_Char_Size(
lu5.fonts[0]->face,
0,
lu5.style.fontSize * 64,
0,
0
);

// Load default font statically
lu5_loadfont_err err;
if ((err = lu5_load_font(&lu5, NULL, NULL, true)) != LU5_LOADFONT_NONE) {
luaL_error(L, "Error loading default font, code: %i", err);
}

return 0;
}
Expand Down
Loading

0 comments on commit adb784f

Please sign in to comment.