freetype/
bitmap.rs

1use core::slice;
2use {ffi, Error, FtResult};
3
4/// An enumeration type used to describe the format of pixels in a given bitmap. Note that
5/// additional formats may be added in the future.
6#[derive(Copy, Clone, Debug, PartialEq, Eq)]
7pub enum PixelMode {
8    /// This value is reserved.
9    None,
10
11    /// A monochrome bitmap, using 1 bit per pixel. Note that pixels are
12    /// stored in most-significant order (MSB), which means that the left-most
13    /// pixel in a byte has value 128.
14    Mono,
15
16    /// An 8-bit bitmap, generally used to represent anti-aliased glyph images.
17    /// Each pixel is stored in one byte. Note that the number of `gray`
18    /// levels is stored in the `num_grays` field of the FT_Bitmap structure
19    /// (it generally is 256).
20    Gray,
21
22    /// A 2-bit per pixel bitmap, used to represent embedded anti-aliased
23    /// bitmaps in font files according to the OpenType specification. We
24    /// haven't found a single font using this format, however.
25    Gray2,
26
27    /// A 4-bit per pixel bitmap, representing embedded anti-aliased bitmaps in
28    /// font files according to the OpenType specification. We haven't found a
29    /// single font using this format, however.
30    Gray4,
31
32    /// An 8-bit bitmap, representing RGB or BGR decimated glyph images used
33    /// for display on LCD displays; the bitmap is three times wider than the
34    /// original glyph image. See also FT_RENDER_MODE_LCD.
35    Lcd,
36
37    /// An 8-bit bitmap, representing RGB or BGR decimated glyph images used for
38    /// display on rotated LCD displays; the bitmap is three times taller than
39    /// the original glyph image. See also FT_RENDER_MODE_LCD_V.
40    LcdV,
41
42    /// An image with four 8-bit channels per pixel, representing a color image
43    /// (such as emoticons) with alpha channel. For each pixel, the format is
44    /// BGRA, which means, the blue channel comes first in memory. The color
45    /// channels are pre-multiplied and in the sRGB colorspace. For example,
46    /// full red at half-translucent opacity will be represented as
47    /// `00,00,80,80`, not `00,00,FF,80`. See also FT_LOAD_COLOR.
48    Bgra,
49}
50
51#[allow(missing_copy_implementations)]
52pub struct Bitmap {
53    raw: *const ffi::FT_Bitmap,
54}
55
56impl Bitmap {
57    pub unsafe fn from_raw(raw: *const ffi::FT_Bitmap) -> Self {
58        Bitmap { raw }
59    }
60
61    /// A typeless pointer to the bitmap buffer. This value should be aligned
62    /// on 32-bit boundaries in most cases.
63    pub fn buffer(&self) -> &[u8] {
64        unsafe {
65            slice::from_raw_parts(
66                (*self.raw).buffer,
67                (self.pitch().abs() * self.rows()) as usize
68            )
69        }
70    }
71
72    /// The number of pixels in bitmap row.
73    pub fn width(&self) -> i32 {
74        unsafe { (*self.raw).width }
75    }
76
77    /// The number of bitmap rows.
78    pub fn rows(&self) -> i32 {
79        unsafe { (*self.raw).rows }
80    }
81
82    pub fn raw(&self) -> &ffi::FT_Bitmap {
83        unsafe { &*self.raw }
84    }
85
86    /// The pixel mode, i.e., how pixel bits are stored. See `PixelMode` for
87    /// possible values.
88    pub fn pixel_mode(&self) -> FtResult<PixelMode> {
89        let pixel_mode = unsafe { (*self.raw).pixel_mode } as u32;
90
91        Ok(match pixel_mode {
92            ffi::FT_PIXEL_MODE_NONE  => PixelMode::None,
93            ffi::FT_PIXEL_MODE_MONO  => PixelMode::Mono,
94            ffi::FT_PIXEL_MODE_GRAY  => PixelMode::Gray,
95            ffi::FT_PIXEL_MODE_GRAY2 => PixelMode::Gray2,
96            ffi::FT_PIXEL_MODE_GRAY4 => PixelMode::Gray4,
97            ffi::FT_PIXEL_MODE_LCD   => PixelMode::Lcd,
98            ffi::FT_PIXEL_MODE_LCD_V => PixelMode::LcdV,
99            ffi::FT_PIXEL_MODE_BGRA  => PixelMode::Bgra,
100            _ => return Err(Error::UnexpectedPixelMode)
101        })
102    }
103
104    /// The pitch's absolute value is the number of bytes taken by one bitmap row, including
105    /// padding. However, the pitch is positive when the bitmap has a ‘down’ flow, and negative
106    /// when it has an ‘up’ flow. In all cases, the pitch is an offset to add to a bitmap pointer
107    /// in order to go down one row.
108    ///
109    /// Note that ‘padding’ means the alignment of a bitmap to a byte border, and FreeType
110    /// functions normally align to the smallest possible integer value.
111    /// For the B/W rasterizer, ‘pitch’ is always an even number.
112    ///
113    /// To change the pitch of a bitmap (say, to make it a multiple of 4), use FT_Bitmap_Convert.
114    /// Alternatively, you might use callback functions to directly render to the application's
115    /// surface; see the file ‘example2.cpp’ in the tutorial for a demonstration.
116    pub fn pitch(&self) -> i32 {
117        unsafe { (*self.raw).pitch }
118    }
119}