//! Textium is a small library that wires Rusttype up to OpenGL.
|
|
//!
|
|
//! Textium provides a glyph atlas, atlas packers, a glyph rasterizer, and the necessary
|
|
//! shaders and infrastructure to easily connect to (at the moment) Glium.
|
|
//!
|
|
//! A future goal is making rendering backends generic.
|
|
|
|
extern crate rusttype;
|
|
#[macro_use]
|
|
extern crate glium;
|
|
|
|
mod error;
|
|
pub mod texture;
|
|
pub mod geometry;
|
|
pub mod rasterizer;
|
|
pub mod packer;
|
|
pub mod cache;
|
|
|
|
use std::collections::HashMap;
|
|
use std::hash::{Hash, Hasher};
|
|
use glium::texture::Texture2d;
|
|
|
|
pub use error::Error;
|
|
pub use geometry::Rect;
|
|
pub use rasterizer::Font;
|
|
|
|
/// Information used to render a glyph in a font.
|
|
#[derive(Debug, Clone)]
|
|
pub struct GlyphMetadata {
|
|
/// Bounding box in the packed texture
|
|
bounding_box: Rect<usize>,
|
|
/// Scale that the character was rendered at
|
|
scale: f32,
|
|
|
|
/// Distance between the bottom of the glyph and the baseline, in ems.
|
|
// baseline_distance: f32,
|
|
|
|
/// Distance to advance in layout after this character
|
|
advance_width: f32,
|
|
|
|
/// Horizontal offset between glyph origin and leftmost point of the glyph
|
|
left_bearing: f32,
|
|
}
|
|
|
|
/// Maps glyphs to their size, location, and rendering properties in a glyph atlas.
|
|
#[derive(Debug)]
|
|
pub struct FontAtlas {
|
|
pub glyph_metadata: HashMap<char, GlyphMetadata>
|
|
}
|
|
|
|
impl FontAtlas {
|
|
pub fn info(&self, chr: char) -> Option<GlyphMetadata> {
|
|
self.glyph_metadata.get(&chr).cloned()
|
|
}
|
|
}
|
|
|
|
/// A GPU-backed font texture, plus a [`FontAtlas`].
|
|
///
|
|
/// [`FontAtlas`]: struct.FontAtlas.html
|
|
pub struct FontTexture {
|
|
/// The GPU texture of packed glyphs.
|
|
pub data: Texture2d,
|
|
|
|
/// Atlas describing where glyphs are.
|
|
pub atlas: FontAtlas,
|
|
}
|
|
|
|
#[derive(Debug, Clone, Copy)]
|
|
struct Scale(f32);
|
|
impl Scale {
|
|
fn canonicalize(&self) -> i64 {
|
|
(self.0 * 1024.0 * 1024.0).round() as i64
|
|
}
|
|
}
|
|
impl PartialEq for Scale {
|
|
fn eq(&self, other: &Self) -> bool {
|
|
self.canonicalize() == other.canonicalize()
|
|
}
|
|
}
|
|
impl Eq for Scale {}
|
|
impl Hash for Scale {
|
|
fn hash<H>(&self, state: &mut H) where H: Hasher {
|
|
self.canonicalize().hash(state);
|
|
}
|
|
}
|