diff --git a/CHANGELOG.md b/CHANGELOG.md index 016348cd..d41dde09 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -32,6 +32,10 @@ This changelog also contains important changes in dependencies. - `usvg::fontdb::Database` should be set in `usvg::Options` and not passed to the parser separately now. - `usvg::Options` and `usvg::ImageHrefResolver` have a lifetime now. +- Replace `usvg::Visibility` enum with just `bool`. +- `usvg::Path::visibility()` is replaced with `usvg::Path::is_visible()` +- `usvg::Image::visibility()` is replaced with `usvg::Image::is_visible()` +- `usvg::TextSpan::visibility()` is replaced with `usvg::TextSpan::is_visible()` - Always represent `feImage` content as a link to an element.
In SVG, `feImage` can contain a link to an element or a base64 image data, just like `image`. From now, the inlined base64 data will always be represented by a link to an actual `image` element. @@ -63,6 +67,7 @@ This changelog also contains important changes in dependencies. - `usvg::Image::view_box`. No longer needed. - `usvg::Image::pattern`. No longer needed. - `usvg::utils::align_pos`. No longer needed. +- `usvg::Visibility`. No longer needed. - (c-api) `resvg_get_image_viewbox`. Use `resvg_get_image_size` instead. ### Fixed diff --git a/crates/resvg/src/clip.rs b/crates/resvg/src/clip.rs index 2f36a6a8..39c9e428 100644 --- a/crates/resvg/src/clip.rs +++ b/crates/resvg/src/clip.rs @@ -37,7 +37,7 @@ fn draw_children( for child in parent.children() { match child { usvg::Node::Path(ref path) => { - if path.visibility() != usvg::Visibility::Visible { + if !path.is_visible() { continue; } diff --git a/crates/resvg/src/image.rs b/crates/resvg/src/image.rs index a507edcf..68ac72ce 100644 --- a/crates/resvg/src/image.rs +++ b/crates/resvg/src/image.rs @@ -7,7 +7,7 @@ pub fn render( transform: tiny_skia::Transform, pixmap: &mut tiny_skia::PixmapMut, ) { - if image.visibility() != usvg::Visibility::Visible { + if !image.is_visible() { return; } diff --git a/crates/resvg/src/path.rs b/crates/resvg/src/path.rs index 88c0537a..333e029e 100644 --- a/crates/resvg/src/path.rs +++ b/crates/resvg/src/path.rs @@ -11,7 +11,7 @@ pub fn render( transform: tiny_skia::Transform, pixmap: &mut tiny_skia::PixmapMut, ) { - if path.visibility() != usvg::Visibility::Visible { + if !path.is_visible() { return; } diff --git a/crates/usvg/src/parser/converter.rs b/crates/usvg/src/parser/converter.rs index fd11d06f..55584db3 100644 --- a/crates/usvg/src/parser/converter.rs +++ b/crates/usvg/src/parser/converter.rs @@ -785,7 +785,8 @@ fn convert_path( let has_bbox = tiny_skia_path.bounds().width() > 0.0 && tiny_skia_path.bounds().height() > 0.0; let mut fill = super::style::resolve_fill(node, has_bbox, state, cache); let mut stroke = super::style::resolve_stroke(node, has_bbox, state, cache); - let mut visibility: Visibility = node.find_attribute(AId::Visibility).unwrap_or_default(); + let visibility: Visibility = node.find_attribute(AId::Visibility).unwrap_or_default(); + let mut visible = visibility == Visibility::Visible; let rendering_mode: ShapeRendering = node .find_attribute(AId::ShapeRendering) .unwrap_or(state.opt.shape_rendering); @@ -799,7 +800,7 @@ fn convert_path( // If a path doesn't have a fill or a stroke then it's invisible. // By setting `visibility` to `hidden` we are disabling rendering of this path. if fill.is_none() && stroke.is_none() { - visibility = Visibility::Hidden; + visible = false; } if let Some(fill) = fill.as_mut() { @@ -881,7 +882,7 @@ fn convert_path( let path = Path::new( id, - visibility, + visible, fill, stroke, paint_order, diff --git a/crates/usvg/src/parser/filter.rs b/crates/usvg/src/parser/filter.rs index fe2c6791..5dd47df4 100644 --- a/crates/usvg/src/parser/filter.rs +++ b/crates/usvg/src/parser/filter.rs @@ -867,7 +867,7 @@ fn convert_image_inner( super::image::convert_inner( img_data, cache.gen_image_id().take(), - crate::Visibility::Visible, + true, rendering_mode, aspect, actual_size, diff --git a/crates/usvg/src/parser/image.rs b/crates/usvg/src/parser/image.rs index 8b1605d9..3c0aa947 100644 --- a/crates/usvg/src/parser/image.rs +++ b/crates/usvg/src/parser/image.rs @@ -138,7 +138,9 @@ pub(crate) fn convert( let kind = get_href_data(href, state)?; - let visibility = node.find_attribute(AId::Visibility).unwrap_or_default(); + let visibility: Visibility = node.find_attribute(AId::Visibility).unwrap_or_default(); + let visible = visibility == Visibility::Visible; + let rendering_mode = node .find_attribute(AId::ImageRendering) .unwrap_or(state.opt.image_rendering); @@ -188,7 +190,7 @@ pub(crate) fn convert( convert_inner( kind, id, - visibility, + visible, rendering_mode, aspect, actual_size, @@ -201,7 +203,7 @@ pub(crate) fn convert( pub(crate) fn convert_inner( kind: ImageKind, id: String, - visibility: Visibility, + visible: bool, rendering_mode: ImageRendering, aspect: AspectRatio, actual_size: Size, @@ -235,7 +237,7 @@ pub(crate) fn convert_inner( g.id = id; g.children.push(Node::Image(Box::new(Image { id: String::new(), - visibility, + visible, size: actual_size, rendering_mode, kind, diff --git a/crates/usvg/src/parser/text.rs b/crates/usvg/src/parser/text.rs index 9c9dc89e..2d626575 100644 --- a/crates/usvg/src/parser/text.rs +++ b/crates/usvg/src/parser/text.rs @@ -273,6 +273,8 @@ fn collect_text_chunks_impl( } } + let visibility: Visibility = parent.find_attribute(AId::Visibility).unwrap_or_default(); + let span = TextSpan { start: 0, end: 0, @@ -284,7 +286,7 @@ fn collect_text_chunks_impl( small_caps: parent.find_attribute::<&str>(AId::FontVariant) == Some("small-caps"), apply_kerning, decoration: resolve_decoration(parent, state, cache), - visibility: parent.find_attribute(AId::Visibility).unwrap_or_default(), + visible: visibility == Visibility::Visible, dominant_baseline, alignment_baseline: parent .find_attribute(AId::AlignmentBaseline) diff --git a/crates/usvg/src/text/flatten.rs b/crates/usvg/src/text/flatten.rs index 6539b5c7..6972e4a0 100644 --- a/crates/usvg/src/text/flatten.rs +++ b/crates/usvg/src/text/flatten.rs @@ -33,7 +33,7 @@ fn push_outline_paths( if let Some(path) = builder.finish().and_then(|p| { Path::new( String::new(), - span.visibility, + span.visible, span.fill.clone(), span.stroke.clone(), span.paint_order, @@ -226,7 +226,7 @@ impl DatabaseExt for Database { let bitmap_image = BitmapImage { image: Image { id: String::new(), - visibility: Visibility::Visible, + visible: true, size: Size::from_wh(image.width as f32, image.height as f32)?, rendering_mode: ImageRendering::OptimizeQuality, kind: ImageKind::PNG(Arc::new(image.data.into())), diff --git a/crates/usvg/src/text/layout.rs b/crates/usvg/src/text/layout.rs index bca76667..4b274204 100644 --- a/crates/usvg/src/text/layout.rs +++ b/crates/usvg/src/text/layout.rs @@ -18,7 +18,7 @@ use crate::tree::{BBox, IsValidLength}; use crate::{ AlignmentBaseline, ApproxZeroUlps, BaselineShift, DominantBaseline, Fill, FillRule, Font, FontResolver, LengthAdjust, PaintOrder, Path, ShapeRendering, Stroke, Text, TextAnchor, - TextChunk, TextDecorationStyle, TextFlow, TextPath, TextSpan, Visibility, WritingMode, + TextChunk, TextDecorationStyle, TextFlow, TextPath, TextSpan, WritingMode, }; /// A glyph that has already been positioned correctly. @@ -164,7 +164,7 @@ pub struct Span { /// The font size of the span. pub font_size: NonZeroPositiveF32, /// The visibility of the span. - pub visibility: Visibility, + pub visible: bool, /// The glyphs that make up the span. pub positioned_glyphs: Vec, /// An underline text decoration of the span. @@ -359,7 +359,7 @@ pub(crate) fn layout_text( stroke: span.stroke.clone(), paint_order: span.paint_order, font_size: span.font_size, - visibility: span.visibility, + visible: span.visible, positioned_glyphs, underline, overline, @@ -494,7 +494,7 @@ pub(crate) fn convert_decoration( Path::new( String::new(), - span.visibility, + span.visible, decoration.fill.take(), decoration.stroke.take(), PaintOrder::default(), diff --git a/crates/usvg/src/tree/mod.rs b/crates/usvg/src/tree/mod.rs index ea247d45..cf97b509 100644 --- a/crates/usvg/src/tree/mod.rs +++ b/crates/usvg/src/tree/mod.rs @@ -79,7 +79,7 @@ pub(crate) enum Units { /// `visibility` attribute in the SVG. #[allow(missing_docs)] #[derive(Clone, Copy, PartialEq, Debug)] -pub enum Visibility { +pub(crate) enum Visibility { Visible, Hidden, Collapse, @@ -1231,7 +1231,7 @@ impl Default for PaintOrder { #[derive(Clone, Debug)] pub struct Path { pub(crate) id: String, - pub(crate) visibility: Visibility, + pub(crate) visible: bool, pub(crate) fill: Option, pub(crate) stroke: Option, pub(crate) paint_order: PaintOrder, @@ -1248,7 +1248,7 @@ impl Path { pub(crate) fn new_simple(data: Arc) -> Option { Self::new( String::new(), - Visibility::default(), + true, None, None, PaintOrder::default(), @@ -1260,7 +1260,7 @@ impl Path { pub(crate) fn new( id: String, - visibility: Visibility, + visible: bool, fill: Option, stroke: Option, paint_order: PaintOrder, @@ -1289,7 +1289,7 @@ impl Path { Some(Path { id, - visibility, + visible, fill, stroke, paint_order, @@ -1313,8 +1313,8 @@ impl Path { } /// Element visibility. - pub fn visibility(&self) -> Visibility { - self.visibility + pub fn is_visible(&self) -> bool { + self.visible } /// Fill style. @@ -1459,7 +1459,7 @@ impl std::fmt::Debug for ImageKind { #[derive(Clone, Debug)] pub struct Image { pub(crate) id: String, - pub(crate) visibility: Visibility, + pub(crate) visible: bool, pub(crate) size: Size, pub(crate) rendering_mode: ImageRendering, pub(crate) kind: ImageKind, @@ -1478,8 +1478,8 @@ impl Image { } /// Element visibility. - pub fn visibility(&self) -> Visibility { - self.visibility + pub fn is_visible(&self) -> bool { + self.visible } /// The actual image size. diff --git a/crates/usvg/src/tree/text.rs b/crates/usvg/src/tree/text.rs index 7346fbd2..64903fa6 100644 --- a/crates/usvg/src/tree/text.rs +++ b/crates/usvg/src/tree/text.rs @@ -9,9 +9,7 @@ pub use svgtypes::FontFamily; #[cfg(feature = "text")] use crate::layout::Span; -use crate::{ - Fill, Group, NonEmptyString, PaintOrder, Rect, Stroke, TextRendering, Transform, Visibility, -}; +use crate::{Fill, Group, NonEmptyString, PaintOrder, Rect, Stroke, TextRendering, Transform}; /// A font stretch property. #[allow(missing_docs)] @@ -232,7 +230,7 @@ pub struct TextSpan { pub(crate) dominant_baseline: DominantBaseline, pub(crate) alignment_baseline: AlignmentBaseline, pub(crate) baseline_shift: Vec, - pub(crate) visibility: Visibility, + pub(crate) visible: bool, pub(crate) letter_spacing: f32, pub(crate) word_spacing: f32, pub(crate) text_length: Option, @@ -316,8 +314,8 @@ impl TextSpan { } /// A visibility property. - pub fn visibility(&self) -> Visibility { - self.visibility + pub fn is_visible(&self) -> bool { + self.visible } /// A letter spacing property. diff --git a/crates/usvg/src/writer.rs b/crates/usvg/src/writer.rs index 9eee7183..e2d5c6ec 100644 --- a/crates/usvg/src/writer.rs +++ b/crates/usvg/src/writer.rs @@ -601,7 +601,7 @@ fn write_text_path_paths(parent: &Group, opt: &WriteOptions, xml: &mut XmlWriter if let TextFlow::Path(ref text_path) = chunk.text_flow { let path = Path::new( text_path.id().to_string(), - Visibility::default(), + true, None, None, PaintOrder::default(), @@ -640,7 +640,7 @@ fn write_element(node: &Node, is_clip_path: bool, opt: &WriteOptions, xml: &mut xml.write_svg_attribute(AId::Width, &img.size().width()); xml.write_svg_attribute(AId::Height, &img.size().height()); - xml.write_visibility(img.visibility); + xml.write_visibility(img.visible); match img.rendering_mode { ImageRendering::OptimizeQuality => {} @@ -883,7 +883,7 @@ trait XmlWriterExt { fn write_color(&mut self, id: AId, color: Color); fn write_units(&mut self, id: AId, units: Units, def: Units); fn write_transform(&mut self, id: AId, units: Transform, opt: &WriteOptions); - fn write_visibility(&mut self, value: Visibility); + fn write_visibility(&mut self, value: bool); fn write_func_iri(&mut self, aid: AId, id: &str, opt: &WriteOptions); fn write_rect_attrs(&mut self, r: NonZeroRect); fn write_numbers(&mut self, aid: AId, list: &[f32]); @@ -967,11 +967,9 @@ impl XmlWriterExt for XmlWriter { } } - fn write_visibility(&mut self, value: Visibility) { - match value { - Visibility::Visible => {} - Visibility::Hidden => self.write_attribute(AId::Visibility.to_str(), "hidden"), - Visibility::Collapse => self.write_attribute(AId::Visibility.to_str(), "collapse"), + fn write_visibility(&mut self, value: bool) { + if !value { + self.write_attribute(AId::Visibility.to_str(), "hidden"); } } @@ -1187,7 +1185,7 @@ fn write_path( write_fill(&path.fill, is_clip_path, opt, xml); write_stroke(&path.stroke, opt, xml); - xml.write_visibility(path.visibility); + xml.write_visibility(path.visible); if path.paint_order == PaintOrder::StrokeAndFill { xml.write_svg_attribute(AId::PaintOrder, "stroke"); @@ -1480,11 +1478,7 @@ fn write_span( xml.write_svg_attribute(AId::FontSize, &span.font_size); - match span.visibility { - Visibility::Visible => {} - Visibility::Hidden => xml.write_svg_attribute(AId::Visibility, "hidden"), - Visibility::Collapse => xml.write_svg_attribute(AId::Visibility, "collapse"), - } + xml.write_visibility(span.visible); if span.letter_spacing != 0.0 { xml.write_svg_attribute(AId::LetterSpacing, &span.letter_spacing);