diff --git a/Cargo.lock b/Cargo.lock index 41ac047..9167b24 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1622,7 +1622,7 @@ checksum = "afbdc74edc00b6f6a218ca6a5364d6226a259d4b8ea1af4a0ea063f27e179f4d" [[package]] name = "protextinator" -version = "0.3.0" +version = "0.4.0" dependencies = [ "ahash", "cosmic-text", diff --git a/Cargo.toml b/Cargo.toml index 3b6d640..638c871 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "protextinator" -version = "0.3.0" +version = "0.4.0" edition = "2021" description = "Text management, made simple" keywords = ["text", "rendering", "gui", "graphics", "image"] diff --git a/src/math.rs b/src/math.rs index 6a320cf..416de65 100644 --- a/src/math.rs +++ b/src/math.rs @@ -33,7 +33,7 @@ impl Rect { /// assert_eq!(rect.height(), 50.0); /// ``` #[inline(always)] - pub fn new(min: Point, max: Point) -> Self { + pub const fn new(min: Point, max: Point) -> Self { Self { min, max } } @@ -47,7 +47,7 @@ impl Rect { /// assert_eq!(rect.height(), 50.0); /// ``` #[inline(always)] - pub fn height(&self) -> f32 { + pub const fn height(&self) -> f32 { self.max.y - self.min.y } @@ -61,7 +61,7 @@ impl Rect { /// assert_eq!(rect.width(), 100.0); /// ``` #[inline(always)] - pub fn width(&self) -> f32 { + pub const fn width(&self) -> f32 { self.max.x - self.min.x } @@ -75,7 +75,7 @@ impl Rect { /// assert_eq!(rect.size(), (100.0, 50.0)); /// ``` #[inline(always)] - pub fn size(&self) -> (f32, f32) { + pub const fn size(&self) -> (f32, f32) { (self.width(), self.height()) } } @@ -127,7 +127,7 @@ impl Point { /// assert_eq!(point.x, 10.0); /// assert_eq!(point.y, 20.0); /// ``` - pub fn new(x: f32, y: f32) -> Self { + pub const fn new(x: f32, y: f32) -> Self { Self { x, y } } @@ -141,7 +141,7 @@ impl Point { /// assert_eq!(point.to_tuple(), (10.0, 20.0)); /// ``` #[inline(always)] - pub fn to_tuple(self) -> (f32, f32) { + pub const fn to_tuple(self) -> (f32, f32) { (self.x, self.y) } @@ -164,7 +164,7 @@ impl Point { /// assert!(!p1.approx_eq(&p2, 0.0001)); /// ``` #[inline(always)] - pub fn approx_eq(&self, other: &Self, epsilon: f32) -> bool { + pub const fn approx_eq(&self, other: &Self, epsilon: f32) -> bool { (self.x - other.x).abs() <= epsilon && (self.y - other.y).abs() <= epsilon } } diff --git a/src/state.rs b/src/state.rs index 2711323..20d4885 100644 --- a/src/state.rs +++ b/src/state.rs @@ -73,7 +73,7 @@ impl Selection { /// assert!(selection.is_empty()); /// ``` #[inline(always)] - pub fn is_empty(&self) -> bool { + pub const fn is_empty(&self) -> bool { self.origin_character_byte_cursor.is_none() || self.ends_before_character_byte_cursor.is_none() } @@ -235,7 +235,7 @@ impl TextState { /// # let state = TextState::new_with_text("", &mut font_system, ()); /// let width = state.caret_width(); /// ``` - pub fn caret_width(&self) -> f32 { + pub const fn caret_width(&self) -> f32 { self.caret_width } @@ -454,7 +454,7 @@ impl TextState { /// let inner_size = state.inner_size(); /// println!("Text content size: {}x{}", inner_size.x, inner_size.y); /// ``` - pub fn inner_size(&self) -> Size { + pub const fn inner_size(&self) -> Size { self.inner_dimensions } diff --git a/src/style.rs b/src/style.rs index 6c3bef1..ab0ab36 100644 --- a/src/style.rs +++ b/src/style.rs @@ -89,7 +89,7 @@ impl FontSize { /// let font_size = FontSize::new(16.0); /// assert_eq!(font_size.value(), 16.0); /// ``` - pub fn new(size: f32) -> Self { + pub const fn new(size: f32) -> Self { Self(size) } @@ -102,7 +102,7 @@ impl FontSize { /// let font_size = FontSize::new(14.0); /// assert_eq!(font_size.value(), 14.0); /// ``` - pub fn value(&self) -> f32 { + pub const fn value(&self) -> f32 { self.0 } } @@ -180,7 +180,7 @@ impl FontColor { /// /// let color = FontColor::new(Color::rgb(255, 0, 0)); /// ``` - pub fn new(color: Color) -> Self { + pub const fn new(color: Color) -> Self { Self(color) } @@ -199,7 +199,7 @@ impl FontColor { /// let green = FontColor::rgb(0, 255, 0); /// let blue = FontColor::rgb(0, 0, 255); /// ``` - pub fn rgb(r: u8, g: u8, b: u8) -> Self { + pub const fn rgb(r: u8, g: u8, b: u8) -> Self { Self(Color::rgb(r, g, b)) } @@ -218,7 +218,7 @@ impl FontColor { /// let semi_transparent_red = FontColor::rgba(255, 0, 0, 128); /// let opaque_blue = FontColor::rgba(0, 0, 255, 255); /// ``` - pub fn rgba(r: u8, g: u8, b: u8, a: u8) -> Self { + pub const fn rgba(r: u8, g: u8, b: u8, a: u8) -> Self { Self(Color::rgba(r, g, b, a)) } } @@ -295,7 +295,7 @@ impl FontFamily { /// /// let sans_serif = FontFamily::sans_serif(); /// ``` - pub fn sans_serif() -> Self { + pub const fn sans_serif() -> Self { Self::SansSerif } @@ -307,7 +307,7 @@ impl FontFamily { /// /// let serif = FontFamily::serif(); /// ``` - pub fn serif() -> Self { + pub const fn serif() -> Self { Self::Serif } @@ -319,7 +319,7 @@ impl FontFamily { /// /// let monospace = FontFamily::monospace(); /// ``` - pub fn monospace() -> Self { + pub const fn monospace() -> Self { Self::Monospace } @@ -384,7 +384,7 @@ pub enum HorizontalTextAlignment { } impl HorizontalTextAlignment { - pub fn is_centered(&self) -> bool { + pub const fn is_centered(&self) -> bool { matches!(self, HorizontalTextAlignment::Center) } } @@ -476,10 +476,10 @@ impl TextStyle { /// /// let style = TextStyle::new(14.0, Color::rgb(0, 0, 0)); /// ``` - pub fn new(font_size: f32, font_color: Color) -> Self { + pub const fn new(font_size: f32, font_color: Color) -> Self { Self { - font_size: font_size.into(), - line_height: LineHeight::default(), + font_size: FontSize(font_size), + line_height: LineHeight::DEFAULT, font_color: FontColor(font_color), horizontal_alignment: HorizontalTextAlignment::Start, vertical_alignment: VerticalTextAlignment::Start, @@ -499,8 +499,8 @@ impl TextStyle { /// /// let style = TextStyle::default().with_font_size(18.0); /// ``` - pub fn with_font_size(mut self, font_size: f32) -> Self { - self.font_size = font_size.into(); + pub const fn with_font_size(mut self, font_size: f32) -> Self { + self.font_size = FontSize(font_size); self } @@ -515,8 +515,8 @@ impl TextStyle { /// /// let style = TextStyle::default().with_line_height(1.2); /// ``` - pub fn with_line_height(mut self, line_height: f32) -> Self { - self.line_height = line_height.into(); + pub const fn with_line_height(mut self, line_height: f32) -> Self { + self.line_height = LineHeight(line_height); self } @@ -531,8 +531,8 @@ impl TextStyle { /// /// let style = TextStyle::default().with_font_color(FontColor::rgb(255, 0, 0)); /// ``` - pub fn with_font_color(mut self, font_color: impl Into) -> Self { - self.font_color = font_color.into(); + pub const fn with_font_color(mut self, font_color: FontColor) -> Self { + self.font_color = font_color; self } @@ -547,7 +547,7 @@ impl TextStyle { /// /// let style = TextStyle::default().with_horizontal_alignment(HorizontalTextAlignment::Center); /// ``` - pub fn with_horizontal_alignment(mut self, alignment: HorizontalTextAlignment) -> Self { + pub const fn with_horizontal_alignment(mut self, alignment: HorizontalTextAlignment) -> Self { self.horizontal_alignment = alignment; self } @@ -563,7 +563,7 @@ impl TextStyle { /// /// let style = TextStyle::default().with_vertical_alignment(VerticalTextAlignment::Center); /// ``` - pub fn with_vertical_alignment(mut self, alignment: VerticalTextAlignment) -> Self { + pub const fn with_vertical_alignment(mut self, alignment: VerticalTextAlignment) -> Self { self.vertical_alignment = alignment; self } @@ -583,7 +583,7 @@ impl TextStyle { /// VerticalTextAlignment::Center /// ); /// ``` - pub fn with_alignment( + pub const fn with_alignment( mut self, horizontal: HorizontalTextAlignment, vertical: VerticalTextAlignment, @@ -604,7 +604,7 @@ impl TextStyle { /// /// let style = TextStyle::default().with_wrap(TextWrap::Wrap); /// ``` - pub fn with_wrap(mut self, wrap: TextWrap) -> Self { + pub const fn with_wrap(mut self, wrap: TextWrap) -> Self { self.wrap = Some(wrap); self } @@ -622,7 +622,7 @@ impl TextStyle { /// assert_eq!(style.line_height_pt(), 24.0); // 16.0 * 1.5 /// ``` #[inline(always)] - pub fn line_height_pt(&self) -> f32 { + pub const fn line_height_pt(&self) -> f32 { self.line_height.0 * self.font_size.0 } }