use std::os::raw::c_int;
use image::{ImageBuffer, Luma, Rgb32FImage, Rgba32FImage};
use msdf_sys::*;
#[cfg(test)]
mod test_helpers;
#[cfg(test)]
pub mod tests;
mod bitmap;
mod config;
mod loader;
pub use bitmap::*;
pub use config::*;
pub use loader::*;
#[derive(Debug)]
pub enum MsdfError {
FreetypeInitializationFailure,
FontLoadingFailure,
GlyphLoadingFailure,
}
pub struct Shape {
shape: msdfgen_Shape,
}
impl Shape {
pub fn color_edges_simple(mut self, angle: f64) -> ColoredShape {
unsafe {
msdfgen_edgeColoringSimple(&mut self.shape, angle, 0);
}
ColoredShape(self)
}
pub fn color_edges_ink_trap(mut self, angle: f64) -> ColoredShape {
unsafe {
msdfgen_edgeColoringInkTrap(&mut self.shape, angle, 0);
}
ColoredShape(self)
}
pub fn color_edges_by_distance(mut self, angle: f64) -> ColoredShape {
unsafe {
msdfgen_edgeColoringByDistance(&mut self.shape, angle, 0);
}
ColoredShape(self)
}
pub fn generate_sdf(
&self,
width: u32,
height: u32,
range: f64,
projection: &Projection,
config: &SDFConfig,
) -> SDF {
let image = ImageBuffer::<Luma<f32>, Vec<f32>>::new(width, height);
let msdf = msdfgen_Bitmap {
pixels: image.as_flat_samples().samples.as_ptr() as *mut f32,
w: width as c_int,
h: height as c_int,
_phantom_0: Default::default(),
};
let projection = projection.as_msdfgen_projection();
let config = msdfgen_GeneratorConfig {
overlapSupport: config.overlap_support,
};
unsafe {
msdfgen_generateSDF(
&msdf as *const msdfgen_Bitmap<_> as *const _,
&self.shape,
&projection,
range,
&config,
);
}
SDF::from_image(image, range, 0.5)
}
pub fn generate_psuedo_sdf(
&self,
width: u32,
height: u32,
range: f64,
projection: &Projection,
config: &SDFConfig,
) -> SDF {
let image = ImageBuffer::<Luma<f32>, Vec<f32>>::new(width, height);
let msdf = msdfgen_Bitmap {
pixels: image.as_flat_samples().samples.as_ptr() as *mut f32,
w: width as c_int,
h: height as c_int,
_phantom_0: Default::default(),
};
let projection = projection.as_msdfgen_projection();
let config = msdfgen_GeneratorConfig {
overlapSupport: config.overlap_support,
};
unsafe {
msdfgen_generatePseudoSDF(
&msdf as *const msdfgen_Bitmap<_> as *const _,
&self.shape,
&projection,
range,
&config,
);
}
SDF::from_image(image, range, 0.5)
}
}
pub struct ColoredShape(Shape);
impl ColoredShape {
pub fn generate_sdf(
&self,
width: u32,
height: u32,
range: f64,
projection: &Projection,
config: &SDFConfig,
) -> SDF {
self.0
.generate_sdf(width, height, range, projection, config)
}
pub fn generate_psuedo_sdf(
&self,
width: u32,
height: u32,
range: f64,
projection: &Projection,
config: &SDFConfig,
) -> SDF {
self.0
.generate_psuedo_sdf(width, height, range, projection, config)
}
pub fn generate_msdf(
&self,
width: u32,
height: u32,
range: f64,
projection: &Projection,
config: &MSDFConfig,
) -> MSDF {
let image = Rgb32FImage::new(width, height);
let msdf = msdfgen_Bitmap {
pixels: image.as_flat_samples().samples.as_ptr() as *mut f32,
w: width as c_int,
h: height as c_int,
_phantom_0: Default::default(),
};
let projection = projection.as_msdfgen_projection();
let config = config.as_msdfgen_config();
unsafe {
msdfgen_generateMSDF(
&msdf as *const msdfgen_Bitmap<_> as *const _,
&self.0.shape,
&projection,
range,
&config,
);
}
MSDF::from_image(image, range, 0.5)
}
pub fn generate_mtsdf(
&self,
width: u32,
height: u32,
range: f64,
projection: &Projection,
config: &MSDFConfig,
) -> MTSDF {
let image = Rgba32FImage::new(width, height);
let msdf = msdfgen_Bitmap {
pixels: image.as_flat_samples().samples.as_ptr() as *mut f32,
w: width as c_int,
h: height as c_int,
_phantom_0: Default::default(),
};
let projection = projection.as_msdfgen_projection();
let config = config.as_msdfgen_config();
unsafe {
msdfgen_generateMTSDF(
&msdf as *const msdfgen_Bitmap<_> as *const _,
&self.0.shape,
&projection,
range,
&config,
);
}
MTSDF::from_image(image, range, 0.5)
}
}