Skip to content

Commit 8523f56

Browse files
authored
Merge pull request #1681 from LeoSchae/main
Fix bug in bindings of pango::shape_full and pango::shape_with_flags
2 parents a6810cb + 5cec41c commit 8523f56

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

pango/src/functions.rs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Take a look at the license at the top of the repository in the LICENSE file.
22

33
use glib::translate::*;
4+
use std::{ffi::c_char, ptr};
45

56
pub use crate::auto::functions::*;
67
#[cfg(feature = "v1_44")]
@@ -23,17 +24,17 @@ pub fn shape_full(
2324
analysis: &Analysis,
2425
glyphs: &mut GlyphString,
2526
) {
26-
let paragraph_length = match paragraph_text {
27-
Some(s) => s.len(),
28-
None => 0,
29-
} as i32;
30-
let paragraph_text = paragraph_text.to_glib_none();
3127
let item_length = item_text.len() as i32;
28+
let paragraph_length = paragraph_text.map(|t| t.len() as i32).unwrap_or_default();
29+
let paragraph_ptr = paragraph_text.map_or(ptr::null(), |t| t.as_ptr() as *const c_char);
3230
unsafe {
31+
// The function does not take null-terminated strings when a length is provided.
32+
// It also requires item_text to point to a subsequence of paragraph_text.
33+
// Using to_glib_none() on &str will copy the string and cause problems.
3334
ffi::pango_shape_full(
34-
item_text.to_glib_none().0,
35+
item_text.as_ptr() as *const c_char,
3536
item_length,
36-
paragraph_text.0,
37+
paragraph_ptr,
3738
paragraph_length,
3839
analysis.to_glib_none().0,
3940
glyphs.to_glib_none_mut().0,
@@ -53,11 +54,13 @@ pub fn shape_with_flags(
5354
) {
5455
let item_length = item_text.len() as i32;
5556
let paragraph_length = paragraph_text.map(|t| t.len() as i32).unwrap_or_default();
57+
let paragraph_ptr = paragraph_text.map_or(ptr::null(), |t| t.as_ptr() as *const c_char);
5658
unsafe {
59+
// See: shape_full
5760
ffi::pango_shape_with_flags(
58-
item_text.to_glib_none().0,
61+
item_text.as_ptr() as *const c_char,
5962
item_length,
60-
paragraph_text.to_glib_none().0,
63+
paragraph_ptr,
6164
paragraph_length,
6265
analysis.to_glib_none().0,
6366
glyphs.to_glib_none_mut().0,

0 commit comments

Comments
 (0)