Skip to content

Commit

Permalink
Replace Visibility enum with just bool.
Browse files Browse the repository at this point in the history
`Visibility::Collapse` is never used anyway.
  • Loading branch information
RazrFalcon committed Jun 1, 2024
1 parent 80011fa commit 90852a6
Show file tree
Hide file tree
Showing 13 changed files with 50 additions and 48 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.<br>
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.
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion crates/resvg/src/clip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion crates/resvg/src/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion crates/resvg/src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
7 changes: 4 additions & 3 deletions crates/usvg/src/parser/converter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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() {
Expand Down Expand Up @@ -881,7 +882,7 @@ fn convert_path(

let path = Path::new(
id,
visibility,
visible,
fill,
stroke,
paint_order,
Expand Down
2 changes: 1 addition & 1 deletion crates/usvg/src/parser/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
10 changes: 6 additions & 4 deletions crates/usvg/src/parser/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -188,7 +190,7 @@ pub(crate) fn convert(
convert_inner(
kind,
id,
visibility,
visible,
rendering_mode,
aspect,
actual_size,
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 3 additions & 1 deletion crates/usvg/src/parser/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions crates/usvg/src/text/flatten.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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())),
Expand Down
8 changes: 4 additions & 4 deletions crates/usvg/src/text/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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<PositionedGlyph>,
/// An underline text decoration of the span.
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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(),
Expand Down
20 changes: 10 additions & 10 deletions crates/usvg/src/tree/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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<Fill>,
pub(crate) stroke: Option<Stroke>,
pub(crate) paint_order: PaintOrder,
Expand All @@ -1248,7 +1248,7 @@ impl Path {
pub(crate) fn new_simple(data: Arc<tiny_skia_path::Path>) -> Option<Self> {
Self::new(
String::new(),
Visibility::default(),
true,
None,
None,
PaintOrder::default(),
Expand All @@ -1260,7 +1260,7 @@ impl Path {

pub(crate) fn new(
id: String,
visibility: Visibility,
visible: bool,
fill: Option<Fill>,
stroke: Option<Stroke>,
paint_order: PaintOrder,
Expand Down Expand Up @@ -1289,7 +1289,7 @@ impl Path {

Some(Path {
id,
visibility,
visible,
fill,
stroke,
paint_order,
Expand All @@ -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.
Expand Down Expand Up @@ -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,
Expand All @@ -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.
Expand Down
10 changes: 4 additions & 6 deletions crates/usvg/src/tree/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -232,7 +230,7 @@ pub struct TextSpan {
pub(crate) dominant_baseline: DominantBaseline,
pub(crate) alignment_baseline: AlignmentBaseline,
pub(crate) baseline_shift: Vec<BaselineShift>,
pub(crate) visibility: Visibility,
pub(crate) visible: bool,
pub(crate) letter_spacing: f32,
pub(crate) word_spacing: f32,
pub(crate) text_length: Option<f32>,
Expand Down Expand Up @@ -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.
Expand Down
22 changes: 8 additions & 14 deletions crates/usvg/src/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down Expand Up @@ -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 => {}
Expand Down Expand Up @@ -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]);
Expand Down Expand Up @@ -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");
}
}

Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 90852a6

Please sign in to comment.