Mercurial > repos > blastem
annotate nuklear_ui/font.c @ 2179:9a8dd4ba2753
Implement frame advance debugger command
author | Michael Pavone <pavone@retrodev.com> |
---|---|
date | Sat, 13 Aug 2022 19:37:17 -0700 |
parents | 3dd9c68472fb |
children | 369a52e302e2 |
rev | line source |
---|---|
1474
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
1 #include <stdio.h> |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
2 #include <stdlib.h> |
1527
4f6e8acd7b6a
Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
1474
diff
changeset
|
3 #include <stdint.h> |
1934
253c62b7144e
Allow specifying a default font path at build time
Michael Pavone <pavone@retrodev.com>
parents:
1593
diff
changeset
|
4 #include <string.h> |
1527
4f6e8acd7b6a
Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
1474
diff
changeset
|
5 #include "../util.h" |
4f6e8acd7b6a
Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
1474
diff
changeset
|
6 #include "sfnt.h" |
1474
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
7 |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
8 char *default_font_path(void) |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
9 { |
1934
253c62b7144e
Allow specifying a default font path at build time
Michael Pavone <pavone@retrodev.com>
parents:
1593
diff
changeset
|
10 #ifdef FONT_PATH |
253c62b7144e
Allow specifying a default font path at build time
Michael Pavone <pavone@retrodev.com>
parents:
1593
diff
changeset
|
11 FILE *f = fopen(FONT_PATH, "rb"); |
253c62b7144e
Allow specifying a default font path at build time
Michael Pavone <pavone@retrodev.com>
parents:
1593
diff
changeset
|
12 if (f) { |
253c62b7144e
Allow specifying a default font path at build time
Michael Pavone <pavone@retrodev.com>
parents:
1593
diff
changeset
|
13 fclose(f); |
253c62b7144e
Allow specifying a default font path at build time
Michael Pavone <pavone@retrodev.com>
parents:
1593
diff
changeset
|
14 return strdup(FONT_PATH); |
253c62b7144e
Allow specifying a default font path at build time
Michael Pavone <pavone@retrodev.com>
parents:
1593
diff
changeset
|
15 } |
253c62b7144e
Allow specifying a default font path at build time
Michael Pavone <pavone@retrodev.com>
parents:
1593
diff
changeset
|
16 #endif |
1976
3dd9c68472fb
Specify desired language when invoking fc-match to find an appropriate font on Linux
Michael Pavone <pavone@retrodev.com>
parents:
1934
diff
changeset
|
17 //TODO: specify language dynamically once BlastEm is localized |
3dd9c68472fb
Specify desired language when invoking fc-match to find an appropriate font on Linux
Michael Pavone <pavone@retrodev.com>
parents:
1934
diff
changeset
|
18 FILE *fc_pipe = popen("fc-match :lang=en -f '%{file}'", "r"); |
1474
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
19 if (!fc_pipe) { |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
20 return NULL; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
21 } |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
22 size_t buf_size = 128; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
23 char *buffer = NULL; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
24 size_t total = 0, read = 0; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
25 do { |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
26 total += read; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
27 buf_size *= 2; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
28 buffer = realloc(buffer, buf_size); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
29 if (!buffer) { |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
30 return NULL; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
31 } |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
32 read = fread(buffer, 1, buf_size - total, fc_pipe); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
33 } while (read == (buf_size - total)); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
34 total += read; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
35 buffer[total] = 0; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
36 |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
37 return buffer; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
38 } |
1527
4f6e8acd7b6a
Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
1474
diff
changeset
|
39 |
4f6e8acd7b6a
Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
1474
diff
changeset
|
40 uint8_t *default_font(uint32_t *size_out) |
4f6e8acd7b6a
Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
1474
diff
changeset
|
41 { |
4f6e8acd7b6a
Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
1474
diff
changeset
|
42 char *path = default_font_path(); |
4f6e8acd7b6a
Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
1474
diff
changeset
|
43 if (!path) { |
4f6e8acd7b6a
Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
1474
diff
changeset
|
44 goto error; |
4f6e8acd7b6a
Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
1474
diff
changeset
|
45 } |
4f6e8acd7b6a
Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
1474
diff
changeset
|
46 FILE *f = fopen(path, "rb"); |
1593
24508cb54f87
Fix a number of other memory errors (mostly leaks again) identified by valgrind
Michael Pavone <pavone@retrodev.com>
parents:
1527
diff
changeset
|
47 free(path); |
1527
4f6e8acd7b6a
Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
1474
diff
changeset
|
48 if (!f) { |
4f6e8acd7b6a
Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
1474
diff
changeset
|
49 goto error; |
4f6e8acd7b6a
Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
1474
diff
changeset
|
50 } |
4f6e8acd7b6a
Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
1474
diff
changeset
|
51 long size = file_size(f); |
4f6e8acd7b6a
Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
1474
diff
changeset
|
52 uint8_t *buffer = malloc(size); |
4f6e8acd7b6a
Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
1474
diff
changeset
|
53 if (size != fread(buffer, 1, size, f)) { |
4f6e8acd7b6a
Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
1474
diff
changeset
|
54 fclose(f); |
4f6e8acd7b6a
Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
1474
diff
changeset
|
55 goto error; |
4f6e8acd7b6a
Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
1474
diff
changeset
|
56 } |
4f6e8acd7b6a
Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
1474
diff
changeset
|
57 fclose(f); |
4f6e8acd7b6a
Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
1474
diff
changeset
|
58 sfnt_container *sfnt = load_sfnt(buffer, size); |
4f6e8acd7b6a
Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
1474
diff
changeset
|
59 if (!sfnt) { |
4f6e8acd7b6a
Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
1474
diff
changeset
|
60 free(buffer); |
4f6e8acd7b6a
Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
1474
diff
changeset
|
61 goto error; |
4f6e8acd7b6a
Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
1474
diff
changeset
|
62 } |
4f6e8acd7b6a
Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
1474
diff
changeset
|
63 return sfnt_flatten(sfnt->tables, size_out); |
4f6e8acd7b6a
Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
1474
diff
changeset
|
64 error: |
4f6e8acd7b6a
Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
1474
diff
changeset
|
65 //TODO: try to find a suitable font in /usr/share/fonts as a fallback |
4f6e8acd7b6a
Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
1474
diff
changeset
|
66 return NULL; |
4f6e8acd7b6a
Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
1474
diff
changeset
|
67 } |