summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJovansonlee Cesar <ivanceras@gmail.com>2020-11-25 23:46:35 +0800
committerJovansonlee Cesar <ivanceras@gmail.com>2020-11-25 23:46:35 +0800
commitb89293bc61233f746474c57b23db09c71a6ac0c3 (patch)
tree09df40eda8a6889eced3e8584e5cc2e62fba1560
parent2bf94a2eddff2215b5f43e21658502c98d945941 (diff)
cargo fmt
-rw-r--r--svgbob/rustfmt.toml19
-rw-r--r--svgbob/src/buffer/cell_buffer.rs64
-rw-r--r--svgbob/src/buffer/cell_buffer/cell.rs54
-rw-r--r--svgbob/src/buffer/cell_buffer/contacts.rs8
-rw-r--r--svgbob/src/buffer/cell_buffer/endorse.rs18
-rw-r--r--svgbob/src/buffer/cell_buffer/span.rs27
-rw-r--r--svgbob/src/buffer/fragment_buffer.rs15
-rw-r--r--svgbob/src/buffer/fragment_buffer/direction.rs1
-rw-r--r--svgbob/src/buffer/fragment_buffer/fragment.rs145
-rw-r--r--svgbob/src/buffer/fragment_buffer/fragment/arc.rs4
-rw-r--r--svgbob/src/buffer/fragment_buffer/fragment/circle.rs5
-rw-r--r--svgbob/src/buffer/fragment_buffer/fragment/line.rs78
-rw-r--r--svgbob/src/buffer/fragment_buffer/fragment/marker_line.rs17
-rw-r--r--svgbob/src/buffer/fragment_buffer/fragment/polygon.rs20
-rw-r--r--svgbob/src/buffer/fragment_buffer/fragment/rect.rs9
-rw-r--r--svgbob/src/buffer/fragment_buffer/fragment/text.rs8
-rw-r--r--svgbob/src/buffer/fragment_buffer/fragment_tree.rs62
-rw-r--r--svgbob/src/buffer/property_buffer.rs53
-rw-r--r--svgbob/src/buffer/property_buffer/property.rs46
-rw-r--r--svgbob/src/buffer/string_buffer.rs1
-rw-r--r--svgbob/src/lib.rs4
-rw-r--r--svgbob/src/map/ascii_map.rs4
-rw-r--r--svgbob/src/map/circle_map.rs16
-rw-r--r--svgbob/src/map/unicode_map.rs4
-rw-r--r--svgbob/src/util.rs72
25 files changed, 537 insertions, 217 deletions
diff --git a/svgbob/rustfmt.toml b/svgbob/rustfmt.toml
new file mode 100644
index 0000000..503ede8
--- /dev/null
+++ b/svgbob/rustfmt.toml
@@ -0,0 +1,19 @@
+# Use unstable features
+unstable_features = true
+
+max_width = 80
+
+## Visually align, useful in writing the view
+indent_style = "Block"
+imports_indent = "Block"
+reorder_imports = true
+reorder_impl_items = true
+merge_imports = true
+## I want to be able to delete unused imports easily
+imports_layout = "Vertical"
+## Default value is false, yet clipy keeps nagging on this
+use_field_init_shorthand = true
+
+## also format macro
+format_macro_matchers = true
+force_multiline_blocks = true
diff --git a/svgbob/src/buffer/cell_buffer.rs b/svgbob/src/buffer/cell_buffer.rs
index f61e9f5..d47542c 100644
--- a/svgbob/src/buffer/cell_buffer.rs
+++ b/svgbob/src/buffer/cell_buffer.rs
@@ -75,14 +75,15 @@ impl CellBuffer {
pub fn group_adjacents(&self) -> Vec<Span> {
let mut adjacents: Vec<Span> = vec![];
for (cell, ch) in self.iter() {
- let belongs_to_adjacents = adjacents.iter_mut().rev().any(|contacts| {
- if contacts.is_adjacent(cell) {
- contacts.push((*cell, *ch));
- true
- } else {
- false
- }
- });
+ let belongs_to_adjacents =
+ adjacents.iter_mut().rev().any(|contacts| {
+ if contacts.is_adjacent(cell) {
+ contacts.push((*cell, *ch));
+ true
+ } else {
+ false
+ }
+ });
if !belongs_to_adjacents {
adjacents.push(Span::new(*cell, *ch));
}
@@ -122,8 +123,10 @@ impl CellBuffer {
}
fn bounds(&self) -> Option<(Cell, Cell)> {
- let xlimits = self.iter().map(|(cell, _)| cell.x).minmax().into_option();
- let ylimits = self.iter().map(|(cell, _)| cell.y).minmax().into_option();
+ let xlimits =
+ self.iter().map(|(cell, _)| cell.x).minmax().into_option();
+ let ylimits =
+ self.iter().map(|(cell, _)| cell.y).minmax().into_option();
match (xlimits, ylimits) {
(Some((min_x, max_x)), Some((min_y, max_y))) => {
Some((Cell::new(min_x, min_y), Cell::new(max_x, max_y)))
@@ -141,20 +144,27 @@ impl CellBuffer {
/// calculate the appropriate size (w,h) in pixels for the whole cell buffer to fit
/// appropriately
pub(crate) fn get_size(&self, settings: &Settings) -> (f32, f32) {
- let (_top_left, bottom_right) = self.bounds().unwrap_or((Cell::new(0, 0), Cell::new(0, 0)));
+ let (_top_left, bottom_right) =
+ self.bounds().unwrap_or((Cell::new(0, 0), Cell::new(0, 0)));
let w = settings.scale * (bottom_right.x + 2) as f32 * Cell::width();
let h = settings.scale * (bottom_right.y + 2) as f32 * Cell::height();
(w, h)
}
/// get all nodes of this cell buffer
- pub fn get_node_with_size<MSG>(&self, settings: &Settings) -> (Node<MSG>, f32, f32) {
+ pub fn get_node_with_size<MSG>(
+ &self,
+ settings: &Settings,
+ ) -> (Node<MSG>, f32, f32) {
let (w, h) = self.get_size(&settings);
// vec_fragments are the fragment result of successful endorsement
//
// vec_groups are not endorsed, but are still touching, these will be grouped together in
// the svg node
- let (vec_fragments, vec_contacts): (Vec<Vec<Fragment>>, Vec<Vec<Contacts>>) = self
+ let (vec_fragments, vec_contacts): (
+ Vec<Vec<Fragment>>,
+ Vec<Vec<Contacts>>,
+ ) = self
.group_adjacents()
.into_iter()
.map(|span| span.endorse(settings))
@@ -162,10 +172,11 @@ impl CellBuffer {
// partition the vec_groups into groups that is alone and the group
// that is contacting their parts
- let (single_member, vec_groups): (Vec<Contacts>, Vec<Contacts>) = vec_contacts
- .into_iter()
- .flatten()
- .partition(move |contacts| contacts.0.len() == 1);
+ let (single_member, vec_groups): (Vec<Contacts>, Vec<Contacts>) =
+ vec_contacts
+ .into_iter()
+ .flatten()
+ .partition(move |contacts| contacts.0.len() == 1);
let single_member_fragments: Vec<Fragment> = single_member
.into_iter()
@@ -188,11 +199,18 @@ impl CellBuffer {
})
.collect();
- let mut fragments: Vec<Fragment> = vec_fragments.into_iter().flatten().collect();
+ let mut fragments: Vec<Fragment> =
+ vec_fragments.into_iter().flatten().collect();
fragments.extend(single_member_fragments);
fragments.extend(self.escaped_text_nodes());
- let svg_node = Self::fragments_to_node(fragments, self.legend_css(), settings, w, h)
- .add_children(group_nodes);
+ let svg_node = Self::fragments_to_node(
+ fragments,
+ self.legend_css(),
+ settings,
+ w,
+ h,
+ )
+ .add_children(group_nodes);
(svg_node, w, h)
}
@@ -314,7 +332,8 @@ impl CellBuffer {
.into_iter()
.map(|frag| frag.scale(settings.scale))
.collect();
- let fragment_nodes: Vec<Node<MSG>> = FragmentTree::fragments_to_node(fragments_scaled);
+ let fragment_nodes: Vec<Node<MSG>> =
+ FragmentTree::fragments_to_node(fragments_scaled);
let mut children = vec![];
if settings.include_styles {
@@ -488,7 +507,8 @@ impl From<&str> for CellBuffer {
None
};
if let Some((loc, css_styles)) = css_styles {
- let mut cell_buffer = CellBuffer::from(StringBuffer::from(&input[..loc]));
+ let mut cell_buffer =
+ CellBuffer::from(StringBuffer::from(&input[..loc]));
cell_buffer.add_css_styles(css_styles);
cell_buffer
} else {
diff --git a/svgbob/src/buffer/cell_buffer/cell.rs b/svgbob/src/buffer/cell_buffer/cell.rs
index 9aca82d..3768c35 100644
--- a/svgbob/src/buffer/cell_buffer/cell.rs
+++ b/svgbob/src/buffer/cell_buffer/cell.rs
@@ -57,7 +57,8 @@ macro_rules! cell_grid {
impl Cell {
cell_grid!(
- a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y
+ a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x,
+ y
);
pub fn new(x: i32, y: i32) -> Self {
@@ -87,8 +88,10 @@ impl Cell {
.iter()
.map(|point| Self::snap_point(*point))
.collect();
- let (cells, _snap_points): (Vec<Self>, Vec<Point>) = snaps.into_iter().unzip();
- let min_cell: Self = cells.into_iter().min().expect("should have a min cell");
+ let (cells, _snap_points): (Vec<Self>, Vec<Point>) =
+ snaps.into_iter().unzip();
+ let min_cell: Self =
+ cells.into_iter().min().expect("should have a min cell");
min_cell
}
@@ -207,13 +210,21 @@ impl Cell {
util::clip_line(&aabb, start, end)
}
- pub fn clip_line_snap(&self, start: Point, end: Point) -> Option<(Point, Point)> {
+ pub fn clip_line_snap(
+ &self,
+ start: Point,
+ end: Point,
+ ) -> Option<(Point, Point)> {
self.clip_line(start, end)
.map(|(s, e)| (Self::snap(s), Self::snap(e)))
}
/// clip line then localize the points and snap to the nearest cell grid intersection
- pub fn clip_line_localize(&self, start: Point, end: Point) -> Option<(Point, Point)> {
+ pub fn clip_line_localize(
+ &self,
+ start: Point,
+ end: Point,
+ ) -> Option<(Point, Point)> {
self.clip_line_snap(start, end)
.map(|(s, e)| (self.localize_point(s), self.localize_point(e)))
}
@@ -408,49 +419,60 @@ mod tests {
#[test]
fn test_clip_line() {
assert_eq!(
- Cell::new(0, 0).clip_line_snap(Point::new(-0.01, -0.01), Point::new(1.01, 2.01)),
+ Cell::new(0, 0).clip_line_snap(
+ Point::new(-0.01, -0.01),
+ Point::new(1.01, 2.01)
+ ),
Some((CellGrid::a(), CellGrid::y()))
);
assert_eq!(
- Cell::new(0, 0).clip_line_snap(Point::new(0.0, 0.0), Point::new(1.00, 0.0)),
+ Cell::new(0, 0)
+ .clip_line_snap(Point::new(0.0, 0.0), Point::new(1.00, 0.0)),
Some((CellGrid::a(), CellGrid::e()))
);
- let clipped = Cell::new(0, 0).clip_line_snap(Point::new(0.0, 1.0), Point::new(1.0, 1.0));
+ let clipped = Cell::new(0, 0)
+ .clip_line_snap(Point::new(0.0, 1.0), Point::new(1.0, 1.0));
assert_eq!(clipped, Some((CellGrid::k(), CellGrid::o())));
- let clipped =
- Cell::new(0, 0).clip_line_snap(Point::new(-0.01, 1.01), Point::new(1.01, 0.95));
+ let clipped = Cell::new(0, 0)
+ .clip_line_snap(Point::new(-0.01, 1.01), Point::new(1.01, 0.95));
assert_eq!(clipped, Some((CellGrid::k(), CellGrid::o())));
assert_eq!(
- Cell::new(0, 0).clip_line_snap(Point::new(0.0, 2.0), Point::new(1.0, 2.0)),
+ Cell::new(0, 0)
+ .clip_line_snap(Point::new(0.0, 2.0), Point::new(1.0, 2.0)),
Some((CellGrid::u(), CellGrid::y()))
);
assert_eq!(
- Cell::new(0, 0).clip_line_snap(Point::new(0.0, 0.0), Point::new(0.0, 2.0)),
+ Cell::new(0, 0)
+ .clip_line_snap(Point::new(0.0, 0.0), Point::new(0.0, 2.0)),
Some((CellGrid::a(), CellGrid::u()))
);
assert_eq!(
- Cell::new(0, 0).clip_line_snap(Point::new(1.0, 0.0), Point::new(1.0, 2.0)),
+ Cell::new(0, 0)
+ .clip_line_snap(Point::new(1.0, 0.0), Point::new(1.0, 2.0)),
Some((CellGrid::e(), CellGrid::y()))
);
assert_eq!(
- Cell::new(0, 0).clip_line_snap(Point::new(1.0, 0.0), Point::new(0.0, 0.0)),
+ Cell::new(0, 0)
+ .clip_line_snap(Point::new(1.0, 0.0), Point::new(0.0, 0.0)),
Some((CellGrid::e(), CellGrid::a()))
);
assert_eq!(
- Cell::new(0, 0).clip_line_snap(Point::new(0.5, 1.0), Point::new(1.0, 1.0)),
+ Cell::new(0, 0)
+ .clip_line_snap(Point::new(0.5, 1.0), Point::new(1.0, 1.0)),
Some((CellGrid::m(), CellGrid::o()))
);
assert_eq!(
- Cell::new(0, 0).clip_line_snap(Point::new(0.25, 1.0), Point::new(1.0, 1.0)),
+ Cell::new(0, 0)
+ .clip_line_snap(Point::new(0.25, 1.0), Point::new(1.0, 1.0)),
Some((CellGrid::l(), CellGrid::o()))
);
}
diff --git a/svgbob/src/buffer/cell_buffer/contacts.rs b/svgbob/src/buffer/cell_buffer/contacts.rs
index dc832d4..9fe2c11 100644
--- a/svgbob/src/buffer/cell_buffer/contacts.rs
+++ b/svgbob/src/buffer/cell_buffer/contacts.rs
@@ -1,8 +1,6 @@
use super::endorse;
use crate::buffer::{fragment::Fragment, Cell};
-use std::{
- fmt,
-};
+use std::fmt;
/// Contains a group of fragments that are touching each other
/// The purpose of Contacts is to group fragments together
@@ -50,7 +48,9 @@ impl Contacts {
pub(crate) fn endorse_rects(&self) -> Option<Fragment> {
if let Some(rect) = endorse::endorse_rect(self.as_ref()) {
Some(rect.into())
- } else if let Some(rounded_rect) = endorse::endorse_rounded_rect(self.as_ref()) {
+ } else if let Some(rounded_rect) =
+ endorse::endorse_rounded_rect(self.as_ref())
+ {
Some(rounded_rect.into())
} else {
None
diff --git a/svgbob/src/buffer/cell_buffer/endorse.rs b/svgbob/src/buffer/cell_buffer/endorse.rs
index 8f53fa3..5d6e42f 100644
--- a/svgbob/src/buffer/cell_buffer/endorse.rs
+++ b/svgbob/src/buffer/cell_buffer/endorse.rs
@@ -7,7 +7,8 @@ use crate::{
/// rectangle
pub fn endorse_rect(fragments: &Vec<Fragment>) -> Option<Rect> {
if is_rect(fragments) {
- let is_any_broken = fragments.iter().any(|fragment| fragment.is_broken());
+ let is_any_broken =
+ fragments.iter().any(|fragment| fragment.is_broken());
let all_points = fragments.iter().fold(vec![], |mut acc, frag| {
let (p1, p2) = frag.bounds();
acc.push(p1);
@@ -55,7 +56,8 @@ fn is_rect(fragments: &Vec<Fragment>) -> bool {
/// - each of the right angle touches 2 lines that are aabb_perpendicular
pub fn endorse_rounded_rect(fragments: &Vec<Fragment>) -> Option<Rect> {
if let (true, arc_radius) = is_rounded_rect(fragments) {
- let is_any_broken = fragments.iter().any(|fragment| fragment.is_broken());
+ let is_any_broken =
+ fragments.iter().any(|fragment| fragment.is_broken());
let all_points = fragments.iter().fold(vec![], |mut acc, frag| {
let (p1, p2) = frag.bounds();
acc.push(p1);
@@ -90,15 +92,16 @@ fn is_rounded_rect(fragments: &Vec<Fragment>) -> (bool, Option<f32>) {
if parallels.len() == 2 && right_arcs.len() == 4 {
let first_right_arc_index = right_arcs[0];
let arc_fragment = &fragments[first_right_arc_index];
- let arc_radius = arc_fragment.as_arc().expect("expecting an arc").radius;
+ let arc_radius =
+ arc_fragment.as_arc().expect("expecting an arc").radius;
let (a1, a2) = parallels[0];
let (b1, b2) = parallels[1];
let line_a1 = fragments[a1].as_line().expect("expecting a line");
let line_b1 = fragments[b1].as_line().expect("expecting a line");
let line_a2 = fragments[a2].as_line().expect("expecting a line");
let line_b2 = fragments[b2].as_line().expect("expecting a line");
- let passed =
- line_a1.is_aabb_perpendicular(line_b1) && line_a2.is_aabb_perpendicular(line_b2);
+ let passed = line_a1.is_aabb_perpendicular(line_b1)
+ && line_a2.is_aabb_perpendicular(line_b2);
(passed, Some(arc_radius))
} else {
(false, None)
@@ -134,7 +137,10 @@ fn parallel_aabb_group(fragments: &Vec<Fragment>) -> Vec<(usize, usize)> {
for (index2, frag2) in fragments.iter().enumerate() {
if index1 != index2
&& !parallels.iter().any(|(pair1, pair2)| {
- index1 == *pair1 || index1 == *pair2 || index2 == *pair1 || index2 == *pair2
+ index1 == *pair1
+ || index1 == *pair2
+ || index2 == *pair1
+ || index2 == *pair2
})
&& frag1.is_aabb_parallel(&frag2)
{
diff --git a/svgbob/src/buffer/cell_buffer/span.rs b/svgbob/src/buffer/cell_buffer/span.rs
index a7da383..c2a1491 100644
--- a/svgbob/src/buffer/cell_buffer/span.rs
+++ b/svgbob/src/buffer/cell_buffer/span.rs
@@ -1,5 +1,8 @@
use crate::{
- buffer::{cell_buffer::Contacts, FragmentBuffer, Property, PropertyBuffer, StringBuffer},
+ buffer::{
+ cell_buffer::Contacts, FragmentBuffer, Property, PropertyBuffer,
+ StringBuffer,
+ },
fragment,
map::{circle_map, UNICODE_FRAGMENTS},
Cell, Fragment, Settings,
@@ -57,8 +60,11 @@ impl Span {
/// returns the top_left most cell which aligns the top most and the left most cell.
pub(crate) fn bounds(&self) -> Option<(Cell, Cell)> {
- if let Some((min_y, max_y)) = self.iter().map(|(cell, _)| cell.y).minmax().into_option() {
- if let Some((min_x, max_x)) = self.iter().map(|(cell, _)| cell.x).minmax().into_option()
+ if let Some((min_y, max_y)) =
+ self.iter().map(|(cell, _)| cell.y).minmax().into_option()
+ {
+ if let Some((min_x, max_x)) =
+ self.iter().map(|(cell, _)| cell.x).minmax().into_option()
{
Some((Cell::new(min_x, min_y), Cell::new(max_x, max_y)))
} else {
@@ -94,7 +100,8 @@ impl Span {
///
pub(crate) fn get_contacts(self, settings: &Settings) -> Vec<Contacts> {
let localize_self = self.localize();
- let fb: FragmentBuffer = (&localize_self).into_fragment_buffer(settings);
+ let fb: FragmentBuffer =
+ (&localize_self).into_fragment_buffer(settings);
let mut groups: Vec<Contacts> = vec![];
let merged_fragments = fb.merge_fragments();
@@ -169,7 +176,9 @@ impl Span {
/// This function is calling on endorse algorithmn on fragments that
/// are neighbors, but not necessarily touching to be promoted to a shape.
/// These includes: circle, arc, and line with arrow heads.
- fn endorse_circles_and_arcs(groups: Vec<Contacts>) -> (Vec<Fragment>, Vec<Contacts>) {
+ fn endorse_circles_and_arcs(
+ groups: Vec<Contacts>,
+ ) -> (Vec<Fragment>, Vec<Contacts>) {
let mut fragments = vec![];
let mut un_endorsed_circles: Vec<Contacts> = vec![];
if let Some((circle, unmatched)) = circle_map::endorse_circle(&groups) {
@@ -195,13 +204,17 @@ impl Span {
/// The second element of the tuple: `contacts` are fragments that are touching together
/// but can not form a fragment shape. These will be grouped in the svg nodes
/// to keep them go together, when dragged (editing)
- pub(crate) fn endorse(self, settings: &Settings) -> (Vec<Fragment>, Vec<Contacts>) {
+ pub(crate) fn endorse(
+ self,
+ settings: &Settings,
+ ) -> (Vec<Fragment>, Vec<Contacts>) {
let (top_left, _) = self.bounds().expect("mut have bounds");
let groups: Vec<Contacts> = self.get_contacts(settings);
// 1st phase, successful_endorses fragments, unendorsed one)
let (mut fragments, un_endorsed) = Self::endorse_rects(groups);
// 2nd phase, try to endorse to circles and arcs from the rejects of the 1st phase
- let (circle_fragments, un_endorsed) = Self::endorse_circles_and_arcs(un_endorsed);
+ let (circle_fragments, un_endorsed) =
+ Self::endorse_circles_and_arcs(un_endorsed);
fragments.extend(circle_fragments);
(
diff --git a/svgbob/src/buffer/fragment_buffer.rs b/svgbob/src/buffer/fragment_buffer.rs
index 90e833d..5f5c6cd 100644
--- a/svgbob/src/buffer/fragment_buffer.rs
+++ b/svgbob/src/buffer/fragment_buffer.rs
@@ -87,8 +87,10 @@ impl FragmentBuffer {
}
fn bounds(&self) -> Option<(Cell, Cell)> {
- let xlimits = self.iter().map(|(cell, _)| cell.x).minmax().into_option();
- let ylimits = self.iter().map(|(cell, _)| cell.y).minmax().into_option();
+ let xlimits =
+ self.iter().map(|(cell, _)| cell.x).minmax().into_option();
+ let ylimits =
+ self.iter().map(|(cell, _)| cell.y).minmax().into_option();
match (xlimits, ylimits) {
(Some((min_x, max_x)), Some((min_y, max_y))) => {
Some((Cell::new(min_x, min_y), Cell::new(max_x, max_y)))
@@ -98,14 +100,19 @@ impl FragmentBuffer {
}
pub(crate) fn get_size(&self, settings: &Settings) -> (f32, f32) {
- let (_top_left, bottom_right) = self.bounds().unwrap_or((Cell::new(0, 0), Cell::new(0, 0)));
+ let (_top_left, bottom_right) =
+ self.bounds().unwrap_or((Cell::new(0, 0), Cell::new(0, 0)));
let w = settings.scale * (bottom_right.x + 2) as f32 * Cell::width();
let h = settings.scale * (bottom_right.y + 2) as f32 * Cell::height();
(w, h)
}
/// add multiple fragments to cell
- pub fn add_fragments_to_cell(&mut self, cell: Cell, fragments: Vec<Fragment>) {
+ pub fn add_fragments_to_cell(
+ &mut self,
+ cell: Cell,
+ fragments: Vec<Fragment>,
+ ) {
if let Some(existing) = self.get_mut(&cell) {
existing.extend(fragments);
} else {
diff --git a/svgbob/src/buffer/fragment_buffer/direction.rs b/svgbob/src/buffer/fragment_buffer/direction.rs
index 2a2255c..4325db1 100644
--- a/svgbob/src/buffer/fragment_buffer/direction.rs
+++ b/svgbob/src/buffer/fragment_buffer/direction.rs
@@ -1,6 +1,5 @@
use crate::buffer::CellGrid;
-
#[derive(Debug, Clone, PartialEq, Copy)]
pub enum Direction {
TopLeft,
diff --git a/svgbob/src/buffer/fragment_buffer/fragment.rs b/svgbob/src/buffer/fragment_buffer/fragment.rs
index 113b1d2..6be2c77 100644
--- a/svgbob/src/buffer/fragment_buffer/fragment.rs
+++ b/svgbob/src/buffer/fragment_buffer/fragment.rs
@@ -6,7 +6,7 @@ pub use line::Line;
pub use marker_line::{Marker, MarkerLine};
pub use polygon::{Polygon, PolygonTag};
pub use rect::Rect;
-use sauron::{Node};
+use sauron::Node;
use std::{cmp::Ordering, fmt};
pub use text::{CellText, Text};
@@ -111,10 +111,14 @@ impl Fragment {
}
// line and polygon
- (Fragment::Line(line), Fragment::Polygon(polygon)) => line.merge_line_polygon(polygon),
+ (Fragment::Line(line), Fragment::Polygon(polygon)) => {
+ line.merge_line_polygon(polygon)
+ }
// polygon and line
- (Fragment::Polygon(polygon), Fragment::Line(line)) => line.merge_line_polygon(polygon),
+ (Fragment::Polygon(polygon), Fragment::Line(line)) => {
+ line.merge_line_polygon(polygon)
+ }
/*
// line and marker_line
@@ -130,10 +134,14 @@ impl Fragment {
}
*/
// line and circle
- (Fragment::Line(line), Fragment::Circle(circle)) => line.merge_circle(circle),
+ (Fragment::Line(line), Fragment::Circle(circle)) => {
+ line.merge_circle(circle)
+ }
// circle and line
- (Fragment::Circle(circle), Fragment::Line(line)) => line.merge_circle(circle),
+ (Fragment::Circle(circle), Fragment::Line(line)) => {
+ line.merge_circle(circle)
+ }
// cell_text and cell_text
(Fragment::CellText(ctext), Fragment::CellText(other_ctext)) => {
if let Some(merged_ctext) = ctext.merge(other_ctext) {
@@ -150,7 +158,10 @@ impl Fragment {
pub(crate) fn can_fit(&self, other: &Self) -> bool {
let (tl, br) = self.bounds();
let (other_tl, other_br) = other.bounds();
- tl.x <= other_tl.x && tl.y <= other_tl.y && br.x >= other_br.x && br.y >= other_br.y
+ tl.x <= other_tl.x
+ && tl.y <= other_tl.y
+ && br.x >= other_br.x
+ && br.y >= other_br.y
}
/// merge fragments recursively until it hasn't changed the number of fragments
@@ -187,7 +198,9 @@ impl Fragment {
/// are lines axis align and parallel
pub(in crate) fn is_aabb_parallel(&self, other: &Self) -> bool {
match (self, other) {
- (Fragment::Line(line), Fragment::Line(other)) => line.is_aabb_parallel(other),
+ (Fragment::Line(line), Fragment::Line(other)) => {
+ line.is_aabb_parallel(other)
+ }
(_, _) => false,
}
}
@@ -195,7 +208,9 @@ impl Fragment {
#[allow(unused)]
pub(in crate) fn is_aabb_perpendicular(&self, other: &Self) -> bool {
match (self, other) {
- (Fragment::Line(line), Fragment::Line(other)) => line.is_aabb_perpendicular(other),
+ (Fragment::Line(line), Fragment::Line(other)) => {
+ line.is_aabb_perpendicular(other)
+ }
(_, _) => false,
}
}
@@ -207,12 +222,16 @@ impl Fragment {
Fragment::Line(line) => match other {
Fragment::Line(other) => line.is_touching(other),
Fragment::Arc(other_arc) => line.is_touching_arc(other_arc),
- Fragment::Polygon(polygon) => line.merge_line_polygon(polygon).is_some(),
+ Fragment::Polygon(polygon) => {
+ line.merge_line_polygon(polygon).is_some()
+ }
Fragment::Circle(circle) => line.is_touching_circle(circle),
_ => false,
},
Fragment::Polygon(polygon) => match other {
- Fragment::Line(other) => other.merge_line_polygon(polygon).is_some(),
+ Fragment::Line(other) => {
+ other.merge_line_polygon(polygon).is_some()
+ }
_ => false,
},
Fragment::Arc(arc) => match other {
@@ -225,7 +244,9 @@ impl Fragment {
_ => false,
},
Fragment::CellText(ctext) => match other {
- Fragment::CellText(other_ctext) => ctext.is_contacting(other_ctext),
+ Fragment::CellText(other_ctext) => {
+ ctext.is_contacting(other_ctext)
+ }
_ => false,
},
_ => false,
@@ -236,16 +257,28 @@ impl Fragment {
/// offset by the cell location
pub fn absolute_position(&self, cell: Cell) -> Self {
match self {
- Fragment::Line(line) => Fragment::Line(line.absolute_position(cell)),
+ Fragment::Line(line) => {
+ Fragment::Line(line.absolute_position(cell))
+ }
Fragment::MarkerLine(marker_line) => {
Fragment::MarkerLine(marker_line.absolute_position(cell))
}
- Fragment::Circle(circle) => Fragment::Circle(circle.absolute_position(cell)),
+ Fragment::Circle(circle) => {
+ Fragment::Circle(circle.absolute_position(cell))
+ }
Fragment::Arc(arc) => Fragment::Arc(arc.absolute_position(cell)),
- Fragment::Polygon(polygon) => Fragment::Polygon(polygon.absolute_position(cell)),
- Fragment::Rect(rect) => Fragment::Rect(rect.absolute_position(cell)),
- Fragment::Text(text) => Fragment::Text(text.absolute_position(cell)),
- Fragment::CellText(ctext) => Fragment::CellText(ctext.absolute_position(cell)),
+ Fragment::Polygon(polygon) => {
+ Fragment::Polygon(polygon.absolute_position(cell))
+ }
+ Fragment::Rect(rect) => {
+ Fragment::Rect(rect.absolute_position(cell))
+ }
+ Fragment::Text(text) => {
+ Fragment::Text(text.absolute_position(cell))
+ }
+ Fragment::CellText(ctext) => {
+ Fragment::CellText(ctext.absolute_position(cell))
+ }
}
}
@@ -253,10 +286,14 @@ impl Fragment {
pub fn scale(&self, scale: f32) -> Self {
match self {
Fragment::Line(line) => Fragment::Line(line.scale(scale)),
- Fragment::MarkerLine(marker_line) => Fragment::MarkerLine(marker_line.scale(scale)),
+ Fragment::MarkerLine(marker_line) => {
+ Fragment::MarkerLine(marker_line.scale(scale))
+ }
Fragment::Circle(circle) => Fragment::Circle(circle.scale(scale)),
Fragment::Arc(arc) => Fragment::Arc(arc.scale(scale)),
- Fragment::Polygon(polygon) => Fragment::Polygon(polygon.scale(scale)),
+ Fragment::Polygon(polygon) => {
+ Fragment::Polygon(polygon.scale(scale))
+ }
Fragment::Rect(rect) => Fragment::Rect(rect.scale(scale)),
Fragment::Text(text) => Fragment::Text(text.scale(scale)),
// the CellText is converted into text fragment first, then scaled
@@ -269,14 +306,18 @@ impl Fragment {
pub fn align(&self) -> Self {
match self {
Fragment::Line(line) => Fragment::Line(line.align()),
- Fragment::MarkerLine(marker_line) => Fragment::MarkerLine(marker_line.align()),
+ Fragment::MarkerLine(marker_line) => {
+ Fragment::MarkerLine(marker_line.align())
+ }
Fragment::Circle(circle) => Fragment::Circle(circle.clone()),
Fragment::Arc(arc) => Fragment::Arc(arc.clone()),
Fragment::Polygon(polygon) => Fragment::Polygon(polygon.clone()),
Fragment::Rect(rect) => Fragment::Rect(rect.clone()),
Fragment::Text(text) => Fragment::Text(text.clone()),
// the CellText is converted into text fragment first, then scaled
- Fragment::CellText(ctext) => Fragment::Text(Into::<Text>::into(ctext.clone())),
+ Fragment::CellText(ctext) => {
+ Fragment::Text(Into::<Text>::into(ctext.clone()))
+ }
}
}
@@ -334,13 +375,14 @@ impl Fragment {
/// if this is a cell text and is wrapped in braces then it is a css
/// tag for the container
pub fn as_css_tag(&self) -> Vec<String> {
- let input_text: Option<&str> = if let Some(cell_text) = self.as_cell_text() {
- Some(&cell_text.content)
- } else if let Some(text) = self.as_text() {
- Some(&text.text)
- } else {
- None
- };
+ let input_text: Option<&str> =
+ if let Some(cell_text) = self.as_cell_text() {
+ Some(&cell_text.content)
+ } else if let Some(text) = self.as_text() {
+ Some(&text.text)
+ } else {
+ None
+ };
if let Some(input_text) = input_text {
if let Ok(tags) = crate::util::parser::parse_css_tag(&input_text) {
@@ -454,7 +496,13 @@ pub fn marker_line(
start_marker: Option<Marker>,
end_marker: Option<Marker>,
) -> Fragment {
- Fragment::MarkerLine(MarkerLine::new(a, b, is_broken, start_marker, end_marker))
+ Fragment::MarkerLine(MarkerLine::new(
+ a,
+ b,
+ is_broken,
+ start_marker,
+ end_marker,
+ ))
}
pub fn broken_line(a: Point, b: Point) -> Fragment {
@@ -469,15 +517,29 @@ pub fn arc(a: Point, b: Point, r: f32) -> Fragment {
Fragment::Arc(Arc::new(a, b, r))
}