Skip to content

Record your screen, ship a demo. Free and open-source, GPU-accelerated, no watermarks, no subscriptions. Windows, macOS, Linux. Actively maintained.

feat(compositor): text_metal — CoreText/CoreGraphics text rasterizer

Etienne Lescot16ebbde51ec9
diff --git a/crates/compositor/src/text_macos.rs b/crates/compositor/src/text_macos.rs
index cb16e1a22..5e591609a 100644
--- a/crates/compositor/src/text_macos.rs
+++ b/crates/compositor/src/text_macos.rs
@@ -1,4 +1,4 @@
-//! Rastérisation du texte des annotations sur macOS — CoreText/CoreGraphics.
+//! Rastérisation du texte des annotations sur macOS — CoreText + CoreGraphics.
//!
//! Équivalent macOS de `text_windows.rs` (DirectWrite + Direct2D sur surface DXGI).
//! Le module exporte la même surface publique (`TextSpec`, `TextRasterizer`) pour que
@@ -7,23 +7,22 @@
//!
//! # État de ce commit
//!
-//! Seule l'API publique est exposée — la mécanique CoreText/CoreGraphics sera remplie
-//! dans un commit ultérieur. Les commits d'implémentation ajouteront :
-//! - Un `CTFramesetter` créé depuis `CTFontCreateWithName` + l'`NSAttributedString`,
-//! - Un `CGContext` mémoire backed par un `MTLTexture` (via `MTKTextureLoader` /
-//! `CGDataProviderCreateWithData`) où le texte est dessiné en alpha-prémultiplié,
-//! - Le cache `text_cache: RefCell<HashMap<String, (MTLTexture, u64)>>` côté Metal
-//! (symétrique du `text_cache` D3D11).
+//! Première implémentation du rastériseur. Le câblage ObjC/CoreText est posé ; il est
+//! honnête mais pas testé en runtime — la première fois qu'il tourne sur Mac, ce sera
+//! au prochain push (CI macos-14). Les bindings ObjC couvrent le strict minimum
+//! nécessaire : `NSString`, `NSMutableAttributedString`, `NSMutableParagraphStyle`,
+//! `CGColor`, `CTFont`, `CTFramesetter`, `CTFrame`, et `CGBitmapContextCreate`.
//!
-//! Le moteur Metal consomme déjà la texture comme `t0` dans le shader `ps_main` mode
-//! 11 (texte D2D prémultiplié) — le port MSL met juste à jour le sampler pour lire
-//! depuis le format `RGBA8Unorm` (équivalent direct de `DXGI_FORMAT_R8G8B8A8_UNORM`).
-//!
-//! Tant que `TextRasterizer::new` renvoie `Err`, le moteur macOS ne peut pas
-//! composer du texte, et les annotations qui en dépendent (effets texte, fonds de
-//! scène typés) échouent lisiblement. C'est cohérent avec le reste du scaffold.
+//! L'API suit la policy de la version Windows : `TextSpec::cache_key()` byte-identique,
+//! `TextRasterizer::new()` renvoie `Ok` immédiatement (pas de factories CoreText
+//! à allouer — CoreText/CoreGraphics sont prêts dès le link des frameworks).
-use anyhow::{anyhow, Result};
+use crate::metal::Gpu;
+use anyhow::{anyhow, bail, Result};
+use objc2::msg_send;
+use objc2::runtime::AnyClass;
+use std::ffi::{c_void, CString};
+use std::ptr;
/// Spécification d'un texte à rastériser. Mêmes champs que `text_windows::TextSpec`
/// — le moteur macOS les consomme via `cache_key` pour déterminer si une re-rastérisation
@@ -66,33 +65,293 @@ impl TextSpec {
}
}
-/// Rastériseur de texte macOS. Conservé comme struct vide dans ce commit — la
-/// mécanique CoreText/CoreGraphics sera ajoutée dans un commit ultérieur. Les
-/// champs internes (factory CoreText, contexte CoreGraphics) seront ajoutés au fil
-/// de l'implémentation.
+/// Frameworks liés.
+#[link(name = "CoreText", kind = "framework")]
+#[link(name = "CoreGraphics", kind = "framework")]
+#[link(name = "CoreFoundation", kind = "framework")]
+#[link(name = "Foundation", kind = "framework")]
+extern "C" {}
+
+/// Bindings CoreText + CG minimaux.
+extern "C" {
+ fn CFRelease(cf: *const c_void);
+ fn CTFontCreateWithName(
+ name: *const c_void,
+ size: f64,
+ matrix: *const c_void,
+ ) -> *const c_void;
+ fn CTFramesetterCreateWithAttributedString(attr: *const c_void) -> *const c_void;
+ fn CTFramesetterCreateFrame(
+ framesetter: *const c_void,
+ location: usize,
+ length: isize,
+ path: *const c_void,
+ extra: *const c_void,
+ ) -> *const c_void;
+ fn CTFrameDraw(frame: *const c_void, context: *const c_void);
+ fn CGDataProviderCreateWithData(
+ info: *const c_void,
+ data: *const c_void,
+ size: usize,
+ release_data: *const c_void,
+ release_info: *const c_void,
+ ) -> *const c_void;
+ // CGBitmapContextCreate : 8 args, bind via extern "C".
+ fn CGBitmapContextCreate(
+ data: *mut c_void,
+ width: usize,
+ height: usize,
+ bits_per_component: usize,
+ bytes_per_row: usize,
+ color_space: *const c_void,
+ bitmap_info: u32,
+ ) -> *const c_void;
+}
+
+/// Rastériseur de texte macOS. Pas d'état persistant pour l'instant — CoreText
+/// et CoreGraphics sont prêts dès le link des frameworks.
pub struct TextRasterizer;
impl TextRasterizer {
- /// Crée un rastériseur CoreText/CoreGraphics. Renvoie `Err` dans ce commit.
pub fn new() -> Result<TextRasterizer> {
- Err(anyhow!(
- "text_macos::TextRasterizer::new: non implémenté — \
- CoreText/CoreGraphics sera ajouté dans un commit ultérieur"
- ))
+ Ok(TextRasterizer)
}
- /// Rastérise un `TextSpec` vers une texture Metal. Le type de retour
- /// `*mut std::ffi::c_void` est l'opaque « handle de shader-resource-view » —
- /// un `MTLTexture` retainé côté macOS (équivalent direct du `ID3D11ShaderResourceView`
- /// Windows). Les call-sites doivent le cast via cfg : `ID3D11ShaderResourceView::from_raw`
- /// sur Windows, `metal::Texture::from_raw` sur macOS.
+ /// Rastérise `spec` dans une texture Metal neuve (BGRA8Unorm, alpha-prémultiplié).
+ /// Le caller (`Compositor`) retient la texture et la stocke dans son cache
+ /// text_cache (clé = spec.cache_key()).
///
- /// Renvoie `Err` tant que le rastériseur n'est pas implémenté.
+ /// Pipeline :
+ /// 1. Alloue une `MTLTexture` BGRA8Unorm, `StorageMode::Shared`.
+ /// 2. Crée un `CGContext` bitmap backed par un buffer CPU (la métal-rs 0.29 ne
+ /// permet pas d'obtenir le pointeur GPU d'une Shared texture sans IOSurface —
+ /// le buffer CPU est uploadé via `replace_region` à l'étape finale).
+ /// 3. Construit un `NSAttributedString` (content + couleur + font + underline +
+ /// alignement).
+ /// 4. Crée un `CTFramesetter` + `CTFrame`, et `CTFrameDraw` dans le context.
+ /// 5. Upload du buffer CPU → MTLTexture.
pub unsafe fn rasterize(
&self,
- _gpu: &crate::metal::Gpu,
- _spec: &TextSpec,
- ) -> Result<*mut std::ffi::c_void> {
- Err(anyhow!("text_macos::TextRasterizer::rasterize: non implémenté"))
+ gpu: &Gpu,
+ spec: &TextSpec,
+ ) -> Result<*mut c_void> {
+ let (w, h) = (spec.box_px[0].max(1) as usize, spec.box_px[1].max(1) as usize);
+ if spec.content.is_empty() {
+ bail!("text_macos::rasterize: texte vide");
+ }
+
+ // 1. MTLTexture BGRA8Unorm, Shared.
+ let desc = metal::TextureDescriptor::new();
+ desc.set_texture_type(metal::TextureType::Type2D);
+ desc.set_pixel_format(metal::MTLPixelFormat::BGRA8Unorm);
+ desc.set_width(w as u64);
+ desc.set_height(h as u64);
+ desc.set_usage(metal::MTLTextureUsage::ShaderRead);
+ desc.set_storage_mode(metal::MTLStorageMode::Shared);
+ let texture = gpu.device.new_texture(&desc);
+
+ // 2. Buffer CPU + CGContext bitmap.
+ let bytes_per_row = w * 4;
+ let mut buffer: Vec<u8> = vec![0u8; bytes_per_row * h];
+ // kCGImageAlphaPremultipliedFirst (1) | kCGBitmapByteOrder32Little (4<<12 = 8192) | kCGImageAlphaNone (0).
+ // 1 = premultiplied ; 8192 = 32-bit little-endian byte order ; le | 0x8000 final force BGRA layout (par défaut).
+ let bitmap_info: u32 = 0x100 | 0x8000 | (4 << 12);
+ let cg_ctx = CGBitmapContextCreate(
+ buffer.as_mut_ptr() as *mut c_void,
+ w,
+ h,
+ 8, // bits per component
+ bytes_per_row,
+ ptr::null(), // default device-RGB color space
+ bitmap_info,
+ );
+ if cg_ctx.is_null() {
+ bail!("CGBitmapContextCreate a renvoyé NULL");
+ }
+ // 3. NSAttributedString.
+ let attr_str = build_attributed_string(spec)?;
+ let attr_len: isize = spec.content.chars().count() as isize;
+
+ // 4. CTFramesetter + CTFrame + draw.
+ let framesetter = CTFramesetterCreateWithAttributedString(attr_str);
+ CFRelease(attr_str);
+ if framesetter.is_null() {
+ bail!("CTFramesetterCreateWithAttributedString NULL");
+ }
+ let frame = CTFramesetterCreateFrame(
+ framesetter,
+ 0, // location
+ attr_len,
+ ptr::null(), // path = NULL = full context bounds
+ ptr::null(),
+ );
+ CFRelease(framesetter);
+ if frame.is_null() {
+ bail!("CTFramesetterCreateFrame NULL");
+ }
+ CTFrameDraw(frame, cg_ctx);
+ CFRelease(frame);
+ CFRelease(cg_ctx);
+
+ // 5. Upload CPU → texture via replace_region.
+ let region = metal::MTLRegionMake2D(0, 0, w as u64, h as u64);
+ texture.replace_region(
+ region,
+ 0, // mipmap level
+ 0, // slice
+ bytes_per_row as u64,
+ buffer.as_ptr() as *const c_void,
+ );
+
+ Ok(texture.as_ptr())
+ }
+}
+
+/// Construit un NSAttributedString ObjC avec les attributs CoreText demandés.
+unsafe fn build_attributed_string(spec: &TextSpec) -> Result<*const c_void> {
+ // NSString depuis spec.content.
+ let ns_string_cls = AnyClass::get(c"NSString").ok_or_else(|| anyhow!("NSString introuvable"))?;
+ let utf8 = CString::new(spec.content.as_str())?;
+ let ns_string: *mut objc2::runtime::AnyObject =
+ msg_send![ns_string_cls, stringWithUTF8String: utf8.as_ptr()];
+ if ns_string.is_null() {
+ bail!("NSString stringWithUTF8String NULL");
+ }
+ // NSMutableAttributedString.
+ let attr_str_cls =
+ AnyClass::get(c"NSMutableAttributedString").ok_or_else(|| anyhow!("NSMutableAttributedString introuvable"))?;
+ let attr_str: *mut objc2::runtime::AnyObject =
+ msg_send![attr_str_cls, attributedStringWithString: ns_string];
+ if attr_str.is_null() {
+ bail!("NSMutableAttributedString attributedStringWithString NULL");
+ }
+ let len: usize = spec.content.chars().count();
+
+ // Set d'attributs via addAttribute:value:range:.
+ let range: NSRange = NSRange { location: 0, length: len };
+
+ // Foreground color.
+ if let Some(cg_color) = cg_color_create_rgba(spec.color[0], spec.color[1], spec.color[2], spec.color[3]) {
+ let _: () = msg_send![attr_str,
+ addAttribute: sel_foregroundColor(),
+ value: cg_color,
+ range: range,
+ ];
+ }
+
+ // Font.
+ if let Some(ctfont) = ct_font_create_with_name(
+ &spec.font_family,
+ spec.font_size_px.max(1.0) as f64,
+ ) {
+ let _: () = msg_send![attr_str,
+ addAttribute: sel_font(),
+ value: ctfont,
+ range: range,
+ ];
+ }
+
+ // Underline.
+ if spec.underline {
+ let _: () = msg_send![attr_str,
+ addAttribute: sel_underlineStyle(),
+ value: 1usize,
+ range: range,
+ ];
+ }
+
+ // Alignement.
+ let paragraph_cls = AnyClass::get(c"NSMutableParagraphStyle")
+ .ok_or_else(|| anyhow!("NSMutableParagraphStyle introuvable"))?;
+ let paragraph: *mut objc2::runtime::AnyObject = msg_send![paragraph_cls, new];
+ if !paragraph.is_null() {
+ let align: isize = match spec.align.as_str() {
+ "left" => 0,
+ "right" => 1,
+ _ => 2,
+ };
+ let _: () = msg_send![paragraph, setAlignment: align];
+ let _: () = msg_send![attr_str,
+ addAttribute: sel_paragraphStyle(),
+ value: paragraph,
+ range: range,
+ ];
+ }
+
+ Ok(attr_str as *const c_void)
+}
+
+#[repr(C)]
+#[derive(Clone, Copy)]
+struct NSRange {
+ location: usize,
+ length: usize,
+}
+
+/// Sélecteurs Objective-C (constants, enregistrées une seule fois).
+unsafe fn sel_foregroundColor() -> *const c_void {
+ objc2::runtime::Sel::register(c"NSColor").as_ptr() as *const c_void
+}
+unsafe fn sel_font() -> *const c_void {
+ objc2::runtime::Sel::register(c"NSFont").as_ptr() as *const c_void
+}
+unsafe fn sel_underlineStyle() -> *const c_void {
+ objc2::runtime::Sel::register(c"NSUnderlineStyle").as_ptr() as *const c_void
+}
+unsafe fn sel_paragraphStyle() -> *const c_void {
+ objc2::runtime::Sel::register(c"NSParagraphStyle").as_ptr() as *const c_void
+}
+
+/// Crée un CGColorRef (retainable) à partir de (r,g,b,a) en [0,1].
+unsafe fn cg_color_create_rgba(r: f64, g: f64, b: f64, a: f64) -> Option<*mut objc2::runtime::AnyObject> {
+ let color_cls = AnyClass::get(c"CGColor")?;
+ let space_cls = AnyClass::get(c"CGColorSpace")?;
+ let device_rgb: *mut objc2::runtime::AnyObject = msg_send![space_cls, deviceRGBColorSpace];
+ if device_rgb.is_null() {
+ return None;
+ }
+ let color: *mut objc2::runtime::AnyObject = msg_send![color_cls,
+ colorWithRed: r,
+ green: g,
+ blue: b,
+ alpha: a,
+ ];
+ if color.is_null() {
+ None
+ } else {
+ Some(color)
+ }
+}
+
+/// Crée un CTFontRef (retainable) depuis un nom de famille et une taille.
+unsafe fn ct_font_create_with_name(
+ family: &str,
+ size: f64,
+) -> Option<*mut objc2::runtime::AnyObject> {
+ let cf_string_cls = AnyClass::get(c"CFString")?;
+ let family_bytes = CString::new(family).ok()?;
+ // CFStringCreateWithCString(kCFAllocatorDefault, cstr, kCFStringEncodingUTF8).
+ let alloc_default_cls = AnyClass::get(c"kCFAllocatorDefault");
+ let alloc: *mut objc2::runtime::AnyObject = if let Some(c) = alloc_default_cls {
+ msg_send![c, self]
+ } else {
+ ptr::null_mut()
+ };
+ let family_cf: *mut objc2::runtime::AnyObject = msg_send![cf_string_cls,
+ stringWithCString: family_bytes.as_ptr()
+ encoding: 0x08000100u32 /* kCFStringEncodingUTF8 */
+ ];
+ if family_cf.is_null() {
+ return None;
+ }
+ let font: *mut objc2::runtime::AnyObject = CTFontCreateWithName(
+ family_cf as *const c_void,
+ size,
+ ptr::null(),
+ ) as *mut objc2::runtime::AnyObject;
+ if font.is_null() {
+ None
+ } else {
+ Some(font)
}
}
\ No newline at end of file