use crate::Color;
use ramp_text::SubpixelBin;
pub type CustomGlyphId = u16;
#[derive(Default, Debug, Clone, Copy, PartialEq)]
pub struct CustomGlyph {
pub id: CustomGlyphId,
pub left: f32,
pub top: f32,
pub width: f32,
pub height: f32,
pub color: Option<Color>,
pub snap_to_physical_pixel: bool,
pub metadata: usize,
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct RasterizeCustomGlyphRequest {
pub id: CustomGlyphId,
pub width: u16,
pub height: u16,
pub x_bin: SubpixelBin,
pub y_bin: SubpixelBin,
pub scale: f32,
}
#[derive(Debug, Clone)]
pub struct RasterizedCustomGlyph {
pub data: Vec<u8>,
pub content_type: ContentType,
}
impl RasterizedCustomGlyph {
pub(crate) fn validate(
&self,
input: &RasterizeCustomGlyphRequest,
expected_type: Option<ContentType>,
) {
if let Some(expected_type) = expected_type {
assert_eq!(self.content_type, expected_type, "Custom glyph rasterizer must always produce the same content type for a given input. Expected {:?}, got {:?}. Input: {:?}", expected_type, self.content_type, input);
}
assert_eq!(
self.data.len(),
input.width as usize * input.height as usize * self.content_type.bytes_per_pixel(),
"Invalid custom glyph rasterizer output. Expected data of length {}, got length {}. Input: {:?}",
input.width as usize * input.height as usize * self.content_type.bytes_per_pixel(),
self.data.len(),
input,
);
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct CustomGlyphCacheKey {
pub glyph_id: CustomGlyphId,
pub width: u16,
pub height: u16,
pub x_bin: SubpixelBin,
pub y_bin: SubpixelBin,
}
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub enum ContentType {
Color,
Mask,
}
impl ContentType {
pub fn bytes_per_pixel(&self) -> usize {
match self {
Self::Color => 4,
Self::Mask => 1,
}
}
}