Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
jangler opened this issue Jan 14, 2024 · 3 comments
Open

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

jangler opened this issue Jan 14, 2024 · 3 comments

Comments

@jangler
Copy link

jangler commented Jan 14, 2024

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
Copy link

sogaiu commented Jan 15, 2024

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
Copy link

sogaiu commented Jan 17, 2024

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 pushed a commit to sogaiu/jaylib that referenced this issue Jan 17, 2024
@sogaiu
Copy link

sogaiu commented Jan 17, 2024

Tried it out, seems to work.

Will submit PR.

Submitted #57.

bakpakin added a commit that referenced this issue May 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants