@@ -144,6 +144,29 @@ pub fn new_from_name(name: &str, pt_size: f64) -> Result<CTFont, ()> {
144
144
}
145
145
}
146
146
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
+
147
170
impl CTFont {
148
171
// Properties
149
172
pub fn symbolic_traits ( & self ) -> CTFontSymbolicTraits {
@@ -541,7 +564,6 @@ extern {
541
564
fn CTFontCreateWithFontDescriptor ( descriptor : CTFontDescriptorRef , size : CGFloat ,
542
565
matrix : * const CGAffineTransform ) -> CTFontRef ;
543
566
//fn CTFontCreateWithFontDescriptorAndOptions
544
- #[ cfg( test) ]
545
567
fn CTFontCreateUIFontForLanguage ( uiType : CTFontUIFontType , size : CGFloat , language : CFStringRef ) -> CTFontRef ;
546
568
fn CTFontCreateCopyWithAttributes ( font : CTFontRef , size : CGFloat , matrix : * const CGAffineTransform ,
547
569
attributes : CTFontDescriptorRef ) -> CTFontRef ;
@@ -695,11 +717,7 @@ fn macos_version() -> (i32, i32, i32) {
695
717
fn copy_system_font ( ) {
696
718
use crate :: * ;
697
719
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 ) ;
703
721
let big = small. clone_with_font_size ( 20. ) ;
704
722
705
723
// ensure that we end up with different fonts for the different sizes before 10.15
@@ -758,11 +776,7 @@ fn copy_system_font() {
758
776
fn out_of_range_variations ( ) {
759
777
use crate :: * ;
760
778
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 ) ;
766
780
767
781
let axes = small. get_variation_axes ( ) ;
768
782
if macos_version ( ) < ( 10 , 12 , 0 ) {
@@ -819,11 +833,7 @@ fn equal_descriptor_different_font() {
819
833
let variation_attribute = unsafe { CFString :: wrap_under_get_rule ( font_descriptor:: kCTFontVariationAttribute) } ;
820
834
let size_attribute = unsafe { CFString :: wrap_under_get_rule ( font_descriptor:: kCTFontSizeAttribute) } ;
821
835
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 ) ;
827
837
828
838
829
839
// but we can still construct the CGFont by name
@@ -890,11 +900,7 @@ fn equal_descriptor_different_font() {
890
900
fn system_font_variation ( ) {
891
901
use crate :: * ;
892
902
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 ) ;
898
904
899
905
// but we can still construct the CGFont by name
900
906
let ps = small. postscript_name ( ) ;
@@ -919,3 +925,9 @@ fn system_font_variation() {
919
925
920
926
dbg ! ( ct_var_font_desc) ;
921
927
}
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