Skip to content

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

Merged
adriadam10 merged 2 commits into
boykopovar:mainfrom
Misjudgments:fix/psp-classics-general
Oct 29, 2026
Merged

adriadam10 merged 2 commits into
boykopovar:mainfrom
Misjudgments:fix/psp-classics-general

Conversation

@Misjudgments

Copy link
Copy Markdown
Contributor

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

@adriadam10 adriadam10 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

JAB-dev commented Oct 18, 2026

Copy link
Copy Markdown
Contributor

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

@github-actions

github-actions Bot commented Oct 21, 2026 •

Copy link
Copy Markdown

Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.

Pull request Shared files not compared
PR 8250 docs/dev/debt/libSceFont.md

@github-actions github-actions Bot added the needs rebase Conflicts with main, rebase needed label Oct 21, 2026
@JJenkx

JJenkx commented Oct 21, 2026

Copy link
Copy Markdown
Contributor

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 + fix(agc): resolve and render a multisampled target through its sRGB twin format #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.

@klosixgaming-arch

Copy link
Copy Markdown
Contributor

Test run (not a code review). I tried to merge this PR onto current main (49d1f9b) to test it with Tekken Dark Resurrection, and the merge conflicts in docs/dev/debt/libSceFont.md (the rest of the files auto-merged). It needs a rebase before it can be tested, so I have no results for it.

@XatiX

XatiX commented Oct 26, 2026

Copy link
Copy Markdown
Contributor

This also fixes a PS1 classic: Ridge Racer (PPSA31372 01.000.002), which runs in Sony's PS1HD emulator.

Setup

  • Windows 11 (build 26300), RTX 5080 with driver 617.42.
  • main 74acfad65, with system fonts from the Noto substitutes in anyps5-fonts/. Font set 0x1808B4D4 opens NotoSansCJK-Regular.ttc with NotoSans-Regular.ttf as fallback.

On main. The emulator's controller guide lays its text out one character per line, running diagonally down the screen. Temporary logging in sceFontWritingRefersRenderStep showed the pen moving down by the font size on every character:

step idx=0 code=0x50 x=0.00  y=0.00  adv=20.25,32.00
step idx=1 code=0x72 x=20.25 y=32.00 adv=12.42,32.00
step idx=2 code=0x65 x=32.67 y=64.00 adv=17.73,32.00

The step's advanceY is the glyph's vertical advance, 32 at sceFontSetScalePixel(32, 32). Since a44e709 ("report vertical glyph metrics"), that is no longer zero, so every horizontal step also moves the pen down.

With this PR merged on main 74acfad65. The text is on one baseline and complete: "Click the touch pad button to use the SELECT button and START button" and "Press START to play!". The emulator's menu (Rewind / Load & Save / Settings) is also correct. The title goes on to its loading screen, menu and a race at 600 presents per 10 s.

Merge conflict. The PR now conflicts with main, but only in docs/dev/debt/libSceFont.md:

  • main rewrote the "Font writing lines" entry and added others since.
  • Keeping main's text and adding this PR's "Font writing steps" entry resolves it.
  • Text.cpp merges cleanly.

@klosixgaming-arch

Copy link
Copy Markdown
Contributor

Test run (not a code review). Tekken Dark Resurrection (PPSA31371, PSPHD), Windows 11, RTX 5090, Release build.

This PR still conflicts with current main (74acfad65b and later) in docs/dev/debt/libSceFont.md only. To test it I merged it locally with -X theirs, so the docs conflict was resolved in the PR's favour and the code merged without conflicts. This says nothing about the rebase itself.

  • Builds, and the game runs for the full 112 s with no new errors in the log.
  • A screenshot at about 100 s shows the in-game HUD text on single lines ("DAMAGE 20 (100%)", the "DEVIL JIN" name plate). On plain main the same text was laid out on the wrong lines for me earlier.
  • Caveat: my baseline main screenshot at 100 s is a different scene (the intro video), because the two runs ran at different speeds, so I have no same-scene before/after of the HUD. This is consistent with the fix but not a controlled comparison.
  • Unrelated to this PR: character meshes still show shattered triangles.

It needs a rebase before it can merge.

Misjudgments and others added 2 commits October 28, 2026 12:50
Horizontal writing steps 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 reports the real glyph width/height.
Test adapted from JAB-dev's comment on boykopovar#5209.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@adriadam10
adriadam10 force-pushed the fix/psp-classics-general branch from 6912742 to 35f8545 Compare October 28, 2026 11:54
@github-actions github-actions Bot removed the needs rebase Conflicts with main, rebase needed label Oct 28, 2026
@adriadam10
adriadam10 merged commit 14b3441 into boykopovar:main Oct 29, 2026
10 checks passed
@adriadam10

Copy link
Copy Markdown
Collaborator

Thanks for the fix, and to everyone who tested it. Since there was no push after the last review, I added the missing pieces on your branch:

  • Rebased onto main (conflict only in docs/dev/debt/libSceFont.md).
  • Added the Linux test in guest_system_font (based on the one JAB-dev posted): writes "AA" through sceFontWritingInit / sceFontWritingRefersRenderStep, pins y == 0, advanceY == 0, the second step at x == 70, GlyphMetrics.width/height equal to the glyph's own 50/70 on both steps (first step at pen (0, 0) included) and the vertical advance (100) kept in the step's metrics.
  • Reworked the debt entry: zeroing follows the PS4 11.00 firmware steps JAB-dev measured (y stays 0, advanceY 0), not shadPS4's FillWritingMetricsFromGlyph; the vertical advance value itself is still unconfirmed and the entry says so. Width/height of the step are not measured on firmware.

Checked on top of that: the drift came from advancing the pen, not from the vertical advance value; confirmed on several titles by @JAB-dev, @JJenkx, @XatiX and @klosixgaming-arch; merged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants