Skip to content

Commit

Permalink
Support retrieving class names
Browse files Browse the repository at this point in the history
  • Loading branch information
paxbun committed Jan 21, 2024
1 parent 5b509a4 commit 84d9284
Show file tree
Hide file tree
Showing 17 changed files with 97 additions and 25 deletions.
12 changes: 6 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion crates/c-api/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "resvg-capi"
version = "0.36.0"
version = "0.36.0+class"
authors = ["Yevhenii Reizner <razrfalcon@gmail.com>"]
keywords = ["svg", "render", "raster", "c-api"]
license = "MPL-2.0"
Expand Down
5 changes: 3 additions & 2 deletions crates/resvg/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "resvg"
version = "0.36.0"
version = "0.36.0+class"
authors = ["Yevhenii Reizner <razrfalcon@gmail.com>"]
keywords = ["svg", "render", "raster"]
license = "MPL-2.0"
Expand All @@ -23,7 +23,7 @@ png = { version = "0.17", optional = true }
rgb = "0.8"
svgtypes = "0.12"
tiny-skia = "0.11.2"
usvg = { path = "../usvg", version = "0.36.0", default-features = false }
usvg = { path = "../usvg", version = "0.36.0+class", default-features = false }

[dev-dependencies]
once_cell = "1.5"
Expand All @@ -41,3 +41,4 @@ memmap-fonts = ["usvg/memmap-fonts"]
# When disabled, `image` elements with SVG data will still be rendered.
# Adds around 200KiB to your binary.
raster-images = ["gif", "jpeg-decoder", "png"]
class = ["usvg/class"]
2 changes: 2 additions & 0 deletions crates/resvg/src/filter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1087,6 +1087,8 @@ fn apply_image(

let uimage = usvg::Image {
id: String::new(),
#[cfg(feature = "class")]
class: String::new(),
transform: usvg::Transform::default(),
visibility: usvg::Visibility::Visible,
view_box,
Expand Down
7 changes: 5 additions & 2 deletions crates/usvg-parser/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "usvg-parser"
version = "0.36.0"
version = "0.36.0+class"
authors = ["Yevhenii Reizner <razrfalcon@gmail.com>"]
keywords = ["svg"]
license = "MPL-2.0"
Expand All @@ -23,4 +23,7 @@ roxmltree = "0.18"
simplecss = "0.2"
siphasher = "0.3" # perfect hash implementation
svgtypes = "0.12"
usvg-tree = { path = "../usvg-tree", version = "0.36.0" }
usvg-tree = { path = "../usvg-tree", version = "0.36.0+class" }

[features]
class = ["usvg-tree/class"]
8 changes: 8 additions & 0 deletions crates/usvg-parser/src/converter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -584,9 +584,13 @@ pub(crate) fn convert_group(
} else {
String::new()
};
#[cfg(feature = "class")]
let class = node.class().to_string();

let g = parent.append_kind(NodeKind::Group(Group {
id,
#[cfg(feature = "class")]
class,
transform,
opacity,
blend_mode,
Expand Down Expand Up @@ -684,9 +688,13 @@ fn convert_path(
} else {
String::new()
};
#[cfg(feature = "class")]
let class = node.element_id().to_string();

parent.append_kind(NodeKind::Path(Path {
id,
#[cfg(feature = "class")]
class,
transform: Default::default(),
visibility,
fill,
Expand Down
4 changes: 4 additions & 0 deletions crates/usvg-parser/src/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,13 @@ pub(crate) fn convert(node: SvgNode, state: &converter::State, parent: &mut Node
} else {
String::new()
};
#[cfg(feature = "class")]
let class = node.class().to_string();

parent.append_kind(NodeKind::Image(Image {
id,
#[cfg(feature = "class")]
class,
transform: Default::default(),
visibility,
view_box,
Expand Down
9 changes: 9 additions & 0 deletions crates/usvg-parser/src/svgtree/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,15 @@ impl<'a, 'input: 'a> SvgNode<'a, 'input> {
self.attribute(AId::Id).unwrap_or("")
}

/// Returns element's `class` attribute value.
///
/// Returns an empty string otherwise.
#[cfg(feature = "class")]
#[inline]
pub fn class(&self) -> &'a str {
self.attribute(AId::Class).unwrap_or("")
}

/// Returns an attribute value.
pub fn attribute<T: FromValue<'a, 'input>>(&self, aid: AId) -> Option<T> {
let value = self
Expand Down
15 changes: 9 additions & 6 deletions crates/usvg-parser/src/svgtree/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -320,12 +320,15 @@ fn append_attribute<'input>(
value: roxmltree::StringStorage<'input>,
doc: &mut Document<'input>,
) -> bool {
match aid {
// The `style` attribute will be split into attributes, so we don't need it.
AId::Style |
// No need to copy a `class` attribute since CSS were already resolved.
AId::Class => return false,
_ => {}
// The `style` attribute will be split into attributes, so we don't need it.
if let AId::Style = aid {
return false;
}

#[cfg(not(feature = "class"))]
// No need to copy a `class` attribute since CSS were already resolved.
if let AId::Class = aid {
return false;
}

// Ignore `xlink:href` on `tspan` (which was originally `tref` or `a`),
Expand Down
4 changes: 4 additions & 0 deletions crates/usvg-parser/src/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,13 @@ pub(crate) fn convert(
} else {
String::new()
};
#[cfg(feature = "class")]
let class = text_node.class().to_string();

let text = Text {
id,
#[cfg(feature = "class")]
class,
transform: Transform::default(),
rendering_mode,
positions: pos_list,
Expand Down
5 changes: 3 additions & 2 deletions crates/usvg-text-layout/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "usvg-text-layout"
version = "0.36.0"
version = "0.36.0+class"
authors = ["Yevhenii Reizner <razrfalcon@gmail.com>"]
keywords = ["svg"]
license = "MPL-2.0"
Expand All @@ -19,11 +19,12 @@ rustybuzz = "0.10"
unicode-bidi = "0.3"
unicode-script = "0.5"
unicode-vo = "0.1"
usvg-tree = { path = "../usvg-tree", version = "0.36.0" }
usvg-tree = { path = "../usvg-tree", version = "0.36.0+class" }

[features]
default = ["system-fonts", "memmap-fonts"]
# Enables system fonts loading.
system-fonts = ["fontdb/fs", "fontdb/fontconfig"]
# Enables font files memmaping for faster loading.
memmap-fonts = ["fontdb/memmap"]
class = ["usvg-tree/class"]
2 changes: 2 additions & 0 deletions crates/usvg-text-layout/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -707,6 +707,8 @@ fn convert_span(

let path = Path {
id: String::new(),
#[cfg(feature = "class")]
class: String::new(),
transform: Transform::default(),
visibility: span.visibility,
fill,
Expand Down
5 changes: 4 additions & 1 deletion crates/usvg-tree/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "usvg-tree"
version = "0.36.0"
version = "0.36.0+class"
authors = ["Yevhenii Reizner <razrfalcon@gmail.com>"]
keywords = ["svg"]
license = "MPL-2.0"
Expand All @@ -17,3 +17,6 @@ rctree = "0.5"
strict-num = "0.1.1"
svgtypes = "0.12"
tiny-skia-path = "0.11.2"

[features]
class = []
27 changes: 27 additions & 0 deletions crates/usvg-tree/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -733,6 +733,17 @@ impl NodeKind {
}
}

/// Returns note's class names.
#[cfg(feature = "class")]
pub fn class(&self) -> &str {
match self {
NodeKind::Group(ref e) => e.class.as_str(),
NodeKind::Path(ref e) => e.class.as_str(),
NodeKind::Image(ref e) => e.class.as_str(),
NodeKind::Text(ref e) => e.class.as_str(),
}
}

/// Returns node's transform.
pub fn transform(&self) -> Transform {
match self {
Expand All @@ -759,6 +770,10 @@ pub struct Group {
/// Can be empty.
pub id: String,

/// Element's class names.
#[cfg(feature = "class")]
pub class: String,

/// Element transform.
pub transform: Transform,

Expand Down Expand Up @@ -792,6 +807,8 @@ impl Default for Group {
fn default() -> Self {
Group {
id: String::new(),
#[cfg(feature = "class")]
class: String::new(),
transform: Transform::default(),
opacity: Opacity::ONE,
blend_mode: BlendMode::Normal,
Expand Down Expand Up @@ -844,6 +861,10 @@ pub struct Path {
/// Can be empty.
pub id: String,

/// Element's class names.
#[cfg(feature = "class")]
pub class: String,

/// Element transform.
pub transform: Transform,

Expand Down Expand Up @@ -892,6 +913,8 @@ impl Path {
pub fn new(data: Rc<tiny_skia_path::Path>) -> Self {
Path {
id: String::new(),
#[cfg(feature = "class")]
class: String::new(),
transform: Transform::default(),
visibility: Visibility::Visible,
fill: None,
Expand Down Expand Up @@ -940,6 +963,10 @@ pub struct Image {
/// Can be empty.
pub id: String,

/// Element's class names.
#[cfg(feature = "class")]
pub class: String,

/// Element transform.
pub transform: Transform,

Expand Down
4 changes: 4 additions & 0 deletions crates/usvg-tree/src/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,10 @@ pub struct Text {
/// Can be empty.
pub id: String,

/// Element's class names.
#[cfg(feature = "class")]
pub class: String,

/// Element transform.
pub transform: Transform,

Expand Down
9 changes: 5 additions & 4 deletions crates/usvg/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "usvg"
version = "0.36.0"
version = "0.36.0+class"
authors = ["Yevhenii Reizner <razrfalcon@gmail.com>"]
keywords = ["svg"]
license = "MPL-2.0"
Expand All @@ -21,13 +21,13 @@ required-features = ["text", "system-fonts", "memmap-fonts"]
base64 = "0.21" # for embedded images
log = "0.4"
pico-args = { version = "0.5", features = ["eq-separator"] }
usvg-parser = { path = "../usvg-parser", version = "0.36.0" }
usvg-tree = { path = "../usvg-tree", version = "0.36.0" }
usvg-parser = { path = "../usvg-parser", version = "0.36.0+class" }
usvg-tree = { path = "../usvg-tree", version = "0.36.0+class" }
xmlwriter = "0.1"

[dependencies.usvg-text-layout]
path = "../usvg-text-layout"
version = "0.36.0"
version = "0.36.0+class"
default-features = false
optional = true

Expand All @@ -40,3 +40,4 @@ text = ["usvg-text-layout"]
system-fonts = ["usvg-text-layout/system-fonts"]
# Enables font files memmaping for faster loading.
memmap-fonts = ["usvg-text-layout/memmap-fonts"]
class = ["usvg-parser/class", "usvg-tree/class"]
2 changes: 1 addition & 1 deletion tools/explorer-thumbnailer/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "explorer-thumbnailer"
version = "0.36.0"
version = "0.36.0+class"
authors = ["gentoo90 <gentoo90@gmail.com>"]
license = "MPL-2.0"
edition = "2018"
Expand Down

0 comments on commit 84d9284

Please sign in to comment.