From a85ddda2a7d50bbe9194e7631a0393a8ba6bc81c Mon Sep 17 00:00:00 2001 From: Allen Webster Date: Mon, 20 Nov 2017 15:05:14 -0500 Subject: [PATCH] Mac Italics and Bold --- platform_mac/mac_4ed.cpp | 31 +++++++++- platform_mac/mac_4ed.m | 64 +++++++++++++++++---- platform_mac/osx_objective_c_to_cpp_links.h | 8 +++ 3 files changed, 92 insertions(+), 11 deletions(-) diff --git a/platform_mac/mac_4ed.cpp b/platform_mac/mac_4ed.cpp index 5d45916d..151020bf 100644 --- a/platform_mac/mac_4ed.cpp +++ b/platform_mac/mac_4ed.cpp @@ -338,7 +338,36 @@ global u32 system_font_method = SystemFontMethod_FilePath; internal Sys_Font_Path(name, parameters){ - // TODO(allen) + b32 italic = (parameters != 0 && parameters->italics); + b32 bold = (parameters != 0 && parameters->bold); + i32 pt_size = 12; + if (parameters != 0){ + pt_size = parameters->pt_size; + } + + OSX_Font_Match match = osx_get_font_match(name, pt_size, italic, bold); + + Font_Path path = {0}; + if (match.path != 0){ + Partition *part = &shared_vars.font_scratch; + path.temp = begin_temp_memory(part); + + i32 len = str_size(match.path); + char *buffer = push_array(part, char, len + 1); + if (buffer == 0){ + sysshared_partition_grow(part, l_round_up_i32(len + 1, KB(4))); + buffer = push_array(part, char, len + 1); + } + + if (buffer != 0){ + push_align(part, 8); + memcpy(buffer, match.path, len + 1); + path.len = len; + path.name = buffer; + } + } + + return(path); } Sys_Font_Data_Not_Used; diff --git a/platform_mac/mac_4ed.m b/platform_mac/mac_4ed.m index 3f3f02d6..7ee6f6ed 100644 --- a/platform_mac/mac_4ed.m +++ b/platform_mac/mac_4ed.m @@ -726,6 +726,8 @@ osx_timer_seconds(void){ return(result); } +NSFontManager *font_manager = 0; + NSString *get_font_path(NSFont *font){ CFStringRef name = (CFStringRef)[font fontName]; CGFloat size = [font pointSize]; @@ -735,9 +737,60 @@ NSString *get_font_path(NSFont *font){ return(path); } +OSX_Font_Match +osx_get_font_match(char *name, i32 pt_size, b32 italic, b32 bold){ + if (font_manager == 0){ + font_manager = [NSFontManager sharedFontManager]; + } + + NSString *name_string = [NSString stringWithUTF8String:name]; + NSFontTraitMask trait_mask = 0; + if (italic){ + trait_mask = (trait_mask | NSItalicFontMask); + } + NSInteger weight = 5; + if (bold){ + weight = 9; + } + + b32 used_base_file = false; + NSFont *font = [font_manager + fontWithFamily: name_string + traits: trait_mask + weight: weight + size:(float)pt_size]; + + if (font == nil){ + font = [font_manager + fontWithFamily: name_string + traits: 0 + weight: 5 + size:(float)pt_size]; + used_base_file = true; + } + + OSX_Font_Match match = {0}; + if (font != nil){ + NSString *path = get_font_path(font); + char *path_c = 0; + if (path != nil){ + path_c = (char*)[path UTF8String]; + } + if (path_c != 0){ + match.path = path_c; + match.used_base_file = used_base_file; + } + } + + return(match); +} + OSX_Loadable_Fonts osx_list_loadable_fonts(void){ - NSFontManager *font_manager = [NSFontManager sharedFontManager]; + if (font_manager == 0){ + font_manager = [NSFontManager sharedFontManager]; + } + NSArray *fonts = [font_manager availableFontFamilies]; OSX_Loadable_Fonts result = {0}; @@ -768,15 +821,6 @@ osx_list_loadable_fonts(void){ result.paths[i] = path_c; } -#if 0 - for (int i = 0; i < count; ++i){ - char *name = result.names[i]; - char *path = result.paths[i]; - fprintf(stdout, "found: %s\nat: %s\n", name, path); - fflush(stdout); - } -#endif - return(result); } diff --git a/platform_mac/osx_objective_c_to_cpp_links.h b/platform_mac/osx_objective_c_to_cpp_links.h index 31f8069f..92f9f5f9 100644 --- a/platform_mac/osx_objective_c_to_cpp_links.h +++ b/platform_mac/osx_objective_c_to_cpp_links.h @@ -62,6 +62,11 @@ typedef struct OSX_Loadable_Fonts{ i32 count; } OSX_Loadable_Fonts; +typedef struct OSX_Font_Match{ + char *path; + b32 used_base_file; +} OSX_Font_Match; + // In C++ layer. extern OSX_Objective_C_Vars osx_objc; @@ -135,6 +140,9 @@ osx_close_app(void); external f32 osx_timer_seconds(void); +external OSX_Font_Match +osx_get_font_match(char *name, i32 pt_size, b32 italic, b32 bold); + external OSX_Loadable_Fonts osx_list_loadable_fonts(void);