glyph_brush/lib.rs
1//! ```
2//! use glyph_brush::{
3//! ab_glyph::FontArc, BrushAction, BrushError, GlyphBrushBuilder, Section, Text,
4//! };
5//!
6//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
7//! let dejavu = FontArc::try_from_slice(include_bytes!("../../fonts/DejaVuSans.ttf"))?;
8//! let mut glyph_brush = GlyphBrushBuilder::using_font(dejavu).build();
9//! # let some_other_section = Section::default();
10//!
11//! glyph_brush.queue(Section::default().add_text(Text::new("Hello glyph_brush")));
12//! glyph_brush.queue(some_other_section);
13//!
14//! # fn update_texture(_: glyph_brush::Rectangle<u32>, _: &[u8]) {}
15//! # fn into_vertex(v: glyph_brush::GlyphVertex) { () }
16//! match glyph_brush.process_queued(
17//! |rect, tex_data| update_texture(rect, tex_data),
18//! |vertex_data| into_vertex(vertex_data),
19//! ) {
20//! Ok(BrushAction::Draw(vertices)) => {
21//! // Draw new vertices.
22//! }
23//! Ok(BrushAction::ReDraw) => {
24//! // Re-draw last frame's vertices unmodified.
25//! }
26//! Err(BrushError::TextureTooSmall { suggested }) => {
27//! // Enlarge texture + glyph_brush texture cache and retry.
28//! }
29//! }
30//! # Ok(())
31//! # }
32//! ```
33mod extra;
34mod glyph_brush;
35mod glyph_calculator;
36mod section;
37
38pub mod legacy;
39
40pub use crate::{extra::*, glyph_brush::*, glyph_calculator::*, section::*};
41pub use glyph_brush_draw_cache::Rectangle;
42pub use glyph_brush_layout::*;
43
44use glyph_brush_layout::ab_glyph::*;
45
46/// A "practically collision free" `Section` hasher
47#[cfg(not(target_arch = "wasm32"))]
48pub type DefaultSectionHasher = twox_hash::xxhash64::RandomState;
49// Work around for rand issues in wasm #61
50#[cfg(target_arch = "wasm32")]
51pub type DefaultSectionHasher = std::hash::BuildHasherDefault<twox_hash::XxHash64>;
52
53#[test]
54fn default_section_hasher() {
55 use std::hash::BuildHasher;
56
57 let section_a = Section::default().add_text(Text::new("Hovered Tile: Some((0, 0))"));
58 let section_b = Section::default().add_text(Text::new("Hovered Tile: Some((1, 0))"));
59 let hash = |s: &Section| DefaultSectionHasher::default().hash_one(s);
60 assert_ne!(hash(§ion_a), hash(§ion_b));
61}