Add font loading functionality and refactor font handling in core

This commit is contained in:
scawful
2024-11-15 21:21:13 -05:00
parent 8490625078
commit 876a83f61c
6 changed files with 188 additions and 183 deletions

View File

@@ -16,7 +16,6 @@
#include "imgui/backends/imgui_impl_sdl2.h"
#include "imgui/backends/imgui_impl_sdlrenderer2.h"
#include "imgui/imgui.h"
#include "imgui/imgui_internal.h"
namespace yaze {
namespace app {
@@ -180,7 +179,7 @@ absl::Status Controller::OnEntry(std::string filename) {
#else
platform_ = Platform::kUnknown;
#endif
RETURN_IF_ERROR(CreateSDL_Window())
RETURN_IF_ERROR(CreateWindow())
RETURN_IF_ERROR(CreateRenderer())
RETURN_IF_ERROR(CreateGuiContext())
RETURN_IF_ERROR(LoadAudioDevice())
@@ -264,7 +263,7 @@ void Controller::OnExit() {
SDL_Quit();
}
absl::Status Controller::CreateSDL_Window() {
absl::Status Controller::CreateWindow() {
auto sdl_flags = SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER;
if (flags()->kUseNewImGuiInput) {
sdl_flags |= SDL_INIT_GAMECONTROLLER;
@@ -273,25 +272,26 @@ absl::Status Controller::CreateSDL_Window() {
if (SDL_Init(sdl_flags) != 0) {
return absl::InternalError(
absl::StrFormat("SDL_Init: %s\n", SDL_GetError()));
} else {
SDL_DisplayMode displayMode;
SDL_GetCurrentDisplayMode(0, &displayMode);
int screenWidth = displayMode.w * 0.8;
int screenHeight = displayMode.h * 0.8;
window_ = std::unique_ptr<SDL_Window, core::SDL_Deleter>(
SDL_CreateWindow("Yet Another Zelda3 Editor", // window title
SDL_WINDOWPOS_UNDEFINED, // initial x position
SDL_WINDOWPOS_UNDEFINED, // initial y position
screenWidth, // width, in pixels
screenHeight, // height, in pixels
SDL_WINDOW_RESIZABLE),
core::SDL_Deleter());
if (window_ == nullptr) {
return absl::InternalError(
absl::StrFormat("SDL_CreateWindow: %s\n", SDL_GetError()));
}
}
SDL_DisplayMode display_mode;
SDL_GetCurrentDisplayMode(0, &display_mode);
int screen_width = display_mode.w * 0.8;
int screen_height = display_mode.h * 0.8;
window_ = std::unique_ptr<SDL_Window, core::SDL_Deleter>(
SDL_CreateWindow("Yet Another Zelda3 Editor", // window title
SDL_WINDOWPOS_UNDEFINED, // initial x position
SDL_WINDOWPOS_UNDEFINED, // initial y position
screen_width, // width, in pixels
screen_height, // height, in pixels
SDL_WINDOW_RESIZABLE),
core::SDL_Deleter());
if (window_ == nullptr) {
return absl::InternalError(
absl::StrFormat("SDL_CreateWindow: %s\n", SDL_GetError()));
}
return absl::OkStatus();
}
@@ -305,22 +305,17 @@ absl::Status Controller::CreateGuiContext() {
ImGuiIO &io = ImGui::GetIO();
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;
if (flags()->kUseNewImGuiInput) {
io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad;
}
// Initialize ImGui for SDL
// Initialize ImGui based on the backend
ImGui_ImplSDL2_InitForSDLRenderer(window_.get(),
Renderer::GetInstance().renderer());
ImGui_ImplSDLRenderer2_Init(Renderer::GetInstance().renderer());
// Check if the assets/fonts directory exists in our CWD
// Otherwise, load the system fonts.
// const auto assets_path = std::filesystem::path("assets");
// if (std::filesystem::is_directory(assets_path)) {
RETURN_IF_ERROR(LoadFontFamilies());
// } else {
// #ifdef __APPLE__
// LoadSystemFonts();
// #else
@@ -328,7 +323,6 @@ absl::Status Controller::CreateGuiContext() {
// "Could not find assets/fonts directory in the current working "
// "directory");
// #endif
//}
// Set the default style
gui::ColorsYaze();
@@ -340,6 +334,32 @@ absl::Status Controller::CreateGuiContext() {
return absl::OkStatus();
}
absl::Status Controller::LoadFontFamilies() const {
// LoadSystemFonts();
return LoadPackageFonts();
}
absl::Status Controller::LoadAudioDevice() {
SDL_AudioSpec want, have;
SDL_memset(&want, 0, sizeof(want));
want.freq = audio_frequency_;
want.format = AUDIO_S16;
want.channels = 2;
want.samples = 2048;
want.callback = NULL; // Uses the queue
audio_device_ = SDL_OpenAudioDevice(NULL, 0, &want, &have, 0);
if (audio_device_ == 0) {
return absl::InternalError(
absl::StrFormat("Failed to open audio: %s\n", SDL_GetError()));
}
// audio_buffer_ = new int16_t[audio_frequency_ / 50 * 4];
audio_buffer_ = std::make_shared<int16_t>(audio_frequency_ / 50 * 4);
SDL_PauseAudioDevice(audio_device_, 0);
editor_manager_.emulator().set_audio_buffer(audio_buffer_.get());
editor_manager_.emulator().set_audio_device_id(audio_device_);
return absl::OkStatus();
}
absl::Status Controller::LoadConfigFiles() {
// Create and load a dotfile for the application
// This will store the user's preferences and settings
@@ -368,124 +388,6 @@ absl::Status Controller::LoadConfigFiles() {
return absl::OkStatus();
}
absl::Status Controller::LoadFontFamilies() const {
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();
}
absl::Status Controller::LoadAudioDevice() {
SDL_AudioSpec want, have;
SDL_memset(&want, 0, sizeof(want));
want.freq = audio_frequency_;
want.format = AUDIO_S16;
want.channels = 2;
want.samples = 2048;
want.callback = NULL; // Uses the queue
audio_device_ = SDL_OpenAudioDevice(NULL, 0, &want, &have, 0);
if (audio_device_ == 0) {
return absl::InternalError(
absl::StrFormat("Failed to open audio: %s\n", SDL_GetError()));
}
// audio_buffer_ = new int16_t[audio_frequency_ / 50 * 4];
audio_buffer_ = std::make_shared<int16_t>(audio_frequency_ / 50 * 4);
SDL_PauseAudioDevice(audio_device_, 0);
editor_manager_.emulator().set_audio_buffer(audio_buffer_.get());
editor_manager_.emulator().set_audio_device_id(audio_device_);
return absl::OkStatus();
}
} // namespace core
} // namespace app
} // namespace yaze

View File

@@ -5,16 +5,15 @@
#include <memory>
#include "imgui/backends/imgui_impl_sdl2.h"
#include "imgui/backends/imgui_impl_sdlrenderer2.h"
#include "imgui/imconfig.h"
#include "imgui/imgui.h"
#include "imgui/imgui_internal.h"
#include "absl/status/status.h"
#include "app/core/platform/renderer.h"
#include "app/core/utils/file_util.h"
#include "app/editor/editor_manager.h"
#include "app/editor/utils/editor.h"
#include "imgui/backends/imgui_impl_sdl2.h"
#include "imgui/backends/imgui_impl_sdlrenderer2.h"
#include "imgui/imconfig.h"
#include "imgui/imgui.h"
int main(int argc, char **argv);
@@ -38,12 +37,12 @@ class Controller : public ExperimentFlags {
void DoRender() const;
void OnExit();
absl::Status CreateSDL_Window();
absl::Status CreateWindow();
absl::Status CreateRenderer();
absl::Status CreateGuiContext();
absl::Status LoadConfigFiles();
absl::Status LoadFontFamilies() const;
absl::Status LoadAudioDevice();
absl::Status LoadConfigFiles();
void SetupScreen(std::string filename = "") {
editor_manager_.SetupScreen(filename);

View File

@@ -19,6 +19,7 @@ if(APPLE)
list(APPEND YAZE_APP_CORE_SRC
app/core/platform/file_dialog.mm
app/core/platform/app_delegate.mm
app/core/platform/font_loader.cc
app/core/platform/font_loader.mm
app/core/platform/clipboard.mm
app/core/platform/file_path.mm

View File

@@ -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

View File

@@ -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

View File

@@ -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