Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions core/libs/prx/libSceFreeType/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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})
Expand All @@ -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)
Expand All @@ -25,5 +31,7 @@ if(BUILD_TESTING)
"PATH=path_list_prepend:$<TARGET_FILE_DIR:libSceFreeType>")
set_tests_properties(guest_freetype_charmap PROPERTIES ENVIRONMENT_MODIFICATION
"PATH=path_list_prepend:$<TARGET_FILE_DIR:libSceFreeType>")
set_tests_properties(guest_freetype_outline PROPERTIES ENVIRONMENT_MODIFICATION
"PATH=path_list_prepend:$<TARGET_FILE_DIR:libSceFreeType>")
endif()
endif()
37 changes: 37 additions & 0 deletions core/libs/prx/libSceFreeType/src/Outline.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include "prx/libSceFreeType/include/GuestFreeType.hpp"
#include "prx/libc/include/general/VabiMacros.hpp"

#include <ft2build.h>
#include FT_OUTLINE_H

#include <algorithm>
#include <cstddef>

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;
}

}
105 changes: 105 additions & 0 deletions core/libs/tests/GuestFreeTypeOutline.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
#include "prx/libSceFreeType/include/GuestFreeType.hpp"
#include "prx/libc/include/general/VabiMacros.hpp"

#include <ft2build.h>
#include FT_OUTLINE_H

#include <array>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <limits>
#include <vector>

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<std::uint16_t>& contours) {
GuestOutline guest{static_cast<std::uint16_t>(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<std::uint16_t>& 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<GuestVector> points) {
std::vector<FT_Vector> nativePoints;
for (const auto& point : points) nativePoints.push_back({static_cast<FT_Pos>(point.x), static_cast<FT_Pos>(point.y)});
GuestOutline guest{65535, static_cast<std::uint16_t>(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<std::uint16_t> contours(contourCount);
EnumerateContours(pointCount, contours, 0);
}
}
std::vector<std::uint16_t> maximum = {65534};
CompareCheck(65535, maximum);
GuestOutline high{1, 65535, nullptr, nullptr, maximum.data(), 0};
Require(FT_Outline_Check_nid_postfix(&high) == 0);
std::vector<std::uint16_t> maximumContours(65535);
for (std::size_t index = 0; index < maximumContours.size(); ++index) maximumContours[index] = static_cast<std::uint16_t>(index);
CompareCheck(65535, maximumContours);
CompareBounds({});
CompareBounds({{17, -29}});
CompareBounds({{-10, 20}, {5000, -7000}, {30, 40}, {-200, 9000}});
CompareBounds({{std::numeric_limits<std::int32_t>::min(), 0}, {0, std::numeric_limits<std::int32_t>::max()}});
std::vector<GuestVector> maximumPoints(65535, {3, 7});
maximumPoints.back() = {-12345, 54321};
CompareBounds(maximumPoints);
std::array<GuestVector, 3> wide = {{{std::numeric_limits<std::int64_t>::min(), 1},
{std::int64_t{1} << 40, std::numeric_limits<std::int64_t>::max()},
{std::numeric_limits<std::int64_t>::max(), std::numeric_limits<std::int64_t>::min()}}};
GuestOutline outline{0, static_cast<std::uint16_t>(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<std::int64_t>::min(), std::numeric_limits<std::int64_t>::min(),
std::numeric_limits<std::int64_t>::max(), std::numeric_limits<std::int64_t>::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);
}
2 changes: 2 additions & 0 deletions docs/dev/debt/libSceFreeType.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down
Loading