janet-lang / jaylib

Janet bindings to Raylib
MIT License
143 stars 37 forks source link

Can't pass null to font loading functions? #56

Open jangler opened 10 months ago

jangler commented 10 months ago

The Raylib documentation for LoadFontEx says to "use NULL for codepoints and 0 for codepointCount to load the default character set". Literally every example of Raylib font loading I could find does this, but it seems not to be possible with Janet's wrapping of the API.

sogaiu commented 10 months ago

The current implementation of load-font-ex doesn't seem to handle that:

static Janet cfun_LoadFontEx(int32_t argc, Janet *argv) {
    janet_fixarity(argc, 3);
    const char *fileName = janet_getcstring(argv, 0);
    int fontSize = janet_getinteger(argv, 1);
    JanetView ints = janet_getindexed(argv, 2);
    int *raw_ints = janet_smalloc(sizeof(int) * ints.len);
    for (int32_t i = 0; i < ints.len; i++) {
        raw_ints[i] = janet_getinteger(ints.items, i);
    }
    Font *font = janet_abstract(&AT_Font, sizeof(Font));
    *font = LoadFontEx(fileName, fontSize, raw_ints, ints.len);
    janet_sfree(raw_ints);
    return janet_wrap_abstract(font);
}

May be that's fairly straight-forward to do though by examining the arguments and acting appropriately.

sogaiu commented 10 months ago

May be a change along the following lines could work:

diff --git a/src/text.h b/src/text.h
index c9712df..5ce4ce7 100644
--- a/src/text.h
+++ b/src/text.h
@@ -42,17 +42,21 @@ static Janet cfun_IsFontReady(int32_t argc, Janet *argv) {
 }

 static Janet cfun_LoadFontEx(int32_t argc, Janet *argv) {
-    janet_fixarity(argc, 3);
+    janet_arity(argc, 2, 3);
     const char *fileName = janet_getcstring(argv, 0);
     int fontSize = janet_getinteger(argv, 1);
-    JanetView ints = janet_getindexed(argv, 2);
-    int *raw_ints = janet_smalloc(sizeof(int) * ints.len);
-    for (int32_t i = 0; i < ints.len; i++) {
-        raw_ints[i] = janet_getinteger(ints.items, i);
-    }
     Font *font = janet_abstract(&AT_Font, sizeof(Font));
-    *font = LoadFontEx(fileName, fontSize, raw_ints, ints.len);
-    janet_sfree(raw_ints);
+    if (argc == 2) {
+        *font = LoadFontEx(fileName, fontSize, NULL, 0);
+    } else {
+        JanetView ints = janet_getindexed(argv, 2);
+        int *raw_ints = janet_smalloc(sizeof(int) * ints.len);
+        for (int32_t i = 0; i < ints.len; i++) {
+            raw_ints[i] = janet_getinteger(ints.items, i);
+        }
+        *font = LoadFontEx(fileName, fontSize, raw_ints, ints.len);
+        janet_sfree(raw_ints);
+    }
     return janet_wrap_abstract(font);
 }

So, when invoked with 2 arguments:

(load-font-ex file-name font-size)

the underlying LoadFontEx would get NULL for its 3rd argument and 0 for its 4th argument. Or something like that (^^;

(Note: compiled ok, but execution untested)

sogaiu commented 10 months ago

Tried it out, seems to work.

~Will submit PR.~

Submitted #57.