From 1fb3d2058a3687057698c4aac3d34b8f910cc606 Mon Sep 17 00:00:00 2001 From: scawful Date: Fri, 24 Nov 2023 13:34:00 -0500 Subject: [PATCH] macOS load system fonts objective-c++ --- src/app/core/platform/font_loader.h | 8 ++++++ src/app/core/platform/font_loader.mm | 39 ++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 src/app/core/platform/font_loader.h create mode 100644 src/app/core/platform/font_loader.mm diff --git a/src/app/core/platform/font_loader.h b/src/app/core/platform/font_loader.h new file mode 100644 index 00000000..31856826 --- /dev/null +++ b/src/app/core/platform/font_loader.h @@ -0,0 +1,8 @@ +// FontLoader.h +#ifndef FONTLOADER_H +#define FONTLOADER_H + +// Function declaration for loading system fonts into ImGui +void LoadSystemFonts(); + +#endif // FONTLOADER_H diff --git a/src/app/core/platform/font_loader.mm b/src/app/core/platform/font_loader.mm new file mode 100644 index 00000000..83511a3f --- /dev/null +++ b/src/app/core/platform/font_loader.mm @@ -0,0 +1,39 @@ +// FontLoader.mm +#include "app/core/platform/font_loader.h" +#import +#import +#include + +void LoadSystemFonts() { + // List of common macOS system fonts + NSArray *fontNames = @[ @"Helvetica", @"Times New Roman", @"Courier", @"Arial", @"Verdana" ]; + + for (NSString *fontName in fontNames) { + NSFont *font = [NSFont fontWithName:fontName size:14.0]; + if (!font) { + NSLog(@"Font not found: %@", fontName); + continue; + } + + CTFontDescriptorRef fontDescriptor = + CTFontDescriptorCreateWithNameAndSize((CFStringRef)font.fontName, font.pointSize); + CFURLRef fontURL = (CFURLRef)CTFontDescriptorCopyAttribute(fontDescriptor, kCTFontURLAttribute); + NSString *fontPath = [(NSURL *)fontURL path]; + CFRelease(fontDescriptor); + + if (fontPath != nil && [[NSFileManager defaultManager] isReadableFileAtPath:fontPath]) { + // Load the font into ImGui + ImGuiIO &io = ImGui::GetIO(); + ImFont *imFont = io.Fonts->AddFontFromFileTTF([fontPath UTF8String], 14.0f); + if (!imFont) { + NSLog(@"Failed to load font: %@", fontPath); + } + } else { + NSLog(@"Font file not accessible: %@", fontPath); + } + + if (fontURL) { + CFRelease(fontURL); + } + } +} \ No newline at end of file