Put svg rendering behind a feature gate

This reduces binary size when SVG supoprt is not needed.
This commit is contained in:
Héctor Ramón Jiménez 2019-12-15 06:45:20 +01:00
parent aa29849976
commit 232d4873ba
5 changed files with 49 additions and 16 deletions

View file

@ -14,6 +14,8 @@ categories = ["gui"]
[features] [features]
# Enables a debug view in native platforms (press F12) # Enables a debug view in native platforms (press F12)
debug = ["iced_winit/debug"] debug = ["iced_winit/debug"]
# Enables support for SVG rendering
svg = ["iced_wgpu/svg"]
[badges] [badges]
maintenance = { status = "actively-developed" } maintenance = { status = "actively-developed" }

View file

@ -1,4 +1,4 @@
use iced::{Column, Container, Element, Length, Sandbox, Settings, Svg}; use iced::{Container, Element, Length, Sandbox, Settings};
pub fn main() { pub fn main() {
Tiger::run(Settings::default()) Tiger::run(Settings::default())
@ -21,14 +21,28 @@ impl Sandbox for Tiger {
fn update(&mut self, _message: ()) {} fn update(&mut self, _message: ()) {}
fn view(&mut self) -> Element<()> { fn view(&mut self) -> Element<()> {
let content = #[cfg(feature = "svg")]
let content = {
use iced::{Column, Svg};
Column::new() Column::new()
.width(Length::Shrink) .width(Length::Shrink)
.padding(20) .padding(20)
.push(Svg::new(format!( .push(Svg::new(format!(
"{}/examples/resources/tiger.svg", "{}/examples/resources/tiger.svg",
env!("CARGO_MANIFEST_DIR") env!("CARGO_MANIFEST_DIR")
))); )))
};
#[cfg(not(feature = "svg"))]
let content = {
use iced::{HorizontalAlignment, Text};
Text::new("You need to enable the `svg` feature!")
.width(Length::Shrink)
.horizontal_alignment(HorizontalAlignment::Center)
.size(30)
};
Container::new(content) Container::new(content)
.width(Length::Fill) .width(Length::Fill)

View file

@ -7,6 +7,9 @@ description = "A wgpu renderer for Iced"
license = "MIT AND OFL-1.1" license = "MIT AND OFL-1.1"
repository = "https://github.com/hecrj/iced" repository = "https://github.com/hecrj/iced"
[features]
svg = ["resvg"]
[dependencies] [dependencies]
iced_native = { version = "0.1.0", path = "../native" } iced_native = { version = "0.1.0", path = "../native" }
wgpu = "0.4" wgpu = "0.4"
@ -17,4 +20,4 @@ image = "0.22"
glam = "0.8" glam = "0.8"
font-kit = "0.4" font-kit = "0.4"
log = "0.4" log = "0.4"
resvg = { version = "0.8", features = ["raqote-backend"] } resvg = { version = "0.8", features = ["raqote-backend"], optional = true }

View file

@ -1,4 +1,5 @@
mod raster; mod raster;
#[cfg(feature = "svg")]
mod vector; mod vector;
use crate::Transformation; use crate::Transformation;
@ -9,6 +10,7 @@ use std::{cell::RefCell, mem};
#[derive(Debug)] #[derive(Debug)]
pub struct Pipeline { pub struct Pipeline {
raster_cache: RefCell<raster::Cache>, raster_cache: RefCell<raster::Cache>,
#[cfg(feature = "svg")]
vector_cache: RefCell<vector::Cache>, vector_cache: RefCell<vector::Cache>,
pipeline: wgpu::RenderPipeline, pipeline: wgpu::RenderPipeline,
@ -190,6 +192,7 @@ impl Pipeline {
Pipeline { Pipeline {
raster_cache: RefCell::new(raster::Cache::new()), raster_cache: RefCell::new(raster::Cache::new()),
#[cfg(feature = "svg")]
vector_cache: RefCell::new(vector::Cache::new()), vector_cache: RefCell::new(vector::Cache::new()),
pipeline, pipeline,
@ -209,6 +212,7 @@ impl Pipeline {
memory.dimensions() memory.dimensions()
} }
#[cfg(feature = "svg")]
pub fn viewport_dimensions(&self, handle: &svg::Handle) -> (u32, u32) { pub fn viewport_dimensions(&self, handle: &svg::Handle) -> (u32, u32) {
let mut cache = self.vector_cache.borrow_mut(); let mut cache = self.vector_cache.borrow_mut();
@ -227,7 +231,7 @@ impl Pipeline {
transformation: Transformation, transformation: Transformation,
bounds: Rectangle<u32>, bounds: Rectangle<u32>,
target: &wgpu::TextureView, target: &wgpu::TextureView,
scale: f32, _scale: f32,
) { ) {
let uniforms_buffer = device let uniforms_buffer = device
.create_buffer_mapped(1, wgpu::BufferUsage::COPY_SRC) .create_buffer_mapped(1, wgpu::BufferUsage::COPY_SRC)
@ -255,17 +259,23 @@ impl Pipeline {
memory.upload(device, encoder, &self.texture_layout) memory.upload(device, encoder, &self.texture_layout)
} }
Handle::Vector(handle) => { Handle::Vector(_handle) => {
let mut cache = self.vector_cache.borrow_mut(); #[cfg(feature = "svg")]
{
let mut cache = self.vector_cache.borrow_mut();
cache.upload( cache.upload(
handle, _handle,
image.scale, image.scale,
scale, _scale,
device, device,
encoder, encoder,
&self.texture_layout, &self.texture_layout,
) )
}
#[cfg(not(feature = "svg"))]
None
} }
}; };
@ -333,6 +343,8 @@ impl Pipeline {
pub fn trim_cache(&mut self) { pub fn trim_cache(&mut self) {
self.raster_cache.borrow_mut().trim(); self.raster_cache.borrow_mut().trim();
#[cfg(feature = "svg")]
self.vector_cache.borrow_mut().trim(); self.vector_cache.borrow_mut().trim();
} }
} }

View file

@ -6,6 +6,8 @@ mod radio;
mod row; mod row;
mod scrollable; mod scrollable;
mod slider; mod slider;
mod svg;
mod text; mod text;
mod text_input; mod text_input;
#[cfg(feature = "svg")]
mod svg;