use std::{collections::HashMap, ffi::c_void, sync::Mutex};
use objc2::{
class,
encode::{Encode, Encoding, RefEncode},
msg_send,
rc::Retained,
runtime::AnyObject,
};
use crate::{images::ImageData, node::color::Color};
type Ref = *const c_void;
#[repr(C)]
#[derive(Clone, Copy)]
struct CGSize {
width: f64,
height: f64,
}
unsafe impl Encode for CGSize {
const ENCODING: Encoding = Encoding::Struct("CGSize", &[f64::ENCODING, f64::ENCODING]);
}
#[repr(C)]
#[derive(Clone, Copy)]
struct CGRect {
x: f64,
y: f64,
width: f64,
height: f64,
}
#[repr(C)]
struct CGImage {
_opaque: [u8; 0],
}
unsafe impl RefEncode for CGImage {
const ENCODING_REF: Encoding = Encoding::Pointer(&Encoding::Struct("CGImage", &[]));
}
#[link(name = "UIKit", kind = "framework")]
unsafe extern "C" {}
#[link(name = "CoreGraphics", kind = "framework")]
unsafe extern "C" {
fn CGColorSpaceCreateDeviceRGB() -> Ref;
fn CGColorSpaceRelease(space: Ref);
fn CGBitmapContextCreate(
data: *mut c_void,
width: usize,
height: usize,
bits_per_component: usize,
bytes_per_row: usize,
space: Ref,
bitmap_info: u32,
) -> Ref;
fn CGContextDrawImage(context: Ref, rect: CGRect, image: Ref);
fn CGContextRelease(context: Ref);
}
const PREMULTIPLIED_LAST: u32 = 1;
const SEMIBOLD: isize = 6;
const LARGE: isize = 3;
const SCALE: f64 = 3.0;
#[derive(Clone)]
pub(crate) struct Symbol {
pub(crate) image: ImageData,
pub(crate) width: f32,
pub(crate) height: f32,
}
pub(crate) fn symbol(name: &str, point_size: f32, color: Color) -> Option<Symbol> {
static DRAWN: Mutex<Option<HashMap<(String, u32, [u8; 3]), Option<Symbol>>>> = Mutex::new(None);
let key = (name.to_owned(), point_size.to_bits(), [color.r(), color.g(), color.b()]);
let mut drawn = DRAWN.lock().unwrap();
drawn
.get_or_insert_with(HashMap::new)
.entry(key)
.or_insert_with(|| draw(name, point_size, color))
.clone()
}
fn draw(name: &str, point_size: f32, color: Color) -> Option<Symbol> {
let units: Vec<u16> = name.encode_utf16().collect();
unsafe {
let name: Retained<AnyObject> =
msg_send![class!(NSString), stringWithCharacters: units.as_ptr(), length: units.len()];
let configuration: Retained<AnyObject> = msg_send![
class!(UIImageSymbolConfiguration),
configurationWithPointSize: f64::from(point_size),
weight: SEMIBOLD,
scale: LARGE
];
let image: Option<Retained<AnyObject>> =
msg_send![class!(UIImage), systemImageNamed: &*name, withConfiguration: &*configuration];
let image = image?;
let size: CGSize = msg_send![&*image, size];
let shape: *const CGImage = msg_send![&*image, CGImage];
let shape: Ref = shape.cast();
if shape.is_null() || size.width <= 0.0 || size.height <= 0.0 {
return None;
}
let (width, height) = (
(size.width * SCALE).ceil() as usize,
(size.height * SCALE).ceil() as usize,
);
let mut pixels = vec![0u8; width * height * 4];
let space = CGColorSpaceCreateDeviceRGB();
let context = CGBitmapContextCreate(
pixels.as_mut_ptr().cast(),
width,
height,
8,
width * 4,
space,
PREMULTIPLIED_LAST,
);
CGColorSpaceRelease(space);
if context.is_null() {
return None;
}
CGContextDrawImage(
context,
CGRect {
x: 0.0,
y: 0.0,
width: width as f64,
height: height as f64,
},
shape,
);
CGContextRelease(context);
let (red, green, blue) = (color.r(), color.g(), color.b());
for pixel in pixels.chunks_exact_mut(4) {
pixel[0] = red;
pixel[1] = green;
pixel[2] = blue;
}
Some(Symbol {
image: ImageData::from_rgba(pixels, width as u32, height as u32),
width: size.width as f32,
height: size.height as f32,
})
}
}