From 9d4e6d2f28ad31750fcdecc63ce278d8d626f6cf Mon Sep 17 00:00:00 2001 From: Dean Galvin Date: Sat, 3 Oct 2026 21:29:28 +1300 Subject: [PATCH] feat(font): open system font sets from a font directory sceFontOpenFontSet failed for every set type, so PPSA21564 drew no text in system fonts (the title screen copyright line, credits, button guides). It now loads the set's primary font file from ANYPS5_SYSTEM_FONTS, or anyps5-fonts beside the executable: console files under their own names first, then openly licensed substitutes (Noto Sans, Noto Sans Mono, Noto Sans Thai, Noto Sans CJK). The PS5-only JG2 set types are accepted. System font handles use the system font context. Their faces are loaded by FreeType in host memory: about 1 MiB per CJK face does not fit the font memory the title sizes for the console's fonts, and sceFontCreateRenderer then failed with ALLOCATION_FAILED. Handles of the same font file share one copy of its bytes. --- core/libs/prx/libSceFont/CMakeLists.txt | 7 +- core/libs/prx/libSceFont/FontLibrary.cmake | 1 + .../prx/libSceFont/include/FontDriver.hpp | 1 + .../prx/libSceFont/include/FontInternal.hpp | 18 +- core/libs/prx/libSceFont/src/Layout.cpp | 10 +- core/libs/prx/libSceFont/src/Open.cpp | 87 +++---- core/libs/prx/libSceFont/src/State.cpp | 12 +- core/libs/prx/libSceFont/src/SystemFont.cpp | 131 +++++++++++ core/libs/prx/libSceFontFt/src/Driver.cpp | 56 ++++- core/libs/tests/GuestSystemFont.cpp | 218 ++++++++++++++++++ docs/dev/TechnicalDebt.md | 1 + docs/user/USAGE.md | 4 + 12 files changed, 492 insertions(+), 54 deletions(-) create mode 100644 core/libs/prx/libSceFont/src/SystemFont.cpp create mode 100644 core/libs/tests/GuestSystemFont.cpp diff --git a/core/libs/prx/libSceFont/CMakeLists.txt b/core/libs/prx/libSceFont/CMakeLists.txt index 3ae1d162b..8866b00b4 100644 --- a/core/libs/prx/libSceFont/CMakeLists.txt +++ b/core/libs/prx/libSceFont/CMakeLists.txt @@ -12,8 +12,13 @@ if(BUILD_TESTING) target_include_directories(guest_font_tests PRIVATE ${LIBS_INCLUDE_DIR}) target_link_libraries(guest_font_tests PRIVATE libSceFont libSceFontFt libc) add_test(NAME guest_font COMMAND guest_font_tests) + add_test_executable(guest_system_font_tests ${LIBS_INCLUDE_DIR}/tests/GuestSystemFont.cpp) + target_include_directories(guest_system_font_tests PRIVATE ${LIBS_INCLUDE_DIR}) + target_link_libraries(guest_system_font_tests PRIVATE libSceFont libSceFontFt libc) + configure_windows_unwind(guest_system_font_tests) + add_test(NAME guest_system_font COMMAND guest_system_font_tests) if(WIN32) - set_tests_properties(guest_font PROPERTIES ENVIRONMENT_MODIFICATION + set_tests_properties(guest_font guest_system_font PROPERTIES ENVIRONMENT_MODIFICATION "PATH=path_list_prepend:$") endif() endif() diff --git a/core/libs/prx/libSceFont/FontLibrary.cmake b/core/libs/prx/libSceFont/FontLibrary.cmake index 26e287f39..d8ff9e0d3 100644 --- a/core/libs/prx/libSceFont/FontLibrary.cmake +++ b/core/libs/prx/libSceFont/FontLibrary.cmake @@ -7,6 +7,7 @@ function(add_sce_font_library target) ${fontDir}/src/Render.cpp ${fontDir}/src/State.cpp ${fontDir}/src/Style.cpp + ${fontDir}/src/SystemFont.cpp ${fontDir}/src/Text.cpp ${fontDir}/src/Unimplemented.cpp ) diff --git a/core/libs/prx/libSceFont/include/FontDriver.hpp b/core/libs/prx/libSceFont/include/FontDriver.hpp index e58ae256e..889c367dd 100644 --- a/core/libs/prx/libSceFont/include/FontDriver.hpp +++ b/core/libs/prx/libSceFont/include/FontDriver.hpp @@ -23,6 +23,7 @@ constexpr std::uint16_t STYLE_FRAME_MAGIC = 0x0F09; constexpr std::uint32_t LOCK_BIT = 0x80000000u; constexpr std::uint32_t OPEN_BIT = 0x40000000u; constexpr std::uint32_t COUNT_MASK = 0x0FFFFFFFu; +constexpr std::uint64_t SYSFONT_FLAG_SYSTEM_SET = 0x100u; constexpr std::size_t HORIZONTAL_LAYOUT_SIZE = 0x30; constexpr std::size_t HORIZONTAL_LINE_ADVANCE = 0x00; diff --git a/core/libs/prx/libSceFont/include/FontInternal.hpp b/core/libs/prx/libSceFont/include/FontInternal.hpp index 426a21bee..b65648a0d 100644 --- a/core/libs/prx/libSceFont/include/FontInternal.hpp +++ b/core/libs/prx/libSceFont/include/FontInternal.hpp @@ -6,6 +6,9 @@ #include #include +#include +#include +#include #include #include @@ -21,7 +24,7 @@ constexpr std::uint8_t STYLE_FRAME_FLAG_WEIGHT = 0x04; constexpr float POINTS_PER_INCH = 72.0f; struct FontState { - std::vector faceData; + std::shared_ptr> faceData; FT_Face face = nullptr; float scaleW = 16.0f; float scaleH = 16.0f; @@ -48,6 +51,11 @@ struct GeneratedGlyph { bool outlineInitialized = false; }; +struct SystemFontFile { + std::filesystem::path path; + std::uint32_t subFontIndex = 0; +}; + struct RenderSurfaceSystemUse { FontStyleFrame* styleframe; float catchedScale; @@ -70,12 +78,18 @@ void** EntryObjectSlot(FontCtxEntry* entry, std::uint32_t modeLow); FontCtxEntry* AcquireFontCtxEntry(void* ctx, std::uint32_t index, std::uint32_t modeLow, FontObj** outObject, std::uint32_t* outLockWord); void ReleaseFontCtxEntryLock(FontCtxEntry* entry, std::uint32_t modeLow, std::uint32_t lockWord); FontObj* FindSubFont(FontObj* head, std::uint32_t subFontIndex); +void* FontContext(const FontLibNative* library, const FontHandleNative* font); void ReleaseFontObjectsForHandle(FontHandleNative* font); FontState* TryGetState(FontHandle handle); FontState& ResetState(FontHandle handle); void RemoveState(FontHandle handle); -void LoadStateFace(FontState& state, std::vector bytes, std::uint32_t subFontIndex); +void LoadStateFace(FontState& state, std::shared_ptr> bytes, std::uint32_t subFontIndex); + +bool IsSystemFontSet(std::uint32_t fontSetType); +std::vector SystemFontCandidates(std::uint32_t fontSetType); +std::filesystem::path SystemFontDirectory(); +std::optional FindSystemFontFile(std::uint32_t fontSetType); void TrackGeneratedGlyph(FontGlyph glyph); bool ForgetGeneratedGlyph(FontGlyph glyph); diff --git a/core/libs/prx/libSceFont/src/Layout.cpp b/core/libs/prx/libSceFont/src/Layout.cpp index 7b76c46d0..68d242d53 100644 --- a/core/libs/prx/libSceFont/src/Layout.cpp +++ b/core/libs/prx/libSceFont/src/Layout.cpp @@ -212,7 +212,7 @@ int Font::ComputeHorizontalLayout(FontHandle handle, const StyleStateBlock* styl auto* font = GetNativeFont(handle); if (!font) return fail(); auto* library = static_cast(font->library); - if (!library || !library->external_fonts_ctx) return fail(); + if (!library || !FontContext(library, font)) return fail(); const SysDriver* driver = library->sys_driver; if (!driver || !driver->set_char_with_dpi || !driver->set_char_default_dpi || !driver->compute_layout || !style) return fail(); const std::uint32_t modeLow = font->flags & 0x0Fu; @@ -226,7 +226,7 @@ int Font::ComputeHorizontalLayout(FontHandle handle, const StyleStateBlock* styl if (static_cast(fontId) >= 0) { FontObj* head = nullptr; std::uint32_t lockWord = 0; - auto* entry = AcquireFontCtxEntry(library->external_fonts_ctx, fontId, modeLow, &head, &lockWord); + auto* entry = AcquireFontCtxEntry(FontContext(library, font), fontId, modeLow, &head, &lockWord); if (!entry) return fail(); FontObj* match = FindSubFont(head, font->open_info.sub_font_index); int rc = SCE_FONT_ERROR_FATAL; @@ -288,7 +288,7 @@ int Font::ComputeVerticalLayout(FontHandle handle, const StyleStateBlock* style, }; auto* font = GetNativeFont(handle); auto* library = font ? static_cast(font->library) : nullptr; - if (!library || !library->external_fonts_ctx || !library->sys_driver) return fail(SCE_FONT_ERROR_INVALID_FONT_HANDLE); + if (!library || !FontContext(library, font) || !library->sys_driver) return fail(SCE_FONT_ERROR_INVALID_FONT_HANDLE); const SysDriver* driver = library->sys_driver; const std::uint32_t modeLow = font->flags & 0x0Fu; const std::uint32_t fontId = font->open_info.ctx_entry_index; @@ -303,7 +303,7 @@ int Font::ComputeVerticalLayout(FontHandle handle, const StyleStateBlock* style, if (static_cast(fontId) >= 0) { FontObj* head = nullptr; std::uint32_t lockWord = 0; - auto* entry = AcquireFontCtxEntry(library->external_fonts_ctx, fontId, modeLow, &head, &lockWord); + auto* entry = AcquireFontCtxEntry(FontContext(library, font), fontId, modeLow, &head, &lockWord); FontObj* obj = FindSubFont(head, font->open_info.sub_font_index); if (!entry || !obj) { if (entry) ReleaseFontCtxEntryLock(entry, modeLow, lockWord); @@ -454,7 +454,7 @@ int Font::RenderCharGlyphImageCore(FontHandle handle, std::uint32_t code, FontRe const StyleStateBlock* style = ResolveSurfaceStyleState(&font->cached_style.state, surface, surfaceStyle); FontObj* obj = nullptr; std::uint32_t lockWord = 0; - auto* entry = AcquireFontCtxEntry(library->external_fonts_ctx, font->open_info.ctx_entry_index, modeLow, &obj, &lockWord); + auto* entry = AcquireFontCtxEntry(FontContext(library, font), font->open_info.ctx_entry_index, modeLow, &obj, &lockWord); obj = FindSubFont(obj, font->open_info.sub_font_index); if (!entry || !obj) { if (entry) ReleaseFontCtxEntryLock(entry, modeLow, lockWord); diff --git a/core/libs/prx/libSceFont/src/Open.cpp b/core/libs/prx/libSceFont/src/Open.cpp index b92f7ebf9..3ed2eaa05 100644 --- a/core/libs/prx/libSceFont/src/Open.cpp +++ b/core/libs/prx/libSceFont/src/Open.cpp @@ -1,13 +1,15 @@ // SPDX-FileCopyrightText: Copyright 2024 shadPS4 Emulator Project // SPDX-License-Identifier: GPL-2.0-or-later -#include -#include #include #include #include #include #include +#include +#include +#include +#include #include #include #include @@ -18,28 +20,11 @@ namespace { using namespace Font; -constexpr std::array SYSTEM_FONT_SET_TYPES = { - 0x18070043u, 0x18070044u, 0x18070045u, 0x18070047u, 0x18070053u, 0x18070054u, - 0x18070055u, 0x18070057u, 0x180700C3u, 0x180700C4u, 0x180700C5u, 0x180700C7u, - 0x18070444u, 0x18070447u, 0x18070454u, 0x18070457u, 0x180704C4u, 0x180704C7u, - 0x18071053u, 0x18071054u, 0x18071055u, 0x18071057u, 0x18071454u, 0x18071457u, - 0x18072444u, 0x18072447u, 0x180724C4u, 0x180724C7u, 0x18073454u, 0x18073457u, - 0x180734D4u, 0x180734D7u, 0x18078044u, 0x180780C4u, 0x18079054u, 0x1807A044u, - 0x1807A0C4u, 0x1807A444u, 0x1807A4C4u, 0x1807AC44u, 0x1807ACC4u, 0x1807B054u, - 0x1807B0D4u, 0x1807B454u, 0x1807B4D4u, 0x1807BC54u, 0x1807BCD4u, 0x18080444u, - 0x18080447u, 0x18080454u, 0x18080457u, 0x180804C4u, 0x180804C7u, 0x18081454u, - 0x18081457u, 0x18082444u, 0x18082447u, 0x180824C4u, 0x180824C7u, 0x18083454u, - 0x18083457u, 0x180834D4u, 0x180834D7u, 0x1808A444u, 0x1808A4C4u, 0x1808B454u, - 0x1808B4D4u, 0x180C8044u, 0x180C80C4u, 0x180C9054u, 0x180CA044u, 0x180CA0C4u, - 0x180CAC44u, 0x180CACC4u, 0x180CB054u, 0x180CB0D4u, 0x180CBC54u, 0x180CBCD4u, - 0x18170043u, 0x18170044u, 0x18170045u, 0x18170047u, 0x18170444u, 0x18170447u, - 0x18370044u, 0x18370047u, 0x18370444u, 0x18370447u, -}; - constexpr std::uint16_t HANDLE_OWNED = 0x10; struct OpenRequest { FontLibNative* library; + void* context; std::uint32_t modeLow; std::uint32_t driverMode; const void* source; @@ -111,7 +96,7 @@ void SetOpenFlags(FontLibNative* library, std::uint32_t modeLow) { if ((flags & secondBit) != 0) library->sysfont_flags |= 2; } -std::uint32_t ClaimExternalEntry(FontCtxHeader* header, const OpenRequest& request) { +std::uint32_t ClaimEntry(FontCtxHeader* header, const OpenRequest& request) { AcquireWordLock(header->lock_word); auto* entries = static_cast(header->base); std::uint32_t entryIndex = 0xFFFFFFFFu; @@ -140,7 +125,7 @@ std::uint32_t ClaimExternalEntry(FontCtxHeader* header, const OpenRequest& reque return entryIndex; } -int OpenExternalFont(const OpenRequest& request, std::uint32_t libraryLock, FontHandle* pFontHandle) { +int OpenFontInContext(const OpenRequest& request, std::uint32_t libraryLock, FontHandle* pFontHandle) { FontLibNative* library = request.library; FontHandle handle = PrepareHandle(library, *pFontHandle); if (!handle) { @@ -154,8 +139,8 @@ int OpenExternalFont(const OpenRequest& request, std::uint32_t libraryLock, Font *pFontHandle = nullptr; return rc; }; - auto* header = static_cast(library->external_fonts_ctx); - const std::uint32_t entryIndex = ClaimExternalEntry(header, request); + auto* header = static_cast(request.context); + const std::uint32_t entryIndex = ClaimEntry(header, request); if (entryIndex == 0xFFFFFFFFu) return fail(SCE_FONT_ERROR_FONT_OPEN_MAX); const std::uint32_t packedId = request.uniqueId == -1 ? entryIndex + 0x40000000u : (static_cast(request.uniqueId) | 0x80000000u); FontObj* fontObj = nullptr; @@ -183,6 +168,7 @@ int OpenExternalFont(const OpenRequest& request, std::uint32_t libraryLock, Font } if (needOpen) { SetOpenFlags(library, request.modeLow); + if (request.context == library->sysfonts_ctx) library->sysfont_flags |= SYSFONT_FLAG_SYSTEM_SET; rc = driver->open(library, request.driverMode, request.source, request.sourceSize, request.subFontIndex, packedId, &fontObj); library->sysfont_flags = 0; if (rc == SCE_FONT_OK) { @@ -221,11 +207,23 @@ int BeginOpen(FontLibrary library, FontHandle* pFontHandle, std::uint32_t& libra return rc; } -bool ReadFileBytes(const std::filesystem::path& path, std::vector& bytes) { +std::shared_ptr> ReadFileBytes(const std::filesystem::path& path) { std::ifstream file(path, std::ios::binary); - if (!file) return false; - bytes.assign(std::istreambuf_iterator(file), std::istreambuf_iterator()); - return !file.bad(); + if (!file) return nullptr; + std::vector bytes{std::istreambuf_iterator(file), std::istreambuf_iterator()}; + if (file.bad()) return nullptr; + return std::make_shared>(std::move(bytes)); +} + +std::shared_ptr> SystemFontBytes(const std::filesystem::path& path) { + static std::mutex mutex; + static std::map>> loaded; + std::lock_guard lock(mutex); + auto& slot = loaded[path]; + if (auto bytes = slot.lock()) return bytes; + auto bytes = ReadFileBytes(path); + slot = bytes; + return bytes; } void CopyStyleToCache(FontHandleNative* font) { @@ -250,11 +248,11 @@ int APS5_VABI sceFontOpenFontMemory(FontLibrary library, const void* fontAddress if (pFontHandle) *pFontHandle = nullptr; return SCE_FONT_ERROR_INVALID_PARAMETER; } - const OpenRequest request{lib, 1, 1, fontAddress, fontSize, true, subFontIndex, uniqueId}; - const int rc = OpenExternalFont(request, libraryLock, pFontHandle); + const OpenRequest request{lib, lib->external_fonts_ctx, 1, 1, fontAddress, fontSize, true, subFontIndex, uniqueId}; + const int rc = OpenFontInContext(request, libraryLock, pFontHandle); if (rc != SCE_FONT_OK) return rc; const auto* bytes = static_cast(fontAddress); - LoadStateFace(ResetState(*pFontHandle), std::vector(bytes, bytes + fontSize), subFontIndex); + LoadStateFace(ResetState(*pFontHandle), std::make_shared>(bytes, bytes + fontSize), subFontIndex); return SCE_FONT_OK; } @@ -278,12 +276,11 @@ int APS5_VABI sceFontOpenFontFile(FontLibrary library, const char* path, std::ui *pFontHandle = nullptr; return SCE_FONT_ERROR_INVALID_PARAMETER; } - const OpenRequest request{lib, modeLow, modeLow + 4, hostPathString.c_str(), 0, false, subFontIndex, uniqueId}; - const int rc = OpenExternalFont(request, libraryLock, pFontHandle); + const OpenRequest request{lib, lib->external_fonts_ctx, modeLow, modeLow + 4, hostPathString.c_str(), 0, false, subFontIndex, uniqueId}; + const int rc = OpenFontInContext(request, libraryLock, pFontHandle); if (rc != SCE_FONT_OK) return rc; - std::vector bytes; FontState& state = ResetState(*pFontHandle); - if (ReadFileBytes(hostPath, bytes)) LoadStateFace(state, std::move(bytes), subFontIndex); + if (auto bytes = ReadFileBytes(hostPath)) LoadStateFace(state, std::move(bytes), subFontIndex); return SCE_FONT_OK; } @@ -309,7 +306,7 @@ int APS5_VABI sceFontOpenFontInstance(FontHandle fontHandle, FontHandle setupFon const std::uint32_t modeLow = source->flags & 0x0Fu; const std::uint32_t subFontIndex = source->open_info.sub_font_index; const std::uint32_t entryIndex = source->open_info.ctx_entry_index; - auto* header = static_cast(library->external_fonts_ctx); + auto* header = static_cast(FontContext(library, source)); if (!header) return fail(SCE_FONT_ERROR_INVALID_FONT_HANDLE); FontHandle target = setupFont; bool owned = false; @@ -371,7 +368,7 @@ int APS5_VABI sceFontOpenFontInstance(FontHandle fontHandle, FontHandle setupFon if (FontState* sourceState = TryGetState(fontHandle)) { targetState.scaleW = sourceState->scaleW; targetState.scaleH = sourceState->scaleH; - if (!sourceState->faceData.empty()) LoadStateFace(targetState, sourceState->faceData, subFontIndex); + if (sourceState->faceData && !sourceState->faceData->empty()) LoadStateFace(targetState, sourceState->faceData, subFontIndex); } if (pFontHandle) *pFontHandle = target; return SCE_FONT_OK; @@ -385,6 +382,8 @@ int APS5_VABI sceFontOpenFontSet(FontLibrary library, std::uint32_t fontSetType, return SCE_FONT_ERROR_INVALID_LIBRARY; } if (!pFontHandle) return SCE_FONT_ERROR_INVALID_PARAMETER; + const std::optional file = FindSystemFontFile(fontSetType); + APS5_LOG_OUT("fontSetType=0x%08X file=%s", fontSetType, file ? file->path.string().c_str() : "(none)"); std::uint32_t libraryLock = 0; if (!AcquireLibraryLock(lib, libraryLock)) { *pFontHandle = nullptr; @@ -399,10 +398,20 @@ int APS5_VABI sceFontOpenFontSet(FontLibrary library, std::uint32_t fontSetType, rc = SCE_FONT_ERROR_INVALID_MEMORY; } else if (!lib->sysfonts_ctx) { rc = SCE_FONT_ERROR_NO_SUPPORT_FUNCTION; - } else if (std::find(SYSTEM_FONT_SET_TYPES.begin(), SYSTEM_FONT_SET_TYPES.end(), fontSetType) == SYSTEM_FONT_SET_TYPES.end()) { + } else if (!IsSystemFontSet(fontSetType)) { rc = SCE_FONT_ERROR_NO_SUPPORT_FONTSET; - } else { + } else if (!file) { rc = SCE_FONT_ERROR_FONT_OPEN_FAILED; + } else { + const std::string hostPathString = file->path.string(); + const OpenRequest request{lib, lib->sysfonts_ctx, openMode, openMode + 4, hostPathString.c_str(), 0, false, file->subFontIndex, static_cast(fontSetType)}; + const int openRc = OpenFontInContext(request, libraryLock, pFontHandle); + if (openRc == SCE_FONT_ERROR_FS_OPEN_FAILED || openRc == SCE_FONT_ERROR_NO_SUPPORT_FORMAT) return SCE_FONT_ERROR_FONT_OPEN_FAILED; + if (openRc != SCE_FONT_OK) return openRc; + GetNativeFont(*pFontHandle)->open_info.fontset_flags = fontSetType; + FontState& state = ResetState(*pFontHandle); + if (auto bytes = SystemFontBytes(file->path)) LoadStateFace(state, std::move(bytes), file->subFontIndex); + return SCE_FONT_OK; } ReleaseLibraryLock(lib, libraryLock); *pFontHandle = nullptr; diff --git a/core/libs/prx/libSceFont/src/State.cpp b/core/libs/prx/libSceFont/src/State.cpp index 58b7e0e49..6911fa24f 100644 --- a/core/libs/prx/libSceFont/src/State.cpp +++ b/core/libs/prx/libSceFont/src/State.cpp @@ -193,6 +193,10 @@ Font::FontObj* Font::FindSubFont(FontObj* head, std::uint32_t subFontIndex) { return nullptr; } +void* Font::FontContext(const FontLibNative* library, const FontHandleNative* font) { + return font->open_info.fontset_flags != 0 ? library->sysfonts_ctx : library->external_fonts_ctx; +} + void Font::ReleaseFontObjectsForHandle(FontHandleNative* font) { auto* library = static_cast(font->library); if (!library || library->magic != LIBRARY_MAGIC || !library->sys_driver || !library->sys_driver->close) return; @@ -201,7 +205,7 @@ void Font::ReleaseFontObjectsForHandle(FontHandleNative* font) { const std::uint32_t modeLow = font->flags & 0x0Fu; FontObj* head = nullptr; std::uint32_t lockWord = 0; - auto* entry = AcquireFontCtxEntry(library->external_fonts_ctx, fontId, modeLow, &head, &lockWord); + auto* entry = AcquireFontCtxEntry(FontContext(library, font), fontId, modeLow, &head, &lockWord); if (!entry) return; FontObj* match = FindSubFont(head, font->open_info.sub_font_index); const std::uint32_t openFlag = lockWord & OPEN_BIT; @@ -243,16 +247,16 @@ void Font::RemoveState(FontHandle handle) { fontStates.erase(handle); } -void Font::LoadStateFace(FontState& state, std::vector bytes, std::uint32_t subFontIndex) { +void Font::LoadStateFace(FontState& state, std::shared_ptr> bytes, std::uint32_t subFontIndex) { if (state.face) { FT_Done_Face(state.face); state.face = nullptr; } state.faceData = std::move(bytes); const FT_Library library = HostFreeTypeLibrary(); - if (!library || state.faceData.empty()) return; + if (!library || !state.faceData || state.faceData->empty()) return; FT_Face face = nullptr; - if (FT_New_Memory_Face(library, state.faceData.data(), static_cast(state.faceData.size()), static_cast(subFontIndex), &face) != 0) return; + if (FT_New_Memory_Face(library, state.faceData->data(), static_cast(state.faceData->size()), static_cast(subFontIndex), &face) != 0) return; FT_Select_Charmap(face, FT_ENCODING_UNICODE); state.face = face; } diff --git a/core/libs/prx/libSceFont/src/SystemFont.cpp b/core/libs/prx/libSceFont/src/SystemFont.cpp new file mode 100644 index 000000000..bf1281380 --- /dev/null +++ b/core/libs/prx/libSceFont/src/SystemFont.cpp @@ -0,0 +1,131 @@ +#include +#include +#include +#include +#include +#include +#ifdef _WIN32 +#include +#endif + +#include "prx/libSceFont/include/FontInternal.hpp" + +namespace { + +constexpr std::array SYSTEM_FONT_SET_TYPES = { + 0x18070043u, 0x18070044u, 0x18070045u, 0x18070047u, 0x18070053u, 0x18070054u, + 0x18070055u, 0x18070057u, 0x180700C3u, 0x180700C4u, 0x180700C5u, 0x180700C7u, + 0x18070444u, 0x18070447u, 0x18070454u, 0x18070457u, 0x180704C4u, 0x180704C7u, + 0x18071053u, 0x18071054u, 0x18071055u, 0x18071057u, 0x18071454u, 0x18071457u, + 0x18072444u, 0x18072447u, 0x180724C4u, 0x180724C7u, 0x18073454u, 0x18073457u, + 0x180734D4u, 0x180734D7u, 0x18078044u, 0x180780C4u, 0x18079054u, 0x1807A044u, + 0x1807A0C4u, 0x1807A444u, 0x1807A4C4u, 0x1807AC44u, 0x1807ACC4u, 0x1807B054u, + 0x1807B0D4u, 0x1807B454u, 0x1807B4D4u, 0x1807BC54u, 0x1807BCD4u, 0x18080444u, + 0x18080447u, 0x18080454u, 0x18080457u, 0x180804C4u, 0x180804C7u, 0x18081454u, + 0x18081457u, 0x18082444u, 0x18082447u, 0x180824C4u, 0x180824C7u, 0x18083454u, + 0x18083457u, 0x180834D4u, 0x180834D7u, 0x1808A444u, 0x1808A4C4u, 0x1808B454u, + 0x1808B4D4u, 0x180C8044u, 0x180C80C4u, 0x180C9054u, 0x180CA044u, 0x180CA0C4u, + 0x180CAC44u, 0x180CACC4u, 0x180CB054u, 0x180CB0D4u, 0x180CBC54u, 0x180CBCD4u, + 0x18170043u, 0x18170044u, 0x18170045u, 0x18170047u, 0x18170444u, 0x18170447u, + 0x18370044u, 0x18370047u, 0x18370444u, 0x18370447u, 0x1A070543u, 0x1A070547u, + 0x1A070553u, 0x1A070557u, 0x1A0705C3u, 0x1A0705C7u, 0x1A071553u, 0x1A071557u, + 0x1A072543u, 0x1A072547u, 0x1A0725C3u, 0x1A0725C7u, 0x1A073553u, 0x1A073557u, + 0x1A0735D3u, 0x1A0735D7u, 0x1A080543u, 0x1A080547u, 0x1A080553u, 0x1A080557u, + 0x1A0805C3u, 0x1A0805C7u, 0x1A081553u, 0x1A081557u, 0x1A082543u, 0x1A082547u, + 0x1A0825C3u, 0x1A0825C7u, 0x1A083553u, 0x1A083557u, 0x1A0835D3u, 0x1A0835D7u, +}; + +enum class Family { Latin, LatinItalic, Typewriter, Vietnamese, Thai, Japanese, ChineseGb }; + +struct FamilyFiles { + std::array system; + std::array substitute; + std::uint32_t substituteSubFont; +}; + +constexpr std::array FAMILY_FILES = {{ + {{"SST-Light.otf", "SST-Roman.otf", "SST-Medium.otf", "SST-Bold.otf"}, + {"NotoSans-Light.ttf", "NotoSans-Regular.ttf", "NotoSans-Medium.ttf", "NotoSans-Bold.ttf"}, 0}, + {{"SST-LightItalic.otf", "SST-Italic.otf", "SST-MediumItalic.otf", "SST-BoldItalic.otf"}, + {"NotoSans-LightItalic.ttf", "NotoSans-Italic.ttf", "NotoSans-MediumItalic.ttf", "NotoSans-BoldItalic.ttf"}, 0}, + {{"SSTTypewriter-Roman.otf", "SSTTypewriter-Roman.otf", "SSTTypewriter-Roman.otf", "SSTTypewriter-Bd.otf"}, + {"NotoSansMono-Light.ttf", "NotoSansMono-Regular.ttf", "NotoSansMono-Medium.ttf", "NotoSansMono-Bold.ttf"}, 0}, + {{"SSTVietnamese-Light.otf", "SSTVietnamese-Roman.otf", "SSTVietnamese-Medium.otf", "SSTVietnamese-Bold.otf"}, + {"NotoSans-Light.ttf", "NotoSans-Regular.ttf", "NotoSans-Medium.ttf", "NotoSans-Bold.ttf"}, 0}, + {{"SSTThai-Light.otf", "SSTThai-Roman.otf", "SSTThai-Medium.otf", "SSTThai-Bold.otf"}, + {"NotoSansThai-Light.ttf", "NotoSansThai-Regular.ttf", "NotoSansThai-Medium.ttf", "NotoSansThai-Bold.ttf"}, 0}, + {{"SSTJpPro-Regular.otf", "SSTJpPro-Regular.otf", "SSTJpPro-Regular.otf", "SSTJpPro-Bold.otf"}, + {"NotoSansCJK-Light.ttc", "NotoSansCJK-Regular.ttc", "NotoSansCJK-Medium.ttc", "NotoSansCJK-Bold.ttc"}, 0}, + {{"DFHEI5-SONY.ttf", "DFHEI5-SONY.ttf", "DFHEI5-SONY.ttf", "DFHEI5-SONY.ttf"}, + {"NotoSansCJK-Light.ttc", "NotoSansCJK-Regular.ttc", "NotoSansCJK-Medium.ttc", "NotoSansCJK-Bold.ttc"}, 2}, +}}; + +Family FamilyOf(std::uint32_t fontSetType) { + const std::uint32_t region = (fontSetType >> 16) & 0xFFu; + const std::uint32_t scripts = (fontSetType >> 8) & 0xFEu; + const bool vietnamese = (fontSetType & 0x10u) != 0; + switch (region) { + case 0x17: return Family::LatinItalic; + case 0x37: return Family::Typewriter; + case 0x08: return scripts == 0x04 && vietnamese ? Family::Vietnamese : Family::Japanese; + case 0x0C: return Family::ChineseGb; + default: + if (scripts == 0x10) return Family::Thai; + if (scripts == 0 && vietnamese) return Family::Vietnamese; + return Family::Latin; + } +} + +std::size_t WeightOf(std::uint32_t fontSetType) { + switch (fontSetType & 0x0Fu) { + case 0x3: return 0; + case 0x5: return 2; + case 0x7: return 3; + default: return 1; + } +} + +std::filesystem::path ExecutableDirectory() { +#ifdef _WIN32 + wchar_t module[MAX_PATH]; + const auto length = GetModuleFileNameW(nullptr, module, MAX_PATH); + if (length == 0 || length == MAX_PATH) throw std::runtime_error("libSceFont: cannot locate the executable"); + return std::filesystem::path(module).parent_path(); +#else + return std::filesystem::read_symlink("/proc/self/exe").parent_path(); +#endif +} + +} + +bool Font::IsSystemFontSet(std::uint32_t fontSetType) { + return std::find(SYSTEM_FONT_SET_TYPES.begin(), SYSTEM_FONT_SET_TYPES.end(), fontSetType) != SYSTEM_FONT_SET_TYPES.end(); +} + +std::vector Font::SystemFontCandidates(std::uint32_t fontSetType) { + if (!IsSystemFontSet(fontSetType)) return {}; + const FamilyFiles& files = FAMILY_FILES[static_cast(FamilyOf(fontSetType))]; + const std::size_t weight = WeightOf(fontSetType); + return {{std::filesystem::path(files.system[weight]), 0}, {std::filesystem::path(files.substitute[weight]), files.substituteSubFont}}; +} + +std::filesystem::path Font::SystemFontDirectory() { + const char* configured = std::getenv("ANYPS5_SYSTEM_FONTS"); + if (!configured || configured[0] == '\0') return ExecutableDirectory() / "anyps5-fonts"; + const std::filesystem::path directory(configured); + std::error_code error; + if (!std::filesystem::is_directory(directory, error)) throw std::runtime_error("libSceFont: ANYPS5_SYSTEM_FONTS is not a directory: " + directory.string()); + return directory; +} + +std::optional Font::FindSystemFontFile(std::uint32_t fontSetType) { + const std::vector candidates = SystemFontCandidates(fontSetType); + if (candidates.empty()) return std::nullopt; + const std::filesystem::path directory = SystemFontDirectory(); + for (const SystemFontFile& candidate : candidates) { + std::error_code error; + const std::filesystem::path path = directory / candidate.path; + if (std::filesystem::is_regular_file(path, error)) return SystemFontFile{path, candidate.subFontIndex}; + } + return std::nullopt; +} diff --git a/core/libs/prx/libSceFontFt/src/Driver.cpp b/core/libs/prx/libSceFontFt/src/Driver.cpp index 71c1ff144..ad09bd5a2 100644 --- a/core/libs/prx/libSceFontFt/src/Driver.cpp +++ b/core/libs/prx/libSceFontFt/src/Driver.cpp @@ -5,6 +5,7 @@ #include #include #include +#include #include #include @@ -26,6 +27,8 @@ struct FtLibraryContext { const FontMemoryInterface* iface; FT_Memory memory; FT_Library library; + FT_Memory hostMemory; + FT_Library hostLibrary; }; void* FtAlloc(FT_Memory memory, long size) { @@ -58,6 +61,42 @@ void* FtRealloc(FT_Memory memory, long currentSize, long newSize, void* block) { return result; } +void* HostAlloc(FT_Memory, long size) { + return size > 0 ? std::malloc(static_cast(size)) : nullptr; +} + +void HostFree(FT_Memory, void* block) { + std::free(block); +} + +void* HostRealloc(FT_Memory, long, long newSize, void* block) { + if (newSize <= 0) { + std::free(block); + return nullptr; + } + return std::realloc(block, static_cast(newSize)); +} + +FT_Library SystemFontLibrary(FtLibraryContext* ctx) { + if (ctx->hostLibrary) return ctx->hostLibrary; + auto* hostMemory = static_cast(ctx->iface->alloc(ctx->allocCtx, sizeof(FT_MemoryRec_))); + if (!hostMemory) return nullptr; + std::memset(hostMemory, 0, sizeof(*hostMemory)); + hostMemory->user = ctx; + hostMemory->alloc = &HostAlloc; + hostMemory->free = &HostFree; + hostMemory->realloc = &HostRealloc; + FT_Library hostLibrary = nullptr; + if (FT_New_Library(hostMemory, &hostLibrary) != 0 || !hostLibrary) { + ctx->iface->dealloc(ctx->allocCtx, hostMemory); + return nullptr; + } + FT_Add_Default_Modules(hostLibrary); + ctx->hostMemory = hostMemory; + ctx->hostLibrary = hostLibrary; + return hostLibrary; +} + void WriteU16(std::uint8_t* out, std::size_t offset, std::uint16_t value) { std::memcpy(out + offset, &value, sizeof(value)); } @@ -131,6 +170,14 @@ int APS5_VABI LibraryTerm(FontLibNative* library) { FT_Done_Library(ctx->library); ctx->library = nullptr; } + if (ctx->hostLibrary) { + FT_Done_Library(ctx->hostLibrary); + ctx->hostLibrary = nullptr; + } + if (ctx->hostMemory) { + freeFn(library->alloc_ctx, ctx->hostMemory); + ctx->hostMemory = nullptr; + } if (ctx->memory) { freeFn(library->alloc_ctx, ctx->memory); ctx->memory = nullptr; @@ -162,12 +209,15 @@ int APS5_VABI LibraryOpenFont(FontLibNative* library, std::uint32_t mode, const } auto* ctx = static_cast(library->fontset_registry); if (!ctx || !ctx->library) return SCE_FONT_ERROR_INVALID_LIBRARY; + const bool systemSet = (library->sysfont_flags & SYSFONT_FLAG_SYSTEM_SET) != 0; + FT_Library* ftLibrary = systemSet ? &ctx->hostLibrary : &ctx->library; + if (systemSet && !SystemFontLibrary(ctx)) return SCE_FONT_ERROR_ALLOCATION_FAILED; FT_Face face = nullptr; FT_Error error; if (fromMemory) { - error = FT_New_Memory_Face(ctx->library, static_cast(fontAddress), static_cast(fontSize), static_cast(subFontIndex), &face); + error = FT_New_Memory_Face(*ftLibrary, static_cast(fontAddress), static_cast(fontSize), static_cast(subFontIndex), &face); } else { - error = FT_New_Face(ctx->library, static_cast(fontAddress), static_cast(subFontIndex), &face); + error = FT_New_Face(*ftLibrary, static_cast(fontAddress), static_cast(subFontIndex), &face); } if (error != 0 || !face) { if (fromMemory || error == FT_Err_Unknown_File_Format) return SCE_FONT_ERROR_NO_SUPPORT_FORMAT; @@ -186,7 +236,7 @@ int APS5_VABI LibraryOpenFont(FontLibNative* library, std::uint32_t mode, const obj->next = *inoutFontObj; if (obj->next) obj->next->prev = obj; obj->ft_face = face; - obj->ft_ctx_0x58 = &ctx->library; + obj->ft_ctx_0x58 = ftLibrary; *inoutFontObj = obj; return SCE_FONT_OK; } diff --git a/core/libs/tests/GuestSystemFont.cpp b/core/libs/tests/GuestSystemFont.cpp new file mode 100644 index 000000000..53f205e5d --- /dev/null +++ b/core/libs/tests/GuestSystemFont.cpp @@ -0,0 +1,218 @@ +#include "prx/libSceFont/include/FontTypes.hpp" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +extern "C" { +int APS5_VABI sceFontMemoryInit(FontMemory*, void*, std::uint32_t, const FontMemoryInterface*, void*, FontMemoryDestroyFunction, void*); +int APS5_VABI sceFontCreateLibrary(const FontMemory*, const void*, FontLibrary*); +int APS5_VABI sceFontDestroyLibrary(FontLibrary*); +int APS5_VABI sceFontSupportSystemFonts(FontLibrary); +int APS5_VABI sceFontSupportExternalFonts(FontLibrary, std::uint32_t, std::uint32_t); +int APS5_VABI sceFontOpenFontSet(FontLibrary, std::uint32_t, std::uint32_t, const FontOpenDetail*, FontHandle*); +int APS5_VABI sceFontOpenFontMemory(FontLibrary, const void*, std::uint32_t, const FontOpenDetail*, FontHandle*); +int APS5_VABI sceFontOpenFontInstance(FontHandle, FontHandle, FontHandle*); +int APS5_VABI sceFontCloseFont(FontHandle); +int APS5_VABI sceFontSetScalePixel(FontHandle, float, float); +int APS5_VABI sceFontGetCharGlyphMetrics(FontHandle, std::uint32_t, FontGlyphMetrics*); +int APS5_VABI sceFontGetHorizontalLayout(FontHandle, FontHorizontalLayout*); +const void* APS5_VABI sceFontSelectLibraryFt(int); +} + +static void Check(bool value, int line) { + if (!value) { + std::fprintf(stderr, "System font check failed at line %d\n", line); + std::abort(); + } +} +#define Require(value) Check((value), __LINE__) + +static int allocations = 0; +static void* APS5_VABI Allocate(void*, std::uint32_t size) { + ++allocations; + return std::malloc(size); +} +static void APS5_VABI Release(void*, void* pointer) { + if (pointer) --allocations; + std::free(pointer); +} + +static void Put16(std::vector& out, std::uint32_t value) { + out.push_back(static_cast(value >> 8)); + out.push_back(static_cast(value)); +} + +static void Put32(std::vector& out, std::uint32_t value) { + Put16(out, value >> 16); + Put16(out, value & 0xFFFFu); +} + +static void PutAll16(std::vector& out, std::initializer_list values) { + for (const int value : values) Put16(out, static_cast(static_cast(value))); +} + +static std::vector SquareGlyphFont(std::uint32_t codepoint, int width) { + std::vector head; + Put32(head, 0x00010000u); + Put32(head, 0x00010000u); + Put32(head, 0); + Put32(head, 0x5F0F3CF5u); + PutAll16(head, {0x000B, 1000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, width, 800, 0, 8, 2, 0, 0}); + std::vector hhea; + Put32(hhea, 0x00010000u); + PutAll16(hhea, {800, -200, 0, width, 0, 0, width, 1, 0, 0, 0, 0, 0, 0, 0, 2}); + std::vector maxp; + Put32(maxp, 0x00010000u); + PutAll16(maxp, {2, 4, 1, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0}); + std::vector hmtx; + PutAll16(hmtx, {width, 0, width, 100}); + std::vector glyf; + PutAll16(glyf, {1, 100, 0, width - 100, 700, 3, 0}); + glyf.insert(glyf.end(), {1, 1, 1, 1}); + PutAll16(glyf, {100, 0, width - 200, 0, 0, 700, 0, -700}); + std::vector loca; + PutAll16(loca, {0, 0, static_cast(glyf.size() / 2)}); + std::vector cmap; + PutAll16(cmap, {0, 1, 3, 1}); + Put32(cmap, 12); + PutAll16(cmap, {4, 32, 0, 4, 4, 1, 0, static_cast(codepoint), 0xFFFF, 0, static_cast(codepoint), 0xFFFF, 1 - static_cast(codepoint), 1, 0, 0}); + const std::vector*>> tables = { + {"cmap", &cmap}, {"glyf", &glyf}, {"head", &head}, {"hhea", &hhea}, {"hmtx", &hmtx}, {"loca", &loca}, {"maxp", &maxp}}; + std::vector font; + Put32(font, 0x00010000u); + PutAll16(font, {static_cast(tables.size()), 64, 2, static_cast(tables.size()) * 16 - 64}); + std::uint32_t offset = 12 + static_cast(tables.size()) * 16; + for (const auto& [tag, data] : tables) { + font.insert(font.end(), tag, tag + 4); + Put32(font, 0); + Put32(font, offset); + Put32(font, static_cast(data->size())); + offset += (static_cast(data->size()) + 3u) & ~3u; + } + for (const auto& [tag, data] : tables) { + font.insert(font.end(), data->begin(), data->end()); + font.resize((font.size() + 3u) & ~std::size_t{3}); + } + return font; +} + +static void WriteFile(const std::filesystem::path& path, const std::vector& bytes) { + std::ofstream file(path, std::ios::binary); + file.write(reinterpret_cast(bytes.data()), static_cast(bytes.size())); + Require(static_cast(file)); +} + +static void SetFontDirectory(const std::filesystem::path& directory) { +#ifdef _WIN32 + Require(_putenv_s("ANYPS5_SYSTEM_FONTS", directory.string().c_str()) == 0); +#else + Require(::setenv("ANYPS5_SYSTEM_FONTS", directory.string().c_str(), 1) == 0); +#endif +} + +static float AdvanceOf(FontHandle font, std::uint32_t code) { + FontGlyphMetrics metrics{}; + Require(sceFontGetCharGlyphMetrics(font, code, &metrics) == SCE_FONT_OK); + return metrics.Horizontal.advance; +} + +int main() { + constexpr std::uint32_t EuropeanLight = 0x180700C3u; + constexpr std::uint32_t EuropeanBold = 0x180700C7u; + constexpr std::uint32_t EuropeanItalic = 0x18170044u; + constexpr std::uint32_t ThaiMedium = 0x18071055u; + constexpr std::uint32_t VietnameseBold = 0x18070057u; + constexpr std::uint32_t JapaneseJg2Light = 0x1A0835D3u; + constexpr std::uint32_t ChineseGb = 0x180CB0D4u; + const std::filesystem::path root = std::filesystem::temp_directory_path() / "anyps5_guest_system_font"; + std::filesystem::remove_all(root); + const std::filesystem::path empty = root / "empty"; + const std::filesystem::path fonts = root / "fonts"; + std::filesystem::create_directories(empty); + std::filesystem::create_directories(fonts); + WriteFile(fonts / "SST-Bold.otf", SquareGlyphFont('A', 700)); + WriteFile(fonts / "NotoSans-Bold.ttf", SquareGlyphFont('A', 300)); + WriteFile(fonts / "NotoSans-Light.ttf", SquareGlyphFont('A', 500)); + WriteFile(fonts / "SST-Italic.otf", SquareGlyphFont('A', 520)); + WriteFile(fonts / "NotoSansThai-Medium.ttf", SquareGlyphFont(0x0E01, 540)); + WriteFile(fonts / "SSTVietnamese-Bold.otf", SquareGlyphFont(0x1EA0, 560)); + WriteFile(fonts / "NotoSansCJK-Light.ttc", SquareGlyphFont(0x3042, 580)); + + const FontMemoryInterface iface{Allocate, Release, nullptr, nullptr, nullptr, nullptr}; + FontMemory memory{}; + Require(sceFontMemoryInit(&memory, nullptr, 0, &iface, nullptr, nullptr, nullptr) == SCE_FONT_OK); + FontLibrary library = nullptr; + Require(sceFontCreateLibrary(&memory, sceFontSelectLibraryFt(0), &library) == SCE_FONT_OK); + Require(sceFontSupportSystemFonts(library) == SCE_FONT_OK); + + FontHandle font = nullptr; + SetFontDirectory(empty); + Require(sceFontOpenFontSet(library, EuropeanBold, 1, nullptr, &font) == SCE_FONT_ERROR_FONT_OPEN_FAILED && font == nullptr); + Require(sceFontOpenFontSet(library, 0x18070046u, 1, nullptr, &font) == SCE_FONT_ERROR_NO_SUPPORT_FONTSET && font == nullptr); + SetFontDirectory(root / "missing"); + bool threw = false; + try { + sceFontOpenFontSet(library, EuropeanBold, 1, nullptr, &font); + } catch (const std::runtime_error&) { + threw = true; + } + Require(threw); + + SetFontDirectory(fonts); + const int beforeOpen = allocations; + Require(sceFontOpenFontSet(library, EuropeanBold, 1, nullptr, &font) == SCE_FONT_OK && font != nullptr); + Require(allocations - beforeOpen == 3); + Require(sceFontSetScalePixel(font, 100.0f, 100.0f) == SCE_FONT_OK); + Require(AdvanceOf(font, 'A') == 70.0f); + FontGlyphMetrics metrics{}; + Require(sceFontGetCharGlyphMetrics(font, 'B', &metrics) == SCE_FONT_ERROR_NO_SUPPORT_GLYPH); + FontHorizontalLayout layout{}; + Require(sceFontGetHorizontalLayout(font, &layout) == SCE_FONT_OK && layout.baselineOffset > 0.0f && layout.lineAdvance >= layout.baselineOffset); + + FontHandle again = nullptr; + Require(sceFontOpenFontSet(library, EuropeanBold, 2, nullptr, &again) == SCE_FONT_OK && again != font); + FontHandle instance = nullptr; + Require(sceFontOpenFontInstance(font, nullptr, &instance) == SCE_FONT_OK && instance != nullptr); + Require(sceFontGetHorizontalLayout(instance, &layout) == SCE_FONT_OK && layout.baselineOffset > 0.0f); + + const std::vector>> expected = { + {EuropeanLight, {'A', 50.0f}}, {EuropeanItalic, {'A', 52.0f}}, {ThaiMedium, {0x0E01, 54.0f}}, + {VietnameseBold, {0x1EA0, 56.0f}}, {JapaneseJg2Light, {0x3042, 58.0f}}}; + for (const auto& [type, glyph] : expected) { + FontHandle set = nullptr; + Require(sceFontOpenFontSet(library, type, 1, nullptr, &set) == SCE_FONT_OK); + Require(sceFontSetScalePixel(set, 100.0f, 100.0f) == SCE_FONT_OK); + Require(AdvanceOf(set, glyph.first) == glyph.second); + Require(sceFontCloseFont(set) == SCE_FONT_OK); + } + Require(sceFontCloseFont(again) == SCE_FONT_OK); + FontHandle missing = nullptr; + Require(sceFontOpenFontSet(library, ChineseGb, 1, nullptr, &missing) == SCE_FONT_ERROR_FONT_OPEN_FAILED && missing == nullptr); + + std::filesystem::remove(fonts / "SST-Bold.otf"); + FontHandle substitute = nullptr; + Require(sceFontOpenFontSet(library, EuropeanBold, 3, nullptr, &substitute) == SCE_FONT_OK); + Require(sceFontSetScalePixel(substitute, 100.0f, 100.0f) == SCE_FONT_OK); + Require(AdvanceOf(substitute, 'A') == 30.0f); + Require(AdvanceOf(font, 'A') == 70.0f); + + Require(sceFontSupportExternalFonts(library, 2, 0x52) == SCE_FONT_OK); + const std::vector external = SquareGlyphFont('A', 900); + FontHandle memoryFont = nullptr; + Require(sceFontOpenFontMemory(library, external.data(), static_cast(external.size()), nullptr, &memoryFont) == SCE_FONT_OK); + Require(sceFontSetScalePixel(memoryFont, 100.0f, 100.0f) == SCE_FONT_OK); + Require(AdvanceOf(memoryFont, 'A') == 90.0f); + Require(sceFontGetHorizontalLayout(memoryFont, &layout) == SCE_FONT_OK); + + for (FontHandle handle : {font, instance, substitute, memoryFont}) Require(sceFontCloseFont(handle) == SCE_FONT_OK); + Require(sceFontDestroyLibrary(&library) == SCE_FONT_OK); + Require(allocations == 0); + std::filesystem::remove_all(root); +} diff --git a/docs/dev/TechnicalDebt.md b/docs/dev/TechnicalDebt.md index c457ddd14..5fd46685b 100644 --- a/docs/dev/TechnicalDebt.md +++ b/docs/dev/TechnicalDebt.md @@ -127,3 +127,4 @@ Throughout the project, every function at every stage either **does exactly what - [64-bit LDS atomics](../../core/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvMemoryInstructions.cpp) (`ds_*_u64`, `ds_*_b64`, `ds_*_f64`) read and write the two dwords under a workgroup lock stored after the guest LDS, as LDS is a 32-bit array without 64-bit atomics. They are atomic with respect to each other only: 32-bit accesses to the same dwords issued concurrently are not ordered by the lock. 64-bit GDS atomics throw. `ds_min/max(_rtn)_f64` and `ds_cmpst(_rtn)_f64` follow the NaN and signed-zero rules measured for the f32 forms; the f64 forms were not measured on hardware. - [wcsrtombs_s](../../core/libs/prx/libc/src/Multibyte.cpp) (libc) follows the console's `libc.prx`, but a runtime-constraint violation only returns its error: the default handler there writes the message and a newline to `stderr` before returning, which is not done here. Conversion uses the C locale of `wcrtomb` (`MB_CUR_MAX` 1), so the path where a multibyte character does not fit the remaining space is not reached - [Thread exit destructors](../../core/libs/prx/libSceLibcInternal/Export.cpp) (libSceLibcInternal) run when a libkernel thread finishes; on the main or a host thread only `_sceLibcInternalThreadDtors` runs them, as `exit` does, else they are dropped +- [sceFontOpenFontSet](../../core/libs/prx/libSceFont/src/SystemFont.cpp) (libSceFont) opens only the primary file of a system font set. Characters the console takes from the set's other files (Hangul, Thai and Arabic in Japanese and Chinese sets, Arabic in European sets) are reported as unsupported glyphs. The console file names are those of the PS4 firmware; the names of the PS5-only JG2 sets (`0x1A......`) are unknown, so they use the SSTJpPro files. When a console file is missing, an openly licensed substitute (Noto Sans, Noto Sans Mono, Noto Sans Thai, Noto Sans CJK) is loaded instead. It is not the system font: its metrics differ, so text width, line height and wrapping differ from the console. System font faces are loaded by FreeType in host memory, not in the font memory the title passes to the library: FreeType needs about 1 MiB per CJK face, which the title's font memory is not sized for. diff --git a/docs/user/USAGE.md b/docs/user/USAGE.md index 7cb429298..999714177 100644 --- a/docs/user/USAGE.md +++ b/docs/user/USAGE.md @@ -85,6 +85,10 @@ Windows PowerShell: .\app.exe ``` +### System fonts + +Games that open the console's system font sets (`sceFontOpenFontSet`) need font files in an `anyps5-fonts/` directory beside the output executable; set `ANYPS5_SYSTEM_FONTS` to use another directory. Files dumped from the console are used under their own names (`SST-Roman.otf`, `SST-Bold.otf`, `SSTJpPro-Regular.otf`, ...). Without them, these openly licensed substitutes are used when present: `NotoSans-{Light,Regular,Medium,Bold}.ttf` and `NotoSans-{LightItalic,Italic,MediumItalic,BoldItalic}.ttf` (Latin and Vietnamese), `NotoSansMono-{Light,Regular,Medium,Bold}.ttf` (typewriter), `NotoSansThai-{Light,Regular,Medium,Bold}.ttf` (Thai) and `NotoSansCJK-{Light,Regular,Medium,Bold}.ttc` (Japanese and Chinese). Without either, opening a system font set fails and the game shows no text in those fonts. + ## Exit codes `0`: conversion succeeded. `1`: invalid arguments. `2`: conversion failed; the error is printed to stderr. With `--autorun`, successful conversion returns the launched application's exit code.