Skip to content

fonts: Port macOS font code to use objc2-* crates#41711

Merged
mrobinson merged 1 commit intoservo:mainfrom
mrobinson:port-mac-fonts-to-use-objc2
Feb 5, 2026
Merged

fonts: Port macOS font code to use objc2-* crates#41711
mrobinson merged 1 commit intoservo:mainfrom
mrobinson:port-mac-fonts-to-use-objc2

Conversation

@mrobinson
Copy link
Copy Markdown
Member

@mrobinson mrobinson commented Jan 6, 2026

This change moves Servo's macOS font code away from using our homegrown
core-* crates and toward the more general-purpose objc2-* crates.
Development of these crates is more active and they use automatic code
generation to have more complete coverage of the relevant platform APIs. In
addition, this means that it is easier to understand Servo's code if you
are familiar with the platform APIs as the objc2 crate are a more
direct Rust wrapper over them. In comparison, our wrappers had more
batteries-included behavior that was less flexible.

This change:

Testing: This should not change behavior and macOS is currently untested via WPT on the Ci.

@mrobinson mrobinson requested a review from nicoburns as a code owner January 6, 2026 15:29
@servo-highfive servo-highfive added the S-awaiting-review There is new code that needs to be reviewed. label Jan 6, 2026
@mrobinson
Copy link
Copy Markdown
Member Author

@madsmtm I would be grateful if you could take a look at this one. I'm particularly unsure if I'm converting between the opaque versions of CFDictionary and the concrete types in the best way, but I'm happy to receive any feedback. Thank you!

@mrobinson mrobinson force-pushed the port-mac-fonts-to-use-objc2 branch from d6ae1b2 to 95d4c70 Compare January 6, 2026 15:45
@jschwe
Copy link
Copy Markdown
Member

jschwe commented Jan 6, 2026

[...] and they use automatic code general [...]

did you mean generation?

@mrobinson
Copy link
Copy Markdown
Member Author

[...] and they use automatic code general [...]

did you mean generation?

Indeed. I've fixed this typo.

Copy link
Copy Markdown

@madsmtm madsmtm left a comment

Choose a reason for hiding this comment

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

This is probably how I'd do the transition myself, so +1 from me.

Not that happy with how some things are a bit more cumbersome now, as I noted in servo/core-foundation-rs#729 there's still work to do on objc2-core-text (and it shows in this PR).

Two things I'm noting in particular is that it's annoying that madsmtm/objc2@90d4c33 is not yet released, and that things like CTFontManagerCopyAvailableFontFamilyNames still return untyped CFArrays (would be a breaking change, so I'm still on fixing that), but if you have other gripes, I'd love to know!

Comment on lines +215 to +222
&variation_keys
.iter()
.map(CFRetained::as_ref)
.collect::<Vec<_>>(),
&variation_values
.iter()
.map(CFRetained::as_ref)
.collect::<Vec<_>>(),
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Remark: Ideally, objc2-core-foundation would allow you to avoid these extra allocations (probably by having variants of CFDictionary::from_slices similar to CFArray::from_retained_objects).

.iter()
.map(|variation| CFNumber::new_f32(variation.value))
.collect();
let values_dict: CFRetained<CFDictionary<CFNumber, CFNumber>> = CFDictionary::from_slices(
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

FYI, I tend to prefer:

Suggested change
let values_dict: CFRetained<CFDictionary<CFNumber, CFNumber>> = CFDictionary::from_slices(
let values_dict = CFDictionary::<CFNumber, CFNumber>::from_slices(

It makes it a bit easier on the eyes.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yeah, that looks a lot better.

&glyphs[0],
self.ctfont.advances_for_glyphs(
CTFontOrientation::Default,
NonNull::new(&mut (glyph as CGGlyph))?,
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Tip: You can use NonNull::from when converting references.

Suggested change
NonNull::new(&mut (glyph as CGGlyph))?,
NonNull::from(&mut (glyph as CGGlyph)),

Similarly in a few other places.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Oh, great!

@servo-highfive servo-highfive added the S-needs-rebase There are merge conflict errors. label Jan 13, 2026
@mrobinson mrobinson changed the title fonts: Port macOS font code to use Use objc2-* crates fonts: Port macOS font code to use objc2-* crates Jan 15, 2026
@mrobinson
Copy link
Copy Markdown
Member Author

Thanks for the review @madsmtm. We also end up having to make another copy of font data due to the lack of CFData::from_arc, but as you said on a somewhere objc2 GitHub issue, it's not clear if it's safe or not.

@mrobinson mrobinson force-pushed the port-mac-fonts-to-use-objc2 branch from 95d4c70 to 6fa15f4 Compare January 16, 2026 17:55
@servo-highfive servo-highfive removed the S-needs-rebase There are merge conflict errors. label Jan 16, 2026
@mrobinson
Copy link
Copy Markdown
Member Author

Okay. I think this is ready for review now.

@servo-highfive servo-highfive added S-needs-rebase There are merge conflict errors. and removed S-awaiting-review There is new code that needs to be reviewed. labels Feb 4, 2026
@mrobinson mrobinson force-pushed the port-mac-fonts-to-use-objc2 branch from 6fa15f4 to 18524b4 Compare February 5, 2026 20:45
@servo-highfive servo-highfive added S-awaiting-review There is new code that needs to be reviewed. and removed S-needs-rebase There are merge conflict errors. labels Feb 5, 2026
This change moves Servo's macOS font code away from using our homegrown
`core-*` crates and toward the more general-purpose `objc2-*` crates.
Development of these crates is more active and they use automatic code
general to have more complete coverage of the relevant platform APIs. In
addition, this means that it is easier to understand Servo's code if you
are familiar with the platform APIs as the `objc2` crate are a more
direct Rust wrapper over them. In comparison, our wrappers had more
batteries-included behavior that was less flexible.

This change:
 - is the first step toward more flexible font fallback on macOS (servo#41426)
 - means we can now remove our manually FFI'd font variation code.

Signed-off-by: Martin Robinson <[email protected]>
@mrobinson mrobinson force-pushed the port-mac-fonts-to-use-objc2 branch from 18524b4 to 99c646b Compare February 5, 2026 20:45
@mrobinson mrobinson enabled auto-merge February 5, 2026 20:45
@mrobinson mrobinson added this pull request to the merge queue Feb 5, 2026
@servo-highfive servo-highfive added the S-awaiting-merge The PR is in the process of compiling and running tests on the automated CI. label Feb 5, 2026
Merged via the queue into servo:main with commit 1db3ad5 Feb 5, 2026
29 checks passed
@mrobinson mrobinson deleted the port-mac-fonts-to-use-objc2 branch February 5, 2026 21:51
@servo-highfive servo-highfive removed the S-awaiting-merge The PR is in the process of compiling and running tests on the automated CI. label Feb 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-awaiting-review There is new code that needs to be reviewed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants