fix(libSceFont): keep horizontal writing on a single baseline #5209

Merged
Misjudgments merged 2 commits from upstream-pr/5209 into main 2026-10-29 07:43:03 +00:00
Misjudgments commented 2026-10-13 14:39:58 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Misjudgments on 2026-10-13T14:39:58Z.


What

Horizontal writing steps in libSceFont advanced the pen by the glyph's vertical
advance on every character, so text drifted down diagonally — steepest for CJK
fonts, whose vertical advance is roughly the em size. The step's GlyphMetrics
width/height were also overwritten with the pen position, leaving the first
glyph of each run invisible at pen (0, 0).

Every supported writing form (0x10, 0x11, 0x12) is horizontal, so the pen
must advance only along X and the step must keep the real glyph width/height.
The vertical advance stays available as a metric through
sceFontGetCharGlyphMetrics.

Tested

Windows 11 x64. Current main boots the PSP Classics title Ape Academy 2
(UCES00302) to a playable state; the emulator's own UI dialog ("Confirm Rewind":
Rewind / Cancel / Yes) rendered its text with each glyph stepped diagonally
downwards and dropped the first glyph of every word. After this fix, rebuilt
libSceFont.prx and libSceFont-module.prx were redeployed and the dialog text
renders on a single baseline with all glyphs present; the title remains
playable. No other libSceFont callers regressed in this test.

Checklist

  • Based on current main; no equivalent open PR implements this fix
  • One topic per PR
  • No new code comments; behavior documented in docs/dev/debt/libSceFont.md
  • No silent stubs added
  • No investigation files, images or game data added to the repository
  • No new third-party code
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Unblocks: none
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4844911961 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5209). Originally posted by [Misjudgments](https://github.com/Misjudgments) on 2026-10-13T14:39:58Z. --- ### What Horizontal writing steps in libSceFont advanced the pen by the glyph's vertical advance on every character, so text drifted down diagonally — steepest for CJK fonts, whose vertical advance is roughly the em size. The step's `GlyphMetrics` width/height were also overwritten with the pen position, leaving the first glyph of each run invisible at pen `(0, 0)`. Every supported writing form (`0x10`, `0x11`, `0x12`) is horizontal, so the pen must advance only along X and the step must keep the real glyph width/height. The vertical advance stays available as a metric through `sceFontGetCharGlyphMetrics`. ### Tested Windows 11 x64. Current `main` boots the PSP Classics title Ape Academy 2 (UCES00302) to a playable state; the emulator's own UI dialog ("Confirm Rewind": Rewind / Cancel / Yes) rendered its text with each glyph stepped diagonally downwards and dropped the first glyph of every word. After this fix, rebuilt `libSceFont.prx` and `libSceFont-module.prx` were redeployed and the dialog text renders on a single baseline with all glyphs present; the title remains playable. No other libSceFont callers regressed in this test. ### Checklist - [x] Based on current `main`; no equivalent open PR implements this fix - [x] One topic per PR - [x] No new code comments; behavior documented in `docs/dev/debt/libSceFont.md` - [x] No silent stubs added - [x] No investigation files, images or game data added to the repository - [x] No new third-party code - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Unblocks: none - [x] AI-assisted: yes
adriadam10 commented 2026-10-14 18:02:28 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-14T18:02:28Z.

Review: CHANGES_REQUESTED


Thanks for tracking this down. Before merging I need three things:

  • A test that reproduces it on Linux. The PR has no test, and the libSceFont tests (3/3) pass the same with and without the change. Please add one that walks a short horizontal string through the writing step API (sceFontWritingRefersRenderStep / sceFontWritingGetRenderMetrics) and pins step.y constant across characters, advanceY == 0, and GlyphMetrics.width/height equal to the glyph's own values (including the first character at pen (0, 0)). A manual run on one title is not enough to merge a behaviour change.
  • Why the reference is wrong. shadPS4's FillWritingMetricsFromGlyph (src/core/libraries/font/font.cpp:145) does what the code you removed did (advanceY = Vertical.advance, GlyphMetrics.width/height = step.x/y), with a comment saying the real library keeps the anchor at the pen position. Please say which source you follow for zeroing them, and whether the real library's step keeps the glyph's width/height.
  • Possible root cause. docs/dev/debt/libSceFont.md:23 notes that our vertical metrics (FreeType's vertical advance) are not confirmed on a title. If the real vertical advance of a CJK face is small or 0 for horizontal text, the drift may come from that value rather than from advancing the pen by it. Please check which of the two explains the dialog, and make the debt line say so.
<!-- greyfinch-sync:boykopovar/AnyPS5:review:5522366447 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5209#pullrequestreview-5522366447). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-14T18:02:28Z. **Review:** CHANGES_REQUESTED --- Thanks for tracking this down. Before merging I need three things: - **A test that reproduces it on Linux.** The PR has no test, and the libSceFont tests (3/3) pass the same with and without the change. Please add one that walks a short horizontal string through the writing step API (`sceFontWritingRefersRenderStep` / `sceFontWritingGetRenderMetrics`) and pins `step.y` constant across characters, `advanceY == 0`, and `GlyphMetrics.width/height` equal to the glyph's own values (including the first character at pen `(0, 0)`). A manual run on one title is not enough to merge a behaviour change. - **Why the reference is wrong.** shadPS4's `FillWritingMetricsFromGlyph` (`src/core/libraries/font/font.cpp:145`) does what the code you removed did (`advanceY = Vertical.advance`, `GlyphMetrics.width/height = step.x/y`), with a comment saying the real library keeps the anchor at the pen position. Please say which source you follow for zeroing them, and whether the real library's step keeps the glyph's width/height. - **Possible root cause.** `docs/dev/debt/libSceFont.md:23` notes that our vertical metrics (FreeType's vertical advance) are not confirmed on a title. If the real vertical advance of a CJK face is small or 0 for horizontal text, the drift may come from that value rather than from advancing the pen by it. Please check which of the two explains the dialog, and make the debt line say so.
JAB-dev commented 2026-10-18 11:40:38 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by JAB-dev on 2026-10-18T11:40:38Z.


Test that fails on current main and passes with this change, plus a measurement from real firmware.

Test (added to guest_system_font, uses its square-glyph font at 100 px, whose glyph has horizontal advance 70 and vertical advance 100): writes the string "AA" through sceFontWritingInit and sceFontWritingRefersRenderStep and checks both steps.

  • main: fails at the first step check (advanceY is 100, the second step lands at y 100).
  • With this change: step 1 is x 0, y 0, advanceX 70, advanceY 0, step 2 is x 70, y 0, advanceX 70, advanceY 0, and the step's own glyph metrics still report the vertical advance 100. guest_system_font, guest_font and guest_font_writing_line pass (3/3), Windows 11, GCC 15.2.
  • It does not cover the glyph width/height part of this change.

Real firmware (PS4 firmware 11.00 libSceFont loaded on Windows, not the PS5 library): the steps of "Ab c" come back x=0 y=0 adv=22.4688,0, x=22.4688 y=0 adv=17.7969,0, x=40.2656 y=0 adv=8.89062,0, x=49.1562 y=0 adv=16,0. y stays 0 and the second advance is 0 on every step, so horizontal text does not step vertically, which is what this change does.

Game: Ape Academy 2 (PPSA11132, PSP Classics) on current main, Windows 11, RTX 4070 SUPER: the controller screen text ("Click the touch pad button...", "Press START to play!") has letters scattered down the screen. With this change in libSceFont it is on one line.

Test diff
diff --git a/core/libs/tests/GuestSystemFont.cpp b/core/libs/tests/GuestSystemFont.cpp
index ec2f80785..d8fc39a43 100644
--- a/core/libs/tests/GuestSystemFont.cpp
+++ b/core/libs/tests/GuestSystemFont.cpp
@@ -25,6 +25,12 @@ 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);
+int APS5_VABI sceFontTextSourceInit(FontTextSource*, const void*, std::uint32_t, FontTextParseFunction, void*);
+int APS5_VABI sceFontCreateString(const FontMemory*, FontTextSource*, const FontCreateStringDetail*, FontString*);
+int APS5_VABI sceFontDestroyString(FontString*);
+FontTextCharacter* APS5_VABI sceFontStringRefersTextCharacters(FontString, std::uint32_t*);
+int APS5_VABI sceFontWritingInit(FontWriting*, FontString, const FontTextCharacter*);
+const FontWritingStep* APS5_VABI sceFontWritingRefersRenderStep(FontWriting*);
 }
 
 static void Check(bool value, int line) {
@@ -124,6 +130,24 @@ static float AdvanceOf(FontHandle font, std::uint32_t code) {
     return metrics.Horizontal.advance;
 }
 
+struct ParsedText {
+    const char* text;
+    std::size_t at;
+    FontHandle font;
+};
+
+static std::int32_t APS5_VABI ParseText(FontTextSource* source, void** order, FontTextParseResult* result) {
+    auto* parsed = static_cast<ParsedText*>(source->textObject);
+    *order = const_cast<char*>(parsed->text + parsed->at);
+    if (parsed->text[parsed->at] == 0) {
+        result->Terminate.terminateCode = 0;
+        return 0;
+    }
+    result->FontCode.font = parsed->font;
+    result->FontCode.code = static_cast<unsigned char>(parsed->text[parsed->at++]);
+    return 1;
+}
+
 int main() {
     constexpr std::uint32_t EuropeanLight = 0x180700C3u;
     constexpr std::uint32_t EuropeanBold = 0x180700C7u;
@@ -180,6 +204,27 @@ int main() {
     FontHorizontalLayout layout{};
     Require(sceFontGetHorizontalLayout(font, &layout) == SCE_FONT_OK && layout.baselineOffset > 0.0f && layout.lineAdvance >= layout.baselineOffset);
 
+    ParsedText parsed{"AA", 0, font};
+    FontTextSource source{};
+    Require(sceFontTextSourceInit(&source, parsed.text, 2, ParseText, &parsed) == SCE_FONT_OK);
+    FontString string = nullptr;
+    Require(sceFontCreateString(&memory, &source, nullptr, &string) == SCE_FONT_OK && string != nullptr);
+    std::uint32_t characterCount = 0;
+    FontTextCharacter* characters = sceFontStringRefersTextCharacters(string, &characterCount);
+    Require(characters != nullptr && characterCount == 2);
+    FontWriting writing{};
+    Require(sceFontWritingInit(&writing, string, characters) == SCE_FONT_OK);
+    const FontWritingStep* step = sceFontWritingRefersRenderStep(&writing);
+    Require(step != nullptr);
+    const FontWritingStep first = *step;
+    step = sceFontWritingRefersRenderStep(&writing);
+    Require(step != nullptr);
+    const FontWritingStep second = *step;
+    Require(first.x == 0.0f && first.y == 0.0f && first.advanceX == 70.0f && first.advanceY == 0.0f);
+    Require(second.x == 70.0f && second.y == 0.0f && second.advanceX == 70.0f && second.advanceY == 0.0f);
+    Require(second.GlyphMetrics.Vertical.advance == 100.0f);
+    Require(sceFontDestroyString(&string) == SCE_FONT_OK);
+
     FontHandle again = nullptr;
     Require(sceFontOpenFontSet(library, EuropeanBold, 2, nullptr, &again) == SCE_FONT_OK && again != font);
     FontHandle instance = nullptr;

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6249479209 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5209#issuecomment-6249479209). Originally posted by [JAB-dev](https://github.com/JAB-dev) on 2026-10-18T11:40:38Z. --- Test that fails on current `main` and passes with this change, plus a measurement from real firmware. **Test** (added to `guest_system_font`, uses its square-glyph font at 100 px, whose glyph has horizontal advance 70 and vertical advance 100): writes the string "AA" through `sceFontWritingInit` and `sceFontWritingRefersRenderStep` and checks both steps. - `main`: fails at the first step check (`advanceY` is 100, the second step lands at `y` 100). - With this change: step 1 is `x 0, y 0, advanceX 70, advanceY 0`, step 2 is `x 70, y 0, advanceX 70, advanceY 0`, and the step's own glyph metrics still report the vertical advance 100. `guest_system_font`, `guest_font` and `guest_font_writing_line` pass (3/3), Windows 11, GCC 15.2. - It does not cover the glyph width/height part of this change. **Real firmware** (PS4 firmware 11.00 libSceFont loaded on Windows, not the PS5 library): the steps of "Ab c" come back `x=0 y=0 adv=22.4688,0`, `x=22.4688 y=0 adv=17.7969,0`, `x=40.2656 y=0 adv=8.89062,0`, `x=49.1562 y=0 adv=16,0`. `y` stays 0 and the second advance is 0 on every step, so horizontal text does not step vertically, which is what this change does. **Game**: Ape Academy 2 (PPSA11132, PSP Classics) on current `main`, Windows 11, RTX 4070 SUPER: the controller screen text ("Click the touch pad button...", "Press START to play!") has letters scattered down the screen. With this change in `libSceFont` it is on one line. <details><summary>Test diff</summary> ```diff diff --git a/core/libs/tests/GuestSystemFont.cpp b/core/libs/tests/GuestSystemFont.cpp index ec2f80785..d8fc39a43 100644 --- a/core/libs/tests/GuestSystemFont.cpp +++ b/core/libs/tests/GuestSystemFont.cpp @@ -25,6 +25,12 @@ 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); +int APS5_VABI sceFontTextSourceInit(FontTextSource*, const void*, std::uint32_t, FontTextParseFunction, void*); +int APS5_VABI sceFontCreateString(const FontMemory*, FontTextSource*, const FontCreateStringDetail*, FontString*); +int APS5_VABI sceFontDestroyString(FontString*); +FontTextCharacter* APS5_VABI sceFontStringRefersTextCharacters(FontString, std::uint32_t*); +int APS5_VABI sceFontWritingInit(FontWriting*, FontString, const FontTextCharacter*); +const FontWritingStep* APS5_VABI sceFontWritingRefersRenderStep(FontWriting*); } static void Check(bool value, int line) { @@ -124,6 +130,24 @@ static float AdvanceOf(FontHandle font, std::uint32_t code) { return metrics.Horizontal.advance; } +struct ParsedText { + const char* text; + std::size_t at; + FontHandle font; +}; + +static std::int32_t APS5_VABI ParseText(FontTextSource* source, void** order, FontTextParseResult* result) { + auto* parsed = static_cast<ParsedText*>(source->textObject); + *order = const_cast<char*>(parsed->text + parsed->at); + if (parsed->text[parsed->at] == 0) { + result->Terminate.terminateCode = 0; + return 0; + } + result->FontCode.font = parsed->font; + result->FontCode.code = static_cast<unsigned char>(parsed->text[parsed->at++]); + return 1; +} + int main() { constexpr std::uint32_t EuropeanLight = 0x180700C3u; constexpr std::uint32_t EuropeanBold = 0x180700C7u; @@ -180,6 +204,27 @@ int main() { FontHorizontalLayout layout{}; Require(sceFontGetHorizontalLayout(font, &layout) == SCE_FONT_OK && layout.baselineOffset > 0.0f && layout.lineAdvance >= layout.baselineOffset); + ParsedText parsed{"AA", 0, font}; + FontTextSource source{}; + Require(sceFontTextSourceInit(&source, parsed.text, 2, ParseText, &parsed) == SCE_FONT_OK); + FontString string = nullptr; + Require(sceFontCreateString(&memory, &source, nullptr, &string) == SCE_FONT_OK && string != nullptr); + std::uint32_t characterCount = 0; + FontTextCharacter* characters = sceFontStringRefersTextCharacters(string, &characterCount); + Require(characters != nullptr && characterCount == 2); + FontWriting writing{}; + Require(sceFontWritingInit(&writing, string, characters) == SCE_FONT_OK); + const FontWritingStep* step = sceFontWritingRefersRenderStep(&writing); + Require(step != nullptr); + const FontWritingStep first = *step; + step = sceFontWritingRefersRenderStep(&writing); + Require(step != nullptr); + const FontWritingStep second = *step; + Require(first.x == 0.0f && first.y == 0.0f && first.advanceX == 70.0f && first.advanceY == 0.0f); + Require(second.x == 70.0f && second.y == 0.0f && second.advanceX == 70.0f && second.advanceY == 0.0f); + Require(second.GlyphMetrics.Vertical.advance == 100.0f); + Require(sceFontDestroyString(&string) == SCE_FONT_OK); + FontHandle again = nullptr; Require(sceFontOpenFontSet(library, EuropeanBold, 2, nullptr, &again) == SCE_FONT_OK && again != font); FontHandle instance = nullptr; ``` </details> 🤖 Generated with [Claude Code](https://claude.com/claude-code)
JJenkx commented 2026-10-21 22:50:01 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by JJenkx on 2026-10-21T22:50:01Z.


Independent confirmation on a second Sony classics emulator: Syphon Filter (PPSA06323) 01.000.003, a PS1 classics title. Linux 7.2.9, Radeon RX 6650 XT, RADV Mesa 26.2.4, GCC Release.

Setup

  • Base: my Release build of main 38d3c0858a + #8847 (an unrelated AGC change).
  • Candidate: the same build with only libSceFont.prx, libSceFont-module.prx and libSceFont.native.prx replaced by a build of 38d3c0858a with this PR merged. It merges cleanly onto 38d3c0858a. Every other library is byte-identical, and the title is relinked against each set.
  • Both runs use the same input schedule and the Noto substitutes from USAGE.md through ANYPS5_SYSTEM_FONTS. The title opens system font set 0x1808B4D4, which takes NotoSansCJK-Regular.ttc with NotoSans-Regular.ttf as fallback. Without those files the title crashes 6 s in, which is the documented missing-fonts case.

Result at the emulator's controller-layout screen, 95 s into each run:

  • Base: only the first word of each string appears, one glyph per line going down: "Click" over the SELECT label, "Press" under the button bar. The rest is off the visible area.
  • This PR: "Click the touch pad button to use the SELECT button and START button" and "Press START to play!", each on one baseline with every glyph drawn.

Before/after crop: 20261021-pr5209-PPSA06323-before-after.png

Both runs hold 59.94 FPS for 183 s with no driver stop. I scripted only Cross, so neither went past the screen, which asks for START; this checks the text layout only. The PR conflicts with current main but applied cleanly at 38d3c0858a. Candidate library sha256 prefixes: libSceFont.prx 28c90aaa121e1489, libSceFont-module.prx 6830d8a9b95af65a.

AI-assisted testing and report: yes, Claude Code.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6328287902 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5209#issuecomment-6328287902). Originally posted by [JJenkx](https://github.com/JJenkx) on 2026-10-21T22:50:01Z. --- Independent confirmation on a second Sony classics emulator: **Syphon Filter (PPSA06323) 01.000.003**, a PS1 classics title. Linux 7.2.9, Radeon RX 6650 XT, RADV Mesa 26.2.4, GCC Release. **Setup** - Base: my Release build of `main` `38d3c0858a` + #8847 (an unrelated AGC change). - Candidate: the same build with only `libSceFont.prx`, `libSceFont-module.prx` and `libSceFont.native.prx` replaced by a build of `38d3c0858a` with this PR merged. It merges cleanly onto `38d3c0858a`. Every other library is byte-identical, and the title is relinked against each set. - Both runs use the same input schedule and the Noto substitutes from USAGE.md through `ANYPS5_SYSTEM_FONTS`. The title opens system font set `0x1808B4D4`, which takes `NotoSansCJK-Regular.ttc` with `NotoSans-Regular.ttf` as fallback. Without those files the title crashes 6 s in, which is the documented missing-fonts case. **Result** at the emulator's controller-layout screen, 95 s into each run: - Base: only the first word of each string appears, one glyph per line going down: "Click" over the SELECT label, "Press" under the button bar. The rest is off the visible area. - This PR: "Click the touch pad button to use the SELECT button and START button" and "Press START to play!", each on one baseline with every glyph drawn. Before/after crop: [20261021-pr5209-PPSA06323-before-after.png](https://github.com/JJenkx/AnyPS5/releases/download/game-library-checks-20261016/20261021-pr5209-PPSA06323-before-after.png) Both runs hold 59.94 FPS for 183 s with no driver stop. I scripted only Cross, so neither went past the screen, which asks for START; this checks the text layout only. The PR conflicts with current `main` but applied cleanly at `38d3c0858a`. Candidate library sha256 prefixes: `libSceFont.prx` `28c90aaa121e1489`, `libSceFont-module.prx` `6830d8a9b95af65a`. AI-assisted testing and report: yes, Claude Code.
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!5209
No description provided.