Add font loading functionality and refactor font handling in core
This commit is contained in:
@@ -4,17 +4,120 @@
|
||||
#include <unordered_set>
|
||||
#include <vector>
|
||||
|
||||
#include "absl/status/status.h"
|
||||
#include "absl/strings/str_cat.h"
|
||||
#include "absl/strings/str_format.h"
|
||||
#include "app/core/platform/file_path.h"
|
||||
#include "app/gui/icons.h"
|
||||
#include "imgui/imgui.h"
|
||||
|
||||
namespace yaze {
|
||||
namespace app {
|
||||
namespace core {
|
||||
|
||||
absl::Status LoadPackageFonts() {
|
||||
ImGuiIO &io = ImGui::GetIO();
|
||||
|
||||
static const char *KARLA_REGULAR = "Karla-Regular.ttf";
|
||||
static const char *ROBOTO_MEDIUM = "Roboto-Medium.ttf";
|
||||
static const char *COUSINE_REGULAR = "Cousine-Regular.ttf";
|
||||
static const char *DROID_SANS = "DroidSans.ttf";
|
||||
static const char *NOTO_SANS_JP = "NotoSansJP.ttf";
|
||||
static const char *IBM_PLEX_JP = "IBMPlexSansJP-Bold.ttf";
|
||||
static const float FONT_SIZE_DEFAULT = 16.0f;
|
||||
static const float FONT_SIZE_DROID_SANS = 18.0f;
|
||||
static const float ICON_FONT_SIZE = 18.0f;
|
||||
|
||||
// Icon configuration
|
||||
static const ImWchar icons_ranges[] = {ICON_MIN_MD, 0xf900, 0};
|
||||
ImFontConfig icons_config;
|
||||
icons_config.MergeMode = true;
|
||||
icons_config.GlyphOffset.y = 5.0f;
|
||||
icons_config.GlyphMinAdvanceX = 13.0f;
|
||||
icons_config.PixelSnapH = true;
|
||||
|
||||
// Japanese font configuration
|
||||
ImFontConfig japanese_font_config;
|
||||
japanese_font_config.MergeMode = true;
|
||||
icons_config.GlyphOffset.y = 5.0f;
|
||||
icons_config.GlyphMinAdvanceX = 13.0f;
|
||||
icons_config.PixelSnapH = true;
|
||||
|
||||
// List of fonts to be loaded
|
||||
std::vector<const char *> font_paths = {
|
||||
KARLA_REGULAR, ROBOTO_MEDIUM, COUSINE_REGULAR, IBM_PLEX_JP, DROID_SANS};
|
||||
|
||||
// Load fonts with associated icon and Japanese merges
|
||||
for (const auto &font_path : font_paths) {
|
||||
float font_size =
|
||||
(font_path == DROID_SANS) ? FONT_SIZE_DROID_SANS : FONT_SIZE_DEFAULT;
|
||||
|
||||
std::string actual_font_path;
|
||||
#ifdef __APPLE__
|
||||
#if TARGET_OS_IOS == 1
|
||||
const std::string kBundlePath = GetBundleResourcePath();
|
||||
actual_font_path = kBundlePath + font_path;
|
||||
#else
|
||||
actual_font_path = absl::StrCat(GetBundleResourcePath(),
|
||||
"Contents/Resources/font/", font_path);
|
||||
#endif
|
||||
#else
|
||||
actual_font_path = std::filesystem::absolute(font_path).string();
|
||||
#endif
|
||||
|
||||
if (!io.Fonts->AddFontFromFileTTF(actual_font_path.data(), font_size)) {
|
||||
return absl::InternalError(
|
||||
absl::StrFormat("Failed to load font from %s", actual_font_path));
|
||||
}
|
||||
|
||||
// Merge icon set
|
||||
std::string actual_icon_font_path = "";
|
||||
const char *icon_font_path = FONT_ICON_FILE_NAME_MD;
|
||||
#if defined(__APPLE__) && defined(__MACH__)
|
||||
#if TARGET_OS_IOS == 1
|
||||
const std::string kIconBundlePath = GetBundleResourcePath();
|
||||
actual_icon_font_path = kIconBundlePath + "MaterialIcons-Regular.ttf";
|
||||
#else
|
||||
actual_icon_font_path =
|
||||
absl::StrCat(GetBundleResourcePath(),
|
||||
"Contents/Resources/font/MaterialIcons-Regular.ttf");
|
||||
#endif
|
||||
#else
|
||||
actual_icon_font_path = std::filesystem::absolute(icon_font_path).string();
|
||||
#endif
|
||||
io.Fonts->AddFontFromFileTTF(actual_icon_font_path.data(), ICON_FONT_SIZE,
|
||||
&icons_config, icons_ranges);
|
||||
|
||||
// Merge Japanese font
|
||||
std::string actual_japanese_font_path = "";
|
||||
const char *japanese_font_path = NOTO_SANS_JP;
|
||||
#if defined(__APPLE__) && defined(__MACH__)
|
||||
#if TARGET_OS_IOS == 1
|
||||
const std::string kJapaneseBundlePath = GetBundleResourcePath();
|
||||
actual_japanese_font_path = kJapaneseBundlePath + japanese_font_path;
|
||||
#else
|
||||
actual_japanese_font_path =
|
||||
absl::StrCat(GetBundleResourcePath(), "Contents/Resources/font/",
|
||||
japanese_font_path);
|
||||
#endif
|
||||
#else
|
||||
actual_japanese_font_path =
|
||||
std::filesystem::absolute(japanese_font_path).string();
|
||||
#endif
|
||||
io.Fonts->AddFontFromFileTTF(actual_japanese_font_path.data(), 18.0f,
|
||||
&japanese_font_config,
|
||||
io.Fonts->GetGlyphRangesJapanese());
|
||||
}
|
||||
return absl::OkStatus();
|
||||
}
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <Windows.h>
|
||||
|
||||
int CALLBACK EnumFontFamExProc(const LOGFONT* lpelfe, const TEXTMETRIC* lpntme,
|
||||
int CALLBACK EnumFontFamExProc(const LOGFONT *lpelfe, const TEXTMETRIC *lpntme,
|
||||
DWORD FontType, LPARAM lParam) {
|
||||
// Step 3: Load the font into ImGui
|
||||
ImGuiIO& io = ImGui::GetIO();
|
||||
ImGuiIO &io = ImGui::GetIO();
|
||||
io.Fonts->AddFontFromFileTTF(lpelfe->lfFaceName, 16.0f);
|
||||
|
||||
return 1;
|
||||
@@ -37,8 +140,8 @@ void LoadSystemFonts() {
|
||||
RegQueryInfoKey(hKey, NULL, NULL, NULL, NULL, NULL, NULL, &valueCount,
|
||||
&maxValueNameSize, &maxValueDataSize, NULL, NULL);
|
||||
|
||||
char* valueName = new char[maxValueNameSize + 1]; // +1 for null terminator
|
||||
BYTE* valueData = new BYTE[maxValueDataSize + 1]; // +1 for null terminator
|
||||
char *valueName = new char[maxValueNameSize + 1]; // +1 for null terminator
|
||||
BYTE *valueData = new BYTE[maxValueDataSize + 1]; // +1 for null terminator
|
||||
|
||||
// Enumerate all font entries
|
||||
for (DWORD i = 0; i < valueCount; i++) {
|
||||
@@ -55,7 +158,7 @@ void LoadSystemFonts() {
|
||||
valueData, &valueDataSize) == ERROR_SUCCESS) {
|
||||
if (valueType == REG_SZ) {
|
||||
// Add the font file path to the vector
|
||||
std::string fontPath(reinterpret_cast<char*>(valueData),
|
||||
std::string fontPath(reinterpret_cast<char *>(valueData),
|
||||
valueDataSize);
|
||||
|
||||
fontPaths.push_back(fontPath);
|
||||
@@ -69,7 +172,7 @@ void LoadSystemFonts() {
|
||||
RegCloseKey(hKey);
|
||||
}
|
||||
|
||||
ImGuiIO& io = ImGui::GetIO();
|
||||
ImGuiIO &io = ImGui::GetIO();
|
||||
|
||||
// List of common font face names
|
||||
static const std::unordered_set<std::string> commonFontFaceNames = {
|
||||
@@ -88,7 +191,7 @@ void LoadSystemFonts() {
|
||||
"Tahoma",
|
||||
"Lucida Console"};
|
||||
|
||||
for (auto& fontPath : fontPaths) {
|
||||
for (auto &fontPath : fontPaths) {
|
||||
// Check if the font path has a "C:\" prefix
|
||||
if (fontPath.substr(0, 2) != "C:") {
|
||||
// Add "C:\Windows\Fonts\" prefix to the font path
|
||||
@@ -131,3 +234,7 @@ void LoadSystemFonts() {
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
} // namespace core
|
||||
} // namespace app
|
||||
} // namespace yaze
|
||||
@@ -1,6 +1,17 @@
|
||||
#ifndef YAZE_APP_CORE_PLATFORM_FONTLOADER_H
|
||||
#define YAZE_APP_CORE_PLATFORM_FONTLOADER_H
|
||||
|
||||
void LoadSystemFonts();
|
||||
#include "absl/status/status.h"
|
||||
|
||||
#endif // YAZE_APP_CORE_PLATFORM_FONTLOADER_H
|
||||
namespace yaze {
|
||||
namespace app {
|
||||
namespace core {
|
||||
|
||||
void LoadSystemFonts();
|
||||
absl::Status LoadPackageFonts();
|
||||
|
||||
} // namespace core
|
||||
} // namespace app
|
||||
} // namespace yaze
|
||||
|
||||
#endif // YAZE_APP_CORE_PLATFORM_FONTLOADER_H
|
||||
|
||||
@@ -1,32 +1,20 @@
|
||||
// FontLoader.mm
|
||||
#include "app/core/platform/font_loader.h"
|
||||
|
||||
#include "imgui/imgui.h"
|
||||
|
||||
#include "app/gui/icons.h"
|
||||
|
||||
#if defined(__APPLE__) && defined(__MACH__)
|
||||
/* Apple OSX and iOS (Darwin). */
|
||||
#import <CoreText/CoreText.h>
|
||||
#include <TargetConditionals.h>
|
||||
|
||||
#import <CoreText/CoreText.h>
|
||||
#include "app/gui/icons.h"
|
||||
#include "imgui/imgui.h"
|
||||
|
||||
#if TARGET_IPHONE_SIMULATOR == 1
|
||||
/* iOS in Xcode simulator */
|
||||
void LoadSystemFonts() {}
|
||||
|
||||
#elif TARGET_OS_IPHONE == 1
|
||||
#if TARGET_OS_IPHONE == 1 || TARGET_IPHONE_SIMULATOR == 1
|
||||
/* iOS */
|
||||
void LoadSystemFonts() {}
|
||||
void yaze::app::core::LoadSystemFonts() {}
|
||||
|
||||
#elif TARGET_OS_MAC == 1
|
||||
/* macOS */
|
||||
|
||||
#import <Cocoa/Cocoa.h>
|
||||
|
||||
// MacOS Implementation
|
||||
void LoadSystemFonts() {
|
||||
// List of common macOS system fonts
|
||||
void yaze::app::core::LoadSystemFonts() {
|
||||
NSArray *fontNames = @[ @"Helvetica", @"Times New Roman", @"Courier", @"Arial", @"Verdana" ];
|
||||
|
||||
for (NSString *fontName in fontNames) {
|
||||
@@ -67,8 +55,5 @@ void LoadSystemFonts() {
|
||||
}
|
||||
}
|
||||
}
|
||||
#else
|
||||
// Unsupported platform
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user