Introduce Svg struct in core::svg

This commit is contained in:
Héctor Ramón Jiménez 2024-08-04 04:52:55 +02:00
parent 8708101c89
commit d4b08462e5
No known key found for this signature in database
GPG key ID: 7CC46565708259A7
18 changed files with 146 additions and 257 deletions

View file

@ -16,7 +16,7 @@ pub struct Image<H = Handle> {
/// The filter method of the image.
pub filter_method: FilterMethod,
/// The rotation to be applied to the image, from its center.
/// The rotation to be applied to the image; on its center.
pub rotation: Radians,
/// The opacity of the image.
@ -26,7 +26,7 @@ pub struct Image<H = Handle> {
/// If set to `true`, the image will be snapped to the pixel grid.
///
/// This can avoid graphical glitches, specially when using a
/// This can avoid graphical glitches, specially when using
/// [`FilterMethod::Nearest`].
pub snap: bool,
}

View file

@ -70,6 +70,7 @@ pub use rotation::Rotation;
pub use shadow::Shadow;
pub use shell::Shell;
pub use size::Size;
pub use svg::Svg;
pub use text::Text;
pub use theme::Theme;
pub use transformation::Transformation;

View file

@ -4,8 +4,7 @@ use crate::renderer::{self, Renderer};
use crate::svg;
use crate::text::{self, Text};
use crate::{
Background, Color, Font, Pixels, Point, Radians, Rectangle, Size,
Transformation,
Background, Color, Font, Pixels, Point, Rectangle, Size, Transformation,
};
impl Renderer for () {
@ -192,13 +191,5 @@ impl svg::Renderer for () {
Size::default()
}
fn draw_svg(
&mut self,
_handle: svg::Handle,
_color: Option<Color>,
_bounds: Rectangle,
_rotation: Radians,
_opacity: f32,
) {
}
fn draw_svg(&mut self, _svg: svg::Svg, _bounds: Rectangle) {}
}

View file

@ -7,6 +7,66 @@ use std::hash::{Hash, Hasher as _};
use std::path::PathBuf;
use std::sync::Arc;
/// A raster image that can be drawn.
#[derive(Debug, Clone, PartialEq)]
pub struct Svg<H = Handle> {
/// The handle of the [`Svg`].
pub handle: H,
/// The [`Color`] filter to be applied to the [`Svg`].
///
/// If some [`Color`] is set, the whole [`Svg`] will be
/// painted with it—ignoring any intrinsic colors.
///
/// This can be useful for coloring icons programmatically
/// (e.g. with a theme).
pub color: Option<Color>,
/// The rotation to be applied to the image; on its center.
pub rotation: Radians,
/// The opacity of the [`Svg`].
///
/// 0 means transparent. 1 means opaque.
pub opacity: f32,
}
impl Svg<Handle> {
/// Creates a new [`Svg`] with the given handle.
pub fn new(handle: impl Into<Handle>) -> Self {
Self {
handle: handle.into(),
color: None,
rotation: Radians(0.0),
opacity: 1.0,
}
}
/// Sets the [`Color`] filter of the [`Svg`].
pub fn color(mut self, color: impl Into<Color>) -> Self {
self.color = Some(color.into());
self
}
/// Sets the rotation of the [`Svg`].
pub fn rotation(mut self, rotation: impl Into<Radians>) -> Self {
self.rotation = rotation.into();
self
}
/// Sets the opacity of the [`Svg`].
pub fn opacity(mut self, opacity: impl Into<f32>) -> Self {
self.opacity = opacity.into();
self
}
}
impl From<&Handle> for Svg {
fn from(handle: &Handle) -> Self {
Svg::new(handle.clone())
}
}
/// A handle of Svg data.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Handle {
@ -95,12 +155,5 @@ pub trait Renderer: crate::Renderer {
fn measure_svg(&self, handle: &Handle) -> Size<u32>;
/// Draws an SVG with the given [`Handle`], an optional [`Color`] filter, and inside the provided `bounds`.
fn draw_svg(
&mut self,
handle: Handle,
color: Option<Color>,
bounds: Rectangle,
rotation: Radians,
opacity: f32,
);
fn draw_svg(&mut self, svg: Svg, bounds: Rectangle);
}

View file

@ -16,7 +16,7 @@ pub use stroke::{LineCap, LineDash, LineJoin, Stroke};
pub use style::Style;
pub use text::Text;
pub use crate::core::Image;
pub use crate::core::{Image, Svg};
pub use crate::gradient::{self, Gradient};
use crate::cache::Cached;

View file

@ -1,7 +1,6 @@
//! Draw and generate geometry.
use crate::core::svg;
use crate::core::{Color, Point, Radians, Rectangle, Size, Vector};
use crate::geometry::{self, Fill, Image, Path, Stroke, Text};
use crate::core::{Point, Radians, Rectangle, Size, Vector};
use crate::geometry::{self, Fill, Image, Path, Stroke, Svg, Text};
/// The region of a surface that can be used to draw geometry.
#[allow(missing_debug_implementations)]
@ -206,15 +205,7 @@ pub trait Backend: Sized {
);
fn draw_image(&mut self, bounds: Rectangle, image: impl Into<Image>);
fn draw_svg(
&mut self,
handle: &svg::Handle,
bounds: Rectangle,
color: Option<Color>,
rotation: Radians,
opacity: f32,
);
fn draw_svg(&mut self, bounds: Rectangle, svg: impl Into<Svg>);
fn into_geometry(self) -> Self::Geometry;
}
@ -262,17 +253,8 @@ impl Backend for () {
) {
}
fn into_geometry(self) -> Self::Geometry {}
fn draw_image(&mut self, _bounds: Rectangle, _image: impl Into<Image>) {}
fn draw_svg(&mut self, _bounds: Rectangle, _svg: impl Into<Svg>) {}
fn draw_svg(
&mut self,
_handle: &svg::Handle,
_bounds: Rectangle,
_color: Option<Color>,
_rotation: Radians,
_opacity: f32,
) {
}
fn into_geometry(self) -> Self::Geometry {}
}

View file

@ -2,7 +2,9 @@
#[cfg(feature = "image")]
pub use ::image as image_rs;
use crate::core::{image, svg, Color, Radians, Rectangle};
use crate::core::image;
use crate::core::svg;
use crate::core::Rectangle;
/// A raster or vector image.
#[derive(Debug, Clone, PartialEq)]
@ -11,22 +13,7 @@ pub enum Image {
Raster(image::Image, Rectangle),
/// A vector image.
Vector {
/// The handle of a vector image.
handle: svg::Handle,
/// The [`Color`] filter
color: Option<Color>,
/// The bounds of the image.
bounds: Rectangle,
/// The rotation of the image.
rotation: Radians,
/// The opacity of the image.
opacity: f32,
},
Vector(svg::Svg, Rectangle),
}
impl Image {
@ -34,9 +21,7 @@ impl Image {
pub fn bounds(&self) -> Rectangle {
match self {
Image::Raster(image, bounds) => bounds.rotate(image.rotation),
Image::Vector {
bounds, rotation, ..
} => bounds.rotate(*rotation),
Image::Vector(svg, bounds) => bounds.rotate(svg.rotation),
}
}
}

View file

@ -3,8 +3,7 @@ use crate::core::image;
use crate::core::renderer;
use crate::core::svg;
use crate::core::{
self, Background, Color, Image, Point, Radians, Rectangle, Size,
Transformation,
self, Background, Color, Image, Point, Rectangle, Size, Svg, Transformation,
};
use crate::graphics;
use crate::graphics::compositor;
@ -164,19 +163,8 @@ where
delegate!(self, renderer, renderer.measure_svg(handle))
}
fn draw_svg(
&mut self,
handle: svg::Handle,
color: Option<Color>,
bounds: Rectangle,
rotation: Radians,
opacity: f32,
) {
delegate!(
self,
renderer,
renderer.draw_svg(handle, color, bounds, rotation, opacity)
);
fn draw_svg(&mut self, svg: Svg, bounds: Rectangle) {
delegate!(self, renderer, renderer.draw_svg(svg, bounds));
}
}
@ -425,8 +413,7 @@ where
#[cfg(feature = "geometry")]
mod geometry {
use super::Renderer;
use crate::core::svg;
use crate::core::{Color, Point, Radians, Rectangle, Size, Vector};
use crate::core::{Point, Radians, Rectangle, Size, Svg, Vector};
use crate::graphics::cache::{self, Cached};
use crate::graphics::geometry::{self, Fill, Image, Path, Stroke, Text};
@ -561,19 +548,8 @@ mod geometry {
delegate!(self, frame, frame.draw_image(bounds, image));
}
fn draw_svg(
&mut self,
handle: &svg::Handle,
bounds: Rectangle,
color: Option<Color>,
rotation: Radians,
opacity: f32,
) {
delegate!(
self,
frame,
frame.draw_svg(handle, bounds, color, rotation, opacity)
);
fn draw_svg(&mut self, bounds: Rectangle, svg: impl Into<Svg>) {
delegate!(self, frame, frame.draw_svg(bounds, svg));
}
fn push_transform(&mut self) {

View file

@ -580,13 +580,7 @@ impl Engine {
);
}
#[cfg(feature = "svg")]
Image::Vector {
handle,
color,
bounds,
rotation,
opacity,
} => {
Image::Vector(svg, bounds) => {
let physical_bounds = *bounds * _transformation;
if !_clip_bounds.intersects(&physical_bounds) {
@ -597,7 +591,7 @@ impl Engine {
.then_some(_clip_mask as &_);
let center = physical_bounds.center();
let radians = f32::from(*rotation);
let radians = f32::from(svg.rotation);
let transform = into_transform(_transformation).post_rotate_at(
radians.to_degrees(),
@ -606,10 +600,10 @@ impl Engine {
);
self.vector_pipeline.draw(
handle,
*color,
&svg.handle,
svg.color,
physical_bounds,
*opacity,
svg.opacity,
_pixels,
transform,
clip_mask,

View file

@ -1,11 +1,10 @@
use crate::core::svg;
use crate::core::text::LineHeight;
use crate::core::{Color, Pixels, Point, Radians, Rectangle, Size, Vector};
use crate::core::{self, Pixels, Point, Radians, Rectangle, Size, Svg, Vector};
use crate::graphics::cache::{self, Cached};
use crate::graphics::geometry::fill::{self, Fill};
use crate::graphics::geometry::stroke::{self, Stroke};
use crate::graphics::geometry::{self, Image, Path, Style};
use crate::graphics::{self, Gradient, Text};
use crate::graphics::geometry::{self, Path, Style};
use crate::graphics::{self, Gradient, Image, Text};
use crate::Primitive;
use std::rc::Rc;
@ -282,7 +281,7 @@ impl geometry::frame::Backend for Frame {
}
}
fn draw_image(&mut self, bounds: Rectangle, image: impl Into<Image>) {
fn draw_image(&mut self, bounds: Rectangle, image: impl Into<core::Image>) {
let mut image = image.into();
let (bounds, external_rotation) =
@ -293,24 +292,15 @@ impl geometry::frame::Backend for Frame {
self.images.push(graphics::Image::Raster(image, bounds));
}
fn draw_svg(
&mut self,
handle: &svg::Handle,
bounds: Rectangle,
color: Option<Color>,
rotation: Radians,
opacity: f32,
) {
fn draw_svg(&mut self, bounds: Rectangle, svg: impl Into<Svg>) {
let mut svg = svg.into();
let (bounds, external_rotation) =
transform_rectangle(bounds, self.transform);
self.images.push(graphics::Image::Vector {
handle: handle.clone(),
bounds,
color,
rotation: rotation + external_rotation,
opacity,
});
svg.rotation += external_rotation;
self.images.push(Image::Vector(svg, bounds));
}
}

View file

@ -1,7 +1,6 @@
use crate::core::renderer::Quad;
use crate::core::svg;
use crate::core::{
self, Background, Color, Point, Radians, Rectangle, Transformation,
self, Background, Color, Point, Rectangle, Svg, Transformation,
};
use crate::graphics::damage;
use crate::graphics::layer;
@ -119,23 +118,10 @@ impl Layer {
pub fn draw_image(&mut self, image: Image, transformation: Transformation) {
match image {
Image::Raster(raster, bounds) => {
self.draw_raster(raster.clone(), bounds, transformation);
self.draw_raster(raster, bounds, transformation);
}
Image::Vector {
handle,
color,
bounds,
rotation,
opacity,
} => {
self.draw_svg(
handle.clone(),
color,
bounds,
transformation,
rotation,
opacity,
);
Image::Vector(svg, bounds) => {
self.draw_svg(svg, bounds, transformation);
}
}
}
@ -153,20 +139,11 @@ impl Layer {
pub fn draw_svg(
&mut self,
handle: svg::Handle,
color: Option<Color>,
svg: Svg,
bounds: Rectangle,
transformation: Transformation,
rotation: Radians,
opacity: f32,
) {
let svg = Image::Vector {
handle,
color,
bounds: bounds * transformation,
rotation,
opacity,
};
let svg = Image::Vector(svg, bounds * transformation);
self.images.push(svg);
}

View file

@ -396,23 +396,9 @@ impl core::svg::Renderer for Renderer {
self.engine.vector_pipeline.viewport_dimensions(handle)
}
fn draw_svg(
&mut self,
handle: core::svg::Handle,
color: Option<Color>,
bounds: Rectangle,
rotation: core::Radians,
opacity: f32,
) {
fn draw_svg(&mut self, svg: core::Svg, bounds: Rectangle) {
let (layer, transformation) = self.layers.current_mut();
layer.draw_svg(
handle,
color,
bounds,
transformation,
rotation,
opacity,
);
layer.draw_svg(svg, bounds, transformation);
}
}

View file

@ -1,18 +1,17 @@
//! Build and draw geometry.
use crate::core::svg;
use crate::core::text::LineHeight;
use crate::core::{
Color, Pixels, Point, Radians, Rectangle, Size, Transformation, Vector,
self, Pixels, Point, Radians, Rectangle, Size, Svg, Transformation, Vector,
};
use crate::graphics::cache::{self, Cached};
use crate::graphics::color;
use crate::graphics::geometry::fill::{self, Fill};
use crate::graphics::geometry::{
self, Image, LineCap, LineDash, LineJoin, Path, Stroke, Style,
self, LineCap, LineDash, LineJoin, Path, Stroke, Style,
};
use crate::graphics::gradient::{self, Gradient};
use crate::graphics::mesh::{self, Mesh};
use crate::graphics::{self, Text};
use crate::graphics::{Image, Text};
use crate::text;
use crate::triangle;
@ -26,7 +25,7 @@ use std::sync::Arc;
pub enum Geometry {
Live {
meshes: Vec<Mesh>,
images: Vec<graphics::Image>,
images: Vec<Image>,
text: Vec<Text>,
},
Cached(Cache),
@ -35,7 +34,7 @@ pub enum Geometry {
#[derive(Debug, Clone)]
pub struct Cache {
pub meshes: Option<triangle::Cache>,
pub images: Option<Arc<[graphics::Image]>>,
pub images: Option<Arc<[Image]>>,
pub text: Option<text::Cache>,
}
@ -98,7 +97,7 @@ pub struct Frame {
clip_bounds: Rectangle,
buffers: BufferStack,
meshes: Vec<Mesh>,
images: Vec<graphics::Image>,
images: Vec<Image>,
text: Vec<Text>,
transforms: Transforms,
fill_tessellator: tessellation::FillTessellator,
@ -292,7 +291,7 @@ impl geometry::frame::Backend for Frame {
height: f32::INFINITY,
};
self.text.push(graphics::Text::Cached {
self.text.push(Text::Cached {
content: text.content,
bounds,
color: text.color,
@ -376,7 +375,7 @@ impl geometry::frame::Backend for Frame {
}
}
fn draw_image(&mut self, bounds: Rectangle, image: impl Into<Image>) {
fn draw_image(&mut self, bounds: Rectangle, image: impl Into<core::Image>) {
let mut image = image.into();
let (bounds, external_rotation) =
@ -384,27 +383,18 @@ impl geometry::frame::Backend for Frame {
image.rotation += external_rotation;
self.images.push(graphics::Image::Raster(image, bounds));
self.images.push(Image::Raster(image, bounds));
}
fn draw_svg(
&mut self,
handle: &svg::Handle,
bounds: Rectangle,
color: Option<Color>,
rotation: Radians,
opacity: f32,
) {
fn draw_svg(&mut self, bounds: Rectangle, svg: impl Into<Svg>) {
let mut svg = svg.into();
let (bounds, external_rotation) =
self.transforms.current.transform_rectangle(bounds);
self.images.push(graphics::Image::Vector {
handle: handle.clone(),
color,
bounds,
rotation: rotation + external_rotation,
opacity,
});
svg.rotation += external_rotation;
self.images.push(Image::Vector(svg, bounds));
}
}

View file

@ -246,23 +246,22 @@ impl Pipeline {
Image::Raster { .. } => {}
#[cfg(feature = "svg")]
Image::Vector {
handle,
color,
bounds,
rotation,
opacity,
} => {
Image::Vector(svg, bounds) => {
let size = [bounds.width, bounds.height];
if let Some(atlas_entry) = cache.upload_vector(
device, encoder, handle, *color, size, scale,
device,
encoder,
&svg.handle,
svg.color,
size,
scale,
) {
add_instances(
[bounds.x, bounds.y],
size,
f32::from(*rotation),
*opacity,
f32::from(svg.rotation),
svg.opacity,
true,
atlas_entry,
nearest_instances,

View file

@ -1,6 +1,5 @@
use crate::core::{
self, renderer, Background, Color, Point, Radians, Rectangle,
Transformation,
self, renderer, Background, Color, Point, Rectangle, Svg, Transformation,
};
use crate::graphics;
use crate::graphics::color;
@ -118,21 +117,8 @@ impl Layer {
Image::Raster(image, bounds) => {
self.draw_raster(image, bounds, transformation);
}
Image::Vector {
handle,
color,
bounds,
rotation,
opacity,
} => {
self.draw_svg(
handle.clone(),
color,
bounds,
transformation,
rotation,
opacity,
);
Image::Vector(svg, bounds) => {
self.draw_svg(svg, bounds, transformation);
}
}
}
@ -150,20 +136,11 @@ impl Layer {
pub fn draw_svg(
&mut self,
handle: crate::core::svg::Handle,
color: Option<Color>,
svg: Svg,
bounds: Rectangle,
transformation: Transformation,
rotation: Radians,
opacity: f32,
) {
let svg = Image::Vector {
handle,
color,
bounds: bounds * transformation,
rotation,
opacity,
};
let svg = Image::Vector(svg, bounds * transformation);
self.images.push(svg);
}

View file

@ -539,23 +539,9 @@ impl core::svg::Renderer for Renderer {
self.image_cache.borrow_mut().measure_svg(handle)
}
fn draw_svg(
&mut self,
handle: core::svg::Handle,
color_filter: Option<Color>,
bounds: Rectangle,
rotation: core::Radians,
opacity: f32,
) {
fn draw_svg(&mut self, svg: core::Svg, bounds: Rectangle) {
let (layer, transformation) = self.layers.current_mut();
layer.draw_svg(
handle,
color_filter,
bounds,
transformation,
rotation,
opacity,
);
layer.draw_svg(svg, bounds, transformation);
}
}

View file

@ -8,8 +8,8 @@ use crate::core::mouse;
use crate::core::renderer;
use crate::core::widget::Tree;
use crate::core::{
self, ContentFit, Element, Layout, Length, Point, Rectangle, Rotation,
Size, Vector, Widget,
ContentFit, Element, Layout, Length, Point, Rectangle, Rotation, Size,
Vector, Widget,
};
pub use image::{FilterMethod, Handle};
@ -181,7 +181,7 @@ pub fn draw<Renderer, Handle>(
let render = |renderer: &mut Renderer| {
renderer.draw_image(
core::Image {
image::Image {
handle: handle.clone(),
filter_method,
rotation: rotation.radians(),

View file

@ -211,11 +211,13 @@ where
let render = |renderer: &mut Renderer| {
renderer.draw_svg(
self.handle.clone(),
style.color,
svg::Svg {
handle: self.handle.clone(),
color: style.color,
rotation: self.rotation.radians(),
opacity: self.opacity,
},
drawing_bounds,
self.rotation.radians(),
self.opacity,
);
};