diff --git a/core/libs/prx/libSceFreeType/CMakeLists.txt b/core/libs/prx/libSceFreeType/CMakeLists.txt index e0a3b3e1163f..a0c0c765f1e0 100644 --- a/core/libs/prx/libSceFreeType/CMakeLists.txt +++ b/core/libs/prx/libSceFreeType/CMakeLists.txt @@ -1,5 +1,6 @@ add_library(libSceFreeType SHARED EXCLUDE_FROM_ALL Export.cpp + src/Outline.cpp src/GuestObjects.cpp ) target_include_directories(libSceFreeType PRIVATE ${LIBS_INCLUDE_DIR}) @@ -10,6 +11,11 @@ endif() configure_windows_unwind(libSceFreeType) if(BUILD_TESTING) + add_test_executable(guest_freetype_outline_tests ${LIBS_INCLUDE_DIR}/tests/GuestFreeTypeOutline.cpp) + target_include_directories(guest_freetype_outline_tests PRIVATE ${LIBS_INCLUDE_DIR}) + target_link_libraries(guest_freetype_outline_tests PRIVATE libSceFreeType freetype libc) + configure_windows_unwind(guest_freetype_outline_tests) + add_test(NAME guest_freetype_outline COMMAND guest_freetype_outline_tests) add_test_executable(guest_freetype_tests ${LIBS_INCLUDE_DIR}/tests/GuestFreeType.cpp) target_include_directories(guest_freetype_tests PRIVATE ${LIBS_INCLUDE_DIR}) target_link_libraries(guest_freetype_tests PRIVATE libSceFreeType freetype libc) @@ -25,5 +31,7 @@ if(BUILD_TESTING) "PATH=path_list_prepend:$") set_tests_properties(guest_freetype_charmap PROPERTIES ENVIRONMENT_MODIFICATION "PATH=path_list_prepend:$") + set_tests_properties(guest_freetype_outline PROPERTIES ENVIRONMENT_MODIFICATION + "PATH=path_list_prepend:$") endif() endif() diff --git a/core/libs/prx/libSceFreeType/src/Outline.cpp b/core/libs/prx/libSceFreeType/src/Outline.cpp new file mode 100644 index 000000000000..6c68b2aee5cb --- /dev/null +++ b/core/libs/prx/libSceFreeType/src/Outline.cpp @@ -0,0 +1,37 @@ +#include "prx/libSceFreeType/include/GuestFreeType.hpp" +#include "prx/libc/include/general/VabiMacros.hpp" + +#include +#include FT_OUTLINE_H + +#include +#include + +using namespace FreeType; + +extern "C" { + +int APS5_VABI FT_Outline_Check_nid_postfix(GuestOutline* outline) { + if (!outline) return FT_Outline_Check(nullptr); + FT_Outline native{outline->n_contours, outline->n_points, nullptr, nullptr, outline->contours, outline->flags}; + return FT_Outline_Check(&native); +} + +void APS5_VABI FT_Outline_Get_CBox_nid_postfix(const GuestOutline* outline, GuestBBox* box) { + if (!outline || !box) return; + GuestBBox bounds{}; + if (outline->n_points) { + const GuestVector& first = outline->points[0]; + bounds = {first.x, first.y, first.x, first.y}; + for (std::size_t index = 1; index < outline->n_points; ++index) { + const GuestVector& point = outline->points[index]; + bounds.xMin = std::min(bounds.xMin, point.x); + bounds.yMin = std::min(bounds.yMin, point.y); + bounds.xMax = std::max(bounds.xMax, point.x); + bounds.yMax = std::max(bounds.yMax, point.y); + } + } + *box = bounds; +} + +} \ No newline at end of file diff --git a/core/libs/tests/GuestFreeTypeOutline.cpp b/core/libs/tests/GuestFreeTypeOutline.cpp new file mode 100644 index 000000000000..234090218fd4 --- /dev/null +++ b/core/libs/tests/GuestFreeTypeOutline.cpp @@ -0,0 +1,105 @@ +#include "prx/libSceFreeType/include/GuestFreeType.hpp" +#include "prx/libc/include/general/VabiMacros.hpp" + +#include +#include FT_OUTLINE_H + +#include +#include +#include +#include +#include +#include + +using namespace FreeType; + +extern "C" { +int APS5_VABI FT_Outline_Check_nid_postfix(GuestOutline*); +void APS5_VABI FT_Outline_Get_CBox_nid_postfix(const GuestOutline*, GuestBBox*); +} + +static void Check(bool value, int line) { + if (!value) { + std::fprintf(stderr, "FreeType outline check failed at line %d\n", line); + std::abort(); + } +} +#define Require(value) Check((value), __LINE__) + +static void CompareCheck(std::uint16_t pointCount, std::vector& contours) { + GuestOutline guest{static_cast(contours.size()), pointCount, nullptr, nullptr, contours.data(), 0x71}; + FT_Outline native{guest.n_contours, pointCount, nullptr, nullptr, contours.data(), guest.flags}; + const auto before = contours; + Require(FT_Outline_Check_nid_postfix(&guest) == FT_Outline_Check(&native)); + Require(contours == before && guest.flags == 0x71 && guest.points == nullptr && guest.tags == nullptr); +} + +static void EnumerateContours(std::uint16_t pointCount, std::vector& contours, std::size_t index) { + if (index == contours.size()) { + CompareCheck(pointCount, contours); + return; + } + for (const std::uint16_t end : {0, 1, 2, 3, 4, 5, 6, 65535}) { + contours[index] = end; + EnumerateContours(pointCount, contours, index + 1); + } +} + +static bool Equal(const GuestBBox& actual, const GuestBBox& expected) { + return actual.xMin == expected.xMin && actual.yMin == expected.yMin && actual.xMax == expected.xMax && actual.yMax == expected.yMax; +} + +static void CompareBounds(std::vector points) { + std::vector nativePoints; + for (const auto& point : points) nativePoints.push_back({static_cast(point.x), static_cast(point.y)}); + GuestOutline guest{65535, static_cast(points.size()), points.data(), nullptr, nullptr, 0x71}; + FT_Outline native{guest.n_contours, guest.n_points, nativePoints.data(), nullptr, nullptr, guest.flags}; + struct GuardedBox { + std::uint64_t before; + GuestBBox value; + std::uint64_t after; + } actual{0x1122334455667788, {91, 92, 93, 94}, 0x8877665544332211}; + FT_BBox expected{91, 92, 93, 94}; + FT_Outline_Get_CBox(&native, &expected); + FT_Outline_Get_CBox_nid_postfix(&guest, &actual.value); + Require(Equal(actual.value, {expected.xMin, expected.yMin, expected.xMax, expected.yMax})); + Require(actual.before == 0x1122334455667788 && actual.after == 0x8877665544332211); + FT_Outline_Get_CBox_nid_postfix(&guest, nullptr); + Require(guest.n_contours == 65535 && guest.flags == 0x71 && guest.tags == nullptr && guest.contours == nullptr); +} + +int main() { + Require(FT_Outline_Check_nid_postfix(nullptr) == FT_Outline_Check(nullptr)); + for (std::uint16_t pointCount = 0; pointCount <= 6; ++pointCount) { + for (std::size_t contourCount = 0; contourCount <= 3; ++contourCount) { + std::vector contours(contourCount); + EnumerateContours(pointCount, contours, 0); + } + } + std::vector maximum = {65534}; + CompareCheck(65535, maximum); + GuestOutline high{1, 65535, nullptr, nullptr, maximum.data(), 0}; + Require(FT_Outline_Check_nid_postfix(&high) == 0); + std::vector maximumContours(65535); + for (std::size_t index = 0; index < maximumContours.size(); ++index) maximumContours[index] = static_cast(index); + CompareCheck(65535, maximumContours); + CompareBounds({}); + CompareBounds({{17, -29}}); + CompareBounds({{-10, 20}, {5000, -7000}, {30, 40}, {-200, 9000}}); + CompareBounds({{std::numeric_limits::min(), 0}, {0, std::numeric_limits::max()}}); + std::vector maximumPoints(65535, {3, 7}); + maximumPoints.back() = {-12345, 54321}; + CompareBounds(maximumPoints); + std::array wide = {{{std::numeric_limits::min(), 1}, + {std::int64_t{1} << 40, std::numeric_limits::max()}, + {std::numeric_limits::max(), std::numeric_limits::min()}}}; + GuestOutline outline{0, static_cast(wide.size()), wide.data(), nullptr, nullptr, 0}; + GuestBBox box{1, 2, 3, 4}; + FT_Outline_Get_CBox_nid_postfix(&outline, &box); + Require(Equal(box, {std::numeric_limits::min(), std::numeric_limits::min(), + std::numeric_limits::max(), std::numeric_limits::max()})); + const GuestBBox retained = box; + FT_Outline_Get_CBox_nid_postfix(nullptr, &box); + Require(Equal(box, retained)); + FT_Outline_Get_CBox_nid_postfix(nullptr, nullptr); +} diff --git a/docs/dev/debt/libSceFreeType.md b/docs/dev/debt/libSceFreeType.md index 6d4f98438dde..1dbcf13cc196 100644 --- a/docs/dev/debt/libSceFreeType.md +++ b/docs/dev/debt/libSceFreeType.md @@ -8,6 +8,8 @@ - [FT_New_Memory_Face, FT_Done_Face and FT_Done_FreeType](../../../core/libs/prx/libSceFreeType/Export.cpp) (libSceFreeType) - console signatures, errors and generic-finalizer ordering are unverified; they follow the bundled [FreeType 2.13.3](https://github.com/freetype/freetype/blob/42608f77f20749dd6ddc9e0536788eaad70ea4b5/include/freetype/freetype.h) with LP64 guest arguments. Caller-owned font bytes must remain valid until the face is freed. +- [FT_Outline_Check, FT_Outline_Get_CBox](../../../core/libs/prx/libSceFreeType/src/Outline.cpp) (libSceFreeType) - console signatures, count signedness and errors are unverified; the adapter follows bundled [FreeType 2.13.3](https://github.com/freetype/freetype/blob/42608f77f20749dd6ddc9e0536788eaad70ea4b5/src/base/ftoutln.c), including its unsigned 16-bit counts and contour endpoints. Control-box coordinates use the existing guest LP64 layout and retain their full 64-bit range on Windows; contour checking does not validate point tags + ### Functional - [libSceFreeType](../../../core/libs/prx/libSceFreeType/src/GuestObjects.cpp) (libSceFreeType) - private fields (`driver`, `memory`, `stream`, `sizes_list`, `extensions`) and `subglyphs` stay null. Face, size and glyph-slot records are copies. Only edits to outline points and flags are written back before `FT_Render_Glyph`; replacing arrays or exceeding the host `FT_Pos` range throws. `FT_New_Face` reads the whole file into memory and throws for files of 2 GiB or more on Windows. On macOS, native FreeType symbols remain exported and could resolve guest imports without adaptation; Windows and Linux hide them with `--exclude-libs`.