|
| 1 | +use super::{sys, CFString, CFType, CTFontDescriptor, CoreType}; |
| 2 | +use std::ptr; |
| 3 | +use std::ptr::NonNull; |
| 4 | + |
| 5 | +/// An attribute that may be found in a font, such as family name or URL. |
| 6 | +#[derive(Copy, Clone)] |
| 7 | +pub enum FontAttribute { |
| 8 | + /// Name |
| 9 | + Name, |
| 10 | + /// Family Name |
| 11 | + FamilyName, |
| 12 | + /// Display Name |
| 13 | + DisplayName, |
| 14 | + /// URL |
| 15 | + URL, |
| 16 | + /// Cascade List - fallbacks to use if glyph not present in this font. |
| 17 | + CascadeList, |
| 18 | +} |
| 19 | + |
| 20 | +impl FontAttribute { |
| 21 | + /// Convert this attribute into its matching [`CFString`]. |
| 22 | + pub fn to_str(self) -> CFString { |
| 23 | + let ptr = NonNull::new(self.to_raw().cast_mut()).unwrap(); |
| 24 | + // SAFETY: The value returned by `to_raw` is guaranteed to be a valid CFStringRef |
| 25 | + unsafe { CFString::new_borrowed(ptr) } |
| 26 | + } |
| 27 | + |
| 28 | + /// Convert this attribute into a raw pointer to a [`CFString`]. |
| 29 | + pub fn to_raw(self) -> sys::CFStringRef { |
| 30 | + match self { |
| 31 | + // SAFETY: Static guaranteed to exist and by a valid CFStringRef |
| 32 | + FontAttribute::Name => unsafe { sys::kCTFontNameAttribute }, |
| 33 | + // SAFETY: sic |
| 34 | + FontAttribute::FamilyName => unsafe { sys::kCTFontFamilyNameAttribute }, |
| 35 | + // SAFETY: sic |
| 36 | + FontAttribute::DisplayName => unsafe { sys::kCTFontDisplayNameAttribute }, |
| 37 | + // SAFETY: sic |
| 38 | + FontAttribute::URL => unsafe { sys::kCTFontURLAttribute }, |
| 39 | + // SAFETY: sic |
| 40 | + FontAttribute::CascadeList => unsafe { sys::kCTFontCascadeListAttribute }, |
| 41 | + } |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +/// A type of font name that may be available. |
| 46 | +#[derive(Copy, Clone)] |
| 47 | +pub enum FontNameKey { |
| 48 | + /// Full name |
| 49 | + Full, |
| 50 | + /// Family name |
| 51 | + Family, |
| 52 | + /// Style name |
| 53 | + Style, |
| 54 | + /// PostScript name |
| 55 | + PostScript, |
| 56 | +} |
| 57 | + |
| 58 | +impl FontNameKey { |
| 59 | + /// Convert this attribute into its matching [`CFString`]. |
| 60 | + pub fn to_str(self) -> CFString { |
| 61 | + let ptr = NonNull::new(self.to_raw().cast_mut()).unwrap(); |
| 62 | + // SAFETY: The value returned by `to_raw` is guaranteed to be a valid CFStringRef |
| 63 | + unsafe { CFString::new_borrowed(ptr) } |
| 64 | + } |
| 65 | + |
| 66 | + fn to_raw(self) -> sys::CFStringRef { |
| 67 | + match self { |
| 68 | + // SAFETY: Static guaranteed to exist and by a valid CFStringRef |
| 69 | + FontNameKey::Full => unsafe { sys::kCTFontFullNameKey }, |
| 70 | + // SAFETY: sic |
| 71 | + FontNameKey::Family => unsafe { sys::kCTFontFamilyNameKey }, |
| 72 | + // SAFETY: sic |
| 73 | + FontNameKey::Style => unsafe { sys::kCTFontStyleNameKey }, |
| 74 | + // SAFETY: sic |
| 75 | + FontNameKey::PostScript => unsafe { sys::kCTFontPostScriptNameKey }, |
| 76 | + } |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +cfty! { |
| 81 | + /// A font, combining a descriptor and a size, as well as any other necessary transforms to |
| 82 | + /// render glyphs. |
| 83 | + CTFont : CTFontGetTypeID |
| 84 | +} |
| 85 | + |
| 86 | +impl CTFont { |
| 87 | + /// Create a new font from a [`CTFontDescriptor`] and a size to render at. |
| 88 | + pub fn new_descriptor(descriptor: &CTFontDescriptor, size: f64) -> CTFont { |
| 89 | + // SAFETY: Provided descriptor is guaranteed valid. Any size will work. Null matrix is always |
| 90 | + // allowed. |
| 91 | + let ptr = unsafe { |
| 92 | + sys::CTFontCreateWithFontDescriptor( |
| 93 | + descriptor.as_type_ref(), |
| 94 | + size as sys::CGFloat, |
| 95 | + ptr::null_mut(), |
| 96 | + ) |
| 97 | + }; |
| 98 | + let ptr = NonNull::new(ptr.cast_mut()).unwrap(); |
| 99 | + // SAFETY: If non-null, pointer from CTFontCreateWithFontDescriptor is a new, owned CTFont. |
| 100 | + unsafe { CTFont::new_owned(ptr) } |
| 101 | + } |
| 102 | + |
| 103 | + /// Get an attribute of this font, if present |
| 104 | + pub fn attr(&self, attr: FontAttribute) -> Option<CFType> { |
| 105 | + // SAFETY: Internal pointer and attribute string guaranteed valid. |
| 106 | + let ptr = unsafe { sys::CTFontCopyAttribute(self.as_type_ref(), attr.to_raw()) }; |
| 107 | + // SAFETY: In non-null, returned name guaranteed valid and owned. |
| 108 | + NonNull::new(ptr.cast_mut()).map(|ptr| unsafe { CFType::new_owned(ptr) }) |
| 109 | + } |
| 110 | + |
| 111 | + /// Get a name value of this font, if present |
| 112 | + pub fn name(&self, name: FontNameKey) -> Option<CFString> { |
| 113 | + // SAFETY: Internal pointer and name string guaranteed valid. |
| 114 | + let ptr = unsafe { sys::CTFontCopyName(self.as_type_ref(), name.to_raw()) }; |
| 115 | + // SAFETY: In non-null, returned name guaranteed valid and owned. |
| 116 | + NonNull::new(ptr.cast_mut()).map(|ptr| unsafe { CFString::new_owned(ptr) }) |
| 117 | + } |
| 118 | + |
| 119 | + /// Get the name of this font, localized to a specific language |
| 120 | + pub fn localized_name(&self, name: FontNameKey) -> Option<CFString> { |
| 121 | + // SAFETY: Internal pointer and name string guaranteed valid. |
| 122 | + let ptr = unsafe { |
| 123 | + sys::CTFontCopyLocalizedName(self.as_type_ref(), name.to_raw(), ptr::null_mut()) |
| 124 | + }; |
| 125 | + // SAFETY: In non-null, returned name guaranteed valid and owned. |
| 126 | + NonNull::new(ptr.cast_mut()).map(|ptr| unsafe { CFString::new_owned(ptr) }) |
| 127 | + } |
| 128 | +} |
0 commit comments