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

@ -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);
}
}