@@ -2,84 +2,127 @@ use super::{sys, CFString, CFType, CTFontDescriptor, CoreType};
22use std:: ptr;
33use std:: ptr:: NonNull ;
44
5+ /// An attribute that may be found in a font, such as family name or URL.
56#[ derive( Copy , Clone ) ]
67pub enum FontAttribute {
8+ /// Name
79 Name ,
10+ /// Family Name
811 FamilyName ,
12+ /// Display Name
913 DisplayName ,
14+ /// URL
1015 URL ,
16+ /// Cascade List - fallbacks to use if glyph not present in this font.
1117 CascadeList ,
1218}
1319
1420impl FontAttribute {
21+ /// Convert this attribute into its matching [`CFString`].
1522 pub fn to_str ( self ) -> CFString {
16- CFString :: new_borrowed ( NonNull :: new ( self . to_raw ( ) . cast_mut ( ) ) . unwrap ( ) )
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) }
1726 }
1827
28+ /// Convert this attribute into a raw pointer to a [`CFString`].
1929 pub fn to_raw ( self ) -> sys:: CFStringRef {
2030 match self {
31+ // SAFETY: Static guaranteed to exist and by a valid CFStringRef
2132 FontAttribute :: Name => unsafe { sys:: kCTFontNameAttribute } ,
33+ // SAFETY: sic
2234 FontAttribute :: FamilyName => unsafe { sys:: kCTFontFamilyNameAttribute } ,
35+ // SAFETY: sic
2336 FontAttribute :: DisplayName => unsafe { sys:: kCTFontDisplayNameAttribute } ,
37+ // SAFETY: sic
2438 FontAttribute :: URL => unsafe { sys:: kCTFontURLAttribute } ,
39+ // SAFETY: sic
2540 FontAttribute :: CascadeList => unsafe { sys:: kCTFontCascadeListAttribute } ,
2641 }
2742 }
2843}
2944
45+ /// A type of font name that may be available.
3046#[ derive( Copy , Clone ) ]
3147pub enum FontNameKey {
48+ /// Full name
3249 Full ,
50+ /// Family name
3351 Family ,
52+ /// Style name
3453 Style ,
54+ /// PostScript name
3555 PostScript ,
3656}
3757
3858impl FontNameKey {
59+ /// Convert this attribute into its matching [`CFString`].
3960 pub fn to_str ( self ) -> CFString {
40- CFString :: new_borrowed ( NonNull :: new ( self . to_raw ( ) . cast_mut ( ) ) . unwrap ( ) )
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) }
4164 }
4265
4366 fn to_raw ( self ) -> sys:: CFStringRef {
4467 match self {
68+ // SAFETY: Static guaranteed to exist and by a valid CFStringRef
4569 FontNameKey :: Full => unsafe { sys:: kCTFontFullNameKey } ,
70+ // SAFETY: sic
4671 FontNameKey :: Family => unsafe { sys:: kCTFontFamilyNameKey } ,
72+ // SAFETY: sic
4773 FontNameKey :: Style => unsafe { sys:: kCTFontStyleNameKey } ,
74+ // SAFETY: sic
4875 FontNameKey :: PostScript => unsafe { sys:: kCTFontPostScriptNameKey } ,
4976 }
5077 }
5178}
5279
5380cfty ! {
81+ /// A font, combining a descriptor and a size, as well as any other necessary transforms to
82+ /// render glyphs.
5483 CTFont : CTFontGetTypeID
5584}
5685
5786impl CTFont {
87+ /// Create a new font from a [`CTFontDescriptor`] and a size to render at.
5888 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.
5991 let ptr = unsafe {
6092 sys:: CTFontCreateWithFontDescriptor (
6193 descriptor. as_type_ref ( ) ,
6294 size as sys:: CGFloat ,
6395 ptr:: null_mut ( ) ,
6496 )
6597 } ;
66- CTFont :: new_owned ( NonNull :: new ( ptr. cast_mut ( ) ) . unwrap ( ) )
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) }
67101 }
68102
103+ /// Get an attribute of this font, if present
69104 pub fn attr ( & self , attr : FontAttribute ) -> Option < CFType > {
105+ // SAFETY: Internal pointer and attribute string guaranteed valid.
70106 let ptr = unsafe { sys:: CTFontCopyAttribute ( self . as_type_ref ( ) , attr. to_raw ( ) ) } ;
71- NonNull :: new ( ptr. cast_mut ( ) ) . map ( CFType :: new_owned)
107+ // SAFETY: In non-null, returned name guaranteed valid and owned.
108+ NonNull :: new ( ptr. cast_mut ( ) ) . map ( |ptr| unsafe { CFType :: new_owned ( ptr) } )
72109 }
73110
111+ /// Get a name value of this font, if present
74112 pub fn name ( & self , name : FontNameKey ) -> Option < CFString > {
113+ // SAFETY: Internal pointer and name string guaranteed valid.
75114 let ptr = unsafe { sys:: CTFontCopyName ( self . as_type_ref ( ) , name. to_raw ( ) ) } ;
76- NonNull :: new ( ptr. cast_mut ( ) ) . map ( CFString :: new_owned)
115+ // SAFETY: In non-null, returned name guaranteed valid and owned.
116+ NonNull :: new ( ptr. cast_mut ( ) ) . map ( |ptr| unsafe { CFString :: new_owned ( ptr) } )
77117 }
78118
119+ /// Get the name of this font, localized to a specific language
79120 pub fn localized_name ( & self , name : FontNameKey ) -> Option < CFString > {
121+ // SAFETY: Internal pointer and name string guaranteed valid.
80122 let ptr = unsafe {
81123 sys:: CTFontCopyLocalizedName ( self . as_type_ref ( ) , name. to_raw ( ) , ptr:: null_mut ( ) )
82124 } ;
83- NonNull :: new ( ptr. cast_mut ( ) ) . map ( CFString :: new_owned)
125+ // SAFETY: In non-null, returned name guaranteed valid and owned.
126+ NonNull :: new ( ptr. cast_mut ( ) ) . map ( |ptr| unsafe { CFString :: new_owned ( ptr) } )
84127 }
85128}
0 commit comments