annotate nuklear_ui/font.c @ 1971:80920c21bb52

Add an event log soft flush and call it twice per frame in between hard flushes to netplay latency when there are insufficient hardware updates to flush packets in the middle of a frame
author Michael Pavone <pavone@retrodev.com>
date Fri, 08 May 2020 11:40:30 -0700
parents 253c62b7144e
children 3dd9c68472fb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
1474
c5c022c7aa54 Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
17 FILE *fc_pipe = popen("fc-match -f '%{file}'", "r");
c5c022c7aa54 Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
18 if (!fc_pipe) {
c5c022c7aa54 Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
19 return NULL;
c5c022c7aa54 Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
20 }
c5c022c7aa54 Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
21 size_t buf_size = 128;
c5c022c7aa54 Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
22 char *buffer = NULL;
c5c022c7aa54 Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
23 size_t total = 0, read = 0;
c5c022c7aa54 Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
24 do {
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 buf_size *= 2;
c5c022c7aa54 Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
27 buffer = realloc(buffer, buf_size);
c5c022c7aa54 Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
28 if (!buffer) {
c5c022c7aa54 Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
29 return NULL;
c5c022c7aa54 Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
30 }
c5c022c7aa54 Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
31 read = fread(buffer, 1, buf_size - total, fc_pipe);
c5c022c7aa54 Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
32 } while (read == (buf_size - total));
c5c022c7aa54 Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
33 total += read;
c5c022c7aa54 Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
34 buffer[total] = 0;
c5c022c7aa54 Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
35
c5c022c7aa54 Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
36 return buffer;
c5c022c7aa54 Initial work on Nuklear-based UI
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
37 }
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
38
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 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
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 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
42 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
43 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
44 }
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 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
46 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
47 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
48 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
49 }
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 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
51 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
52 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
53 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
54 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
55 }
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 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
57 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
58 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
59 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
60 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
61 }
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 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
63 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
64 //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
65 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
66 }