[][src]Struct glyph_brush::GlyphBrush

pub struct GlyphBrush<'font, V, H = DefaultSectionHasher> { /* fields omitted */ }

Object allowing glyph drawing, containing cache state. Manages glyph positioning cacheing, glyph draw caching & efficient GPU texture cache updating.

Build using a GlyphBrushBuilder.

Also see GlyphCruncher trait which providers extra functionality, such as glyph_bounds.

Caching behaviour

Calls to GlyphBrush::queue, GlyphBrush::pixel_bounds, GlyphBrush::glyphs calculate the positioned glyphs for a section. This is cached so future calls to any of the methods for the same section are much cheaper. In the case of GlyphBrush::queue the calculations will also be used for actual drawing.

The cache for a section will be cleared after a GlyphBrush::process_queued call when that section has not been used since the previous call.

Texture caching behaviour

Note the gpu/draw cache may contain multiple versions of the same glyph at different subpixel positions. This is required for high quality text as a glyph's positioning is always exactly aligned to it's draw positioning.

This behaviour can be adjusted with GlyphBrushBuilder::gpu_cache_position_tolerance (struct.GlyphBrushBuilder.html#method.gpu_cache_position_tolerance).

Methods

impl<'font, V, H> GlyphBrush<'font, V, H> where
    V: Clone + 'static,
    H: BuildHasher
[src]

pub fn queue_custom_layout<'a, S, G>(&mut self, section: S, custom_layout: &G) where
    G: GlyphPositioner,
    S: Into<Cow<'a, VariedSection<'a>>>, 
[src]

Queues a section/layout to be processed by the next call of process_queued. Can be called multiple times to queue multiple sections for drawing.

Used to provide custom GlyphPositioner logic, if using built-in Layout simply use queue

Benefits from caching, see caching behaviour.

pub fn queue<'a, S>(&mut self, section: S) where
    S: Into<Cow<'a, VariedSection<'a>>>, 
[src]

Queues a section/layout to be processed by the next call of process_queued. Can be called multiple times to queue multiple sections for drawing.

Benefits from caching, see caching behaviour.

glyph_brush.queue(Section {
    text: "Hello glyph_brush",
    ..Section::default()
});

pub fn queue_pre_positioned(
    &mut self,
    glyphs: Vec<(PositionedGlyph<'font>, Color, FontId)>,
    bounds: Rect<f32>,
    z: f32
)
[src]

Queues pre-positioned glyphs to be processed by the next call of process_queued. Can be called multiple times.

pub fn process_queued<F1, F2>(
    &mut self,
    update_texture: F1,
    to_vertex: F2
) -> Result<BrushAction<V>, BrushError> where
    F1: FnMut(Rect<u32>, &[u8]),
    F2: Fn(GlyphVertex) -> V + Copy
[src]

Processes all queued sections, calling texture update logic when necessary & returning a BrushAction. See queue.

Two closures are required:

  • update_texture is called when new glyph texture data has been drawn for update in the actual texture. The arguments are the rect position of the data in the texture & the byte data itself which is a single u8 alpha value per pixel.
  • to_vertex maps a single glyph's GlyphVertex data into a generic vertex type. The mapped vertices are returned in an Ok(BrushAction::Draw(vertices)) result. It's recommended to use a single vertex per glyph quad for best performance.

Trims the cache, see caching behaviour.

glyph_brush.process_queued(
    |rect, tex_data| update_texture(rect, tex_data),
    |vertex_data| into_vertex(vertex_data),
)?

pub fn resize_texture(&mut self, new_width: u32, new_height: u32)[src]

Rebuilds the logical texture cache with new dimensions. Should be avoided if possible.

Example

glyph_brush.resize_texture(512, 512);

pub fn texture_dimensions(&self) -> (u32, u32)[src]

Returns the logical texture cache pixel dimensions (width, height).

pub fn add_font_bytes<'a: 'font, B: Into<SharedBytes<'a>>>(
    &mut self,
    font_data: B
) -> FontId
[src]

Adds an additional font to the one(s) initially added on build.

Returns a new FontId to reference this font.

Example

use glyph_brush::{GlyphBrush, GlyphBrushBuilder, Section};

// dejavu is built as default `FontId(0)`
let dejavu: &[u8] = include_bytes!("../../fonts/DejaVuSans.ttf");
let mut glyph_brush: GlyphBrush<'_, Vertex> =
    GlyphBrushBuilder::using_font_bytes(dejavu).build();

// some time later, add another font referenced by a new `FontId`
let open_sans_italic: &[u8] = include_bytes!("../../fonts/OpenSans-Italic.ttf");
let open_sans_italic_id = glyph_brush.add_font_bytes(open_sans_italic);

pub fn add_font<'a: 'font>(&mut self, font_data: Font<'a>) -> FontId[src]

Adds an additional font to the one(s) initially added on build.

Returns a new FontId to reference this font.

pub fn keep_cached_custom_layout<'a, S, G>(
    &mut self,
    section: S,
    custom_layout: &G
) where
    S: Into<Cow<'a, VariedSection<'a>>>,
    G: GlyphPositioner
[src]

Retains the section in the cache as if it had been used in the last draw-frame.

Should not generally be necessary, see caching behaviour.

pub fn keep_cached<'a, S>(&mut self, section: S) where
    S: Into<Cow<'a, VariedSection<'a>>>, 
[src]

Retains the section in the cache as if it had been used in the last draw-frame.

Should not generally be necessary, see caching behaviour.

impl<'font, V, H: BuildHasher + Clone> GlyphBrush<'font, V, H>[src]

pub fn to_builder(&self) -> GlyphBrushBuilder<'font, H>[src]

Return a GlyphBrushBuilder prefilled with the properties of this GlyphBrush.

Example

let glyph_brush: GlyphBrush<'_, Vertex> = GlyphBrushBuilder::using_font(sans)
    .initial_cache_size((128, 128))
    .build();

let new_brush: GlyphBrush<'_, Vertex> = glyph_brush.to_builder().build();
assert_eq!(new_brush.texture_dimensions(), (128, 128));

Trait Implementations

impl<'_, V, H> Debug for GlyphBrush<'_, V, H>[src]

impl<'font, V, H> GlyphCruncher<'font> for GlyphBrush<'font, V, H> where
    V: Clone + 'static,
    H: BuildHasher
[src]

Auto Trait Implementations

impl<'font, V, H> RefUnwindSafe for GlyphBrush<'font, V, H> where
    H: RefUnwindSafe,
    V: RefUnwindSafe

impl<'font, V, H> Send for GlyphBrush<'font, V, H> where
    H: Send,
    V: Send

impl<'font, V, H> Sync for GlyphBrush<'font, V, H> where
    H: Sync,
    V: Sync

impl<'font, V, H> Unpin for GlyphBrush<'font, V, H> where
    H: Unpin,
    V: Unpin

impl<'font, V, H> UnwindSafe for GlyphBrush<'font, V, H> where
    H: UnwindSafe,
    V: UnwindSafe

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

impl<V, T> VZip<V> for T where
    V: MultiLane<T>,