libFont: fix writing-line advances and character iteration #2075
No reviewers
Labels
No labels
bug
documentation
duplicate
enhancement
GPU
GUI
help wanted
question
Shaders
Video
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/KytyPS5--KytyPS5!2075
Loading…
Reference in a new issue
No description provided.
Delete branch "gh-pr/2075/head"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
@gallexme on GitHub · 2026-11-02 18:12 UTC · original
gallexme:fix/scefont-writing-metrics→main· +69 −57 · 1 files · 2 commits · merged 2026-11-03 18:28 UTCProblem
Text laid out through the
sceFontWriting*APIs was not positioned correctly.fill_writing_metrics()and the writing-step helpers hardcoded an 8 px advance regardless of the font scale,FontWritingRefersRenderStep()never moved to the next character, andFontStringRefersRenderCharacters()counted character ranges exclusive of their last character.This is visible in the built-in PS1 emulator menu of TEKKEN™ 3 / PPSA31291:
EmptySav,PlayTim,FileSiz,Bac);Fix
Only
src/libs/libFont.cppis changed:step.x/y,positioning.x/y, andletter_stepadvances, then accumulate the line metrics;FontWritingRefersRenderStep()and return the current character fromFontWritingRefersRenderStepCharacter();FontStringRefersRenderCharacters()ranges.Testing
Built
kyty_emulatorand ran the TEKKEN™ 3 / PPSA31291 save menu. Slot labels, dates, play time, file size and button prompts now render as readable text (Slot,EmptySave,PlayTime,FileSize,5.05M,Back).There are no existing unit tests for
libFont; I did not add one because the behavior is driven by guest font/writing handles. Happy to add a focused test if desired.before/after
almost forgot a before and after
@coderabbitai[bot] on GitHub · 2026-11-02 18:18 UTC · original · edited 2026-11-03 18:27 UTC
Walkthrough
Font writing now uses glyph advances and font metrics when available, with fallback metrics otherwise. The writing state tracks iteration, pen position, and accumulated extents. Render-step retrieval advances through valid characters. Character retrieval returns the current character once. Font operations track active fonts and validate registered font and string handles.
Suggested reviewers:
nmzik,mrbigbritchesChange: Bug fix
Merge Risk:
Low · previous review
9ac34Empty text and some returned character metrics may behave incorrectly, while reusing a destroyed handle can cause a failure. These are bounded issues, but the handle deletion guard should be fixed before merging.
Pre-merge checks:
4 ·
1
Full details: Docstring Coverage
Explanation
Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 19 functions across 1 files.
Resolution
Write docstrings for the functions missing them to satisfy the coverage threshold.
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
Share
CodeRabbit Verify
502189a5-b205-495b-9ae4-8877ef7bb043Reviewing files that changed from the base of the PR and between 0cd517a54e451d438dab1c264f7c2e59997da06a and 9ac34c041f067e8aa5a63575c0f4061215b25e44.
src/libs/libFont.cppIncluded review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 8 remain after this review.
@coderabbitai[bot] on GitHub · 2026-11-02 18:18 UTC · original · edited 2026-11-02 18:29 UTC
On
src/libs/libFont.cppline 411 (outdated):Fix the dangling
g_active_fontpointer. It can cause a use-after-free.g_active_fontstores a rawFontState*.FontCloseFont(Lines 1289-1290) deletes the font and does not clear this pointer. The pointer isthread_local, so a close on one thread cannot clear the copy on another thread.Trigger: the guest touches a font, for example with
FontSetScalePixel,FontBindRenderer, orFontCreateString. The guest then callsFontCloseFont. After that, the guest callsFontWritingLineWritesOrderwithwriting_metrics == nullptr. This reachesfill_writing_metrics(&order_metrics, 1, g_active_font).That call reads
font->scale_h. It also callsfill_metrics_for_code, which callsensure_stb_fontand can write to the freed object throughinit_font_face.Before this change, this path used fixed metrics and never dereferenced a font. Guests commonly close a font after use and then keep building writing lines.
Fix: track live fonts in a mutex-protected registry. Validate
g_active_fontagainst the registry before each use. Remove the font from the registry inFontCloseFont. Alternatively, store a global atomic pointer and clear it inFontCloseFontwhen it matches. Then fall back tonullptrmetrics.Sketch of a fix
Register the font in
open_font. Erase it inFontCloseFontbeforedelete. Useget_live_active_font()at Line 2058.This check must run for the whole use of the font, not only at the lookup. If fonts can close concurrently with use, hold the lock during the
fill_writing_metricscall.Also applies to: 2058-2058
✅ Confirmed as addressed by @gallexme
@coderabbitai[bot] on GitHub · 2026-11-02 18:18 UTC · original · reviewed
@gallexme on GitHub · 2026-11-02 18:21 UTC · original
On
src/libs/libFont.cppline 411 (outdated):Addressed: opened fonts are now tracked in a mutex-protected registry.
FontCloseFonterases the handle before deleting it, and theg_active_fontfallback validates the pointer against the registry while holding the same lock, falling back to the fixed metrics if the font was closed.@coderabbitai[bot] on GitHub · 2026-11-02 18:26 UTC · original · edited 2026-11-02 18:50 UTC
On
src/libs/libFont.cppline 2077 (outdated):Use a nonzero fallback when no font is live.
A zero-advance glyph sets
g_last_advanceto zero. If the guest then closes the active font, this call uses that zero value for every order without supplied metrics. Those orders overlap at the same position. Use a fixed fallback advance whenfontis null.✅ Confirmed as addressed by @gallexme
@coderabbitai[bot] on GitHub · 2026-11-02 18:27 UTC · original · reviewed
@coderabbitai[bot] on GitHub · 2026-11-02 18:29 UTC · original
On
src/libs/libFont.cppline 411 (outdated):@gallexme The supplied code addresses this finding.
FontCloseFontremoves the font and deletes it underg_font_mutex.FontWritingLineWritesOrderholds the same mutex through registry validation andfill_writing_metrics, which prevents concurrent deletion during that call. If the font is no longer registered, the call usesnullptrfallback metrics.If you found this review helpful, would you consider giving us a shout-out on X?
Thank you for using CodeRabbit!
Review thread resolved.
You are interacting with an AI system.
@gallexme on GitHub · 2026-11-02 18:43 UTC · original
On
src/libs/libFont.cppline 2077 (outdated):Good catch. The null-font fallback now uses
g_last_advance > 0 ? g_last_advance : 8.0f, so a zero-width glyph (or a font closed right after one) cannot collapse subsequent orders onto the same position. Rebuilt and pushed.@coderabbitai[bot] on GitHub · 2026-11-02 18:49 UTC · original · edited 2026-11-02 19:20 UTC
On
src/libs/libFont.cppline 619 (outdated):Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 42468
Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 22061
Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 10276
Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 4004
Use conservative font bounds for the null-metrics fallback.
When
writing_metricsis null,FontWritingLineWritesOrderuses-0.75 * scale_hand0.25 * scale_hfor the vertical extent. A valid TTF glyph can exceed these limits. A surface sized from these metrics can clip glyphs. Use scaledstbtt_GetFontBoundingBoxbounds when per-order glyph bounds are unavailable, while retaining the bitmap fallback for fonts without TTF data.✅ Confirmed as addressed by @gallexme
@coderabbitai[bot] on GitHub · 2026-11-02 18:49 UTC · original · reviewed
@coderabbitai[bot] on GitHub · 2026-11-02 18:50 UTC · original
On
src/libs/libFont.cppline 2077 (outdated):@gallexme Thanks. The supplied
fill_writing_metrics()code confirms the positive-value guard. When no font is live andg_last_advanceis zero, the fallback uses8.0f. This addresses the reported overlap case.If you found this review helpful, would you consider giving us a shout-out on X?
Thank you for using CodeRabbit!
Review thread resolved.
You are interacting with an AI system.
@gallexme on GitHub · 2026-11-02 19:04 UTC · original
On
src/libs/libFont.cppline 619 (outdated):Updated. When a TTF is available the null-metrics fallback now derives ascent/descent from scaled
stbtt_GetFontBoundingBoxinstead of the fixed 0.75/0.25 scale factors; bitmap-only fonts keep the previous fallback. Rebuilt and pushed.@coderabbitai[bot] on GitHub · 2026-11-02 19:18 UTC · original · reviewed
e87f0604-bcb1-4666-b665-ecc7d7e32c58Reviewing files that changed from the base of the PR and between 33a41820a27def183c756cd206352d78d94fbd26 and 0cd517a54e451d438dab1c264f7c2e59997da06a.
src/libs/libFont.cppIncluded review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 7 remain after this review.
@coderabbitai[bot] on GitHub · 2026-11-02 19:20 UTC · original
On
src/libs/libFont.cppline 619 (outdated):@gallexme Confirmed in the supplied
fill_writing_metrics()code: TTF ascent and descent now use scaledstbtt_GetFontBoundingBoxbounds. Bitmap-only fonts retain the previous fallback. This addresses the clipping concern.If you found this review helpful, would you consider giving us a shout-out on X?
Thank you for using CodeRabbit!
Review thread resolved.
You are interacting with an AI system.
@gallexme on GitHub · 2026-11-02 19:24 UTC · original
Addressed the review note about substituting 'M' for single-character orders: when a null-metrics order covers one character,
fill_writing_metrics()now keeps the advance recorded by the preceding writing walk (g_last_advance) instead of using 'M'. Multi-character orders still use the representative advance. Rebuilt and pushed.@gallexme on GitHub · 2026-11-02 19:51 UTC · original
Self-review pass completed. In addition to the earlier fixes, the branch now:
lastCharacterrange;The null-
writing_metricspath still uses the last measured advance as a compatibility fallback; this is only reached when the guest omits metrics and avoids collapsing/overlapping orders in the title being tested.@gallexme on GitHub · 2026-11-02 19:51 UTC · original
@coderabbitai review
@coderabbitai[bot] on GitHub · 2026-11-02 19:51 UTC · original · edited 2026-11-02 19:58 UTC
Action performed
Review finished.
@gallexme on GitHub · 2026-11-02 19:56 UTC · original
Verified on the latest commit (
9ac34c04) against the TEKKEN™ 3 / PPSA31291 save menu. Slot names, dates, play time, file size and the button prompts all render correctly:Slot 1,Slot 4,Empty Save,02/11/2026 17:46,Date,Play Time,File Size,4.92 MB,Accept,Back,Delete.@coderabbitai[bot] on GitHub · 2026-11-02 19:58 UTC · original · edited 2026-11-02 20:14 UTC
On
src/libs/libFont.cppline 1725 (outdated):Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 2467
Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 19105
Guard both deletes with successful registry removal.
FontDestroyStringclears only the handle slot passed to it, so a copied handle remains nonnull.FontCloseFonttakes its handle by value and clears no caller slot. A second call with either stale handle can delete an unregistered object. Delete only when the correspondingerasesucceeds.Suggested fix
Committable suggestion
✅ Confirmed as addressed by @gallexme
@coderabbitai[bot] on GitHub · 2026-11-02 19:58 UTC · original · edited 2026-11-02 20:13 UTC
On
src/libs/libFont.cppline 1965 (outdated):Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 18137
Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 8076
Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 3944
Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 4251
Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 15229
Accept registered empty strings in
FontWritingInit.FontTextSourceInitcan produce an empty text range, andFontCreateStringregisters the resulting string.populate_writing_step(state, 0)then fails because the string has no characters, soFontWritingInitreturns-1. Keep returning-1for unregistered strings.Suggested fix
Committable suggestion
✅ Confirmed as addressed by @gallexme
@coderabbitai[bot] on GitHub · 2026-11-02 19:58 UTC · original · edited 2026-11-02 20:13 UTC
On
src/libs/libFont.cppline 2057 (outdated):FontWritingRefersRenderStepCharacterdiscards the letter-step fields thatfill_writing_letter_stepsets.populate_writing_stepalready fillsstate->letter_stepthroughfill_writing_letter_step. That function setsbase_components_count,base_focus_count, andbase_text_countto 1. It also setsglyphs_indexto 0.Line 2047 zeroes the struct. The code then rebuilds only part of it. The three
base_*counts are lost, andglyphs_indexbecomescurrent_index. The same struct now has two different definitions, depending on whether the guest calls this function.A guest that reads
letter_step->components.base_components_countorbase_text_countgets 0 instead of 1. Do not zero the struct. Overwrite onlyx,y, and the advances, and keep one definition for the index fields.Proposed fix
Committable suggestion
✅ Confirmed as addressed by @gallexme
@coderabbitai[bot] on GitHub · 2026-11-02 19:58 UTC · original · reviewed
@gallexme on GitHub · 2026-11-02 20:13 UTC · original
On
src/libs/libFont.cppline 1725 (outdated):Fixed in
3e211335.@gallexme on GitHub · 2026-11-02 20:13 UTC · original
On
src/libs/libFont.cppline 1965 (outdated):Fixed in
3e211335.@gallexme on GitHub · 2026-11-02 20:13 UTC · original
On
src/libs/libFont.cppline 2057 (outdated):Fixed in
3e211335.@gallexme on GitHub · 2026-11-02 20:13 UTC · original
Addressed the latest three review notes in
3e211335:FontWritingRefersRenderStepCharacter.Build passes and the save menu still renders correctly.