use crate::blend::BlendMode;
use crate::brush::{Brush, Image, Sampling};
use crate::geometry::Affine;
use crate::mesh::Mesh;
use crate::path::{FillRule, Path};
use crate::pick::PickId;
pub mod recording;
pub trait SceneBuilder {
fn clear(&mut self);
fn fill(
&mut self,
rule: FillRule,
transform: Affine,
brush: &Brush,
brush_transform: Option<Affine>,
path: &Path,
pick_id: PickId,
);
fn stroke(
&mut self,
stroke: &crate::stroke::Stroke,
transform: Affine,
brush: &Brush,
brush_transform: Option<Affine>,
path: &Path,
pick_id: PickId,
);
fn draw_image(
&mut self,
image: &Image,
transform: Affine,
sampling: Sampling,
alpha: f32,
pick_id: PickId,
);
fn draw_glyphs(&mut self, run: &GlyphRun<'_>, pick_id: PickId);
fn draw_mesh(&mut self, mesh: &Mesh, transform: Affine, pick_id: PickId);
fn push_layer(&mut self, blend: BlendMode, alpha: f32, transform: Affine, clip: &Path);
fn pop_layer(&mut self);
}
#[derive(Debug, Clone)]
pub struct Font(peniko::FontData);
impl PartialEq for Font {
fn eq(&self, other: &Self) -> bool {
if self.0.index != other.0.index {
return false;
}
self.0.data.id() == other.0.data.id() || self.0.data.as_ref() == other.0.data.as_ref()
}
}
impl Font {
pub(crate) fn from_data(data: peniko::FontData) -> Self {
Self(data)
}
#[cfg_attr(not(feature = "vello"), allow(dead_code))]
pub(crate) fn data(&self) -> &peniko::FontData {
&self.0
}
pub fn new(data: peniko::Blob<u8>, index: u32) -> Self {
Self(peniko::FontData::new(data, index))
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Glyph {
pub id: u32,
pub x: f32,
pub y: f32,
}
#[derive(Debug, Clone, Copy)]
pub struct GlyphRun<'a> {
pub font: &'a Font,
pub font_size: f32,
pub transform: Affine,
pub glyph_transform: Option<Affine>,
pub brush: &'a Brush,
pub brush_alpha: f32,
pub hint: bool,
pub glyphs: &'a [Glyph],
pub style: Option<&'a crate::stroke::Stroke>,
}
#[cfg(test)]
mod tests {
use super::*;
use peniko::Blob;
#[test]
fn fonts_naming_the_same_face_are_equal_across_separate_blobs() {
let bytes = vec![7u8, 8, 9, 10];
let a = Font::new(Blob::from(bytes.clone()), 0);
let b = Font::new(Blob::from(bytes), 0);
assert_ne!(
a.data().data.id(),
b.data().data.id(),
"the two blobs should have distinct ids, or this proves nothing"
);
assert_eq!(a, b);
}
#[test]
fn a_font_equals_a_clone_of_itself() {
let font = Font::new(Blob::from(vec![1u8, 2, 3]), 2);
assert_eq!(font.clone(), font);
}
#[test]
fn fonts_differing_in_face_index_are_not_equal() {
let bytes = vec![1u8, 2, 3];
let a = Font::new(Blob::from(bytes.clone()), 0);
let b = Font::new(Blob::from(bytes), 1);
assert_ne!(a, b);
}
#[test]
fn fonts_with_different_bytes_are_not_equal() {
let a = Font::new(Blob::from(vec![1u8, 2, 3]), 0);
let b = Font::new(Blob::from(vec![1u8, 2, 4]), 0);
assert_ne!(a, b);
}
}