From d3ad6c0dafb0e95ccdda14894b2158cb056152f6 Mon Sep 17 00:00:00 2001 From: samu698 Date: Tue, 14 Nov 2023 19:41:08 +0100 Subject: [PATCH 1/3] Added layout::with_settings function, to construct layout with non default settings --- src/layout.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/layout.rs b/src/layout.rs index 5e2f5c9c..1bd7a7d8 100644 --- a/src/layout.rs +++ b/src/layout.rs @@ -304,9 +304,17 @@ pub struct Layout { impl<'a, U: Copy + Clone> Layout { /// Creates a layout instance. This requires the direction that the Y coordinate increases in. /// Layout needs to be aware of your coordinate system to place the glyphs correctly. + /// If you want to construct a layout with settings you can use the Layout::with_settings + /// function. pub fn new(coordinate_system: CoordinateSystem) -> Layout { let settings = LayoutSettings::default(); + Self::with_settings(coordinate_system, settings) + } + /// Creates a layout instance with settings. This requires the direction that the Y coordinate + /// increases in. + /// Layout needs to be aware of your coordinate system to place the glyphs correctly. + pub fn with_settings(coordinate_system: CoordinateSystem, settings: LayoutSettings) -> Layout { let mut layout = Layout { flip: coordinate_system == CoordinateSystem::PositiveYDown, x: 0.0, From 50190f316c250c6374012594e43dff994701858e Mon Sep 17 00:00:00 2001 From: samu698 Date: Tue, 14 Nov 2023 20:52:20 +0100 Subject: [PATCH 2/3] Added builder pattern for LayoutConfig This allows for easier configuration of layout --- src/layout.rs | 81 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) diff --git a/src/layout.rs b/src/layout.rs index 1bd7a7d8..ee38950f 100644 --- a/src/layout.rs +++ b/src/layout.rs @@ -86,6 +86,13 @@ pub struct LayoutSettings { pub wrap_hard_breaks: bool, } +impl LayoutSettings { + /// Instatiates a builder for the LayoutSettings allowing for easier configuration. + pub fn builder() -> LayoutSettingsBuilder { + LayoutSettingsBuilder::new() + } +} + impl Default for LayoutSettings { fn default() -> LayoutSettings { LayoutSettings { @@ -102,6 +109,80 @@ impl Default for LayoutSettings { } } +/// Builder helper for the construction of LayoutSettings. +/// When instatianting the builder a default LayoutSettings is used, by calling the various +/// configuration methods you can change the values, the methods consume an instance of the builder +/// and return a new one with the setting modified allowing the chaining of methods. +pub struct LayoutSettingsBuilder { + /// The layout settings that are being configured + value: LayoutSettings +} + +impl LayoutSettingsBuilder { + /// Instatiates a new LayoutSettingsBuilder + pub fn new() -> Self { + Self { + value: LayoutSettings::default() + } + } + /// The leftmost boundary of the text region. + pub fn x(mut self, x: f32) -> Self { + self.value.x = x; + self + } + /// The topmost boundary of the text region. + pub fn y(mut self, y: f32) -> Self { + self.value.y = y; + self + } + /// An optional rightmost boundary on the text region. A line of text that exceeds the + /// max_width is wrapped to the line below. If the width of a glyph is larger than the + /// max_width, the glyph will overflow past the max_width. The application is responsible for + /// handling the overflow. + pub fn max_width(mut self, max_width: Option) -> Self { + self.value.max_width = max_width; + self + } + /// An optional bottom boundary on the text region. This is used for positioning the + /// vertical_align option. Text that exceeds the defined max_height will overflow past it. The + /// application is responsible for handling the overflow. + pub fn max_height(mut self, max_height: Option) -> Self { + self.value.max_height = max_height; + self + } + /// The default is Left. This option does nothing if the max_width isn't set. + pub fn horizontal_align(mut self, horizontal_align: HorizontalAlign) -> Self { + self.value.horizontal_align = horizontal_align; + self + } + /// The default is Top. This option does nothing if the max_height isn't set. + pub fn vertical_align(mut self, vertical_align: VerticalAlign) -> Self { + self.value.vertical_align = vertical_align; + self + } + /// The height of each line as a multiplier of the default. + pub fn line_height(mut self, line_height: f32) -> Self { + self.value.line_height = line_height; + self + } + /// The default is Word. Wrap style is a hint for how strings of text should be wrapped to the + /// next line. Line wrapping can happen when the max width/height is reached. + pub fn wrap_style(mut self, wrap_style: WrapStyle) -> Self { + self.value.wrap_style = wrap_style; + self + } + /// The default is true. This option enables hard breaks, like new line characters, to + /// prematurely wrap lines. If false, hard breaks will not prematurely create a new line. + pub fn wrap_hard_breaks(mut self, wrap_hard_breaks: bool) -> Self { + self.value.wrap_hard_breaks = wrap_hard_breaks; + self + } + /// Creates LayoutSettings instance from the parameters set in the builder. + pub fn build(self) -> LayoutSettings { + self.value + } +} + /// Configuration for rasterizing a glyph. This struct is also a hashable key that can be used to /// uniquely identify a rasterized glyph for applications that want to cache glyphs. #[derive(Debug, Copy, Clone)] From 31568abed52a1f4e68bcb9eb6456c2bfeeb7518d Mon Sep 17 00:00:00 2001 From: samu698 Date: Tue, 14 Nov 2023 21:03:16 +0100 Subject: [PATCH 3/3] Made all LayoutSettingsBuilder's methods const Because LayoutSettings::default() is not a const function I moved the implementation of it to a const function, allowing for usage in const and non-const contexts --- src/layout.rs | 39 +++++++++++++++++++++++---------------- 1 file changed, 23 insertions(+), 16 deletions(-) diff --git a/src/layout.rs b/src/layout.rs index ee38950f..0496f35a 100644 --- a/src/layout.rs +++ b/src/layout.rs @@ -88,13 +88,13 @@ pub struct LayoutSettings { impl LayoutSettings { /// Instatiates a builder for the LayoutSettings allowing for easier configuration. - pub fn builder() -> LayoutSettingsBuilder { + pub const fn builder() -> LayoutSettingsBuilder { LayoutSettingsBuilder::new() } -} -impl Default for LayoutSettings { - fn default() -> LayoutSettings { + /// Const version of the default function allowing, required for implementing the const + /// LayoutSettingsBuilder. + const fn const_default() -> Self { LayoutSettings { x: 0.0, y: 0.0, @@ -109,6 +109,12 @@ impl Default for LayoutSettings { } } +impl Default for LayoutSettings { + fn default() -> LayoutSettings { + Self::const_default() + } +} + /// Builder helper for the construction of LayoutSettings. /// When instatianting the builder a default LayoutSettings is used, by calling the various /// configuration methods you can change the values, the methods consume an instance of the builder @@ -120,18 +126,19 @@ pub struct LayoutSettingsBuilder { impl LayoutSettingsBuilder { /// Instatiates a new LayoutSettingsBuilder - pub fn new() -> Self { + pub const fn new() -> Self { + let settings = LayoutSettings::const_default(); Self { - value: LayoutSettings::default() + value: settings } } /// The leftmost boundary of the text region. - pub fn x(mut self, x: f32) -> Self { + pub const fn x(mut self, x: f32) -> Self { self.value.x = x; self } /// The topmost boundary of the text region. - pub fn y(mut self, y: f32) -> Self { + pub const fn y(mut self, y: f32) -> Self { self.value.y = y; self } @@ -139,46 +146,46 @@ impl LayoutSettingsBuilder { /// max_width is wrapped to the line below. If the width of a glyph is larger than the /// max_width, the glyph will overflow past the max_width. The application is responsible for /// handling the overflow. - pub fn max_width(mut self, max_width: Option) -> Self { + pub const fn max_width(mut self, max_width: Option) -> Self { self.value.max_width = max_width; self } /// An optional bottom boundary on the text region. This is used for positioning the /// vertical_align option. Text that exceeds the defined max_height will overflow past it. The /// application is responsible for handling the overflow. - pub fn max_height(mut self, max_height: Option) -> Self { + pub const fn max_height(mut self, max_height: Option) -> Self { self.value.max_height = max_height; self } /// The default is Left. This option does nothing if the max_width isn't set. - pub fn horizontal_align(mut self, horizontal_align: HorizontalAlign) -> Self { + pub const fn horizontal_align(mut self, horizontal_align: HorizontalAlign) -> Self { self.value.horizontal_align = horizontal_align; self } /// The default is Top. This option does nothing if the max_height isn't set. - pub fn vertical_align(mut self, vertical_align: VerticalAlign) -> Self { + pub const fn vertical_align(mut self, vertical_align: VerticalAlign) -> Self { self.value.vertical_align = vertical_align; self } /// The height of each line as a multiplier of the default. - pub fn line_height(mut self, line_height: f32) -> Self { + pub const fn line_height(mut self, line_height: f32) -> Self { self.value.line_height = line_height; self } /// The default is Word. Wrap style is a hint for how strings of text should be wrapped to the /// next line. Line wrapping can happen when the max width/height is reached. - pub fn wrap_style(mut self, wrap_style: WrapStyle) -> Self { + pub const fn wrap_style(mut self, wrap_style: WrapStyle) -> Self { self.value.wrap_style = wrap_style; self } /// The default is true. This option enables hard breaks, like new line characters, to /// prematurely wrap lines. If false, hard breaks will not prematurely create a new line. - pub fn wrap_hard_breaks(mut self, wrap_hard_breaks: bool) -> Self { + pub const fn wrap_hard_breaks(mut self, wrap_hard_breaks: bool) -> Self { self.value.wrap_hard_breaks = wrap_hard_breaks; self } /// Creates LayoutSettings instance from the parameters set in the builder. - pub fn build(self) -> LayoutSettings { + pub const fn build(self) -> LayoutSettings { self.value } }