feat(libfreetype): implement the color palette and layer functions #15468

Merged
VakzOs merged 8 commits from upstream-pr/15468 into main 2026-11-07 05:38:34 +00:00
VakzOs commented 2026-11-03 13:37:14 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by VakzOs on 2026-11-03T13:37:14Z.


What

The libfreetype exports +AF9KH7aZbc, qCyybpSyZzI and ypZKJwPSVgQ threw. The verified NID name database names them FT_Palette_Data_Get, FT_Palette_Select and FT_Get_Color_Glyph_Layer, and tools/nid_names.py computes the same NIDs for those names. They are now implemented in src/Color.cpp.

All three resolve the host FT_Face behind the guest FT_FaceRec from libSceFreeType and call the bundled FreeType 2.15.0, so their results are FreeType's own. FT_Palette_Data, FT_Color and FT_LayerIterator hold only FT_UShort, FT_UInt, bytes and pointers, so their console layouts match on Linux and Windows. The guest structs are copied field by field, and static_asserts pin their sizes:

  • FT_Palette_Data_Get copies the palette data on success. Its arrays point into the host face, as in FreeType.
  • FT_Palette_Select returns the host face's palette array, read as the guest's 4-byte BGRA colours.
  • FT_Get_Color_Glyph_Layer runs FreeType's iterator on a native copy and writes the iterator state back after every call.

docs/dev/debt/libfreetype.md moves the three names from the callable-export placeholders to a functional entry.

Tested

Linux, GCC 13, Release: the new guest_legacy_freetype_color passes. No colour font ships with the FreeType submodule, so the test embeds a 588-byte font generated with fontTools (script below): four empty glyphs, a COLRv0 table where glyph 1 has layers 2 and 3, and a CPALv1 table with two palettes of two colours plus palette types and labels. The test opens it through the guest API and native FreeType and compares:

  • palette data fields and arrays;
  • palettes 0, 1 and 2 (out of range), with and without an output pointer;
  • the full layer iteration for base glyphs 0, 1, 2 and 4 (out of range), including the iterator state after each call;
  • the expected red first colour and the two layers of glyph 1;
  • the null-face, null-output and null-iterator results.

Not copying the iterator back, or swapping two palette data fields, makes the test fail. The patched module exports the three NIDs and no FT_* symbols. The test uses only public FreeType headers and an embedded font, so it also builds with ANYPS5_USE_SYSTEM_DEPS. All 53 guest_freetype* and guest_legacy_freetype* tests pass, and python3 tools/check_conventions.py --base origin/main passes.

How the test font was made

The 588-byte ColorFont array is generated with fontTools 4.53.1 by the script below. The head timestamps are pinned, so it reproduces the array byte for byte (checked with cmp); xxd -i color.ttf gives the array. SHA-256: ac87d356df87ef60bef614c6a9c4e33500f02671846d87480cc0617bc21e8160.

python3 -m pip install fonttools==4.53.1
python3 make_color_font.py color.ttf
import sys

from fontTools.colorLib.builder import buildCOLR, buildCPAL
from fontTools.fontBuilder import FontBuilder
from fontTools.pens.ttGlyphPen import TTGlyphPen

glyphs = [".notdef", "A", "L1", "L2"]
builder = FontBuilder(1000, isTTF=True)
builder.setupGlyphOrder(glyphs)
builder.setupCharacterMap({0x41: "A"})
empty = TTGlyphPen(None).glyph()
builder.setupGlyf({name: empty for name in glyphs})
builder.setupHorizontalMetrics({name: (500, 0) for name in glyphs})
builder.setupHorizontalHeader(ascent=800, descent=-200)
builder.setupMaxp()
builder.setupNameTable({"familyName": "C", "styleName": "R"})
font = builder.font
font["COLR"] = buildCOLR({"A": [("L1", 0), ("L2", 1)]})
font["CPAL"] = buildCPAL(
    [[(1, 0, 0, 1), (0, 0.5, 1, 1)], [(0, 1, 0, 0.5), (1, 1, 1, 1)]],
    paletteTypes=[1, 2],
    paletteLabels=["P0", "P1"],
    paletteEntryLabels=["E0", "E1"],
    nameTable=font["name"],
)
font["head"].created = font["head"].modified = 3876556789
font.recalcTimestamp = False
font.save(sys.argv[1])

Side note: FreeType 2.15.0 reads CPALv1's 32-bit palette types as 16-bit values, so palette_flags comes back as {0, 1} for types {1, 2}. The wrapper returns what FreeType returns, and the test only compares with native FreeType there.

tools/pr_overlap.py --branch reports the same PRs as #15447: #15408 (shared new CMakeLists.txt and adjacent lines) and #10606 and #10609 (GuestObjects.hpp, which merges cleanly with both; they already conflict with main). No export is shared.

Not tested on hardware or with a title.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: #15439, #15442, #15444, #15447 (land order: #15408, #15439, #15442, #15444, #15447, then this PR)
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5138175087 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/15468). Originally posted by [VakzOs](https://github.com/VakzOs) on 2026-11-03T13:37:14Z. --- ### What The `libfreetype` exports `+AF9KH7aZbc`, `qCyybpSyZzI` and `ypZKJwPSVgQ` threw. The [verified NID name database](https://github.com/zecoxao/sce_symbols/blob/2883963a0a514ba6e77407a08ef4730a65e88254/aerolib.csv) names them `FT_Palette_Data_Get`, `FT_Palette_Select` and `FT_Get_Color_Glyph_Layer`, and `tools/nid_names.py` computes the same NIDs for those names. They are now implemented in `src/Color.cpp`. All three resolve the host `FT_Face` behind the guest `FT_FaceRec` from libSceFreeType and call the bundled FreeType 2.15.0, so their results are FreeType's own. `FT_Palette_Data`, `FT_Color` and `FT_LayerIterator` hold only `FT_UShort`, `FT_UInt`, bytes and pointers, so their console layouts match on Linux and Windows. The guest structs are copied field by field, and `static_assert`s pin their sizes: - `FT_Palette_Data_Get` copies the palette data on success. Its arrays point into the host face, as in FreeType. - `FT_Palette_Select` returns the host face's palette array, read as the guest's 4-byte BGRA colours. - `FT_Get_Color_Glyph_Layer` runs FreeType's iterator on a native copy and writes the iterator state back after every call. `docs/dev/debt/libfreetype.md` moves the three names from the callable-export placeholders to a functional entry. ### Tested Linux, GCC 13, Release: the new `guest_legacy_freetype_color` passes. No colour font ships with the FreeType submodule, so the test embeds a 588-byte font generated with fontTools (script below): four empty glyphs, a COLRv0 table where glyph 1 has layers 2 and 3, and a CPALv1 table with two palettes of two colours plus palette types and labels. The test opens it through the guest API and native FreeType and compares: - palette data fields and arrays; - palettes 0, 1 and 2 (out of range), with and without an output pointer; - the full layer iteration for base glyphs 0, 1, 2 and 4 (out of range), including the iterator state after each call; - the expected red first colour and the two layers of glyph 1; - the null-face, null-output and null-iterator results. Not copying the iterator back, or swapping two palette data fields, makes the test fail. The patched module exports the three NIDs and no `FT_*` symbols. The test uses only public FreeType headers and an embedded font, so it also builds with `ANYPS5_USE_SYSTEM_DEPS`. All 53 `guest_freetype*` and `guest_legacy_freetype*` tests pass, and `python3 tools/check_conventions.py --base origin/main` passes. <details><summary>How the test font was made</summary> The 588-byte `ColorFont` array is generated with fontTools 4.53.1 by the script below. The `head` timestamps are pinned, so it reproduces the array byte for byte (checked with `cmp`); `xxd -i color.ttf` gives the array. SHA-256: `ac87d356df87ef60bef614c6a9c4e33500f02671846d87480cc0617bc21e8160`. ```sh python3 -m pip install fonttools==4.53.1 python3 make_color_font.py color.ttf ``` ```python import sys from fontTools.colorLib.builder import buildCOLR, buildCPAL from fontTools.fontBuilder import FontBuilder from fontTools.pens.ttGlyphPen import TTGlyphPen glyphs = [".notdef", "A", "L1", "L2"] builder = FontBuilder(1000, isTTF=True) builder.setupGlyphOrder(glyphs) builder.setupCharacterMap({0x41: "A"}) empty = TTGlyphPen(None).glyph() builder.setupGlyf({name: empty for name in glyphs}) builder.setupHorizontalMetrics({name: (500, 0) for name in glyphs}) builder.setupHorizontalHeader(ascent=800, descent=-200) builder.setupMaxp() builder.setupNameTable({"familyName": "C", "styleName": "R"}) font = builder.font font["COLR"] = buildCOLR({"A": [("L1", 0), ("L2", 1)]}) font["CPAL"] = buildCPAL( [[(1, 0, 0, 1), (0, 0.5, 1, 1)], [(0, 1, 0, 0.5), (1, 1, 1, 1)]], paletteTypes=[1, 2], paletteLabels=["P0", "P1"], paletteEntryLabels=["E0", "E1"], nameTable=font["name"], ) font["head"].created = font["head"].modified = 3876556789 font.recalcTimestamp = False font.save(sys.argv[1]) ``` </details> Side note: FreeType 2.15.0 reads CPALv1's 32-bit palette types as 16-bit values, so `palette_flags` comes back as `{0, 1}` for types `{1, 2}`. The wrapper returns what FreeType returns, and the test only compares with native FreeType there. `tools/pr_overlap.py --branch` reports the same PRs as #15447: #15408 (shared new `CMakeLists.txt` and adjacent lines) and #10606 and #10609 (`GuestObjects.hpp`, which merges cleanly with both; they already conflict with `main`). No export is shared. Not tested on hardware or with a title. ### Checklist - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: #15439, #15442, #15444, #15447 (land order: #15408, #15439, #15442, #15444, #15447, then this PR) - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
ahmadmuj commented 2026-11-06 08:05:41 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by ahmadmuj on 2026-11-06T08:05:41Z.

Review: COMMENTED


Checked head afd33dbd38206891177ec324e4993a5ff7a1597c, base 4f41f4c375f5abb487dbd184fbc150f31994d762.

No issue found in the three palette/layer functions. I built the production modules on Linux with pinned FreeType 37515cf56ba3349656049edbed89ffc6f6466f2d. The existing color test and a separate ownership probe pass, including after NID patching. Loader traces confirm the palette imports bind to libfreetype.

The probe loads generated CPALv0, types-only CPALv1, and labels-only CPALv1 fonts through the guest API. It checks fixed BGRA bytes, absent arrays, invalid-output canaries, and foreground layer index 0xffff. A write through face A's returned palette reaches its real TT_Face storage. Face B stays unchanged, including after A closes. Switching A from 0 to 1 and back to 0 restores the original bytes.

Three deliberate mutations fail the intended assertions: detached palette storage, shared palette storage, and omitted null-field writes. Conventions also pass.

The source checks use pinned ftcolor.c and ttcpal.c. This uses an isolated CMake parent with the production module targets, not a full project build. Unused guest libc imports remain unresolved. No Windows, renderer, title, or console behavior is checked.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5797376446 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/15468#pullrequestreview-5797376446). Originally posted by [ahmadmuj](https://github.com/ahmadmuj) on 2026-11-06T08:05:41Z. **Review:** COMMENTED --- Checked head `afd33dbd38206891177ec324e4993a5ff7a1597c`, base `4f41f4c375f5abb487dbd184fbc150f31994d762`. No issue found in the three palette/layer functions. I built the production modules on Linux with pinned FreeType `37515cf56ba3349656049edbed89ffc6f6466f2d`. The existing color test and a separate ownership probe pass, including after NID patching. Loader traces confirm the palette imports bind to libfreetype. The probe loads generated CPALv0, types-only CPALv1, and labels-only CPALv1 fonts through the guest API. It checks fixed BGRA bytes, absent arrays, invalid-output canaries, and foreground layer index `0xffff`. A write through face A's returned palette reaches its real `TT_Face` storage. Face B stays unchanged, including after A closes. Switching A from 0 to 1 and back to 0 restores the original bytes. Three deliberate mutations fail the intended assertions: detached palette storage, shared palette storage, and omitted null-field writes. Conventions also pass. The source checks use pinned [ftcolor.c](https://github.com/freetype/freetype/blob/37515cf56ba3349656049edbed89ffc6f6466f2d/src/base/ftcolor.c) and [ttcpal.c](https://github.com/freetype/freetype/blob/37515cf56ba3349656049edbed89ffc6f6466f2d/src/sfnt/ttcpal.c). This uses an isolated CMake parent with the production module targets, not a full project build. Unused guest libc imports remain unresolved. No Windows, renderer, title, or console behavior is checked.
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!15468
No description provided.