taffy/compute/grid/types/
coordinates.rs1use super::super::types::TrackCounts;
3use crate::geometry::Line;
4use core::cmp::{max, Ordering};
5use core::ops::{Add, AddAssign, Sub};
6
7#[derive(Debug, Clone, Copy, PartialEq, Eq)]
16#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
17#[repr(transparent)]
18pub struct GridLine(i16);
19
20impl From<i16> for GridLine {
21 fn from(value: i16) -> Self {
22 Self(value)
23 }
24}
25
26impl GridLine {
27 pub fn as_i16(self) -> i16 {
29 self.0
30 }
31
32 pub(crate) fn into_origin_zero_line(self, explicit_track_count: u16) -> OriginZeroLine {
34 let explicit_line_count = explicit_track_count + 1;
35 let oz_line = match self.0.cmp(&0) {
36 Ordering::Greater => self.0 - 1,
37 Ordering::Less => self.0 + explicit_line_count as i16,
38 Ordering::Equal => panic!("Grid line of zero is invalid"),
39 };
40 OriginZeroLine(oz_line)
41 }
42}
43
44#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
51#[repr(transparent)]
52pub struct OriginZeroLine(pub i16);
53
54impl Add<OriginZeroLine> for OriginZeroLine {
56 type Output = Self;
57 fn add(self, rhs: OriginZeroLine) -> Self::Output {
58 OriginZeroLine(self.0 + rhs.0)
59 }
60}
61impl Sub<OriginZeroLine> for OriginZeroLine {
62 type Output = Self;
63 fn sub(self, rhs: OriginZeroLine) -> Self::Output {
64 OriginZeroLine(self.0 - rhs.0)
65 }
66}
67
68impl Add<u16> for OriginZeroLine {
70 type Output = Self;
71 fn add(self, rhs: u16) -> Self::Output {
72 OriginZeroLine(self.0 + rhs as i16)
73 }
74}
75impl AddAssign<u16> for OriginZeroLine {
76 fn add_assign(&mut self, rhs: u16) {
77 self.0 += rhs as i16;
78 }
79}
80impl Sub<u16> for OriginZeroLine {
81 type Output = Self;
82 fn sub(self, rhs: u16) -> Self::Output {
83 OriginZeroLine(self.0 - rhs as i16)
84 }
85}
86
87impl OriginZeroLine {
88 pub(crate) fn into_track_vec_index(self, track_counts: TrackCounts) -> usize {
90 self.try_into_track_vec_index(track_counts).unwrap_or_else(|| {
91 if self.0 > 0 {
92 panic!("OriginZero grid line cannot be more than the number of positive grid lines");
93 } else {
94 panic!("OriginZero grid line cannot be less than the number of negative grid lines");
95 }
96 })
97 }
98
99 pub(crate) fn try_into_track_vec_index(self, track_counts: TrackCounts) -> Option<usize> {
109 if self.0 < -(track_counts.negative_implicit as i16) {
111 return None;
112 };
113 if self.0 > (track_counts.explicit + track_counts.positive_implicit) as i16 {
115 return None;
116 };
117
118 Some(2 * ((self.0 + track_counts.negative_implicit as i16) as usize))
119 }
120
121 pub(crate) fn implied_negative_implicit_tracks(self) -> u16 {
123 if self.0 < 0 {
124 self.0.unsigned_abs()
125 } else {
126 0
127 }
128 }
129
130 pub(crate) fn implied_positive_implicit_tracks(self, explicit_track_count: u16) -> u16 {
132 if self.0 > explicit_track_count as i16 {
133 self.0 as u16 - explicit_track_count
134 } else {
135 0
136 }
137 }
138}
139
140impl Line<OriginZeroLine> {
141 pub(crate) fn span(self) -> u16 {
143 max(self.end.0 - self.start.0, 0) as u16
144 }
145}
146
147pub trait GridCoordinate: Copy {}
149impl GridCoordinate for GridLine {}
150impl GridCoordinate for OriginZeroLine {}