@@ -14,8 +14,6 @@ pub(crate) mod imp;
14
14
#[ path = "xetex_font_manager_coretext.rs" ]
15
15
pub ( crate ) mod imp;
16
16
17
- use std:: ffi:: { CStr , CString } ;
18
-
19
17
use crate :: xetex_ext:: Fix2D ;
20
18
use crate :: xetex_ini:: loaded_font_design_size;
21
19
use crate :: xetex_layout_interface:: createFont;
@@ -28,8 +26,6 @@ use self::imp::XeTeXFontMgr_Mac_create;
28
26
29
27
use harfbuzz_sys:: { hb_font_get_face, hb_ot_layout_get_size_params} ;
30
28
31
- use libc:: { strchr, strlen} ;
32
-
33
29
#[ cfg( not( target_os = "macos" ) ) ]
34
30
use imp:: FcPattern ;
35
31
@@ -83,7 +79,7 @@ pub(crate) struct XeTeXFontMgrOpSizeRec {
83
79
#[ derive( Clone ) ]
84
80
#[ repr( C ) ]
85
81
pub ( crate ) struct XeTeXFontMgrFamily {
86
- pub ( crate ) styles : BTreeMap < CString , Rc < RefCell < XeTeXFontMgrFont > > > ,
82
+ pub ( crate ) styles : BTreeMap < String , Rc < RefCell < XeTeXFontMgrFont > > > ,
87
83
pub ( crate ) minWeight : u16 ,
88
84
pub ( crate ) maxWeight : u16 ,
89
85
pub ( crate ) minWidth : u16 ,
@@ -94,10 +90,10 @@ pub(crate) struct XeTeXFontMgrFamily {
94
90
#[ derive( Clone ) ]
95
91
#[ repr( C ) ]
96
92
pub ( crate ) struct XeTeXFontMgrFont {
97
- pub ( crate ) m_fullName : Option < CString > ,
98
- pub ( crate ) m_psName : CString ,
99
- pub ( crate ) m_familyName : Option < CString > ,
100
- pub ( crate ) m_styleName : Option < CString > ,
93
+ pub ( crate ) m_fullName : Option < String > ,
94
+ pub ( crate ) m_psName : String ,
95
+ pub ( crate ) m_familyName : Option < String > ,
96
+ pub ( crate ) m_styleName : Option < String > ,
101
97
pub ( crate ) parent : Option < Rc < RefCell < XeTeXFontMgrFamily > > > ,
102
98
pub ( crate ) fontRef : PlatformFontRef ,
103
99
pub ( crate ) opSizeInfo : XeTeXFontMgrOpSizeRec ,
@@ -112,31 +108,31 @@ pub(crate) struct XeTeXFontMgrFont {
112
108
#[ derive( Clone ) ]
113
109
#[ repr( C ) ]
114
110
pub ( crate ) struct XeTeXFontMgrNameCollection {
115
- pub ( crate ) m_familyNames : VecDeque < CString > ,
116
- pub ( crate ) m_styleNames : VecDeque < CString > ,
117
- pub ( crate ) m_fullNames : VecDeque < CString > ,
118
- pub ( crate ) m_psName : CString ,
119
- pub ( crate ) m_subFamily : CString ,
111
+ pub ( crate ) m_familyNames : VecDeque < String > ,
112
+ pub ( crate ) m_styleNames : VecDeque < String > ,
113
+ pub ( crate ) m_fullNames : VecDeque < String > ,
114
+ pub ( crate ) m_psName : String ,
115
+ pub ( crate ) m_subFamily : String ,
120
116
}
121
117
impl XeTeXFontMgrNameCollection {
122
118
unsafe fn new ( ) -> Self {
123
119
Self {
124
120
m_familyNames : VecDeque :: default ( ) ,
125
121
m_styleNames : VecDeque :: default ( ) ,
126
122
m_fullNames : VecDeque :: default ( ) ,
127
- m_psName : CString :: default ( ) ,
128
- m_subFamily : CString :: default ( ) ,
123
+ m_psName : String :: new ( ) ,
124
+ m_subFamily : String :: new ( ) ,
129
125
}
130
126
}
131
127
}
132
128
133
129
#[ derive( Clone ) ]
134
130
#[ repr( C ) ]
135
131
pub ( crate ) struct XeTeXFontMgr {
136
- pub ( crate ) m_nameToFont : BTreeMap < CString , Rc < RefCell < XeTeXFontMgrFont > > > ,
137
- pub ( crate ) m_nameToFamily : BTreeMap < CString , Rc < RefCell < XeTeXFontMgrFamily > > > ,
132
+ pub ( crate ) m_nameToFont : BTreeMap < String , Rc < RefCell < XeTeXFontMgrFont > > > ,
133
+ pub ( crate ) m_nameToFamily : BTreeMap < String , Rc < RefCell < XeTeXFontMgrFamily > > > ,
138
134
pub ( crate ) m_platformRefToFont : BTreeMap < PlatformFontRef , Rc < RefCell < XeTeXFontMgrFont > > > ,
139
- pub ( crate ) m_psNameToFont : BTreeMap < CString , Rc < RefCell < XeTeXFontMgrFont > > > ,
135
+ pub ( crate ) m_psNameToFont : BTreeMap < String , Rc < RefCell < XeTeXFontMgrFont > > > ,
140
136
// maps PS name (as used in .xdv) to font record
141
137
}
142
138
@@ -196,7 +192,7 @@ impl XeTeXFontMgrFamily {
196
192
}
197
193
}
198
194
impl XeTeXFontMgrFont {
199
- unsafe fn new ( ref_0 : PlatformFontRef , ps_name : CString ) -> Self {
195
+ unsafe fn new ( ref_0 : PlatformFontRef , ps_name : String ) -> Self {
200
196
Self {
201
197
m_fullName : None ,
202
198
m_psName : ps_name,
@@ -334,34 +330,22 @@ pub(crate) unsafe fn XeTeXFontMgr_findFont(
334
330
// SIDE EFFECT: edits /variant/ string in-place removing /B or /I
335
331
// ptSize is in TeX points, or negative for 'scaled' factor
336
332
// "variant" string will be shortened (in-place) by removal of /B and /I if present
337
- let nameStr = CString :: new ( name) . unwrap ( ) ;
338
333
let mut font: Option < Rc < RefCell < XeTeXFontMgrFont > > > = None ;
339
334
let mut dsize: i32 = 100i32 ;
340
335
loaded_font_design_size = Scaled ( 655360 ) ;
341
- for pass in 0 ..2i32 {
336
+ for pass in 0 ..2 {
342
337
// try full name as given
343
- if let Some ( font) = self_0. m_nameToFont . get ( & nameStr ) {
344
- if font. borrow ( ) . opSizeInfo . designSize != 0i32 as libc :: c_uint {
338
+ if let Some ( font) = self_0. m_nameToFont . get ( name ) {
339
+ if font. borrow ( ) . opSizeInfo . designSize != 0 {
345
340
dsize = font. borrow ( ) . opSizeInfo . designSize as i32
346
341
}
347
342
break ;
348
343
}
349
344
// if there's a hyphen, split there and try Family-Style
350
- let nameStr_cstr = nameStr. as_ptr ( ) ;
351
- let nameStr_len = strlen ( nameStr_cstr) as i32 ;
352
- let hyph_pos = strchr ( nameStr_cstr, '-' as i32 ) ;
353
- let hyph = ( if !hyph_pos. is_null ( ) {
354
- hyph_pos. offset_from ( nameStr_cstr) as i64
355
- } else {
356
- -1i32 as i64
357
- } ) as i32 ;
358
- if hyph > 0i32 && hyph < nameStr_len - 1i32 {
359
- let family =
360
- CString :: new ( & CStr :: from_ptr ( nameStr_cstr) . to_bytes ( ) [ ..hyph as usize ] ) . unwrap ( ) ;
345
+ if let Some ( hyph) = name[ ..name. len ( ) - 1 ] . find ( '-' ) {
346
+ let family = name[ ..hyph] . to_string ( ) ;
361
347
if let Some ( family_ptr) = self_0. m_nameToFamily . get ( & family) {
362
- let style =
363
- CString :: new ( & CStr :: from_ptr ( nameStr_cstr) . to_bytes ( ) [ ( hyph as usize + 1 ) ..] )
364
- . unwrap ( ) ;
348
+ let style = name[ hyph + 1 ..] . to_string ( ) ;
365
349
if let Some ( style_FONT_PTR) = family_ptr. borrow ( ) . styles . get ( & style) . cloned ( ) {
366
350
if style_FONT_PTR. borrow ( ) . opSizeInfo . designSize != 0 {
367
351
dsize = style_FONT_PTR. borrow ( ) . opSizeInfo . designSize as i32
@@ -372,14 +356,14 @@ pub(crate) unsafe fn XeTeXFontMgr_findFont(
372
356
}
373
357
}
374
358
// try as PostScript name
375
- if let Some ( font) = self_0. m_psNameToFont . get ( & nameStr ) {
359
+ if let Some ( font) = self_0. m_psNameToFont . get ( name ) {
376
360
if font. borrow ( ) . opSizeInfo . designSize != 0i32 as libc:: c_uint {
377
361
dsize = font. borrow ( ) . opSizeInfo . designSize as i32
378
362
}
379
363
break ;
380
364
}
381
365
// try for the name as a family name
382
- if let Some ( family_ptr) = self_0. m_nameToFamily . get ( & nameStr ) {
366
+ if let Some ( family_ptr) = self_0. m_nameToFamily . get ( name ) {
383
367
// look for a family member with the "regular" bit set in OS/2
384
368
let mut regFonts: i32 = 0i32 ;
385
369
for ( _k, v) in family_ptr. borrow ( ) . styles . iter ( ) {
@@ -394,18 +378,11 @@ pub(crate) unsafe fn XeTeXFontMgr_findFont(
394
378
// which confuses the search above... so try some known names
395
379
if font. is_none ( ) || regFonts > 1i32 {
396
380
// try for style "Regular", "Plain", "Normal", "Roman"
397
- let regular_style_names = [
398
- & b"Regular\x00 " [ ..] ,
399
- & b"Plain\x00 " [ ..] ,
400
- & b"Normal\x00 " [ ..] ,
401
- & b"Roman\x00 " [ ..] ,
402
- ] ;
403
- ' style_name_loop: for style in & regular_style_names {
404
- let style: & [ u8 ] = * style;
405
- let style = CStr :: from_ptr ( style. as_ptr ( ) as * const i8 ) ;
381
+ const regular_style_names: [ & str ; 4 ] = [ "Regular" , "Plain" , "Normal" , "Roman" ] ;
382
+ for & style in regular_style_names. iter ( ) {
406
383
if let Some ( style_FONT_PTR) = family_ptr. borrow ( ) . styles . get ( style) {
407
384
font = Some ( style_FONT_PTR. clone ( ) ) ;
408
- break ' style_name_loop ;
385
+ break ;
409
386
}
410
387
}
411
388
}
@@ -427,7 +404,7 @@ pub(crate) unsafe fn XeTeXFontMgr_findFont(
427
404
// didn't find it in our caches, so do a platform search (may be relatively expensive);
428
405
// this will update the caches with any fonts that seem to match the name given,
429
406
// so that the second pass might find it
430
- self_0. search_for_host_platform_fonts ( nameStr . as_ptr ( ) ) ;
407
+ self_0. search_for_host_platform_fonts ( name ) ;
431
408
}
432
409
}
433
410
if font. is_none ( ) {
@@ -717,7 +694,7 @@ pub(crate) unsafe fn XeTeXFontMgr_findFont(
717
694
pub ( crate ) unsafe fn XeTeXFontMgr_getFullName (
718
695
self_0 : & XeTeXFontMgr ,
719
696
font : PlatformFontRef ,
720
- ) -> * const libc :: c_char {
697
+ ) -> String {
721
698
// return the full name of the font, suitable for use in XeTeX source
722
699
// without requiring style qualifiers
723
700
let FONT_PTR = if let Some ( FONT_PTR ) = self_0. m_platformRefToFont . get ( & font) {
@@ -726,9 +703,9 @@ pub(crate) unsafe fn XeTeXFontMgr_getFullName(
726
703
abort ! ( "internal error {} in XeTeXFontMgr" , 2 ) ;
727
704
} ;
728
705
if let Some ( name) = FONT_PTR . borrow ( ) . m_fullName . as_ref ( ) {
729
- name. as_ptr ( )
706
+ name. clone ( )
730
707
} else {
731
- FONT_PTR . borrow ( ) . m_psName . as_ptr ( )
708
+ FONT_PTR . borrow ( ) . m_psName . clone ( )
732
709
}
733
710
}
734
711
pub ( crate ) unsafe fn XeTeXFontMgr_weightAndWidthDiff (
@@ -878,12 +855,12 @@ impl XeTeXFontMgr {
878
855
// append a name but only if it's not already in the list
879
856
pub ( crate ) unsafe fn XeTeXFontMgr_appendToList (
880
857
mut _self_0 : & XeTeXFontMgr ,
881
- list : & mut VecDeque < CString > ,
882
- cstr : & CStr ,
858
+ list : & mut VecDeque < String > ,
859
+ cstr : & str ,
883
860
) {
884
- fn has_occur ( list : & VecDeque < CString > , val : & CStr ) -> bool {
861
+ fn has_occur ( list : & VecDeque < String > , val : & str ) -> bool {
885
862
for item in list. iter ( ) {
886
- if & * * item == val {
863
+ if item == val {
887
864
return true ;
888
865
}
889
866
}
@@ -892,18 +869,18 @@ pub(crate) unsafe fn XeTeXFontMgr_appendToList(
892
869
if has_occur ( list, cstr) {
893
870
return ;
894
871
}
895
- list. push_back ( cstr. to_owned ( ) ) ;
872
+ list. push_back ( cstr. to_string ( ) ) ;
896
873
}
897
874
// prepend a name, removing it from later in the list if present
898
875
pub ( crate ) unsafe fn XeTeXFontMgr_prependToList (
899
876
_self_0 : & XeTeXFontMgr ,
900
- list : & mut VecDeque < CString > ,
901
- cstr : & CStr ,
877
+ list : & mut VecDeque < String > ,
878
+ cstr : & str ,
902
879
) {
903
- fn remove_first_occur ( list : & mut VecDeque < CString > , val : & CStr ) -> bool {
880
+ fn remove_first_occur ( list : & mut VecDeque < String > , val : & str ) -> bool {
904
881
let mut found_idx = None ;
905
882
for ( idx, item) in list. iter ( ) . enumerate ( ) {
906
- if & * * item == val {
883
+ if item == val {
907
884
found_idx = Some ( idx) ;
908
885
break ;
909
886
}
@@ -917,7 +894,7 @@ pub(crate) unsafe fn XeTeXFontMgr_prependToList(
917
894
}
918
895
919
896
remove_first_occur ( list, cstr) ;
920
- list. push_front ( cstr. to_owned ( ) ) ;
897
+ list. push_front ( cstr. to_string ( ) ) ;
921
898
}
922
899
pub ( crate ) unsafe fn XeTeXFontMgr_addToMaps (
923
900
self_0 : & mut FontMgr ,
@@ -927,7 +904,7 @@ pub(crate) unsafe fn XeTeXFontMgr_addToMaps(
927
904
if self_0. m_platformRefToFont . contains_key ( & platformFont) {
928
905
return ;
929
906
}
930
- if names. m_psName . to_bytes ( ) . is_empty ( ) {
907
+ if names. m_psName . is_empty ( ) {
931
908
return ;
932
909
}
933
910
if self_0. m_psNameToFont . contains_key ( & names. m_psName ) {
@@ -962,7 +939,7 @@ pub(crate) unsafe fn XeTeXFontMgr_addToMaps(
962
939
thisFont. borrow_mut ( ) . m_styleName = if !names. m_styleNames . is_empty ( ) {
963
940
Some ( names. m_styleNames [ 0 ] . clone ( ) )
964
941
} else {
965
- Some ( CString :: default ( ) )
942
+ Some ( String :: new ( ) )
966
943
} ;
967
944
for familyName in names. m_familyNames . iter ( ) {
968
945
let family = if let Some ( family_rc) = self_0. m_nameToFamily . get_mut ( familyName) {
@@ -1037,7 +1014,7 @@ pub(crate) trait FontMgrExt {
1037
1014
type FontRef ;
1038
1015
unsafe fn terminate ( & mut self ) ;
1039
1016
unsafe fn get_platform_font_desc ( & self , font : Self :: FontRef ) -> String ;
1040
- unsafe fn search_for_host_platform_fonts ( & mut self , _ : * const libc :: c_char ) ;
1017
+ unsafe fn search_for_host_platform_fonts ( & mut self , name : & str ) ;
1041
1018
1042
1019
unsafe fn read_names ( & self , _: Self :: FontRef ) -> XeTeXFontMgrNameCollection ;
1043
1020
unsafe fn get_op_size_rec_and_style_flags ( & self , _: & mut XeTeXFontMgrFont ) ;
0 commit comments