1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
use super::*;
use std::fmt;
use std::ffi::CString;
use ffi::wchar_t;
use std::iter::repeat;
use std::ptr;
use std::mem;
use std::char::{decode_utf16, REPLACEMENT_CHARACTER};
use foreign_types::{ForeignType, ForeignTypeRef};
foreign_type! {
type CType = ffi::MLU;
fn drop = ffi::cmsMLUfree;
pub struct MLU;
pub struct MLURef;
}
impl MLU {
pub fn new(items: usize) -> Self {
unsafe {
let handle = ffi::cmsMLUalloc(ptr::null_mut(), items as u32);
assert!(!handle.is_null());
MLU::from_ptr(handle)
}
}
}
impl MLURef {
pub fn set_text_ascii(&mut self, text: &str, locale: Locale) -> bool {
let cstr = CString::new(text).unwrap();
unsafe {
ffi::cmsMLUsetASCII(self.as_ptr(),
locale.language_ptr(),
locale.country_ptr(),
cstr.as_ptr()) != 0
}
}
pub fn set_text(&mut self, text: &str, locale: Locale) -> bool {
let chars: Vec<_> = text.chars()
.map(|c| c as wchar_t)
.chain(repeat(0 as wchar_t).take(1))
.collect();
unsafe {
ffi::cmsMLUsetWide(self.as_ptr(),
locale.language_ptr(),
locale.country_ptr(),
chars[..].as_ptr()) != 0
}
}
pub fn text_ascii(&self, locale: Locale) -> LCMSResult<CString> {
let len = unsafe {
ffi::cmsMLUgetASCII(self.as_ptr(),
locale.language_ptr(),
locale.country_ptr(),
ptr::null_mut(), 0)
};
if len == 0 {
return Err(Error::MissingData);
}
let mut buf = vec![0u8; len as usize];
unsafe {
ffi::cmsMLUgetASCII(self.as_ptr(),
locale.language_ptr(),
locale.country_ptr(),
buf[..].as_ptr() as *mut i8, len);
if let Some(0) = buf.pop() {
for c in &mut buf {
if *c > 127 {*c = b'?'}
}
CString::new(buf).map_err(|_| Error::InvalidString)
} else {
Err(Error::InvalidString)
}
}
}
pub fn text(&self, locale: Locale) -> LCMSResult<String> {
let len_bytes = unsafe {
ffi::cmsMLUgetWide(self.as_ptr(),
locale.language_ptr(),
locale.country_ptr(),
ptr::null_mut(), 0)
};
let len_wchars = len_bytes as usize / mem::size_of::<wchar_t>();
if len_wchars == 0 || (len_bytes&1) != 0 {
return Err(Error::MissingData);
}
let mut buf = vec![0 as wchar_t; len_wchars];
unsafe {
ffi::cmsMLUgetWide(self.as_ptr(),
locale.language_ptr(),
locale.country_ptr(),
buf[..].as_ptr() as *mut wchar_t, len_bytes);
if let Some(0) = buf.pop() {
Ok(decode_utf16(buf.into_iter().map(|c| c as u16))
.map(|r| r.unwrap_or(REPLACEMENT_CHARACTER))
.collect())
} else {
Err(Error::InvalidString)
}
}
}
pub fn tanslations(&self) -> Vec<Locale> {
let count = unsafe { ffi::cmsMLUtranslationsCount(self.as_ptr()) };
let mut out = Vec::with_capacity(count as usize);
for i in 0..count {
let mut locale = Locale::none();
if unsafe {
ffi::cmsMLUtranslationsCodes(self.as_ptr(), i,
locale.language_ptr_mut(),
locale.country_ptr_mut()) != 0
} {
out.push(locale);
}
}
out
}
pub fn tanslation(&self, locale: Locale) -> LCMSResult<Locale> {
let mut out = Locale::none();
if unsafe {
ffi::cmsMLUgetTranslation(self.as_ptr(),
locale.language_ptr(),
locale.country_ptr(),
out.language_ptr_mut(),
out.country_ptr_mut()) != 0
} {
Ok(out)
} else {
Err(Error::MissingData)
}
}
}
impl fmt::Debug for MLURef {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let t = self.text(Locale::none());
write!(f, "MLU({:?} {:?})", if let Ok(ref t) = t { &t } else { "None" }, self.tanslations())
}
}
#[test]
fn mlu() {
let _ = MLU::new(0);
let mut m = MLU::new(1);
assert!(m.set_text("Hello 世界!", Locale::none()));
assert_eq!(Ok("Hello 世界!".to_owned()), m.text(Locale::none()));
assert!(!m.set_text_ascii("エッロル", Locale::none()));
let mut m = MLU::new(1);
assert!(m.set_text_ascii("OK", Locale::none()));
assert_eq!(Ok(CString::new("OK").unwrap()), m.text_ascii(Locale::none()));
}