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