diff --git a/glyph-brush/CHANGELOG.md b/glyph-brush/CHANGELOG.md index 369dca3..68fa259 100644 --- a/glyph-brush/CHANGELOG.md +++ b/glyph-brush/CHANGELOG.md @@ -1,4 +1,7 @@ # Unreleased (v0.7.7) +* Allow `Text::new` to work with any `X` type. **This may break usage**, however it will hopefully be non-breaking in practice as the compiler should always be able to infer this. +* Add `Section::builder` for more flexible `X`/"extra" type usage than `Section::default`. +* Add more flexible `X` type usage to `GlyphBrush::keep_cached`. * Update `GlyphCruncher::glyphs`, `GlyphCruncher::glyph_bounds` docs. # v0.7.6 diff --git a/glyph-brush/src/glyph_brush.rs b/glyph-brush/src/glyph_brush.rs index 83be2fe..f694787 100644 --- a/glyph-brush/src/glyph_brush.rs +++ b/glyph-brush/src/glyph_brush.rs @@ -831,8 +831,7 @@ mod glyph_brush_test { let font_b = FontRef::try_from_slice(include_bytes!("../../fonts/Exo2-Light.otf")).unwrap(); let unqueued_glyph = font_a.glyph_id('c').with_scale(50.0); - let mut brush: GlyphBrush<_, (), _> = - GlyphBrushBuilder::using_fonts(vec![font_a, font_b]).build(); + let mut brush = GlyphBrushBuilder::using_fonts(vec![font_a, font_b]).build(); let section = Section::default() .add_text(Text::new("a ")) diff --git a/glyph-brush/src/glyph_calculator.rs b/glyph-brush/src/glyph_calculator.rs index 1d7256d..9167cc8 100644 --- a/glyph-brush/src/glyph_calculator.rs +++ b/glyph-brush/src/glyph_calculator.rs @@ -106,7 +106,7 @@ pub trait GlyphCruncher { /// use glyph_brush::{ab_glyph::FontArc, GlyphCalculatorBuilder, GlyphCruncher, Section, Text}; /// /// let dejavu = FontArc::try_from_slice(include_bytes!("../../fonts/DejaVuSans.ttf")).unwrap(); -/// let glyphs = GlyphCalculatorBuilder::using_font(dejavu).build::<()>(); +/// let glyphs = GlyphCalculatorBuilder::using_font(dejavu).build(); /// /// let section = Section::default().add_text(Text::new("Hello glyph_brush")); /// @@ -383,7 +383,7 @@ mod test { #[test] fn glyph_bounds() { - let glyphs = GlyphCalculatorBuilder::using_font(MONO_FONT.clone()).build::(); + let glyphs = GlyphCalculatorBuilder::using_font(MONO_FONT.clone()).build(); let mut glyphs = glyphs.cache_scope(); let scale = PxScale::from(16.0); @@ -411,7 +411,7 @@ mod test { #[test] fn glyph_bounds_respect_layout_bounds() { - let glyphs = GlyphCalculatorBuilder::using_font(MONO_FONT.clone()).build::(); + let glyphs = GlyphCalculatorBuilder::using_font(MONO_FONT.clone()).build(); let mut glyphs = glyphs.cache_scope(); let section = Section::default() @@ -481,7 +481,7 @@ mod test { /// Issue #87 #[test] fn glyph_bound_section_bound_consistency() { - let calc = GlyphCalculatorBuilder::using_font(OPEN_SANS_LIGHT.clone()).build::<()>(); + let calc = GlyphCalculatorBuilder::using_font(OPEN_SANS_LIGHT.clone()).build(); let mut calc = calc.cache_scope(); let section = @@ -506,7 +506,7 @@ mod test { /// Issue #87 #[test] fn glyph_bound_section_bound_consistency_trailing_space() { - let calc = GlyphCalculatorBuilder::using_font(OPEN_SANS_LIGHT.clone()).build::<()>(); + let calc = GlyphCalculatorBuilder::using_font(OPEN_SANS_LIGHT.clone()).build(); let mut calc = calc.cache_scope(); let section = @@ -532,7 +532,7 @@ mod test { /// error between the calculated glyph_bounds bounds & those used during layout. #[test] fn glyph_bound_section_bound_consistency_floating_point() { - let calc = GlyphCalculatorBuilder::using_font(MONO_FONT.clone()).build::<()>(); + let calc = GlyphCalculatorBuilder::using_font(MONO_FONT.clone()).build(); let mut calc = calc.cache_scope(); let section = Section::default().add_text(Text::new("Eins Zwei Drei Vier Funf")); diff --git a/glyph-brush/src/legacy.rs b/glyph-brush/src/legacy.rs index 5abdc08..7d7cf28 100644 --- a/glyph-brush/src/legacy.rs +++ b/glyph-brush/src/legacy.rs @@ -229,7 +229,7 @@ impl From<&VariedSection<'_>> for SectionGeometry { impl<'a> From<&VariedSection<'a>> for crate::Section<'a> { #[inline] fn from(s: &VariedSection<'a>) -> Self { - crate::Section::::default() + crate::Section::builder() .with_layout(s.layout) .with_bounds(s.bounds) .with_screen_position(s.screen_position) diff --git a/glyph-brush/src/section.rs b/glyph-brush/src/section.rs index 0e1263a..f4cbda6 100644 --- a/glyph-brush/src/section.rs +++ b/glyph-brush/src/section.rs @@ -1,7 +1,10 @@ +mod builder; + use super::{owned_section::*, *}; use ordered_float::OrderedFloat; use std::{borrow::Cow, f32, hash::*}; +pub use builder::SectionBuilder; pub type Color = [f32; 4]; /// An object that contains all the info to render a varied section of text. That is one including @@ -40,7 +43,8 @@ impl Section<'_, X> { } } -impl Default for Section<'static, X> { +impl Default for Section<'static, Extra> { + /// Note it's more flexible to use [`Section::builder`] instead. #[inline] fn default() -> Self { Section::new() @@ -50,12 +54,14 @@ impl Default for Section<'static, X> { impl<'a, X> Section<'a, X> { #[inline] pub fn new() -> Self { - Self { - screen_position: (0.0, 0.0), - bounds: (f32::INFINITY, f32::INFINITY), - layout: Layout::default(), - text: vec![], - } + Section::builder().with_text(vec![]) + } +} + +impl Section<'_, ()> { + #[inline] + pub fn builder() -> SectionBuilder { + <_>::default() } } diff --git a/glyph-brush/src/section/builder.rs b/glyph-brush/src/section/builder.rs new file mode 100644 index 0000000..be1dbfd --- /dev/null +++ b/glyph-brush/src/section/builder.rs @@ -0,0 +1,59 @@ +use crate::{Section, Text}; +use glyph_brush_layout::{BuiltInLineBreaker, Layout}; + +/// [`Section`] builder. +#[derive(Debug, Clone, Copy, PartialEq)] +pub struct SectionBuilder { + /// Position on screen to render text, in pixels from top-left. Defaults to (0, 0). + pub screen_position: (f32, f32), + /// Max (width, height) bounds, in pixels from top-left. Defaults to unbounded. + pub bounds: (f32, f32), + /// Built in layout, can be overridden with custom layout logic + /// see [`queue_custom_layout`](struct.GlyphBrush.html#method.queue_custom_layout) + pub layout: Layout, +} + +impl Default for SectionBuilder { + fn default() -> Self { + Self { + screen_position: (0.0, 0.0), + bounds: (f32::INFINITY, f32::INFINITY), + layout: Layout::default(), + } + } +} + +impl SectionBuilder { + #[inline] + pub fn with_screen_position>(mut self, position: P) -> Self { + self.screen_position = position.into(); + self + } + + #[inline] + pub fn with_bounds>(mut self, bounds: P) -> Self { + self.bounds = bounds.into(); + self + } + + #[inline] + pub fn with_layout>>(mut self, layout: L) -> Self { + self.layout = layout.into(); + self + } + + #[inline] + pub fn add_text(self, text: Text<'_, X>) -> Section<'_, X> { + self.with_text(vec![text]) + } + + #[inline] + pub fn with_text(self, text: Vec>) -> Section<'_, X> { + Section { + text, + screen_position: self.screen_position, + bounds: self.bounds, + layout: self.layout, + } + } +}