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
1 change: 1 addition & 0 deletions core/libs/prx/libSceFreeType/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ add_library(libSceFreeType SHARED EXCLUDE_FROM_ALL
src/FaceInfo.cpp
src/GuestObjects.cpp
src/Names.cpp
src/Trigonometry.cpp
)
target_include_directories(libSceFreeType PRIVATE ${LIBS_INCLUDE_DIR})
target_link_libraries(libSceFreeType PRIVATE freetype)
Expand Down
81 changes: 81 additions & 0 deletions core/libs/prx/libSceFreeType/src/Trigonometry.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#include "prx/libSceFreeType/include/GuestFreeType.hpp"

#include <ft2build.h>
#include FT_TRIGONOMETRY_H

#include <array>
#include <cstdint>
#include <limits>
#include <stdexcept>

using namespace FreeType;

namespace {

FT_Angle NormalizeAngle(std::int64_t angle) {
return static_cast<FT_Angle>(angle % (360ll << 16));
}

GuestVector TangentVector(std::int64_t angle) {
constexpr std::array<std::int64_t, 22> angles{
1740967, 919879, 466945, 234379, 117304, 58666, 29335, 14668, 7334, 3667, 1833,
917, 458, 229, 115, 57, 29, 14, 7, 4, 2, 1};
std::int64_t remaining = NormalizeAngle(angle);
GuestVector vector{1ll << 24, 0};
while (remaining < -(45ll << 16)) {
vector = {vector.y, -vector.x};
remaining += 90ll << 16;
}
while (remaining > (45ll << 16)) {
vector = {-vector.y, vector.x};
remaining -= 90ll << 16;
}
std::int64_t rounding = 1;
for (std::size_t iteration = 1; iteration <= angles.size(); ++iteration) {
const std::int64_t direction = remaining < 0 ? 1 : -1;
const GuestVector step{(vector.y + rounding) >> iteration, (vector.x + rounding) >> iteration};
vector.x += direction * step.x;
vector.y -= direction * step.y;
remaining += direction * angles[iteration - 1];
rounding <<= 1;
}
return vector;
}

}

extern "C" {

std::int64_t APS5_VABI FT_Cos_nid_postfix(std::int64_t angle) {
return FT_Cos(NormalizeAngle(angle));
}

std::int64_t APS5_VABI FT_Sin_nid_postfix(std::int64_t angle) {
return FT_Sin(NormalizeAngle(angle));
}

std::int64_t APS5_VABI FT_Tan_nid_postfix(std::int64_t angle) {
const GuestVector vector = TangentVector(angle);
const std::uint64_t x = static_cast<std::uint64_t>(vector.x < 0 ? -vector.x : vector.x);
const std::uint64_t y = static_cast<std::uint64_t>(vector.y < 0 ? -vector.y : vector.y);
const auto quotient = static_cast<std::int64_t>(x == 0 ? 0x7FFFFFFFull : ((y << 16) + (x >> 1)) / x);
return (vector.x < 0) != (vector.y < 0) ? -quotient : quotient;
}

void APS5_VABI FT_Vector_Unit_nid_postfix(GuestVector* vector, std::int64_t angle) {
if (!vector) return;
FT_Vector native{};
FT_Vector_Unit(&native, NormalizeAngle(angle));
vector->x = native.x;
vector->y = native.y;
}

std::int64_t APS5_VABI FT_Angle_Diff_nid_postfix(std::int64_t first, std::int64_t second) {
if ((first > 0 && second < std::numeric_limits<std::int64_t>::min() + first) ||
(first < 0 && second > std::numeric_limits<std::int64_t>::max() + first)) {
throw std::runtime_error("FT_Angle_Diff: overflowing signed angle subtraction is not implemented");
}
return FT_Angle_Diff(0, NormalizeAngle(second - first));
}

}
196 changes: 196 additions & 0 deletions core/libs/tests/GuestFreeType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include FT_FREETYPE_H
#include FT_GASP_H
#include FT_OUTLINE_H
#include FT_TRIGONOMETRY_H

#include <algorithm>
#include <array>
Expand All @@ -27,6 +28,11 @@ using namespace FreeType;

extern "C" {
void APS5_VABI FT_Set_Transform_nid_postfix(GuestFaceRec*, const GuestMatrix*, const GuestVector*);
std::int64_t APS5_VABI FT_Cos_nid_postfix(std::int64_t);
std::int64_t APS5_VABI FT_Sin_nid_postfix(std::int64_t);
std::int64_t APS5_VABI FT_Tan_nid_postfix(std::int64_t);
std::int64_t APS5_VABI FT_Angle_Diff_nid_postfix(std::int64_t, std::int64_t);
void APS5_VABI FT_Vector_Unit_nid_postfix(GuestVector*, std::int64_t);
int APS5_VABI FT_Init_FreeType_nid_postfix(void**);
int APS5_VABI FT_Done_FreeType_nid_postfix(void*);
int APS5_VABI FT_New_Face_nid_postfix(void*, const char*, std::int64_t, GuestFaceRec**);
Expand Down Expand Up @@ -294,6 +300,195 @@ static void SizeMatches(const GuestFaceRec* face, FT_Face host) {
Require(actual.height == expected.height && actual.max_advance == expected.max_advance);
}

static void Trigonometry() {
constexpr std::int64_t circle = 360ll << 16;
const std::array<std::pair<std::int64_t, std::int64_t>, 68> poleTangents{{
{-17694728ll, 33692373208ll},
{-17694727ll, 45725363639ll},
{-17694726ll, 45725363639ll},
{-17694725ll, 64015509094ll},
{-17694724ll, 64015509094ll},
{-17694723ll, 128031018189ll},
{-17694722ll, 128031018189ll},
{-17694721ll, 640155090944ll},
{-17694720ll, -640155090944ll},
{-17694719ll, -128031018189ll},
{-17694718ll, -128031018189ll},
{-17694717ll, -64015509094ll},
{-17694716ll, -64015509094ll},
{-17694715ll, -45725363639ll},
{-17694714ll, -45725363639ll},
{-17694713ll, -33692373208ll},
{-17694712ll, -33692373208ll},
{-5898248ll, 33692373208ll},
{-5898247ll, 45725363639ll},
{-5898246ll, 45725363639ll},
{-5898245ll, 64015509094ll},
{-5898244ll, 64015509094ll},
{-5898243ll, 128031018189ll},
{-5898242ll, 128031018189ll},
{-5898241ll, 640155090944ll},
{-5898240ll, -640155090944ll},
{-5898239ll, -128031018189ll},
{-5898238ll, -128031018189ll},
{-5898237ll, -64015509094ll},
{-5898236ll, -64015509094ll},
{-5898235ll, -45725363639ll},
{-5898234ll, -45725363639ll},
{-5898233ll, -33692373208ll},
{-5898232ll, -33692373208ll},
{5898232ll, 33692373208ll},
{5898233ll, 45725363639ll},
{5898234ll, 45725363639ll},
{5898235ll, 64015509094ll},
{5898236ll, 64015509094ll},
{5898237ll, 128031018189ll},
{5898238ll, 128031018189ll},
{5898239ll, 640155090944ll},
{5898240ll, -640155090944ll},
{5898241ll, -128031018189ll},
{5898242ll, -128031018189ll},
{5898243ll, -64015509094ll},
{5898244ll, -64015509094ll},
{5898245ll, -45725363639ll},
{5898246ll, -45725363639ll},
{5898247ll, -33692373208ll},
{5898248ll, -33692373208ll},
{17694712ll, 33692373208ll},
{17694713ll, 45725363639ll},
{17694714ll, 45725363639ll},
{17694715ll, 64015509094ll},
{17694716ll, 64015509094ll},
{17694717ll, 128031018189ll},
{17694718ll, 128031018189ll},
{17694719ll, 640155090944ll},
{17694720ll, -640155090944ll},
{17694721ll, -128031018189ll},
{17694722ll, -128031018189ll},
{17694723ll, -64015509094ll},
{17694724ll, -64015509094ll},
{17694725ll, -45725363639ll},
{17694726ll, -45725363639ll},
{17694727ll, -33692373208ll},
{17694728ll, -33692373208ll}}};
int scalarCases = 0;
int vectorCases = 0;
int differenceCases = 0;
for (const auto& [angle, tangent] : poleTangents) {
Require(FT_Tan_nid_postfix(angle) == tangent);
if (sizeof(FT_Fixed) == 4) {
Require(static_cast<std::int64_t>(FT_Tan(static_cast<FT_Angle>(angle))) != tangent);
} else {
Require(static_cast<std::int64_t>(FT_Tan(static_cast<FT_Angle>(angle))) == tangent);
}
}
auto compare = [&](std::int64_t angle, FT_Angle nativeAngle, std::int64_t expectedTangent) {
Require(FT_Cos_nid_postfix(angle) == FT_Cos(nativeAngle));
Require(FT_Sin_nid_postfix(angle) == FT_Sin(nativeAngle));
Require(FT_Tan_nid_postfix(angle) == expectedTangent);
FT_Vector expected{};
FT_Vector_Unit(&expected, nativeAngle);
struct GuardedVector {
std::uint64_t before;
GuestVector vector;
std::uint64_t after;
} actual{0xA55A0FF0FF00AA55ull, {INT64_MAX, INT64_MIN}, 0x55AA00FFF00F5AA5ull};
FT_Vector_Unit_nid_postfix(&actual.vector, angle);
Require(actual.vector.x == expected.x && actual.vector.y == expected.y);
Require(actual.vector.x == FT_Cos_nid_postfix(angle) && actual.vector.y == FT_Sin_nid_postfix(angle));
Require(actual.before == 0xA55A0FF0FF00AA55ull && actual.after == 0x55AA00FFF00F5AA5ull);
++scalarCases;
++vectorCases;
};
std::mt19937 random(0x17A693Bu);
std::uniform_int_distribution<std::int32_t> angles(INT32_MIN, INT32_MAX);
for (unsigned sample = 0; sample < 4096; ++sample) {
const std::int32_t angle = angles(random);
Require(FT_Cos_nid_postfix(angle) == FT_Cos(angle));
Require(FT_Sin_nid_postfix(angle) == FT_Sin(angle));
Require(static_cast<std::int32_t>(FT_Tan_nid_postfix(angle)) == FT_Tan(angle));
FT_Vector expected{};
GuestVector actual{};
FT_Vector_Unit(&expected, angle);
FT_Vector_Unit_nid_postfix(&actual, angle);
Require(actual.x == expected.x && actual.y == expected.y);
++scalarCases;
++vectorCases;
}
const std::array<std::int64_t, 14> bases{-circle + 1, -(180ll << 16), -(90ll << 16) - 1, -(45ll << 16),
-1, 0, 1, 25783, 45ll << 16, (90ll << 16) - 1, 90ll << 16, (90ll << 16) + 1, 180ll << 16, circle - 1};
for (const std::int64_t turns : {0ll, 1ll, -1ll, 97ll, -97ll, 100000000000ll, -100000000000ll, 390000000000ll, -390000000000ll}) {
for (const std::int64_t base : bases) {
const std::int64_t tangent = FT_Tan_nid_postfix(base);
compare(base + circle * turns, static_cast<FT_Angle>(base), tangent);
for (const std::int64_t other : bases) {
Require(FT_Angle_Diff_nid_postfix(base + circle * turns, other + circle * turns) ==
FT_Angle_Diff(static_cast<FT_Angle>(base), static_cast<FT_Angle>(other)));
++differenceCases;
}
}
}
for (const auto boundary : std::array<std::pair<std::int64_t, std::int64_t>, 4>{
std::pair{INT64_MIN, -16252928ll}, std::pair{INT64_MAX, 16252927ll},
std::pair{INT64_MIN + 1, -16252927ll}, std::pair{INT64_MAX - 1, 16252926ll}}) {
Require(FT_Cos_nid_postfix(boundary.first) == FT_Cos(static_cast<FT_Angle>(boundary.second)));
Require(FT_Sin_nid_postfix(boundary.first) == FT_Sin(static_cast<FT_Angle>(boundary.second)));
Require(FT_Tan_nid_postfix(boundary.first) == FT_Tan_nid_postfix(boundary.second));
FT_Vector expected{};
GuestVector actual{};
FT_Vector_Unit(&expected, static_cast<FT_Angle>(boundary.second));
FT_Vector_Unit_nid_postfix(&actual, boundary.first);
Require(actual.x == expected.x && actual.y == expected.y);
++scalarCases;
++vectorCases;
}
FT_Vector_Unit_nid_postfix(nullptr, INT64_MIN);
Require(FT_Angle_Diff_nid_postfix(INT64_MIN, INT64_MIN + 1) == 1);
Require(FT_Angle_Diff_nid_postfix(INT64_MAX, INT64_MAX - 1) == -1);
Require(FT_Angle_Diff_nid_postfix(INT64_MIN, INT64_MIN) == 0);
Require(FT_Angle_Diff_nid_postfix(INT64_MAX, INT64_MAX) == 0);
for (const auto pair : std::array<std::pair<std::int64_t, std::int64_t>, 4>{
std::pair{INT64_MIN, INT64_MAX}, std::pair{INT64_MAX, INT64_MIN}, std::pair{INT64_MIN, 0ll}, std::pair{0ll, INT64_MIN}}) {
if (pair.first == 0) {
Require(FT_Angle_Diff_nid_postfix(pair.first, pair.second) == FT_Angle_Diff_nid_postfix(0, -16252928));
} else {
Require(ThrowsWith([&] { FT_Angle_Diff_nid_postfix(pair.first, pair.second); }, "FT_Angle_Diff: overflowing signed angle subtraction"));
}
}
const auto font = SquareGlyphFont('A', 700);
void* library = nullptr;
FT_Library oracle = nullptr;
GuestFaceRec* face = nullptr;
FT_Face native = nullptr;
Require(FT_Init_FreeType_nid_postfix(&library) == 0 && FT_Init_FreeType(&oracle) == 0);
Require(FT_New_Memory_Face_nid_postfix(library, font.data(), font.size(), 0, &face) == 0);
Require(FT_New_Memory_Face(oracle, font.data(), static_cast<FT_Long>(font.size()), 0, &native) == 0);
int rendered = 0;
for (const unsigned pixels : {32u, 64u}) {
Require(FT_Set_Pixel_Sizes_nid_postfix(face, 0, pixels) == FT_Set_Pixel_Sizes(native, 0, pixels));
for (const FT_Angle angle : {0L, 30L << 16, 45L << 16, 90L << 16, 135L << 16}) {
Require(FT_Load_Char_nid_postfix(face, 'A', FT_LOAD_DEFAULT) == FT_Load_Char(native, 'A', FT_LOAD_DEFAULT));
GuestVector unit{};
FT_Vector_Unit_nid_postfix(&unit, angle + circle * 100000000000ll);
for (unsigned point = 0; point < face->glyph->outline.n_points; ++point) {
auto& value = face->glyph->outline.points[point];
const auto x = static_cast<FT_Long>(value.x);
const auto y = static_cast<FT_Long>(value.y);
value = {FT_MulFix(x, static_cast<FT_Long>(unit.x)) - FT_MulFix(y, static_cast<FT_Long>(unit.y)),
FT_MulFix(x, static_cast<FT_Long>(unit.y)) + FT_MulFix(y, static_cast<FT_Long>(unit.x))};
}
FT_Matrix matrix{FT_Cos(angle), -FT_Sin(angle), FT_Sin(angle), FT_Cos(angle)};
FT_Outline_Transform(&native->glyph->outline, &matrix);
Require(FT_Render_Glyph_nid_postfix(face->glyph, FT_RENDER_MODE_NORMAL) == FT_Render_Glyph(native->glyph, FT_RENDER_MODE_NORMAL));
SlotMatches(face->glyph, native->glyph);
++rendered;
}
}
Require(FT_Done_FreeType_nid_postfix(library) == 0 && FT_Done_FreeType(oracle) == 0);
std::printf("FreeType trigonometry: %d scalar cases, %d vectors, %d angle differences, 68 LP64 tangent goldens, %d rendered glyphs\n",
scalarCases, vectorCases, differenceCases, rendered);
}

struct Finalizers {
std::vector<int> calls;
};
Expand Down Expand Up @@ -437,6 +632,7 @@ int main() {
const auto root = std::filesystem::temp_directory_path() / ("anyps5_guest_freetype-" + std::to_string(std::random_device{}()));
std::filesystem::create_directories(root / "app0");
CheckGasp();
Trigonometry();
const std::vector<unsigned char> font = SquareGlyphFont('A', 700);
{
std::ofstream file(root / "app0" / "font.ttf", std::ios::binary);
Expand Down
2 changes: 2 additions & 0 deletions docs/dev/debt/libSceFreeType.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

- [FT_Select_Charmap, FT_Set_Charmap, FT_Get_First_Char, FT_Get_Next_Char](../../../core/libs/prx/libSceFreeType/Export.cpp), [FT_Get_Glyph_Name, FT_Get_Name_Index, FT_Get_Postscript_Name](../../../core/libs/prx/libSceFreeType/src/Names.cpp) (libSceFreeType) - console signatures, naming rules and errors are unverified; the LP64 adapter follows bundled [FreeType 2.13.3](https://github.com/freetype/freetype/blob/42608f77f20749dd6ddc9e0536788eaad70ea4b5/src/base/ftobjs.c). Iteration uses the low 32 input bits and updates only the selected guest charmap; charmap fields are read-only copies. Naming preserves LP64 invalid-index errors on Windows. PostScript names borrow native face storage and remain valid only while the face is alive

- [FT_Cos, FT_Sin, FT_Tan, FT_Vector_Unit and FT_Angle_Diff](../../../core/libs/prx/libSceFreeType/src/Trigonometry.cpp) (libSceFreeType) - console ABI/version and CORDIC equivalence are unverified; the LP64 adapter follows bundled [FreeType 2.13.3](https://github.com/freetype/freetype/blob/42608f77f20749dd6ddc9e0536788eaad70ea4b5/src/base/fttrigon.c). Tangent retains the 64-bit quotient near poles instead of Windows FT_Long truncation. Angle differences whose signed 64-bit subtraction overflows throw; the bundled source's subtraction is undefined there.

- [FT_Init_FreeType, FT_New_Face, FT_Set_Pixel_Sizes, FT_Get_Char_Index, FT_Load_Glyph, FT_Render_Glyph](../../../core/libs/prx/libSceFreeType/Export.cpp) (libSceFreeType) - the PS4 library is taken to be FreeType 2 built for LP64: the face, size, charmap, glyph slot, bitmap and outline layouts follow the public headers of the bundled FreeType 2.13.3 with a 64-bit `long`, which matches the offsets the OpenOrbis samples read (`face->glyph` at 0x98, `slot->advance` at 0x80, `slot->bitmap` at 0x98, `slot->bitmap_left` at 0xc0). Which FreeType version and options the console library is built with is unknown, so the behaviour and error codes are those of FreeType 2.13.3. The OpenOrbis font, keyboard, threading, system and SDL2 samples import these six functions

- [FT_Request_Size](../../../core/libs/prx/libSceFreeType/src/SizeRequest.cpp) (libSceFreeType) - console signature, request layout and errors are unverified; the guest request follows the LP64 public headers of the bundled [FreeType 2.13.3](https://github.com/freetype/freetype/blob/42608f77f20749dd6ddc9e0536788eaad70ea4b5/include/freetype/freetype.h). Host overflow behavior remains that of the bundled build. On Windows, native `FT_Long` is 32 bits; valid requests whose width or height exceeds that range throw before changing the size.
Expand Down
Loading