comparison nuklear_ui/font.c @ 1527:4f6e8acd7b6a nuklear_ui

Added support for TTC and dfont format true type fonts. More robust font selection on Windows
author Michael Pavone <pavone@retrodev.com>
date Tue, 06 Mar 2018 21:27:12 -0800
parents c5c022c7aa54
children 24508cb54f87
comparison
equal deleted inserted replaced
1526:9bea1a199f15 1527:4f6e8acd7b6a
1 #include <stdio.h> 1 #include <stdio.h>
2 #include <stdlib.h> 2 #include <stdlib.h>
3 #include <stdint.h>
4 #include "../util.h"
5 #include "sfnt.h"
3 6
4 char *default_font_path(void) 7 char *default_font_path(void)
5 { 8 {
6 FILE *fc_pipe = popen("fc-match -f '%{file}'", "r"); 9 FILE *fc_pipe = popen("fc-match -f '%{file}'", "r");
7 if (!fc_pipe) { 10 if (!fc_pipe) {
22 total += read; 25 total += read;
23 buffer[total] = 0; 26 buffer[total] = 0;
24 27
25 return buffer; 28 return buffer;
26 } 29 }
30
31 uint8_t *default_font(uint32_t *size_out)
32 {
33 char *path = default_font_path();
34 if (!path) {
35 goto error;
36 }
37 FILE *f = fopen(path, "rb");
38 if (!f) {
39 goto error;
40 }
41 long size = file_size(f);
42 uint8_t *buffer = malloc(size);
43 if (size != fread(buffer, 1, size, f)) {
44 fclose(f);
45 goto error;
46 }
47 fclose(f);
48 sfnt_container *sfnt = load_sfnt(buffer, size);
49 if (!sfnt) {
50 free(buffer);
51 goto error;
52 }
53 return sfnt_flatten(sfnt->tables, size_out);
54 error:
55 //TODO: try to find a suitable font in /usr/share/fonts as a fallback
56 return NULL;
57 }