Mercurial > repos > blastem
annotate nuklear_ui/font.c @ 1556:075df0844baa
Randomize soft reset timing and fix silly bug that was accidentally clearing IO state on soft reset
author | Michael Pavone <pavone@retrodev.com> |
---|---|
date | Fri, 30 Mar 2018 22:01:05 -0700 |
parents | 4f6e8acd7b6a |
children | 24508cb54f87 |
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> |
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
|
4 #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
|
5 #include "sfnt.h" |
1474
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
6 |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
7 char *default_font_path(void) |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
8 { |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
9 FILE *fc_pipe = popen("fc-match -f '%{file}'", "r"); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
10 if (!fc_pipe) { |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
11 return NULL; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
12 } |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
13 size_t buf_size = 128; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
14 char *buffer = NULL; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
15 size_t total = 0, read = 0; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
16 do { |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
17 total += read; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
18 buf_size *= 2; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
19 buffer = realloc(buffer, buf_size); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
20 if (!buffer) { |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
21 return NULL; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
22 } |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
23 read = fread(buffer, 1, buf_size - total, fc_pipe); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
24 } while (read == (buf_size - total)); |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
25 total += read; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
26 buffer[total] = 0; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
27 |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
28 return buffer; |
c5c022c7aa54
Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
29 } |
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
|
30 |
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
|
31 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
|
32 { |
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
|
33 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
|
34 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
|
35 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
|
36 } |
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
|
37 FILE *f = fopen(path, "rb"); |
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
|
38 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
|
39 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
|
40 } |
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 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
|
42 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
|
43 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
|
44 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
|
45 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
|
46 } |
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
|
47 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
|
48 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
|
49 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
|
50 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
|
51 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
|
52 } |
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 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
|
54 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
|
55 //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
|
56 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
|
57 } |