use crate::canvas::Canvas;
use crate::color::Color;
use crate::font_file::{FontFile, Set};
use crate::macos_sys::{CFRelease, CFStringCreateWithBytes, NSPoint, NSRect, NSSize, Obj, UTF8, array_items, rust_string, send};
use crate::macos_sys::{CGBitmapContextCreate, CGColorSpaceCreateDeviceGray, CGColorSpaceRelease, CGContextRelease, CGContextSetGrayFillColor, CGContextSetShouldAntialias};
use crate::system_fonts::Style;
use crate::text::{Face, Font, Metrics, TextSystem};
use std::collections::HashMap;
use std::ffi::c_void;
use std::path::PathBuf;
#[repr(C)]
struct CFRange {
location: isize,
length: isize,
}
#[link(name = "CoreText", kind = "framework")]
unsafe extern "C" {
fn CTFontCreateUIFontForLanguage(kind: u32, size: f64, language: *const c_void) -> *const c_void;
fn CTFontCreateWithName(name: *const c_void, size: f64, matrix: *const c_void) -> *const c_void;
fn CTFontCreateCopyWithSymbolicTraits(font: *const c_void, size: f64, matrix: *const c_void, value: u32, mask: u32) -> *const c_void;
fn CTFontCreateForString(font: *const c_void, text: *const c_void, range: CFRange) -> *const c_void;
fn CTFontGetGlyphsForCharacters(font: *const c_void, characters: *const u16, glyphs: *mut u16, count: isize) -> bool;
fn CTFontGetAdvancesForGlyphs(font: *const c_void, orientation: u32, glyphs: *const u16, advances: *mut NSSize, count: isize) -> f64;
fn CTFontGetBoundingRectsForGlyphs(font: *const c_void, orientation: u32, glyphs: *const u16, rects: *mut NSRect, count: isize) -> NSRect;
fn CTFontDrawGlyphs(font: *const c_void, glyphs: *const u16, positions: *const NSPoint, count: usize, context: *mut c_void);
fn CTFontGetAscent(font: *const c_void) -> f64;
fn CTFontGetDescent(font: *const c_void) -> f64;
fn CTFontGetCapHeight(font: *const c_void) -> f64;
fn CTFontGetXHeight(font: *const c_void) -> f64;
fn CTFontCopyAttribute(font: *const c_void, attribute: *const c_void) -> *const c_void;
fn CTFontCollectionCreateFromAvailableFonts(options: *const c_void) -> *const c_void;
fn CTFontCollectionCreateMatchingFontDescriptors(collection: *const c_void) -> *const c_void;
fn CTFontDescriptorCopyAttribute(descriptor: *const c_void, attribute: *const c_void) -> *const c_void;
static kCTFontURLAttribute: *const c_void;
static kCTFontFamilyNameAttribute: *const c_void;
static kCTFontTraitsAttribute: *const c_void;
static kCTFontWeightTrait: *const c_void;
static kCTFontSymbolicTrait: *const c_void;
fn CTFontCreateCopyWithAttributes(font: *const c_void, size: f64, matrix: *const c_void, attributes: *const c_void) -> *const c_void;
fn CTLineCreateWithAttributedString(text: *const c_void) -> *const c_void;
fn CTLineGetGlyphRuns(line: *const c_void) -> *const c_void;
fn CTLineGetTypographicBounds(line: *const c_void, ascent: *mut f64, descent: *mut f64, leading: *mut f64) -> f64;
fn CTRunGetGlyphCount(run: *const c_void) -> isize;
fn CTRunGetGlyphs(run: *const c_void, range: CFRange, glyphs: *mut u16);
fn CTRunGetPositions(run: *const c_void, range: CFRange, positions: *mut NSPoint);
fn CTRunGetAttributes(run: *const c_void) -> *const c_void;
static kCTFontAttributeName: *const c_void;
}
#[link(name = "CoreFoundation", kind = "framework")]
unsafe extern "C" {
fn CFEqual(one: *const c_void, other: *const c_void) -> bool;
fn CFDictionaryGetValue(dictionary: *const c_void, key: *const c_void) -> *const c_void;
}
const SYSTEM: u32 = 2;
const FIXED: u32 = 1;
const ITALIC: u32 = 1;
const BOLD: u32 = 2;
struct Owned(*const c_void);
impl Drop for Owned {
fn drop(&mut self) {
if !self.0.is_null() {
unsafe { CFRelease(self.0) }
}
}
}
fn cf_string(text: &str) -> Owned {
Owned(unsafe { CFStringCreateWithBytes(std::ptr::null(), text.as_ptr(), text.len() as isize, UTF8, 0) })
}
fn font_of(face: Face, pixels: f64) -> Owned {
let (family, of, style) = crate::system_fonts::asked(face);
let kind = if of == Face::Mono { FIXED } else { SYSTEM };
let base = match family {
Some(family) => Owned(unsafe { CTFontCreateWithName(cf_string(&family).0, pixels, std::ptr::null()) }),
None => Owned(unsafe { CTFontCreateUIFontForLanguage(kind, pixels, std::ptr::null()) }),
};
let wanted = if style.weight >= 600 { BOLD } else { 0 } | if style.italic { ITALIC } else { 0 };
if wanted == 0 || base.0.is_null() {
return base;
}
let styled = Owned(unsafe { CTFontCreateCopyWithSymbolicTraits(base.0, 0.0, std::ptr::null(), wanted, BOLD | ITALIC) });
if styled.0.is_null() { base } else { styled }
}
fn glyph_in(font: *const c_void, letter: char) -> Option<u16> {
let mut units = [0u16; 2];
let units = letter.encode_utf16(&mut units);
let mut glyphs = [0u16; 2];
let found = unsafe { CTFontGetGlyphsForCharacters(font, units.as_ptr(), glyphs.as_mut_ptr(), units.len() as isize) };
(found && glyphs[0] != 0).then_some(glyphs[0])
}
fn fallback(font: *const c_void, letter: char) -> Option<(Owned, u16)> {
let text = cf_string(letter.encode_utf8(&mut [0; 4]));
let other = Owned(unsafe { CTFontCreateForString(font, text.0, CFRange { location: 0, length: letter.len_utf16() as isize }) });
let glyph = (!other.0.is_null()).then(|| glyph_in(other.0, letter)).flatten()?;
Some((other, glyph))
}
struct Glyph {
width: usize,
left: i32,
top: i32,
advance: f32,
cover: Vec<u8>,
}
fn drawn(font: *const c_void, glyph: u16) -> Glyph {
let mut advance = NSSize::default();
let mut bounds = NSRect::default();
unsafe {
CTFontGetAdvancesForGlyphs(font, 0, &glyph, &mut advance, 1);
CTFontGetBoundingRectsForGlyphs(font, 0, &glyph, &mut bounds, 1);
}
let advance = advance.width.round() as f32;
let (left, bottom) = (bounds.origin.x.floor() as i32 - 1, bounds.origin.y.floor() as i32 - 1);
let (right, top) = ((bounds.origin.x + bounds.size.width).ceil() as i32 + 1, (bounds.origin.y + bounds.size.height).ceil() as i32 + 1);
let (width, height) = ((right - left).max(0) as usize, (top - bottom).max(0) as usize);
if bounds.size.width <= 0.0 || bounds.size.height <= 0.0 || width * height > 1 << 22 {
return Glyph { width: 0, left: 0, top: 0, advance, cover: Vec::new() };
}
let mut cover = vec![0u8; width * height];
unsafe {
let grey = CGColorSpaceCreateDeviceGray();
let context = CGBitmapContextCreate(cover.as_mut_ptr().cast(), width, height, 8, width, grey, 0);
CGColorSpaceRelease(grey);
if !context.is_null() {
CGContextSetShouldAntialias(context, true);
CGContextSetGrayFillColor(context, 1.0, 1.0);
CTFontDrawGlyphs(font, &glyph, &NSPoint { x: f64::from(-left), y: f64::from(-bottom) }, 1, context);
CGContextRelease(context);
}
}
Glyph { width, left, top, advance, cover }
}
pub struct SystemText {
fonts: HashMap<(u32, u32), Owned>,
glyphs: HashMap<(u32, u32, char), Glyph>,
}
impl std::fmt::Debug for SystemText {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("SystemText").field("fonts", &self.fonts.len()).field("letters", &self.glyphs.len()).finish()
}
}
impl SystemText {
pub fn new() -> Option<Self> {
let text = SystemText { fonts: HashMap::new(), glyphs: HashMap::new() };
let system = Owned(unsafe { CTFontCreateUIFontForLanguage(SYSTEM, 12.0, std::ptr::null()) });
(!system.0.is_null()).then_some(text)
}
fn face(&mut self, font: Font, scale: f32) -> (u32, u32) {
let key = (font.face.number(), (font.size * scale).round().max(1.0) as u32);
self.fonts.entry(key).or_insert_with(|| font_of(font.face, f64::from(key.1)));
key
}
fn glyph(&mut self, (face, pixels): (u32, u32), letter: char) -> &Glyph {
let font = self.fonts[&(face, pixels)].0;
self.glyphs.entry((face, pixels, letter)).or_insert_with(|| match glyph_in(font, letter) {
Some(glyph) => drawn(font, glyph),
None => fallback(font, letter).map_or_else(|| drawn(font, 0), |(other, glyph)| drawn(other.0, glyph)),
})
}
}
impl TextSystem for SystemText {
fn metrics(&mut self, font: Font, scale: f32) -> Metrics {
let key = self.face(font, scale);
let of = self.fonts[&key].0;
let [ascent, descent, cap, small] = unsafe { [CTFontGetAscent(of), CTFontGetDescent(of), CTFontGetCapHeight(of), CTFontGetXHeight(of)] };
let [ascent, descent, cap, small] = [ascent.ceil(), descent.ceil(), cap.round(), small.round()].map(|pixels| pixels as f32 / scale);
Metrics { ascent, descent, cap_height: cap, x_height: small }
}
fn measure(&mut self, text: &str, font: Font, scale: f32, mut stops: Option<&mut Vec<f32>>) -> f32 {
let (key, mut x) = (self.face(font, scale), 0.0);
for letter in text.chars() {
x += self.glyph(key, letter).advance;
if let Some(stops) = stops.as_deref_mut() {
stops.push(x / scale);
}
}
x / scale
}
fn draw(&mut self, canvas: &mut Canvas, text: &str, x: f32, y: f32, font: Font, color: Color) {
let scale = canvas.scale();
let ascent = (self.metrics(font, scale).ascent * scale).round() as i32;
let (key, mut pen, line) = (self.face(font, scale), (x * scale).round(), (y * scale).round() as i32 + ascent);
for letter in text.chars() {
let glyph = self.glyph(key, letter);
canvas.cover(pen as i32 + glyph.left, line - glyph.top, glyph.width, &glyph.cover, color);
pen += glyph.advance;
}
}
}
fn weight_of(trait_weight: f64) -> u16 {
const WEIGHTS: [(f64, u16); 9] = [(-0.8, 100), (-0.6, 200), (-0.4, 300), (0.0, 400), (0.23, 500), (0.3, 600), (0.4, 700), (0.56, 800), (0.62, 900)];
WEIGHTS.iter().min_by(|(a, _), (b, _)| (a - trait_weight).abs().total_cmp(&(b - trait_weight).abs())).map_or(400, |(_, weight)| *weight)
}
pub(crate) fn listed(family: Option<&str>) -> Vec<(String, Style)> {
let _pool = crate::macos_sys::Pool::new();
let all = Owned(unsafe { CTFontCollectionCreateFromAvailableFonts(std::ptr::null()) });
let found = Owned(unsafe { CTFontCollectionCreateMatchingFontDescriptors(all.0) });
let mut listed = Vec::new();
for descriptor in array_items(found.0.cast_mut()) {
let name = Owned(unsafe { CTFontDescriptorCopyAttribute(descriptor, kCTFontFamilyNameAttribute) });
let Some(name) = rust_string(name.0.cast_mut()).filter(|name| family.is_none_or(|family| family.eq_ignore_ascii_case(name))) else { continue };
let traits = Owned(unsafe { CTFontDescriptorCopyAttribute(descriptor, kCTFontTraitsAttribute) });
listed.push((name, style_of(traits.0.cast_mut())));
}
listed
}
fn style_of(traits: Obj) -> Style {
unsafe {
let weight: Obj = send(traits, c"objectForKey:", (kCTFontWeightTrait,));
let symbolic: Obj = send(traits, c"objectForKey:", (kCTFontSymbolicTrait,));
let (weight, symbolic) = (send::<f64>(weight, c"doubleValue", ()), send::<u32>(symbolic, c"unsignedIntValue", ()));
Style { weight: weight_of(weight), italic: symbolic & ITALIC != 0 }
}
}
pub(crate) fn file_for(face: Face, letter: char) -> Option<PathBuf> {
let _pool = crate::macos_sys::Pool::new();
let font = font_of(face, 12.0);
let other = glyph_in(font.0, letter).is_none().then(|| fallback(font.0, letter)).flatten();
let used = other.as_ref().map_or(font.0, |(other, _)| other.0);
let url = Owned(unsafe { CTFontCopyAttribute(used, kCTFontURLAttribute) });
let path: Obj = unsafe { send(url.0.cast_mut(), c"path", ()) };
rust_string(path).map(PathBuf::from)
}
static EMOJI: std::sync::Mutex<(usize, Vec<u32>)> = std::sync::Mutex::new((0, Vec::new()));
const EMOJI_FILES: usize = 64;
pub(crate) fn emoji_file() -> Option<FontFile> {
let _pool = crate::macos_sys::Pool::new();
let base = font_of(Face::Regular, 12.0);
let (other, glyph) = fallback(base.0, '😀')?;
let url = Owned(unsafe { CTFontCopyAttribute(other.0, kCTFontURLAttribute) });
let path = rust_string(unsafe { send(url.0.cast_mut(), c"path", ()) })?;
let file = (0..4).filter_map(|nth| FontFile::open(&path)?.nth(nth)).find(|font| font.glyph('😀') == glyph)?;
let mut kept = EMOJI.lock().unwrap_or_else(|poisoned| poisoned.into_inner());
if kept.0 == 0 {
kept.0 = unsafe { CTFontCreateCopyWithAttributes(other.0, f64::from(file.units()), std::ptr::null(), std::ptr::null()) } as usize;
}
if kept.1.len() >= EMOJI_FILES {
kept.1.clear();
}
kept.1.push(file.serial());
Some(file)
}
pub(crate) fn sequence(font: &FontFile, letters: &str, glyphs: &mut Vec<Set>) -> Option<f32> {
let shaper = {
let kept = EMOJI.lock().unwrap_or_else(|poisoned| poisoned.into_inner());
kept.1.contains(&font.serial()).then_some(kept.0 as *const c_void)?
};
let _pool = crate::macos_sys::Pool::new();
unsafe {
let looks: Obj = send(crate::macos_sys::class(c"NSDictionary"), c"dictionaryWithObject:forKey:", (shaper, kCTFontAttributeName));
let text: Obj = send(crate::macos_sys::class(c"NSAttributedString"), c"alloc", ());
let text: Obj = send(send::<Obj>(text, c"initWithString:attributes:", (crate::macos_sys::ns_string(letters), looks)), c"autorelease", ());
let line = Owned(CTLineCreateWithAttributedString(text.cast_const()));
glyphs.clear();
for run in array_items(CTLineGetGlyphRuns(line.0).cast_mut()) {
let used = CFDictionaryGetValue(CTRunGetAttributes(run), kCTFontAttributeName);
if !CFEqual(used, shaper) {
return None;
}
let count = CTRunGetGlyphCount(run).max(0) as usize;
let (mut ids, mut places) = (vec![0u16; count], vec![NSPoint::default(); count]);
CTRunGetGlyphs(run, CFRange { location: 0, length: 0 }, ids.as_mut_ptr());
CTRunGetPositions(run, CFRange { location: 0, length: 0 }, places.as_mut_ptr());
let placed = ids.into_iter().zip(places).filter(|(glyph, _)| *glyph != u16::MAX);
glyphs.extend(placed.map(|(glyph, at)| Set { glyph, at: [at.x as f32, at.y as f32] }));
}
Some(CTLineGetTypographicBounds(line.0, std::ptr::null_mut(), std::ptr::null_mut(), std::ptr::null_mut()) as f32)
}
}