use crate::glyph::cache::*;
use crate::glyph::layout::ab_glyph::*;
use crate::glyph::{DefaultSectionHasher, Font, FontId, GlyphBrush};
use std::hash::BuildHasher;
#[non_exhaustive]
pub struct GlyphBrushBuilder<F = FontArc, H = DefaultSectionHasher> {
pub font_data: Vec<F>,
pub cache_glyph_positioning: bool,
pub cache_redraws: bool,
pub section_hasher: H,
pub draw_cache_builder: DrawCacheBuilder,
}
impl GlyphBrushBuilder<()> {
pub fn using_fonts<F: Font>(fonts: Vec<F>) -> GlyphBrushBuilder<F> {
Self::without_fonts().replace_fonts(|_| fonts)
}
#[inline]
pub fn using_font<F: Font>(font: F) -> GlyphBrushBuilder<F> {
Self::using_fonts(vec![font])
}
pub fn without_fonts() -> GlyphBrushBuilder<()> {
GlyphBrushBuilder {
font_data: Vec::new(),
cache_glyph_positioning: true,
cache_redraws: true,
section_hasher: DefaultSectionHasher::default(),
draw_cache_builder: DrawCache::builder()
.dimensions(256, 256)
.scale_tolerance(0.5)
.position_tolerance(0.1)
.align_4x4(false),
}
}
}
impl<F, H> GlyphBrushBuilder<F, H> {
pub fn replace_fonts<F2: Font, V, NF>(self, font_fn: NF) -> GlyphBrushBuilder<F2, H>
where
V: Into<Vec<F2>>,
NF: FnOnce(Vec<F>) -> V,
{
let font_data = font_fn(self.font_data).into();
GlyphBrushBuilder {
font_data,
cache_glyph_positioning: self.cache_glyph_positioning,
cache_redraws: self.cache_redraws,
section_hasher: self.section_hasher,
draw_cache_builder: self.draw_cache_builder,
}
}
}
impl<F: Font, H: BuildHasher> GlyphBrushBuilder<F, H> {
pub fn add_font<I: Into<F>>(&mut self, font_data: I) -> FontId {
self.font_data.push(font_data.into());
FontId(self.font_data.len() - 1)
}
pub fn initial_cache_size(mut self, (w, h): (u32, u32)) -> Self {
self.draw_cache_builder = self.draw_cache_builder.dimensions(w, h);
self
}
pub fn draw_cache_scale_tolerance(mut self, tolerance: f32) -> Self {
self.draw_cache_builder = self.draw_cache_builder.scale_tolerance(tolerance);
self
}
pub fn draw_cache_position_tolerance(mut self, tolerance: f32) -> Self {
self.draw_cache_builder = self.draw_cache_builder.position_tolerance(tolerance);
self
}
pub fn multithread(mut self, multithread: bool) -> Self {
self.draw_cache_builder = self.draw_cache_builder.multithread(multithread);
self
}
pub fn draw_cache_align_4x4(mut self, align: bool) -> Self {
self.draw_cache_builder = self.draw_cache_builder.align_4x4(align);
self
}
pub fn cache_glyph_positioning(mut self, cache: bool) -> Self {
self.cache_glyph_positioning = cache;
self
}
pub fn cache_redraws(mut self, cache_redraws: bool) -> Self {
self.cache_redraws = cache_redraws;
self
}
pub fn section_hasher<T: BuildHasher>(
self,
section_hasher: T,
) -> GlyphBrushBuilder<F, T> {
GlyphBrushBuilder {
section_hasher,
font_data: self.font_data,
cache_glyph_positioning: self.cache_glyph_positioning,
cache_redraws: self.cache_redraws,
draw_cache_builder: self.draw_cache_builder,
}
}
pub fn build<V, X>(self) -> GlyphBrush<V, X, F, H> {
GlyphBrush {
fonts: self.font_data,
texture_cache: self.draw_cache_builder.build(),
last_draw: <_>::default(),
section_buffer: <_>::default(),
calculate_glyph_cache: <_>::default(),
last_frame_seq_id_sections: <_>::default(),
frame_seq_id_sections: <_>::default(),
keep_in_cache: <_>::default(),
cache_glyph_positioning: self.cache_glyph_positioning,
cache_redraws: self.cache_redraws && self.cache_glyph_positioning,
section_hasher: self.section_hasher,
last_pre_positioned: <_>::default(),
pre_positioned: <_>::default(),
}
}
pub fn rebuild<V, X>(self, brush: &mut GlyphBrush<V, X, F, H>) {
*brush = self.build();
}
}