ascii-chat 0.11.33
Video chat in your terminal
Loading...
Searching...
No Matches
platform/macos/font.c
Go to the documentation of this file.
1
6#ifdef __APPLE__
8#include <ascii-chat/font.h>
10#include <string.h>
11#include <sys/stat.h>
12
13static bool is_absolute_path(const char *s) {
14 return s && s[0] == '/';
15}
16static bool file_exists(const char *p) {
17 struct stat st;
18 return stat(p, &st) == 0;
19}
20
21asciichat_error_t platform_font_resolve(const char *spec, char *out, size_t out_size, bool *out_is_path,
22 const uint8_t **out_font_data, size_t *out_font_data_size) {
23 // If no font is specified, use the bundled default font (DejaVu Sans Mono) for render-file output.
24 // This ensures render-file always has a suitable fallback font available.
25 const char *eff = (spec && spec[0]) ? spec : "default";
26
27 if (out_font_data)
28 *out_font_data = NULL;
29 if (out_font_data_size)
30 *out_font_data_size = 0;
31
32 if (is_absolute_path(eff)) {
33 snprintf(out, out_size, "%s", eff);
34 *out_is_path = true;
35 return file_exists(out) ? ASCIICHAT_OK : SET_ERRNO(ERROR_NOT_FOUND, "render-font: not found: %s", out);
36 }
37
38 // Check for bundled font names ("matrix" or "default")
39 if (strcmp(eff, "matrix") == 0) {
40 if (out_font_data)
41 *out_font_data = g_font_matrix_resurrected;
42 if (out_font_data_size)
43 *out_font_data_size = g_font_matrix_resurrected_size;
44 // For macOS, write bundled font to temp file since the renderer expects file paths
45 static char tmp_path[4096] = {0};
46 if (tmp_path[0] == '\0' && out_font_data && *out_font_data) {
47 strlcpy(tmp_path, "/tmp/ascii-chat-matrix-XXXXXX.ttf", sizeof(tmp_path));
48 int fd = mkstemps(tmp_path, 4); // 4 = ".ttf" suffix
49 if (fd >= 0) {
50 write(fd, *out_font_data, *out_font_data_size);
51 close(fd);
52 log_debug("platform_font_resolve: wrote bundled matrix font to %s", tmp_path);
53 }
54 }
55 if (tmp_path[0] != '\0') {
56 snprintf(out, out_size, "%s", tmp_path);
57 *out_is_path = true;
58 }
59 return ASCIICHAT_OK;
60 }
61
62 if (strcmp(eff, "default") == 0) {
63 if (out_font_data)
64 *out_font_data = g_font_default;
65 if (out_font_data_size)
66 *out_font_data_size = g_font_default_size;
67 // For macOS, write bundled font to temp file since the renderer expects file paths
68 static char tmp_path_default[4096] = {0};
69 if (tmp_path_default[0] == '\0' && out_font_data && *out_font_data) {
70 strlcpy(tmp_path_default, "/tmp/ascii-chat-default-XXXXXX.ttf", sizeof(tmp_path_default));
71 int fd = mkstemps(tmp_path_default, 4); // 4 = ".ttf" suffix
72 if (fd >= 0) {
73 write(fd, *out_font_data, *out_font_data_size);
74 close(fd);
75 log_debug("platform_font_resolve: wrote bundled default font to %s", tmp_path_default);
76 }
77 }
78 if (tmp_path_default[0] != '\0') {
79 snprintf(out, out_size, "%s", tmp_path_default);
80 *out_is_path = true;
81 }
82 return ASCIICHAT_OK;
83 }
84
85 // Try to use system font (passed to CoreText).
86 // If this fails at runtime, CoreText will log an error but we can't detect that here.
87 // Note: macOS CoreText is more forgiving with font names than Linux fontconfig.
88 snprintf(out, out_size, "%s", eff);
89 *out_is_path = false;
90
91 // Log that we're using a system font name (will be resolved by CoreText)
92 log_debug("platform_font_resolve: using system font name '%s' (will be resolved by CoreText)", eff);
93 return ASCIICHAT_OK;
94}
95#endif // __APPLE__
Font resolution and bundled font data.
unsigned char uint8_t
Definition common.h:56
#define SET_ERRNO(code, context_msg,...)
Set error code with custom context message and log it, returning the error code.
asciichat_error_t
Error and exit codes - unified status values (0-255)
Definition error_codes.h:49
@ ERROR_NOT_FOUND
@ ASCIICHAT_OK
Definition error_codes.h:51
#define log_debug(...)
Log a DEBUG message.
Definition log/log.h:548
📝 Logging API with multiple log levels and terminal output control
const size_t g_font_matrix_resurrected_size
const unsigned char g_font_matrix_resurrected[]
const size_t g_font_default_size
const unsigned char g_font_default[]
Platform-specific font resolution for render-file output.
asciichat_error_t platform_font_resolve(const char *spec, char *out, size_t out_size, bool *out_is_path, const uint8_t **out_font_data, size_t *out_font_data_size)