feat(libcairo): implement FreeType font-face lifetime APIs #16892

Closed
hyvvs wants to merge 6 commits from upstream-pr/16892 into main
hyvvs commented 2026-11-07 20:52:33 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by hyvvs on 2026-11-07T20:52:33Z.


What

Implement five Cairo FreeType/font-face exports:

  • cairo_ft_font_face_create_for_ft_face retains the guest FreeType face with FT_Reference_Face_nid_postfix and releases it when the final Cairo font-face reference is destroyed.
  • cairo_font_face_reference and cairo_font_face_destroy provide owned, reference-counted font-face lifetime.
  • cairo_font_face_set_user_data and cairo_font_face_get_user_data implement pointer-identity keys, replacement/removal, guest destroy callbacks, finalization order and reentrant-finalizer status.

The implementation uses the repository's pinned FreeType submodule at 37515cf56ba3349656049edbed89ffc6f6466f2d.

cairo_ft_font_options_substitute is intentionally excluded from this change. Its existing throwing stub is retained here; PR #16895 owns the functional Fontconfig option-substitution implementation.

Lifetime constraint

The Cairo font face retains its FT_Face, but the caller must keep the owning FT_Library alive until the Cairo font face is destroyed. This PR does not claim scaled-font creation or glyph rasterization.

Tested

  • Earlier standalone font-face Windows Release targets built successfully. After merging upstream Cairo regions, the four focused Windows targets built on commit 59849abb57.
  • Windows MinGW GCC 15.2 Release after region integration: focused CTest matrix, font options, font face, and regions passed 4/4 on exact commit 59849abb57.
  • WSL Ubuntu GCC 13.3 Debug with ASan and UBSan (both address and undefined sanitizers enabled): on exact regions-integrated merge commit 59849abb57, four focused executables built, and CTest matrix, font options, font face, and regions passed 4/4 with ASAN_OPTIONS=detect_leaks=1 and UBSAN_OPTIONS=halt_on_error=1. The earlier standalone head had also passed four focused tests, including surface.
  • python -I tools/check_conventions.py --base upstream/main --head HEAD passed.
  • git diff --check passed.
  • Reconciled with upstream main d03beaf110, retaining both FreeType font-face and six newly merged region exports; on that head, conventions and whitespace checks passed. Six files in the new PR-specific diff.

The original overlap scan covered 1,758 open PRs at initial publication. It found 13 shared-file relationships and no same-export claims. Two comparisons failed closed because #14212 and #15681 conflict with the base; I manually reviewed the relevant libcairo hunks. #14212's callable-export wording matches this branch, while #15681's shared CMake/export/inventory/debt files implement distinct context/pattern APIs and remain an integration dependency. #16895 now shares only the debt document; the duplicate font-options implementation has been removed from this branch.

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: #15208, #15315, #15317, #15324, #15430, #15675, #15679, #15681, #15685 (the earlier #14329 and #16315 dependencies have merged)
  • Fixes: none
  • Unblocks: none (supports a separate scaled-font follow-up)
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5205740206 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/16892). Originally posted by [hyvvs](https://github.com/hyvvs) on 2026-11-07T20:52:33Z. --- ### What Implement five Cairo FreeType/font-face exports: - `cairo_ft_font_face_create_for_ft_face` retains the guest FreeType face with `FT_Reference_Face_nid_postfix` and releases it when the final Cairo font-face reference is destroyed. - `cairo_font_face_reference` and `cairo_font_face_destroy` provide owned, reference-counted font-face lifetime. - `cairo_font_face_set_user_data` and `cairo_font_face_get_user_data` implement pointer-identity keys, replacement/removal, guest destroy callbacks, finalization order and reentrant-finalizer status. The implementation uses the repository's pinned FreeType submodule at `37515cf56ba3349656049edbed89ffc6f6466f2d`. `cairo_ft_font_options_substitute` is intentionally excluded from this change. Its existing throwing stub is retained here; PR #16895 owns the functional Fontconfig option-substitution implementation. ### Lifetime constraint The Cairo font face retains its `FT_Face`, but the caller must keep the owning `FT_Library` alive until the Cairo font face is destroyed. This PR does not claim scaled-font creation or glyph rasterization. ### Tested <!-- OS, title or homebrew, what was checked. "Not tested" is a valid answer. A performance change: the summary lines of main and of this PR from the same machine and scene, with the traces attached, captured as the Performance report issue form describes. --> - Earlier standalone font-face Windows Release targets built successfully. After merging upstream Cairo regions, the four focused Windows targets built on commit 59849abb578cdb93e25b57ac4065f27ef08409ca. - Windows MinGW GCC 15.2 Release after region integration: focused CTest matrix, font options, font face, and regions passed 4/4 on exact commit 59849abb578cdb93e25b57ac4065f27ef08409ca. - WSL Ubuntu GCC 13.3 Debug with ASan and UBSan (both address and undefined sanitizers enabled): on exact regions-integrated merge commit 59849abb578cdb93e25b57ac4065f27ef08409ca, four focused executables built, and CTest matrix, font options, font face, and regions passed 4/4 with ASAN_OPTIONS=detect_leaks=1 and UBSAN_OPTIONS=halt_on_error=1. The earlier standalone head had also passed four focused tests, including surface. - `python -I tools/check_conventions.py --base upstream/main --head HEAD` passed. - `git diff --check` passed. - Reconciled with upstream main d03beaf11059bad1642b6072d6be80ad2b478307, retaining both FreeType font-face and six newly merged region exports; on that head, conventions and whitespace checks passed. Six files in the new PR-specific diff. The original overlap scan covered 1,758 open PRs at initial publication. It found 13 shared-file relationships and no same-export claims. Two comparisons failed closed because #14212 and #15681 conflict with the base; I manually reviewed the relevant libcairo hunks. #14212's callable-export wording matches this branch, while #15681's shared CMake/export/inventory/debt files implement distinct context/pattern APIs and remain an integration dependency. #16895 now shares only the debt document; the duplicate font-options implementation has been removed from this branch. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [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: #15208, #15315, #15317, #15324, #15430, #15675, #15679, #15681, #15685 (the earlier #14329 and #16315 dependencies have merged) - [x] Fixes: none - [x] Unblocks: none (supports a separate scaled-font follow-up) - [x] GPUs tested: n/a - [x] AI-assisted: yes
gabichuuuuu commented 2026-11-07 22:15:40 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by gabichuuuuu on 2026-11-07T22:15:40Z.

Review: COMMENTED


Read 38fc893a0c against main.

cairo_ft_font_options_substitute doesn't match Cairo 1.18.0. In _cairo_ft_font_options_substitute (src/cairo-ft-font.c), FC_HINTING is set from hint_style != CAIRO_HINT_STYLE_NONE, inside the hint_style != DEFAULT block; hint metrics are never read there. FontFace.cpp:153 derives it from hintMetrics, so FULL with METRICS_OFF gives hinting=false where upstream gives true, and NONE with default metrics adds nothing where upstream adds hinting=false. Upstream also adds rgba=FC_RGBA_NONE for every non-default, non-subpixel antialias (deleting any existing rgba when it adds antialias) and FC_RGBA_RGB for subpixel with the default order; line 149 adds nothing in those cases. Every add upstream is guarded by FcPatternGet(...) == FcResultNoMatch, while here a value already in the pattern gets a second one appended, since this repo's FcPatternAdd* appends (libfontconfig/Objects.cpp:201). Out-of-range enums go in unchanged instead of mapping to FULL/RGB. GuestCairoFontFace.cpp:150 and :165 assert the non-upstream results. #16895 ports this export faithfully, and both PRs remove the same stub and edit the same count line, so I'd drop it from this one.

Status values. kInvalidStatus = 12 is CAIRO_STATUS_SURFACE_FINISHED in cairo.h (INVALID_STATUS is 6), and kUserFontError = 51 is past CAIRO_STATUS_LAST_STATUS (45). Upstream returns _cairo_font_face_nil (NO_MEMORY) for any create_from_face failure, and has no reentrant-finalizer status: __put leaves the count at 1 while user data is finalized, so set_user_data from a destroy callback falls through to the array code. I'd return NoMemoryFace() on the FT_Reference_Face failure and throw NotImplemented_nid_no_patch (with a debt entry) for the reentrant case, rather than invent a code.

Same FT_Face, different objects. Upstream caches font faces by FT_Face and load_flags, so calling create_for_ft_face(f, 0) twice returns the same face with refcount 2, and user data set through one handle is visible through the other. This PR allocates a new face each time.

Smaller things. docs/dev/debt/libcairo.md:5 still says "other 124 pending" (118 after this PR). The body is missing the template's Tested section and checklist (Fixes, Unblocks, GPUs tested, AI-assisted).

Not run here; this is from reading the code against the 1.18.0 sources.

AI-assisted: yes.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5815249105 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/16892#pullrequestreview-5815249105). Originally posted by [gabichuuuuu](https://github.com/gabichuuuuu) on 2026-11-07T22:15:40Z. **Review:** COMMENTED --- Read `38fc893a0c` against `main`. **`cairo_ft_font_options_substitute` doesn't match Cairo 1.18.0.** In `_cairo_ft_font_options_substitute` (`src/cairo-ft-font.c`), `FC_HINTING` is set from `hint_style != CAIRO_HINT_STYLE_NONE`, inside the `hint_style != DEFAULT` block; hint metrics are never read there. `FontFace.cpp:153` derives it from `hintMetrics`, so FULL with METRICS_OFF gives `hinting=false` where upstream gives true, and NONE with default metrics adds nothing where upstream adds `hinting=false`. Upstream also adds `rgba=FC_RGBA_NONE` for every non-default, non-subpixel antialias (deleting any existing `rgba` when it adds `antialias`) and `FC_RGBA_RGB` for subpixel with the default order; line 149 adds nothing in those cases. Every add upstream is guarded by `FcPatternGet(...) == FcResultNoMatch`, while here a value already in the pattern gets a second one appended, since this repo's `FcPatternAdd*` appends (`libfontconfig/Objects.cpp:201`). Out-of-range enums go in unchanged instead of mapping to FULL/RGB. `GuestCairoFontFace.cpp:150` and `:165` assert the non-upstream results. #16895 ports this export faithfully, and both PRs remove the same stub and edit the same count line, so I'd drop it from this one. **Status values.** `kInvalidStatus = 12` is `CAIRO_STATUS_SURFACE_FINISHED` in `cairo.h` (`INVALID_STATUS` is 6), and `kUserFontError = 51` is past `CAIRO_STATUS_LAST_STATUS` (45). Upstream returns `_cairo_font_face_nil` (NO_MEMORY) for any `create_from_face` failure, and has no reentrant-finalizer status: `__put` leaves the count at 1 while user data is finalized, so `set_user_data` from a destroy callback falls through to the array code. I'd return `NoMemoryFace()` on the `FT_Reference_Face` failure and throw `NotImplemented_nid_no_patch` (with a debt entry) for the reentrant case, rather than invent a code. **Same `FT_Face`, different objects.** Upstream caches font faces by `FT_Face` and `load_flags`, so calling `create_for_ft_face(f, 0)` twice returns the same face with refcount 2, and user data set through one handle is visible through the other. This PR allocates a new face each time. **Smaller things.** `docs/dev/debt/libcairo.md:5` still says "other 124 pending" (118 after this PR). The body is missing the template's Tested section and checklist (Fixes, Unblocks, GPUs tested, AI-assisted). Not run here; this is from reading the code against the 1.18.0 sources. AI-assisted: yes.

Pull request closed

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!16892
No description provided.