Mercurial > repos > blastem
annotate nuklear_ui/font_mac.m @ 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 | b4914d92308b |
children | 098c11aaf8f0 |
rev | line source |
---|---|
1537
b4914d92308b
Added OSX implementation of default_font
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
1 #import <AppKit/AppKit.h> |
b4914d92308b
Added OSX implementation of default_font
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
2 #include <stddef.h> |
b4914d92308b
Added OSX implementation of default_font
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
3 #include "../paths.h" |
b4914d92308b
Added OSX implementation of default_font
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
4 #include "../util.h" |
b4914d92308b
Added OSX implementation of default_font
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
5 #include "sfnt.h" |
b4914d92308b
Added OSX implementation of default_font
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
6 |
b4914d92308b
Added OSX implementation of default_font
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
7 sfnt_table *find_font_in_dir(char *path, char *prefix, const char *ps_name) |
b4914d92308b
Added OSX implementation of default_font
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
8 { |
b4914d92308b
Added OSX implementation of default_font
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
9 size_t num_entries; |
b4914d92308b
Added OSX implementation of default_font
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
10 dir_entry *entries = get_dir_list(path, &num_entries); |
b4914d92308b
Added OSX implementation of default_font
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
11 size_t prefix_len = strlen(prefix); |
b4914d92308b
Added OSX implementation of default_font
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
12 sfnt_table *selected = NULL; |
b4914d92308b
Added OSX implementation of default_font
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
13 for (size_t i = 0; i < num_entries && !selected; i++) |
b4914d92308b
Added OSX implementation of default_font
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
14 { |
b4914d92308b
Added OSX implementation of default_font
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
15 char *ext = path_extension(entries[i].name); |
b4914d92308b
Added OSX implementation of default_font
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
16 if (!ext || (strcasecmp(ext, "ttf") && strcasecmp(ext, "ttc") && strcasecmp(ext, "dfont"))) { |
b4914d92308b
Added OSX implementation of default_font
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
17 //not a truetype font, ignore |
b4914d92308b
Added OSX implementation of default_font
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
18 free(ext); |
b4914d92308b
Added OSX implementation of default_font
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
19 continue; |
b4914d92308b
Added OSX implementation of default_font
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
20 } |
b4914d92308b
Added OSX implementation of default_font
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
21 free(ext); |
b4914d92308b
Added OSX implementation of default_font
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
22 if (!strncasecmp(entries[i].name, prefix, prefix_len)) { |
b4914d92308b
Added OSX implementation of default_font
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
23 char *full_path = path_append(path, entries[i].name); |
b4914d92308b
Added OSX implementation of default_font
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
24 FILE *f = fopen(full_path, "rb"); |
b4914d92308b
Added OSX implementation of default_font
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
25 if (f) |
b4914d92308b
Added OSX implementation of default_font
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
26 { |
b4914d92308b
Added OSX implementation of default_font
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
27 long font_size = file_size(f); |
b4914d92308b
Added OSX implementation of default_font
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
28 uint8_t *blob = malloc(font_size); |
b4914d92308b
Added OSX implementation of default_font
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
29 if (font_size == fread(blob, 1, font_size, f)) |
b4914d92308b
Added OSX implementation of default_font
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
30 { |
b4914d92308b
Added OSX implementation of default_font
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
31 sfnt_container *sfnt = load_sfnt(blob, font_size); |
b4914d92308b
Added OSX implementation of default_font
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
32 if (sfnt) { |
b4914d92308b
Added OSX implementation of default_font
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
33 for (uint8_t j = 0; j < sfnt->num_fonts && !selected; j++) |
b4914d92308b
Added OSX implementation of default_font
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
34 { |
b4914d92308b
Added OSX implementation of default_font
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
35 char *cur_ps = sfnt_name(sfnt->tables + j, SFNT_POSTSCRIPT); |
b4914d92308b
Added OSX implementation of default_font
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
36 if (!strcmp(cur_ps, ps_name)) { |
b4914d92308b
Added OSX implementation of default_font
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
37 selected = sfnt->tables + j; |
b4914d92308b
Added OSX implementation of default_font
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
38 } |
b4914d92308b
Added OSX implementation of default_font
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
39 free(cur_ps); |
b4914d92308b
Added OSX implementation of default_font
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
40 } |
b4914d92308b
Added OSX implementation of default_font
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
41 } else { |
b4914d92308b
Added OSX implementation of default_font
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
42 free(blob); |
b4914d92308b
Added OSX implementation of default_font
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
43 } |
b4914d92308b
Added OSX implementation of default_font
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
44 } else { |
b4914d92308b
Added OSX implementation of default_font
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
45 free(blob); |
b4914d92308b
Added OSX implementation of default_font
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
46 } |
b4914d92308b
Added OSX implementation of default_font
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
47 fclose(f); |
b4914d92308b
Added OSX implementation of default_font
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
48 } |
b4914d92308b
Added OSX implementation of default_font
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
49 free(full_path); |
b4914d92308b
Added OSX implementation of default_font
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
50 } |
b4914d92308b
Added OSX implementation of default_font
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
51 } |
b4914d92308b
Added OSX implementation of default_font
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
52 return selected; |
b4914d92308b
Added OSX implementation of default_font
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
53 } |
b4914d92308b
Added OSX implementation of default_font
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
54 |
b4914d92308b
Added OSX implementation of default_font
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
55 uint8_t *default_font(uint32_t *size_out) |
b4914d92308b
Added OSX implementation of default_font
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
56 { |
b4914d92308b
Added OSX implementation of default_font
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
57 NSFont *sys = [NSFont systemFontOfSize:0]; |
b4914d92308b
Added OSX implementation of default_font
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
58 NSString *name = [sys fontName]; |
b4914d92308b
Added OSX implementation of default_font
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
59 const char *ps_name = [name UTF8String]; |
b4914d92308b
Added OSX implementation of default_font
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
60 const unsigned char *prefix_start = (const unsigned char *)ps_name; |
b4914d92308b
Added OSX implementation of default_font
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
61 while(*prefix_start && ( |
b4914d92308b
Added OSX implementation of default_font
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
62 *prefix_start < '0' || |
b4914d92308b
Added OSX implementation of default_font
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
63 (*prefix_start > 'z' && *prefix_start <= 0x80) || |
b4914d92308b
Added OSX implementation of default_font
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
64 (*prefix_start > 'Z' && *prefix_start < 'a') || |
b4914d92308b
Added OSX implementation of default_font
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
65 (*prefix_start > '9' && *prefix_start < 'A') |
b4914d92308b
Added OSX implementation of default_font
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
66 )) |
b4914d92308b
Added OSX implementation of default_font
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
67 { |
b4914d92308b
Added OSX implementation of default_font
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
68 prefix_start++; |
b4914d92308b
Added OSX implementation of default_font
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
69 } |
b4914d92308b
Added OSX implementation of default_font
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
70 if (!*prefix_start) { |
b4914d92308b
Added OSX implementation of default_font
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
71 //Didn't find a suitable starting character, just start from the beginning |
b4914d92308b
Added OSX implementation of default_font
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
72 prefix_start = (const unsigned char *)ps_name; |
b4914d92308b
Added OSX implementation of default_font
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
73 } |
b4914d92308b
Added OSX implementation of default_font
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
74 const unsigned char *prefix_end = (const unsigned char *)prefix_start + 1; |
b4914d92308b
Added OSX implementation of default_font
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
75 while (*prefix_end && *prefix_end >= 'a') |
b4914d92308b
Added OSX implementation of default_font
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
76 { |
b4914d92308b
Added OSX implementation of default_font
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
77 prefix_end++; |
b4914d92308b
Added OSX implementation of default_font
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
78 } |
b4914d92308b
Added OSX implementation of default_font
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
79 char *prefix = malloc(prefix_end - prefix_start + 1); |
b4914d92308b
Added OSX implementation of default_font
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
80 memcpy(prefix, prefix_start, prefix_end - prefix_start); |
b4914d92308b
Added OSX implementation of default_font
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
81 prefix[prefix_end-prefix_start] = 0; |
b4914d92308b
Added OSX implementation of default_font
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
82 //check /Library/Fonts first |
b4914d92308b
Added OSX implementation of default_font
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
83 sfnt_table *selected = find_font_in_dir("/Library/Fonts", (char *)prefix, ps_name); |
b4914d92308b
Added OSX implementation of default_font
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
84 if (!selected) { |
b4914d92308b
Added OSX implementation of default_font
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
85 selected = find_font_in_dir("/System/Library/Fonts", (char *)prefix, ps_name); |
b4914d92308b
Added OSX implementation of default_font
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
86 } |
b4914d92308b
Added OSX implementation of default_font
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
87 if (!selected) { |
b4914d92308b
Added OSX implementation of default_font
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
88 fatal_error("Failed to find system font %s using prefix %s\n", ps_name, prefix); |
b4914d92308b
Added OSX implementation of default_font
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
89 } |
b4914d92308b
Added OSX implementation of default_font
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
90 free(prefix); |
b4914d92308b
Added OSX implementation of default_font
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
91 return sfnt_flatten(selected, size_out); |
b4914d92308b
Added OSX implementation of default_font
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
92 } |
b4914d92308b
Added OSX implementation of default_font
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
93 |