1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License in the LICENSE-APACHE file or at:
//     https://www.apache.org/licenses/LICENSE-2.0

//! KAS Rich-Text library — simple data types

use crate::conv::{to_u32, to_usize};

/// 2D vector (position/size/offset) over `f32`
#[derive(Clone, Copy, Debug, Default, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Vec2(pub f32, pub f32);

impl Vec2 {
    /// Zero
    pub const ZERO: Vec2 = Vec2(0.0, 0.0);

    /// Positive infinity
    pub const INFINITY: Vec2 = Vec2(f32::INFINITY, f32::INFINITY);

    /// Take the absolute value of each component
    #[inline]
    pub fn abs(self) -> Self {
        Vec2(self.0.abs(), self.1.abs())
    }

    /// Return the minimum, componentwise
    #[inline]
    pub fn min(self, other: Self) -> Self {
        Vec2(self.0.min(other.0), self.1.min(other.1))
    }

    /// Return the maximum, componentwise
    #[inline]
    pub fn max(self, other: Self) -> Self {
        Vec2(self.0.max(other.0), self.1.max(other.1))
    }
}

impl std::ops::Add for Vec2 {
    type Output = Self;

    #[inline]
    fn add(self, other: Self) -> Self {
        Vec2(self.0 + other.0, self.1 + other.1)
    }
}
impl std::ops::AddAssign for Vec2 {
    #[inline]
    fn add_assign(&mut self, rhs: Self) {
        self.0 += rhs.0;
        self.1 += rhs.1;
    }
}

impl std::ops::Sub for Vec2 {
    type Output = Self;

    #[inline]
    fn sub(self, other: Self) -> Self {
        Vec2(self.0 - other.0, self.1 - other.1)
    }
}
impl std::ops::SubAssign for Vec2 {
    #[inline]
    fn sub_assign(&mut self, rhs: Self) {
        self.0 -= rhs.0;
        self.1 -= rhs.1;
    }
}

impl From<Vec2> for (f32, f32) {
    fn from(size: Vec2) -> Self {
        (size.0, size.1)
    }
}

/// Range type
///
/// Essentially this is just a `std::ops::Range<u32>`, but with convenient
/// implementations.
///
/// Note that we consider `u32` large enough for any text we wish to display
/// and the library is too complex to be useful on 16-bit CPUs, so using `u32`
/// makes more sense than `usize`.
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Range {
    pub start: u32,
    pub end: u32,
}

impl Range {
    /// The start, as `usize`
    pub fn start(self) -> usize {
        to_usize(self.start)
    }

    /// The end, as `usize`
    pub fn end(self) -> usize {
        to_usize(self.end)
    }

    /// The number of iterable items, as `usize`
    pub fn len(self) -> usize {
        to_usize(self.end) - to_usize(self.start)
    }

    /// True if the given value is contained, inclusive of end points
    pub fn includes(self, value: usize) -> bool {
        to_usize(self.start) <= value && value <= to_usize(self.end)
    }

    /// Convert to a standard range
    pub fn to_std(self) -> std::ops::Range<usize> {
        to_usize(self.start)..to_usize(self.end)
    }

    /// Convert to `usize` and iterate
    pub fn iter(self) -> impl Iterator<Item = usize> {
        self.to_std()
    }
}

impl std::ops::Index<Range> for String {
    type Output = str;

    fn index(&self, range: Range) -> &str {
        let range = std::ops::Range::<usize>::from(range);
        &self[range]
    }
}

impl std::ops::Index<Range> for str {
    type Output = str;

    fn index(&self, range: Range) -> &str {
        let range = std::ops::Range::<usize>::from(range);
        &self[range]
    }
}

impl<T> std::ops::Index<Range> for [T] {
    type Output = [T];

    fn index(&self, range: Range) -> &[T] {
        let range = std::ops::Range::<usize>::from(range);
        &self[range]
    }
}

impl std::ops::IndexMut<Range> for String {
    fn index_mut(&mut self, range: Range) -> &mut str {
        let range = std::ops::Range::<usize>::from(range);
        &mut self[range]
    }
}

impl std::ops::IndexMut<Range> for str {
    fn index_mut(&mut self, range: Range) -> &mut str {
        let range = std::ops::Range::<usize>::from(range);
        &mut self[range]
    }
}

impl<T> std::ops::IndexMut<Range> for [T] {
    fn index_mut(&mut self, range: Range) -> &mut [T] {
        let range = std::ops::Range::<usize>::from(range);
        &mut self[range]
    }
}

impl From<Range> for std::ops::Range<usize> {
    fn from(range: Range) -> std::ops::Range<usize> {
        to_usize(range.start)..to_usize(range.end)
    }
}

impl From<std::ops::Range<u32>> for Range {
    fn from(range: std::ops::Range<u32>) -> Range {
        Range {
            start: range.start,
            end: range.end,
        }
    }
}

impl From<std::ops::Range<usize>> for Range {
    fn from(range: std::ops::Range<usize>) -> Range {
        Range {
            start: to_u32(range.start),
            end: to_u32(range.end),
        }
    }
}