accept font names, as well as font paths

This commit is contained in:
Ryan Fleury
2025-04-26 21:41:27 -07:00
parent 693ba7d077
commit 66187931aa
6 changed files with 78 additions and 26 deletions
+13 -7
View File
@@ -141,16 +141,22 @@ fnt_tag_from_path(String8 path)
}
//- rjf: allocate & push new node if we don't have an existing one
FNT_FontHashNode *new_node = 0;
if(existing_node == 0)
{
FP_Handle handle = fp_font_open(path);
FNT_FontHashSlot *slot = &fnt_state->font_hash_table[slot_idx];
new_node = push_array(fnt_state->permanent_arena, FNT_FontHashNode, 1);
new_node->tag = result;
new_node->handle = fp_font_open(path);
new_node->metrics = fp_metrics_from_font(new_node->handle);
new_node->path = push_str8_copy(fnt_state->permanent_arena, path);
SLLQueuePush_N(slot->first, slot->last, new_node, hash_next);
existing_node = push_array(fnt_state->permanent_arena, FNT_FontHashNode, 1);
existing_node->tag = result;
existing_node->handle = handle;
existing_node->metrics = fp_metrics_from_font(existing_node->handle);
existing_node->path = push_str8_copy(fnt_state->permanent_arena, path);
SLLQueuePush_N(slot->first, slot->last, existing_node, hash_next);
}
//- rjf: tag result must be zero if this is not a valid font
if(fp_handle_match(existing_node->handle, fp_handle_zero()))
{
MemoryZeroStruct(&result);
}
//- rjf: return
@@ -318,18 +318,64 @@ fp_font_open(String8 path)
{
ProfBeginFunction();
Temp scratch = scratch_begin(0, 0);
String16 path16 = str16_from_8(scratch.arena, path);
FP_DWrite_Font font = {0};
HRESULT error = 0;
//- rjf: open font file reference
error = IDWriteFactory_CreateFontFileReference(fp_dwrite_state->factory, (WCHAR *)path16.str, 0, &font.file);
//- rjf: build initial path task
typedef struct PathTask PathTask;
struct PathTask
{
PathTask *next;
String8 path;
};
PathTask start_task = {0, path};
PathTask *first_task = &start_task;
PathTask *last_task = first_task;
//- rjf: open font face
error = IDWriteFactory_CreateFontFace(fp_dwrite_state->factory, DWRITE_FONT_FACE_TYPE_TRUETYPE, 1, &font.file, 0, DWRITE_FONT_SIMULATIONS_NONE, &font.face);
//- rjf: try to open font
for(PathTask *t = first_task; t != 0 && font.file == 0; t = t->next)
{
String16 path16 = str16_from_8(scratch.arena, t->path);
error = IDWriteFactory_CreateFontFileReference(fp_dwrite_state->factory, (WCHAR *)path16.str, 0, &font.file);
error = IDWriteFactory_CreateFontFace(fp_dwrite_state->factory, DWRITE_FONT_FACE_TYPE_TRUETYPE, 1, &font.file, 0, DWRITE_FONT_SIMULATIONS_NONE, &font.face);
// rjf: failure trying just the normal path? -> generate new tasks that search in system folders
if(t == first_task && font.file == 0)
{
// rjf: generate task for user-installed fonts
{
HKEY reg_key = 0;
LSTATUS status = 0;
char name[256] = {0};
char data[256] = {0};
DWORD name_size = sizeof(name);
DWORD data_size = sizeof(data);
DWORD type = 0;
status = RegOpenKeyExW(HKEY_CURRENT_USER, L"Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders\\Fonts", 0, KEY_QUERY_VALUE, &reg_key);
status = RegEnumValueA(reg_key, 0, name, &name_size, 0, &type, data, &data_size);
String8 user_fonts_path = str8_cstring(data);
PathTask *task = push_array(scratch.arena, PathTask, 1);
task->path = push_str8f(scratch.arena, "%s/%S", user_fonts_path, path);
SLLQueuePush(first_task, last_task, task);
}
// rjf: generate task for windows directory (C:/Windows/Fonts, generally)
{
char windows_path[256] = {0};
GetWindowsDirectoryA(windows_path, sizeof(windows_path));
PathTask *task = push_array(scratch.arena, PathTask, 1);
task->path = push_str8f(scratch.arena, "%s/Fonts/%S", windows_path, path);
SLLQueuePush(first_task, last_task, task);
}
}
}
//- rjf: handlify & return
FP_Handle handle = fp_dwrite_handle_from_font(font);
FP_Handle handle = {0};
if(font.file != 0)
{
handle = fp_dwrite_handle_from_font(font);
}
scratch_end(scratch);
ProfEnd();
return handle;
File diff suppressed because one or more lines are too long
+4 -2
View File
@@ -225,8 +225,10 @@ RD_VocabTable:
@default(1) 'scrolling_animations': bool,
//- rjf: fonts
'main_font': string,
'code_font': string,
@display_name('UI Font') @description("The name or path to the font used when displaying non-code UI elements.")
'main_font': string,
@display_name('Code Font') @description("The name or path to the font used when displaying code.")
'code_font': string,
//- rjf: colors
@display_name('Color Preset')
+5 -6
View File
@@ -10394,7 +10394,11 @@ rd_font_from_slot(RD_FontSlot slot)
// rjf: map name -> tag
FNT_Tag result = {0};
if(font_name.size == 0)
if(font_name.size != 0)
{
result = fnt_tag_from_path(font_name);
}
if(font_name.size == 0 || fnt_tag_match(fnt_tag_zero(), result))
{
switch(slot)
{
@@ -10404,11 +10408,6 @@ rd_font_from_slot(RD_FontSlot slot)
case RD_FontSlot_Icons:{result = fnt_tag_from_static_data_string(&rd_icon_font_bytes);}break;
}
}
else
{
// TODO(rjf): need to handle "system font names" here.
result = fnt_tag_from_path(font_name);
}
return result;
}
+3 -4
View File
@@ -207,10 +207,6 @@
////////////////////////////////
//~ rjf: Hot, Medium Priority Tasks (Low-Hanging-Fruit Features, UI Jank, Cleanup)
//
// [ ] Setting the code_font/main_font values to a font name doesn't work.
// Should probably make note that you have to set it to a path to a TTF,
// since that's not normally how Windows fonts work.
//
// [ ] "root" concept in hash store, which buckets keys & allows usage code to
// jettison a collection of keys in retained mode fashion
//
@@ -422,6 +418,9 @@
// size dynamically
// [x] automatically start search query with selected text
// [x] @feature processor/data breakpoints
// [x] Setting the code_font/main_font values to a font name doesn't work.
// Should probably make note that you have to set it to a path to a TTF,
// since that's not normally how Windows fonts work.
////////////////////////////////
//~ rjf: Build Options