Skip to content

Commit d525703

Browse files
committed
rust strings
1 parent 5d5905f commit d525703

5 files changed

+95
-124
lines changed

engine/src/xetex_ext.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -915,10 +915,7 @@ pub(crate) unsafe fn find_native_font(uname: &str, mut scaled_size: Scaled) -> O
915915
let fontRef = findFontByName(&nameString, &mut varString, Fix2D(scaled_size));
916916
if !fontRef.is_null() {
917917
/* update name_of_font to the full name of the font, for error messages during font loading */
918-
let fullName: *const i8 = getFullName(fontRef);
919-
name_of_font = std::ffi::CStr::from_ptr(fullName)
920-
.to_string_lossy()
921-
.to_string();
918+
name_of_font = getFullName(fontRef);
922919
if scaled_size < Scaled::ZERO {
923920
if let Some(font) = createFont(fontRef, scaled_size) {
924921
let dsize_0 = D2Fix(getDesignSize(&font));

engine/src/xetex_font_manager.rs

+44-67
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ pub(crate) mod imp;
1414
#[path = "xetex_font_manager_coretext.rs"]
1515
pub(crate) mod imp;
1616

17-
use std::ffi::{CStr, CString};
18-
1917
use crate::xetex_ext::Fix2D;
2018
use crate::xetex_ini::loaded_font_design_size;
2119
use crate::xetex_layout_interface::createFont;
@@ -28,8 +26,6 @@ use self::imp::XeTeXFontMgr_Mac_create;
2826

2927
use harfbuzz_sys::{hb_font_get_face, hb_ot_layout_get_size_params};
3028

31-
use libc::{strchr, strlen};
32-
3329
#[cfg(not(target_os = "macos"))]
3430
use imp::FcPattern;
3531

@@ -83,7 +79,7 @@ pub(crate) struct XeTeXFontMgrOpSizeRec {
8379
#[derive(Clone)]
8480
#[repr(C)]
8581
pub(crate) struct XeTeXFontMgrFamily {
86-
pub(crate) styles: BTreeMap<CString, Rc<RefCell<XeTeXFontMgrFont>>>,
82+
pub(crate) styles: BTreeMap<String, Rc<RefCell<XeTeXFontMgrFont>>>,
8783
pub(crate) minWeight: u16,
8884
pub(crate) maxWeight: u16,
8985
pub(crate) minWidth: u16,
@@ -94,10 +90,10 @@ pub(crate) struct XeTeXFontMgrFamily {
9490
#[derive(Clone)]
9591
#[repr(C)]
9692
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>,
10197
pub(crate) parent: Option<Rc<RefCell<XeTeXFontMgrFamily>>>,
10298
pub(crate) fontRef: PlatformFontRef,
10399
pub(crate) opSizeInfo: XeTeXFontMgrOpSizeRec,
@@ -112,31 +108,31 @@ pub(crate) struct XeTeXFontMgrFont {
112108
#[derive(Clone)]
113109
#[repr(C)]
114110
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,
120116
}
121117
impl XeTeXFontMgrNameCollection {
122118
unsafe fn new() -> Self {
123119
Self {
124120
m_familyNames: VecDeque::default(),
125121
m_styleNames: VecDeque::default(),
126122
m_fullNames: VecDeque::default(),
127-
m_psName: CString::default(),
128-
m_subFamily: CString::default(),
123+
m_psName: String::new(),
124+
m_subFamily: String::new(),
129125
}
130126
}
131127
}
132128

133129
#[derive(Clone)]
134130
#[repr(C)]
135131
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>>>,
138134
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>>>,
140136
// maps PS name (as used in .xdv) to font record
141137
}
142138

@@ -196,7 +192,7 @@ impl XeTeXFontMgrFamily {
196192
}
197193
}
198194
impl XeTeXFontMgrFont {
199-
unsafe fn new(ref_0: PlatformFontRef, ps_name: CString) -> Self {
195+
unsafe fn new(ref_0: PlatformFontRef, ps_name: String) -> Self {
200196
Self {
201197
m_fullName: None,
202198
m_psName: ps_name,
@@ -334,34 +330,22 @@ pub(crate) unsafe fn XeTeXFontMgr_findFont(
334330
// SIDE EFFECT: edits /variant/ string in-place removing /B or /I
335331
// ptSize is in TeX points, or negative for 'scaled' factor
336332
// "variant" string will be shortened (in-place) by removal of /B and /I if present
337-
let nameStr = CString::new(name).unwrap();
338333
let mut font: Option<Rc<RefCell<XeTeXFontMgrFont>>> = None;
339334
let mut dsize: i32 = 100i32;
340335
loaded_font_design_size = Scaled(655360);
341-
for pass in 0..2i32 {
336+
for pass in 0..2 {
342337
// 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 {
345340
dsize = font.borrow().opSizeInfo.designSize as i32
346341
}
347342
break;
348343
}
349344
// 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();
361347
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();
365349
if let Some(style_FONT_PTR) = family_ptr.borrow().styles.get(&style).cloned() {
366350
if style_FONT_PTR.borrow().opSizeInfo.designSize != 0 {
367351
dsize = style_FONT_PTR.borrow().opSizeInfo.designSize as i32
@@ -372,14 +356,14 @@ pub(crate) unsafe fn XeTeXFontMgr_findFont(
372356
}
373357
}
374358
// 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) {
376360
if font.borrow().opSizeInfo.designSize != 0i32 as libc::c_uint {
377361
dsize = font.borrow().opSizeInfo.designSize as i32
378362
}
379363
break;
380364
}
381365
// 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) {
383367
// look for a family member with the "regular" bit set in OS/2
384368
let mut regFonts: i32 = 0i32;
385369
for (_k, v) in family_ptr.borrow().styles.iter() {
@@ -394,18 +378,11 @@ pub(crate) unsafe fn XeTeXFontMgr_findFont(
394378
// which confuses the search above... so try some known names
395379
if font.is_none() || regFonts > 1i32 {
396380
// 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() {
406383
if let Some(style_FONT_PTR) = family_ptr.borrow().styles.get(style) {
407384
font = Some(style_FONT_PTR.clone());
408-
break 'style_name_loop;
385+
break;
409386
}
410387
}
411388
}
@@ -427,7 +404,7 @@ pub(crate) unsafe fn XeTeXFontMgr_findFont(
427404
// didn't find it in our caches, so do a platform search (may be relatively expensive);
428405
// this will update the caches with any fonts that seem to match the name given,
429406
// 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);
431408
}
432409
}
433410
if font.is_none() {
@@ -717,7 +694,7 @@ pub(crate) unsafe fn XeTeXFontMgr_findFont(
717694
pub(crate) unsafe fn XeTeXFontMgr_getFullName(
718695
self_0: &XeTeXFontMgr,
719696
font: PlatformFontRef,
720-
) -> *const libc::c_char {
697+
) -> String {
721698
// return the full name of the font, suitable for use in XeTeX source
722699
// without requiring style qualifiers
723700
let FONT_PTR = if let Some(FONT_PTR) = self_0.m_platformRefToFont.get(&font) {
@@ -726,9 +703,9 @@ pub(crate) unsafe fn XeTeXFontMgr_getFullName(
726703
abort!("internal error {} in XeTeXFontMgr", 2);
727704
};
728705
if let Some(name) = FONT_PTR.borrow().m_fullName.as_ref() {
729-
name.as_ptr()
706+
name.clone()
730707
} else {
731-
FONT_PTR.borrow().m_psName.as_ptr()
708+
FONT_PTR.borrow().m_psName.clone()
732709
}
733710
}
734711
pub(crate) unsafe fn XeTeXFontMgr_weightAndWidthDiff(
@@ -878,12 +855,12 @@ impl XeTeXFontMgr {
878855
// append a name but only if it's not already in the list
879856
pub(crate) unsafe fn XeTeXFontMgr_appendToList(
880857
mut _self_0: &XeTeXFontMgr,
881-
list: &mut VecDeque<CString>,
882-
cstr: &CStr,
858+
list: &mut VecDeque<String>,
859+
cstr: &str,
883860
) {
884-
fn has_occur(list: &VecDeque<CString>, val: &CStr) -> bool {
861+
fn has_occur(list: &VecDeque<String>, val: &str) -> bool {
885862
for item in list.iter() {
886-
if &**item == val {
863+
if item == val {
887864
return true;
888865
}
889866
}
@@ -892,18 +869,18 @@ pub(crate) unsafe fn XeTeXFontMgr_appendToList(
892869
if has_occur(list, cstr) {
893870
return;
894871
}
895-
list.push_back(cstr.to_owned());
872+
list.push_back(cstr.to_string());
896873
}
897874
// prepend a name, removing it from later in the list if present
898875
pub(crate) unsafe fn XeTeXFontMgr_prependToList(
899876
_self_0: &XeTeXFontMgr,
900-
list: &mut VecDeque<CString>,
901-
cstr: &CStr,
877+
list: &mut VecDeque<String>,
878+
cstr: &str,
902879
) {
903-
fn remove_first_occur(list: &mut VecDeque<CString>, val: &CStr) -> bool {
880+
fn remove_first_occur(list: &mut VecDeque<String>, val: &str) -> bool {
904881
let mut found_idx = None;
905882
for (idx, item) in list.iter().enumerate() {
906-
if &**item == val {
883+
if item == val {
907884
found_idx = Some(idx);
908885
break;
909886
}
@@ -917,7 +894,7 @@ pub(crate) unsafe fn XeTeXFontMgr_prependToList(
917894
}
918895

919896
remove_first_occur(list, cstr);
920-
list.push_front(cstr.to_owned());
897+
list.push_front(cstr.to_string());
921898
}
922899
pub(crate) unsafe fn XeTeXFontMgr_addToMaps(
923900
self_0: &mut FontMgr,
@@ -927,7 +904,7 @@ pub(crate) unsafe fn XeTeXFontMgr_addToMaps(
927904
if self_0.m_platformRefToFont.contains_key(&platformFont) {
928905
return;
929906
}
930-
if names.m_psName.to_bytes().is_empty() {
907+
if names.m_psName.is_empty() {
931908
return;
932909
}
933910
if self_0.m_psNameToFont.contains_key(&names.m_psName) {
@@ -962,7 +939,7 @@ pub(crate) unsafe fn XeTeXFontMgr_addToMaps(
962939
thisFont.borrow_mut().m_styleName = if !names.m_styleNames.is_empty() {
963940
Some(names.m_styleNames[0].clone())
964941
} else {
965-
Some(CString::default())
942+
Some(String::new())
966943
};
967944
for familyName in names.m_familyNames.iter() {
968945
let family = if let Some(family_rc) = self_0.m_nameToFamily.get_mut(familyName) {
@@ -1037,7 +1014,7 @@ pub(crate) trait FontMgrExt {
10371014
type FontRef;
10381015
unsafe fn terminate(&mut self);
10391016
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);
10411018

10421019
unsafe fn read_names(&self, _: Self::FontRef) -> XeTeXFontMgrNameCollection;
10431020
unsafe fn get_op_size_rec_and_style_flags(&self, _: &mut XeTeXFontMgrFont);

engine/src/xetex_font_manager_coretext.rs

+10-12
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ pub(crate) unsafe fn XeTeXFontMgr_findFontWithName(
115115
pub(crate) unsafe fn XeTeXFontMgr_Mac_appendNameToList(
116116
self_0: &XeTeXFontMgr_Mac,
117117
font: CTFontRef,
118-
nameList: &mut VecDeque<CString>,
118+
nameList: &mut VecDeque<String>,
119119
nameKey: CFStringRef,
120120
) {
121121
let name: CFStringRef = CTFontCopyName(font, nameKey);
@@ -228,16 +228,17 @@ impl FontMgrExt for XeTeXFontMgr_Mac {
228228
crate::c_pointer_to_str(path).to_string()
229229
}
230230
}
231-
unsafe fn search_for_host_platform_fonts(&mut self, name: *const libc::c_char) {
231+
unsafe fn search_for_host_platform_fonts(&mut self, name: &str) {
232232
// the name might be:
233233
// FullName
234234
// Family-Style (if there's a hyphen)
235235
// PSName
236236
// Family
237237
// ...so we need to try it as each of these
238+
let cname = CString::new(name).unwrap();
238239
let nameStr = CFStringCreateWithCString(
239240
kCFAllocatorDefault,
240-
name,
241+
cname.as_ptr(),
241242
kCFStringEncodingUTF8 as libc::c_int as CFStringEncoding,
242243
);
243244
let mut matched: CTFontDescriptorRef =
@@ -248,14 +249,8 @@ impl FontMgrExt for XeTeXFontMgr_Mac {
248249
CFRelease(matched as CFTypeRef);
249250
return;
250251
}
251-
let hyph_pos = strchr(name, '-' as i32);
252-
let hyph = (if !hyph_pos.is_null() {
253-
hyph_pos.offset_from(name) as libc::c_long
254-
} else {
255-
-1i32 as libc::c_long
256-
}) as libc::c_int;
257-
if hyph > 0i32 && (hyph as usize) < strlen(name) - 1 {
258-
let family = CString::new(&CStr::from_ptr(name).to_bytes()[..hyph as usize]).unwrap();
252+
if let Some(hyph) = name[..name.len() - 1].find('-') {
253+
let family = CString::new(&name[..hyph]).unwrap();
259254
let familyStr = CFStringCreateWithCString(
260255
kCFAllocatorDefault,
261256
family.as_ptr(),
@@ -308,7 +303,10 @@ impl FontMgrExt for XeTeXFontMgr_Mac {
308303
}
309304
autoreleasepool(|| {
310305
let psName: *const NSString = psName.cast();
311-
names.m_psName = CStr::from_ptr(msg_send![psName, UTF8String]).to_owned();
306+
names.m_psName = CStr::from_ptr(msg_send![psName, UTF8String])
307+
.to_str()
308+
.unwrap()
309+
.to_string();
312310
CFRelease(psName as CFTypeRef);
313311
let font = CTFontCreateWithFontDescriptor(fontRef, 0.0f64, ptr::null());
314312
XeTeXFontMgr_Mac_appendNameToList(

0 commit comments

Comments
 (0)