Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More flexible X/"extra" type handling #163

Merged
merged 4 commits into from
Feb 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions glyph-brush/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
# 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`.
* Add `Section::from(text)` & `Section::from(vec![text])` conversions.
* Update `GlyphCruncher::glyphs`, `GlyphCruncher::glyph_bounds` docs.

# v0.7.6
Expand Down
6 changes: 4 additions & 2 deletions glyph-brush/src/glyph_brush.rs
Original file line number Diff line number Diff line change
Expand Up @@ -371,8 +371,9 @@ where
/// Should not generally be necessary, see [caching behaviour](#caching-behaviour).
pub fn keep_cached_custom_layout<'a, S, G>(&mut self, section: S, custom_layout: &G)
where
S: Into<Cow<'a, Section<'a>>>,
S: Into<Cow<'a, Section<'a, X>>>,
G: GlyphPositioner,
X: 'a,
{
if !self.cache_glyph_positioning {
return;
Expand All @@ -393,7 +394,8 @@ where
/// Should not generally be necessary, see [caching behaviour](#caching-behaviour).
pub fn keep_cached<'a, S>(&mut self, section: S)
where
S: Into<Cow<'a, Section<'a>>>,
S: Into<Cow<'a, Section<'a, X>>>,
X: 'a,
{
let section = section.into();
let layout = section.layout;
Expand Down
2 changes: 1 addition & 1 deletion glyph-brush/src/glyph_calculator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ impl<F: Font, H: BuildHasher> GlyphCalculatorBuilder<F, H> {
}

#[derive(Debug, Clone, PartialEq)]
pub(crate) struct GlyphedSection<X = Extra> {
pub(crate) struct GlyphedSection<X> {
pub bounds: Rect,
pub glyphs: Vec<SectionGlyph>,
pub extra: Vec<X>,
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::default()
crate::Section::builder()
.with_layout(s.layout)
.with_bounds(s.bounds)
.with_screen_position(s.screen_position)
Expand Down
37 changes: 30 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 @@ -41,6 +44,7 @@ impl<X: Clone> Section<'_, X> {
}

impl Default for Section<'static, Extra> {
/// Note this only works for `X=Extra` for more flexible use see [`Section::builder`].
#[inline]
fn default() -> Self {
Section::new()
Expand All @@ -50,12 +54,29 @@ impl Default for Section<'static, Extra> {
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<'_, ()> {
/// Return a `SectionBuilder` to fluently build up a `Section`.
#[inline]
pub fn builder() -> SectionBuilder {
<_>::default()
}
}

impl<'a, X> From<Text<'a, X>> for Section<'a, X> {
#[inline]
fn from(text: Text<'a, X>) -> Self {
Section::builder().add_text(text)
}
}

impl<'a, X> From<Vec<Text<'a, X>>> for Section<'a, X> {
#[inline]
fn from(text: Vec<Text<'a, X>>) -> Self {
Section::builder().with_text(text)
}
}

Expand Down Expand Up @@ -196,12 +217,14 @@ impl<'a, X> Text<'a, X> {
}
}

impl<'a> Text<'a, Extra> {
impl<'a, X: Default> Text<'a, X> {
#[inline]
pub fn new(text: &'a str) -> Self {
Text::default().with_text(text)
}
}

impl<'a> Text<'a, Extra> {
#[inline]
pub fn with_color<C: Into<Color>>(mut self, color: C) -> Self {
self.extra.color = color.into();
Expand Down
61 changes: 61 additions & 0 deletions glyph-brush/src/section/builder.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
use crate::{Section, Text};
use glyph_brush_layout::{BuiltInLineBreaker, Layout};

/// [`Section`] builder.
///
/// Usage can avoid generic `X` type issues as it's not mentioned until text is involved.
#[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,
}
}
}