Skip to content

Commit

Permalink
Destroy FT_BitmapGlyph when BitmapGlyph is droped
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelzoech committed Feb 8, 2018
1 parent f470811 commit 3fb2ebd
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
34 changes: 32 additions & 2 deletions src/bitmap_glyph.rs
@@ -1,13 +1,16 @@
use std::ptr::null_mut;
use { ffi, Bitmap };

#[derive(Copy, Clone)]
pub struct BitmapGlyph {
library_raw: ffi::FT_Library,
raw: ffi::FT_BitmapGlyph
}

impl BitmapGlyph {
pub unsafe fn from_raw(raw: ffi::FT_BitmapGlyph) -> Self {
pub unsafe fn from_raw(library_raw: ffi::FT_Library, raw: ffi::FT_BitmapGlyph) -> Self {
ffi::FT_Reference_Library(library_raw);
BitmapGlyph {
library_raw: library_raw,
raw: raw
}
}
Expand Down Expand Up @@ -38,3 +41,30 @@ impl BitmapGlyph {
}
}
}

impl Clone for BitmapGlyph {
fn clone(&self) -> Self {
let mut target = null_mut();

let err = unsafe {
ffi::FT_Glyph_Copy(self.raw as ffi::FT_Glyph, &mut target)
};
if err == ffi::FT_Err_Ok {
unsafe { BitmapGlyph::from_raw(self.library_raw, target as ffi::FT_BitmapGlyph) }
} else {
panic!("Failed to copy bitmap glyph")
}
}
}

impl Drop for BitmapGlyph {
fn drop(&mut self) {
let err = unsafe {
ffi::FT_Done_Glyph(self.raw as ffi::FT_Glyph);
ffi::FT_Done_Library(self.library_raw)
};
if err != ffi::FT_Err_Ok {
panic!("Failed to drop library")
}
}
}
2 changes: 1 addition & 1 deletion src/glyph.rs
Expand Up @@ -82,7 +82,7 @@ impl Glyph {
ffi::FT_Glyph_To_Bitmap(&mut the_glyph, render_mode as u32, p_origin, 0)
};
if err == ffi::FT_Err_Ok {
Ok(unsafe { BitmapGlyph::from_raw(the_glyph as ffi::FT_BitmapGlyph) })
Ok(unsafe { BitmapGlyph::from_raw(self.library_raw, the_glyph as ffi::FT_BitmapGlyph) })
} else {
Err(err.into())
}
Expand Down

0 comments on commit 3fb2ebd

Please sign in to comment.