Skip to content

Commit f97ceec

Browse files
committed
Change font-loading methods to use codepoint count instead of byte count
1 parent c7227f6 commit f97ceec

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

raylib/src/core/text.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ impl Drop for Codepoints {
103103
}
104104
}
105105

106+
const TOO_MANY_CODEPOINTS: &str = "fonts exceeding 2,147,483,647 characters are not supported; at most 1,112,064 characters can possibly be encoded in utf-8.";
107+
106108
impl RaylibHandle {
107109
#[must_use]
108110
/// Load all codepoints from a UTF-8 text string, codepoints count returned by parameter
@@ -113,7 +115,7 @@ impl RaylibHandle {
113115

114116
unsafe {
115117
Codepoints(std::mem::ManuallyDrop::new(Box::from_raw(
116-
std::slice::from_raw_parts_mut(u, text.len()),
118+
std::slice::from_raw_parts_mut(u, len.try_into().expect("codepoint count should never be negative")),
117119
)))
118120
}
119121
}
@@ -166,7 +168,7 @@ impl RaylibHandle {
166168
c_filename.as_ptr(),
167169
font_size,
168170
co.0.as_mut_ptr(),
169-
c.len() as i32,
171+
co.0.len().try_into().expect(TOO_MANY_CODEPOINTS),
170172
)
171173
}
172174
None => ffi::LoadFontEx(c_filename.as_ptr(), font_size, std::ptr::null_mut(), 0),
@@ -220,7 +222,7 @@ impl RaylibHandle {
220222
file_data.len() as i32,
221223
font_size,
222224
co.0.as_mut_ptr(),
223-
c.len() as i32,
225+
co.0.len().try_into().expect(TOO_MANY_CODEPOINTS),
224226
)
225227
}
226228
None => ffi::LoadFontFromMemory(
@@ -257,7 +259,7 @@ impl RaylibHandle {
257259
data.len() as i32,
258260
font_size,
259261
co.0.as_mut_ptr(),
260-
c.len() as i32,
262+
co.0.len().try_into().expect(TOO_MANY_CODEPOINTS),
261263
sdf,
262264
)
263265
}

0 commit comments

Comments
 (0)