| // Copyright 2026 the Vello Authors | |
| // SPDX-License-Identifier: Apache-2.0 OR MIT | |
| //! Traits for rendering glyphs and replaying them. | |
| use crate::atlas::AtlasSlot; | |
| use crate::kurbo::{Affine, BezPath, Rect, Shape, Stroke}; | |
| use crate::peniko::BlendMode; | |
| use vello_common::paint::{ImageSource, Paint}; | |
| // TODO: This trait is only temporary and will hopefully be replaced once we have a better | |
| // unifying imaging API. | |
| /// A sink for low-level glyph drawing commands. | |
| pub trait DrawSink { | |
| /// Fill a path. | |
| fn fill_path(&mut self, path: &BezPath, paint: &Paint, transform: Affine); | |
| /// Fill a rectangle. | |
| fn fill_rect(&mut self, rect: &Rect, paint: &Paint, transform: Affine); | |
| // TODO: `push/pop_clip_layer` are only needed temporarily for Vello renderers so the impact of | |
| // destructive blend modes can be limited to a smaller area. Once the limitation is lifted, | |
| // we can remove this method and just use `push_clip_path` and `push_blend_layer`. | |
| /// Push a clip layer defined by a path. | |
| fn push_clip_layer(&mut self, clip: &BezPath, transform: Affine); | |
| /// Push a clip path. | |
| /// | |
| /// This method _can_ be implemented in terms of `push_clip_layer` without losing correctness. | |
| /// However, clients are allowed to choose a more efficient clipping implementation that | |
| /// doesn't require pushing an isolated layer. | |
| /// | |
| /// When providing a custom implementation of this method, the `pop_clip_path` method also | |
| /// needs to be overridden correspondingly. | |
| fn push_clip_path(&mut self, clip: &BezPath, transform: Affine) { | |
| self.push_clip_layer(clip, transform); | |
| } | |
| /// Push a rectangular clip path. | |
| fn push_clip_rect(&mut self, clip: &Rect, transform: Affine) { | |
| self.push_clip_path(&clip.to_path(0.1), transform); | |
| } | |
| /// Push a blend/compositing layer. | |
| fn push_blend_layer(&mut self, blend_mode: BlendMode); | |
| /// Pop the most recent clip or blend layer. | |
| fn pop_layer(&mut self); | |
| /// Pop the most recent clip path. | |
| /// | |
| /// See the documentation for [`DrawSink::push_clip_path`]. | |
| fn pop_clip_path(&mut self) { | |
| self.pop_layer(); | |
| } | |
| /// Width of the surface. | |
| fn width(&self) -> u16; | |
| /// Height of the surface. | |
| fn height(&self) -> u16; | |
| } | |
| /// A renderer that can draw sequences of cached and uncached glyphs. | |
| pub trait GlyphRenderer: DrawSink { | |
| /// Stroke a path. | |
| fn stroke_path(&mut self, path: &BezPath, stroke: &Stroke, paint: &Paint, transform: Affine); | |
| // Hopefully we can get rid of those below in the future. | |
| /// Construct the [`ImageSource`] for sampling a cached glyph from the atlas. | |
| fn atlas_image_source(&self, atlas_slot: &AtlasSlot) -> ImageSource; | |
| /// Compute the paint transform for sampling a cached glyph from the atlas. | |
| fn atlas_paint_transform(&self, atlas_slot: &AtlasSlot) -> Affine; | |
| } |