/* SPDX-License-Identifier: GPL-2.0-or-later */ /* Font discovery for the portable text engine (see gpu_fonts.h). * * Families by name: each font file in the font folders is opened once and * only its table directory, `name` table (family names 1 and 16) and `OS/2` * table (weight, italic) are read, never the outlines; collections (.ttc) * give each of their faces. That is a few kilobytes per file, on the first * lookup of a family that is neither registered nor generic. The folders are * read on a thread of their own from the first text drawn * (gpu_font_index_warm), and the result is kept in the user's cache folder * until a font folder or file changes; a lookup waits only if the thread is still * reading. */ #include "gpu_fonts.h" #include "GPU_raster.h" #include #include #include #ifdef _WIN32 # include #else # include # include # include # include #endif int gpu_font_dirs(char dirs[][GPU_FONT_PATH], int max) { int n = 0; const char *env = getenv("BUI_FONT_DIR"); if (env && env[0] && n < max) { snprintf(dirs[n++], GPU_FONT_PATH, "%s", env); } #ifdef _WIN32 const char *win = getenv("WINDIR"); if (n < max) { snprintf(dirs[n++], GPU_FONT_PATH, "%s/Fonts", win ? win : "C:/Windows"); } const char *local = getenv("LOCALAPPDATA"); if (local && n < max) { snprintf(dirs[n++], GPU_FONT_PATH, "%s/Microsoft/Windows/Fonts", local); } #elif defined(__APPLE__) static const char *mac[] = {"/System/Library/Fonts", "/System/Library/Fonts/Supplemental", "/Library/Fonts"}; for (int i = 0; i < 3 && n < max; i++) { snprintf(dirs[n++], GPU_FONT_PATH, "%s", mac[i]); } const char *home = getenv("HOME"); if (home && n < max) { snprintf(dirs[n++], GPU_FONT_PATH, "%s/Library/Fonts", home); } #elif defined(__ANDROID__) /* the system's Noto set (CJK, Thai, Indic, symbols ...) and a vendor's extra fonts */ static const char *droid[] = {"/system/fonts", "/product/fonts"}; for (int i = 0; i < 2 && n < max; i++) { snprintf(dirs[n++], GPU_FONT_PATH, "%s", droid[i]); } #else static const char *nix[] = { "/usr/share/fonts/truetype/dejavu", "/usr/share/fonts/dejavu", "/usr/share/fonts/TTF", "/usr/share/fonts/truetype/liberation", "/usr/share/fonts/liberation", "/usr/share/fonts/truetype/noto", "/usr/share/fonts/noto", "/usr/share/fonts/google-noto", "/usr/share/fonts/opentype/noto", "/usr/share/fonts/noto-cjk", "/usr/share/fonts/google-noto-cjk", "/usr/share/fonts/truetype", "/usr/local/share/fonts", "/usr/share/fonts"}; for (int i = 0; i < (int)(sizeof(nix) / sizeof(nix[0])) && n < max; i++) { snprintf(dirs[n++], GPU_FONT_PATH, "%s", nix[i]); } const char *home = getenv("HOME"); if (home && n < max) { snprintf(dirs[n++], GPU_FONT_PATH, "%s/.local/share/fonts", home); } if (home && n < max) { snprintf(dirs[n++], GPU_FONT_PATH, "%s/.fonts", home); } #endif return n; } int BUI_text_font_dir(int i, char *r_dir, int cap) { char dirs[GPU_FONT_DIRS_MAX][GPU_FONT_PATH]; if (i < 0 || i >= gpu_font_dirs(dirs, GPU_FONT_DIRS_MAX) || !r_dir || cap <= 0) { return 0; } snprintf(r_dir, (size_t)cap, "%s", dirs[i]); return (int)strlen(r_dir); } /* -- the family index ---------------------------------------------------------------- */ /* A font file read, as it was then: a font rewritten in place changes no folder's stamp, so * the saved index keeps each file's own (index_load). */ typedef struct FontFile { long long size, stamp; char path[GPU_FONT_PATH]; } FontFile; typedef struct Named { char family[64]; int file; /* in g_paths */ int index; /* face in a collection */ int style; /* BUI_FONT_BOLD | BUI_FONT_ITALIC */ } Named; static FontFile *g_paths = NULL; static int g_path_n = 0, g_path_cap = 0; static Named *g_named = NULL; /* by file, in the order the files were read */ static int g_named_n = 0, g_named_cap = 0; static int g_index_lost = 0; /* something read couldn't be kept: the index isn't saved */ /* Room for one more of `*items` (`n` held, `*cap` allocated, `size` bytes each); 0 when * there is none, and the index isn't saved. */ static int grow(void **items, int n, int *cap, size_t size) { if (n < *cap) { return 1; } const int more = *cap ? *cap * 2 : 64; void *grown = realloc(*items, size * (size_t)more); if (!grown) { g_index_lost = 1; return 0; } *items = grown; *cap = more; return 1; } static unsigned rd16(const unsigned char *p) { return (unsigned)p[0] << 8 | p[1]; } static unsigned long rd32(const unsigned char *p) { return (unsigned long)p[0] << 24 | (unsigned long)p[1] << 16 | (unsigned long)p[2] << 8 | p[3]; } static int read_at(FILE *fp, unsigned long off, void *buf, size_t n) { return fseek(fp, (long)off, SEEK_SET) == 0 && fread(buf, 1, n, fp) == n; } static int same_name(const char *a, const char *b) { for (; *a && *b; a++, b++) { const char ca = (char)(*a >= 'A' && *a <= 'Z' ? *a + 32 : *a); const char cb = (char)(*b >= 'A' && *b <= 'Z' ? *b + 32 : *b); if (ca != cb) { return 0; } } return *a == *b; } /* Appends a font file, as it is (`size`, `stamp`: see path_stamp); its records follow it * (named_push). 0 when there's no room. */ static int path_push(const char *path, long long size, long long stamp) { if (!grow((void **)&g_paths, g_path_n, &g_path_cap, sizeof(FontFile))) { return 0; } FontFile *f = &g_paths[g_path_n++]; f->size = size; f->stamp = stamp; snprintf(f->path, sizeof(f->path), "%s", path); return 1; } /* Appends a record of the last font file (a saved index's are distinct already: see * named_add). */ static void named_push(const char *family, int index, int style) { if (!grow((void **)&g_named, g_named_n, &g_named_cap, sizeof(Named))) { return; } Named *e = &g_named[g_named_n++]; snprintf(e->family, sizeof(e->family), "%s", family); e->file = g_path_n - 1; e->index = index; e->style = style; } /* Adds a record read from the last font file, once (family 1 and 16 are often the same * name): only that file's records, the last ones, can be the same. */ static void named_add(const char *family, int index, int style) { for (int i = g_named_n - 1; i >= 0 && g_named[i].file == g_path_n - 1; i--) { const Named *e = &g_named[i]; if (e->index == index && e->style == style && same_name(e->family, family)) { return; } } named_push(family, index, style); } /* A `name` record's string as UTF-8: UTF-16BE (Windows / Unicode * platforms) or a single-byte Mac encoding (its ASCII part). */ static void name_utf8(const unsigned char *s, unsigned len, int utf16, char *out, size_t cap) { size_t o = 0; for (unsigned i = 0; i < len && o + 4 < cap;) { unsigned c; if (utf16) { if (i + 1 >= len) { break; } c = rd16(s + i); i += 2; } else { c = s[i++]; if (c >= 0x80) { continue; } } if (c < 0x80) { out[o++] = (char)c; } else if (c < 0x800) { out[o++] = (char)(0xc0 | c >> 6); out[o++] = (char)(0x80 | (c & 0x3f)); } else if (c < 0xd800 || c > 0xdfff) { out[o++] = (char)(0xe0 | c >> 12); out[o++] = (char)(0x80 | (c >> 6 & 0x3f)); out[o++] = (char)(0x80 | (c & 0x3f)); } } out[o] = 0; } /* The family names and style of the face at `off` of the last font file. */ static void named_face(FILE *fp, int index, unsigned long off) { unsigned char hdr[12]; if (!read_at(fp, off, hdr, sizeof(hdr))) { return; } const unsigned nt = rd16(hdr + 4); if (nt == 0 || nt > 64) { return; } unsigned char dir[64 * 16]; if (!read_at(fp, off + 12, dir, (size_t)nt * 16)) { return; } unsigned long name_off = 0, name_len = 0, os2_off = 0, os2_len = 0; for (unsigned k = 0; k < nt; k++) { const unsigned char *t = dir + k * 16; if (memcmp(t, "name", 4) == 0) { name_off = rd32(t + 8); name_len = rd32(t + 12); } else if (memcmp(t, "OS/2", 4) == 0) { os2_off = rd32(t + 8); os2_len = rd32(t + 12); } } if (!name_off || name_len < 6 || name_len > (1ul << 20)) { return; } /* only the fields the table holds: usWeightClass (bytes 4-5), fsSelection (62-63) */ int style = 0; unsigned char os2[64]; const size_t os2_read = os2_len < sizeof(os2) ? (size_t)os2_len : sizeof(os2); if (os2_off && os2_read >= 6 && read_at(fp, os2_off, os2, os2_read)) { style |= rd16(os2 + 4) >= 600 ? BUI_FONT_BOLD : 0; style |= (os2_read >= 64 && (rd16(os2 + 62) & 1)) ? BUI_FONT_ITALIC : 0; } unsigned char *nm = (unsigned char *)malloc(name_len); if (!nm || !read_at(fp, name_off, nm, name_len)) { free(nm); return; } const unsigned count = rd16(nm + 2), strings = rd16(nm + 4); for (unsigned r = 0; r < count && 6 + (r + 1) * 12 <= name_len; r++) { const unsigned char *rec = nm + 6 + r * 12; const unsigned plat = rd16(rec), id = rd16(rec + 6), len = rd16(rec + 8), soff = rd16(rec + 10); /* family (1) and typographic family (16): "Yu Gothic Medium" and "Yu Gothic" */ if ((id != 1 && id != 16) || (plat != 0 && plat != 1 && plat != 3) || (unsigned long)strings + soff + len > name_len) { continue; } char family[64]; name_utf8(nm + strings + soff, len, plat != 1, family, sizeof(family)); if (family[0]) { named_add(family, index, style); } } free(nm); } static long long path_stamp(const char *path, long long *r_size); static void named_file(const char *path) { long long size = 0; const long long stamp = path_stamp(path, &size); /* before it's read: a change after is seen */ if (!path_push(path, size, stamp)) { return; } #ifdef _WIN32 FILE *fp = fopen(path, "rb"); #else /* nonblocking, and a regular file only: a FIFO named like a font (or put in one's place * since the folder was read) would stall the open or the reads */ const int fd = open(path, O_RDONLY | O_NONBLOCK); struct stat st; FILE *fp = fd >= 0 && fstat(fd, &st) == 0 && S_ISREG(st.st_mode) ? fdopen(fd, "rb") : NULL; if (!fp && fd >= 0) { close(fd); } #endif if (!fp) { return; } unsigned char hdr[12]; if (read_at(fp, 0, hdr, sizeof(hdr))) { if (memcmp(hdr, "ttcf", 4) == 0) { const unsigned long n = rd32(hdr + 8); for (unsigned long i = 0; i < n && i < 32; i++) { unsigned char o[4]; if (read_at(fp, 12 + i * 4, o, 4)) { named_face(fp, (int)i, rd32(o)); } } } else { named_face(fp, 0, 0); } } fclose(fp); } static int font_file(const char *name) { const size_t n = strlen(name); if (n < 5 || name[n - 4] != '.') { return 0; } const char *e = name + n - 3; return same_name(e, "ttf") || same_name(e, "otf") || same_name(e, "ttc"); } /* The folders read below the font folders, with when each last changed: a * font added to one changes its stamp, not the font folder's, so the saved * index keeps them all (index_save). */ typedef struct SubDir { long long stamp; char dir[GPU_FONT_PATH]; } SubDir; static SubDir *g_subdirs = NULL; static int g_subdir_n = 0, g_subdir_cap = 0; static void subdir_add(const char *dir) { if (!grow((void **)&g_subdirs, g_subdir_n, &g_subdir_cap, sizeof(SubDir))) { return; } SubDir *s = &g_subdirs[g_subdir_n++]; s->stamp = path_stamp(dir, NULL); snprintf(s->dir, sizeof(s->dir), "%s", dir); } /* Every font file under `dir`, `depth` folders deep. */ static void named_dir(const char *dir, int depth) { char path[GPU_FONT_PATH]; #ifdef _WIN32 char pattern[GPU_FONT_PATH + 4]; snprintf(pattern, sizeof(pattern), "%s/*", dir); WIN32_FIND_DATAA fd; HANDLE h = FindFirstFileA(pattern, &fd); if (h == INVALID_HANDLE_VALUE) { return; } do { if (fd.cFileName[0] == '.') { continue; } snprintf(path, sizeof(path), "%s/%s", dir, fd.cFileName); if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { if (depth > 0) { subdir_add(path); named_dir(path, depth - 1); } } else if (font_file(fd.cFileName)) { named_file(path); } } while (FindNextFileA(h, &fd)); FindClose(h); #else DIR *d = opendir(dir); if (!d) { return; } struct dirent *ent; while ((ent = readdir(d)) != NULL) { if (ent->d_name[0] == '.') { continue; } snprintf(path, sizeof(path), "%s/%s", dir, ent->d_name); struct stat st; if (stat(path, &st) != 0) { continue; } if (S_ISDIR(st.st_mode)) { if (depth > 0) { subdir_add(path); named_dir(path, depth - 1); } } else if (S_ISREG(st.st_mode) && font_file(ent->d_name)) { named_file(path); } } closedir(d); #endif } /* -- the saved index ------------------------------------------------------------------- */ #define INDEX_MAGIC "BUI font index 3" /* When `path` last changed (a folder: a font added or removed; a file: rewritten), and its * size in `*r_size` (may be NULL); 0 when absent. */ static long long path_stamp(const char *path, long long *r_size) { #ifdef _WIN32 WIN32_FILE_ATTRIBUTE_DATA a; if (!GetFileAttributesExA(path, GetFileExInfoStandard, &a)) { return 0; } if (r_size) { *r_size = (long long)a.nFileSizeHigh << 32 | a.nFileSizeLow; } return (long long)a.ftLastWriteTime.dwHighDateTime << 32 | a.ftLastWriteTime.dwLowDateTime; #else struct stat st; if (stat(path, &st) != 0) { return 0; } if (r_size) { *r_size = (long long)st.st_size; } return (long long)st.st_mtime; #endif } static void make_dir(const char *dir) { #ifdef _WIN32 CreateDirectoryA(dir, NULL); #else mkdir(dir, 0755); #endif } /* Where the index is kept between runs (the user's cache folder); 0 when there is none. */ static int index_path(char r_path[GPU_FONT_PATH]) { #ifdef _WIN32 const char *base = getenv("LOCALAPPDATA"); if (!base || !base[0]) { return 0; } snprintf(r_path, GPU_FONT_PATH, "%s/bui", base); #elif defined(__APPLE__) const char *home = getenv("HOME"); if (!home || !home[0]) { return 0; } snprintf(r_path, GPU_FONT_PATH, "%s/Library/Caches/bui", home); #else const char *xdg = getenv("XDG_CACHE_HOME"), *home = getenv("HOME"); if (xdg && xdg[0]) { snprintf(r_path, GPU_FONT_PATH, "%s/bui", xdg); } else if (home && home[0]) { char cache[GPU_FONT_PATH]; snprintf(cache, sizeof(cache), "%s/.cache", home); make_dir(cache); snprintf(r_path, GPU_FONT_PATH, "%s/bui", cache); } else { return 0; } #endif make_dir(r_path); const size_t n = strlen(r_path); snprintf(r_path + n, GPU_FONT_PATH - n, "/font-index.txt"); return 1; } /* The saved index, when it was made from these folders and font files as they are now. * Lines: the magic, "D " per font folder, "S " per folder * read below them, then per font file "P " followed by its records, * "F