Skip to content

Commit

Permalink
Make changes less breaking. Add Section::builder
Browse files Browse the repository at this point in the history
  • Loading branch information
alexheretic committed Feb 21, 2023
1 parent 5f4c8a9 commit 9910be9
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 16 deletions.
3 changes: 3 additions & 0 deletions glyph-brush/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
3 changes: 1 addition & 2 deletions glyph-brush/src/glyph_brush.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 "))
Expand Down
12 changes: 6 additions & 6 deletions glyph-brush/src/glyph_calculator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ pub trait GlyphCruncher<F: Font = FontArc, X: Clone = Extra> {
/// 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"));
///
Expand Down Expand Up @@ -383,7 +383,7 @@ mod test {

#[test]
fn glyph_bounds() {
let glyphs = GlyphCalculatorBuilder::using_font(MONO_FONT.clone()).build::<Extra>();
let glyphs = GlyphCalculatorBuilder::using_font(MONO_FONT.clone()).build();
let mut glyphs = glyphs.cache_scope();

let scale = PxScale::from(16.0);
Expand Down Expand Up @@ -411,7 +411,7 @@ mod test {

#[test]
fn glyph_bounds_respect_layout_bounds() {
let glyphs = GlyphCalculatorBuilder::using_font(MONO_FONT.clone()).build::<Extra>();
let glyphs = GlyphCalculatorBuilder::using_font(MONO_FONT.clone()).build();
let mut glyphs = glyphs.cache_scope();

let section = Section::default()
Expand Down Expand Up @@ -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 =
Expand All @@ -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 =
Expand All @@ -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"));
Expand Down
2 changes: 1 addition & 1 deletion glyph-brush/src/legacy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Extra>::default()
crate::Section::builder()
.with_layout(s.layout)
.with_bounds(s.bounds)
.with_screen_position(s.screen_position)
Expand Down
20 changes: 13 additions & 7 deletions glyph-brush/src/section.rs
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -40,7 +43,8 @@ impl<X: Clone> Section<'_, X> {
}
}

impl<X> 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()
Expand All @@ -50,12 +54,14 @@ impl<X> 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()
}
}

Expand Down
59 changes: 59 additions & 0 deletions glyph-brush/src/section/builder.rs
Original file line number Diff line number Diff line change
@@ -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<BuiltInLineBreaker>,
}

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<P: Into<(f32, f32)>>(mut self, position: P) -> Self {
self.screen_position = position.into();
self
}

#[inline]
pub fn with_bounds<P: Into<(f32, f32)>>(mut self, bounds: P) -> Self {
self.bounds = bounds.into();
self
}

#[inline]
pub fn with_layout<L: Into<Layout<BuiltInLineBreaker>>>(mut self, layout: L) -> Self {
self.layout = layout.into();
self
}

#[inline]
pub fn add_text<X>(self, text: Text<'_, X>) -> Section<'_, X> {
self.with_text(vec![text])
}

#[inline]
pub fn with_text<X>(self, text: Vec<Text<'_, X>>) -> Section<'_, X> {
Section {
text,
screen_position: self.screen_position,
bounds: self.bounds,
layout: self.layout,
}
}
}

0 comments on commit 9910be9

Please sign in to comment.