Skip to content

Commit

Permalink
Use Arc instead of Rc everywhere.
Browse files Browse the repository at this point in the history
  • Loading branch information
RazrFalcon committed Feb 10, 2024
1 parent 70c6d6f commit e30e0fa
Show file tree
Hide file tree
Showing 14 changed files with 107 additions and 101 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ This changelog also contains important changes in dependencies.

## [Unreleased]
### Added
- `usvg::Tree` is `Send + Sync` compatible now.
- `usvg::XmlOptions::preserve_text` to control how `usvg` generates an SVG.
- `usvg::Image::abs_bounding_box`

Expand All @@ -32,7 +33,7 @@ This changelog also contains important changes in dependencies.
- `usvg::Text::flattened` returns `&Group` and not `Option<&Group>` now.
- `usvg::ImageHrefDataResolverFn` and `usvg::ImageHrefStringResolverFn`
require `fontdb::Database` argument.
- All shared nodes are stored in `Rc` and not `Rc<RefCell>` now.
- All shared nodes are stored in `Arc` and not `Rc<RefCell>` now.

### Removed
- `usvg::Tree::postprocess()` and `usvg::PostProcessingSteps`. No longer needed.
Expand Down
6 changes: 3 additions & 3 deletions crates/usvg/src/parser/clippath.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

use std::rc::Rc;
use std::str::FromStr;
use std::sync::Arc;

use super::converter;
use super::svgtree::{AId, EId, SvgNode};
Expand All @@ -13,7 +13,7 @@ pub(crate) fn convert(
node: SvgNode,
state: &converter::State,
cache: &mut converter::Cache,
) -> Option<Rc<ClipPath>> {
) -> Option<Arc<ClipPath>> {
// A `clip-path` attribute must reference a `clipPath` element.
if node.tag_name() != Some(EId::ClipPath) {
return None;
Expand Down Expand Up @@ -58,7 +58,7 @@ pub(crate) fn convert(

if clip.root.has_children() {
clip.root.calculate_bounding_boxes();
let clip = Rc::new(clip);
let clip = Arc::new(clip);
cache
.clip_paths
.insert(node.element_id().to_string(), clip.clone());
Expand Down
10 changes: 5 additions & 5 deletions crates/usvg/src/parser/converter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

use std::collections::{HashMap, HashSet};
use std::hash::{Hash, Hasher};
use std::rc::Rc;
use std::str::FromStr;
use std::sync::Arc;

use svgtypes::{Length, LengthUnit as Unit, PaintOrderKind, TransformOrigin};

Expand All @@ -32,9 +32,9 @@ pub struct State<'a> {

#[derive(Clone, Default)]
pub struct Cache {
pub clip_paths: HashMap<String, Rc<ClipPath>>,
pub masks: HashMap<String, Rc<Mask>>,
pub filters: HashMap<String, Rc<filter::Filter>>,
pub clip_paths: HashMap<String, Arc<ClipPath>>,
pub masks: HashMap<String, Arc<Mask>>,
pub filters: HashMap<String, Arc<filter::Filter>>,
pub paint: HashMap<String, Paint>,

// used for ID generation
Expand Down Expand Up @@ -705,7 +705,7 @@ pub(crate) fn convert_group(

fn convert_path(
node: SvgNode,
path: Rc<tiny_skia_path::Path>,
path: Arc<tiny_skia_path::Path>,
state: &State,
cache: &mut Cache,
parent: &mut Group,
Expand Down
12 changes: 6 additions & 6 deletions crates/usvg/src/parser/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
//! A collection of SVG filters.

use std::collections::HashSet;
use std::rc::Rc;
use std::str::FromStr;
use std::sync::Arc;

use strict_num::PositiveF32;
use svgtypes::{Length, LengthUnit as Unit};
Expand Down Expand Up @@ -35,7 +35,7 @@ pub(crate) fn convert(
node: SvgNode,
state: &converter::State,
cache: &mut converter::Cache,
) -> Result<Vec<Rc<Filter>>, ()> {
) -> Result<Vec<Arc<Filter>>, ()> {
let value = match node.attribute::<&str>(AId::Filter) {
Some(v) => v,
None => return Ok(Vec::new()),
Expand All @@ -45,7 +45,7 @@ pub(crate) fn convert(
let mut filters = Vec::new();

let create_base_filter_func =
|kind, filters: &mut Vec<Rc<Filter>>, cache: &mut converter::Cache| {
|kind, filters: &mut Vec<Arc<Filter>>, cache: &mut converter::Cache| {
// Filter functions, unlike `filter` elements, do not have a filter region.
// We're currently do not support an unlimited region, so we simply use a fairly large one.
// This if far from ideal, but good for now.
Expand All @@ -57,7 +57,7 @@ pub(crate) fn convert(
_ => NonZeroRect::from_xywh(-0.1, -0.1, 1.2, 1.2).unwrap(),
};

filters.push(Rc::new(Filter {
filters.push(Arc::new(Filter {
id: cache.gen_filter_id(),
units: Units::ObjectBoundingBox,
primitive_units: Units::UserSpaceOnUse,
Expand Down Expand Up @@ -156,7 +156,7 @@ fn convert_url(
node: SvgNode,
state: &converter::State,
cache: &mut converter::Cache,
) -> Result<Option<Rc<Filter>>, ()> {
) -> Result<Option<Arc<Filter>>, ()> {
if let Some(filter) = cache.filters.get(node.element_id()) {
return Ok(Some(filter.clone()));
}
Expand Down Expand Up @@ -214,7 +214,7 @@ fn convert_url(

let id = NonEmptyString::new(node.element_id().to_string()).ok_or(())?;

let filter = Rc::new(Filter {
let filter = Arc::new(Filter {
id,
units,
primitive_units,
Expand Down
6 changes: 3 additions & 3 deletions crates/usvg/src/parser/marker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

use std::rc::Rc;
use std::sync::Arc;

use strict_num::NonZeroPositiveF32;
use svgtypes::Length;
Expand Down Expand Up @@ -121,14 +121,14 @@ fn resolve(

let mut clip_path = ClipPath::empty(cache.gen_clip_path_id());

let mut path = Path::new_simple(Rc::new(tiny_skia_path::PathBuilder::from_rect(
let mut path = Path::new_simple(Arc::new(tiny_skia_path::PathBuilder::from_rect(
clip_rect.to_rect(),
)))?;
path.fill = Some(Fill::default());

clip_path.root.children.push(Node::Path(Box::new(path)));

Some(Rc::new(clip_path))
Some(Arc::new(clip_path))
} else {
None
};
Expand Down
6 changes: 3 additions & 3 deletions crates/usvg/src/parser/mask.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

use std::rc::Rc;
use std::sync::Arc;

use svgtypes::{Length, LengthUnit as Unit};

Expand All @@ -14,7 +14,7 @@ pub(crate) fn convert(
node: SvgNode,
state: &converter::State,
cache: &mut converter::Cache,
) -> Option<Rc<Mask>> {
) -> Option<Arc<Mask>> {
// A `mask` attribute must reference a `mask` element.
if node.tag_name() != Some(EId::Mask) {
return None;
Expand Down Expand Up @@ -75,7 +75,7 @@ pub(crate) fn convert(

if mask.root.has_children() {
mask.root.calculate_bounding_boxes();
let mask = Rc::new(mask);
let mask = Arc::new(mask);
cache
.masks
.insert(node.element_id().to_string(), mask.clone());
Expand Down
8 changes: 4 additions & 4 deletions crates/usvg/src/parser/paint_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

use std::rc::Rc;
use std::str::FromStr;
use std::sync::Arc;

use strict_num::PositiveF32;
use svgtypes::{Length, LengthUnit as Unit};
Expand Down Expand Up @@ -77,7 +77,7 @@ fn convert_linear(node: SvgNode, state: &converter::State) -> Option<ServerOrCol
},
};

Some(ServerOrColor::Server(Paint::LinearGradient(Rc::new(
Some(ServerOrColor::Server(Paint::LinearGradient(Arc::new(
gradient,
))))
}
Expand Down Expand Up @@ -140,7 +140,7 @@ fn convert_radial(node: SvgNode, state: &converter::State) -> Option<ServerOrCol
},
};

Some(ServerOrColor::Server(Paint::RadialGradient(Rc::new(
Some(ServerOrColor::Server(Paint::RadialGradient(Arc::new(
gradient,
))))
}
Expand Down Expand Up @@ -200,7 +200,7 @@ fn convert_pattern(

patt.root.calculate_bounding_boxes();

Some(ServerOrColor::Server(Paint::Pattern(Rc::new(patt))))
Some(ServerOrColor::Server(Paint::Pattern(Arc::new(patt))))
}

fn convert_spread_method(node: SvgNode) -> SpreadMethod {
Expand Down
32 changes: 16 additions & 16 deletions crates/usvg/src/parser/shapes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

use std::rc::Rc;
use std::sync::Arc;

use svgtypes::Length;
use tiny_skia_path::Path;
Expand All @@ -11,7 +11,7 @@ use super::svgtree::{AId, EId, SvgNode};
use super::{converter, units};
use crate::{ApproxEqUlps, IsValidLength, Rect};

pub(crate) fn convert(node: SvgNode, state: &converter::State) -> Option<Rc<Path>> {
pub(crate) fn convert(node: SvgNode, state: &converter::State) -> Option<Arc<Path>> {
match node.tag_name()? {
EId::Rect => convert_rect(node, state),
EId::Circle => convert_circle(node, state),
Expand All @@ -24,7 +24,7 @@ pub(crate) fn convert(node: SvgNode, state: &converter::State) -> Option<Rc<Path
}
}

pub(crate) fn convert_path(node: SvgNode) -> Option<Rc<Path>> {
pub(crate) fn convert_path(node: SvgNode) -> Option<Arc<Path>> {
let value: &str = node.attribute(AId::D)?;
let mut builder = tiny_skia_path::PathBuilder::new();
for segment in svgtypes::SimplifyingPathParser::from(value) {
Expand Down Expand Up @@ -61,10 +61,10 @@ pub(crate) fn convert_path(node: SvgNode) -> Option<Rc<Path>> {
}
}

builder.finish().map(Rc::new)
builder.finish().map(Arc::new)
}

fn convert_rect(node: SvgNode, state: &converter::State) -> Option<Rc<Path>> {
fn convert_rect(node: SvgNode, state: &converter::State) -> Option<Arc<Path>> {
// 'width' and 'height' attributes must be positive and non-zero.
let width = node.convert_user_length(AId::Width, state, Length::zero());
let height = node.convert_user_length(AId::Height, state, Length::zero());
Expand Down Expand Up @@ -122,7 +122,7 @@ fn convert_rect(node: SvgNode, state: &converter::State) -> Option<Rc<Path>> {
builder.finish()?
};

Some(Rc::new(path))
Some(Arc::new(path))
}

fn resolve_rx_ry(node: SvgNode, state: &converter::State) -> (f32, f32) {
Expand Down Expand Up @@ -160,7 +160,7 @@ fn resolve_rx_ry(node: SvgNode, state: &converter::State) -> (f32, f32) {
}
}

fn convert_line(node: SvgNode, state: &converter::State) -> Option<Rc<Path>> {
fn convert_line(node: SvgNode, state: &converter::State) -> Option<Arc<Path>> {
let x1 = node.convert_user_length(AId::X1, state, Length::zero());
let y1 = node.convert_user_length(AId::Y1, state, Length::zero());
let x2 = node.convert_user_length(AId::X2, state, Length::zero());
Expand All @@ -169,18 +169,18 @@ fn convert_line(node: SvgNode, state: &converter::State) -> Option<Rc<Path>> {
let mut builder = tiny_skia_path::PathBuilder::new();
builder.move_to(x1, y1);
builder.line_to(x2, y2);
builder.finish().map(Rc::new)
builder.finish().map(Arc::new)
}

fn convert_polyline(node: SvgNode) -> Option<Rc<Path>> {
fn convert_polyline(node: SvgNode) -> Option<Arc<Path>> {
let builder = points_to_path(node, "Polyline")?;
builder.finish().map(Rc::new)
builder.finish().map(Arc::new)
}

fn convert_polygon(node: SvgNode) -> Option<Rc<Path>> {
fn convert_polygon(node: SvgNode) -> Option<Arc<Path>> {
let mut builder = points_to_path(node, "Polygon")?;
builder.close();
builder.finish().map(Rc::new)
builder.finish().map(Arc::new)
}

fn points_to_path(node: SvgNode, eid: &str) -> Option<tiny_skia_path::PathBuilder> {
Expand Down Expand Up @@ -220,7 +220,7 @@ fn points_to_path(node: SvgNode, eid: &str) -> Option<tiny_skia_path::PathBuilde
Some(builder)
}

fn convert_circle(node: SvgNode, state: &converter::State) -> Option<Rc<Path>> {
fn convert_circle(node: SvgNode, state: &converter::State) -> Option<Arc<Path>> {
let cx = node.convert_user_length(AId::Cx, state, Length::zero());
let cy = node.convert_user_length(AId::Cy, state, Length::zero());
let r = node.convert_user_length(AId::R, state, Length::zero());
Expand All @@ -236,7 +236,7 @@ fn convert_circle(node: SvgNode, state: &converter::State) -> Option<Rc<Path>> {
ellipse_to_path(cx, cy, r, r)
}

fn convert_ellipse(node: SvgNode, state: &converter::State) -> Option<Rc<Path>> {
fn convert_ellipse(node: SvgNode, state: &converter::State) -> Option<Arc<Path>> {
let cx = node.convert_user_length(AId::Cx, state, Length::zero());
let cy = node.convert_user_length(AId::Cy, state, Length::zero());
let (rx, ry) = resolve_rx_ry(node, state);
Expand All @@ -260,15 +260,15 @@ fn convert_ellipse(node: SvgNode, state: &converter::State) -> Option<Rc<Path>>
ellipse_to_path(cx, cy, rx, ry)
}

fn ellipse_to_path(cx: f32, cy: f32, rx: f32, ry: f32) -> Option<Rc<Path>> {
fn ellipse_to_path(cx: f32, cy: f32, rx: f32, ry: f32) -> Option<Arc<Path>> {
let mut builder = tiny_skia_path::PathBuilder::new();
builder.move_to(cx + rx, cy);
builder.arc_to(rx, ry, 0.0, false, true, cx, cy + ry);
builder.arc_to(rx, ry, 0.0, false, true, cx - rx, cy);
builder.arc_to(rx, ry, 0.0, false, true, cx, cy - ry);
builder.arc_to(rx, ry, 0.0, false, true, cx + rx, cy);
builder.close();
builder.finish().map(Rc::new)
builder.finish().map(Arc::new)
}

trait PathBuilderExt {
Expand Down
6 changes: 3 additions & 3 deletions crates/usvg/src/parser/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

use std::rc::Rc;
use std::sync::Arc;

use kurbo::{ParamCurve, ParamCurveArclen};
use svgtypes::{parse_font_families, FontFamily, Length, LengthUnit};
Expand Down Expand Up @@ -363,7 +363,7 @@ fn resolve_text_flow(node: SvgNode, state: &converter::State) -> Option<TextFlow
let path = if !transform.is_identity() {
let mut path_copy = path.as_ref().clone();
path_copy = path_copy.transform(transform)?;
Rc::new(path_copy)
Arc::new(path_copy)
} else {
path
};
Expand All @@ -379,7 +379,7 @@ fn resolve_text_flow(node: SvgNode, state: &converter::State) -> Option<TextFlow
};

let id = NonEmptyString::new(linked_node.element_id().to_string())?;
Some(TextFlow::Path(Rc::new(TextPath {
Some(TextFlow::Path(Arc::new(TextPath {
id,
start_offset,
path,
Expand Down
6 changes: 3 additions & 3 deletions crates/usvg/src/parser/use_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

use std::rc::Rc;
use std::sync::Arc;

use svgtypes::{Length, LengthUnit};

Expand Down Expand Up @@ -207,7 +207,7 @@ fn clip_element(

let mut clip_path = crate::ClipPath::empty(cache.gen_clip_path_id());

let mut path = Path::new_simple(Rc::new(tiny_skia_path::PathBuilder::from_rect(
let mut path = Path::new_simple(Arc::new(tiny_skia_path::PathBuilder::from_rect(
clip_rect.to_rect(),
)))
.unwrap();
Expand All @@ -224,7 +224,7 @@ fn clip_element(
Group {
id,
transform,
clip_path: Some(Rc::new(clip_path)),
clip_path: Some(Arc::new(clip_path)),
..Group::empty()
}
}
Expand Down

0 comments on commit e30e0fa

Please sign in to comment.