@@ -144,6 +144,29 @@ pub fn new_from_name(name: &str, pt_size: f64) -> Result<CTFont, ()> {
144144 }
145145}
146146
147+ pub fn new_ui_font_for_language ( ui_type : CTFontUIFontType ,
148+ size : f64 ,
149+ language : Option < CFString > )
150+ -> CTFont {
151+ unsafe {
152+ let font_ref = CTFontCreateUIFontForLanguage (
153+ ui_type,
154+ size,
155+ language. as_ref ( )
156+ . map ( |x| x. as_concrete_TypeRef ( ) )
157+ . unwrap_or ( std:: ptr:: null ( ) ) ,
158+ ) ;
159+ if font_ref. is_null ( ) {
160+ // CTFontCreateUIFontForLanguage can fail, but is unlikely to do so during
161+ // normal usage (if you pass a bad ui_type it will). To make things more
162+ // convenient, just panic if it fails.
163+ panic ! ( ) ;
164+ } else {
165+ CTFont :: wrap_under_create_rule ( font_ref)
166+ }
167+ }
168+ }
169+
147170impl CTFont {
148171 // Properties
149172 pub fn symbolic_traits ( & self ) -> CTFontSymbolicTraits {
@@ -541,7 +564,6 @@ extern {
541564 fn CTFontCreateWithFontDescriptor ( descriptor : CTFontDescriptorRef , size : CGFloat ,
542565 matrix : * const CGAffineTransform ) -> CTFontRef ;
543566 //fn CTFontCreateWithFontDescriptorAndOptions
544- #[ cfg( test) ]
545567 fn CTFontCreateUIFontForLanguage ( uiType : CTFontUIFontType , size : CGFloat , language : CFStringRef ) -> CTFontRef ;
546568 fn CTFontCreateCopyWithAttributes ( font : CTFontRef , size : CGFloat , matrix : * const CGAffineTransform ,
547569 attributes : CTFontDescriptorRef ) -> CTFontRef ;
@@ -695,11 +717,7 @@ fn macos_version() -> (i32, i32, i32) {
695717fn copy_system_font ( ) {
696718 use crate :: * ;
697719
698- let small = unsafe {
699- CTFont :: wrap_under_create_rule (
700- CTFontCreateUIFontForLanguage ( kCTFontSystemDetailFontType, 19. , std:: ptr:: null ( ) )
701- )
702- } ;
720+ let small = new_ui_font_for_language ( kCTFontSystemDetailFontType, 19. , None ) ;
703721 let big = small. clone_with_font_size ( 20. ) ;
704722
705723 // ensure that we end up with different fonts for the different sizes before 10.15
@@ -758,11 +776,7 @@ fn copy_system_font() {
758776fn out_of_range_variations ( ) {
759777 use crate :: * ;
760778
761- let small = unsafe {
762- CTFont :: wrap_under_create_rule (
763- CTFontCreateUIFontForLanguage ( kCTFontSystemDetailFontType, 19. , std:: ptr:: null ( ) )
764- )
765- } ;
779+ let small = new_ui_font_for_language ( kCTFontSystemDetailFontType, 19. , None ) ;
766780
767781 let axes = small. get_variation_axes ( ) ;
768782 if macos_version ( ) < ( 10 , 12 , 0 ) {
@@ -819,11 +833,7 @@ fn equal_descriptor_different_font() {
819833 let variation_attribute = unsafe { CFString :: wrap_under_get_rule ( font_descriptor:: kCTFontVariationAttribute) } ;
820834 let size_attribute = unsafe { CFString :: wrap_under_get_rule ( font_descriptor:: kCTFontSizeAttribute) } ;
821835
822- let sys_font = unsafe {
823- CTFont :: wrap_under_create_rule (
824- CTFontCreateUIFontForLanguage ( kCTFontSystemDetailFontType, 120. , std:: ptr:: null ( ) )
825- )
826- } ;
836+ let sys_font = new_ui_font_for_language ( kCTFontSystemDetailFontType, 19. , None ) ;
827837
828838
829839 // but we can still construct the CGFont by name
@@ -890,11 +900,7 @@ fn equal_descriptor_different_font() {
890900fn system_font_variation ( ) {
891901 use crate :: * ;
892902
893- let small = unsafe {
894- CTFont :: wrap_under_create_rule (
895- CTFontCreateUIFontForLanguage ( kCTFontSystemDetailFontType, 17. , std:: ptr:: null ( ) )
896- )
897- } ;
903+ let small = new_ui_font_for_language ( kCTFontSystemDetailFontType, 19. , None ) ;
898904
899905 // but we can still construct the CGFont by name
900906 let ps = small. postscript_name ( ) ;
@@ -919,3 +925,9 @@ fn system_font_variation() {
919925
920926 dbg ! ( ct_var_font_desc) ;
921927}
928+
929+ #[ test]
930+ fn ui_font ( ) {
931+ // pass some garbagey inputs
932+ new_ui_font_for_language ( kCTFontSystemDetailFontType, 10000009. , Some ( CFString :: from ( "Gofld" ) ) ) ;
933+ }
0 commit comments