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
use std::ptr::null_mut;
use { ffi, Bitmap };

pub struct BitmapGlyph {
    library_raw: ffi::FT_Library,
    raw: ffi::FT_BitmapGlyph
}

impl BitmapGlyph {
    pub unsafe fn from_raw(library_raw: ffi::FT_Library, raw: ffi::FT_BitmapGlyph) -> Self {
        ffi::FT_Reference_Library(library_raw);
        BitmapGlyph { library_raw, raw }
    }

    #[inline(always)]
    pub fn left(&self) -> i32 {
        unsafe {
            (*self.raw).left
        }
    }

    #[inline(always)]
    pub fn top(&self) -> i32 {
        unsafe {
            (*self.raw).top
        }
    }

    #[inline(always)]
    pub fn bitmap(&self) -> Bitmap {
        unsafe { Bitmap::from_raw(&(*self.raw).bitmap) }
    }

    #[inline(always)]
    pub fn raw(&self) -> &ffi::FT_BitmapGlyphRec {
        unsafe {
            &*self.raw
        }
    }
}

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")
        }
    }
}